]>
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
;
18 use base qw
(PVE
::VZDump
::Plugin
);
21 my ($class, $vzdump) = @_;
23 PVE
::VZDump
::check_bin
('qm');
25 my $self = bless { vzdump
=> $vzdump };
27 $self->{vmlist
} = PVE
::QemuServer
::vzlist
();
28 $self->{storecfg
} = PVE
::Storage
::config
();
41 return [ keys %{$self->{vmlist
}} ];
45 my ($self, $task, $vmid, $mode) = @_;
49 my $conf = $self->{vmlist
}->{$vmid} = PVE
::QemuServer
::load_config
($vmid);
51 $self->{vm_was_running
} = 1;
52 if (!PVE
::QemuServer
::check_running
($vmid)) {
53 $self->{vm_was_running
} = 0;
56 $task->{hostname
} = $conf->{name
};
58 my $hostname = PVE
::INotify
::nodename
();
62 PVE
::QemuServer
::foreach_drive
($conf, sub {
63 my ($ds, $drive) = @_;
65 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
67 if (defined($drive->{backup
}) && $drive->{backup
} eq "no") {
68 $self->loginfo("exclude disk '$ds' (backup=no)");
72 my $volid = $drive->{file
};
74 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
75 push @$vollist, $volid if $storeid;
76 $drivehash->{$ds} = $drive;
79 PVE
::Storage
::activate_volumes
($self->{storecfg
}, $vollist);
81 foreach my $ds (sort keys %$drivehash) {
82 my $drive = $drivehash->{$ds};
84 my $volid = $drive->{file
};
88 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
90 $path = PVE
::Storage
::path
($self->{storecfg
}, $volid);
101 ($size, $format) = PVE
::Storage
::volume_size_info
($self->{storecfg
}, $volid, 5);
103 die "no such volume '$volid'\n" if $@;
105 my $diskinfo = { path
=> $path , volid
=> $volid, storeid
=> $storeid,
106 format
=> $format, virtdev
=> $ds, qmdevice
=> "drive-$ds" };
109 $diskinfo->{type
} = 'block';
111 $diskinfo->{type
} = 'file';
114 push @{$task->{disks
}}, $diskinfo;
119 my ($self, $vmid) = @_;
121 my $running = PVE
::QemuServer
::check_running
($vmid) ?
1 : 0;
123 return wantarray ?
($running, $running ?
'running' : 'stopped') : $running;
127 my ($self, $vmid) = @_;
129 $self->cmd ("qm set $vmid --lock backup");
133 my ($self, $vmid) = @_;
135 $self->cmd ("qm unlock $vmid");
139 my ($self, $task, $vmid) = @_;
141 my $opts = $self->{vzdump
}->{opts
};
143 my $wait = $opts->{stopwait
} * 60;
144 # send shutdown and wait
145 $self->cmd ("qm shutdown $vmid --skiplock --keepActive --timeout $wait");
149 my ($self, $task, $vmid) = @_;
151 $self->cmd ("qm start $vmid --skiplock");
155 my ($self, $task, $vmid) = @_;
157 $self->cmd ("qm suspend $vmid --skiplock");
161 my ($self, $task, $vmid) = @_;
163 $self->cmd ("qm resume $vmid --skiplock");
167 my ($self, $task, $vmid) = @_;
169 my $conffile = PVE
::QemuServer
::config_file
($vmid);
171 my $outfile = "$task->{tmpdir}/qemu-server.conf";
178 $outfd = IO
::File-
>new (">$outfile") ||
179 die "unable to open '$outfile'";
180 $conffd = IO
::File-
>new ($conffile, 'r') ||
181 die "unable open '$conffile'";
184 while (defined (my $line = <$conffd>)) {
185 next if $line =~ m/^\#vzdump\#/; # just to be sure
186 next if $line =~ m/^\#qmdump\#/; # just to be sure
187 if ($line =~ m/^\[.*\]\s*$/) {
190 next if $found_snapshot; # skip all snapshots data
191 if ($line =~ m/^unused\d+:\s*(\S+)\s*/) {
192 $self->loginfo("skip unused drive '$1' (not included into backup)");
195 next if $line =~ m/^lock:/ || $line =~ m/^parent:/;
200 foreach my $di (@{$task->{disks
}}) {
201 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
202 my $storeid = $di->{storeid
} || '';
203 my $format = $di->{format
} || '';
204 print $outfd "#qmdump#map:$di->{virtdev}:$di->{qmdevice}:$storeid:$format:\n";
206 die "internal error";
210 if ($found_snapshot) {
211 $self->loginfo("snapshots found (not included into backup)");
216 close ($outfd) if $outfd;
217 close ($conffd) if $conffd;
223 my ($self, $task, $vmid, $filename, $comp) = @_;
225 my $conffile = "$task->{tmpdir}/qemu-server.conf";
227 my $opts = $self->{vzdump
}->{opts
};
229 my $starttime = time ();
232 if ($opts->{bwlimit
}) {
233 $speed = $opts->{bwlimit
}*1024;
236 my $diskcount = scalar(@{$task->{disks
}});
238 if (PVE
::QemuServer
::is_template
($self->{vmlist
}->{$vmid}) || !$diskcount) {
240 foreach my $di (@{$task->{disks
}}) {
241 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
242 push @pathlist, "$di->{qmdevice}=$di->{path}";
249 $self->loginfo("backup contains no disks");
254 $outcmd = "exec:$comp";
256 $outcmd = "exec:cat";
259 $outcmd .= ">$filename" if !$opts->{stdout
};
261 my $cmd = ['/usr/bin/vma', 'create', '-v', '-c', $conffile, $outcmd, @pathlist];
263 $self->loginfo("starting template backup");
264 $self->loginfo(join(' ', @$cmd));
266 if ($opts->{stdout
}) {
267 $self->cmd($cmd, output
=> ">&=" . fileno($opts->{stdout
}));
277 foreach my $di (@{$task->{disks
}}) {
278 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
279 $devlist .= $devlist ?
",$di->{qmdevice}" : $di->{qmdevice
};
285 my $stop_after_backup;
286 my $resume_on_backup;
289 my $vm_is_running = PVE
::QemuServer
::check_running
($vmid);
290 if (!$vm_is_running) {
292 $self->loginfo("starting kvm to execute backup task");
293 PVE
::QemuServer
::vm_start
($self->{storecfg
}, $vmid, undef,
294 $skiplock, undef, 1);
295 if ($self->{vm_was_running
}) {
296 $resume_on_backup = 1;
298 $stop_after_backup = 1;
307 my $interrupt_msg = "interrupted by signal\n";
309 $SIG{INT
} = $SIG{TERM
} = $SIG{QUIT
} = $SIG{HUP
} = $SIG{PIPE
} = sub {
313 my $qmpclient = PVE
::QMPClient-
>new();
317 my $backup_cb = sub {
318 my ($vmid, $resp) = @_;
319 $uuid = $resp->{return};
323 if ($opts->{stdout
}) {
324 $outfh = $opts->{stdout
};
326 $outfh = IO
::File-
>new($filename, "w") ||
327 die "unable to open file '$filename' - $!\n";
332 my @pipefd = POSIX
::pipe();
334 die "unable to fork worker - $!" if !defined($cpid);
337 POSIX
::close($pipefd[1]);
339 my $fd = fileno(STDIN
);
341 POSIX
::close(0) if $fd != 0;
342 die "unable to redirect STDIN - $!"
343 if !open(STDIN
, "<&", $pipefd[0]);
346 $fd = fileno(STDOUT
);
348 POSIX
::close (1) if $fd != 1;
350 die "unable to redirect STDOUT - $!"
351 if !open(STDOUT
, ">&", fileno($outfh));
354 die "fork compressor '$comp' failed\n";
363 POSIX
::close($pipefd[0]);
364 $outfileno = $pipefd[1];
367 $outfileno = fileno($outfh);
370 my $add_fd_cb = sub {
371 my ($vmid, $resp) = @_;
373 $qmpclient->queue_cmd($vmid, $backup_cb, 'backup',
374 'backup-file' => "/dev/fdname/backup",
376 'config-file' => $conffile,
377 devlist
=> $devlist);
381 $qmpclient->queue_cmd($vmid, $add_fd_cb, 'getfd',
382 fd
=> $outfileno, fdname
=> "backup");
384 if ($self->{vmlist
}->{$vmid}->{agent
} && $vm_is_running){
385 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-freeze"); };
391 $qmpclient->queue_execute();
393 if ($self->{vmlist
}->{$vmid}->{agent
} && $vm_is_running ){
394 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-thaw"); };
399 die $qmpclient->{errors
}->{$vmid} if $qmpclient->{errors
}->{$vmid};
402 POSIX
::close($outfileno) == 0 ||
403 die "close output file handle failed\n";
406 die "got no uuid for backup task\n" if !$uuid;
408 $self->loginfo("started backup task '$uuid'");
410 if ($resume_on_backup) {
411 $self->loginfo("resume VM");
412 PVE
::QemuServer
::vm_mon_cmd
($vmid, 'cont');
416 my $starttime = time ();
420 my $last_transferred = 0;
421 my $last_time = time();
425 $status = PVE
::QemuServer
::vm_mon_cmd
($vmid, 'query-backup');
426 my $total = $status->{total
} || 0;
427 $transferred = $status->{transferred
} || 0;
428 my $per = $total ?
int(($transferred * 100)/$total) : 0;
429 my $zero = $status->{'zero-bytes'} || 0;
430 my $zero_per = $total ?
int(($zero * 100)/$total) : 0;
432 die "got unexpected uuid\n" if !$status->{uuid
} || ($status->{uuid
} ne $uuid);
435 my $duration = $ctime - $starttime;
437 my $rbytes = $transferred - $last_transferred;
438 my $wbytes = $rbytes - ($zero - $last_zero);
440 my $timediff = ($ctime - $last_time) || 1; # fixme
441 my $mbps_read = ($rbytes > 0) ?
442 int(($rbytes/$timediff)/(1000*1000)) : 0;
443 my $mbps_write = ($wbytes > 0) ?
444 int(($wbytes/$timediff)/(1000*1000)) : 0;
446 my $statusline = "status: $per% ($transferred/$total), " .
447 "sparse ${zero_per}% ($zero), duration $duration, " .
448 "$mbps_read/$mbps_write MB/s";
449 my $res = $status->{status
} || 'unknown';
450 if ($res ne 'active') {
451 $self->loginfo($statusline);
452 die(($status->{errmsg
} || "unknown error") . "\n")
454 die "got unexpected status '$res'\n"
456 die "got wrong number of transfered bytes ($total != $transferred)\n"
457 if ($res eq 'done') && ($total != $transferred);
461 if ($per != $last_per && ($timediff > 2)) {
462 $self->loginfo($statusline);
464 $last_total = $total if $total;
465 $last_zero = $zero if $zero;
466 $last_transferred = $transferred if $transferred;
472 my $duration = time() - $starttime;
473 if ($transferred && $duration) {
474 my $mb = int($transferred/(1000*1000));
475 my $mbps = int(($transferred/$duration)/(1000*1000));
476 $self->loginfo("transferred $mb MB in $duration seconds ($mbps MB/s)");
483 $self->loginfo("aborting backup job");
484 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, 'backup-cancel'); };
486 $self->logerr($err1);
490 if ($stop_after_backup) {
491 # stop if not running
493 my $resp = PVE
::QemuServer
::vm_mon_cmd
($vmid, 'query-status');
494 my $status = $resp && $resp->{status
} ?
$resp->{status
} : 'unknown';
495 if ($status eq 'prelaunch') {
496 $self->loginfo("stopping kvm after backup task");
497 PVE
::QemuServer
::vm_stop
($self->{storecfg
}, $vmid, $skiplock);
499 $self->loginfo("kvm status changed after backup ('$status')" .
500 " - keep VM running");
513 if ($cpid && (waitpid($cpid, 0) > 0)) {
516 my $signal = $stat & 127;
517 if ($ec || $signal) {
518 die "$comp failed - wrong exit status $ec" .
519 ($signal ?
" (signal $signal)\n" : "\n");
525 my ($self, $task, $vmid) = @_;
531 my ($self, $task, $vmid) = @_;