Warning: session_start(): open(/tmp/sess_3212ef88aba97ddfc66fc620999e9107, O_RDWR) failed: No space left on device (28) in /var/www/html/inc/init.php on line 265

Warning: session_start(): Failed to read session data: files (path: ) in /var/www/html/inc/init.php on line 265
Writing /var/www/html/data/log/error/2024-06-17.log failed

Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/inc/init.php:265) in /var/www/html/inc/auth.php on line 432
Writing /var/www/html/data/log/error/2024-06-17.log failed

Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/inc/init.php:265) in /var/www/html/inc/actions.php on line 38
migrate_kvm_vm [Ben's notes]

Ben's notes

Linux, Unix, network, radio...

User Tools

Site Tools


KVM Migrate VM to other host


  • Restart VM on source host to be sure reboot is successful before the change.

Migration iSCSI LV to image

Example migration from iSCSI backed storage host to other host with local LVM based storage.

  • List the storage device, here 'vmns2'.
    multipath -ll vmns2
    vmns2 (3600140548f12334da9fed3985da3d6df) dm-14 SYNOLOGY,iSCSI Storage
    size=64G features='0' hwhandler='0' wp=rw
    `-+- policy='round-robin 0' prio=1 status=active
      |- 6:0:0:17 sdo  8:224  active ready running
      `- 7:0:0:17 sdaf 65:240 active ready running
  • Shutdown the VM.
    virsh shutdown ns2
  • Create compressed image from storage. I used 'pv' to show progress.
    fdisk -l /dev/mapper/vmns2
        Disk /dev/mapper/vmns2: 68.7 GB, 68719476736 bytes
    dd if=/dev/mapper/vmns2 bs=1M | pv -s 68719476736 | gzip -c > /var/tmp/ns2-disk-backup.img.gz
  • Or use SSH direct:
    dd if=/dev/mapper/vmdesktop bs=1M | pv -s 17179869184 | gzip -c | ssh -c arcfour benst@kvm1 'cat - > /var/tmp/desktop-disk-image.gz'
  • Meanwhile… dump the xml.
    virsh dumpxml ns2 > /tmp/ns2.xml
  • Copy ns2.xml to new host and import. Edit file to change name / storage device etc…
    virsh define /tmp/ns2.xml
  • Create storage, same size as other host.
    lvcreate -L 64G -n vmns2 vg_kvm1
  • Copy image to new host, dd image to storage device.
    gunzip -c ns2-disk-backup.img.gz | pv -s 68719476736 | dd of=/dev/vg_kvm1/vmns2 bs=1M
      64GB 0:14:05 [77.5MB/s] [================================>] 100%
    134217728+0 records in
    134217728+0 records out
    68719476736 bytes (69 GB) copied, 851.545 s, 80.7 MB/s
  • Start vm on new host.
    virsh start vmns2
  • Disable autostart on old host.
    virsh autostart --disable ns2
  • Enable autostart on new host.
    virsh autostart vmns2
  • Clean up vm and storage on old host / NAS.

LV to qcow2 image

  • Use qemu-img to convert from an lvm to qcow2 format
    qemu-img convert -O qcow2 /dev/vg_name/lv_name/ /var/lib/libvirt/images/image_name.qcow2
  • If you want the image compressed add '-c' right after the word convert.
  • Edit the xml for the image
    virsh edit image_name
  • Modify the disk stanza, adding a type to the driver line; on the source line change 'dev' to 'file' and modify the path:
    driver name='qemu' type='qcow2'
    source file='/var/lib/libvirt/images/image_name.qcow2'
  • Shrink the file
    qemu-img convert -c -O qcow2 source.qcow2 shrunk.qcow2
migrate_kvm_vm.txt · Last modified: 2021/10/09 15:14 by