From 6c54e1d61fe731c8fc4e86594a78b3d152670cc8 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 17 Jan 2023 21:42:21 +0100 Subject: [PATCH] repart: auto-probe sector size when not specified --- src/partition/repart.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/partition/repart.c b/src/partition/repart.c index 1fe1c06a3c4..586a5edd2d5 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -1978,11 +1978,26 @@ static int context_load_partition_table(Context *context) { if (!c) return log_oom(); - if (arg_sector_size > 0) { + if (arg_sector_size > 0) r = fdisk_save_user_sector_size(c, /* phy= */ 0, arg_sector_size); + else { + uint32_t ssz; + + if (context->backing_fd < 0) { + context->backing_fd = open(context->node, O_RDONLY|O_CLOEXEC); + if (context->backing_fd < 0) + return log_error_errno(errno, "Failed to open device '%s': %m", context->node); + } + + /* Auto-detect sector size if not specified. */ + r = probe_sector_size_prefer_ioctl(context->backing_fd, &ssz); if (r < 0) - return log_error_errno(r, "Failed to set sector size: %m"); + return log_error_errno(r, "Failed to probe sector size of '%s': %m", context->node); + + r = fdisk_save_user_sector_size(c, /* phy= */ 0, ssz); } + if (r < 0) + return log_error_errno(r, "Failed to set sector size: %m"); /* libfdisk doesn't have an API to operate on arbitrary fds, hence reopen the fd going via the * /proc/self/fd/ magic path if we have an existing fd. Open the original file otherwise. */ @@ -1990,7 +2005,6 @@ static int context_load_partition_table(Context *context) { c, context->backing_fd >= 0 ? FORMAT_PROC_FD_PATH(context->backing_fd) : context->node, arg_dry_run); - if (r == -EINVAL && arg_size_auto) { struct stat st; -- 2.47.3