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 sfdisk (const char *device, int n, int cyls, int heads, int sectors,
34 const char *extra_flag,
41 strcpy (buf, "/sbin/sfdisk");
44 sprintf (buf + strlen (buf), " -N %d", n);
46 sprintf (buf + strlen (buf), " -C %d", cyls);
48 sprintf (buf + strlen (buf), " -H %d", heads);
50 sprintf (buf + strlen (buf), " -S %d", sectors);
52 /* The above are all guaranteed to fit in the fixed-size buffer.
53 However, extra_flag and device have no restrictions,
57 if (strlen (buf) + 1 + strlen (extra_flag) >= sizeof buf) {
58 reply_with_error ("internal buffer overflow: sfdisk extra_flag too long");
61 sprintf (buf + strlen (buf), " %s", extra_flag);
64 if (strlen (buf) + 1 + strlen (device) >= sizeof buf) {
65 reply_with_error ("internal buffer overflow: sfdisk device name too long");
68 sprintf (buf + strlen (buf), " %s", device);
73 fp = popen (buf, "w");
75 reply_with_perror ("failed to open pipe: %s", buf);
79 for (i = 0; lines[i] != NULL; ++i) {
80 if (fprintf (fp, "%s\n", lines[i]) < 0) {
81 reply_with_perror ("failed to write to pipe: %s", buf);
87 if (pclose (fp) != 0) {
88 reply_with_error ("%s: external command failed", buf);
98 do_sfdisk (const char *device, int cyls, int heads, int sectors,
101 return sfdisk (device, 0, cyls, heads, sectors, NULL, lines);
105 do_sfdisk_N (const char *device, int n, int cyls, int heads, int sectors,
108 char const *const lines[2] = { line, NULL };
110 return sfdisk (device, n, cyls, heads, sectors, NULL, (void *) lines);
114 do_sfdiskM (const char *device, char *const *lines)
116 return sfdisk (device, 0, 0, 0, 0, "-uM", lines);
120 sfdisk_flag (const char *device, const char *flag)
125 r = command (&out, &err, "/sbin/sfdisk", flag, device, NULL);
127 reply_with_error ("sfdisk: %s: %s", device, err);
137 return out; /* caller frees */
141 do_sfdisk_l (const char *device)
143 return sfdisk_flag (device, "-l");
147 do_sfdisk_kernel_geometry (const char *device)
149 return sfdisk_flag (device, "-g");
153 do_sfdisk_disk_geometry (const char *device)
155 return sfdisk_flag (device, "-G");