Simplify GPT a little by explicitly taking care of the GPT primary & secondary

Do the math for the GPT offset a little differently
This commit is contained in:
Anders Ingemann 2015-01-19 01:22:12 +01:00
parent f0402d6a9b
commit 4d74c72d99

View file

@ -24,12 +24,18 @@ class GPTPartitionMap(AbstractPartitionMap):
def last_partition():
return self.partitions[-1] if len(self.partitions) > 0 else None
# If we are using the grub bootloader we need to create an unformatted partition
# at the beginning of the map. Its size is 1007kb, which we will steal from the
# next partition.
# The first 34 sectors are reserved for the primary GPT
primary_gpt = PartitionGap(Sectors(34, sector_size), last_partition())
self.partitions.append(primary_gpt)
if bootloader == 'grub':
# If we are using the grub bootloader we need to create an unformatted partition
# at the beginning of the map. Its size is 1007kb, which seems to be chosen so that
# gpt_primary + grub = 1024KiB
# So lets just specify grub size as 1MiB - 34 sectors
from ..partitions.unformatted import UnformattedPartition
self.grub_boot = UnformattedPartition(Sectors('1007KiB', sector_size), last_partition())
grub_size = Sectors('1MiB', sector_size) - primary_gpt.size
self.grub_boot = UnformattedPartition(grub_size, last_partition())
# Mark the partition as a bios_grub partition
self.grub_boot.flags.append('bios_grub')
self.partitions.append(self.grub_boot)
@ -48,22 +54,14 @@ class GPTPartitionMap(AbstractPartitionMap):
'root', last_partition())
self.partitions.append(self.root)
# We need to move the first partition to make space for the gpt offset
gpt_offset = Sectors('17KiB', sector_size)
self.partitions[0].offset += gpt_offset
# The last 34 sectors are reserved for the secondary GPT
secondary_gpt = PartitionGap(Sectors(34, sector_size), last_partition())
self.partitions.append(secondary_gpt)
# reduce the size of the root partition so that the overall volume size is not exceeded
self.root.size -= primary_gpt.size + secondary_gpt.size
if hasattr(self, 'grub_boot'):
# grub_boot should not increase the size of the volume,
# so we reduce the size of the succeeding partition.
# gpt_offset is included here, because of the offset we added above (grub_boot is partition[0])
self.partitions[1].size -= self.grub_boot.get_end()
else:
# Avoid increasing the volume size because of gpt_offset
self.partitions[0].size -= gpt_offset
# Leave the last sector unformatted
self.partitions[-1].size -= 1
self.partitions.append(PartitionGap(Sectors(1, sector_size), last_partition()))
self.root.size -= self.grub_boot.size
super(GPTPartitionMap, self).__init__(bootloader)