diff options
author | Karel Zak | 2016-02-18 12:50:11 +0100 |
---|---|---|
committer | Karel Zak | 2016-02-18 12:58:12 +0100 |
commit | cb9a4b0033eca429689a403be2a192fe2842f2e9 (patch) | |
tree | d2bb843aaa8462e6714fa609aa1c9cc03b6aab27 | |
parent | libfdisk: add API to control signatures wiping (diff) | |
download | kernel-qcow2-util-linux-cb9a4b0033eca429689a403be2a192fe2842f2e9.tar.gz kernel-qcow2-util-linux-cb9a4b0033eca429689a403be2a192fe2842f2e9.tar.xz kernel-qcow2-util-linux-cb9a4b0033eca429689a403be2a192fe2842f2e9.zip |
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
<auto|never|always>.
The program does not change behavior when executed in scripts (echo
<something> | fdisk), the option "--wipe=always" is required to enable
in this case.
Signed-off-by: Karel Zak <kzak@redhat.com>
-rw-r--r-- | disk-utils/fdisk-list.h | 28 | ||||
-rw-r--r-- | disk-utils/fdisk.8 | 10 | ||||
-rw-r--r-- | disk-utils/fdisk.c | 28 |
3 files changed, 65 insertions, 1 deletions
diff --git a/disk-utils/fdisk-list.h b/disk-utils/fdisk-list.h index c24c4ddcc..eddab92ec 100644 --- a/disk-utils/fdisk-list.h +++ b/disk-utils/fdisk-list.h @@ -15,4 +15,32 @@ extern void print_all_devices_freespace(struct fdisk_context *cxt); extern void list_available_columns(FILE *out); extern int *init_fields(struct fdisk_context *cxt, const char *str, size_t *n); + +/* used by fdisk and sfdisk */ +enum { + WIPEMODE_AUTO = 0, + WIPEMODE_NEVER = 1, + WIPEMODE_ALWAYS = 2 +}; + +static inline int wipemode_from_string(const char *str) +{ + size_t i; + static const char *modes[] = { + [WIPEMODE_AUTO] = "auto", + [WIPEMODE_NEVER] = "never", + [WIPEMODE_ALWAYS] = "always" + }; + + if (!str || !*str) + return -EINVAL; + + for (i = 0; i < ARRAY_SIZE(modes); i++) { + if (strcasecmp(str, modes[i]) == 0) + return i; + } + + return -EINVAL; +} + #endif /* UTIL_LINUX_FDISK_LIST_H */ diff --git a/disk-utils/fdisk.8 b/disk-utils/fdisk.8 index fb298220e..f0c6def45 100644 --- a/disk-utils/fdisk.8 +++ b/disk-utils/fdisk.8 @@ -119,6 +119,16 @@ Specify the number of sectors per track of the disk. partition tables.) A reasonable value is 63. .TP +\fB\-w\fR, \fB\-\-wipe\fR \fImode\fR +Wipe filesystems, RAIDs and partition tables signatures from the device, in +order to avoid possible collisions. The \fImode\fR can be \fBauto\fR, +\fBnever\fR or \fBalways\fR. The default is \fBauto\fR, in this case signatures +are wiped in the interactive mode only, in all cases detected signatures are +reported by warning message before a new partition table is created. See also +.BR wipefs (8) +command. + +.TP \fB\-V\fR, \fB\-\-version\fR Display version information and exit. 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[=<unit>] 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 <mode> wipe signatures (auto, always or never)\n"), out); fputs(USAGE_SEPARATOR, out); fputs(_(" -C, --cylinders <number> 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); |