From cb9a4b0033eca429689a403be2a192fe2842f2e9 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 18 Feb 2016 12:50:11 +0100 Subject: fdisk: add --wipe This patch changes fdisk behavior and it wipes foreign signatures from the device to avoid collisions. The wipe functionality is automatically enabled in the interactive mode only (user is always warned about it), otherwise it's possible to control all by --wipe . The program does not change behavior when executed in scripts (echo | fdisk), the option "--wipe=always" is required to enable in this case. Signed-off-by: Karel Zak --- disk-utils/fdisk.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'disk-utils/fdisk.c') diff --git a/disk-utils/fdisk.c b/disk-utils/fdisk.c index c924d26b6..0b229839e 100644 --- a/disk-utils/fdisk.c +++ b/disk-utils/fdisk.c @@ -734,6 +734,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out) fputs(_(" -u, --units[=] display units: 'cylinders' or 'sectors' (default)\n"), 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 wipe signatures (auto, always or never)\n"), out); fputs(USAGE_SEPARATOR, out); fputs(_(" -C, --cylinders specify the number of cylinders\n"), out); @@ -761,6 +762,7 @@ 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 { @@ -782,6 +784,7 @@ int main(int argc, char **argv) { "version", no_argument, NULL, 'V' }, { "output", no_argument, NULL, 'o' }, { "protect-boot", no_argument, NULL, 'B' }, + { "wipe", required_argument, NULL, 'w' }, { NULL, 0, NULL, 0 } }; @@ -800,7 +803,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::vV", + while ((c = getopt_long(argc, argv, "b:Bc::C:hH:lL::o:sS:t:u::vVw:", longopts, NULL)) != -1) { switch (c) { case 'b': @@ -891,6 +894,11 @@ int main(int argc, char **argv) case 'v': /* for backward compatibility only */ printf(UTIL_LINUX_VERSION); return EXIT_SUCCESS; + case 'w': + wipemode = wipemode_from_string(optarg); + if (wipemode < 0) + errx(EXIT_FAILURE, _("unsupported wipe mode")); + break; case 'h': usage(stdout); case OPT_BYTES: @@ -957,6 +965,24 @@ int main(int argc, char **argv) fflush(stdout); + if (fdisk_get_collision(cxt)) { + int dowipe = wipemode == WIPEMODE_ALWAYS ? 1 : 0; + + fdisk_warnx(cxt, _("%s: device already contains %s signature."), + argv[optind], fdisk_get_collision(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 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); -- cgit v1.2.3-55-g7522