1 package PVE
::VZDump
::QemuServer
;
11 use PVE
::Cluster
qw(cfs_read_file);
15 use PVE
::Storage
::Plugin
;
22 use base qw
(PVE
::VZDump
::Plugin
);
25 my ($class, $vzdump) = @_;
27 PVE
::VZDump
::check_bin
('qm');
29 my $self = bless { vzdump
=> $vzdump }, $class;
31 $self->{vmlist
} = PVE
::QemuServer
::vzlist
();
32 $self->{storecfg
} = PVE
::Storage
::config
();
45 return [ keys %{$self->{vmlist
}} ];
49 my ($self, $task, $vmid, $mode) = @_;
53 my $conf = $self->{vmlist
}->{$vmid} = PVE
::QemuConfig-
>load_config($vmid);
55 $self->loginfo("VM Name: $conf->{name}")
56 if defined($conf->{name
});
58 $self->{vm_was_running
} = 1;
59 if (!PVE
::QemuServer
::check_running
($vmid)) {
60 $self->{vm_was_running
} = 0;
63 $task->{hostname
} = $conf->{name
};
65 my $hostname = PVE
::INotify
::nodename
();
69 PVE
::QemuServer
::foreach_drive
($conf, sub {
70 my ($ds, $drive) = @_;
72 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
74 my $volid = $drive->{file
};
76 if (defined($drive->{backup
}) && !$drive->{backup
}) {
77 $self->loginfo("exclude disk '$ds' '$volid' (backup=no)");
79 } elsif ($self->{vm_was_running
} && $drive->{iothread
}) {
80 if (!PVE
::QemuServer
::runs_at_least_qemu_version
($vmid, 4, 0, 1)) {
81 die "disk '$ds' '$volid' (iothread=on) can't use backup feature with running QEMU " .
82 "version < 4.0.1! Either set backup=no for this drive or upgrade QEMU and restart VM\n";
85 my $log = "include disk '$ds' '$volid'";
86 if (defined $drive->{size
}) {
87 my $readable_size = PVE
::JSONSchema
::format_size
($drive->{size
});
88 $log .= " $readable_size";
93 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
94 push @$vollist, $volid if $storeid;
95 $drivehash->{$ds} = $drive;
98 PVE
::Storage
::activate_volumes
($self->{storecfg
}, $vollist);
100 foreach my $ds (sort keys %$drivehash) {
101 my $drive = $drivehash->{$ds};
103 my $volid = $drive->{file
};
107 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
109 $path = PVE
::Storage
::path
($self->{storecfg
}, $volid);
120 ($size, $format) = PVE
::Storage
::volume_size_info
($self->{storecfg
}, $volid, 5);
122 die "no such volume '$volid'\n" if $@;
124 my $diskinfo = { path
=> $path , volid
=> $volid, storeid
=> $storeid,
125 format
=> $format, virtdev
=> $ds, qmdevice
=> "drive-$ds" };
128 $diskinfo->{type
} = 'block';
130 $diskinfo->{type
} = 'file';
133 push @{$task->{disks
}}, $diskinfo;
138 my ($self, $vmid) = @_;
140 my $running = PVE
::QemuServer
::check_running
($vmid) ?
1 : 0;
142 return wantarray ?
($running, $running ?
'running' : 'stopped') : $running;
146 my ($self, $vmid) = @_;
148 $self->cmd ("qm set $vmid --lock backup");
152 my ($self, $vmid) = @_;
154 $self->cmd ("qm unlock $vmid");
158 my ($self, $task, $vmid) = @_;
160 my $opts = $self->{vzdump
}->{opts
};
162 my $wait = $opts->{stopwait
} * 60;
163 # send shutdown and wait
164 $self->cmd ("qm shutdown $vmid --skiplock --keepActive --timeout $wait");
168 my ($self, $task, $vmid) = @_;
170 $self->cmd ("qm start $vmid --skiplock");
174 my ($self, $task, $vmid) = @_;
176 $self->cmd ("qm suspend $vmid --skiplock");
180 my ($self, $task, $vmid) = @_;
182 $self->cmd ("qm resume $vmid --skiplock");
186 my ($self, $task, $vmid) = @_;
188 my $conffile = PVE
::QemuConfig-
>config_file($vmid);
190 my $outfile = "$task->{tmpdir}/qemu-server.conf";
191 my $firewall_src = "/etc/pve/firewall/$vmid.fw";
192 my $firewall_dest = "$task->{tmpdir}/qemu-server.fw";
194 my $outfd = IO
::File-
>new (">$outfile") ||
195 die "unable to open '$outfile'";
196 my $conffd = IO
::File-
>new ($conffile, 'r') ||
197 die "unable open '$conffile'";
201 while (defined (my $line = <$conffd>)) {
202 next if $line =~ m/^\#vzdump\#/; # just to be sure
203 next if $line =~ m/^\#qmdump\#/; # just to be sure
204 if ($line =~ m/^\[(.*)\]\s*$/) {
205 if ($1 =~ m/PENDING/i) {
212 next if $found_snapshot; # skip all snapshots data
213 next if $found_pending; # skip all pending changes
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)");
238 if ($found_pending) {
239 $self->loginfo("pending configuration changes found (not included into backup)");
242 PVE
::Tools
::file_copy
($firewall_src, $firewall_dest) if -f
$firewall_src;
246 my ($self, $task, $vmid, $filename, $comp) = @_;
248 my $conffile = "$task->{tmpdir}/qemu-server.conf";
249 my $firewall = "$task->{tmpdir}/qemu-server.fw";
251 my $opts = $self->{vzdump
}->{opts
};
253 my $starttime = time ();
256 if ($opts->{bwlimit
}) {
257 $speed = $opts->{bwlimit
}*1024;
260 my $diskcount = scalar(@{$task->{disks
}});
262 if (PVE
::QemuConfig-
>is_template($self->{vmlist
}->{$vmid}) || !$diskcount) {
264 foreach my $di (@{$task->{disks
}}) {
265 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
266 push @pathlist, "$di->{qmdevice}=$di->{path}";
273 $self->loginfo("backup contains no disks");
278 $outcmd = "exec:$comp";
280 $outcmd = "exec:cat";
283 $outcmd .= " > $filename" if !$opts->{stdout
};
285 my $cmd = ['/usr/bin/vma', 'create', '-v', '-c', $conffile];
286 push @$cmd, '-c', $firewall if -e
$firewall;
287 push @$cmd, $outcmd, @pathlist;
289 $self->loginfo("starting template backup");
290 $self->loginfo(join(' ', @$cmd));
292 if ($opts->{stdout
}) {
293 $self->cmd($cmd, output
=> ">&=" . fileno($opts->{stdout
}));
303 foreach my $di (@{$task->{disks
}}) {
304 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
305 $devlist .= $devlist ?
",$di->{qmdevice}" : $di->{qmdevice
};
311 my $stop_after_backup;
312 my $resume_on_backup;
315 my $vm_is_running = PVE
::QemuServer
::check_running
($vmid);
316 if (!$vm_is_running) {
318 $self->loginfo("starting kvm to execute backup task");
319 PVE
::QemuServer
::vm_start
($self->{storecfg
}, $vmid, undef,
320 $skiplock, undef, 1);
321 if ($self->{vm_was_running
}) {
322 $resume_on_backup = 1;
324 $stop_after_backup = 1;
333 my $interrupt_msg = "interrupted by signal\n";
335 $SIG{INT
} = $SIG{TERM
} = $SIG{QUIT
} = $SIG{HUP
} = $SIG{PIPE
} = sub {
339 my $qmpclient = PVE
::QMPClient-
>new();
343 my $backup_cb = sub {
344 my ($vmid, $resp) = @_;
345 $uuid = $resp->{return}->{UUID
};
349 if ($opts->{stdout
}) {
350 $outfh = $opts->{stdout
};
352 $outfh = IO
::File-
>new($filename, "w") ||
353 die "unable to open file '$filename' - $!\n";
358 my @pipefd = POSIX
::pipe();
360 die "unable to fork worker - $!" if !defined($cpid);
363 POSIX
::close($pipefd[1]);
365 my $fd = fileno(STDIN
);
367 POSIX
::close(0) if $fd != 0;
368 die "unable to redirect STDIN - $!"
369 if !open(STDIN
, "<&", $pipefd[0]);
372 $fd = fileno(STDOUT
);
374 POSIX
::close (1) if $fd != 1;
376 die "unable to redirect STDOUT - $!"
377 if !open(STDOUT
, ">&", fileno($outfh));
380 die "fork compressor '$comp' failed\n";
389 POSIX
::close($pipefd[0]);
390 $outfileno = $pipefd[1];
393 $outfileno = fileno($outfh);
396 my $add_fd_cb = sub {
397 my ($vmid, $resp) = @_;
400 'backup-file' => "/dev/fdname/backup",
402 'config-file' => $conffile,
406 $params->{'firewall-file'} = $firewall if -e
$firewall;
407 $qmpclient->queue_cmd($vmid, $backup_cb, 'backup', %$params);
410 $qmpclient->queue_cmd($vmid, $add_fd_cb, 'getfd',
411 fd
=> $outfileno, fdname
=> "backup");
413 my $agent_running = 0;
415 if ($self->{vmlist
}->{$vmid}->{agent
} && $vm_is_running) {
416 $agent_running = PVE
::QemuServer
::qga_check_running
($vmid);
420 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-freeze"); };
426 eval { $qmpclient->queue_execute() };
430 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-thaw"); };
435 die $qmperr if $qmperr;
436 die $qmpclient->{errors
}->{$vmid} if $qmpclient->{errors
}->{$vmid};
439 POSIX
::close($outfileno) == 0 ||
440 die "close output file handle failed\n";
443 die "got no uuid for backup task\n" if !$uuid;
445 $self->loginfo("started backup task '$uuid'");
447 if ($resume_on_backup) {
448 if (my $stoptime = $task->{vmstoptime
}) {
449 my $delay = time() - $task->{vmstoptime
};
450 $task->{vmstoptime
} = undef; # avoid printing 'online after ..' twice
451 $self->loginfo("resuming VM again after $delay seconds");
453 $self->loginfo("resuming VM again");
455 PVE
::QemuServer
::vm_mon_cmd
($vmid, 'cont');
459 my $starttime = time ();
463 my $last_transferred = 0;
464 my $last_time = time();
468 $status = PVE
::QemuServer
::vm_mon_cmd
($vmid, 'query-backup');
469 my $total = $status->{total
} || 0;
470 $transferred = $status->{transferred
} || 0;
471 my $per = $total ?
int(($transferred * 100)/$total) : 0;
472 my $zero = $status->{'zero-bytes'} || 0;
473 my $zero_per = $total ?
int(($zero * 100)/$total) : 0;
475 die "got unexpected uuid\n" if !$status->{uuid
} || ($status->{uuid
} ne $uuid);
478 my $duration = $ctime - $starttime;
480 my $rbytes = $transferred - $last_transferred;
481 my $wbytes = $rbytes - ($zero - $last_zero);
483 my $timediff = ($ctime - $last_time) || 1; # fixme
484 my $mbps_read = ($rbytes > 0) ?
485 int(($rbytes/$timediff)/(1000*1000)) : 0;
486 my $mbps_write = ($wbytes > 0) ?
487 int(($wbytes/$timediff)/(1000*1000)) : 0;
489 my $statusline = "status: $per% ($transferred/$total), " .
490 "sparse ${zero_per}% ($zero), duration $duration, " .
491 "read/write $mbps_read/$mbps_write MB/s";
492 my $res = $status->{status
} || 'unknown';
493 if ($res ne 'active') {
494 $self->loginfo($statusline);
495 die(($status->{errmsg
} || "unknown error") . "\n")
497 die "got unexpected status '$res'\n"
499 die "got wrong number of transfered bytes ($total != $transferred)\n"
500 if ($res eq 'done') && ($total != $transferred);
504 if ($per != $last_per && ($timediff > 2)) {
505 $self->loginfo($statusline);
507 $last_total = $total if $total;
508 $last_zero = $zero if $zero;
509 $last_transferred = $transferred if $transferred;
515 my $duration = time() - $starttime;
516 if ($transferred && $duration) {
517 my $mb = int($transferred/(1000*1000));
518 my $mbps = int(($transferred/$duration)/(1000*1000));
519 $self->loginfo("transferred $mb MB in $duration seconds ($mbps MB/s)");
526 $self->loginfo("aborting backup job");
527 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, 'backup-cancel'); };
529 $self->logerr($err1);
533 if ($stop_after_backup) {
534 # stop if not running
536 my $resp = PVE
::QemuServer
::vm_mon_cmd
($vmid, 'query-status');
537 my $status = $resp && $resp->{status
} ?
$resp->{status
} : 'unknown';
538 if ($status eq 'prelaunch') {
539 $self->loginfo("stopping kvm after backup task");
540 PVE
::QemuServer
::vm_stop
($self->{storecfg
}, $vmid, $skiplock);
542 $self->loginfo("kvm status changed after backup ('$status')" .
543 " - keep VM running");
556 if ($cpid && (waitpid($cpid, 0) > 0)) {
559 my $signal = $stat & 127;
560 if ($ec || $signal) {
561 die "$comp failed - wrong exit status $ec" .
562 ($signal ?
" (signal $signal)\n" : "\n");
568 my ($self, $task, $vmid) = @_;
574 my ($self, $task, $vmid) = @_;