Ubuntu 12.04.2 LTS and lxc continued

This is a continuation of my previous post here, but with more detail. In case you want to jump straight in and have a play with lxc (Linux Containers) on Ubuntu 12.04.2 LTS.

I moved to Ubuntu 12.04.2 LTS purely cause lxc seemed to be workable out of the box, thus I left behind Debian 7 for now.

Getting Started.

Installation of lxc on Ubuntu 12.04.2 LTS is a simple as running the command below;

apt-get install lxc

It will install what is needed and even configure the cgroup mount that is required (a manual step of Debian). This will install and configure a NAT 10.x.x.x network device called lxcbr0 on your host, which ALL templates use when you attempt to setup other linux containers on your host.

If you want bridged network for your linux containers, i.e. share the same network used by your hosts ethernet device, you can do the following. Requires installation of bridge-utils and configuration change to your network file.

apt-get install bridge-utils

Next you need to configure /etc/network/interfaces to ensure that your network device is now configured for bridged networking. In my case, I wanted my eth0 to be the bridged device, so you hash out all eth0 networking references. Create the additional lines below in the file;

auto br0
iface br0 inet static
 bridge_ports eth0
 bridge_fd 0
 bridge_maxwait 0
 address 192.168.4.10
 netmask 255.255.255.0
 network 192.168.4.0
 broadcast 192.168.4.255
 gateway 192.168.4.254
 dns-nameservers 192.168.4.254
 dns-search lan.heimic.net

As you can see above, I have configured a static IP assignment. If my eth0 was using 192.168.4.10, I’ve now taken it to use on br0 and would of hashed out all eth0 related configuration. Restart networking (and/or simply reboot). Be sure to have access to the machine should you break it and need to fix it via a console.

Creating Container

The command to create a container is easy, and below is a sample.

lxc-create -n lxc1 -t ubuntu

This says to create a linux container named (-n) lxc1 and use template (-t) ubuntu. This will end up being a Ubuntu 12.04.2 LTS container. Default location is /var/lib/lxc by the way, you could change this by creating a symlink to where you want them or changing the lxc configuration accordingly.

When it’s completed creating you will get told that the account to logon is “ubuntu” and password is “ubuntu”, be sure to change it.

If you want your container to make use of the bridged network and not the NAT based one which the templates default too.

Find the config file associated with your new container, if your using the default location still it will be;

/var/lib/lxc/lxc1/config

Edit the file and find the line that says “lxc.network.link=lxcbr0″ and change it to be “lxc.network.link=br0″ and save the change.

Starting Container

To start the container you just created issue the command below;

lxc-start -d -n lxc1

Once again name (-n) is passed, the -d tells it to go in background. If you don’t do this it will boot and show you the output. Good for troubleshooting, so drop the -d if you have problems. Note I haven’t worked out a way to exit from the container when I don’t pass -d, so you might have to kill your ssh session and/or halt the container to get your terminal session back.

Container Console

If you start the container using -d, you can access it’s console via the command below;

lxc-console -n lxc1

At which point you will get the logon banner for the console of the container. Logon now using the details you got during creation. Change the password.

At this point you can make changes to the linux install as needed, just like it was a normal physical install on its own dedicated hardware.

To exit the lxc-console, as it will have stated is control a + q.

Stopping Container

To shutdown down a container, you issue the command below;

lxc-halt -n lxc1

Where -n is the name of the container as always. See the trend with the commands.

Container Autostart

If you want to have the container autostart when the host is rebooted, you should go into /etc/lxc/auto and create a symlink to your containers config file. By default on Ubuntu 12.04.2 LTS this directory is looked at during system startup and any container configs found will have them autostart. Below is an example from my own environment;

root@alpha:~# cd /etc/lxc/auto
ln -s /data0/lxc/bravo/config bravo
root@alpha:/etc/lxc/auto# ls -la
total 8
drwxr-xr-x 2 root root 4096 Aug 1 13:59 .
drwxr-xr-x 3 root root 4096 Jul 31 20:59 ..
lrwxrwxrwx 1 root root 23 Aug 1 13:59 bravo -> /data0/lxc/bravo/config

If this has worked, when you run the command below, you will see the word (auto) next to the name of the container that will be start automatically when host reboots.

root@alpha:~# lxc-list
RUNNING
 bravo (auto)

FROZEN

STOPPED
 vm0

Host/Container sharing mounts/file systems

If you’d like a filesystem from your host to be available on the container, you need to have the container use a bind mount and have it come up during container start and removed during container shutdown. DO NOT MAKE THE BIND MOUNT STATIC ON HOST via /etc/fstab, as I found when I lxc-destroy my container, that it will remove data from any bind mounts.

Best way to describe the bind mount is to provide an example and what to populate in the containers config file. See below;

root@alpha:/var/lib/lxc/bravo# cat config | grep lxc.mount
lxc.mount.entry                         = /data1/cifs/backup data1/backup none defaults,bind 0 0

Which means /data1/cifs/backup from my host will be mounted at /data1/backup on the container.

root@alpha:/data0/lxc/bravo# df /data1/cifs/backup
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/alpha1-data1 3845456920 1166862816 2639526500 31% /data1

on container shown as;

root@bravo:~# df /data1/backup
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/alpha1-data1 3845456920 1166862816 2639526500 31% /data1/backup

Hope this helps get someone started, as this information was found by research, reading and putting into practice.

 

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>