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;
41 dir = opendir ("/sys/block");
43 reply_with_perror ("opendir: /sys/block");
47 while ((d = readdir (dir)) != NULL) {
48 if (strncmp (d->d_name, "sd", 2) == 0 ||
49 strncmp (d->d_name, "hd", 2) == 0 ||
50 strncmp (d->d_name, "vd", 2) == 0) {
51 snprintf (buf, sizeof buf, "/dev/%s", d->d_name);
53 /* RHBZ#514505: Some versions of qemu <= 0.10 add a
54 * CD-ROM device even though we didn't request it. Try to
55 * detect this by seeing if the device contains media.
57 int fd = open (buf, O_RDONLY);
64 if (add_string (&r, &size, &alloc, buf) == -1) {
71 if (add_string (&r, &size, &alloc, NULL) == -1) {
76 if (closedir (dir) == -1) {
77 reply_with_perror ("closedir: /sys/block");
82 sort_strings (r, size-1);
87 do_list_partitions (void)
90 int size = 0, alloc = 0;
93 char buf[256], devname[256];
95 dir = opendir ("/sys/block");
97 reply_with_perror ("opendir: /sys/block");
101 while ((d = readdir (dir)) != NULL) {
102 if (strncmp (d->d_name, "sd", 2) == 0 ||
103 strncmp (d->d_name, "hd", 2) == 0 ||
104 strncmp (d->d_name, "vd", 2) == 0) {
105 snprintf (buf, sizeof buf, "/dev/%s", d->d_name);
107 /* RHBZ#514505: Some versions of qemu <= 0.10 add a
108 * CD-ROM device even though we didn't request it. Try to
109 * detect this by seeing if the device contains media.
111 int fd = open (buf, O_RDONLY);
118 strncpy (devname, d->d_name, sizeof devname);
119 devname[sizeof devname - 1] = '\0';
121 snprintf (buf, sizeof buf, "/sys/block/%s", devname);
123 dir2 = opendir (buf);
125 reply_with_perror ("opendir: %s", buf);
126 free_stringslen (r, size);
129 while ((d = readdir (dir2)) != NULL) {
130 if (strncmp (d->d_name, devname, strlen (devname)) == 0) {
131 snprintf (buf, sizeof buf, "/dev/%s", d->d_name);
133 if (add_string (&r, &size, &alloc, buf) == -1) {
141 if (closedir (dir2) == -1) {
142 reply_with_perror ("closedir: /sys/block/%s", devname);
143 free_stringslen (r, size);
149 if (add_string (&r, &size, &alloc, NULL) == -1) {
154 if (closedir (dir) == -1) {
155 reply_with_perror ("closedir: /sys/block");
160 sort_strings (r, size-1);
165 do_mkfs (char *fstype, char *device)
170 IS_DEVICE (device, -1);
172 r = command (NULL, &err, "/sbin/mkfs", "-t", fstype, device, NULL);
174 reply_with_error ("mkfs: %s", err);