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 (char *device, int n, int cyls, int heads, int sectors,
34 const char *extra_flag,
35 char * const* const lines)
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 sprintf (buf + strlen (buf), " %s", extra_flag);
54 /* Safe because of RESOLVE_DEVICE above: */
55 sprintf (buf + strlen (buf), " %s", device);
60 fp = popen (buf, "w");
62 reply_with_perror (buf);
66 for (i = 0; lines[i] != NULL; ++i) {
67 if (fprintf (fp, "%s\n", lines[i]) < 0) {
68 reply_with_perror (buf);
74 if (pclose (fp) != 0) {
75 reply_with_error ("%s: external command failed", buf);
85 do_sfdisk (char *device, int cyls, int heads, int sectors,
88 return sfdisk (device, 0, cyls, heads, sectors, NULL, lines);
92 do_sfdisk_N (char *device, int n, int cyls, int heads, int sectors,
95 const char *lines[2] = { line, NULL };
97 return sfdisk (device, n, cyls, heads, sectors, NULL, lines);
101 do_sfdiskM (char *device, char **lines)
103 return sfdisk (device, 0, 0, 0, 0, "-uM", lines);
107 sfdisk_flag (char *device, const char *flag)
112 r = command (&out, &err, "/sbin/sfdisk", flag, device, NULL);
114 reply_with_error ("sfdisk: %s: %s", device, err);
124 return out; /* caller frees */
128 do_sfdisk_l (char *device)
130 return sfdisk_flag (device, "-l");
134 do_sfdisk_kernel_geometry (char *device)
136 return sfdisk_flag (device, "-g");
140 do_sfdisk_disk_geometry (char *device)
142 return sfdisk_flag (device, "-G");