From 35ca51182782193f555fbdcb06bb10766550d017 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 30 Nov 2016 12:43:10 +0100 Subject: sfdisk: support empty label use-case By default sfdisk creates partition table when a first partition is specified, otherwise the device is not modified. This force users to create at least one partition. This commit allows to create empty label without partitions if "label: " header line is specified by script. The commit also modifies "New situation:" output to list label name and label identifier. Addresses: https://github.com/karelzak/util-linux/issues/374 Signed-off-by: Karel Zak --- disk-utils/sfdisk.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'disk-utils/sfdisk.c') diff --git a/disk-utils/sfdisk.c b/disk-utils/sfdisk.c index 10307ad14..2d6597434 100644 --- a/disk-utils/sfdisk.c +++ b/disk-utils/sfdisk.c @@ -1766,8 +1766,25 @@ static int command_fdisk(struct sfdisk *sf, int argc, char **argv) } } while (1); + /* create empty disk label if label, but no partition specified */ + if (rc == SFDISK_DONE_EOF && created == 0 + && fdisk_script_has_force_label(dp) == 1 + && fdisk_table_get_nents(tb) == 0 + && fdisk_script_get_header(dp, "label")) { + + int xrc = fdisk_apply_script_headers(sf->cxt, dp); + created = !xrc; + if (xrc) { + fdisk_warnx(sf->cxt, _( + "Failed to apply script headers, " + "disk label not created.")); + rc = SFDISK_DONE_ABORT; + } + } + if (!sf->quiet && rc != SFDISK_DONE_ABORT) { fdisk_info(sf->cxt, _("\nNew situation:")); + list_disk_identifier(sf->cxt); list_disklabel(sf->cxt); } -- cgit v1.2.3-55-g7522