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