1 package PVE
::VZDump
::QemuServer
;
11 use PVE
::Cluster
qw(cfs_read_file);
16 use PVE
::Storage
::Plugin
;
22 use PVE
::QemuServer
::Machine
;
23 use PVE
::QemuServer
::Monitor
qw(mon_cmd);
25 use base qw
(PVE
::VZDump
::Plugin
);
28 my ($class, $vzdump) = @_;
30 PVE
::VZDump
::check_bin
('qm');
32 my $self = bless { vzdump
=> $vzdump }, $class;
34 $self->{vmlist
} = PVE
::QemuServer
::vzlist
();
35 $self->{storecfg
} = PVE
::Storage
::config
();
48 return [ keys %{$self->{vmlist
}} ];
52 my ($self, $task, $vmid, $mode) = @_;
56 my $conf = $self->{vmlist
}->{$vmid} = PVE
::QemuConfig-
>load_config($vmid);
58 $self->loginfo("VM Name: $conf->{name}")
59 if defined($conf->{name
});
61 $self->{vm_was_running
} = 1;
62 if (!PVE
::QemuServer
::check_running
($vmid)) {
63 $self->{vm_was_running
} = 0;
66 $task->{hostname
} = $conf->{name
};
68 my $hostname = PVE
::INotify
::nodename
();
72 PVE
::QemuServer
::foreach_drive
($conf, sub {
73 my ($ds, $drive) = @_;
75 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
77 my $volid = $drive->{file
};
79 if (defined($drive->{backup
}) && !$drive->{backup
}) {
80 $self->loginfo("exclude disk '$ds' '$volid' (backup=no)");
82 } elsif ($self->{vm_was_running
} && $drive->{iothread
}) {
83 if (!PVE
::QemuServer
::Machine
::runs_at_least_qemu_version
($vmid, 4, 0, 1)) {
84 die "disk '$ds' '$volid' (iothread=on) can't use backup feature with running QEMU " .
85 "version < 4.0.1! Either set backup=no for this drive or upgrade QEMU and restart VM\n";
87 } elsif ($ds =~ m/^efidisk/ && (!defined($conf->{bios
}) || $conf->{bios
} ne 'ovmf')) {
88 $self->loginfo("excluding '$ds' (efidisks can only be backed up when BIOS is set to 'ovmf')");
91 my $log = "include disk '$ds' '$volid'";
92 if (defined $drive->{size
}) {
93 my $readable_size = PVE
::JSONSchema
::format_size
($drive->{size
});
94 $log .= " $readable_size";
99 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
100 push @$vollist, $volid if $storeid;
101 $drivehash->{$ds} = $drive;
104 PVE
::Storage
::activate_volumes
($self->{storecfg
}, $vollist);
106 foreach my $ds (sort keys %$drivehash) {
107 my $drive = $drivehash->{$ds};
109 my $volid = $drive->{file
};
113 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
115 $path = PVE
::Storage
::path
($self->{storecfg
}, $volid);
126 ($size, $format) = PVE
::Storage
::volume_size_info
($self->{storecfg
}, $volid, 5);
128 die "no such volume '$volid'\n" if $@;
130 my $diskinfo = { path
=> $path , volid
=> $volid, storeid
=> $storeid,
131 format
=> $format, virtdev
=> $ds, qmdevice
=> "drive-$ds" };
134 $diskinfo->{type
} = 'block';
136 $diskinfo->{type
} = 'file';
139 push @{$task->{disks
}}, $diskinfo;
144 my ($self, $vmid) = @_;
146 my $running = PVE
::QemuServer
::check_running
($vmid) ?
1 : 0;
148 return wantarray ?
($running, $running ?
'running' : 'stopped') : $running;
152 my ($self, $vmid) = @_;
154 $self->cmd ("qm set $vmid --lock backup");
158 my ($self, $vmid) = @_;
160 $self->cmd ("qm unlock $vmid");
164 my ($self, $task, $vmid) = @_;
166 my $opts = $self->{vzdump
}->{opts
};
168 my $wait = $opts->{stopwait
} * 60;
169 # send shutdown and wait
170 $self->cmd ("qm shutdown $vmid --skiplock --keepActive --timeout $wait");
174 my ($self, $task, $vmid) = @_;
176 $self->cmd ("qm start $vmid --skiplock");
180 my ($self, $task, $vmid) = @_;
182 $self->cmd ("qm suspend $vmid --skiplock");
186 my ($self, $task, $vmid) = @_;
188 $self->cmd ("qm resume $vmid --skiplock");
192 my ($self, $task, $vmid) = @_;
194 my $conffile = PVE
::QemuConfig-
>config_file($vmid);
196 my $outfile = "$task->{tmpdir}/qemu-server.conf";
197 my $firewall_src = "/etc/pve/firewall/$vmid.fw";
198 my $firewall_dest = "$task->{tmpdir}/qemu-server.fw";
200 my $outfd = IO
::File-
>new (">$outfile") ||
201 die "unable to open '$outfile'";
202 my $conffd = IO
::File-
>new ($conffile, 'r') ||
203 die "unable open '$conffile'";
207 while (defined (my $line = <$conffd>)) {
208 next if $line =~ m/^\#vzdump\#/; # just to be sure
209 next if $line =~ m/^\#qmdump\#/; # just to be sure
210 if ($line =~ m/^\[(.*)\]\s*$/) {
211 if ($1 =~ m/PENDING/i) {
218 next if $found_snapshot; # skip all snapshots data
219 next if $found_pending; # skip all pending changes
221 if ($line =~ m/^unused\d+:\s*(\S+)\s*/) {
222 $self->loginfo("skip unused drive '$1' (not included into backup)");
225 next if $line =~ m/^lock:/ || $line =~ m/^parent:/;
230 foreach my $di (@{$task->{disks
}}) {
231 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
232 my $storeid = $di->{storeid
} || '';
233 my $format = $di->{format
} || '';
234 print $outfd "#qmdump#map:$di->{virtdev}:$di->{qmdevice}:$storeid:$format:\n";
236 die "internal error";
240 if ($found_snapshot) {
241 $self->loginfo("snapshots found (not included into backup)");
244 if ($found_pending) {
245 $self->loginfo("pending configuration changes found (not included into backup)");
248 PVE
::Tools
::file_copy
($firewall_src, $firewall_dest) if -f
$firewall_src;
252 my ($self, $task, $vmid, $filename, $comp) = @_;
254 my $conffile = "$task->{tmpdir}/qemu-server.conf";
255 my $firewall = "$task->{tmpdir}/qemu-server.fw";
257 my $opts = $self->{vzdump
}->{opts
};
259 my $starttime = time ();
262 if ($opts->{bwlimit
}) {
263 $speed = $opts->{bwlimit
}*1024;
266 my $diskcount = scalar(@{$task->{disks
}});
268 if (PVE
::QemuConfig-
>is_template($self->{vmlist
}->{$vmid}) || !$diskcount) {
270 foreach my $di (@{$task->{disks
}}) {
271 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
272 push @pathlist, "$di->{qmdevice}=$di->{path}";
279 $self->loginfo("backup contains no disks");
284 $outcmd = "exec:$comp";
286 $outcmd = "exec:cat";
289 $outcmd .= " > $filename" if !$opts->{stdout
};
291 my $cmd = ['/usr/bin/vma', 'create', '-v', '-c', $conffile];
292 push @$cmd, '-c', $firewall if -e
$firewall;
293 push @$cmd, $outcmd, @pathlist;
295 $self->loginfo("starting template backup");
296 $self->loginfo(join(' ', @$cmd));
298 if ($opts->{stdout
}) {
299 $self->cmd($cmd, output
=> ">&=" . fileno($opts->{stdout
}));
309 foreach my $di (@{$task->{disks
}}) {
310 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
311 $devlist .= $devlist ?
",$di->{qmdevice}" : $di->{qmdevice
};
317 my $stop_after_backup;
318 my $resume_on_backup;
321 my $vm_is_running = PVE
::QemuServer
::check_running
($vmid);
322 if (!$vm_is_running) {
324 $self->loginfo("starting kvm to execute backup task");
325 PVE
::QemuServer
::vm_start
($self->{storecfg
}, $vmid, undef,
326 $skiplock, undef, 1);
327 if ($self->{vm_was_running
}) {
328 $resume_on_backup = 1;
330 $stop_after_backup = 1;
339 my $interrupt_msg = "interrupted by signal\n";
341 $SIG{INT
} = $SIG{TERM
} = $SIG{QUIT
} = $SIG{HUP
} = $SIG{PIPE
} = sub {
345 my $qmpclient = PVE
::QMPClient-
>new();
349 my $backup_cb = sub {
350 my ($vmid, $resp) = @_;
351 $uuid = $resp->{return}->{UUID
};
355 if ($opts->{stdout
}) {
356 $outfh = $opts->{stdout
};
358 $outfh = IO
::File-
>new($filename, "w") ||
359 die "unable to open file '$filename' - $!\n";
364 my @pipefd = POSIX
::pipe();
366 die "unable to fork worker - $!" if !defined($cpid);
369 POSIX
::close($pipefd[1]);
371 my $fd = fileno(STDIN
);
373 POSIX
::close(0) if $fd != 0;
374 die "unable to redirect STDIN - $!"
375 if !open(STDIN
, "<&", $pipefd[0]);
378 $fd = fileno(STDOUT
);
380 POSIX
::close (1) if $fd != 1;
382 die "unable to redirect STDOUT - $!"
383 if !open(STDOUT
, ">&", fileno($outfh));
386 die "fork compressor '$comp' failed\n";
395 POSIX
::close($pipefd[0]);
396 $outfileno = $pipefd[1];
399 $outfileno = fileno($outfh);
402 my $add_fd_cb = sub {
403 my ($vmid, $resp) = @_;
406 'backup-file' => "/dev/fdname/backup",
408 'config-file' => $conffile,
412 $params->{'firewall-file'} = $firewall if -e
$firewall;
413 $qmpclient->queue_cmd($vmid, $backup_cb, 'backup', %$params);
416 $qmpclient->queue_cmd($vmid, $add_fd_cb, 'getfd',
417 fd
=> $outfileno, fdname
=> "backup");
419 my $agent_running = 0;
421 if ($self->{vmlist
}->{$vmid}->{agent
} && $vm_is_running) {
422 $agent_running = PVE
::QemuServer
::qga_check_running
($vmid);
423 $self->loginfo("skipping guest-agent 'fs-freeze', agent configured but not running?")
428 $self->loginfo("issuing guest-agent 'fs-freeze' command");
429 eval { mon_cmd
($vmid, "guest-fsfreeze-freeze"); };
435 eval { $qmpclient->queue_execute() };
439 $self->loginfo("issuing guest-agent 'fs-thaw' command");
440 eval { mon_cmd
($vmid, "guest-fsfreeze-thaw"); };
445 die $qmperr if $qmperr;
446 die $qmpclient->{errors
}->{$vmid} if $qmpclient->{errors
}->{$vmid};
449 POSIX
::close($outfileno) == 0 ||
450 die "close output file handle failed\n";
453 die "got no uuid for backup task\n" if !$uuid;
455 $self->loginfo("started backup task '$uuid'");
457 if ($resume_on_backup) {
458 if (my $stoptime = $task->{vmstoptime
}) {
459 my $delay = time() - $task->{vmstoptime
};
460 $task->{vmstoptime
} = undef; # avoid printing 'online after ..' twice
461 $self->loginfo("resuming VM again after $delay seconds");
463 $self->loginfo("resuming VM again");
465 mon_cmd
($vmid, 'cont');
469 my $starttime = time ();
473 my $last_transferred = 0;
474 my $last_time = time();
478 $status = mon_cmd
($vmid, 'query-backup');
479 my $total = $status->{total
} || 0;
480 $transferred = $status->{transferred
} || 0;
481 my $per = $total ?
int(($transferred * 100)/$total) : 0;
482 my $zero = $status->{'zero-bytes'} || 0;
483 my $zero_per = $total ?
int(($zero * 100)/$total) : 0;
485 die "got unexpected uuid\n" if !$status->{uuid
} || ($status->{uuid
} ne $uuid);
488 my $duration = $ctime - $starttime;
490 my $rbytes = $transferred - $last_transferred;
491 my $wbytes = $rbytes - ($zero - $last_zero);
493 my $timediff = ($ctime - $last_time) || 1; # fixme
494 my $mbps_read = ($rbytes > 0) ?
495 int(($rbytes/$timediff)/(1000*1000)) : 0;
496 my $mbps_write = ($wbytes > 0) ?
497 int(($wbytes/$timediff)/(1000*1000)) : 0;
499 my $statusline = "status: $per% ($transferred/$total), " .
500 "sparse ${zero_per}% ($zero), duration $duration, " .
501 "read/write $mbps_read/$mbps_write MB/s";
502 my $res = $status->{status
} || 'unknown';
503 if ($res ne 'active') {
504 $self->loginfo($statusline);
505 die(($status->{errmsg
} || "unknown error") . "\n")
507 die "got unexpected status '$res'\n"
509 die "got wrong number of transfered bytes ($total != $transferred)\n"
510 if ($res eq 'done') && ($total != $transferred);
514 if ($per != $last_per && ($timediff > 2)) {
515 $self->loginfo($statusline);
517 $last_total = $total if $total;
518 $last_zero = $zero if $zero;
519 $last_transferred = $transferred if $transferred;
525 my $duration = time() - $starttime;
526 if ($transferred && $duration) {
527 my $mb = int($transferred/(1000*1000));
528 my $mbps = int(($transferred/$duration)/(1000*1000));
529 $self->loginfo("transferred $mb MB in $duration seconds ($mbps MB/s)");
536 $self->loginfo("aborting backup job");
537 eval { mon_cmd
($vmid, 'backup-cancel'); };
539 $self->logerr($err1);
543 if ($stop_after_backup) {
544 # stop if not running
546 my $resp = mon_cmd
($vmid, 'query-status');
547 my $status = $resp && $resp->{status
} ?
$resp->{status
} : 'unknown';
548 if ($status eq 'prelaunch') {
549 $self->loginfo("stopping kvm after backup task");
550 PVE
::QemuServer
::vm_stop
($self->{storecfg
}, $vmid, $skiplock);
552 $self->loginfo("kvm status changed after backup ('$status')" .
553 " - keep VM running");
566 if ($cpid && (waitpid($cpid, 0) > 0)) {
569 my $signal = $stat & 127;
570 if ($ec || $signal) {
571 die "$comp failed - wrong exit status $ec" .
572 ($signal ?
" (signal $signal)\n" : "\n");
578 my ($self, $task, $vmid) = @_;
584 my ($self, $task, $vmid) = @_;