From 38447f1dd79f9e1f05e44511a9ce04f53bd0a7d0 Mon Sep 17 00:00:00 2001 From: BigfootACA Date: Sun, 21 Jul 2024 17:19:38 +0800 Subject: [PATCH] builder: lib: context.py: add mount and umount Signed-off-by: BigfootACA --- builder/build/mount.py | 23 ++--------------------- builder/lib/context.py | 30 +++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/builder/build/mount.py b/builder/build/mount.py index 6d44e1e..d533bf2 100644 --- a/builder/build/mount.py +++ b/builder/build/mount.py @@ -1,7 +1,7 @@ import os from logging import getLogger from builder.lib.context import ArchBuilderContext -from builder.lib.mount import MountTab, MountPoint +from builder.lib.mount import MountTab log = getLogger(__name__) @@ -49,25 +49,6 @@ def undo_mounts(ctx: ArchBuilderContext): raise RuntimeError("mount points not cleanup") -def do_mount( - ctx: ArchBuilderContext, - source: str, - target: str, - fstype: str, - options: str -): - """ - Add a mount point - """ - mnt = MountPoint() - mnt.source = source - mnt.target = target - mnt.fstype = fstype - mnt.options = options - mnt.mount() - ctx.mounted.insert(0, mnt) - - def init_mount(ctx: ArchBuilderContext): """ Setup mount points for rootfs @@ -80,7 +61,7 @@ def init_mount(ctx: ArchBuilderContext): os.symlink(target, real) def root_mount(source, target, fstype, options): real = os.path.realpath(os.path.join(root, target)) - do_mount(ctx, source, real, fstype, options) + ctx.mount(source, real, fstype, options) try: # ensure mount point is clean mnts = MountTab.parse_mounts() diff --git a/builder/lib/context.py b/builder/lib/context.py index 3d23ac5..7a32001 100644 --- a/builder/lib/context.py +++ b/builder/lib/context.py @@ -7,7 +7,7 @@ from builder.lib.cpu import cpu_arch_get from builder.lib.utils import parse_cmd_args from builder.lib.subscript import dict_get from builder.lib.loop import loop_detach -from builder.lib.mount import MountTab +from builder.lib.mount import MountTab, MountPoint from builder.lib.cgroup import CGroup from builder.lib.subscript import SubScript from builder.lib.shadow import PasswdFile, GroupFile @@ -189,3 +189,31 @@ class ArchBuilderContext: ss = SubScript() self.config = deepcopy(self.config_orig) ss.parse(self.config) + + def mount( + self, + source: str, + target: str, + fstype: str, + options: str, + ) -> MountPoint: + """ + Add a mount point + """ + mnt = MountPoint() + mnt.source = source + mnt.target = os.path.realpath(target) + mnt.fstype = fstype + mnt.options = options + mnt.mount() + self.mounted.insert(0, mnt) + return mnt + + def umount(self, path: str): + """ + Remove a mount point + """ + real = os.path.realpath(path) + if not os.path.ismount(real): return + for mnt in self.mounted.find_target(real): + self.mounted.remove(mnt)