1 package PVE
::VZDump
::QemuServer
;
11 use POSIX
qw(EINTR EAGAIN);
13 use PVE
::Cluster
qw(cfs_read_file);
18 use PVE
::RESTEnvironment
qw(log_warn);
20 use PVE
::Storage
::Plugin
;
21 use PVE
::Storage
::PBSPlugin
;
25 use PVE
::Format
qw(render_duration render_bytes);
29 use PVE
::QemuServer
::Machine
;
30 use PVE
::QemuServer
::Monitor
qw(mon_cmd);
32 use base qw
(PVE
::VZDump
::Plugin
);
35 my ($class, $vzdump) = @_;
37 PVE
::VZDump
::check_bin
('qm');
39 my $self = bless { vzdump
=> $vzdump }, $class;
41 $self->{vmlist
} = PVE
::QemuServer
::vzlist
();
42 $self->{storecfg
} = PVE
::Storage
::config
();
53 return [ keys %{$self->{vmlist
}} ];
57 my ($self, $task, $vmid, $mode) = @_;
61 my $conf = $self->{vmlist
}->{$vmid} = PVE
::QemuConfig-
>load_config($vmid);
63 $self->loginfo("VM Name: $conf->{name}")
64 if defined($conf->{name
});
66 $self->{vm_was_running
} = 1;
67 $self->{vm_was_paused
} = 0;
68 if (!PVE
::QemuServer
::check_running
($vmid)) {
69 $self->{vm_was_running
} = 0;
70 } elsif (PVE
::QemuServer
::vm_is_paused
($vmid)) {
71 $self->{vm_was_paused
} = 1;
74 $task->{hostname
} = $conf->{name
};
76 my $hostname = PVE
::INotify
::nodename
();
80 my $backup_volumes = PVE
::QemuConfig-
>get_backup_volumes($conf);
82 foreach my $volume (@{$backup_volumes}) {
83 my $name = $volume->{key
};
84 my $volume_config = $volume->{volume_config
};
85 my $volid = $volume_config->{file
};
87 if (!$volume->{included
}) {
88 $self->loginfo("exclude disk '$name' '$volid' ($volume->{reason})");
90 } elsif ($self->{vm_was_running
} && $volume_config->{iothread
} &&
91 !PVE
::QemuServer
::Machine
::runs_at_least_qemu_version
($vmid, 4, 0, 1)) {
92 die "disk '$name' '$volid' (iothread=on) can't use backup feature with running QEMU " .
93 "version < 4.0.1! Either set backup=no for this drive or upgrade QEMU and restart VM\n";
95 my $log = "include disk '$name' '$volid'";
96 if (defined(my $size = $volume_config->{size
})) {
97 my $readable_size = PVE
::JSONSchema
::format_size
($size);
98 $log .= " $readable_size";
100 $self->loginfo($log);
103 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
104 push @$vollist, $volid if $storeid;
105 $drivehash->{$name} = $volume->{volume_config
};
108 PVE
::Storage
::activate_volumes
($self->{storecfg
}, $vollist);
110 foreach my $ds (sort keys %$drivehash) {
111 my $drive = $drivehash->{$ds};
113 my $volid = $drive->{file
};
114 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
118 $path = PVE
::Storage
::path
($self->{storecfg
}, $volid);
124 # The call in list context can be expensive for certain plugins like RBD, just get size
125 $size = eval { PVE
::Storage
::volume_size_info
($self->{storecfg
}, $volid, 5) };
126 die "cannot determine size of volume '$volid' - $@\n" if $@;
128 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $storeid);
129 $format = PVE
::QemuServer
::qemu_img_format
($scfg, $volname);
131 ($size, $format) = eval {
132 PVE
::Storage
::volume_size_info
($self->{storecfg
}, $volid, 5);
134 die "cannot determine size and format of volume '$volid' - $@\n" if $@;
143 qmdevice
=> "drive-$ds",
146 if ($ds eq 'tpmstate0') {
147 # TPM drive only exists for backup, which is reflected in the name
148 $diskinfo->{qmdevice
} = 'drive-tpmstate0-backup';
149 $task->{tpmpath
} = $path;
153 $diskinfo->{type
} = 'block';
155 $diskinfo->{type
} = 'file';
158 push @{$task->{disks
}}, $diskinfo;
163 my ($self, $vmid) = @_;
165 my $running = PVE
::QemuServer
::check_running
($vmid) ?
1 : 0;
167 return wantarray ?
($running, $running ?
'running' : 'stopped') : $running;
171 my ($self, $vmid) = @_;
173 PVE
::QemuConfig-
>set_lock($vmid, 'backup');
177 my ($self, $vmid) = @_;
179 PVE
::QemuConfig-
>remove_lock($vmid, 'backup');
183 my ($self, $task, $vmid) = @_;
185 my $opts = $self->{vzdump
}->{opts
};
187 my $wait = $opts->{stopwait
} * 60;
188 # send shutdown and wait
189 $self->cmd ("qm shutdown $vmid --skiplock --keepActive --timeout $wait");
193 my ($self, $task, $vmid) = @_;
195 $self->cmd ("qm start $vmid --skiplock");
199 my ($self, $task, $vmid) = @_;
201 return if $self->{vm_was_paused
};
203 $self->cmd ("qm suspend $vmid --skiplock");
207 my ($self, $task, $vmid) = @_;
209 return if $self->{vm_was_paused
};
211 $self->cmd ("qm resume $vmid --skiplock");
215 my ($self, $task, $vmid) = @_;
217 my $conffile = PVE
::QemuConfig-
>config_file($vmid);
219 my $outfile = "$task->{tmpdir}/qemu-server.conf";
220 my $firewall_src = "/etc/pve/firewall/$vmid.fw";
221 my $firewall_dest = "$task->{tmpdir}/qemu-server.fw";
223 my $outfd = IO
::File-
>new (">$outfile") ||
224 die "unable to open '$outfile'";
225 my $conffd = IO
::File-
>new ($conffile, 'r') ||
226 die "unable open '$conffile'";
231 while (defined (my $line = <$conffd>)) {
232 next if $line =~ m/^\#vzdump\#/; # just to be sure
233 next if $line =~ m/^\#qmdump\#/; # just to be sure
234 if ($line =~ m/^\[(.*)\]\s*$/) {
235 if ($1 =~ m/PENDING/i) {
237 } elsif ($1 =~ m/special:cloudinit/) {
238 $found_cloudinit = 1;
243 next if $found_snapshot || $found_pending || $found_cloudinit; # skip all snapshots,pending changes and cloudinit config data
245 if ($line =~ m/^unused\d+:\s*(\S+)\s*/) {
246 $self->loginfo("skip unused drive '$1' (not included into backup)");
249 next if $line =~ m/^lock:/ || $line =~ m/^parent:/;
254 foreach my $di (@{$task->{disks
}}) {
255 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
256 my $storeid = $di->{storeid
} || '';
257 my $format = $di->{format
} || '';
258 print $outfd "#qmdump#map:$di->{virtdev}:$di->{qmdevice}:$storeid:$format:\n";
260 die "internal error";
264 if ($found_snapshot) {
265 $self->loginfo("snapshots found (not included into backup)");
267 if ($found_pending) {
268 $self->loginfo("pending configuration changes found (not included into backup)");
271 PVE
::Tools
::file_copy
($firewall_src, $firewall_dest) if -f
$firewall_src;
275 my ($self, $task, $vmid, $filename, $comp) = @_;
277 my $opts = $self->{vzdump
}->{opts
};
278 my $scfg = $opts->{scfg
};
280 if ($self->{vzdump
}->{opts
}->{pbs
}) {
281 $self->archive_pbs($task, $vmid);
283 $self->archive_vma($task, $vmid, $filename, $comp);
287 my $bitmap_action_to_human = sub {
288 my ($self, $info) = @_;
290 my $action = $info->{action
};
292 if ($action eq "not-used") {
293 return "disabled (no support)";
294 } elsif ($action eq "not-used-removed") {
295 return "disabled (old bitmap cleared)";
296 } elsif ($action eq "new") {
297 return "created new";
298 } elsif ($action eq "used") {
299 if ($info->{dirty
} == 0) {
300 return "OK (drive clean)";
302 my $size = render_bytes
($info->{size
}, 1);
303 my $dirty = render_bytes
($info->{dirty
}, 1);
304 return "OK ($dirty of $size dirty)";
306 } elsif ($action eq "invalid") {
307 return "existing bitmap was invalid and has been cleared";
313 my $query_backup_status_loop = sub {
314 my ($self, $vmid, $job_uuid, $qemu_support) = @_;
316 my $starttime = time ();
317 my $last_time = $starttime;
318 my ($last_percent, $last_total, $last_target, $last_zero, $last_transferred) = (-1, 0, 0, 0, 0);
319 my ($transferred, $reused);
322 my ($mb, $delta) = @_;
323 return "0 B/s" if $mb <= 0;
324 my $bw = int(($mb / $delta));
325 return render_bytes
($bw, 1) . "/s";
330 my $has_query_bitmap = $qemu_support && $qemu_support->{'query-bitmap-info'};
331 my $is_template = PVE
::QemuConfig-
>is_template($self->{vmlist
}->{$vmid});
332 if ($has_query_bitmap) {
334 my $bitmap_info = mon_cmd
($vmid, 'query-pbs-bitmap-info');
335 for my $info (sort { $a->{drive
} cmp $b->{drive
} } @$bitmap_info) {
337 my $text = $bitmap_action_to_human->($self, $info);
338 my $drive = $info->{drive
};
339 $drive =~ s/^drive-//; # for consistency
340 $self->loginfo("$drive: dirty-bitmap status: $text");
342 $target += $info->{dirty
};
343 $total += $info->{size
};
344 $last_reused += $info->{size
} - $info->{dirty
};
346 if ($target < $total) {
347 my $total_h = render_bytes
($total, 1);
348 my $target_h = render_bytes
($target, 1);
349 $self->loginfo("using fast incremental mode (dirty-bitmap), $target_h dirty of $total_h total");
353 my $last_finishing = 0;
355 my $status = mon_cmd
($vmid, 'query-backup');
357 my $total = $status->{total
} || 0;
358 my $dirty = $status->{dirty
};
359 $target = (defined($dirty) && $dirty < $total) ?
$dirty : $total if !$has_query_bitmap;
360 $transferred = $status->{transferred
} || 0;
361 $reused = $status->{reused
};
362 my $percent = $target ?
int(($transferred * 100)/$target) : 100;
363 my $zero = $status->{'zero-bytes'} || 0;
365 die "got unexpected uuid\n" if !$status->{uuid
} || ($status->{uuid
} ne $job_uuid);
368 my $duration = $ctime - $starttime;
370 my $rbytes = $transferred - $last_transferred;
373 # reused includes zero bytes for PBS
374 $wbytes = $rbytes - ($reused - $last_reused);
376 $wbytes = $rbytes - ($zero - $last_zero);
379 my $timediff = ($ctime - $last_time) || 1; # fixme
380 my $mbps_read = $get_mbps->($rbytes, $timediff);
381 my $mbps_write = $get_mbps->($wbytes, $timediff);
382 my $target_h = render_bytes
($target, 1);
383 my $transferred_h = render_bytes
($transferred, 1);
385 my $statusline = sprintf("%3d%% ($transferred_h of $target_h) in %s"
386 .", read: $mbps_read, write: $mbps_write", $percent, render_duration
($duration));
388 my $res = $status->{status
} || 'unknown';
389 if ($res ne 'active') {
390 if ($last_percent < 100) {
391 $self->loginfo($statusline);
393 if ($res ne 'done') {
394 die (($status->{errmsg
} || "unknown error") . "\n") if $res eq 'error';
395 die "got unexpected status '$res'\n";
397 $last_target = $target if $target;
398 $last_total = $total if $total;
399 $last_zero = $zero if $zero;
400 $last_transferred = $transferred if $transferred;
403 if ($percent != $last_percent && ($timediff > 2)) {
404 $self->loginfo($statusline);
405 $last_percent = $percent;
406 $last_target = $target if $target;
407 $last_total = $total if $total;
408 $last_zero = $zero if $zero;
409 $last_transferred = $transferred if $transferred;
411 $last_reused = $reused;
413 if (!$last_finishing && $status->{finishing
}) {
414 $self->loginfo("Waiting for server to finish backup validation...");
416 $last_finishing = $status->{finishing
};
421 my $duration = time() - $starttime;
424 my $zero_per = $last_target ?
int(($last_zero * 100)/$last_target) : 0;
425 my $zero_h = render_bytes
($last_zero);
426 $self->loginfo("backup is sparse: $zero_h (${zero_per}%) total zero data");
429 my $reused_h = render_bytes
($reused);
430 my $reuse_per = int($reused * 100 / $last_total);
431 $self->loginfo("backup was done incrementally, reused $reused_h (${reuse_per}%)");
434 my $transferred_h = render_bytes
($transferred);
436 my $mbps = $get_mbps->($transferred, $duration);
437 $self->loginfo("transferred $transferred_h in $duration seconds ($mbps)");
439 $self->loginfo("transferred $transferred_h in <1 seconds");
444 total
=> $last_total,
449 my $attach_tpmstate_drive = sub {
450 my ($self, $task, $vmid) = @_;
452 return if !$task->{tpmpath
};
454 # unconditionally try to remove the tpmstate-named drive - it only exists
455 # for backing up, and avoids errors if left over from some previous event
456 eval { PVE
::QemuServer
::qemu_drivedel
($vmid, "tpmstate0-backup"); };
458 $self->loginfo('attaching TPM drive to QEMU for backup');
460 my $drive = "file=$task->{tpmpath},if=none,read-only=on,id=drive-tpmstate0-backup";
461 $drive =~ s/\\/\\\\/g;
462 my $ret = PVE
::QemuServer
::Monitor
::hmp_cmd
($vmid, "drive_add auto \"$drive\"");
463 die "attaching TPM drive failed
- $ret\n" if $ret !~ m/OK/s;
466 my $detach_tpmstate_drive = sub {
467 my ($task, $vmid) = @_;
468 return if !$task->{tpmpath} || !PVE::QemuServer::check_running($vmid);
469 eval { PVE::QemuServer::qemu_drivedel($vmid, "tpmstate0-backup
"); };
472 my sub add_backup_performance_options {
473 my ($qmp_param, $perf, $qemu_support) = @_;
475 return if !$perf || scalar(keys $perf->%*) == 0;
477 if (!$qemu_support) {
478 my $settings_string = join(', ', sort keys $perf->%*);
479 log_warn("ignoring setting
(s
): $settings_string - issue checking
if supported
");
483 if (defined($perf->{'max-workers'})) {
484 if ($qemu_support->{'backup-max-workers'}) {
485 $qmp_param->{'max-workers'} = int($perf->{'max-workers'});
487 log_warn("ignoring
'max-workers' setting
- not supported by running QEMU
");
493 my ($self, $task, $vmid) = @_;
495 my $conffile = "$task->{tmpdir
}/qemu-server
.conf
";
496 my $firewall = "$task->{tmpdir
}/qemu-server
.fw
";
498 my $opts = $self->{vzdump}->{opts};
499 my $scfg = $opts->{scfg};
501 my $starttime = time();
503 my $fingerprint = $scfg->{fingerprint};
504 my $repo = PVE::PBSClient::get_repository($scfg);
505 my $password = PVE::Storage::PBSPlugin::pbs_get_password($scfg, $opts->{storage});
506 my $keyfile = PVE::Storage::PBSPlugin::pbs_encryption_key_file_name($scfg, $opts->{storage});
507 my $master_keyfile = PVE::Storage::PBSPlugin::pbs_master_pubkey_file_name($scfg, $opts->{storage});
509 my $diskcount = scalar(@{$task->{disks}});
510 # proxmox-backup-client can only handle raw files and block devs
511 # only use it (directly) for disk-less VMs
514 $self->loginfo("backup contains
no disks
");
516 local $ENV{PBS_PASSWORD} = $password;
517 local $ENV{PBS_FINGERPRINT} = $fingerprint if defined($fingerprint);
519 '/usr/bin/proxmox-backup-client',
521 '--repository', $repo,
522 '--backup-type', 'vm',
523 '--backup-id', "$vmid",
524 '--backup-time', $task->{backup_time},
526 if (defined(my $ns = $scfg->{namespace})) {
527 push @$cmd, '--ns', $ns;
530 push @$cmd, "qemu-server
.conf
:$conffile";
531 push @$cmd, "fw
.conf
:$firewall" if -e $firewall;
533 $self->loginfo("starting template backup
");
534 $self->loginfo(join(' ', @$cmd));
541 # get list early so we die on unkown drive types before doing anything
542 my $devlist = _get_task_devlist($task);
544 $self->enforce_vm_running_for_backup($vmid);
545 $self->{qmeventd_fh} = PVE::QemuServer::register_qmeventd_handle($vmid);
549 $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub {
550 die "interrupted by signal
\n";
553 my $qemu_support = eval { mon_cmd($vmid, "query-proxmox-support
") };
555 if (!$qemu_support || $err) {
556 die "query-proxmox-support returned empty value
\n" if !$err;
557 if ($err =~ m/The command query-proxmox-support has not been found/) {
558 die "PBS backups are
not supported by the running QEMU version
. Please make
"
559 . "sure you
've installed the latest version and the VM has been restarted.\n";
561 die "QMP command query-proxmox-support failed - $err\n";
565 if (!defined($qemu_support->{"pbs-masterkey"}) && -e $master_keyfile) {
566 $self->loginfo("WARNING: backup target is configured with master key, but running QEMU version does not support master keys.");
567 $self->loginfo("Please make sure you've installed the latest version
and the VM
has been restarted to
use master key feature
.");
568 $master_keyfile = undef; # skip rest of master key handling below
571 $attach_tpmstate_drive->($self, $task, $vmid);
573 my $fs_frozen = $self->qga_fs_freeze($task, $vmid);
577 'backup-file' => $repo,
578 'backup-id' => "$vmid",
579 'backup-time' => $task->{backup_time},
580 password => $password,
582 'config-file' => $conffile,
584 if (defined(my $ns = $scfg->{namespace})) {
585 $params->{'backup-ns'} = $ns;
588 $params->{speed} = $opts->{bwlimit}*1024 if $opts->{bwlimit};
589 add_backup_performance_options($params, $opts->{performance}, $qemu_support);
591 $params->{fingerprint} = $fingerprint if defined($fingerprint);
592 $params->{'firewall-file'} = $firewall if -e $firewall;
594 $self->loginfo("enabling encryption
");
595 $params->{keyfile} = $keyfile;
596 $params->{encrypt} = JSON::true;
597 if (defined($master_keyfile)) {
598 if (-e $master_keyfile) {
599 $self->loginfo("enabling master key feature
");
600 $params->{"master-keyfile
"} = $master_keyfile;
601 } elsif ($scfg->{'master-pubkey'}) {
602 die "master public key configured but
no key file found
\n";
606 my $encryption_fp = $scfg->{'encryption-key'};
607 die "encryption configured
('$encryption_fp') but
no encryption key file found
!\n"
609 $self->loginfo("WARNING
: backup target
is configured with master key
, but this backup
is not encrypted
- master key settings will be ignored
!")
610 if defined($master_keyfile) && -e $master_keyfile;
611 $params->{encrypt} = JSON::false;
614 my $is_template = PVE::QemuConfig->is_template($self->{vmlist}->{$vmid});
615 $params->{'use-dirty-bitmap'} = JSON::true
616 if $qemu_support->{'pbs-dirty-bitmap'} && !$is_template;
618 $params->{timeout} = 125; # give some time to connect to the backup server
620 my $res = eval { mon_cmd($vmid, "backup
", %$params) };
622 $backup_job_uuid = $res->{UUID} if $res;
625 $self->qga_fs_thaw($vmid);
628 die $qmperr if $qmperr;
629 die "got
no uuid
for backup task
\n" if !defined($backup_job_uuid);
631 $self->loginfo("started backup task
'$backup_job_uuid'");
633 $self->resume_vm_after_job_start($task, $vmid);
635 my $stat = $query_backup_status_loop->($self, $vmid, $backup_job_uuid, $qemu_support);
636 $task->{size} = $stat->{total};
641 $self->mon_backup_cancel($vmid);
642 $self->resume_vm_after_job_start($task, $vmid);
644 $self->restore_vm_power_state($vmid);
649 my $fork_compressor_pipe = sub {
650 my ($self, $comp, $outfileno) = @_;
652 my @pipefd = POSIX::pipe();
654 die "unable to
fork worker
- $!" if !defined($cpid) || $cpid < 0;
657 POSIX::close($pipefd[1]);
659 my $fd = fileno(STDIN);
661 POSIX::close(0) if $fd != 0;
662 die "unable to redirect STDIN
- $!"
663 if !open(STDIN, "<&", $pipefd[0]);
666 $fd = fileno(STDOUT);
668 POSIX::close (1) if $fd != 1;
670 die "unable to redirect STDOUT
- $!"
671 if !open(STDOUT, ">&", $outfileno);
674 die "fork compressor
'$comp' failed
\n";
683 POSIX::close($pipefd[0]);
684 $outfileno = $pipefd[1];
687 return ($cpid, $outfileno);
691 my ($self, $task, $vmid, $filename, $comp) = @_;
693 my $conffile = "$task->{tmpdir
}/qemu-server
.conf
";
694 my $firewall = "$task->{tmpdir
}/qemu-server
.fw
";
696 my $opts = $self->{vzdump}->{opts};
698 my $starttime = time();
701 if ($opts->{bwlimit}) {
702 $speed = $opts->{bwlimit}*1024;
705 my $diskcount = scalar(@{$task->{disks}});
706 if (PVE::QemuConfig->is_template($self->{vmlist}->{$vmid}) || !$diskcount) {
708 foreach my $di (@{$task->{disks}}) {
709 if ($di->{type} eq 'block' || $di->{type} eq 'file') {
710 push @pathlist, "$di->{qmdevice
}=$di->{path
}";
717 $self->loginfo("backup contains
no disks
");
722 $outcmd = "exec:$comp";
724 $outcmd = "exec:cat
";
727 $outcmd .= " > $filename" if !$opts->{stdout};
729 my $cmd = ['/usr/bin/vma', 'create', '-v', '-c', $conffile];
730 push @$cmd, '-c', $firewall if -e $firewall;
731 push @$cmd, $outcmd, @pathlist;
733 $self->loginfo("starting template backup
");
734 $self->loginfo(join(' ', @$cmd));
736 if ($opts->{stdout}) {
737 $self->cmd($cmd, output => ">&" . fileno($opts->{stdout}));
745 my $devlist = _get_task_devlist($task);
747 $self->enforce_vm_running_for_backup($vmid);
748 $self->{qmeventd_fh} = PVE::QemuServer::register_qmeventd_handle($vmid);
754 $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub {
755 die "interrupted by signal
\n";
758 # Currently, failing to determine Proxmox support is not critical here, because it's only
759 # used for performance settings like 'max-workers'.
760 my $qemu_support = eval { mon_cmd($vmid, "query-proxmox-support
") };
763 $attach_tpmstate_drive->($self, $task, $vmid);
766 if ($opts->{stdout}) {
767 $outfh = $opts->{stdout};
769 $outfh = IO::File->new($filename, "w
") ||
770 die "unable to
open file
'$filename' - $!\n";
772 my $outfileno = fileno($outfh);
775 ($cpid, $outfileno) = $fork_compressor_pipe->($self, $comp, $outfileno);
778 my $qmpclient = PVE::QMPClient->new();
779 my $backup_cb = sub {
780 my ($vmid, $resp) = @_;
781 $backup_job_uuid = $resp->{return}->{UUID};
783 my $add_fd_cb = sub {
784 my ($vmid, $resp) = @_;
787 'backup-file' => "/dev/fdname
/backup
",
789 'config-file' => $conffile,
792 $params->{'firewall-file'} = $firewall if -e $firewall;
793 add_backup_performance_options($params, $opts->{performance}, $qemu_support);
795 $qmpclient->queue_cmd($vmid, $backup_cb, 'backup', %$params);
798 $qmpclient->queue_cmd($vmid, $add_fd_cb, 'getfd', fd => $outfileno, fdname => "backup
");
800 my $fs_frozen = $self->qga_fs_freeze($task, $vmid);
802 eval { $qmpclient->queue_execute(30) };
806 $self->qga_fs_thaw($vmid);
809 die $qmperr if $qmperr;
810 die $qmpclient->{errors}->{$vmid} if $qmpclient->{errors}->{$vmid};
813 POSIX::close($outfileno) == 0 ||
814 die "close output file handle failed
\n";
817 die "got
no uuid
for backup task
\n" if !defined($backup_job_uuid);
819 $self->loginfo("started backup task
'$backup_job_uuid'");
821 $self->resume_vm_after_job_start($task, $vmid);
823 $query_backup_status_loop->($self, $vmid, $backup_job_uuid);
828 $self->mon_backup_cancel($vmid);
829 $self->resume_vm_after_job_start($task, $vmid);
832 $self->restore_vm_power_state($vmid);
842 if ($cpid && (waitpid($cpid, 0) > 0)) {
845 my $signal = $stat & 127;
846 if ($ec || $signal) {
847 die "$comp failed
- wrong
exit status
$ec" .
848 ($signal ? " (signal
$signal)\n" : "\n");
853 sub _get_task_devlist {
857 foreach my $di (@{$task->{disks}}) {
858 if ($di->{type} eq 'block' || $di->{type} eq 'file') {
859 $devlist .= ',' if $devlist;
860 $devlist .= $di->{qmdevice};
862 die "implement me
(type
'$di->{type}')";
869 my ($self, $task, $vmid) = @_;
870 return if !$self->{vmlist}->{$vmid}->{agent} || $task->{mode} eq 'stop' || !$self->{vm_was_running} || $self->{vm_was_paused};
872 if (!PVE::QemuServer::qga_check_running($vmid, 1)) {
873 $self->loginfo("skipping guest-agent
'fs-freeze', agent configured but
not running?
");
877 my $freeze = PVE::QemuServer::get_qga_key($self->{vmlist}->{$vmid}, 'freeze-fs-on-backup') // 1;
879 $self->loginfo("skipping guest-agent
'fs-freeze', disabled
in VM options
");
883 $self->loginfo("issuing guest-agent
'fs-freeze' command
");
884 eval { mon_cmd($vmid, "guest-fsfreeze-freeze
") };
885 $self->logerr($@) if $@;
887 return 1; # even on mon command error, ensure we always thaw again
890 # only call if fs_freeze return 1
892 my ($self, $vmid) = @_;
894 $self->loginfo("issuing guest-agent
'fs-thaw' command
");
895 eval { mon_cmd($vmid, "guest-fsfreeze-thaw
") };
896 $self->logerr($@) if $@;
899 # we need a running QEMU/KVM process for backup, starts a paused (prelaunch)
900 # one if VM isn't already running
901 sub enforce_vm_running_for_backup {
902 my ($self, $vmid) = @_;
904 if (PVE::QemuServer::check_running($vmid)) {
905 $self->{vm_was_running} = 1;
910 $self->loginfo("starting kvm to execute backup task
");
911 # start with skiplock
917 PVE::QemuServer::vm_start($self->{storecfg}, $vmid, $params);
922 # resume VM again once in a clear state (stop mode backup of running VM)
923 sub resume_vm_after_job_start {
924 my ($self, $task, $vmid) = @_;
926 return if !$self->{vm_was_running} || $self->{vm_was_paused};
928 if (my $stoptime = $task->{vmstoptime}) {
929 my $delay = time() - $task->{vmstoptime};
930 $task->{vmstoptime} = undef; # avoid printing 'online after ..' twice
931 $self->loginfo("resuming VM again after
$delay seconds
");
933 $self->loginfo("resuming VM again
");
935 mon_cmd($vmid, 'cont', timeout => 45);
938 # stop again if VM was not running before
939 sub restore_vm_power_state {
940 my ($self, $vmid) = @_;
942 # we always let VMs keep running
943 return if $self->{vm_was_running};
946 my $resp = mon_cmd($vmid, 'query-status');
947 my $status = $resp && $resp->{status} ? $resp->{status} : 'unknown';
948 if ($status eq 'prelaunch') {
949 $self->loginfo("stopping kvm after backup task
");
950 PVE::QemuServer::vm_stop($self->{storecfg}, $vmid, 1);
952 $self->loginfo("kvm status changed after backup
('$status') - keep VM running
");
958 sub mon_backup_cancel {
959 my ($self, $vmid) = @_;
961 $self->loginfo("aborting backup job
");
962 eval { mon_cmd($vmid, 'backup-cancel') };
963 $self->logerr($@) if $@;
967 my ($self, $task, $vmid) = @_;
973 my ($self, $task, $vmid) = @_;
975 $detach_tpmstate_drive->($task, $vmid);
977 if ($self->{qmeventd_fh}) {
978 close($self->{qmeventd_fh});