From fbb8eb09fb851bfadea0afe68ec80d928e94488a Mon Sep 17 00:00:00 2001 From: Ryan Harper Date: Tue, 1 Sep 2015 17:01:07 -0500 Subject: [PATCH] Adjust partition alignment for GPT and curtin sgdisk Curtin uses sgdisk to create partitions. To avoid sgdisk fixing alignment issues we ensure that all partitions are 1M aligned. GPT partitions also require space at the end, reserve the same number of sectors (1K, 2048 512b sectors) at the start and end of the disk. Signed-off-by: Ryan Harper --- subiquity/controllers/filesystem.py | 15 +++++++++------ subiquity/models/blockdev.py | 18 ++++++++++++++++-- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/subiquity/controllers/filesystem.py b/subiquity/controllers/filesystem.py index 8b26dba6..b4a8f093 100644 --- a/subiquity/controllers/filesystem.py +++ b/subiquity/controllers/filesystem.py @@ -16,7 +16,6 @@ import logging from subiquity.controller import ControllerPolicy from subiquity.models import FilesystemModel -from subiquity.models.blockdev import FIRST_PARTITION_OFFSET from subiquity.ui.views import (DiskPartitionView, AddPartitionView, FilesystemView) from subiquity.ui.dummy import DummyView @@ -97,14 +96,18 @@ class FilesystemController(ControllerPolicy): if current_disk.parttype == 'gpt' and \ len(current_disk.disk.partitions) == 0: log.debug('Adding grub_bios gpt partition first') - current_disk.add_partition(partnum=1, - size=BIOS_GRUB_SIZE_BYTES, - fstype=None, - flag='bios_grub') + size_added = \ + current_disk.add_partition(partnum=1, + size=BIOS_GRUB_SIZE_BYTES, + fstype=None, + flag='bios_grub') # adjust downward the partition size to accommodate # the offset and bios/grub partition - spec['bytes'] -= FIRST_PARTITION_OFFSET + BIOS_GRUB_SIZE_BYTES + log.debug("Adjusting request down:" + + "{} - {} = {}".format(spec['bytes'], size_added, + spec['bytes'] - size_added)) + spec['bytes'] -= size_added spec['partnum'] = 2 if spec["fstype"] in ["swap"]: diff --git a/subiquity/models/blockdev.py b/subiquity/models/blockdev.py index 4fc14e29..fad466e0 100644 --- a/subiquity/models/blockdev.py +++ b/subiquity/models/blockdev.py @@ -29,6 +29,7 @@ from .actions import ( log = logging.getLogger("subiquity.filesystem.blockdev") FIRST_PARTITION_OFFSET = 1 << 20 # 1K offset/aligned +GPT_END_RESERVE = 1 << 20 # save room at the end for GPT # TODO: Bcachepart class @@ -215,12 +216,23 @@ class Blockdev(): if fstype in ["swap"]: fstype = "linux-swap(v1)" + # round up length by 1M + def _align_up(size, block_size=1 << 30): + return size + (block_size - (size % block_size)) + if len(self.disk.partitions) == 0: offset = FIRST_PARTITION_OFFSET else: offset = 0 - log.debug('requested start: {} length: {}'.format(offset, size)) + log.debug('Aligning start and length on 1M boundaries') + new_size = _align_up(size + offset) + if new_size > self.freespace - GPT_END_RESERVE: + new_size = self.freespace - GPT_END_RESERVE + log.debug('Old size: {} New size: {}'.format(size, new_size)) + + log.debug('requested start: {} length: {}'.format(offset, + new_size - offset)) valid_flags = [ "boot", "lvm", @@ -232,7 +244,8 @@ class Blockdev(): # create partition and add part_action = PartitionAction(self.baseaction, partnum, - offset, size, flag) + offset, new_size - offset, flag) + log.debug('PartitionAction:\n{}'.format(part_action.get())) self.disk.partitions.update({partnum: part_action}) @@ -250,6 +263,7 @@ class Blockdev(): self._mounts[partpath] = mountpoint log.debug('Partition Added') + return new_size def is_mounted(self): with open('/proc/mounts') as pm: