Recognise cd-rom devices in devsparts.c
[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 typedef int (*block_dev_func_t)(const char *dev,
33                                 char ***r, int *size, int *alloc);
34
35 /* Execute a given function for each discovered block device */
36 static char**
37 foreach_block_device (block_dev_func_t func)
38 {
39   char **r = NULL;
40   int size = 0, alloc = 0;
41
42   DIR *dir;
43   struct dirent *d;
44   int err = 0;
45
46   dir = opendir ("/sys/block");
47   if (!dir) {
48     reply_with_perror ("opendir: /sys/block");
49     return NULL;
50   }
51
52   errno = 0;
53   while ((d = readdir (dir)) != NULL) {
54     if (strncmp (d->d_name, "sd", 2) == 0 ||
55         strncmp (d->d_name, "hd", 2) == 0 ||
56         strncmp (d->d_name, "vd", 2) == 0 ||
57         strncmp (d->d_name, "sr", 2) == 0) {
58       char dev_path[256];
59       snprintf (dev_path, sizeof dev_path, "/dev/%s", d->d_name);
60
61       /* RHBZ#514505: Some versions of qemu <= 0.10 add a
62        * CD-ROM device even though we didn't request it.  Try to
63        * detect this by seeing if the device contains media.
64        */
65       int fd = open (dev_path, O_RDONLY);
66       if (fd == -1) {
67         perror (dev_path);
68         continue;
69       }
70       close (fd);
71
72       /* Call the map function for this device */
73       if((*func)(d->d_name, &r, &size, &alloc) != 0) {
74         err = 1;
75         break;
76       }
77     }
78   }
79
80   /* Check readdir didn't fail */
81   if(0 != errno) {
82     reply_with_perror ("readdir: /sys/block");
83     free_stringslen(r, size);
84     return NULL;
85   }
86
87   /* Close the directory handle */
88   if (closedir (dir) == -1) {
89     reply_with_perror ("closedir: /sys/block");
90     free_stringslen(r, size);
91     return NULL;
92   }
93
94   /* Free the result list on error */
95   if(err) {
96     free_stringslen(r, size);
97     return NULL;
98   }
99
100   /* Sort the devices */
101   sort_strings (r, size);
102
103   /* NULL terminate the list */
104   if (add_string (&r, &size, &alloc, NULL) == -1) {
105     return NULL;
106   }
107
108   return r;
109 }
110
111 /* Add a device to the list of devices */
112 static int
113 add_device(const char *device,
114            char ***const r, int *const size, int *const alloc)
115 {
116   char dev_path[256];
117   snprintf (dev_path, sizeof dev_path, "/dev/%s", device);
118
119   if (add_string (r, size, alloc, dev_path) == -1) {
120     return -1;
121   }
122
123   return 0;
124 }
125
126 char **
127 do_list_devices (void)
128 {
129   return foreach_block_device(add_device);
130 }
131
132 static int
133 add_partitions(const char *device,
134                char ***const r, int *const size, int *const alloc)
135 {
136   char devdir[256];
137
138   /* Open the device's directory under /sys/block */
139   snprintf (devdir, sizeof devdir, "/sys/block/%s", device);
140
141   DIR *dir = opendir (devdir);
142   if (!dir) {
143     reply_with_perror ("opendir: %s", devdir);
144     free_stringslen (*r, *size);
145     return -1;
146   }
147
148   /* Look in /sys/block/<device>/ for entries starting with <device>
149    * e.g. /sys/block/sda/sda1
150    */
151   errno = 0;
152   struct dirent *d;
153   while ((d = readdir (dir)) != NULL) {
154     if (strncmp (d->d_name, device, strlen (device)) == 0) {
155       char part[256];
156       snprintf (part, sizeof part, "/dev/%s", d->d_name);
157
158       if (add_string (r, size, alloc, part) == -1) {
159         closedir (dir);
160         return -1;
161       }
162     }
163   }
164
165   /* Check if readdir failed */
166   if(0 != errno) {
167       reply_with_perror ("readdir: %s", devdir);
168       free_stringslen(*r, *size);
169       return -1;
170   }
171
172   /* Close the directory handle */
173   if (closedir (dir) == -1) {
174     reply_with_perror ("closedir: /sys/block/%s", device);
175     free_stringslen (*r, *size);
176     return -1;
177   }
178
179   return 0;
180 }
181
182 char **
183 do_list_partitions (void)
184 {
185   return foreach_block_device(add_partitions);
186 }
187
188 int
189 do_mkfs (char *fstype, char *device)
190 {
191   char *err;
192   int r;
193
194   IS_DEVICE (device, -1);
195
196   r = command (NULL, &err, "/sbin/mkfs", "-t", fstype, device, NULL);
197   if (r == -1) {
198     reply_with_error ("mkfs: %s", err);
199     free (err);
200     return -1;
201   }
202
203   free (err);
204   return 0;
205 }