builder: lib: context.py: add mount and umount

Signed-off-by: BigfootACA <bigfoot@classfun.cn>
This commit is contained in:
BigfootACA 2024-07-21 17:19:38 +08:00
parent f6a1c73125
commit 38447f1dd7
2 changed files with 31 additions and 22 deletions

View File

@ -1,7 +1,7 @@
import os import os
from logging import getLogger from logging import getLogger
from builder.lib.context import ArchBuilderContext from builder.lib.context import ArchBuilderContext
from builder.lib.mount import MountTab, MountPoint from builder.lib.mount import MountTab
log = getLogger(__name__) log = getLogger(__name__)
@ -49,25 +49,6 @@ def undo_mounts(ctx: ArchBuilderContext):
raise RuntimeError("mount points not cleanup") 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): def init_mount(ctx: ArchBuilderContext):
""" """
Setup mount points for rootfs Setup mount points for rootfs
@ -80,7 +61,7 @@ def init_mount(ctx: ArchBuilderContext):
os.symlink(target, real) os.symlink(target, real)
def root_mount(source, target, fstype, options): def root_mount(source, target, fstype, options):
real = os.path.realpath(os.path.join(root, target)) real = os.path.realpath(os.path.join(root, target))
do_mount(ctx, source, real, fstype, options) ctx.mount(source, real, fstype, options)
try: try:
# ensure mount point is clean # ensure mount point is clean
mnts = MountTab.parse_mounts() mnts = MountTab.parse_mounts()

View File

@ -7,7 +7,7 @@ from builder.lib.cpu import cpu_arch_get
from builder.lib.utils import parse_cmd_args from builder.lib.utils import parse_cmd_args
from builder.lib.subscript import dict_get from builder.lib.subscript import dict_get
from builder.lib.loop import loop_detach 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.cgroup import CGroup
from builder.lib.subscript import SubScript from builder.lib.subscript import SubScript
from builder.lib.shadow import PasswdFile, GroupFile from builder.lib.shadow import PasswdFile, GroupFile
@ -189,3 +189,31 @@ class ArchBuilderContext:
ss = SubScript() ss = SubScript()
self.config = deepcopy(self.config_orig) self.config = deepcopy(self.config_orig)
ss.parse(self.config) 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)