Skip to content

Commit 7f1d397

Browse files
committed
improve init mount autodetection
Signed-off-by: Zen <[email protected]>
1 parent 455b4a6 commit 7f1d397

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

src/ugrd/fs/mounts.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
__author__ = "desultory"
2-
__version__ = "6.4.0"
2+
__version__ = "6.5.0"
33

44
from pathlib import Path
55
from typing import Union
@@ -55,7 +55,7 @@ def _resolve_dev(self, device_path) -> str:
5555

5656

5757
def _find_mountpoint(self, path: str) -> str:
58-
""" Finds the mountpoint of a file or directory,
58+
"""Finds the mountpoint of a file or directory,
5959
Checks if the parent dir is a mountpoint, if not, recursively checks the parent dir."""
6060
check_path = Path(path).resolve()
6161
parent = check_path.parent if not check_path.is_dir() else check_path
@@ -341,32 +341,34 @@ def get_blkid_info(self, device=None) -> dict:
341341

342342

343343
@contains("init_target", "init_target must be set", raise_exception=True)
344-
@contains(
345-
"autodetect_init_mount", "Skipping init mount autodetection, autodetect_init_mount is disabled.", log_level=30
346-
)
344+
@contains("autodetect_init_mount", "Init mount autodetection disabled, skipping.", log_level=30)
347345
@contains("hostonly", "Skipping init mount autodetection, hostonly mode is disabled.", log_level=30)
348-
def autodetect_init_mount(self, parent=None) -> None:
346+
def autodetect_init_mount(self) -> None:
349347
"""Checks the parent directories of init_target, if the path is a mountpoint, add it to late_mounts."""
350-
if not parent:
351-
parent = self["init_target"].parent
352-
if parent == Path("/"):
348+
init_mount = _find_mountpoint(self, self["init_target"])
349+
if init_mount == "/":
353350
return
354-
if str(parent) in self["_mounts"]:
355-
self.logger.info("Detected init mount: %s" % colorize(parent, "cyan"))
356-
mount_name = str(parent).removeprefix("/")
357-
mount_dest = str(parent)
358-
mount_device = self["_mounts"][str(parent)]["device"]
359-
mount_type = self["_mounts"][str(parent)]["fstype"]
360-
mount_options = self["_mounts"][str(parent)]["options"]
361-
blkid_info = self["_blkid_info"][mount_device]
362-
mount_source_type, mount_source = _get_mount_source_type(self, blkid_info, with_val=True)
363-
self["late_mounts"][mount_name] = {
364-
"destination": mount_dest,
365-
mount_source_type: mount_source,
366-
"type": mount_type,
367-
"options": mount_options,
368-
}
369-
autodetect_init_mount(self, parent.parent)
351+
352+
if init_mount in self["late_mounts"]:
353+
return self.logger.debug("Init mount already detected: %s" % init_mount)
354+
355+
if init_mount not in self["_mounts"]:
356+
raise AutodetectError("Init mount not found in host mounts: %s" % init_mount)
357+
358+
self.logger.info("Detected init mount: %s" % colorize(init_mount, "cyan"))
359+
mount_name = init_mount.removeprefix("/")
360+
mount_dest = init_mount
361+
mount_device = self["_mounts"][init_mount]["device"]
362+
mount_type = self["_mounts"][init_mount]["fstype"]
363+
mount_options = self["_mounts"][init_mount]["options"]
364+
blkid_info = self["_blkid_info"][mount_device]
365+
mount_source_type, mount_source = _get_mount_source_type(self, blkid_info, with_val=True)
366+
self["late_mounts"][mount_name] = {
367+
"destination": mount_dest,
368+
mount_source_type: mount_source,
369+
"type": mount_type,
370+
"options": mount_options,
371+
}
370372

371373

372374
@contains("hostonly", "Skipping virtual block device enumeration, hostonly mode is disabled.", log_level=30)

0 commit comments

Comments
 (0)