Skip to content

Commit

Permalink
fix: ensure mount point exists for binds
Browse files Browse the repository at this point in the history
  • Loading branch information
clay-lake committed Oct 17, 2024
1 parent 040ee1c commit fbc8ac7
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions craft_parts/overlays/chroot.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,19 @@ def _cleanup_chroot(path: Path, use_host_sources: bool) -> None:

class _Mount:
def __init__(
self, src: str | Path, mountpoint: str | Path, *args, fstype: str | None = None
self,
src: str | Path,
mountpoint: str | Path,
*args,
fstype: str | None = None,
skip_missing: bool = True,
) -> None:
"""Mount entry for chroot setup."""

self.src = Path(src)
self.mountpoint = Path(mountpoint)
self.args = list(args)
self.skip_missing = skip_missing

if fstype is not None:
self.args.append(f"-t{fstype}")
Expand All @@ -171,7 +177,7 @@ def _umount(self, mountpoint: Path, *args: str) -> None:
def mount_to(self, chroot: Path, *args: str) -> None:
abs_mountpoint = self.get_abs_path(chroot, self.mountpoint)

if not self.mountpoint_exists(abs_mountpoint):
if self.skip_missing and not self.mountpoint_exists(abs_mountpoint):
logger.warning("[pid=%d] mount: %r not found!", os.getpid(), abs_mountpoint)
return

Expand All @@ -180,7 +186,7 @@ def mount_to(self, chroot: Path, *args: str) -> None:
def unmount_from(self, chroot: Path, *args: str) -> None:
abs_mountpoint = self.get_abs_path(chroot, self.mountpoint)

if not self.mountpoint_exists(abs_mountpoint):
if self.skip_missing and not self.mountpoint_exists(abs_mountpoint):
logger.warning("[pid=%d] umount: %r not found!", os.getpid(), chroot)
return

Expand All @@ -195,8 +201,11 @@ def __init__(
src: str | Path,
mountpoint: str | Path,
*args: str,
skip_missing: bool = True,
) -> None:
super().__init__(src, mountpoint, f"--{self.bind_type}", *args)
super().__init__(
src, mountpoint, f"--{self.bind_type}", *args, skip_missing=skip_missing
)

def mountpoint_exists(self, mountpoint: Path):
if self.src.exists() and self.src.is_file():
Expand All @@ -211,14 +220,14 @@ def _mount(self, src: Path, mountpoint: Path, *args: str) -> None:
rmtree(mountpoint)

# prep mount point
mountpoint.mkdir(exist_ok=True)
mountpoint.mkdir(parents=True, exist_ok=True)

elif src.is_file():
# remove existing file
if mountpoint.exists():
mountpoint.unlink()
else:
mountpoint.parent.mkdir(exist_ok=True)
mountpoint.parent.mkdir(parents=True, exist_ok=True)

# prep mount point
mountpoint.touch()
Expand All @@ -237,7 +246,7 @@ def _umount(self, mountpoint: Path, *args) -> None:

class _TempFSClone(_Mount):
def __init__(self, src: str, mountpoint: str, *args) -> None:
super().__init__(src, mountpoint, *args, fstype="tmpfs")
super().__init__(src, mountpoint, *args, fstype="tmpfs", skip_missing=False)

def _mount(self, src: Path, mountpoint: Path, *args) -> None:
if src.is_dir():
Expand Down Expand Up @@ -279,9 +288,15 @@ def _mount(self, src: Path, mountpoint: Path, *args) -> None:
# TODO: parameterize this per linux distribution / package manager
_ubuntu_apt_mounts = [
_TempFSClone("/etc/apt", "/etc/apt"),
_BindMount("/usr/share/ca-certificates/", "/usr/share/ca-certificates/"),
_BindMount("/etc/ssl/certs/", "/etc/ssl/certs/"),
_BindMount("/etc/ca-certificates.conf", "/etc/ca-certificates.conf"),
_BindMount(
"/usr/share/ca-certificates/",
"/usr/share/ca-certificates/",
skip_missing=False,
),
_BindMount("/etc/ssl/certs/", "/etc/ssl/certs/", skip_missing=False),
_BindMount(
"/etc/ca-certificates.conf", "/etc/ca-certificates.conf", skip_missing=False
),
]


Expand Down

0 comments on commit fbc8ac7

Please sign in to comment.