1 package PVE
::Storage
::PBSPlugin
;
3 # Plugin to access Proxmox Backup Server
7 use Fcntl
qw(F_GETFD F_SETFD FD_CLOEXEC);
12 use POSIX
qw(strftime ENOENT);
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);
18 use base
qw(PVE::Storage::Plugin);
28 content
=> [ {backup
=> 1, none
=> 1}, { backup
=> 1 }],
35 description
=> "Proxmox backup server datastore name.",
38 # openssl s_client -connect <host>:8007 2>&1 |openssl x509 -fingerprint -sha256
39 fingerprint
=> get_standard_option
('fingerprint-sha256'),
41 description
=> "Encryption key. Use 'autogen' to generate one automatically without passphrase.",
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 },
64 sub pbs_password_file_name
{
65 my ($scfg, $storeid) = @_;
67 return "/etc/pve/priv/storage/${storeid}.pw";
70 sub pbs_set_password
{
71 my ($scfg, $storeid, $password) = @_;
73 my $pwfile = pbs_password_file_name
($scfg, $storeid);
74 mkdir "/etc/pve/priv/storage";
76 PVE
::Tools
::file_set_contents
($pwfile, "$password\n");
79 sub pbs_delete_password
{
80 my ($scfg, $storeid) = @_;
82 my $pwfile = pbs_password_file_name
($scfg, $storeid);
87 sub pbs_get_password
{
88 my ($scfg, $storeid) = @_;
90 my $pwfile = pbs_password_file_name
($scfg, $storeid);
92 return PVE
::Tools
::file_read_firstline
($pwfile);
95 sub pbs_encryption_key_file_name
{
96 my ($scfg, $storeid) = @_;
98 return "/etc/pve/priv/storage/${storeid}.enc";
101 sub pbs_set_encryption_key
{
102 my ($scfg, $storeid, $key) = @_;
104 my $pwfile = pbs_encryption_key_file_name
($scfg, $storeid);
105 mkdir "/etc/pve/priv/storage";
107 PVE
::Tools
::file_set_contents
($pwfile, "$key\n");
110 sub pbs_delete_encryption_key
{
111 my ($scfg, $storeid) = @_;
113 my $pwfile = pbs_encryption_key_file_name
($scfg, $storeid);
118 sub pbs_get_encryption_key
{
119 my ($scfg, $storeid) = @_;
121 my $pwfile = pbs_encryption_key_file_name
($scfg, $storeid);
123 return PVE
::Tools
::file_get_contents
($pwfile);
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) = @_;
130 my $encryption_key_file = pbs_encryption_key_file_name
($scfg, $storeid);
133 if (!open($keyfd, '<', $encryption_key_file)) {
134 return undef if $! == ENOENT
;
135 die "failed to open encryption key: $encryption_key_file: $!\n";
142 my ($storeid, $btype, $bid, $btime) = @_;
144 my $time_str = strftime
("%FT%TZ", gmtime($btime));
145 my $volname = "backup/${btype}/${bid}/${time_str}";
147 return "${storeid}:${volname}";
150 my $USE_CRYPT_PARAMS = {
156 my sub do_raw_client_cmd
{
157 my ($scfg, $storeid, $client_cmd, $param, %opts) = @_;
159 my $use_crypto = $USE_CRYPT_PARAMS->{$client_cmd};
161 my $client_exe = '/usr/bin/proxmox-backup-client';
162 die "executable not found '$client_exe'! Proxmox backup client not installed?\n"
165 my $server = $scfg->{server
};
166 my $datastore = $scfg->{datastore
};
167 my $username = $scfg->{username
} // 'root@pam';
169 my $userns_cmd = delete $opts{userns_cmd
};
173 push @$cmd, @$userns_cmd if defined($userns_cmd);
175 push @$cmd, $client_exe, $client_cmd;
177 # This must live in the top scope to not get closed before the `run_command`
180 if (defined($keyfd = pbs_open_encryption_key
($scfg, $storeid))) {
181 my $flags = fcntl($keyfd, F_GETFD
, 0)
182 // die "failed to get file descriptor flags: $!\n";
183 fcntl($keyfd, F_SETFD
, $flags & ~FD_CLOEXEC
)
184 or die "failed to remove FD_CLOEXEC from encryption key file descriptor\n";
185 push @$cmd, '--crypt-mode=encrypt', '--keyfd='.fileno($keyfd);
187 push @$cmd, '--crypt-mode=none';
191 push @$cmd, @$param if defined($param);
193 push @$cmd, "--repository", "$username\@$server:$datastore";
195 local $ENV{PBS_PASSWORD
} = pbs_get_password
($scfg, $storeid);
197 local $ENV{PBS_FINGERPRINT
} = $scfg->{fingerprint
};
199 # no ascii-art on task logs
200 local $ENV{PROXMOX_OUTPUT_NO_BORDER
} = 1;
201 local $ENV{PROXMOX_OUTPUT_NO_HEADER
} = 1;
203 if (my $logfunc = $opts{logfunc
}) {
204 $logfunc->("run: " . join(' ', @$cmd));
207 run_command
($cmd, %opts);
210 # FIXME: External perl code should NOT have access to this.
212 # There should be separate functions to
217 sub run_raw_client_cmd
{
218 my ($scfg, $storeid, $client_cmd, $param, %opts) = @_;
219 return do_raw_client_cmd
($scfg, $storeid, $client_cmd, $param, %opts);
223 my ($scfg, $storeid, $client_cmd, $param, $no_output) = @_;
226 my $outfunc = sub { $json_str .= "$_[0]\n" };
228 $param = [] if !defined($param);
229 $param = [ $param ] if !ref($param);
231 $param = [@$param, '--output-format=json'] if !$no_output;
233 do_raw_client_cmd
($scfg, $storeid, $client_cmd, $param,
234 outfunc
=> $outfunc, errmsg
=> 'proxmox-backup-client failed');
236 return undef if $no_output;
238 my $res = decode_json
($json_str);
243 # Storage implementation
245 sub extract_vzdump_config
{
246 my ($class, $scfg, $volname, $storeid) = @_;
248 my ($vtype, $name, $vmid, undef, undef, undef, $format) = $class->parse_volname($volname);
251 my $outfunc = sub { $config .= "$_[0]\n" };
254 if ($format eq 'pbs-vm') {
255 $config_name = 'qemu-server.conf';
256 } elsif ($format eq 'pbs-ct') {
257 $config_name = 'pct.conf';
259 die "unable to extract configuration for backup format '$format'\n";
262 do_raw_client_cmd
($scfg, $storeid, 'restore', [ $name, $config_name, '-' ],
263 outfunc
=> $outfunc, errmsg
=> 'proxmox-backup-client failed');
268 my $autogen_encryption_key = sub {
269 my ($scfg, $storeid) = @_;
270 my $encfile = pbs_encryption_key_file_name
($scfg, $storeid);
271 run_command
(['proxmox-backup-client', 'key', 'create', '--kdf', 'none', $encfile]);
275 my ($class, $storeid, $scfg, %param) = @_;
277 if (defined(my $password = $param{password
})) {
278 pbs_set_password
($scfg, $storeid, $password);
280 pbs_delete_password
($scfg, $storeid);
283 if (defined(my $encryption_key = $param{'encryption-key'})) {
284 if ($encryption_key eq 'autogen') {
285 $autogen_encryption_key->($scfg, $storeid);
287 pbs_set_encryption_key
($scfg, $storeid, $encryption_key);
290 pbs_delete_encryption_key
($scfg, $storeid);
295 my ($class, $storeid, $scfg, %param) = @_;
297 if (exists($param{password
})) {
298 if (defined($param{password
})) {
299 pbs_set_password
($scfg, $storeid, $param{password
});
301 pbs_delete_password
($scfg, $storeid);
305 if (exists($param{'encryption-key'})) {
306 if (defined(my $encryption_key = delete($param{'encryption-key'}))) {
307 if ($encryption_key eq 'autogen') {
308 $autogen_encryption_key->($scfg, $storeid);
310 pbs_set_encryption_key
($scfg, $storeid, $encryption_key);
313 pbs_delete_encryption_key
($scfg, $storeid);
319 my ($class, $storeid, $scfg) = @_;
321 pbs_delete_password
($scfg, $storeid);
322 pbs_delete_encryption_key
($scfg, $storeid);
326 my ($class, $volname) = @_;
328 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)$!) {
332 my $format = "pbs-$btype";
334 my $name = "$btype/$bid/$btime";
336 if ($bid =~ m/^\d+$/) {
337 return ('backup', $name, $bid, undef, undef, undef, $format);
339 return ('backup', $name, undef, undef, undef, undef, $format);
343 die "unable to parse PBS volume name '$volname'\n";
347 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
349 die "volume snapshot is not possible on pbs storage"
350 if defined($snapname);
352 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
354 my $server = $scfg->{server
};
355 my $datastore = $scfg->{datastore
};
356 my $username = $scfg->{username
} // 'root@pam';
358 # artifical url - we currently do not use that anywhere
359 my $path = "pbs://$username\@$server:$datastore/$name";
361 return ($path, $vmid, $vtype);
365 my ($class, $storeid, $scfg, $volname) = @_;
367 die "can't create base images in pbs storage\n";
371 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
373 die "can't clone images in pbs storage\n";
377 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
379 die "can't allocate space in pbs storage\n";
383 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
385 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
387 run_client_cmd
($scfg, $storeid, "forget", [ $name ], 1);
392 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
400 my ($class, $storeid, $scfg, $vmid, $content_types) = @_;
404 return $res if !grep { $_ eq 'backup' } @$content_types;
406 my $data = run_client_cmd
($scfg, $storeid, "snapshots");
408 foreach my $item (@$data) {
409 my $btype = $item->{"backup-type"};
410 my $bid = $item->{"backup-id"};
411 my $epoch = $item->{"backup-time"};
412 my $size = $item->{size
} // 1;
414 next if !($btype eq 'vm' || $btype eq 'ct');
415 next if $bid !~ m/^\d+$/;
416 next if defined($vmid) && $bid ne $vmid;
418 my $volid = print_volid
($storeid, $btype, $bid, $epoch);
422 format
=> "pbs-$btype",
436 my ($class, $storeid, $scfg, $cache) = @_;
444 my $res = run_client_cmd
($scfg, $storeid, "status");
447 $total = $res->{total
};
448 $used = $res->{used
};
449 $free = $res->{avail
};
455 return ($total, $free, $used, $active);
458 sub activate_storage
{
459 my ($class, $storeid, $scfg, $cache) = @_;
461 run_client_cmd
($scfg, $storeid, "status");
466 sub deactivate_storage
{
467 my ($class, $storeid, $scfg, $cache) = @_;
471 sub activate_volume
{
472 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
474 die "volume snapshot is not possible on pbs device" if $snapname;
479 sub deactivate_volume
{
480 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
482 die "volume snapshot is not possible on pbs device" if $snapname;
487 sub volume_size_info
{
488 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
490 my ($vtype, $name, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
492 my $data = run_client_cmd
($scfg, $storeid, "files", [ $name ]);
495 foreach my $info (@$data) {
496 $size += $info->{size
} if $info->{size
};
501 return wantarray ?
($size, $format, $used, undef) : $size;
505 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
506 die "volume resize is not possible on pbs device";
509 sub volume_snapshot
{
510 my ($class, $scfg, $storeid, $volname, $snap) = @_;
511 die "volume snapshot is not possible on pbs device";
514 sub volume_snapshot_rollback
{
515 my ($class, $scfg, $storeid, $volname, $snap) = @_;
516 die "volume snapshot rollback is not possible on pbs device";
519 sub volume_snapshot_delete
{
520 my ($class, $scfg, $storeid, $volname, $snap) = @_;
521 die "volume snapshot delete is not possible on pbs device";
524 sub volume_has_feature
{
525 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;