From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57830) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VykvU-0004NZ-7m for qemu-devel@nongnu.org; Thu, 02 Jan 2014 11:15:21 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VykvP-0002tT-1z for qemu-devel@nongnu.org; Thu, 02 Jan 2014 11:15:16 -0500 Received: from mx1.redhat.com ([209.132.183.28]:12816) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VykvO-0002tM-PZ for qemu-devel@nongnu.org; Thu, 02 Jan 2014 11:15:10 -0500 Date: Thu, 2 Jan 2014 16:15:03 +0000 From: "Richard W.M. Jones" Message-ID: <20140102161503.GA14565@redhat.com> References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="cWoXeonUoKmBZSoM" Content-Disposition: inline In-Reply-To: Subject: Re: [Qemu-devel] TRIM/DISCARD/UNMAP support on qemu-nbd List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Teng-Feng Yang Cc: pbonzini@redhat.com, qemu-devel@nongnu.org --cWoXeonUoKmBZSoM Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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. > As I surf through internet, some mailing list discussion had mentioned > that qemu-nbd and nbd module both support UNMAP command. > 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. > > Have I done anything wrong? There are a lot of factors for getting unmap/discard/trim to work, including: - guest tools (sg_unmap) or guest filesystem must support it - guest kernel must support it - host qemu must support it - host filesystem/etc must support it My (possibly weak) understanding of the upstream qemu code is that unmap/discard/trim is not supported in qcow2. It is only supported in raw files when using a POSIX-like host OS which has either of: - block devices supporting BLKDISCARDZEROES - files on XFS - files on other filesystems that support FALLOC_FL_PUNCH_HOLE (eg ext4) Having said that, I did some tests using libguestfs and I could not show that unmap was working, either using raw or qcow2 (both on ext4), with virtio-scsi, and recent kernel & qemu. I did not see any errors, but also I don't see what I'm doing wrong. Attached is my test script. You will need to compile libguestfs with: ./configure --with-extra-packages="sg3_utils" The results on my machine are: $ /tmp/sparsetest.sh 0 /tmp/test1 0 /tmp/test2 Read Capacity results: Protection: prot_en=0, p_type=0, p_i_exponent=0 Logical block provisioning: lbpme=1, lbprz=0 Last logical block address=204799 (0x31fff), Number of logical blocks=204800 Logical block length=512 bytes Logical blocks per physical block exponent=0 Lowest aligned logical block address=0 Hence: Device size: 104857600 bytes, 100.0 MiB, 0.10 GB Block limits VPD page (SBC): Write same no zero (WSNZ): 1 Maximum compare and write length: 0 blocks Optimal transfer length granularity: 0 blocks Maximum transfer length: 0 blocks Optimal transfer length: 0 blocks Maximum prefetch length: 0 blocks Maximum unmap LBA count: 0 Maximum unmap block descriptor count: 0 Optimal unmap granularity: 8 Unmap granularity alignment valid: 0 Unmap granularity alignment: 0 Maximum write same length: 0x0 blocks 16M /tmp/test1 <--- note no sparseness is created 16M /tmp/test2 Please let us know if you get this working, because I'd really like to fix virt-sparsify so it can work in-place! Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into KVM guests. http://libguestfs.org/virt-v2v --cWoXeonUoKmBZSoM Content-Type: application/x-sh Content-Disposition: attachment; filename="sparsetest.sh" Content-Transfer-Encoding: quoted-printable #!/bin/bash -=0A=0Aset -e=0A=0A# You'll need a modified libguestfs configur= ed with:=0A# ./configure --with-extra-packages=3D"sg3_utils"=0A=0A#export L= IBGUESTFS_DEBUG=3D1=0Aexport LIBGUESTFS_HV=3D/home/rjones/d/qemu/x86_64-sof= tmmu/qemu-system-x86_64=0A=0Arun=3D/home/rjones/d/libguestfs/run=0Atest1=3D= /tmp/test1=0Atest2=3D/tmp/test2=0A# bytes=0Asize=3D$((16*1024*1024))=0A# lo= gical blocks=0Aunmap_start=3D0=0Aunmap_size=3D8192=0A=0Arm -f $test1 $test2= =0A=0Atruncate -s 100M $test1=0Atruncate -s 100M $test2=0A=0Adu -sh $test1 = $test2=0A=0A$run guestfish -a $test1 -a $test2 <