Run a script in the host before container start

I’m going to switch to lxd from lxc, but I can’t find the lxd counterpart of lxc “lxc.hook.pre-start” config option (I would like to do something on the host just before container start).

Any suggestions?

mrupio

I can, of course, set raw.lxc parameter to ‘lxc.hook.pre-start = /path/to/script.sh’ and it works - the script starts, but since I use Ubuntu 20.04 with lxd supplied from snap, the script script.sh is already sandboxed in /tmp/snap.lxd/ and it cannot see my real filesystem root (and /usr/bin/qemu-nbd in particular - the executable that I want to run).

I guess the script started by lxd and not by the underlying lxc wouldn’t be sandboxed. So hence my previous question – how to do it in lxd?

There isn’t an equivalent in LXD to LXC hooks.

I believe you can use the events API in LXD to monitor for a container start and action an event though.

@stgraber may be able to advise further.

I have the same problem. I have a pre-start script that increases hashsize so I can run microk8s inside of my lxd container on a machine with a lot of processors. In older versions I was doing this in a pre-start:

echo 196608 > /sys/module/nf_conntrack/parameters/hashsize

Now it causes the container to fail. Is there a replacement for pre-start hooks or should I do something else? Thanks!!!

Self Answer:

I placed my startup hook in /var/snap/lxd/common/lxc and now it’s happy!

2 Likes

Ok. Would you like to share more details to what you did in /var/snap/lxd/common/lxc ?
Here, I have an empty local.conf and can’t find details on syntax or available options

/var/snap/lxd/common/lxcshould be available inside the LXD process (daemon?) which starts the lxc container, so if you put there a script for the hook and configure it like above, it should be able to execute it (currently untested). I couldnt find good documentation for LXC

lxc.hook.pre-start 	A hook to be run in the host namespace before the container ttys, consoles, or mounts are loaded
lxc.hook.pre-mount 	A hook to be run in the container's filesystem namespace, but before the rootfs has been set up
lxc.hook.mount 	    A hook to be run in the container after mounting has been done, but before the pivot_root
lxc.hook.autodev 	A hook to be run in the container after mounting has been done and after any mount hooks have run, but before the pivot_root
lxc.hook.start 	    A hook to be run in the container right before executing the container's init
lxc.hook.stop 	    A hook to be run in the host's namespace after the container has been shut down
lxc.hook.post-stop 	A hook to be run in the host's namespace after the container has been shut down...

probably dump the env to see what variables are available from there (e.g. LXC_CONFIG_FILE, LXC_LOG_LEVEL, LXC_NAME, LXC_HOOK_VERSION, LXC_CGNS_AWARE, LXC_TARGET,…)