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!