qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] Trying to get fstrim / discard=unmap to work
@ 2014-03-10 16:11 Richard W.M. Jones
  2014-03-10 16:14 ` Richard W.M. Jones
  0 siblings, 1 reply; 6+ messages in thread
From: Richard W.M. Jones @ 2014-03-10 16:11 UTC (permalink / raw)
  To: qemu-devel; +Cc: pbonzini


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.

-- 
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/

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Qemu-devel] Trying to get fstrim / discard=unmap to work
  2014-03-10 16:11 [Qemu-devel] Trying to get fstrim / discard=unmap to work Richard W.M. Jones
@ 2014-03-10 16:14 ` Richard W.M. Jones
  2014-03-10 16:57   ` Paolo Bonzini
  0 siblings, 1 reply; 6+ messages in thread
From: Richard W.M. Jones @ 2014-03-10 16:14 UTC (permalink / raw)
  To: qemu-devel; +Cc: pbonzini

On Mon, Mar 10, 2014 at 04:11:20PM +0000, Richard W.M. Jones wrote:
> 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.

-- 
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/

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Qemu-devel] Trying to get fstrim / discard=unmap to work
  2014-03-10 16:14 ` Richard W.M. Jones
@ 2014-03-10 16:57   ` Paolo Bonzini
  2014-03-10 17:15     ` Richard W.M. Jones
  2014-03-11 20:00     ` Richard W.M. Jones
  0 siblings, 2 replies; 6+ messages in thread
From: Paolo Bonzini @ 2014-03-10 16:57 UTC (permalink / raw)
  To: Richard W.M. Jones, qemu-devel

Il 10/03/2014 17:14, Richard W.M. Jones ha scritto:
> On Mon, Mar 10, 2014 at 04:11:20PM +0000, Richard W.M. Jones wrote:
>> 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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Qemu-devel] Trying to get fstrim / discard=unmap to work
  2014-03-10 16:57   ` Paolo Bonzini
@ 2014-03-10 17:15     ` Richard W.M. Jones
  2014-03-11 20:00     ` Richard W.M. Jones
  1 sibling, 0 replies; 6+ messages in thread
From: Richard W.M. Jones @ 2014-03-10 17:15 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: qemu-devel

On Mon, Mar 10, 2014 at 05:57:01PM +0100, Paolo Bonzini wrote:
> Il 10/03/2014 17:14, Richard W.M. Jones ha scritto:
> >On Mon, Mar 10, 2014 at 04:11:20PM +0000, Richard W.M. Jones wrote:
> >>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:

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

Rich.

-- 
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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Qemu-devel] Trying to get fstrim / discard=unmap to work
  2014-03-10 16:57   ` Paolo Bonzini
  2014-03-10 17:15     ` Richard W.M. Jones
@ 2014-03-11 20:00     ` Richard W.M. Jones
  2014-03-12 17:55       ` Paolo Bonzini
  1 sibling, 1 reply; 6+ messages in thread
From: Richard W.M. Jones @ 2014-03-11 20:00 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: qemu-devel

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.

-- 
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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Qemu-devel] Trying to get fstrim / discard=unmap to work
  2014-03-11 20:00     ` Richard W.M. Jones
@ 2014-03-12 17:55       ` Paolo Bonzini
  0 siblings, 0 replies; 6+ messages in thread
From: Paolo Bonzini @ 2014-03-12 17:55 UTC (permalink / raw)
  To: Richard W.M. Jones; +Cc: qemu-devel

Il 11/03/2014 21:00, Richard W.M. Jones ha scritto:
> 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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2014-03-12 17:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-03-10 16:11 [Qemu-devel] Trying to get fstrim / discard=unmap to work Richard W.M. Jones
2014-03-10 16:14 ` Richard W.M. Jones
2014-03-10 16:57   ` Paolo Bonzini
2014-03-10 17:15     ` Richard W.M. Jones
2014-03-11 20:00     ` Richard W.M. Jones
2014-03-12 17:55       ` Paolo Bonzini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).