]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/PBSPlugin.pm
bump version to 6.2-7
[pve-storage.git] / PVE / Storage / PBSPlugin.pm
CommitLineData
271fe394
DM
1package PVE::Storage::PBSPlugin;
2
3# Plugin to access Proxmox Backup Server
4
5use strict;
6use warnings;
76bb5feb 7use Fcntl qw(F_GETFD F_SETFD FD_CLOEXEC);
271fe394 8use HTTP::Request;
76bb5feb 9use IO::File;
271fe394 10use JSON;
76bb5feb
WB
11use LWP::UserAgent;
12use POSIX qw(strftime ENOENT);
271fe394
DM
13
14use PVE::Tools qw(run_command file_read_firstline trim dir_glob_regex dir_glob_foreach);
15use PVE::Storage::Plugin;
16use PVE::JSONSchema qw(get_standard_option);
17
18use base qw(PVE::Storage::Plugin);
19
20# Configuration
21
22sub type {
23 return 'pbs';
24}
25
26sub plugindata {
27 return {
28 content => [ {backup => 1, none => 1}, { backup => 1 }],
29 };
30}
31
32sub 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'),
ce2e2733 40 'encryption-key' => {
1aeb322b 41 description => "Encryption key. Use 'autogen' to generate one automatically without passphrase.",
76bb5feb
WB
42 type => 'string',
43 },
271fe394
DM
44 };
45}
46
47sub 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 },
ce2e2733
TL
55 password => { optional => 1 },
56 'encryption-key' => { optional => 1 },
271fe394 57 maxfiles => { optional => 1 },
3353698f 58 'prune-backups' => { optional => 1 },
271fe394
DM
59 fingerprint => { optional => 1 },
60 };
61}
62
63# Helpers
64
65sub pbs_password_file_name {
66 my ($scfg, $storeid) = @_;
67
462537a2 68 return "/etc/pve/priv/storage/${storeid}.pw";
271fe394
DM
69}
70
71sub pbs_set_password {
72 my ($scfg, $storeid, $password) = @_;
73
74 my $pwfile = pbs_password_file_name($scfg, $storeid);
9e34813f 75 mkdir "/etc/pve/priv/storage";
271fe394
DM
76
77 PVE::Tools::file_set_contents($pwfile, "$password\n");
78}
79
80sub pbs_delete_password {
81 my ($scfg, $storeid) = @_;
82
83 my $pwfile = pbs_password_file_name($scfg, $storeid);
84
85 unlink $pwfile;
86}
87
88sub 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
76bb5feb
WB
96sub pbs_encryption_key_file_name {
97 my ($scfg, $storeid) = @_;
98
99 return "/etc/pve/priv/storage/${storeid}.enc";
100}
101
102sub 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
111sub pbs_delete_encryption_key {
112 my ($scfg, $storeid) = @_;
113
114 my $pwfile = pbs_encryption_key_file_name($scfg, $storeid);
115
4ef17e1f
TL
116 if (!unlink $pwfile) {
117 return if $! == ENOENT;
118 die "failed to delete encryption key! $!\n";
119 }
18cf6c9f 120 delete $scfg->{'encryption-key'};
76bb5feb
WB
121}
122
123sub 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.
132sub 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
8602fd56
FE
146sub 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}
271fe394 154
02cc5e10
WB
155my $USE_CRYPT_PARAMS = {
156 backup => 1,
157 restore => 1,
158 'upload-log' => 1,
159};
160
76bb5feb 161my sub do_raw_client_cmd {
02cc5e10
WB
162 my ($scfg, $storeid, $client_cmd, $param, %opts) = @_;
163
164 my $use_crypto = $USE_CRYPT_PARAMS->{$client_cmd};
271fe394 165
1574a590
TL
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
271fe394
DM
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
1574a590 180 push @$cmd, $client_exe, $client_cmd;
271fe394 181
76bb5feb
WB
182 # This must live in the top scope to not get closed before the `run_command`
183 my $keyfd;
02cc5e10 184 if ($use_crypto) {
76bb5feb
WB
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
271fe394
DM
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
8b4c2a7e
DM
204 # no ascii-art on task logs
205 local $ENV{PROXMOX_OUTPUT_NO_BORDER} = 1;
206 local $ENV{PROXMOX_OUTPUT_NO_HEADER} = 1;
207
271fe394 208 if (my $logfunc = $opts{logfunc}) {
e6d1edcb 209 $logfunc->("run: " . join(' ', @$cmd));
271fe394
DM
210 }
211
212 run_command($cmd, %opts);
213}
214
76bb5feb
WB
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
02cc5e10 222sub run_raw_client_cmd {
76bb5feb 223 my ($scfg, $storeid, $client_cmd, $param, %opts) = @_;
02cc5e10 224 return do_raw_client_cmd($scfg, $storeid, $client_cmd, $param, %opts);
76bb5feb
WB
225}
226
271fe394
DM
227sub run_client_cmd {
228 my ($scfg, $storeid, $client_cmd, $param, $no_output) = @_;
229
230 my $json_str = '';
fee2ece3 231 my $outfunc = sub { $json_str .= "$_[0]\n" };
271fe394
DM
232
233 $param = [] if !defined($param);
234 $param = [ $param ] if !ref($param);
235
236 $param = [@$param, '--output-format=json'] if !$no_output;
237
02cc5e10 238 do_raw_client_cmd($scfg, $storeid, $client_cmd, $param,
76bb5feb 239 outfunc => $outfunc, errmsg => 'proxmox-backup-client failed');
271fe394
DM
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
c855ac15
DM
250sub 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 = '';
fee2ece3 256 my $outfunc = sub { $config .= "$_[0]\n" };
c855ac15
DM
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
02cc5e10 267 do_raw_client_cmd($scfg, $storeid, 'restore', [ $name, $config_name, '-' ],
76bb5feb 268 outfunc => $outfunc, errmsg => 'proxmox-backup-client failed');
c855ac15
DM
269
270 return $config;
271}
272
8f26b391
FE
273sub 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
1aeb322b
TL
341my $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
271fe394
DM
347sub on_add_hook {
348 my ($class, $storeid, $scfg, %param) = @_;
349
76bb5feb
WB
350 if (defined(my $password = $param{password})) {
351 pbs_set_password($scfg, $storeid, $password);
b494636a
DM
352 } else {
353 pbs_delete_password($scfg, $storeid);
354 }
76bb5feb 355
ce2e2733 356 if (defined(my $encryption_key = $param{'encryption-key'})) {
1aeb322b
TL
357 if ($encryption_key eq 'autogen') {
358 $autogen_encryption_key->($scfg, $storeid);
359 } else {
360 pbs_set_encryption_key($scfg, $storeid, $encryption_key);
361 }
18cf6c9f 362 $scfg->{'encryption-key'} = 1;
76bb5feb
WB
363 } else {
364 pbs_delete_encryption_key($scfg, $storeid);
365 }
b494636a
DM
366}
367
368sub on_update_hook {
369 my ($class, $storeid, $scfg, %param) = @_;
370
76bb5feb
WB
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 }
b494636a 378
ce2e2733
TL
379 if (exists($param{'encryption-key'})) {
380 if (defined(my $encryption_key = delete($param{'encryption-key'}))) {
1aeb322b
TL
381 if ($encryption_key eq 'autogen') {
382 $autogen_encryption_key->($scfg, $storeid);
383 } else {
384 pbs_set_encryption_key($scfg, $storeid, $encryption_key);
385 }
18cf6c9f 386 $scfg->{'encryption-key'} = 1;
76bb5feb
WB
387 } else {
388 pbs_delete_encryption_key($scfg, $storeid);
389 }
271fe394
DM
390 }
391}
392
393sub on_delete_hook {
394 my ($class, $storeid, $scfg) = @_;
395
396 pbs_delete_password($scfg, $storeid);
76bb5feb 397 pbs_delete_encryption_key($scfg, $storeid);
271fe394
DM
398}
399
400sub 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
421sub 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
439sub create_base {
440 my ($class, $storeid, $scfg, $volname) = @_;
441
442 die "can't create base images in pbs storage\n";
443}
444
445sub clone_image {
446 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
447
448 die "can't clone images in pbs storage\n";
449}
450
451sub alloc_image {
452 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
453
454 die "can't allocate space in pbs storage\n";
455}
456
457sub 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
466sub list_images {
467 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
468
469 my $res = [];
470
471 return $res;
472}
473
474sub 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"};
545e127e 486 my $epoch = $item->{"backup-time"};
271fe394
DM
487 my $size = $item->{size} // 1;
488
489 next if !($btype eq 'vm' || $btype eq 'ct');
490 next if $bid !~ m/^\d+$/;
ddf7fdaa 491 next if defined($vmid) && $bid ne $vmid;
271fe394 492
8602fd56 493 my $volid = print_volid($storeid, $btype, $bid, $epoch);
271fe394 494
545e127e 495 my $info = {
c05b1a8c
TL
496 volid => $volid,
497 format => "pbs-$btype",
498 size => $size,
499 content => 'backup',
500 vmid => int($bid),
501 ctime => $epoch,
545e127e 502 };
271fe394
DM
503
504 push @$res, $info;
505 }
506
507 return $res;
508}
509
510sub 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};
f155c912
TL
524 $free = $res->{avail};
525 };
271fe394
DM
526 if (my $err = $@) {
527 warn $err;
528 }
529
530 return ($total, $free, $used, $active);
531}
532
533sub activate_storage {
534 my ($class, $storeid, $scfg, $cache) = @_;
bb0a0f96
TL
535
536 run_client_cmd($scfg, $storeid, "status");
537
271fe394
DM
538 return 1;
539}
540
541sub deactivate_storage {
542 my ($class, $storeid, $scfg, $cache) = @_;
543 return 1;
544}
545
546sub 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
554sub 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
562sub 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
579sub volume_resize {
580 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
581 die "volume resize is not possible on pbs device";
582}
583
584sub volume_snapshot {
585 my ($class, $scfg, $storeid, $volname, $snap) = @_;
586 die "volume snapshot is not possible on pbs device";
587}
588
589sub volume_snapshot_rollback {
590 my ($class, $scfg, $storeid, $volname, $snap) = @_;
591 die "volume snapshot rollback is not possible on pbs device";
592}
593
594sub volume_snapshot_delete {
595 my ($class, $scfg, $storeid, $volname, $snap) = @_;
596 die "volume snapshot delete is not possible on pbs device";
597}
598
599sub volume_has_feature {
600 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
601
602 return undef;
603}
604
6051;