Specify file mount explicitly

This commit is contained in:
Disassembler 2020-02-22 18:06:59 +01:00
parent cdfd0de2b6
commit ebb45e502a
No known key found for this signature in database
GPG Key ID: 524BD33A0EE29499
2 changed files with 10 additions and 6 deletions

View File

@ -33,11 +33,8 @@ def modify_depend(container, depend):
def modify_mount(container, mount): def modify_mount(container, mount):
volume,mountpoint = mount.split(':', 1) volume,mountpoint = mount.split(':', 1)
mountpoint = mountpoint.lstrip('/')
if mountpoint: if mountpoint:
# If the volume doesn't exist yet, assume it will be a directory container.mounts[volume] = mountpoint
is_dir = not os.path.isfile(os.path.join(VOLUMES_DIR, volume))
container.mounts[volume] = (mountpoint, is_dir)
else: else:
try: try:
del container.mounts[volume] 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 = subparsers.add_parser('create')
parser_create.set_defaults(action=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('-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('-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('-u', '--uid', help='Sets the container init UID')
parser_create.add_argument('-g', '--gid', help='Sets the container init GID') parser_create.add_argument('-g', '--gid', help='Sets the container init GID')

View File

@ -107,6 +107,13 @@ class Container:
for item in os.scandir(self.ephemeral_layer_path): for item in os.scandir(self.ephemeral_layer_path):
shutil.rmtree(item.path) if item.is_dir() else os.unlink(item.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): def create(self):
# Create container directories # Create container directories
os.makedirs(self.rootfs_path, 0o755, True) 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 # 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) os.chown(self.ephemeral_layer_path, 100000, 100000)
# Create container configuration file based on the container definition # 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()]) env = '\n'.join([f'lxc.environment = {k}={v}' for k,v in self.env.items()])
uid = self.uid if self.uid else 0 uid = self.uid if self.uid else 0
gid = self.gid if self.gid else 0 gid = self.gid if self.gid else 0