Being able to migrate a physical machine to a container is very useful, especially when you need to update the OS or its libraries. This way, we can take a snapshot before touching the system, and if something goes wrong, we can simply revert to the snapshot.
In LXD3.X, the lxd-p2c utility was incorporated to migrate an existing machine to a container quickly and easily.
In this article, we will explain how to carry out a migration from a physical Gentoo to LXD.
We install everything necessary to compile:
We download the source code of the tool on our computer and compile it:
export GOPATH=$(go env GOPATH)
export GOPATH=/root/go
export GOPATH=$(go env GOPATH)
cd /root/go/src/github.com/lxc/lxd/
make lxd-p2c
We copy it to the server that we are going to convert to LXD:
On the server, we start the dump process indicating each of the partitions that the physical server has, with the exception of /boot since it will not be necessary as it is a CT:
S.ficheros Tamaño Usados Disp Uso% Montado en
df -h
S.ficheros Tamaño Usados Disp Uso% Montado en
/dev/sda4 39G 13G 24G 35% /
/dev/sda2 97M 22M 71M 24% /boot
/dev/mapper/volgroup_01-volumen_01 183G 117G 56G 68% /var
Generating a temporary client certificate. This may take a minute...
Admin password for https://LXDSERVER:8443:
Transferring container: CTNAME: 686.81MB (9.50MB/s)
We can see the CT on the LXD server:
| CTNAME | STOPPED | | | PERSISTENT | 0 |
We start the CT:
Remapping container filesystem
We check that it is running:
| CTNAME | RUNNING | | | PERSISTENT | 0 |
We enter the CT:
We edit the network configuration since the interface names have probably changed:
We remove the old network interface from the boot and add the new one:
rm net.enp6s0f1
rc-update del net.enp6s0f1 default
ln -s net.lo net.eth0
rc-update add net.eth0 default
To make Gentoo work correctly inside LXD, we must modify a devfs script. It may not seem necessary at first, but in future OS updates, it will cause problems if we don’t do it. We remove the ‘-’ from lxc:
keyword -docker lxc -prefix -systemd-nspawn -vserver
In addition, we comment out the terminal startup since it doesn’t make sense in a CT:
# TERMINALS
#x1:12345:respawn:/sbin/agetty 38400 console linux
#c1:12345:respawn:/sbin/agetty 38400 tty1 linux
#c2:2345:respawn:/sbin/agetty 38400 tty2 linux
#c3:2345:respawn:/sbin/agetty 38400 tty3 linux
#c4:2345:respawn:/sbin/agetty 38400 tty4 linux
#c5:2345:respawn:/sbin/agetty 38400 tty5 linux
#c6:2345:respawn:/sbin/agetty 38400 tty6 linux