'\" -*- coding: UTF-8 -*- .if \n(.g .ds T< \\FC .if \n(.g .ds T> \\F[\n[.fam]] .de URL \\$2 \(la\\$1\(ra\\$3 .. .if \n(.g .mso www.tmac .TH lxc 7 2023-02-19 "Version 4.0.6 " "" .SH NAME lxc \- linux containers .SH OVERVIEW The container technology is actively being pushed into the mainstream Linux kernel. It provides resource management through control groups and resource isolation via namespaces. .PP \fBlxc\fR, aims to use these new functionalities to provide a userspace container object which provides full resource isolation and resource control for an applications or a full system. .PP \fBlxc\fR is small enough to easily manage a container with simple command lines and complete enough to be used for other purposes. .SH REQUIREMENTS The kernel version >= 3.10 shipped with the distros, will work with \fBlxc\fR, this one will have less functionalities but enough to be interesting. .PP \fBlxc\fR relies on a set of functionalities provided by the kernel. The helper script \fBlxc-checkconfig\fR will give you information about your kernel configuration, required, and missing features. .SH "FUNCTIONAL SPECIFICATION" A container is an object isolating some resources of the host, for the application or system running in it. .PP The application / system will be launched inside a container specified by a configuration that is either initially created or passed as a parameter of the commands. .PP How to run an application in a container .PP Before running an application, you should know what are the resources you want to isolate. The default configuration is to isolate PIDs, the sysv IPC and mount points. If you want to run a simple shell inside a container, a basic configuration is needed, especially if you want to share the rootfs. If you want to run an application like \fBsshd\fR, you should provide a new network stack and a new hostname. If you want to avoid conflicts with some files eg. \*(T<\fI/var/run/httpd.pid\fR\*(T>, you should remount \*(T<\fI/var/run\fR\*(T> with an empty directory. If you want to avoid the conflicts in all the cases, you can specify a rootfs for the container. The rootfs can be a directory tree, previously bind mounted with the initial rootfs, so you can still use your distro but with your own \*(T<\fI/etc\fR\*(T> and \*(T<\fI/home\fR\*(T> .PP Here is an example of directory tree for \fBsshd\fR: .nf \*(T< [root@lxc sshd]$ tree \-d rootfs rootfs |\-\- bin |\-\- dev | |\-\- pts | `\-\- shm | `\-\- network |\-\- etc | `\-\- ssh |\-\- lib |\-\- proc |\-\- root |\-\- sbin |\-\- sys |\-\- usr `\-\- var |\-\- empty | `\-\- sshd |\-\- lib | `\-\- empty | `\-\- sshd `\-\- run `\-\- sshd \*(T> .fi and the mount points file associated with it: .nf \*(T< [root@lxc sshd]$ cat fstab /lib /home/root/sshd/rootfs/lib none ro,bind 0 0 /bin /home/root/sshd/rootfs/bin none ro,bind 0 0 /usr /home/root/sshd/rootfs/usr none ro,bind 0 0 /sbin /home/root/sshd/rootfs/sbin none ro,bind 0 0 \*(T> .fi .PP How to run a system in a container .PP Running a system inside a container is paradoxically easier than running an application. Why? Because you don't have to care about the resources to be isolated, everything needs to be isolated, the other resources are specified as being isolated but without configuration because the container will set them up. eg. the ipv4 address will be setup by the system container init scripts. Here is an example of the mount points file: .PP .nf \*(T< [root@lxc debian]$ cat fstab /dev /home/root/debian/rootfs/dev none bind 0 0 /dev/pts /home/root/debian/rootfs/dev/pts none bind 0 0 \*(T> .fi .SS "CONTAINER LIFE CYCLE" When the container is created, it contains the configuration information. When a process is launched, the container will be starting and running. When the last process running inside the container exits, the container is stopped. .PP In case of failure when the container is initialized, it will pass through the aborting state. .PP .nf \*(T< \-\-\-\-\-\-\-\-\- | STOPPED |<\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\-\- | | | start | | | V | \-\-\-\-\-\-\-\-\-\- | | STARTING |\-\-error\- | \-\-\-\-\-\-\-\-\-\- | | | | | V V | \-\-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\-\-\- | | RUNNING | | ABORTING | | \-\-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\-\-\- | | | | no process | | | | | V | | \-\-\-\-\-\-\-\-\-\- | | | STOPPING |<\-\-\-\-\-\-\- | \-\-\-\-\-\-\-\-\-\- | | | \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \*(T> .fi .SS CONFIGURATION The container is configured through a configuration file, the format of the configuration file is described in \fB\*(T<\fIlxc.conf\fR\*(T>\fR(5) .SS "CREATING / DESTROYING CONTAINERS" A persistent container object can be created via the \fBlxc-create\fR command. It takes a container name as parameter and optional configuration file and template. The name is used by the different commands to refer to this container. The \fBlxc-destroy\fR command will destroy the container object. .nf \*(T< lxc\-create \-n foo lxc\-destroy \-n foo \*(T> .fi .SS "VOLATILE CONTAINER" It is not mandatory to create a container object before starting it. The container can be directly started with a configuration file as parameter. .SS "STARTING / STOPPING CONTAINER" When the container has been created, it is ready to run an application / system. This is the purpose of the \fBlxc-execute\fR and \fBlxc-start\fR commands. If the container was not created before starting the application, the container will use the configuration file passed as parameter to the command, and if there is no such parameter either, then it will use a default isolation. If the application ended, the container will be stopped, but if needed the \fBlxc-stop\fR command can be used to stop the container. .PP Running an application inside a container is not exactly the same thing as running a system. For this reason, there are two different commands to run an application into a container: .nf \*(T< lxc\-execute \-n foo [\-f config] /bin/bash lxc\-start \-n foo [\-f config] [/bin/bash] \*(T> .fi .PP The \fBlxc-execute\fR command will run the specified command into a container via an intermediate process, \fBlxc-init\fR. This lxc-init after launching the specified command, will wait for its end and all other reparented processes. (to support daemons in the container). In other words, in the container, \fBlxc-init\fR has PID 1 and the first process of the application has PID 2. .PP The \fBlxc-start\fR command will directly run the specified command in the container. The PID of the first process is 1. If no command is specified \fBlxc-start\fR will run the command defined in lxc.init.cmd or if not set, \*(T<\fI/sbin/init\fR\*(T> . .PP To summarize, \fBlxc-execute\fR is for running an application and \fBlxc-start\fR is better suited for running a system. .PP If the application is no longer responding, is inaccessible or is not able to finish by itself, a wild \fBlxc-stop\fR command will kill all the processes in the container without pity. .nf \*(T< lxc\-stop \-n foo \-k \*(T> .fi .SS "CONNECT TO AN AVAILABLE TTY" If the container is configured with ttys, it is possible to access it through them. It is up to the container to provide a set of available ttys to be used by the following command. When the tty is lost, it is possible to reconnect to it without login again. .nf \*(T< lxc\-console \-n foo \-t 3 \*(T> .fi .SS "FREEZE / UNFREEZE CONTAINER" Sometime, it is useful to stop all the processes belonging to a container, eg. for job scheduling. The commands: .nf \*(T< lxc\-freeze \-n foo \*(T> .fi will put all the processes in an uninteruptible state and .nf \*(T< lxc\-unfreeze \-n foo \*(T> .fi will resume them. .PP This feature is enabled if the freezer cgroup v1 controller is enabled in the kernel. .SS "GETTING INFORMATION ABOUT CONTAINER" When there are a lot of containers, it is hard to follow what has been created or destroyed, what is running or what are the PIDs running in a specific container. For this reason, the following commands may be useful: .nf \*(T< lxc\-ls \-f lxc\-info \-n foo \*(T> .fi .PP \fBlxc-ls\fR lists containers. .PP \fBlxc-info\fR gives information for a specific container. .PP Here is an example on how the combination of these commands allows one to list all the containers and retrieve their state. .nf \*(T< for i in $(lxc\-ls \-1); do lxc\-info \-n $i done \*(T> .fi .SS "MONITORING CONTAINER" It is sometime useful to track the states of a container, for example to monitor it or just to wait for a specific state in a script. .PP \fBlxc-monitor\fR command will monitor one or several containers. The parameter of this command accepts a regular expression for example: .nf \*(T< lxc\-monitor \-n "foo|bar" \*(T> .fi will monitor the states of containers named 'foo' and 'bar', and: .nf \*(T< lxc\-monitor \-n ".*" \*(T> .fi will monitor all the containers. .PP For a container 'foo' starting, doing some work and exiting, the output will be in the form: .nf \*(T< 'foo' changed state to [STARTING] 'foo' changed state to [RUNNING] 'foo' changed state to [STOPPING] 'foo' changed state to [STOPPED] \*(T> .fi .PP \fBlxc-wait\fR command will wait for a specific state change and exit. This is useful for scripting to synchronize the launch of a container or the end. The parameter is an ORed combination of different states. The following example shows how to wait for a container if it successfully started as a daemon. .nf \*(T< # launch lxc\-wait in background lxc\-wait \-n foo \-s STOPPED & LXC_WAIT_PID=$! # this command goes in background lxc\-execute \-n foo mydaemon & # block until the lxc\-wait exits # and lxc\-wait exits when the container # is STOPPED wait $LXC_WAIT_PID echo "'foo' is finished" \*(T> .fi .SS "CGROUP SETTINGS FOR CONTAINERS" The container is tied with the control groups, when a container is started a control group is created and associated with it. The control group properties can be read and modified when the container is running by using the lxc-cgroup command. .PP \fBlxc-cgroup\fR command is used to set or get a control group subsystem which is associated with a container. The subsystem name is handled by the user, the command won't do any syntax checking on the subsystem name, if the subsystem name does not exists, the command will fail. .PP .nf \*(T< lxc\-cgroup \-n foo cpuset.cpus \*(T> .fi will display the content of this subsystem. .nf \*(T< lxc\-cgroup \-n foo cpu.shares 512 \*(T> .fi will set the subsystem to the specified value. .SH "SEE ALSO" \fBlxc\fR(7), \fBlxc-create\fR(1), \fBlxc-copy\fR(1), \fBlxc-destroy\fR(1), \fBlxc-start\fR(1), \fBlxc-stop\fR(1), \fBlxc-execute\fR(1), \fBlxc-console\fR(1), \fBlxc-monitor\fR(1), \fBlxc-wait\fR(1), \fBlxc-cgroup\fR(1), \fBlxc-ls\fR(1), \fBlxc-info\fR(1), \fBlxc-freeze\fR(1), \fBlxc-unfreeze\fR(1), \fBlxc-attach\fR(1), \fBlxc.conf\fR(5) .SH AUTHOR Daniel Lezcano <\*(T> .PP Christian Brauner <\*(T> .PP Serge Hallyn <\*(T> .PP St\('ephane Graber <\*(T>