On Sat, Nov 30, 2019 at 01:12:31 +0100, ort_libvirt(a)bergersen.no wrote:
Hi!
I need to convert a KVM virtual machine from LVM to a qcow2 image, and wonder if this is
the best way to do it (*1):
[$] qemu-img convert -O qcow2 /dev/vg_name/lv_name/
/var/lib/libvirt/images/image_name.qcow2
If this is the best way to do this, will this keep the information on:
/dev/vg_name/lv_name/ untouched?
As long as you also reconfigure the VM to use
/var/lib/libvirt/images/image_name.qcow2 after this point the old
logical volume will not be used.
The above approach is good if you are okay with some downtime on the VM
while the image is converted over.
There are a few other approaches you can conisder when you want to do it
live or for example save some space in the qcow2 image by using fstrim
first.
For a live conversion, you can do an external disk-only snapshot first
which creates a qcow2 overlay file:
virsh snapshot-create-as $VMNAME --no-metadata --disk-only --diskspec
$DISK,file=/path/to/image.qcow2 --diskspec ...
Optionally if your disk frontend supports it [1] and you've configured it
properly you can now run 'fstrim' on the filesystems. That records which
parts of the disk are empty into the overlay file and thus will not be
converted later
After that you pull the backing image into the qcow2:
virsh blockpull --wait --verbose $DOMNAME $DISK
if you executed the trim step the image will be smaller and also the
conversion step will avoid copying unused parts. Optionally that step
can be skipped and then the result will be the same as in the offline
conversion approach.
Optionally if you don't care about the trim step above you can use also
the 'blockcopy' operation instead of the two steps above to achieve the
same:
virsh blockcopy $VMNAME $DISK --dest /path/to/new.qcow2 --wait --verbose
--pivot --format qcow2 --transient-job
Also if you want to decrease the size after an offline coversion you can
use the 'virt-sparsify' tool from the libguestfs suite on the image if
the VM is offline.
[1] You must have at least qemu-4.0 if you want to use fstrim on a
virtio-blk disk interface, or switch to virtio-scsi based disks. Also
your disks must have the 'discard="unmap"' option in the <driver>
element of your vm's XML.
I've used the above approach to convert few production VMs from LVs to
qcow2 for moving them to a different host later on. Apart from a sub 1
minute downtime to restart the VM to switch to virtio-scsi from
virtio-blk to enable support for trimming the images the conversion was
then executed while the VM were running. Obviously if you don't require
this you can easily go with the qemu-img approach.