1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009 Red Hat Inc.
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.
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.
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.
34 typedef int (*block_dev_func_t)(const char *dev,
35 char ***r, int *size, int *alloc);
37 /* Execute a given function for each discovered block device */
39 foreach_block_device (block_dev_func_t func)
42 int size = 0, alloc = 0;
47 dir = opendir ("/sys/block");
49 reply_with_perror ("opendir: /sys/block");
55 struct dirent *d = readdir(dir);
58 if (STREQLEN (d->d_name, "sd", 2) ||
59 STREQLEN (d->d_name, "hd", 2) ||
60 STREQLEN (d->d_name, "vd", 2) ||
61 STREQLEN (d->d_name, "sr", 2)) {
63 snprintf (dev_path, sizeof dev_path, "/dev/%s", d->d_name);
65 /* Ignore the root device. */
66 if (is_root_device (dev_path))
69 /* RHBZ#514505: Some versions of qemu <= 0.10 add a
70 * CD-ROM device even though we didn't request it. Try to
71 * detect this by seeing if the device contains media.
73 int fd = open (dev_path, O_RDONLY);
80 /* Call the map function for this device */
81 if((*func)(d->d_name, &r, &size, &alloc) != 0) {
88 /* Check readdir didn't fail */
90 reply_with_perror ("readdir: /sys/block");
91 free_stringslen(r, size);
96 /* Close the directory handle */
97 if (closedir (dir) == -1) {
98 reply_with_perror ("closedir: /sys/block");
99 free_stringslen(r, size);
103 /* Free the result list on error */
105 free_stringslen(r, size);
109 /* Sort the devices. Note that r might be NULL if there are no devices. */
111 sort_strings (r, size);
113 /* NULL terminate the list */
114 if (add_string (&r, &size, &alloc, NULL) == -1) {
121 /* Add a device to the list of devices */
123 add_device(const char *device,
124 char ***const r, int *const size, int *const alloc)
127 snprintf (dev_path, sizeof dev_path, "/dev/%s", device);
129 if (add_string (r, size, alloc, dev_path) == -1) {
137 do_list_devices (void)
139 return foreach_block_device(add_device);
143 add_partitions(const char *device,
144 char ***const r, int *const size, int *const alloc)
148 /* Open the device's directory under /sys/block */
149 snprintf (devdir, sizeof devdir, "/sys/block/%s", device);
151 DIR *dir = opendir (devdir);
153 reply_with_perror ("opendir: %s", devdir);
154 free_stringslen (*r, *size);
158 /* Look in /sys/block/<device>/ for entries starting with <device>
159 * e.g. /sys/block/sda/sda1
163 while ((d = readdir (dir)) != NULL) {
164 if (STREQLEN (d->d_name, device, strlen (device))) {
166 snprintf (part, sizeof part, "/dev/%s", d->d_name);
168 if (add_string (r, size, alloc, part) == -1) {
175 /* Check if readdir failed */
177 reply_with_perror ("readdir: %s", devdir);
178 free_stringslen(*r, *size);
183 /* Close the directory handle */
184 if (closedir (dir) == -1) {
185 reply_with_perror ("closedir: /sys/block/%s", device);
186 free_stringslen (*r, *size);
194 do_list_partitions (void)
196 return foreach_block_device(add_partitions);
200 do_part_to_dev (const char *part)
203 size_t n = strlen (part);
205 while (n >= 1 && c_isdigit (part[n-1])) {
211 reply_with_error ("device name is not a partition");
215 char *r = strndup (part, n);
217 reply_with_perror ("strdup");