Difference between revisions of "LXC Fun"

Line 1: Line 1:
 
Linux Containers, or LXC, is a Linux feature that allows Linux to run one or more isolated virtual systems (with their own network interfaces, process namespace, user namespace, and power state) using a single Linux kernel on a single server.  To learn more take a look at the [[LXC]] article.
 
Linux Containers, or LXC, is a Linux feature that allows Linux to run one or more isolated virtual systems (with their own network interfaces, process namespace, user namespace, and power state) using a single Linux kernel on a single server.  To learn more take a look at the [[LXC]] article.
  
In this Funtoo (Howto) you will be shown how to create containers, how to start, stop, freeze and unfreeze them and also some more fun parts like snapshoting and clonig. To have all this working you will have to have your lxc store to be on a btrfs system.
+
In this Howto you will be shown how to create containers, how to start, stop, freeze and unfreeze them and also some more fun parts like snapshoting and clonig. To have all this working you will have to have your lxc store (/var/lib/lxc/ and /var/lib/lxcsnaps) to be on a '''btrfs filesystem'''.
  
 
== Creating containers ==
 
== Creating containers ==
Creating containers is quite easy using lxc-templates. They are located in the /usr/share/lxc/templates directory. You can find there many distributions like archlinux, centos, debian, fedora, opensuse, ubuntu, gentoo and some more. There is also an inofficial funtoo template that can be found at https://github.com/golodhrim/lxc-funtoo/blob/master/lxc-funtoo. The script creates funtoo container, however I was not able to use it with lxc-create script from the lxc utils.  
+
Creating containers is quite easy using lxc-templates. They are located in the /usr/share/lxc/templates directory. You can find there many distributions like archlinux, centos, debian, fedora, opensuse, ubuntu, gentoo and some more. There is also an inofficial funtoo template that can be found at https://github.com/golodhrim/lxc-funtoo/blob/master/lxc-funtoo. The script creates funtoo container, however I was not able to use it with lxc-create script from the lxc utils. You have to run it as a stand-alone script.
  
 
So how do you create other containers? I am going to use a debian container for this purpose. You will have to emerge debootstrap.
 
So how do you create other containers? I am going to use a debian container for this purpose. You will have to emerge debootstrap.
Line 27: Line 27:
 
</console>
 
</console>
  
After this you can create your debian container using:
+
After installing debootstrap, you can create your debian container using:
  
 
<console>
 
<console>
###i## lxc-create -n vm1 -t debian
+
###i## lxc-create -B btrfs -n vm1 -t debian
 
debootstrap is /usr/bin/debootstrap
 
debootstrap is /usr/bin/debootstrap
 
Checking cache download in /var/cache/lxc/debian/rootfs-wheezy-armhf ...  
 
Checking cache download in /var/cache/lxc/debian/rootfs-wheezy-armhf ...  
Line 46: Line 46:
 
Timezone in container is not configured. Adjust it manually.
 
Timezone in container is not configured. Adjust it manually.
 
Root password is 'root', please change !
 
Root password is 'root', please change !
 +
</console>
 +
 +
We will see that the lxc-create command created a subvolume on BTRFS backing file system.
 +
<console>
 +
###i## btrfs sub list /
 +
---- snip ----
 +
ID 1143 gen 437 top level 5 path /var/lib/lxc/vm1/rootfs
 +
---- snip ----
 
</console>
 
</console>
  
Line 76: Line 84:
 
</console>
 
</console>
  
 +
== Freezing/unfreezing containers ==
 +
The command lxc-freeze freezes all the processes running inside the container.  The processes will be blocked until they are explicitly thawed by the lxc-unfreeze command. To freeze a previously started container use the lxc utils:
 +
 +
<console>
 +
###i## lxc-freeze -n vm1
 +
###i## lxc-info -n vm1
 +
Name:          vm1
 +
State:          FROZEN
 +
PID:            6817
 +
IP:            172.16.65.234
 +
CPU use:        2.78 seconds
 +
BlkIO use:      0 bytes
 +
Memory use:    2.47 MiB
 +
KMem use:      0 bytes
 +
Link:          veth7E1J8R
 +
TX bytes:      1.45 KiB
 +
RX bytes:      3.85 KiB
 +
Total bytes:  5.31 KiB
 +
###i## lxc-unfreeze -n vm1
 +
###i## lxc-info -n vm1
 +
Name:          vm1
 +
State:          RUNNING
 +
PID:            6817
 +
IP:            172.16.65.234
 +
CPU use:        2.78 seconds
 +
BlkIO use:      0 bytes
 +
Memory use:    2.47 MiB
 +
KMem use:      0 bytes
 +
Link:          veth7E1J8R
 +
TX bytes:      1.58 KiB
 +
RX bytes:      11.13 KiB
 +
Total bytes:  12.71 KiB
 +
</console>
 +
 +
== Clones and snapshots  ==
 +
... tbc ...
  
 
[[Category:Virtualization]]
 
[[Category:Virtualization]]

Revision as of 21:07, November 14, 2014

Linux Containers, or LXC, is a Linux feature that allows Linux to run one or more isolated virtual systems (with their own network interfaces, process namespace, user namespace, and power state) using a single Linux kernel on a single server. To learn more take a look at the LXC article.

In this Howto you will be shown how to create containers, how to start, stop, freeze and unfreeze them and also some more fun parts like snapshoting and clonig. To have all this working you will have to have your lxc store (/var/lib/lxc/ and /var/lib/lxcsnaps) to be on a btrfs filesystem.

Creating containers

Creating containers is quite easy using lxc-templates. They are located in the /usr/share/lxc/templates directory. You can find there many distributions like archlinux, centos, debian, fedora, opensuse, ubuntu, gentoo and some more. There is also an inofficial funtoo template that can be found at https://github.com/golodhrim/lxc-funtoo/blob/master/lxc-funtoo. The script creates funtoo container, however I was not able to use it with lxc-create script from the lxc utils. You have to run it as a stand-alone script.

So how do you create other containers? I am going to use a debian container for this purpose. You will have to emerge debootstrap.

# emerge -av debootstrap

 * IMPORTANT: 8 news items need reading for repository 'gentoo'.
 * Use eselect news to read news items.


These are the packages that would be merged, in order:

Calculating dependencies... done!
[ebuild  N     ] dev-perl/TimeDate-2.300.0  31 kB
[ebuild  N     ] app-arch/dpkg-1.17.10  USE="bzip2 lzma nls unicode update-alternatives zlib -dselect {-test}" 4,100 kB
[ebuild  N     ] dev-util/debootstrap-1.0.59  96 kB

Total: 3 packages (3 new), Size of downloads: 4,226 kB

Would you like to merge these packages? [Yes/No]

After installing debootstrap, you can create your debian container using:

# lxc-create -B btrfs -n vm1 -t debian
debootstrap is /usr/bin/debootstrap
Checking cache download in /var/cache/lxc/debian/rootfs-wheezy-armhf ... 
Copying rootfs to /var/lib/lxc/vm1/rootfs...Generating locales (this might take a while)...
  en_US.UTF-8... done
Generation complete.
update-rc.d: using dependency based boot sequencing
update-rc.d: using dependency based boot sequencing
update-rc.d: using dependency based boot sequencing
update-rc.d: using dependency based boot sequencing
Creating SSH2 RSA key; this may take some time ...
Creating SSH2 DSA key; this may take some time ...
Creating SSH2 ECDSA key; this may take some time ...
invoke-rc.d: policy-rc.d denied execution of restart.
Timezone in container is not configured. Adjust it manually.
Root password is 'root', please change !

We will see that the lxc-create command created a subvolume on BTRFS backing file system.

# btrfs sub list /
---- snip ----
ID 1143 gen 437 top level 5 path /var/lib/lxc/vm1/rootfs
---- snip ----

Now you are ready to do all the fun stuff with your LXCs.

Starting/stoping containers

To start a previously created container use the lxc utils:

# lxc-start -n vm1 -d
# lxc-info -n vm1
Name:           vm1
State:          RUNNING
PID:            29742
IP:             172.16.65.234
CPU use:        2.92 seconds
BlkIO use:      260.00 KiB
Memory use:     2.99 MiB
KMem use:       0 bytes
Link:           vethTN4NGU
 TX bytes:      2.33 KiB
 RX bytes:      39.54 KiB
 Total bytes:   41.87 KiB
# lxc-attach -n vm1
# root@vm1:~#
# root@vm1:~# exit
# lxc-stop -n vm1
Name:           vm1
State:          STOPPED

Freezing/unfreezing containers

The command lxc-freeze freezes all the processes running inside the container. The processes will be blocked until they are explicitly thawed by the lxc-unfreeze command. To freeze a previously started container use the lxc utils:

# lxc-freeze -n vm1
# lxc-info -n vm1
Name:           vm1
State:          FROZEN
PID:            6817
IP:             172.16.65.234
CPU use:        2.78 seconds
BlkIO use:      0 bytes
Memory use:     2.47 MiB
KMem use:       0 bytes
Link:           veth7E1J8R
 TX bytes:      1.45 KiB
 RX bytes:      3.85 KiB
 Total bytes:   5.31 KiB
# lxc-unfreeze -n vm1
# lxc-info -n vm1
Name:           vm1
State:          RUNNING
PID:            6817
IP:             172.16.65.234
CPU use:        2.78 seconds
BlkIO use:      0 bytes
Memory use:     2.47 MiB
KMem use:       0 bytes
Link:           veth7E1J8R
 TX bytes:      1.58 KiB
 RX bytes:      11.13 KiB
 Total bytes:   12.71 KiB

Clones and snapshots

... tbc ...