LXD: Machine containers for Development

18 April 2017
lxd lxc containers ubuntu

Overview

LXD is an awesome tool in my toolbox that I use everyday. LXD offers a streamlined user experience to Linux containers making them easy and simple to use. These containers are referred to as system containers or machine containers as they are not focused on a single process or software distribution. The project is very well documented and even has a try it online service available.

Stephane Graber’s LXD 2.0 blog post series is required reading. He gives a great introduction and overview of all the major features and functionality of LXD including images, profiles, configuration, and snapshots.

In my use, rather that booting up a VM I can quickly use LXD to boot up a variety of operating systems. I can then use various containers for bug reproduction and triage or to try new deployment scripts and tests in a clean enviornment.

Install

LXD is easily installed in Ubuntu with apt and there is also the option of using a PPA to get the latest upstream release quickly. All of this is part of the excellent documentation.

The only item I would call out is running lxd init. This command is what sets up and configures the LXD daemon by telling it what storage and networking to use. Normally pressing enter through the process will get the correct settings, but it is worth reading through each option.

Usage

While the tool is called lxd, the commands all center around ‘lxc’. The basics include listing your containers with lxc list. To get pre-build images you can look at two example image remotes by running lxc image list images: or lxc image list ubuntu:. Based on those results launching an image is as simple as lxc image launch ubuntu:xenial <name>.

Executing on a container is done via lxc exec <name> <command>.

Stopping and deleting a container is done via lxc stop <name> and lxc delete <name>.

For even more details again take a look at Stephane’s blog post.

Reset

Sometimes there are days where I am switching between versions or trying various features out and I run into an issue with LXD. In these cases I like to wipe LXD out and start from scratch. This documents the process I take.

First, start with removing any containers or images and the packages themselves:

lxc list
lxc list -c n | grep '^|' | cut -d' ' -f2 | sed -n '1!p' | xargs lxc delete --force
lxc image list
lxc image list | grep '^|' | cut -d' ' -f9 | xargs lxc image delete
sudo apt-get purge lxd lxc

The reset is not done at this point. There are a few other areas to go and cleanup that will make sure that a new version or older version of lxd will not have any issues.

If lxd was setup to use zfs, delete the zpool lxd was using:

sudo zpool list
sudo zpool destroy default  # maybe named lxd in older versions

Delete the lxd bridge:

sudo ip link set lxdbr0 down
sudo brctl delbr lxdbr0

Finally, remove any final items in var, like logs:

sudo umount -l /var/lib/lxd/shmounts
sudo umount -l /var/lib/lxd/devlxd
sudo rm -Rf /var/lib/lxd
sudo rm -Rf /var/log/lxd
sudo rm -Rf /var/log/lxc

I suggest doing a reboot at this point to make sure things are fully cleaned up. Then afterwards reinstall and configure LXD:

sudo apt update
sudo apt upgrade
sudo apt install lxd lxc
sudo lxd init

Ubuntu 18.10 Released

19 October 2018
ubuntu cosmic

Deploying Ubuntu root on ZFS with MAAS

12 October 2018
ubuntu maas zfs

cloud-init 18.4 Released

03 October 2018
cloud-init ubuntu
comments powered by Disqus