]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/PBSPlugin.pm
api: content: pass encrypted status for PBS backups
[pve-storage.git] / PVE / Storage / PBSPlugin.pm
1 package PVE::Storage::PBSPlugin;
2
3 # Plugin to access Proxmox Backup Server
4
5 use strict;
6 use warnings;
7
8 use Fcntl qw(F_GETFD F_SETFD FD_CLOEXEC);
9 use HTTP::Request;
10 use IO::File;
11 use JSON;
12 use LWP::UserAgent;
13 use POSIX qw(strftime ENOENT);
14
15 use PVE::JSONSchema qw(get_standard_option);
16 use PVE::Network;
17 use PVE::Storage::Plugin;
18 use PVE::Tools qw(run_command file_read_firstline trim dir_glob_regex dir_glob_foreach $IPV6RE);
19
20 use base qw(PVE::Storage::Plugin);
21
22 # Configuration
23
24 sub type {
25 return 'pbs';
26 }
27
28 sub plugindata {
29 return {
30 content => [ {backup => 1, none => 1}, { backup => 1 }],
31 };
32 }
33
34 sub properties {
35 return {
36 datastore => {
37 description => "Proxmox Backup Server datastore name.",
38 type => 'string',
39 },
40 # openssl s_client -connect <host>:8007 2>&1 |openssl x509 -fingerprint -sha256
41 fingerprint => get_standard_option('fingerprint-sha256'),
42 'encryption-key' => {
43 description => "Encryption key. Use 'autogen' to generate one automatically without passphrase.",
44 type => 'string',
45 },
46 port => {
47 description => "For non default port.",
48 type => 'integer',
49 minimum => 1,
50 maximum => 65535,
51 default => 8007,
52 }
53 };
54 }
55
56 sub options {
57 return {
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 },
70 };
71 }
72
73 # Helpers
74
75 sub pbs_password_file_name {
76 my ($scfg, $storeid) = @_;
77
78 return "/etc/pve/priv/storage/${storeid}.pw";
79 }
80
81 sub pbs_set_password {
82 my ($scfg, $storeid, $password) = @_;
83
84 my $pwfile = pbs_password_file_name($scfg, $storeid);
85 mkdir "/etc/pve/priv/storage";
86
87 PVE::Tools::file_set_contents($pwfile, "$password\n");
88 }
89
90 sub pbs_delete_password {
91 my ($scfg, $storeid) = @_;
92
93 my $pwfile = pbs_password_file_name($scfg, $storeid);
94
95 unlink $pwfile;
96 }
97
98 sub pbs_get_password {
99 my ($scfg, $storeid) = @_;
100
101 my $pwfile = pbs_password_file_name($scfg, $storeid);
102
103 return PVE::Tools::file_read_firstline($pwfile);
104 }
105
106 sub pbs_encryption_key_file_name {
107 my ($scfg, $storeid) = @_;
108
109 return "/etc/pve/priv/storage/${storeid}.enc";
110 }
111
112 sub pbs_set_encryption_key {
113 my ($scfg, $storeid, $key) = @_;
114
115 my $pwfile = pbs_encryption_key_file_name($scfg, $storeid);
116 mkdir "/etc/pve/priv/storage";
117
118 PVE::Tools::file_set_contents($pwfile, "$key\n");
119 }
120
121 sub pbs_delete_encryption_key {
122 my ($scfg, $storeid) = @_;
123
124 my $pwfile = pbs_encryption_key_file_name($scfg, $storeid);
125
126 if (!unlink $pwfile) {
127 return if $! == ENOENT;
128 die "failed to delete encryption key! $!\n";
129 }
130 delete $scfg->{'encryption-key'};
131 }
132
133 sub pbs_get_encryption_key {
134 my ($scfg, $storeid) = @_;
135
136 my $pwfile = pbs_encryption_key_file_name($scfg, $storeid);
137
138 return PVE::Tools::file_get_contents($pwfile);
139 }
140
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) = @_;
144
145 my $encryption_key_file = pbs_encryption_key_file_name($scfg, $storeid);
146
147 my $keyfd;
148 if (!open($keyfd, '<', $encryption_key_file)) {
149 return undef if $! == ENOENT;
150 die "failed to open encryption key: $encryption_key_file: $!\n";
151 }
152
153 return $keyfd;
154 }
155
156 sub print_volid {
157 my ($storeid, $btype, $bid, $btime) = @_;
158
159 my $time_str = strftime("%FT%TZ", gmtime($btime));
160 my $volname = "backup/${btype}/${bid}/${time_str}";
161
162 return "${storeid}:${volname}";
163 }
164
165 my sub get_server_with_port {
166 my ($scfg) = @_;
167
168 my $server = $scfg->{server};
169 $server = "[$server]" if $server =~ /^$IPV6RE$/;
170
171 if (my $port = $scfg->{port}) {
172 $server .= ":$port" if $port != 8007;
173 }
174 return $server;
175 }
176
177 my $USE_CRYPT_PARAMS = {
178 backup => 1,
179 restore => 1,
180 'upload-log' => 1,
181 };
182
183 my sub do_raw_client_cmd {
184 my ($scfg, $storeid, $client_cmd, $param, %opts) = @_;
185
186 my $use_crypto = $USE_CRYPT_PARAMS->{$client_cmd};
187
188 my $client_exe = '/usr/bin/proxmox-backup-client';
189 die "executable not found '$client_exe'! Proxmox backup client not installed?\n"
190 if ! -x $client_exe;
191
192 my $server = get_server_with_port($scfg);
193 my $datastore = $scfg->{datastore};
194 my $username = $scfg->{username} // 'root@pam';
195
196 my $userns_cmd = delete $opts{userns_cmd};
197
198 my $cmd = [];
199
200 push @$cmd, @$userns_cmd if defined($userns_cmd);
201
202 push @$cmd, $client_exe, $client_cmd;
203
204 # This must live in the top scope to not get closed before the `run_command`
205 my $keyfd;
206 if ($use_crypto) {
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);
213 } else {
214 push @$cmd, '--crypt-mode=none';
215 }
216 }
217
218 push @$cmd, @$param if defined($param);
219
220 push @$cmd, "--repository", "$username\@$server:$datastore";
221
222 local $ENV{PBS_PASSWORD} = pbs_get_password($scfg, $storeid);
223
224 local $ENV{PBS_FINGERPRINT} = $scfg->{fingerprint};
225
226 # no ascii-art on task logs
227 local $ENV{PROXMOX_OUTPUT_NO_BORDER} = 1;
228 local $ENV{PROXMOX_OUTPUT_NO_HEADER} = 1;
229
230 if (my $logfunc = $opts{logfunc}) {
231 $logfunc->("run: " . join(' ', @$cmd));
232 }
233
234 run_command($cmd, %opts);
235 }
236
237 # FIXME: External perl code should NOT have access to this.
238 #
239 # There should be separate functions to
240 # - make backups
241 # - restore backups
242 # - restore files
243 # with a sane API
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);
247 }
248
249 sub run_client_cmd {
250 my ($scfg, $storeid, $client_cmd, $param, $no_output) = @_;
251
252 my $json_str = '';
253 my $outfunc = sub { $json_str .= "$_[0]\n" };
254
255 $param = [] if !defined($param);
256 $param = [ $param ] if !ref($param);
257
258 $param = [@$param, '--output-format=json'] if !$no_output;
259
260 do_raw_client_cmd($scfg, $storeid, $client_cmd, $param,
261 outfunc => $outfunc, errmsg => 'proxmox-backup-client failed');
262
263 return undef if $no_output;
264
265 my $res = decode_json($json_str);
266
267 return $res;
268 }
269
270 # Storage implementation
271
272 sub extract_vzdump_config {
273 my ($class, $scfg, $volname, $storeid) = @_;
274
275 my ($vtype, $name, $vmid, undef, undef, undef, $format) = $class->parse_volname($volname);
276
277 my $config = '';
278 my $outfunc = sub { $config .= "$_[0]\n" };
279
280 my $config_name;
281 if ($format eq 'pbs-vm') {
282 $config_name = 'qemu-server.conf';
283 } elsif ($format eq 'pbs-ct') {
284 $config_name = 'pct.conf';
285 } else {
286 die "unable to extract configuration for backup format '$format'\n";
287 }
288
289 do_raw_client_cmd($scfg, $storeid, 'restore', [ $name, $config_name, '-' ],
290 outfunc => $outfunc, errmsg => 'proxmox-backup-client failed');
291
292 return $config;
293 }
294
295 sub prune_backups {
296 my ($class, $scfg, $storeid, $keep, $vmid, $type, $dryrun, $logfunc) = @_;
297
298 $logfunc //= sub { print "$_[1]\n" };
299
300 my $backups = $class->list_volumes($storeid, $scfg, $vmid, ['backup']);
301
302 $type = 'vm' if defined($type) && $type eq 'qemu';
303 $type = 'ct' if defined($type) && $type eq 'lxc';
304
305 my $backup_groups = {};
306 foreach my $backup (@{$backups}) {
307 (my $backup_type = $backup->{format}) =~ s/^pbs-//;
308
309 next if defined($type) && $backup_type ne $type;
310
311 my $backup_group = "$backup_type/$backup->{vmid}";
312 $backup_groups->{$backup_group} = 1;
313 }
314
315 my @param;
316
317 my $keep_all = delete $keep->{'keep-all'};
318
319 if (!$keep_all) {
320 foreach my $opt (keys %{$keep}) {
321 next if $keep->{$opt} == 0;
322 push @param, "--$opt";
323 push @param, "$keep->{$opt}";
324 }
325 } else { # no need to pass anything to PBS
326 $keep = { 'keep-all' => 1 };
327 }
328
329 push @param, '--dry-run' if $dryrun;
330
331 my $prune_list = [];
332 my $failed;
333
334 foreach my $backup_group (keys %{$backup_groups}) {
335 $logfunc->('info', "running 'proxmox-backup-client prune' for '$backup_group'")
336 if !$dryrun;
337 eval {
338 my $res = run_client_cmd($scfg, $storeid, 'prune', [ $backup_group, @param ]);
339
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'});
346
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);
351
352 push @{$prune_list}, {
353 ctime => $ctime,
354 mark => $backup->{keep} ? 'keep' : 'remove',
355 type => $type eq 'vm' ? 'qemu' : 'lxc',
356 vmid => $vmid,
357 volid => $volid,
358 };
359 }
360 };
361 if (my $err = $@) {
362 $logfunc->('err', "prune '$backup_group': $err\n");
363 $failed = 1;
364 }
365 }
366 die "error pruning backups - check log\n" if $failed;
367
368 return $prune_list;
369 }
370
371 my $autogen_encryption_key = sub {
372 my ($scfg, $storeid) = @_;
373 my $encfile = pbs_encryption_key_file_name($scfg, $storeid);
374 if (-f $encfile) {
375 rename $encfile, "$encfile.old";
376 }
377 my $cmd = ['proxmox-backup-client', 'key', 'create', '--kdf', 'none', $encfile];
378 run_command($cmd, errmsg => 'failed to create encryption key');
379 return PVE::Tools::file_get_contents($encfile);
380 };
381
382 sub on_add_hook {
383 my ($class, $storeid, $scfg, %param) = @_;
384
385 my $res = {};
386
387 if (defined(my $password = $param{password})) {
388 pbs_set_password($scfg, $storeid, $password);
389 } else {
390 pbs_delete_password($scfg, $storeid);
391 }
392
393 if (defined(my $encryption_key = $param{'encryption-key'})) {
394 my $decoded_key;
395 if ($encryption_key eq 'autogen') {
396 $res->{'encryption-key'} = $autogen_encryption_key->($scfg, $storeid);
397 $decoded_key = decode_json($res->{'encryption-key'});
398 } else {
399 $decoded_key = eval { decode_json($encryption_key) };
400 if ($@ || !exists($decoded_key->{data})) {
401 die "Value does not seems like a valid, JSON formatted encryption key!\n";
402 }
403 pbs_set_encryption_key($scfg, $storeid, $encryption_key);
404 $res->{'encryption-key'} = $encryption_key;
405 }
406 $scfg->{'encryption-key'} = $decoded_key->{fingerprint} || 1;
407 } else {
408 pbs_delete_encryption_key($scfg, $storeid);
409 }
410
411 return $res;
412 }
413
414 sub on_update_hook {
415 my ($class, $storeid, $scfg, %param) = @_;
416
417 my $res = {};
418
419 if (exists($param{password})) {
420 if (defined($param{password})) {
421 pbs_set_password($scfg, $storeid, $param{password});
422 } else {
423 pbs_delete_password($scfg, $storeid);
424 }
425 }
426
427 if (exists($param{'encryption-key'})) {
428 if (defined(my $encryption_key = delete($param{'encryption-key'}))) {
429 my $decoded_key;
430 if ($encryption_key eq 'autogen') {
431 $res->{'encryption-key'} = $autogen_encryption_key->($scfg, $storeid);
432 $decoded_key = decode_json($res->{'encryption-key'});
433 } else {
434 $decoded_key = eval { decode_json($encryption_key) };
435 if ($@ || !exists($decoded_key->{data})) {
436 die "Value does not seems like a valid, JSON formatted encryption key!\n";
437 }
438 pbs_set_encryption_key($scfg, $storeid, $encryption_key);
439 $res->{'encryption-key'} = $encryption_key;
440 }
441 $scfg->{'encryption-key'} = $decoded_key->{fingerprint} || 1;
442 } else {
443 pbs_delete_encryption_key($scfg, $storeid);
444 }
445 }
446
447 return $res;
448 }
449
450 sub on_delete_hook {
451 my ($class, $storeid, $scfg) = @_;
452
453 pbs_delete_password($scfg, $storeid);
454 pbs_delete_encryption_key($scfg, $storeid);
455 }
456
457 sub parse_volname {
458 my ($class, $volname) = @_;
459
460 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)$!) {
461 my $btype = $1;
462 my $bid = $2;
463 my $btime = $3;
464 my $format = "pbs-$btype";
465
466 my $name = "$btype/$bid/$btime";
467
468 if ($bid =~ m/^\d+$/) {
469 return ('backup', $name, $bid, undef, undef, undef, $format);
470 } else {
471 return ('backup', $name, undef, undef, undef, undef, $format);
472 }
473 }
474
475 die "unable to parse PBS volume name '$volname'\n";
476 }
477
478 sub path {
479 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
480
481 die "volume snapshot is not possible on pbs storage"
482 if defined($snapname);
483
484 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
485
486 my $server = get_server_with_port($scfg);
487 my $datastore = $scfg->{datastore};
488 my $username = $scfg->{username} // 'root@pam';
489
490 # artifical url - we currently do not use that anywhere
491 my $path = "pbs://$username\@$server:$datastore/$name";
492
493 return ($path, $vmid, $vtype);
494 }
495
496 sub create_base {
497 my ($class, $storeid, $scfg, $volname) = @_;
498
499 die "can't create base images in pbs storage\n";
500 }
501
502 sub clone_image {
503 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
504
505 die "can't clone images in pbs storage\n";
506 }
507
508 sub alloc_image {
509 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
510
511 die "can't allocate space in pbs storage\n";
512 }
513
514 sub free_image {
515 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
516
517 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
518
519 run_client_cmd($scfg, $storeid, "forget", [ $name ], 1);
520 }
521
522
523 sub list_images {
524 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
525
526 my $res = [];
527
528 return $res;
529 }
530
531 my sub snapshot_files_encrypted {
532 my ($files) = @_;
533 return 0 if !$files;
534
535 my $any;
536 my $all = 1;
537 for my $file (@$files) {
538 my $fn = $file->{filename};
539 next if $fn eq 'client.log.blob' || $fn eq 'index.json.blob';
540
541 my $crypt = $file->{'crypt-mode'};
542
543 $all = 0 if !$crypt || $crypt ne 'encrypt';
544 $any ||= $crypt eq 'encrypt';
545 }
546 return $any && $all;
547 }
548
549 sub list_volumes {
550 my ($class, $storeid, $scfg, $vmid, $content_types) = @_;
551
552 my $res = [];
553
554 return $res if !grep { $_ eq 'backup' } @$content_types;
555
556 my $data = run_client_cmd($scfg, $storeid, "snapshots");
557
558 foreach my $item (@$data) {
559 my $btype = $item->{"backup-type"};
560 my $bid = $item->{"backup-id"};
561 my $epoch = $item->{"backup-time"};
562 my $size = $item->{size} // 1;
563
564 next if !($btype eq 'vm' || $btype eq 'ct');
565 next if $bid !~ m/^\d+$/;
566 next if defined($vmid) && $bid ne $vmid;
567
568 my $volid = print_volid($storeid, $btype, $bid, $epoch);
569
570 my $info = {
571 volid => $volid,
572 format => "pbs-$btype",
573 size => $size,
574 content => 'backup',
575 vmid => int($bid),
576 ctime => $epoch,
577 };
578
579 $info->{verification} = $item->{verification} if defined($item->{verification});
580 $info->{notes} = $item->{comment} if defined($item->{comment});
581 if (defined($item->{fingerprint})) {
582 $info->{encrypted} = $item->{fingerprint};
583 } elsif (snapshot_files_encrypted($item->{files})) {
584 $info->{encrypted} = '1';
585 }
586
587 push @$res, $info;
588 }
589
590 return $res;
591 }
592
593 sub status {
594 my ($class, $storeid, $scfg, $cache) = @_;
595
596 my $total = 0;
597 my $free = 0;
598 my $used = 0;
599 my $active = 0;
600
601 eval {
602 my $res = run_client_cmd($scfg, $storeid, "status");
603
604 $active = 1;
605 $total = $res->{total};
606 $used = $res->{used};
607 $free = $res->{avail};
608 };
609 if (my $err = $@) {
610 warn $err;
611 }
612
613 return ($total, $free, $used, $active);
614 }
615
616 sub activate_storage {
617 my ($class, $storeid, $scfg, $cache) = @_;
618
619 # a 'status' client command is to expensive here
620 # TODO: use a dummy ping API call to ensure the PBS API daemon is available for real
621 my $server = $scfg->{server};
622 my $port = $scfg->{port} // 8007;
623 PVE::Network::tcp_ping($server, $port, 2);
624
625 return 1;
626 }
627
628 sub deactivate_storage {
629 my ($class, $storeid, $scfg, $cache) = @_;
630 return 1;
631 }
632
633 sub activate_volume {
634 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
635
636 die "volume snapshot is not possible on pbs device" if $snapname;
637
638 return 1;
639 }
640
641 sub deactivate_volume {
642 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
643
644 die "volume snapshot is not possible on pbs device" if $snapname;
645
646 return 1;
647 }
648
649 sub get_volume_notes {
650 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
651
652 my (undef, $name, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
653
654 my $data = run_client_cmd($scfg, $storeid, "snapshot", [ "notes", "show", $name ]);
655
656 return $data->{notes};
657 }
658
659 sub update_volume_notes {
660 my ($class, $scfg, $storeid, $volname, $notes, $timeout) = @_;
661
662 my (undef, $name, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
663
664 run_client_cmd($scfg, $storeid, "snapshot", [ "notes", "update", $name, $notes ], 1);
665
666 return undef;
667 }
668
669 sub volume_size_info {
670 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
671
672 my ($vtype, $name, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
673
674 my $data = run_client_cmd($scfg, $storeid, "files", [ $name ]);
675
676 my $size = 0;
677 foreach my $info (@$data) {
678 $size += $info->{size} if $info->{size};
679 }
680
681 my $used = $size;
682
683 return wantarray ? ($size, $format, $used, undef) : $size;
684 }
685
686 sub volume_resize {
687 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
688 die "volume resize is not possible on pbs device";
689 }
690
691 sub volume_snapshot {
692 my ($class, $scfg, $storeid, $volname, $snap) = @_;
693 die "volume snapshot is not possible on pbs device";
694 }
695
696 sub volume_snapshot_rollback {
697 my ($class, $scfg, $storeid, $volname, $snap) = @_;
698 die "volume snapshot rollback is not possible on pbs device";
699 }
700
701 sub volume_snapshot_delete {
702 my ($class, $scfg, $storeid, $volname, $snap) = @_;
703 die "volume snapshot delete is not possible on pbs device";
704 }
705
706 sub volume_has_feature {
707 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
708
709 return undef;
710 }
711
712 1;