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