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 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');
260 my $autogen_encryption_key = sub {
261 my ($scfg, $storeid) = @_;
262 my $encfile = pbs_encryption_key_file_name
($scfg, $storeid);
263 run_command
(['proxmox-backup-client', 'key', 'create', '--kdf', 'none', $encfile]);
267 my ($class, $storeid, $scfg, %param) = @_;
269 if (defined(my $password = $param{password
})) {
270 pbs_set_password
($scfg, $storeid, $password);
272 pbs_delete_password
($scfg, $storeid);
275 if (defined(my $encryption_key = $param{'encryption-key'})) {
276 if ($encryption_key eq 'autogen') {
277 $autogen_encryption_key->($scfg, $storeid);
279 pbs_set_encryption_key
($scfg, $storeid, $encryption_key);
282 pbs_delete_encryption_key
($scfg, $storeid);
287 my ($class, $storeid, $scfg, %param) = @_;
289 if (exists($param{password
})) {
290 if (defined($param{password
})) {
291 pbs_set_password
($scfg, $storeid, $param{password
});
293 pbs_delete_password
($scfg, $storeid);
297 if (exists($param{'encryption-key'})) {
298 if (defined(my $encryption_key = delete($param{'encryption-key'}))) {
299 if ($encryption_key eq 'autogen') {
300 $autogen_encryption_key->($scfg, $storeid);
302 pbs_set_encryption_key
($scfg, $storeid, $encryption_key);
305 pbs_delete_encryption_key
($scfg, $storeid);
311 my ($class, $storeid, $scfg) = @_;
313 pbs_delete_password
($scfg, $storeid);
314 pbs_delete_encryption_key
($scfg, $storeid);
318 my ($class, $volname) = @_;
320 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)$!) {
324 my $format = "pbs-$btype";
326 my $name = "$btype/$bid/$btime";
328 if ($bid =~ m/^\d+$/) {
329 return ('backup', $name, $bid, undef, undef, undef, $format);
331 return ('backup', $name, undef, undef, undef, undef, $format);
335 die "unable to parse PBS volume name '$volname'\n";
339 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
341 die "volume snapshot is not possible on pbs storage"
342 if defined($snapname);
344 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
346 my $server = $scfg->{server
};
347 my $datastore = $scfg->{datastore
};
348 my $username = $scfg->{username
} // 'root@pam';
350 # artifical url - we currently do not use that anywhere
351 my $path = "pbs://$username\@$server:$datastore/$name";
353 return ($path, $vmid, $vtype);
357 my ($class, $storeid, $scfg, $volname) = @_;
359 die "can't create base images in pbs storage\n";
363 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
365 die "can't clone images in pbs storage\n";
369 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
371 die "can't allocate space in pbs storage\n";
375 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
377 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
379 run_client_cmd
($scfg, $storeid, "forget", [ $name ], 1);
384 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
392 my ($class, $storeid, $scfg, $vmid, $content_types) = @_;
396 return $res if !grep { $_ eq 'backup' } @$content_types;
398 my $data = run_client_cmd
($scfg, $storeid, "snapshots");
400 foreach my $item (@$data) {
401 my $btype = $item->{"backup-type"};
402 my $bid = $item->{"backup-id"};
403 my $epoch = $item->{"backup-time"};
404 my $size = $item->{size
} // 1;
406 next if !($btype eq 'vm' || $btype eq 'ct');
407 next if $bid !~ m/^\d+$/;
408 next if defined($vmid) && $bid ne $vmid;
410 my $volid = print_volid
($storeid, $btype, $bid, $epoch);
414 format
=> "pbs-$btype",
428 my ($class, $storeid, $scfg, $cache) = @_;
436 my $res = run_client_cmd
($scfg, $storeid, "status");
439 $total = $res->{total
};
440 $used = $res->{used
};
441 $free = $res->{avail
};
447 return ($total, $free, $used, $active);
450 sub activate_storage
{
451 my ($class, $storeid, $scfg, $cache) = @_;
455 sub deactivate_storage
{
456 my ($class, $storeid, $scfg, $cache) = @_;
460 sub activate_volume
{
461 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
463 die "volume snapshot is not possible on pbs device" if $snapname;
468 sub deactivate_volume
{
469 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
471 die "volume snapshot is not possible on pbs device" if $snapname;
476 sub volume_size_info
{
477 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
479 my ($vtype, $name, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
481 my $data = run_client_cmd
($scfg, $storeid, "files", [ $name ]);
484 foreach my $info (@$data) {
485 $size += $info->{size
} if $info->{size
};
490 return wantarray ?
($size, $format, $used, undef) : $size;
494 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
495 die "volume resize is not possible on pbs device";
498 sub volume_snapshot
{
499 my ($class, $scfg, $storeid, $volname, $snap) = @_;
500 die "volume snapshot is not possible on pbs device";
503 sub volume_snapshot_rollback
{
504 my ($class, $scfg, $storeid, $volname, $snap) = @_;
505 die "volume snapshot rollback is not possible on pbs device";
508 sub volume_snapshot_delete
{
509 my ($class, $scfg, $storeid, $volname, $snap) = @_;
510 die "volume snapshot delete is not possible on pbs device";
513 sub volume_has_feature
{
514 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;