Mar 14, 2018#Tutorial#Virtual Machine#qcow2#KVMHow to shrink qcow2 file (LVM Logical Volume Manager, XFS file system)?First we need to shrink the partition, and then shrink the qcow2 file. Guest df -h Let's get more information before we proceed. vgdisplay pvdisplay It tells us the volume group name, total allocated size, free size, and physical volume name. Name Value Volume group name centos_learn Physical volume name /dev/vda2 Allocated size 103.75G Free size 7.25G Let's get information on physical disk. fdisk -l The physical disk size /dev/vda is 120.3G. Let's review our situation. We want to shrink /dev/vda to 80G. In order to do that, we need to shrink /home volume from 50G to 10G. Resize home Centos use XFS file system which cannot resize. To workaround this, we need to remove and create an new one. Backup the content in /home, either using rsync or tar Rsyncrsync -avP /home/ /root/home Tartar -czvf /root/home.tar.gz -C /home . Unmount home umount /dev/mapper/centos_learn-home Remove home logical volume lvremove /dev/mapper/centos_learn-home Create an new home logical volume lvcreate -L 10G -n home centos_learn Format home logical volume to XFS file system mkfs.xfs /dev/centos_learn/home Mount home mount /dev/mapper/centos_learn-home Restore the content in home Rsyncrsync -avP /root/home/ /home Tartar -xf /root/home.tar.gz -C /home Reduce volume group size Check segments pvs -v --segments /dev/vda2 We want free space all the way at the end. In order to do that, we need to move other data above free space. In our situation, we need to move root logical volume. pvmove --alloc anywhere /dev/vda2:13760-26559 Reduce the size First we need to do a testpvresize -tv --setphysicalvolumesize 60G /dev/vda2 Reduce the size, if there is no error from testingpvresize -v --setphysicalvolumesize 60G /dev/vda2 Verify vgdisplay pvdisplay Shutdown Resize qcow2 file requires the guest to be shutdown. shutdown -h now Host Install virt-filesystems. yum -y install libguestfs-tools-c Check qcow2 file system virt-filesystems --long --parts --blkdevs -h -a vm_learn.qcow2 Currently, the virtual size of our qcow2 is 112G. We want to shrink it to 80G. Create output disk truncate -s 80G vm_learn_new.qcow2 Shrink /dev/sda2 virt-resize --output-format qcow2 --shrink /dev/sda2 vm_learn.qcow2 vm_learn_new.qcow2 Finally Edit guest to use the newly created qcow2 disk file. virsh edit vm_learn Make sure everything works before deleting the old disk.