Ben's notes

Linux, Unix, network, radio...

User Tools

Site Tools


migrate_kvm_vm

KVM Migrate VM to other host

Prerequisites

  • 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 127.0.0.1