How to auto start LXD containers VM at boot time in Linux

last updated in Categories , ,

I am using LXD (“Linux container”) based VM. How do I set an LXD container to start on boot in Linux operating system?

You can always start the container when LXD starts on boot. You need to set boot.autostart to true. You can define the order to start the containers in (starting with highest first) using boot.autostart.priority (default value is 0) option. You can also define the number of seconds to wait after the container started before starting the next one using boot.autostart.delay (default value 0) option.

Syntax

Above discussed keys can be set using the lxc tool with the following syntax:
$ lxc config set {vm-name} {key} {value}
$ lxc config set {vm-name} boot.autostart {true|false}
$ lxc config set {vm-name} boot.autostart.priority integer
$ lxc config set {vm-name} boot.autostart.delay integer

How do I set an LXD container to start on boot in Ubuntu Linux 16.10?

Type the following command:
$ lxc config set {vm-name} boot.autostart true
Set an LXD container name ‘nginx-vm’ to start on boot
$ lxc config set nginx-vm boot.autostart true
You can verify setting using the following syntax:
$ lxc config get {vm-name} boot.autostart
$ lxc config get nginx-vm boot.autostart

Sample outputs:

true

You can the 10 seconds to wait after the container started before starting the next one using the following syntax:
$ lxc config set nginx-vm boot.autostart.delay 10
Finally, define the order to start the containers in by setting with highest value. Make sure db_vm container start first and next start nginx_vm
$ lxc config set db_vm boot.autostart.priority 100
$ lxc config set nginx_vm boot.autostart.priority 99

Use the following bash for loop on Linux to view all values:

#!/bin/bash
echo 'The current values of each vm boot parameters:'
for c in db_vm nginx_vm memcache_vm
do
   echo "*** VM: $c ***"
   for v in boot.autostart boot.autostart.priority boot.autostart.delay 
   do
      echo "Key: $v => $(lxc config get $c $v)"
   done
      echo ""
done

Sample outputs:

Fig.01: Get autostarting LXD containers values using a bash shell script
Fig.01: Get autostarting LXD containers values using a bash shell script

Another way is to grab all lxd VMs using

#!/bin/bash
x=$(lxc list -c n | awk '{ print $2}'  | sed -e '/^$/d' -e '/^NAME/d')
echo 'The current values of each vm boot parameters:'
for c in $x
do
   echo "*** VM: $c ***"
   for v in boot.autostart boot.autostart.priority boot.autostart.delay 
   do
      echo "Key: $v => $(lxc config get $c $v)"
   done
      echo ""
done
This entry is 6 of 13 in the LXD Tutorial series. Keep reading the rest of the series:
  1. How to install LXD container hypervisor on Ubuntu 16.04 LTS
  2. How to install and setup LXC (Linux Container) on Fedora Linux 26
  3. How to install LXD container under KVM or Xen virtual machine
  4. How to list VM images in LXD (Linux Containers)
  5. How to upgrade LXD VMs powered by Ubuntu/Debian or CentOS Linux
  6. How to auto start LXD containers at boot time in Linux
  7. How to rename LXD / LXC container
  8. How to run commands on Linux Container (LXD) instance at provision launch time
  9. How to use LXD (Linux containers) in a shell script to create VM when the cloud instance launches
  10. How to move/migrate LXD VM to another host on Linux
  11. How to install and setup LXD on Fedora Linux server
  12. How to install and setup LXD on CentOS Linux 7.x server
  13. Install LXD pure-container hypervisor on Ubuntu 18.04 LTS

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

1 comment

    Have a question? Post it on our forum!