Moving a VM to another KVM host

I have two KVM systems: one that run on my NAS, and one dedicated for KVM, which is also the one I use for lab testing.

I've found a couple of times where I've had a VM on my lab KVM that I wanted to move/migrate to my NAS (non-lab, or prod) KVM.

Not difficult to do, as both systems are set up the same way, network-wise.

In this example, we'll move a VM named linux-test01 from one KVM system to another, using virsh commands.

Dump VM information on the source system

  • List the VM:

    virsh list -all
    Id   Name                        State
    --------------------------------------------
     27   linux-test01               running
    
  • Shutdown the VM:

    virsh shutdown linux-test01
    Domain linux-test01 is being shutdown
    
  • Make sure the VM shows shut off before migrating:

    virsh list --all
     Id   Name                        State
    --------------------------------------------
     -    linux-test01                shut off
    
  • Dump the VM configuration to a XML file:

    virsh dumpxml linux-test01 > linux-test01.xml
    
  • List the network information for the VM:

    virsh domiflist linux-test01
     Interface   Type      Source     Model    MAC
    --------------------------------------------------------------
     -           direct    eno1       virtio   52:54:00:9e:95:29
     -           network   isolated   virtio   52:54:00:2c:a5:32
    
  • If you have different network configurations between the two KVM hosts, dump the VM network information to a XML file:

    virsh net-dumpxml isolated > linux-test01-isolated-net.xml
    

Import the VM data on the destination system

In this example, we transferred these files to the destination system:

  1. The image file: linux-test01.qcow2
  2. The XML dump for the VM: linux-test01.xml
  3. The XML dump for the isolated network that the VM resides on: linux-test01-net.xml
  • After transferring the image file and XML file(s), create, if necessary, the network:

    virsh net-define isolated linux-test01-isolated-net.xml
    
    virsh net-start isolated
    
    virsh net-autostart isolated
    
  • Edit the linux-test01.xml file, if necessary, for the source of the image file.

    • On the source system, KVM image files are kept in /kvm/images.

    • On the destination system, KVM images are kept in /mnt/kvm/images

    • In this example, I would need to change the "source file" location in the XML file.

    • Resulting "source file" location on the desintation KVM:

      grep "source file" linux-test01.xml
      <source file='/mnt/kvm/images/linux-test01.qcow2'/>
      
  • Import the VM:

    • This assumes that: a) The network for the VM either existed, or was created, on the KVM. b) The VM XML file was edited for the source file location. c) The VM file image is in the appropriate directory.

    • Use virsh define command to import the VM:

      virsh define linux-test01.xml
      Domain linux-test01 defined from linux-test01.xml
      

Start the imported VM

On the destination system, start the VM:

virsh start linux-test01

References

libvirt.org - man pages - virsh https://libvirt.org/manpages/virsh.html