From aab9be66c4a2525a6ca3b2b7a47b8ad2a54aa659 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 17 Sep 2015 13:57:44 +0200 Subject: [PATCH] sfdisk: add --delete Signed-off-by: Karel Zak --- disk-utils/sfdisk.8 | 3 +++ disk-utils/sfdisk.c | 50 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/disk-utils/sfdisk.8 b/disk-utils/sfdisk.8 index 44db73bb54..77fb3f7521 100644 --- a/disk-utils/sfdisk.8 +++ b/disk-utils/sfdisk.8 @@ -70,6 +70,9 @@ unused partition given with \fB\-N\fR. See also \fB\---append\fR. Switch on the bootable flag. If no \fIpartition-number\fR is specified, then all partitions with an enabled flag are listed. .TP +.BR " \-\-delete \fIdevice\fR [" \fIpartition-number\fR...] +Delete all or specified partitions. +.TP .BR \-d , " \-\-dump " \fIdevice\fR Dump the partitions of a device in a format that is usable as input to \fBsfdisk\fR. See the section \fBBACKING UP THE PARTITION TABLE\fR. diff --git a/disk-utils/sfdisk.c b/disk-utils/sfdisk.c index 743719ef9a..8b213001bf 100644 --- a/disk-utils/sfdisk.c +++ b/disk-utils/sfdisk.c @@ -83,6 +83,7 @@ enum { ACT_PARTUUID, ACT_PARTLABEL, ACT_PARTATTRS, + ACT_DELETE }; struct sfdisk { @@ -840,6 +841,45 @@ static int command_activate(struct sfdisk *sf, int argc, char **argv) return rc; } +/* + * sfdisk --delete [ ...] + */ +static int command_delete(struct sfdisk *sf, int argc, char **argv) +{ + size_t i; + const char *devname = NULL; + + if (argc < 1) + errx(EXIT_FAILURE, _("no disk device specified")); + devname = argv[0]; + + if (fdisk_assign_device(sf->cxt, devname, 0) != 0) + err(EXIT_FAILURE, _("cannot open %s"), devname); + + if (sf->backup) + backup_partition_table(sf, devname); + + /* delate all */ + if (argc == 1) { + size_t nparts = fdisk_get_npartitions(sf->cxt); + for (i = 0; i < nparts; i++) { + if (fdisk_is_partition_used(sf->cxt, i) && + fdisk_delete_partition(sf->cxt, i) != 0) + errx(EXIT_FAILURE, _("%s: partition %zu: failed to delete"), devname, i + 1); + } + /* delete specified */ + } else { + for (i = 1; i < (size_t) argc; i++) { + size_t n = strtou32_or_err(argv[i], _("failed to parse partition number")); + + if (fdisk_delete_partition(sf->cxt, n - 1) != 0) + errx(EXIT_FAILURE, _("%s: partition %zu: failed to delete"), devname, n); + } + } + + return write_changes(sf); +} + /* * sfdisk --reorder */ @@ -1670,6 +1710,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out) fputs(_(" -s, --show-size [ ...] list sizes of all or specified devices\n"), out); fputs(_(" -T, --list-types print the recognized types (see -X)\n"), out); fputs(_(" -V, --verify [ ...] test whether partitions seem correct\n"), out); + fputs(_(" --delete [ ...] delete all or specified partitions\n"), out); fputs(USAGE_SEPARATOR, out); fputs(_(" --part-label [] print or change partition label\n"), out); @@ -1736,6 +1777,7 @@ int main(int argc, char *argv[]) OPT_BYTES, OPT_COLOR, OPT_MOVEDATA, + OPT_DELETE }; static const struct option longopts[] = { @@ -1745,6 +1787,7 @@ int main(int argc, char *argv[]) { "backup-file", required_argument, NULL, 'O' }, { "bytes", no_argument, NULL, OPT_BYTES }, { "color", optional_argument, NULL, OPT_COLOR }, + { "delete", no_argument, NULL, OPT_DELETE }, { "dump", no_argument, NULL, 'd' }, { "help", no_argument, NULL, 'h' }, { "force", no_argument, NULL, 'f' }, @@ -1904,6 +1947,9 @@ int main(int argc, char *argv[]) sf->movedata = 1; sf->move_typescript = optarg; break; + case OPT_DELETE: + sf->act = ACT_DELETE; + break; default: usage(stderr); } @@ -1931,6 +1977,10 @@ int main(int argc, char *argv[]) rc = command_activate(sf, argc - optind, argv + optind); break; + case ACT_DELETE: + rc = command_delete(sf, argc - optind, argv + optind); + break; + case ACT_LIST: rc = command_list_partitions(sf, argc - optind, argv + optind); break; -- 2.39.2