]>
Commit | Line | Data |
---|---|---|
271fe394 DM |
1 | package PVE::Storage::PBSPlugin; |
2 | ||
3 | # Plugin to access Proxmox Backup Server | |
4 | ||
5 | use strict; | |
6 | use warnings; | |
4133e6e2 | 7 | |
76bb5feb | 8 | use Fcntl qw(F_GETFD F_SETFD FD_CLOEXEC); |
76bb5feb | 9 | use IO::File; |
271fe394 | 10 | use JSON; |
c56f7a71 | 11 | use MIME::Base64 qw(decode_base64); |
bfba9626 FE |
12 | use POSIX qw(mktime strftime ENOENT); |
13 | use POSIX::strptime; | |
271fe394 | 14 | |
2f9eb6dc | 15 | use PVE::APIClient::LWP; |
271fe394 | 16 | use PVE::JSONSchema qw(get_standard_option); |
4133e6e2 | 17 | use PVE::Network; |
53003cb5 | 18 | use PVE::PBSClient; |
4133e6e2 TL |
19 | use PVE::Storage::Plugin; |
20 | use PVE::Tools qw(run_command file_read_firstline trim dir_glob_regex dir_glob_foreach $IPV6RE); | |
271fe394 DM |
21 | |
22 | use base qw(PVE::Storage::Plugin); | |
23 | ||
24 | # Configuration | |
25 | ||
26 | sub type { | |
27 | return 'pbs'; | |
28 | } | |
29 | ||
30 | sub plugindata { | |
31 | return { | |
32 | content => [ {backup => 1, none => 1}, { backup => 1 }], | |
33 | }; | |
34 | } | |
35 | ||
36 | sub properties { | |
37 | return { | |
38 | datastore => { | |
4133e6e2 | 39 | description => "Proxmox Backup Server datastore name.", |
271fe394 DM |
40 | type => 'string', |
41 | }, | |
42 | # openssl s_client -connect <host>:8007 2>&1 |openssl x509 -fingerprint -sha256 | |
43 | fingerprint => get_standard_option('fingerprint-sha256'), | |
ce2e2733 | 44 | 'encryption-key' => { |
1aeb322b | 45 | description => "Encryption key. Use 'autogen' to generate one automatically without passphrase.", |
76bb5feb WB |
46 | type => 'string', |
47 | }, | |
c56f7a71 | 48 | 'master-pubkey' => { |
5b955999 | 49 | description => "Base64-encoded, PEM-formatted public RSA key. Used to encrypt a copy of the encryption-key which will be added to each encrypted backup.", |
c56f7a71 FG |
50 | type => 'string', |
51 | }, | |
4133e6e2 TL |
52 | port => { |
53 | description => "For non default port.", | |
54 | type => 'integer', | |
55 | minimum => 1, | |
56 | maximum => 65535, | |
57 | default => 8007, | |
2f9eb6dc | 58 | }, |
271fe394 DM |
59 | }; |
60 | } | |
61 | ||
62 | sub options { | |
63 | return { | |
64 | server => { fixed => 1 }, | |
65 | datastore => { fixed => 1 }, | |
78eac5ba | 66 | namespace => { optional => 1 }, |
4133e6e2 | 67 | port => { optional => 1 }, |
271fe394 DM |
68 | nodes => { optional => 1}, |
69 | disable => { optional => 1}, | |
70 | content => { optional => 1}, | |
71 | username => { optional => 1 }, | |
ce2e2733 TL |
72 | password => { optional => 1 }, |
73 | 'encryption-key' => { optional => 1 }, | |
c56f7a71 | 74 | 'master-pubkey' => { optional => 1 }, |
271fe394 | 75 | maxfiles => { optional => 1 }, |
3353698f | 76 | 'prune-backups' => { optional => 1 }, |
8009417d | 77 | 'max-protected-backups' => { optional => 1 }, |
271fe394 DM |
78 | fingerprint => { optional => 1 }, |
79 | }; | |
80 | } | |
81 | ||
82 | # Helpers | |
83 | ||
84 | sub pbs_password_file_name { | |
85 | my ($scfg, $storeid) = @_; | |
86 | ||
462537a2 | 87 | return "/etc/pve/priv/storage/${storeid}.pw"; |
271fe394 DM |
88 | } |
89 | ||
90 | sub pbs_set_password { | |
91 | my ($scfg, $storeid, $password) = @_; | |
92 | ||
93 | my $pwfile = pbs_password_file_name($scfg, $storeid); | |
9e34813f | 94 | mkdir "/etc/pve/priv/storage"; |
271fe394 DM |
95 | |
96 | PVE::Tools::file_set_contents($pwfile, "$password\n"); | |
97 | } | |
98 | ||
99 | sub pbs_delete_password { | |
100 | my ($scfg, $storeid) = @_; | |
101 | ||
102 | my $pwfile = pbs_password_file_name($scfg, $storeid); | |
103 | ||
104 | unlink $pwfile; | |
105 | } | |
106 | ||
107 | sub pbs_get_password { | |
108 | my ($scfg, $storeid) = @_; | |
109 | ||
110 | my $pwfile = pbs_password_file_name($scfg, $storeid); | |
111 | ||
112 | return PVE::Tools::file_read_firstline($pwfile); | |
113 | } | |
114 | ||
76bb5feb WB |
115 | sub pbs_encryption_key_file_name { |
116 | my ($scfg, $storeid) = @_; | |
117 | ||
118 | return "/etc/pve/priv/storage/${storeid}.enc"; | |
119 | } | |
120 | ||
121 | sub pbs_set_encryption_key { | |
122 | my ($scfg, $storeid, $key) = @_; | |
123 | ||
124 | my $pwfile = pbs_encryption_key_file_name($scfg, $storeid); | |
125 | mkdir "/etc/pve/priv/storage"; | |
126 | ||
127 | PVE::Tools::file_set_contents($pwfile, "$key\n"); | |
128 | } | |
129 | ||
130 | sub pbs_delete_encryption_key { | |
131 | my ($scfg, $storeid) = @_; | |
132 | ||
133 | my $pwfile = pbs_encryption_key_file_name($scfg, $storeid); | |
134 | ||
4ef17e1f TL |
135 | if (!unlink $pwfile) { |
136 | return if $! == ENOENT; | |
137 | die "failed to delete encryption key! $!\n"; | |
138 | } | |
18cf6c9f | 139 | delete $scfg->{'encryption-key'}; |
76bb5feb WB |
140 | } |
141 | ||
142 | sub pbs_get_encryption_key { | |
143 | my ($scfg, $storeid) = @_; | |
144 | ||
145 | my $pwfile = pbs_encryption_key_file_name($scfg, $storeid); | |
146 | ||
147 | return PVE::Tools::file_get_contents($pwfile); | |
148 | } | |
149 | ||
150 | # Returns a file handle if there is an encryption key, or `undef` if there is not. Dies on error. | |
151 | sub pbs_open_encryption_key { | |
152 | my ($scfg, $storeid) = @_; | |
153 | ||
154 | my $encryption_key_file = pbs_encryption_key_file_name($scfg, $storeid); | |
155 | ||
156 | my $keyfd; | |
157 | if (!open($keyfd, '<', $encryption_key_file)) { | |
2bc4cfb8 FG |
158 | if ($! == ENOENT) { |
159 | my $encryption_fp = $scfg->{'encryption-key'}; | |
160 | die "encryption configured ('$encryption_fp') but no encryption key file found!\n" | |
161 | if $encryption_fp; | |
162 | return undef; | |
163 | } | |
76bb5feb WB |
164 | die "failed to open encryption key: $encryption_key_file: $!\n"; |
165 | } | |
166 | ||
167 | return $keyfd; | |
168 | } | |
169 | ||
c56f7a71 FG |
170 | sub pbs_master_pubkey_file_name { |
171 | my ($scfg, $storeid) = @_; | |
172 | ||
173 | return "/etc/pve/priv/storage/${storeid}.master.pem"; | |
174 | } | |
175 | ||
176 | sub pbs_set_master_pubkey { | |
177 | my ($scfg, $storeid, $key) = @_; | |
178 | ||
179 | my $pwfile = pbs_master_pubkey_file_name($scfg, $storeid); | |
180 | mkdir "/etc/pve/priv/storage"; | |
181 | ||
182 | PVE::Tools::file_set_contents($pwfile, "$key\n"); | |
183 | } | |
184 | ||
185 | sub pbs_delete_master_pubkey { | |
186 | my ($scfg, $storeid) = @_; | |
187 | ||
188 | my $pwfile = pbs_master_pubkey_file_name($scfg, $storeid); | |
189 | ||
190 | if (!unlink $pwfile) { | |
191 | return if $! == ENOENT; | |
192 | die "failed to delete master public key! $!\n"; | |
193 | } | |
194 | delete $scfg->{'master-pubkey'}; | |
195 | } | |
196 | ||
197 | sub pbs_get_master_pubkey { | |
198 | my ($scfg, $storeid) = @_; | |
199 | ||
200 | my $pwfile = pbs_master_pubkey_file_name($scfg, $storeid); | |
201 | ||
202 | return PVE::Tools::file_get_contents($pwfile); | |
203 | } | |
204 | ||
205 | # Returns a file handle if there is a master key, or `undef` if there is not. Dies on error. | |
206 | sub pbs_open_master_pubkey { | |
207 | my ($scfg, $storeid) = @_; | |
208 | ||
209 | my $master_pubkey_file = pbs_master_pubkey_file_name($scfg, $storeid); | |
210 | ||
211 | my $keyfd; | |
212 | if (!open($keyfd, '<', $master_pubkey_file)) { | |
de635c26 | 213 | if ($! == ENOENT) { |
de0f9244 | 214 | die "master public key configured but no key file found!\n" |
de635c26 FG |
215 | if $scfg->{'master-pubkey'}; |
216 | return undef; | |
217 | } | |
c56f7a71 FG |
218 | die "failed to open master public key: $master_pubkey_file: $!\n"; |
219 | } | |
220 | ||
221 | return $keyfd; | |
222 | } | |
223 | ||
8602fd56 FE |
224 | sub print_volid { |
225 | my ($storeid, $btype, $bid, $btime) = @_; | |
226 | ||
227 | my $time_str = strftime("%FT%TZ", gmtime($btime)); | |
228 | my $volname = "backup/${btype}/${bid}/${time_str}"; | |
229 | ||
230 | return "${storeid}:${volname}"; | |
231 | } | |
271fe394 | 232 | |
78eac5ba WB |
233 | my sub ns : prototype($$) { |
234 | my ($scfg, $name) = @_; | |
235 | my $ns = $scfg->{namespace}; | |
236 | return defined($ns) ? ($name, $ns) : (); | |
237 | } | |
238 | ||
bfba9626 | 239 | # essentially the inverse of print_volid |
78eac5ba WB |
240 | my sub api_param_from_volname : prototype($$$) { |
241 | my ($class, $scfg, $volname) = @_; | |
bfba9626 FE |
242 | |
243 | my $name = ($class->parse_volname($volname))[1]; | |
244 | ||
245 | my ($btype, $bid, $timestr) = split('/', $name); | |
246 | ||
247 | my @tm = (POSIX::strptime($timestr, "%FT%TZ")); | |
248 | # expect sec, min, hour, mday, mon, year | |
249 | die "error parsing time from '$volname'" if grep { !defined($_) } @tm[0..5]; | |
250 | ||
251 | my $btime; | |
252 | { | |
253 | local $ENV{TZ} = 'UTC'; # $timestr is UTC | |
254 | ||
255 | # Fill in isdst to avoid undef warning. No daylight saving time for UTC. | |
256 | $tm[8] //= 0; | |
257 | ||
258 | my $since_epoch = mktime(@tm) or die "error converting time from '$volname'\n"; | |
259 | $btime = int($since_epoch); | |
260 | } | |
261 | ||
262 | return { | |
4c9c5f37 | 263 | (ns($scfg, 'ns')), |
bfba9626 FE |
264 | 'backup-type' => $btype, |
265 | 'backup-id' => $bid, | |
266 | 'backup-time' => $btime, | |
267 | }; | |
268 | } | |
269 | ||
02cc5e10 WB |
270 | my $USE_CRYPT_PARAMS = { |
271 | backup => 1, | |
272 | restore => 1, | |
273 | 'upload-log' => 1, | |
274 | }; | |
275 | ||
c56f7a71 FG |
276 | my $USE_MASTER_KEY = { |
277 | backup => 1, | |
278 | }; | |
279 | ||
76bb5feb | 280 | my sub do_raw_client_cmd { |
02cc5e10 WB |
281 | my ($scfg, $storeid, $client_cmd, $param, %opts) = @_; |
282 | ||
283 | my $use_crypto = $USE_CRYPT_PARAMS->{$client_cmd}; | |
c56f7a71 | 284 | my $use_master = $USE_MASTER_KEY->{$client_cmd}; |
271fe394 | 285 | |
1574a590 TL |
286 | my $client_exe = '/usr/bin/proxmox-backup-client'; |
287 | die "executable not found '$client_exe'! Proxmox backup client not installed?\n" | |
288 | if ! -x $client_exe; | |
289 | ||
53003cb5 | 290 | my $repo = PVE::PBSClient::get_repository($scfg); |
271fe394 DM |
291 | |
292 | my $userns_cmd = delete $opts{userns_cmd}; | |
293 | ||
294 | my $cmd = []; | |
295 | ||
296 | push @$cmd, @$userns_cmd if defined($userns_cmd); | |
297 | ||
1574a590 | 298 | push @$cmd, $client_exe, $client_cmd; |
271fe394 | 299 | |
76bb5feb | 300 | # This must live in the top scope to not get closed before the `run_command` |
c56f7a71 | 301 | my ($keyfd, $master_fd); |
02cc5e10 | 302 | if ($use_crypto) { |
76bb5feb WB |
303 | if (defined($keyfd = pbs_open_encryption_key($scfg, $storeid))) { |
304 | my $flags = fcntl($keyfd, F_GETFD, 0) | |
305 | // die "failed to get file descriptor flags: $!\n"; | |
306 | fcntl($keyfd, F_SETFD, $flags & ~FD_CLOEXEC) | |
307 | or die "failed to remove FD_CLOEXEC from encryption key file descriptor\n"; | |
308 | push @$cmd, '--crypt-mode=encrypt', '--keyfd='.fileno($keyfd); | |
c56f7a71 FG |
309 | if ($use_master && defined($master_fd = pbs_open_master_pubkey($scfg, $storeid))) { |
310 | my $flags = fcntl($master_fd, F_GETFD, 0) | |
311 | // die "failed to get file descriptor flags: $!\n"; | |
312 | fcntl($master_fd, F_SETFD, $flags & ~FD_CLOEXEC) | |
313 | or die "failed to remove FD_CLOEXEC from master public key file descriptor\n"; | |
314 | push @$cmd, '--master-pubkey-fd='.fileno($master_fd); | |
315 | } | |
76bb5feb WB |
316 | } else { |
317 | push @$cmd, '--crypt-mode=none'; | |
318 | } | |
319 | } | |
320 | ||
271fe394 DM |
321 | push @$cmd, @$param if defined($param); |
322 | ||
53003cb5 | 323 | push @$cmd, "--repository", $repo; |
78eac5ba WB |
324 | if ($client_cmd ne 'status' && defined(my $ns = $scfg->{namespace})) { |
325 | push @$cmd, '--ns', $ns; | |
326 | } | |
271fe394 DM |
327 | |
328 | local $ENV{PBS_PASSWORD} = pbs_get_password($scfg, $storeid); | |
329 | ||
330 | local $ENV{PBS_FINGERPRINT} = $scfg->{fingerprint}; | |
331 | ||
8b4c2a7e DM |
332 | # no ascii-art on task logs |
333 | local $ENV{PROXMOX_OUTPUT_NO_BORDER} = 1; | |
334 | local $ENV{PROXMOX_OUTPUT_NO_HEADER} = 1; | |
335 | ||
271fe394 | 336 | if (my $logfunc = $opts{logfunc}) { |
e6d1edcb | 337 | $logfunc->("run: " . join(' ', @$cmd)); |
271fe394 DM |
338 | } |
339 | ||
340 | run_command($cmd, %opts); | |
341 | } | |
342 | ||
76bb5feb WB |
343 | # FIXME: External perl code should NOT have access to this. |
344 | # | |
345 | # There should be separate functions to | |
346 | # - make backups | |
347 | # - restore backups | |
348 | # - restore files | |
349 | # with a sane API | |
02cc5e10 | 350 | sub run_raw_client_cmd { |
76bb5feb | 351 | my ($scfg, $storeid, $client_cmd, $param, %opts) = @_; |
02cc5e10 | 352 | return do_raw_client_cmd($scfg, $storeid, $client_cmd, $param, %opts); |
76bb5feb WB |
353 | } |
354 | ||
271fe394 DM |
355 | sub run_client_cmd { |
356 | my ($scfg, $storeid, $client_cmd, $param, $no_output) = @_; | |
357 | ||
358 | my $json_str = ''; | |
fee2ece3 | 359 | my $outfunc = sub { $json_str .= "$_[0]\n" }; |
271fe394 DM |
360 | |
361 | $param = [] if !defined($param); | |
362 | $param = [ $param ] if !ref($param); | |
363 | ||
364 | $param = [@$param, '--output-format=json'] if !$no_output; | |
365 | ||
02cc5e10 | 366 | do_raw_client_cmd($scfg, $storeid, $client_cmd, $param, |
76bb5feb | 367 | outfunc => $outfunc, errmsg => 'proxmox-backup-client failed'); |
271fe394 DM |
368 | |
369 | return undef if $no_output; | |
370 | ||
371 | my $res = decode_json($json_str); | |
372 | ||
373 | return $res; | |
374 | } | |
375 | ||
376 | # Storage implementation | |
377 | ||
c855ac15 DM |
378 | sub extract_vzdump_config { |
379 | my ($class, $scfg, $volname, $storeid) = @_; | |
380 | ||
381 | my ($vtype, $name, $vmid, undef, undef, undef, $format) = $class->parse_volname($volname); | |
382 | ||
383 | my $config = ''; | |
fee2ece3 | 384 | my $outfunc = sub { $config .= "$_[0]\n" }; |
c855ac15 DM |
385 | |
386 | my $config_name; | |
387 | if ($format eq 'pbs-vm') { | |
388 | $config_name = 'qemu-server.conf'; | |
389 | } elsif ($format eq 'pbs-ct') { | |
390 | $config_name = 'pct.conf'; | |
391 | } else { | |
392 | die "unable to extract configuration for backup format '$format'\n"; | |
393 | } | |
394 | ||
02cc5e10 | 395 | do_raw_client_cmd($scfg, $storeid, 'restore', [ $name, $config_name, '-' ], |
76bb5feb | 396 | outfunc => $outfunc, errmsg => 'proxmox-backup-client failed'); |
c855ac15 DM |
397 | |
398 | return $config; | |
399 | } | |
400 | ||
8f26b391 FE |
401 | sub prune_backups { |
402 | my ($class, $scfg, $storeid, $keep, $vmid, $type, $dryrun, $logfunc) = @_; | |
403 | ||
404 | $logfunc //= sub { print "$_[1]\n" }; | |
405 | ||
406 | my $backups = $class->list_volumes($storeid, $scfg, $vmid, ['backup']); | |
407 | ||
408 | $type = 'vm' if defined($type) && $type eq 'qemu'; | |
409 | $type = 'ct' if defined($type) && $type eq 'lxc'; | |
410 | ||
411 | my $backup_groups = {}; | |
412 | foreach my $backup (@{$backups}) { | |
413 | (my $backup_type = $backup->{format}) =~ s/^pbs-//; | |
414 | ||
415 | next if defined($type) && $backup_type ne $type; | |
416 | ||
417 | my $backup_group = "$backup_type/$backup->{vmid}"; | |
418 | $backup_groups->{$backup_group} = 1; | |
419 | } | |
420 | ||
421 | my @param; | |
1b87f013 FE |
422 | |
423 | my $keep_all = delete $keep->{'keep-all'}; | |
424 | ||
425 | if (!$keep_all) { | |
426 | foreach my $opt (keys %{$keep}) { | |
427 | next if $keep->{$opt} == 0; | |
428 | push @param, "--$opt"; | |
429 | push @param, "$keep->{$opt}"; | |
430 | } | |
431 | } else { # no need to pass anything to PBS | |
432 | $keep = { 'keep-all' => 1 }; | |
8f26b391 FE |
433 | } |
434 | ||
435 | push @param, '--dry-run' if $dryrun; | |
436 | ||
437 | my $prune_list = []; | |
438 | my $failed; | |
439 | ||
440 | foreach my $backup_group (keys %{$backup_groups}) { | |
441 | $logfunc->('info', "running 'proxmox-backup-client prune' for '$backup_group'") | |
442 | if !$dryrun; | |
443 | eval { | |
444 | my $res = run_client_cmd($scfg, $storeid, 'prune', [ $backup_group, @param ]); | |
445 | ||
446 | foreach my $backup (@{$res}) { | |
447 | die "result from proxmox-backup-client is not as expected\n" | |
448 | if !defined($backup->{'backup-time'}) | |
449 | || !defined($backup->{'backup-type'}) | |
450 | || !defined($backup->{'backup-id'}) | |
451 | || !defined($backup->{'keep'}); | |
452 | ||
453 | my $ctime = $backup->{'backup-time'}; | |
454 | my $type = $backup->{'backup-type'}; | |
455 | my $vmid = $backup->{'backup-id'}; | |
456 | my $volid = print_volid($storeid, $type, $vmid, $ctime); | |
457 | ||
bfba9626 FE |
458 | my $mark = $backup->{keep} ? 'keep' : 'remove'; |
459 | $mark = 'protected' if $backup->{protected}; | |
460 | ||
8f26b391 FE |
461 | push @{$prune_list}, { |
462 | ctime => $ctime, | |
bfba9626 | 463 | mark => $mark, |
8f26b391 FE |
464 | type => $type eq 'vm' ? 'qemu' : 'lxc', |
465 | vmid => $vmid, | |
466 | volid => $volid, | |
467 | }; | |
468 | } | |
469 | }; | |
470 | if (my $err = $@) { | |
471 | $logfunc->('err', "prune '$backup_group': $err\n"); | |
472 | $failed = 1; | |
473 | } | |
474 | } | |
475 | die "error pruning backups - check log\n" if $failed; | |
476 | ||
477 | return $prune_list; | |
478 | } | |
479 | ||
1aeb322b TL |
480 | my $autogen_encryption_key = sub { |
481 | my ($scfg, $storeid) = @_; | |
482 | my $encfile = pbs_encryption_key_file_name($scfg, $storeid); | |
478609d3 TL |
483 | if (-f $encfile) { |
484 | rename $encfile, "$encfile.old"; | |
485 | } | |
4558cb6e TL |
486 | my $cmd = ['proxmox-backup-client', 'key', 'create', '--kdf', 'none', $encfile]; |
487 | run_command($cmd, errmsg => 'failed to create encryption key'); | |
488 | return PVE::Tools::file_get_contents($encfile); | |
1aeb322b TL |
489 | }; |
490 | ||
271fe394 DM |
491 | sub on_add_hook { |
492 | my ($class, $storeid, $scfg, %param) = @_; | |
493 | ||
0b6b98d1 TL |
494 | my $res = {}; |
495 | ||
76bb5feb WB |
496 | if (defined(my $password = $param{password})) { |
497 | pbs_set_password($scfg, $storeid, $password); | |
b494636a DM |
498 | } else { |
499 | pbs_delete_password($scfg, $storeid); | |
500 | } | |
76bb5feb | 501 | |
ce2e2733 | 502 | if (defined(my $encryption_key = $param{'encryption-key'})) { |
d2c47b38 | 503 | my $decoded_key; |
1aeb322b | 504 | if ($encryption_key eq 'autogen') { |
0b6b98d1 | 505 | $res->{'encryption-key'} = $autogen_encryption_key->($scfg, $storeid); |
3cc2eb73 | 506 | $decoded_key = decode_json($res->{'encryption-key'}); |
1aeb322b | 507 | } else { |
d2c47b38 TL |
508 | $decoded_key = eval { decode_json($encryption_key) }; |
509 | if ($@ || !exists($decoded_key->{data})) { | |
510 | die "Value does not seems like a valid, JSON formatted encryption key!\n"; | |
511 | } | |
1aeb322b | 512 | pbs_set_encryption_key($scfg, $storeid, $encryption_key); |
0b6b98d1 | 513 | $res->{'encryption-key'} = $encryption_key; |
1aeb322b | 514 | } |
3cc2eb73 | 515 | $scfg->{'encryption-key'} = $decoded_key->{fingerprint} || 1; |
76bb5feb WB |
516 | } else { |
517 | pbs_delete_encryption_key($scfg, $storeid); | |
518 | } | |
0b6b98d1 | 519 | |
c56f7a71 FG |
520 | if (defined(my $master_key = delete $param{'master-pubkey'})) { |
521 | die "'master-pubkey' can only be used together with 'encryption-key'\n" | |
522 | if !defined($scfg->{'encryption-key'}); | |
523 | ||
524 | my $decoded = decode_base64($master_key); | |
525 | pbs_set_master_pubkey($scfg, $storeid, $decoded); | |
526 | $scfg->{'master-pubkey'} = 1; | |
527 | } else { | |
528 | pbs_delete_master_pubkey($scfg, $storeid); | |
529 | } | |
530 | ||
0b6b98d1 | 531 | return $res; |
b494636a DM |
532 | } |
533 | ||
534 | sub on_update_hook { | |
535 | my ($class, $storeid, $scfg, %param) = @_; | |
536 | ||
0b6b98d1 TL |
537 | my $res = {}; |
538 | ||
76bb5feb WB |
539 | if (exists($param{password})) { |
540 | if (defined($param{password})) { | |
541 | pbs_set_password($scfg, $storeid, $param{password}); | |
542 | } else { | |
543 | pbs_delete_password($scfg, $storeid); | |
544 | } | |
545 | } | |
b494636a | 546 | |
ce2e2733 TL |
547 | if (exists($param{'encryption-key'})) { |
548 | if (defined(my $encryption_key = delete($param{'encryption-key'}))) { | |
d2c47b38 | 549 | my $decoded_key; |
1aeb322b | 550 | if ($encryption_key eq 'autogen') { |
0b6b98d1 | 551 | $res->{'encryption-key'} = $autogen_encryption_key->($scfg, $storeid); |
3cc2eb73 | 552 | $decoded_key = decode_json($res->{'encryption-key'}); |
1aeb322b | 553 | } else { |
d2c47b38 TL |
554 | $decoded_key = eval { decode_json($encryption_key) }; |
555 | if ($@ || !exists($decoded_key->{data})) { | |
556 | die "Value does not seems like a valid, JSON formatted encryption key!\n"; | |
557 | } | |
1aeb322b | 558 | pbs_set_encryption_key($scfg, $storeid, $encryption_key); |
0b6b98d1 | 559 | $res->{'encryption-key'} = $encryption_key; |
1aeb322b | 560 | } |
3cc2eb73 | 561 | $scfg->{'encryption-key'} = $decoded_key->{fingerprint} || 1; |
76bb5feb WB |
562 | } else { |
563 | pbs_delete_encryption_key($scfg, $storeid); | |
c56f7a71 FG |
564 | delete $scfg->{'encryption-key'}; |
565 | } | |
566 | } | |
567 | ||
568 | if (exists($param{'master-pubkey'})) { | |
569 | if (defined(my $master_key = delete($param{'master-pubkey'}))) { | |
570 | my $decoded = decode_base64($master_key); | |
571 | ||
572 | pbs_set_master_pubkey($scfg, $storeid, $decoded); | |
573 | $scfg->{'master-pubkey'} = 1; | |
574 | } else { | |
575 | pbs_delete_master_pubkey($scfg, $storeid); | |
76bb5feb | 576 | } |
271fe394 | 577 | } |
0b6b98d1 TL |
578 | |
579 | return $res; | |
271fe394 DM |
580 | } |
581 | ||
582 | sub on_delete_hook { | |
583 | my ($class, $storeid, $scfg) = @_; | |
584 | ||
585 | pbs_delete_password($scfg, $storeid); | |
76bb5feb | 586 | pbs_delete_encryption_key($scfg, $storeid); |
c56f7a71 | 587 | pbs_delete_master_pubkey($scfg, $storeid); |
f3ccd0ef FE |
588 | |
589 | return; | |
271fe394 DM |
590 | } |
591 | ||
592 | sub parse_volname { | |
593 | my ($class, $volname) = @_; | |
594 | ||
595 | 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)$!) { | |
596 | my $btype = $1; | |
597 | my $bid = $2; | |
598 | my $btime = $3; | |
599 | my $format = "pbs-$btype"; | |
600 | ||
601 | my $name = "$btype/$bid/$btime"; | |
602 | ||
603 | if ($bid =~ m/^\d+$/) { | |
604 | return ('backup', $name, $bid, undef, undef, undef, $format); | |
605 | } else { | |
606 | return ('backup', $name, undef, undef, undef, undef, $format); | |
607 | } | |
608 | } | |
609 | ||
610 | die "unable to parse PBS volume name '$volname'\n"; | |
611 | } | |
612 | ||
613 | sub path { | |
614 | my ($class, $scfg, $volname, $storeid, $snapname) = @_; | |
615 | ||
616 | die "volume snapshot is not possible on pbs storage" | |
617 | if defined($snapname); | |
618 | ||
619 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
620 | ||
53003cb5 | 621 | my $repo = PVE::PBSClient::get_repository($scfg); |
271fe394 | 622 | |
ffc31266 | 623 | # artificial url - we currently do not use that anywhere |
53003cb5 | 624 | my $path = "pbs://$repo/$name"; |
78eac5ba WB |
625 | if (defined(my $ns = $scfg->{namespace})) { |
626 | $ns =~ s|/|%2f|g; # other characters to escape aren't allowed in the namespace schema | |
627 | $path .= "?ns=$ns"; | |
628 | } | |
271fe394 DM |
629 | |
630 | return ($path, $vmid, $vtype); | |
631 | } | |
632 | ||
633 | sub create_base { | |
634 | my ($class, $storeid, $scfg, $volname) = @_; | |
635 | ||
636 | die "can't create base images in pbs storage\n"; | |
637 | } | |
638 | ||
639 | sub clone_image { | |
640 | my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_; | |
641 | ||
642 | die "can't clone images in pbs storage\n"; | |
643 | } | |
644 | ||
645 | sub alloc_image { | |
646 | my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; | |
647 | ||
648 | die "can't allocate space in pbs storage\n"; | |
649 | } | |
650 | ||
651 | sub free_image { | |
652 | my ($class, $storeid, $scfg, $volname, $isBase) = @_; | |
653 | ||
654 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
655 | ||
656 | run_client_cmd($scfg, $storeid, "forget", [ $name ], 1); | |
7ae13a34 FE |
657 | |
658 | return; | |
271fe394 DM |
659 | } |
660 | ||
661 | ||
662 | sub list_images { | |
663 | my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_; | |
664 | ||
665 | my $res = []; | |
666 | ||
667 | return $res; | |
668 | } | |
669 | ||
878fe017 TL |
670 | my sub snapshot_files_encrypted { |
671 | my ($files) = @_; | |
672 | return 0 if !$files; | |
673 | ||
674 | my $any; | |
675 | my $all = 1; | |
676 | for my $file (@$files) { | |
677 | my $fn = $file->{filename}; | |
678 | next if $fn eq 'client.log.blob' || $fn eq 'index.json.blob'; | |
679 | ||
680 | my $crypt = $file->{'crypt-mode'}; | |
681 | ||
682 | $all = 0 if !$crypt || $crypt ne 'encrypt'; | |
dfa374d3 | 683 | $any ||= defined($crypt) && $crypt eq 'encrypt'; |
878fe017 TL |
684 | } |
685 | return $any && $all; | |
686 | } | |
687 | ||
af07f67a WB |
688 | # TODO: use a client with native rust/proxmox-backup bindings to profit from |
689 | # API schema checks and types | |
690 | my sub pbs_api_connect { | |
691 | my ($scfg, $password) = @_; | |
692 | ||
693 | my $params = {}; | |
694 | ||
695 | my $user = $scfg->{username} // 'root@pam'; | |
696 | ||
697 | if (my $tokenid = PVE::AccessControl::pve_verify_tokenid($user, 1)) { | |
698 | $params->{apitoken} = "PBSAPIToken=${tokenid}:${password}"; | |
699 | } else { | |
700 | $params->{password} = $password; | |
701 | $params->{username} = $user; | |
702 | } | |
703 | ||
704 | if (my $fp = $scfg->{fingerprint}) { | |
705 | $params->{cached_fingerprints}->{uc($fp)} = 1; | |
706 | } | |
707 | ||
708 | my $conn = PVE::APIClient::LWP->new( | |
709 | %$params, | |
710 | host => $scfg->{server}, | |
711 | port => $scfg->{port} // 8007, | |
712 | timeout => 7, # cope with a 401 (3s api delay) and high latency | |
713 | cookie_name => 'PBSAuthCookie', | |
714 | ); | |
715 | ||
716 | return $conn; | |
717 | } | |
718 | ||
271fe394 DM |
719 | sub list_volumes { |
720 | my ($class, $storeid, $scfg, $vmid, $content_types) = @_; | |
721 | ||
722 | my $res = []; | |
723 | ||
724 | return $res if !grep { $_ eq 'backup' } @$content_types; | |
725 | ||
af07f67a WB |
726 | my $password = pbs_get_password($scfg, $storeid); |
727 | my $conn = pbs_api_connect($scfg, $password); | |
728 | my $datastore = $scfg->{datastore}; | |
729 | ||
730 | my $param = {}; | |
731 | $param->{'backup-id'} = "$vmid" if defined($vmid); | |
ad3666d7 | 732 | $param->{'ns'} = "$scfg->{namespace}" if defined($scfg->{namespace}); |
af07f67a WB |
733 | my $data = eval { $conn->get("/api2/json/admin/datastore/$datastore/snapshots", $param); }; |
734 | die "error listing snapshots - $@" if $@; | |
271fe394 DM |
735 | |
736 | foreach my $item (@$data) { | |
737 | my $btype = $item->{"backup-type"}; | |
738 | my $bid = $item->{"backup-id"}; | |
545e127e | 739 | my $epoch = $item->{"backup-time"}; |
271fe394 DM |
740 | my $size = $item->{size} // 1; |
741 | ||
742 | next if !($btype eq 'vm' || $btype eq 'ct'); | |
743 | next if $bid !~ m/^\d+$/; | |
ddf7fdaa | 744 | next if defined($vmid) && $bid ne $vmid; |
271fe394 | 745 | |
8602fd56 | 746 | my $volid = print_volid($storeid, $btype, $bid, $epoch); |
271fe394 | 747 | |
545e127e | 748 | my $info = { |
c05b1a8c TL |
749 | volid => $volid, |
750 | format => "pbs-$btype", | |
751 | size => $size, | |
752 | content => 'backup', | |
753 | vmid => int($bid), | |
754 | ctime => $epoch, | |
c66e0b8a | 755 | subtype => $btype eq 'vm' ? 'qemu' : 'lxc', # convert to PVE backup type |
545e127e | 756 | }; |
271fe394 | 757 | |
9778e5c2 | 758 | $info->{verification} = $item->{verification} if defined($item->{verification}); |
6fef456c | 759 | $info->{notes} = $item->{comment} if defined($item->{comment}); |
bfba9626 | 760 | $info->{protected} = 1 if $item->{protected}; |
878fe017 TL |
761 | if (defined($item->{fingerprint})) { |
762 | $info->{encrypted} = $item->{fingerprint}; | |
763 | } elsif (snapshot_files_encrypted($item->{files})) { | |
764 | $info->{encrypted} = '1'; | |
765 | } | |
9778e5c2 | 766 | |
271fe394 DM |
767 | push @$res, $info; |
768 | } | |
769 | ||
770 | return $res; | |
771 | } | |
772 | ||
773 | sub status { | |
774 | my ($class, $storeid, $scfg, $cache) = @_; | |
775 | ||
776 | my $total = 0; | |
777 | my $free = 0; | |
778 | my $used = 0; | |
779 | my $active = 0; | |
780 | ||
781 | eval { | |
782 | my $res = run_client_cmd($scfg, $storeid, "status"); | |
783 | ||
784 | $active = 1; | |
785 | $total = $res->{total}; | |
786 | $used = $res->{used}; | |
f155c912 TL |
787 | $free = $res->{avail}; |
788 | }; | |
271fe394 DM |
789 | if (my $err = $@) { |
790 | warn $err; | |
791 | } | |
792 | ||
793 | return ($total, $free, $used, $active); | |
794 | } | |
795 | ||
8b62ac6a TL |
796 | # can also be used for not (yet) added storages, pass $scfg with |
797 | # { | |
798 | # server | |
799 | # user | |
800 | # port (optional default to 8007) | |
801 | # fingerprint (optional for trusted certs) | |
802 | # } | |
803 | sub scan_datastores { | |
804 | my ($scfg, $password) = @_; | |
805 | ||
806 | my $conn = pbs_api_connect($scfg, $password); | |
807 | ||
808 | my $response = eval { $conn->get('/api2/json/admin/datastore', {}) }; | |
809 | die "error fetching datastores - $@" if $@; | |
810 | ||
811 | return $response; | |
812 | } | |
813 | ||
271fe394 DM |
814 | sub activate_storage { |
815 | my ($class, $storeid, $scfg, $cache) = @_; | |
bb0a0f96 | 816 | |
2cd10f58 | 817 | my $password = pbs_get_password($scfg, $storeid); |
bb0a0f96 | 818 | |
2cd10f58 TL |
819 | my $datastores = eval { scan_datastores($scfg, $password) }; |
820 | die "$storeid: $@" if $@; | |
821 | ||
822 | my $datastore = $scfg->{datastore}; | |
823 | ||
824 | for my $ds (@$datastores) { | |
825 | if ($ds->{store} eq $datastore) { | |
826 | return 1; | |
827 | } | |
828 | } | |
829 | ||
ffc31266 | 830 | die "$storeid: Cannot find datastore '$datastore', check permissions and existence!\n"; |
271fe394 DM |
831 | } |
832 | ||
833 | sub deactivate_storage { | |
834 | my ($class, $storeid, $scfg, $cache) = @_; | |
835 | return 1; | |
836 | } | |
837 | ||
838 | sub activate_volume { | |
839 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; | |
840 | ||
841 | die "volume snapshot is not possible on pbs device" if $snapname; | |
842 | ||
843 | return 1; | |
844 | } | |
845 | ||
846 | sub deactivate_volume { | |
847 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; | |
848 | ||
849 | die "volume snapshot is not possible on pbs device" if $snapname; | |
850 | ||
851 | return 1; | |
852 | } | |
853 | ||
f1de8281 FE |
854 | # FIXME remove on the next APIAGE reset. |
855 | # Deprecated, use get_volume_attribute instead. | |
45e93e6d DC |
856 | sub get_volume_notes { |
857 | my ($class, $scfg, $storeid, $volname, $timeout) = @_; | |
858 | ||
859 | my (undef, $name, undef, undef, undef, undef, $format) = $class->parse_volname($volname); | |
860 | ||
861 | my $data = run_client_cmd($scfg, $storeid, "snapshot", [ "notes", "show", $name ]); | |
862 | ||
863 | return $data->{notes}; | |
864 | } | |
865 | ||
f1de8281 FE |
866 | # FIXME remove on the next APIAGE reset. |
867 | # Deprecated, use update_volume_attribute instead. | |
45e93e6d DC |
868 | sub update_volume_notes { |
869 | my ($class, $scfg, $storeid, $volname, $notes, $timeout) = @_; | |
870 | ||
871 | my (undef, $name, undef, undef, undef, undef, $format) = $class->parse_volname($volname); | |
872 | ||
873 | run_client_cmd($scfg, $storeid, "snapshot", [ "notes", "update", $name, $notes ], 1); | |
874 | ||
875 | return undef; | |
876 | } | |
877 | ||
f1de8281 FE |
878 | sub get_volume_attribute { |
879 | my ($class, $scfg, $storeid, $volname, $attribute) = @_; | |
880 | ||
881 | if ($attribute eq 'notes') { | |
882 | return $class->get_volume_notes($scfg, $storeid, $volname); | |
883 | } | |
884 | ||
bfba9626 | 885 | if ($attribute eq 'protected') { |
78eac5ba | 886 | my $param = api_param_from_volname($class, $scfg, $volname); |
bfba9626 FE |
887 | |
888 | my $password = pbs_get_password($scfg, $storeid); | |
889 | my $conn = pbs_api_connect($scfg, $password); | |
890 | my $datastore = $scfg->{datastore}; | |
891 | ||
892 | my $res = eval { $conn->get("/api2/json/admin/datastore/$datastore/$attribute", $param); }; | |
893 | if (my $err = $@) { | |
894 | return if $err->{code} == 404; # not supported | |
895 | die $err; | |
896 | } | |
897 | return $res; | |
898 | } | |
899 | ||
f1de8281 FE |
900 | return; |
901 | } | |
902 | ||
903 | sub update_volume_attribute { | |
904 | my ($class, $scfg, $storeid, $volname, $attribute, $value) = @_; | |
905 | ||
906 | if ($attribute eq 'notes') { | |
907 | return $class->update_volume_notes($scfg, $storeid, $volname, $value); | |
908 | } | |
909 | ||
bfba9626 | 910 | if ($attribute eq 'protected') { |
78eac5ba | 911 | my $param = api_param_from_volname($class, $scfg, $volname); |
bfba9626 FE |
912 | $param->{$attribute} = $value; |
913 | ||
914 | my $password = pbs_get_password($scfg, $storeid); | |
915 | my $conn = pbs_api_connect($scfg, $password); | |
916 | my $datastore = $scfg->{datastore}; | |
917 | ||
904f06a8 FE |
918 | eval { $conn->put("/api2/json/admin/datastore/$datastore/$attribute", $param); }; |
919 | if (my $err = $@) { | |
920 | die "Server is not recent enough to support feature '$attribute'\n" | |
921 | if $err->{code} == 404; | |
922 | die $err; | |
923 | } | |
bfba9626 FE |
924 | return; |
925 | } | |
926 | ||
f1de8281 FE |
927 | die "attribute '$attribute' is not supported for storage type '$scfg->{type}'\n"; |
928 | } | |
929 | ||
271fe394 DM |
930 | sub volume_size_info { |
931 | my ($class, $scfg, $storeid, $volname, $timeout) = @_; | |
932 | ||
933 | my ($vtype, $name, undef, undef, undef, undef, $format) = $class->parse_volname($volname); | |
934 | ||
935 | my $data = run_client_cmd($scfg, $storeid, "files", [ $name ]); | |
936 | ||
937 | my $size = 0; | |
938 | foreach my $info (@$data) { | |
d4e00f2b | 939 | if ($info->{size} && $info->{size} =~ /^(\d+)$/) { # untaints |
ac598d85 SI |
940 | $size += $1; |
941 | } | |
271fe394 DM |
942 | } |
943 | ||
944 | my $used = $size; | |
945 | ||
946 | return wantarray ? ($size, $format, $used, undef) : $size; | |
947 | } | |
948 | ||
949 | sub volume_resize { | |
950 | my ($class, $scfg, $storeid, $volname, $size, $running) = @_; | |
951 | die "volume resize is not possible on pbs device"; | |
952 | } | |
953 | ||
954 | sub volume_snapshot { | |
955 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
956 | die "volume snapshot is not possible on pbs device"; | |
957 | } | |
958 | ||
959 | sub volume_snapshot_rollback { | |
960 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
961 | die "volume snapshot rollback is not possible on pbs device"; | |
962 | } | |
963 | ||
964 | sub volume_snapshot_delete { | |
965 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
966 | die "volume snapshot delete is not possible on pbs device"; | |
967 | } | |
968 | ||
969 | sub volume_has_feature { | |
970 | my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_; | |
971 | ||
972 | return undef; | |
973 | } | |
974 | ||
975 | 1; |