Migrate LXC to KVM

AlexanderR

Active Member
Jan 19, 2019
31
8
28
29
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
 
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

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!