Skip to contentSkip to conversation

block: fix BDRV_O_SNAPSHOT with protocols

PATCH Applied Stefan Hajnoczi 3 participants 6 messages Mar 18, 2013 – Mar 19, 2013

lore
Share & tools
lore
#0 lore raw

v2 cover block: fix BDRV_O_SNAPSHOT with protocols

Richard Jones <[email protected]> reported that BDRV_O_SNAPSHOT does not work with NBD:

  /usr/bin/qemu-system-x86_64 \
  [...]
      -device virtio-scsi-pci,id=scsi \
      -drive file=nbd:localhost:61930,snapshot=on,format=raw,id=hd0,if=none \
      -device scsi-hd,drive=hd0 \
  [...]

gives the error:

qemu-system-x86_64: -drive file=nbd:localhost:61930,snapshot=on,format=raw,id=hd0,if=none: could not open disk image nbd:localhost:61930: No such file or directory

This series fixes the issue and adds a regression test, qemu-iotests 052. Run the test like this:

  $ QEMU_PROG=... PATH=... ./check -nbd 052

If Patch 1 is not applied, the test case fails. Note that the test case is also useful with -raw or other formats.

v2:
* Rebased onto latest qemu.git/master

Stefan Hajnoczi (2):
block: fix BDRV_O_SNAPSHOT protocol detection
qemu-iotests: add 052 BDRV_O_SNAPSHOT test

 block.c                    |  6 +----
 tests/qemu-iotests/052     | 61 ++++++++++++++++++++++++++++++++++++++++++++++
 tests/qemu-iotests/052.out | 13 ++++++++++
 tests/qemu-iotests/group   |  1 +
 4 files changed, 76 insertions(+), 5 deletions(-)
 create mode 100755 tests/qemu-iotests/052
 create mode 100644 tests/qemu-iotests/052.out
Signature · 1 line
1.8.1.4
#1 lore raw

PATCH v2 1/2 block: fix BDRV_O_SNAPSHOT protocol detection

realpath(3) is used to get an absolute path to the image file when creating a -drive snapshot=on temporary qcow2. This does not work for protocols since their filenames ("proto:foo:...") do not correspond to file system paths.

Commit 7c96d46ec245d73fd76726588409f9abe4bd5dc1 ("Let snapshot work with protocols") skipped realpath(3) for protocols. Later on the "raw" format was introduced and broke the check.

Use path_has_protocol(filename) to decide if this image uses a protocol or a filename.

---
 block.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
block.c +1 -5
index 037e15e..0a062c9 100644
@@ -830,7 +830,6 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
     if (flags & BDRV_O_SNAPSHOT) {
         BlockDriverState *bs1;
         int64_t total_size;
-        int is_protocol = 0;
         BlockDriver *bdrv_qcow2;
         QEMUOptionParameter *options;
         char backing_filename[PATH_MAX];
@@ -847,9 +846,6 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
         }
         total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK;
 
-        if (bs1->drv && bs1->drv->protocol_name)
-            is_protocol = 1;
-
         bdrv_delete(bs1);
 
         ret = get_tmp_filename(tmp_filename, sizeof(tmp_filename));
@@ -858,7 +854,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
         }
 
         /* Real path is meaningless for protocols */
-        if (is_protocol) {
+        if (path_has_protocol(filename)) {
             snprintf(backing_filename, sizeof(backing_filename),
                      "%s", filename);
         } else if (!realpath(filename, backing_filename)) {
Signature · 1 line
1.8.1.4
#2 lore raw

PATCH v2 2/2 qemu-iotests: add 052 BDRV_O_SNAPSHOT test

In reply to Stefan Hajnoczi

Check that writes to an image opened with BDRV_O_SNAPSHOT do not modify the underlying image file.

---
 tests/qemu-iotests/052     | 61 ++++++++++++++++++++++++++++++++++++++++++++++
 tests/qemu-iotests/052.out | 13 ++++++++++
 tests/qemu-iotests/group   |  1 +
 3 files changed, 75 insertions(+)
 create mode 100755 tests/qemu-iotests/052
 create mode 100644 tests/qemu-iotests/052.out
tests/qemu-iotests/052 new file +61 -0
new file mode 100755
index 0000000..14a5126
@@ -0,0 +1,61 @@
+#!/bin/bash
+#
+# Test bdrv_read/bdrv_write using BDRV_O_SNAPSHOT
+#
+# Copyright (C) 2013 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+# creator
[email protected]
+
+seq=`basename $0`
+echo "QA output created by $seq"
+
+here=`pwd`
+tmp=/tmp/$$
+status=1	# failure is the default!
+
+_cleanup()
+{
+	_cleanup_test_img
+}
+trap "_cleanup; exit \$status" 0 1 2 3 15
+
+# get standard environment, filters and checks
+. ./common.rc
+. ./common.filter
+
+_supported_fmt generic
+_supported_proto generic
+_supported_os Linux
+
+
+size=128M
+_make_test_img $size
+
+echo
+echo "== reading whole image =="
+$QEMU_IO -s -c "read 0 $size" $TEST_IMG | _filter_qemu_io
+
+echo
+echo "== writing whole image does not modify image =="
+$QEMU_IO -s -c "write -P 0xa 0 $size" $TEST_IMG | _filter_qemu_io
+$QEMU_IO -c "read -P 0 0 $size" $TEST_IMG | _filter_qemu_io
+
+# success, all done
+echo "*** done"
+rm -f $seq.full
+status=0
tests/qemu-iotests/052.out new file +13 -0
new file mode 100644
index 0000000..8617aa2
@@ -0,0 +1,13 @@
+QA output created by 052
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 
+
+== reading whole image ==
+read 134217728/134217728 bytes at offset 0
+128 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+
+== writing whole image does not modify image ==
+wrote 134217728/134217728 bytes at offset 0
+128 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+read 134217728/134217728 bytes at offset 0
+128 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+*** done
tests/qemu-iotests/group +1 -0
index 1d7e4f3..73c5966 100644
@@ -57,3 +57,4 @@
 048 img auto quick
 049 rw auto
 050 rw auto backing quick
+052 rw auto backing
Signature · 1 line
1.8.1.4
Applied
#3 lore raw
In reply to Stefan Hajnoczi

Am 18.03.2013 um 17:58 hat Stefan Hajnoczi geschrieben:

Quoted 17 lines

Richard Jones <[email protected]> reported that BDRV_O_SNAPSHOT does not work with NBD:

  /usr/bin/qemu-system-x86_64 \
  [...]
      -device virtio-scsi-pci,id=scsi \
      -drive file=nbd:localhost:61930,snapshot=on,format=raw,id=hd0,if=none \
      -device scsi-hd,drive=hd0 \
  [...]

gives the error:

qemu-system-x86_64: -drive file=nbd:localhost:61930,snapshot=on,format=raw,id=hd0,if=none: could not open disk image nbd:localhost:61930: No such file or directory

This series fixes the issue and adds a regression test, qemu-iotests 052. Run the test like this:

Quoted 7 lines
  $ QEMU_PROG=... PATH=... ./check -nbd 052

If Patch 1 is not applied, the test case fails. Note that the test case is also useful with -raw or other formats.

v2:
* Rebased onto latest qemu.git/master

Thanks, applied all to the block branch.

Kevin