I had great difficulty in getting lxd-p2c to work so I have written some notes on the process which will hopefully help someone get up and running faster. Feel free to improve these notes, put them on the wiki or whatever.
LXD 3.x introduced lxd-p2c, a go program to ease the process of migrating an existing Linux server (most likely physical, but it could be another type of VM) into an LXD container. The LXD devs have decided not to bundle lxd-p2c in either their Ubuntu packages nor the LXD snap so it has to be manually built from the LXD github repo. It is expected that the source machine is likely to not have a version of go new enough to build lxd-p2c in which case it can be built on a newer machine and then the static build transferred over.
Building lxd-p2c
You should run the exact same version of lxd-p2c on the machine being containerised as is running on the lxd server, otherwise you may get rsync errors preventing the transfer starting.
If the machine to be containerised is running a 64 bit Linux OS and your lxd server is running LXD 3.8, for example, you would build lxd-p2c like so:
~$ go get github.com/lxc/lxd/lxd-p2c
~$ cd go/src/github.com/lxc/lxd
~/go/src/github.com/lxc/lxd$ git checkout lxd-3.8
~/go/src/github.com/lxc/lxd$ go get github.com/lxc/lxd/lxd-p2c
If the build completes successfully, the lxd-p2c binary should be under ~/go/bin .
If the machine to be containerised is running a 32 bit Linux OS and youâre bullding lxd-p2c on a 64 bit OS, you would do the same but the last command would be replaced with:
$ env GOOS=linux GOARCH=386 go get github.com/lxc/lxd/lxd-p2c
Preparing to run lxd-p2c
lxd-p2c depends upon TLS to work and so it will fail if the clocks of the machine being containerised and the LXD server are out of sync so its best to ensure NTP is enabled on both beforehand.
When you run lxd-p2c, it will request you enter an admin password before the transfer begins. This is the password created when you are configuring the bridge interface to be used by lxd when you run lxd init
on the lxd server to do the initial configuration. In case you have forgot what that password was, it can be reset by running:
$ sudo lxc config set core.trust_password some-password
It is recommended to stop the active services (eg web servers, databases etc) before running lxd-p2c. Failure to do so could result in data loss or corruption. At the bare minimum you should at least stop the logging services before running lxd-p2c, which under recent releases of Ubuntu can be achieved by running:
$ sudo systemctl disable rsyslog
$ sudo systemctl stop rsyslog
$ sudo systemctl mask systemd-journald
$ sudo systemctl stop systemd-journald
Running lxd-p2c
Simply stopping rsyslog isnât enough because systemd automatically restarts rsyslog when data is written to the system log socket.Running lxd-p2cIf you only have one filesystem / mount point to containerise (the root fs) and you wanted to create a container called test, then a simple invocation of lx2-p2c would look like:
$ sudo ./lxd-p2c https://lxdserver:8443 test /
Doing the same but omitting /var/cache by passing an rsync option would look like:
$ sudo ./lxd-p2c https://lxdserver:8443 test / --rsync-args="--exclude=/var/cache"