Don't show empty CD devices (RHBZ#514505).
[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   int fd;
41
42   dir = opendir ("/sys/block");
43   if (!dir) {
44     reply_with_perror ("opendir: /sys/block");
45     return NULL;
46   }
47
48   while ((d = readdir (dir)) != NULL) {
49     if (strncmp (d->d_name, "sd", 2) == 0 ||
50         strncmp (d->d_name, "hd", 2) == 0 ||
51         strncmp (d->d_name, "vd", 2) == 0) {
52       snprintf (buf, sizeof buf, "/dev/%s", d->d_name);
53
54       /* RHBZ#514505: Some versions of qemu <= 0.10 device to add a
55        * CD-ROM device even though we didn't request it.  Try to
56        * detect this by seeing if the device contains media.
57        */
58       fd = open (buf, O_RDONLY);
59       if (fd == -1) {
60         perror (buf);
61         continue;
62       }
63       close (fd);
64
65       if (add_string (&r, &size, &alloc, buf) == -1) {
66         closedir (dir);
67         return NULL;
68       }
69     }
70   }
71
72   if (add_string (&r, &size, &alloc, NULL) == -1) {
73     closedir (dir);
74     return NULL;
75   }
76
77   if (closedir (dir) == -1) {
78     reply_with_perror ("closedir: /sys/block");
79     free_strings (r);
80     return NULL;
81   }
82
83   sort_strings (r, size-1);
84   return r;
85 }
86
87 char **
88 do_list_partitions (void)
89 {
90   char **r = NULL;
91   int size = 0, alloc = 0;
92   DIR *dir, *dir2;
93   struct dirent *d;
94   char buf[256], devname[256];
95   int fd;
96
97   dir = opendir ("/sys/block");
98   if (!dir) {
99     reply_with_perror ("opendir: /sys/block");
100     return NULL;
101   }
102
103   while ((d = readdir (dir)) != NULL) {
104     if (strncmp (d->d_name, "sd", 2) == 0 ||
105         strncmp (d->d_name, "hd", 2) == 0 ||
106         strncmp (d->d_name, "vd", 2) == 0) {
107       snprintf (buf, sizeof buf, "/dev/%s", d->d_name);
108
109       /* RHBZ#514505: Some versions of qemu <= 0.10 device to add a
110        * CD-ROM device even though we didn't request it.  Try to
111        * detect this by seeing if the device contains media.
112        */
113       fd = open (buf, O_RDONLY);
114       if (fd == -1) {
115         perror (buf);
116         continue;
117       }
118       close (fd);
119
120       strncpy (devname, d->d_name, sizeof devname);
121       devname[sizeof devname - 1] = '\0';
122
123       snprintf (buf, sizeof buf, "/sys/block/%s", devname);
124
125       dir2 = opendir (buf);
126       if (!dir2) {
127         reply_with_perror ("opendir: %s", buf);
128         free_stringslen (r, size);
129         return NULL;
130       }
131       while ((d = readdir (dir2)) != NULL) {
132         if (strncmp (d->d_name, devname, strlen (devname)) == 0) {
133           snprintf (buf, sizeof buf, "/dev/%s", d->d_name);
134
135           if (add_string (&r, &size, &alloc, buf) == -1) {
136             closedir (dir2);
137             closedir (dir);
138             return NULL;
139           }
140         }
141       }
142
143       if (closedir (dir2) == -1) {
144         reply_with_perror ("closedir: /sys/block/%s", devname);
145         free_stringslen (r, size);
146         return NULL;
147       }
148     }
149   }
150
151   if (add_string (&r, &size, &alloc, NULL) == -1) {
152     closedir (dir);
153     return NULL;
154   }
155
156   if (closedir (dir) == -1) {
157     reply_with_perror ("closedir: /sys/block");
158     free_strings (r);
159     return NULL;
160   }
161
162   sort_strings (r, size-1);
163   return r;
164 }
165
166 int
167 do_mkfs (char *fstype, char *device)
168 {
169   char *err;
170   int r;
171
172   IS_DEVICE (device, -1);
173
174   r = command (NULL, &err, "/sbin/mkfs", "-t", fstype, device, NULL);
175   if (r == -1) {
176     reply_with_error ("mkfs: %s", err);
177     free (err);
178     return -1;
179   }
180
181   free (err);
182   return 0;
183 }