From 6e145f6aca47801ea8f9d849d4e8da27f5487b52 Mon Sep 17 00:00:00 2001 From: Anders Ingemann Date: Sun, 23 Feb 2014 17:52:05 +0100 Subject: [PATCH] Introduce format_command. Fixes #7 --- base/fs/partitionmaps/gpt.py | 6 ++++-- base/fs/partitionmaps/msdos.py | 8 ++++++-- base/fs/partitionmaps/none.py | 3 ++- base/fs/partitions/abstract.py | 23 ++++++++++++++++------- base/fs/partitions/base.py | 4 ++-- base/fs/partitions/gpt.py | 4 ++-- base/fs/partitions/gpt_swap.py | 2 +- base/fs/partitions/msdos_swap.py | 2 +- base/fs/partitions/unformatted.py | 2 +- base/manifest-schema.json | 7 ++++++- 10 files changed, 41 insertions(+), 20 deletions(-) diff --git a/base/fs/partitionmaps/gpt.py b/base/fs/partitionmaps/gpt.py index 2502288..12f3383 100644 --- a/base/fs/partitionmaps/gpt.py +++ b/base/fs/partitionmaps/gpt.py @@ -23,13 +23,15 @@ class GPTPartitionMap(AbstractPartitionMap): self.partitions.append(self.grub_boot) if 'boot' in data: - self.boot = GPTPartition(Bytes(data['boot']['size']), data['boot']['filesystem'], + self.boot = GPTPartition(Bytes(data['boot']['size']), + data['boot']['filesystem'], data['boot'].get('format_command', None), 'boot', last_partition()) self.partitions.append(self.boot) if 'swap' in data: self.swap = GPTSwapPartition(Bytes(data['swap']['size']), last_partition()) self.partitions.append(self.swap) - self.root = GPTPartition(Bytes(data['root']['size']), data['root']['filesystem'], + self.root = GPTPartition(Bytes(data['root']['size']), + data['root']['filesystem'], data['root'].get('format_command', None), 'root', last_partition()) self.partitions.append(self.root) diff --git a/base/fs/partitionmaps/msdos.py b/base/fs/partitionmaps/msdos.py index ab71978..988178a 100644 --- a/base/fs/partitionmaps/msdos.py +++ b/base/fs/partitionmaps/msdos.py @@ -14,12 +14,16 @@ class MSDOSPartitionMap(AbstractPartitionMap): return self.partitions[-1] if len(self.partitions) > 0 else None if 'boot' in data: - self.boot = MSDOSPartition(Bytes(data['boot']['size']), data['boot']['filesystem'], None) + self.boot = MSDOSPartition(Bytes(data['boot']['size']), + data['boot']['filesystem'], data['boot'].get('format_command', None), + last_partition()) self.partitions.append(self.boot) if 'swap' in data: self.swap = MSDOSSwapPartition(Bytes(data['swap']['size']), last_partition()) self.partitions.append(self.swap) - self.root = MSDOSPartition(Bytes(data['root']['size']), data['root']['filesystem'], last_partition()) + self.root = MSDOSPartition(Bytes(data['root']['size']), + data['root']['filesystem'], data['root'].get('format_command', None), + last_partition()) self.partitions.append(self.root) getattr(self, 'boot', self.root).flags.append('boot') diff --git a/base/fs/partitionmaps/none.py b/base/fs/partitionmaps/none.py index 8fc7650..550cbf5 100644 --- a/base/fs/partitionmaps/none.py +++ b/base/fs/partitionmaps/none.py @@ -5,7 +5,8 @@ class NoPartitions(object): def __init__(self, data, bootloader): from common.bytes import Bytes - self.root = SinglePartition(Bytes(data['root']['size']), data['root']['filesystem']) + self.root = SinglePartition(Bytes(data['root']['size']), + data['root']['filesystem'], data['root'].get('format_command', None)) self.partitions = [self.root] def is_blocking(self): diff --git a/base/fs/partitions/abstract.py b/base/fs/partitions/abstract.py index 5684561..f987bfb 100644 --- a/base/fs/partitions/abstract.py +++ b/base/fs/partitions/abstract.py @@ -36,11 +36,12 @@ class AbstractPartition(FSMProxy): log_check_call(['/bin/umount', self.mount_dir]) del self.mount_dir - def __init__(self, size, filesystem): - self.size = size - self.filesystem = filesystem - self.device_path = None - self.mounts = {} + def __init__(self, size, filesystem, format_command): + self.size = size + self.filesystem = filesystem + self.format_command = format_command + self.device_path = None + self.mounts = {} cfg = {'initial': 'nonexistent', 'events': self.events, 'callbacks': {}} super(AbstractPartition, self).__init__(cfg) @@ -57,8 +58,16 @@ class AbstractPartition(FSMProxy): return self.get_start() + self.size def _before_format(self, e): - mkfs = '/sbin/mkfs.{fs}'.format(fs=self.filesystem) - log_check_call([mkfs, self.device_path]) + if self.format_command is None: + format_command = ['/sbin/mkfs.{fs}', '{device_path}'] + else: + format_command = self.format_command + variables = {'fs': self.filesystem, + 'device_path': self.device_path, + 'size': self.size, + } + command = map(lambda part: part.format(**variables), format_command) + log_check_call(command) def _before_mount(self, e): log_check_call(['/bin/mount', '--types', self.filesystem, self.device_path, e.destination]) diff --git a/base/fs/partitions/base.py b/base/fs/partitions/base.py index 216479e..78b7e74 100644 --- a/base/fs/partitions/base.py +++ b/base/fs/partitions/base.py @@ -14,12 +14,12 @@ class BasePartition(AbstractPartition): {'name': 'unmap', 'src': 'mapped', 'dst': 'unmapped'}, ] - def __init__(self, size, filesystem, previous): + def __init__(self, size, filesystem, format_command, previous): self.previous = previous from common.bytes import Bytes self.offset = Bytes(0) self.flags = [] - super(BasePartition, self).__init__(size, filesystem) + super(BasePartition, self).__init__(size, filesystem, format_command) def create(self, volume): self.fsm.create(volume=volume) diff --git a/base/fs/partitions/gpt.py b/base/fs/partitions/gpt.py index aed7f2f..52996ab 100644 --- a/base/fs/partitions/gpt.py +++ b/base/fs/partitions/gpt.py @@ -4,9 +4,9 @@ from base import BasePartition class GPTPartition(BasePartition): - def __init__(self, size, filesystem, name, previous): + def __init__(self, size, filesystem, format_command, name, previous): self.name = name - super(GPTPartition, self).__init__(size, filesystem, previous) + super(GPTPartition, self).__init__(size, filesystem, format_command, previous) def _before_create(self, e): super(GPTPartition, self)._before_create(e) diff --git a/base/fs/partitions/gpt_swap.py b/base/fs/partitions/gpt_swap.py index 0217770..4af9245 100644 --- a/base/fs/partitions/gpt_swap.py +++ b/base/fs/partitions/gpt_swap.py @@ -5,7 +5,7 @@ from gpt import GPTPartition class GPTSwapPartition(GPTPartition): def __init__(self, size, previous): - super(GPTSwapPartition, self).__init__(size, 'swap', 'swap', previous) + super(GPTSwapPartition, self).__init__(size, 'swap', None, 'swap', previous) def _before_format(self, e): log_check_call(['/sbin/mkswap', self.device_path]) diff --git a/base/fs/partitions/msdos_swap.py b/base/fs/partitions/msdos_swap.py index 4b1d1dc..f4e0339 100644 --- a/base/fs/partitions/msdos_swap.py +++ b/base/fs/partitions/msdos_swap.py @@ -5,7 +5,7 @@ from msdos import MSDOSPartition class MSDOSSwapPartition(MSDOSPartition): def __init__(self, size, previous): - super(MSDOSSwapPartition, self).__init__(size, 'swap', previous) + super(MSDOSSwapPartition, self).__init__(size, 'swap', None, previous) def _before_format(self, e): log_check_call(['/sbin/mkswap', self.device_path]) diff --git a/base/fs/partitions/unformatted.py b/base/fs/partitions/unformatted.py index bbbc357..bb8e343 100644 --- a/base/fs/partitions/unformatted.py +++ b/base/fs/partitions/unformatted.py @@ -9,4 +9,4 @@ class UnformattedPartition(BasePartition): ] def __init__(self, size, previous): - super(UnformattedPartition, self).__init__(size, None, previous) + super(UnformattedPartition, self).__init__(size, None, None, previous) diff --git a/base/manifest-schema.json b/base/manifest-schema.json index 17dc233..f4d3d43 100644 --- a/base/manifest-schema.json +++ b/base/manifest-schema.json @@ -141,7 +141,12 @@ "type": "object", "properties": { "size": { "$ref": "#/definitions/bytes" }, - "filesystem": { "enum": ["ext2", "ext3", "ext4", "xfs"] } + "filesystem": { "enum": ["ext2", "ext3", "ext4", "xfs"] }, + "format_command": { + "type": "array", + "items": {"type": "string"}, + "minItems": 1 + } }, "required": ["size", "filesystem"] }