MaiDeveloper

How 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

centos_7_df

Let's get more information before we proceed.

vgdisplay
pvdisplay

centos_7_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

centos_7_fdisk

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.

  1. Backup the content in /home, either using rsync or tar

    • Rsync
      rsync -avP /home/ /root/home
      
    • Tar
      tar -czvf /root/home.tar.gz -C /home .
      
  2. Unmount home

    umount /dev/mapper/centos_learn-home
    
  3. Remove home logical volume

    lvremove /dev/mapper/centos_learn-home
    
  4. Create an new home logical volume

    lvcreate -L 10G -n home centos_learn
    
  5. Format home logical volume to XFS file system

    mkfs.xfs /dev/centos_learn/home
    
  6. Mount home

    mount /dev/mapper/centos_learn-home
    
  7. Restore the content in home

    • Rsync
      rsync -avP /root/home/ /home
      
    • Tar
      tar -xf /root/home.tar.gz -C /home
      

Reduce volume group size

  1. Check segments

    pvs -v --segments /dev/vda2
    

    centos_7_segments

    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
    
  2. Reduce the size

    • First we need to do a test
      pvresize -tv --setphysicalvolumesize 60G /dev/vda2
      
    • Reduce the size, if there is no error from testing
      pvresize -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

virt-filesystems

Currently, the virtual size of our qcow2 is 112G. We want to shrink it to 80G.

  1. Create output disk

    truncate -s 80G vm_learn_new.qcow2
    
  2. 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.



Mike Mai
Mike Mai   Brooklyn, New York
I am a full-stack web developer. I love to learn cutting-edge technology and solve problems. I also like photography and graphic designs.