Virtualization (LXC Containers)

From CSCWiki
Revision as of 15:58, 28 July 2015 by Ehashman (talk | contribs) (Major LXC update, remove linux vserver)
Jump to navigation Jump to search

As of Fall 2009, we use Linux containers to maintain virtual machines, most notably caffeine, which is hosted on glomag. The various commands to manipulate Linux containers are prefixed with "lxc-"; see their individual manpages for usage.

Creating a new container

Create a new container using `lxc-create`:

# Create new container "containername" with root fs located at /vm/containername
lxc-create --dir=/vm/containername -n containername --template download

This will prompt you for distribution, release, and architecture. (Architecture *must* match host machine.)

# List containers
lxc-ls

to ensure that your container has been successfully created; it should be listed. You can also list its root directory if you like. To start it in the background and obtain a root shell, do

# Start and attach a root shell
lxc-start -d -n containername
lxc-attach -n containername

Now you're ready to configure your machine.

Networking

Networking might not be enabled on your container by default. If this is the case (easily checked with `ifconfig`), you'll need to modify its config file, located at /var/lib/lxc/containername/config:

# Network configuration
lxc.network.type = veth
lxc.network.flags = up

# that's the interface defined above in host's interfaces file
lxc.network.link = br0

# name of network device inside the container,
# defaults to eth0, you could choose a name freely
# lxc.network.name = lxcnet0

lxc.network.hwaddr = DE:AD:BE:EF:70:10 # your favourite fake MAC

# the ip may be set to 0.0.0.0/24 or skip this line
# if you like to use a dhcp client inside the container
lxc.network.ipv4 = 129.97.134.XXX/24

# define a gateway to have access to the internet
lxc.network.ipv4.gateway = 129.97.134.1

Management Quick Guide

To manage containers, use the lxc-* tools, which require root privilege. Some examples (replace caffeine with the appropriate container name):

# check if caffeine is running
lxc-info -n caffeine

# start caffeine in the background
lxc-start -d -n caffeine

# stop caffeine gracefully
lxc-halt -n caffeine

# stop caffeine forcefully
lxc-stop -n caffeine

# launch a TTY console for the container
lxc-console -n caffeine

To install Linux container support on a recent Debian (squeeze or newer) system:

  • Install the lxc and bridge-utils packages.
  • Create a bridged network interface (this can be configured in /etc/network/interfaces as though it were a normal Ethernet device, with the additional bridge_ports parameter. This is usually called br0 (can be created manually with brctl). LXC will create a virtual Ethernet device and add it to the bridge when each container starts.

To start caffeine, run the following command as root on glomag:

lxc-start -d -n caffeine

Containers are stored on the host filesystem in /var/lib/lxc (root filesystems are symlinked to the appropriate directory on /vm).