Richard W.M. Jones
2012-Nov-19 15:44 UTC
[Libguestfs] [PATCH] daemon: wipefs: Use --force option if available.
From: "Richard W.M. Jones" <rjones at redhat.com> See https://bugzilla.redhat.com/show_bug.cgi?id=872831 and https://bugzilla.redhat.com/show_bug.cgi?id=865961 --- daemon/zero.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/daemon/zero.c b/daemon/zero.c index 1a66881..4997583 100644 --- a/daemon/zero.c +++ b/daemon/zero.c @@ -82,14 +82,52 @@ optgroup_wipefs_available (void) return prog_exists (str_wipefs); } +/* See RHBZ#872831 */ +static int +wipefs_has_force_option (void) +{ + static int flag = -1; + int r; + char *out, *err; + + if (flag == -1) { + r = command (&out, &err, "wipefs", "--help", NULL); + if (r == -1) { + reply_with_error ("%s", err); + free (out); + free (err); + return -1; + } + free (err); + flag = strstr (out, "--force") != NULL; + free (out); + } + + return flag; +} + int do_wipefs (const char *device) { + int force; int r; char *err = NULL; + const size_t MAX_ARGS = 16; + const char *argv[MAX_ARGS]; + size_t i = 0; + + force = wipefs_has_force_option (); + if (force == -1) + return -1; + + ADD_ARG (argv, i, str_wipefs); + ADD_ARG (argv, i, "-a"); + if (force) + ADD_ARG (argv, i, "--force"); + ADD_ARG (argv, i, device); + ADD_ARG (argv, i, NULL); - const char *wipefs[] = {str_wipefs, "-a", device, NULL}; - r = commandv (NULL, &err, wipefs); + r = commandv (NULL, &err, argv); if (r == -1) { reply_with_error ("%s", err); free (err); -- 1.7.11.4