]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/PBSPlugin.pm
pbs: check if encryption key could be deleted
[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 use Fcntl qw(F_GETFD F_SETFD FD_CLOEXEC);
8 use HTTP::Request;
9 use IO::File;
10 use JSON;
11 use LWP::UserAgent;
12 use POSIX qw(strftime ENOENT);
13
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);
17
18 use base qw(PVE::Storage::Plugin);
19
20 # Configuration
21
22 sub type {
23 return 'pbs';
24 }
25
26 sub plugindata {
27 return {
28 content => [ {backup => 1, none => 1}, { backup => 1 }],
29 };
30 }
31
32 sub properties {
33 return {
34 datastore => {
35 description => "Proxmox backup server datastore name.",
36 type => 'string',
37 },
38 # openssl s_client -connect <host>:8007 2>&1 |openssl x509 -fingerprint -sha256
39 fingerprint => get_standard_option('fingerprint-sha256'),
40 'encryption-key' => {
41 description => "Encryption key. Use 'autogen' to generate one automatically without passphrase.",
42 type => 'string',
43 },
44 };
45 }
46
47 sub options {
48 return {
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 },
60 };
61 }
62
63 # Helpers
64
65 sub pbs_password_file_name {
66 my ($scfg, $storeid) = @_;
67
68 return "/etc/pve/priv/storage/${storeid}.pw";
69 }
70
71 sub pbs_set_password {
72 my ($scfg, $storeid, $password) = @_;
73
74 my $pwfile = pbs_password_file_name($scfg, $storeid);
75 mkdir "/etc/pve/priv/storage";
76
77 PVE::Tools::file_set_contents($pwfile, "$password\n");
78 }
79
80 sub pbs_delete_password {
81 my ($scfg, $storeid) = @_;
82
83 my $pwfile = pbs_password_file_name($scfg, $storeid);
84
85 unlink $pwfile;
86 }
87
88 sub pbs_get_password {
89 my ($scfg, $storeid) = @_;
90
91 my $pwfile = pbs_password_file_name($scfg, $storeid);
92
93 return PVE::Tools::file_read_firstline($pwfile);
94 }
95
96 sub pbs_encryption_key_file_name {
97 my ($scfg, $storeid) = @_;
98
99 return "/etc/pve/priv/storage/${storeid}.enc";
100 }
101
102 sub pbs_set_encryption_key {
103 my ($scfg, $storeid, $key) = @_;
104
105 my $pwfile = pbs_encryption_key_file_name($scfg, $storeid);
106 mkdir "/etc/pve/priv/storage";
107
108 PVE::Tools::file_set_contents($pwfile, "$key\n");
109 }
110
111 sub pbs_delete_encryption_key {
112 my ($scfg, $storeid) = @_;
113
114 my $pwfile = pbs_encryption_key_file_name($scfg, $storeid);
115
116 if (!unlink $pwfile) {
117 return if $! == ENOENT;
118 die "failed to delete encryption key! $!\n";
119 }
120 delete $scfg->{'encryption-key'};
121 }
122
123 sub pbs_get_encryption_key {
124 my ($scfg, $storeid) = @_;
125
126 my $pwfile = pbs_encryption_key_file_name($scfg, $storeid);
127
128 return PVE::Tools::file_get_contents($pwfile);
129 }
130
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) = @_;
134
135 my $encryption_key_file = pbs_encryption_key_file_name($scfg, $storeid);
136
137 my $keyfd;
138 if (!open($keyfd, '<', $encryption_key_file)) {
139 return undef if $! == ENOENT;
140 die "failed to open encryption key: $encryption_key_file: $!\n";
141 }
142
143 return $keyfd;
144 }
145
146 sub print_volid {
147 my ($storeid, $btype, $bid, $btime) = @_;
148
149 my $time_str = strftime("%FT%TZ", gmtime($btime));
150 my $volname = "backup/${btype}/${bid}/${time_str}";
151
152 return "${storeid}:${volname}";
153 }
154
155 my $USE_CRYPT_PARAMS = {
156 backup => 1,
157 restore => 1,
158 'upload-log' => 1,
159 };
160
161 my sub do_raw_client_cmd {
162 my ($scfg, $storeid, $client_cmd, $param, %opts) = @_;
163
164 my $use_crypto = $USE_CRYPT_PARAMS->{$client_cmd};
165
166 my $client_exe = '/usr/bin/proxmox-backup-client';
167 die "executable not found '$client_exe'! Proxmox backup client not installed?\n"
168 if ! -x $client_exe;
169
170 my $server = $scfg->{server};
171 my $datastore = $scfg->{datastore};
172 my $username = $scfg->{username} // 'root@pam';
173
174 my $userns_cmd = delete $opts{userns_cmd};
175
176 my $cmd = [];
177
178 push @$cmd, @$userns_cmd if defined($userns_cmd);
179
180 push @$cmd, $client_exe, $client_cmd;
181
182 # This must live in the top scope to not get closed before the `run_command`
183 my $keyfd;
184 if ($use_crypto) {
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);
191 } else {
192 push @$cmd, '--crypt-mode=none';
193 }
194 }
195
196 push @$cmd, @$param if defined($param);
197
198 push @$cmd, "--repository", "$username\@$server:$datastore";
199
200 local $ENV{PBS_PASSWORD} = pbs_get_password($scfg, $storeid);
201
202 local $ENV{PBS_FINGERPRINT} = $scfg->{fingerprint};
203
204 # no ascii-art on task logs
205 local $ENV{PROXMOX_OUTPUT_NO_BORDER} = 1;
206 local $ENV{PROXMOX_OUTPUT_NO_HEADER} = 1;
207
208 if (my $logfunc = $opts{logfunc}) {
209 $logfunc->("run: " . join(' ', @$cmd));
210 }
211
212 run_command($cmd, %opts);
213 }
214
215 # FIXME: External perl code should NOT have access to this.
216 #
217 # There should be separate functions to
218 # - make backups
219 # - restore backups
220 # - restore files
221 # with a sane API
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);
225 }
226
227 sub run_client_cmd {
228 my ($scfg, $storeid, $client_cmd, $param, $no_output) = @_;
229
230 my $json_str = '';
231 my $outfunc = sub { $json_str .= "$_[0]\n" };
232
233 $param = [] if !defined($param);
234 $param = [ $param ] if !ref($param);
235
236 $param = [@$param, '--output-format=json'] if !$no_output;
237
238 do_raw_client_cmd($scfg, $storeid, $client_cmd, $param,
239 outfunc => $outfunc, errmsg => 'proxmox-backup-client failed');
240
241 return undef if $no_output;
242
243 my $res = decode_json($json_str);
244
245 return $res;
246 }
247
248 # Storage implementation
249
250 sub extract_vzdump_config {
251 my ($class, $scfg, $volname, $storeid) = @_;
252
253 my ($vtype, $name, $vmid, undef, undef, undef, $format) = $class->parse_volname($volname);
254
255 my $config = '';
256 my $outfunc = sub { $config .= "$_[0]\n" };
257
258 my $config_name;
259 if ($format eq 'pbs-vm') {
260 $config_name = 'qemu-server.conf';
261 } elsif ($format eq 'pbs-ct') {
262 $config_name = 'pct.conf';
263 } else {
264 die "unable to extract configuration for backup format '$format'\n";
265 }
266
267 do_raw_client_cmd($scfg, $storeid, 'restore', [ $name, $config_name, '-' ],
268 outfunc => $outfunc, errmsg => 'proxmox-backup-client failed');
269
270 return $config;
271 }
272
273 sub prune_backups {
274 my ($class, $scfg, $storeid, $keep, $vmid, $type, $dryrun, $logfunc) = @_;
275
276 $logfunc //= sub { print "$_[1]\n" };
277
278 my $backups = $class->list_volumes($storeid, $scfg, $vmid, ['backup']);
279
280 $type = 'vm' if defined($type) && $type eq 'qemu';
281 $type = 'ct' if defined($type) && $type eq 'lxc';
282
283 my $backup_groups = {};
284 foreach my $backup (@{$backups}) {
285 (my $backup_type = $backup->{format}) =~ s/^pbs-//;
286
287 next if defined($type) && $backup_type ne $type;
288
289 my $backup_group = "$backup_type/$backup->{vmid}";
290 $backup_groups->{$backup_group} = 1;
291 }
292
293 my @param;
294 foreach my $opt (keys %{$keep}) {
295 push @param, "--$opt";
296 push @param, "$keep->{$opt}";
297 }
298
299 push @param, '--dry-run' if $dryrun;
300
301 my $prune_list = [];
302 my $failed;
303
304 foreach my $backup_group (keys %{$backup_groups}) {
305 $logfunc->('info', "running 'proxmox-backup-client prune' for '$backup_group'")
306 if !$dryrun;
307 eval {
308 my $res = run_client_cmd($scfg, $storeid, 'prune', [ $backup_group, @param ]);
309
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'});
316
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);
321
322 push @{$prune_list}, {
323 ctime => $ctime,
324 mark => $backup->{keep} ? 'keep' : 'remove',
325 type => $type eq 'vm' ? 'qemu' : 'lxc',
326 vmid => $vmid,
327 volid => $volid,
328 };
329 }
330 };
331 if (my $err = $@) {
332 $logfunc->('err', "prune '$backup_group': $err\n");
333 $failed = 1;
334 }
335 }
336 die "error pruning backups - check log\n" if $failed;
337
338 return $prune_list;
339 }
340
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]);
345 };
346
347 sub on_add_hook {
348 my ($class, $storeid, $scfg, %param) = @_;
349
350 if (defined(my $password = $param{password})) {
351 pbs_set_password($scfg, $storeid, $password);
352 } else {
353 pbs_delete_password($scfg, $storeid);
354 }
355
356 if (defined(my $encryption_key = $param{'encryption-key'})) {
357 if ($encryption_key eq 'autogen') {
358 $autogen_encryption_key->($scfg, $storeid);
359 } else {
360 pbs_set_encryption_key($scfg, $storeid, $encryption_key);
361 }
362 $scfg->{'encryption-key'} = 1;
363 } else {
364 pbs_delete_encryption_key($scfg, $storeid);
365 }
366 }
367
368 sub on_update_hook {
369 my ($class, $storeid, $scfg, %param) = @_;
370
371 if (exists($param{password})) {
372 if (defined($param{password})) {
373 pbs_set_password($scfg, $storeid, $param{password});
374 } else {
375 pbs_delete_password($scfg, $storeid);
376 }
377 }
378
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);
383 } else {
384 pbs_set_encryption_key($scfg, $storeid, $encryption_key);
385 }
386 $scfg->{'encryption-key'} = 1;
387 } else {
388 pbs_delete_encryption_key($scfg, $storeid);
389 }
390 }
391 }
392
393 sub on_delete_hook {
394 my ($class, $storeid, $scfg) = @_;
395
396 pbs_delete_password($scfg, $storeid);
397 pbs_delete_encryption_key($scfg, $storeid);
398 }
399
400 sub parse_volname {
401 my ($class, $volname) = @_;
402
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)$!) {
404 my $btype = $1;
405 my $bid = $2;
406 my $btime = $3;
407 my $format = "pbs-$btype";
408
409 my $name = "$btype/$bid/$btime";
410
411 if ($bid =~ m/^\d+$/) {
412 return ('backup', $name, $bid, undef, undef, undef, $format);
413 } else {
414 return ('backup', $name, undef, undef, undef, undef, $format);
415 }
416 }
417
418 die "unable to parse PBS volume name '$volname'\n";
419 }
420
421 sub path {
422 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
423
424 die "volume snapshot is not possible on pbs storage"
425 if defined($snapname);
426
427 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
428
429 my $server = $scfg->{server};
430 my $datastore = $scfg->{datastore};
431 my $username = $scfg->{username} // 'root@pam';
432
433 # artifical url - we currently do not use that anywhere
434 my $path = "pbs://$username\@$server:$datastore/$name";
435
436 return ($path, $vmid, $vtype);
437 }
438
439 sub create_base {
440 my ($class, $storeid, $scfg, $volname) = @_;
441
442 die "can't create base images in pbs storage\n";
443 }
444
445 sub clone_image {
446 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
447
448 die "can't clone images in pbs storage\n";
449 }
450
451 sub alloc_image {
452 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
453
454 die "can't allocate space in pbs storage\n";
455 }
456
457 sub free_image {
458 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
459
460 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
461
462 run_client_cmd($scfg, $storeid, "forget", [ $name ], 1);
463 }
464
465
466 sub list_images {
467 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
468
469 my $res = [];
470
471 return $res;
472 }
473
474 sub list_volumes {
475 my ($class, $storeid, $scfg, $vmid, $content_types) = @_;
476
477 my $res = [];
478
479 return $res if !grep { $_ eq 'backup' } @$content_types;
480
481 my $data = run_client_cmd($scfg, $storeid, "snapshots");
482
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;
488
489 next if !($btype eq 'vm' || $btype eq 'ct');
490 next if $bid !~ m/^\d+$/;
491 next if defined($vmid) && $bid ne $vmid;
492
493 my $volid = print_volid($storeid, $btype, $bid, $epoch);
494
495 my $info = {
496 volid => $volid,
497 format => "pbs-$btype",
498 size => $size,
499 content => 'backup',
500 vmid => int($bid),
501 ctime => $epoch,
502 };
503
504 push @$res, $info;
505 }
506
507 return $res;
508 }
509
510 sub status {
511 my ($class, $storeid, $scfg, $cache) = @_;
512
513 my $total = 0;
514 my $free = 0;
515 my $used = 0;
516 my $active = 0;
517
518 eval {
519 my $res = run_client_cmd($scfg, $storeid, "status");
520
521 $active = 1;
522 $total = $res->{total};
523 $used = $res->{used};
524 $free = $res->{avail};
525 };
526 if (my $err = $@) {
527 warn $err;
528 }
529
530 return ($total, $free, $used, $active);
531 }
532
533 sub activate_storage {
534 my ($class, $storeid, $scfg, $cache) = @_;
535
536 run_client_cmd($scfg, $storeid, "status");
537
538 return 1;
539 }
540
541 sub deactivate_storage {
542 my ($class, $storeid, $scfg, $cache) = @_;
543 return 1;
544 }
545
546 sub activate_volume {
547 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
548
549 die "volume snapshot is not possible on pbs device" if $snapname;
550
551 return 1;
552 }
553
554 sub deactivate_volume {
555 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
556
557 die "volume snapshot is not possible on pbs device" if $snapname;
558
559 return 1;
560 }
561
562 sub volume_size_info {
563 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
564
565 my ($vtype, $name, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
566
567 my $data = run_client_cmd($scfg, $storeid, "files", [ $name ]);
568
569 my $size = 0;
570 foreach my $info (@$data) {
571 $size += $info->{size} if $info->{size};
572 }
573
574 my $used = $size;
575
576 return wantarray ? ($size, $format, $used, undef) : $size;
577 }
578
579 sub volume_resize {
580 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
581 die "volume resize is not possible on pbs device";
582 }
583
584 sub volume_snapshot {
585 my ($class, $scfg, $storeid, $volname, $snap) = @_;
586 die "volume snapshot is not possible on pbs device";
587 }
588
589 sub volume_snapshot_rollback {
590 my ($class, $scfg, $storeid, $volname, $snap) = @_;
591 die "volume snapshot rollback is not possible on pbs device";
592 }
593
594 sub volume_snapshot_delete {
595 my ($class, $scfg, $storeid, $volname, $snap) = @_;
596 die "volume snapshot delete is not possible on pbs device";
597 }
598
599 sub volume_has_feature {
600 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
601
602 return undef;
603 }
604
605 1;