Ben's notes

Linux, Unix, network, radio...

User Tools

Site Tools


kvm_recover_space_from_vm

Archlinux - KVM Recover space from VM

Note: A VM image on NFS storage might not work. I had success with local and iSCSI storage.

Create VM

  • Create VM with virtio-scsi controler and unmap setting:
    --disk path=$IMAGEFILE,format=qcow2,bus=scsi,discard=unmap \
    --controller scsi,model=virtio-scsi \
  • Note, use 'sda' in kickstart script, instead of 'vda'.
  • Check if trim is supported:
    $ grep "" /sys/block/sda/queue/discard_*
    /sys/block/sda/queue/discard_granularity:4096
    /sys/block/sda/queue/discard_max_bytes:1073741824
    /sys/block/sda/queue/discard_zeroes_data:0
    
    $ cat /sys/block/sda/device/scsi_disk/*/provisioning_mode
    unmap
    

Configure discard

  • Add discard option to /etc/fstab, or schedule fstrim operation.
  • Configure /etc/lvm/lvm.conf:
    issue_discards = 1
  • reboot

Test

  • Monitor VM image size. Use the -s ls option to see real usage.
  • Create large file:
    cd /var/tmp/
    time dd if=/dev/urandom of=tstfile bs=2048k count=512 oflag=direct
  • Delete tstfile
  • Trim the filesystem:
    fstrim -v /var
  • Check VM image size. You can see in the first column the size grow and shrink after fstrim.
    $ ls -ls vmcentos.qcow2
    1226824 -rw-r--r-- 1 nobody kvm 1256325120 Dec  2 21:59 vmcentos.qcow2
    1355912 -rw-r--r-- 1 nobody kvm 1388511232 Dec  2 21:59 vmcentos.qcow2
    1552968 -rw-r--r-- 1 nobody kvm 1589837824 Dec  2 21:59 vmcentos.qcow2
    1755128 -rw-r--r-- 1 nobody kvm 1796943872 Dec  2 22:00 vmcentos.qcow2
    1949832 -rw-r--r-- 1 nobody kvm 1996685312 Dec  2 22:00 vmcentos.qcow2
    2156152 -rw-r--r-- 1 nobody kvm 2207477760 Dec  2 22:00 vmcentos.qcow2
    2245256 -rw-r--r-- 1 nobody kvm 2299199488 Dec  2 22:00 vmcentos.qcow2
    2245256 -rw-r--r-- 1 nobody kvm 2299199488 Dec  2 22:00 vmcentos.qcow2
    2245256 -rw-r--r-- 1 nobody kvm 2299199488 Dec  2 22:00 vmcentos.qcow2
    2245256 -rw-r--r-- 1 nobody kvm 2299199488 Dec  2 22:00 vmcentos.qcow2
    2245256 -rw-r--r-- 1 nobody kvm 2299199488 Dec  2 22:00 vmcentos.qcow2
    1130376 -rw-r--r-- 1 nobody kvm 2299199488 Dec  2 22:00 vmcentos.qcow2
    1130376 -rw-r--r-- 1 nobody kvm 2299199488 Dec  2 22:00 vmcentos.qcow2
    1130376 -rw-r--r-- 1 nobody kvm 2299199488 Dec  2 22:00 vmcentos.qcow2
    1130376 -rw-r--r-- 1 nobody kvm 2299199488 Dec  2 22:00 vmcentos.qcow2
kvm_recover_space_from_vm.txt · Last modified: 2021/10/09 15:14 by 127.0.0.1