diff --git a/bootstrapvz/base/fs/partitionmaps/gpt.py b/bootstrapvz/base/fs/partitionmaps/gpt.py index c51571561..1dbd943db 100644 --- a/bootstrapvz/base/fs/partitionmaps/gpt.py +++ b/bootstrapvz/base/fs/partitionmaps/gpt.py @@ -41,7 +41,7 @@ def last_partition(): if 'boot' in data: self.boot = GPTPartition(Sectors(data['boot']['size'], sector_size), data['boot']['filesystem'], data['boot'].get('format_command', None), - 'boot', last_partition()) + data['boot'].get('mountopts', None), 'boot', last_partition()) if self.boot.previous is not None: # No need to pad if this is the first partition self.boot.pad_start += partition_gap @@ -57,12 +57,23 @@ def last_partition(): self.root = GPTPartition(Sectors(data['root']['size'], sector_size), data['root']['filesystem'], data['root'].get('format_command', None), - 'root', last_partition()) + 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) + # Create all additional partitions + for partition in data: + if partition not in ["boot", "swap", "root", "type"] and not None: + part_tmp = GPTPartition(Sectors(data[partition]['size'], sector_size), + data[partition]['filesystem'], data[partition].get('format_command', None), + data[partition].get('mountopts', None), partition, last_partition()) + part_tmp.pad_start += partition_gap + part_tmp.size -= partition_gap + setattr(self, partition, part_tmp) + self.partitions.append(part_tmp) + if hasattr(self, 'grub_boot'): # Mark the grub partition as a bios_grub partition self.grub_boot.flags.append('bios_grub') diff --git a/bootstrapvz/base/fs/partitionmaps/msdos.py b/bootstrapvz/base/fs/partitionmaps/msdos.py index 22e53fd8a..ebc04cc2e 100644 --- a/bootstrapvz/base/fs/partitionmaps/msdos.py +++ b/bootstrapvz/base/fs/partitionmaps/msdos.py @@ -1,4 +1,5 @@ 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 @@ -28,7 +29,7 @@ def last_partition(): if 'boot' in data: self.boot = MSDOSPartition(Sectors(data['boot']['size'], sector_size), data['boot']['filesystem'], data['boot'].get('format_command', None), - last_partition()) + data['boot'].get('mountopts', None), 'boot', last_partition()) self.partitions.append(self.boot) # Offset all partitions by 1 sector. @@ -46,12 +47,19 @@ def last_partition(): self.root = MSDOSPartition(Sectors(data['root']['size'], sector_size), data['root']['filesystem'], data['root'].get('format_command', None), - last_partition()) + 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') diff --git a/bootstrapvz/base/fs/partitionmaps/none.py b/bootstrapvz/base/fs/partitionmaps/none.py index e73495ba9..18346bc34 100644 --- a/bootstrapvz/base/fs/partitionmaps/none.py +++ b/bootstrapvz/base/fs/partitionmaps/none.py @@ -17,7 +17,7 @@ def __init__(self, data, sector_size, bootloader): # In the NoPartitions partitions map we only have a single 'partition' self.root = SinglePartition(Sectors(data['root']['size'], sector_size), - data['root']['filesystem'], data['root'].get('format_command', None)) + data['root']['filesystem'], data['root'].get('format_command', None), data['root'].get('mount_opts', None)) self.partitions = [self.root] def is_blocking(self): diff --git a/bootstrapvz/base/fs/partitions/abstract.py b/bootstrapvz/base/fs/partitions/abstract.py index d2f18cc35..cbbad80d7 100644 --- a/bootstrapvz/base/fs/partitions/abstract.py +++ b/bootstrapvz/base/fs/partitions/abstract.py @@ -19,7 +19,7 @@ class AbstractPartition(FSMProxy): {'name': 'unmount', 'src': 'mounted', 'dst': 'formatted'}, ] - def __init__(self, size, filesystem, format_command): + def __init__(self, size, filesystem, format_command, mountopts): """ :param Bytes size: Size of the partition :param str filesystem: Filesystem the partition should be formatted with @@ -28,6 +28,8 @@ def __init__(self, size, filesystem, format_command): self.size = size self.filesystem = filesystem self.format_command = format_command + # List of mount options + self.mountopts = mountopts # Initialize the start & end padding to 0 sectors, may be changed later self.pad_start = Sectors(0, size.sector_size) self.pad_end = Sectors(0, size.sector_size) @@ -80,7 +82,12 @@ def _before_format(self, e): def _before_mount(self, e): """Mount the partition """ - log_check_call(['mount', '--types', self.filesystem, self.device_path, e.destination]) + if self.mountopts is None: + mount_command = ['mount', '--types', self.filesystem, self.device_path, e.destination] + else: + mount_command = ['mount', '--options', ",".join(self.mountopts), '--types', self.filesystem, self.device_path, e.destination] + # Mount the partition + log_check_call(mount_command) self.mount_dir = e.destination def _after_mount(self, e): diff --git a/bootstrapvz/base/fs/partitions/base.py b/bootstrapvz/base/fs/partitions/base.py index 00d006368..76c8f3bcd 100644 --- a/bootstrapvz/base/fs/partitions/base.py +++ b/bootstrapvz/base/fs/partitions/base.py @@ -20,7 +20,7 @@ class BasePartition(AbstractPartition): {'name': 'unmap', 'src': 'mapped', 'dst': 'unmapped'}, ] - def __init__(self, size, filesystem, format_command, previous): + def __init__(self, size, filesystem, format_command, mountopts, previous): """ :param Bytes size: Size of the partition :param str filesystem: Filesystem the partition should be formatted with @@ -34,7 +34,7 @@ def __init__(self, size, filesystem, format_command, previous): self.flags = [] # Path to symlink in /dev/disk/by-uuid (manually maintained by this class) self.disk_by_uuid_path = None - super(BasePartition, self).__init__(size, filesystem, format_command) + super(BasePartition, self).__init__(size, filesystem, format_command, mountopts) def create(self, volume): """Creates the partition diff --git a/bootstrapvz/base/fs/partitions/gpt.py b/bootstrapvz/base/fs/partitions/gpt.py index c8b1f31a7..aa77b8ae4 100644 --- a/bootstrapvz/base/fs/partitions/gpt.py +++ b/bootstrapvz/base/fs/partitions/gpt.py @@ -6,7 +6,7 @@ class GPTPartition(BasePartition): """Represents a GPT partition """ - def __init__(self, size, filesystem, format_command, name, previous): + def __init__(self, size, filesystem, format_command, mountopts, name, previous): """ :param Bytes size: Size of the partition :param str filesystem: Filesystem the partition should be formatted with @@ -15,7 +15,7 @@ def __init__(self, size, filesystem, format_command, name, previous): :param BasePartition previous: The partition that preceeds this one """ self.name = name - super(GPTPartition, self).__init__(size, filesystem, format_command, previous) + super(GPTPartition, self).__init__(size, filesystem, format_command, mountopts, previous) def _before_create(self, e): # Create the partition and then set the name of the partition afterwards diff --git a/bootstrapvz/base/fs/partitions/gpt_swap.py b/bootstrapvz/base/fs/partitions/gpt_swap.py index 3dd2c5319..c7f7c28ed 100644 --- a/bootstrapvz/base/fs/partitions/gpt_swap.py +++ b/bootstrapvz/base/fs/partitions/gpt_swap.py @@ -11,7 +11,7 @@ def __init__(self, size, previous): :param Bytes size: Size of the partition :param BasePartition previous: The partition that preceeds this one """ - super(GPTSwapPartition, self).__init__(size, 'swap', None, 'swap', previous) + super(GPTSwapPartition, self).__init__(size, 'swap', None, None, 'swap', previous) def _before_format(self, e): log_check_call(['mkswap', self.device_path]) diff --git a/bootstrapvz/base/fs/partitions/msdos.py b/bootstrapvz/base/fs/partitions/msdos.py index ad1dd6d7f..90c0bf9f6 100644 --- a/bootstrapvz/base/fs/partitions/msdos.py +++ b/bootstrapvz/base/fs/partitions/msdos.py @@ -4,4 +4,13 @@ class MSDOSPartition(BasePartition): """Represents an MS-DOS partition """ - pass + def __init__(self, size, filesystem, format_command, mountopts, name, previous): + """ + :param Bytes size: Size of the partition + :param str filesystem: Filesystem the partition should be formatted with + :param list format_command: Optional format command, valid variables are fs, device_path and size + :param str name: The name of the partition + :param BasePartition previous: The partition that preceeds this one + """ + self.name = name + super(MSDOSPartition, self).__init__(size, filesystem, format_command, mountopts, previous) diff --git a/bootstrapvz/base/fs/partitions/msdos_swap.py b/bootstrapvz/base/fs/partitions/msdos_swap.py index 24c40d221..48afcfc37 100644 --- a/bootstrapvz/base/fs/partitions/msdos_swap.py +++ b/bootstrapvz/base/fs/partitions/msdos_swap.py @@ -11,7 +11,7 @@ def __init__(self, size, previous): :param Bytes size: Size of the partition :param BasePartition previous: The partition that preceeds this one """ - super(MSDOSSwapPartition, self).__init__(size, 'swap', None, previous) + super(MSDOSSwapPartition, self).__init__(size, 'swap', None, None, 'swap', previous) def _before_format(self, e): log_check_call(['mkswap', self.device_path]) diff --git a/bootstrapvz/base/fs/partitions/unformatted.py b/bootstrapvz/base/fs/partitions/unformatted.py index 73af32d1e..b71be9192 100644 --- a/bootstrapvz/base/fs/partitions/unformatted.py +++ b/bootstrapvz/base/fs/partitions/unformatted.py @@ -17,4 +17,4 @@ def __init__(self, size, previous): :param Bytes size: Size of the partition :param BasePartition previous: The partition that preceeds this one """ - super(UnformattedPartition, self).__init__(size, None, None, previous) + super(UnformattedPartition, self).__init__(size, None, None, None, previous) diff --git a/bootstrapvz/base/manifest-schema.yml b/bootstrapvz/base/manifest-schema.yml index bf3bd899c..170cc2ba0 100644 --- a/bootstrapvz/base/manifest-schema.yml +++ b/bootstrapvz/base/manifest-schema.yml @@ -153,7 +153,7 @@ definitions: properties: root: {$ref: '#/definitions/partition'} type: {enum: [none]} - required: [root] + required: [type, root] additionalProperties: false partition: type: object @@ -164,21 +164,25 @@ definitions: items: {type: string} minItems: 1 type: array + mountopts: + items: {type: string} + minItems: 1 + type: array size: {$ref: '#/definitions/bytes'} required: [size, filesystem] additionalProperties: false partition_table: type: object + patternProperties: + ^(?!type|swap).*: {$ref: '#/definitions/partition'} properties: - boot: {$ref: '#/definitions/partition'} - root: {$ref: '#/definitions/partition'} swap: type: object properties: size: {$ref: '#/definitions/bytes'} required: [size] type: {enum: [msdos, gpt]} - required: [root] + required: [type, root] additionalProperties: false path: type: string diff --git a/bootstrapvz/common/task_groups.py b/bootstrapvz/common/task_groups.py index e087d3373..89709dc5f 100644 --- a/bootstrapvz/common/task_groups.py +++ b/bootstrapvz/common/task_groups.py @@ -74,6 +74,7 @@ def get_base_group(manifest): mounting_group = [filesystem.CreateMountDir, filesystem.MountRoot, + filesystem.MountAdditional, filesystem.MountSpecials, filesystem.CopyMountTable, filesystem.RemoveMountTable, diff --git a/bootstrapvz/common/tasks/filesystem.py b/bootstrapvz/common/tasks/filesystem.py index 014554f79..4fb73931a 100644 --- a/bootstrapvz/common/tasks/filesystem.py +++ b/bootstrapvz/common/tasks/filesystem.py @@ -99,6 +99,33 @@ def run(cls, info): p_map.root.add_mount(p_map.boot, 'boot') +class MountAdditional(Task): + description = 'Mounting additional partitions' + phase = phases.volume_mounting + predecessors = [MountRoot] + + @classmethod + def run(cls, info): + import os + from bootstrapvz.base.fs.partitions.unformatted import UnformattedPartition + + def is_additional(partition): + return (not isinstance(partition, UnformattedPartition) and + partition.name not in ["boot", "swap", "root"]) + + p_map = info.volume.partition_map + partitions = p_map.partitions + for partition in sorted( + filter(is_additional, partitions), + key=lambda partition: len(partition.name)): + partition = getattr(p_map, partition.name) + os.makedirs(os.path.join(info.root, partition.name)) + if partition.mountopts is None: + p_map.root.add_mount(getattr(p_map, partition.name), partition.name) + else: + p_map.root.add_mount(getattr(p_map, partition.name), partition.name, ['--options'] + partition.mountopts) + + class MountSpecials(Task): description = 'Mounting special block devices' phase = phases.os_installation @@ -165,7 +192,14 @@ class FStab(Task): @classmethod def run(cls, info): import os.path + from bootstrapvz.base.fs.partitions.unformatted import UnformattedPartition + + def is_additional(partition): + return (not isinstance(partition, UnformattedPartition) and + partition.name not in ["boot", "swap", "root"]) + p_map = info.volume.partition_map + partitions = p_map.partitions mount_points = [{'path': '/', 'partition': p_map.root, 'dump': '1', @@ -184,10 +218,21 @@ def run(cls, info): 'pass_num': '0', }) + for partition in sorted( + filter(is_additional, partitions), + key=lambda partition: len(partition.name)): + mount_points.append({'path': "/" + partition.name, + 'partition': getattr(p_map, partition.name), + 'dump': '1', + 'pass_num': '2', + }) fstab_lines = [] for mount_point in mount_points: partition = mount_point['partition'] - mount_opts = ['defaults'] + if partition.mountopts is None: + mount_opts = ['defaults'] + else: + mount_opts = partition.mountopts fstab_lines.append('UUID={uuid} {mountpoint} {filesystem} {mount_opts} {dump} {pass_num}' .format(uuid=partition.get_uuid(), mountpoint=mount_point['path'], diff --git a/manifests/README.rst b/manifests/README.rst index d4dc26079..ce3973c44 100644 --- a/manifests/README.rst +++ b/manifests/README.rst @@ -302,17 +302,29 @@ boot, root and swap. - ``{fs}``: The filesystem of the partition. - ``{device_path}``: The device path of the partition. - ``{size}``: The size of the partition. + - ``{mount_opts}``: Options to mount the partition with. This optional + setting overwrites the default option list bootstrap-vz would + normally use to mount the partiton (defaults). The List is specified + as a string array where each option/argument is an item in that array. + ``optional`` Here some examples: + - ``nodev`` + - ``nosuid`` + - ``noexec`` + - ``journal_ioprio=3`` The default command used by boostrap-vz is ``['mkfs.{fs}', '{device_path}']``. - - ``boot``: Configuration of the boot partition. The three - settings equal those of the root partition. + - ``boot``: Configuration of the boot partition. All settings equal + those of the root partition. ``optional`` - ``swap``: Configuration of the swap partition. Since the swap partition has its own filesystem you can only specify the size for this partition. ``optional`` + - ``additional_path``: Configuration of additional partitions. (e.g. /var/tmp) + All settings equal those of the root partition. + Example: diff --git a/manifests/examples/kvm/stretch-virtio-partitions.yml b/manifests/examples/kvm/stretch-virtio-partitions.yml new file mode 100644 index 000000000..49330cf98 --- /dev/null +++ b/manifests/examples/kvm/stretch-virtio-partitions.yml @@ -0,0 +1,46 @@ +--- +name: debian-{system.release}-{system.architecture}-{%Y}{%m}{%d} +provider: + name: kvm + virtio_modules: + - virtio_pci + - virtio_blk +bootstrapper: + workspace: /target +system: + release: stretch + architecture: amd64 + bootloader: grub + charmap: UTF-8 + locale: en_US + timezone: UTC +volume: + backing: raw + partitions: + type: gpt + boot: + filesystem: ext2 + size: 1GiB + swap: + size: 128MiB + root: + filesystem: ext4 + size: 8GiB + tmp: + mountopts: + - nodev + - noexec + - nosuid + - journal_ioprio=3 + filesystem: ext4 + size: 1GiB + var: + filesystem: ext4 + size: 1GiB + var/tmp: + filesystem: ext4 + size: 1GiB + +plugins: + root_password: + password: test