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 snprintf (buf, sizeof buf, "/dev/%s", d->d_name);
50 if (add_string (&r, &size, &alloc, buf) == -1) {
57 if (add_string (&r, &size, &alloc, NULL) == -1) {
62 if (closedir (dir) == -1) {
63 reply_with_perror ("closedir: /sys/block");
68 sort_strings (r, size-1);
73 do_list_partitions (void)
76 int size = 0, alloc = 0;
79 char buf[256], devname[256];
81 dir = opendir ("/sys/block");
83 reply_with_perror ("opendir: /sys/block");
87 while ((d = readdir (dir)) != NULL) {
88 if (strncmp (d->d_name, "sd", 2) == 0) {
89 strncpy (devname, d->d_name, sizeof devname);
90 devname[sizeof devname - 1] = '\0';
92 snprintf (buf, sizeof buf, "/sys/block/%s", devname);
96 reply_with_perror ("opendir: %s", buf);
97 free_stringslen (r, size);
100 while ((d = readdir (dir2)) != NULL) {
101 if (strncmp (d->d_name, devname, strlen (devname)) == 0) {
102 snprintf (buf, sizeof buf, "/dev/%s", d->d_name);
104 if (add_string (&r, &size, &alloc, buf) == -1) {
112 if (closedir (dir2) == -1) {
113 reply_with_perror ("closedir: /sys/block/%s", devname);
114 free_stringslen (r, size);
120 if (add_string (&r, &size, &alloc, NULL) == -1) {
125 if (closedir (dir) == -1) {
126 reply_with_perror ("closedir: /sys/block");
131 sort_strings (r, size-1);
136 do_mkfs (const char *fstype, const char *device)
141 IS_DEVICE (device, -1);
143 r = command (NULL, &err, "/sbin/mkfs", "-t", fstype, device, NULL);
145 reply_with_error ("mkfs: %s", err);
155 do_sfdisk (const char *device, int cyls, int heads, int sectors,
156 char * const* const lines)
162 IS_DEVICE (device, -1);
164 /* Safe because of IS_DEVICE above. */
165 strcpy (buf, "/sbin/sfdisk");
167 sprintf (buf + strlen (buf), " -C %d", cyls);
169 sprintf (buf + strlen (buf), " -H %d", heads);
171 sprintf (buf + strlen (buf), " -S %d", sectors);
172 sprintf (buf + strlen (buf), " %s", device);
174 fp = popen (buf, "w");
176 reply_with_perror (buf);
180 for (i = 0; lines[i] != NULL; ++i) {
181 if (fprintf (fp, "%s\n", lines[i]) < 0) {
182 reply_with_perror (buf);
188 if (fclose (fp) == EOF) {
189 reply_with_perror (buf);