Thursday, November 26, 2015

Working with snapshot clones of containers stored on Btrfs filesystem

1. To clone a container, we use lxc-clone command. The option -B btrfs creates a Btrfs volume which later can be used for snapshot clones. Snapshot clones are similar to linked clones of Virtualbox.

# lxc-clone -B btrfs c1 c2

2. We can use -s option to create a snapshot volume.

# lxc-clone -s -B btrfs  c2 c3

Here are some useful Btrfs commands to get more information about underlying volumes of Btrfs.

1. Listing subvolumes

# btrfs subvolume list /vm
ID 257 gen 187 top level 5 path vm
ID 260 gen 168 top level 257 path c2/rootfs
ID 261 gen 168 top level 257 path c3/rootfs

2. Getting details of a subvolume

# btrfs subvolume show /vm/c2/rootfs
/vm/c2/rootfs
    Name:                  rootfs
    uuid:                  68628679-d0ae-724a-be01-5ba74e2c00b1
    Parent uuid:           -
    Creation time:         2015-11-24 17:34:33
    Object ID:             260
    Generation (Gen):      168
    Gen at creation:       161
    Parent:                257
    Top Level:             257
    Flags:                 -
    Snapshot(s):
                           vm/c3/rootfs

3. Enable quotas and see disk usage of subvolumes

# btrfs quota enable /vm

# btrfs qgroup show  /vm
qgroupid rfer      excl      
-------- ----      ----      
0/5      16384     16384     
0/257    827707392 827707392 
0/260    825425920 7421952   
0/261    825425920 7421952   
0/263    825425920 7421952

Here rfer column shows total bytes of data referred by subvolume, and excl column shows bytes of data exclusive for the volume that is not shared with other volumes.

Working with LXC containers

1. We use lxc-create command to create a new container. But it installs OS in container from the CentOS repositories available on Internet. But, to use a local repository, we can set repo environment variable for this.

# export repo="http://127.0.0.1/centos7_1503"

2. Create a container with below command, here c1 is container name.

# lxc-create -t centos -n c1

3. Commands to stop, start, and attach to console of container are

# lxc-stop -n c1
# lxc-start -n c1
# lxc-attach -n c1

4. When we stop container using lxc-stop command, it send SIGPWR signal to container. But, CentsOS 7 does not handle SIGPWR correctly to poweroff. So, here is the fix we have execute in container OS.

[root@c1 ~]# cd /usr/lib/systemd/system
[root@c1 ~]# ln -s poweroff.target sigpwr.target

5. LXC container creation script for CentOS does Minimal Install. To have a good working set of packages, configure yum and install Base group of packages.

Add following lines to file /etc/yum.repos.d/local.repo

[local]
name=local_centos7_1503
baseurl=http://192.168.2.1/centos7_1503
gpgcheck=0

Install Base group

[root@c1 ~]# yum groupinstall Base

Installing LXC from source code on a CentOS 7 host, and configuring

1. Before beginning to install LXC, the pre-requisite package libcap-devel should be installed. This assumes Yum configured on host already.

# yum install libcap-devel

2. Unzip LXC source code.

# tar -zxvf lxc-1.1.5.tar.gz
# cd lxc-1.1.5

3. Install with following commands. By default, LXC will be installed in /usr/local directory. So, we have to mention correct directory names by specifying options for ./configure command.

# ./autogen.sh
# ./configure --enable-capabilities --prefix=/usr --sysconfdir=/etc --localstatedir=/var
# make
# make install

4. Add following configuration information in /etc/sysconfig/lxc-net (new file) to configure networking for LXC host.

LXC_BRIDGE="lxcbr0"
USE_LXC_BRIDGE="true"

LXC_ADDR="192.168.2.1"
LXC_NETMASK="255.255.255.0"
LXC_DHCP_RANGE="192.168.2.70,192.168.2.99"

5. Add following line in /root/.bash_profile and /etc/init.d/lxc-net files to make LXC libraries available for startup script and environment.

# export LD_LIBRARY_PATH=/usr/lib

6. Start the service lxc-net and make it autostart during system boot.

# service lxc-net start
# chkconfig lxc-net on

7. Verify if lxcbr0 interface is showing up in ifconfig output.

# ifconfig
....
lxcbr0: flags=4163  mtu 1500
        inet 192.168.2.1  netmask 255.255.255.0  broadcast 0.0.0.0
....

8. Add following line in /etc/lxc/lxc.conf (new file) to specify where all containers' root file systems should be stored.

lxc.lxcpath = /vm