Setup LXC on Debian


I've tested this installation on Debian Wheezy, but it also works on older distributions (Squeeze etc.).

You can install lxc including some needed components by executing:
apt-get install lxc debootstrap bridge-utils lvm2

Then you should configure a bridge network interface in order to be used by your virtual machines. Therefore edit /etc/network/interfaces, comment out the existing lines for eth0 (or the interface you wish to use) and add the new bridge interface as shown below (you may want to assign a static IP address instead of using DHCP):
# The primary network interface
#allow-hotplug eth0
#iface eth0 inet dhcp

# Setup bridge
auto br0
iface br0 inet dhcp
   bridge_ports eth0
   bridge_fd 0

Now it's time to setup your first container. By default, lxc stores its containers in /var/lib/lxc (if you chose a different path while the installation, you need to change the path below, too). We're just creating a basic Debian Squeeze container:

lxc-create -n squeeze
mkdir -p /var/lib/lxc/squeeze/rootfs
debootstrap –verbose –variant=minbase –arch=amd64 –include ifupdown,locales,libui-dialog-perl,dialog,dhcp3-client,netbase,net-tools,iproute,openssh-server squeeze /var/lib/lxc/squeeze/rootfs http://ftp.debian.org/debian

Edit the container configuration file /var/lib/lxc/squeeze/config with the following content:
lxc.tty = 6
lxc.pts = 1024
lxc.rootfs = /var/lib/lxc/squeeze/rootfs
lxc.cgroup.devices.deny = a
lxc.cgroup.devices.allow = c 1:3 rwm
lxc.cgroup.devices.allow = c 1:5 rwm
lxc.cgroup.devices.allow = c 5:1 rwm
lxc.cgroup.devices.allow = c 5:0 rwm
lxc.cgroup.devices.allow = c 4:0 rwm
lxc.cgroup.devices.allow = c 4:1 rwm
lxc.cgroup.devices.allow = c 1:9 rwm
lxc.cgroup.devices.allow = c 1:8 rwm
lxc.cgroup.devices.allow = c 136:* rwm
lxc.cgroup.devices.allow = c 5:2 rwm
lxc.cgroup.devices.allow = c 254:0 rwm
lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = br0
lxc.network.name = eth0
lxc.network.hwaddr = 00:F0:11:22:33:44

Before starting up your virtual machine the first time, you still have to do a few steps:

  • Edit the network configuration file /var/lib/lxc/squeeze/rootfs/etc/network/interfaces in the VM according to your network (by default it's left to DHCP)
  • Remove unneeded startup services:
    chroot /var/lib/lxc/squeeze/rootfs /usr/sbin/update-rc.d -f umountfs remove
    chroot /var/lib/lxc/squeeze/rootfs /usr/sbin/update-rc.d -f hwclock.sh remove
    chroot /var/lib/lxc/squeeze/rootfs /usr/sbin/update-rc.d -f hwclockfirst.sh remove
  • Set the root password inside your VM:
    chroot /var/lib/lxc/squeeze/rootfs passwd
  • If you want your container to startup at boot time, just create a symlink to your config file in /etc/lxc/auto/: ln -s /var/lib/lxc/squeeze/config /etc/lxc/auto/squeeze

Now start up your VM: lxc-start -n squeeze
To start your VM in background: lxc-start -d -n squeeze
To stop your VM: lxc-shutdown -n squeeze
To stop your VM immediately: lxc-stop -n squeeze
To clone your VM: lxc-clone -o squeeze -n squeeze2
To list all your VMs with their status: lxc-list
Show your current config: lxc-checkconfig 
There are more lxc-* commands, but that might be enough to start!