From 4877d7f61a72e46aba3fc52f347d1cd74124dba5 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Mon, 2 Sep 2024 14:04:18 +0200 Subject: [PATCH] sfdisk: ignore last-lba from script on --force The last LBA depends on the disk size. It seems like a good idea to ignore it when using the --force option, in order to make it possible to apply the script on smaller disks. Addresses: https://github.com/util-linux/util-linux/issues/3186 Signed-off-by: Karel Zak --- disk-utils/sfdisk.8.adoc | 2 +- disk-utils/sfdisk.c | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/disk-utils/sfdisk.8.adoc b/disk-utils/sfdisk.8.adoc index bc3ff9bb87..b2e5279d26 100644 --- a/disk-utils/sfdisk.8.adoc +++ b/disk-utils/sfdisk.8.adoc @@ -243,7 +243,7 @@ Specify the partition table identifier. It should be a hexadecimal number (with *first-lba*:: Specify the first usable sector for GPT partitions. This header is ignored if the script and device sector size differ. In this case *sfdisk* uses label specific default. *last-lba*:: -Specify the last usable sector for GPT partitions. This header is ignored if the script and device sector size differ. In this case *sfdisk* uses label specific default. +Specify the last usable sector for GPT partitions. This header is ignored if the script and device sector size differ. In this case *sfdisk* uses label specific default. The last-lba header is ignored if *--force* is specified, making the script usable on disks with different sizes. *table-length*:: Specify the maximal number of GPT partitions. *grain*:: diff --git a/disk-utils/sfdisk.c b/disk-utils/sfdisk.c index 0575373dca..71b0191d21 100644 --- a/disk-utils/sfdisk.c +++ b/disk-utils/sfdisk.c @@ -1925,7 +1925,14 @@ static int command_fdisk(struct sfdisk *sf, int argc, char **argv) ignored++; continue; } - if (!created) { /* create a new disklabel */ + if (!created) { + /* ignore "last-lba" and use default if --force specified */ + if (sf->force && fdisk_script_get_header(dp, "last-lba")) { + fdisk_info(sf->cxt, _("Ignoring last-lba script header.")); + fdisk_script_set_header(dp, "last-lba", NULL); + } + + /* create a new disklabel */ rc = fdisk_apply_script_headers(sf->cxt, dp); created = !rc; if (rc) { @@ -1933,7 +1940,6 @@ static int command_fdisk(struct sfdisk *sf, int argc, char **argv) fdisk_warn(sf->cxt, _( "Failed to apply script headers, disk label not created")); } - if (rc == 0 && fdisk_get_collision(sf->cxt)) follow_wipe_mode(sf); } -- 2.47.2