You are on page 1of 4

##Original posting can be found here @ http://blog.bodhizazen.

net/linux/lxc-conf
igure-ubuntu-lucid-containers/

Bodhi.Zazen: LXC Configure Ubuntu Lucid Containers


In this post I will demonstrate how to use debootstrap to make a root file syste
m (rootfs) for a LXC container using Ubuntu Lucid (10.04).
Note: At the time of this post, Lucid (Ubuntu 10.04) is in the Alpha stage of de
velopment. As with all development releases, breakage may occur.
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 an
d rootfs.ubuntu are both located in /home/bodhi
Make a rootfs via debootstrap
debootstrap –variant=minbase lucid 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 dev
ices.
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/b
in/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
apt-get install –force-yes -y gpgv
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 open
ssh-blacklist-extra openssh-server console-setup sudo
#Set a root passwd
passwd
#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.60
netmask 255.255.255.0
broadcast 192.168.0.255
gateway 192.168.0.1
Remove tty4, 5, & 6
rm rootfs.ubuntu/etc/init/tty{4,5,6}.conf
Fix /var/run/network/ifstate
mkdir -p rootfs.ubuntu/var/run/network
touch rootfs.ubuntu/var/run/network/ifstate
Edit rootfs.ubuntu/lib/init/fstab
Using any editor, open rootfs.ubuntu/lib/init/fstab and comment out the followin
g line:
#none /dev devtmpfs,tmpfs mode=0755 0 0
Edit rootfs.ubuntu/etc/init/rc-sysinit.conf
Using any editor, open rootfs.ubuntu/etc/init/rc-sysinit.conf, look for the line
start on filesystem and net-device-up IFACE=lo
and change it to
start on filesystem # and net-device-up IFACE=lo
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.60
lxc-console -n ubuntu
Note: I am unable to start the container with the -d option (lxc-start -d -n luc
id), so I use screen …
screen -dmS ubuntu lxc-start -n ubuntu

You might also like