1 package PVE
::Storage
::PBSPlugin
;
3 # Plugin to access Proxmox Backup Server
8 use Fcntl
qw(F_GETFD F_SETFD FD_CLOEXEC);
13 use POSIX
qw(strftime ENOENT);
15 use PVE
::JSONSchema
qw(get_standard_option);
17 use PVE
::Storage
::Plugin
;
18 use PVE
::Tools
qw(run_command file_read_firstline trim dir_glob_regex dir_glob_foreach $IPV6RE);
20 use base
qw(PVE::Storage::Plugin);
30 content
=> [ {backup
=> 1, none
=> 1}, { backup
=> 1 }],
37 description
=> "Proxmox Backup Server datastore name.",
40 # openssl s_client -connect <host>:8007 2>&1 |openssl x509 -fingerprint -sha256
41 fingerprint
=> get_standard_option
('fingerprint-sha256'),
43 description
=> "Encryption key. Use 'autogen' to generate one automatically without passphrase.",
47 description
=> "For non default port.",
58 server
=> { fixed
=> 1 },
59 datastore
=> { fixed
=> 1 },
60 port
=> { optional
=> 1 },
61 nodes
=> { optional
=> 1},
62 disable
=> { optional
=> 1},
63 content
=> { optional
=> 1},
64 username
=> { optional
=> 1 },
65 password
=> { optional
=> 1 },
66 'encryption-key' => { optional
=> 1 },
67 maxfiles
=> { optional
=> 1 },
68 'prune-backups' => { optional
=> 1 },
69 fingerprint
=> { optional
=> 1 },
75 sub pbs_password_file_name
{
76 my ($scfg, $storeid) = @_;
78 return "/etc/pve/priv/storage/${storeid}.pw";
81 sub pbs_set_password
{
82 my ($scfg, $storeid, $password) = @_;
84 my $pwfile = pbs_password_file_name
($scfg, $storeid);
85 mkdir "/etc/pve/priv/storage";
87 PVE
::Tools
::file_set_contents
($pwfile, "$password\n");
90 sub pbs_delete_password
{
91 my ($scfg, $storeid) = @_;
93 my $pwfile = pbs_password_file_name
($scfg, $storeid);
98 sub pbs_get_password
{
99 my ($scfg, $storeid) = @_;
101 my $pwfile = pbs_password_file_name
($scfg, $storeid);
103 return PVE
::Tools
::file_read_firstline
($pwfile);
106 sub pbs_encryption_key_file_name
{
107 my ($scfg, $storeid) = @_;
109 return "/etc/pve/priv/storage/${storeid}.enc";
112 sub pbs_set_encryption_key
{
113 my ($scfg, $storeid, $key) = @_;
115 my $pwfile = pbs_encryption_key_file_name
($scfg, $storeid);
116 mkdir "/etc/pve/priv/storage";
118 PVE
::Tools
::file_set_contents
($pwfile, "$key\n");
121 sub pbs_delete_encryption_key
{
122 my ($scfg, $storeid) = @_;
124 my $pwfile = pbs_encryption_key_file_name
($scfg, $storeid);
126 if (!unlink $pwfile) {
127 return if $! == ENOENT
;
128 die "failed to delete encryption key! $!\n";
130 delete $scfg->{'encryption-key'};
133 sub pbs_get_encryption_key
{
134 my ($scfg, $storeid) = @_;
136 my $pwfile = pbs_encryption_key_file_name
($scfg, $storeid);
138 return PVE
::Tools
::file_get_contents
($pwfile);
141 # Returns a file handle if there is an encryption key, or `undef` if there is not. Dies on error.
142 sub pbs_open_encryption_key
{
143 my ($scfg, $storeid) = @_;
145 my $encryption_key_file = pbs_encryption_key_file_name
($scfg, $storeid);
148 if (!open($keyfd, '<', $encryption_key_file)) {
149 return undef if $! == ENOENT
;
150 die "failed to open encryption key: $encryption_key_file: $!\n";
157 my ($storeid, $btype, $bid, $btime) = @_;
159 my $time_str = strftime
("%FT%TZ", gmtime($btime));
160 my $volname = "backup/${btype}/${bid}/${time_str}";
162 return "${storeid}:${volname}";
165 my sub get_server_with_port
{
168 my $server = $scfg->{server
};
169 $server = "[$server]" if $server =~ /^$IPV6RE$/;
171 if (my $port = $scfg->{port
}) {
172 $server .= ":$port" if $port != 8007;
177 my $USE_CRYPT_PARAMS = {
183 my sub do_raw_client_cmd
{
184 my ($scfg, $storeid, $client_cmd, $param, %opts) = @_;
186 my $use_crypto = $USE_CRYPT_PARAMS->{$client_cmd};
188 my $client_exe = '/usr/bin/proxmox-backup-client';
189 die "executable not found '$client_exe'! Proxmox backup client not installed?\n"
192 my $server = get_server_with_port
($scfg);
193 my $datastore = $scfg->{datastore
};
194 my $username = $scfg->{username
} // 'root@pam';
196 my $userns_cmd = delete $opts{userns_cmd
};
200 push @$cmd, @$userns_cmd if defined($userns_cmd);
202 push @$cmd, $client_exe, $client_cmd;
204 # This must live in the top scope to not get closed before the `run_command`
207 if (defined($keyfd = pbs_open_encryption_key
($scfg, $storeid))) {
208 my $flags = fcntl($keyfd, F_GETFD
, 0)
209 // die "failed to get file descriptor flags: $!\n";
210 fcntl($keyfd, F_SETFD
, $flags & ~FD_CLOEXEC
)
211 or die "failed to remove FD_CLOEXEC from encryption key file descriptor\n";
212 push @$cmd, '--crypt-mode=encrypt', '--keyfd='.fileno($keyfd);
214 push @$cmd, '--crypt-mode=none';
218 push @$cmd, @$param if defined($param);
220 push @$cmd, "--repository", "$username\@$server:$datastore";
222 local $ENV{PBS_PASSWORD
} = pbs_get_password
($scfg, $storeid);
224 local $ENV{PBS_FINGERPRINT
} = $scfg->{fingerprint
};
226 # no ascii-art on task logs
227 local $ENV{PROXMOX_OUTPUT_NO_BORDER
} = 1;
228 local $ENV{PROXMOX_OUTPUT_NO_HEADER
} = 1;
230 if (my $logfunc = $opts{logfunc
}) {
231 $logfunc->("run: " . join(' ', @$cmd));
234 run_command
($cmd, %opts);
237 # FIXME: External perl code should NOT have access to this.
239 # There should be separate functions to
244 sub run_raw_client_cmd
{
245 my ($scfg, $storeid, $client_cmd, $param, %opts) = @_;
246 return do_raw_client_cmd
($scfg, $storeid, $client_cmd, $param, %opts);
250 my ($scfg, $storeid, $client_cmd, $param, $no_output) = @_;
253 my $outfunc = sub { $json_str .= "$_[0]\n" };
255 $param = [] if !defined($param);
256 $param = [ $param ] if !ref($param);
258 $param = [@$param, '--output-format=json'] if !$no_output;
260 do_raw_client_cmd
($scfg, $storeid, $client_cmd, $param,
261 outfunc
=> $outfunc, errmsg
=> 'proxmox-backup-client failed');
263 return undef if $no_output;
265 my $res = decode_json
($json_str);
270 # Storage implementation
272 sub extract_vzdump_config
{
273 my ($class, $scfg, $volname, $storeid) = @_;
275 my ($vtype, $name, $vmid, undef, undef, undef, $format) = $class->parse_volname($volname);
278 my $outfunc = sub { $config .= "$_[0]\n" };
281 if ($format eq 'pbs-vm') {
282 $config_name = 'qemu-server.conf';
283 } elsif ($format eq 'pbs-ct') {
284 $config_name = 'pct.conf';
286 die "unable to extract configuration for backup format '$format'\n";
289 do_raw_client_cmd
($scfg, $storeid, 'restore', [ $name, $config_name, '-' ],
290 outfunc
=> $outfunc, errmsg
=> 'proxmox-backup-client failed');
296 my ($class, $scfg, $storeid, $keep, $vmid, $type, $dryrun, $logfunc) = @_;
298 $logfunc //= sub { print "$_[1]\n" };
300 my $backups = $class->list_volumes($storeid, $scfg, $vmid, ['backup']);
302 $type = 'vm' if defined($type) && $type eq 'qemu';
303 $type = 'ct' if defined($type) && $type eq 'lxc';
305 my $backup_groups = {};
306 foreach my $backup (@{$backups}) {
307 (my $backup_type = $backup->{format
}) =~ s/^pbs-//;
309 next if defined($type) && $backup_type ne $type;
311 my $backup_group = "$backup_type/$backup->{vmid}";
312 $backup_groups->{$backup_group} = 1;
317 my $keep_all = delete $keep->{'keep-all'};
320 foreach my $opt (keys %{$keep}) {
321 next if $keep->{$opt} == 0;
322 push @param, "--$opt";
323 push @param, "$keep->{$opt}";
325 } else { # no need to pass anything to PBS
326 $keep = { 'keep-all' => 1 };
329 push @param, '--dry-run' if $dryrun;
334 foreach my $backup_group (keys %{$backup_groups}) {
335 $logfunc->('info', "running 'proxmox-backup-client prune' for '$backup_group'")
338 my $res = run_client_cmd
($scfg, $storeid, 'prune', [ $backup_group, @param ]);
340 foreach my $backup (@{$res}) {
341 die "result from proxmox-backup-client is not as expected\n"
342 if !defined($backup->{'backup-time'})
343 || !defined($backup->{'backup-type'})
344 || !defined($backup->{'backup-id'})
345 || !defined($backup->{'keep'});
347 my $ctime = $backup->{'backup-time'};
348 my $type = $backup->{'backup-type'};
349 my $vmid = $backup->{'backup-id'};
350 my $volid = print_volid
($storeid, $type, $vmid, $ctime);
352 push @{$prune_list}, {
354 mark
=> $backup->{keep
} ?
'keep' : 'remove',
355 type
=> $type eq 'vm' ?
'qemu' : 'lxc',
362 $logfunc->('err', "prune '$backup_group': $err\n");
366 die "error pruning backups - check log\n" if $failed;
371 my $autogen_encryption_key = sub {
372 my ($scfg, $storeid) = @_;
373 my $encfile = pbs_encryption_key_file_name
($scfg, $storeid);
374 my $cmd = ['proxmox-backup-client', 'key', 'create', '--kdf', 'none', $encfile];
375 run_command
($cmd, errmsg
=> 'failed to create encryption key');
376 return PVE
::Tools
::file_get_contents
($encfile);
380 my ($class, $storeid, $scfg, %param) = @_;
384 if (defined(my $password = $param{password
})) {
385 pbs_set_password
($scfg, $storeid, $password);
387 pbs_delete_password
($scfg, $storeid);
390 if (defined(my $encryption_key = $param{'encryption-key'})) {
391 if ($encryption_key eq 'autogen') {
392 $res->{'encryption-key'} = $autogen_encryption_key->($scfg, $storeid);
394 pbs_set_encryption_key
($scfg, $storeid, $encryption_key);
395 $res->{'encryption-key'} = $encryption_key;
397 $scfg->{'encryption-key'} = 1;
399 pbs_delete_encryption_key
($scfg, $storeid);
406 my ($class, $storeid, $scfg, %param) = @_;
410 if (exists($param{password
})) {
411 if (defined($param{password
})) {
412 pbs_set_password
($scfg, $storeid, $param{password
});
414 pbs_delete_password
($scfg, $storeid);
418 if (exists($param{'encryption-key'})) {
419 if (defined(my $encryption_key = delete($param{'encryption-key'}))) {
420 if ($encryption_key eq 'autogen') {
421 $res->{'encryption-key'} = $autogen_encryption_key->($scfg, $storeid);
423 pbs_set_encryption_key
($scfg, $storeid, $encryption_key);
424 $res->{'encryption-key'} = $encryption_key;
426 $scfg->{'encryption-key'} = 1;
428 pbs_delete_encryption_key
($scfg, $storeid);
436 my ($class, $storeid, $scfg) = @_;
438 pbs_delete_password
($scfg, $storeid);
439 pbs_delete_encryption_key
($scfg, $storeid);
443 my ($class, $volname) = @_;
445 if ($volname =~ m!^backup/([^\s_]+)/([^\s_]+)/([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z)$!) {
449 my $format = "pbs-$btype";
451 my $name = "$btype/$bid/$btime";
453 if ($bid =~ m/^\d+$/) {
454 return ('backup', $name, $bid, undef, undef, undef, $format);
456 return ('backup', $name, undef, undef, undef, undef, $format);
460 die "unable to parse PBS volume name '$volname'\n";
464 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
466 die "volume snapshot is not possible on pbs storage"
467 if defined($snapname);
469 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
471 my $server = get_server_with_port
($scfg);
472 my $datastore = $scfg->{datastore
};
473 my $username = $scfg->{username
} // 'root@pam';
475 # artifical url - we currently do not use that anywhere
476 my $path = "pbs://$username\@$server:$datastore/$name";
478 return ($path, $vmid, $vtype);
482 my ($class, $storeid, $scfg, $volname) = @_;
484 die "can't create base images in pbs storage\n";
488 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
490 die "can't clone images in pbs storage\n";
494 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
496 die "can't allocate space in pbs storage\n";
500 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
502 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
504 run_client_cmd
($scfg, $storeid, "forget", [ $name ], 1);
509 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
517 my ($class, $storeid, $scfg, $vmid, $content_types) = @_;
521 return $res if !grep { $_ eq 'backup' } @$content_types;
523 my $data = run_client_cmd
($scfg, $storeid, "snapshots");
525 foreach my $item (@$data) {
526 my $btype = $item->{"backup-type"};
527 my $bid = $item->{"backup-id"};
528 my $epoch = $item->{"backup-time"};
529 my $size = $item->{size
} // 1;
531 next if !($btype eq 'vm' || $btype eq 'ct');
532 next if $bid !~ m/^\d+$/;
533 next if defined($vmid) && $bid ne $vmid;
535 my $volid = print_volid
($storeid, $btype, $bid, $epoch);
539 format
=> "pbs-$btype",
546 $info->{verification
} = $item->{verification
} if defined($item->{verification
});
547 $info->{comment
} = $item->{comment
} if defined($item->{comment
});
556 my ($class, $storeid, $scfg, $cache) = @_;
564 my $res = run_client_cmd
($scfg, $storeid, "status");
567 $total = $res->{total
};
568 $used = $res->{used
};
569 $free = $res->{avail
};
575 return ($total, $free, $used, $active);
578 sub activate_storage
{
579 my ($class, $storeid, $scfg, $cache) = @_;
581 # a 'status' client command is to expensive here
582 # TODO: use a dummy ping API call to ensure the PBS API daemon is available for real
583 my $server = $scfg->{server
};
584 my $port = $scfg->{port
} // 8007;
585 PVE
::Network
::tcp_ping
($server, $port, 2);
590 sub deactivate_storage
{
591 my ($class, $storeid, $scfg, $cache) = @_;
595 sub activate_volume
{
596 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
598 die "volume snapshot is not possible on pbs device" if $snapname;
603 sub deactivate_volume
{
604 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
606 die "volume snapshot is not possible on pbs device" if $snapname;
611 sub volume_size_info
{
612 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
614 my ($vtype, $name, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
616 my $data = run_client_cmd
($scfg, $storeid, "files", [ $name ]);
619 foreach my $info (@$data) {
620 $size += $info->{size
} if $info->{size
};
625 return wantarray ?
($size, $format, $used, undef) : $size;
629 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
630 die "volume resize is not possible on pbs device";
633 sub volume_snapshot
{
634 my ($class, $scfg, $storeid, $volname, $snap) = @_;
635 die "volume snapshot is not possible on pbs device";
638 sub volume_snapshot_rollback
{
639 my ($class, $scfg, $storeid, $volname, $snap) = @_;
640 die "volume snapshot rollback is not possible on pbs device";
643 sub volume_snapshot_delete
{
644 my ($class, $scfg, $storeid, $volname, $snap) = @_;
645 die "volume snapshot delete is not possible on pbs device";
648 sub volume_has_feature
{
649 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;