public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Demayl <denis.kanchev@storpool.com>
To: pve-devel@lists.proxmox.com
Subject: [Add group snapshots 2/4] snapshot: add atomic snapshots
Date: Tue,  1 Dec 2026 16:19:07 +0200	[thread overview]
Message-ID: <20261201141918.35872-3-denis.kanchev@storpool.com> (raw)
In-Reply-To: <20261201141918.35872-1-denis.kanchev@storpool.com>

Allows atomic (group) snapshots when the storage allows it.
It must be enabled as a feature in the storage.
The plugindata feature atomic_snapshots is a Hash adding
2 options:
freeze_fs=1 - bool allows freezefs if needed.
allow_different_templates=0 - bool allows snapshotting
volumes from different storage templates.

Without atomic_snapshots enabled it behaves like before
and creates sequential volume snapshots.

Signed-off-by: Demayl <denis.kanchev@storpool.com>
---
 src/PVE/AbstractConfig.pm | 94 ++++++++++++++++++++++++++++++++++-----
 1 file changed, 82 insertions(+), 12 deletions(-)

diff --git a/src/PVE/AbstractConfig.pm b/src/PVE/AbstractConfig.pm
index 68e203c..8f68d9e 100644
--- a/src/PVE/AbstractConfig.pm
+++ b/src/PVE/AbstractConfig.pm
@@ -1056,10 +1056,69 @@ sub __snapshot_activate_storages {
     return;
 }
 
+# Get list of atomic volumes data and checks if all volumes have the atomic feature on
+# plugindata->features->atomic_snapshot
+sub get_atomic_snapshot_volumes {
+    my ($class, $snap) = @_;
+    my $store_main_id  = undef;
+    my $storage_main   = undef;
+    my $feature_atomic = undef;
+    my $store_cfg    = PVE::Storage::config();
+    my @volumes;
+
+    $class->foreach_volume($snap, sub {
+        my ($vs, $volume) = @_;
+        if ($volume->{file} && $volume->{file} ne 'none') {
+            my $volid = $volume->{file};
+            my ( $storeid, $volname )  = PVE::Storage::parse_volume_id($volid);
+
+            die "unable to parse volume ID '$volid'\n" if !$storeid;
+
+            my $storage        = PVE::Storage::storage_config($store_cfg, $storeid);
+            $feature_atomic = PVE::Storage::storage_has_feature(
+            $store_cfg, $storeid, 'atomic_snapshot');
+            $store_main_id     = $storeid if !defined $store_main_id;
+            $storage_main      = $storage->{type} if !defined $storage_main;
+            $volume->{storeid} = $storeid;
+            $volume->{storage} = $storage;
+            $volume->{volname} = $volname;
+            $volume->{drive}   = $vs;
+
+            if (!$feature_atomic) {
+                die "storage '$storeid' doesn't support atomic snapshots"
+            }
+
+            if (
+            !$feature_atomic->{allow_different_templates}
+            && $storeid ne $store_main_id
+            ) {
+                die "storage '$storeid' differs from '$store_main_id' "
+                ."and can't perform atomic snapshots on different templates!"
+            }
+
+            if ($storage_main ne $storage->{type}) {
+                die "atomic snapshots are not allowed for different storages.";
+            }
+            push @volumes, $volume;
+        }
+    });
+    return { volumes => \@volumes, config => $feature_atomic };
+}
+
+sub __snapshot_volumes_atomically {
+    my ($class, $voldata, $snapname) = @_;
+    my $scfg     = $voldata->[0]->{storage};
+    my $storeid  = $voldata->[0]->{storeid};
+    my @volnames = map {{ name => $_->{volname}, storeid => $_->{storeid} }} @$voldata;
+    # Every volume can be part of different store
+    PVE::Storage::volumes_atomic_snapshot($scfg, \@volnames, $snapname);
+}
+
 # Creates a snapshot for the VM/CT.
 sub snapshot_create {
-    my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
+    my ($class, $vmid, $snapname, $save_vmstate, $comment, $type) = @_;
 
+    my $atomic = $type && $type eq 'atomic';
     my $snap = $class->__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
 
     $save_vmstate = 0 if !$snap->{vmstate};
@@ -1070,32 +1129,43 @@ sub snapshot_create {
         $class->__snapshot_check_freeze_needed($vmid, $conf, $snap->{vmstate});
 
     my $drivehash = {};
+    my $allow_freeze = 1;
+    my $atomic_volumes;
 
     eval {
         $class->__snapshot_activate_storages($conf, 0);
 
-        if ($freezefs) {
-            print "freeze guest filesystem\n";
+        if ($atomic) {
+            my $atomic_data = $class->get_atomic_snapshot_volumes($snap);
+            if (defined $atomic_data->{config}->{freeze_fs}) {
+                $allow_freeze = $atomic_data->{config}->{freeze_fs};
+            }
+            $atomic_volumes = $atomic_data->{volumes};
+        }
+
+        if ($freezefs && $allow_freeze) {
             $class->__snapshot_freeze($vmid, 0);
         }
 
         $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "before");
 
-        $class->foreach_volume(
-            $snap,
-            sub {
-                my ($vs, $volume) = @_;
+        if ($atomic) {
+            $class->__snapshot_volumes_atomically($atomic_volumes, $snapname);
+            $drivehash = { map { $_->{drive} => 1 } @$atomic_volumes };
+        } else {
+            $class->foreach_volume($snap, sub {
+            my ($vs, $volume) = @_;
 
-                $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname);
-                $drivehash->{$vs} = 1;
-            },
-        );
+            $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname);
+            $drivehash->{$vs} = 1;
+            });
+        }
     };
     my $err = $@;
 
     if ($running) {
         $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after");
-        if ($freezefs) {
+        if ($freezefs && $allow_freeze) {
             print "thaw guest filesystem\n";
             $class->__snapshot_freeze($vmid, 1);
         }
-- 
2.53.0




  parent reply	other threads:[~2026-12-01 14:20 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 14:19 [Add group snapshots 0/4] Adds support for atomic snapshots Demayl
2026-12-01 14:19 ` [Add group snapshots 1/4] plugin: add volumes_atomic_snapshot Demayl
2026-12-01 14:19 ` Demayl [this message]
2026-12-01 14:19 ` [Add group snapshots 3/4] snapshot: add atomic_snapshot Demayl
2026-12-01 14:19 ` [Add group snapshots 4/4] ui: add atomic snapshot type when creating a snapshot Demayl
2026-12-01 16:19 ` [Add group snapshots 0/4] Adds support for atomic snapshots DERUMIER, Alexandre

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=20261201141918.35872-3-denis.kanchev@storpool.com \
    --to=denis.kanchev@storpool.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal