Difference between pages "Linux Containers" and "Form:FLOP"

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.

+

<noinclude>

+

__NOTOC__

−

== Status ==

+

== Add a New FLOP ==

−

As of Linux kernel 3.1.5, LXC is usable for isolating your own private workloads from one another. It is not yet ready to isolate potentially malicious users from one another or the host system. For a more mature containers solution that is appropriate for hosting environments, see [[OpenVZ]].

+

{{#forminput:form=FLOP|query string=namespace=FLOP}}

+

</noinclude><includeonly>

+

{{{for template|FLOP}}}

+

{| class="formtable"

+

!Created on:

+

| {{{field|Created on|input type=datepicker|date format=dd M yy}}}

+

|-

+

! Summary of FLOP:

+

| {{{field|Summary|}}}{{#info:Specify a short summary of the FLOP here. One or two sentences max.}}

| {{{field|Author|values from namespace=User|list|size=100}}}{{#info:Specify the FLOP author's name here. Multiple authors can be specified, separated by commas. Will autocomplete using wiki usernames.}}

| {{{field|Maintainer|values from namespace=User|list|size=100}}} {{#info:Specify the current maintainer of the FLOP. Multiple names can be specified, separated by commas. Will autocomplete using wiki usernames.}}

+

|-

+

! Organization:

+

| {{{field|Organization|values from category=Organizations}}} {{#info:Specify the organization, if any, that authored this FLOP. Note that you can create your own Organization on this wiki -- add it to the Organizations category.}}

LXC containers don't yet have their own system uptime, and they see everything that's in the host's <tt>dmesg</tt> output, among other things. But in general, the technology works.

−

== Basic Info ==

+

== Documentation ==

−

app-emulation/lxc are userspace tools for Linux containers

+

Enter documentation or extended information for the FLOP below. If you need to add more complex wikitext, use the "Edit" button after saving this page, and enter your wiki text at the bottom. This allows you to provide rich documentation content for your FLOP.

−

* Linux Containers are based on:

+

−

** Kernel namespaces for resource isolation

+

−

** CGroups for resource limitation and accounting

+

−

== Control groups ==

+

{{{standard input|free text|rows=30}}}

−

* Control groups (cgroups) in kernel since 2.6.24

+

{{{for template|FLOPFooter}}}

−

** Allows aggregation of tasks and their children

+

{{{end template}}}

−

** Subsystems (cpuset, memory, blkio,...)

+

−

** accounting - to measure how much resources certain systems use

+

−

** resource limiting - groups can be set to not exceed a set memory limit

+

−

** prioritization - some groups may get a larger share of CPU

+

−

** control - freezing/unfreezing of cgroups, checkpointing and restarting

Any kernel beyond 3.1.5 will probably work. Personally I prefer the sys-kernel/gentoo-sources-3.4.9 as these have support for all the namespaces without sacrificing the xfs, FUSE or NFS support for example. These checks were introduced later starting from kernel 3.5, this could also mean that the user namespace is not working optimally.

+

−

+

−

* User namespace (EXPERIMENTAL) depends on EXPERIMENTAL and on UIDGID_CONVERTED

+

−

** config UIDGID_CONVERTED

+

−

*** True if all of the selected software components are known to have uid_t and gid_t converted to kuid_t and kgid_t where appropriate and are otherwise safe to use with the user namespace.

** As of 3.10.xx kernel, all of the above options are safe to use with User namespaces, except for XFS_FS, therefore with kernel >=3.10.xx, you should answer XFS_FS = n, if you want User namespaces support.

+

−

+

−

==== Kernel configuration ====

+

−

These options should be enable in your kernel to be able to take full advantage of LXC.

Once you have lxc installed, you can then check your kernel config with:

+

−

<console>

+

−

# ##i##CONFIG=/path/to/config /usr/sbin/lxc-checkconfig

+

−

</console>

+

−

+

−

=== Emerge lxc ===

+

−

<console>

+

−

# ##i##emerge -av app-emulation/lxc

+

−

</console>

+

−

=== Configure Networking For Container ===

+

−

+

−

Typically, one uses a bridge to allow containers to connect to the network. This is how to do it under Funtoo Linux:

+

−

+

−

# create a bridge using the Funtoo network configuration scripts. Name the bridge something like <tt>brwan</tt> (using <tt>/etc/init.d/netif.brwan</tt>). Configure your bridge to have an IP address.

+

−

# Make your physical interface, such as <tt>eth0</tt>, an interface with no IP address (use the Funtoo <tt>interface-noip</tt> template.)

+

−

# Make <tt>netif.eth0</tt> a slave of <tt>netif.brwan</tt> in <tt>/etc/conf.d/netif.brwan</tt>.

+

−

# Enable your new bridged network and make sure it is functioning properly on the host.

+

−

+

−

You will now be able to configure LXC to automatically add your container's virtual ethernet interface to the bridge when it starts, which will connect it to your network.

+

−

+

−

== Setting up a Funtoo Linux LXC Container ==

+

−

+

−

Here are the steps required to get Funtoo Linux running <i>inside</i> a container. The steps below show you how to set up a container using an existing Funtoo Linux OpenVZ template. It is now also possible to use [[Metro]] to build an lxc container tarball directly, which will save you manual configuration steps and will provide an <tt>/etc/fstab.lxc</tt> file that you can use for your host container config. See [[Metro Recipes]] for info on how to use Metro to generate an lxc container.

+

−

+

−

=== Create and Configure Container Filesystem ===

+

−

+

−

# Start with a Funtoo LXC template, and unpack it to a directory such as <tt>/lxc/funtoo0/rootfs/</tt>

+

−

# Create an empty <tt>/lxc/funtoo0/fstab</tt> file

+

−

# Ensure <tt>c1</tt> line is uncommented (enabled) and <tt>c2</tt> through <tt>c6</tt> lines are disabled in <tt>/lxc/funtoo0/rootfs/etc/inittab</tt>

+

−

+

−

That's almost all you need to get the container filesystem ready to start.

+

−

+

−

=== Create Container Configuration Files ===

+

−

+

−

Create the following files:

+

−

+

−

==== <tt>/lxc/funtoo0/config</tt> ====

+

−

+

−

+

−

and also create symlink from

+

−

==== <tt> /lxc/funtoo0/config to /etc/lxc/funtoo0.conf </tt> ====

+

−

<console>

+

−

ln -s /lxc/funtoo0/config /etc/lxc/funtoo0.conf

+

−

</console>

+

−

+

−

{{fancynote|Daniel Robbins needs to update this config to be more in line with http://wiki.progress-linux.org/software/lxc/ -- this config appears to have nice, refined device node permissions and other goodies. // note by Havis to Daniel, this config is already superior.}}

lxc.tty = 6 # if you plan to use container with physical terminals (eg F1..F6)

+

−

#lxc.tty = 0 # set to 0 if you dont plan to use the container with physical terminal, also comment out in your containers /etc/inittab c1 to c6 respawns (e.g. c1:12345:respawn:/sbin/agetty 38400 tty1 linux)

Above, use the following command to generate a random MAC for <tt>lxc.network.hwaddr</tt>:

+

−

+

−

<pre>

+

−

# openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'

+

−

</pre>

+

−

+

−

It is a very good idea to assign a static MAC address to your container using <tt>lxc.network.hwaddr</tt>. If you don't, LXC will auto-generate a new random MAC every time your container starts, which may confuse network equipment that expects MAC addresses to remain constant.

+

−

+

−

It might happen from case to case that you aren't able to start your LXC Container with the above generated MAC address so for all these who run into that problem here is a little script that connects your IP for the container with the MAC address. Just save the following code as <tt>/etc/lxc/hwaddr.sh</tt>, make it executable and run it like <tt>/etc/lxc/hwaddr.sh xxx.xxx.xxx.xxx</tt> where xxx.xxx.xxx.xxx represents your Container IP.

+

−

+

−

<pre>

+

−

#!/bin/sh

+

−

IP=$*

+

−

HA=`printf "02:00:%x:%x:%x:%x" ${IP//./ }`

+

−

echo $HA

+

−

</pre>

+

−

+

−

==== <tt>/lxc/funtoo0/fstab</tt> ====

+

−

Note: it is now preferable to have mount entries directly in config file instead of separate fstab

+

−

+

−

<pre>

+

−

none /lxc/funtoo0/dev/pts devpts defaults 0 0

+

−

none /lxc/funtoo0/proc proc defaults 0 0

+

−

none /lxc/funtoo0/sys sysfs defaults 0 0

+

−

none /lxc/funtoo0/dev/shm tmpfs nodev,nosuid,noexec,mode=1777,rw 0 0

+

−

</pre>

+

−

+

−

== Initializing and Starting the Container ==

+

−

+

−

You will probably need to set the root password for the container before you can log in. You can use chroot to do this quickly:

+

−

+

−

<pre>

+

−

# chroot /lxc/funtoo0/rootfs

+

−

(chroot) # passwd

+

−

New password: XXXXXXXX

+

−

Retype new password: XXXXXXXX

+

−

passwd: password updated successfully

+

−

# exit

+

−

</pre>

+

−

+

−

Now that the root password is set, run:

+

−

+

−

<pre>

+

−

# lxc-start -n funtoo0 -d

+

−

</pre>

+

−

+

−

The <tt>-d</tt> option will cause it to run in the background.

+

−

+

−

To attach to the console:

+

−

+

−

<pre>

+

−

# lxc-console -n funtoo0

+

−

</pre>

+

−

+

−

You should now be able to log in and use the container. In addition, the container should now be accessible on the network.

+

−

+

−

To directly attach to container:

+

−

+

−

<pre>

+

−

# lxc-attach -n funtoo0

+

−

</pre>

+

−

+

−

To stop the container:

+

−

+

−

<pre>

+

−

# lxc-stop -n funtoo0

+

−

</pre>

+

−

+

−

Ensure that networking is working from within the container while it is running, and you're good to go!

+

−

+

−

== Starting LXC container during host boot ==

+

−

+

−

# You need to create symlink in /etc/init.d/ to /etc/init.d/lxc, so that it reflects your container.

+

−

# ln -s /etc/init.d/lxc /etc/init.d/lxc.funtoo0

+

−

# now you can add lxc.funtoo0 to default runlevel

+

−

# rc-update add lxc.funtoo0 default

+

−

<console>

+

−

# rc

+

−

* Starting funtoo0 ... [ ok ]

+

−

</console>

+

−

+

−

== LXC Bugs/Missing Features ==

+

−

+

−

This section is devoted to documenting issues with the current implementation of LXC and its associated tools. We will be gradually expanding this section with detailed descriptions of problems, their status, and proposed solutions.

+

−

+

−

=== reboot ===

+

−

+

−

By default, lxc does not support rebooting a container from within. It will simply stop and the host will not know to start it.

+

−

+

−

=== PID namespaces ===

+

−

+

−

Process ID namespaces are functional, but the container can still see the CPU utilization of the host via the system load (ie. in <tt>top</tt>).

+

−

+

−

=== /dev/pts newinstance ===

+

−

+

−

* Some changes may be required to the host to properly implement "newinstance" <tt>/dev/pts</tt>. See [https://bugzilla.redhat.com/show_bug.cgi?id=501718 This Red Hat bug].

+

−

+

−

=== lxc-create and lxc-destroy ===

+

−

+

−

* LXC's shell scripts are badly designed and are sure way to destruction, avoid using lxc-create and lxc-destroy.

+

−

+

−

=== network initialization and cleanup ===

+

−

+

−

* If used network.type = phys after lxc-stop the interface will be renamed to value from lxc.network.link. It supposed to be fixed in 0.7.4, happens still on 0.7.5 - http://www.mail-archive.com/lxc-users@lists.sourceforge.net/msg01760.html

+

−

+

−

* Re-starting a container can result in a failure as network resource are tied up from the already-defunct instance: [http://www.mail-archive.com/lxc-devel@lists.sourceforge.net/msg00824.html]

+

−

+

−

=== lxc-halt ===

+

−

+

−

* Missing tool to graceful shutdown container. 'lxc-halt' should be written and be posix sh-compatible, using lxc-execute to run halt in container.

+

−

+

−

=== funtoo ===

+

−

+

−

* Our udev should be updated to contain <tt>-lxc</tt> in scripts. (This has been done as of 02-Nov-2011, so should be resolved. But not fixed in our openvz templates, so need to regen them in a few days.)

+

−

* Our openrc should be patched to handle the case where it cannot mount tmpfs, and gracefully handle this situation somehow. (Work-around in our docs above, which is to mount tmpfs to <tt>/libexec/rc/init.d</tt> using the container-specific <tt>fstab</tt> file (on the host.)

+

−

* Emerging udev within a container can/will fail when realdev is run, if a device node cannot be created (such as /dev/console) if there are no mknod capabilities within the container. This should be fixed.

+

−

+

−

== References ==

+

−

+

−

* <tt>man 7 capabilities</tt>

+

−

* <tt>man 5 lxc.conf</tt>

+

−

+

−

== Links ==

+

−

+

−

* There are a number of additional lxc features that can be enabled via patches: [http://lxc.sourceforge.net/patches/linux/3.0.0/3.0.0-lxc1/]