]> git.ipfire.org Git - people/ms/bricklayer.git/commitdiff
disk: Write /etc/fstab to the installed system
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 29 Nov 2022 14:37:24 +0000 (14:37 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 29 Nov 2022 14:37:24 +0000 (14:37 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/python/__init__.py
src/python/disk.py

index 60a3300d9a0c48f070d0bcfcb665e58c446d196f..e92e8ac13b076eaa6795815d2927756555d0cb7c 100644 (file)
@@ -118,6 +118,7 @@ class Bricklayer(object):
                disk.MountFilesystems,
                packages.InstallPackages,
                step.SetRootPassword,
+               disk.WriteFilesystemTable,
                bootloaders.InstallBootloader,
 
                # Done!
index 916cce33f0c3cbff9672666f748b583397079ad9..99fdf181064e26f4d2e32d7b384f3265ab773d4a 100644 (file)
@@ -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()