From nobody Sat Apr 11 18:38:41 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1775677275; cv=none; d=zohomail.com; s=zohoarc; b=MH9/1cIee5O2OGT2co5FGyhqN8vckRL/hzYN6Xph6EugHAWmIhtcHVYZoNhskA+gKr4wayUJEzn4avhzSLkgJxoRsjnLzIJoq3PRtPC0acm0AmY+0IuxzTMCS1Q1y46mCrxXpy9zQZuOn29DYRtMdPkqGORHw4tPDf5Q0HxfK0o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1775677275; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=lLHbJbpbVpCc7ZPDfJzkupfRH4+Vp1i+3Y5xDT7n2Qg=; b=Wtm7IQVf06nod2rcGhLD3IYD90STmWdl8BnoIA4avCLYpaqpIPYplqEuhHxPv7VAwVDvxgV+Gc4UvDSljAbaeTr3NImg3Fp2pO+IgMCpqynYv+E85a7ss9uwSBUXQPntryYw23nE9I7snuxSKjv9ZNj+PFiNuC3i7IS+sZ2F0nw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1775677275658329.0927678440428; Wed, 8 Apr 2026 12:41:15 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wAYc8-0004wh-G5; Wed, 08 Apr 2026 15:31:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wAYA1-0000gb-9u for qemu-devel@nongnu.org; Wed, 08 Apr 2026 15:01:57 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wAITH-0004jo-Kt for qemu-devel@nongnu.org; Tue, 07 Apr 2026 22:16:49 -0400 Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-336-jsNwypOKPeqbz4VomXJdWQ-1; Tue, 07 Apr 2026 22:16:43 -0400 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B694D180044D; Wed, 8 Apr 2026 02:16:41 +0000 (UTC) Received: from green.redhat.com (unknown [10.2.16.26]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id BAFFB1800673; Wed, 8 Apr 2026 02:16:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1775614606; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=lLHbJbpbVpCc7ZPDfJzkupfRH4+Vp1i+3Y5xDT7n2Qg=; b=XxwY1oYgffl3coeecc1tYrqiXmoGE7U2ZBgkNI479Hr22QrgkqJG+TbpogMfiQMSE0GFtg db45ZeYlVwZo3eGZDeT8wNtIctdR3RmRt+ovBtDZKeXiu9fQZ1bMEPKot5Lp4f3QMeZzrt 0xn3zIyVlYlfO5Ew1NfjPryojyad24o= X-MC-Unique: jsNwypOKPeqbz4VomXJdWQ-1 X-Mimecast-MFC-AGG-ID: jsNwypOKPeqbz4VomXJdWQ_1775614602 From: Eric Blake To: qemu-devel@nongnu.org Cc: Vladimir Sementsov-Ogievskiy , Pierrick Bouvier , Kevin Wolf , Hanna Reitz , qemu-block@nongnu.org (open list:Network Block Dev...) Subject: [PATCH] qemu-nbd: Support -s and -l at the same time Date: Tue, 7 Apr 2026 21:16:33 -0500 Message-ID: <20260408021636.794794-2-eblake@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.54, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1775677277881158500 Content-Type: text/plain; charset="utf-8" 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 Signed-off-by: Eric Blake Reviewed-by: Vladimir Sementsov-Ogievskiy --- 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 diff --git a/docs/tools/qemu-nbd.rst b/docs/tools/qemu-nbd.rst index f82ea5fd77b..336b4aa46e7 100644 --- a/docs/tools/qemu-nbd.rst +++ b/docs/tools/qemu-nbd.rst @@ -95,9 +95,10 @@ driver options if :option:`--image-opts` is specified. .. option:: -l, --load-snapshot=3DSNAPSHOT_PARAM - Load an internal snapshot inside *filename* and export it - as an read-only device, SNAPSHOT_PARAM format is - ``snapshot.id=3D[ID],snapshot.name=3D[NAME]`` or ``[ID_OR_NAME]`` + Load an internal snapshot inside *filename* and export it. + SNAPSHOT_PARAM format is ``snapshot.id=3D[ID],snapshot.name=3D[NAME]`` + or ``[ID_OR_NAME]``. The export is read-only unless ``-s`` is also + in use. .. option:: --cache=3DCACHE diff --git a/qemu-nbd.c b/qemu-nbd.c index ed5895861bb..128486a0ec2 100644 --- a/qemu-nbd.c +++ b/qemu-nbd.c @@ -534,6 +534,7 @@ int main(int argc, char **argv) { BlockBackend *blk; BlockDriverState *bs; + BlockDriverState *orig_bs; uint64_t dev_offset =3D 0; bool readonly =3D false; bool disconnect =3D false; @@ -716,7 +717,7 @@ int main(int argc, char **argv) } else { sn_id_or_name =3D optarg; } - /* fall through */ + break; case 'r': readonly =3D true; flags &=3D ~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) =3D=3D 0) { + readonly =3D true; + flags &=3D ~BDRV_O_RDWR; + } opts.srcpath =3D 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 =3D bs; + if (flags & BDRV_O_SNAPSHOT) { + orig_bs =3D bs->backing->bs; + } if (sn_opts) { - ret =3D bdrv_snapshot_load_tmp(bs, + ret =3D bdrv_snapshot_load_tmp(orig_bs, qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID= ), qemu_opt_get(sn_opts, SNAPSHOT_OPT_NA= ME), &local_err); } else if (sn_id_or_name) { - ret =3D bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name, + ret =3D bdrv_snapshot_load_tmp_by_id_or_name(orig_bs, sn_id_or_nam= e, &local_err); } if (ret < 0) { diff --git a/tests/qemu-iotests/tests/qemu-nbd-snapshots b/tests/qemu-iotes= ts/tests/qemu-nbd-snapshots new file mode 100755 index 00000000000..de405e43720 --- /dev/null +++ b/tests/qemu-iotests/tests/qemu-nbd-snapshots @@ -0,0 +1,79 @@ +#!/usr/bin/env bash +# group: rw quick +# +# Test qemu-nbd snapshot handling +# +# Copyright (C) Red Hat, Inc. +# +# SPDX-License-Identifier: GPL-2.0-or-later + +seq=3D"$(basename $0)" +echo "QA output created by $seq" + +status=3D1 # failure is the default! + +_cleanup() +{ + _cleanup_test_img + nbd_server_stop +} +trap "_cleanup; exit \$status" 0 1 2 3 15 + +# get standard environment, filters and checks +cd .. +. ./common.rc +. ./common.filter +. ./common.nbd + +_supported_fmt qcow2 +_supported_proto file +_supported_os Linux +_require_command QEMU_NBD + +echo +echo "=3D=3D=3D Initial image setup =3D=3D=3D" +echo + +# Create qcow2 image with one internal snapshot +_make_test_img 10M +$QEMU_IO -c 'w -P 1 3M 1M' -f $IMGFMT "$TEST_IMG" | _filter_qemu_io +$QEMU_IMG snapshot -c snap -f $IMGFMT "$TEST_IMG" | _filter_qemu_img +$QEMU_IO -c 'w -P 2 3M 1M' -f $IMGFMT "$TEST_IMG" | _filter_qemu_io + +echo +echo "=3D=3D=3D Main image in snapshot mode =3D=3D=3D" +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 \ + nbd:unix:$nbd_unix_socket | _filter_qemu_io +nbd_server_stop + +echo +echo "=3D=3D=3D Read-only snapshot access =3D=3D=3D" +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 +nbd_server_stop + +echo +echo "=3D=3D=3D Combine -s and -l for read-write internal snapshot access = =3D=3D=3D" +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 \ + nbd:unix:$nbd_unix_socket | _filter_qemu_io +nbd_server_stop + +echo +echo "=3D=3D=3D Check that original image is unchanged =3D=3D=3D" +echo + +$QEMU_IO -c 'r -P 2 3M 1M' -f $IMGFMT "$TEST_IMG" | _filter_qemu_io + +# success, all done +echo '*** done' +rm -f $seq.full +status=3D0 diff --git a/tests/qemu-iotests/tests/qemu-nbd-snapshots.out b/tests/qemu-i= otests/tests/qemu-nbd-snapshots.out new file mode 100644 index 00000000000..6bce9d90253 --- /dev/null +++ b/tests/qemu-iotests/tests/qemu-nbd-snapshots.out @@ -0,0 +1,34 @@ +QA output created by qemu-nbd-snapshots + +=3D=3D=3D Initial image setup =3D=3D=3D + +Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D10485760 +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) + +=3D=3D=3D Main image in snapshot mode =3D=3D=3D + +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) + +=3D=3D=3D Read-only snapshot access =3D=3D=3D + +read 1048576/1048576 bytes at offset 3145728 +1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) + +=3D=3D=3D Combine -s and -l for read-write internal snapshot access =3D=3D= =3D + +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) + +=3D=3D=3D Check that original image is unchanged =3D=3D=3D + +read 1048576/1048576 bytes at offset 3145728 +1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +*** done --=20 2.53.0