Skip to content

Commit

Permalink
Merge pull request #222 from desultory/dev
Browse files Browse the repository at this point in the history
stop if mounts are detected in the build dir
  • Loading branch information
desultory authored Feb 18, 2025
2 parents 20b02fc + 795b8d0 commit 927c509
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
9 changes: 8 additions & 1 deletion src/ugrd/base/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,16 @@ def get_shell(self) -> None:

@contains("clean", "Skipping cleaning build directory", log_level=30)
def clean_build_dir(self) -> None:
"""Cleans the build directory."""
"""Cleans the build directory.
Ensures there are no active mounts in the build directory.
"""
build_dir = self._get_build_path("/")

if any(mount.startswith(str(build_dir)) for mount in self["_mounts"]):
self.logger.critical("Mount detected in build directory, stopping: %s" % build_dir)
self.logger.critical("Active mounts: %s" % self["_mounts"])
exit(1)

if build_dir.is_dir():
self.logger.warning("Cleaning build directory: %s" % colorize(build_dir, "yellow"))
rmtree(build_dir)
Expand Down
2 changes: 1 addition & 1 deletion src/ugrd/base/test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ test_rootfs_name = 'ugrd-test-rootfs'
test_rootfs_build_dir = 'initramfs_test_rootfs'
test_image_size = 16

test_copy_config = ["mounts", "out_dir", "tmpdir", "clean", "test_image_size", "test_flag", "cryptsetup"]
test_copy_config = ["_mounts", "mounts", "out_dir", "tmpdir", "clean", "test_image_size", "test_flag", "cryptsetup"]

test_memory = '256M'
test_cpu = 'host'
Expand Down
14 changes: 8 additions & 6 deletions src/ugrd/fs/mounts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__author__ = "desultory"
__version__ = "6.6.3"
__version__ = "6.6.4"

from pathlib import Path
from re import search
Expand Down Expand Up @@ -327,13 +327,15 @@ def umount_fstab(self) -> list[str]:
return out


@contains("hostonly", "Skipping mount autodetection, hostonly mode is disabled.", log_level=30)
def get_mounts_info(self) -> None:
"""Gets the mount info for all devices."""
with open("/proc/mounts", "r") as mounts:
for line in mounts:
device, mountpoint, fstype, options, _, _ = line.split()
self["_mounts"][mountpoint] = {"device": device, "fstype": fstype, "options": options.split(",")}
try:
with open("/proc/mounts", "r") as mounts:
for line in mounts:
device, mountpoint, fstype, options, _, _ = line.split()
self["_mounts"][mountpoint] = {"device": device, "fstype": fstype, "options": options.split(",")}
except FileNotFoundError:
self.logger.critical("Failed to get mount info, detection and validation may fail!!!")

self.logger.debug("Mount info: %s" % pretty_print(self["_mounts"]))

Expand Down
1 change: 1 addition & 0 deletions src/ugrd/fs/test_image.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ _cryptsetup_root = "root"

[custom_parameters]
shebang = "str" # Add the shebang property, because the test mode disables the base module
_mounts = "dict" # For build dir check
mounts = "dict" # We only need the mounts dict, not the whole module
cryptsetup = "dict" # Same as above
_cryptsetup_root = "str" # Define the root device for cryptsetup
Expand Down

0 comments on commit 927c509

Please sign in to comment.