On Mon, Dec 30, 2013 at 07:58:29PM +0800, Teng-Feng Yang wrote: > I have been studying QCOW2 file format for a couple of days, and I am > a little bit confused about whether QCOW2 supports UNMAP or not.
Discard is an area that has seen a lot of development activity over the past year or two. That means it's still relatively new, you may find outdated information online, etc. > As I surf through internet, some mailing list discussion had mentioned > that qemu-nbd and nbd module both support UNMAP command. Yes: * qemu-nbd since QEMU 1.1 supports TRIM * nbd.ko since Linux 3.7 supports discard > So I follow the steps below on my machine (Ubuntu 13.10 with linux > kernel 3.12) to test if qemu-nbd and QCOW2 do support UNMAP. > > 1. Create a qcow2 file via qemu-img > > sudo qemu-img create -f qcow2 -o cluster_size=524288 base.qcow2 1G > > 2. Connect this qcow2 file with qemu-nbd > > sudo qemu-nbd -c /dev/nbd0 base.qcow2 --discard=unmap > > 3. Use sg_unmap command to issue UNMAP command to this NBD > > sudo sg_unmap --lba=0 --num=1 /dev/nbd0 > > Everytime I get the following error message: > > unmap cdb: 42 00 00 00 00 00 00 00 18 00 > unmap: pass through os error: Inappropriate ioctl for device > UNMAP failed (use '-v' to get more information) > > I also try to format this nbd device with EXT4 and mount it, but still > cannot perform fstrim on the mount point. NBD isn't a SCSI device so sending UNMAP commands doesn't work. I think you need to issue ioctl(BLKDISCARD) instead. See blkdiscard(8). Also, make sure to use qemu.git/master if you want qcow2 discard support. I didn't check the details but the unmap implementation for qcow2 has recently been added/modified. Stefan