LXC Configure Ubuntu Karmic Containers

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),

This entry was posted in Linux and tagged , . Bookmark the permalink.

7 Responses to LXC Configure Ubuntu Karmic Containers

  1. Pingback: Shadows of epiphany » Blog Archive » LXC Configure Ubuntu Karmic … | Just linux!

  2. Pingback: uberVU - social comments

  3. marc zonzon says:

    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

  4. bodhi.zazen says:

    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.

  5. Pingback: vsftpd | HackerZ4U

  6. Massimiliano says:

    Hi.
    I took an Ubuntu chroot envinronment that I already had and than I applied all your tricks.
    It worked. Many thanks.
    I suggest to create also /etc/init/console.conf
    This is identical to /etc/init.d/tty.conf, where only the last row is different, and looks like this:
    exec /sbin/getty -8 38400 console
    By doing this, if you run the container interactive (without putting in background, you’ll be able to login).

    Only one thing is not working: powering off the machine from inside the container. The init process hangs forever. I have two container with Debian and I don’t have such problem.
    Of course, I know that I can use “lxc-stop” command.

  7. Pingback: Playing with LXC « High Tech Sorcery

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>