Skip to contentSkip to conversation

qemu-nbd: Support -s and -l at the same time

PATCH In review Eric Blake 2 participants 3 messages Apr 8 – Oct 7

lore
Share & tools
lore

Eric Blake

author
#0 lore raw

PATCH 1/1 qemu-nbd: Support -s and -l at the same time

qemu-nbd already has the ability to wrap a read-write layer on top of the export of a read-only image (-s) and the ability to export an internal snapshot rather than the main image in a qcow2 file (-l SNAPID). But it gave a confusing error message when trying to mix the two, because the snapshot overlay created first no longer has access to the internal snapshots of the image being wrapped.

WIth a bit of finesse, this useful scenario is now supported.

https://gitlab.com/qemu-project/qemu/-/work_items/159

---
 docs/tools/qemu-nbd.rst                       |  7 +-
 qemu-nbd.c                                    | 20 ++++-
 tests/qemu-iotests/tests/qemu-nbd-snapshots   | 79 +++++++++++++++++++
 .../qemu-iotests/tests/qemu-nbd-snapshots.out | 34 ++++++++
 4 files changed, 134 insertions(+), 6 deletions(-)
 create mode 100755 tests/qemu-iotests/tests/qemu-nbd-snapshots
 create mode 100644 tests/qemu-iotests/tests/qemu-nbd-snapshots.out
docs/tools/qemu-nbd.rst +4 -3
index f82ea5fd77b..336b4aa46e7 100644
@@ -95,9 +95,10 @@ driver options if :option:`--image-opts` is specified.
 
 .. option:: -l, --load-snapshot=SNAPSHOT_PARAM
 
-  Load an internal snapshot inside *filename* and export it
-  as an read-only device, SNAPSHOT_PARAM format is
-  ``snapshot.id=[ID],snapshot.name=[NAME]`` or ``[ID_OR_NAME]``
+  Load an internal snapshot inside *filename* and export it.
+  SNAPSHOT_PARAM format is ``snapshot.id=[ID],snapshot.name=[NAME]``
+  or ``[ID_OR_NAME]``. The export is read-only unless ``-s`` is also
+  in use.
 
 .. option:: --cache=CACHE
 
qemu-nbd.c +17 -3
index ed5895861bb..128486a0ec2 100644
@@ -534,6 +534,7 @@ int main(int argc, char **argv)
 {
     BlockBackend *blk;
     BlockDriverState *bs;
+    BlockDriverState *orig_bs;
     uint64_t dev_offset = 0;
     bool readonly = false;
     bool disconnect = false;
@@ -716,7 +717,7 @@ int main(int argc, char **argv)
             } else {
                 sn_id_or_name = optarg;
             }
-            /* fall through */
+            break;
         case 'r':
             readonly = true;
             flags &= ~BDRV_O_RDWR;
@@ -1110,6 +1111,15 @@ int main(int argc, char **argv)
     bdrv_init();
     atexit(qemu_nbd_shutdown);
 
+    /*
+     * -s and -l can be used together to create read-write wrapper
+     * around an internal snapshot. Otherwise, -l in isolation implies
+     * read-only.
+     */
+    if ((sn_opts || sn_id_or_name) && (flags & BDRV_O_SNAPSHOT) == 0) {
+        readonly = true;
+        flags &= ~BDRV_O_RDWR;
+    }
     opts.srcpath = argv[optind];
     if (imageOpts) {
         QemuOpts *o;
@@ -1155,13 +1165,17 @@ int main(int argc, char **argv)
 
     blk_set_enable_write_cache(blk, !writethrough);
 
+    orig_bs = bs;
+    if (flags & BDRV_O_SNAPSHOT) {
+        orig_bs = bs->backing->bs;
+    }
     if (sn_opts) {
-        ret = bdrv_snapshot_load_tmp(bs,
+        ret = bdrv_snapshot_load_tmp(orig_bs,
                                      qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
                                      qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
                                      &local_err);
     } else if (sn_id_or_name) {
-        ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
+        ret = bdrv_snapshot_load_tmp_by_id_or_name(orig_bs, sn_id_or_name,
                                                    &local_err);
     }
     if (ret < 0) {
tests/qemu-iotests/tests/qemu-nbd-snapshots new file +79 -0

79 lines not on this page · raw message

tests/qemu-iotests/tests/qemu-nbd-snapshots.out new file +34 -0
new file mode 100644
index 00000000000..6bce9d90253
@@ -0,0 +1,34 @@
+QA output created by qemu-nbd-snapshots
+
+=== Initial image setup ===
+
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=10485760
+wrote 1048576/1048576 bytes at offset 3145728
+1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+wrote 1048576/1048576 bytes at offset 3145728
+1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+
+=== Main image in snapshot mode ===
+
+read 1048576/1048576 bytes at offset 3145728
+1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+wrote 1048576/1048576 bytes at offset 3145728
+1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+
+=== Read-only snapshot access ===
+
+read 1048576/1048576 bytes at offset 3145728
+1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+
+=== Combine -s and -l for read-write internal snapshot access ===
+
+read 1048576/1048576 bytes at offset 3145728
+1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+wrote 1048576/1048576 bytes at offset 3145728
+1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+
+=== Check that original image is unchanged ===
+
+read 1048576/1048576 bytes at offset 3145728
+1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+*** done
Signature · 1 line
2.53.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Eric Blake

qemu-nbd already has the ability to wrap a read-write layer on top of the export of a read-only image (-s) and the ability to export an internal snapshot rather than the main image in a qcow2 file (-l SNAPID). But it gave a confusing error message when trying to mix the two, because the snapshot overlay created first no longer has access to the internal snapshots of the image being wrapped.

Quoted 5 lines

WIth a bit of finesse, this useful scenario is now supported.

https://gitlab.com/qemu-project/qemu/-/work_items/159

Some optional suggestions below:

tests/qemu-iotests/tests/qemu-nbd-snapshots line 48

Quoted 7 lines
tests/qemu-iotests/tests/qemu-nbd-snapshots new file +7 -0
+
+echo
+echo "=== Main image in snapshot mode ==="
+echo
+
+nbd_server_start_unix_socket -s -f qcow2 "$TEST_IMG"
+$QEMU_IO -c 'r -P 2 3M 1M' -c 'w -P 3 3M 1M' -f raw \

Maybe, add read command after write, to check write correctness. [*]

tests/qemu-iotests/tests/qemu-nbd-snapshots line 57

Quoted 9 lines
tests/qemu-iotests/tests/qemu-nbd-snapshots new file +9 -0
+  nbd:unix:$nbd_unix_socket | _filter_qemu_io
+nbd_server_stop
+
+echo
+echo "=== Read-only snapshot access ==="
+echo
+
+nbd_server_start_unix_socket -l snap -f qcow2 "$TEST_IMG"
+$QEMU_IO -r -c 'r -P 1 3M 1M' -f raw \

Suggest, add write command, to check that it fails.

tests/qemu-iotests/tests/qemu-nbd-snapshots line 66

Quoted 9 lines
tests/qemu-iotests/tests/qemu-nbd-snapshots new file +9 -0
+  nbd:unix:$nbd_unix_socket | _filter_qemu_io
+nbd_server_stop
+
+echo
+echo "=== Combine -s and -l for read-write internal snapshot access ==="
+echo
+
+nbd_server_start_unix_socket -s -l snap -f qcow2 "$TEST_IMG"
+$QEMU_IO -c 'r -P 1 3M 1M' -c 'w -P 3 3M 1M' -f raw \

[*] and here

Quoted 7 lines
tests/qemu-iotests/tests/qemu-nbd-snapshots new file +7 -0
+  nbd:unix:$nbd_unix_socket | _filter_qemu_io
+nbd_server_stop
+
+echo
+echo "=== Check that original image is unchanged ==="
+echo
+
Signature · 2 lines
Best regards,
Vladimir
author
#2 lore raw

[checking through my old branches]

Quoted 18 lines · Vladimir Sementsov-Ogievskiy
Quoted 7 lines · Eric Blake

qemu-nbd already has the ability to wrap a read-write layer on top of the export of a read-only image (-s) and the ability to export an internal snapshot rather than the main image in a qcow2 file (-l SNAPID). But it gave a confusing error message when trying to mix the two, because the snapshot overlay created first no longer has access to the internal snapshots of the image being wrapped.

Quoted 5 lines

WIth a bit of finesse, this useful scenario is now supported.

https://gitlab.com/qemu-project/qemu/-/work_items/159

Some optional suggestions below:

Quoted 9 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+
+echo
+echo "=== Main image in snapshot mode ==="
+echo
+
+nbd_server_start_unix_socket -s -f qcow2 "$TEST_IMG"
+$QEMU_IO -c 'r -P 2 3M 1M' -c 'w -P 3 3M 1M' -f raw \

Maybe, add read command after write, to check write correctness. [*]

Good idea.

Quoted 12 lines

Review

Quoted 10 lines
(quoted diff) +9 -0
+  nbd:unix:$nbd_unix_socket | _filter_qemu_io
+nbd_server_stop
+
+echo
+echo "=== Read-only snapshot access ==="
+echo
+
+nbd_server_start_unix_socket -l snap -f qcow2 "$TEST_IMG"
+$QEMU_IO -r -c 'r -P 1 3M 1M' -f raw \

Suggest, add write command, to check that it fails.

qemu-io rejects attempts to do -c 'w ...' on a read-only connection, but it is a good idea to prove that qemu-io won't connect read-write to a read-only server.

I'm queueing this patch with the following squashed in.

tests/qemu-iotests/tests/qemu-nbd-snapshots +5 -2
index de405e43720..5bc7a1a5d23 100755
@@ -45,7 +45,7 @@ echo "=== Main image in snapshot mode ==="
 echo
 
 nbd_server_start_unix_socket -s -f qcow2 "$TEST_IMG"
-$QEMU_IO -c 'r -P 2 3M 1M' -c 'w -P 3 3M 1M' -f raw \
+$QEMU_IO -c 'r -P 2 3M 1M' -c 'w -P 3 3M 1M' -c 'r -P 3 3M 1M' -f raw \
   nbd:unix:$nbd_unix_socket | _filter_qemu_io
 nbd_server_stop
 
@@ -56,6 +56,9 @@ echo
 nbd_server_start_unix_socket -l snap -f qcow2 "$TEST_IMG"
 $QEMU_IO -r -c 'r -P 1 3M 1M' -f raw \
   nbd:unix:$nbd_unix_socket | _filter_qemu_io
+$QEMU_IO -c 'r -P 1 3M 1M' -f raw \
+  nbd:unix:$nbd_unix_socket 2>&1 | _filter_qemu_io | _filter_testdir
+
 nbd_server_stop
 
 echo
@@ -63,7 +66,7 @@ echo "=== Combine -s and -l for read-write internal snapshot access ==="
 echo
 
 nbd_server_start_unix_socket -s -l snap -f qcow2 "$TEST_IMG"
-$QEMU_IO -c 'r -P 1 3M 1M' -c 'w -P 3 3M 1M' -f raw \
+$QEMU_IO -c 'r -P 1 3M 1M' -c 'w -P 3 3M 1M' -c 'r -P 3 3M 1M' -f raw \
   nbd:unix:$nbd_unix_socket | _filter_qemu_io
 nbd_server_stop
 
tests/qemu-iotests/tests/qemu-nbd-snapshots.out +5 -0
index 6bce9d90253..47289c30f1d 100644
@@ -14,11 +14,14 @@ read 1048576/1048576 bytes at offset 3145728
 1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
 wrote 1048576/1048576 bytes at offset 3145728
 1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+read 1048576/1048576 bytes at offset 3145728
+1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
 
 === Read-only snapshot access ===
 
 read 1048576/1048576 bytes at offset 3145728
 1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+qemu-io: can't open device nbd:unix:SOCK_DIR/qemu-nbd.sock: Could not open image: Permission denied
 
 === Combine -s and -l for read-write internal snapshot access ===
 
@@ -26,6 +29,8 @@ read 1048576/1048576 bytes at offset 3145728
 1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
 wrote 1048576/1048576 bytes at offset 3145728
 1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+read 1048576/1048576 bytes at offset 3145728
+1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
 
 === Check that original image is unchanged ===
 
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org