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
();
49 return [ keys %{$self->{vmlist
}} ];
53 my ($self, $task, $vmid, $mode) = @_;
57 my $conf = $self->{vmlist
}->{$vmid} = PVE
::QemuConfig-
>load_config($vmid);
59 $self->loginfo("VM Name: $conf->{name}")
60 if defined($conf->{name
});
62 $self->{vm_was_running
} = 1;
63 if (!PVE
::QemuServer
::check_running
($vmid)) {
64 $self->{vm_was_running
} = 0;
67 $task->{hostname
} = $conf->{name
};
69 my $hostname = PVE
::INotify
::nodename
();
73 PVE
::QemuServer
::foreach_drive
($conf, sub {
74 my ($ds, $drive) = @_;
76 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
78 my $volid = $drive->{file
};
80 if (defined($drive->{backup
}) && !$drive->{backup
}) {
81 $self->loginfo("exclude disk '$ds' '$volid' (backup=no)");
83 } elsif ($self->{vm_was_running
} && $drive->{iothread
}) {
84 if (!PVE
::QemuServer
::Machine
::runs_at_least_qemu_version
($vmid, 4, 0, 1)) {
85 die "disk '$ds' '$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";
88 } elsif ($ds =~ m/^efidisk/ && (!defined($conf->{bios
}) || $conf->{bios
} ne 'ovmf')) {
89 $self->loginfo("excluding '$ds' (efidisks can only be backed up when BIOS is set to 'ovmf')");
92 my $log = "include disk '$ds' '$volid'";
93 if (defined $drive->{size
}) {
94 my $readable_size = PVE
::JSONSchema
::format_size
($drive->{size
});
95 $log .= " $readable_size";
100 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
101 push @$vollist, $volid if $storeid;
102 $drivehash->{$ds} = $drive;
105 PVE
::Storage
::activate_volumes
($self->{storecfg
}, $vollist);
107 foreach my $ds (sort keys %$drivehash) {
108 my $drive = $drivehash->{$ds};
110 my $volid = $drive->{file
};
114 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
116 $path = PVE
::Storage
::path
($self->{storecfg
}, $volid);
127 ($size, $format) = PVE
::Storage
::volume_size_info
($self->{storecfg
}, $volid, 5);
129 die "no such volume '$volid'\n" if $@;
131 my $diskinfo = { path
=> $path , volid
=> $volid, storeid
=> $storeid,
132 format
=> $format, virtdev
=> $ds, qmdevice
=> "drive-$ds" };
135 $diskinfo->{type
} = 'block';
137 $diskinfo->{type
} = 'file';
140 push @{$task->{disks
}}, $diskinfo;
145 my ($self, $vmid) = @_;
147 my $running = PVE
::QemuServer
::check_running
($vmid) ?
1 : 0;
149 return wantarray ?
($running, $running ?
'running' : 'stopped') : $running;
153 my ($self, $vmid) = @_;
155 $self->cmd ("qm set $vmid --lock backup");
159 my ($self, $vmid) = @_;
161 $self->cmd ("qm unlock $vmid");
165 my ($self, $task, $vmid) = @_;
167 my $opts = $self->{vzdump
}->{opts
};
169 my $wait = $opts->{stopwait
} * 60;
170 # send shutdown and wait
171 $self->cmd ("qm shutdown $vmid --skiplock --keepActive --timeout $wait");
175 my ($self, $task, $vmid) = @_;
177 $self->cmd ("qm start $vmid --skiplock");
181 my ($self, $task, $vmid) = @_;
183 $self->cmd ("qm suspend $vmid --skiplock");
187 my ($self, $task, $vmid) = @_;
189 $self->cmd ("qm resume $vmid --skiplock");
193 my ($self, $task, $vmid) = @_;
195 my $conffile = PVE
::QemuConfig-
>config_file($vmid);
197 my $outfile = "$task->{tmpdir}/qemu-server.conf";
198 my $firewall_src = "/etc/pve/firewall/$vmid.fw";
199 my $firewall_dest = "$task->{tmpdir}/qemu-server.fw";
201 my $outfd = IO
::File-
>new (">$outfile") ||
202 die "unable to open '$outfile'";
203 my $conffd = IO
::File-
>new ($conffile, 'r') ||
204 die "unable open '$conffile'";
208 while (defined (my $line = <$conffd>)) {
209 next if $line =~ m/^\#vzdump\#/; # just to be sure
210 next if $line =~ m/^\#qmdump\#/; # just to be sure
211 if ($line =~ m/^\[(.*)\]\s*$/) {
212 if ($1 =~ m/PENDING/i) {
219 next if $found_snapshot; # skip all snapshots data
220 next if $found_pending; # skip all pending changes
222 if ($line =~ m/^unused\d+:\s*(\S+)\s*/) {
223 $self->loginfo("skip unused drive '$1' (not included into backup)");
226 next if $line =~ m/^lock:/ || $line =~ m/^parent:/;
231 foreach my $di (@{$task->{disks
}}) {
232 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
233 my $storeid = $di->{storeid
} || '';
234 my $format = $di->{format
} || '';
235 print $outfd "#qmdump#map:$di->{virtdev}:$di->{qmdevice}:$storeid:$format:\n";
237 die "internal error";
241 if ($found_snapshot) {
242 $self->loginfo("snapshots found (not included into backup)");
245 if ($found_pending) {
246 $self->loginfo("pending configuration changes found (not included into backup)");
249 PVE
::Tools
::file_copy
($firewall_src, $firewall_dest) if -f
$firewall_src;
253 my ($self, $task, $vmid, $filename, $comp) = @_;
255 my $opts = $self->{vzdump
}->{opts
};
257 my $scfg = $opts->{scfg
};
259 if ($scfg->{type
} eq 'pbs') {
260 $self->archive_pbs($task, $vmid);
262 $self->archive_vma($task, $vmid, $filename, $comp);
266 my $query_backup_status_loop = sub {
267 my ($self, $vmid, $job_uuid) = @_;
270 my $starttime = time ();
274 my $last_transferred = 0;
275 my $last_time = time();
279 $status = mon_cmd
($vmid, 'query-backup');
280 my $total = $status->{total
} || 0;
281 $transferred = $status->{transferred
} || 0;
282 my $per = $total ?
int(($transferred * 100)/$total) : 0;
283 my $zero = $status->{'zero-bytes'} || 0;
284 my $zero_per = $total ?
int(($zero * 100)/$total) : 0;
286 die "got unexpected uuid\n" if !$status->{uuid
} || ($status->{uuid
} ne $job_uuid);
289 my $duration = $ctime - $starttime;
291 my $rbytes = $transferred - $last_transferred;
292 my $wbytes = $rbytes - ($zero - $last_zero);
294 my $timediff = ($ctime - $last_time) || 1; # fixme
295 my $mbps_read = ($rbytes > 0) ?
296 int(($rbytes/$timediff)/(1000*1000)) : 0;
297 my $mbps_write = ($wbytes > 0) ?
298 int(($wbytes/$timediff)/(1000*1000)) : 0;
300 my $statusline = "status: $per% ($transferred/$total), " .
301 "sparse ${zero_per}% ($zero), duration $duration, " .
302 "read/write $mbps_read/$mbps_write MB/s";
303 my $res = $status->{status
} || 'unknown';
304 if ($res ne 'active') {
305 $self->loginfo($statusline);
306 die(($status->{errmsg
} || "unknown error") . "\n")
308 die "got unexpected status '$res'\n"
310 die "got wrong number of transfered bytes ($total != $transferred)\n"
311 if ($res eq 'done') && ($total != $transferred);
315 if ($per != $last_per && ($timediff > 2)) {
316 $self->loginfo($statusline);
318 $last_total = $total if $total;
319 $last_zero = $zero if $zero;
320 $last_transferred = $transferred if $transferred;
326 my $duration = time() - $starttime;
327 if ($transferred && $duration) {
328 my $mb = int($transferred/(1000*1000));
329 my $mbps = int(($transferred/$duration)/(1000*1000));
330 $self->loginfo("transferred $mb MB in $duration seconds ($mbps MB/s)");
335 my ($self, $task, $vmid) = @_;
337 my $conffile = "$task->{tmpdir}/qemu-server.conf";
338 my $firewall = "$task->{tmpdir}/qemu-server.fw";
340 my $opts = $self->{vzdump
}->{opts
};
342 my $scfg = $opts->{scfg
};
344 my $starttime = time();
346 my $diskcount = scalar(@{$task->{disks
}});
348 my $server = $scfg->{server
};
349 my $datastore = $scfg->{datastore
};
350 my $username = $scfg->{username
} // 'root@pam';
351 my $fingerprint = $scfg->{fingerprint
};
353 my $repo = "$username\@$server:$datastore";
354 my $password = PVE
::Storage
::PBSPlugin
::pbs_get_password
($scfg, $opts->{storage
});
356 if (PVE
::QemuConfig-
>is_template($self->{vmlist
}->{$vmid}) || !$diskcount) {
358 foreach my $di (@{$task->{disks
}}) {
359 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
360 push @pathlist, "$di->{qmdevice}.img:$di->{path}";
367 $self->loginfo("backup contains no disks");
370 local $ENV{PBS_PASSWORD
} = $password;
372 '/usr/bin/proxmox-backup-client',
374 '--repository', $repo,
375 '--backup-type', 'vm',
376 '--backup-id', "$vmid",
377 '--backup-time', $task->{backup_time
},
380 push @$cmd, '--fingerprint', $fingerprint if defined($fingerprint);
382 push @$cmd, "qemu-server.conf:$conffile";
383 push @$cmd, "fw.conf:$firewall" if -e
$firewall;
384 push @$cmd, @pathlist if scalar(@pathlist);
386 $self->loginfo("starting template backup");
387 $self->loginfo(join(' ', @$cmd));
395 foreach my $di (@{$task->{disks
}}) {
396 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
397 $devlist .= $devlist ?
",$di->{qmdevice}" : $di->{qmdevice
};
403 my $stop_after_backup;
404 my $resume_on_backup;
407 my $vm_is_running = PVE
::QemuServer
::check_running
($vmid);
408 if (!$vm_is_running) {
410 $self->loginfo("starting kvm to execute backup task");
411 PVE
::QemuServer
::vm_start
($self->{storecfg
}, $vmid, undef,
412 $skiplock, undef, 1);
413 if ($self->{vm_was_running
}) {
414 $resume_on_backup = 1;
416 $stop_after_backup = 1;
424 my $interrupt_msg = "interrupted by signal\n";
426 $SIG{INT
} = $SIG{TERM
} = $SIG{QUIT
} = $SIG{HUP
} = $SIG{PIPE
} = sub {
430 my $agent_running = 0;
432 if ($self->{vmlist
}->{$vmid}->{agent
} && $vm_is_running) {
433 $agent_running = PVE
::QemuServer
::qga_check_running
($vmid);
437 eval { mon_cmd
($vmid, "guest-fsfreeze-freeze"); };
449 'backup-file' => $repo,
450 'backup-id' => "$vmid",
451 'backup-time' => $task->{backup_time
},
452 password
=> $password,
454 'config-file' => $conffile,
456 $params->{fingerprint
} = $fingerprint if defined($fingerprint);
457 $params->{'firewall-file'} = $firewall if -e
$firewall;
458 my $res = mon_cmd
($vmid, "backup", %$params);
459 $uuid = $res->{UUID
};
465 eval { mon_cmd
($vmid, "guest-fsfreeze-thaw"); };
471 die $qmperr if $qmperr;
473 die "got no uuid for backup task\n" if !$uuid;
475 $self->loginfo("started backup task '$uuid'");
477 if ($resume_on_backup) {
478 if (my $stoptime = $task->{vmstoptime
}) {
479 my $delay = time() - $task->{vmstoptime
};
480 $task->{vmstoptime
} = undef; # avoid printing 'online after ..' twice
481 $self->loginfo("resuming VM again after $delay seconds");
483 $self->loginfo("resuming VM again");
485 mon_cmd
($vmid, 'cont');
488 $query_backup_status_loop->($self, $vmid, $uuid);
494 $self->loginfo("aborting backup job");
495 eval { mon_cmd
($vmid, 'backup-cancel'); };
497 $self->logerr($err1);
501 if ($stop_after_backup) {
502 # stop if not running
504 my $resp = mon_cmd
($vmid, 'query-status');
505 my $status = $resp && $resp->{status
} ?
$resp->{status
} : 'unknown';
506 if ($status eq 'prelaunch') {
507 $self->loginfo("stopping kvm after backup task");
508 PVE
::QemuServer
::vm_stop
($self->{storecfg
}, $vmid, $skiplock);
510 $self->loginfo("kvm status changed after backup ('$status')" .
511 " - keep VM running");
520 my ($self, $task, $vmid, $filename, $comp) = @_;
522 my $conffile = "$task->{tmpdir}/qemu-server.conf";
523 my $firewall = "$task->{tmpdir}/qemu-server.fw";
525 my $opts = $self->{vzdump
}->{opts
};
527 my $starttime = time();
530 if ($opts->{bwlimit
}) {
531 $speed = $opts->{bwlimit
}*1024;
534 my $diskcount = scalar(@{$task->{disks
}});
536 if (PVE
::QemuConfig-
>is_template($self->{vmlist
}->{$vmid}) || !$diskcount) {
538 foreach my $di (@{$task->{disks
}}) {
539 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
540 push @pathlist, "$di->{qmdevice}=$di->{path}";
547 $self->loginfo("backup contains no disks");
552 $outcmd = "exec:$comp";
554 $outcmd = "exec:cat";
557 $outcmd .= " > $filename" if !$opts->{stdout
};
559 my $cmd = ['/usr/bin/vma', 'create', '-v', '-c', $conffile];
560 push @$cmd, '-c', $firewall if -e
$firewall;
561 push @$cmd, $outcmd, @pathlist;
563 $self->loginfo("starting template backup");
564 $self->loginfo(join(' ', @$cmd));
566 if ($opts->{stdout
}) {
567 $self->cmd($cmd, output
=> ">&=" . fileno($opts->{stdout
}));
577 foreach my $di (@{$task->{disks
}}) {
578 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
579 $devlist .= $devlist ?
",$di->{qmdevice}" : $di->{qmdevice
};
585 my $stop_after_backup;
586 my $resume_on_backup;
589 my $vm_is_running = PVE
::QemuServer
::check_running
($vmid);
590 if (!$vm_is_running) {
592 $self->loginfo("starting kvm to execute backup task");
593 PVE
::QemuServer
::vm_start
($self->{storecfg
}, $vmid, undef,
594 $skiplock, undef, 1);
595 if ($self->{vm_was_running
}) {
596 $resume_on_backup = 1;
598 $stop_after_backup = 1;
607 my $interrupt_msg = "interrupted by signal\n";
609 $SIG{INT
} = $SIG{TERM
} = $SIG{QUIT
} = $SIG{HUP
} = $SIG{PIPE
} = sub {
613 my $qmpclient = PVE
::QMPClient-
>new();
617 my $backup_cb = sub {
618 my ($vmid, $resp) = @_;
619 $uuid = $resp->{return}->{UUID
};
623 if ($opts->{stdout
}) {
624 $outfh = $opts->{stdout
};
626 $outfh = IO
::File-
>new($filename, "w") ||
627 die "unable to open file '$filename' - $!\n";
632 my @pipefd = POSIX
::pipe();
634 die "unable to fork worker - $!" if !defined($cpid);
637 POSIX
::close($pipefd[1]);
639 my $fd = fileno(STDIN
);
641 POSIX
::close(0) if $fd != 0;
642 die "unable to redirect STDIN - $!"
643 if !open(STDIN
, "<&", $pipefd[0]);
646 $fd = fileno(STDOUT
);
648 POSIX
::close (1) if $fd != 1;
650 die "unable to redirect STDOUT - $!"
651 if !open(STDOUT
, ">&", fileno($outfh));
654 die "fork compressor '$comp' failed\n";
663 POSIX
::close($pipefd[0]);
664 $outfileno = $pipefd[1];
667 $outfileno = fileno($outfh);
670 my $add_fd_cb = sub {
671 my ($vmid, $resp) = @_;
674 'backup-file' => "/dev/fdname/backup",
676 'config-file' => $conffile,
680 $params->{'firewall-file'} = $firewall if -e
$firewall;
681 $qmpclient->queue_cmd($vmid, $backup_cb, 'backup', %$params);
684 $qmpclient->queue_cmd($vmid, $add_fd_cb, 'getfd',
685 fd
=> $outfileno, fdname
=> "backup");
687 my $agent_running = 0;
689 if ($self->{vmlist
}->{$vmid}->{agent
} && $vm_is_running) {
690 $agent_running = PVE
::QemuServer
::qga_check_running
($vmid);
691 $self->loginfo("skipping guest-agent 'fs-freeze', agent configured but not running?")
696 $self->loginfo("issuing guest-agent 'fs-freeze' command");
697 eval { mon_cmd
($vmid, "guest-fsfreeze-freeze"); };
703 eval { $qmpclient->queue_execute(30) };
707 $self->loginfo("issuing guest-agent 'fs-thaw' command");
708 eval { mon_cmd
($vmid, "guest-fsfreeze-thaw"); };
713 die $qmperr if $qmperr;
714 die $qmpclient->{errors
}->{$vmid} if $qmpclient->{errors
}->{$vmid};
717 POSIX
::close($outfileno) == 0 ||
718 die "close output file handle failed\n";
721 die "got no uuid for backup task\n" if !$uuid;
723 $self->loginfo("started backup task '$uuid'");
725 if ($resume_on_backup) {
726 if (my $stoptime = $task->{vmstoptime
}) {
727 my $delay = time() - $task->{vmstoptime
};
728 $task->{vmstoptime
} = undef; # avoid printing 'online after ..' twice
729 $self->loginfo("resuming VM again after $delay seconds");
731 $self->loginfo("resuming VM again");
733 mon_cmd
($vmid, 'cont');
736 $query_backup_status_loop->($self, $vmid, $uuid);
742 $self->loginfo("aborting backup job");
743 eval { mon_cmd
($vmid, 'backup-cancel'); };
745 $self->logerr($err1);
749 if ($stop_after_backup) {
750 # stop if not running
752 my $resp = mon_cmd
($vmid, 'query-status');
753 my $status = $resp && $resp->{status
} ?
$resp->{status
} : 'unknown';
754 if ($status eq 'prelaunch') {
755 $self->loginfo("stopping kvm after backup task");
756 PVE
::QemuServer
::vm_stop
($self->{storecfg
}, $vmid, $skiplock);
758 $self->loginfo("kvm status changed after backup ('$status')" .
759 " - keep VM running");
772 if ($cpid && (waitpid($cpid, 0) > 0)) {
775 my $signal = $stat & 127;
776 if ($ec || $signal) {
777 die "$comp failed - wrong exit status $ec" .
778 ($signal ?
" (signal $signal)\n" : "\n");
784 my ($self, $task, $vmid) = @_;
790 my ($self, $task, $vmid) = @_;