Creating an LXC Container on the Raspberry Pi

This post assumes you’ve followed the instructions in our post “Building an LXC-friendly Kernel on the Raspberry Pi” to get kernel support working and install the lxc tools from the LXC git repository.

Mount a cgroup

# pico /etc/fstab

# add the line “lxc /sys/fs/cgroup cgroup defaults 0 0”

# mount -a

Create a Directory to Store Hosts

# mkdir -p /var/lxc/guests

Create a File System for the Container

Let’s create a container called “test”.

First, create a filesystem for the container. This may take some time

# apt-get install debootstrap

# mkdir -p /var/lxc/guests/test

# debootstrap wheezy /var/lxc/guests/test/fs/ http://archive.raspbian.org/raspbian

Modify the Container’s File System

# chroot /var/lxc/guests/test/fs/

Change the root password.

# passwd

Change the hostname as you wish.

# pico /etc/hostname

undo chroot

# exit

Create a Minimal Configuration File

# pico /var/lxc/guests/test/config

Enter the following:

lxc.utsname = test

lxc.tty = 2

lxc.rootfs = /var/lxc/guests/test/fs

Create the Container

lxc-create -f /var/lxc/guests/test/config -n test

Test the Container

# lxc-start -n test -d

[wait for a while, a few minutes]

# lxc-console -n test -t 1

Advertisements

Building an LXC-friendly Kernel for the Raspberry Pi

This post is heavily based on Yohei Kuga’s post on Google+

https://plus.google.com/113091037050058478853/posts/8tYdBrbxu8i

We’ve spent a lot of time installing LXC in various ways and using different configurations. Recently Yohei Kuga posted a neat and minimal process, which was certainly more streamlined than our approach. I’ve expanded his notes and tested everything to ensure it works.

Download Raspbian and bake an SD Card

See:

http://www.raspberrypi.org/downloads

We used the 2013-02-09-wheezy-raspbian.zip image.

We made no other changes to this image other than those instructions listed below.

Switch to Root User on the Pi

These commands must be run as root. You can use “su”, or for convenience (I know):

# sudo su root

Expand to fill SD Card

Expand to fill SD card and reboot after entering:

# raspi-config

Update Raspbian

# apt-get update

# apt-get dist-upgrade

Install git

# sudo apt-get install git-core

Update Firmware

The clone will take a while. You might consider cloning on a desktop machine to save time. Just transfer the firmware/boot and modules/ directories from your desktop PC to the Pi after the checkout.

# cd /opt

# git clone git://github.com/raspberrypi/firmware.git

# cd firmware/boot

# cp * /boot

# cd ../modules

# cp -r * /lib/modules

# reboot

Increase the Swap File Size

I found that in order to check out the source on the Pi, you’ll need a swap file with the 256MB Pi, otherwise it will run out of RAM during the checkout (with fatal: index-pack failed).

# pico /etc/dphys-swapfile

# change to 500 (MB)

# sudo dphys-swapfile setup

# reboot

Prepare to Build Kernel

The clone will take a while. Again, you may consider using a desktop PC. Of course, if you do that, you’ll need to issue the “zcat” command from your Pi and copy the resulting “.config” file to the “linux” directory on your desktop PC.

# cd /opt

# mkdir raspberrypi

# cd raspberrypi

# git clone git://github.com/raspberrypi/linux.git

# cd linux

# zcat /proc/config.gz > .config

Decrease the Swap Space File

# pico /etc/dphys-swapfile

# change to 100 (MB)

# dphys-swapfile setup

# reboot

Install Packages for Kernel Compilation

# apt-get install ncurses-dev

Kernel Options

You’ll now need to set some kernel options to support LXC, via the menu config tool.

# cd /opt/raspberrypi/linx

# make menuconfig

You need to enable these options:

* General -> Control Group Support -> Memory Resource Controller for Control Groups (*and its three child options*)

(this has high overhead;only enable if you really need it, or else enable and remember to disable using the Kernel command line option “cgroup_disable=memory”)

* General -> Control Group Support -> cpuset support

* Device Drivers -> Character Devices -> Support multiple instances of devpts

* Device Drivers -> Network Device Support -> Virtual ethernet pair device

Build Kernel 

# make

# make modules_install

# cd /opt/raspberrypi

# git clone git://github.com/raspberrypi/tools.git

# cd tools/mkimage

# python ./imagetool-uncompressed.py /opt/raspberrypi/linux/arch/arm/boot/Image

# cp /boot/kernel.img /boot/kernel-old.img

# cp kernel.img /boot/

# reboot

Download Latest LXC

The LXC tools provided with Raspbian are out-of-date.

# mkdir /opt/lxc

# cd /opt/lxc

# git clone https://github.com/lxc/lxc.git

# apt-get install automake libcap-dev

# cd lxc

# ./autogen.sh && ./configure && make && make install

Testing the Install

Check LXC is happy with your kernel:

# lxc-checkconfig

User namespace should be “missing” (it checks for a kernel option that no longer exists) and Cgroup namespace should say “required”.