diff --git a/src/repart/repart.c b/src/repart/repart.c index 3eada3bc42a..838786e1532 100644 --- a/src/repart/repart.c +++ b/src/repart/repart.c @@ -3355,7 +3355,7 @@ static int context_copy_from_one(Context *context, const char *src) { _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL; _cleanup_(fdisk_unref_tablep) struct fdisk_table *t = NULL; Partition *last = NULL; - unsigned long secsz, grainsz; + unsigned long secsz; size_t n_partitions; int r; @@ -3374,7 +3374,6 @@ static int context_copy_from_one(Context *context, const char *src) { return log_error_errno(r, "Failed to create fdisk context: %m"); secsz = sym_fdisk_get_sector_size(c); - grainsz = sym_fdisk_get_grain_size(c); /* Insist on a power of two, and that it's a multiple of 512, i.e. the traditional sector size. */ if (secsz < 512 || !ISPOWEROF2(secsz)) @@ -3455,7 +3454,9 @@ static int context_copy_from_one(Context *context, const char *src) { if (!np->split_name_format) return log_oom(); - r = determine_current_padding(c, t, p, secsz, grainsz, &padding); + /* Pass grain size of 1 to disable rounding by grain as we don't know the grain size + * of the old image. We'll round paddings to the grain size of the new image later. */ + r = determine_current_padding(c, t, p, secsz, /* grainsz= */ 1, &padding); if (r < 0) return r; diff --git a/test/units/TEST-58-REPART.sh b/test/units/TEST-58-REPART.sh index 88d486ebc05..f5dfd88c4a7 100755 --- a/test/units/TEST-58-REPART.sh +++ b/test/units/TEST-58-REPART.sh @@ -451,6 +451,48 @@ $imgs/zzz8 : start= 6422488, size= 131072, type=4D21B016-B534-45C2-A9FB losetup -d "$loop" } +testcase_copy_from_grain_padding() { + local defs imgs output + + defs="$(mktemp --directory "/tmp/test-repart.defs.XXXXXXXXXX")" + imgs="$(mktemp --directory "/var/tmp/test-repart.imgs.XXXXXXXXXX")" + # shellcheck disable=SC2064 + trap "rm -rf '$defs' '$imgs'" RETURN + chmod 0755 "$defs" + + truncate -s 80MiB "$imgs/copy_from" + sfdisk "$imgs/copy_from" <