5

I have created a lxc container easily with LXD tools however I couldn't find a way to auto-start it on reboot. (Ubuntu 15.04)

For LXC it's said that adding lxc.start.auto = 1 to the container config would be enough however I couldn't find the config file for the container created with LXD. I tried

lxc config set my_machine raw.lxc "lxc.start.auto=1"

without any luck. The configuration is saved but the container didn't start... Any ideas?

3 Answers 3

6

It's a few months later now, and autostart is (for some time now) supported in lxd itself. You can just

lxc init ubuntu u1
lxc config set u1 boot.autostart true

Now u1 will autostart on each reboot.

2

Based on the official documentation here LXC container startup was designed using upstart. As of March 9, 2015 Ubuntu 15.04 changed to systemd by default. More detail regarding the impact of this change and how to switch from systemd to upstart and back again is available on this page.

To check to see if your system is using systemd vs. upstart you can test by issuing the command sudo initctl version which will return upstart if upstart is handling init. another useful method is to issue the command dpkg -S /sbin/init which will tell you which package installed it (in my case on 14.04 it's upstart) you you can find more very good answers on how to do so from our friends at Unix & Linux. Further detail regarding identifying the init system is also available.

To see if your LXC container auto-starts under upstart as designed, you can install the upstart-sysv package, which will remove ubuntu-standard and systemd-sysv (but should not remove anything else. Give the developers a heads up if it does!), and run sudo update-initramfs -u. After that, grub's "Advanced options" menu will have a corresponding "Ubuntu, with Linux ... (systemd)" entry where you can do an one-time boot with systemd.

You can revert back to systemd installing systemd-sysv and ubuntu-standard packages.

Further sources (not linked elsewhere):

https://unix.stackexchange.com/questions/196166/how-to-find-out-if-a-system-uses-sysv-upstart-or-systemd-initsystem

1
  • Thanks for the detailed answer but it's not about upstart/systemd change. I haven't tried but lxc.service contains the necessary code to start lxc containers which should work out of the box...
    – madpoet
    May 6, 2015 at 0:50
0

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
5
  • 1
    Could you edit to explain how you did this?
    – Tim
    May 5, 2015 at 16:00
  • @Tim: Oh, sorry, it was just that it was talking about the author adding a script, so I assumed that it was an edit.
    – user364819
    May 5, 2015 at 16:03
  • @ParanoidPanda Don't worry, easy mistake! :)
    – Tim
    May 5, 2015 at 16:03
  • OK, edited as requested. The question is not about how to write a systemd job but how to start containers created by lxd. systemd vs. upstart change and the details are already documented pretty well in the official ubuntu docs so I thought it's too obvious to be mentioned.
    – madpoet
    May 6, 2015 at 0:41
  • This really ought to be done using boot.autostart nowadays.
    – stgraber
    Nov 23, 2015 at 17:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .