]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/PBSPlugin.pm
pbs: encryption support, split "raw client command" API
[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.",
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 fingerprint => { optional => 1 },
59 };
60 }
61
62 # Helpers
63
64 sub pbs_password_file_name {
65 my ($scfg, $storeid) = @_;
66
67 return "/etc/pve/priv/storage/${storeid}.pw";
68 }
69
70 sub pbs_set_password {
71 my ($scfg, $storeid, $password) = @_;
72
73 my $pwfile = pbs_password_file_name($scfg, $storeid);
74 mkdir "/etc/pve/priv/storage";
75
76 PVE::Tools::file_set_contents($pwfile, "$password\n");
77 }
78
79 sub pbs_delete_password {
80 my ($scfg, $storeid) = @_;
81
82 my $pwfile = pbs_password_file_name($scfg, $storeid);
83
84 unlink $pwfile;
85 }
86
87 sub pbs_get_password {
88 my ($scfg, $storeid) = @_;
89
90 my $pwfile = pbs_password_file_name($scfg, $storeid);
91
92 return PVE::Tools::file_read_firstline($pwfile);
93 }
94
95 sub pbs_encryption_key_file_name {
96 my ($scfg, $storeid) = @_;
97
98 return "/etc/pve/priv/storage/${storeid}.enc";
99 }
100
101 sub pbs_set_encryption_key {
102 my ($scfg, $storeid, $key) = @_;
103
104 my $pwfile = pbs_encryption_key_file_name($scfg, $storeid);
105 mkdir "/etc/pve/priv/storage";
106
107 PVE::Tools::file_set_contents($pwfile, "$key\n");
108 }
109
110 sub pbs_delete_encryption_key {
111 my ($scfg, $storeid) = @_;
112
113 my $pwfile = pbs_encryption_key_file_name($scfg, $storeid);
114
115 unlink $pwfile;
116 }
117
118 sub pbs_get_encryption_key {
119 my ($scfg, $storeid) = @_;
120
121 my $pwfile = pbs_encryption_key_file_name($scfg, $storeid);
122
123 return PVE::Tools::file_get_contents($pwfile);
124 }
125
126 # Returns a file handle if there is an encryption key, or `undef` if there is not. Dies on error.
127 sub pbs_open_encryption_key {
128 my ($scfg, $storeid) = @_;
129
130 my $encryption_key_file = pbs_encryption_key_file_name($scfg, $storeid);
131
132 my $keyfd;
133 if (!open($keyfd, '<', $encryption_key_file)) {
134 return undef if $! == ENOENT;
135 die "failed to open encryption key: $encryption_key_file: $!\n";
136 }
137
138 return $keyfd;
139 }
140
141 sub print_volid {
142 my ($storeid, $btype, $bid, $btime) = @_;
143
144 my $time_str = strftime("%FT%TZ", gmtime($btime));
145 my $volname = "backup/${btype}/${bid}/${time_str}";
146
147 return "${storeid}:${volname}";
148 }
149
150 my sub do_raw_client_cmd {
151 my ($scfg, $storeid, $client_cmd, $param, $can_encrypt, %opts) = @_;
152
153 my $client_exe = '/usr/bin/proxmox-backup-client';
154 die "executable not found '$client_exe'! Proxmox backup client not installed?\n"
155 if ! -x $client_exe;
156
157 my $server = $scfg->{server};
158 my $datastore = $scfg->{datastore};
159 my $username = $scfg->{username} // 'root@pam';
160
161 my $userns_cmd = delete $opts{userns_cmd};
162
163 my $cmd = [];
164
165 push @$cmd, @$userns_cmd if defined($userns_cmd);
166
167 push @$cmd, $client_exe, $client_cmd;
168
169 # This must live in the top scope to not get closed before the `run_command`
170 my $keyfd;
171 if ($can_encrypt) {
172 if (defined($keyfd = pbs_open_encryption_key($scfg, $storeid))) {
173 my $flags = fcntl($keyfd, F_GETFD, 0)
174 // die "failed to get file descriptor flags: $!\n";
175 fcntl($keyfd, F_SETFD, $flags & ~FD_CLOEXEC)
176 or die "failed to remove FD_CLOEXEC from encryption key file descriptor\n";
177 push @$cmd, '--crypt-mode=encrypt', '--keyfd='.fileno($keyfd);
178 } else {
179 push @$cmd, '--crypt-mode=none';
180 }
181 }
182
183 push @$cmd, @$param if defined($param);
184
185 push @$cmd, "--repository", "$username\@$server:$datastore";
186
187 local $ENV{PBS_PASSWORD} = pbs_get_password($scfg, $storeid);
188
189 local $ENV{PBS_FINGERPRINT} = $scfg->{fingerprint};
190
191 # no ascii-art on task logs
192 local $ENV{PROXMOX_OUTPUT_NO_BORDER} = 1;
193 local $ENV{PROXMOX_OUTPUT_NO_HEADER} = 1;
194
195 if (my $logfunc = $opts{logfunc}) {
196 $logfunc->("run: " . join(' ', @$cmd));
197 }
198
199 run_command($cmd, %opts);
200 }
201
202 # FIXME: External perl code should NOT have access to this.
203 #
204 # There should be separate functions to
205 # - make backups
206 # - restore backups
207 # - restore files
208 # with a sane API
209 sub run_raw_client_cmd{
210 my ($scfg, $storeid, $client_cmd, $param, %opts) = @_;
211 return do_raw_client_cmd($scfg, $storeid, $client_cmd, $param, 1, %opts);
212 }
213
214 sub run_client_cmd {
215 my ($scfg, $storeid, $client_cmd, $param, $no_output) = @_;
216
217 my $json_str = '';
218 my $outfunc = sub { $json_str .= "$_[0]\n" };
219
220 $param = [] if !defined($param);
221 $param = [ $param ] if !ref($param);
222
223 $param = [@$param, '--output-format=json'] if !$no_output;
224
225 do_raw_client_cmd($scfg, $storeid, $client_cmd, $param, 0,
226 outfunc => $outfunc, errmsg => 'proxmox-backup-client failed');
227
228 return undef if $no_output;
229
230 my $res = decode_json($json_str);
231
232 return $res;
233 }
234
235 # Storage implementation
236
237 sub extract_vzdump_config {
238 my ($class, $scfg, $volname, $storeid) = @_;
239
240 my ($vtype, $name, $vmid, undef, undef, undef, $format) = $class->parse_volname($volname);
241
242 my $config = '';
243 my $outfunc = sub { $config .= "$_[0]\n" };
244
245 my $config_name;
246 if ($format eq 'pbs-vm') {
247 $config_name = 'qemu-server.conf';
248 } elsif ($format eq 'pbs-ct') {
249 $config_name = 'pct.conf';
250 } else {
251 die "unable to extract configuration for backup format '$format'\n";
252 }
253
254 do_raw_client_cmd($scfg, $storeid, 'restore', [ $name, $config_name, '-' ], 0,
255 outfunc => $outfunc, errmsg => 'proxmox-backup-client failed');
256
257 return $config;
258 }
259
260 sub on_add_hook {
261 my ($class, $storeid, $scfg, %param) = @_;
262
263 if (defined(my $password = $param{password})) {
264 pbs_set_password($scfg, $storeid, $password);
265 } else {
266 pbs_delete_password($scfg, $storeid);
267 }
268
269 if (defined(my $encryption_key = delete($scfg->{encryption_key}))) {
270 pbs_set_encryption_key($scfg, $storeid, $encryption_key);
271 } else {
272 pbs_delete_encryption_key($scfg, $storeid);
273 }
274 }
275
276 sub on_update_hook {
277 my ($class, $storeid, $scfg, %param) = @_;
278
279 if (exists($param{password})) {
280 if (defined($param{password})) {
281 pbs_set_password($scfg, $storeid, $param{password});
282 } else {
283 pbs_delete_password($scfg, $storeid);
284 }
285 }
286
287 if (exists($scfg->{encryption_key})) {
288 if (defined(my $encryption_key = delete($scfg->{encryption_key}))) {
289 pbs_set_encryption_key($scfg, $storeid, $encryption_key);
290 } else {
291 pbs_delete_encryption_key($scfg, $storeid);
292 }
293 }
294 }
295
296 sub on_delete_hook {
297 my ($class, $storeid, $scfg) = @_;
298
299 pbs_delete_password($scfg, $storeid);
300 pbs_delete_encryption_key($scfg, $storeid);
301 }
302
303 sub parse_volname {
304 my ($class, $volname) = @_;
305
306 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)$!) {
307 my $btype = $1;
308 my $bid = $2;
309 my $btime = $3;
310 my $format = "pbs-$btype";
311
312 my $name = "$btype/$bid/$btime";
313
314 if ($bid =~ m/^\d+$/) {
315 return ('backup', $name, $bid, undef, undef, undef, $format);
316 } else {
317 return ('backup', $name, undef, undef, undef, undef, $format);
318 }
319 }
320
321 die "unable to parse PBS volume name '$volname'\n";
322 }
323
324 sub path {
325 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
326
327 die "volume snapshot is not possible on pbs storage"
328 if defined($snapname);
329
330 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
331
332 my $server = $scfg->{server};
333 my $datastore = $scfg->{datastore};
334 my $username = $scfg->{username} // 'root@pam';
335
336 # artifical url - we currently do not use that anywhere
337 my $path = "pbs://$username\@$server:$datastore/$name";
338
339 return ($path, $vmid, $vtype);
340 }
341
342 sub create_base {
343 my ($class, $storeid, $scfg, $volname) = @_;
344
345 die "can't create base images in pbs storage\n";
346 }
347
348 sub clone_image {
349 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
350
351 die "can't clone images in pbs storage\n";
352 }
353
354 sub alloc_image {
355 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
356
357 die "can't allocate space in pbs storage\n";
358 }
359
360 sub free_image {
361 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
362
363 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
364
365 run_client_cmd($scfg, $storeid, "forget", [ $name ], 1);
366 }
367
368
369 sub list_images {
370 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
371
372 my $res = [];
373
374 return $res;
375 }
376
377 sub list_volumes {
378 my ($class, $storeid, $scfg, $vmid, $content_types) = @_;
379
380 my $res = [];
381
382 return $res if !grep { $_ eq 'backup' } @$content_types;
383
384 my $data = run_client_cmd($scfg, $storeid, "snapshots");
385
386 foreach my $item (@$data) {
387 my $btype = $item->{"backup-type"};
388 my $bid = $item->{"backup-id"};
389 my $epoch = $item->{"backup-time"};
390 my $size = $item->{size} // 1;
391
392 next if !($btype eq 'vm' || $btype eq 'ct');
393 next if $bid !~ m/^\d+$/;
394 next if defined($vmid) && $bid ne $vmid;
395
396 my $volid = print_volid($storeid, $btype, $bid, $epoch);
397
398 my $info = {
399 volid => $volid,
400 format => "pbs-$btype",
401 size => $size,
402 content => 'backup',
403 vmid => int($bid),
404 ctime => $epoch,
405 };
406
407 push @$res, $info;
408 }
409
410 return $res;
411 }
412
413 sub status {
414 my ($class, $storeid, $scfg, $cache) = @_;
415
416 my $total = 0;
417 my $free = 0;
418 my $used = 0;
419 my $active = 0;
420
421 eval {
422 my $res = run_client_cmd($scfg, $storeid, "status");
423
424 $active = 1;
425 $total = $res->{total};
426 $used = $res->{used};
427 $free = $res->{avail};
428 };
429 if (my $err = $@) {
430 warn $err;
431 }
432
433 return ($total, $free, $used, $active);
434 }
435
436 sub activate_storage {
437 my ($class, $storeid, $scfg, $cache) = @_;
438 return 1;
439 }
440
441 sub deactivate_storage {
442 my ($class, $storeid, $scfg, $cache) = @_;
443 return 1;
444 }
445
446 sub activate_volume {
447 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
448
449 die "volume snapshot is not possible on pbs device" if $snapname;
450
451 return 1;
452 }
453
454 sub deactivate_volume {
455 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
456
457 die "volume snapshot is not possible on pbs device" if $snapname;
458
459 return 1;
460 }
461
462 sub volume_size_info {
463 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
464
465 my ($vtype, $name, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
466
467 my $data = run_client_cmd($scfg, $storeid, "files", [ $name ]);
468
469 my $size = 0;
470 foreach my $info (@$data) {
471 $size += $info->{size} if $info->{size};
472 }
473
474 my $used = $size;
475
476 return wantarray ? ($size, $format, $used, undef) : $size;
477 }
478
479 sub volume_resize {
480 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
481 die "volume resize is not possible on pbs device";
482 }
483
484 sub volume_snapshot {
485 my ($class, $scfg, $storeid, $volname, $snap) = @_;
486 die "volume snapshot is not possible on pbs device";
487 }
488
489 sub volume_snapshot_rollback {
490 my ($class, $scfg, $storeid, $volname, $snap) = @_;
491 die "volume snapshot rollback is not possible on pbs device";
492 }
493
494 sub volume_snapshot_delete {
495 my ($class, $scfg, $storeid, $volname, $snap) = @_;
496 die "volume snapshot delete is not possible on pbs device";
497 }
498
499 sub volume_has_feature {
500 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
501
502 return undef;
503 }
504
505 1;