public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Fabian Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH v2 storage 7/7] pbs: integrate support for protected
Date: Thu, 30 Sep 2021 13:42:10 +0200	[thread overview]
Message-ID: <20210930114215.240095-8-f.ebner@proxmox.com> (raw)
In-Reply-To: <20210930114215.240095-1-f.ebner@proxmox.com>

free_image doesn't need to check for protection, because that will
happen on the server.

Getting/updating notes has also been refactored to re-use the code
for the PBS api calls.

Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
---

Changes from v1:
    * Rebase, because of newly introduced fall-back to
      {get, update}_volume_notes.
    * Set mark 'protected' rather than 'keep' if backup is protected
      in prune_backups().

Needs new external dependency for strptime, because it's not in perl's
POSIX module.

An alternative would be to use perlmod and export the proxmox crate's
function for parsing the timestring.

Also depends on Dominik's patches for PBS:
https://lists.proxmox.com/pipermail/pbs-devel/2021-September/004099.html

 PVE/Storage/PBSPlugin.pm | 66 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 64 insertions(+), 2 deletions(-)

diff --git a/PVE/Storage/PBSPlugin.pm b/PVE/Storage/PBSPlugin.pm
index 76699e6..06ebfa7 100644
--- a/PVE/Storage/PBSPlugin.pm
+++ b/PVE/Storage/PBSPlugin.pm
@@ -9,7 +9,8 @@ use Fcntl qw(F_GETFD F_SETFD FD_CLOEXEC);
 use IO::File;
 use JSON;
 use MIME::Base64 qw(decode_base64);
-use POSIX qw(strftime ENOENT);
+use POSIX qw(mktime strftime ENOENT);
+use POSIX::strptime;
 
 use PVE::APIClient::LWP;
 use PVE::JSONSchema qw(get_standard_option);
@@ -218,6 +219,36 @@ sub print_volid {
     return "${storeid}:${volname}";
 }
 
+# essentially the inverse of print_volid
+sub api_param_from_volname {
+    my ($class, $volname) = @_;
+
+    my $name = ($class->parse_volname($volname))[1];
+
+    my ($btype, $bid, $timestr) = split('/', $name);
+
+    my @tm = (POSIX::strptime($timestr, "%FT%TZ"));
+    # expect sec, min, hour, mday, mon, year
+    die "error parsing time from '$volname'" if grep { !defined($_) } @tm[0..5];
+
+    my $btime;
+    {
+	local $ENV{TZ} = 'UTC'; # $timestr is UTC
+
+	# Fill in isdst to avoid undef warning. No daylight saving time for UTC.
+	$tm[8] //= 0;
+
+	my $since_epoch = mktime(@tm) or die "error converting time from '$volname'\n";
+	$btime = int($since_epoch);
+    }
+
+    return {
+	'backup-type' => $btype,
+	'backup-id' => $bid,
+	'backup-time' => $btime,
+    };
+}
+
 my $USE_CRYPT_PARAMS = {
     backup => 1,
     restore => 1,
@@ -403,9 +434,12 @@ sub prune_backups {
 		my $vmid = $backup->{'backup-id'};
 		my $volid = print_volid($storeid, $type, $vmid, $ctime);
 
+		my $mark = $backup->{keep} ? 'keep' : 'remove';
+		$mark = 'protected' if $backup->{protected};
+
 		push @{$prune_list}, {
 		    ctime => $ctime,
-		    mark => $backup->{keep} ? 'keep' : 'remove',
+		    mark => $mark,
 		    type => $type eq 'vm' ? 'qemu' : 'lxc',
 		    vmid => $vmid,
 		    volid => $volid,
@@ -658,6 +692,7 @@ sub list_volumes {
 
 	$info->{verification} = $item->{verification} if defined($item->{verification});
 	$info->{notes} = $item->{comment} if defined($item->{comment});
+	$info->{protected} = 1 if $item->{protected};
 	if (defined($item->{fingerprint})) {
 	    $info->{encrypted} = $item->{fingerprint};
 	} elsif (snapshot_files_encrypted($item->{files})) {
@@ -813,6 +848,21 @@ sub get_volume_attribute {
 	return $class->get_volume_notes($scfg, $storeid, $volname);
     }
 
+    if ($attribute eq 'protected') {
+	my $param = $class->api_param_from_volname($volname);
+
+	my $password = pbs_get_password($scfg, $storeid);
+	my $conn = pbs_api_connect($scfg, $password);
+	my $datastore = $scfg->{datastore};
+
+	my $res = eval { $conn->get("/api2/json/admin/datastore/$datastore/$attribute", $param); };
+	if (my $err = $@) {
+	    return if $err->{code} == 404; # not supported
+	    die $err;
+	}
+	return $res;
+    }
+
     return;
 }
 
@@ -823,6 +873,18 @@ sub update_volume_attribute {
 	return $class->update_volume_notes($scfg, $storeid, $volname, $value);
     }
 
+    if ($attribute eq 'protected') {
+	my $param = $class->api_param_from_volname($volname);
+	$param->{$attribute} = $value;
+
+	my $password = pbs_get_password($scfg, $storeid);
+	my $conn = pbs_api_connect($scfg, $password);
+	my $datastore = $scfg->{datastore};
+
+	$conn->put("/api2/json/admin/datastore/$datastore/$attribute", $param);
+	return;
+    }
+
     die "attribute '$attribute' is not supported for storage type '$scfg->{type}'\n";
 }
 
-- 
2.30.2





  parent reply	other threads:[~2021-09-30 11:42 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-30 11:42 [pve-devel] [PATCH-SERIES v2 manager/storage] fix #3307: allow backups to be marked as protected Fabian Ebner
2021-09-30 11:42 ` [pve-devel] [PATCH v2 storage 1/7] dir plugin: update notes: don't fail if file is already removed Fabian Ebner
2021-09-30 11:42 ` [pve-devel] [PATCH v2 storage 2/7] dir plugin: get notes: return undef if notes are not supported Fabian Ebner
2021-09-30 11:42 ` [pve-devel] [PATCH v2 storage 3/7] add generalized functions to manage volume attributes Fabian Ebner
2021-09-30 11:42 ` [pve-devel] [PATCH v2 storage 4/7] prune mark: preserve additional information for the keep-all case Fabian Ebner
2021-09-30 11:42 ` [pve-devel] [PATCH v2 storage 5/7] fix #3307: make it possible to set protection for backups Fabian Ebner
2021-09-30 11:42 ` [pve-devel] [PATCH v2 storage 6/7] prune: mark renamed and protected backups differently Fabian Ebner
2021-09-30 11:42 ` Fabian Ebner [this message]
2021-09-30 11:42 ` [pve-devel] [PATCH v2 manager 1/5] vzdump: skip protected backups for dumpdir pruning Fabian Ebner
2021-09-30 11:42 ` [pve-devel] [PATCH v2 manager 2/5] ui: storage content: avoid redundant options hasNotesColumn and hideColumns Fabian Ebner
2021-09-30 11:42 ` [pve-devel] [PATCH v2 manager 3/5] ui: backup views: add protected column Fabian Ebner
2021-09-30 11:42 ` [pve-devel] [PATCH v2 manager 4/5] ui: backup views: add button to change protection status Fabian Ebner
2021-09-30 11:42 ` [pve-devel] [PATCH v2 manager 5/5] ui: prune: also handle new 'renamed' keep reason Fabian Ebner
2021-11-09 16:51 ` [pve-devel] applied-series: [PATCH-SERIES v2 manager/storage] fix #3307: allow backups to be marked as protected Fabian Grünbichler

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=20210930114215.240095-8-f.ebner@proxmox.com \
    --to=f.ebner@proxmox.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