I asked the lxc/lxd developers themselves and it appears that it's not possible to do it with configuration if lxd is used to create the containers. It's not implemented yet, but planned they say...
lxd is somewhat different from lxc scripts: i.e. the default directory for the containers is /var/lib/lxd/lxc/
instead of /var/lib/lxc/
and the container configurations are held in a central sqlite db instead of some config file per container.
So I had to create a systemd job by creating the file: /etc/systemd/system/multi-user.target.wants/lxd-autostart.service
with this content:
[Unit]
Description=Start lxc containers created with lxd
After=lxd.service
Requires=lxd.service
[Service]
ExecStartPre=/usr/local/bin/lxd-autostart-check.sh
ExecStart=/usr/bin/lxc start my_machine
Restart=on-failure
[Install]
WantedBy=multi-user.target
and /usr/local/bin/lxd-autostart-check.sh
is:
#!/bin/bash
for i in {1..10}
do
echo "."
[ -S /var/lib/lxd/unix.socket ] && exit 0
sleep 1
done
echo "/var/lib/lxd/unix.socket not detected!"
exit 1