]> git.proxmox.com Git - qemu-server.git/blame - PVE/VZDump/QemuServer.pm
vzdump: indentation and code cleanup
[qemu-server.git] / PVE / VZDump / QemuServer.pm
CommitLineData
1e3baf05
DM
1package PVE::VZDump::QemuServer;
2
1e3baf05
DM
3use strict;
4use warnings;
d610b145 5
1e3baf05 6use File::Basename;
d610b145
TL
7use File::Path;
8use IO::File;
9use IPC::Open3;
10
11use PVE::Cluster qw(cfs_read_file);
66ab1d91 12use PVE::INotify;
91bd6c90 13use PVE::IPCC;
d610b145 14use PVE::JSONSchema;
0a13e08e 15use PVE::QMPClient;
f5bdefa4 16use PVE::Storage::Plugin;
c5983223 17use PVE::Storage::PBSPlugin;
1e3baf05 18use PVE::Storage;
d610b145
TL
19use PVE::Tools;
20use PVE::VZDump;
21
1e3baf05 22use PVE::QemuServer;
3392d6ca 23use PVE::QemuServer::Machine;
0a13e08e 24use PVE::QemuServer::Monitor qw(mon_cmd);
1e3baf05
DM
25
26use base qw (PVE::VZDump::Plugin);
27
28sub new {
29 my ($class, $vzdump) = @_;
874a096e 30
66ab1d91 31 PVE::VZDump::check_bin('qm');
1e3baf05 32
e2812738 33 my $self = bless { vzdump => $vzdump }, $class;
1e3baf05
DM
34
35 $self->{vmlist} = PVE::QemuServer::vzlist();
36 $self->{storecfg} = PVE::Storage::config();
37
38 return $self;
39};
40
1e3baf05
DM
41sub type {
42 return 'qemu';
43}
44
45sub vmlist {
46 my ($self) = @_;
1e3baf05
DM
47 return [ keys %{$self->{vmlist}} ];
48}
49
50sub prepare {
51 my ($self, $task, $vmid, $mode) = @_;
52
53 $task->{disks} = [];
54
ffda963f 55 my $conf = $self->{vmlist}->{$vmid} = PVE::QemuConfig->load_config($vmid);
1e3baf05 56
85b84b7b
WL
57 $self->loginfo("VM Name: $conf->{name}")
58 if defined($conf->{name});
59
91bd6c90
DM
60 $self->{vm_was_running} = 1;
61 if (!PVE::QemuServer::check_running($vmid)) {
62 $self->{vm_was_running} = 0;
585b6e28
DM
63 }
64
1e3baf05
DM
65 $task->{hostname} = $conf->{name};
66
874a096e 67 my $hostname = PVE::INotify::nodename();
1e3baf05 68
b969cc68
DM
69 my $vollist = [];
70 my $drivehash = {};
1e3baf05
DM
71 PVE::QemuServer::foreach_drive($conf, sub {
72 my ($ds, $drive) = @_;
73
f5bdefa4 74 return if PVE::QemuServer::drive_is_cdrom($drive);
1e3baf05 75
b53b958b
EK
76 my $volid = $drive->{file};
77
bea021ac 78 if (defined($drive->{backup}) && !$drive->{backup}) {
b53b958b 79 $self->loginfo("exclude disk '$ds' '$volid' (backup=no)");
1e3baf05 80 return;
317c55c2 81 } elsif ($self->{vm_was_running} && $drive->{iothread}) {
3392d6ca 82 if (!PVE::QemuServer::Machine::runs_at_least_qemu_version($vmid, 4, 0, 1)) {
48343b3f
TL
83 die "disk '$ds' '$volid' (iothread=on) can't use backup feature with running QEMU " .
84 "version < 4.0.1! Either set backup=no for this drive or upgrade QEMU and restart VM\n";
85 }
1249d579
SR
86 } elsif ($ds =~ m/^efidisk/ && (!defined($conf->{bios}) || $conf->{bios} ne 'ovmf')) {
87 $self->loginfo("excluding '$ds' (efidisks can only be backed up when BIOS is set to 'ovmf')");
88 return;
b53b958b 89 } else {
0db93c2d
EK
90 my $log = "include disk '$ds' '$volid'";
91 if (defined $drive->{size}) {
92 my $readable_size = PVE::JSONSchema::format_size($drive->{size});
93 $log .= " $readable_size";
94 }
95 $self->loginfo($log);
874a096e 96 }
b969cc68 97
f5bdefa4 98 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
b969cc68
DM
99 push @$vollist, $volid if $storeid;
100 $drivehash->{$ds} = $drive;
101 });
102
103 PVE::Storage::activate_volumes($self->{storecfg}, $vollist);
104
075b417a
DM
105 foreach my $ds (sort keys %$drivehash) {
106 my $drive = $drivehash->{$ds};
874a096e 107
1e3baf05 108 my $volid = $drive->{file};
f5bdefa4 109 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
a113a58a
TL
110
111 my $path = $volid;
1e3baf05 112 if ($storeid) {
b969cc68 113 $path = PVE::Storage::path($self->{storecfg}, $volid);
1e3baf05 114 }
b969cc68 115 next if !$path;
1e3baf05 116
a113a58a 117 my ($size, $format) = eval { PVE::Storage::volume_size_info($self->{storecfg}, $volid, 5) };
daca220d 118 die "no such volume '$volid'\n" if $@;
91bd6c90 119
a113a58a
TL
120 my $diskinfo = {
121 path => $path,
122 volid => $volid,
123 storeid => $storeid,
124 format => $format,
125 virtdev => $ds,
126 qmdevice => "drive-$ds",
127 };
1e3baf05
DM
128
129 if (-b $path) {
1e3baf05 130 $diskinfo->{type} = 'block';
1e3baf05 131 } else {
1e3baf05 132 $diskinfo->{type} = 'file';
1e3baf05
DM
133 }
134
135 push @{$task->{disks}}, $diskinfo;
b969cc68 136 }
1e3baf05
DM
137}
138
139sub vm_status {
140 my ($self, $vmid) = @_;
141
66ab1d91 142 my $running = PVE::QemuServer::check_running($vmid) ? 1 : 0;
874a096e
DM
143
144 return wantarray ? ($running, $running ? 'running' : 'stopped') : $running;
1e3baf05
DM
145}
146
147sub lock_vm {
148 my ($self, $vmid) = @_;
149
f301bc0d 150 PVE::QemuConfig->set_lock($vmid, 'backup');
1e3baf05
DM
151}
152
153sub unlock_vm {
154 my ($self, $vmid) = @_;
155
f301bc0d 156 PVE::QemuConfig->remove_lock($vmid, 'backup');
1e3baf05
DM
157}
158
159sub stop_vm {
160 my ($self, $task, $vmid) = @_;
161
162 my $opts = $self->{vzdump}->{opts};
163
164 my $wait = $opts->{stopwait} * 60;
165 # send shutdown and wait
254575e9 166 $self->cmd ("qm shutdown $vmid --skiplock --keepActive --timeout $wait");
1e3baf05
DM
167}
168
169sub start_vm {
170 my ($self, $task, $vmid) = @_;
171
66ab1d91 172 $self->cmd ("qm start $vmid --skiplock");
1e3baf05
DM
173}
174
175sub suspend_vm {
176 my ($self, $task, $vmid) = @_;
177
66ab1d91 178 $self->cmd ("qm suspend $vmid --skiplock");
1e3baf05
DM
179}
180
181sub resume_vm {
182 my ($self, $task, $vmid) = @_;
183
66ab1d91 184 $self->cmd ("qm resume $vmid --skiplock");
1e3baf05
DM
185}
186
1e3baf05
DM
187sub assemble {
188 my ($self, $task, $vmid) = @_;
189
04096e7b 190 my $conffile = PVE::QemuConfig->config_file($vmid);
1e3baf05
DM
191
192 my $outfile = "$task->{tmpdir}/qemu-server.conf";
c05f7f3f
WL
193 my $firewall_src = "/etc/pve/firewall/$vmid.fw";
194 my $firewall_dest = "$task->{tmpdir}/qemu-server.fw";
195
196 my $outfd = IO::File->new (">$outfile") ||
197 die "unable to open '$outfile'";
198 my $conffd = IO::File->new ($conffile, 'r') ||
199 die "unable open '$conffile'";
200
201 my $found_snapshot;
391c2230 202 my $found_pending;
c05f7f3f
WL
203 while (defined (my $line = <$conffd>)) {
204 next if $line =~ m/^\#vzdump\#/; # just to be sure
205 next if $line =~ m/^\#qmdump\#/; # just to be sure
391c2230
FG
206 if ($line =~ m/^\[(.*)\]\s*$/) {
207 if ($1 =~ m/PENDING/i) {
208 $found_pending = 1;
209 } else {
210 $found_snapshot = 1;
211 }
cd544fb2 212 next; # skip all snapshots and pending changes config data
1e3baf05 213 }
391c2230 214
c05f7f3f
WL
215 if ($line =~ m/^unused\d+:\s*(\S+)\s*/) {
216 $self->loginfo("skip unused drive '$1' (not included into backup)");
217 next;
1e3baf05 218 }
c05f7f3f 219 next if $line =~ m/^lock:/ || $line =~ m/^parent:/;
91bd6c90 220
c05f7f3f
WL
221 print $outfd $line;
222 }
223
224 foreach my $di (@{$task->{disks}}) {
225 if ($di->{type} eq 'block' || $di->{type} eq 'file') {
226 my $storeid = $di->{storeid} || '';
227 my $format = $di->{format} || '';
228 print $outfd "#qmdump#map:$di->{virtdev}:$di->{qmdevice}:$storeid:$format:\n";
229 } else {
230 die "internal error";
91bd6c90 231 }
c05f7f3f 232 }
1e3baf05 233
c05f7f3f
WL
234 if ($found_snapshot) {
235 $self->loginfo("snapshots found (not included into backup)");
236 }
391c2230
FG
237 if ($found_pending) {
238 $self->loginfo("pending configuration changes found (not included into backup)");
239 }
240
c05f7f3f 241 PVE::Tools::file_copy($firewall_src, $firewall_dest) if -f $firewall_src;
1e3baf05
DM
242}
243
244sub archive {
fad02a16 245 my ($self, $task, $vmid, $filename, $comp) = @_;
1e3baf05 246
c5983223 247 my $opts = $self->{vzdump}->{opts};
c5983223
DM
248 my $scfg = $opts->{scfg};
249
250 if ($scfg->{type} eq 'pbs') {
251 $self->archive_pbs($task, $vmid);
252 } else {
253 $self->archive_vma($task, $vmid, $filename, $comp);
254 }
255}
256
257my $query_backup_status_loop = sub {
258 my ($self, $vmid, $job_uuid) = @_;
259
c5983223 260 my $starttime = time ();
09eb196b
TL
261 my $last_time = $starttime;
262 my ($last_percent, $last_total, $last_zero, $last_transferred) = (-1, 0, 0, 0);
c5983223
DM
263 my $transferred;
264
09eb196b
TL
265 my $get_mbps = sub {
266 my ($mb, $delta) = @_;
267 return ($mb > 0) ? int(($mb / $delta) / (1000 * 1000)) : 0;
268 };
269
c5983223 270 while(1) {
09eb196b
TL
271 my $status = mon_cmd($vmid, 'query-backup');
272
c5983223
DM
273 my $total = $status->{total} || 0;
274 $transferred = $status->{transferred} || 0;
09eb196b 275 my $percent = $total ? int(($transferred * 100)/$total) : 0;
c5983223
DM
276 my $zero = $status->{'zero-bytes'} || 0;
277 my $zero_per = $total ? int(($zero * 100)/$total) : 0;
278
279 die "got unexpected uuid\n" if !$status->{uuid} || ($status->{uuid} ne $job_uuid);
280
281 my $ctime = time();
282 my $duration = $ctime - $starttime;
283
284 my $rbytes = $transferred - $last_transferred;
285 my $wbytes = $rbytes - ($zero - $last_zero);
286
287 my $timediff = ($ctime - $last_time) || 1; # fixme
09eb196b
TL
288 my $mbps_read = $get_mbps->($rbytes, $timediff);
289 my $mbps_write = $get_mbps->($wbytes, $timediff);
290
291 my $statusline = "status: $percent% ($transferred/$total), sparse ${zero_per}% ($zero), duration $duration, read/write $mbps_read/$mbps_write MB/s";
292
c5983223
DM
293 my $res = $status->{status} || 'unknown';
294 if ($res ne 'active') {
295 $self->loginfo($statusline);
09eb196b
TL
296 if ($res ne 'done') {
297 die (($status->{errmsg} || "unknown error") . "\n") if $res eq 'error';
298 die "got unexpected status '$res'\n";
299 } elsif ($total != $transferred) {
300 die "got wrong number of transfered bytes ($total != $transferred)\n";
301 }
c5983223
DM
302 last;
303 }
09eb196b 304 if ($percent != $last_percent && ($timediff > 2)) {
c5983223 305 $self->loginfo($statusline);
09eb196b 306 $last_percent = $percent;
c5983223
DM
307 $last_total = $total if $total;
308 $last_zero = $zero if $zero;
309 $last_transferred = $transferred if $transferred;
310 $last_time = $ctime;
311 }
312 sleep(1);
313 }
314
315 my $duration = time() - $starttime;
316 if ($transferred && $duration) {
09eb196b
TL
317 my $mb = int($transferred / (1000 * 1000));
318 my $mbps = $get_mbps->($transferred, $duration);
c5983223
DM
319 $self->loginfo("transferred $mb MB in $duration seconds ($mbps MB/s)");
320 }
321};
322
323sub archive_pbs {
324 my ($self, $task, $vmid) = @_;
325
1e3baf05 326 my $conffile = "$task->{tmpdir}/qemu-server.conf";
c05f7f3f 327 my $firewall = "$task->{tmpdir}/qemu-server.fw";
1e3baf05
DM
328
329 my $opts = $self->{vzdump}->{opts};
c5983223
DM
330 my $scfg = $opts->{scfg};
331
332 my $starttime = time();
333
c5983223
DM
334 my $server = $scfg->{server};
335 my $datastore = $scfg->{datastore};
336 my $username = $scfg->{username} // 'root@pam';
337 my $fingerprint = $scfg->{fingerprint};
338
339 my $repo = "$username\@$server:$datastore";
340 my $password = PVE::Storage::PBSPlugin::pbs_get_password($scfg, $opts->{storage});
341
d11e91d2 342 my $diskcount = scalar(@{$task->{disks}});
c5983223
DM
343 if (PVE::QemuConfig->is_template($self->{vmlist}->{$vmid}) || !$diskcount) {
344 my @pathlist;
345 foreach my $di (@{$task->{disks}}) {
346 if ($di->{type} eq 'block' || $di->{type} eq 'file') {
347 push @pathlist, "$di->{qmdevice}.img:$di->{path}";
348 } else {
d11e91d2 349 die "implement me (type $di->{type})";
c5983223
DM
350 }
351 }
352
353 if (!$diskcount) {
354 $self->loginfo("backup contains no disks");
355 }
356
357 local $ENV{PBS_PASSWORD} = $password;
358 my $cmd = [
359 '/usr/bin/proxmox-backup-client',
360 'backup',
361 '--repository', $repo,
362 '--backup-type', 'vm',
363 '--backup-id', "$vmid",
364 '--backup-time', $task->{backup_time},
d11e91d2 365 ];
c5983223
DM
366 push @$cmd, '--fingerprint', $fingerprint if defined($fingerprint);
367
368 push @$cmd, "qemu-server.conf:$conffile";
369 push @$cmd, "fw.conf:$firewall" if -e $firewall;
370 push @$cmd, @pathlist if scalar(@pathlist);
371
372 $self->loginfo("starting template backup");
373 $self->loginfo(join(' ', @$cmd));
374
375 $self->cmd($cmd);
376
377 return;
378 }
379
81dcd479
TL
380 # get list early so we die on unkown drive types before doing anything
381 my $devlist = _get_task_devlist($task);
c5983223 382
0b2f574b
TL
383 my ($stop_after_backup, $resume_on_backup) = $self->handle_vm_powerstate($vmid);
384 $self->enforce_vm_running_for_backup($vmid);
c5983223 385
f6168f1a 386 my $backup_job_uuid;
c5983223
DM
387 eval {
388 $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub {
d11e91d2 389 die "interrupted by signal\n";
c5983223
DM
390 };
391
1ece829a 392 my $fs_frozen = $self->qga_fs_freeze($vmid);
c5983223 393
d11e91d2
TL
394 my $params = {
395 format => "pbs",
396 'backup-file' => $repo,
397 'backup-id' => "$vmid",
398 'backup-time' => $task->{backup_time},
399 password => $password,
400 devlist => $devlist,
401 'config-file' => $conffile,
c5983223 402 };
d11e91d2
TL
403 $params->{fingerprint} = $fingerprint if defined($fingerprint);
404 $params->{'firewall-file'} = $firewall if -e $firewall;
c5983223 405
d11e91d2 406 my $res = eval { mon_cmd($vmid, "backup", %$params) };
c5983223 407 my $qmperr = $@;
d11e91d2 408 $backup_job_uuid = $res->{UUID} if $res;
c5983223 409
1ece829a
TL
410 if ($fs_frozen) {
411 $self->qga_fs_thaw($vmid);
c5983223
DM
412 }
413
414 die $qmperr if $qmperr;
f6168f1a 415 die "got no uuid for backup task\n" if !defined($backup_job_uuid);
c5983223 416
f6168f1a 417 $self->loginfo("started backup task '$backup_job_uuid'");
c5983223 418
0b2f574b 419 $self->resume_vm_after_job_start($task, $vmid);
c5983223 420
f6168f1a 421 $query_backup_status_loop->($self, $vmid, $backup_job_uuid);
c5983223
DM
422 };
423 my $err = $@;
c5983223
DM
424 if ($err) {
425 $self->logerr($err);
0b2f574b 426 $self->mon_backup_cancel($vmid) if defined($backup_job_uuid);
c5983223 427 }
0b2f574b 428 $self->restore_vm_power_state($vmid);
c5983223
DM
429
430 die $err if $err;
431}
432
02da0c65
TL
433my $fork_compressor_pipe = sub {
434 my ($self, $comp, $outfileno) = @_;
435
436 my @pipefd = POSIX::pipe();
437 my $cpid = fork();
438 die "unable to fork worker - $!" if !defined($cpid) || $cpid < 0;
439 if ($cpid == 0) {
440 eval {
441 POSIX::close($pipefd[1]);
442 # redirect STDIN
443 my $fd = fileno(STDIN);
444 close STDIN;
445 POSIX::close(0) if $fd != 0;
446 die "unable to redirect STDIN - $!"
447 if !open(STDIN, "<&", $pipefd[0]);
448
449 # redirect STDOUT
450 $fd = fileno(STDOUT);
451 close STDOUT;
452 POSIX::close (1) if $fd != 1;
453
454 die "unable to redirect STDOUT - $!"
455 if !open(STDOUT, ">&", $outfileno);
456
457 exec($comp);
458 die "fork compressor '$comp' failed\n";
459 };
460 if (my $err = $@) {
461 $self->logerr($err);
462 POSIX::_exit(1);
463 }
464 POSIX::_exit(0);
465 kill(-9, $$);
466 } else {
467 POSIX::close($pipefd[0]);
468 $outfileno = $pipefd[1];
469 }
470
471 return ($cpid, $outfileno);
472};
473
c5983223
DM
474sub archive_vma {
475 my ($self, $task, $vmid, $filename, $comp) = @_;
476
477 my $conffile = "$task->{tmpdir}/qemu-server.conf";
478 my $firewall = "$task->{tmpdir}/qemu-server.fw";
479
480 my $opts = $self->{vzdump}->{opts};
481
482 my $starttime = time();
1e3baf05 483
91bd6c90
DM
484 my $speed = 0;
485 if ($opts->{bwlimit}) {
874a096e 486 $speed = $opts->{bwlimit}*1024;
91bd6c90 487 }
1e3baf05 488
c82935e9 489 my $diskcount = scalar(@{$task->{disks}});
ffda963f 490 if (PVE::QemuConfig->is_template($self->{vmlist}->{$vmid}) || !$diskcount) {
23b4120b
DM
491 my @pathlist;
492 foreach my $di (@{$task->{disks}}) {
493 if ($di->{type} eq 'block' || $di->{type} eq 'file') {
494 push @pathlist, "$di->{qmdevice}=$di->{path}";
495 } else {
496 die "implement me";
497 }
498 }
499
c82935e9
DM
500 if (!$diskcount) {
501 $self->loginfo("backup contains no disks");
502 }
503
23b4120b
DM
504 my $outcmd;
505 if ($comp) {
874a096e 506 $outcmd = "exec:$comp";
23b4120b 507 } else {
874a096e 508 $outcmd = "exec:cat";
23b4120b
DM
509 }
510
a2fab11a 511 $outcmd .= " > $filename" if !$opts->{stdout};
23b4120b 512
c05f7f3f
WL
513 my $cmd = ['/usr/bin/vma', 'create', '-v', '-c', $conffile];
514 push @$cmd, '-c', $firewall if -e $firewall;
515 push @$cmd, $outcmd, @pathlist;
23b4120b
DM
516
517 $self->loginfo("starting template backup");
518 $self->loginfo(join(' ', @$cmd));
519
520 if ($opts->{stdout}) {
521 $self->cmd($cmd, output => ">&=" . fileno($opts->{stdout}));
522 } else {
523 $self->cmd($cmd);
524 }
525
526 return;
527 }
528
81dcd479 529 my $devlist = _get_task_devlist($task);
1e3baf05 530
0b2f574b 531 $self->enforce_vm_running_for_backup($vmid);
91bd6c90
DM
532
533 my $cpid;
d2cc2cbe
DM
534 my $backup_job_uuid;
535
91bd6c90
DM
536 eval {
537 $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub {
d11e91d2 538 die "interrupted by signal\n";
91bd6c90
DM
539 };
540
541 my $qmpclient = PVE::QMPClient->new();
542
91bd6c90
DM
543 my $backup_cb = sub {
544 my ($vmid, $resp) = @_;
d2cc2cbe 545 $backup_job_uuid = $resp->{return}->{UUID};
91bd6c90
DM
546 };
547
548 my $outfh;
549 if ($opts->{stdout}) {
550 $outfh = $opts->{stdout};
551 } else {
552 $outfh = IO::File->new($filename, "w") ||
553 die "unable to open file '$filename' - $!\n";
554 }
02da0c65 555 my $outfileno = fileno($outfh);
91bd6c90 556
91bd6c90 557 if ($comp) {
02da0c65 558 ($cpid, $outfileno) = $fork_compressor_pipe->($self, $comp, $outfileno);
91bd6c90
DM
559 }
560
561 my $add_fd_cb = sub {
562 my ($vmid, $resp) = @_;
563
c05f7f3f
WL
564 my $params = {
565 'backup-file' => "/dev/fdname/backup",
566 speed => $speed,
567 'config-file' => $conffile,
568 devlist => $devlist
569 };
c05f7f3f 570 $params->{'firewall-file'} = $firewall if -e $firewall;
d11e91d2 571
c05f7f3f 572 $qmpclient->queue_cmd($vmid, $backup_cb, 'backup', %$params);
91bd6c90
DM
573 };
574
d11e91d2 575 $qmpclient->queue_cmd($vmid, $add_fd_cb, 'getfd', fd => $outfileno, fdname => "backup");
ab6a9a0c 576
1ece829a 577 my $fs_frozen = $self->qga_fs_freeze($vmid);
874a096e 578
c5983223 579 eval { $qmpclient->queue_execute(30) };
f0f30448 580 my $qmperr = $@;
91bd6c90 581
1ece829a
TL
582 if ($fs_frozen) {
583 $self->qga_fs_thaw($vmid);
ab6a9a0c 584 }
d11e91d2 585
f0f30448 586 die $qmperr if $qmperr;
874a096e 587 die $qmpclient->{errors}->{$vmid} if $qmpclient->{errors}->{$vmid};
91bd6c90
DM
588
589 if ($cpid) {
874a096e 590 POSIX::close($outfileno) == 0 ||
91bd6c90
DM
591 die "close output file handle failed\n";
592 }
593
d2cc2cbe 594 die "got no uuid for backup task\n" if !defined($backup_job_uuid);
91bd6c90 595
d2cc2cbe 596 $self->loginfo("started backup task '$backup_job_uuid'");
91bd6c90 597
0b2f574b 598 $self->resume_vm_after_job_start($task, $vmid);
91bd6c90 599
d2cc2cbe 600 $query_backup_status_loop->($self, $vmid, $backup_job_uuid);
91bd6c90
DM
601 };
602 my $err = $@;
19599cd9 603 if ($err) {
60635a57 604 $self->logerr($err);
0b2f574b 605 $self->mon_backup_cancel($vmid) if defined($backup_job_uuid);
19599cd9
DM
606 }
607
0b2f574b 608 $self->restore_vm_power_state($vmid);
91bd6c90
DM
609
610 if ($err) {
874a096e
DM
611 if ($cpid) {
612 kill(9, $cpid);
91bd6c90
DM
613 waitpid($cpid, 0);
614 }
615 die $err;
616 }
617
618 if ($cpid && (waitpid($cpid, 0) > 0)) {
619 my $stat = $?;
620 my $ec = $stat >> 8;
621 my $signal = $stat & 127;
622 if ($ec || $signal) {
874a096e 623 die "$comp failed - wrong exit status $ec" .
91bd6c90
DM
624 ($signal ? " (signal $signal)\n" : "\n");
625 }
626 }
627}
628
81dcd479
TL
629sub _get_task_devlist {
630 my ($task) = @_;
631
632 my $devlist = '';
633 foreach my $di (@{$task->{disks}}) {
634 if ($di->{type} eq 'block' || $di->{type} eq 'file') {
635 $devlist .= ',' if $devlist;
636 $devlist .= $di->{qmdevice};
637 } else {
638 die "implement me (type '$di->{type}')";
639 }
640 }
641 return $devlist;
642}
643
1ece829a
TL
644sub qga_fs_freeze {
645 my ($self, $vmid) = @_;
646 return if !$self->{vmlist}->{$vmid}->{agent} || !$self->{vm_was_running};
647
648 if (!PVE::QemuServer::qga_check_running($vmid, 1)) {
649 $self->loginfo("skipping guest-agent 'fs-freeze', agent configured but not running?");
650 return;
651 }
652
653 $self->loginfo("issuing guest-agent 'fs-freeze' command");
654 eval { mon_cmd($vmid, "guest-fsfreeze-freeze") };
655 $self->logerr($@) if $@;
656
657 return 1; # even on mon command error, ensure we always thaw again
658}
659
660# only call if fs_freeze return 1
661sub qga_fs_thaw {
662 my ($self, $vmid) = @_;
663
664 $self->loginfo("issuing guest-agent 'fs-thaw' command");
665 eval { mon_cmd($vmid, "guest-fsfreeze-thaw") };
666 $self->logerr($@) if $@;
667}
668
0b2f574b
TL
669# we need a running QEMU/KVM process for backup, starts a paused (prelaunch)
670# one if VM isn't already running
671sub enforce_vm_running_for_backup {
672 my ($self, $vmid) = @_;
673
674 if (PVE::QemuServer::check_running($vmid)) {
675 $self->{vm_was_running} = 1;
676 return;
677 }
678
679 eval {
680 $self->loginfo("starting kvm to execute backup task");
681 # start with skiplock
682 PVE::QemuServer::vm_start($self->{storecfg}, $vmid, undef, 1, undef, 1);
683 };
684 die $@ if $@;
685}
686
687# resume VM againe once we got in a clear state (stop mode backup of running VM)
688sub resume_vm_after_job_start {
689 my ($self, $task, $vmid) = @_;
690
691 return if !$self->{vm_was_running};
692
693 if (my $stoptime = $task->{vmstoptime}) {
694 my $delay = time() - $task->{vmstoptime};
695 $task->{vmstoptime} = undef; # avoid printing 'online after ..' twice
696 $self->loginfo("resuming VM again after $delay seconds");
697 } else {
698 $self->loginfo("resuming VM again");
699 }
700 mon_cmd($vmid, 'cont');
701}
702
703# stop again if VM was not running before
704sub restore_vm_power_state {
705 my ($self, $vmid) = @_;
706
707 # we always let VMs keep running
708 return if $self->{vm_was_running};
709
710 eval {
711 my $resp = mon_cmd($vmid, 'query-status');
712 my $status = $resp && $resp->{status} ? $resp->{status} : 'unknown';
713 if ($status eq 'prelaunch') {
714 $self->loginfo("stopping kvm after backup task");
715 PVE::QemuServer::vm_stop($self->{storecfg}, $vmid, 1);
716 } else {
717 $self->loginfo("kvm status changed after backup ('$status') - keep VM running");
718 }
719 };
720 warn $@ if $@;
721}
722
723sub mon_backup_cancel {
724 my ($self, $vmid) = @_;
725
726 $self->loginfo("aborting backup job");
727 eval { mon_cmd($vmid, 'backup-cancel') };
728 $self->logerr($@) if $@;
729}
730
91bd6c90
DM
731sub snapshot {
732 my ($self, $task, $vmid) = @_;
733
734 # nothing to do
1e3baf05
DM
735}
736
737sub cleanup {
738 my ($self, $task, $vmid) = @_;
739
91bd6c90 740 # nothing to do ?
1e3baf05
DM
741}
742
7431;