]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/PBSPlugin.pm
use one liner closure for outfunc
[pve-storage.git] / PVE / Storage / PBSPlugin.pm
CommitLineData
271fe394
DM
1package PVE::Storage::PBSPlugin;
2
3# Plugin to access Proxmox Backup Server
4
5use strict;
6use warnings;
7use POSIX qw(strftime);
8use IO::File;
9use HTTP::Request;
10use LWP::UserAgent;
11use JSON;
12use Data::Dumper; # fixme: remove
13
14use PVE::Tools qw(run_command file_read_firstline trim dir_glob_regex dir_glob_foreach);
15use PVE::Storage::Plugin;
16use PVE::JSONSchema qw(get_standard_option);
17
18use base qw(PVE::Storage::Plugin);
19
20# Configuration
21
22sub type {
23 return 'pbs';
24}
25
26sub plugindata {
27 return {
28 content => [ {backup => 1, none => 1}, { backup => 1 }],
29 };
30}
31
32sub 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 };
41}
42
43sub options {
44 return {
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 },
54 };
55}
56
57# Helpers
58
59sub pbs_password_file_name {
60 my ($scfg, $storeid) = @_;
61
62 return "/etc/pve/priv/${storeid}.pw";
63}
64
65sub pbs_set_password {
66 my ($scfg, $storeid, $password) = @_;
67
68 my $pwfile = pbs_password_file_name($scfg, $storeid);
69
70 PVE::Tools::file_set_contents($pwfile, "$password\n");
71}
72
73sub pbs_delete_password {
74 my ($scfg, $storeid) = @_;
75
76 my $pwfile = pbs_password_file_name($scfg, $storeid);
77
78 unlink $pwfile;
79}
80
81sub pbs_get_password {
82 my ($scfg, $storeid) = @_;
83
84 my $pwfile = pbs_password_file_name($scfg, $storeid);
85
86 return PVE::Tools::file_read_firstline($pwfile);
87}
88
89
90sub run_raw_client_cmd {
91 my ($scfg, $storeid, $client_cmd, $param, %opts) = @_;
92
93 my $server = $scfg->{server};
94 my $datastore = $scfg->{datastore};
95 my $username = $scfg->{username} // 'root@pam';
96
97 my $userns_cmd = delete $opts{userns_cmd};
98
99 my $cmd = [];
100
101 push @$cmd, @$userns_cmd if defined($userns_cmd);
102
103 push @$cmd, "/usr/bin/proxmox-backup-client", $client_cmd;
104
105 push @$cmd, @$param if defined($param);
106
107 push @$cmd, "--repository", "$username\@$server:$datastore";
108
109 local $ENV{PBS_PASSWORD} = pbs_get_password($scfg, $storeid);
110
111 local $ENV{PBS_FINGERPRINT} = $scfg->{fingerprint};
112
113 if (my $logfunc = $opts{logfunc}) {
114 $logfunc->("run bps command: " . join(' ', @$cmd));
115 }
116
117 run_command($cmd, %opts);
118}
119
120sub run_client_cmd {
121 my ($scfg, $storeid, $client_cmd, $param, $no_output) = @_;
122
123 my $json_str = '';
fee2ece3 124 my $outfunc = sub { $json_str .= "$_[0]\n" };
271fe394
DM
125
126 $param = [] if !defined($param);
127 $param = [ $param ] if !ref($param);
128
129 $param = [@$param, '--output-format=json'] if !$no_output;
130
131 run_raw_client_cmd($scfg, $storeid, $client_cmd, $param,
132 outfunc => $outfunc, errmsg => 'proxmox-backup-client failed');
133
134 return undef if $no_output;
135
136 my $res = decode_json($json_str);
137
138 return $res;
139}
140
141# Storage implementation
142
c855ac15
DM
143sub extract_vzdump_config {
144 my ($class, $scfg, $volname, $storeid) = @_;
145
146 my ($vtype, $name, $vmid, undef, undef, undef, $format) = $class->parse_volname($volname);
147
148 my $config = '';
fee2ece3 149 my $outfunc = sub { $config .= "$_[0]\n" };
c855ac15
DM
150
151 my $config_name;
152 if ($format eq 'pbs-vm') {
153 $config_name = 'qemu-server.conf';
154 } elsif ($format eq 'pbs-ct') {
155 $config_name = 'pct.conf';
156 } else {
157 die "unable to extract configuration for backup format '$format'\n";
158 }
159
160 run_raw_client_cmd(undef, $scfg, $storeid, 'restore', [ $name, $config_name, '-' ],
161 outfunc => $outfunc, errmsg => 'proxmox-backup-client failed');
162
163 return $config;
164}
165
271fe394
DM
166sub on_add_hook {
167 my ($class, $storeid, $scfg, %param) = @_;
168
169 if (my $password = $param{password}) {
170 pbs_set_password($scfg, $storeid, $password);
171 }
172}
173
174sub on_delete_hook {
175 my ($class, $storeid, $scfg) = @_;
176
177 pbs_delete_password($scfg, $storeid);
178}
179
180sub parse_volname {
181 my ($class, $volname) = @_;
182
183 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)$!) {
184 my $btype = $1;
185 my $bid = $2;
186 my $btime = $3;
187 my $format = "pbs-$btype";
188
189 my $name = "$btype/$bid/$btime";
190
191 if ($bid =~ m/^\d+$/) {
192 return ('backup', $name, $bid, undef, undef, undef, $format);
193 } else {
194 return ('backup', $name, undef, undef, undef, undef, $format);
195 }
196 }
197
198 die "unable to parse PBS volume name '$volname'\n";
199}
200
201sub path {
202 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
203
204 die "volume snapshot is not possible on pbs storage"
205 if defined($snapname);
206
207 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
208
209 my $server = $scfg->{server};
210 my $datastore = $scfg->{datastore};
211 my $username = $scfg->{username} // 'root@pam';
212
213 # artifical url - we currently do not use that anywhere
214 my $path = "pbs://$username\@$server:$datastore/$name";
215
216 return ($path, $vmid, $vtype);
217}
218
219sub create_base {
220 my ($class, $storeid, $scfg, $volname) = @_;
221
222 die "can't create base images in pbs storage\n";
223}
224
225sub clone_image {
226 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
227
228 die "can't clone images in pbs storage\n";
229}
230
231sub alloc_image {
232 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
233
234 die "can't allocate space in pbs storage\n";
235}
236
237sub free_image {
238 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
239
240 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
241
242 run_client_cmd($scfg, $storeid, "forget", [ $name ], 1);
243}
244
245
246sub list_images {
247 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
248
249 my $res = [];
250
251 return $res;
252}
253
254sub list_volumes {
255 my ($class, $storeid, $scfg, $vmid, $content_types) = @_;
256
257 my $res = [];
258
259 return $res if !grep { $_ eq 'backup' } @$content_types;
260
261 my $data = run_client_cmd($scfg, $storeid, "snapshots");
262
263 foreach my $item (@$data) {
264 my $btype = $item->{"backup-type"};
265 my $bid = $item->{"backup-id"};
266 my $btime = $item->{"backup-time"};
267 my $size = $item->{size} // 1;
268
269 next if !($btype eq 'vm' || $btype eq 'ct');
270 next if $bid !~ m/^\d+$/;
271
272 $btime = strftime("%FT%TZ", gmtime($btime));
273 my $volname = "backup/${btype}/${bid}/${btime}";
274
275 my $volid = "$storeid:$volname";
276
277 my $info = { volid => $volid , format => "pbs-$btype", size => $size, content => 'backup', vmid => int($bid) };
278
279 push @$res, $info;
280 }
281
282 return $res;
283}
284
285sub status {
286 my ($class, $storeid, $scfg, $cache) = @_;
287
288 my $total = 0;
289 my $free = 0;
290 my $used = 0;
291 my $active = 0;
292
293 eval {
294 my $res = run_client_cmd($scfg, $storeid, "status");
295
296 $active = 1;
297 $total = $res->{total};
298 $used = $res->{used};
f155c912
TL
299 $free = $res->{avail};
300 };
271fe394
DM
301 if (my $err = $@) {
302 warn $err;
303 }
304
305 return ($total, $free, $used, $active);
306}
307
308sub activate_storage {
309 my ($class, $storeid, $scfg, $cache) = @_;
310 return 1;
311}
312
313sub deactivate_storage {
314 my ($class, $storeid, $scfg, $cache) = @_;
315 return 1;
316}
317
318sub activate_volume {
319 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
320
321 die "volume snapshot is not possible on pbs device" if $snapname;
322
323 return 1;
324}
325
326sub deactivate_volume {
327 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
328
329 die "volume snapshot is not possible on pbs device" if $snapname;
330
331 return 1;
332}
333
334sub volume_size_info {
335 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
336
337 my ($vtype, $name, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
338
339 my $data = run_client_cmd($scfg, $storeid, "files", [ $name ]);
340
341 my $size = 0;
342 foreach my $info (@$data) {
343 $size += $info->{size} if $info->{size};
344 }
345
346 my $used = $size;
347
348 return wantarray ? ($size, $format, $used, undef) : $size;
349}
350
351sub volume_resize {
352 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
353 die "volume resize is not possible on pbs device";
354}
355
356sub volume_snapshot {
357 my ($class, $scfg, $storeid, $volname, $snap) = @_;
358 die "volume snapshot is not possible on pbs device";
359}
360
361sub volume_snapshot_rollback {
362 my ($class, $scfg, $storeid, $volname, $snap) = @_;
363 die "volume snapshot rollback is not possible on pbs device";
364}
365
366sub volume_snapshot_delete {
367 my ($class, $scfg, $storeid, $volname, $snap) = @_;
368 die "volume snapshot delete is not possible on pbs device";
369}
370
371sub volume_has_feature {
372 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
373
374 return undef;
375}
376
3771;