1 package PVE
::VZDump
::QemuServer
;
11 use PVE
::Cluster
qw(cfs_read_file);
16 use PVE
::Storage
::Plugin
;
17 use PVE
::Storage
::PBSPlugin
;
23 use PVE
::QemuServer
::Machine
;
24 use PVE
::QemuServer
::Monitor
qw(mon_cmd);
26 use base qw
(PVE
::VZDump
::Plugin
);
29 my ($class, $vzdump) = @_;
31 PVE
::VZDump
::check_bin
('qm');
33 my $self = bless { vzdump
=> $vzdump }, $class;
35 $self->{vmlist
} = PVE
::QemuServer
::vzlist
();
36 $self->{storecfg
} = PVE
::Storage
::config
();
47 return [ keys %{$self->{vmlist
}} ];
51 my ($self, $task, $vmid, $mode) = @_;
55 my $conf = $self->{vmlist
}->{$vmid} = PVE
::QemuConfig-
>load_config($vmid);
57 $self->loginfo("VM Name: $conf->{name}")
58 if defined($conf->{name
});
60 $self->{vm_was_running
} = 1;
61 if (!PVE
::QemuServer
::check_running
($vmid)) {
62 $self->{vm_was_running
} = 0;
65 $task->{hostname
} = $conf->{name
};
67 my $hostname = PVE
::INotify
::nodename
();
71 PVE
::QemuServer
::foreach_drive
($conf, sub {
72 my ($ds, $drive) = @_;
74 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
76 my $volid = $drive->{file
};
78 if (defined($drive->{backup
}) && !$drive->{backup
}) {
79 $self->loginfo("exclude disk '$ds' '$volid' (backup=no)");
81 } elsif ($self->{vm_was_running
} && $drive->{iothread
}) {
82 if (!PVE
::QemuServer
::Machine
::runs_at_least_qemu_version
($vmid, 4, 0, 1)) {
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";
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')");
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";
98 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
99 push @$vollist, $volid if $storeid;
100 $drivehash->{$ds} = $drive;
103 PVE
::Storage
::activate_volumes
($self->{storecfg
}, $vollist);
105 foreach my $ds (sort keys %$drivehash) {
106 my $drive = $drivehash->{$ds};
108 my $volid = $drive->{file
};
109 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
113 $path = PVE
::Storage
::path
($self->{storecfg
}, $volid);
117 my ($size, $format) = eval { PVE
::Storage
::volume_size_info
($self->{storecfg
}, $volid, 5) };
118 die "no such volume '$volid'\n" if $@;
126 qmdevice
=> "drive-$ds",
130 $diskinfo->{type
} = 'block';
132 $diskinfo->{type
} = 'file';
135 push @{$task->{disks
}}, $diskinfo;
140 my ($self, $vmid) = @_;
142 my $running = PVE
::QemuServer
::check_running
($vmid) ?
1 : 0;
144 return wantarray ?
($running, $running ?
'running' : 'stopped') : $running;
148 my ($self, $vmid) = @_;
150 PVE
::QemuConfig-
>set_lock($vmid, 'backup');
154 my ($self, $vmid) = @_;
156 PVE
::QemuConfig-
>remove_lock($vmid, 'backup');
160 my ($self, $task, $vmid) = @_;
162 my $opts = $self->{vzdump
}->{opts
};
164 my $wait = $opts->{stopwait
} * 60;
165 # send shutdown and wait
166 $self->cmd ("qm shutdown $vmid --skiplock --keepActive --timeout $wait");
170 my ($self, $task, $vmid) = @_;
172 $self->cmd ("qm start $vmid --skiplock");
176 my ($self, $task, $vmid) = @_;
178 $self->cmd ("qm suspend $vmid --skiplock");
182 my ($self, $task, $vmid) = @_;
184 $self->cmd ("qm resume $vmid --skiplock");
188 my ($self, $task, $vmid) = @_;
190 my $conffile = PVE
::QemuConfig-
>config_file($vmid);
192 my $outfile = "$task->{tmpdir}/qemu-server.conf";
193 my $firewall_src = "/etc/pve/firewall/$vmid.fw";
194 my $firewall_dest = "$task->{tmpdir}/qemu-server.fw";
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'";
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
206 if ($line =~ m/^\[(.*)\]\s*$/) {
207 if ($1 =~ m/PENDING/i) {
212 next; # skip all snapshots and pending changes config data
215 if ($line =~ m/^unused\d+:\s*(\S+)\s*/) {
216 $self->loginfo("skip unused drive '$1' (not included into backup)");
219 next if $line =~ m/^lock:/ || $line =~ m/^parent:/;
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";
230 die "internal error";
234 if ($found_snapshot) {
235 $self->loginfo("snapshots found (not included into backup)");
237 if ($found_pending) {
238 $self->loginfo("pending configuration changes found (not included into backup)");
241 PVE
::Tools
::file_copy
($firewall_src, $firewall_dest) if -f
$firewall_src;
245 my ($self, $task, $vmid, $filename, $comp) = @_;
247 my $opts = $self->{vzdump
}->{opts
};
248 my $scfg = $opts->{scfg
};
250 if ($scfg->{type
} eq 'pbs') {
251 $self->archive_pbs($task, $vmid);
253 $self->archive_vma($task, $vmid, $filename, $comp);
257 my $query_backup_status_loop = sub {
258 my ($self, $vmid, $job_uuid) = @_;
260 my $starttime = time ();
261 my $last_time = $starttime;
262 my ($last_percent, $last_total, $last_zero, $last_transferred) = (-1, 0, 0, 0);
266 my ($mb, $delta) = @_;
267 return ($mb > 0) ?
int(($mb / $delta) / (1000 * 1000)) : 0;
271 my $status = mon_cmd
($vmid, 'query-backup');
273 my $total = $status->{total
} || 0;
274 $transferred = $status->{transferred
} || 0;
275 my $percent = $total ?
int(($transferred * 100)/$total) : 0;
276 my $zero = $status->{'zero-bytes'} || 0;
277 my $zero_per = $total ?
int(($zero * 100)/$total) : 0;
279 die "got unexpected uuid\n" if !$status->{uuid
} || ($status->{uuid
} ne $job_uuid);
282 my $duration = $ctime - $starttime;
284 my $rbytes = $transferred - $last_transferred;
285 my $wbytes = $rbytes - ($zero - $last_zero);
287 my $timediff = ($ctime - $last_time) || 1; # fixme
288 my $mbps_read = $get_mbps->($rbytes, $timediff);
289 my $mbps_write = $get_mbps->($wbytes, $timediff);
291 my $statusline = "status: $percent% ($transferred/$total), sparse ${zero_per}% ($zero), duration $duration, read/write $mbps_read/$mbps_write MB/s";
293 my $res = $status->{status
} || 'unknown';
294 if ($res ne 'active') {
295 $self->loginfo($statusline);
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";
304 if ($percent != $last_percent && ($timediff > 2)) {
305 $self->loginfo($statusline);
306 $last_percent = $percent;
307 $last_total = $total if $total;
308 $last_zero = $zero if $zero;
309 $last_transferred = $transferred if $transferred;
315 my $duration = time() - $starttime;
316 if ($transferred && $duration) {
317 my $mb = int($transferred / (1000 * 1000));
318 my $mbps = $get_mbps->($transferred, $duration);
319 $self->loginfo("transferred $mb MB in $duration seconds ($mbps MB/s)");
324 my ($self, $task, $vmid) = @_;
326 my $conffile = "$task->{tmpdir}/qemu-server.conf";
327 my $firewall = "$task->{tmpdir}/qemu-server.fw";
329 my $opts = $self->{vzdump
}->{opts
};
331 my $scfg = $opts->{scfg
};
333 my $starttime = time();
335 my $diskcount = scalar(@{$task->{disks
}});
337 my $server = $scfg->{server
};
338 my $datastore = $scfg->{datastore
};
339 my $username = $scfg->{username
} // 'root@pam';
340 my $fingerprint = $scfg->{fingerprint
};
342 my $repo = "$username\@$server:$datastore";
343 my $password = PVE
::Storage
::PBSPlugin
::pbs_get_password
($scfg, $opts->{storage
});
345 if (PVE
::QemuConfig-
>is_template($self->{vmlist
}->{$vmid}) || !$diskcount) {
347 foreach my $di (@{$task->{disks
}}) {
348 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
349 push @pathlist, "$di->{qmdevice}.img:$di->{path}";
356 $self->loginfo("backup contains no disks");
359 local $ENV{PBS_PASSWORD
} = $password;
361 '/usr/bin/proxmox-backup-client',
363 '--repository', $repo,
364 '--backup-type', 'vm',
365 '--backup-id', "$vmid",
366 '--backup-time', $task->{backup_time
},
369 push @$cmd, '--fingerprint', $fingerprint if defined($fingerprint);
371 push @$cmd, "qemu-server.conf:$conffile";
372 push @$cmd, "fw.conf:$firewall" if -e
$firewall;
373 push @$cmd, @pathlist if scalar(@pathlist);
375 $self->loginfo("starting template backup");
376 $self->loginfo(join(' ', @$cmd));
383 # get list early so we die on unkown drive types before doing anything
384 my $devlist = _get_task_devlist
($task);
386 my $stop_after_backup;
387 my $resume_on_backup;
390 my $vm_is_running = PVE
::QemuServer
::check_running
($vmid);
391 if (!$vm_is_running) {
393 $self->loginfo("starting kvm to execute backup task");
394 PVE
::QemuServer
::vm_start
($self->{storecfg
}, $vmid, undef,
395 $skiplock, undef, 1);
396 if ($self->{vm_was_running
}) {
397 $resume_on_backup = 1;
399 $stop_after_backup = 1;
409 my $interrupt_msg = "interrupted by signal\n";
411 $SIG{INT
} = $SIG{TERM
} = $SIG{QUIT
} = $SIG{HUP
} = $SIG{PIPE
} = sub {
415 my $agent_running = 0;
417 if ($self->{vmlist
}->{$vmid}->{agent
} && $vm_is_running) {
418 $agent_running = PVE
::QemuServer
::qga_check_running
($vmid);
422 eval { mon_cmd
($vmid, "guest-fsfreeze-freeze"); };
432 'backup-file' => $repo,
433 'backup-id' => "$vmid",
434 'backup-time' => $task->{backup_time
},
435 password
=> $password,
437 'config-file' => $conffile,
439 $params->{fingerprint
} = $fingerprint if defined($fingerprint);
440 $params->{'firewall-file'} = $firewall if -e
$firewall;
441 my $res = mon_cmd
($vmid, "backup", %$params);
442 $backup_job_uuid = $res->{UUID
};
448 eval { mon_cmd
($vmid, "guest-fsfreeze-thaw"); };
454 die $qmperr if $qmperr;
456 die "got no uuid for backup task\n" if !defined($backup_job_uuid);
458 $self->loginfo("started backup task '$backup_job_uuid'");
460 if ($resume_on_backup) {
461 if (my $stoptime = $task->{vmstoptime
}) {
462 my $delay = time() - $task->{vmstoptime
};
463 $task->{vmstoptime
} = undef; # avoid printing 'online after ..' twice
464 $self->loginfo("resuming VM again after $delay seconds");
466 $self->loginfo("resuming VM again");
468 mon_cmd
($vmid, 'cont');
471 $query_backup_status_loop->($self, $vmid, $backup_job_uuid);
477 if (defined($backup_job_uuid)) {
478 $self->loginfo("aborting backup job");
479 eval { mon_cmd
($vmid, 'backup-cancel'); };
481 $self->logerr($err1);
486 if ($stop_after_backup) {
487 # stop if not running
489 my $resp = mon_cmd
($vmid, 'query-status');
490 my $status = $resp && $resp->{status
} ?
$resp->{status
} : 'unknown';
491 if ($status eq 'prelaunch') {
492 $self->loginfo("stopping kvm after backup task");
493 PVE
::QemuServer
::vm_stop
($self->{storecfg
}, $vmid, $skiplock);
495 $self->loginfo("kvm status changed after backup ('$status')" .
496 " - keep VM running");
505 my ($self, $task, $vmid, $filename, $comp) = @_;
507 my $conffile = "$task->{tmpdir}/qemu-server.conf";
508 my $firewall = "$task->{tmpdir}/qemu-server.fw";
510 my $opts = $self->{vzdump
}->{opts
};
512 my $starttime = time();
515 if ($opts->{bwlimit
}) {
516 $speed = $opts->{bwlimit
}*1024;
519 my $diskcount = scalar(@{$task->{disks
}});
521 if (PVE
::QemuConfig-
>is_template($self->{vmlist
}->{$vmid}) || !$diskcount) {
523 foreach my $di (@{$task->{disks
}}) {
524 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
525 push @pathlist, "$di->{qmdevice}=$di->{path}";
532 $self->loginfo("backup contains no disks");
537 $outcmd = "exec:$comp";
539 $outcmd = "exec:cat";
542 $outcmd .= " > $filename" if !$opts->{stdout
};
544 my $cmd = ['/usr/bin/vma', 'create', '-v', '-c', $conffile];
545 push @$cmd, '-c', $firewall if -e
$firewall;
546 push @$cmd, $outcmd, @pathlist;
548 $self->loginfo("starting template backup");
549 $self->loginfo(join(' ', @$cmd));
551 if ($opts->{stdout
}) {
552 $self->cmd($cmd, output
=> ">&=" . fileno($opts->{stdout
}));
560 my $devlist = _get_task_devlist
($task);
562 my $stop_after_backup;
563 my $resume_on_backup;
566 my $vm_is_running = PVE
::QemuServer
::check_running
($vmid);
567 if (!$vm_is_running) {
569 $self->loginfo("starting kvm to execute backup task");
570 PVE
::QemuServer
::vm_start
($self->{storecfg
}, $vmid, undef,
571 $skiplock, undef, 1);
572 if ($self->{vm_was_running
}) {
573 $resume_on_backup = 1;
575 $stop_after_backup = 1;
586 my $interrupt_msg = "interrupted by signal\n";
588 $SIG{INT
} = $SIG{TERM
} = $SIG{QUIT
} = $SIG{HUP
} = $SIG{PIPE
} = sub {
592 my $qmpclient = PVE
::QMPClient-
>new();
594 my $backup_cb = sub {
595 my ($vmid, $resp) = @_;
596 $backup_job_uuid = $resp->{return}->{UUID
};
600 if ($opts->{stdout
}) {
601 $outfh = $opts->{stdout
};
603 $outfh = IO
::File-
>new($filename, "w") ||
604 die "unable to open file '$filename' - $!\n";
609 my @pipefd = POSIX
::pipe();
611 die "unable to fork worker - $!" if !defined($cpid);
614 POSIX
::close($pipefd[1]);
616 my $fd = fileno(STDIN
);
618 POSIX
::close(0) if $fd != 0;
619 die "unable to redirect STDIN - $!"
620 if !open(STDIN
, "<&", $pipefd[0]);
623 $fd = fileno(STDOUT
);
625 POSIX
::close (1) if $fd != 1;
627 die "unable to redirect STDOUT - $!"
628 if !open(STDOUT
, ">&", fileno($outfh));
631 die "fork compressor '$comp' failed\n";
640 POSIX
::close($pipefd[0]);
641 $outfileno = $pipefd[1];
644 $outfileno = fileno($outfh);
647 my $add_fd_cb = sub {
648 my ($vmid, $resp) = @_;
651 'backup-file' => "/dev/fdname/backup",
653 'config-file' => $conffile,
657 $params->{'firewall-file'} = $firewall if -e
$firewall;
658 $qmpclient->queue_cmd($vmid, $backup_cb, 'backup', %$params);
661 $qmpclient->queue_cmd($vmid, $add_fd_cb, 'getfd',
662 fd
=> $outfileno, fdname
=> "backup");
664 my $agent_running = 0;
666 if ($self->{vmlist
}->{$vmid}->{agent
} && $vm_is_running) {
667 $agent_running = PVE
::QemuServer
::qga_check_running
($vmid);
668 $self->loginfo("skipping guest-agent 'fs-freeze', agent configured but not running?")
673 $self->loginfo("issuing guest-agent 'fs-freeze' command");
674 eval { mon_cmd
($vmid, "guest-fsfreeze-freeze"); };
680 eval { $qmpclient->queue_execute(30) };
684 $self->loginfo("issuing guest-agent 'fs-thaw' command");
685 eval { mon_cmd
($vmid, "guest-fsfreeze-thaw"); };
690 die $qmperr if $qmperr;
691 die $qmpclient->{errors
}->{$vmid} if $qmpclient->{errors
}->{$vmid};
694 POSIX
::close($outfileno) == 0 ||
695 die "close output file handle failed\n";
698 die "got no uuid for backup task\n" if !defined($backup_job_uuid);
700 $self->loginfo("started backup task '$backup_job_uuid'");
702 if ($resume_on_backup) {
703 if (my $stoptime = $task->{vmstoptime
}) {
704 my $delay = time() - $task->{vmstoptime
};
705 $task->{vmstoptime
} = undef; # avoid printing 'online after ..' twice
706 $self->loginfo("resuming VM again after $delay seconds");
708 $self->loginfo("resuming VM again");
710 mon_cmd
($vmid, 'cont');
713 $query_backup_status_loop->($self, $vmid, $backup_job_uuid);
719 if (defined($backup_job_uuid)) {
720 $self->loginfo("aborting backup job");
721 eval { mon_cmd
($vmid, 'backup-cancel'); };
723 $self->logerr($err1);
728 if ($stop_after_backup) {
729 # stop if not running
731 my $resp = mon_cmd
($vmid, 'query-status');
732 my $status = $resp && $resp->{status
} ?
$resp->{status
} : 'unknown';
733 if ($status eq 'prelaunch') {
734 $self->loginfo("stopping kvm after backup task");
735 PVE
::QemuServer
::vm_stop
($self->{storecfg
}, $vmid, $skiplock);
737 $self->loginfo("kvm status changed after backup ('$status')" .
738 " - keep VM running");
751 if ($cpid && (waitpid($cpid, 0) > 0)) {
754 my $signal = $stat & 127;
755 if ($ec || $signal) {
756 die "$comp failed - wrong exit status $ec" .
757 ($signal ?
" (signal $signal)\n" : "\n");
762 sub _get_task_devlist
{
766 foreach my $di (@{$task->{disks
}}) {
767 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
768 $devlist .= ',' if $devlist;
769 $devlist .= $di->{qmdevice
};
771 die "implement me (type '$di->{type}')";
778 my ($self, $task, $vmid) = @_;
784 my ($self, $task, $vmid) = @_;