LXC Container based Virtualization on Debian Squeeze HowTo
Thought This will benefit those who are new to LXC Container based
Virtualization.
http://en.wikipedia.org/wiki/Lxc
http://lxc.sourceforge.net/
=====================
LXC - Container Based Virtualization
=====================
1) Installation of LXC on Squeeze
-----------------------------------------------------------------------
root@vmsrv:~# aptitude install lxc
The following NEW packages will be installed:
libcap2-bin{a} lxc
0 packages upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 164 kB of archives. After unpacking 901 kB will be used.
Do you want to continue? [Y/n/?] y
Get:1 http://ftp.ca.debian.org/debian/ squeeze/main libcap2-bin
amd64 1:2.19-3 [23.6 kB]
Get:2 http://ftp.ca.debian.org/debian/ squeeze/main lxc amd64
0.7.2-1 [141 kB]
Fetched 164 kB in 4s (36.9 kB/s)
Selecting previously deselected package libcap2-bin.
(Reading database ... 48686 files and directories currently installed.)
Unpacking libcap2-bin (from .../libcap2-bin_1%3a2.19-3_amd64.deb) ...
Selecting previously deselected package lxc.
Unpacking lxc (from .../archives/lxc_0.7.2-1_amd64.deb) ...
Processing triggers for man-db ...
Setting up libcap2-bin (1:2.19-3) ...
Setting up lxc (0.7.2-1) ...
lxc init script disabled; edit /etc/default/lxc.
root@vmsrv:~# uname -a
Linux vmsrv 2.6.32-5-amd64 #1 SMP Sat Oct 30 14:18:21 UTC 2010
x86_64 GNU/Linux
--------------------------------------------------------------------------------------------------------------------
2) Check configuration.
---------------------------------------------------------------------------------------------------
root@vmsrv:~# lxc-checkconfig
Kernel config /proc/config.gz not found, looking in other places...
Found kernel config file /boot/config-2.6.32-5-amd64
--- Namespaces ---
Namespaces: enabled
Utsname namespace: enabled
Ipc namespace: enabled
Pid namespace: enabled
User namespace: enabled
Network namespace: enabled
Multiple /dev/pts instances: enabled
--- Control groups ---
Cgroup: enabled
Cgroup namespace: enabled
Cgroup device: enabled
Cgroup sched: enabled
Cgroup cpu account: enabled
Cgroup memory controller: missing
Cgroup cpuset: enabled
--- Misc ---
Veth pair device: enabled
Macvlan: enabled
Vlan: enabled
File capabilities: enabled
Note : Before booting a new kernel, you can check its configuration
usage : CONFIG=/path/to/config /usr/bin/lxc-checkconfig
-----------------------------------------------------------------------------------------------------
3) Add a cgroup file system in fstab ( cgroup /lxc/cgroup cgroup
defaults 0 0 )
-----------------------------------------------------------------------------------------------------
root@vmsrv:~# lvcreate -n lxc -L 20G vg0
Logical volume "lxc" created
root@vmsrv:/# cd /
root@vmsrv:/# mkdir lxc
root@vmsrv:/# lvdisplay lxc
Volume group "lxc" not found
Skipping volume group lxc
root@vmsrv:/# lvdisplay /dev/vg0/lxc
--- Logical volume ---
LV Name /dev/vg0/lxc
VG Name vg0
LV UUID A8bKi2-LqTk-BrOG-KSPb-J5SL-fnLe-l9PAms
LV Write Access read/write
LV Status available
# open 0
LV Size 20.00 GiB
Current LE 5120
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:5
root@vmsrv:/# mkfs.jfs /dev/vg0/lxc
mkfs.jfs version 1.1.12, 24-Aug-2007
Warning! All data on device /dev/vg0/lxc will be lost!
Continue? (Y/N) y
\
Format completed successfully.
20971520 kilobytes total disk space.
root@vmsrv:/# mount
/dev/md1 on / type ext3 (rw,errors=remount-ro)
tmpfs on /lib/init/rw type tmpfs (rw,nosuid,mode=0755)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
udev on /dev type tmpfs (rw,mode=0755)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=620)
/dev/md0 on /boot type ext3 (rw)
/dev/mapper/vg0-home on /home type jfs (rw)
/dev/mapper/vg0-tmp on /tmp type jfs (rw)
/dev/mapper/vg0-usr on /usr type jfs (rw)
/dev/mapper/vg0-var on /var type jfs (rw)
/dev/mapper/vg0-vms on /vms type jfs (rw)
fusectl on /sys/fs/fuse/connections type fusectl (rw)
root@vmsrv:/# mkdir /lxc/cgroup
root@vmsrv:/# vi /etc/fstab
root@vmsrv:/# mount -a
root@vmsrv:/# mount
/dev/md1 on / type ext3 (rw,errors=remount-ro)
tmpfs on /lib/init/rw type tmpfs (rw,nosuid,mode=0755)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
udev on /dev type tmpfs (rw,mode=0755)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=620)
/dev/md0 on /boot type ext3 (rw)
/dev/mapper/vg0-home on /home type jfs (rw)
/dev/mapper/vg0-tmp on /tmp type jfs (rw)
/dev/mapper/vg0-usr on /usr type jfs (rw)
/dev/mapper/vg0-var on /var type jfs (rw)
/dev/mapper/vg0-vms on /vms type jfs (rw)
fusectl on /sys/fs/fuse/connections type fusectl (rw)
/dev/mapper/vg0-lxc on /lxc type jfs (rw)
cgroup on /lxc/cgroup type cgroup (rw)
root@vmsrv:~# mkdir /lxc/containers
root@vmsrv:~# rmdir /var/lib/lxc
root@vmsrv:~# ln -s /lxc/containers /var/lib/lxc
root@vmsrv:~# ls -l /var/lib/lxc
lrwxrwxrwx 1 root root 4 Nov 22 16:03 /var/lib/lxc -> /lxc/containers
-----------------------------------------------------------------------------------------------
4) Set up bridged networking for Containers to communicate to the
outside world
--------------------------------------------------------------------------------------------------------------
root@vmsrv:/lxc# cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
allow-hotplug eth0
auto br0
iface br0 inet static
bridge_ports eth0
bridge_stp off
bridge_maxwait 0
bridge_fd 0
address 172.17.10.1
netmask 255.240.0.0
network 172.16.0.0
broadcast 172.31.255.255
gateway 172.17.1.0
# dns-* options are implemented by the resolvconf package,
if installed
dns-nameservers 172.16.2.0 172.17.1.0
dns-search hifxnx.local
------------------------------------------------------------------------------------------
5) Download lxc-debian scripts from
http://git.nigel.mcnie.name/?p=lxc-debian.git;a=snapshot;h=refs/heads/master;sf=tgz
and install these scripts
http://nigel.mcnie.name/blog/a-five-minute-guide-to-linux-containers-for-debian
--------------------------------------------------------------------------------------------------------------
root@vmsrv:/var/software/lxc-debian# ls
COPYING debian default init lxc-debian lxc-halt lxc-status README
root@vmsrv:/var/software/lxc-debian# cp lxc-debian /usr/local/bin/
root@vmsrv:/var/software/lxc-debian# cp init /etc/init.d/lxc
-------------------------------------------------------------------------------------------
6) Install debootstrap
-----------------------------------------------------------------------------------------
root@vmsrv:/vms# aptitude install debootstrap
The following NEW packages will be installed:
debootstrap
0 packages upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 58.2 kB of archives. After unpacking 274 kB will be used.
Get:1 http://ftp.ca.debian.org/debian/ squeeze/main debootstrap
all 1.0.26 [58.2 kB]
Fetched 58.2 kB in 6s (9,699 B/s)
Selecting previously deselected package debootstrap.
(Reading database ... 48788 files and directories currently installed.)
Unpacking debootstrap (from .../debootstrap_1.0.26_all.deb) ...
Processing triggers for man-db ...
Setting up debootstrap (1.0.26) ...
---------------------------------------------------------------------------
7) Install a container
---------------------------------------------------------------------------
root@vmsrv:~# lxc-debian create -n mysql-master0
What hostname do you wish for this container ? [mysql-master0]
Specify the location for an extra fstab file [(none)]
'mysql-master0' created
Choose the architecture for the container (choices as for
deboostrap, e.g.: amd64, i386
Choice ? [amd64]
Specify the debian mirror to use to download the rootfs
[http://ftp.debian.org/debian]
Downloading debian minimal ...
I: Retrieving Release
I: Retrieving Packages
I: Validating Packages
I: Resolving dependencies of required packages...
I: Resolving dependencies of base packages...
I: Found additional base dependencies: adduser
debian-archive-keyring dhcp3-common gnupg gpgv libbz2-1.0 libedit2
libgdbm3 libkeyutils1 libkrb53 libncursesw5 libreadline5 libssl0.9.8
libusb-0.1-4 libwrap0 openssh-blacklist openssh-client perl
perl-modules readline-common
I: Checking component main on http://ftp.debian.org/debian...
I: Retrieving libacl1
I: Validating libacl1
......
............
I: Retrieving adduser
I: Validating adduser
I: Configuring libui-dialog-perl...
I: Base system installed successfully.
Generating locales (this might take a while)...
en_US.UTF-8... done
Generation complete.
Current default timezone: 'Asia/Kolkata'
Local time is now: Mon Nov 22 16:26:09 IST 2010.
Universal Time is now: Mon Nov 22 10:56:09 UTC 2010.
Removing any system startup links for /etc/init.d/umountfs ...
/etc/rc0.d/S40umountfs
/etc/rc6.d/S40umountfs
Removing any system startup links for /etc/init.d/hwclock.sh ...
/etc/rc0.d/K25hwclock.sh
/etc/rc6.d/K25hwclock.sh
/etc/rcS.d/S11hwclock.sh
Removing any system startup links for /etc/init.d/hwclockfirst.sh ...
/etc/rcS.d/S08hwclockfirst.sh
Done.
You can run your container with the 'lxc-start -n mysql-master0'
-----------------------------------------------------------------------------------------------------
7) Configure the container /lxc/containers/mysql-master0/config
-----------------------------------------------------------------------------------------
root@vmsrv:~# cat /lxc/containers/mysql-master0/config
lxc.utsname = mysql-master0
lxc.tty = 4
lxc.pts = 1024
lxc.rootfs = /lxc/containers/mysql-master0/rootfs
lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = br0
lxc.network.name = eth0
lxc.network.mtu = 1500
lxc.mount = /lxc/fstabs/mysql-master0.fstab
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
lxc.cgroup.devices.allow = c 136:* rwm
lxc.cgroup.devices.allow = c 5:2 rwm
# rtc
lxc.cgroup.devices.allow = c 254:0 rwm
----------------------------------------------------------------------
8) Configure Container's fstab
-----------------------------------------------------------------
root@vmsrv:/lxc/fstabs# cat /lxc/fstabs/mysql-master0.fstab
rootfs /lxc/containers/mysql-master0/rootfs
none /lxc/containers/mysql-master0/rootfs/dev/pts devpts defaults 0 0
none /lxc/containers/containers/mysql-master0/rootfs/proc proc
defaults 0 0
none /lxc/containers/mysql-master0/rootfs/sys sysfs defaults 0 0
none /lxc/containers/mysql-master0/rootfs/dev/shm tmpfs defaults 0 0
-------------------------------------------------------------------------------------------------------------
9) Configure container's Networking
--------------------------------------------------------------------------------------------------
root@vmsrv:/lxc/fstabs# cat
/lxc/containers/mysql-master0/rootfs/etc/network/interfaces
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 172.17.10.24
netmask 255.240.0.0
network 172.16.0.0
broadcast 172.31.255.255
gateway 172.17.1.0
# dns-* options are implemented by the resolvconf package,
if installed
dns-nameservers 172.16.2.0 172.17.1.0
-----------------------------------------------------------------------------------------------------------
These commands can be used to administer the container.
lxc-attach lxc-console lxc-execute lxc-kill
lxc-ps lxc-start lxc-unshare
lxc-cgroup lxc-create lxc-freeze lxc-ls
lxc-restart lxc-status lxc-version
lxc-checkconfig lxc-debian lxc-halt lxc-monitor
lxc-setcap lxc-stop lxc-wait
lxc-checkpoint lxc-destroy lxc-info lxc-netstat
lxc-setuid lxc-unfreeze
==============================================================
Thanks
-Siju
Reply to: