From 7591cf2d477c9d97c96e13ee276da0bc0cf2a47e Mon Sep 17 00:00:00 2001 From: Disassembler Date: Tue, 24 Sep 2019 19:14:16 +0200 Subject: [PATCH] Fix mounts and start_post --- usr/lib/python3.6/lxcmgr/lxcmgr.py | 7 ++++--- usr/lib/python3.6/lxcmgr/svcmgr.py | 10 +++++++--- usr/lib/python3.6/lxcmgr/templates.py | 13 ++++--------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/usr/lib/python3.6/lxcmgr/lxcmgr.py b/usr/lib/python3.6/lxcmgr/lxcmgr.py index 3e6b613..d237d0b 100644 --- a/usr/lib/python3.6/lxcmgr/lxcmgr.py +++ b/usr/lib/python3.6/lxcmgr/lxcmgr.py @@ -23,7 +23,7 @@ def prepare_container(container, layers): subprocess.run(['mount', '--bind', layers[0], rootfs]) else: olwork = os.path.join(LXC_ROOT, container, 'olwork') - subprocess.run(['mount', '-t', 'overlay', '-o', 'upperdir={},lowerdir={},workdir={}'.format(layers[-1], ':'.join(layers[:-1]), olwork), 'none', rootfs]) + subprocess.run(['mount', '-t', 'overlay', '-o', 'upperdir={},lowerdir={},workdir={}'.format(layers[0], ':'.join(layers[1:]), olwork), 'none', rootfs]) def clean_ephemeral_layer(container): # Cleans containers ephemeral layer. Called in lxc.hook.post-stop and lxc.hook.pre-start in case of unclean shutdown @@ -50,10 +50,11 @@ def create_container(container, image): os.chown(ephemeral, 100000, 100000) # Create container configuration file layers = ','.join([os.path.join(LXC_STORAGE_DIR, layer) for layer in image['layers']]) + # Add ephemeral layer if the container is not created as part of build process if 'build' not in image: layers = '{},{}'.format(layers, ephemeral) - mounts = '\n'.join(['lxc.mount.entry = {} {} none bind,create={} 0 0'.format(m[1], m[2].lstrip('/'), m[0].lower()) for m in image['mounts']]) if 'mounts' in image else '' - env = '\n'.join(['lxc.environment = {}={}'.format(e[0], e[1]) for e in image['env']]) if 'env' in image else '' + mounts = '\n{}'.format('\n'.join(['lxc.mount.entry = {} {} none bind,create={} 0 0'.format(m[1], m[2].lstrip('/'), m[0].lower()) for m in image['mounts']])) if 'mounts' in image else '' + env = '\n{}'.format('\n'.join(['lxc.environment = {}={}'.format(e[0], e[1]) for e in image['env']])) if 'env' in image else '' uid = image['uid'] if 'uid' in image else '0' gid = image['gid'] if 'gid' in image else '0' cmd = image['cmd'] if 'cmd' in image else '/bin/sh' diff --git a/usr/lib/python3.6/lxcmgr/svcmgr.py b/usr/lib/python3.6/lxcmgr/svcmgr.py index cffadb9..5343279 100644 --- a/usr/lib/python3.6/lxcmgr/svcmgr.py +++ b/usr/lib/python3.6/lxcmgr/svcmgr.py @@ -8,9 +8,13 @@ from .templates import SERVICE def create_service(app, container, image): depends = ' '.join(image['depends']) if 'depends' in image else '' - ready = 'lxc-attach {} -- sh -c \'until $({}); do sleep 0.1; done\''.format(container, image['ready']) if 'ready' in image else '' - with open(os.path.join(SERVICE_DIR, container), 'w') as f: - f.write(SERVICE.format(app=app, container=container, depends=depends, ready=ready)) + # Add ready check to start_post + # This could arguably be better done via some template engine, but introducing one for 2 template files seems like overkill + start_post = '\nstart_post() {{\n timeout -t 60 lxc-attach {} -- sh -c \'until {}; do sleep 0.1; done\'\n}}\n'.format(container, image['ready']) if 'ready' in image else '' + service_file = os.path.join(SERVICE_DIR, container) + with open(service_file, 'w') as f: + f.write(SERVICE.format(app=app, container=container, depends=depends, start_post=start_post)) + os.chmod(service_file, 0o755) update_services() def delete_service(service): diff --git a/usr/lib/python3.6/lxcmgr/templates.py b/usr/lib/python3.6/lxcmgr/templates.py index 107a9f5..673d458 100644 --- a/usr/lib/python3.6/lxcmgr/templates.py +++ b/usr/lib/python3.6/lxcmgr/templates.py @@ -16,17 +16,16 @@ lxc.rootfs.path = /var/lib/lxc/{name}/rootfs # Mounts lxc.mount.entry = shm dev/shm tmpfs rw,nodev,noexec,nosuid,relatime,mode=1777,create=dir 0 0 lxc.mount.entry = /etc/hosts etc/hosts none bind,create=file 0 0 -lxc.mount.entry = /etc/resolv.conf etc/resolv.conf none bind,create=file 0 0 -{mounts} +lxc.mount.entry = /etc/resolv.conf etc/resolv.conf none bind,create=file 0 0{mounts} # Init lxc.init.uid = {uid} lxc.init.gid = {gid} lxc.init.cwd = {cwd} +lxc.init.cmd = {cmd} # Environment -lxc.environment = PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin -{env} +lxc.environment = PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin{env} # Halt lxc.signal.halt = {halt} @@ -61,11 +60,7 @@ depend() {{ start() {{ lxc-start {container} }} - -start_post() {{ - {ready} -}} - +{start_post} stop() {{ lxc-stop {container} }}