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