From a601e76a937482fa4244f509857a5364124a6626 Mon Sep 17 00:00:00 2001 From: Enno Tensing Date: Tue, 29 Jul 2025 10:03:14 +0200 Subject: [PATCH] generate: container: Enable usage of host files / directories as volumes From podman-create(1): If a volume source is specified, it must be a path on the host or the name of a named volume. Host paths are allowed to be absolute or relative; relative paths are resolved relative to the directory Podman is run in. If the source does not exist, Podman returns an error. Users must pre-create the source files or directories. Any source that does not begin with a . or / is treated as the name of a named volume. Implement this with the is_host_volume() check. Signed-off-by: Enno Tensing --- generate/container.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/generate/container.py b/generate/container.py index 9b4bfa6..7d10a1e 100644 --- a/generate/container.py +++ b/generate/container.py @@ -63,12 +63,22 @@ class Volume: return None return [cls(key, value) for key, value in val.items()] + def is_host_volume(self) -> bool: + """Check if this Volume is a named or a host volume.""" + return self.name.startswith("/") or self.name.startswith(".") + def command(self) -> str: """Option for podman container create.""" return f"--volume {self.name}:{self.path}" def create(self) -> str: """Create volume, if it does not exist.""" + if self.is_host_volume(): + # A HOST-DIR starting with a slash or dot is interpreted as a + # directory or file on the host machine. Since the script should + # not modify things outside its own tree, i.e. + # /var/lib/containerctl, ignore it. + return "" cmd = f"# Create volume {self.name}\n" cmd += f"if ! podman volume exists '{self.name}' 2> /dev/null\n" cmd += "then\n" @@ -78,6 +88,9 @@ class Volume: def remove(self) -> str: """Remove volume if it exists.""" + if self.is_host_volume(): + # As with create, don't touch the host system + return "" cmd = f"# Remove volume {self.name}\n" cmd += f"if podman volume exists '{self.name}' 2> /dev/null\n" cmd += "then\n"