]> git.ipfire.org Git - people/ms/bricklayer.git/commitdiff
Mount/umount filesystems
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 May 2021 14:56:59 +0000 (14:56 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 May 2021 14:57:45 +0000 (14:57 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/python/__init__.py
src/python/disk.py

index 59d340f28cbe2a012a09f1364ed60f10915ad8cc..b715fd9f8c41ee8cde728475baed96e5597d6300 100644 (file)
@@ -21,6 +21,7 @@
 import logging
 import subprocess
 import sys
+import tempfile
 import traceback
 
 from . import disk
@@ -66,6 +67,9 @@ class Bricklayer(object):
                # Initialise the text user interface
                self.tui = tui.Tui(self)
 
+               # Create root directory
+               self.root = tempfile.mkdtemp(prefix="bricklayer-", suffix="-root")
+
                # Log when we are ready
                if self.test:
                        log.info("Bricklayer initialized in test mode")
@@ -82,8 +86,10 @@ class Bricklayer(object):
                # Go!
                disk.CreatePartitionLayout,
                disk.CreateFilesystems,
+               disk.MountFilesystems,
 
                # Done!
+               disk.UmountFilesystems,
                step.Congratulations,
        )
 
@@ -165,7 +171,7 @@ class Bricklayer(object):
                with open("/etc/os-release") as f:
                        return util.config_read(f)
 
-       def command(self, command):
+       def command(self, command, error_ok=False):
                """
                        Runs a command in a shell environment
                """
@@ -175,7 +181,8 @@ class Bricklayer(object):
                p = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
 
                # Check the return code (raises CalledProcessError on non-zero)
-               p.check_returncode()
+               if not error_ok:
+                       p.check_returncode()
 
                # Decode output
                output = p.stdout.decode()
index ee821584f79644e8debb240102d303164389585e..f8ba461f10ab1456e114c61e6d374de7ed07b987 100644 (file)
@@ -121,10 +121,55 @@ class Disks(object):
                # Create one giant root partition
                root.create_system_partitions()
 
+       def _find_partition(self, name):
+               """
+                       Returns the partition with name
+               """
+               for disk in self.selected:
+                       for partition in disk.partitions:
+                               if partition.name == name:
+                                       return partition
+
+       def mount(self):
+               """
+                       Mounts all filesystems
+               """
+               # Find root partition
+               partition = self._find_partition("ROOT")
+               if not partition:
+                       FileNotFoundError("Could not find root partition")
+
+               # Mount the root partition
+               self._mount(partition.path, self.bricklayer.root)
+
+               # Find ESP partition
+               partition = self._find_partition("ESP")
+               if partition:
+                       self._mount(partition.path, os.path.join(self.bricklayer.root, "boot/efi"))
+
+       def _mount(self, source, target):
+               """
+                       Mounts source to target
+               """
+               # Make sure the target exists
+               os.makedirs(target, exist_ok=True)
+
+               # Call mount(8)
+               self.bricklayer.command(["mount", source, target])
+
+       def umount(self):
+               """
+                       Umounts all filesystems
+               """
+               # Umount everything mounted in root
+               self.bricklayer.command(["umount", "-Rv", self.bricklayer.root], error_ok=True)
+
        def tear_down(self):
                """
                        Shuts down any storage
                """
+               self.umount()
+
                for disk in self.disks:
                        disk.tear_down()
 
@@ -447,3 +492,27 @@ class CreateFilesystems(step.Step):
                                        _("Formatting partition \"%s\"...") % (partition.name or partition.path)
                                ):
                                        partition.format()
+
+
+class MountFilesystems(step.Step):
+       """
+               Mount all filesystems
+       """
+       def run(self, tui):
+               with tui.progress(
+                       _("Mounting Filesystems"),
+                       _("Mounting filesystems..."),
+               ):
+                       self.bricklayer.disks.mount()
+
+
+class UmountFilesystems(step.Step):
+       """
+               Umount all filesystems
+       """
+       def run(self, tui):
+               with tui.progress(
+                       _("Umounting Filesystems"),
+                       _("Umounting filesystems..."),
+               ):
+                       self.bricklayer.disks.umount()