From e021d29afc295f3b3ad38ffb2726372dc4415a41 Mon Sep 17 00:00:00 2001 From: Markus Hilger Date: Tue, 3 Nov 2026 15:45:52 +0100 Subject: [PATCH] Keep image2disk partitions inside the disk When the largest filesystem of a captured image is a plain partition, it was sized as the disk minus the other filesystems. That ignored the 2048 sector gap before the first partition and the rounding of every partition up to a 2048 sector boundary, so the last partition always ran past the end of the disk. The clamp then set its end to the sector count, one past the last sector and inside the backup GPT, and parted refused it with 'The location ... is outside of the device'. Images with the largest filesystem in LVM were not affected. Reserve that overhead plus 1MiB at the end of the disk for the backup GPT, and clamp to that last sector instead. Also round the swap partition by its own size rather than by the previous partition's. --- .../profiles/default/scripts/image2disk.py | 18 ++++++++++++------ .../profiles/default/scripts/image2disk.py | 18 ++++++++++++------ .../profiles/default/scripts/image2disk.py | 18 ++++++++++++------ .../profiles/default/scripts/image2disk.py | 18 ++++++++++++------ 4 files changed, 48 insertions(+), 24 deletions(-) diff --git a/confluent_osdeploy/el7-diskless/profiles/default/scripts/image2disk.py b/confluent_osdeploy/el7-diskless/profiles/default/scripts/image2disk.py index ec67bad0..eaccfb62 100644 --- a/confluent_osdeploy/el7-diskless/profiles/default/scripts/image2disk.py +++ b/confluent_osdeploy/el7-diskless/profiles/default/scripts/image2disk.py @@ -256,7 +256,13 @@ def install_to_disk(imgpath): minsectors = mintotsize // sectorsize if sectors < (minsectors + 65536): raise Exception('Disk too small to fit image') - biggestsectors = sectors - (minexcsize // sectorsize) + # leave room for the 1MiB gap before the first partition, up to 1MiB of + # rounding per partition and the backup GPT at the end of the disk + lastsector = sectors - 2048 + nparts = len(plainvols) + if swapsize and not lvmvols: + nparts += 1 + biggestsectors = lastsector - 2048 * (nparts + 1) - (minexcsize // sectorsize) biggestsize = sectorsize * biggestsectors parted.run('mklabel gpt') curroffset = 2048 @@ -268,18 +274,18 @@ def install_to_disk(imgpath): size = biggestsize // sectorsize size += 2047 - (size % 2048) end = curroffset + size - if end > sectors: - end = sectors + if end > lastsector: + end = lastsector parted.run('mkpart primary {}s {}s'.format(curroffset, end)) vol['targetdisk'] = get_partname(instdisk , volidx) curroffset += size + 1 if not lvmvols: if swapsize: swapsize = swapsize // sectorsize - swapsize += 2047 - (size % 2048) + swapsize += 2047 - (swapsize % 2048) end = curroffset + swapsize - if end > sectors: - end = sectors + if end > lastsector: + end = lastsector parted.run('mkpart swap {}s {}s'.format(curroffset, end)) subprocess.check_call(['mkswap', get_partname(instdisk, volidx + 1)]) else: diff --git a/confluent_osdeploy/el8-diskless/profiles/default/scripts/image2disk.py b/confluent_osdeploy/el8-diskless/profiles/default/scripts/image2disk.py index 3971ca71..65ca4fe6 100644 --- a/confluent_osdeploy/el8-diskless/profiles/default/scripts/image2disk.py +++ b/confluent_osdeploy/el8-diskless/profiles/default/scripts/image2disk.py @@ -345,7 +345,13 @@ def install_to_disk(imgpath): minsectors = mintotsize // sectorsize if sectors < (minsectors + 65536): raise Exception('Disk too small to fit image') - biggestsectors = sectors - (minexcsize // sectorsize) + # leave room for the 1MiB gap before the first partition, up to 1MiB of + # rounding per partition and the backup GPT at the end of the disk + lastsector = sectors - 2048 + nparts = len(plainvols) + if swapsize and not lvmvols: + nparts += 1 + biggestsectors = lastsector - 2048 * (nparts + 1) - (minexcsize // sectorsize) biggestsize = sectorsize * biggestsectors parted.run('mklabel gpt') curroffset = 2048 @@ -357,18 +363,18 @@ def install_to_disk(imgpath): size = biggestsize // sectorsize size += 2047 - (size % 2048) end = curroffset + size - if end > sectors: - end = sectors + if end > lastsector: + end = lastsector parted.run('mkpart primary {}s {}s'.format(curroffset, end)) vol['targetdisk'] = get_partname(instdisk, volidx) curroffset += size + 1 if not lvmvols: if swapsize: swapsize = swapsize // sectorsize - swapsize += 2047 - (size % 2048) + swapsize += 2047 - (swapsize % 2048) end = curroffset + swapsize - if end > sectors: - end = sectors + if end > lastsector: + end = lastsector parted.run('mkpart swap {}s {}s'.format(curroffset, end)) subprocess.check_call(['mkswap', get_partname(instdisk, volidx + 1)]) else: diff --git a/confluent_osdeploy/el9-diskless/profiles/default/scripts/image2disk.py b/confluent_osdeploy/el9-diskless/profiles/default/scripts/image2disk.py index a0e22247..8cab5e50 100644 --- a/confluent_osdeploy/el9-diskless/profiles/default/scripts/image2disk.py +++ b/confluent_osdeploy/el9-diskless/profiles/default/scripts/image2disk.py @@ -447,7 +447,13 @@ def install_to_disk(imgpath): minsectors = mintotsize // sectorsize if sectors < (minsectors + 65536): raise Exception('Disk too small to fit image') - biggestsectors = sectors - (minexcsize // sectorsize) + # leave room for the 1MiB gap before the first partition, up to 1MiB of + # rounding per partition and the backup GPT at the end of the disk + lastsector = sectors - 2048 + nparts = len(plainvols) + if swapsize and not lvmvols: + nparts += 1 + biggestsectors = lastsector - 2048 * (nparts + 1) - (minexcsize // sectorsize) biggestsize = sectorsize * biggestsectors parted.run('mklabel gpt') curroffset = 2048 @@ -459,8 +465,8 @@ def install_to_disk(imgpath): size = biggestsize // sectorsize size += 2047 - (size % 2048) end = curroffset + size - if end > sectors: - end = sectors + if end > lastsector: + end = lastsector parted.run('mkpart primary {}s {}s'.format(curroffset, end)) vol['targetdisk'] = get_partname(instdisk, volidx) if vol['mount'] == '/': @@ -469,10 +475,10 @@ def install_to_disk(imgpath): if not lvmvols: if swapsize: swapsize = swapsize // sectorsize - swapsize += 2047 - (size % 2048) + swapsize += 2047 - (swapsize % 2048) end = curroffset + swapsize - if end > sectors: - end = sectors + if end > lastsector: + end = lastsector parted.run('mkpart swap {}s {}s'.format(curroffset, end)) newswapdev = get_partname(instdisk, volidx + 1) subprocess.check_call(['mkswap', newswapdev]) diff --git a/confluent_osdeploy/ubuntu20.04-diskless/profiles/default/scripts/image2disk.py b/confluent_osdeploy/ubuntu20.04-diskless/profiles/default/scripts/image2disk.py index ef9419f4..3bca6302 100644 --- a/confluent_osdeploy/ubuntu20.04-diskless/profiles/default/scripts/image2disk.py +++ b/confluent_osdeploy/ubuntu20.04-diskless/profiles/default/scripts/image2disk.py @@ -287,7 +287,13 @@ def install_to_disk(imgpath): minsectors = mintotsize // sectorsize if sectors < (minsectors + 65536): raise Exception('Disk too small to fit image') - biggestsectors = sectors - (minexcsize // sectorsize) + # leave room for the 1MiB gap before the first partition, up to 1MiB of + # rounding per partition and the backup GPT at the end of the disk + lastsector = sectors - 2048 + nparts = len(plainvols) + if swapsize and not lvmvols: + nparts += 1 + biggestsectors = lastsector - 2048 * (nparts + 1) - (minexcsize // sectorsize) biggestsize = sectorsize * biggestsectors parted.run('mklabel gpt') curroffset = 2048 @@ -299,18 +305,18 @@ def install_to_disk(imgpath): size = biggestsize // sectorsize size += 2047 - (size % 2048) end = curroffset + size - if end > sectors: - end = sectors + if end > lastsector: + end = lastsector parted.run('mkpart primary {}s {}s'.format(curroffset, end)) vol['targetdisk'] = get_partname(instdisk, volidx) curroffset += size + 1 if not lvmvols: if swapsize: swapsize = swapsize // sectorsize - swapsize += 2047 - (size % 2048) + swapsize += 2047 - (swapsize % 2048) end = curroffset + swapsize - if end > sectors: - end = sectors + if end > lastsector: + end = lastsector parted.run('mkpart swap {}s {}s'.format(curroffset, end)) subprocess.check_call(['mkswap', get_partname(instdisk, volidx + 1)]) else: