From: Michael Tremer Date: Sat, 8 May 2021 13:14:41 +0000 (+0000) Subject: disk: Add step that creates filesystems X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6aaa8c3253d02f132a87fa192b38d55c7815c736;p=people%2Fms%2Fbricklayer.git disk: Add step that creates filesystems Signed-off-by: Michael Tremer --- diff --git a/src/python/__init__.py b/src/python/__init__.py index 4c09d38..38661f7 100644 --- a/src/python/__init__.py +++ b/src/python/__init__.py @@ -80,6 +80,7 @@ class Bricklayer(object): # Go! disk.CreatePartitionLayout, + disk.CreateFilesystems, # Done! step.Congratulations, diff --git a/src/python/disk.py b/src/python/disk.py index a239b40..e3e069f 100644 --- a/src/python/disk.py +++ b/src/python/disk.py @@ -198,6 +198,13 @@ class Disk(object): def size(self): return self.device.length * self.device.sectorSize + @property + def partitions(self): + """ + Returns a list of all partitions on this device + """ + return [Partition(self.bricklayer, p) for p in self.parted.partitions] + def create_system_partitions(self): """ This method creates a basic partition layout on this disk with all @@ -269,6 +276,67 @@ class Disk(object): # Write the new partition table self.parted.commit() + # For loop devices, we have to manually create the partition mappings + if self.path.startswith("/dev/loop"): + self.bricklayer.command(["kpartx", "-av", self.path]) + + +class Partition(object): + def __init__(self, bricklayer, parted): + self.bricklayer = bricklayer + + # The parted device + self.parted = parted + + def __repr__(self): + return "<%s %s>" % (self.__class__.__name__, self.name or self.path) + + @property + def name(self): + return self.parted.name + + @property + def path(self): + # Map path for loop devices + if self.parted.path.startswith("/dev/loop"): + return self.parted.path.replace("/dev/loop", "/dev/mapper/loop") + + return self.parted.path + + def wipe(self): + """ + Wipes the entire partition (i.e. writes zeroes) + """ + log.info("Wiping %s (%s)..." % (self.name, self.path)) + + zero = bytearray(1024) + + with open(self.path, "wb") as f: + f.write(zero) + + def format(self): + """ + Formats the filesystem + """ + # Wipe BIOS_GRUB partitions instead of formatting them + if self.parted.getFlag(parted.PARTITION_BIOS_GRUB): + return self.wipe() + + # Fetch file-system type + filesystem = self.parted.fileSystem.type + + log.info("Formatting %s (%s) with %s..." % (self.name, self.path, filesystem)) + + if filesystem == "fat32": + command = ["mkfs.vfat", self.path] + elif filesystem == "linux-swap(v1)": + command = ["mkswap", "-v1", self.path] + else: + command = ["mkfs.%s" % filesystem, self.path] + + # Run command + self.bricklayer.command(command) + class SelectDisk(step.InteractiveStep): """ @@ -340,3 +408,13 @@ class CreatePartitionLayout(step.Step): for disk in self.bricklayer.disks.selected: disk.commit() + + +class CreateFilesystems(step.Step): + """ + Formats all newly created partitions + """ + def run(self, tui): + for disk in self.bricklayer.disks.selected: + for partition in disk.partitions: + partition.format()