]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/PBSPlugin.pm
PBS: add support to specify port
[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;
4133e6e2 7
76bb5feb 8use Fcntl qw(F_GETFD F_SETFD FD_CLOEXEC);
271fe394 9use HTTP::Request;
76bb5feb 10use IO::File;
271fe394 11use JSON;
76bb5feb
WB
12use LWP::UserAgent;
13use POSIX qw(strftime ENOENT);
271fe394 14
271fe394 15use PVE::JSONSchema qw(get_standard_option);
4133e6e2
TL
16use PVE::Network;
17use PVE::Storage::Plugin;
18use PVE::Tools qw(run_command file_read_firstline trim dir_glob_regex dir_glob_foreach $IPV6RE);
271fe394
DM
19
20use base qw(PVE::Storage::Plugin);
21
22# Configuration
23
24sub type {
25 return 'pbs';
26}
27
28sub plugindata {
29 return {
30 content => [ {backup => 1, none => 1}, { backup => 1 }],
31 };
32}
33
34sub properties {
35 return {
36 datastore => {
4133e6e2 37 description => "Proxmox Backup Server datastore name.",
271fe394
DM
38 type => 'string',
39 },
40 # openssl s_client -connect <host>:8007 2>&1 |openssl x509 -fingerprint -sha256
41 fingerprint => get_standard_option('fingerprint-sha256'),
ce2e2733 42 'encryption-key' => {
1aeb322b 43 description => "Encryption key. Use 'autogen' to generate one automatically without passphrase.",
76bb5feb
WB
44 type => 'string',
45 },
4133e6e2
TL
46 port => {
47 description => "For non default port.",
48 type => 'integer',
49 minimum => 1,
50 maximum => 65535,
51 default => 8007,
52 }
271fe394
DM
53 };
54}
55
56sub options {
57 return {
58 server => { fixed => 1 },
59 datastore => { fixed => 1 },
4133e6e2 60 port => { optional => 1 },
271fe394
DM
61 nodes => { optional => 1},
62 disable => { optional => 1},
63 content => { optional => 1},
64 username => { optional => 1 },
ce2e2733
TL
65 password => { optional => 1 },
66 'encryption-key' => { optional => 1 },
271fe394 67 maxfiles => { optional => 1 },
3353698f 68 'prune-backups' => { optional => 1 },
271fe394
DM
69 fingerprint => { optional => 1 },
70 };
71}
72
73# Helpers
74
75sub pbs_password_file_name {
76 my ($scfg, $storeid) = @_;
77
462537a2 78 return "/etc/pve/priv/storage/${storeid}.pw";
271fe394
DM
79}
80
81sub pbs_set_password {
82 my ($scfg, $storeid, $password) = @_;
83
84 my $pwfile = pbs_password_file_name($scfg, $storeid);
9e34813f 85 mkdir "/etc/pve/priv/storage";
271fe394
DM
86
87 PVE::Tools::file_set_contents($pwfile, "$password\n");
88}
89
90sub pbs_delete_password {
91 my ($scfg, $storeid) = @_;
92
93 my $pwfile = pbs_password_file_name($scfg, $storeid);
94
95 unlink $pwfile;
96}
97
98sub 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
76bb5feb
WB
106sub pbs_encryption_key_file_name {
107 my ($scfg, $storeid) = @_;
108
109 return "/etc/pve/priv/storage/${storeid}.enc";
110}
111
112sub 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
121sub pbs_delete_encryption_key {
122 my ($scfg, $storeid) = @_;
123
124 my $pwfile = pbs_encryption_key_file_name($scfg, $storeid);
125
4ef17e1f
TL
126 if (!unlink $pwfile) {
127 return if $! == ENOENT;
128 die "failed to delete encryption key! $!\n";
129 }
18cf6c9f 130 delete $scfg->{'encryption-key'};
76bb5feb
WB
131}
132
133sub 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.
142sub 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
8602fd56
FE
156sub 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}
271fe394 164
4133e6e2
TL
165my 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
02cc5e10
WB
177my $USE_CRYPT_PARAMS = {
178 backup => 1,
179 restore => 1,
180 'upload-log' => 1,
181};
182
76bb5feb 183my sub do_raw_client_cmd {
02cc5e10
WB
184 my ($scfg, $storeid, $client_cmd, $param, %opts) = @_;
185
186 my $use_crypto = $USE_CRYPT_PARAMS->{$client_cmd};
271fe394 187
1574a590
TL
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
4133e6e2 192 my $server = get_server_with_port($scfg);
271fe394
DM
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
1574a590 202 push @$cmd, $client_exe, $client_cmd;
271fe394 203
76bb5feb
WB
204 # This must live in the top scope to not get closed before the `run_command`
205 my $keyfd;
02cc5e10 206 if ($use_crypto) {
76bb5feb
WB
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
271fe394
DM
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
8b4c2a7e
DM
226 # no ascii-art on task logs
227 local $ENV{PROXMOX_OUTPUT_NO_BORDER} = 1;
228 local $ENV{PROXMOX_OUTPUT_NO_HEADER} = 1;
229
271fe394 230 if (my $logfunc = $opts{logfunc}) {
e6d1edcb 231 $logfunc->("run: " . join(' ', @$cmd));
271fe394
DM
232 }
233
234 run_command($cmd, %opts);
235}
236
76bb5feb
WB
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
02cc5e10 244sub run_raw_client_cmd {
76bb5feb 245 my ($scfg, $storeid, $client_cmd, $param, %opts) = @_;
02cc5e10 246 return do_raw_client_cmd($scfg, $storeid, $client_cmd, $param, %opts);
76bb5feb
WB
247}
248
271fe394
DM
249sub run_client_cmd {
250 my ($scfg, $storeid, $client_cmd, $param, $no_output) = @_;
251
252 my $json_str = '';
fee2ece3 253 my $outfunc = sub { $json_str .= "$_[0]\n" };
271fe394
DM
254
255 $param = [] if !defined($param);
256 $param = [ $param ] if !ref($param);
257
258 $param = [@$param, '--output-format=json'] if !$no_output;
259
02cc5e10 260 do_raw_client_cmd($scfg, $storeid, $client_cmd, $param,
76bb5feb 261 outfunc => $outfunc, errmsg => 'proxmox-backup-client failed');
271fe394
DM
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
c855ac15
DM
272sub 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 = '';
fee2ece3 278 my $outfunc = sub { $config .= "$_[0]\n" };
c855ac15
DM
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
02cc5e10 289 do_raw_client_cmd($scfg, $storeid, 'restore', [ $name, $config_name, '-' ],
76bb5feb 290 outfunc => $outfunc, errmsg => 'proxmox-backup-client failed');
c855ac15
DM
291
292 return $config;
293}
294
8f26b391
FE
295sub 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 foreach my $opt (keys %{$keep}) {
317 push @param, "--$opt";
318 push @param, "$keep->{$opt}";
319 }
320
321 push @param, '--dry-run' if $dryrun;
322
323 my $prune_list = [];
324 my $failed;
325
326 foreach my $backup_group (keys %{$backup_groups}) {
327 $logfunc->('info', "running 'proxmox-backup-client prune' for '$backup_group'")
328 if !$dryrun;
329 eval {
330 my $res = run_client_cmd($scfg, $storeid, 'prune', [ $backup_group, @param ]);
331
332 foreach my $backup (@{$res}) {
333 die "result from proxmox-backup-client is not as expected\n"
334 if !defined($backup->{'backup-time'})
335 || !defined($backup->{'backup-type'})
336 || !defined($backup->{'backup-id'})
337 || !defined($backup->{'keep'});
338
339 my $ctime = $backup->{'backup-time'};
340 my $type = $backup->{'backup-type'};
341 my $vmid = $backup->{'backup-id'};
342 my $volid = print_volid($storeid, $type, $vmid, $ctime);
343
344 push @{$prune_list}, {
345 ctime => $ctime,
346 mark => $backup->{keep} ? 'keep' : 'remove',
347 type => $type eq 'vm' ? 'qemu' : 'lxc',
348 vmid => $vmid,
349 volid => $volid,
350 };
351 }
352 };
353 if (my $err = $@) {
354 $logfunc->('err', "prune '$backup_group': $err\n");
355 $failed = 1;
356 }
357 }
358 die "error pruning backups - check log\n" if $failed;
359
360 return $prune_list;
361}
362
1aeb322b
TL
363my $autogen_encryption_key = sub {
364 my ($scfg, $storeid) = @_;
365 my $encfile = pbs_encryption_key_file_name($scfg, $storeid);
366 run_command(['proxmox-backup-client', 'key', 'create', '--kdf', 'none', $encfile]);
367};
368
271fe394
DM
369sub on_add_hook {
370 my ($class, $storeid, $scfg, %param) = @_;
371
76bb5feb
WB
372 if (defined(my $password = $param{password})) {
373 pbs_set_password($scfg, $storeid, $password);
b494636a
DM
374 } else {
375 pbs_delete_password($scfg, $storeid);
376 }
76bb5feb 377
ce2e2733 378 if (defined(my $encryption_key = $param{'encryption-key'})) {
1aeb322b
TL
379 if ($encryption_key eq 'autogen') {
380 $autogen_encryption_key->($scfg, $storeid);
381 } else {
382 pbs_set_encryption_key($scfg, $storeid, $encryption_key);
383 }
18cf6c9f 384 $scfg->{'encryption-key'} = 1;
76bb5feb
WB
385 } else {
386 pbs_delete_encryption_key($scfg, $storeid);
387 }
b494636a
DM
388}
389
390sub on_update_hook {
391 my ($class, $storeid, $scfg, %param) = @_;
392
76bb5feb
WB
393 if (exists($param{password})) {
394 if (defined($param{password})) {
395 pbs_set_password($scfg, $storeid, $param{password});
396 } else {
397 pbs_delete_password($scfg, $storeid);
398 }
399 }
b494636a 400
ce2e2733
TL
401 if (exists($param{'encryption-key'})) {
402 if (defined(my $encryption_key = delete($param{'encryption-key'}))) {
1aeb322b
TL
403 if ($encryption_key eq 'autogen') {
404 $autogen_encryption_key->($scfg, $storeid);
405 } else {
406 pbs_set_encryption_key($scfg, $storeid, $encryption_key);
407 }
18cf6c9f 408 $scfg->{'encryption-key'} = 1;
76bb5feb
WB
409 } else {
410 pbs_delete_encryption_key($scfg, $storeid);
411 }
271fe394
DM
412 }
413}
414
415sub on_delete_hook {
416 my ($class, $storeid, $scfg) = @_;
417
418 pbs_delete_password($scfg, $storeid);
76bb5feb 419 pbs_delete_encryption_key($scfg, $storeid);
271fe394
DM
420}
421
422sub parse_volname {
423 my ($class, $volname) = @_;
424
425 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)$!) {
426 my $btype = $1;
427 my $bid = $2;
428 my $btime = $3;
429 my $format = "pbs-$btype";
430
431 my $name = "$btype/$bid/$btime";
432
433 if ($bid =~ m/^\d+$/) {
434 return ('backup', $name, $bid, undef, undef, undef, $format);
435 } else {
436 return ('backup', $name, undef, undef, undef, undef, $format);
437 }
438 }
439
440 die "unable to parse PBS volume name '$volname'\n";
441}
442
443sub path {
444 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
445
446 die "volume snapshot is not possible on pbs storage"
447 if defined($snapname);
448
449 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
450
4133e6e2 451 my $server = get_server_with_port($scfg);
271fe394
DM
452 my $datastore = $scfg->{datastore};
453 my $username = $scfg->{username} // 'root@pam';
454
455 # artifical url - we currently do not use that anywhere
456 my $path = "pbs://$username\@$server:$datastore/$name";
457
458 return ($path, $vmid, $vtype);
459}
460
461sub create_base {
462 my ($class, $storeid, $scfg, $volname) = @_;
463
464 die "can't create base images in pbs storage\n";
465}
466
467sub clone_image {
468 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
469
470 die "can't clone images in pbs storage\n";
471}
472
473sub alloc_image {
474 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
475
476 die "can't allocate space in pbs storage\n";
477}
478
479sub free_image {
480 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
481
482 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
483
484 run_client_cmd($scfg, $storeid, "forget", [ $name ], 1);
485}
486
487
488sub list_images {
489 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
490
491 my $res = [];
492
493 return $res;
494}
495
496sub list_volumes {
497 my ($class, $storeid, $scfg, $vmid, $content_types) = @_;
498
499 my $res = [];
500
501 return $res if !grep { $_ eq 'backup' } @$content_types;
502
503 my $data = run_client_cmd($scfg, $storeid, "snapshots");
504
505 foreach my $item (@$data) {
506 my $btype = $item->{"backup-type"};
507 my $bid = $item->{"backup-id"};
545e127e 508 my $epoch = $item->{"backup-time"};
271fe394
DM
509 my $size = $item->{size} // 1;
510
511 next if !($btype eq 'vm' || $btype eq 'ct');
512 next if $bid !~ m/^\d+$/;
ddf7fdaa 513 next if defined($vmid) && $bid ne $vmid;
271fe394 514
8602fd56 515 my $volid = print_volid($storeid, $btype, $bid, $epoch);
271fe394 516
545e127e 517 my $info = {
c05b1a8c
TL
518 volid => $volid,
519 format => "pbs-$btype",
520 size => $size,
521 content => 'backup',
522 vmid => int($bid),
523 ctime => $epoch,
545e127e 524 };
271fe394
DM
525
526 push @$res, $info;
527 }
528
529 return $res;
530}
531
532sub status {
533 my ($class, $storeid, $scfg, $cache) = @_;
534
535 my $total = 0;
536 my $free = 0;
537 my $used = 0;
538 my $active = 0;
539
540 eval {
541 my $res = run_client_cmd($scfg, $storeid, "status");
542
543 $active = 1;
544 $total = $res->{total};
545 $used = $res->{used};
f155c912
TL
546 $free = $res->{avail};
547 };
271fe394
DM
548 if (my $err = $@) {
549 warn $err;
550 }
551
552 return ($total, $free, $used, $active);
553}
554
555sub activate_storage {
556 my ($class, $storeid, $scfg, $cache) = @_;
bb0a0f96
TL
557
558 run_client_cmd($scfg, $storeid, "status");
559
271fe394
DM
560 return 1;
561}
562
563sub deactivate_storage {
564 my ($class, $storeid, $scfg, $cache) = @_;
565 return 1;
566}
567
568sub activate_volume {
569 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
570
571 die "volume snapshot is not possible on pbs device" if $snapname;
572
573 return 1;
574}
575
576sub deactivate_volume {
577 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
578
579 die "volume snapshot is not possible on pbs device" if $snapname;
580
581 return 1;
582}
583
584sub volume_size_info {
585 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
586
587 my ($vtype, $name, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
588
589 my $data = run_client_cmd($scfg, $storeid, "files", [ $name ]);
590
591 my $size = 0;
592 foreach my $info (@$data) {
593 $size += $info->{size} if $info->{size};
594 }
595
596 my $used = $size;
597
598 return wantarray ? ($size, $format, $used, undef) : $size;
599}
600
601sub volume_resize {
602 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
603 die "volume resize is not possible on pbs device";
604}
605
606sub volume_snapshot {
607 my ($class, $scfg, $storeid, $volname, $snap) = @_;
608 die "volume snapshot is not possible on pbs device";
609}
610
611sub volume_snapshot_rollback {
612 my ($class, $scfg, $storeid, $volname, $snap) = @_;
613 die "volume snapshot rollback is not possible on pbs device";
614}
615
616sub volume_snapshot_delete {
617 my ($class, $scfg, $storeid, $volname, $snap) = @_;
618 die "volume snapshot delete is not possible on pbs device";
619}
620
621sub volume_has_feature {
622 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
623
624 return undef;
625}
626
6271;