]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/PBSPlugin.pm
pbs: ensure storage secret file directory exists
[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/storage/${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 mkdir "/etc/pve/priv/storage";
70
71 PVE::Tools::file_set_contents($pwfile, "$password\n");
72 }
73
74 sub pbs_delete_password {
75 my ($scfg, $storeid) = @_;
76
77 my $pwfile = pbs_password_file_name($scfg, $storeid);
78
79 unlink $pwfile;
80 }
81
82 sub 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
91 sub run_raw_client_cmd {
92 my ($scfg, $storeid, $client_cmd, $param, %opts) = @_;
93
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
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
108 push @$cmd, $client_exe, $client_cmd;
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
125 sub run_client_cmd {
126 my ($scfg, $storeid, $client_cmd, $param, $no_output) = @_;
127
128 my $json_str = '';
129 my $outfunc = sub { $json_str .= "$_[0]\n" };
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
148 sub 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 = '';
154 my $outfunc = sub { $config .= "$_[0]\n" };
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
165 run_raw_client_cmd($scfg, $storeid, 'restore', [ $name, $config_name, '-' ],
166 outfunc => $outfunc, errmsg => 'proxmox-backup-client failed');
167
168 return $config;
169 }
170
171 sub on_add_hook {
172 my ($class, $storeid, $scfg, %param) = @_;
173
174 if (my $password = $param{password}) {
175 pbs_set_password($scfg, $storeid, $password);
176 }
177 }
178
179 sub on_delete_hook {
180 my ($class, $storeid, $scfg) = @_;
181
182 pbs_delete_password($scfg, $storeid);
183 }
184
185 sub parse_volname {
186 my ($class, $volname) = @_;
187
188 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)$!) {
189 my $btype = $1;
190 my $bid = $2;
191 my $btime = $3;
192 my $format = "pbs-$btype";
193
194 my $name = "$btype/$bid/$btime";
195
196 if ($bid =~ m/^\d+$/) {
197 return ('backup', $name, $bid, undef, undef, undef, $format);
198 } else {
199 return ('backup', $name, undef, undef, undef, undef, $format);
200 }
201 }
202
203 die "unable to parse PBS volume name '$volname'\n";
204 }
205
206 sub path {
207 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
208
209 die "volume snapshot is not possible on pbs storage"
210 if defined($snapname);
211
212 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
213
214 my $server = $scfg->{server};
215 my $datastore = $scfg->{datastore};
216 my $username = $scfg->{username} // 'root@pam';
217
218 # artifical url - we currently do not use that anywhere
219 my $path = "pbs://$username\@$server:$datastore/$name";
220
221 return ($path, $vmid, $vtype);
222 }
223
224 sub create_base {
225 my ($class, $storeid, $scfg, $volname) = @_;
226
227 die "can't create base images in pbs storage\n";
228 }
229
230 sub clone_image {
231 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
232
233 die "can't clone images in pbs storage\n";
234 }
235
236 sub alloc_image {
237 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
238
239 die "can't allocate space in pbs storage\n";
240 }
241
242 sub free_image {
243 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
244
245 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
246
247 run_client_cmd($scfg, $storeid, "forget", [ $name ], 1);
248 }
249
250
251 sub list_images {
252 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
253
254 my $res = [];
255
256 return $res;
257 }
258
259 sub list_volumes {
260 my ($class, $storeid, $scfg, $vmid, $content_types) = @_;
261
262 my $res = [];
263
264 return $res if !grep { $_ eq 'backup' } @$content_types;
265
266 my $data = run_client_cmd($scfg, $storeid, "snapshots");
267
268 foreach my $item (@$data) {
269 my $btype = $item->{"backup-type"};
270 my $bid = $item->{"backup-id"};
271 my $btime = $item->{"backup-time"};
272 my $size = $item->{size} // 1;
273
274 next if !($btype eq 'vm' || $btype eq 'ct');
275 next if $bid !~ m/^\d+$/;
276
277 $btime = strftime("%FT%TZ", gmtime($btime));
278 my $volname = "backup/${btype}/${bid}/${btime}";
279
280 my $volid = "$storeid:$volname";
281
282 my $info = { volid => $volid , format => "pbs-$btype", size => $size, content => 'backup', vmid => int($bid) };
283
284 push @$res, $info;
285 }
286
287 return $res;
288 }
289
290 sub status {
291 my ($class, $storeid, $scfg, $cache) = @_;
292
293 my $total = 0;
294 my $free = 0;
295 my $used = 0;
296 my $active = 0;
297
298 eval {
299 my $res = run_client_cmd($scfg, $storeid, "status");
300
301 $active = 1;
302 $total = $res->{total};
303 $used = $res->{used};
304 $free = $res->{avail};
305 };
306 if (my $err = $@) {
307 warn $err;
308 }
309
310 return ($total, $free, $used, $active);
311 }
312
313 sub activate_storage {
314 my ($class, $storeid, $scfg, $cache) = @_;
315 return 1;
316 }
317
318 sub deactivate_storage {
319 my ($class, $storeid, $scfg, $cache) = @_;
320 return 1;
321 }
322
323 sub activate_volume {
324 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
325
326 die "volume snapshot is not possible on pbs device" if $snapname;
327
328 return 1;
329 }
330
331 sub deactivate_volume {
332 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
333
334 die "volume snapshot is not possible on pbs device" if $snapname;
335
336 return 1;
337 }
338
339 sub volume_size_info {
340 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
341
342 my ($vtype, $name, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
343
344 my $data = run_client_cmd($scfg, $storeid, "files", [ $name ]);
345
346 my $size = 0;
347 foreach my $info (@$data) {
348 $size += $info->{size} if $info->{size};
349 }
350
351 my $used = $size;
352
353 return wantarray ? ($size, $format, $used, undef) : $size;
354 }
355
356 sub volume_resize {
357 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
358 die "volume resize is not possible on pbs device";
359 }
360
361 sub volume_snapshot {
362 my ($class, $scfg, $storeid, $volname, $snap) = @_;
363 die "volume snapshot is not possible on pbs device";
364 }
365
366 sub volume_snapshot_rollback {
367 my ($class, $scfg, $storeid, $volname, $snap) = @_;
368 die "volume snapshot rollback is not possible on pbs device";
369 }
370
371 sub volume_snapshot_delete {
372 my ($class, $scfg, $storeid, $volname, $snap) = @_;
373 die "volume snapshot delete is not possible on pbs device";
374 }
375
376 sub volume_has_feature {
377 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
378
379 return undef;
380 }
381
382 1;