import logging
import subprocess
import sys
+import tempfile
import traceback
from . import disk
# 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")
# Go!
disk.CreatePartitionLayout,
disk.CreateFilesystems,
+ disk.MountFilesystems,
# Done!
+ disk.UmountFilesystems,
step.Congratulations,
)
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
"""
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()
# 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()
_("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()