From: Michael Tremer Date: Sat, 8 May 2021 14:56:59 +0000 (+0000) Subject: Mount/umount filesystems X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4f78c42ff0f42f3691dbd6361d84b4df476850cc;p=people%2Fms%2Fbricklayer.git Mount/umount filesystems Signed-off-by: Michael Tremer --- diff --git a/src/python/__init__.py b/src/python/__init__.py index 59d340f..b715fd9 100644 --- a/src/python/__init__.py +++ b/src/python/__init__.py @@ -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() diff --git a/src/python/disk.py b/src/python/disk.py index ee82158..f8ba461 100644 --- a/src/python/disk.py +++ b/src/python/disk.py @@ -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()