How to experiment with LXC on Debian

Okay LXC is hot and getting widely used by different stack in the infrastructure.So I decided to play with it little bit and show you how you can actually do it yourself with minimal fuss.

I am sitting on Debian Wheezy and I will get debian lxc container to run it on top of host os.First thing first…..let get the lxc package from debian repo…

You don’t have to sweat a bit ,because debian package maintainers do all the hard work for you.So,just go ahead and type in console like below:

 

apt-get install lxc lxcctl

 

The outcome will lot of packages if not all the dependency already installed in your system.But, nevermind ,as I said early..all will be downloaded if you say “y” on the prompt.

Like I have mine like this :

 

root@Debian_09:59:55_Sat Feb 22:/usr/share/lxc/templates # apt-get install lxc lxctl
Reading package lists… Done
Building dependency tree
Reading state information… Done
lxc is already the newest version.
lxctl is already the newest version.
The following packages were automatically installed and are no longer required:
anthy-common aumix aumix-common bsh bsh-gcj dasher dasher-data db4.8-util dmz-cursor-theme dvd+rw-tools ekiga emacs espeak etherboot-qemu foomatic-filters-ppds freedesktop-sound-theme
freeglut3 fuse-utils g++-4.4 genisoimage gnome-desktop-data gnome-doc-utils gnome-media-common gnome-office gnome-session-canberra gnome-themes gnome-themes-extras gnome-themes-more
growisofs gstreamer0.10-fluendo-mp3 gtk2-engines-pixbuf gtk2-engines-smooth guile-1.8-libs javascript-common kaboom libanthy0 libart2.0-cil libaudiofile0 libavahi-ui0 libavcodec52
libavdevice52 libavfilter0 libavformat52 libavutil49 libbabl-0.0-0 libboost-date-time1.42.0 libboost-python1.42.0 libboost-python1.49.0 libboost-signals1.42.0 libboost-thread1.42.0
libcamel1.2-14 libcapi20-3 libcdio-cdda0 libcdio-paranoia0 libcdio10 libcdt4 libcelt0-0 libchamplain-0.4-0 libchamplain-gtk-0.4-0 libcheese-gtk18 libclutter-gtk-0.10-0 libcryptui0
libdb4.7-java libdb4.7-java-gcj libdvbpsi6 libebackend1.2-0 libebml0 libedataserver1.2-13 libeggdbus-1-0 libegroupwise1.2-13 libevince2 libexiv2-9 libexo-0.3-0 libfreerdp-plugins-standard
libfreerdp0 libfreerdp1 libgalago3 libgcr0 libgdata-google1.2-1 libgdata1.2-1 libgdata7 libgegl-0.0-0 libgexiv2-0 libgirepository1.0-0 libglade2.0-cil libglademm-2.4-1c2a libgmime-2.4-2
libgmime2.4-cil libgmp3c2 libgnome-bluetooth7 libgnome-desktop-2-17 libgnome-media0 libgnome-menu2 libgnome-vfs2.0-cil libgnome-window-settings1 libgnome2-canvas-perl libgnome2-perl
libgnome2-vfs-perl libgnome2.24-cil libgnomekbd4 libgp11-0 libgraph4 libgraphicsmagick3 libgraphite3 libgs8 libgssdp-1.0-2 libgtkglext1 libgtkhtml-editor-common libgtkhtml3.14-19
libgtkimageview0 libgtksourceview2.0-0 libgtksourceview2.0-common libgucharmap7 libgupnp-1.0-3 libgupnp-igd-1.0-3 libgvc5 libgweather1 libhunspell-1.2-0 libicu44 libimobiledevice1
libiso9660-7 libjpeg62 libjs-jquery libkpathsea5 libkprintutils4 libkutils4 liblapack3gf libmagick++3 libmagickcore3 libmagickcore3-extra libmagickwand3 libmatroska0 libmono-cairo2.0-cil
libmono-corlib2.0-cil libmono-i18n-west2.0-cil libmono-posix2.0-cil libmono-security2.0-cil libmono-sharpzip2.84-cil libmono-system2.0-cil libmozjs17d libmozjs2d libmusicbrainz3-6
libmusicbrainz4c2a libmysqlclient16 libndesk-dbus-glib1.0-cil libndesk-dbus1.0-cil libnet1 libnice0 libnm-glib2 libnm-util1 libntfs10 liboil0.3 liboobs-1-4 libopal3.10.4 libopal3.6.8
libopenspc0 libpathplan4 libpolkit-qt-1-0 libpoppler-glib4 libpoppler5 libpostproc51 libpt2.10.4 libpt2.6.7 libpython2.6 libqt4-webkit libraptor1 librasqal2 librpmio1 libserf-0-0 libslab0a
libsoundtouch1c2 libsrtp0 libstdc++6-4.4-dev libswscale0 libsysfs2 libtcltk-ruby libtcltk-ruby1.8 libtextcat-data-utf8 libthunar-vfs-1-2 libthunar-vfs-1-common libtracker-client-0.8-0
libunique-1.0-0 libvncserver0 libvpx0 libwebkit-1.0-2 libwebkit-1.0-common libwpd8c2a libwpg-0.1-1 libwps-0.1-1 libxcb-atom1 libxcb-aux0 libxcb-event1 libxcb-render-util0 libxdot4
libxfce4menu-0.1-0 libxine1 libxine1-bin libxine1-console libxine1-ffmpeg libxine1-misc-plugins libxine1-plugins libxine1-x liferea liferea-data menu-xdg min12xxw mono-2.0-gac ntfsprogs
openoffice.org-base openoffice.org-calc openoffice.org-common openoffice.org-draw openoffice.org-emailmerge openoffice.org-filter-binfilter openoffice.org-filter-mobiledev
openoffice.org-gtk openoffice.org-impress openoffice.org-java-common openoffice.org-math openoffice.org-officebean openoffice.org-style-galaxy openoffice.org-style-tango
openoffice.org-writer openvpn-blacklist phonon-backend-xine pnm2ppa postgresql-contrib-8.4 python-axiom python-bugbuddy python-clientform python-coherence python-configobj python-crypto
python-epsilon python-gdata python-gmenu python-gnomedesktop python-gtkglext1 python-gtksourceview2 python-lazr.restfulclient python-lazr.uri python-louie python-mechanize python-nevow
python-oauth python-opengl python-openssl python-pam python-pyasn1 python-pysqlite2 python-rdflib python-rsvg python-serial python-simplejson python-tagpy python-twisted-bin
python-twisted-conch python-twisted-core python-twisted-web python-wadllib python-webkit python-zope.interface remmina remmina-common remmina-plugin-rdp remmina-plugin-vnc ruby-elisp
ruby1.8-elisp rubygems1.8 software-center tcptraceroute ttf-lyx ttf-opensymbol ttf-sil-gentium update-manager-core update-manager-gnome update-notifier update-notifier-common xli xsltproc
xulrunner-1.9.1 xulrunner-10.0 xulrunner-17.0
Use ‘apt-get autoremove’ to remove them.
0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.

See,I have already got those packages …nothing to be done in my case.But if you don’t have you will be prompted for install ,probably along with the dependency.

Now,lets check what it bring down with those package ,we are going to use! fist thing you need to run this to know how lxc inflated in your host system:

 

root@Debian_12:02:13_Sat Feb 22:~ # lxc-checkconfig
Kernel config /proc/config.gz not found, looking in other places…
Found kernel config file /boot/config-3.2.0-4-686-pae
— 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 clone_children flag: enabled
Cgroup device: enabled
Cgroup sched: enabled
Cgroup cpu account: enabled
Cgroup memory controller: enabled
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

Then,

I added a line like this on my host debian /etc/fstab

cgroup  /sys/fs/cgroup  cgroup  defaults  0   0

 

O BTW when you install lxc ,it will ask you where to store your container,the default path /var/lib/lxc, but I decided to installed it on one of my lvm partition ,nothing just because of more space and out of root file system.It all depends on you where you want to put up those…mine is here

/lvm/lxc/ .

 

root@Debian_10:02:56_Sat Feb 22:~ # whereis lxc
lxc: /usr/bin/lxc /etc/lxc /usr/bin/X11/lxc /usr/share/lxc /usr/share/man/man1/lxc.1.gz /usr/share/man/man7/lxc.7.gz
root@Debian_10:03:00_Sat Feb 22:~ # cd /usr/share/lxc
root@Debian_10:03:18_Sat Feb 22:/usr/share/lxc # ls
cache  packages  templates
root@Debian_10:03:19_Sat Feb 22:/usr/share/lxc # cd templates/
root@Debian_10:03:21_Sat Feb 22:/usr/share/lxc/templates # ls
lxc-altlinux  lxc-archlinux  lxc-debconf  lxc-debconf.d  lxc-debian  lxc-fedora  lxc-opensuse  lxc-progress  lxc-progress.d  lxc-sshd  lxc-ubuntu-cloud

The bold section in the above is the predefined lxc template come along with the package you download .In this example I will deal with lxc-debian

How to create lxc template with userspace tools:

 

root@Debian_11:43:47_Sat Feb 22:~ # lxc-create -n debian-lxc -t debian
Note: Usually the template option is called with a configuration
file option too, mostly to configure the network.
For more information look at lxc.conf (5)

debootstrap is /usr/sbin/debootstrap
Checking cache download in /var/cache/lxc/debian/rootfs-wheezy-i386 …
Copying rootfs to /var/lib/lxc/debian-lxc/rootfs…
Copying rootfs to /var/lib/lxc/debian-lxc/rootfs…Generating locales (this might take a while)…
Generation complete.
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LANGUAGE = “en_IN:en”,
        LC_ALL = (unset),
        LANG = “en_IN”
    are supported and installed on your system.
perl: warning: Falling back to the standard locale (“C”).
*** update-locale: Error: invalid locale settings:  LANG=en_IN
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LANGUAGE = “en_IN:en”,
        LC_ALL = (unset),
        LANG = “en_IN”
    are supported and installed on your system.
perl: warning: Falling back to the standard locale (“C”).
update-rc.d: using dependency based boot sequencing
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LANGUAGE = “en_IN:en”,
        LC_ALL = (unset),
        LANG = “en_IN”
    are supported and installed on your system.
perl: warning: Falling back to the standard locale (“C”).
update-rc.d: using dependency based boot sequencing
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LANGUAGE = “en_IN:en”,
        LC_ALL = (unset),
        LANG = “en_IN”
    are supported and installed on your system.
perl: warning: Falling back to the standard locale (“C”).
update-rc.d: using dependency based boot sequencing
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LANGUAGE = “en_IN:en”,
        LC_ALL = (unset),
        LANG = “en_IN”
    are supported and installed on your system.
perl: warning: Falling back to the standard locale (“C”).
update-rc.d: using dependency based boot sequencing
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LANGUAGE = “en_IN:en”,
        LC_ALL = (unset),
        LANG = “en_IN”
    are supported and installed on your system.
perl: warning: Falling back to the standard locale (“C”).
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory

Current default time zone: ‘Asia/Kolkata’
Local time is now:      Sat Feb 22 11:44:48 IST 2014.
Universal Time is now:  Sat Feb 22 06:14:48 UTC 2014.

Root password is ‘root’, please change !
‘debian’ template installed
‘debian-lxc’ created

Whoa! so fast..no it was not at first time! because I have done it in the past on this same machine ,so the packages are cached so it get from there.I just destroy my previous container of debian and recreate it for the sake of showing you.But if it hand’t been done before it will simply pull all the required package and verify then show the last section of the above code!!

Now,time to get into the console and change the root passwd first.But,please take a note that when you get into the lxc console always use screen or some other tool to get in.Because CTRL-c ,CTRL-d doesn’t work properly.So , the console can hung and you can no way terminate it normally.

So here is the procedure to get into the lxc debian(in this case) console,but before that you need to start the container like below:

 

root@Debian_11:52:01_Sat Feb 22:~ # lxc-start -n debian-lxc -d

Make sure you use “d” or daemonize option to send it to background…not doing so has other effect ..which I will point out with a pointer in the below resource section.

Verify you container is up

 

root@Debian_11:52:55_Sat Feb 22:~ # lxc-list
RUNNING
debian-lxc

FROZEN

STOPPED

Okay, lets get into the container 🙂

 

root@Debian_11:53:55_Sat Feb 22:~ #lxc-console -n debian-lxc




Here is pic how it look like :

Debian-lxc

Debian-lxc

 

Hope this will help.

Resources:

https://wiki.debian.org/LXC

http://lxc.teegra.net/

http://www.pdxsys.com/articles/lxc/lxc1/

http://www.ibm.com/developerworks/library/l-lxc-containers/index.html

Cheers!

Bhaskar

PS: I am having trouble to get fedora and opensuse container going on Debian …hopefully it will resolve soon and I will post the content here.