From: Ciro Iriarte <cyruspy@gmail.com>
To: pve-devel@lists.proxmox.com
Subject: [RFC PATCH storage v2 1/6] storage: add asynchronous copy-offload hook for full copies
Date: Thu, 26 Nov 2026 13:56:57 -0300 [thread overview]
Message-ID: <20261126.1.copyoffload-v2@cyruspy.gmail.com> (raw)
In-Reply-To: <20261126.0.copyoffload-v2@cyruspy.gmail.com>
From: Ciro Iriarte <ciro.iriarte+software@gmail.com>
Let a plugin perform a full copy, for example a full clone, on the
storage backend instead of reading and writing the data through the host
with qemu-img. See bug #7780.
The hook has three phases:
copy_image_prepare() reserve the target under the storage lock
copy_image_start() capture the instant inside a guest freeze
copy_image_status() poll until independent outside both
It cannot be synchronous: for shared storage the core calls into the
plugin under a pmxcfs lock that kills the locked code after 60 seconds,
while a backend copy can run for hours. Allocation cannot happen inside
a guest freeze either, so only the start, a single cheap backend call,
runs while the guest is frozen.
'complete' means independent of the source, not merely readable. An RBD
clone is readable immediately but depends on its parent snapshot until
the flatten finishes; reporting completion early would let the caller
delete the source out from under it.
copy_image_check() reports whether the backend can copy a volume to a
given target storage, returning undef or a reason. The core calls it
before choosing offload, so a pair the backend cannot copy (another
pool, another filesystem, a format it cannot produce) takes the
host-side path instead of failing. Plugins repeat its probe-free checks
in prepare, which runs under the storage lock, so the two cannot
disagree. A check that dies is treated as a fault rather than a refusal:
the clone still falls back, but logs a warning, so the task shows it.
copy_image_start() receives the source snapshot, since it is the call
that captures the point in time. Because it runs while the guest is
frozen, the contract asks plugins to put a timeout on calls that can
block on a remote backend and to leave any cleanup that does not undo
the start to free_image().
copy_image_status() receives what the copy was made from, so a plugin
can clean up state it left on the source once the target is independent.
copy_images_start() starts a batch between one pair of storages. Its
default starts them one by one, so there is no capability to negotiate;
a backend that can capture several volumes at the same instant overrides
it. All starts of a batch happen inside one guest freeze.
The two capability classes, copy-offload-atomic and copy-offload-bulk,
are separate features rather than one feature with an argument, so
volume_has_feature() keeps its interface. Offload is enabled per storage
with 'copy-offload' and only between instances of the same plugin type,
as the operation is opaque to PVE. vdisk_copy_wait() gives up after
'copy-offload-timeout' seconds without progress, so a dead backend copy
fails instead of hanging the worker. It polls every second at first and
backs off to every ten seconds. It leaves a failed target to the caller,
which owns it from prepare onwards: freeing it in two places would leave
a window in which another allocation takes the name and the second free
removes that volume. vdisk_copy() frees the target itself, since its
caller only gets the volume on success.
APIVER 17 -> 18 and APIAGE 8 -> 9, keeping the oldest accepted plugin
API at 9: the new methods are optional. The base plugin declares the two
options, so under the v17 rules they become common properties; the
ApiChangeLog notes this and plugin_properties_test.pm lists them.
Signed-off-by: Ciro Iriarte <ciro.iriarte+software@gmail.com>
---
Changes in v2:
* rebased onto current master
* APIVER 17 -> 18, APIAGE 8 -> 9 (v1: 15 -> 16, 6 -> 7)
* new copy_image_check(): the core asks the plugin before choosing
offload, and a pair it refuses takes the host-side path. In v1,
prepare() died instead and the whole clone failed.
* copy_offload_class() takes the target format, for that check
* a failed copy's target is freed by one owner only: vdisk_copy_wait()
leaves it to the caller, which already holds it
* a check that dies falls back with log_warn() instead of silently,
so the task ends with a warning
* the start contract asks plugins to put a timeout on calls that can
block on a remote backend, as the start runs inside a guest freeze
* vdisk_copy_wait() backs off from polling every second to every ten
seconds
* the two options become common properties under the v17 rules; the
ApiChangeLog says so, plugin_properties_test.pm lists them, and they
have titles for the generated web UI dialog
ApiChangeLog | 38 ++++
src/PVE/Storage.pm | 268 ++++++++++++++++++++++++++++-
src/PVE/Storage/Plugin.pm | 180 +++++++++++++++++++
src/test/copy_offload_test.pm | 90 ++++++++++
src/test/plugin_properties_test.pm | 5 +-
src/test/run_plugin_tests.pl | 1 +
6 files changed, 579 insertions(+), 3 deletions(-)
create mode 100644 src/test/copy_offload_test.pm
diff --git a/ApiChangeLog b/ApiChangeLog
index b7e7442..e452fab 100644
--- a/ApiChangeLog
+++ b/ApiChangeLog
@@ -6,6 +6,44 @@ without breaking anything unaware of it.)
Future changes should be documented in here.
+## Version 18:
+
+* Add optional `copy_image_check()`, `copy_image_prepare()`, `copy_image_start()`,
+ `copy_image_status()` and `copy_images_start()` plugin methods, plus a `copy-offload` storage
+ option
+
+ These let a plugin perform a full copy (e.g. a full clone) on the storage backend instead of
+ reading and writing the data through the host. They are optional: the base prepare, start and
+ status implementations die, and are only reached if a plugin advertises one of the new
+ `copy-offload-atomic` or `copy-offload-bulk` features from `volume_has_feature()`, so existing
+ plugins are unaffected.
+
+ The copy is asynchronous. `copy_image_prepare()` allocates the target and returns its volname,
+ `copy_image_start()` captures the copy's point in time and begins it, and `copy_image_status()`
+ is polled until the target is independent of its source, not merely readable. The split matters
+ for callers that must capture several volumes at the same instant: only the start has to run
+ inside a guest freeze, and it must stay cheap enough for that.
+
+ `copy_images_start()` starts a batch between the same pair of storages. Its default
+ implementation starts them one by one, so it needs no capability negotiation; a backend
+ that can capture several volumes atomically overrides it.
+
+ `copy_image_start()` receives the source `$snapname`, as it is the call that captures the point
+ in time. A plugin that snapshots the current image instead would copy live data when a snapshot
+ was requested.
+
+ `copy_image_check()` reports whether the backend can copy a volume to a given target storage,
+ returning `undef` or the reason it cannot. The core asks before choosing offload, so a pair the
+ backend cannot copy, for example one in another pool, takes the host-side path instead of
+ failing. The base implementation accepts everything the feature check accepts.
+
+* Add the common properties `copy-offload` and `copy-offload-timeout`
+
+ The base plugin declares them, so they join the common properties an isolating plugin may use
+ and must not declare itself. A third-party plugin that declares a property of either name is
+ not loaded until it renames it. A plugin that also supports older hosts should only list them
+ in `options()` when `PVE::Storage::APIVER` is at least 18, as older hosts do not declare them.
+
## Version 17:
* Add the volume types `vm-vol`, `vm-state` and `ct-vol`
diff --git a/src/PVE/Storage.pm b/src/PVE/Storage.pm
index c77a675..81b38d2 100755
--- a/src/PVE/Storage.pm
+++ b/src/PVE/Storage.pm
@@ -44,11 +44,11 @@ use PVE::Storage::BTRFSPlugin;
use PVE::Storage::ESXiPlugin;
# Storage API version. Increment it on changes in storage API interface.
-use constant APIVER => 17;
+use constant APIVER => 18;
# Age is the number of versions we're backward compatible with.
# This is like having 'current=APIVER' and age='APIAGE' in libtool,
# see https://www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html
-use constant APIAGE => 8;
+use constant APIAGE => 9;
our $KNOWN_EXPORT_FORMATS = ['raw+size', 'tar+size', 'qcow2+size', 'vmdk+size', 'zfs', 'btrfs'];
@@ -1188,6 +1188,270 @@ sub vdisk_clone {
);
}
+# Returns the copy-offload class usable for $volid -> $target_storeid, or undef.
+#
+# Offload requires: the target storage to have copy-offload enabled, both storages to be
+# instances of the same plugin type, the plugin to advertise a class for this volume, and
+# its copy_image_check() to accept this source/target pair. $opts->{format} is the
+# target format. 'atomic' is preferred when a plugin advertises both, as it needs no
+# host-side mirror. Only the target's copy-offload option is consulted: it is the
+# storage the new volume lands on.
+sub copy_offload_class {
+ my ($cfg, $volid, $target_storeid, $snap, $running, $opts) = @_;
+
+ my ($storeid, $volname) = parse_volume_id($volid, 1);
+ return undef if !$storeid;
+
+ my $scfg = storage_config($cfg, $storeid);
+ my $target_scfg = storage_config($cfg, $target_storeid);
+
+ return undef if !$target_scfg->{'copy-offload'};
+ return undef if $scfg->{type} ne $target_scfg->{type};
+
+ for my $class (qw(atomic bulk)) {
+ next if !volume_has_feature($cfg, "copy-offload-$class", $volid, $snap, $running);
+
+ # The feature says what the source supports; whether the backend can copy it
+ # to this target is the plugin's call. A pair it cannot copy is not an error,
+ # it just takes the host-side path. A check that fails is a fault, not an
+ # answer: it falls back too, but as a warning, so the task shows it.
+ my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
+ my $reason = eval {
+ $plugin->copy_image_check(
+ $scfg, $storeid, $volname, $target_scfg, $target_storeid, $snap, $opts // {},
+ );
+ };
+ if (my $err = $@) {
+ log_warn("copy-offload check for '$volid' failed, copying through the host - $err");
+ return undef;
+ }
+ if (defined($reason)) {
+ chomp $reason;
+ print "not offloading copy of '$volid': $reason\n";
+ return undef;
+ }
+ return $class;
+ }
+
+ return undef;
+}
+
+# Allocate the target of an offloaded copy, returning the new volid. Its content is
+# undefined until vdisk_copy_start() has been called for it.
+#
+# Split out from the start so a caller cloning a running guest can prepare every disk's
+# target first and then start them all inside a single guest freeze; see vdisk_copy().
+#
+# Only this half runs under the storage lock: it is where the allocation happens, and
+# it is bounded. The copy itself must not run under the lock: for shared storage that is
+# a pmxcfs lock whose locked code is killed by an alarm(60) in PVE::Cluster, while a full
+# array copy runs for minutes to hours. The host-side path is arranged the same way,
+# allocating under the lock and running the long qemu-img convert outside it.
+sub vdisk_copy_prepare {
+ my ($cfg, $volid, $target_storeid, $target_vmid, $snap, $opts) = @_;
+
+ my ($storeid, $volname) = parse_volume_id($volid);
+
+ my $scfg = storage_config($cfg, $storeid);
+ my $target_scfg = storage_config($cfg, $target_storeid);
+
+ my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
+
+ activate_storage($cfg, $storeid);
+ activate_storage($cfg, $target_storeid) if $target_storeid ne $storeid;
+
+ my $new_volname = $plugin->cluster_lock_storage(
+ $target_storeid,
+ $target_scfg->{shared},
+ undef,
+ sub {
+ return $plugin->copy_image_prepare(
+ $scfg, $storeid, $volname,
+ $target_scfg, $target_storeid, $target_vmid, $snap, $opts,
+ );
+ },
+ );
+
+ return "$target_storeid:$new_volname";
+}
+
+# Pin the source's point-in-time and begin copying into a prepared target.
+#
+# Must stay cheap: a caller cloning a running guest holds a filesystem freeze across
+# this call for every disk of the VM. It takes no storage lock: the
+# allocation already happened in vdisk_copy_prepare(), and taking a cluster lock inside
+# a guest freeze would be a bad trade.
+sub vdisk_copy_start {
+ my ($cfg, $volid, $target_volid, $snap) = @_;
+
+ my ($storeid, $volname) = parse_volume_id($volid);
+ my ($target_storeid, $target_volname) = parse_volume_id($target_volid);
+
+ my $scfg = storage_config($cfg, $storeid);
+ my $target_scfg = storage_config($cfg, $target_storeid);
+
+ my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
+
+ return $plugin->copy_image_start(
+ $scfg, $storeid, $volname, $target_scfg, $target_storeid, $target_volname, $snap,
+ );
+}
+
+# Start several prepared copies, grouping them so a backend that can capture multiple
+# volumes at one instant does so. $copies is an arrayref of
+# { source => $volid, target => $target_volid }.
+#
+# Copies are grouped by their (source storage, target storage) pair, since that is the
+# granularity at which a backend can act atomically; each group is handed to the
+# plugin's copy_images_start(), whose default starts them one by one.
+#
+# Same constraints as vdisk_copy_start(): cheap, no locking; the caller may hold a
+# guest freeze across this.
+sub vdisk_copy_start_group {
+ my ($cfg, $copies) = @_;
+
+ return if !$copies || !scalar(@$copies);
+
+ my $groups = {};
+ for my $copy (@$copies) {
+ my ($storeid, $volname) = parse_volume_id($copy->{source});
+ my ($target_storeid, $target_volname) = parse_volume_id($copy->{target});
+
+ my $key = "$storeid/$target_storeid";
+ $groups->{$key} //= {
+ storeid => $storeid,
+ target_storeid => $target_storeid,
+ copies => [],
+ };
+ push $groups->{$key}->{copies}->@*,
+ { source => $volname, target => $target_volname, snap => $copy->{snap} };
+ push $groups->{$key}->{orig}->@*, $copy;
+ }
+
+ # Mark each group's copies started as soon as that group returns, not after the
+ # whole batch. A later group can fail, and the caller must still know which copies
+ # are running. Otherwise cleanup cannot tell a started copy from one that never
+ # began, and would either leak a running copy or wait for one that will never
+ # report.
+ for my $key (sort keys %$groups) {
+ my $group = $groups->{$key};
+
+ my $scfg = storage_config($cfg, $group->{storeid});
+ my $target_scfg = storage_config($cfg, $group->{target_storeid});
+
+ my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
+
+ $plugin->copy_images_start(
+ $scfg, $group->{storeid},
+ $target_scfg, $group->{target_storeid},
+ $group->{copies},
+ );
+
+ $_->{started} = 1 for $group->{orig}->@*;
+ }
+
+ return;
+}
+
+# Wait for a started copy to become independent of its source. On failure the target
+# is left for the caller to free, as it owns it since vdisk_copy_prepare().
+#
+# $progress is an optional coderef called with a percentage.
+sub vdisk_copy_wait {
+ my ($cfg, $target_volid, $volid, $progress) = @_;
+
+ my ($target_storeid, $target_volname) = parse_volume_id($target_volid);
+
+ my $target_scfg = storage_config($cfg, $target_storeid);
+ my $plugin = PVE::Storage::Plugin->lookup($target_scfg->{type});
+
+ # What the copy was made from, so the plugin can clean up state it left there.
+ my $source;
+ if ($volid) {
+ my ($storeid, $volname) = parse_volume_id($volid);
+ $source = {
+ scfg => storage_config($cfg, $storeid),
+ storeid => $storeid,
+ volname => $volname,
+ };
+ }
+
+ # A bounded wait, not a bare loop: a backend whose copy dies can otherwise report
+ # 'pending' forever and hang this worker with no error and no progress. The deadline
+ # is generous because the copy is a full data copy, and it is reset whenever the
+ # backend reports forward progress, so a slow-but-live copy is never killed.
+ my $timeout = $target_scfg->{'copy-offload-timeout'} // (24 * 3600);
+ my $deadline = time() + $timeout;
+ my $last_progress;
+ # Poll quickly at first, so a copy that finishes at once is not held up, then back
+ # off: a status call may run backend commands, and a full copy can take hours.
+ my $interval = 1;
+
+ eval {
+ while (1) {
+ my $status = $plugin->copy_image_status(
+ $target_scfg, $target_storeid, $target_volname, $source,
+ );
+
+ my $state = $status->{state} // '';
+ my $done = $status->{progress};
+
+ if (defined($done) && (!defined($last_progress) || $done != $last_progress)) {
+ $progress->(int($done))
+ if $progress && (!defined($last_progress) || int($done) != int($last_progress));
+ $last_progress = $done;
+ $deadline = time() + $timeout; # forward progress: give it the full budget again
+ }
+
+ last if $state eq 'complete';
+
+ die "copy of '$target_volid' reported unexpected state '$state'\n"
+ if $state ne 'pending';
+
+ die "copy of '$target_volid' did not finish within ${timeout}s\n"
+ if time() >= $deadline;
+
+ sleep($interval);
+ $interval = $interval * 2 > 10 ? 10 : $interval * 2;
+ }
+ };
+ # The target is not freed here: the caller got it from vdisk_copy_prepare() and owns
+ # it. Freeing it in two places would leave a window in which a concurrent allocation
+ # takes the name and the second free removes that other volume.
+ die $@ if $@;
+
+ return $target_volid;
+}
+
+# Offload a full copy of $volid to $target_storeid, returning the new volid.
+#
+# The simple path, for a source nothing is writing to: prepare, start, wait. A caller
+# cloning a running guest must not use this. It has to prepare every disk, then start
+# them all inside one freeze, then wait; see the three calls above.
+#
+# The caller is responsible for checking copy_offload_class() first and, for the 'bulk'
+# class with a running guest, for the write-tracking/convergence the class implies.
+sub vdisk_copy {
+ my ($cfg, $volid, $target_storeid, $target_vmid, $snap, $opts, $progress) = @_;
+
+ my $target_volid =
+ vdisk_copy_prepare($cfg, $volid, $target_storeid, $target_vmid, $snap, $opts);
+
+ # The caller only learns the target on success, so a failure is cleaned up here,
+ # including a worker abort during the wait.
+ eval {
+ vdisk_copy_start($cfg, $volid, $target_volid, $snap);
+ vdisk_copy_wait($cfg, $target_volid, $volid, $progress);
+ };
+ if (my $err = $@) {
+ eval { vdisk_free($cfg, $target_volid) };
+ warn "could not clean up target volume '$target_volid' - $@" if $@;
+ die $err;
+ }
+
+ return $target_volid;
+}
+
sub vdisk_create_base {
my ($cfg, $volid) = @_;
diff --git a/src/PVE/Storage/Plugin.pm b/src/PVE/Storage/Plugin.pm
index 998bc68..ef2d5d6 100644
--- a/src/PVE/Storage/Plugin.pm
+++ b/src/PVE/Storage/Plugin.pm
@@ -227,6 +227,28 @@ my $defaultData = {
type => 'boolean',
optional => 1,
},
+ 'copy-offload-timeout' => {
+ title => "Copy Offload Timeout (s)",
+ description =>
+ "Maximum time in seconds to wait for an offloaded copy to become "
+ . "independent of its source. The timer restarts whenever the backend "
+ . "reports progress, so this bounds a stalled copy rather than a slow one.",
+ type => 'integer',
+ minimum => 1,
+ optional => 1,
+ default => 24 * 3600,
+ },
+ 'copy-offload' => {
+ title => "Copy Offload",
+ description =>
+ "Offload full-copy operations (e.g. full clone) to the storage backend "
+ . "instead of copying the data through the host. Only honored by plugins that "
+ . "advertise a 'copy-offload-atomic' or 'copy-offload-bulk' feature; ignored "
+ . "otherwise. Both source and target must be instances of the same plugin type.",
+ type => 'boolean',
+ optional => 1,
+ default => 0,
+ },
path => {
title => "Path",
description => "Path of the storage in the local file system,"
@@ -1261,6 +1283,152 @@ sub clone_image {
return $newvol;
}
+# Whether the backend can copy $volname (or its snapshot $snap) onto the target storage.
+# Returns undef if it can, or a short reason if it cannot. copy_offload_class() asks
+# before choosing offload, so a pair the backend cannot copy (another pool, another
+# filesystem, a format it cannot produce) takes the normal host-side path instead of
+# failing the clone. Keep it cheap: it runs for every disk of a clone. A plugin should
+# repeat in copy_image_prepare() the parts that need no backend probe, so the two cannot
+# disagree; prepare runs under the storage lock, so leave probes out there. Die only for a
+# fault, such as a failed probe: the caller falls back to the host-side copy then too,
+# but with a warning.
+#
+# $opts->{format} is the format the caller resolved for the target. The base accepts
+# everything the copy-offload feature check accepted.
+sub copy_image_check {
+ my ($class, $scfg, $storeid, $volname, $target_scfg, $target_storeid, $snap, $opts) = @_;
+
+ return undef;
+}
+
+# Reserve the target of a full copy of $volname on the target storage and return its
+# volname. The copy itself is asynchronous: copy_image_start() begins it and the caller
+# polls copy_image_status() until it is complete.
+#
+# It must be async because a full array copy runs for minutes to hours, while the core
+# calls this under cluster_lock_storage(). For shared storage that is a pmxcfs lock,
+# whose locked code is killed by an alarm(60) (PVE::Cluster), so a blocking copy would
+# be aborted mid-flight and the lock broken. Splitting start from wait also mirrors what
+# the host-side path already does: it allocates under the lock and runs the long
+# qemu-img convert outside it.
+#
+# The target volname is returned immediately, rather than only when the data is there,
+# so the caller can record it for rollback before anything can go wrong, and
+# free_image() is the cleanup path if the copy later fails or is aborted. A plugin must
+# therefore accept free_image() on a volume whose copy is still in flight, cancelling
+# the backend job.
+#
+# Only called when the plugin advertises 'copy-offload-atomic' or 'copy-offload-bulk'
+# via volume_has_feature() and the target storage has copy-offload enabled, so the base
+# implementation dies rather than returning undef: reaching it means a plugin advertised
+# a capability it does not implement.
+#
+# $target_scfg/$target_storeid describe the target storage. A plugin only ever receives
+# its own per-instance scfg, so it cannot judge whether a given source/target pair can
+# be copied by the backend (same array, same pool group, ...) without them.
+# Both are guaranteed to be instances of the same plugin type as the source; cross-type
+# offload is out of scope, since the operation is opaque to PVE and cannot be lowered to
+# volume_export/volume_import the way a generic copy can.
+#
+# $snap is optional: copy from that snapshot rather than the current state.
+# $opts->{format} is the format the caller resolved for the target. A plugin that cannot
+# produce it must die here rather than produce something else.
+#
+# The copy is split into prepare + start so that a caller cloning a running guest can
+# hold all the disks of a VM at one point in time: it prepares every target first, then
+# fs-freezes the guest once and calls copy_image_start() for each disk inside that
+# freeze. Consistency across disks is a VM-level property, so the core needs a cheap
+# operation it can put inside a freeze, which allocation is not.
+sub copy_image_prepare {
+ my (
+ $class, $scfg, $storeid, $volname,
+ $target_scfg, $target_storeid, $target_vmid, $snap, $opts,
+ ) = @_;
+
+ die "storage plugin '" . $class->type() . "' does not implement copy_image_prepare\n";
+}
+
+# Fix the source's point-in-time and begin copying it into the target that
+# copy_image_prepare() returned. Only after this returns is the copy's content defined.
+#
+# Must be fast and must not block: the caller may hold a guest filesystem freeze (or a
+# suspended VM) across this call for every disk of a VM. Do the minimum that pins the
+# source (create the snapshot/pair) and let the data movement proceed in the
+# background, reported by copy_image_status(). A call that can block on a remote
+# backend needs a timeout, so that a backend that hangs fails the start rather than
+# keeps the guest frozen. Cleanup that is not needed to undo the start belongs in
+# free_image(), which the caller runs on the target after the guest is thawed.
+#
+# For the 'copy-offload-atomic' class this call is what defines the copy's point in
+# time; writes to the source after it returns must not affect the result. A plugin that
+# cannot make that guarantee must advertise 'copy-offload-bulk' instead.
+#
+# $snap names the source snapshot to copy from, or is undef for the current state. It
+# is passed here and not only to prepare because this call is what fixes the point in
+# time: a plugin that took its own snapshot of the current image here would
+# copy live data when the caller asked for a snapshot.
+sub copy_image_start {
+ my (
+ $class, $scfg, $storeid, $volname,
+ $target_scfg, $target_storeid, $target_volname, $snap,
+ ) = @_;
+
+ die "storage plugin '" . $class->type() . "' does not implement copy_image_start\n";
+}
+
+# Start several copies as one operation. $copies is an arrayref of
+# { source => $volname, target => $target_volname, snap => $snap }, all between the
+# same pair of storages. As in copy_image_start(), 'snap' names the source snapshot to
+# copy from and may be undef; each copy in a batch carries its own.
+#
+# The default starts them one after another. A backend that can capture several
+# volumes at one instant (an array consistency group) overrides it: the disks are then
+# crash-consistent with each other even without a guest freeze, and the freeze, when
+# there is one, covers one backend call instead of N. The plugin decides how to split a
+# batch into backend groups; the core only guarantees that every start of a batch
+# happens inside one guest freeze.
+#
+# Same contract as copy_image_start(): fast, non-blocking, and for the atomic class it
+# is this call that fixes the point in time of every copy in the group.
+sub copy_images_start {
+ my ($class, $scfg, $storeid, $target_scfg, $target_storeid, $copies) = @_;
+
+ for my $copy (@$copies) {
+ $class->copy_image_start(
+ $scfg, $storeid, $copy->{source},
+ $target_scfg, $target_storeid, $copy->{target}, $copy->{snap},
+ );
+ }
+
+ return;
+}
+
+# Poll a copy started by copy_image_start(). Returns a hashref:
+#
+# { state => 'pending'|'complete', progress => $percent }
+#
+# 'progress' is optional and informational only. On failure this must die; the caller
+# then frees the target volume.
+#
+# 'complete' means the target is independent of the source, not merely that the data
+# is readable. For backends where independence is asynchronous (an RBD flatten, a ZFS
+# clone still tied to its origin, an array pair that has not dissolved yet) reporting
+# 'complete' early produces something that is not a full copy, and the caller is then
+# free to delete the source.
+#
+# This is called outside the storage lock, so it may talk to the backend, but it should
+# stay cheap: it runs in a poll loop.
+#
+# $source is { scfg, storeid, volname } describing what the copy was made from. A
+# plugin may need it to clean up state it left on the source: an RBD copy, for
+# instance, is a clone of a snapshot taken on the source image, and once the copy is
+# independent nothing else records where it came from.
+sub copy_image_status {
+ my ($class, $scfg, $storeid, $volname, $source) = @_;
+
+ die "storage plugin '" . $class->type() . "' does not implement copy_image_status\n";
+}
+
sub alloc_image {
my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
@@ -1785,6 +1953,18 @@ sub storage_can_replicate {
return 0;
}
+# Besides the features listed below, a plugin may advertise one of the two copy-offload
+# classes (see copy_image_prepare()). They are separate features rather than one feature with a
+# class argument so that this interface stays unchanged; a plugin supports whichever one
+# fits its backend, and a caller that can use both prefers 'copy-offload-atomic':
+#
+# copy-offload-atomic The backend can produce the copy from a static point-in-time
+# source (snapshot/clone), so no host-side mirror is needed to
+# keep it consistent while the source keeps changing.
+# copy-offload-bulk The copy is a bulk data movement that smears over a changing
+# source (e.g. NFS 4.2 server-side copy, zfs send/recv). For a
+# running guest the caller must track writes and converge, the
+# way live migration does.
sub volume_has_feature {
my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running, $opts) = @_;
diff --git a/src/test/copy_offload_test.pm b/src/test/copy_offload_test.pm
new file mode 100644
index 0000000..81a8b82
--- /dev/null
+++ b/src/test/copy_offload_test.pm
@@ -0,0 +1,90 @@
+package PVE::Storage::TestCopyOffload;
+
+use strict;
+use warnings;
+
+use lib qw(..);
+
+use PVE::Storage;
+use Test::More;
+
+# copy_offload_class() decides whether a full copy may be handed to the backend, and
+# which of the two classes to use. Getting it wrong either declines a possible offload
+# or drives one onto a storage pair that cannot perform it, so the rules are pinned here.
+#
+# The checks are: the target storage must have copy-offload enabled, both storages must
+# be instances of the same plugin type, and the plugin must advertise a class for that
+# volume. 'atomic' wins when a plugin advertises both, as it needs no host-side mirror.
+
+my $features = {}; # volid => { feature => 1 }
+
+{
+ no warnings 'redefine';
+ *PVE::Storage::volume_has_feature = sub {
+ my ($cfg, $feature, $volid, $snap, $running, $opts) = @_;
+ return $features->{$volid}->{$feature} ? 1 : 0;
+ };
+}
+
+my $cfg = {
+ ids => {
+ 'rbd-a' => { type => 'rbd', 'copy-offload' => 1 },
+ 'rbd-b' => { type => 'rbd', 'copy-offload' => 1 },
+ 'rbd-off' => { type => 'rbd' },
+ 'dir-a' => { type => 'dir', 'copy-offload' => 1 },
+ },
+};
+
+my $volid = 'rbd-a:vm-100-disk-0';
+
+my $tests = [
+ # [ description, target storeid, advertised features, expected class ]
+ ['nothing advertised => no offload', 'rbd-b', {}, undef],
+ ['atomic advertised', 'rbd-b', { 'copy-offload-atomic' => 1 }, 'atomic'],
+ ['bulk advertised', 'rbd-b', { 'copy-offload-bulk' => 1 }, 'bulk'],
+ [
+ 'atomic preferred when both are advertised',
+ 'rbd-b',
+ { 'copy-offload-atomic' => 1, 'copy-offload-bulk' => 1 },
+ 'atomic',
+ ],
+ [
+ 'target has copy-offload disabled',
+ 'rbd-off',
+ { 'copy-offload-atomic' => 1 },
+ undef,
+ ],
+ [
+ 'cross-type is out of scope even when advertised',
+ 'dir-a',
+ { 'copy-offload-atomic' => 1 },
+ undef,
+ ],
+ [
+ 'same storage as source is allowed',
+ 'rbd-a',
+ { 'copy-offload-atomic' => 1 },
+ 'atomic',
+ ],
+];
+
+plan tests => scalar(@$tests) + 1;
+
+for my $t (@$tests) {
+ my ($desc, $target, $adv, $expected) = @$t;
+
+ $features = { $volid => $adv };
+
+ my $got = PVE::Storage::copy_offload_class($cfg, $volid, $target, undef, 0);
+ is($got, $expected, $desc);
+}
+
+# A path that is not a storage volume must not be mistaken for an offloadable one.
+$features = { $volid => { 'copy-offload-atomic' => 1 } };
+is(
+ PVE::Storage::copy_offload_class($cfg, '/dev/sdb', 'rbd-b', undef, 0),
+ undef,
+ 'a bare path is never offloadable',
+);
+
+done_testing();
diff --git a/src/test/plugin_properties_test.pm b/src/test/plugin_properties_test.pm
index 80d36dc..3699c58 100644
--- a/src/test/plugin_properties_test.pm
+++ b/src/test/plugin_properties_test.pm
@@ -19,6 +19,8 @@ my $available = {
comstar_tg => 'string',
content => 'string:pve-storage-content-list',
'content-dirs' => 'string:pve-dir-override-list',
+ 'copy-offload' => 'boolean',
+ 'copy-offload-timeout' => 'integer minimum=1',
'create-base-path' => 'boolean',
'create-subdirs' => 'boolean',
'data-pool' => 'string',
@@ -138,7 +140,8 @@ for my $name (sort keys $available->%*) {
# adding one can break third-party plugins. A property belongs here only if methods of the base
# plugin or code outside of the plugins read it from storages of any type.
my $base = [qw(
- base bwlimit content content-dirs create-base-path create-subdirs disable format
+ base bwlimit content content-dirs copy-offload copy-offload-timeout create-base-path
+ create-subdirs disable format
max-protected-backups mkdir nodes path preallocation prune-backups shared
snapshot-as-volume-chain storage type
)];
diff --git a/src/test/run_plugin_tests.pl b/src/test/run_plugin_tests.pl
index da0d0de..57e3140 100755
--- a/src/test/run_plugin_tests.pl
+++ b/src/test/run_plugin_tests.pl
@@ -25,6 +25,7 @@ my $res = $harness->runtests(
"plugin_isolation_test.pm",
"plugin_load_test.pm",
"plugin_properties_test.pm",
+ "copy_offload_test.pm",
);
exit -1 if !$res || $res->{failed} || $res->{parse_errors};
--
2.55.0
next prev parent reply other threads:[~2026-11-26 16:57 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 16:56 [RFC PATCH storage/qemu-server/docs v2 0/6] offload full clone to the storage backend Ciro Iriarte
2026-11-26 16:56 ` Ciro Iriarte [this message]
2026-11-26 16:56 ` [RFC PATCH storage v2 2/6] rbd: implement copy-offload via snapshot + clone + flatten Ciro Iriarte
2026-11-26 16:57 ` [RFC PATCH storage v2 3/6] dir: implement copy-offload via reflink (FICLONE) Ciro Iriarte
2026-11-26 16:57 ` [RFC PATCH storage v2 4/6] btrfs, lvmthin: implement copy-offload (atomic class) Ciro Iriarte
2026-11-26 16:57 ` [RFC PATCH qemu-server v2 5/6] use storage copy-offload for full clone Ciro Iriarte
2026-11-26 16:57 ` [RFC PATCH docs v2 6/6] pvesm: document copy-offload and add it to the storage capability table Ciro Iriarte
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261126.1.copyoffload-v2@cyruspy.gmail.com \
--to=cyruspy@gmail.com \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.