]> git.proxmox.com Git - qemu-server.git/blob - PVE/VZDump/QemuServer.pm
vzdump: output sparseness again
[qemu-server.git] / PVE / VZDump / QemuServer.pm
1 package PVE::VZDump::QemuServer;
2
3 use strict;
4 use warnings;
5
6 use File::Basename;
7 use File::Path;
8 use IO::File;
9 use IPC::Open3;
10 use JSON;
11
12 use PVE::Cluster qw(cfs_read_file);
13 use PVE::INotify;
14 use PVE::IPCC;
15 use PVE::JSONSchema;
16 use PVE::QMPClient;
17 use PVE::Storage::Plugin;
18 use PVE::Storage::PBSPlugin;
19 use PVE::Storage;
20 use PVE::Tools;
21 use PVE::VZDump;
22
23 use PVE::QemuConfig;
24 use PVE::QemuServer;
25 use PVE::QemuServer::Machine;
26 use PVE::QemuServer::Monitor qw(mon_cmd);
27
28 use base qw (PVE::VZDump::Plugin);
29
30 sub new {
31 my ($class, $vzdump) = @_;
32
33 PVE::VZDump::check_bin('qm');
34
35 my $self = bless { vzdump => $vzdump }, $class;
36
37 $self->{vmlist} = PVE::QemuServer::vzlist();
38 $self->{storecfg} = PVE::Storage::config();
39
40 return $self;
41 };
42
43 sub type {
44 return 'qemu';
45 }
46
47 sub vmlist {
48 my ($self) = @_;
49 return [ keys %{$self->{vmlist}} ];
50 }
51
52 sub prepare {
53 my ($self, $task, $vmid, $mode) = @_;
54
55 $task->{disks} = [];
56
57 my $conf = $self->{vmlist}->{$vmid} = PVE::QemuConfig->load_config($vmid);
58
59 $self->loginfo("VM Name: $conf->{name}")
60 if defined($conf->{name});
61
62 $self->{vm_was_running} = 1;
63 if (!PVE::QemuServer::check_running($vmid)) {
64 $self->{vm_was_running} = 0;
65 }
66
67 $task->{hostname} = $conf->{name};
68
69 my $hostname = PVE::INotify::nodename();
70
71 my $vollist = [];
72 my $drivehash = {};
73 my $backup_volumes = PVE::QemuConfig->get_backup_volumes($conf);
74
75 foreach my $volume (@{$backup_volumes}) {
76 my $name = $volume->{key};
77 my $volume_config = $volume->{volume_config};
78 my $volid = $volume_config->{file};
79
80 if (!$volume->{included}) {
81 $self->loginfo("exclude disk '$name' '$volid' ($volume->{reason})");
82 next;
83 } elsif ($self->{vm_was_running} && $volume_config->{iothread}) {
84 if (!PVE::QemuServer::Machine::runs_at_least_qemu_version($vmid, 4, 0, 1)) {
85 die "disk '$name' '$volid' (iothread=on) can't use backup feature with running QEMU " .
86 "version < 4.0.1! Either set backup=no for this drive or upgrade QEMU and restart VM\n";
87 }
88 } else {
89 my $log = "include disk '$name' '$volid'";
90 if (defined(my $size = $volume_config->{size})) {
91 my $readable_size = PVE::JSONSchema::format_size($size);
92 $log .= " $readable_size";
93 }
94 $self->loginfo($log);
95 }
96
97 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
98 push @$vollist, $volid if $storeid;
99 $drivehash->{$name} = $volume->{volume_config};
100 }
101
102 PVE::Storage::activate_volumes($self->{storecfg}, $vollist);
103
104 foreach my $ds (sort keys %$drivehash) {
105 my $drive = $drivehash->{$ds};
106
107 my $volid = $drive->{file};
108 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
109
110 my $path = $volid;
111 if ($storeid) {
112 $path = PVE::Storage::path($self->{storecfg}, $volid);
113 }
114 next if !$path;
115
116 my ($size, $format) = eval { PVE::Storage::volume_size_info($self->{storecfg}, $volid, 5) };
117 die "no such volume '$volid'\n" if $@;
118
119 my $diskinfo = {
120 path => $path,
121 volid => $volid,
122 storeid => $storeid,
123 format => $format,
124 virtdev => $ds,
125 qmdevice => "drive-$ds",
126 };
127
128 if (-b $path) {
129 $diskinfo->{type} = 'block';
130 } else {
131 $diskinfo->{type} = 'file';
132 }
133
134 push @{$task->{disks}}, $diskinfo;
135 }
136 }
137
138 sub vm_status {
139 my ($self, $vmid) = @_;
140
141 my $running = PVE::QemuServer::check_running($vmid) ? 1 : 0;
142
143 return wantarray ? ($running, $running ? 'running' : 'stopped') : $running;
144 }
145
146 sub lock_vm {
147 my ($self, $vmid) = @_;
148
149 PVE::QemuConfig->set_lock($vmid, 'backup');
150 }
151
152 sub unlock_vm {
153 my ($self, $vmid) = @_;
154
155 PVE::QemuConfig->remove_lock($vmid, 'backup');
156 }
157
158 sub stop_vm {
159 my ($self, $task, $vmid) = @_;
160
161 my $opts = $self->{vzdump}->{opts};
162
163 my $wait = $opts->{stopwait} * 60;
164 # send shutdown and wait
165 $self->cmd ("qm shutdown $vmid --skiplock --keepActive --timeout $wait");
166 }
167
168 sub start_vm {
169 my ($self, $task, $vmid) = @_;
170
171 $self->cmd ("qm start $vmid --skiplock");
172 }
173
174 sub suspend_vm {
175 my ($self, $task, $vmid) = @_;
176
177 $self->cmd ("qm suspend $vmid --skiplock");
178 }
179
180 sub resume_vm {
181 my ($self, $task, $vmid) = @_;
182
183 $self->cmd ("qm resume $vmid --skiplock");
184 }
185
186 sub assemble {
187 my ($self, $task, $vmid) = @_;
188
189 my $conffile = PVE::QemuConfig->config_file($vmid);
190
191 my $outfile = "$task->{tmpdir}/qemu-server.conf";
192 my $firewall_src = "/etc/pve/firewall/$vmid.fw";
193 my $firewall_dest = "$task->{tmpdir}/qemu-server.fw";
194
195 my $outfd = IO::File->new (">$outfile") ||
196 die "unable to open '$outfile'";
197 my $conffd = IO::File->new ($conffile, 'r') ||
198 die "unable open '$conffile'";
199
200 my $found_snapshot;
201 my $found_pending;
202 while (defined (my $line = <$conffd>)) {
203 next if $line =~ m/^\#vzdump\#/; # just to be sure
204 next if $line =~ m/^\#qmdump\#/; # just to be sure
205 if ($line =~ m/^\[(.*)\]\s*$/) {
206 if ($1 =~ m/PENDING/i) {
207 $found_pending = 1;
208 } else {
209 $found_snapshot = 1;
210 }
211 }
212 next if $found_snapshot || $found_pending; # skip all snapshots and pending changes config data
213
214 if ($line =~ m/^unused\d+:\s*(\S+)\s*/) {
215 $self->loginfo("skip unused drive '$1' (not included into backup)");
216 next;
217 }
218 next if $line =~ m/^lock:/ || $line =~ m/^parent:/;
219
220 print $outfd $line;
221 }
222
223 foreach my $di (@{$task->{disks}}) {
224 if ($di->{type} eq 'block' || $di->{type} eq 'file') {
225 my $storeid = $di->{storeid} || '';
226 my $format = $di->{format} || '';
227 print $outfd "#qmdump#map:$di->{virtdev}:$di->{qmdevice}:$storeid:$format:\n";
228 } else {
229 die "internal error";
230 }
231 }
232
233 if ($found_snapshot) {
234 $self->loginfo("snapshots found (not included into backup)");
235 }
236 if ($found_pending) {
237 $self->loginfo("pending configuration changes found (not included into backup)");
238 }
239
240 PVE::Tools::file_copy($firewall_src, $firewall_dest) if -f $firewall_src;
241 }
242
243 sub archive {
244 my ($self, $task, $vmid, $filename, $comp) = @_;
245
246 my $opts = $self->{vzdump}->{opts};
247 my $scfg = $opts->{scfg};
248
249 if ($self->{vzdump}->{opts}->{pbs}) {
250 $self->archive_pbs($task, $vmid);
251 } else {
252 $self->archive_vma($task, $vmid, $filename, $comp);
253 }
254 }
255
256 # number, [precision=1]
257 my $num2str = sub {
258 return sprintf( "%." . ( $_[1] || 1 ) . "f", $_[0] );
259 };
260 sub bytes_to_human {
261 my ($bytes, $precission) = @_;
262
263 return $num2str->($bytes, $precission) . ' B' if $bytes < 1024;
264 my $kb = $bytes/1024;
265
266 return $num2str->($kb, $precission) . " KiB" if $kb < 1024;
267 my $mb = $kb/1024;
268
269 return $num2str->($mb, $precission) . " MiB" if $mb < 1024;
270 my $gb = $mb/1024;
271
272 return $num2str->($gb, $precission) . " GiB" if $gb < 1024;
273 my $tb = $gb/1024;
274
275 return $num2str->($tb, $precission) . " TiB";
276 };
277
278 my $bitmap_action_to_human = sub {
279 my ($self, $info) = @_;
280
281 my $action = $info->{action};
282
283 if ($action eq "not-used") {
284 return "disabled (no support)" if $self->{vm_was_running};
285 return "disabled (VM not running)";
286 } elsif ($action eq "not-used-removed") {
287 return "disabled (old bitmap cleared)";
288 } elsif ($action eq "new") {
289 return "created new";
290 } elsif ($action eq "used") {
291 if ($info->{dirty} == 0) {
292 return "OK (drive clean)";
293 } else {
294 my $size = bytes_to_human($info->{size});
295 my $dirty = bytes_to_human($info->{dirty});
296 return "OK ($dirty of $size dirty)";
297 }
298 } elsif ($action eq "invalid") {
299 return "existing bitmap was invalid and has been cleared";
300 } else {
301 return "unknown";
302 }
303 };
304
305 my $query_backup_status_loop = sub {
306 my ($self, $vmid, $job_uuid, $pbs_features) = @_;
307
308 my $starttime = time ();
309 my $last_time = $starttime;
310 my ($last_percent, $last_total, $last_target, $last_zero, $last_transferred) = (-1, 0, 0, 0, 0);
311 my ($transferred, $reused);
312
313 my $get_mbps = sub {
314 my ($mb, $delta) = @_;
315 return "0 B/s" if $mb <= 0;
316 my $bw = int(($mb / $delta));
317 return bytes_to_human($bw) . "/s";
318 };
319
320 my $target = 0;
321 my $last_reused = 0;
322 my $has_query_bitmap = 0;
323 if (defined($pbs_features) && $pbs_features->{'query-bitmap-info'}) {
324 $has_query_bitmap = 1;
325 my $total = 0;
326 my $bitmap_info = mon_cmd($vmid, 'query-pbs-bitmap-info');
327 foreach my $info (sort { $a->{drive} cmp $b->{drive} } @$bitmap_info) {
328 my $text = $bitmap_action_to_human->($self, $info);
329 my $drive = $info->{drive};
330 $drive =~ s/^drive-//; # for consistency
331 $self->loginfo("$drive: dirty-bitmap status: $text");
332 $target += $info->{dirty};
333 $total += $info->{size};
334 $last_reused += $info->{size} - $info->{dirty};
335 }
336 if ($target < $total) {
337 my $total_h = bytes_to_human($total);
338 my $target_h = bytes_to_human($target);
339 $self->loginfo("using fast incremental mode (dirty-bitmap), $target_h dirty of $total_h total");
340 }
341 }
342
343 while(1) {
344 my $status = mon_cmd($vmid, 'query-backup');
345
346 my $total = $status->{total} || 0;
347 $target = $total if !$has_query_bitmap;
348 $transferred = $status->{transferred} || 0;
349 $reused = $status->{reused};
350 my $percent = $target ? int(($transferred * 100)/$target) : 100;
351 my $zero = $status->{'zero-bytes'} || 0;
352
353 die "got unexpected uuid\n" if !$status->{uuid} || ($status->{uuid} ne $job_uuid);
354
355 my $ctime = time();
356 my $duration = $ctime - $starttime;
357
358 my $rbytes = $transferred - $last_transferred;
359 my $wbytes;
360 if ($reused) {
361 # reused includes zero bytes for PBS
362 $wbytes = $rbytes - ($reused - $last_reused);
363 } else {
364 $wbytes = $rbytes - ($zero - $last_zero);
365 }
366
367 my $timediff = ($ctime - $last_time) || 1; # fixme
368 my $mbps_read = $get_mbps->($rbytes, $timediff);
369 my $mbps_write = $get_mbps->($wbytes, $timediff);
370 my $target_h = bytes_to_human($target);
371 my $transferred_h = bytes_to_human($transferred);
372
373 my $statusline = "status: $percent% ($transferred_h of $target_h), duration $duration"
374 .", read: $mbps_read, write: $mbps_write";
375
376 my $res = $status->{status} || 'unknown';
377 if ($res ne 'active') {
378 $self->loginfo($statusline);
379 if ($res ne 'done') {
380 die (($status->{errmsg} || "unknown error") . "\n") if $res eq 'error';
381 die "got unexpected status '$res'\n";
382 }
383 $last_target = $target if $target;
384 $last_total = $total if $total;
385 $last_zero = $zero if $zero;
386 $last_transferred = $transferred if $transferred;
387 last;
388 }
389 if ($percent != $last_percent && ($timediff > 2)) {
390 $self->loginfo($statusline);
391 $last_percent = $percent;
392 $last_target = $target if $target;
393 $last_total = $total if $total;
394 $last_zero = $zero if $zero;
395 $last_transferred = $transferred if $transferred;
396 $last_time = $ctime;
397 $last_reused = $reused;
398 }
399 sleep(1);
400 }
401
402 my $duration = time() - $starttime;
403
404 if ($last_zero) {
405 my $zero_per = $last_target ? int(($last_zero * 100)/$last_target) : 0;
406 my $zero_h = bytes_to_human($last_zero, 2);
407 $self->loginfo("backup is sparse: $zero_h (${zero_per}%) total zero data");
408 }
409 if ($transferred) {
410 my $transferred_h = bytes_to_human($transferred, 2);
411 if ($reused) {
412 my $reused_h = bytes_to_human($reused, 2);
413 my $reuse_per = int($reused * 100 / $last_total);
414 $self->loginfo("backup was done incrementally, reused $reused_h (${reuse_per}%)");
415 }
416 if ($duration) {
417 my $mbps = $get_mbps->($transferred, $duration);
418 $self->loginfo("transferred $transferred_h in $duration seconds ($mbps)");
419 } else {
420 $self->loginfo("transferred $transferred_h in <1 seconds");
421 }
422 }
423
424 return {
425 total => $last_total,
426 reused => $reused,
427 };
428 };
429
430 sub archive_pbs {
431 my ($self, $task, $vmid) = @_;
432
433 my $conffile = "$task->{tmpdir}/qemu-server.conf";
434 my $firewall = "$task->{tmpdir}/qemu-server.fw";
435
436 my $opts = $self->{vzdump}->{opts};
437 my $scfg = $opts->{scfg};
438
439 my $starttime = time();
440
441 my $server = $scfg->{server};
442 my $datastore = $scfg->{datastore};
443 my $username = $scfg->{username} // 'root@pam';
444 my $fingerprint = $scfg->{fingerprint};
445
446 my $repo = "$username\@$server:$datastore";
447 my $password = PVE::Storage::PBSPlugin::pbs_get_password($scfg, $opts->{storage});
448 my $keyfile = PVE::Storage::PBSPlugin::pbs_encryption_key_file_name($scfg, $opts->{storage});
449
450 my $diskcount = scalar(@{$task->{disks}});
451 if (PVE::QemuConfig->is_template($self->{vmlist}->{$vmid}) || !$diskcount) {
452 my @pathlist;
453 # FIXME: accumulate disk sizes to use for backup job (email) log
454 foreach my $di (@{$task->{disks}}) {
455 if ($di->{type} eq 'block' || $di->{type} eq 'file') {
456 push @pathlist, "$di->{qmdevice}.img:$di->{path}";
457 } else {
458 die "implement me (type $di->{type})";
459 }
460 }
461
462 if (!$diskcount) {
463 $self->loginfo("backup contains no disks");
464 }
465
466 local $ENV{PBS_PASSWORD} = $password;
467 local $ENV{PBS_FINGERPRINT} = $fingerprint if defined($fingerprint);
468 my $cmd = [
469 '/usr/bin/proxmox-backup-client',
470 'backup',
471 '--repository', $repo,
472 '--backup-type', 'vm',
473 '--backup-id', "$vmid",
474 '--backup-time', $task->{backup_time},
475 ];
476
477 push @$cmd, "qemu-server.conf:$conffile";
478 push @$cmd, "fw.conf:$firewall" if -e $firewall;
479 push @$cmd, @pathlist if scalar(@pathlist);
480
481 $self->loginfo("starting template backup");
482 $self->loginfo(join(' ', @$cmd));
483
484 $self->cmd($cmd);
485
486 return;
487 }
488
489 # get list early so we die on unkown drive types before doing anything
490 my $devlist = _get_task_devlist($task);
491
492 $self->enforce_vm_running_for_backup($vmid);
493
494 my $backup_job_uuid;
495 eval {
496 $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub {
497 die "interrupted by signal\n";
498 };
499
500 my $qemu_support = eval { mon_cmd($vmid, "query-proxmox-support") };
501 if (!$qemu_support) {
502 die "PBS backups are not supported by the running QEMU version. Please make "
503 . "sure you've installed the latest version and the VM has been restarted.\n";
504 }
505
506 my $fs_frozen = $self->qga_fs_freeze($task, $vmid);
507
508 my $params = {
509 format => "pbs",
510 'backup-file' => $repo,
511 'backup-id' => "$vmid",
512 'backup-time' => $task->{backup_time},
513 password => $password,
514 devlist => $devlist,
515 'config-file' => $conffile,
516 };
517 $params->{fingerprint} = $fingerprint if defined($fingerprint);
518 $params->{'firewall-file'} = $firewall if -e $firewall;
519 if (-e $keyfile) {
520 $self->loginfo("enabling encryption");
521 $params->{keyfile} = $keyfile;
522 $params->{encrypt} = JSON::true;
523 } else {
524 $params->{encrypt} = JSON::false;
525 }
526
527 $params->{'use-dirty-bitmap'} = JSON::true
528 if $qemu_support->{'pbs-dirty-bitmap'} && $self->{vm_was_running};
529
530 $params->{timeout} = 60; # give some time to connect to the backup server
531
532 my $res = eval { mon_cmd($vmid, "backup", %$params) };
533 my $qmperr = $@;
534 $backup_job_uuid = $res->{UUID} if $res;
535
536 if ($fs_frozen) {
537 $self->qga_fs_thaw($vmid);
538 }
539
540 die $qmperr if $qmperr;
541 die "got no uuid for backup task\n" if !defined($backup_job_uuid);
542
543 $self->loginfo("started backup task '$backup_job_uuid'");
544
545 $self->resume_vm_after_job_start($task, $vmid);
546
547 my $stat = $query_backup_status_loop->($self, $vmid, $backup_job_uuid, $qemu_support);
548 $task->{size} = $stat->{total};
549 };
550 my $err = $@;
551 if ($err) {
552 $self->logerr($err);
553 $self->mon_backup_cancel($vmid) if defined($backup_job_uuid);
554 }
555 $self->restore_vm_power_state($vmid);
556
557 die $err if $err;
558 }
559
560 my $fork_compressor_pipe = sub {
561 my ($self, $comp, $outfileno) = @_;
562
563 my @pipefd = POSIX::pipe();
564 my $cpid = fork();
565 die "unable to fork worker - $!" if !defined($cpid) || $cpid < 0;
566 if ($cpid == 0) {
567 eval {
568 POSIX::close($pipefd[1]);
569 # redirect STDIN
570 my $fd = fileno(STDIN);
571 close STDIN;
572 POSIX::close(0) if $fd != 0;
573 die "unable to redirect STDIN - $!"
574 if !open(STDIN, "<&", $pipefd[0]);
575
576 # redirect STDOUT
577 $fd = fileno(STDOUT);
578 close STDOUT;
579 POSIX::close (1) if $fd != 1;
580
581 die "unable to redirect STDOUT - $!"
582 if !open(STDOUT, ">&", $outfileno);
583
584 exec($comp);
585 die "fork compressor '$comp' failed\n";
586 };
587 if (my $err = $@) {
588 $self->logerr($err);
589 POSIX::_exit(1);
590 }
591 POSIX::_exit(0);
592 kill(-9, $$);
593 } else {
594 POSIX::close($pipefd[0]);
595 $outfileno = $pipefd[1];
596 }
597
598 return ($cpid, $outfileno);
599 };
600
601 sub archive_vma {
602 my ($self, $task, $vmid, $filename, $comp) = @_;
603
604 my $conffile = "$task->{tmpdir}/qemu-server.conf";
605 my $firewall = "$task->{tmpdir}/qemu-server.fw";
606
607 my $opts = $self->{vzdump}->{opts};
608
609 my $starttime = time();
610
611 my $speed = 0;
612 if ($opts->{bwlimit}) {
613 $speed = $opts->{bwlimit}*1024;
614 }
615
616 my $diskcount = scalar(@{$task->{disks}});
617 if (PVE::QemuConfig->is_template($self->{vmlist}->{$vmid}) || !$diskcount) {
618 my @pathlist;
619 foreach my $di (@{$task->{disks}}) {
620 if ($di->{type} eq 'block' || $di->{type} eq 'file') {
621 push @pathlist, "$di->{qmdevice}=$di->{path}";
622 } else {
623 die "implement me";
624 }
625 }
626
627 if (!$diskcount) {
628 $self->loginfo("backup contains no disks");
629 }
630
631 my $outcmd;
632 if ($comp) {
633 $outcmd = "exec:$comp";
634 } else {
635 $outcmd = "exec:cat";
636 }
637
638 $outcmd .= " > $filename" if !$opts->{stdout};
639
640 my $cmd = ['/usr/bin/vma', 'create', '-v', '-c', $conffile];
641 push @$cmd, '-c', $firewall if -e $firewall;
642 push @$cmd, $outcmd, @pathlist;
643
644 $self->loginfo("starting template backup");
645 $self->loginfo(join(' ', @$cmd));
646
647 if ($opts->{stdout}) {
648 $self->cmd($cmd, output => ">&" . fileno($opts->{stdout}));
649 } else {
650 $self->cmd($cmd);
651 }
652
653 return;
654 }
655
656 my $devlist = _get_task_devlist($task);
657
658 $self->enforce_vm_running_for_backup($vmid);
659
660 my $cpid;
661 my $backup_job_uuid;
662
663 eval {
664 $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub {
665 die "interrupted by signal\n";
666 };
667
668 my $outfh;
669 if ($opts->{stdout}) {
670 $outfh = $opts->{stdout};
671 } else {
672 $outfh = IO::File->new($filename, "w") ||
673 die "unable to open file '$filename' - $!\n";
674 }
675 my $outfileno = fileno($outfh);
676
677 if ($comp) {
678 ($cpid, $outfileno) = $fork_compressor_pipe->($self, $comp, $outfileno);
679 }
680
681 my $qmpclient = PVE::QMPClient->new();
682 my $backup_cb = sub {
683 my ($vmid, $resp) = @_;
684 $backup_job_uuid = $resp->{return}->{UUID};
685 };
686 my $add_fd_cb = sub {
687 my ($vmid, $resp) = @_;
688
689 my $params = {
690 'backup-file' => "/dev/fdname/backup",
691 speed => $speed,
692 'config-file' => $conffile,
693 devlist => $devlist
694 };
695 $params->{'firewall-file'} = $firewall if -e $firewall;
696
697 $qmpclient->queue_cmd($vmid, $backup_cb, 'backup', %$params);
698 };
699
700 $qmpclient->queue_cmd($vmid, $add_fd_cb, 'getfd', fd => $outfileno, fdname => "backup");
701
702 my $fs_frozen = $self->qga_fs_freeze($task, $vmid);
703
704 eval { $qmpclient->queue_execute(30) };
705 my $qmperr = $@;
706
707 if ($fs_frozen) {
708 $self->qga_fs_thaw($vmid);
709 }
710
711 die $qmperr if $qmperr;
712 die $qmpclient->{errors}->{$vmid} if $qmpclient->{errors}->{$vmid};
713
714 if ($cpid) {
715 POSIX::close($outfileno) == 0 ||
716 die "close output file handle failed\n";
717 }
718
719 die "got no uuid for backup task\n" if !defined($backup_job_uuid);
720
721 $self->loginfo("started backup task '$backup_job_uuid'");
722
723 $self->resume_vm_after_job_start($task, $vmid);
724
725 $query_backup_status_loop->($self, $vmid, $backup_job_uuid);
726 };
727 my $err = $@;
728 if ($err) {
729 $self->logerr($err);
730 $self->mon_backup_cancel($vmid) if defined($backup_job_uuid);
731 }
732
733 $self->restore_vm_power_state($vmid);
734
735 if ($err) {
736 if ($cpid) {
737 kill(9, $cpid);
738 waitpid($cpid, 0);
739 }
740 die $err;
741 }
742
743 if ($cpid && (waitpid($cpid, 0) > 0)) {
744 my $stat = $?;
745 my $ec = $stat >> 8;
746 my $signal = $stat & 127;
747 if ($ec || $signal) {
748 die "$comp failed - wrong exit status $ec" .
749 ($signal ? " (signal $signal)\n" : "\n");
750 }
751 }
752 }
753
754 sub _get_task_devlist {
755 my ($task) = @_;
756
757 my $devlist = '';
758 foreach my $di (@{$task->{disks}}) {
759 if ($di->{type} eq 'block' || $di->{type} eq 'file') {
760 $devlist .= ',' if $devlist;
761 $devlist .= $di->{qmdevice};
762 } else {
763 die "implement me (type '$di->{type}')";
764 }
765 }
766 return $devlist;
767 }
768
769 sub qga_fs_freeze {
770 my ($self, $task, $vmid) = @_;
771 return if !$self->{vmlist}->{$vmid}->{agent} || $task->{mode} eq 'stop' || !$self->{vm_was_running};
772
773 if (!PVE::QemuServer::qga_check_running($vmid, 1)) {
774 $self->loginfo("skipping guest-agent 'fs-freeze', agent configured but not running?");
775 return;
776 }
777
778 $self->loginfo("issuing guest-agent 'fs-freeze' command");
779 eval { mon_cmd($vmid, "guest-fsfreeze-freeze") };
780 $self->logerr($@) if $@;
781
782 return 1; # even on mon command error, ensure we always thaw again
783 }
784
785 # only call if fs_freeze return 1
786 sub qga_fs_thaw {
787 my ($self, $vmid) = @_;
788
789 $self->loginfo("issuing guest-agent 'fs-thaw' command");
790 eval { mon_cmd($vmid, "guest-fsfreeze-thaw") };
791 $self->logerr($@) if $@;
792 }
793
794 # we need a running QEMU/KVM process for backup, starts a paused (prelaunch)
795 # one if VM isn't already running
796 sub enforce_vm_running_for_backup {
797 my ($self, $vmid) = @_;
798
799 if (PVE::QemuServer::check_running($vmid)) {
800 $self->{vm_was_running} = 1;
801 return;
802 }
803
804 eval {
805 $self->loginfo("starting kvm to execute backup task");
806 # start with skiplock
807 my $params = {
808 skiplock => 1,
809 paused => 1,
810 };
811 PVE::QemuServer::vm_start($self->{storecfg}, $vmid, $params);
812 };
813 die $@ if $@;
814 }
815
816 # resume VM againe once we got in a clear state (stop mode backup of running VM)
817 sub resume_vm_after_job_start {
818 my ($self, $task, $vmid) = @_;
819
820 return if !$self->{vm_was_running};
821
822 if (my $stoptime = $task->{vmstoptime}) {
823 my $delay = time() - $task->{vmstoptime};
824 $task->{vmstoptime} = undef; # avoid printing 'online after ..' twice
825 $self->loginfo("resuming VM again after $delay seconds");
826 } else {
827 $self->loginfo("resuming VM again");
828 }
829 mon_cmd($vmid, 'cont');
830 }
831
832 # stop again if VM was not running before
833 sub restore_vm_power_state {
834 my ($self, $vmid) = @_;
835
836 # we always let VMs keep running
837 return if $self->{vm_was_running};
838
839 eval {
840 my $resp = mon_cmd($vmid, 'query-status');
841 my $status = $resp && $resp->{status} ? $resp->{status} : 'unknown';
842 if ($status eq 'prelaunch') {
843 $self->loginfo("stopping kvm after backup task");
844 PVE::QemuServer::vm_stop($self->{storecfg}, $vmid, 1);
845 } else {
846 $self->loginfo("kvm status changed after backup ('$status') - keep VM running");
847 }
848 };
849 warn $@ if $@;
850 }
851
852 sub mon_backup_cancel {
853 my ($self, $vmid) = @_;
854
855 $self->loginfo("aborting backup job");
856 eval { mon_cmd($vmid, 'backup-cancel') };
857 $self->logerr($@) if $@;
858 }
859
860 sub snapshot {
861 my ($self, $task, $vmid) = @_;
862
863 # nothing to do
864 }
865
866 sub cleanup {
867 my ($self, $task, $vmid) = @_;
868
869 # nothing to do ?
870 }
871
872 1;