File: msdos.py

package info (click to toggle)
bootstrap-vz 0.9.11%2B20180121git-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,244 kB
  • sloc: python: 8,800; sh: 813; makefile: 16
file content (94 lines) | stat: -rw-r--r-- 4,402 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from abstract import AbstractPartitionMap
from ..exceptions import PartitionError
from ..partitions.msdos import MSDOSPartition
from ..partitions.msdos_swap import MSDOSSwapPartition
from bootstrapvz.common.tools import log_check_call


class MSDOSPartitionMap(AbstractPartitionMap):
    """Represents a MS-DOS partition map
    Sometimes also called MBR (but that confuses the hell out of me, so ms-dos it is)
    """

    def __init__(self, data, sector_size, bootloader):
        """
        :param dict data: volume.partitions part of the manifest
        :param int sector_size: Sectorsize of the volume
        :param str bootloader: Name of the bootloader we will use for bootstrapping
        """
        from bootstrapvz.common.sectors import Sectors

        # List of partitions
        self.partitions = []

        # Returns the last partition unless there is none
        def last_partition():
            return self.partitions[-1] if len(self.partitions) > 0 else None

        # The boot and swap partitions are optional
        if 'boot' in data:
            self.boot = MSDOSPartition(Sectors(data['boot']['size'], sector_size),
                                       data['boot']['filesystem'], data['boot'].get('format_command', None),
                                       data['boot'].get('mountopts', None), 'boot', last_partition())
            self.partitions.append(self.boot)

        # Offset all partitions by 1 sector.
        # parted in jessie has changed and no longer allows
        # partitions to be right next to each other.
        partition_gap = Sectors(1, sector_size)

        if 'swap' in data:
            self.swap = MSDOSSwapPartition(Sectors(data['swap']['size'], sector_size), last_partition())
            if self.swap.previous is not None:
                # No need to pad if this is the first partition
                self.swap.pad_start += partition_gap
                self.swap.size -= partition_gap
            self.partitions.append(self.swap)

        self.root = MSDOSPartition(Sectors(data['root']['size'], sector_size),
                                   data['root']['filesystem'], data['root'].get('format_command', None),
                                   data['root'].get('mountopts', None), 'root', last_partition())
        if self.root.previous is not None:
            self.root.pad_start += partition_gap
            self.root.size -= partition_gap
        self.partitions.append(self.root)

        # Raise exception while trying to create additional partitions
        # as its hard to calculate the actual size of the extended partition ATM
        # And anyhow - we should go with GPT...
        for partition in data:
            if partition not in ["boot", "swap", "root", "type"]:
                raise PartitionError("If you want to have additional partitions please use GPT partition scheme")

        # Mark boot as the boot partition, or root, if boot does not exist
        getattr(self, 'boot', self.root).flags.append('boot')

        # 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.
        if bootloader == 'grub':
            mbr_offset = Sectors('2MiB', sector_size)
        else:
            mbr_offset = Sectors('512B', sector_size)

        self.partitions[0].pad_start += mbr_offset
        self.partitions[0].size -= mbr_offset

        # Leave the last sector unformatted
        # 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
        self.partitions[-1].size -= 1

        super(MSDOSPartitionMap, self).__init__(bootloader)

    def _before_create(self, event):
        volume = event.volume
        # 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'
        log_check_call(['parted', '--script', '--align', 'none', volume.device_path,
                        '--', 'mklabel', 'msdos'])
        # Create the partitions
        for partition in self.partitions:
            partition.create(volume)