Manually Migrating VM’s from one KVM host to another

Hello everyone! Been a while since I posted a blog. This one was a doozy. I tried to find this information online and there was a ton to peruse through. Luckily, I was able to peice a few of them together to finally get it working the way I needed for my environment.

So, this is what I was doing. I needed to retire a KVM host, but it was running a couple of VM’s that I couldn’t migrate using virt-manger or with virsh migrate so I decided I would try to just move the qcow2 files and build them from new.

That did not work at all.

So, after researching some solutions, I finally have one that works, and I’m going to share it with you now.

NOTE: I shared my public ssh keys between the hosts so I don’t need to type passwords in when ssh’ing and scp’ing between the hosts.

First, power off the VM on the original host if it is running:

virsh stop <vm name>

Now, I had to chown the storage file since I don’t enable root on any of my systems and I needed to scp the file from one host to the new host:

sudo chown wililupy:wililupy server.qcow2

I could then scp it to the server where I keep my vm storage.

NOTE: The path for the storage of my VM’s is the same on both hosts, if it is different for you, you are going to have to make some modifications to the xml file that is part of the next step.

scp server.qcow2 host2:/data/vms

I then ssh into the new host and chown the file back to root:root.

Back on the first host machine, I execute:

virsh dumpxml server > ~/server.xml

I then change to root using sudo and copy the NVRAM file for my host since I use UEFI for my VM’s:

sudo -s
cd /var/lib/libvirt/qemu/nvram
cp server_VARS.fd ~wililupy
exit
cd ~
sudo chown wililupy:wililupy server_VARS.fd

I then scp’d the server_VARS.fd and the server.xml files to the new host:

scp server_VARS.fd server.xml host2:~

I then ssh’d to the new host and perform the following:

virsh define server.xml
sudo chown root:root server_VARS.fd
sudo mv server_VARS.fd /var/lib/libvirt/qemu/nvram

I was then able to start my VM’s on my new host and everything worked perfectly.

virsh start server

NOTE: My new host and old host had the same network and storage for the VM paths the same, which made this integration much easier. If yours are different, you will have to modify the xml file to match your new hosts information otherwise network will not work or the VM won’t find the storage correctly.

Leave me a comment if this helps or you need some pointers!


Comments

2 responses to “Manually Migrating VM’s from one KVM host to another”

  1. Thanks for the HOWTO, was helpful. Maybe you can add the following two remarks:
    – If you use BIOS you don’t need to copy the NVRAM
    – If the CPU architecture on the new host is different, you need to adapt this info also in the .XML file. (or set to inherit from host)

  2. It worked for me, thank you

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.