How to migrate containers from LXC to LXD

If you are using LXC and want to migrate all or some of your LXC containers to a LXD installation on the same machine, you can use the lxc-to-lxd tool. The LXC containers must exist on the same machine as the LXD server.

The tool analyzes the LXC configuration and copies the data and configuration of your existing LXC containers into new LXD containers.

Note

Alternatively, you can use the lxd-migrate tool within a LXC container to migrate it to LXD (see How to import physical or virtual machines to LXD instances). However, this tool does not migrate any of the LXC container configuration.

Get the tool

If you’re using the snap, the lxc-to-lxd is automatically installed. It is available as lxd.lxc-to-lxd.

Note

The lxd.lxc-to-lxd command was last included in the 5.0 snap which should be installed to do the conversion from lxc to lxd:

    sudo install lxd --channel=5.0/stable
    sudo lxd.lxc-to-lxd --all

After successfully running the lxd.lxc-to-lxd command, you can then switch to a newer snap channel if desired, like the 5.21 one:

    sudo refresh lxd --channel=5.21/stable

Otherwise, make sure that you have go (Go) installed and get the tool with the following command:

go install github.com/canonical/lxd/lxc-to-lxd@latest

Prepare your LXC containers

You can migrate one container at a time or all of your LXC containers at the same time.

Note

Migrated containers use the same name as the original containers. You cannot migrate containers with a name that already exists as an instance name in LXD.

Therefore, rename any LXC containers that might cause name conflicts before you start the migration process.

Before you start the migration process, stop the LXC containers that you want to migrate.

Start the migration process

Run sudo lxd.lxc-to-lxd [flags] to migrate the containers. (This command assumes that you are using the snap; otherwise, replace lxd.lxc-to-lxd with lxc-to-lxd, also in the following examples.)

For example, to migrate all containers:

sudo lxd.lxc-to-lxd --all

To migrate only the lxc1 container:

sudo lxd.lxc-to-lxd --containers lxc1

To migrate two containers (lxc1 and lxc2) and use the my-storage storage pool in LXD:

sudo lxd.lxc-to-lxd --containers lxc1,lxc2 --storage my-storage

To test the migration of all containers without actually running it:

sudo lxd.lxc-to-lxd --all --dry-run

To migrate all containers but limit the rsync bandwidth to 5000 KB/s:

sudo lxd.lxc-to-lxd --all --rsync-args --bwlimit=5000

Run sudo lxd.lxc-to-lxd --help to check all available flags.

Note

If you get an error that the linux64 architecture isn’t supported, either update the tool to the latest version or change the architecture in the LXC container configuration from linux64 to either amd64 or x86_64.

Check the configuration

The tool analyzes the LXC configuration and the configuration of the container (or containers) and migrates as much of the configuration as possible. You will see output similar to the following:

user@host:~$ sudo lxd.lxc-to-lxd --containers lxc1
Parsing LXC configurationChecking for unsupported LXC configuration keysChecking for existing containersChecking whether container has already been migratedValidating whether incomplete AppArmor support is enabledValidating whether mounting a minimal /dev is enabledValidating container rootfsProcessing network configurationProcessing storage configurationProcessing environment configurationProcessing container boot configurationProcessing container apparmor configurationProcessing container seccomp configurationProcessing container SELinux configurationProcessing container capabilities configurationProcessing container architecture configurationCreating containerTransferring container: lxc1: ...Container 'lxc1' successfully created

After the migration process is complete, you can check and, if necessary, update the configuration in LXD before you start the migrated LXD container.