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.",
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 sub do_raw_client_cmd
{
151 my ($scfg, $storeid, $client_cmd, $param, $can_encrypt, %opts) = @_;
153 my $client_exe = '/usr/bin/proxmox-backup-client';
154 die "executable not found '$client_exe'! Proxmox backup client not installed?\n"
157 my $server = $scfg->{server
};
158 my $datastore = $scfg->{datastore
};
159 my $username = $scfg->{username
} // 'root@pam';
161 my $userns_cmd = delete $opts{userns_cmd
};
165 push @$cmd, @$userns_cmd if defined($userns_cmd);
167 push @$cmd, $client_exe, $client_cmd;
169 # This must live in the top scope to not get closed before the `run_command`
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);
179 push @$cmd, '--crypt-mode=none';
183 push @$cmd, @$param if defined($param);
185 push @$cmd, "--repository", "$username\@$server:$datastore";
187 local $ENV{PBS_PASSWORD
} = pbs_get_password
($scfg, $storeid);
189 local $ENV{PBS_FINGERPRINT
} = $scfg->{fingerprint
};
191 # no ascii-art on task logs
192 local $ENV{PROXMOX_OUTPUT_NO_BORDER
} = 1;
193 local $ENV{PROXMOX_OUTPUT_NO_HEADER
} = 1;
195 if (my $logfunc = $opts{logfunc
}) {
196 $logfunc->("run: " . join(' ', @$cmd));
199 run_command
($cmd, %opts);
202 # FIXME: External perl code should NOT have access to this.
204 # There should be separate functions to
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);
215 my ($scfg, $storeid, $client_cmd, $param, $no_output) = @_;
218 my $outfunc = sub { $json_str .= "$_[0]\n" };
220 $param = [] if !defined($param);
221 $param = [ $param ] if !ref($param);
223 $param = [@$param, '--output-format=json'] if !$no_output;
225 do_raw_client_cmd
($scfg, $storeid, $client_cmd, $param, 0,
226 outfunc
=> $outfunc, errmsg
=> 'proxmox-backup-client failed');
228 return undef if $no_output;
230 my $res = decode_json
($json_str);
235 # Storage implementation
237 sub extract_vzdump_config
{
238 my ($class, $scfg, $volname, $storeid) = @_;
240 my ($vtype, $name, $vmid, undef, undef, undef, $format) = $class->parse_volname($volname);
243 my $outfunc = sub { $config .= "$_[0]\n" };
246 if ($format eq 'pbs-vm') {
247 $config_name = 'qemu-server.conf';
248 } elsif ($format eq 'pbs-ct') {
249 $config_name = 'pct.conf';
251 die "unable to extract configuration for backup format '$format'\n";
254 do_raw_client_cmd
($scfg, $storeid, 'restore', [ $name, $config_name, '-' ], 0,
255 outfunc
=> $outfunc, errmsg
=> 'proxmox-backup-client failed');
261 my ($class, $storeid, $scfg, %param) = @_;
263 if (defined(my $password = $param{password
})) {
264 pbs_set_password
($scfg, $storeid, $password);
266 pbs_delete_password
($scfg, $storeid);
269 if (defined(my $encryption_key = $param{encryption_key
})) {
270 pbs_set_encryption_key
($scfg, $storeid, $encryption_key);
272 pbs_delete_encryption_key
($scfg, $storeid);
277 my ($class, $storeid, $scfg, %param) = @_;
279 if (exists($param{password
})) {
280 if (defined($param{password
})) {
281 pbs_set_password
($scfg, $storeid, $param{password
});
283 pbs_delete_password
($scfg, $storeid);
287 if (exists($param{encryption_key
})) {
288 if (defined(my $encryption_key = delete($param{encryption_key
}))) {
289 pbs_set_encryption_key
($scfg, $storeid, $encryption_key);
291 pbs_delete_encryption_key
($scfg, $storeid);
297 my ($class, $storeid, $scfg) = @_;
299 pbs_delete_password
($scfg, $storeid);
300 pbs_delete_encryption_key
($scfg, $storeid);
304 my ($class, $volname) = @_;
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)$!) {
310 my $format = "pbs-$btype";
312 my $name = "$btype/$bid/$btime";
314 if ($bid =~ m/^\d+$/) {
315 return ('backup', $name, $bid, undef, undef, undef, $format);
317 return ('backup', $name, undef, undef, undef, undef, $format);
321 die "unable to parse PBS volume name '$volname'\n";
325 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
327 die "volume snapshot is not possible on pbs storage"
328 if defined($snapname);
330 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
332 my $server = $scfg->{server
};
333 my $datastore = $scfg->{datastore
};
334 my $username = $scfg->{username
} // 'root@pam';
336 # artifical url - we currently do not use that anywhere
337 my $path = "pbs://$username\@$server:$datastore/$name";
339 return ($path, $vmid, $vtype);
343 my ($class, $storeid, $scfg, $volname) = @_;
345 die "can't create base images in pbs storage\n";
349 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
351 die "can't clone images in pbs storage\n";
355 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
357 die "can't allocate space in pbs storage\n";
361 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
363 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
365 run_client_cmd
($scfg, $storeid, "forget", [ $name ], 1);
370 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
378 my ($class, $storeid, $scfg, $vmid, $content_types) = @_;
382 return $res if !grep { $_ eq 'backup' } @$content_types;
384 my $data = run_client_cmd
($scfg, $storeid, "snapshots");
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;
392 next if !($btype eq 'vm' || $btype eq 'ct');
393 next if $bid !~ m/^\d+$/;
394 next if defined($vmid) && $bid ne $vmid;
396 my $volid = print_volid
($storeid, $btype, $bid, $epoch);
400 format
=> "pbs-$btype",
414 my ($class, $storeid, $scfg, $cache) = @_;
422 my $res = run_client_cmd
($scfg, $storeid, "status");
425 $total = $res->{total
};
426 $used = $res->{used
};
427 $free = $res->{avail
};
433 return ($total, $free, $used, $active);
436 sub activate_storage
{
437 my ($class, $storeid, $scfg, $cache) = @_;
441 sub deactivate_storage
{
442 my ($class, $storeid, $scfg, $cache) = @_;
446 sub activate_volume
{
447 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
449 die "volume snapshot is not possible on pbs device" if $snapname;
454 sub deactivate_volume
{
455 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
457 die "volume snapshot is not possible on pbs device" if $snapname;
462 sub volume_size_info
{
463 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
465 my ($vtype, $name, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
467 my $data = run_client_cmd
($scfg, $storeid, "files", [ $name ]);
470 foreach my $info (@$data) {
471 $size += $info->{size
} if $info->{size
};
476 return wantarray ?
($size, $format, $used, undef) : $size;
480 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
481 die "volume resize is not possible on pbs device";
484 sub volume_snapshot
{
485 my ($class, $scfg, $storeid, $volname, $snap) = @_;
486 die "volume snapshot is not possible on pbs device";
489 sub volume_snapshot_rollback
{
490 my ($class, $scfg, $storeid, $volname, $snap) = @_;
491 die "volume snapshot rollback is not possible on pbs device";
494 sub volume_snapshot_delete
{
495 my ($class, $scfg, $storeid, $volname, $snap) = @_;
496 die "volume snapshot delete is not possible on pbs device";
499 sub volume_has_feature
{
500 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;