LXC Configure Ubuntu Karmic Containers

February 17th, 2010 by bodhi.zazen

This post is in follow up to my previous post on configuring Ubuntu Lucid (10.04) LXC containers and in this post I will show you how to configure an Ubuntu Karmic (9.10) LXC container.

I will again use debootstrap to make a root file system (rootfs) for a LXC container using Ubuntu Karmic (9.10).

Commands in this tutorial are run as root, so to obtain a root shell use:

sudo -i

The working directory for this tutorial is /home/bodhi/lxc , so config.ubuntu and rootfs.ubuntu are both located in /home/bodhi/lxc

Make a rootfs via debootstrap

debootstrap –variant=minbase karmic rootfs.ubuntu # two – - in front of “- -variant”

Configure the container

Copy resolv.conf from host node to container

cp /etc/resolv.conf rootfs.ubuntu/etc

Fix devices in rootfs.ubuntu/dev

udev does not run in lxc containers, so you need to manually make the needed devices.

I use this script to configure the devices:

#!/bin/bash

# bodhi.zazen's lxc-config
# Makes default devices needed in lxc containers
# modified from http://lxc.teegra.net/

ROOT=$(pwd)
DEV=${ROOT}/dev
if [ $ROOT = '/' ]; then
printf "\033[22;35m\nDO NOT RUN ON THE HOST NODE\n\n"
tput sgr0
exit 1
fi
if [ ! -d $DEV ]; then
printf "\033[01;33m\nRun this script in rootfs\n\n"
tput sgr0
exit 1
fi
rm -rf ${DEV}
mkdir ${DEV}
mknod -m 666 ${DEV}/null c 1 3
mknod -m 666 ${DEV}/zero c 1 5
mknod -m 666 ${DEV}/random c 1 8
mknod -m 666 ${DEV}/urandom c 1 9
mkdir -m 755 ${DEV}/pts
mkdir -m 1777 ${DEV}/shm
mknod -m 666 ${DEV}/tty c 5 0
mknod -m 666 ${DEV}/tty0 c 4 0
mknod -m 666 ${DEV}/tty1 c 4 1
mknod -m 666 ${DEV}/tty2 c 4 2
mknod -m 666 ${DEV}/tty3 c 4 3
mknod -m 666 ${DEV}/tty4 c 4 4
mknod -m 600 ${DEV}/console c 5 1
mknod -m 666 ${DEV}/full c 1 7
mknod -m 600 ${DEV}/initctl p
mknod -m 666 ${DEV}/ptmx c 5 2

exit 0

The script is very slightly modified from This page and is saved in /usr/local/bin/lxc-config .

Make it executable :

chmod u+x /usr/local/bin/lxc-config

Run the script in rootfs.ubuntu

cd rootfs.ubuntu
/usr/local/bin/lxc-config # fix /dev

Generate a config file

I call it config.ubuntu . Make sure the following information is accurate:

container name (lxc.utsname)
network (lxc.network.ipv4)
rootfs (lxc.rootfs)


lxc.utsname = ubuntu
lxc.tty = 4
lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = br0
lxc.network.name = eth0
lxc.network.mtu = 1500
lxc.network.ipv4 = 192.168.0.0/24
lxc.rootfs = /home/bodhi/lxc/rootfs.ubuntu
lxc.cgroup.devices.deny = a
# /dev/null and zero
lxc.cgroup.devices.allow = c 1:3 rwm
lxc.cgroup.devices.allow = c 1:5 rwm
# consoles
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
# /dev/{,u}random
lxc.cgroup.devices.allow = c 1:9 rwm
lxc.cgroup.devices.allow = c 1:8 rwm
# /dev/pts/* - pts namespaces are "coming soon"
lxc.cgroup.devices.allow = c 136:* rwm
lxc.cgroup.devices.allow = c 5:2 rwm
# rtc
lxc.cgroup.devices.allow = c 254:0 rwm

Modify the rootfs

chroot into rootfs.ubuntu and configure

chroot rootfs.ubuntu

# mount /proc /sys and /dev/pts
mount -t devpts devpts /dev/pts
mount -t proc proc /proc
mount -t sysfs sysfs /sys

apt-get install --force-yes -y gpgv # two - - in front of "--force-yes"
apt-get update

# set locales
apt-get install -y language-pack-en
update-locale LANG=”en_US.UTF-8″ LANGUAGE=”en_US.UTF-8″ LC_ALL=”en_US.UTF-8″

# Add to the installed applications
apt-get install -y adduser apt-utils iproute netbase nano openssh-blacklist openssh-blacklist-extra openssh-server console-setup sudo ping

#Remove udev
apt-get remove --purge udev # two – - in front of “--purge”
rm -rf /etc/udev /lib/udev
apt-get autoremove

#Remove a few upstart scripts
cd /etc/init
rm mountall* upstart*

#Set a root passwd
passwd

# As an alternate to setting a root password, you may of course add a new user and configure sudo.

#unmount /proc /sys and /dev/pts
umount /dev/pts
umount /proc
umount /sys

#exit chroot
exit

Configure networking

edit rootfs.ubuntu/etc/network/interfaces

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 192.168.0.61
netmask 255.255.255.0
broadcast 192.168.0.255
gateway 192.168.0.1

Remove tty 5 & 6

rm rootfs.ubuntu/etc/init/tty{5,6}.conf

Fix /var/run/network/ifstate

mkdir -p rootfs.ubuntu/var/run/network
touch rootfs.ubuntu/var/run/network/ifstate

Add an upstart script to boot karmic

Save the following script as rootfs.ubuntu/etc/init/lxc.conf

# LXC – Fix init sequence to have LXC working with upstart

# description “Fix LXC container - Karmic”

start on startup

task
pre-start script
mount -t proc proc /proc
mount -t devpts devpts /dev/pts
mount -t sysfs sys /sys
mount -t tmpfs varrun /var/run
mount -t tmpfs varlock /var/lock
mkdir -p /var/run/network
touch /var/run/utmp
chmod 664 /var/run/utmp
chown root.utmp /var/run/utmp
if [ "$(find /etc/network/ -name upstart -type f)" ]; then
chmod -x /etc/network/*/upstart || true
fi
end script

script
start networking
initctl emit filesystem --no-wait
initctl emit local-filesystems --no-wait
initctl emit virtual-filesystems --no-wait
init 2
end script

Configure and start the container

Create the container:

lxc-create -f /home/bodhi/lxc/conf.ubuntu -n ubuntu
lxc-start -n ubuntu

You should now be able to access the container with either lxc-console or ssh

ssh root@192.168.0.61

lxc-console -n ubuntu

Assuming you get no error messages , you may start the container with the -d option

lxc-console -d -n ubuntu

Note: Unlike lucid (Ubuntu 10.04) I am able to start the container with the -d option (lxc-start -d -n karmic),

StumbleUpon

Posted in Linux

5 Comments »

  1. [...] View original here: Shadows of epiphany » Blog Archive » LXC Configure Ubuntu Karmic … [...]

    Pingback by Shadows of epiphany » Blog Archive » LXC Configure Ubuntu Karmic … | Just linux! — February 17, 2010 @ 2:32 am

  2. Social comments and analytics for this post…

    This post was mentioned on Twitter by planetubuntu: Bodhi.Zazen: LXC Configure Ubuntu Karmic Containers: This post is in follow up to my previous post on configuring … http://bit.ly/bfjEw9...

    Trackback by uberVU - social comments — February 26, 2010 @ 4:43 pm

  3. I have tested your script and found somme minor inaccuracy / improvement.

    You don’ have to mount:
    mount -t devpts devpts /dev/pts
    because it would mount your host pts not the guest. When doing it I was unable to ssh to my guest.

    If you preserve your rc.sysinit.conf you don’t have to do the init in lxc.conf, it will be done in rc.sysinit.

    The mkdir -p rootfs.ubuntu/var/run/network is of no use since you mount at boot a tmpfs on /var/run.

    Marc

    Comment by marc zonzon — March 7, 2010 @ 5:59 am

  4. marc zonzon : Thank you for the feedback. I will take a look at your advice and you may well be correct.

    Basically I took my openvz experience and applied it to LXC, but I am learning LXC as I go.

    Comment by bodhi.zazen — March 9, 2010 @ 11:24 am

  5. [...] Bodhi.Zazen: LXC Configure Ubuntu Karmic Containers (bodhizazen.net) [...]

    Pingback by vsftpd | HackerZ4U — March 14, 2010 @ 5:57 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment