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.
32 typedef int (*block_dev_func_t)(const char *dev,
33 char ***r, int *size, int *alloc);
35 /* Execute a given function for each discovered block device */
37 foreach_block_device (block_dev_func_t func)
40 int size = 0, alloc = 0;
45 dir = opendir ("/sys/block");
47 reply_with_perror ("opendir: /sys/block");
53 struct dirent *d = readdir(dir);
56 if (STREQLEN (d->d_name, "sd", 2) ||
57 STREQLEN (d->d_name, "hd", 2) ||
58 STREQLEN (d->d_name, "vd", 2) ||
59 STREQLEN (d->d_name, "sr", 2)) {
61 snprintf (dev_path, sizeof dev_path, "/dev/%s", d->d_name);
63 /* Ignore the root device. */
64 if (is_root_device (dev_path))
67 /* RHBZ#514505: Some versions of qemu <= 0.10 add a
68 * CD-ROM device even though we didn't request it. Try to
69 * detect this by seeing if the device contains media.
71 int fd = open (dev_path, O_RDONLY);
78 /* Call the map function for this device */
79 if((*func)(d->d_name, &r, &size, &alloc) != 0) {
86 /* Check readdir didn't fail */
88 reply_with_perror ("readdir: /sys/block");
89 free_stringslen(r, size);
93 /* Close the directory handle */
94 if (closedir (dir) == -1) {
95 reply_with_perror ("closedir: /sys/block");
96 free_stringslen(r, size);
100 /* Free the result list on error */
102 free_stringslen(r, size);
106 /* Sort the devices */
107 sort_strings (r, size);
109 /* NULL terminate the list */
110 if (add_string (&r, &size, &alloc, NULL) == -1) {
117 /* Add a device to the list of devices */
119 add_device(const char *device,
120 char ***const r, int *const size, int *const alloc)
123 snprintf (dev_path, sizeof dev_path, "/dev/%s", device);
125 if (add_string (r, size, alloc, dev_path) == -1) {
133 do_list_devices (void)
135 return foreach_block_device(add_device);
139 add_partitions(const char *device,
140 char ***const r, int *const size, int *const alloc)
144 /* Open the device's directory under /sys/block */
145 snprintf (devdir, sizeof devdir, "/sys/block/%s", device);
147 DIR *dir = opendir (devdir);
149 reply_with_perror ("opendir: %s", devdir);
150 free_stringslen (*r, *size);
154 /* Look in /sys/block/<device>/ for entries starting with <device>
155 * e.g. /sys/block/sda/sda1
159 while ((d = readdir (dir)) != NULL) {
160 if (STREQLEN (d->d_name, device, strlen (device))) {
162 snprintf (part, sizeof part, "/dev/%s", d->d_name);
164 if (add_string (r, size, alloc, part) == -1) {
171 /* Check if readdir failed */
173 reply_with_perror ("readdir: %s", devdir);
174 free_stringslen(*r, *size);
178 /* Close the directory handle */
179 if (closedir (dir) == -1) {
180 reply_with_perror ("closedir: /sys/block/%s", device);
181 free_stringslen (*r, *size);
189 do_list_partitions (void)
191 return foreach_block_device(add_partitions);