How to Migrate Physical 12.04 Machine into LXC

Since I posted this question I have done this process half a dozen times without too much trouble. I thought I would add some hints from what I've learned in the hope that someone finds them useful:

All you need to do is copy the root file system into the container's rootfs directory (/var/lib/lxc/YOUR_CONTAINER/rootfs/ for disk based backing storage). Exclude /proc, /dev, /sys and /tmp.

Afterwards:

  • Make fstab empty in the container
  • Ensure a minimal set of device files under /dev. I copy the device files from a sample container that I created using a template.
  • If you are going to use lxcbr0 for networking, set the network interface to DHCP in /etc/network/interfaces
  • If you are going to bridge to a physical interface, set the interface to static or manual. If manual, put the IP address in the container's config. Do not set to DHCP.
  • be sure you set a hwaddr in your container config. If you don't LXC will randomly generate one when the container loads causing a lot of stale ARP entries on your network.
  • read through /usr/share/lxc/templates/lxc-ubuntu. Pay special attention to the configure_ubuntu() function. You probably want to run most of those commands in your container.

After the container is up, go through all the processes in pstree, removing those that aren't needed in containers (udevd, ntpdate, mdadm, etc). Feel free to remove all kernel packages.

Here is a sample container configuration for completeness. This configuration is for a trusty host. The container in this instance is precise.

lxc.include = /usr/share/lxc/config/ubuntu.common.conf
lxc.start.auto=1
lxc.rootfs = /var/lib/lxc/myhost/rootfs
lxc.mount = /var/lib/lxc/myhost/fstab
lxc.utsname = myhost
lxc.arch = amd64

# Network configuration
lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = br0
lxc.network.ipv4 = 172.27.3.13/24
lxc.network.ipv4 = 172.27.3.15/24
lxc.network.ipv4.gateway = 172.27.3.1
lxc.network.hwaddr = 52:12:a1:a5:d3:23

If the host OS is precise, you'll want this in your config as well. The common file won't be available.

lxc.devttydir = lxc
lxc.tty = 4
lxc.pts = 1024
lxc.cap.drop = sys_module mac_admin
lxc.pivotdir = lxc_putold