KVM was an improvement over Xen for me. Still for many use-cases a LXC are a more performance, light-weight alternative – which also seems to be en vougue nowadays.
Through switching to LXC I’ve reduced my overall memory usage a bit – the main benefit is, that processes within an LXC container are separated processes within the host system. This should allow the host system to manage memory (think cache, buffers, swap, etc.) more efficiently.
I’ve started converting most of my trusted KVM images into LXC containers, this post contains the necessary steps.
Step 1: create a new empty lxc container
First of all we’ll create a new empty LXC container with a default configuration. I’ll name it ‘imap’, guess it’s purpose.
|
|
LXC’s containers are created within /var/lib/lxc
and, surprise!, a new and aptly named /var/lib/lxc/imap
container can be found. The container containes two entries for now: config
for its configuration and an empty rootfs
-directory which will contain the containers filesystem soon.
Step 2: copy the KVM image into the LXC container
I’m using LVM-backed KVM/QEMU images, each LVM volume mirrors a physical harddrive including partition tables, etc. Alas this prevents us from just mounting the KVM root image.
First of all let’s output the KVM image’s partition table:
|
|
So the real filesystem starts at sector 2048 and each sector is 512 bytes long. This allows us to calculate this partitions offset within the image and loopback mount the partition into the hosts filesystem:
|
|
Now we can copy the old KVM image’s contents into the new container:
|
|
Step 3: prepare the container’s device nodes and fix fstab
LXC does not support udev, so we’ll have to create the container’s device nodes by ourself. To simplify this, I’ve used the following bash script and copied it to /usr/local/sbin/create-lxc-nodes.sh
|
|
Use this script to create all needed device nodes:
|
|
As all filesystems were already prepared by the host system there’s no need for the guest system’s init system to do any work during bootup (actually this might rather be harmful). To prevent any problems I’ve commented out each and every line within the guests “etc/fstab” configuration file.
Step 4: create LXC configuration file
Each LXC container’s configuration is stored in the “config” file which is situated around the “rootfs” directory. Let’s create a new one:
|
|
We’ll allow a couple of devices (mostly terminals) and provide a mounted proc and sys filesystem to the guest. Note the container’s name (lxc.utsname) and the configured path for it’s root filesystem (/var/lib/lxc/imap/rootfs). In addition this configuration file contains the network configuration (ipv4 sets the IP address which the container will be assigned to by the internal DHCP server). Please don’t forget to provide an unique MAC address (hwaddr) to each container.
Step 5: start the container
Start the container in the background and SSH into it
|
|
Say welcome to your new container!
Appendix 1: improve the container..
There are some things that are not needed anymore. All commands are entered within the container (which is a Debian 7.0 system BTW)!
|
|
Let’s see how we can improve the container in the future..