Live Compression of KVM VM Disk from LVM/RAW to QCOW2 Without Downtime
I’m trying to perform live migration of a KVM virtual machine from LVM storage (RAW format) to file storage (QCOW2 format) with compression, but facing several challenges:
Current Setup:
- Source: LVM volume with RAW format
- Target: File storage with QCOW2 format
- Requirements: Minimal downtime, compressed storage
What I’ve Tried:
-
Using virsh blockcopy - migrates disk but without compression:
blockcopy --domain vm-name vda --xml disk.xml --wait --pivot -
Using virsh migrate --live with storage copy - migrates but without compression:
virsh migrate --live vm-name qemu+ssh://target/system --copy-storage-all --xml new-config.xml
Result: QCOW2 file occupies 100% of virtual size like RAW
Specific Questions:
- Does libvirt have any built-in feature for live compression during storage migration?
- What’s the most reliable method to achieve true compression from LVM/RAW to QCOW2 with minimal downtime?
- Are there any hidden options in virsh migrate or virsh blockcopy that enable compression?
- How do others solve this common scenario in production environments?
I’m looking for proven production-ready solutions rather than theoretical approaches. Any insights from experience would be greatly appreciated!
Live compression of KVM VM disks from LVM/RAW to QCOW2 format without downtime is challenging because libvirt’s native migration commands don’t support compression during the migration process. The most reliable production-ready approach involves using a combination of live block migration followed by post-migration compression techniques, or using specialized tools like virt-sparsify to achieve true compression with minimal service interruption. While virsh commands like blockcopy and migrate can handle the live aspect, they don’t provide built-in compression capabilities, requiring additional steps to reduce the final QCOW2 image size.
Contents
- Understanding the Challenge
- Built-in Libvirt Capabilities and Limitations
- Live Migration Methods Without Compression
- Production-Ready Solutions
- Step-by-Step Implementation Guide
- Best Practices and Considerations
Understanding the Challenge
The core issue you’re facing stems from fundamental differences between storage formats and how migration tools work. LVM volumes with RAW format allocate space immediately, while QCOW2 uses thin provisioning and compression. When you perform live migration using standard libvirt commands, the system copies the raw data without applying any compression transformation.
As explained in the Stack Overflow discussion, the challenge is that “qemu-img convert -O qcow2” with compression flag needs to process the entire image, which typically requires downtime or significant performance impact during the conversion process.
The fundamental problem is that live migration tools focus on maintaining VM availability during the transfer, while compression operations require reading and rewriting the entire image content, which conflicts with the real-time requirements of running workloads.
Built-in Libvirt Capabilities and Limitations
Libvirt does not currently offer built-in compression capabilities during live storage migration. The libvirt documentation confirms that standard migration commands like virsh migrate and virsh blockcopy are designed for data integrity and minimal downtime, not for storage optimization.
According to Red Hat’s Virtualization Administration Guide, “Red Hat Enterprise Linux 6 supports live migration of guest virtual machines using raw and qcow2 images on shared storage” but doesn’t mention compression as a feature.
The virsh migrate --live command with --copy-storage-all option and virsh blockcopy command are the primary tools for live storage migration, but as you’ve discovered, they maintain the original data format and don’t apply compression during the transfer process.
Live Migration Methods Without Compression
Virsh Blockcopy
The virsh blockcopy command you mentioned is designed for live block-level migration:
blockcopy --domain vm-name vda --xml disk.xml --wait --pivot
As noted in the ServerFault discussion, “It can be done with the virsh blockcopy command. This can copy contents of an image to an arbitrary new location and live update QEMU to point to the new location at the right point in time.”
However, this method copies data as-is without compression, resulting in QCOW2 files that occupy their full virtual size rather than being compressed.
Virsh Migrate with Storage Copy
The virsh migrate --live command with --copy-storage-all is another approach:
virsh migrate --live vm-name qemu+ssh://target/system --copy-storage-all --xml new-config.xml
According to the Proxmox forum discussion, “if your storage is a ‘shared storage’, available to all cluster servers, you can ‘live’ move the VM to the other server, without stopping it or have clients to disconnect.” But again, no compression occurs during this process.
Production-Ready Solutions
Solution 1: Live Migration + Post-Compression
This is the most common production approach:
- Perform live migration to the target system using standard methods
- After migration, compress the QCOW2 image while the VM is running
The post-compression can be achieved using:
# While VM is running, use virt-sparsify
virt-sparsify --in-place /path/to/disk.qcow2
As mentioned in the Medium article, “from the directory where your image is stored call: virt-sparsify — in-place defenders-disk01.qcow2 · Once the command completes successfully, restart the VM”
Solution 2: Pre-Migration Compression
For better performance:
- Stop the VM temporarily
- Convert the LVM volume to compressed QCOW2:
qemu-img convert -c -O qcow2 /dev/vg_name/lv_name /path/to/compressed.qcow2
- Perform live migration of the already compressed image
As noted in the nocoast blog, “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.”
Solution 3: Block-Level Streaming with Compression
For minimal downtime scenarios:
- Start a block streaming operation
- Use external compression tools on the target
- Gradually switch to the compressed image
The QEMU wiki on LiveBlockMigration mentions that “libvirt already has implemented block_stream QMP commands, so let’s keep them unchanged if possible.”
Step-by-Step Implementation Guide
Method 1: Production-Ready Live Migration + Post-Compression
# Step 1: Perform live migration without compression
virsh migrate --live vm-name qemu+ssh://target/system --copy-storage-all
# Step 2: On target system, while VM is running
virt-sparsify --in-place /var/lib/libvirt/images/vm-name.qcow2
# Step 3: Verify compression worked
qemu-img info /var/lib/libvirt/images/vm-name.qcow2
Method 2: Minimal Downtime Approach
# Step 1: Create compressed image from LVM (requires VM stop)
qemu-img convert -c -O qcow2 /dev/vg_name/lv_name /var/lib/libvirt/images/vm-name-compressed.qcow2
# Step 2: Update VM configuration
virsh edit vm-name
# Change disk source to the new compressed file
# Step 3: Start VM on target
virsh start vm-name
Method 3: Advanced Block-Level Approach
# Step 1: Prepare target storage
virsh vol-create-as default vm-name-compressed.qcow2 --capacity 100G --format qcow2
# Step 2: Start block copy with compression (requires custom scripts)
# Note: Standard virsh doesn't support compression in blockcopy
# You may need to use QEMU monitor commands directly
Best Practices and Considerations
Performance Considerations
When using post-migration compression with virt-sparsify, performance can be impacted. As noted in the LinuxConfig article, “This image format makes use of thin provisioning, so, after we initially set the maximum virtual size of a disk, space is actually allocated only when used, but not made available back to the host when freed.”
Storage Requirements
During the migration process, you’ll need temporary storage space equal to the size of your VM disk, as both the source and target copies may exist simultaneously.
Error Handling
Always have backups ready before performing any disk conversion or migration operations. The Proxmox wiki warns: “IMPORTANT WARNING: Always have offsite backups ready, you never know!”
Monitoring
Monitor disk I/O and network performance during the migration process to ensure minimal impact on running applications.
Sources
- Live compression of KVM VM disk from LVM/RAW to QCOW2 without downtime - Stack Overflow
- Converting kvm guests from lvm to qcow2, base images and snapshots - nocoast-tech
- Migrating from RAW LVM to qcow2 - Proxmox Support Forum
- libvirt live storage migration but keep vm on same host - Server Fault
- Chapter 4. KVM Live Migration - Red Hat Enterprise Linux Documentation
- Converting LVM to qcow2 - libvirt List Archives
- How to reduce the size of KVM QCOW2 images that grow out of hand - Medium
- Shrink Qcow2 Disk Files - Proxmox VE Wiki
- How to resize a qcow2 disk image on Linux - LinuxConfig
- Features/LiveBlockMigration - QEMU Wiki
Conclusion
The challenge of live compression during KVM VM migration from LVM/RAW to QCOW2 format requires a multi-step approach since libvirt doesn’t provide built-in compression capabilities during live migration. The most production-ready solution involves performing live migration first, then applying compression tools like virt-sparsify afterward. For scenarios requiring minimal downtime, consider pre-compressing the image during a brief maintenance window. Always maintain proper backups before performing any disk operations and monitor system performance during the process. Remember that while live migration ensures zero downtime, true compression always requires some form of processing time - either during a brief service interruption or while the VM is running with potential performance impact.