''lxc-shutdown'' should be used for clean shutdown or reboot of the container, but only the ''reboot'' is working out of the box when using systemd.

+

+

Shutdown will be signalled to the container with ''SIGPWR'' but current systemd doesn't have any services in place to handle the ''sigpwr.target''. But for the container we can simply reuse the ''poweroff.target'' and get exactly what we want.

Introduction

Synopsis

Linux Containers (LXC) are an operating system-level virtualization method for running multiple isolated server installs (containers) on a single control host. LXC does not provide a virtual machine, but rather provides a virtual environment that has its own process and network space. It is similar to a chroot, but offers much more isolation.

About this HowTo

This document is intended as an overview on setting up and deploying containers, and is not an in depth detailed instruction by instruction guide. A certain amount of prerequisite knowledge and skills are assumed (running commands as root, kernel configuration, mounting filesystems, shell scripting, chroot type environments, networking setup, etc).

Much of this was taken verbatim from Dwight Schauer, Tuxce and Ulhume. It has been copied here both to enable to community to share their collective wisdom and to expand on a few points.

Less verbose tutorial

Delerious010 21:43, 1 December 2009 (EST) I have come to realize I have added a lot of text to this HowTo. If you would like something more streamlined, please head on over to http://lxc.teegra.net/ for Dwight's excellent guide.

Testing capabilities

Once the lxc package is installed, running lxc-checkconfig will print out a list of your system's capabilities

Host configuration

Control group filesystem

LXC depends on the control group filesystem being mounted. The standard location for it is /sys/fs/cgroup. If you use systemd, the cgroup filesystem will be mounted automatically, including the default controllers, but with other initsystems you might have to do it yourself:

mount -t tmpfs none /sys/fs/cgroup

Userspace tools

Bridge device setup

The package bridge-utils, which provides the brctl command, provides the ability to set up one or more network bridges to be used by LXC. You can use brctl directly to set up the bridges, but the process is archaic and probably best avoided.

If using netcfg, you could try to refer to the netcfg wiki page, but the bridge related documentation has been removed. So when using netcfg, you will be mostly on your own.

OpenVPN has complete instructions for setting up a bridge, so it is probably your best option at the current. OpenVPN Bridge

Container configuration

Configuration file

The main configuration files are used to describe how to originally create a container. Though these files may be located anywhere, /etc/lxc is probably a good place.

23/Aug/2010: Be aware that the kernel may not handle additional whitespace in the configuration file. This has been experienced on "lxc.cgroup.devices.allow" settings but may also be true on other settings. If in doubt use only one space wherever whitespace is required.

Basic settings explained

lxc.utsname : This will be the name of the cgroup for the container. Once the container is started, you should be able to see a new folder named /cgroup/$CONTAINER_NAME.

Furthermore, this will also be the value returned by hostname from within the container. Assuming you have not removed access, the container may overwrite this with it's init script.

lxc.mount : This points to an fstab formatted file that is a listing of the mount points used when lxc-start is called. This file is further explained further

Terminal settings

The following configuration is optional. You may add them to your main configuration file if you wish to login via lxc-console, or through a terminal ( e.g.: Template:Keypress ).

The container can be configured with virtual consoles (tty devices). These may be devices from the host that the container is given permission to use (by its configuration file) or they may be devices created locally within the container.

The container's local virtual consoles may be accessed using the "lxc-console" command.

Host Virtual Consoles

The container may access the host's virtual consoles if the host is not using them and the container's configuration allows it. Typical container configuration would deny access to all devices and then allow access to specific devices like this:

For a container to be able to use a host's virtual console it must not be in use by the host. This will most likely require the host's /etc/inittab to be modified to ensure no getty or other process runs on any virtual console that is to be used by the container.

After editing the host's /etc/inittab file, issung a killall -HUP init will terminate any getty processes that are no longer configured and this will free up the virtual conosole for use by the container.

Note that local virtual consoles take precedence over host virtual consoles. This is described in the next section.

Local Virtual Consoles

The number of local virtual consoles that the container has is defined in the container's configuration file (normally on the host in /etc/lxc). It is defined thus:

lxc.tty = n

where n is the number of local virtual consoles required.

The local virtual consoles are numbered starting at tty1 and take precedence over any of the host's virtual consoles that the container might be entitled to use. This means that, for example, if n = 2 then the container will not be able to use the host's tty1 and tty2 devices even entitled to do so by its configuration file. Setting n to 0 will prevent local virtual consoles from being created thus allowing full access to any of host's virtual consoles that the container might be entitled to use.

/dev/tty Device Files

The container must have a tty device file (e.g. /dev/tty1) for each virtual console (host or local). These can be created thus:

# mknod -m 666 /dev/tty1 c 4 1
# mknod -m 666 /dev/tty2 c 4 2

and so on...

In the above, c means character device, 4 is the major device number (tty devices) and 1, 2, 3, etc., is the minor device number (specific tty device). Note that /dev/tty0 is special and always refers to the current virtual console.

If a virtual console's device file does not exist in the container, then the container cannot use the virtual console.

Configuring Log-In Ability

The container's virtual consoles may be used for login sessions if the container runs "getty" services on their tty devices. This is normally done by the container's "init" process and is configured in the container's /etc/inittab file using lines like this:

c1:2345:respawn:/sbin/agetty -8 38400 tty1 linux

There is one line per device. The first part c1 is just a unique label, the second part defines applicable run levels, the third part tells init to start a new getty when the current one terminates and the last part gives the command line for the getty. For further information refer to man init.

If there is no getty process on a virtual console it will not be possible to log in via that virtual console. A getty is not required on a virtual console unless it is to be used to log in.

If a virtual console is to allow root logins it also needs to be listed in the container's /etc/securetty file.

Troubleshooting virtual consoles

If lxc.tty is set to a number, n, then no host devices numbered n or below will be accessible even if the above configuration is present because they will be replaced with local virtual consoles instead.

A tty device file's major number will change from 4 to 136 if it is a local virtual console. This change is visible within the container but not when viewing the container's devices from the host's filesystem. This information is useful when troubleshooting.

Pseudo Terminals

lxc.pseudo = 1024

Maximum amount of pseudo terminals that may be created in /dev/pts. Currently, assuming the kernel was compiled with CONFIG_DEVPTS_MULTIPLE_INSTANCES, this tells lxc-start to mount the devpts filesystem with the newinstance flag.

Host device access settings explained

lxc.cgroup.devices.deny : By settings this to a, we are stating that the container has access to no devices unless explicitely defined within the configuration file.

Configuration file notes

At runtime /dev/ttyX devices are recreated

If you have enabled multiple DevPTS instances in your kernel, lxc-start will recreate lxc.tty amount of /dev/ttyX devices when it is executed.

This means that you will have lxc.tty amount of pseudo ttys. If you are planning on accessing the container via a "real" terminal (Template:Keypress), make sure that it is a number that is inferior to lxc.tty.

To tell whether it has been re-created, just log in to the container via either lxc-console or SSH and perform a ls -Al command on the tty. Devices with a major number of 4 are "real" tty devices whereas a major number of 136 indicates a pts.

Be aware that this is only visible from within the container itself and not from the host.

Containers have access to host's TTY nodes

If you do not properly restrict the container's access to the /dev/tty nodes, the container may have access to the host's.

Taking into consideration that, as previously mentioned, lxc-start recreates lxc.tty amount of /dev/tty devices, any tty nodes present in the container that are of a greater minor number than lxc.tty will be linked to the host's.

To access the container from a host TTY

On the host, verify no getty is started for that tty by checking /etc/inittab.

In the container, start a getty for that tty.

To prevent access to the host TTY

Via the lxc.cgroup.devices.deny = a we are preventing access to all host level devices. And then, throuh lxc.cgroup.devices.allow = c 4:1 rwm we are allowing access to the host's /dev/tty1. In the above example, simply removing all allow statements for major number 4 and minor > 1 should be sufficient.

To test this access

I may be off here, but looking at the output of the ls command below should show you both the major and minor device numbers. These are located after the user and group and represented as : 4, 2

Set lxc.tty to 1

Make there that the container has dev/tty1 and /dev/tty2

lxc-start the container

lxc-console into the container

ls -Al /dev/ttycrw------- 1 root root 4, 2 Dec 2 00:20 /dev/tty2

echo "test output" > /dev/tty2

Ctrl+Alt+F2 to view the host's second terminal

You should see "test output" printed on the screen

Configuration troubleshooting

console access denied: Permission denied

If, when executing lxc-console, you receive the error lxc-console: console access denied: Permission denied you have most likely either omitted lxc.tty or set it to 0.

lxc-console does not provide a login prompt

Though you are reaching a tty on the container, it most likely is not running a getty. You will want to double check that you have a getty defined in the container's /etc/inittab for the specific tty.

If using systemd chances are that a problem with the getty@.service script will bite you. The script only starts a getty if /dev/tty0 exists. And since this condition is not met in the container, you get no getty. Use this patch, to let lxc-console finally work.

For more than one getty you have to explicitly enable the needed service (and decrease lxc.tty in the container configuration). In the real system a configurable number of getty-services is automatically created from the systemd-logind.service

Configuring fstab

This fstab is used by lxc-start when mounting the container. As such, you can define any mount that would be possible on the host such as bind mounting to the host's own filesystem. However, please be aware of any and all security implications that this may have.

Warning : You certainly do not want to bind mount the host's /dev to the container as this would allow it to, amongst other things, reboot the host.

Container Creation and Destruction

Creation

lxc-create -f $CONTAINER_CONFIGPATH -n $CONTAINER_NAME

lxc-create will create /var/lib/lxc/$CONTAINER_NAME with a new copy of the container configuration file found in $CONTAINER_CONFIGPATH.

As such, if you need to make modifications to the container's configuration file, it's advisable to modify only the original file and then perform lxc-destroy and lxc-create operations afterwards. No data will be lost by doing this.

Note : When copying the file over, lxc-create will strip all comments from the file.

Note : As of lxc-git from atleast 2009-12-01, performing lxc-create no longer splits the config file into multiple files and folders. Therefore, we only have the configuration file to worry about.

Destruction

lxc-destroy -n $CONTAINER_NAME

This will delete /var/lib/lxc/$CONTAINER_NAME which only contains configuration files. No data will be lost.

Readying the host for virtualization

/etc/inittab

Comment out any getty that are not required

/etc/rc.sysinit replacement

Since we are running in a virtual environment, a number of steps undertaken by rc.sysinit are superfluous and may even flat out fail or stall. As such, until the initscripts are made virtualization aware, this will take some hack and slash.

/etc/rc.conf cleanup

You may want to remove any and all hardware related daemons from the DAEMONS line. Furthermore, depending on your situation, you may also want to remove the network daemon.

TBC

Known Problems

Container cannot be shutdown if using systemd

lxc-shutdown should be used for clean shutdown or reboot of the container, but only the reboot is working out of the box when using systemd.

Shutdown will be signalled to the container with SIGPWR but current systemd doesn't have any services in place to handle the sigpwr.target. But for the container we can simply reuse the poweroff.target and get exactly what we want.