Make /sysroot path configurable.
[libguestfs.git] / daemon / devsparts.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <dirent.h>
27 #include <sys/stat.h>
28
29 #include "daemon.h"
30 #include "actions.h"
31
32 char **
33 do_list_devices (void)
34 {
35   char **r = NULL;
36   int size = 0, alloc = 0;
37   DIR *dir;
38   struct dirent *d;
39   char buf[256];
40
41   dir = opendir ("/sys/block");
42   if (!dir) {
43     reply_with_perror ("opendir: /sys/block");
44     return NULL;
45   }
46
47   while ((d = readdir (dir)) != NULL) {
48     if (strncmp (d->d_name, "sd", 2) == 0 ||
49         strncmp (d->d_name, "hd", 2) == 0 ||
50         strncmp (d->d_name, "vd", 2) == 0) {
51       snprintf (buf, sizeof buf, "/dev/%s", d->d_name);
52       if (add_string (&r, &size, &alloc, buf) == -1) {
53         closedir (dir);
54         return NULL;
55       }
56     }
57   }
58
59   if (add_string (&r, &size, &alloc, NULL) == -1) {
60     closedir (dir);
61     return NULL;
62   }
63
64   if (closedir (dir) == -1) {
65     reply_with_perror ("closedir: /sys/block");
66     free_strings (r);
67     return NULL;
68   }
69
70   sort_strings (r, size-1);
71   return r;
72 }
73
74 char **
75 do_list_partitions (void)
76 {
77   char **r = NULL;
78   int size = 0, alloc = 0;
79   DIR *dir, *dir2;
80   struct dirent *d;
81   char buf[256], devname[256];
82
83   dir = opendir ("/sys/block");
84   if (!dir) {
85     reply_with_perror ("opendir: /sys/block");
86     return NULL;
87   }
88
89   while ((d = readdir (dir)) != NULL) {
90     if (strncmp (d->d_name, "sd", 2) == 0 ||
91         strncmp (d->d_name, "hd", 2) == 0 ||
92         strncmp (d->d_name, "vd", 2) == 0) {
93       strncpy (devname, d->d_name, sizeof devname);
94       devname[sizeof devname - 1] = '\0';
95
96       snprintf (buf, sizeof buf, "/sys/block/%s", devname);
97
98       dir2 = opendir (buf);
99       if (!dir2) {
100         reply_with_perror ("opendir: %s", buf);
101         free_stringslen (r, size);
102         return NULL;
103       }
104       while ((d = readdir (dir2)) != NULL) {
105         if (strncmp (d->d_name, devname, strlen (devname)) == 0) {
106           snprintf (buf, sizeof buf, "/dev/%s", d->d_name);
107
108           if (add_string (&r, &size, &alloc, buf) == -1) {
109             closedir (dir2);
110             closedir (dir);
111             return NULL;
112           }
113         }
114       }
115
116       if (closedir (dir2) == -1) {
117         reply_with_perror ("closedir: /sys/block/%s", devname);
118         free_stringslen (r, size);
119         return NULL;
120       }
121     }
122   }
123
124   if (add_string (&r, &size, &alloc, NULL) == -1) {
125     closedir (dir);
126     return NULL;
127   }
128
129   if (closedir (dir) == -1) {
130     reply_with_perror ("closedir: /sys/block");
131     free_strings (r);
132     return NULL;
133   }
134
135   sort_strings (r, size-1);
136   return r;
137 }
138
139 int
140 do_mkfs (char *fstype, char *device)
141 {
142   char *err;
143   int r;
144
145   IS_DEVICE (device, -1);
146
147   r = command (NULL, &err, "/sbin/mkfs", "-t", fstype, device, NULL);
148   if (r == -1) {
149     reply_with_error ("mkfs: %s", err);
150     free (err);
151     return -1;
152   }
153
154   free (err);
155   return 0;
156 }