2013-09-18 00:46:58 +02:00
|
|
|
from abstract import AbstractPartitionMap
|
|
|
|
from ..partitions.gpt import GPTPartition
|
|
|
|
from ..partitions.gpt_swap import GPTSwapPartition
|
2014-03-23 23:12:07 +01:00
|
|
|
from bootstrapvz.common.tools import log_check_call
|
2013-09-18 00:46:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
class GPTPartitionMap(AbstractPartitionMap):
|
2014-03-23 16:04:03 +01:00
|
|
|
"""Represents a GPT partition map
|
|
|
|
"""
|
2013-09-18 00:46:58 +02:00
|
|
|
|
2014-01-19 01:02:29 +01:00
|
|
|
def __init__(self, data, bootloader):
|
2014-03-23 16:04:03 +01:00
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
data (dict): volume.partitions part of the manifest
|
|
|
|
bootloader (str): Name of the bootloader we will use for bootstrapping
|
|
|
|
"""
|
2014-03-23 23:12:07 +01:00
|
|
|
from bootstrapvz.common.bytes import Bytes
|
2014-03-23 16:04:03 +01:00
|
|
|
# List of partitions
|
2013-10-05 20:41:05 +02:00
|
|
|
self.partitions = []
|
2013-11-30 14:33:22 +01:00
|
|
|
|
2014-03-23 16:04:03 +01:00
|
|
|
# Returns the last partition unless there is none
|
2013-11-30 14:33:22 +01:00
|
|
|
def last_partition():
|
|
|
|
return self.partitions[-1] if len(self.partitions) > 0 else None
|
2014-01-19 01:02:29 +01:00
|
|
|
|
2014-03-23 16:04:03 +01:00
|
|
|
# GPT offset
|
2014-01-19 12:39:07 +01:00
|
|
|
gpt_offset = Bytes('17KiB')
|
|
|
|
|
2014-03-23 16:04:03 +01:00
|
|
|
# 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.
|
2014-01-19 01:02:29 +01:00
|
|
|
if bootloader == 'grub':
|
|
|
|
from ..partitions.unformatted import UnformattedPartition
|
2014-01-19 13:00:26 +01:00
|
|
|
self.grub_boot = UnformattedPartition(Bytes('1007KiB'), last_partition())
|
2014-01-25 13:56:28 +01:00
|
|
|
self.grub_boot.offset = gpt_offset
|
2014-03-23 16:04:03 +01:00
|
|
|
# Mark the partition as a bios_grub partition
|
2014-01-19 01:02:29 +01:00
|
|
|
self.grub_boot.flags.append('bios_grub')
|
|
|
|
self.partitions.append(self.grub_boot)
|
|
|
|
|
2014-03-23 16:04:03 +01:00
|
|
|
# The boot and swap partitions are optional
|
2013-09-18 00:46:58 +02:00
|
|
|
if 'boot' in data:
|
2014-02-23 17:52:05 +01:00
|
|
|
self.boot = GPTPartition(Bytes(data['boot']['size']),
|
|
|
|
data['boot']['filesystem'], data['boot'].get('format_command', None),
|
2014-01-19 12:39:07 +01:00
|
|
|
'boot', last_partition())
|
2013-10-05 20:41:05 +02:00
|
|
|
self.partitions.append(self.boot)
|
2013-09-18 00:46:58 +02:00
|
|
|
if 'swap' in data:
|
2014-01-19 12:39:07 +01:00
|
|
|
self.swap = GPTSwapPartition(Bytes(data['swap']['size']), last_partition())
|
2013-10-05 20:41:05 +02:00
|
|
|
self.partitions.append(self.swap)
|
2014-02-23 17:52:05 +01:00
|
|
|
self.root = GPTPartition(Bytes(data['root']['size']),
|
|
|
|
data['root']['filesystem'], data['root'].get('format_command', None),
|
2014-01-25 13:56:28 +01:00
|
|
|
'root', last_partition())
|
2013-11-30 14:33:22 +01:00
|
|
|
self.partitions.append(self.root)
|
2013-09-18 00:46:58 +02:00
|
|
|
|
2014-03-23 16:04:03 +01:00
|
|
|
# Depending on whether we have a grub boot partition
|
|
|
|
# we will need to set the offset accordingly.
|
2014-01-19 12:39:07 +01:00
|
|
|
if hasattr(self, 'grub_boot'):
|
2014-01-25 13:56:28 +01:00
|
|
|
self.partitions[1].size -= gpt_offset
|
2014-01-19 12:39:07 +01:00
|
|
|
self.partitions[1].size -= self.grub_boot.size
|
2014-01-25 13:56:28 +01:00
|
|
|
else:
|
|
|
|
self.partitions[0].offset = gpt_offset
|
|
|
|
self.partitions[0].size -= gpt_offset
|
2014-01-19 01:02:29 +01:00
|
|
|
|
|
|
|
super(GPTPartitionMap, self).__init__(bootloader)
|
2013-09-22 21:20:09 +02:00
|
|
|
|
2013-09-25 23:27:31 +02:00
|
|
|
def _before_create(self, event):
|
2014-03-23 16:04:03 +01:00
|
|
|
"""Creates the partition map
|
|
|
|
"""
|
2013-09-25 23:27:31 +02:00
|
|
|
volume = event.volume
|
2014-03-23 16:04:03 +01:00
|
|
|
# Disk alignment still plays a role in virtualized environment,
|
|
|
|
# but I honestly have no clue as to what best practice is here, so we choose 'none'
|
2014-02-23 22:16:10 +01:00
|
|
|
log_check_call(['parted', '--script', '--align', 'none', volume.device_path,
|
2013-09-18 00:46:58 +02:00
|
|
|
'--', 'mklabel', 'gpt'])
|
2014-03-23 16:04:03 +01:00
|
|
|
# Create the partitions
|
2013-09-18 00:46:58 +02:00
|
|
|
for partition in self.partitions:
|
|
|
|
partition.create(volume)
|