]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/PBSPlugin.pm
2a4c19c9dd8d62587226dbd4ca6df394c6c51a02
1 package PVE
::Storage
::PBSPlugin
;
3 # Plugin to access Proxmox Backup Server
7 use POSIX
qw(strftime);
12 use Data
::Dumper
; # fixme: remove
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'),
45 server
=> { fixed
=> 1 },
46 datastore
=> { fixed
=> 1 },
47 nodes
=> { optional
=> 1},
48 disable
=> { optional
=> 1},
49 content
=> { optional
=> 1},
50 username
=> { optional
=> 1 },
51 password
=> { optional
=> 1},
52 maxfiles
=> { optional
=> 1 },
53 fingerprint
=> { optional
=> 1 },
59 sub pbs_password_file_name
{
60 my ($scfg, $storeid) = @_;
62 return "/etc/pve/priv/storage/${storeid}.pw";
65 sub pbs_set_password
{
66 my ($scfg, $storeid, $password) = @_;
68 my $pwfile = pbs_password_file_name
($scfg, $storeid);
69 mkdir "/etc/pve/priv/storage";
71 PVE
::Tools
::file_set_contents
($pwfile, "$password\n");
74 sub pbs_delete_password
{
75 my ($scfg, $storeid) = @_;
77 my $pwfile = pbs_password_file_name
($scfg, $storeid);
82 sub pbs_get_password
{
83 my ($scfg, $storeid) = @_;
85 my $pwfile = pbs_password_file_name
($scfg, $storeid);
87 return PVE
::Tools
::file_read_firstline
($pwfile);
91 sub run_raw_client_cmd
{
92 my ($scfg, $storeid, $client_cmd, $param, %opts) = @_;
94 my $client_exe = '/usr/bin/proxmox-backup-client';
95 die "executable not found '$client_exe'! Proxmox backup client not installed?\n"
98 my $server = $scfg->{server
};
99 my $datastore = $scfg->{datastore
};
100 my $username = $scfg->{username
} // 'root@pam';
102 my $userns_cmd = delete $opts{userns_cmd
};
106 push @$cmd, @$userns_cmd if defined($userns_cmd);
108 push @$cmd, $client_exe, $client_cmd;
110 push @$cmd, @$param if defined($param);
112 push @$cmd, "--repository", "$username\@$server:$datastore";
114 local $ENV{PBS_PASSWORD
} = pbs_get_password
($scfg, $storeid);
116 local $ENV{PBS_FINGERPRINT
} = $scfg->{fingerprint
};
118 if (my $logfunc = $opts{logfunc
}) {
119 $logfunc->("run bps command: " . join(' ', @$cmd));
122 run_command
($cmd, %opts);
126 my ($scfg, $storeid, $client_cmd, $param, $no_output) = @_;
129 my $outfunc = sub { $json_str .= "$_[0]\n" };
131 $param = [] if !defined($param);
132 $param = [ $param ] if !ref($param);
134 $param = [@$param, '--output-format=json'] if !$no_output;
136 run_raw_client_cmd
($scfg, $storeid, $client_cmd, $param,
137 outfunc
=> $outfunc, errmsg
=> 'proxmox-backup-client failed');
139 return undef if $no_output;
141 my $res = decode_json
($json_str);
146 # Storage implementation
148 sub extract_vzdump_config
{
149 my ($class, $scfg, $volname, $storeid) = @_;
151 my ($vtype, $name, $vmid, undef, undef, undef, $format) = $class->parse_volname($volname);
154 my $outfunc = sub { $config .= "$_[0]\n" };
157 if ($format eq 'pbs-vm') {
158 $config_name = 'qemu-server.conf';
159 } elsif ($format eq 'pbs-ct') {
160 $config_name = 'pct.conf';
162 die "unable to extract configuration for backup format '$format'\n";
165 run_raw_client_cmd
($scfg, $storeid, 'restore', [ $name, $config_name, '-' ],
166 outfunc
=> $outfunc, errmsg
=> 'proxmox-backup-client failed');
172 my ($class, $storeid, $scfg, %param) = @_;
174 if (defined($param{password
})) {
175 pbs_set_password
($scfg, $storeid, $param{password
});
177 pbs_delete_password
($scfg, $storeid);
182 my ($class, $storeid, $scfg, %param) = @_;
184 return if !exists($param{password
});
186 if (defined($param{password
})) {
187 pbs_set_password
($scfg, $storeid, $param{password
});
189 pbs_delete_password
($scfg, $storeid);
194 my ($class, $storeid, $scfg) = @_;
196 pbs_delete_password
($scfg, $storeid);
200 my ($class, $volname) = @_;
202 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)$!) {
206 my $format = "pbs-$btype";
208 my $name = "$btype/$bid/$btime";
210 if ($bid =~ m/^\d+$/) {
211 return ('backup', $name, $bid, undef, undef, undef, $format);
213 return ('backup', $name, undef, undef, undef, undef, $format);
217 die "unable to parse PBS volume name '$volname'\n";
221 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
223 die "volume snapshot is not possible on pbs storage"
224 if defined($snapname);
226 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
228 my $server = $scfg->{server
};
229 my $datastore = $scfg->{datastore
};
230 my $username = $scfg->{username
} // 'root@pam';
232 # artifical url - we currently do not use that anywhere
233 my $path = "pbs://$username\@$server:$datastore/$name";
235 return ($path, $vmid, $vtype);
239 my ($class, $storeid, $scfg, $volname) = @_;
241 die "can't create base images in pbs storage\n";
245 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
247 die "can't clone images in pbs storage\n";
251 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
253 die "can't allocate space in pbs storage\n";
257 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
259 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
261 run_client_cmd
($scfg, $storeid, "forget", [ $name ], 1);
266 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
274 my ($class, $storeid, $scfg, $vmid, $content_types) = @_;
278 return $res if !grep { $_ eq 'backup' } @$content_types;
280 my $data = run_client_cmd
($scfg, $storeid, "snapshots");
282 foreach my $item (@$data) {
283 my $btype = $item->{"backup-type"};
284 my $bid = $item->{"backup-id"};
285 my $epoch = $item->{"backup-time"};
286 my $size = $item->{size
} // 1;
288 next if !($btype eq 'vm' || $btype eq 'ct');
289 next if $bid !~ m/^\d+$/;
291 my $btime = strftime
("%FT%TZ", gmtime($epoch));
292 my $volname = "backup/${btype}/${bid}/${btime}";
294 my $volid = "$storeid:$volname";
297 volid
=> $volid , format
=> "pbs-$btype", size
=> $size,
298 content
=> 'backup', vmid
=> int($bid), ctime
=> $epoch
308 my ($class, $storeid, $scfg, $cache) = @_;
316 my $res = run_client_cmd
($scfg, $storeid, "status");
319 $total = $res->{total
};
320 $used = $res->{used
};
321 $free = $res->{avail
};
327 return ($total, $free, $used, $active);
330 sub activate_storage
{
331 my ($class, $storeid, $scfg, $cache) = @_;
335 sub deactivate_storage
{
336 my ($class, $storeid, $scfg, $cache) = @_;
340 sub activate_volume
{
341 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
343 die "volume snapshot is not possible on pbs device" if $snapname;
348 sub deactivate_volume
{
349 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
351 die "volume snapshot is not possible on pbs device" if $snapname;
356 sub volume_size_info
{
357 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
359 my ($vtype, $name, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
361 my $data = run_client_cmd
($scfg, $storeid, "files", [ $name ]);
364 foreach my $info (@$data) {
365 $size += $info->{size
} if $info->{size
};
370 return wantarray ?
($size, $format, $used, undef) : $size;
374 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
375 die "volume resize is not possible on pbs device";
378 sub volume_snapshot
{
379 my ($class, $scfg, $storeid, $volname, $snap) = @_;
380 die "volume snapshot is not possible on pbs device";
383 sub volume_snapshot_rollback
{
384 my ($class, $scfg, $storeid, $volname, $snap) = @_;
385 die "volume snapshot rollback is not possible on pbs device";
388 sub volume_snapshot_delete
{
389 my ($class, $scfg, $storeid, $volname, $snap) = @_;
390 die "volume snapshot delete is not possible on pbs device";
393 sub volume_has_feature
{
394 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;