]>
Commit | Line | Data |
---|---|---|
aaeeeebe DM |
1 | package PVE::VZDump; |
2 | ||
aaeeeebe DM |
3 | use strict; |
4 | use warnings; | |
df2d3636 | 5 | |
3f488b61 | 6 | use Clone; |
aaeeeebe | 7 | use Fcntl ':flock'; |
bbd4cdd8 | 8 | use File::Basename; |
df2d3636 | 9 | use File::Path; |
aaeeeebe DM |
10 | use IO::File; |
11 | use IO::Select; | |
12 | use IPC::Open3; | |
e2b5eb29 | 13 | use POSIX qw(strftime); |
aaeeeebe | 14 | use Time::Local; |
df2d3636 TL |
15 | |
16 | use PVE::Cluster qw(cfs_read_file); | |
17 | use PVE::DataCenterConfig; | |
18 | use PVE::Exception qw(raise_param_exc); | |
2de3ee58 | 19 | use PVE::HA::Config; |
df2d3636 TL |
20 | use PVE::HA::Env::PVE2; |
21 | use PVE::JSONSchema qw(get_standard_option); | |
22 | use PVE::RPCEnvironment; | |
23 | use PVE::Storage; | |
2424074e | 24 | use PVE::VZDump::Common; |
df2d3636 | 25 | use PVE::VZDump::Plugin; |
05447e04 | 26 | use PVE::Tools qw(extract_param split_list); |
5c4da4c3 | 27 | use PVE::API2Tools; |
aaeeeebe DM |
28 | |
29 | my @posix_filesystems = qw(ext3 ext4 nfs nfs4 reiserfs xfs); | |
30 | ||
31 | my $lockfile = '/var/run/vzdump.lock'; | |
8682f6fc | 32 | my $pidfile = '/var/run/vzdump.pid'; |
aaeeeebe DM |
33 | my $logdir = '/var/log/vzdump'; |
34 | ||
dafb6246 | 35 | my @plugins = qw(); |
aaeeeebe | 36 | |
2424074e | 37 | my $confdesc = PVE::VZDump::Common::get_confdesc(); |
cc61ea36 | 38 | |
3f488b61 FE |
39 | my $confdesc_for_defaults = Clone::clone($confdesc); |
40 | delete $confdesc_for_defaults->{$_}->{requires} for qw(notes-template protected); | |
41 | ||
aaeeeebe | 42 | # Load available plugins |
8187f6b0 DM |
43 | my @pve_vzdump_classes = qw(PVE::VZDump::QemuServer PVE::VZDump::LXC); |
44 | foreach my $plug (@pve_vzdump_classes) { | |
45 | my $filename = "/usr/share/perl5/$plug.pm"; | |
46 | $filename =~ s!::!/!g; | |
47 | if (-f $filename) { | |
48 | eval { require $filename; }; | |
49 | if (!$@) { | |
50 | $plug->import (); | |
51 | push @plugins, $plug; | |
52 | } else { | |
c76d0106 | 53 | die $@; |
8187f6b0 DM |
54 | } |
55 | } | |
aaeeeebe DM |
56 | } |
57 | ||
58 | # helper functions | |
59 | ||
aaeeeebe DM |
60 | sub debugmsg { |
61 | my ($mtype, $msg, $logfd, $syslog) = @_; | |
62 | ||
a5c94797 | 63 | PVE::VZDump::Plugin::debugmsg(@_); |
aaeeeebe DM |
64 | } |
65 | ||
66 | sub run_command { | |
67 | my ($logfd, $cmdstr, %param) = @_; | |
68 | ||
7f910306 | 69 | my $logfunc = sub { |
4a4051d8 | 70 | my $line = shift; |
4a4051d8 | 71 | debugmsg ('info', $line, $logfd); |
aaeeeebe DM |
72 | }; |
73 | ||
7f910306 | 74 | PVE::Tools::run_command($cmdstr, %param, logfunc => $logfunc); |
aaeeeebe DM |
75 | } |
76 | ||
facf65a6 FE |
77 | my $verify_notes_template = sub { |
78 | my ($template) = @_; | |
79 | ||
80 | die "contains a line feed\n" if $template =~ /\n/; | |
81 | ||
82 | my @problematic = (); | |
83 | while ($template =~ /\\(.)/g) { | |
84 | my $char = $1; | |
85 | push @problematic, "escape sequence '\\$char' at char " . (pos($template) - 2) | |
86 | if $char !~ /^[n\\]$/; | |
87 | } | |
88 | ||
89 | while ($template =~ /\{\{([^\s{}]+)\}\}/g) { | |
90 | my $var = $1; | |
91 | push @problematic, "variable '$var' at char " . (pos($template) - length($var)) | |
92 | if $var !~ /^(cluster|guestname|node|vmid)$/; | |
93 | } | |
94 | ||
95 | die "found unknown: " . join(', ', @problematic) . "\n" if scalar(@problematic); | |
96 | }; | |
97 | ||
e01438a7 FE |
98 | my $generate_notes = sub { |
99 | my ($notes_template, $task) = @_; | |
100 | ||
facf65a6 FE |
101 | $verify_notes_template->($notes_template); |
102 | ||
e01438a7 | 103 | my $info = { |
2078e359 FE |
104 | cluster => PVE::Cluster::get_clinfo()->{cluster}->{name} // 'standalone node', |
105 | guestname => $task->{hostname} // "VM $task->{vmid}", # is always set for CTs | |
e01438a7 FE |
106 | node => PVE::INotify::nodename(), |
107 | vmid => $task->{vmid}, | |
108 | }; | |
109 | ||
31213d61 FE |
110 | my $unescape = sub { |
111 | my ($char) = @_; | |
112 | return '\\' if $char eq '\\'; | |
113 | return "\n" if $char eq 'n'; | |
114 | die "unexpected escape character '$char'\n"; | |
e01438a7 FE |
115 | }; |
116 | ||
31213d61 | 117 | $notes_template =~ s/\\(.)/$unescape->($1)/eg; |
e01438a7 FE |
118 | |
119 | my $vars = join('|', keys $info->%*); | |
4ed6e1b1 | 120 | $notes_template =~ s/\{\{($vars)\}\}/$info->{$1}/g; |
e01438a7 FE |
121 | |
122 | return $notes_template; | |
123 | }; | |
124 | ||
93880785 FE |
125 | my sub parse_performance { |
126 | my ($param) = @_; | |
127 | ||
128 | if (defined(my $perf = $param->{performance})) { | |
129 | return if ref($perf) eq 'HASH'; # already parsed | |
130 | $param->{performance} = PVE::JSONSchema::parse_property_string('backup-performance', $perf); | |
131 | } | |
132 | } | |
133 | ||
1c56bcf3 FE |
134 | my $parse_prune_backups_maxfiles = sub { |
135 | my ($param, $kind) = @_; | |
136 | ||
137 | my $maxfiles = delete $param->{maxfiles}; | |
138 | my $prune_backups = $param->{'prune-backups'}; | |
139 | ||
fb6871fc | 140 | debugmsg('warn', "both 'maxfiles' and 'prune-backups' defined as ${kind} - ignoring 'maxfiles'") |
1c56bcf3 FE |
141 | if defined($maxfiles) && defined($prune_backups); |
142 | ||
143 | if (defined($prune_backups)) { | |
ecc08c34 | 144 | return if ref($prune_backups) eq 'HASH'; # already parsed |
1c56bcf3 FE |
145 | $param->{'prune-backups'} = PVE::JSONSchema::parse_property_string( |
146 | 'prune-backups', | |
147 | $prune_backups | |
148 | ); | |
149 | } elsif (defined($maxfiles)) { | |
150 | if ($maxfiles) { | |
151 | $param->{'prune-backups'} = { 'keep-last' => $maxfiles }; | |
152 | } else { | |
153 | $param->{'prune-backups'} = { 'keep-all' => 1 }; | |
154 | } | |
155 | } | |
156 | }; | |
157 | ||
aaeeeebe DM |
158 | sub storage_info { |
159 | my $storage = shift; | |
160 | ||
bbcfdc08 | 161 | my $cfg = PVE::Storage::config(); |
4a4051d8 | 162 | my $scfg = PVE::Storage::storage_config($cfg, $storage); |
aaeeeebe | 163 | my $type = $scfg->{type}; |
60e049c2 | 164 | |
60e049c2 | 165 | die "can't use storage '$storage' for backups - wrong content type\n" |
aaeeeebe DM |
166 | if (!$scfg->{content}->{backup}); |
167 | ||
3a805f8d FE |
168 | my $info = { |
169 | scfg => $scfg, | |
3a805f8d FE |
170 | }; |
171 | ||
e6946086 FE |
172 | $info->{'prune-backups'} = PVE::JSONSchema::parse_property_string('prune-backups', $scfg->{'prune-backups'}) |
173 | if defined($scfg->{'prune-backups'}); | |
174 | ||
1a87db9e | 175 | if ($type eq 'pbs') { |
3a805f8d | 176 | $info->{pbs} = 1; |
1a87db9e | 177 | } else { |
3a805f8d | 178 | $info->{dumpdir} = PVE::Storage::get_backup_dir($cfg, $storage); |
1a87db9e | 179 | } |
3a805f8d FE |
180 | |
181 | return $info; | |
aaeeeebe DM |
182 | } |
183 | ||
184 | sub format_size { | |
185 | my $size = shift; | |
186 | ||
187 | my $kb = $size / 1024; | |
188 | ||
189 | if ($kb < 1024) { | |
190 | return int ($kb) . "KB"; | |
191 | } | |
192 | ||
193 | my $mb = $size / (1024*1024); | |
aaeeeebe DM |
194 | if ($mb < 1024) { |
195 | return int ($mb) . "MB"; | |
9c3a51ee TL |
196 | } |
197 | my $gb = $mb / 1024; | |
198 | if ($gb < 1024) { | |
aaeeeebe | 199 | return sprintf ("%.2fGB", $gb); |
60e049c2 | 200 | } |
9c3a51ee TL |
201 | my $tb = $gb / 1024; |
202 | return sprintf ("%.2fTB", $tb); | |
aaeeeebe DM |
203 | } |
204 | ||
205 | sub format_time { | |
206 | my $seconds = shift; | |
207 | ||
208 | my $hours = int ($seconds/3600); | |
209 | $seconds = $seconds - $hours*3600; | |
210 | my $min = int ($seconds/60); | |
211 | $seconds = $seconds - $min*60; | |
212 | ||
213 | return sprintf ("%02d:%02d:%02d", $hours, $min, $seconds); | |
214 | } | |
215 | ||
216 | sub encode8bit { | |
217 | my ($str) = @_; | |
218 | ||
219 | $str =~ s/^(.{990})/$1\n/mg; # reduce line length | |
220 | ||
221 | return $str; | |
222 | } | |
223 | ||
224 | sub escape_html { | |
225 | my ($str) = @_; | |
226 | ||
227 | $str =~ s/&/&/g; | |
228 | $str =~ s/</</g; | |
229 | $str =~ s/>/>/g; | |
230 | ||
231 | return $str; | |
232 | } | |
233 | ||
234 | sub check_bin { | |
235 | my ($bin) = @_; | |
236 | ||
237 | foreach my $p (split (/:/, $ENV{PATH})) { | |
238 | my $fn = "$p/$bin"; | |
239 | if (-x $fn) { | |
240 | return $fn; | |
241 | } | |
242 | } | |
243 | ||
244 | die "unable to find command '$bin'\n"; | |
245 | } | |
246 | ||
247 | sub check_vmids { | |
248 | my (@vmids) = @_; | |
249 | ||
250 | my $res = []; | |
50ba40ec | 251 | for my $vmid (sort {$a <=> $b} @vmids) { |
aaeeeebe DM |
252 | die "ERROR: strange VM ID '${vmid}'\n" if $vmid !~ m/^\d+$/; |
253 | $vmid = int ($vmid); # remove leading zeros | |
4a4051d8 | 254 | next if !$vmid; |
aaeeeebe DM |
255 | push @$res, $vmid; |
256 | } | |
257 | ||
258 | return $res; | |
259 | } | |
260 | ||
261 | ||
262 | sub read_vzdump_defaults { | |
263 | ||
264 | my $fn = "/etc/vzdump.conf"; | |
265 | ||
cc61ea36 | 266 | my $defaults = { |
1ab98f05 WB |
267 | map { |
268 | my $default = $confdesc->{$_}->{default}; | |
269 | defined($default) ? ($_ => $default) : () | |
3f488b61 | 270 | } keys %$confdesc_for_defaults |
aaeeeebe | 271 | }; |
dd5d80f5 | 272 | $parse_prune_backups_maxfiles->($defaults, "defaults in VZDump schema"); |
93880785 | 273 | parse_performance($defaults); |
aaeeeebe | 274 | |
cc61ea36 TL |
275 | my $raw; |
276 | eval { $raw = PVE::Tools::file_get_contents($fn); }; | |
277 | return $defaults if $@; | |
aaeeeebe | 278 | |
3f488b61 | 279 | my $conf_schema = { type => 'object', properties => $confdesc_for_defaults }; |
cc61ea36 | 280 | my $res = PVE::JSONSchema::parse_config($conf_schema, $fn, $raw); |
6368bbd4 WB |
281 | if (my $excludes = $res->{'exclude-path'}) { |
282 | $res->{'exclude-path'} = PVE::Tools::split_args($excludes); | |
283 | } | |
74b47fd8 | 284 | if (defined($res->{mailto})) { |
05447e04 | 285 | my @mailto = split_list($res->{mailto}); |
74b47fd8 FG |
286 | $res->{mailto} = [ @mailto ]; |
287 | } | |
dd5d80f5 | 288 | $parse_prune_backups_maxfiles->($res, "options in '$fn'"); |
93880785 | 289 | parse_performance($res); |
cc61ea36 TL |
290 | |
291 | foreach my $key (keys %$defaults) { | |
e94fa5cb | 292 | $res->{$key} = $defaults->{$key} if !defined($res->{$key}); |
aaeeeebe | 293 | } |
aaeeeebe | 294 | |
f1dd7629 FE |
295 | if (defined($res->{storage}) && defined($res->{dumpdir})) { |
296 | debugmsg('warn', "both 'storage' and 'dumpdir' defined in '$fn' - ignoring 'dumpdir'"); | |
297 | delete $res->{dumpdir}; | |
298 | } | |
299 | ||
aaeeeebe DM |
300 | return $res; |
301 | } | |
302 | ||
ce84a950 | 303 | use constant MAX_MAIL_SIZE => 1024*1024; |
6ec9de44 | 304 | sub sendmail { |
44b21574 | 305 | my ($self, $tasklist, $totaltime, $err, $detail_pre, $detail_post) = @_; |
aaeeeebe DM |
306 | |
307 | my $opts = $self->{opts}; | |
308 | ||
309 | my $mailto = $opts->{mailto}; | |
310 | ||
4a4051d8 | 311 | return if !($mailto && scalar(@$mailto)); |
aaeeeebe DM |
312 | |
313 | my $cmdline = $self->{cmdline}; | |
314 | ||
315 | my $ecount = 0; | |
316 | foreach my $task (@$tasklist) { | |
317 | $ecount++ if $task->{state} ne 'ok'; | |
318 | chomp $task->{msg} if $task->{msg}; | |
319 | $task->{backuptime} = 0 if !$task->{backuptime}; | |
320 | $task->{size} = 0 if !$task->{size}; | |
6cba1855 | 321 | $task->{target} = 'unknown' if !$task->{target}; |
aaeeeebe DM |
322 | $task->{hostname} = "VM $task->{vmid}" if !$task->{hostname}; |
323 | ||
324 | if ($task->{state} eq 'todo') { | |
325 | $task->{msg} = 'aborted'; | |
326 | } | |
327 | } | |
328 | ||
02e59759 DM |
329 | my $notify = $opts->{mailnotification} || 'always'; |
330 | return if (!$ecount && !$err && ($notify eq 'failure')); | |
8b4b4860 | 331 | |
6ec9de44 | 332 | my $stat = ($ecount || $err) ? 'backup failed' : 'backup successful'; |
403761c4 WB |
333 | if ($err) { |
334 | if ($err =~ /\n/) { | |
335 | $stat .= ": multiple problems"; | |
336 | } else { | |
337 | $stat .= ": $err"; | |
338 | $err = undef; | |
339 | } | |
340 | } | |
aaeeeebe | 341 | |
4a4051d8 | 342 | my $hostname = `hostname -f` || PVE::INotify::nodename(); |
aaeeeebe DM |
343 | chomp $hostname; |
344 | ||
aaeeeebe | 345 | # text part |
403761c4 | 346 | my $text = $err ? "$err\n\n" : ''; |
2cc64d0e DJ |
347 | my $namelength = 20; |
348 | $text .= sprintf ( | |
349 | "%-10s %-${namelength}s %-6s %10s %10s %s\n", | |
350 | qw(VMID NAME STATUS TIME SIZE FILENAME) | |
351 | ); | |
aaeeeebe | 352 | foreach my $task (@$tasklist) { |
2cc64d0e DJ |
353 | my $name = substr($task->{hostname}, 0, $namelength); |
354 | my $successful = $task->{state} eq 'ok'; | |
355 | my $size = $successful ? format_size ($task->{size}) : 0; | |
356 | my $filename = $successful ? $task->{target} : '-'; | |
357 | my $size_fmt = $successful ? "%10s": "%8.2fMB"; | |
358 | $text .= sprintf( | |
359 | "%-10s %-${namelength}s %-6s %10s $size_fmt %s\n", | |
360 | $task->{vmid}, | |
361 | $name, | |
362 | $task->{state}, | |
363 | format_time($task->{backuptime}), | |
364 | $size, | |
365 | $filename, | |
366 | ); | |
aaeeeebe | 367 | } |
7a0a8e67 | 368 | |
ce84a950 DJ |
369 | my $text_log_part; |
370 | $text_log_part .= "\nDetailed backup logs:\n\n"; | |
371 | $text_log_part .= "$cmdline\n\n"; | |
aaeeeebe | 372 | |
ce84a950 | 373 | $text_log_part .= $detail_pre . "\n" if defined($detail_pre); |
aaeeeebe DM |
374 | foreach my $task (@$tasklist) { |
375 | my $vmid = $task->{vmid}; | |
376 | my $log = $task->{tmplog}; | |
377 | if (!$log) { | |
ce84a950 | 378 | $text_log_part .= "$vmid: no log available\n\n"; |
aaeeeebe DM |
379 | next; |
380 | } | |
5620e576 TL |
381 | if (open (my $TMP, '<', "$log")) { |
382 | while (my $line = <$TMP>) { | |
ce84a950 DJ |
383 | next if $line =~ /^status: \d+/; # not useful in mails |
384 | $text_log_part .= encode8bit ("$vmid: $line"); | |
385 | } | |
5620e576 | 386 | close ($TMP); |
ce84a950 DJ |
387 | } else { |
388 | $text_log_part .= "$vmid: Could not open log file\n\n"; | |
389 | } | |
ce84a950 | 390 | $text_log_part .= "\n"; |
aaeeeebe | 391 | } |
ce84a950 | 392 | $text_log_part .= $detail_post if defined($detail_post); |
aaeeeebe | 393 | |
aaeeeebe | 394 | # html part |
7a0a8e67 | 395 | my $html = "<html><body>\n"; |
403761c4 | 396 | $html .= "<p>" . (escape_html($err) =~ s/\n/<br>/gr) . "</p>\n" if $err; |
7a0a8e67 TL |
397 | $html .= "<table border=1 cellpadding=3>\n"; |
398 | $html .= "<tr><td>VMID<td>NAME<td>STATUS<td>TIME<td>SIZE<td>FILENAME</tr>\n"; | |
aaeeeebe DM |
399 | |
400 | my $ssize = 0; | |
aaeeeebe DM |
401 | foreach my $task (@$tasklist) { |
402 | my $vmid = $task->{vmid}; | |
403 | my $name = $task->{hostname}; | |
404 | ||
405 | if ($task->{state} eq 'ok') { | |
aaeeeebe DM |
406 | $ssize += $task->{size}; |
407 | ||
1353489e TL |
408 | $html .= sprintf ( |
409 | "<tr><td>%s<td>%s<td>OK<td>%s<td align=right>%s<td>%s</tr>\n", | |
410 | $vmid, | |
411 | $name, | |
412 | format_time($task->{backuptime}), | |
413 | format_size ($task->{size}), | |
414 | escape_html ($task->{target}), | |
415 | ); | |
aaeeeebe | 416 | } else { |
1353489e TL |
417 | $html .= sprintf ( |
418 | "<tr><td>%s<td>%s<td><font color=red>FAILED<td>%s<td colspan=2>%s</tr>\n", | |
419 | $vmid, | |
420 | $name, | |
421 | format_time($task->{backuptime}), | |
422 | escape_html ($task->{msg}), | |
423 | ); | |
aaeeeebe DM |
424 | } |
425 | } | |
426 | ||
7a0a8e67 | 427 | $html .= sprintf ("<tr><td align=left colspan=3>TOTAL<td>%s<td>%s<td></tr>", |
aaeeeebe DM |
428 | format_time ($totaltime), format_size ($ssize)); |
429 | ||
ce84a950 DJ |
430 | $html .= "\n</table><br><br>\n"; |
431 | my $html_log_part; | |
432 | $html_log_part .= "Detailed backup logs:<br /><br />\n"; | |
433 | $html_log_part .= "<pre>\n"; | |
434 | $html_log_part .= escape_html($cmdline) . "\n\n"; | |
aaeeeebe | 435 | |
ce84a950 | 436 | $html_log_part .= escape_html($detail_pre) . "\n" if defined($detail_pre); |
aaeeeebe DM |
437 | foreach my $task (@$tasklist) { |
438 | my $vmid = $task->{vmid}; | |
439 | my $log = $task->{tmplog}; | |
440 | if (!$log) { | |
ce84a950 | 441 | $html_log_part .= "$vmid: no log available\n\n"; |
aaeeeebe DM |
442 | next; |
443 | } | |
5620e576 TL |
444 | if (open (my $TMP, '<', "$log")) { |
445 | while (my $line = <$TMP>) { | |
ce84a950 DJ |
446 | next if $line =~ /^status: \d+/; # not useful in mails |
447 | if ($line =~ m/^\S+\s\d+\s+\d+:\d+:\d+\s+(ERROR|WARN):/) { | |
448 | $html_log_part .= encode8bit ("$vmid: <font color=red>". | |
449 | escape_html ($line) . "</font>"); | |
450 | } else { | |
451 | $html_log_part .= encode8bit ("$vmid: " . escape_html ($line)); | |
452 | } | |
aaeeeebe | 453 | } |
5620e576 | 454 | close ($TMP); |
ce84a950 DJ |
455 | } else { |
456 | $html_log_part .= "$vmid: Could not open log file\n\n"; | |
aaeeeebe | 457 | } |
ce84a950 | 458 | $html_log_part .= "\n"; |
aaeeeebe | 459 | } |
ce84a950 DJ |
460 | $html_log_part .= escape_html($detail_post) if defined($detail_post); |
461 | $html_log_part .= "</pre>"; | |
213e0eb2 | 462 | my $html_end = "\n</body></html>\n"; |
7a0a8e67 | 463 | # end html part |
aaeeeebe | 464 | |
ce84a950 | 465 | if (length($text) + length($text_log_part) + |
213e0eb2 FE |
466 | length($html) + length($html_log_part) + |
467 | length($html_end) < MAX_MAIL_SIZE) | |
ce84a950 DJ |
468 | { |
469 | $html .= $html_log_part; | |
213e0eb2 | 470 | $html .= $html_end; |
ce84a950 DJ |
471 | $text .= $text_log_part; |
472 | } else { | |
473 | my $msg = "Log output was too long to be sent by mail. ". | |
474 | "See Task History for details!\n"; | |
475 | $text .= $msg; | |
476 | $html .= "<p>$msg</p>"; | |
477 | $html .= $html_end; | |
478 | } | |
479 | ||
7a0a8e67 | 480 | my $subject = "vzdump backup status ($hostname) : $stat"; |
aaeeeebe | 481 | |
7a0a8e67 TL |
482 | my $dcconf = PVE::Cluster::cfs_read_file('datacenter.cfg'); |
483 | my $mailfrom = $dcconf->{email_from} || "root"; | |
aaeeeebe | 484 | |
7a0a8e67 | 485 | PVE::Tools::sendmail($mailto, $subject, $text, $html, $mailfrom, "vzdump backup tool"); |
aaeeeebe DM |
486 | }; |
487 | ||
488 | sub new { | |
a7e42354 | 489 | my ($class, $cmdline, $opts, $skiplist) = @_; |
aaeeeebe DM |
490 | |
491 | mkpath $logdir; | |
492 | ||
493 | check_bin ('cp'); | |
494 | check_bin ('df'); | |
495 | check_bin ('sendmail'); | |
496 | check_bin ('rsync'); | |
497 | check_bin ('tar'); | |
498 | check_bin ('mount'); | |
499 | check_bin ('umount'); | |
500 | check_bin ('cstream'); | |
501 | check_bin ('ionice'); | |
502 | ||
47664cbe | 503 | if ($opts->{mode} && $opts->{mode} eq 'snapshot') { |
aaeeeebe DM |
504 | check_bin ('lvcreate'); |
505 | check_bin ('lvs'); | |
506 | check_bin ('lvremove'); | |
507 | } | |
508 | ||
509 | my $defaults = read_vzdump_defaults(); | |
510 | ||
511 | foreach my $k (keys %$defaults) { | |
acc963c3 | 512 | next if $k eq 'exclude-path' || $k eq 'prune-backups'; # dealt with separately |
aaeeeebe DM |
513 | if ($k eq 'dumpdir' || $k eq 'storage') { |
514 | $opts->{$k} = $defaults->{$k} if !defined ($opts->{dumpdir}) && | |
515 | !defined ($opts->{storage}); | |
516 | } else { | |
517 | $opts->{$k} = $defaults->{$k} if !defined ($opts->{$k}); | |
518 | } | |
519 | } | |
520 | ||
aaeeeebe DM |
521 | $opts->{dumpdir} =~ s|/+$|| if ($opts->{dumpdir}); |
522 | $opts->{tmpdir} =~ s|/+$|| if ($opts->{tmpdir}); | |
523 | ||
a7e42354 | 524 | $skiplist = [] if !$skiplist; |
94009776 TL |
525 | my $self = bless { |
526 | cmdline => $cmdline, | |
527 | opts => $opts, | |
528 | skiplist => $skiplist, | |
529 | }, $class; | |
aaeeeebe | 530 | |
45f16a06 | 531 | my $findexcl = $self->{findexcl} = []; |
f4a8bab4 | 532 | if ($defaults->{'exclude-path'}) { |
45f16a06 | 533 | push @$findexcl, @{$defaults->{'exclude-path'}}; |
f4a8bab4 DM |
534 | } |
535 | ||
aaeeeebe | 536 | if ($opts->{'exclude-path'}) { |
45f16a06 | 537 | push @$findexcl, @{$opts->{'exclude-path'}}; |
aaeeeebe DM |
538 | } |
539 | ||
540 | if ($opts->{stdexcludes}) { | |
1353489e TL |
541 | push @$findexcl, |
542 | '/tmp/?*', | |
543 | '/var/tmp/?*', | |
544 | '/var/run/?*.pid', | |
545 | ; | |
aaeeeebe DM |
546 | } |
547 | ||
548 | foreach my $p (@plugins) { | |
1353489e | 549 | my $pd = $p->new($self); |
aaeeeebe DM |
550 | |
551 | push @{$self->{plugins}}, $pd; | |
aaeeeebe DM |
552 | } |
553 | ||
1a87db9e | 554 | if (defined($opts->{storage}) && $opts->{stdout}) { |
a6fcd7d9 FE |
555 | die "cannot use options 'storage' and 'stdout' at the same time\n"; |
556 | } elsif (defined($opts->{storage}) && defined($opts->{dumpdir})) { | |
557 | die "cannot use options 'storage' and 'dumpdir' at the same time\n"; | |
1a87db9e DM |
558 | } |
559 | ||
aaeeeebe | 560 | if (!$opts->{dumpdir} && !$opts->{storage}) { |
19d5c0f2 | 561 | $opts->{storage} = 'local'; |
aaeeeebe DM |
562 | } |
563 | ||
3f488b61 FE |
564 | # Enforced by the API too, but these options might come in via defaults. Drop them if necessary. |
565 | if (!$opts->{storage}) { | |
566 | delete $opts->{$_} for qw(notes-template protected); | |
567 | } | |
568 | ||
403761c4 | 569 | my $errors = ''; |
3c5a7616 FE |
570 | my $add_error = sub { |
571 | my ($error) = @_; | |
572 | $errors .= "\n" if $errors; | |
573 | chomp($error); | |
574 | $errors .= $error; | |
575 | }; | |
403761c4 | 576 | |
c527d28f FE |
577 | eval { |
578 | $self->{job_init_log} = ''; | |
579 | open my $job_init_fd, '>', \$self->{job_init_log}; | |
580 | $self->run_hook_script('job-init', undef, $job_init_fd); | |
581 | close $job_init_fd; | |
582 | ||
583 | PVE::Cluster::cfs_update(); # Pick up possible changes made by the hook script. | |
584 | }; | |
585 | $add_error->($@) if $@; | |
586 | ||
aaeeeebe | 587 | if ($opts->{storage}) { |
164651da FE |
588 | my $storage_cfg = PVE::Storage::config(); |
589 | eval { PVE::Storage::activate_storage($storage_cfg, $opts->{storage}) }; | |
3c5a7616 | 590 | $add_error->("could not activate storage '$opts->{storage}': $@") if $@; |
164651da | 591 | |
1e4583d5 | 592 | my $info = eval { storage_info ($opts->{storage}) }; |
e6946086 | 593 | if (my $err = $@) { |
3c5a7616 | 594 | $add_error->("could not get storage information for '$opts->{storage}': $err"); |
e6946086 FE |
595 | } else { |
596 | $opts->{dumpdir} = $info->{dumpdir}; | |
597 | $opts->{scfg} = $info->{scfg}; | |
598 | $opts->{pbs} = $info->{pbs}; | |
acc963c3 | 599 | $opts->{'prune-backups'} //= $info->{'prune-backups'}; |
e6946086 | 600 | } |
aaeeeebe | 601 | } elsif ($opts->{dumpdir}) { |
3c5a7616 | 602 | $add_error->("dumpdir '$opts->{dumpdir}' does not exist") |
aaeeeebe DM |
603 | if ! -d $opts->{dumpdir}; |
604 | } else { | |
60e049c2 | 605 | die "internal error"; |
aaeeeebe DM |
606 | } |
607 | ||
acc963c3 | 608 | $opts->{'prune-backups'} //= $defaults->{'prune-backups'}; |
5b6b72e6 | 609 | |
1db8529e FE |
610 | # avoid triggering any remove code path if keep-all is set |
611 | $opts->{remove} = 0 if $opts->{'prune-backups'}->{'keep-all'}; | |
612 | ||
aaeeeebe | 613 | if ($opts->{tmpdir} && ! -d $opts->{tmpdir}) { |
3c5a7616 | 614 | $add_error->("tmpdir '$opts->{tmpdir}' does not exist"); |
403761c4 WB |
615 | } |
616 | ||
617 | if ($errors) { | |
618 | eval { $self->sendmail([], 0, $errors); }; | |
619 | debugmsg ('err', $@) if $@; | |
620 | die "$errors\n"; | |
aaeeeebe DM |
621 | } |
622 | ||
623 | return $self; | |
aaeeeebe DM |
624 | } |
625 | ||
aaeeeebe DM |
626 | sub get_mount_info { |
627 | my ($dir) = @_; | |
628 | ||
8572d646 DM |
629 | # Note: df 'available' can be negative, and percentage set to '-' |
630 | ||
d93d0459 | 631 | my $cmd = [ 'df', '-P', '-T', '-B', '1', $dir]; |
aaeeeebe | 632 | |
d93d0459 | 633 | my $res; |
aaeeeebe | 634 | |
d93d0459 DM |
635 | my $parser = sub { |
636 | my $line = shift; | |
8572d646 DM |
637 | if (my ($fsid, $fstype, undef, $mp) = $line =~ |
638 | m!(\S+.*)\s+(\S+)\s+\d+\s+\-?\d+\s+\d+\s+(\d+%|-)\s+(/.*)$!) { | |
d93d0459 DM |
639 | $res = { |
640 | device => $fsid, | |
641 | fstype => $fstype, | |
642 | mountpoint => $mp, | |
643 | }; | |
644 | } | |
aaeeeebe | 645 | }; |
d93d0459 DM |
646 | |
647 | eval { PVE::Tools::run_command($cmd, errfunc => sub {}, outfunc => $parser); }; | |
648 | warn $@ if $@; | |
649 | ||
650 | return $res; | |
aaeeeebe DM |
651 | } |
652 | ||
aaeeeebe | 653 | sub getlock { |
eab837c4 | 654 | my ($self, $upid) = @_; |
aaeeeebe | 655 | |
8682f6fc | 656 | my $fh; |
60e049c2 | 657 | |
aaeeeebe | 658 | my $maxwait = $self->{opts}->{lockwait} || $self->{lockwait}; |
60e049c2 | 659 | |
dc6e0a52 | 660 | die "missing UPID" if !$upid; # should not happen |
eab837c4 | 661 | |
5620e576 TL |
662 | my $SERVER_FLCK; |
663 | if (!open ($SERVER_FLCK, '>>', "$lockfile")) { | |
aaeeeebe | 664 | debugmsg ('err', "can't open lock on file '$lockfile' - $!", undef, 1); |
6ec9de44 | 665 | die "can't open lock on file '$lockfile' - $!"; |
aaeeeebe DM |
666 | } |
667 | ||
5620e576 | 668 | if (!flock ($SERVER_FLCK, LOCK_EX|LOCK_NB)) { |
eab837c4 | 669 | if (!$maxwait) { |
76189130 TL |
670 | debugmsg ('err', "can't acquire lock '$lockfile' (wait = 0)", undef, 1); |
671 | die "can't acquire lock '$lockfile' (wait = 0)"; | |
eab837c4 | 672 | } |
aaeeeebe | 673 | |
eab837c4 | 674 | debugmsg('info', "trying to get global lock - waiting...", undef, 1); |
eab837c4 DM |
675 | eval { |
676 | alarm ($maxwait * 60); | |
60e049c2 | 677 | |
eab837c4 | 678 | local $SIG{ALRM} = sub { alarm (0); die "got timeout\n"; }; |
aaeeeebe | 679 | |
5620e576 | 680 | if (!flock ($SERVER_FLCK, LOCK_EX)) { |
eab837c4 | 681 | my $err = $!; |
5620e576 | 682 | close ($SERVER_FLCK); |
eab837c4 DM |
683 | alarm (0); |
684 | die "$err\n"; | |
685 | } | |
aaeeeebe | 686 | alarm (0); |
eab837c4 | 687 | }; |
aaeeeebe | 688 | alarm (0); |
60e049c2 | 689 | |
eab837c4 | 690 | my $err = $@; |
60e049c2 | 691 | |
eab837c4 | 692 | if ($err) { |
76189130 TL |
693 | debugmsg ('err', "can't acquire lock '$lockfile' - $err", undef, 1); |
694 | die "can't acquire lock '$lockfile' - $err"; | |
eab837c4 | 695 | } |
aaeeeebe | 696 | |
eab837c4 | 697 | debugmsg('info', "got global lock", undef, 1); |
aaeeeebe DM |
698 | } |
699 | ||
eab837c4 | 700 | PVE::Tools::file_set_contents($pidfile, $upid); |
875c2e5a FE |
701 | |
702 | return $SERVER_FLCK; | |
aaeeeebe DM |
703 | } |
704 | ||
705 | sub run_hook_script { | |
706 | my ($self, $phase, $task, $logfd) = @_; | |
707 | ||
708 | my $opts = $self->{opts}; | |
709 | ||
710 | my $script = $opts->{script}; | |
aaeeeebe DM |
711 | return if !$script; |
712 | ||
0ac20586 TL |
713 | die "Error: The hook script '$script' does not exist.\n" if ! -f $script; |
714 | die "Error: The hook script '$script' is not executable.\n" if ! -x $script; | |
59798893 | 715 | |
58f763e1 | 716 | my $cmd = [$script, $phase]; |
aaeeeebe | 717 | |
58f763e1 FG |
718 | if ($task) { |
719 | push @$cmd, $task->{mode}; | |
720 | push @$cmd, $task->{vmid}; | |
721 | } | |
aaeeeebe DM |
722 | |
723 | local %ENV; | |
28272ca2 DM |
724 | # set immutable opts directly (so they are available in all phases) |
725 | $ENV{STOREID} = $opts->{storage} if $opts->{storage}; | |
726 | $ENV{DUMPDIR} = $opts->{dumpdir} if $opts->{dumpdir}; | |
727 | ||
6cba1855 | 728 | foreach my $ek (qw(vmtype hostname target logfile)) { |
aaeeeebe DM |
729 | $ENV{uc($ek)} = $task->{$ek} if $task->{$ek}; |
730 | } | |
731 | ||
732 | run_command ($logfd, $cmd); | |
733 | } | |
734 | ||
d7550e09 | 735 | sub compressor_info { |
778d5b6d TL |
736 | my ($opts) = @_; |
737 | my $opt_compress = $opts->{compress}; | |
d7550e09 DM |
738 | |
739 | if (!$opt_compress || $opt_compress eq '0') { | |
740 | return undef; | |
741 | } elsif ($opt_compress eq '1' || $opt_compress eq 'lzo') { | |
742 | return ('lzop', 'lzo'); | |
743 | } elsif ($opt_compress eq 'gzip') { | |
778d5b6d | 744 | if ($opts->{pigz} > 0) { |
8555b100 TL |
745 | my $pigz_threads = $opts->{pigz}; |
746 | if ($pigz_threads == 1) { | |
747 | my $cpuinfo = PVE::ProcFSTools::read_cpuinfo(); | |
748 | $pigz_threads = int(($cpuinfo->{cpus} + 1)/2); | |
749 | } | |
819e4ff5 | 750 | return ("pigz -p ${pigz_threads} --rsyncable", 'gz'); |
778d5b6d | 751 | } else { |
e953f92a | 752 | return ('gzip --rsyncable', 'gz'); |
778d5b6d | 753 | } |
3e2c5105 AA |
754 | } elsif ($opt_compress eq 'zstd') { |
755 | my $zstd_threads = $opts->{zstd} // 1; | |
756 | if ($zstd_threads == 0) { | |
757 | my $cpuinfo = PVE::ProcFSTools::read_cpuinfo(); | |
758 | $zstd_threads = int(($cpuinfo->{cpus} + 1)/2); | |
759 | } | |
7420d7ff | 760 | return ("zstd --rsyncable --threads=${zstd_threads}", 'zst'); |
d7550e09 DM |
761 | } else { |
762 | die "internal error - unknown compression option '$opt_compress'"; | |
763 | } | |
764 | } | |
899b8373 | 765 | |
725c0555 | 766 | sub get_backup_file_list { |
0a9ca6ca | 767 | my ($dir, $bkname) = @_; |
899b8373 DM |
768 | |
769 | my $bklist = []; | |
770 | foreach my $fn (<$dir/${bkname}-*>) { | |
de2d177e FE |
771 | my $archive_info = eval { PVE::Storage::archive_info($fn) } // {}; |
772 | if ($archive_info->{is_std_name}) { | |
7bffbd22 | 773 | my $path = "$dir/$archive_info->{filename}"; |
de2d177e | 774 | my $backup = { |
7bffbd22 | 775 | 'path' => $path, |
de2d177e FE |
776 | 'ctime' => $archive_info->{ctime}, |
777 | }; | |
725c0555 FG |
778 | $backup->{mark} = "protected" |
779 | if -e PVE::Storage::protection_file_path($path); | |
de2d177e | 780 | push @{$bklist}, $backup; |
899b8373 DM |
781 | } |
782 | } | |
783 | ||
784 | return $bklist; | |
785 | } | |
60e049c2 | 786 | |
aaeeeebe DM |
787 | sub exec_backup_task { |
788 | my ($self, $task) = @_; | |
60e049c2 | 789 | |
aaeeeebe DM |
790 | my $opts = $self->{opts}; |
791 | ||
e6946086 | 792 | my $cfg = PVE::Storage::config(); |
aaeeeebe DM |
793 | my $vmid = $task->{vmid}; |
794 | my $plugin = $task->{plugin}; | |
1a87db9e DM |
795 | |
796 | $task->{backup_time} = time(); | |
797 | ||
798 | my $pbs_group_name; | |
799 | my $pbs_snapshot_name; | |
800 | ||
aaeeeebe | 801 | my $vmstarttime = time (); |
60e049c2 | 802 | |
aaeeeebe DM |
803 | my $logfd; |
804 | ||
805 | my $cleanup = {}; | |
806 | ||
4e0947c8 TL |
807 | my $log_vm_online_again = sub { |
808 | return if !defined($task->{vmstoptime}); | |
809 | $task->{vmconttime} //= time(); | |
810 | my $delay = $task->{vmconttime} - $task->{vmstoptime}; | |
310cde8b | 811 | $delay = '<1' if $delay < 1; |
4e0947c8 TL |
812 | debugmsg ('info', "guest is online again after $delay seconds", $logfd); |
813 | }; | |
aaeeeebe DM |
814 | |
815 | eval { | |
816 | die "unable to find VM '$vmid'\n" if !$plugin; | |
817 | ||
d51f5a1e FE |
818 | my $vmtype = $plugin->type(); |
819 | ||
820 | if ($self->{opts}->{pbs}) { | |
821 | if ($vmtype eq 'lxc') { | |
822 | $pbs_group_name = "ct/$vmid"; | |
823 | } elsif ($vmtype eq 'qemu') { | |
824 | $pbs_group_name = "vm/$vmid"; | |
825 | } else { | |
826 | die "pbs backup not implemented for plugin type '$vmtype'\n"; | |
827 | } | |
828 | my $btime = strftime("%FT%TZ", gmtime($task->{backup_time})); | |
829 | $pbs_snapshot_name = "$pbs_group_name/$btime"; | |
830 | } | |
831 | ||
2de3ee58 | 832 | # for now we deny backups of a running ha managed service in *stop* mode |
83ec8f81 | 833 | # as it interferes with the HA stack (started services should not stop). |
2de3ee58 | 834 | if ($opts->{mode} eq 'stop' && |
83ec8f81 | 835 | PVE::HA::Config::vm_is_ha_managed($vmid, 'started')) |
2de3ee58 TL |
836 | { |
837 | die "Cannot execute a backup with stop mode on a HA managed and". | |
838 | " enabled Service. Use snapshot mode or disable the Service.\n"; | |
839 | } | |
840 | ||
aaeeeebe DM |
841 | my $tmplog = "$logdir/$vmtype-$vmid.log"; |
842 | ||
aaeeeebe | 843 | my $bkname = "vzdump-$vmtype-$vmid"; |
1a87db9e | 844 | my $basename = $bkname . strftime("-%Y_%m_%d-%H_%M_%S", localtime($task->{backup_time})); |
aaeeeebe | 845 | |
e6946086 FE |
846 | my $prune_options = $opts->{'prune-backups'}; |
847 | ||
848 | my $backup_limit = 0; | |
b63baa39 | 849 | if (!$prune_options->{'keep-all'}) { |
1db8529e FE |
850 | foreach my $keep (values %{$prune_options}) { |
851 | $backup_limit += $keep; | |
852 | } | |
e6946086 | 853 | } |
899b8373 | 854 | |
bbd4cdd8 | 855 | if (($backup_limit && !$opts->{remove}) || $opts->{protected}) { |
1a87db9e | 856 | my $count; |
bbd4cdd8 | 857 | my $protected_count; |
7bffbd22 | 858 | if (my $storeid = $opts->{storage}) { |
bbd4cdd8 FE |
859 | my @backups = grep { |
860 | !$_->{subtype} || $_->{subtype} eq $vmtype | |
861 | } PVE::Storage::volume_list($cfg, $storeid, $vmid, 'backup')->@*; | |
862 | ||
863 | $count = grep { !$_->{protected} } @backups; | |
864 | $protected_count = scalar(@backups) - $count; | |
1a87db9e | 865 | } else { |
725c0555 | 866 | $count = grep { !$_->{mark} || $_->{mark} ne "protected" } get_backup_file_list($opts->{dumpdir}, $bkname)->@*; |
1a87db9e | 867 | } |
7bffbd22 | 868 | |
bbd4cdd8 FE |
869 | if ($opts->{protected}) { |
870 | my $max_protected = PVE::Storage::get_max_protected_backups( | |
871 | $opts->{scfg}, | |
872 | $opts->{storage}, | |
873 | ); | |
874 | if ($max_protected > -1 && $protected_count >= $max_protected) { | |
875 | die "The number of protected backups per guest is limited to $max_protected ". | |
876 | "on storage '$opts->{storage}'\n"; | |
877 | } | |
878 | } elsif ($count >= $backup_limit) { | |
879 | die "There is a max backup limit of $backup_limit enforced by the target storage ". | |
880 | "or the vzdump parameters. Either increase the limit or delete old backups.\n"; | |
881 | } | |
899b8373 DM |
882 | } |
883 | ||
7ddcb3af | 884 | if (!$self->{opts}->{pbs}) { |
1a87db9e DM |
885 | $task->{logfile} = "$opts->{dumpdir}/$basename.log"; |
886 | } | |
aaeeeebe | 887 | |
757fd3d5 | 888 | my $ext = $vmtype eq 'qemu' ? '.vma' : '.tar'; |
778d5b6d | 889 | my ($comp, $comp_ext) = compressor_info($opts); |
d7550e09 DM |
890 | if ($comp && $comp_ext) { |
891 | $ext .= ".${comp_ext}"; | |
892 | } | |
aaeeeebe | 893 | |
7ddcb3af | 894 | if ($self->{opts}->{pbs}) { |
1a87db9e | 895 | die "unable to pipe backup to stdout\n" if $opts->{stdout}; |
b3c3304f | 896 | $task->{target} = $pbs_snapshot_name; |
aaeeeebe | 897 | } else { |
1a87db9e | 898 | if ($opts->{stdout}) { |
6cba1855 | 899 | $task->{target} = '-'; |
1a87db9e | 900 | } else { |
6cba1855 | 901 | $task->{target} = $task->{tmptar} = "$opts->{dumpdir}/$basename$ext"; |
1a87db9e DM |
902 | $task->{tmptar} =~ s/\.[^\.]+$/\.dat/; |
903 | unlink $task->{tmptar}; | |
904 | } | |
aaeeeebe DM |
905 | } |
906 | ||
907 | $task->{vmtype} = $vmtype; | |
908 | ||
dfb16115 | 909 | my $pid = $$; |
01fc3672 | 910 | if ($opts->{tmpdir}) { |
dfb16115 | 911 | $task->{tmpdir} = "$opts->{tmpdir}/vzdumptmp${pid}_$vmid/"; |
01fc3672 | 912 | } elsif ($self->{opts}->{pbs}) { |
dfb16115 | 913 | $task->{tmpdir} = "/var/tmp/vzdumptmp${pid}_$vmid"; |
aaeeeebe DM |
914 | } else { |
915 | # dumpdir is posix? then use it as temporary dir | |
5dc86eb8 | 916 | my $info = get_mount_info($opts->{dumpdir}); |
60e049c2 | 917 | if ($vmtype eq 'qemu' || |
aaeeeebe DM |
918 | grep ($_ eq $info->{fstype}, @posix_filesystems)) { |
919 | $task->{tmpdir} = "$opts->{dumpdir}/$basename.tmp"; | |
920 | } else { | |
dfb16115 | 921 | $task->{tmpdir} = "/var/tmp/vzdumptmp${pid}_$vmid"; |
aaeeeebe DM |
922 | debugmsg ('info', "filesystem type on dumpdir is '$info->{fstype}' -" . |
923 | "using $task->{tmpdir} for temporary files", $logfd); | |
924 | } | |
925 | } | |
926 | ||
927 | rmtree $task->{tmpdir}; | |
928 | mkdir $task->{tmpdir}; | |
929 | -d $task->{tmpdir} || | |
930 | die "unable to create temporary directory '$task->{tmpdir}'"; | |
931 | ||
932 | $logfd = IO::File->new (">$tmplog") || | |
933 | die "unable to create log file '$tmplog'"; | |
934 | ||
935 | $task->{dumpdir} = $opts->{dumpdir}; | |
ff00abe6 | 936 | $task->{storeid} = $opts->{storage}; |
1a87db9e | 937 | $task->{scfg} = $opts->{scfg}; |
aaeeeebe DM |
938 | $task->{tmplog} = $tmplog; |
939 | ||
1a87db9e | 940 | unlink $task->{logfile} if defined($task->{logfile}); |
aaeeeebe | 941 | |
29f26f6d DJ |
942 | debugmsg ('info', "Starting Backup of VM $vmid ($vmtype)", $logfd, 1); |
943 | debugmsg ('info', "Backup started at " . strftime("%F %H:%M:%S", localtime())); | |
aaeeeebe DM |
944 | |
945 | $plugin->set_logfd ($logfd); | |
946 | ||
947 | # test is VM is running | |
948 | my ($running, $status_text) = $plugin->vm_status ($vmid); | |
949 | ||
950 | debugmsg ('info', "status = ${status_text}", $logfd); | |
951 | ||
952 | # lock VM (prevent config changes) | |
953 | $plugin->lock_vm ($vmid); | |
954 | ||
955 | $cleanup->{unlock} = 1; | |
956 | ||
957 | # prepare | |
958 | ||
6acb632a | 959 | my $mode = $running ? $task->{mode} : 'stop'; |
aaeeeebe DM |
960 | |
961 | if ($mode eq 'snapshot') { | |
962 | my %saved_task = %$task; | |
963 | eval { $plugin->prepare ($task, $vmid, $mode); }; | |
964 | if (my $err = $@) { | |
965 | die $err if $err !~ m/^mode failure/; | |
966 | debugmsg ('info', $err, $logfd); | |
967 | debugmsg ('info', "trying 'suspend' mode instead", $logfd); | |
968 | $mode = 'suspend'; # so prepare is called again below | |
60e049c2 | 969 | %$task = %saved_task; |
aaeeeebe DM |
970 | } |
971 | } | |
972 | ||
6acb632a WB |
973 | $cleanup->{prepared} = 1; |
974 | ||
aaeeeebe DM |
975 | $task->{mode} = $mode; |
976 | ||
977 | debugmsg ('info', "backup mode: $mode", $logfd); | |
ff119724 | 978 | debugmsg ('info', "bandwidth limit: $opts->{bwlimit} KB/s", $logfd) if $opts->{bwlimit}; |
aaeeeebe DM |
979 | debugmsg ('info', "ionice priority: $opts->{ionice}", $logfd); |
980 | ||
981 | if ($mode eq 'stop') { | |
aaeeeebe DM |
982 | $plugin->prepare ($task, $vmid, $mode); |
983 | ||
984 | $self->run_hook_script ('backup-start', $task, $logfd); | |
985 | ||
986 | if ($running) { | |
4922b7a9 | 987 | debugmsg ('info', "stopping virtual guest", $logfd); |
4e0947c8 | 988 | $task->{vmstoptime} = time(); |
aaeeeebe DM |
989 | $self->run_hook_script ('pre-stop', $task, $logfd); |
990 | $plugin->stop_vm ($task, $vmid); | |
991 | $cleanup->{restart} = 1; | |
992 | } | |
60e049c2 | 993 | |
aaeeeebe DM |
994 | |
995 | } elsif ($mode eq 'suspend') { | |
aaeeeebe DM |
996 | $plugin->prepare ($task, $vmid, $mode); |
997 | ||
998 | $self->run_hook_script ('backup-start', $task, $logfd); | |
999 | ||
8187f6b0 DM |
1000 | if ($vmtype eq 'lxc') { |
1001 | # pre-suspend rsync | |
1002 | $plugin->copy_data_phase1($task, $vmid); | |
1003 | } | |
1004 | ||
ef7d3fdd | 1005 | debugmsg ('info', "suspending guest", $logfd); |
4e0947c8 | 1006 | $task->{vmstoptime} = time (); |
aaeeeebe DM |
1007 | $self->run_hook_script ('pre-stop', $task, $logfd); |
1008 | $plugin->suspend_vm ($task, $vmid); | |
1009 | $cleanup->{resume} = 1; | |
1010 | ||
8187f6b0 DM |
1011 | if ($vmtype eq 'lxc') { |
1012 | # post-suspend rsync | |
1013 | $plugin->copy_data_phase2($task, $vmid); | |
1014 | ||
ef7d3fdd | 1015 | debugmsg ('info', "resuming guest", $logfd); |
8187f6b0 DM |
1016 | $cleanup->{resume} = 0; |
1017 | $self->run_hook_script('pre-restart', $task, $logfd); | |
1018 | $plugin->resume_vm($task, $vmid); | |
d5047243 | 1019 | $self->run_hook_script('post-restart', $task, $logfd); |
4e0947c8 | 1020 | $log_vm_online_again->(); |
8187f6b0 | 1021 | } |
60e049c2 | 1022 | |
aaeeeebe | 1023 | } elsif ($mode eq 'snapshot') { |
c5be0f8c DM |
1024 | $self->run_hook_script ('backup-start', $task, $logfd); |
1025 | ||
aaeeeebe DM |
1026 | my $snapshot_count = $task->{snapshot_count} || 0; |
1027 | ||
1028 | $self->run_hook_script ('pre-stop', $task, $logfd); | |
1029 | ||
1030 | if ($snapshot_count > 1) { | |
1031 | debugmsg ('info', "suspend vm to make snapshot", $logfd); | |
4e0947c8 | 1032 | $task->{vmstoptime} = time (); |
aaeeeebe DM |
1033 | $plugin->suspend_vm ($task, $vmid); |
1034 | $cleanup->{resume} = 1; | |
1035 | } | |
1036 | ||
1037 | $plugin->snapshot ($task, $vmid); | |
1038 | ||
1039 | $self->run_hook_script ('pre-restart', $task, $logfd); | |
1040 | ||
1041 | if ($snapshot_count > 1) { | |
1042 | debugmsg ('info', "resume vm", $logfd); | |
1043 | $cleanup->{resume} = 0; | |
1044 | $plugin->resume_vm ($task, $vmid); | |
4e0947c8 | 1045 | $log_vm_online_again->(); |
aaeeeebe DM |
1046 | } |
1047 | ||
d5047243 FG |
1048 | $self->run_hook_script ('post-restart', $task, $logfd); |
1049 | ||
aaeeeebe DM |
1050 | } else { |
1051 | die "internal error - unknown mode '$mode'\n"; | |
1052 | } | |
1053 | ||
1054 | # assemble archive image | |
1055 | $plugin->assemble ($task, $vmid); | |
60e049c2 TM |
1056 | |
1057 | # produce archive | |
aaeeeebe DM |
1058 | |
1059 | if ($opts->{stdout}) { | |
1060 | debugmsg ('info', "sending archive to stdout", $logfd); | |
d7550e09 | 1061 | $plugin->archive($task, $vmid, $task->{tmptar}, $comp); |
aaeeeebe DM |
1062 | $self->run_hook_script ('backup-end', $task, $logfd); |
1063 | return; | |
1064 | } | |
1065 | ||
b3c3304f TL |
1066 | my $archive_txt = $self->{opts}->{pbs} ? 'Proxmox Backup Server' : 'vzdump'; |
1067 | debugmsg('info', "creating $archive_txt archive '$task->{target}'", $logfd); | |
d7550e09 | 1068 | $plugin->archive($task, $vmid, $task->{tmptar}, $comp); |
aaeeeebe | 1069 | |
7ddcb3af | 1070 | if ($self->{opts}->{pbs}) { |
e21a31a7 | 1071 | # size is added to task struct in guest vzdump plugins |
1a87db9e | 1072 | } else { |
6cba1855 TL |
1073 | rename ($task->{tmptar}, $task->{target}) || |
1074 | die "unable to rename '$task->{tmptar}' to '$task->{target}'\n"; | |
aaeeeebe | 1075 | |
1a87db9e | 1076 | # determine size |
6cba1855 | 1077 | $task->{size} = (-s $task->{target}) || 0; |
1a87db9e DM |
1078 | my $cs = format_size ($task->{size}); |
1079 | debugmsg ('info', "archive file size: $cs", $logfd); | |
1080 | } | |
aaeeeebe | 1081 | |
bbd4cdd8 FE |
1082 | # Mark as protected before pruning. |
1083 | if (my $storeid = $opts->{storage}) { | |
1084 | my $volname = $opts->{pbs} ? $task->{target} : basename($task->{target}); | |
1085 | my $volid = "${storeid}:backup/${volname}"; | |
1086 | ||
e01438a7 FE |
1087 | if ($opts->{'notes-template'} && $opts->{'notes-template'} ne '') { |
1088 | debugmsg('info', "adding notes to backup", $logfd); | |
31213d61 FE |
1089 | my $notes = eval { $generate_notes->($opts->{'notes-template'}, $task); }; |
1090 | if (my $err = $@) { | |
1091 | debugmsg('warn', "unable to add notes - $err", $logfd); | |
1092 | } else { | |
1093 | eval { PVE::Storage::update_volume_attribute($cfg, $volid, 'notes', $notes) }; | |
1094 | debugmsg('warn', "unable to add notes - $@", $logfd) if $@; | |
1095 | } | |
e01438a7 FE |
1096 | } |
1097 | ||
bbd4cdd8 FE |
1098 | if ($opts->{protected}) { |
1099 | debugmsg('info', "marking backup as protected", $logfd); | |
1100 | eval { PVE::Storage::update_volume_attribute($cfg, $volid, 'protected', 1) }; | |
1101 | die "unable to set protected flag - $@\n" if $@; | |
1102 | } | |
1103 | } | |
1104 | ||
e6946086 | 1105 | if ($opts->{remove}) { |
ca155141 TL |
1106 | my $keepstr = join(', ', map { "$_=$prune_options->{$_}" } sort keys %$prune_options); |
1107 | debugmsg ('info', "prune older backups with retention: $keepstr", $logfd); | |
b4546c7e | 1108 | my $pruned = 0; |
f354fe47 | 1109 | if (!defined($opts->{storage})) { |
725c0555 | 1110 | my $bklist = get_backup_file_list($opts->{dumpdir}, $bkname); |
2fc939a9 | 1111 | |
f354fe47 | 1112 | PVE::Storage::prune_mark_backup_group($bklist, $prune_options); |
e6946086 | 1113 | |
f354fe47 FE |
1114 | foreach my $prune_entry (@{$bklist}) { |
1115 | next if $prune_entry->{mark} ne 'remove'; | |
b4546c7e | 1116 | $pruned++; |
f354fe47 FE |
1117 | my $archive_path = $prune_entry->{path}; |
1118 | debugmsg ('info', "delete old backup '$archive_path'", $logfd); | |
1119 | PVE::Storage::archive_remove($archive_path); | |
1a87db9e | 1120 | } |
f354fe47 | 1121 | } else { |
b4546c7e TL |
1122 | my $pruned_list = PVE::Storage::prune_backups( |
1123 | $cfg, | |
1124 | $opts->{storage}, | |
1125 | $prune_options, | |
1126 | $vmid, | |
1127 | $vmtype, | |
1128 | 0, | |
1129 | sub { debugmsg($_[0], $_[1], $logfd) }, | |
1130 | ); | |
1131 | $pruned = scalar(grep { $_->{mark} eq 'remove' } $pruned_list->@*); | |
aaeeeebe | 1132 | } |
b4546c7e TL |
1133 | my $log_pruned_extra = $pruned > 0 ? " not covered by keep-retention policy" : ""; |
1134 | debugmsg ('info', "pruned $pruned backup(s)${log_pruned_extra}", $logfd); | |
aaeeeebe DM |
1135 | } |
1136 | ||
1137 | $self->run_hook_script ('backup-end', $task, $logfd); | |
1138 | }; | |
1139 | my $err = $@; | |
1140 | ||
1141 | if ($plugin) { | |
1142 | # clean-up | |
1143 | ||
1144 | if ($cleanup->{unlock}) { | |
1145 | eval { $plugin->unlock_vm ($vmid); }; | |
1146 | warn $@ if $@; | |
1147 | } | |
1148 | ||
6acb632a | 1149 | if ($cleanup->{prepared}) { |
ca2605e7 DM |
1150 | # only call cleanup when necessary (when prepare was executed) |
1151 | eval { $plugin->cleanup ($task, $vmid) }; | |
1152 | warn $@ if $@; | |
1153 | } | |
aaeeeebe DM |
1154 | |
1155 | eval { $plugin->set_logfd (undef); }; | |
1156 | warn $@ if $@; | |
1157 | ||
60e049c2 TM |
1158 | if ($cleanup->{resume} || $cleanup->{restart}) { |
1159 | eval { | |
aaeeeebe DM |
1160 | $self->run_hook_script ('pre-restart', $task, $logfd); |
1161 | if ($cleanup->{resume}) { | |
1162 | debugmsg ('info', "resume vm", $logfd); | |
1163 | $plugin->resume_vm ($task, $vmid); | |
1164 | } else { | |
757fd3d5 DM |
1165 | my $running = $plugin->vm_status($vmid); |
1166 | if (!$running) { | |
1167 | debugmsg ('info', "restarting vm", $logfd); | |
1168 | $plugin->start_vm ($task, $vmid); | |
1169 | } | |
d5047243 FG |
1170 | } |
1171 | $self->run_hook_script ('post-restart', $task, $logfd); | |
aaeeeebe DM |
1172 | }; |
1173 | my $err = $@; | |
1174 | if ($err) { | |
1175 | warn $err; | |
1176 | } else { | |
4e0947c8 | 1177 | $log_vm_online_again->(); |
aaeeeebe DM |
1178 | } |
1179 | } | |
1180 | } | |
1181 | ||
1182 | eval { unlink $task->{tmptar} if $task->{tmptar} && -f $task->{tmptar}; }; | |
1183 | warn $@ if $@; | |
1184 | ||
fe6643b6 | 1185 | eval { rmtree $task->{tmpdir} if $task->{tmpdir} && -d $task->{tmpdir}; }; |
aaeeeebe DM |
1186 | warn $@ if $@; |
1187 | ||
1188 | my $delay = $task->{backuptime} = time () - $vmstarttime; | |
1189 | ||
1190 | if ($err) { | |
1191 | $task->{state} = 'err'; | |
1192 | $task->{msg} = $err; | |
1193 | debugmsg ('err', "Backup of VM $vmid failed - $err", $logfd, 1); | |
29f26f6d | 1194 | debugmsg ('info', "Failed at " . strftime("%F %H:%M:%S", localtime())); |
aaeeeebe DM |
1195 | |
1196 | eval { $self->run_hook_script ('backup-abort', $task, $logfd); }; | |
1197 | ||
1198 | } else { | |
1199 | $task->{state} = 'ok'; | |
1200 | my $tstr = format_time ($delay); | |
1201 | debugmsg ('info', "Finished Backup of VM $vmid ($tstr)", $logfd, 1); | |
29f26f6d | 1202 | debugmsg ('info', "Backup finished at " . strftime("%F %H:%M:%S", localtime())); |
aaeeeebe DM |
1203 | } |
1204 | ||
1205 | close ($logfd) if $logfd; | |
60e049c2 | 1206 | |
1a87db9e | 1207 | if ($task->{tmplog}) { |
7ddcb3af | 1208 | if ($self->{opts}->{pbs}) { |
1a87db9e | 1209 | if ($task->{state} eq 'ok') { |
e8d9e1ed TL |
1210 | eval { |
1211 | PVE::Storage::PBSPlugin::run_raw_client_cmd( | |
1212 | $opts->{scfg}, | |
1213 | $opts->{storage}, | |
1214 | 'upload-log', | |
1215 | [ $pbs_snapshot_name, $task->{tmplog} ], | |
1216 | errmsg => "uploading backup task log failed", | |
f332a167 | 1217 | outfunc => sub {}, |
e8d9e1ed TL |
1218 | ); |
1219 | }; | |
1220 | debugmsg('warn', "$@") if $@; # $@ contains already error prefix | |
1a87db9e DM |
1221 | } |
1222 | } elsif ($task->{logfile}) { | |
1223 | system {'cp'} 'cp', $task->{tmplog}, $task->{logfile}; | |
1224 | } | |
aaeeeebe DM |
1225 | } |
1226 | ||
1227 | eval { $self->run_hook_script ('log-end', $task); }; | |
1228 | ||
1229 | die $err if $err && $err =~ m/^interrupted by signal$/; | |
1230 | } | |
1231 | ||
1232 | sub exec_backup { | |
d7550e09 | 1233 | my ($self, $rpcenv, $authuser) = @_; |
aaeeeebe DM |
1234 | |
1235 | my $opts = $self->{opts}; | |
1236 | ||
1237 | debugmsg ('info', "starting new backup job: $self->{cmdline}", undef, 1); | |
f647d0fc FE |
1238 | |
1239 | if (scalar(@{$self->{skiplist}})) { | |
1240 | my $skip_string = join(', ', sort { $a <=> $b } @{$self->{skiplist}}); | |
1241 | debugmsg ('info', "skip external VMs: $skip_string"); | |
1242 | } | |
60e049c2 | 1243 | |
aaeeeebe | 1244 | my $tasklist = []; |
df5875b4 AL |
1245 | my $vzdump_plugins = {}; |
1246 | foreach my $plugin (@{$self->{plugins}}) { | |
1247 | my $type = $plugin->type(); | |
1248 | next if exists $vzdump_plugins->{$type}; | |
1249 | $vzdump_plugins->{$type} = $plugin; | |
1250 | } | |
aaeeeebe | 1251 | |
df5875b4 | 1252 | my $vmlist = PVE::Cluster::get_vmlist(); |
4d73f0e2 FE |
1253 | my $vmids = [ sort { $a <=> $b } @{$opts->{vmids}} ]; |
1254 | foreach my $vmid (@{$vmids}) { | |
7f874148 FE |
1255 | my $plugin; |
1256 | if (defined($vmlist->{ids}->{$vmid})) { | |
1257 | my $guest_type = $vmlist->{ids}->{$vmid}->{type}; | |
1258 | $plugin = $vzdump_plugins->{$guest_type}; | |
1259 | next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Backup' ], $opts->{all}); | |
1260 | } | |
df5875b4 | 1261 | push @$tasklist, { |
f839f3d1 | 1262 | mode => $opts->{mode}, |
df5875b4 | 1263 | plugin => $plugin, |
f839f3d1 TL |
1264 | state => 'todo', |
1265 | vmid => $vmid, | |
1266 | }; | |
aaeeeebe DM |
1267 | } |
1268 | ||
44b21574 WB |
1269 | # Use in-memory files for the outer hook logs to pass them to sendmail. |
1270 | my $job_start_log = ''; | |
1271 | my $job_end_log = ''; | |
1272 | open my $job_start_fd, '>', \$job_start_log; | |
1273 | open my $job_end_fd, '>', \$job_end_log; | |
1274 | ||
aaeeeebe DM |
1275 | my $starttime = time(); |
1276 | my $errcount = 0; | |
1277 | eval { | |
1278 | ||
44b21574 | 1279 | $self->run_hook_script ('job-start', undef, $job_start_fd); |
aaeeeebe DM |
1280 | |
1281 | foreach my $task (@$tasklist) { | |
1282 | $self->exec_backup_task ($task); | |
1283 | $errcount += 1 if $task->{state} ne 'ok'; | |
1284 | } | |
1285 | ||
44b21574 | 1286 | $self->run_hook_script ('job-end', undef, $job_end_fd); |
aaeeeebe DM |
1287 | }; |
1288 | my $err = $@; | |
1289 | ||
aaeeeebe | 1290 | if ($err) { |
573e8320 FE |
1291 | eval { $self->run_hook_script ('job-abort', undef, $job_end_fd); }; |
1292 | $err .= $@ if $@; | |
aaeeeebe DM |
1293 | debugmsg ('err', "Backup job failed - $err", undef, 1); |
1294 | } else { | |
1295 | if ($errcount) { | |
1296 | debugmsg ('info', "Backup job finished with errors", undef, 1); | |
1297 | } else { | |
61ca4432 | 1298 | debugmsg ('info', "Backup job finished successfully", undef, 1); |
aaeeeebe DM |
1299 | } |
1300 | } | |
1301 | ||
44b21574 WB |
1302 | close $job_start_fd; |
1303 | close $job_end_fd; | |
1304 | ||
aaeeeebe DM |
1305 | my $totaltime = time() - $starttime; |
1306 | ||
b241deb7 | 1307 | eval { |
268c6428 FE |
1308 | # otherwise $self->sendmail() will interpret it as multiple problems |
1309 | my $chomped_err = $err; | |
1310 | chomp($chomped_err) if $chomped_err; | |
1311 | ||
b241deb7 FE |
1312 | $self->sendmail( |
1313 | $tasklist, | |
1314 | $totaltime, | |
268c6428 | 1315 | $chomped_err, |
b241deb7 FE |
1316 | $self->{job_init_log} . $job_start_log, |
1317 | $job_end_log, | |
1318 | ); | |
1319 | }; | |
aaeeeebe | 1320 | debugmsg ('err', $@) if $@; |
4a4051d8 DM |
1321 | |
1322 | die $err if $err; | |
1323 | ||
60e049c2 | 1324 | die "job errors\n" if $errcount; |
8682f6fc WL |
1325 | |
1326 | unlink $pidfile; | |
aaeeeebe DM |
1327 | } |
1328 | ||
ac27b58d | 1329 | |
47664cbe DM |
1330 | sub option_exists { |
1331 | my $key = shift; | |
1332 | return defined($confdesc->{$key}); | |
1333 | } | |
1334 | ||
f8ed6af8 FE |
1335 | # NOTE it might make sense to merge this and verify_vzdump_parameters(), but one |
1336 | # needs to adapt command_line() in guest-common's PVE/VZDump/Common.pm and detect | |
1337 | # a second parsing attempt, because verify_vzdump_parameters() is called twice | |
1338 | # during the update_job API call. | |
1339 | sub parse_mailto_exclude_path { | |
1340 | my ($param) = @_; | |
1341 | ||
1342 | # exclude-path list need to be 0 separated | |
1343 | if (defined($param->{'exclude-path'})) { | |
1344 | my @expaths = split(/\0/, $param->{'exclude-path'} || ''); | |
1345 | $param->{'exclude-path'} = [ @expaths ]; | |
1346 | } | |
1347 | ||
1348 | if (defined($param->{mailto})) { | |
1349 | my @mailto = PVE::Tools::split_list(extract_param($param, 'mailto')); | |
1350 | $param->{mailto} = [ @mailto ]; | |
1351 | } | |
1352 | ||
1353 | return; | |
1354 | } | |
1355 | ||
31aef761 DM |
1356 | sub verify_vzdump_parameters { |
1357 | my ($param, $check_missing) = @_; | |
1358 | ||
1359 | raise_param_exc({ all => "option conflicts with option 'vmid'"}) | |
eab837c4 | 1360 | if $param->{all} && $param->{vmid}; |
31aef761 DM |
1361 | |
1362 | raise_param_exc({ exclude => "option conflicts with option 'vmid'"}) | |
1363 | if $param->{exclude} && $param->{vmid}; | |
1364 | ||
f3376261 TM |
1365 | raise_param_exc({ pool => "option conflicts with option 'vmid'"}) |
1366 | if $param->{pool} && $param->{vmid}; | |
1367 | ||
e6946086 FE |
1368 | raise_param_exc({ 'prune-backups' => "option conflicts with option 'maxfiles'"}) |
1369 | if defined($param->{'prune-backups'}) && defined($param->{maxfiles}); | |
1370 | ||
1c56bcf3 | 1371 | $parse_prune_backups_maxfiles->($param, 'CLI parameters'); |
93880785 | 1372 | parse_performance($param); |
e6946086 | 1373 | |
facf65a6 FE |
1374 | if (my $template = $param->{'notes-template'}) { |
1375 | eval { $verify_notes_template->($template); }; | |
1376 | raise_param_exc({'notes-template' => $@}) if $@; | |
1377 | } | |
010ff16e | 1378 | |
f3376261 | 1379 | $param->{all} = 1 if (defined($param->{exclude}) && !$param->{pool}); |
31aef761 DM |
1380 | |
1381 | return if !$check_missing; | |
1382 | ||
1383 | raise_param_exc({ vmid => "property is missing"}) | |
f3376261 | 1384 | if !($param->{all} || $param->{stop} || $param->{pool}) && !$param->{vmid}; |
8682f6fc WL |
1385 | |
1386 | } | |
1387 | ||
eab837c4 | 1388 | sub stop_running_backups { |
8682f6fc WL |
1389 | my($self) = @_; |
1390 | ||
eab837c4 DM |
1391 | my $upid = PVE::Tools::file_read_firstline($pidfile); |
1392 | return if !$upid; | |
8682f6fc | 1393 | |
eab837c4 | 1394 | my $task = PVE::Tools::upid_decode($upid); |
8682f6fc | 1395 | |
60e049c2 | 1396 | if (PVE::ProcFSTools::check_process_running($task->{pid}, $task->{pstart}) && |
eab837c4 DM |
1397 | PVE::ProcFSTools::read_proc_starttime($task->{pid}) == $task->{pstart}) { |
1398 | kill(15, $task->{pid}); | |
1399 | # wait max 15 seconds to shut down (else, do nothing for now) | |
1400 | my $i; | |
1401 | for ($i = 15; $i > 0; $i--) { | |
1402 | last if !PVE::ProcFSTools::check_process_running(($task->{pid}, $task->{pstart})); | |
1403 | sleep (1); | |
1404 | } | |
76189130 | 1405 | die "stopping backup process $task->{pid} failed\n" if $i == 0; |
8682f6fc | 1406 | } |
31aef761 DM |
1407 | } |
1408 | ||
5c4da4c3 AL |
1409 | sub get_included_guests { |
1410 | my ($job) = @_; | |
1411 | ||
5c4da4c3 | 1412 | my $vmids = []; |
df5875b4 AL |
1413 | my $vmids_per_node = {}; |
1414 | ||
1415 | my $vmlist = PVE::Cluster::get_vmlist(); | |
5c4da4c3 | 1416 | |
5c4da4c3 AL |
1417 | if ($job->{pool}) { |
1418 | $vmids = PVE::API2Tools::get_resource_pool_guest_members($job->{pool}); | |
df5875b4 | 1419 | } elsif ($job->{vmid}) { |
05447e04 | 1420 | $vmids = [ split_list($job->{vmid}) ]; |
da7cb515 | 1421 | } elsif ($job->{all}) { |
df5875b4 | 1422 | # all or exclude |
05447e04 TL |
1423 | my $exclude = check_vmids(split_list($job->{exclude})); |
1424 | my $excludehash = { map { $_ => 1 } @$exclude }; | |
df5875b4 | 1425 | |
05447e04 | 1426 | for my $id (keys %{$vmlist->{ids}}) { |
df5875b4 AL |
1427 | next if $excludehash->{$id}; |
1428 | push @$vmids, $id; | |
1429 | } | |
da7cb515 AL |
1430 | } else { |
1431 | return $vmids_per_node; | |
5c4da4c3 | 1432 | } |
50ba40ec | 1433 | $vmids = check_vmids(@$vmids); |
5c4da4c3 | 1434 | |
50ba40ec | 1435 | for my $vmid (@$vmids) { |
7f874148 FE |
1436 | if (defined($vmlist->{ids}->{$vmid})) { |
1437 | my $node = $vmlist->{ids}->{$vmid}->{node}; | |
1438 | next if (defined $job->{node} && $job->{node} ne $node); | |
50ba40ec | 1439 | |
7f874148 FE |
1440 | push @{$vmids_per_node->{$node}}, $vmid; |
1441 | } else { | |
1442 | push @{$vmids_per_node->{''}}, $vmid; | |
1443 | } | |
5c4da4c3 AL |
1444 | } |
1445 | ||
df5875b4 | 1446 | return $vmids_per_node; |
5c4da4c3 AL |
1447 | } |
1448 | ||
aaeeeebe | 1449 | 1; |