X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=daemon%2Fsfdisk.c;h=f512e26b1b52191d0a2c512defd171ddd8e8b7b9;hp=9d7a220f8a15802350295e0a511d74de57cb241d;hb=58e7e42033b1ac5044ae03f0aa5d5082c5fdb497;hpb=5cd39c83e23eb300d1bdfa806902a31b409ff420 diff --git a/daemon/sfdisk.c b/daemon/sfdisk.c index 9d7a220..f512e26 100644 --- a/daemon/sfdisk.c +++ b/daemon/sfdisk.c @@ -1,5 +1,5 @@ /* libguestfs - the guestfsd daemon - * Copyright (C) 2009 Red Hat Inc. + * Copyright (C) 2009 Red Hat Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -30,7 +30,8 @@ #include "actions.h" static int -sfdisk (const char *device, int n, int cyls, int heads, int sectors, +sfdisk (char *device, int n, int cyls, int heads, int sectors, + const char *extra_flag, char * const* const lines) { FILE *fp; @@ -39,7 +40,8 @@ sfdisk (const char *device, int n, int cyls, int heads, int sectors, IS_DEVICE (device, -1); - strcpy (buf, "/sbin/sfdisk --no-reread"); + strcpy (buf, "/sbin/sfdisk"); + if (n > 0) sprintf (buf + strlen (buf), " -N %d", n); if (cyls) @@ -48,9 +50,15 @@ sfdisk (const char *device, int n, int cyls, int heads, int sectors, sprintf (buf + strlen (buf), " -H %d", heads); if (sectors) sprintf (buf + strlen (buf), " -S %d", sectors); + if (extra_flag) + sprintf (buf + strlen (buf), " %s", extra_flag); + /* Safe because of IS_DEVICE above: */ sprintf (buf + strlen (buf), " %s", device); + if (verbose) + printf ("%s\n", buf); + fp = popen (buf, "w"); if (fp == NULL) { reply_with_perror (buf); @@ -60,38 +68,45 @@ sfdisk (const char *device, int n, int cyls, int heads, int sectors, for (i = 0; lines[i] != NULL; ++i) { if (fprintf (fp, "%s\n", lines[i]) < 0) { reply_with_perror (buf); - fclose (fp); + pclose (fp); return -1; } } - if (fclose (fp) == EOF) { - reply_with_perror (buf); - fclose (fp); + if (pclose (fp) != 0) { + reply_with_error ("%s: external command failed", buf); return -1; } + udev_settle (); + return 0; } int -do_sfdisk (const char *device, int cyls, int heads, int sectors, - char * const* const lines) +do_sfdisk (char *device, int cyls, int heads, int sectors, + char **lines) { - return sfdisk (device, 0, cyls, heads, sectors, lines); + return sfdisk (device, 0, cyls, heads, sectors, NULL, lines); } int -do_sfdisk_N (const char *device, int n, int cyls, int heads, int sectors, - const char *line) +do_sfdisk_N (char *device, int n, int cyls, int heads, int sectors, + char *line) { const char *lines[2] = { line, NULL }; - return sfdisk (device, n, cyls, heads, sectors, lines); + return sfdisk (device, n, cyls, heads, sectors, NULL, lines); +} + +int +do_sfdiskM (char *device, char **lines) +{ + return sfdisk (device, 0, 0, 0, 0, "-uM", lines); } static char * -sfdisk_flag (const char *device, const char *flag) +sfdisk_flag (char *device, const char *flag) { char *out, *err; int r; @@ -108,23 +123,25 @@ sfdisk_flag (const char *device, const char *flag) free (err); + udev_settle (); + return out; /* caller frees */ } char * -do_sfdisk_l (const char *device) +do_sfdisk_l (char *device) { return sfdisk_flag (device, "-l"); } char * -do_sfdisk_kernel_geometry (const char *device) +do_sfdisk_kernel_geometry (char *device) { return sfdisk_flag (device, "-g"); } char * -do_sfdisk_disk_geometry (const char *device) +do_sfdisk_disk_geometry (char *device) { return sfdisk_flag (device, "-G"); }