Migrate LXC to KVM

AlexanderR

Well-Known Member
Jan 19, 2019
31
8
48
30
Hello Everyone,

is there a way to migrate a lxc container to a KVM?

I would appreciate your help and tips :)


Greetings,

Alex
 
This is not a trivial task as there is not a single command to convert a container into a whole virtual machine. Those are two very different concepts. Creating a new virtual machine from scratch is likely to be easier.
 
hi,

you can basically

1. create an archive of / from the container rootfs
2. create a kvm guest with the same linux distribution
3. extract archive to the kvm disk
4. install necessary packages and restire necessary configs (like network or similar)

be aware this still isn't any official guide or anything like that, just a possible method to achieve what you want.
 
  • Like
Reactions: AlexanderR
hi,

you can basically

1. create an archive of / from the container rootfs
2. create a kvm guest with the same linux distribution
3. extract archive to the kvm disk
4. install necessary packages and restire necessary configs (like network or similar)

be aware this still isn't any official guide or anything like that, just a possible method to achieve what you want.

Thank you very much ;) could you maybe point me in the right direction on how to create an archive of the conainer rootfs?
 
mount the container with `pct mount CTID`. it will return you the path of the mounted container rootfs. from there, you can use any archive utility like tar or gzip to create an archive of this directory.
 
i forgot to mention, you shouldn't restore the entire archive to the kvm disk probably. just take files you need (databases, config files etc.)

otherwise it might mess up the kvm
 
  • Like
Reactions: Kingneutron
it's not (since /boot isn't filled - it uses the host kernel).

what i meant by messing up the kvm is, if the config files don't work on it or the permissions/owners of files aren't set correctly.
 
In case you want to use the rsync method, the rsync options which have served me well over the years are:

# rsync -XxAaPSH src/ dest/

I know some options are "optional", I just use that string for an easy mnemonic.
 
Hi everyone!

Had the same challenge! Here a short tutorial how I did it with a CentOS 7 Container.

* First create a new KVM with a Harddisk. I used a QEMU-Disk
* Start the VM with a Linux-Live-ISO
* Create the following partitions:
- Linux boot (ext2/3)
- Linux Data (i.e. ext4)
- swap

*set the boot-flag on the boot-partition
* shut down the KVM
* connect to host-console - I preferred to login by ssh
* mount LXC-Disk by enter
pct mount { LXC-ID }
you get the directory of the containers sysroot

* mount the new KVM-harddisk by using the following command:
modprobe nbd max_part=3
qemu-nbd -c /dev/nbd0 { KVM-Disk } // i.e.: vm-114-disk-1.qcow2
mount /dev/nbd0p2 { mount-point }
**** be careful: don't use /mnt - it's used by the host *** better use /media

* change to the sysroot-directory you got before
* copy the whole directory-structure to the mounted disk by using rync
rsync -a * { mount-point }

* unmount KVM-disk by enter
umount { mount point }
qemu-nbd -d /dev/nbd0
rmmod nbd
* unmount container-disk by
pct unmount { LXC-ID }
* now start the KVM again with a Live-ISO - should be equal to the LXC-OS
* mount the KVM-Disk i.e. /mnt
mount /dev/sda2 /mnt
mount /dev/sda1 /mnt/boot

* change sysroot by enter
mount -o bind /sys /mnt/sys
mount -o bind /dev /mnt/dev
mount -t proc /proc /mnt/proc
chroot /mnt
* install bootloader grub2
yum install grub2
* install a kernel
yum install kernel
* configure grub2-bootloader
grub2-install
grub2-mkconfig -o /boot/grub2/grub.cfg
* change the owner-id's of /bin
cd /bin
chown root:root *

* exit chroot by
* reboot in the KVM

Good luck
 
Last edited:
  • Like
Reactions: yarii and bgv2020
In case you want to use the rsync method, the rsync options which have served me well over the years are:

# rsync -XxAaPSH src/ dest/

I know some options are "optional", I just use that string for an easy mnemonic.
(I know that I am responding to an old post made back in 2019). *grins* It is nice to see that somebody else has a particular combination of `rsync` flags, just like me! :-) For the sake of conversation, mine are `-avXP` unless the filesystem does not support extended attributes, in which case is just `rsync -avP`.

For anybody trying to use the syntax that is posted, beware that the destination path must **NOT** include a closing slash. This has plagued me more times than I can remember and I have only in recent times finally managed to nail it into my brain!

Why does this matter? Since I am terrible at doing so briefly, I shall let the manual page for `rsync` do the talking for me:

A trailing slash on the source changes this behavior to
avoid creating an additional directory level at the des‐
tination. You can think of a trailing / on a source as
meaning "copy the contents of this directory" as opposed
to "copy the directory by name", but in both cases the
attributes of the containing directory are transferred
to the containing directory on the destination. In
other words, each of the following commands copies the
files in the same way, including their setting of the
attributes of /dest/foo:

rsync -av /src/foo /dest
rsync -av /src/foo/ /dest/foo

If you are really curious what the difference translates into, you can run `rsync` with `-n` switch (short for `--dry-run`) and perhaps also `-v` (verbose) to visually see what would occur upon copy. I am sure that there are times where the trailing slash is intended for the destination, but at least in my case, it is nearly always not what I intended. Just my two cents. Take it or leave it. Cheers!
 
  • Like
Reactions: Kingneutron

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!