KVM live migration with the Linux command line

KVM live migration wasn’t always possible. But now it’s easy, thanks to the Linux command line. Just make sure you meet a few important requirements.

In the past, a major difference between Kernel-based Virtual Machine (KVM) and other open source solutions was KVM’s lack of support for live migration. In modern Linux distributions, however, KVM live migration is no problem at all, using either the Linux command line or graphical virt-manager tool.

Live migration enables the movement of virtual machines (VMs) from one physical host to another without downtime. It’s often used when administrators need to perform hardware maintenance on a physical host. You can just live-migrate all VMs off the host, then live-migrate them back once maintenance is completed.

KVM live migration requirements
Performing a KVM live migration requires the the same kind of CPU on the hosts, the same kernel version and shared storage among the migrating VMs. Shared storage ensures that the host computers can access the storage back end during a KVM live migration. You can also put the disk image files on shared storage.

A storage area network (SAN) or other advanced solution is recommended, but if that doesn't work for your environment, you can also use Network File System (NFS) for shared storage. Just make sure that for all KVM hosts involved in the migration, an NFS mount exists for the directory where the disk image files are stored.

The next requirement for a solid KVM live migration is the same CPU type on all physical hosts involved. The more similar, the better, but you should at least use the same CPU architecture among hosts. Live migration from a 64-bit host to a 32-bit host, for example, won’t work. As you prepare for a KVM live migration, make sure the VMs don’t use CPU features that exist on one host but not on the other.

Lastly, a KVM live migration requires that all physical hosts use the same Linux kernel version. If not, you’re likely to have problems.

Performing a KVM live migration
After meeting these requirements, you’re ready for a KVM live migration. There are a few different options for how to proceed. If you prefer the command line, you can use the virsh command. The following command, for example, would live-migrate your machine vm1 from the current host to host kvm2:

virsh migrate --live vm1 kvm2

The memory image is then copied from the current host to the destination host, which may take a while, depending on the amount of RAM used by the VM and the speed of your network. Once the KVM live migration is complete, you can use the virsh list command on the destination host to confirm that the migrated VM is now running on that machine.

If a problem occurs during a KVM live migration, the virsh command also reports errors. (A common error is that both hosts don’t have access to the shared storage.) If any error occurs, the affected VM stays on its original host so it doesn’t lose its functionality. You can usually fix whatever problem the report shows and try the KVM live migration again.

If you don't like the Linux command line, there’s an alternative method for performing a KVM live migration. Just start the graphical virt-manager tool, select Virtual Machine and then choose Migrate from the dropdown menu. All you have to do is choose the target host from the dropdown menu, and the graphical tool will perform the KVM live migration.

About the expert
Sander van Vugt
is an independent trainer and consultant based in the Netherlands. Van Vugt is an expert in Linux high availability, virtualization and performance and has completed several projects that implement all three. He is also the writer of various Linux-related books, such as Beginning the Linux Command Line, Beginning Ubuntu Server Administration and Pro Ubuntu Server Administration.

Dig Deeper on Open source virtualization