Use a squashfs attached as /dev/sdd during the C API tests.
[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       snprintf (buf, sizeof buf, "/dev/%s", d->d_name);
51       if (add_string (&r, &size, &alloc, buf) == -1) {
52         closedir (dir);
53         return NULL;
54       }
55     }
56   }
57
58   if (add_string (&r, &size, &alloc, NULL) == -1) {
59     closedir (dir);
60     return NULL;
61   }
62
63   if (closedir (dir) == -1) {
64     reply_with_perror ("closedir: /sys/block");
65     free_strings (r);
66     return NULL;
67   }
68
69   sort_strings (r, size-1);
70   return r;
71 }
72
73 char **
74 do_list_partitions (void)
75 {
76   char **r = NULL;
77   int size = 0, alloc = 0;
78   DIR *dir, *dir2;
79   struct dirent *d;
80   char buf[256], devname[256];
81
82   dir = opendir ("/sys/block");
83   if (!dir) {
84     reply_with_perror ("opendir: /sys/block");
85     return NULL;
86   }
87
88   while ((d = readdir (dir)) != NULL) {
89     if (strncmp (d->d_name, "sd", 2) == 0 ||
90         strncmp (d->d_name, "hd", 2) == 0) {
91       strncpy (devname, d->d_name, sizeof devname);
92       devname[sizeof devname - 1] = '\0';
93
94       snprintf (buf, sizeof buf, "/sys/block/%s", devname);
95
96       dir2 = opendir (buf);
97       if (!dir2) {
98         reply_with_perror ("opendir: %s", buf);
99         free_stringslen (r, size);
100         return NULL;
101       }
102       while ((d = readdir (dir2)) != NULL) {
103         if (strncmp (d->d_name, devname, strlen (devname)) == 0) {
104           snprintf (buf, sizeof buf, "/dev/%s", d->d_name);
105
106           if (add_string (&r, &size, &alloc, buf) == -1) {
107             closedir (dir2);
108             closedir (dir);
109             return NULL;
110           }
111         }
112       }
113
114       if (closedir (dir2) == -1) {
115         reply_with_perror ("closedir: /sys/block/%s", devname);
116         free_stringslen (r, size);
117         return NULL;
118       }
119     }
120   }
121
122   if (add_string (&r, &size, &alloc, NULL) == -1) {
123     closedir (dir);
124     return NULL;
125   }
126
127   if (closedir (dir) == -1) {
128     reply_with_perror ("closedir: /sys/block");
129     free_strings (r);
130     return NULL;
131   }
132
133   sort_strings (r, size-1);
134   return r;
135 }
136
137 int
138 do_mkfs (const char *fstype, const char *device)
139 {
140   char *err;
141   int r;
142
143   IS_DEVICE (device, -1);
144
145   r = command (NULL, &err, "/sbin/mkfs", "-t", fstype, device, NULL);
146   if (r == -1) {
147     reply_with_error ("mkfs: %s", err);
148     free (err);
149     return -1;
150   }
151
152   free (err);
153   return 0;
154 }