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, 0)) {
71 # Do not treat a suspended VM as paused, as it would cause us to skip
72 # fs-freeze even if the VM wakes up before we reach qga_fs_freeze.
73 $self->{vm_was_paused
} = 1;
76 $task->{hostname
} = $conf->{name
};
78 my $hostname = PVE
::INotify
::nodename
();
82 my $backup_volumes = PVE
::QemuConfig-
>get_backup_volumes($conf);
84 foreach my $volume (@{$backup_volumes}) {
85 my $name = $volume->{key
};
86 my $volume_config = $volume->{volume_config
};
87 my $volid = $volume_config->{file
};
89 if (!$volume->{included
}) {
90 $self->loginfo("exclude disk '$name' '$volid' ($volume->{reason})");
92 } elsif ($self->{vm_was_running
} && $volume_config->{iothread
} &&
93 !PVE
::QemuServer
::Machine
::runs_at_least_qemu_version
($vmid, 4, 0, 1)) {
94 die "disk '$name' '$volid' (iothread=on) can't use backup feature with running QEMU " .
95 "version < 4.0.1! Either set backup=no for this drive or upgrade QEMU and restart VM\n";
97 my $log = "include disk '$name' '$volid'";
98 if (defined(my $size = $volume_config->{size
})) {
99 my $readable_size = PVE
::JSONSchema
::format_size
($size);
100 $log .= " $readable_size";
102 $self->loginfo($log);
105 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
106 push @$vollist, $volid if $storeid;
107 $drivehash->{$name} = $volume->{volume_config
};
110 PVE
::Storage
::activate_volumes
($self->{storecfg
}, $vollist);
112 foreach my $ds (sort keys %$drivehash) {
113 my $drive = $drivehash->{$ds};
115 my $volid = $drive->{file
};
116 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
120 $path = PVE
::Storage
::path
($self->{storecfg
}, $volid);
126 # The call in list context can be expensive for certain plugins like RBD, just get size
127 $size = eval { PVE
::Storage
::volume_size_info
($self->{storecfg
}, $volid, 5) };
128 die "cannot determine size of volume '$volid' - $@\n" if $@;
130 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $storeid);
131 $format = PVE
::QemuServer
::qemu_img_format
($scfg, $volname);
133 ($size, $format) = eval {
134 PVE
::Storage
::volume_size_info
($self->{storecfg
}, $volid, 5);
136 die "cannot determine size and format of volume '$volid' - $@\n" if $@;
145 qmdevice
=> "drive-$ds",
148 if ($ds eq 'tpmstate0') {
149 # TPM drive only exists for backup, which is reflected in the name
150 $diskinfo->{qmdevice
} = 'drive-tpmstate0-backup';
151 $task->{tpmpath
} = $path;
155 $diskinfo->{type
} = 'block';
157 $diskinfo->{type
} = 'file';
160 push @{$task->{disks
}}, $diskinfo;
165 my ($self, $vmid) = @_;
167 my $running = PVE
::QemuServer
::check_running
($vmid) ?
1 : 0;
169 return wantarray ?
($running, $running ?
'running' : 'stopped') : $running;
173 my ($self, $vmid) = @_;
175 PVE
::QemuConfig-
>set_lock($vmid, 'backup');
179 my ($self, $vmid) = @_;
181 PVE
::QemuConfig-
>remove_lock($vmid, 'backup');
185 my ($self, $task, $vmid) = @_;
187 my $opts = $self->{vzdump
}->{opts
};
189 my $wait = $opts->{stopwait
} * 60;
190 # send shutdown and wait
191 $self->cmd ("qm shutdown $vmid --skiplock --keepActive --timeout $wait");
195 my ($self, $task, $vmid) = @_;
197 $self->cmd ("qm start $vmid --skiplock");
201 my ($self, $task, $vmid) = @_;
203 return if $self->{vm_was_paused
};
205 $self->cmd ("qm suspend $vmid --skiplock");
209 my ($self, $task, $vmid) = @_;
211 return if $self->{vm_was_paused
};
213 $self->cmd ("qm resume $vmid --skiplock");
217 my ($self, $task, $vmid) = @_;
219 my $conffile = PVE
::QemuConfig-
>config_file($vmid);
221 my $outfile = "$task->{tmpdir}/qemu-server.conf";
222 my $firewall_src = "/etc/pve/firewall/$vmid.fw";
223 my $firewall_dest = "$task->{tmpdir}/qemu-server.fw";
225 my $outfd = IO
::File-
>new (">$outfile") ||
226 die "unable to open '$outfile'";
227 my $conffd = IO
::File-
>new ($conffile, 'r') ||
228 die "unable open '$conffile'";
233 while (defined (my $line = <$conffd>)) {
234 next if $line =~ m/^\#vzdump\#/; # just to be sure
235 next if $line =~ m/^\#qmdump\#/; # just to be sure
236 if ($line =~ m/^\[(.*)\]\s*$/) {
237 if ($1 =~ m/PENDING/i) {
239 } elsif ($1 =~ m/special:cloudinit/) {
240 $found_cloudinit = 1;
245 next if $found_snapshot || $found_pending || $found_cloudinit; # skip all snapshots,pending changes and cloudinit config data
247 if ($line =~ m/^unused\d+:\s*(\S+)\s*/) {
248 $self->loginfo("skip unused drive '$1' (not included into backup)");
251 next if $line =~ m/^lock:/ || $line =~ m/^parent:/;
256 foreach my $di (@{$task->{disks
}}) {
257 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
258 my $storeid = $di->{storeid
} || '';
259 my $format = $di->{format
} || '';
260 print $outfd "#qmdump#map:$di->{virtdev}:$di->{qmdevice}:$storeid:$format:\n";
262 die "internal error";
266 if ($found_snapshot) {
267 $self->loginfo("snapshots found (not included into backup)");
269 if ($found_pending) {
270 $self->loginfo("pending configuration changes found (not included into backup)");
273 PVE
::Tools
::file_copy
($firewall_src, $firewall_dest) if -f
$firewall_src;
277 my ($self, $task, $vmid, $filename, $comp) = @_;
279 my $opts = $self->{vzdump
}->{opts
};
280 my $scfg = $opts->{scfg
};
282 if ($self->{vzdump
}->{opts
}->{pbs
}) {
283 $self->archive_pbs($task, $vmid);
285 $self->archive_vma($task, $vmid, $filename, $comp);
289 my $bitmap_action_to_human = sub {
290 my ($self, $info) = @_;
292 my $action = $info->{action
};
294 if ($action eq "not-used") {
295 return "disabled (no support)";
296 } elsif ($action eq "not-used-removed") {
297 return "disabled (old bitmap cleared)";
298 } elsif ($action eq "new") {
299 return "created new";
300 } elsif ($action eq "used") {
301 if ($info->{dirty
} == 0) {
302 return "OK (drive clean)";
304 my $size = render_bytes
($info->{size
}, 1);
305 my $dirty = render_bytes
($info->{dirty
}, 1);
306 return "OK ($dirty of $size dirty)";
308 } elsif ($action eq "invalid") {
309 return "existing bitmap was invalid and has been cleared";
315 my $query_backup_status_loop = sub {
316 my ($self, $vmid, $job_uuid, $qemu_support) = @_;
318 my $starttime = time ();
319 my $last_time = $starttime;
320 my ($last_percent, $last_total, $last_target, $last_zero, $last_transferred) = (-1, 0, 0, 0, 0);
321 my ($transferred, $reused);
324 my ($mb, $delta) = @_;
325 return "0 B/s" if $mb <= 0;
326 my $bw = int(($mb / $delta));
327 return render_bytes
($bw, 1) . "/s";
332 my $has_query_bitmap = $qemu_support && $qemu_support->{'query-bitmap-info'};
333 my $is_template = PVE
::QemuConfig-
>is_template($self->{vmlist
}->{$vmid});
334 if ($has_query_bitmap) {
336 my $bitmap_info = mon_cmd
($vmid, 'query-pbs-bitmap-info');
337 for my $info (sort { $a->{drive
} cmp $b->{drive
} } @$bitmap_info) {
339 my $text = $bitmap_action_to_human->($self, $info);
340 my $drive = $info->{drive
};
341 $drive =~ s/^drive-//; # for consistency
342 $self->loginfo("$drive: dirty-bitmap status: $text");
344 $target += $info->{dirty
};
345 $total += $info->{size
};
346 $last_reused += $info->{size
} - $info->{dirty
};
348 if ($target < $total) {
349 my $total_h = render_bytes
($total, 1);
350 my $target_h = render_bytes
($target, 1);
351 $self->loginfo("using fast incremental mode (dirty-bitmap), $target_h dirty of $total_h total");
355 my $last_finishing = 0;
357 my $status = mon_cmd
($vmid, 'query-backup');
359 my $total = $status->{total
} || 0;
360 my $dirty = $status->{dirty
};
361 $target = (defined($dirty) && $dirty < $total) ?
$dirty : $total if !$has_query_bitmap;
362 $transferred = $status->{transferred
} || 0;
363 $reused = $status->{reused
};
364 my $percent = $target ?
int(($transferred * 100)/$target) : 100;
365 my $zero = $status->{'zero-bytes'} || 0;
367 die "got unexpected uuid\n" if !$status->{uuid
} || ($status->{uuid
} ne $job_uuid);
370 my $duration = $ctime - $starttime;
372 my $rbytes = $transferred - $last_transferred;
375 # reused includes zero bytes for PBS
376 $wbytes = $rbytes - ($reused - $last_reused);
378 $wbytes = $rbytes - ($zero - $last_zero);
381 my $timediff = ($ctime - $last_time) || 1; # fixme
382 my $mbps_read = $get_mbps->($rbytes, $timediff);
383 my $mbps_write = $get_mbps->($wbytes, $timediff);
384 my $target_h = render_bytes
($target, 1);
385 my $transferred_h = render_bytes
($transferred, 1);
387 my $statusline = sprintf("%3d%% ($transferred_h of $target_h) in %s"
388 .", read: $mbps_read, write: $mbps_write", $percent, render_duration
($duration));
390 my $res = $status->{status
} || 'unknown';
391 if ($res ne 'active') {
392 if ($last_percent < 100) {
393 $self->loginfo($statusline);
395 if ($res ne 'done') {
396 die (($status->{errmsg
} || "unknown error") . "\n") if $res eq 'error';
397 die "got unexpected status '$res'\n";
399 $last_target = $target if $target;
400 $last_total = $total if $total;
401 $last_zero = $zero if $zero;
402 $last_transferred = $transferred if $transferred;
405 if ($percent != $last_percent && ($timediff > 2)) {
406 $self->loginfo($statusline);
407 $last_percent = $percent;
408 $last_target = $target if $target;
409 $last_total = $total if $total;
410 $last_zero = $zero if $zero;
411 $last_transferred = $transferred if $transferred;
413 $last_reused = $reused;
415 if (!$last_finishing && $status->{finishing
}) {
416 $self->loginfo("Waiting for server to finish backup validation...");
418 $last_finishing = $status->{finishing
};
423 my $duration = time() - $starttime;
426 my $zero_per = $last_target ?
int(($last_zero * 100)/$last_target) : 0;
427 my $zero_h = render_bytes
($last_zero);
428 $self->loginfo("backup is sparse: $zero_h (${zero_per}%) total zero data");
431 my $reused_h = render_bytes
($reused);
432 my $reuse_per = int($reused * 100 / $last_total);
433 $self->loginfo("backup was done incrementally, reused $reused_h (${reuse_per}%)");
436 my $transferred_h = render_bytes
($transferred);
438 my $mbps = $get_mbps->($transferred, $duration);
439 $self->loginfo("transferred $transferred_h in $duration seconds ($mbps)");
441 $self->loginfo("transferred $transferred_h in <1 seconds");
446 total
=> $last_total,
451 my $attach_tpmstate_drive = sub {
452 my ($self, $task, $vmid) = @_;
454 return if !$task->{tpmpath
};
456 # unconditionally try to remove the tpmstate-named drive - it only exists
457 # for backing up, and avoids errors if left over from some previous event
458 eval { PVE
::QemuServer
::qemu_drivedel
($vmid, "tpmstate0-backup"); };
460 $self->loginfo('attaching TPM drive to QEMU for backup');
462 my $drive = "file=$task->{tpmpath},if=none,read-only=on,id=drive-tpmstate0-backup";
463 $drive =~ s/\\/\\\\/g;
464 my $ret = PVE
::QemuServer
::Monitor
::hmp_cmd
($vmid, "drive_add auto \"$drive\"");
465 die "attaching TPM drive failed
- $ret\n" if $ret !~ m/OK/s;
468 my $detach_tpmstate_drive = sub {
469 my ($task, $vmid) = @_;
470 return if !$task->{tpmpath} || !PVE::QemuServer::check_running($vmid);
471 eval { PVE::QemuServer::qemu_drivedel($vmid, "tpmstate0-backup
"); };
474 my sub add_backup_performance_options {
475 my ($qmp_param, $perf, $qemu_support) = @_;
477 return if !$perf || scalar(keys $perf->%*) == 0;
479 if (!$qemu_support) {
480 my $settings_string = join(', ', sort keys $perf->%*);
481 log_warn("ignoring setting
(s
): $settings_string - issue checking
if supported
");
485 if (defined($perf->{'max-workers'})) {
486 if ($qemu_support->{'backup-max-workers'}) {
487 $qmp_param->{'max-workers'} = int($perf->{'max-workers'});
489 log_warn("ignoring
'max-workers' setting
- not supported by running QEMU
");
494 sub get_and_check_pbs_encryption_config {
497 my $opts = $self->{vzdump}->{opts};
498 my $scfg = $opts->{scfg};
500 my $keyfile = PVE::Storage::PBSPlugin::pbs_encryption_key_file_name($scfg, $opts->{storage});
501 my $master_keyfile = PVE::Storage::PBSPlugin::pbs_master_pubkey_file_name($scfg, $opts->{storage});
504 if (-e $master_keyfile) {
505 $self->loginfo("enabling encryption with master key feature
");
506 return ($keyfile, $master_keyfile);
507 } elsif ($scfg->{'master-pubkey'}) {
508 die "master public key configured but
no key file found
\n";
510 $self->loginfo("enabling encryption
");
511 return ($keyfile, undef);
514 my $encryption_fp = $scfg->{'encryption-key'};
515 die "encryption configured
('$encryption_fp') but
no encryption key file found
!\n"
517 if (-e $master_keyfile) {
520 "backup target storage
is configured with master-key
, but
no encryption key set
!"
521 ." Ignoring master key settings
and creating unencrypted backup
."
524 return (undef, undef);
526 die "internal error
- unhandled case
for getting
& checking PBS encryption
($keyfile, $master_keyfile)!";
530 my ($self, $task, $vmid) = @_;
532 my $conffile = "$task->{tmpdir
}/qemu-server
.conf
";
533 my $firewall = "$task->{tmpdir
}/qemu-server
.fw
";
535 my $opts = $self->{vzdump}->{opts};
536 my $scfg = $opts->{scfg};
538 my $starttime = time();
540 my $fingerprint = $scfg->{fingerprint};
541 my $repo = PVE::PBSClient::get_repository($scfg);
542 my $password = PVE::Storage::PBSPlugin::pbs_get_password($scfg, $opts->{storage});
543 my ($keyfile, $master_keyfile) = $self->get_and_check_pbs_encryption_config();
545 my $diskcount = scalar(@{$task->{disks}});
546 # proxmox-backup-client can only handle raw files and block devs, so only use it (directly) for
549 $self->loginfo("backup contains
no disks
");
551 local $ENV{PBS_PASSWORD} = $password;
552 local $ENV{PBS_FINGERPRINT} = $fingerprint if defined($fingerprint);
554 '/usr/bin/proxmox-backup-client',
556 '--repository', $repo,
557 '--backup-type', 'vm',
558 '--backup-id', "$vmid",
559 '--backup-time', $task->{backup_time},
561 if (defined(my $ns = $scfg->{namespace})) {
562 push @$cmd, '--ns', $ns;
564 if (defined($keyfile)) {
565 push @$cmd, '--keyfile', $keyfile;
566 push @$cmd, '--master-pubkey-file', $master_keyfile if defined($master_keyfile);
569 push @$cmd, "qemu-server
.conf
:$conffile";
570 push @$cmd, "fw
.conf
:$firewall" if -e $firewall;
572 $self->loginfo("starting template backup
");
573 $self->loginfo(join(' ', @$cmd));
580 # get list early so we die on unkown drive types before doing anything
581 my $devlist = _get_task_devlist($task);
583 $self->enforce_vm_running_for_backup($vmid);
584 $self->{qmeventd_fh} = PVE::QemuServer::register_qmeventd_handle($vmid);
588 $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub {
589 die "interrupted by signal
\n";
592 my $qemu_support = eval { mon_cmd($vmid, "query-proxmox-support
") };
594 if (!$qemu_support || $err) {
595 die "query-proxmox-support returned empty value
\n" if !$err;
596 if ($err =~ m/The command query-proxmox-support has not been found/) {
597 die "PBS backups are
not supported by the running QEMU version
. Please make
"
598 . "sure you
've installed the latest version and the VM has been restarted.\n";
600 die "QMP command query-proxmox-support failed - $err\n";
604 # pve-qemu supports it since 5.2.0-1 (PVE 6.4), so safe to die since PVE 8
605 die "master key configured but running QEMU version does not support master keys\n"
606 if !defined($qemu_support->{'pbs-masterkey
'}) && defined($master_keyfile);
608 $attach_tpmstate_drive->($self, $task, $vmid);
610 my $fs_frozen = $self->qga_fs_freeze($task, $vmid);
614 'backup-file
' => $repo,
615 'backup-id
' => "$vmid",
616 'backup-time
' => $task->{backup_time},
617 password => $password,
619 'config-file
' => $conffile,
621 if (defined(my $ns = $scfg->{namespace})) {
622 $params->{'backup-ns
'} = $ns;
625 $params->{speed} = $opts->{bwlimit}*1024 if $opts->{bwlimit};
626 add_backup_performance_options($params, $opts->{performance}, $qemu_support);
628 $params->{fingerprint} = $fingerprint if defined($fingerprint);
629 $params->{'firewall-file
'} = $firewall if -e $firewall;
631 $params->{encrypt} = defined($keyfile) ? JSON::true : JSON::false;
632 if (defined($keyfile)) {
633 $params->{keyfile} = $keyfile;
634 $params->{"master-keyfile"} = $master_keyfile if defined($master_keyfile);
637 my $is_template = PVE::QemuConfig->is_template($self->{vmlist}->{$vmid});
638 $params->{'use-dirty-bitmap
'} = JSON::true
639 if $qemu_support->{'pbs-dirty-bitmap
'} && !$is_template;
641 $params->{timeout} = 125; # give some time to connect to the backup server
643 my $res = eval { mon_cmd($vmid, "backup", %$params) };
645 $backup_job_uuid = $res->{UUID} if $res;
648 $self->qga_fs_thaw($vmid);
651 die $qmperr if $qmperr;
652 die "got no uuid for backup task\n" if !defined($backup_job_uuid);
654 $self->loginfo("started backup task '$backup_job_uuid'");
656 $self->resume_vm_after_job_start($task, $vmid);
658 my $stat = $query_backup_status_loop->($self, $vmid, $backup_job_uuid, $qemu_support);
659 $task->{size} = $stat->{total};
664 $self->mon_backup_cancel($vmid);
665 $self->resume_vm_after_job_start($task, $vmid);
667 $self->restore_vm_power_state($vmid);
672 my $fork_compressor_pipe = sub {
673 my ($self, $comp, $outfileno) = @_;
675 my @pipefd = POSIX::pipe();
677 die "unable to fork worker - $!" if !defined($cpid) || $cpid < 0;
680 POSIX::close($pipefd[1]);
682 my $fd = fileno(STDIN);
684 POSIX::close(0) if $fd != 0;
685 die "unable to redirect STDIN - $!"
686 if !open(STDIN, "<&", $pipefd[0]);
689 $fd = fileno(STDOUT);
691 POSIX::close (1) if $fd != 1;
693 die "unable to redirect STDOUT - $!"
694 if !open(STDOUT, ">&", $outfileno);
697 die "fork compressor '$comp' failed\n";
706 POSIX::close($pipefd[0]);
707 $outfileno = $pipefd[1];
710 return ($cpid, $outfileno);
714 my ($self, $task, $vmid, $filename, $comp) = @_;
716 my $conffile = "$task->{tmpdir}/qemu-server.conf";
717 my $firewall = "$task->{tmpdir}/qemu-server.fw";
719 my $opts = $self->{vzdump}->{opts};
721 my $starttime = time();
724 if ($opts->{bwlimit}) {
725 $speed = $opts->{bwlimit}*1024;
728 my $diskcount = scalar(@{$task->{disks}});
729 if (PVE::QemuConfig->is_template($self->{vmlist}->{$vmid}) || !$diskcount) {
731 foreach my $di (@{$task->{disks}}) {
732 if ($di->{type} eq 'block
' || $di->{type} eq 'file
') {
733 push @pathlist, "$di->{qmdevice}=$di->{path}";
740 $self->loginfo("backup contains no disks");
745 $outcmd = "exec:$comp";
747 $outcmd = "exec:cat";
750 $outcmd .= " > $filename" if !$opts->{stdout};
752 my $cmd = ['/usr/bin
/vma
', 'create
', '-v
', '-c
', $conffile];
753 push @$cmd, '-c
', $firewall if -e $firewall;
754 push @$cmd, $outcmd, @pathlist;
756 $self->loginfo("starting template backup");
757 $self->loginfo(join(' ', @$cmd));
759 if ($opts->{stdout}) {
760 $self->cmd($cmd, output => ">&" . fileno($opts->{stdout}));
768 my $devlist = _get_task_devlist($task);
770 $self->enforce_vm_running_for_backup($vmid);
771 $self->{qmeventd_fh} = PVE::QemuServer::register_qmeventd_handle($vmid);
777 $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub {
778 die "interrupted by signal\n";
781 # Currently, failing to determine Proxmox support is not critical here, because it's only
782 # used for performance settings like 'max-workers'.
783 my $qemu_support = eval { mon_cmd
($vmid, "query-proxmox-support") };
786 $attach_tpmstate_drive->($self, $task, $vmid);
789 if ($opts->{stdout
}) {
790 $outfh = $opts->{stdout
};
792 $outfh = IO
::File-
>new($filename, "w") ||
793 die "unable to open file '$filename' - $!\n";
795 my $outfileno = fileno($outfh);
798 ($cpid, $outfileno) = $fork_compressor_pipe->($self, $comp, $outfileno);
801 my $qmpclient = PVE
::QMPClient-
>new();
802 my $backup_cb = sub {
803 my ($vmid, $resp) = @_;
804 $backup_job_uuid = $resp->{return}->{UUID
};
806 my $add_fd_cb = sub {
807 my ($vmid, $resp) = @_;
810 'backup-file' => "/dev/fdname/backup",
812 'config-file' => $conffile,
815 $params->{'firewall-file'} = $firewall if -e
$firewall;
816 add_backup_performance_options
($params, $opts->{performance
}, $qemu_support);
818 $qmpclient->queue_cmd($vmid, $backup_cb, 'backup', %$params);
821 $qmpclient->queue_cmd($vmid, $add_fd_cb, 'getfd', fd
=> $outfileno, fdname
=> "backup");
823 my $fs_frozen = $self->qga_fs_freeze($task, $vmid);
825 eval { $qmpclient->queue_execute(30) };
829 $self->qga_fs_thaw($vmid);
832 die $qmperr if $qmperr;
833 die $qmpclient->{errors
}->{$vmid} if $qmpclient->{errors
}->{$vmid};
836 POSIX
::close($outfileno) == 0 ||
837 die "close output file handle failed\n";
840 die "got no uuid for backup task\n" if !defined($backup_job_uuid);
842 $self->loginfo("started backup task '$backup_job_uuid'");
844 $self->resume_vm_after_job_start($task, $vmid);
846 $query_backup_status_loop->($self, $vmid, $backup_job_uuid);
851 $self->mon_backup_cancel($vmid);
852 $self->resume_vm_after_job_start($task, $vmid);
855 $self->restore_vm_power_state($vmid);
865 if ($cpid && (waitpid($cpid, 0) > 0)) {
868 my $signal = $stat & 127;
869 if ($ec || $signal) {
870 die "$comp failed - wrong exit status $ec" .
871 ($signal ?
" (signal $signal)\n" : "\n");
876 sub _get_task_devlist
{
880 foreach my $di (@{$task->{disks
}}) {
881 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
882 $devlist .= ',' if $devlist;
883 $devlist .= $di->{qmdevice
};
885 die "implement me (type '$di->{type}')";
892 my ($self, $task, $vmid) = @_;
893 return if !$self->{vmlist
}->{$vmid}->{agent
} || $task->{mode
} eq 'stop' || !$self->{vm_was_running
} || $self->{vm_was_paused
};
895 if (!PVE
::QemuServer
::qga_check_running
($vmid, 1)) {
896 $self->loginfo("skipping guest-agent 'fs-freeze', agent configured but not running?");
900 my $freeze = PVE
::QemuServer
::get_qga_key
($self->{vmlist
}->{$vmid}, 'freeze-fs-on-backup') // 1;
902 $self->loginfo("skipping guest-agent 'fs-freeze', disabled in VM options");
906 $self->loginfo("issuing guest-agent 'fs-freeze' command");
907 eval { mon_cmd
($vmid, "guest-fsfreeze-freeze") };
908 $self->logerr($@) if $@;
910 return 1; # even on mon command error, ensure we always thaw again
913 # only call if fs_freeze return 1
915 my ($self, $vmid) = @_;
917 $self->loginfo("issuing guest-agent 'fs-thaw' command");
918 eval { mon_cmd
($vmid, "guest-fsfreeze-thaw") };
919 $self->logerr($@) if $@;
922 # we need a running QEMU/KVM process for backup, starts a paused (prelaunch)
923 # one if VM isn't already running
924 sub enforce_vm_running_for_backup
{
925 my ($self, $vmid) = @_;
927 if (PVE
::QemuServer
::check_running
($vmid)) {
928 $self->{vm_was_running
} = 1;
933 $self->loginfo("starting kvm to execute backup task");
934 # start with skiplock
940 PVE
::QemuServer
::vm_start
($self->{storecfg
}, $vmid, $params);
945 # resume VM again once in a clear state (stop mode backup of running VM)
946 sub resume_vm_after_job_start
{
947 my ($self, $task, $vmid) = @_;
949 return if !$self->{vm_was_running
} || $self->{vm_was_paused
};
951 if (my $stoptime = $task->{vmstoptime
}) {
952 my $delay = time() - $task->{vmstoptime
};
953 $task->{vmstoptime
} = undef; # avoid printing 'online after ..' twice
954 $self->loginfo("resuming VM again after $delay seconds");
956 $self->loginfo("resuming VM again");
958 mon_cmd
($vmid, 'cont', timeout
=> 45);
961 # stop again if VM was not running before
962 sub restore_vm_power_state
{
963 my ($self, $vmid) = @_;
965 # we always let VMs keep running
966 return if $self->{vm_was_running
};
969 my $resp = mon_cmd
($vmid, 'query-status');
970 my $status = $resp && $resp->{status
} ?
$resp->{status
} : 'unknown';
971 if ($status eq 'prelaunch') {
972 $self->loginfo("stopping kvm after backup task");
973 PVE
::QemuServer
::vm_stop
($self->{storecfg
}, $vmid, 1);
975 $self->loginfo("kvm status changed after backup ('$status') - keep VM running");
981 sub mon_backup_cancel
{
982 my ($self, $vmid) = @_;
984 $self->loginfo("aborting backup job");
985 eval { mon_cmd
($vmid, 'backup-cancel') };
986 $self->logerr($@) if $@;
990 my ($self, $task, $vmid) = @_;
996 my ($self, $task, $vmid) = @_;
998 $detach_tpmstate_drive->($task, $vmid);
1000 if ($self->{qmeventd_fh
}) {
1001 close($self->{qmeventd_fh
});