This page looks best with JavaScript enabled

lxd-p2c

 ·  🎃 kr0m

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:

emerge -av dev-lang/go app-emulation/lxc dev-db/sqlite dev-go/go-sqlite3

We download the source code of the tool on our computer and compile it:

export GOPATH=/root/go
export GOPATH=$(go env GOPATH)

vi .bashrc

export GOPATH=/root/go  
export GOPATH=$(go env GOPATH)
go get github.com/lxc/lxd/lxd-p2c
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:

scp /root/go/bin/lxd-p2c SERVER:/root/

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:

df -h

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
./lxd-p2c https://LXDSERVER:8443 CTNAME / /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:

lxc list

| CTNAME             | STOPPED |                              |      | PERSISTENT | 0         |

We start the CT:

lxc start CTNAME

Remapping container filesystem

We check that it is running:

lxc list

| CTNAME             | RUNNING |                              |      | PERSISTENT | 0         |

We enter the CT:

lxc exec CTNAME -- bash -l

We edit the network configuration since the interface names have probably changed:

sed -i ’s/enp6s0f1/eth0/g’ /etc/conf.d/net

We remove the old network interface from the boot and add the new one:

cd /etc/init.d
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:

vi /etc/init.d/devfs

keyword -docker lxc -prefix -systemd-nspawn -vserver

In addition, we comment out the terminal startup since it doesn’t make sense in a CT:

vi /etc/inittab

# 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
If you liked the article, you can treat me to a RedBull here