]>
git.proxmox.com Git - qemu-server.git/blob - PVE/VZDump/QemuServer.pm
1 package PVE
::VZDump
::QemuServer
;
10 use PVE
::Cluster
qw(cfs_read_file);
12 use PVE
::Storage
::Plugin
;
19 use base qw
(PVE
::VZDump
::Plugin
);
22 my ($class, $vzdump) = @_;
24 PVE
::VZDump
::check_bin
('qm');
26 my $self = bless { vzdump
=> $vzdump }, $class;
28 $self->{vmlist
} = PVE
::QemuServer
::vzlist
();
29 $self->{storecfg
} = PVE
::Storage
::config
();
42 return [ keys %{$self->{vmlist
}} ];
46 my ($self, $task, $vmid, $mode) = @_;
50 my $conf = $self->{vmlist
}->{$vmid} = PVE
::QemuConfig-
>load_config($vmid);
52 $self->loginfo("VM Name: $conf->{name}")
53 if defined($conf->{name
});
55 $self->{vm_was_running
} = 1;
56 if (!PVE
::QemuServer
::check_running
($vmid)) {
57 $self->{vm_was_running
} = 0;
60 $task->{hostname
} = $conf->{name
};
62 my $hostname = PVE
::INotify
::nodename
();
66 PVE
::QemuServer
::foreach_drive
($conf, sub {
67 my ($ds, $drive) = @_;
69 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
71 my $volid = $drive->{file
};
73 if (defined($drive->{backup
}) && !$drive->{backup
}) {
74 $self->loginfo("exclude disk '$ds' '$volid' (backup=no)");
76 } elsif ($drive->{iothread
}) {
77 die "disk '$ds' '$volid' (iothread=on) can't use backup feature currently. Please set backup=no for this drive";
79 my $log = "include disk '$ds' '$volid'";
80 if (defined $drive->{size
}) {
81 my $readable_size = PVE
::JSONSchema
::format_size
($drive->{size
});
82 $log .= " $readable_size";
87 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
88 push @$vollist, $volid if $storeid;
89 $drivehash->{$ds} = $drive;
92 PVE
::Storage
::activate_volumes
($self->{storecfg
}, $vollist);
94 foreach my $ds (sort keys %$drivehash) {
95 my $drive = $drivehash->{$ds};
97 my $volid = $drive->{file
};
101 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
103 $path = PVE
::Storage
::path
($self->{storecfg
}, $volid);
114 ($size, $format) = PVE
::Storage
::volume_size_info
($self->{storecfg
}, $volid, 5);
116 die "no such volume '$volid'\n" if $@;
118 my $diskinfo = { path
=> $path , volid
=> $volid, storeid
=> $storeid,
119 format
=> $format, virtdev
=> $ds, qmdevice
=> "drive-$ds" };
122 $diskinfo->{type
} = 'block';
124 $diskinfo->{type
} = 'file';
127 push @{$task->{disks
}}, $diskinfo;
132 my ($self, $vmid) = @_;
134 my $running = PVE
::QemuServer
::check_running
($vmid) ?
1 : 0;
136 return wantarray ?
($running, $running ?
'running' : 'stopped') : $running;
140 my ($self, $vmid) = @_;
142 $self->cmd ("qm set $vmid --lock backup");
146 my ($self, $vmid) = @_;
148 $self->cmd ("qm unlock $vmid");
152 my ($self, $task, $vmid) = @_;
154 my $opts = $self->{vzdump
}->{opts
};
156 my $wait = $opts->{stopwait
} * 60;
157 # send shutdown and wait
158 $self->cmd ("qm shutdown $vmid --skiplock --keepActive --timeout $wait");
162 my ($self, $task, $vmid) = @_;
164 $self->cmd ("qm start $vmid --skiplock");
168 my ($self, $task, $vmid) = @_;
170 $self->cmd ("qm suspend $vmid --skiplock");
174 my ($self, $task, $vmid) = @_;
176 $self->cmd ("qm resume $vmid --skiplock");
180 my ($self, $task, $vmid) = @_;
182 my $conffile = PVE
::QemuConfig-
>config_file($vmid);
184 my $outfile = "$task->{tmpdir}/qemu-server.conf";
185 my $firewall_src = "/etc/pve/firewall/$vmid.fw";
186 my $firewall_dest = "$task->{tmpdir}/qemu-server.fw";
188 my $outfd = IO
::File-
>new (">$outfile") ||
189 die "unable to open '$outfile'";
190 my $conffd = IO
::File-
>new ($conffile, 'r') ||
191 die "unable open '$conffile'";
195 while (defined (my $line = <$conffd>)) {
196 next if $line =~ m/^\#vzdump\#/; # just to be sure
197 next if $line =~ m/^\#qmdump\#/; # just to be sure
198 if ($line =~ m/^\[(.*)\]\s*$/) {
199 if ($1 =~ m/PENDING/i) {
206 next if $found_snapshot; # skip all snapshots data
207 next if $found_pending; # skip all pending changes
209 if ($line =~ m/^unused\d+:\s*(\S+)\s*/) {
210 $self->loginfo("skip unused drive '$1' (not included into backup)");
213 next if $line =~ m/^lock:/ || $line =~ m/^parent:/;
218 foreach my $di (@{$task->{disks
}}) {
219 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
220 my $storeid = $di->{storeid
} || '';
221 my $format = $di->{format
} || '';
222 print $outfd "#qmdump#map:$di->{virtdev}:$di->{qmdevice}:$storeid:$format:\n";
224 die "internal error";
228 if ($found_snapshot) {
229 $self->loginfo("snapshots found (not included into backup)");
232 if ($found_pending) {
233 $self->loginfo("pending configuration changes found (not included into backup)");
236 PVE
::Tools
::file_copy
($firewall_src, $firewall_dest) if -f
$firewall_src;
240 my ($self, $task, $vmid, $filename, $comp) = @_;
242 my $conffile = "$task->{tmpdir}/qemu-server.conf";
243 my $firewall = "$task->{tmpdir}/qemu-server.fw";
245 my $opts = $self->{vzdump
}->{opts
};
247 my $starttime = time ();
250 if ($opts->{bwlimit
}) {
251 $speed = $opts->{bwlimit
}*1024;
254 my $diskcount = scalar(@{$task->{disks
}});
256 if (PVE
::QemuConfig-
>is_template($self->{vmlist
}->{$vmid}) || !$diskcount) {
258 foreach my $di (@{$task->{disks
}}) {
259 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
260 push @pathlist, "$di->{qmdevice}=$di->{path}";
267 $self->loginfo("backup contains no disks");
272 $outcmd = "exec:$comp";
274 $outcmd = "exec:cat";
277 $outcmd .= " > $filename" if !$opts->{stdout
};
279 my $cmd = ['/usr/bin/vma', 'create', '-v', '-c', $conffile];
280 push @$cmd, '-c', $firewall if -e
$firewall;
281 push @$cmd, $outcmd, @pathlist;
283 $self->loginfo("starting template backup");
284 $self->loginfo(join(' ', @$cmd));
286 if ($opts->{stdout
}) {
287 $self->cmd($cmd, output
=> ">&=" . fileno($opts->{stdout
}));
297 foreach my $di (@{$task->{disks
}}) {
298 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
299 $devlist .= $devlist ?
",$di->{qmdevice}" : $di->{qmdevice
};
305 my $stop_after_backup;
306 my $resume_on_backup;
309 my $vm_is_running = PVE
::QemuServer
::check_running
($vmid);
310 if (!$vm_is_running) {
312 $self->loginfo("starting kvm to execute backup task");
313 PVE
::QemuServer
::vm_start
($self->{storecfg
}, $vmid, undef,
314 $skiplock, undef, 1);
315 if ($self->{vm_was_running
}) {
316 $resume_on_backup = 1;
318 $stop_after_backup = 1;
327 my $interrupt_msg = "interrupted by signal\n";
329 $SIG{INT
} = $SIG{TERM
} = $SIG{QUIT
} = $SIG{HUP
} = $SIG{PIPE
} = sub {
333 my $qmpclient = PVE
::QMPClient-
>new();
337 my $backup_cb = sub {
338 my ($vmid, $resp) = @_;
339 $uuid = $resp->{return}->{UUID
};
343 if ($opts->{stdout
}) {
344 $outfh = $opts->{stdout
};
346 $outfh = IO
::File-
>new($filename, "w") ||
347 die "unable to open file '$filename' - $!\n";
352 my @pipefd = POSIX
::pipe();
354 die "unable to fork worker - $!" if !defined($cpid);
357 POSIX
::close($pipefd[1]);
359 my $fd = fileno(STDIN
);
361 POSIX
::close(0) if $fd != 0;
362 die "unable to redirect STDIN - $!"
363 if !open(STDIN
, "<&", $pipefd[0]);
366 $fd = fileno(STDOUT
);
368 POSIX
::close (1) if $fd != 1;
370 die "unable to redirect STDOUT - $!"
371 if !open(STDOUT
, ">&", fileno($outfh));
374 die "fork compressor '$comp' failed\n";
383 POSIX
::close($pipefd[0]);
384 $outfileno = $pipefd[1];
387 $outfileno = fileno($outfh);
390 my $add_fd_cb = sub {
391 my ($vmid, $resp) = @_;
394 'backup-file' => "/dev/fdname/backup",
396 'config-file' => $conffile,
400 $params->{'firewall-file'} = $firewall if -e
$firewall;
401 $qmpclient->queue_cmd($vmid, $backup_cb, 'backup', %$params);
404 $qmpclient->queue_cmd($vmid, $add_fd_cb, 'getfd',
405 fd
=> $outfileno, fdname
=> "backup");
407 my $agent_running = 0;
409 if ($self->{vmlist
}->{$vmid}->{agent
} && $vm_is_running) {
410 $agent_running = PVE
::QemuServer
::qga_check_running
($vmid);
414 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-freeze"); };
420 eval { $qmpclient->queue_execute() };
424 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-thaw"); };
429 die $qmperr if $qmperr;
430 die $qmpclient->{errors
}->{$vmid} if $qmpclient->{errors
}->{$vmid};
433 POSIX
::close($outfileno) == 0 ||
434 die "close output file handle failed\n";
437 die "got no uuid for backup task\n" if !$uuid;
439 $self->loginfo("started backup task '$uuid'");
441 if ($resume_on_backup) {
442 if (my $stoptime = $task->{vmstoptime
}) {
443 my $delay = time() - $task->{vmstoptime
};
444 $task->{vmstoptime
} = undef; # avoid printing 'online after ..' twice
445 $self->loginfo("resuming VM again after $delay seconds");
447 $self->loginfo("resuming VM again");
449 PVE
::QemuServer
::vm_mon_cmd
($vmid, 'cont');
453 my $starttime = time ();
457 my $last_transferred = 0;
458 my $last_time = time();
462 $status = PVE
::QemuServer
::vm_mon_cmd
($vmid, 'query-backup');
463 my $total = $status->{total
} || 0;
464 $transferred = $status->{transferred
} || 0;
465 my $per = $total ?
int(($transferred * 100)/$total) : 0;
466 my $zero = $status->{'zero-bytes'} || 0;
467 my $zero_per = $total ?
int(($zero * 100)/$total) : 0;
469 die "got unexpected uuid\n" if !$status->{uuid
} || ($status->{uuid
} ne $uuid);
472 my $duration = $ctime - $starttime;
474 my $rbytes = $transferred - $last_transferred;
475 my $wbytes = $rbytes - ($zero - $last_zero);
477 my $timediff = ($ctime - $last_time) || 1; # fixme
478 my $mbps_read = ($rbytes > 0) ?
479 int(($rbytes/$timediff)/(1000*1000)) : 0;
480 my $mbps_write = ($wbytes > 0) ?
481 int(($wbytes/$timediff)/(1000*1000)) : 0;
483 my $statusline = "status: $per% ($transferred/$total), " .
484 "sparse ${zero_per}% ($zero), duration $duration, " .
485 "read/write $mbps_read/$mbps_write MB/s";
486 my $res = $status->{status
} || 'unknown';
487 if ($res ne 'active') {
488 $self->loginfo($statusline);
489 die(($status->{errmsg
} || "unknown error") . "\n")
491 die "got unexpected status '$res'\n"
493 die "got wrong number of transfered bytes ($total != $transferred)\n"
494 if ($res eq 'done') && ($total != $transferred);
498 if ($per != $last_per && ($timediff > 2)) {
499 $self->loginfo($statusline);
501 $last_total = $total if $total;
502 $last_zero = $zero if $zero;
503 $last_transferred = $transferred if $transferred;
509 my $duration = time() - $starttime;
510 if ($transferred && $duration) {
511 my $mb = int($transferred/(1000*1000));
512 my $mbps = int(($transferred/$duration)/(1000*1000));
513 $self->loginfo("transferred $mb MB in $duration seconds ($mbps MB/s)");
520 $self->loginfo("aborting backup job");
521 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, 'backup-cancel'); };
523 $self->logerr($err1);
527 if ($stop_after_backup) {
528 # stop if not running
530 my $resp = PVE
::QemuServer
::vm_mon_cmd
($vmid, 'query-status');
531 my $status = $resp && $resp->{status
} ?
$resp->{status
} : 'unknown';
532 if ($status eq 'prelaunch') {
533 $self->loginfo("stopping kvm after backup task");
534 PVE
::QemuServer
::vm_stop
($self->{storecfg
}, $vmid, $skiplock);
536 $self->loginfo("kvm status changed after backup ('$status')" .
537 " - keep VM running");
550 if ($cpid && (waitpid($cpid, 0) > 0)) {
553 my $signal = $stat & 127;
554 if ($ec || $signal) {
555 die "$comp failed - wrong exit status $ec" .
556 ($signal ?
" (signal $signal)\n" : "\n");
562 my ($self, $task, $vmid) = @_;
568 my ($self, $task, $vmid) = @_;