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.
33 do_list_devices (void)
36 int size = 0, alloc = 0;
42 dir = opendir ("/sys/block");
44 reply_with_perror ("opendir: /sys/block");
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);
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.
58 fd = open (buf, O_RDONLY);
65 if (add_string (&r, &size, &alloc, buf) == -1) {
72 if (add_string (&r, &size, &alloc, NULL) == -1) {
77 if (closedir (dir) == -1) {
78 reply_with_perror ("closedir: /sys/block");
83 sort_strings (r, size-1);
88 do_list_partitions (void)
91 int size = 0, alloc = 0;
94 char buf[256], devname[256];
97 dir = opendir ("/sys/block");
99 reply_with_perror ("opendir: /sys/block");
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);
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.
113 fd = open (buf, O_RDONLY);
120 strncpy (devname, d->d_name, sizeof devname);
121 devname[sizeof devname - 1] = '\0';
123 snprintf (buf, sizeof buf, "/sys/block/%s", devname);
125 dir2 = opendir (buf);
127 reply_with_perror ("opendir: %s", buf);
128 free_stringslen (r, size);
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);
135 if (add_string (&r, &size, &alloc, buf) == -1) {
143 if (closedir (dir2) == -1) {
144 reply_with_perror ("closedir: /sys/block/%s", devname);
145 free_stringslen (r, size);
151 if (add_string (&r, &size, &alloc, NULL) == -1) {
156 if (closedir (dir) == -1) {
157 reply_with_perror ("closedir: /sys/block");
162 sort_strings (r, size-1);
167 do_mkfs (char *fstype, char *device)
172 IS_DEVICE (device, -1);
174 r = command (NULL, &err, "/sbin/mkfs", "-t", fstype, device, NULL);
176 reply_with_error ("mkfs: %s", err);