]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fdisk: improve --wipe functionality
authorKarel Zak <kzak@redhat.com>
Tue, 14 Feb 2017 14:11:35 +0000 (15:11 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 14 Feb 2017 14:13:35 +0000 (15:13 +0100)
* always (except --wipe=never) wipe old partition tables
* improve warn messages
* improve man page

Addresses: https://github.com/karelzak/util-linux/issues/410
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 3ea5e3ddd3582c1b0d6101ca60b207bd65f811fb..baac9791a04863036b00f6ae24a6efdfc13ec695 100644 (file)
@@ -1032,22 +1032,25 @@ static int createlabel_menu_cb(struct fdisk_context **cxt0,
                        rc = fdisk_create_disklabel(cxt, "sgi");
                        break;
                }
-               return rc;
+       } else {
+               switch (ent->key) {
+                       case 'g':
+                               rc = fdisk_create_disklabel(cxt, "gpt");
+                               break;
+                       case 'G':
+                               rc = fdisk_create_disklabel(cxt, "sgi");
+                               break;
+                       case 'o':
+                               rc = fdisk_create_disklabel(cxt, "dos");
+                               break;
+                       case 's':
+                               rc = fdisk_create_disklabel(cxt, "sun");
+                               break;
+               }
        }
 
-       switch (ent->key) {
-               case 'g':
-                       fdisk_create_disklabel(cxt, "gpt");
-                       break;
-               case 'G':
-                       fdisk_create_disklabel(cxt, "sgi");
-                       break;
-               case 'o':
-                       fdisk_create_disklabel(cxt, "dos");
-                       break;
-               case 's':
-                       fdisk_create_disklabel(cxt, "sun");
-                       break;
-       }
+       if (rc == 0 && fdisk_get_collision(cxt))
+               follow_wipe_mode(cxt);
+
        return rc;
 }
index 91c6b6f84da844618cbbc3f2a7197e3af9035570..1706b928f45c5eb45b5327ef3aed740b3c60b4d8 100644 (file)
@@ -120,17 +120,6 @@ partition tables.) A reasonable value is 63.
 
 .TP
 \fB\-w\fR, \fB\-\-wipe\fR \fIwhen\fR
-Wipe filesystem, RAID and partition-table signatures from the device, 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.
-In all cases detected signatures are reported by warning messages
-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
index a2b4daa36f0a994a12b97455dd92ee61f9aa56c3..4ac58ee4df46dde5e28dc06d7269988c39467f96 100644 (file)
@@ -51,6 +51,7 @@
 #endif
 
 int pwipemode = WIPEMODE_AUTO;
+static int wipemode = WIPEMODE_AUTO;
 
 /*
  * fdisk debug stuff (see fdisk.h and include/debug.h)
@@ -723,6 +724,30 @@ static fdisk_sector_t get_dev_blocks(char *dev)
        return size/2;
 }
 
+
+void follow_wipe_mode(struct fdisk_context *cxt)
+{
+       int dowipe = wipemode == WIPEMODE_ALWAYS ? 1 : 0;
+
+       if (isatty(STDIN_FILENO) && wipemode == WIPEMODE_AUTO)
+               dowipe = 1;     /* do it in interactive mode */
+
+       if (fdisk_is_ptcollision(cxt) && wipemode != WIPEMODE_NEVER)
+               dowipe = 1;     /* always remove old PT */
+
+       fdisk_enable_wipe(cxt, dowipe);
+       if (dowipe)
+               fdisk_warnx(cxt, _(
+                       "The old %s signature will be removed by a write command."),
+                       fdisk_get_collision(cxt));
+       else
+               fdisk_warnx(cxt, _(
+                       "The old %s signature may remain on the device. "
+                       "It is recommended to wipe the device with wipefs(8) or "
+                       "fdisk --wipe, in order to avoid possible collisions."),
+                       fdisk_get_collision(cxt));
+}
+
 static void __attribute__ ((__noreturn__)) usage(FILE *out)
 {
        fputs(USAGE_HEADER, out);
@@ -777,7 +802,6 @@ int main(int argc, char **argv)
 {
        int rc, i, c, act = ACT_FDISK;
        int colormode = UL_COLORMODE_UNDEF;
-       int wipemode = WIPEMODE_AUTO;
        struct fdisk_context *cxt;
        char *outarg = NULL;
        enum {
@@ -995,24 +1019,9 @@ int main(int argc, char **argv)
 
                fflush(stdout);
 
-               if (fdisk_get_collision(cxt)) {
-                       int dowipe = wipemode == WIPEMODE_ALWAYS ? 1 : 0;
-
-                       fdisk_warnx(cxt, _("Device %s already contains a %s signature."),
-                               argv[optind], fdisk_get_collision(cxt));
+               if (fdisk_get_collision(cxt))
+                       follow_wipe_mode(cxt);
 
-                       if (isatty(STDIN_FILENO) && wipemode == WIPEMODE_AUTO)
-                               dowipe = 1;     /* do it in interactive mode */
-
-                       fdisk_enable_wipe(cxt, dowipe);
-                       if (dowipe)
-                               fdisk_warnx(cxt, _(
-                                       "The signature will be removed by a write command."));
-                       else
-                               fdisk_warnx(cxt, _(
-                                       "It is strongly recommended to wipe the device with "
-                                       "wipefs(8), in order to avoid possible collisions."));
-               }
                if (!fdisk_has_label(cxt)) {
                        fdisk_info(cxt, _("Device does not contain a recognized partition table."));
                        fdisk_create_disklabel(cxt, NULL);
index 7a7fb85d357b92f77a8b52d8237425507c2b293e..8c08bc34d240eea021fea01fde64ae692502d21d 100644 (file)
@@ -49,4 +49,6 @@ extern void change_partition_type(struct fdisk_context *cxt);
 
 extern void toggle_dos_compatibility_flag(struct fdisk_context *cxt);
 
+extern void follow_wipe_mode(struct fdisk_context *cxt);
+
 #endif /* UTIL_LINUX_FDISK_H */