From 142ee53b4fb1516384f6ce3a2659174b024767f0 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 29 Nov 2022 14:37:24 +0000 Subject: [PATCH] disk: Write /etc/fstab to the installed system Signed-off-by: Michael Tremer --- src/python/__init__.py | 1 + src/python/disk.py | 98 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 92 insertions(+), 7 deletions(-) diff --git a/src/python/__init__.py b/src/python/__init__.py index 60a3300..e92e8ac 100644 --- a/src/python/__init__.py +++ b/src/python/__init__.py @@ -118,6 +118,7 @@ class Bricklayer(object): disk.MountFilesystems, packages.InstallPackages, step.SetRootPassword, + disk.WriteFilesystemTable, bootloaders.InstallBootloader, # Done! diff --git a/src/python/disk.py b/src/python/disk.py index 916cce3..99fdf18 100644 --- a/src/python/disk.py +++ b/src/python/disk.py @@ -183,6 +183,27 @@ class Disks(object): for disk in self.disks: disk.tear_down() + def write_fstab(self): + """ + Writes the disk configuration to /etc/fstab + """ + path = os.path.join(self.bricklayer.root, "etc/fstab") + + with open(path, "w") as f: + for disk in self.selected: + for partition in disk.partitions: + e = partition.make_fstab_entry() + + # Do nothing if the entry is empty + if not e: + continue + + # Log the entry + log.debug(e) + + # Write it to the file + f.write(e) + class Disk(object): def __init__(self, bricklayer, device): @@ -381,6 +402,27 @@ class Partition(object): return self.parted.path + @property + def mountpoint(self): + """ + Returns the mountpoint for this partition (or None) + """ + if self.name == "ROOT": + return "/" + + elif self.name == "ESP": + return "/boot/efi" + + @property + def filesystem(self): + type = self.parted.fileSystem.type + + # SWAP + if type == "linux-swap(v1)": + return "swap" + + return type + def wipe(self): """ Wipes the entire partition (i.e. writes zeroes) @@ -398,21 +440,57 @@ class Partition(object): 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)) + log.info("Formatting %s (%s) with %s..." % \ + (self.name, self.path, self.filesystem)) - if filesystem == "fat32": + if self.filesystem == "fat32": command = ["mkfs.vfat", self.path] - elif filesystem == "linux-swap(v1)": + elif self.filesystem == "swap": command = ["mkswap", "-v1", self.path] else: - command = ["mkfs.%s" % filesystem, "-f", self.path] + command = ["mkfs.%s" % self.filesystem, "-f", self.path] # Run command self.bricklayer.command(command) + @property + def uuid(self): + """ + Returns the UUID of the filesystem + """ + uuid = self.bricklayer.command([ + "blkid", + + # Don't use the cache + "--probe", + + # Return the UUID only + "--match-tag", "UUID", + + # Only return the value + "--output", "value", + + # Operate on this device + self.path, + ]) + + # Remove the trailing newline + return uuid.rstrip() + + def make_fstab_entry(self): + """ + Returns a /etc/fstab entry for this partition + """ + # The bootloader partition does not get an entry + if self.name == "BOOTLDR": + return + + return "UUID=%s %s %s defaults 0 0\n" % ( + self.uuid, + self.mountpoint or "none", + self.filesystem, + ) + class Scan(step.Step): def run(self): @@ -552,4 +630,10 @@ class UmountFilesystems(step.Step): _("Umounting Filesystems"), _("Umounting filesystems..."), ): + # Umount everything self.bricklayer.disks.umount() + + +class WriteFilesystemTable(step.Step): + def run(self): + self.bricklayer.disks.write_fstab() -- 2.47.3