Skip to contentSkip to conversation

Trying to get fstrim / discard=unmap to work

Qemu-devel Richard W.M. Jones 2 participants 6 messages Mar 10, 2014 – Mar 12, 2014

lore
Share & tools
lore
#0 lore raw

I'm trying to get fstrim in a guest to cause the host disk to become sparse, without success so far. I wonder if anyone can see what I'm missing?

Guest:
- guest kernel: 3.13.4-200.fc20.x86_64
- ext4 guest filesystem
- fstrim from util-linux 2.24.1
- cat /sys/block/sda/device/scsi_disk/*/provisioning_mode unmap

Qemu:
- qemu from git today (e9d818b8b1a)
- Drive uses virtio-scsi
- Drive uses discard=unmap

/home/rjones/d/qemu/x86_64-softmmu/qemu-system-x86_64 -L /home/rjones/d/qemu/pc-bios -global virtio-blk-pci.scsi=off -nodefconfig -enable-fips -nodefaults -display none -machine accel=kvm:tcg -m 500 -no-reboot -rtc driftfix=slew -no-hpet -global kvm-pit.lost_tick_policy=discard -kernel /home/rjones/d/libguestfs/tmp/.guestfs-1000/appliance.d/kernel -initrd /home/rjones/d/libguestfs/tmp/.guestfs-1000/appliance.d/initrd -device virtio-scsi-pci,id=scsi -drive file=/home/rjones/d/libguestfs/test1.img,cache=writeback,discard=unmap,format=raw,id=hd0,if=none -device scsi-hd,drive=hd0 -drive file=/home/rjones/d/libguestfs/tmp/.guestfs-1000/appliance.d/root,snapshot=on,id=appliance,cache=unsafe,if=none -device scsi-hd,drive=appliance -device virtio-serial-pci -serial stdio -device sga -chardev socket,path=/home/rjones/d/libguestfs/tmp/libguestfsSwrSCg/guestfsd.sock,id=channel0 -device virtserialport,chardev=channel0,name=org.libguestfs.channel.0 -append "panic=1 console=ttyS0 udevtimeout=600 no_timer_check acpi=off printk.time=1 cgroup_disable=memory root=/dev/sdb selinux=0 TERM=xterm-256color"

Host:
- host kernel: 3.12.5-302.fc20.x86_64
- ext4 host filesystem
- backed by m-SATA SDD

Methodology:

(0) Observe the file size on host:

$ ls -ls test1.img 
0 -rw-rw-r--. 1 rjones rjones 1073741824 Mar 10 16:08 test1.img
^
+---- sparse file, 0 blocks used

(1) Create an ext4 filesystem on /dev/sda in the guest.

(2) Create a big data file of random data (10 MB) & sync.

(3) Observe the file size on the host:

$ ls -ls test1.img 
59804 -rw-rw-r--. 1 rjones rjones 1073741824 Mar 10 16:09 test1.img

(4) rm /data in guest

(5) fstrim / in guest [actually, I do this several times]

(6) sync

(6) Observe the file size on host:

$ ls -ls test1.img 
59804 -rw-rw-r--. 1 rjones rjones 1073741824 Mar 10 16:10 test1.img

Unchanged! I'm expecting the first number (59804 blocks) to get substantially smaller.

Rich.

Signature · 4 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
virt-df lists disk usage of guests without needing to install any
software inside the virtual machine.  Supports Linux and Windows.
http://people.redhat.com/~rjones/virt-df/
#1 lore raw
Quoted 6 lines · Richard W.M. Jones

Guest:
- guest kernel: 3.13.4-200.fc20.x86_64
- ext4 guest filesystem
- fstrim from util-linux 2.24.1
- cat /sys/block/sda/device/scsi_disk/*/provisioning_mode unmap

I noticed that I wasn't mounting the guest filesystem with '-o discard'. However it still doesn't work even after adding this option.

Rich.

Signature · 4 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
virt-df lists disk usage of guests without needing to install any
software inside the virtual machine.  Supports Linux and Windows.
http://people.redhat.com/~rjones/virt-df/
#2 lore raw

Il 10/03/2014 17:14, Richard W.M. Jones ha scritto:

Quoted 10 lines · Richard W.M. Jones
Quoted 6 lines

Guest:
- guest kernel: 3.13.4-200.fc20.x86_64
- ext4 guest filesystem
- fstrim from util-linux 2.24.1
- cat /sys/block/sda/device/scsi_disk/*/provisioning_mode unmap

I noticed that I wasn't mounting the guest filesystem with '-o discard'. However it still doesn't work even after adding this option.

Works here:

$ qemu-system-x86_64 -drive discard=unmap,file=$HOME/jeos-19-64.qcow2,id=ff,if=none -device virtio-scsi-pci -device scsi-disk,drive=ff -vnc :0 --enable-kvm

Before test:

$ du -h jeos-19-64.qcow2
1,6G	jeos-19-64.qcow2

After "dd if=/dev/zero of=foo bs=1M count=300":

$ du -h jeos-19-64.qcow2
1,8G	jeos-19-64.qcow2

After "rm foo; fstrim -v /":

$ du -h jeos-19-64.qcow2
1,4G	jeos-19-64.qcow2

I also set a breakpoint on fallocate64, and it was called.

Paolo

#3 lore raw
Quoted 9 lines · Paolo Bonzini

Il 10/03/2014 17:14, Richard W.M. Jones ha scritto:

Quoted 8 lines · Richard W.M. Jones
Quoted 6 lines

Guest:
- guest kernel: 3.13.4-200.fc20.x86_64
- ext4 guest filesystem
- fstrim from util-linux 2.24.1
- cat /sys/block/sda/device/scsi_disk/*/provisioning_mode unmap

Quoted 4 lines
Quoted 2 lines

I noticed that I wasn't mounting the guest filesystem with '-o discard'. However it still doesn't work even after adding this option.

Works here:

Indeed. And now it works here too. Very strange, and sorry for the noise.

Rich.

Signature · 4 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
virt-top is 'top' for virtual machines.  Tiny program with many
powerful monitoring features, net stats, disk stats, logging, etc.
http://people.redhat.com/~rjones/virt-top
#4 lore raw
In reply to Paolo Bonzini

Finally I tracked down the reason why my early test failed, but then it just started working "by magic".

The reason is this:

If you:

- create an ext4 filesystem
- mount it WITHOUT -o discard
- create and remove some big files
- unmount
- mount -o discard
- fstrim

then the fstrim has no effect.

If you:

- create an ext4 filesystem
- mount it WITH -o discard
- create and remove some big files
- unmount
- mount -o discard
- fstrim

then the fstrim works. (Actually it's not necessary, because the previous 'rm' already recovered the space.)

I have no idea why this is yet (still investigating). However the fstrim is suspiciously fast, so there's no way it can be scanning the whole disk for deleted files. I don't think fstrim does what it says in the man page.

Rich.

Signature · 4 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
virt-top is 'top' for virtual machines.  Tiny program with many
powerful monitoring features, net stats, disk stats, logging, etc.
http://people.redhat.com/~rjones/virt-top
#5 lore raw

Il 11/03/2014 21:00, Richard W.M. Jones ha scritto:

Quoted 4 lines

However the
fstrim is suspiciously fast, so there's no way it can be scanning the whole disk for deleted files. I don't think fstrim does what it says in the man page.

fstrim definitely has some cache inside. It may also have some bugs. But when it works, it's pretty good at making du-on-the-host almost the same as usage-in-the-guest.

Paolo