From ebb45e502a56a56eaef119245d86f0774ee18edd Mon Sep 17 00:00:00 2001 From: Disassembler Date: Sat, 22 Feb 2020 18:06:59 +0100 Subject: [PATCH] Specify file mount explicitly --- usr/bin/spoc-container | 7 ++----- usr/lib/python3.8/spoc/container.py | 9 ++++++++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/usr/bin/spoc-container b/usr/bin/spoc-container index 545d177..54b5d95 100644 --- a/usr/bin/spoc-container +++ b/usr/bin/spoc-container @@ -33,11 +33,8 @@ def modify_depend(container, depend): def modify_mount(container, mount): volume,mountpoint = mount.split(':', 1) - mountpoint = mountpoint.lstrip('/') if mountpoint: - # If the volume doesn't exist yet, assume it will be a directory - is_dir = not os.path.isfile(os.path.join(VOLUMES_DIR, volume)) - container.mounts[volume] = (mountpoint, is_dir) + container.mounts[volume] = mountpoint else: try: del container.mounts[volume] @@ -112,7 +109,7 @@ parser_list.add_argument('type', choices=('all', 'running', 'stopped'), default= parser_create = subparsers.add_parser('create') parser_create.set_defaults(action=create) parser_create.add_argument('-d', '--depends', action='append', default=[], help='Add another container as a start dependency') -parser_create.add_argument('-m', '--mount', action='append', default=[], help='Add mount to the container - format volume:mountpoint') +parser_create.add_argument('-m', '--mount', action='append', default=[], help='Add mount to the container - format volume:mountpoint[:file]') parser_create.add_argument('-e', '--env', action='append', default=[], help='Add environment variable for the container - format KEY=value') parser_create.add_argument('-u', '--uid', help='Sets the container init UID') parser_create.add_argument('-g', '--gid', help='Sets the container init GID') diff --git a/usr/lib/python3.8/spoc/container.py b/usr/lib/python3.8/spoc/container.py index 78a4aa7..7a1b684 100644 --- a/usr/lib/python3.8/spoc/container.py +++ b/usr/lib/python3.8/spoc/container.py @@ -107,6 +107,13 @@ class Container: for item in os.scandir(self.ephemeral_layer_path): shutil.rmtree(item.path) if item.is_dir() else os.unlink(item.path) + def get_mount_entry(self, volume, mountpoint): + mount_type = 'dir' + if mountpoint.endswith(':file'): + mount_type = 'file' + mountpoint = mountpoint[:-5] + return f'lxc.mount.entry = {os.path.join(VOLUMES_DIR, volume)} {mountpoint} none bind,create={mount_type} 0 0' + def create(self): # Create container directories os.makedirs(self.rootfs_path, 0o755, True) @@ -117,7 +124,7 @@ class Container: # Chown is possible only when the process is running as root, for user namespaces, see https://linuxcontainers.org/lxc/manpages/man1/lxc-usernsexec.1.html os.chown(self.ephemeral_layer_path, 100000, 100000) # Create container configuration file based on the container definition - mounts = '\n'.join([f'lxc.mount.entry = {os.path.join(VOLUMES_DIR, v)} {m[0]} none bind,create={"dir" if m[1] else "file"} 0 0' for v,m in self.mounts.items()]) + mounts = '\n'.join([self.get_mount_entry(v, m) for v,m in self.mounts.items()]) env = '\n'.join([f'lxc.environment = {k}={v}' for k,v in self.env.items()]) uid = self.uid if self.uid else 0 gid = self.gid if self.gid else 0