1
0
Fork 0

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 <tenno@suij.in>
This commit is contained in:
Enno Tensing 2025-07-29 10:03:14 +02:00
parent 4ce53cb20b
commit a601e76a93
Signed by: tenno
GPG key ID: 95265603BD36E66C

View file

@ -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"