1 package PVE
::Storage
::PBSPlugin
;
3 # Plugin to access Proxmox Backup Server
7 use Fcntl
qw(F_GETFD F_SETFD FD_CLOEXEC);
12 use POSIX
qw(strftime ENOENT);
14 use PVE
::Tools
qw(run_command file_read_firstline trim dir_glob_regex dir_glob_foreach);
15 use PVE
::Storage
::Plugin
;
16 use PVE
::JSONSchema
qw(get_standard_option);
18 use base
qw(PVE::Storage::Plugin);
28 content
=> [ {backup
=> 1, none
=> 1}, { backup
=> 1 }],
35 description
=> "Proxmox backup server datastore name.",
38 # openssl s_client -connect <host>:8007 2>&1 |openssl x509 -fingerprint -sha256
39 fingerprint
=> get_standard_option
('fingerprint-sha256'),
41 description
=> "Encryption key. Use 'autogen' to generate one automatically without passphrase.",
49 server
=> { fixed
=> 1 },
50 datastore
=> { fixed
=> 1 },
51 nodes
=> { optional
=> 1},
52 disable
=> { optional
=> 1},
53 content
=> { optional
=> 1},
54 username
=> { optional
=> 1 },
55 password
=> { optional
=> 1 },
56 'encryption-key' => { optional
=> 1 },
57 maxfiles
=> { optional
=> 1 },
58 'prune-backups' => { optional
=> 1 },
59 fingerprint
=> { optional
=> 1 },
65 sub pbs_password_file_name
{
66 my ($scfg, $storeid) = @_;
68 return "/etc/pve/priv/storage/${storeid}.pw";
71 sub pbs_set_password
{
72 my ($scfg, $storeid, $password) = @_;
74 my $pwfile = pbs_password_file_name
($scfg, $storeid);
75 mkdir "/etc/pve/priv/storage";
77 PVE
::Tools
::file_set_contents
($pwfile, "$password\n");
80 sub pbs_delete_password
{
81 my ($scfg, $storeid) = @_;
83 my $pwfile = pbs_password_file_name
($scfg, $storeid);
88 sub pbs_get_password
{
89 my ($scfg, $storeid) = @_;
91 my $pwfile = pbs_password_file_name
($scfg, $storeid);
93 return PVE
::Tools
::file_read_firstline
($pwfile);
96 sub pbs_encryption_key_file_name
{
97 my ($scfg, $storeid) = @_;
99 return "/etc/pve/priv/storage/${storeid}.enc";
102 sub pbs_set_encryption_key
{
103 my ($scfg, $storeid, $key) = @_;
105 my $pwfile = pbs_encryption_key_file_name
($scfg, $storeid);
106 mkdir "/etc/pve/priv/storage";
108 PVE
::Tools
::file_set_contents
($pwfile, "$key\n");
111 sub pbs_delete_encryption_key
{
112 my ($scfg, $storeid) = @_;
114 my $pwfile = pbs_encryption_key_file_name
($scfg, $storeid);
116 if (!unlink $pwfile) {
117 return if $! == ENOENT
;
118 die "failed to delete encryption key! $!\n";
120 delete $scfg->{'encryption-key'};
123 sub pbs_get_encryption_key
{
124 my ($scfg, $storeid) = @_;
126 my $pwfile = pbs_encryption_key_file_name
($scfg, $storeid);
128 return PVE
::Tools
::file_get_contents
($pwfile);
131 # Returns a file handle if there is an encryption key, or `undef` if there is not. Dies on error.
132 sub pbs_open_encryption_key
{
133 my ($scfg, $storeid) = @_;
135 my $encryption_key_file = pbs_encryption_key_file_name
($scfg, $storeid);
138 if (!open($keyfd, '<', $encryption_key_file)) {
139 return undef if $! == ENOENT
;
140 die "failed to open encryption key: $encryption_key_file: $!\n";
147 my ($storeid, $btype, $bid, $btime) = @_;
149 my $time_str = strftime
("%FT%TZ", gmtime($btime));
150 my $volname = "backup/${btype}/${bid}/${time_str}";
152 return "${storeid}:${volname}";
155 my $USE_CRYPT_PARAMS = {
161 my sub do_raw_client_cmd
{
162 my ($scfg, $storeid, $client_cmd, $param, %opts) = @_;
164 my $use_crypto = $USE_CRYPT_PARAMS->{$client_cmd};
166 my $client_exe = '/usr/bin/proxmox-backup-client';
167 die "executable not found '$client_exe'! Proxmox backup client not installed?\n"
170 my $server = $scfg->{server
};
171 my $datastore = $scfg->{datastore
};
172 my $username = $scfg->{username
} // 'root@pam';
174 my $userns_cmd = delete $opts{userns_cmd
};
178 push @$cmd, @$userns_cmd if defined($userns_cmd);
180 push @$cmd, $client_exe, $client_cmd;
182 # This must live in the top scope to not get closed before the `run_command`
185 if (defined($keyfd = pbs_open_encryption_key
($scfg, $storeid))) {
186 my $flags = fcntl($keyfd, F_GETFD
, 0)
187 // die "failed to get file descriptor flags: $!\n";
188 fcntl($keyfd, F_SETFD
, $flags & ~FD_CLOEXEC
)
189 or die "failed to remove FD_CLOEXEC from encryption key file descriptor\n";
190 push @$cmd, '--crypt-mode=encrypt', '--keyfd='.fileno($keyfd);
192 push @$cmd, '--crypt-mode=none';
196 push @$cmd, @$param if defined($param);
198 push @$cmd, "--repository", "$username\@$server:$datastore";
200 local $ENV{PBS_PASSWORD
} = pbs_get_password
($scfg, $storeid);
202 local $ENV{PBS_FINGERPRINT
} = $scfg->{fingerprint
};
204 # no ascii-art on task logs
205 local $ENV{PROXMOX_OUTPUT_NO_BORDER
} = 1;
206 local $ENV{PROXMOX_OUTPUT_NO_HEADER
} = 1;
208 if (my $logfunc = $opts{logfunc
}) {
209 $logfunc->("run: " . join(' ', @$cmd));
212 run_command
($cmd, %opts);
215 # FIXME: External perl code should NOT have access to this.
217 # There should be separate functions to
222 sub run_raw_client_cmd
{
223 my ($scfg, $storeid, $client_cmd, $param, %opts) = @_;
224 return do_raw_client_cmd
($scfg, $storeid, $client_cmd, $param, %opts);
228 my ($scfg, $storeid, $client_cmd, $param, $no_output) = @_;
231 my $outfunc = sub { $json_str .= "$_[0]\n" };
233 $param = [] if !defined($param);
234 $param = [ $param ] if !ref($param);
236 $param = [@$param, '--output-format=json'] if !$no_output;
238 do_raw_client_cmd
($scfg, $storeid, $client_cmd, $param,
239 outfunc
=> $outfunc, errmsg
=> 'proxmox-backup-client failed');
241 return undef if $no_output;
243 my $res = decode_json
($json_str);
248 # Storage implementation
250 sub extract_vzdump_config
{
251 my ($class, $scfg, $volname, $storeid) = @_;
253 my ($vtype, $name, $vmid, undef, undef, undef, $format) = $class->parse_volname($volname);
256 my $outfunc = sub { $config .= "$_[0]\n" };
259 if ($format eq 'pbs-vm') {
260 $config_name = 'qemu-server.conf';
261 } elsif ($format eq 'pbs-ct') {
262 $config_name = 'pct.conf';
264 die "unable to extract configuration for backup format '$format'\n";
267 do_raw_client_cmd
($scfg, $storeid, 'restore', [ $name, $config_name, '-' ],
268 outfunc
=> $outfunc, errmsg
=> 'proxmox-backup-client failed');
274 my ($class, $scfg, $storeid, $keep, $vmid, $type, $dryrun, $logfunc) = @_;
276 $logfunc //= sub { print "$_[1]\n" };
278 my $backups = $class->list_volumes($storeid, $scfg, $vmid, ['backup']);
280 $type = 'vm' if defined($type) && $type eq 'qemu';
281 $type = 'ct' if defined($type) && $type eq 'lxc';
283 my $backup_groups = {};
284 foreach my $backup (@{$backups}) {
285 (my $backup_type = $backup->{format
}) =~ s/^pbs-//;
287 next if defined($type) && $backup_type ne $type;
289 my $backup_group = "$backup_type/$backup->{vmid}";
290 $backup_groups->{$backup_group} = 1;
294 foreach my $opt (keys %{$keep}) {
295 push @param, "--$opt";
296 push @param, "$keep->{$opt}";
299 push @param, '--dry-run' if $dryrun;
304 foreach my $backup_group (keys %{$backup_groups}) {
305 $logfunc->('info', "running 'proxmox-backup-client prune' for '$backup_group'")
308 my $res = run_client_cmd
($scfg, $storeid, 'prune', [ $backup_group, @param ]);
310 foreach my $backup (@{$res}) {
311 die "result from proxmox-backup-client is not as expected\n"
312 if !defined($backup->{'backup-time'})
313 || !defined($backup->{'backup-type'})
314 || !defined($backup->{'backup-id'})
315 || !defined($backup->{'keep'});
317 my $ctime = $backup->{'backup-time'};
318 my $type = $backup->{'backup-type'};
319 my $vmid = $backup->{'backup-id'};
320 my $volid = print_volid
($storeid, $type, $vmid, $ctime);
322 push @{$prune_list}, {
324 mark
=> $backup->{keep
} ?
'keep' : 'remove',
325 type
=> $type eq 'vm' ?
'qemu' : 'lxc',
332 $logfunc->('err', "prune '$backup_group': $err\n");
336 die "error pruning backups - check log\n" if $failed;
341 my $autogen_encryption_key = sub {
342 my ($scfg, $storeid) = @_;
343 my $encfile = pbs_encryption_key_file_name
($scfg, $storeid);
344 run_command
(['proxmox-backup-client', 'key', 'create', '--kdf', 'none', $encfile]);
348 my ($class, $storeid, $scfg, %param) = @_;
350 if (defined(my $password = $param{password
})) {
351 pbs_set_password
($scfg, $storeid, $password);
353 pbs_delete_password
($scfg, $storeid);
356 if (defined(my $encryption_key = $param{'encryption-key'})) {
357 if ($encryption_key eq 'autogen') {
358 $autogen_encryption_key->($scfg, $storeid);
360 pbs_set_encryption_key
($scfg, $storeid, $encryption_key);
362 $scfg->{'encryption-key'} = 1;
364 pbs_delete_encryption_key
($scfg, $storeid);
369 my ($class, $storeid, $scfg, %param) = @_;
371 if (exists($param{password
})) {
372 if (defined($param{password
})) {
373 pbs_set_password
($scfg, $storeid, $param{password
});
375 pbs_delete_password
($scfg, $storeid);
379 if (exists($param{'encryption-key'})) {
380 if (defined(my $encryption_key = delete($param{'encryption-key'}))) {
381 if ($encryption_key eq 'autogen') {
382 $autogen_encryption_key->($scfg, $storeid);
384 pbs_set_encryption_key
($scfg, $storeid, $encryption_key);
386 $scfg->{'encryption-key'} = 1;
388 pbs_delete_encryption_key
($scfg, $storeid);
394 my ($class, $storeid, $scfg) = @_;
396 pbs_delete_password
($scfg, $storeid);
397 pbs_delete_encryption_key
($scfg, $storeid);
401 my ($class, $volname) = @_;
403 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)$!) {
407 my $format = "pbs-$btype";
409 my $name = "$btype/$bid/$btime";
411 if ($bid =~ m/^\d+$/) {
412 return ('backup', $name, $bid, undef, undef, undef, $format);
414 return ('backup', $name, undef, undef, undef, undef, $format);
418 die "unable to parse PBS volume name '$volname'\n";
422 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
424 die "volume snapshot is not possible on pbs storage"
425 if defined($snapname);
427 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
429 my $server = $scfg->{server
};
430 my $datastore = $scfg->{datastore
};
431 my $username = $scfg->{username
} // 'root@pam';
433 # artifical url - we currently do not use that anywhere
434 my $path = "pbs://$username\@$server:$datastore/$name";
436 return ($path, $vmid, $vtype);
440 my ($class, $storeid, $scfg, $volname) = @_;
442 die "can't create base images in pbs storage\n";
446 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
448 die "can't clone images in pbs storage\n";
452 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
454 die "can't allocate space in pbs storage\n";
458 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
460 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
462 run_client_cmd
($scfg, $storeid, "forget", [ $name ], 1);
467 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
475 my ($class, $storeid, $scfg, $vmid, $content_types) = @_;
479 return $res if !grep { $_ eq 'backup' } @$content_types;
481 my $data = run_client_cmd
($scfg, $storeid, "snapshots");
483 foreach my $item (@$data) {
484 my $btype = $item->{"backup-type"};
485 my $bid = $item->{"backup-id"};
486 my $epoch = $item->{"backup-time"};
487 my $size = $item->{size
} // 1;
489 next if !($btype eq 'vm' || $btype eq 'ct');
490 next if $bid !~ m/^\d+$/;
491 next if defined($vmid) && $bid ne $vmid;
493 my $volid = print_volid
($storeid, $btype, $bid, $epoch);
497 format
=> "pbs-$btype",
511 my ($class, $storeid, $scfg, $cache) = @_;
519 my $res = run_client_cmd
($scfg, $storeid, "status");
522 $total = $res->{total
};
523 $used = $res->{used
};
524 $free = $res->{avail
};
530 return ($total, $free, $used, $active);
533 sub activate_storage
{
534 my ($class, $storeid, $scfg, $cache) = @_;
536 run_client_cmd
($scfg, $storeid, "status");
541 sub deactivate_storage
{
542 my ($class, $storeid, $scfg, $cache) = @_;
546 sub activate_volume
{
547 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
549 die "volume snapshot is not possible on pbs device" if $snapname;
554 sub deactivate_volume
{
555 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
557 die "volume snapshot is not possible on pbs device" if $snapname;
562 sub volume_size_info
{
563 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
565 my ($vtype, $name, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
567 my $data = run_client_cmd
($scfg, $storeid, "files", [ $name ]);
570 foreach my $info (@$data) {
571 $size += $info->{size
} if $info->{size
};
576 return wantarray ?
($size, $format, $used, undef) : $size;
580 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
581 die "volume resize is not possible on pbs device";
584 sub volume_snapshot
{
585 my ($class, $scfg, $storeid, $volname, $snap) = @_;
586 die "volume snapshot is not possible on pbs device";
589 sub volume_snapshot_rollback
{
590 my ($class, $scfg, $storeid, $volname, $snap) = @_;
591 die "volume snapshot rollback is not possible on pbs device";
594 sub volume_snapshot_delete
{
595 my ($class, $scfg, $storeid, $volname, $snap) = @_;
596 die "volume snapshot delete is not possible on pbs device";
599 sub volume_has_feature
{
600 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;