Live migration
Live migration of virtual machines (VMs) is a powerful feature of many virtualization tools, including KVM Libvirt. It allows you to move a running VM from one host to another with minimal downtime. In this guide, we’ll walk you through a live migration of a VM from host-01
to host-02
.
Firstly, from host-01
, let’s see what VMs are currently running on host-02
:
virsh -c qemu+ssh://host-02/system list --all
If the VM you wish to migrate (vm-01
in this case) does not already exist on host-02
, you’ll need to create a directory and pool for it. Here, we’ll do this remotely from host-01
, using ssh
:
ssh userx@host-02 'ls -la /data/'
ssh userx@host-02 'mkdir /data/vm-01'
ssh userx@host-02 'ls -la /data/ | grep vm-01'
virsh -c qemu+ssh://host-02/system pool-define-as vm-01 --type dir --target /data/vm-01
virsh -c qemu+ssh://host-02/system pool-start vm-01
virsh -c qemu+ssh://host-02/system pool-autostart vm-01
virsh -c qemu+ssh://host-02/system pool-list
The commands above check the current directories in the /data/
folder on host-02
, create a new directory named vm-01
, confirm its creation, and then create, start, and set to autostart a new storage pool named vm-01
for the VM on host-02
.
Now that host-02
is prepared, you can start the live migration of vm-01
from host-01
to host-02
. This is done using the virsh migrate
command:
virsh --keepalive-interval 10 migrate --live --persistent --undefinesource \
--verbose --unsafe --copy-storage-all vm-01 qemu+ssh://host-02/system
The --live
option specifies that this is a live migration, while --persistent
ensures that the VM remains defined on the destination node after the migration. The --undefinesource
option means that the VM is undefined on the source host after the migration. The --copy-storage-all
option indicates that all disk images are copied to the destination host.
Lastly, to confirm that the live migration was successful, check the list of VMs on host-02
:
virsh -c qemu+ssh://host-02/system list
If everything has gone according to plan, vm-01
should appear in the VM list for host-02
, demonstrating that the live migration was successful.