gsallesl.github.io

Using LXD for your development environment

LXD linux containers provides an interesting solution to isolate project development environments (python, android) from your host system.

The motivations for isolating your project development environments are well known and projects such as Vagrant already provide dev environment on top of Virtualbox, Hyper-V or Docker.

However, if you don't need all the functionalities of Vagrant and just look to get a simple and probably more permanent setup, this blog post is for you. This article describes how to use LXD for wordpress development. In this article we will focus on improving the accessibility to the code.

My requirements for my dev environment are as follow:

  • My dev tools (e.g.: compilers) must be running fast, if possible without the overhead added by a virtualization solution such as VirtualBox or Qemu.

  • My code must be accessible on my host: I want to be able to edit my code on my host and to have my changes already commited to my dev environment (e.g.: I don't want to have to copy my code from my host to my dev environment, especially when working on big projects such as the linux kernel or the AOSP framework).

LXD provides interesting features for this use case:

  • Nearly native performances, or at least performances not as bad as they are when compiling in a virtual machine, thanks to the linux namespace system LXD is build on top of.
  • Easily accessible code: shared folders between the containers and the host use bind mounts. This architecture can be leveraged to editing the code from your host and only compile from the container.

Here is what my setup looks like on a ubuntu system:

Setup

# - Install and configure lxd (one time operation)
sudo apt-get install lxd
sudo lxd init

# - Create a new environment
# for example ubuntu version 14.04 and I call the container ubuntu-dev
lxc launch ubuntu:14.04 ubuntu-dev

# - Create a user that corresponds to the uid given
# to the containers file (one time operation)

# First lookup the uid that corresponds to the container
sudo ls -la /var/lib/lxd/containers/ubuntu-dev/rootfs
[output] drwxr-xr-x  24 165536 165536 4096 Apr 21 20:26 .
[output] drwxr-xr-x+  4 165536 165536 4096 Apr 21 19:13 ..
[output] drwxr-xr-x   2 165536 165536 4096 Apr 20 10:57 bin
[output] ...

# Create the user using the container files uid (one time operation)
sudo adduser --home=/dev/null --no-create-home --gecos "" --uid 166536 --disabled-password --shell /bin/false lxduser

# - Create a directory that will contain the code shared 
# between the host and the containers (one time operation)
sudo mkdir /home/projects
chown -R lxduser:lxduser /home/projects

# - Mount a filesystem outside of the container as the home 
# for the container user (ubuntu by default)
lxc config device add ubuntu-dev projects disk source=/home/projects path=/home/ubuntu

# - Restart the container to apply the changes
lxd restart ubuntu-dev

# - Change ownership of the files in your containers
lxc exec ubuntu-dev -- chown -R ubuntu:ubuntu /home/ubuntu

# - Mount /home/project, which currently belongs to lxduser
# in your host user (gsalles) home tree so you can edit your code locally
# We are going to use bindfs for this operation:
# we use the mapping feature between the lxd container user (lxduser) 
# and your host user (gsallesl)
sudo mount -t fuse.bindfs /home/projects /home/gsallesl/projects -o map=lxduser/gsallesl:@lxduser/@gsallesl

# An fstab entry looks as follow to make the mount automatic at startup
/home/projects  /home/gsallesl/projects     fuse.bindfs     map=lxduser/gsallesl:@lxduser/@gsallesl   0       0

# (optional) Hide lxduser user from the lightdm login screen on ubuntu:
# http://askubuntu.com/questions/92349/how-do-i-hide-a-particular-user-from-the-login-screen/575390#575390