Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

container: T7185: Allow tmpfs mounts within containers #4358

Open
wants to merge 1 commit into
base: current
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions interface-definitions/container.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,35 @@
</constraint>
</properties>
</leafNode>
<tagNode name="tmpfs">
<properties>
<help>Mount a tmpfs filesystem into the container</help>
</properties>
<children>
<leafNode name="destination">
<properties>
<help>Destination container directory</help>
<valueHelp>
<format>txt</format>
<description>Destination container directory</description>
</valueHelp>
</properties>
</leafNode>
<leafNode name="size">
<properties>
<help>tmpfs filesystem size in MB</help>
<valueHelp>
<format>u32:1-65536</format>
<description>tmpfs filesystem size in MB</description>
</valueHelp>
<constraint>
<validator name="numeric" argument="--range 1-65535"/>
</constraint>
<constraintErrorMessage>Container tmpfs size must be between 1 and 65535 MB</constraintErrorMessage>
</properties>
</leafNode>
</children>
</tagNode>
<tagNode name="volume">
<properties>
<help>Mount a volume into the container</help>
Expand Down
26 changes: 25 additions & 1 deletion src/conf_mode/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from ipaddress import ip_network
from json import dumps as json_write

import psutil
from vyos.base import Warning
from vyos.config import Config
from vyos.configdict import dict_merge
Expand Down Expand Up @@ -223,6 +224,21 @@ def verify(container):
if not os.path.exists(source):
raise ConfigError(f'Volume "{volume}" source path "{source}" does not exist!')

if 'tmpfs' in container_config:
for tmpfs, tmpfs_config in container_config['tmpfs'].items():
if 'destination' not in tmpfs_config:
raise ConfigError(f'tmpfs "{tmpfs}" has no destination path configured!')
if 'size' in tmpfs_config:
free_mem_mb: int = psutil.virtual_memory().available / 1024 / 1024
if int(tmpfs_config['size']) > free_mem_mb:
Warning(f'tmpfs "{tmpfs}" size is greater than the current free memory!')

total_mem_mb: int = (psutil.virtual_memory().total / 1024 / 1024) / 2
if int(tmpfs_config['size']) > total_mem_mb:
raise ConfigError(f'tmpfs "{tmpfs}" size should not be more than 50% of total system memory!')
else:
raise ConfigError(f'tmpfs "{tmpfs}" has no size configured!')

if 'port' in container_config:
for tmp in container_config['port']:
if not {'source', 'destination'} <= set(container_config['port'][tmp]):
Expand Down Expand Up @@ -362,6 +378,14 @@ def generate_run_arguments(name, container_config):
prop = vol_config['propagation']
volume += f' --volume {svol}:{dvol}:{mode},{prop}'

# Mount tmpfs
tmpfs = ''
if 'tmpfs' in container_config:
for tmpfs_config in container_config['tmpfs'].values():
dest = tmpfs_config['destination']
size = tmpfs_config['size']
tmpfs += f' --mount=type=tmpfs,tmpfs-size={size}M,destination={dest}'

host_pid = ''
if 'allow_host_pid' in container_config:
host_pid = '--pid host'
Expand All @@ -373,7 +397,7 @@ def generate_run_arguments(name, container_config):

container_base_cmd = f'--detach --interactive --tty --replace {capabilities} --cpus {cpu_quota} {sysctl_opt} ' \
f'--memory {memory}m --shm-size {shared_memory}m --memory-swap 0 --restart {restart} ' \
f'--name {name} {hostname} {device} {port} {name_server} {volume} {env_opt} {label} {uid} {host_pid}'
f'--name {name} {hostname} {device} {port} {name_server} {volume} {tmpfs} {env_opt} {label} {uid} {host_pid}'

entrypoint = ''
if 'entrypoint' in container_config:
Expand Down
Loading