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