[PATCH qemu-server 1/2] blockdev: add set write threshold

Tiago Sousa joao.sousa at eurotux.com
Sat Aug 2 18:42:39 CEST 2025


Signed-off-by: Tiago Sousa <joao.sousa at eurotux.com>
---
 src/PVE/QemuServer/Blockdev.pm | 52 ++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)

diff --git a/src/PVE/QemuServer/Blockdev.pm b/src/PVE/QemuServer/Blockdev.pm
index 0cc4a0f6..a5d7a3b9 100644
--- a/src/PVE/QemuServer/Blockdev.pm
+++ b/src/PVE/QemuServer/Blockdev.pm
@@ -567,9 +567,61 @@ sub attach {
         die $err;
     }
 
+    set_write_threshold($storecfg, $vmid, $drive, $options);
+
     return $blockdev->{'node-name'};
 }
 
+sub block_set_write_threshold {
+    my ($vmid, $nodename, $threshold) = @_;
+
+    print "set threshold $nodename $threshold\n";
+
+    PVE::QemuServer::mon_cmd(
+        $vmid,
+        "block-set-write-threshold",
+        'node-name' => $nodename,
+        'write-threshold' => int($threshold),
+    );
+}
+
+sub compute_write_threshold {
+    my ($scfg, $volid) = @_;
+    my $lv_size = PVE::Storage::volume_size_info($scfg, $volid, 5);
+
+    # FIX: change these vars to config inputs
+    my $chunksize = 1024 * 1024 * 1024; # 1 GB
+    my $alert_chunk_percentage = 0.5; # alert when percetage of chunk used
+
+    my $write_threshold = $lv_size - $chunksize * (1 - $alert_chunk_percentage);
+
+    return $write_threshold;
+}
+
+sub set_write_threshold {
+    my ($storecfg, $vmid, $drive, $options) = @_;
+
+    my $volid = $drive->{'file'};
+    my ($storeid) = PVE::Storage::parse_volume_id($volid);
+    my $support_qemu_snapshots = PVE::Storage::volume_qemu_snapshot_method($storecfg, $volid);
+    my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
+
+    # set write threshold is only supported for lvm storage using
+    # qcow2+external snapshots
+    return if $scfg->{type} ne 'lvm' || $support_qemu_snapshots ne 'mixed';
+
+    my $snapshots = PVE::Storage::volume_snapshot_info($storecfg, $volid);
+    my $parentid = $snapshots->{'current'}->{parent};
+    # for now only set write_threshold for volumes that have snapshots
+    if ($parentid) {
+        my $drive_id = PVE::QemuServer::Drive::get_drive_id($drive);
+        my $nodename = get_node_name('file', $drive_id, $drive->{file}, $options);
+        my $write_threshold = compute_write_threshold($scfg, $volid);
+
+        block_set_write_threshold($vmid, $nodename, $write_threshold);
+    }
+}
+
 =pod
 
 =head3 detach
-- 
2.39.5




More information about the pve-devel mailing list