[2024-feb-29] Sad news: Eric Layton aka Nocturnal Slacker aka vtel57 passed away on Feb 26th, shortly after hospitalization. He was one of our Wiki's most prominent admins. He will be missed.

Welcome to the Slackware Documentation Project

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
howtos:misc:lxc [2018/09/15 04:40 (UTC)] – Add some more sections montagdudehowtos:misc:lxc [2019/12/28 02:27 (UTC)] – Add running GUI applications montagdude
Line 4: Line 4:
 ===== Introduction ===== ===== Introduction =====
  
-Linux Containers, or LXC, is operating system-level method for running multiple separate isolated Linux installations (containers) on a single host. Rather than simulating the computer hardware as in true virtualization, LXC uses the cgroups and namespaces functionality of the host's Linux kernel to provide strong isolation of the container. It is an intermediate solution between chroots and full virtualization, having a small impact on system resource usage similar to chroots, but providing better isolation. They provide a very convenient way to, among other things, maintain a clean build environment or test software against different [Linux] OS versions.+Linux Containers, or LXC, is operating system-level method for running multiple separate isolated Linux installations (containers) on a single host. Rather than simulating the computer hardware as in true virtualization, LXC uses the cgroups and namespaces functionalities of the host's Linux kernel to provide strong isolation of the container. It is an intermediate solution between chroots and full virtualization, having a small impact on system resource usage similar to chroots, but providing better isolation. It provides a very convenient way to, among other things, maintain a clean build environment or test software against different [Linux] OS versions.
  
 ===== Setting up a Network Bridge ===== ===== Setting up a Network Bridge =====
Line 36: Line 36:
 </code> </code>
  
-The name of the bridge, gateway, netmask, and many other parameters can be modified in /etc/default/lxc/lxc-net. For a complete list, simply open up /usr/libexec/lxc/lxc-net in your favorite editor or pager; there is a comment indicating which variables can be changed.+The name of the bridge, gateway, netmask, and many other parameters can be modified by setting the proper variables in /etc/default/lxc/lxc-net. For a complete list, simply open up /usr/libexec/lxc/lxc-net in your favorite editor or pager; there is a comment indicating which variables can be changed. Set them in /etc/default/lxc/lxc-net rather than modifying /usr/libexec/lxc/lxc-net directly.
  
 To actually use this network within the container, there are a few steps that will need to be taken during the initial creation and setup of the container, which will be covered in the next section. To actually use this network within the container, there are a few steps that will need to be taken during the initial creation and setup of the container, which will be covered in the next section.
Line 76: Line 76:
 </code> </code>
  
-When lxc-create is executed, the container will be created at lxcpath/container_name, and slackpkg will download and install the packages needed for a minimal install. The mirror will also be appended to /etc/slackpkg/mirrors.+When lxc-create is executed, the container will be created at $lxcpath/container_name, and slackpkg will download and install the packages needed for a minimal install. The mirror will also be appended to /etc/slackpkg/mirrors.
  
 ===== Container Configuration ===== ===== Container Configuration =====
Line 111: Line 111:
  
 <note tip> <note tip>
-The minimal Slackware installation set up by lxc-create does not have a lot of choice in editors, but //vi// (symlink to //elvis//) is available. If that's not your cup of tea, you can always edit the container's config files from the host using whichever editor you prefer. The container's entire filesystem is at lxcpath/container_name/rootfs.+The minimal Slackware installation set up by lxc-create does not have a lot of choice in editors, but //vi// (symlink to //elvis//) is available. If that's not your cup of tea, you can always edit the container's config files from the host using whichever editor you prefer. The container's entire filesystem is at $lxcpath/container_name/rootfs.
 </note> </note>
  
Line 142: Line 142:
 slackpkg install slackware64 slackpkg install slackware64
 </code> </code>
 +
 +==== Custom Container Boot Process ====
 +
 +In order to customize the boot process you can add or modify the init scripts listed in /usr/share/lxc/scripts/slackware.  Any changes you make to the existing scripts will be copied to /etc/rc.d/ in each freshly created LXC container.  This will allow you to easily customize the boot process if you create many LXC containers.  A good example is if you add a custom rc.inet1.conf.  It can become a bit tedious if you wish to create several LXC containers that all use DHCP network addressing.  The solution is to create your own rc.inet1.conf, set **USE_DHCP[0]="yes"**, named it rc.inet1.conf.lxc, and save it in /usr/share/lxc/scripts/slackware.  Your custom rc.inet1.conf will be copied to each new container.
  
 ==== Sharing Directories with the Host ==== ==== Sharing Directories with the Host ====
  
-Because an LXC container's filesystem is just a directory on the host, if you only need to transfer files from the host to the container, you can simply copy them over. However, often you will want to share the files both ways, or rather, be able to access //the same files// within the container without having to actually transfer anything, and this is a little more difficult because the container is isolated. LXC provides utilities to mount host directories on containers for this purpose. We will assume that the directory to be shared is at /home/username/foo on the host. To share a user directory with the host, first log into the container and create the top-level of the directory to be shared. Then log out and stop the container. Add the following lines to the container's config file (lxcpath/container_name/config):+Because an LXC container's filesystem is just a directory on the host, if you only need to transfer files from the host to the container, you can simply copy them over. However, often you will want to share the files both ways, or rather, be able to access //the same files// within the container without having to actually transfer anything, and this is a little more difficult due to the isolation of the container. LXC provides utilities to mount host directories on containers for this purpose. We will assume that the directory to be shared is at /home/username/foo on the host. To share a user directory with the host, first log into the container and create the top-level of the directory to be shared. Then log out and stop the container. Add the following lines to the container's config file ($lxcpath/container_name/config):
  
 <code> <code>
Line 152: Line 156:
  
 Note that the second path intentionally lacks a leading slash. This is because it is a relative path -- relative to the container's rootfs. Once you start the container again and log in, you should see that the directory has been mounted at /home/container_user/foo. In general, it is best if the username on the host is the same as the container's username, because it avoids any conflicts in file ownership between the host and container user. Note that the second path intentionally lacks a leading slash. This is because it is a relative path -- relative to the container's rootfs. Once you start the container again and log in, you should see that the directory has been mounted at /home/container_user/foo. In general, it is best if the username on the host is the same as the container's username, because it avoids any conflicts in file ownership between the host and container user.
 +
 +==== Running GUI Applications ====
 +
 +Without taking additional steps, it will not be possible to run GUI applications installed in the container. The simplest way to accomplish this is to run them using SSH with X forwarding to the host. However, there are still a few steps required to make this happen. In the container, in /etc/ssh/sshd_config, set the following:
 +
 +<code>
 +X11Forwarding yes                                                                                                                      
 +X11UseLocalhost yes
 +</code>
 +
 +This will allow X forwarding via SSH from the container. (Note: X11UseLocalhost yes is required; otherwise X will give an error about the DISPLAY not existing.) It is also necessary to bind-mount the host's /tmp/.X11-unix directory in the container. Add this line to the container's config file ($lxcpath/container_name/config):
 +
 +<code>
 +lxc.mount.entry = /tmp/.X11-unix tmp/.X11-unix none bind,optional,create=dir
 +</code>
 +
 +Next, restart the container. Upon restart, use `lxc-ls --fancy` to determine the container's IP address. You can then run GUI apps from the container as follows:
 +
 +<code>
 +ssh -Y user@IP appname
 +</code>
 +
 +This is probably the simplest way to run GUI apps in the container. However, more advanced usage is possible, including using LXC to "sandbox" applications in an unprivileged container and/or running GUI applications directly in the container without connecting over SSH. For more information, the following page is a good start:
 +
 +[[https://stgraber.org/2014/02/09/lxc-1-0-gui-in-containers/|LXC 1.0: GUI in containers [9/10]]]
  
 ====== See Also ====== ====== See Also ======
Line 162: Line 191:
  
 [[https://www.linuxquestions.org/questions/slackware-14/starting-lxc-container-in-slackware-14-2-a-4175614421/|LQ thread]] [[https://www.linuxquestions.org/questions/slackware-14/starting-lxc-container-in-slackware-14-2-a-4175614421/|LQ thread]]
 +
 +[[https://stgraber.org/2014/02/09/lxc-1-0-gui-in-containers/|LXC 1.0: GUI in containers [9/10]]]
  
 ====== Sources ====== ====== Sources ======
 howtos:misc:lxc ()