2013-09-18 00:46:58 +02:00
|
|
|
from abstract import AbstractPartitionMap
|
2014-01-18 21:23:54 +00:00
|
|
|
from ..partitions.msdos import MSDOSPartition
|
|
|
|
from ..partitions.msdos_swap import MSDOSSwapPartition
|
2014-03-23 23:12:07 +01:00
|
|
|
from bootstrapvz.common.tools import log_check_call
|
2013-09-18 00:46:58 +02:00
|
|
|
|
|
|
|
|
2014-01-18 21:23:54 +00:00
|
|
|
class MSDOSPartitionMap(AbstractPartitionMap):
|
2014-03-23 16:04:03 +01:00
|
|
|
"""Represents a MS-DOS partition map
|
|
|
|
Sometimes also called MBR (but that confuses the hell out of me, so ms-dos it is)
|
|
|
|
"""
|
2013-09-18 00:46:58 +02:00
|
|
|
|
2015-01-01 21:09:16 +01:00
|
|
|
def __init__(self, data, sector_size, bootloader):
|
2014-03-23 16:04:03 +01:00
|
|
|
"""
|
2014-05-04 19:31:53 +02:00
|
|
|
:param dict data: volume.partitions part of the manifest
|
2015-01-01 21:09:16 +01:00
|
|
|
:param int sector_size: Sectorsize of the volume
|
2014-05-04 19:31:53 +02:00
|
|
|
:param str bootloader: Name of the bootloader we will use for bootstrapping
|
2014-03-23 16:04:03 +01:00
|
|
|
"""
|
2015-01-01 21:09:16 +01:00
|
|
|
from bootstrapvz.common.sectors import Sectors
|
|
|
|
|
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 12:39:07 +01:00
|
|
|
|
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:
|
2015-01-01 21:09:16 +01:00
|
|
|
self.boot = MSDOSPartition(Sectors(data['boot']['size'], sector_size),
|
2014-02-23 17:52:05 +01:00
|
|
|
data['boot']['filesystem'], data['boot'].get('format_command', None),
|
|
|
|
last_partition())
|
2013-10-05 20:41:05 +02:00
|
|
|
self.partitions.append(self.boot)
|
2015-01-19 23:11:19 +01:00
|
|
|
|
2013-09-18 00:46:58 +02:00
|
|
|
if 'swap' in data:
|
2015-01-01 21:09:16 +01:00
|
|
|
self.swap = MSDOSSwapPartition(Sectors(data['swap']['size'], sector_size), last_partition())
|
2013-10-05 20:41:05 +02:00
|
|
|
self.partitions.append(self.swap)
|
2015-01-19 23:11:19 +01:00
|
|
|
|
2015-01-01 21:09:16 +01:00
|
|
|
self.root = MSDOSPartition(Sectors(data['root']['size'], sector_size),
|
2014-02-23 17:52:05 +01:00
|
|
|
data['root']['filesystem'], data['root'].get('format_command', None),
|
|
|
|
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
|
|
|
# Mark boot as the boot partition, or root, if boot does not exist
|
2014-01-19 01:02:29 +01:00
|
|
|
getattr(self, 'boot', self.root).flags.append('boot')
|
|
|
|
|
2014-05-04 12:16:25 +02:00
|
|
|
# If we are using the grub bootloader, we will need to add a 2 MB offset
|
|
|
|
# at the beginning of the partitionmap and steal it from the first partition.
|
|
|
|
# The MBR offset is included in the grub offset, so if we don't use grub
|
|
|
|
# we should reduce the size of the first partition and move it by only 512 bytes.
|
2014-01-19 01:02:29 +01:00
|
|
|
if bootloader == 'grub':
|
2015-01-19 23:11:19 +01:00
|
|
|
mbr_offset = Sectors('2MiB', sector_size)
|
2014-05-04 12:16:25 +02:00
|
|
|
else:
|
2015-01-19 23:11:19 +01:00
|
|
|
mbr_offset = Sectors('512B', sector_size)
|
2014-05-04 12:16:25 +02:00
|
|
|
|
2015-01-19 23:11:19 +01:00
|
|
|
self.partitions[0].pad_start += mbr_offset
|
|
|
|
self.partitions[0].size -= mbr_offset
|
2014-01-19 12:39:07 +01:00
|
|
|
|
2015-01-01 21:09:16 +01:00
|
|
|
# Leave the last sector unformatted
|
2015-01-19 23:11:19 +01:00
|
|
|
# parted in jessie thinks that a partition 10 sectors in size
|
|
|
|
# goes from sector 0 to sector 9 (instead of 0 to 10)
|
|
|
|
self.partitions[-1].pad_end += 1
|
2015-01-01 21:09:16 +01:00
|
|
|
self.partitions[-1].size -= 1
|
|
|
|
|
2014-01-19 01:02:29 +01:00
|
|
|
super(MSDOSPartitionMap, self).__init__(bootloader)
|
2013-09-22 21:20:09 +02:00
|
|
|
|
|
|
|
def _before_create(self, event):
|
|
|
|
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', 'msdos'])
|
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)
|