Converting a bare-metal Debian instance to run in LXC

Copy all the files

rsync -avz is enough to copy everything correctly. If converting the same host, you can run:

mkdir -p /var/lib/lxc/guestname/rootfs/
rsync --exclude=/var/lib/lxc -avz / /var/lib/lxc/guestname/rootfs/

Create LXC config for the guest

Need to create an LXC configuration file at /var/lib/lxc/guestname/config, you can take it from some other container or template and modify accordingly.


Disable all the services

The easiest way to do that is to move away the content of /etc/init.d

cd /var/lib/lxc/guestname/rootfs/etc/init.d/
mkdir _off
mv * _off/

Reenable the required ones

Move back the stuff you are sure you actually need to run inside the container, plus some of the system stuff.

What can be moved back right away is:

cd _off/
mv halt procps rc rcS reboot ssh ../


Replace inittab

The “physical” /etc/inittab is different, for instance it might not handle the poweroff signal from the host correctly (with errors such as described here).

Replace it with inittab taken from a native LXC template, which has been modified accordingly. For Debian 9 (Stretch) the working LXC /etc/inittab would be:

l0:0:wait:/etc/init.d/rc 0
l1:1:wait:/etc/init.d/rc 1
l2:2:wait:/etc/init.d/rc 2
l3:3:wait:/etc/init.d/rc 3
l4:4:wait:/etc/init.d/rc 4
l5:5:wait:/etc/init.d/rc 5
l6:6:wait:/etc/init.d/rc 6
# Normally not reached, but fallthrough in case of emergency.
1:2345:respawn:/sbin/getty 38400 console
#c1:12345:respawn:/sbin/getty 38400 tty1 linux
#c2:12345:respawn:/sbin/getty 38400 tty2 linux
#c3:12345:respawn:/sbin/getty 38400 tty3 linux
#c4:12345:respawn:/sbin/getty 38400 tty4 linux
p6::ctrlaltdel:/sbin/init 6
p0::powerfail:/sbin/init 0


lxc-convert.txt · Last modified: 2021-09-24 19:10 UTC by rm