Friday, July 16, 2010

LXC on Ubuntu 10.04 Lucid Lynx

If you are like me who doesn't have a lot of time anymore, at times trying new stuff is a lot harder. About a year ago I used kvm, which is great and easy enough to get up and running. I have used it since then however Kvm is a bit too much for my needs as its a full paravirt. Container based virtualization like vserver and openvz seems to be more challenging to get running on a laptop.

Fast forward today, it seems LXC Linux Containers ( has now made some strides. Its now part of the main kernel, most modern distro should have like. Here is a quick guide on how to get lxc up and running on Ubuntu Lucid.

Below is to setup a debian lenny guest, that uses virbr0 and has dhcp. Do the following as root:

- install libvirt as its easier to do networking on it. No need to setup your own bridge, ipchains and nat. libvirt default qemu network should create a virbr0 interface.

apt-get install libvirt-bin

I have installed this prior for kvm. What we want is virbr0 is up and running. You check this by ifconfig. Try to start it manually on /etc/init.d/libvirt-bin

- install lxc

apt-get install lxc debootstrap

- create the capabilities dir and mount it

mkdir /cgroup

add to /etc/fstab
none /cgroup cgroup defaults 0 0

mount /cgroup

- get and edit the lxc-debian script from /usr/share/doc/lxc/examples/lxc-debian.gz (gunzip it somewhere and apply correct permissions)

Edit and add the following lxc parameters on copy_configuration() function before EOF

# networking = veth = up = virbr0 = eth0 = 1500

What this does is use the host virbr0 which is already NATed. This would appear as eth0 on the guest container.

You may need to edit increase the tty allowed, I was getting init warning on my syslog respawning. What I did was to edit my guest inittab to reduce the tty.

lxc.tty = 4 to lxc.tty = 6

- create your lxc dir. In my case the name of my container is "altair". Change it as you fit.

mkdir /home/lxc/altair
lxc-debian -p /home/lxc/altair create

This would start downloading debian lenny packages and create the root file system on /home/lxc/altair/rootfs.

- create the container

lxc-create -n altair -f /home/lxc/altair/config

- run the container

lxc-start -n altair

the above runs it on the foreground, once you have setup everything then you can run it as a deamon

lxc-start -n altair -d

Once you are inside the container, things that I did was
- set the hostname
- fix /etc/hosts
- add your favorite repo on /etc/apt/sources.list
- add X forwarding on ssh and install xauth package
- install rsyslog
- maybe edit your hosts /etc/hosts and add the IP address of the container.

Some useful links:


Luisma said...

Thx for post.

I've got the following error:

# lxc-start: failed to clone(0x6c020000): Invalid argument
lxc-start: Invalid argument - failed to fork into a new namespace
lxc-start: failed to spawn '/sbin/init'
lxc-start: cgroup is not mounted

It seems that the problem was:

an the Lucid Linx kernel was updated on 20 Apr 2011:

* (config) Disable CONFIG_NET_NS
- LP: #720095

Running a previous kernel package (the one just before the change: 2.6.32-31) and lxc works well (may be, eating some memory ...)

# lxc-checkconfig
Kernel config /proc/config.gz not found, looking in other places...
Found kernel config file /boot/config-2.6.32-31-generic-pae
--- Namespaces ---
Namespaces: enabled
Network namespace: enabled


Unknown said...

Thanks Luis.

I encountered this too. What i did was to use the backported Maverick kernel. Now I am at Natty Narwhal its ok.