From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id A89B096246 for ; Mon, 23 Jan 2023 16:59:19 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 88CA429E71 for ; Mon, 23 Jan 2023 16:58:49 +0100 (CET) Received: from lana.proxmox.com (unknown [94.136.29.99]) by firstgate.proxmox.com (Proxmox) with ESMTP for ; Mon, 23 Jan 2023 16:58:48 +0100 (CET) Received: by lana.proxmox.com (Postfix, from userid 10043) id 7B9712C24C8; Mon, 23 Jan 2023 16:58:48 +0100 (CET) From: Stefan Hanreich To: pve-devel@lists.proxmox.com Date: Mon, 23 Jan 2023 16:58:47 +0100 Message-Id: <20230123155847.598536-3-s.hanreich@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230123155847.598536-1-s.hanreich@proxmox.com> References: <20230123155847.598536-1-s.hanreich@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 2.069 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 KAM_LAZY_DOMAIN_SECURITY 1 Sending domain does not have any anti-forgery methods NO_DNS_FOR_FROM 0.001 Envelope sender has no MX or A DNS records PROLO_LEO2 0.1 Meta Catches all Leo drug variations so far RCVD_IN_DNSWL_HI -5 Sender listed at https://www.dnswl.org/, high trust RDNS_NONE 0.793 Delivered to internal network by a host with no rDNS SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_NONE 0.001 SPF: sender does not publish an 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. [abstractconfig.pm] Subject: [pve-devel] [PATCH pve-guest-common v3 1/1] partial fix #2530: snapshots: add pre/post/failed-snapshot hooks X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 23 Jan 2023 15:59:19 -0000 This commit adds hooks to the snapshotting process, which can be used to run additional setup scripts to prepare the VM for snapshotting. Examples for use cases include: - forcing processes to flush their writes - blocking processes from writing - altering the configuration of the VM to make snapshotting possible The prepare step has been split into two parts, so the configuration can be locked a bit earlier during the snapshotting process. Doing it this way ensures that the configuration is already locked during the pre-snapshot hook. Because of this split, the VM config gets written in two stages now, rather than one. In case of failure during the preparation step - after the lock is written - error handling has been added so the lock gets released properly. The failed-snapshot hook runs when the snapshot or a snapshot hook fails, but only if the pre-snapshot hook ran already. This enables users to revert any changes done during the pre-snapshot hookscript. The preparation step assumes that the hook does not convert the current VM into a template, which is why the basic checks are not re-run after the pre-snapshot hook. The storage check runs after the pre-snapshot hook, because the hook might get used to setup the storage for snapshotting. If the hook would run after the storage checks, this becomes impossible. cfs_update() gets called after every invocation of a hookscript, since it is impossible to know which changes get made by the hookscript. Doing this ensures that we see the updated state of the CFS after the hookscript got invoked. Signed-off-by: Stefan Hanreich --- src/PVE/AbstractConfig.pm | 76 +++++++++++++++++++++++++++++++++++---- 1 file changed, 70 insertions(+), 6 deletions(-) diff --git a/src/PVE/AbstractConfig.pm b/src/PVE/AbstractConfig.pm index a0c0bc6..9763c0c 100644 --- a/src/PVE/AbstractConfig.pm +++ b/src/PVE/AbstractConfig.pm @@ -704,14 +704,28 @@ sub __snapshot_apply_config { return $newconf; } +sub __snapshot_failed { + my ($class, $vmid, $phase) = @_; + + my $conf = $class->load_config($vmid); + + return if !$conf->{hookscript}; + + { + local $ENV{'PVE_SNAPSHOT_PHASE'} = $phase; + PVE::GuestHelpers::exec_hookscript($conf, $vmid, "failed-snapshot"); + } + + PVE::Cluster::cfs_update(); +} + # Prepares the configuration for snapshotting. sub __snapshot_prepare { my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_; my $snap; - my $updatefn = sub { - + my $run_checks = sub { my $conf = $class->load_config($vmid); die "you can't take a snapshot if it's a template\n" @@ -721,15 +735,21 @@ sub __snapshot_prepare { $conf->{lock} = 'snapshot'; - my $snapshots = $conf->{snapshots}; - die "snapshot name '$snapname' already used\n" - if defined($snapshots->{$snapname}); + if defined($conf->{snapshots}->{$snapname}); + $class->write_config($vmid, $conf); + }; + + my $updatefn = sub { + my $conf = $class->load_config($vmid); my $storecfg = PVE::Storage::config(); + die "snapshot feature is not available\n" if !$class->has_feature('snapshot', $conf, $storecfg, undef, undef, $snapname eq 'vzdump'); + my $snapshots = $conf->{snapshots}; + for my $snap (sort keys %$snapshots) { my $parent_name = $snapshots->{$snap}->{parent} // ''; if ($snapname eq $parent_name) { @@ -753,7 +773,33 @@ sub __snapshot_prepare { $class->write_config($vmid, $conf); }; - $class->lock_config($vmid, $updatefn); + $class->lock_config($vmid, $run_checks); + + my $conf = $class->load_config($vmid); + + if ($conf->{hookscript}) { + eval { + PVE::GuestHelpers::exec_hookscript($conf, $vmid, "pre-snapshot", 1); + }; + my $err = $@; + + PVE::Cluster::cfs_update(); + + if ($err) { + $class->__snapshot_failed($vmid, 'pre-snapshot'); + $class->remove_lock($vmid, 'snapshot'); + die $err; + } + } + + eval { + $class->lock_config($vmid, $updatefn); + }; + if (my $err = $@) { + $class->__snapshot_failed($vmid, 'prepare'); + $class->remove_lock($vmid, 'snapshot'); + die $err; + } return $snap; } @@ -837,11 +883,29 @@ sub snapshot_create { if ($err) { warn "snapshot create failed: starting cleanup\n"; + eval { $class->snapshot_delete($vmid, $snapname, 1, $drivehash); }; warn "$@" if $@; + + $class->__snapshot_failed($vmid, 'snapshot'); + die "$err\n"; } + if ($conf->{hookscript}) { + eval { + PVE::GuestHelpers::exec_hookscript($conf, $vmid, "post-snapshot", 1); + }; + $err = $@; + + PVE::Cluster::cfs_update(); + + if ($err) { + warn $err; + $class->__snapshot_failed($vmid, 'post-snapshot'); + } + } + $class->__snapshot_commit($vmid, $snapname); } -- 2.30.2