]> git.ipfire.org Git - people/ms/bricklayer.git/commitdiff
disk: Add step that creates filesystems
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 May 2021 13:14:41 +0000 (13:14 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 May 2021 13:14:41 +0000 (13:14 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/python/__init__.py
src/python/disk.py

index 4c09d38f63fcd6662fff92aa3f167c2601d619d5..38661f7cdc5167536d226e5c4b3aecae6d51ce61 100644 (file)
@@ -80,6 +80,7 @@ class Bricklayer(object):
 
                # Go!
                disk.CreatePartitionLayout,
+               disk.CreateFilesystems,
 
                # Done!
                step.Congratulations,
index a239b403d8e692fd9a32d5596b2110408d8c2866..e3e069fa2d591ebf043345cd9641ae9b8e5fe277 100644 (file)
@@ -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()