From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <f.ebner@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (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 lists.proxmox.com (Postfix) with ESMTPS id 1EA5791455
 for <pve-devel@lists.proxmox.com>; Tue, 27 Sep 2022 13:50:43 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 03B871E0EC
 for <pve-devel@lists.proxmox.com>; Tue, 27 Sep 2022 13:50:13 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (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 firstgate.proxmox.com (Proxmox) with ESMTPS
 for <pve-devel@lists.proxmox.com>; Tue, 27 Sep 2022 13:50:11 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id B6166445EE
 for <pve-devel@lists.proxmox.com>; Tue, 27 Sep 2022 13:50:11 +0200 (CEST)
From: Fiona Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Tue, 27 Sep 2022 13:50:04 +0200
Message-Id: <20220927115004.121044-1-f.ebner@proxmox.com>
X-Mailer: git-send-email 2.30.2
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.032 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [proxmox.com, qemuserver.pm, qemumigrate.pm]
Subject: [pve-devel] [PATCH qemu-server] migration: use copy-mode
 "write-blocking" for drive mirror
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Tue, 27 Sep 2022 11:50:43 -0000

Otherwise, there is nothing enforcing that the drive mirror is ready
when the migration inactivates the block devices, which can lead to a
failing assertion:
> ../block/io.c:2026: bdrv_co_write_req_prepare: Assertion
> `!(bs->open_flags & BDRV_O_INACTIVE)' failed.

QAPI documentation of 'write-blocking' (currently the only alternative
to the default 'background' mode):
> when data is written to the source, write it (synchronously) to the
> target as well. In addition, data is copied in background just like
> in background mode.

Reported in the community forum [0] (and likely [1]).

Reproduced consistently with a 1 core, 1 GiB RAM, 4 GiB disk Debian 11
VM. I added a sleep of 5 second before issuing the migrate QMP command
and executed the following in the VM after the drive-mirror became
first ready:
> fio --name=make-mirror-work --size=100M --direct=1 --rw=randwrite \
>     --bs=4k --ioengine=psync --numjobs=5 --runtime=60 --time_based
This ensures that there is a large number of dirty clusters and that
the mirror still has work to do when the block device is inactivated.

[0] https://forum.proxmox.com/threads/111831/
[1] https://forum.proxmox.com/threads/100020/

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---
 PVE/QemuMigrate.pm | 14 +++++++++++++-
 PVE/QemuServer.pm  |  6 +++++-
 2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/PVE/QemuMigrate.pm b/PVE/QemuMigrate.pm
index d52dc8db..dd6b073e 100644
--- a/PVE/QemuMigrate.pm
+++ b/PVE/QemuMigrate.pm
@@ -831,7 +831,19 @@ sub phase2 {
 	    my $bitmap = $target->{bitmap};
 
 	    $self->log('info', "$drive: start migration to $nbd_uri");
-	    PVE::QemuServer::qemu_drive_mirror($vmid, $drive, $nbd_uri, $vmid, undef, $self->{storage_migration_jobs}, 'skip', undef, $bwlimit, $bitmap);
+	    PVE::QemuServer::qemu_drive_mirror(
+		$vmid,
+		$drive,
+		$nbd_uri,
+		$vmid,
+		undef,
+		$self->{storage_migration_jobs},
+		'skip',
+		undef,
+		$bwlimit,
+		$bitmap,
+		1,
+	    );
 	}
     }
 
diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 4e85dd02..2901cd83 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -7450,7 +7450,7 @@ sub qemu_img_format {
 }
 
 sub qemu_drive_mirror {
-    my ($vmid, $drive, $dst_volid, $vmiddst, $is_zero_initialized, $jobs, $completion, $qga, $bwlimit, $src_bitmap) = @_;
+    my ($vmid, $drive, $dst_volid, $vmiddst, $is_zero_initialized, $jobs, $completion, $qga, $bwlimit, $src_bitmap, $write_blocking) = @_;
 
     $jobs = {} if !$jobs;
 
@@ -7477,6 +7477,10 @@ sub qemu_drive_mirror {
     my $opts = { timeout => 10, device => "drive-$drive", mode => "existing", sync => "full", target => $qemu_target };
     $opts->{format} = $format if $format;
 
+    # Relevant for migration, to ensure that the mirror will be ready (after being ready once) when
+    # the migration inactivates the block drives.
+    $opts->{'copy-mode'} = 'write-blocking' if $write_blocking;
+
     if (defined($src_bitmap)) {
 	$opts->{sync} = 'incremental';
 	$opts->{bitmap} = $src_bitmap;
-- 
2.30.2