]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fdisk: add --wipe-partitions=auto|never|default
authorKarel Zak <kzak@redhat.com>
Wed, 4 May 2016 10:43:35 +0000 (12:43 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 4 May 2016 10:43:35 +0000 (12:43 +0200)
The option allows to remove filesystes/RAIDs from newly created
partitions before the partition table is updated (and partition
device created).

The default is "auto" in this case wipe is enabled in interactive mode
only and user's confirmation (yes/no dialog) is required. Note that
keep filesystem signature on partition is pretty valid use-case, so we
don't erase anything by default.

Signed-off-by: Karel Zak <kzak@redhat.com>
disk-utils/fdisk-menu.c
disk-utils/fdisk.8
disk-utils/fdisk.c
disk-utils/fdisk.h

index becdb40e4bb15ad94529175794a08093380ad639..b099cc3c29913c184d54f8fb123d60fb7f88eea9 100644 (file)
@@ -516,6 +516,37 @@ done:
        return rc;
 }
 
+static int ask_for_wipe(struct fdisk_context *cxt, size_t partno)
+{
+       struct fdisk_partition *tmp = NULL;
+       char *fstype = NULL;
+       int rc, yes = 0;
+
+       rc = fdisk_get_partition(cxt, partno, &tmp);
+       if (rc)
+               goto done;
+
+       rc = fdisk_partition_to_string(tmp, cxt, FDISK_FIELD_FSTYPE, &fstype);
+       if (rc || fstype == NULL)
+               goto done;
+
+       fdisk_warnx(cxt, _("Partition #%zu contains a %s signature."), partno + 1, fstype);
+
+       if (pwipemode == WIPEMODE_AUTO && isatty(STDIN_FILENO))
+               fdisk_ask_yesno(cxt, _("Do you want to remove the signature?"), &yes);
+       else if (pwipemode == WIPEMODE_ALWAYS)
+               yes = 1;
+
+       if (yes) {
+               fdisk_info(cxt, _("The signature will be removed by a write command."));
+               rc = fdisk_wipe_partition(cxt, partno, TRUE);
+       }
+done:
+       fdisk_unref_partition(tmp);
+       free(fstype);
+       return rc;
+}
+
 /*
  * Basic fdisk actions
  */
@@ -610,8 +641,13 @@ static int generic_menu_cb(struct fdisk_context **cxt0,
                list_partition_types(cxt);
                break;
        case 'n':
-               rc = fdisk_add_partition(cxt, NULL, NULL);
+       {
+               size_t partno;
+               rc = fdisk_add_partition(cxt, NULL, &partno);
+               if (!rc)
+                       rc = ask_for_wipe(cxt, partno);
                break;
+       }
        case 't':
                change_partition_type(cxt);
                break;
index 41069dd7aff2fa1a83ba81fa379cf3794b2b2ec6..5d644d8d696897e8b1b854594f31addf9698d9a2 100644 (file)
@@ -129,6 +129,18 @@ before a new partition table is created.  See also
 .BR wipefs (8)
 command.
 
+.TP
+\fB\-W\fR, \fB\-\-wipe-partition\fR \fIwhen\fR
+Wipe filesystem, RAID and partition-table signatures from a newly created
+partitions, in order to avoid possible collisions.  The argument \fIwhen\fR can
+be \fBauto\fR, \fBnever\fR or \fBalways\fR.  When this option is not given, the
+default is \fBauto\fR, in which case signatures are wiped only when in
+interactive mode and after confirmation by user.  In all cases detected
+signatures are reported by warning messages before a new partition is
+created.  See also
+.BR wipefs (8)
+command.
+
 .TP
 \fB\-V\fR, \fB\-\-version\fR
 Display version information and exit.
index 73d82eb4ca9a556358b869657370946f938e358a..1f1f9ba6b22d4b30dce9ec6f0f0b00641bc40352 100644 (file)
@@ -49,6 +49,8 @@
 # include <linux/blkpg.h>
 #endif
 
+int pwipemode = WIPEMODE_AUTO;
+
 /*
  * fdisk debug stuff (see fdisk.h and include/debug.h)
  */
@@ -741,6 +743,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
        fputs(_(" -s, --getsz                   display device size in 512-byte sectors [DEPRECATED]\n"), out);
        fputs(_("     --bytes                   print SIZE in bytes rather than in human readable format\n"), out);
        fputs(_(" -w, --wipe <mode>             wipe signatures (auto, always or never)\n"), out);
+       fputs(_(" -W, --wipe-partitions <mode>  wipe signatures from new partitions (auto, always or never)\n"), out);
 
        fputs(USAGE_SEPARATOR, out);
        fputs(_(" -C, --cylinders <number>      specify the number of cylinders\n"), out);
@@ -791,6 +794,7 @@ int main(int argc, char **argv)
                { "output",         no_argument,       NULL, 'o' },
                { "protect-boot",   no_argument,       NULL, 'B' },
                { "wipe",           required_argument, NULL, 'w' },
+               { "wipe-partitions",required_argument, NULL, 'W' },
                { NULL, 0, NULL, 0 }
        };
 
@@ -809,7 +813,7 @@ int main(int argc, char **argv)
 
        fdisk_set_ask(cxt, ask_callback, NULL);
 
-       while ((c = getopt_long(argc, argv, "b:Bc::C:hH:lL::o:sS:t:u::vVw:",
+       while ((c = getopt_long(argc, argv, "b:Bc::C:hH:lL::o:sS:t:u::vVw:W:",
                                longopts, NULL)) != -1) {
                switch (c) {
                case 'b':
@@ -905,6 +909,11 @@ int main(int argc, char **argv)
                        if (wipemode < 0)
                                errx(EXIT_FAILURE, _("unsupported wipe mode"));
                        break;
+               case 'W':
+                       pwipemode = wipemode_from_string(optarg);
+                       if (pwipemode < 0)
+                               errx(EXIT_FAILURE, _("unsupported wipe mode"));
+                       break;
                case 'h':
                        usage(stdout);
                case OPT_BYTES:
index f66404d04c2fc979b5a39f76b858aebaea4d250a..7a7fb85d357b92f77a8b52d8237425507c2b293e 100644 (file)
@@ -24,6 +24,8 @@
 #define FDISKPROG_DEBUG_ASK    (1 << 5)
 #define FDISKPROG_DEBUG_ALL    0xFFFF
 
+extern int pwipemode;
+
 UL_DEBUG_DECLARE_MASK(fdisk);
 #define DBG(m, x)       __UL_DBG(fdisk, FDISKPROG_DEBUG_, m, x)
 #define ON_DBG(m, x)    __UL_DBG_CALL(fdisk, FDISKPROG_DEBUG_, m, x)