Skip to content

Commit d0f603c

Browse files
authored
Merge pull request #321 from desultory/mount_logging
improve logging/error handling for mounts
2 parents 2861acf + e609b9c commit d0f603c

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/ugrd/fs/mounts.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
__author__ = "desultory"
2-
__version__ = "7.1.2"
2+
__version__ = "7.1.3"
33

44
from pathlib import Path
55
from re import search
@@ -130,13 +130,16 @@ def _get_mount_dev_fs_type(self, device: str, raise_exception=True) -> str:
130130
self.logger.debug("No mount found for device: %s" % device)
131131

132132

133-
def _get_mount_source_type(self, mount: dict, with_val=False) -> str:
134-
"""Gets the source from the mount config."""
133+
def _get_mount_source(self, mount: dict) -> str:
134+
"""Gets the source from the mount config.
135+
Uses the order of SOURCE_TYPES to determine the source type.
136+
uuid, partuuid, label, path.
137+
138+
Returns the source type and value if found, otherwise raises a ValueError.
139+
"""
135140
for source_type in SOURCE_TYPES:
136141
if source_type in mount:
137-
if with_val:
138-
return source_type, mount[source_type]
139-
return source_type
142+
return source_type, mount[source_type]
140143
raise ValueError("No source type found in mount: %s" % mount)
141144

142145

@@ -249,8 +252,8 @@ def _get_mount_str(self, mount: dict, pad=False, pad_size=44) -> str:
249252
"""returns the mount source string based on the config,
250253
the output string should work with fstab and mount commands.
251254
pad: pads the output string with spaces, defined by pad_size (44)."""
252-
mount_type, mount_name = _get_mount_source_type(self, mount, with_val=True)
253-
out_str = mount_name if mount_type == "path" else f"{mount_type.upper()}={mount_name}"
255+
mount_type, mount_val = _get_mount_source(self, mount)
256+
out_str = mount_val if mount_type == "path" else f"{mount_type.upper()}={mount_val}"
254257

255258
if pad:
256259
if len(out_str) > pad_size:
@@ -899,7 +902,7 @@ def _validate_host_mount(self, mount, destination_path=None) -> bool:
899902
if mount.get("base_mount"):
900903
return self.logger.debug("Skipping host mount validation for base mount: %s" % mount)
901904

902-
mount_type, mount_val = _get_mount_source_type(self, mount, with_val=True)
905+
mount_type, mount_val = _get_mount_source(self, mount)
903906
# If a destination path is passed, like for /, use that instead of the mount's destination
904907
destination_path = str(mount["destination"]) if destination_path is None else destination_path
905908

@@ -994,7 +997,14 @@ def mount_root(self) -> str:
994997

995998
def export_mount_info(self) -> None:
996999
"""Exports mount info based on the config to /run/MOUNTS_ROOT_{option}"""
997-
self["exports"]["MOUNTS_ROOT_SOURCE"] = _get_mount_str(self, self["mounts"]["root"])
1000+
try:
1001+
self["exports"]["MOUNTS_ROOT_SOURCE"] = _get_mount_str(self, self["mounts"]["root"])
1002+
except ValueError as e:
1003+
self.logger.critical(f"Failed to get source info for the root mount: {e}")
1004+
if not self["hostonly"]:
1005+
self.logger.info("Root mount infomrmation can be defined under the '[mounts.root]' section.")
1006+
raise ValidationError("Root mount source information is not set, when hostonly mode is disabled, it must be manually defined.")
1007+
raise ValidationError("Root mount source information is not set even though hostonly mode is enabled. Please report a bug.")
9981008
self["exports"]["MOUNTS_ROOT_TYPE"] = self["mounts"]["root"].get("type", "auto")
9991009
self["exports"]["MOUNTS_ROOT_OPTIONS"] = ",".join(self["mounts"]["root"]["options"])
10001010
self["exports"]["MOUNTS_ROOT_TARGET"] = self["mounts"]["root"]["destination"]

0 commit comments

Comments
 (0)