]>
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);
97 die "no such volume '$volid'\n" if ! -e
$path;
99 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($path);
101 my $diskinfo = { path
=> $path , volid
=> $volid, storeid
=> $storeid,
102 format
=> $format, virtdev
=> $ds, qmdevice
=> "drive-$ds" };
105 $diskinfo->{type
} = 'block';
107 $diskinfo->{type
} = 'file';
110 push @{$task->{disks
}}, $diskinfo;
115 my ($self, $vmid) = @_;
117 my $running = PVE
::QemuServer
::check_running
($vmid) ?
1 : 0;
119 return wantarray ?
($running, $running ?
'running' : 'stopped') : $running;
123 my ($self, $vmid) = @_;
125 $self->cmd ("qm set $vmid --lock backup");
129 my ($self, $vmid) = @_;
131 $self->cmd ("qm unlock $vmid");
135 my ($self, $task, $vmid) = @_;
137 my $opts = $self->{vzdump
}->{opts
};
139 my $wait = $opts->{stopwait
} * 60;
140 # send shutdown and wait
141 $self->cmd ("qm shutdown $vmid --skiplock --keepActive --timeout $wait");
145 my ($self, $task, $vmid) = @_;
147 $self->cmd ("qm start $vmid --skiplock");
151 my ($self, $task, $vmid) = @_;
153 $self->cmd ("qm suspend $vmid --skiplock");
157 my ($self, $task, $vmid) = @_;
159 $self->cmd ("qm resume $vmid --skiplock");
163 my ($self, $task, $vmid) = @_;
165 my $conffile = PVE
::QemuServer
::config_file
($vmid);
167 my $outfile = "$task->{tmpdir}/qemu-server.conf";
174 $outfd = IO
::File-
>new (">$outfile") ||
175 die "unable to open '$outfile'";
176 $conffd = IO
::File-
>new ($conffile, 'r') ||
177 die "unable open '$conffile'";
180 while (defined (my $line = <$conffd>)) {
181 next if $line =~ m/^\#vzdump\#/; # just to be sure
182 next if $line =~ m/^\#qmdump\#/; # just to be sure
183 if ($line =~ m/^\[.*\]\s*$/) {
186 next if $found_snapshot; # skip all snapshots data
187 if ($line =~ m/^unused\d+:\s*(\S+)\s*/) {
188 $self->loginfo("skip unused drive '$1' (not included into backup)");
191 next if $line =~ m/^lock:/ || $line =~ m/^parent:/;
196 foreach my $di (@{$task->{disks
}}) {
197 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
198 my $storeid = $di->{storeid
} || '';
199 my $format = $di->{format
} || '';
200 print $outfd "#qmdump#map:$di->{virtdev}:$di->{qmdevice}:$storeid:$format:\n";
202 die "internal error";
206 if ($found_snapshot) {
207 $self->loginfo("snapshots found (not included into backup)");
212 close ($outfd) if $outfd;
213 close ($conffd) if $conffd;
219 my ($self, $task, $vmid, $filename, $comp) = @_;
221 my $conffile = "$task->{tmpdir}/qemu-server.conf";
223 my $opts = $self->{vzdump
}->{opts
};
225 my $starttime = time ();
228 if ($opts->{bwlimit
}) {
229 $speed = $opts->{bwlimit
}*1024;
233 foreach my $di (@{$task->{disks
}}) {
234 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
235 $devlist .= $devlist ?
",$di->{qmdevice}" : $di->{qmdevice
};
241 my $stop_after_backup;
242 my $resume_on_backup;
246 if (!PVE
::QemuServer
::check_running
($vmid)) {
248 $self->loginfo("starting kvm to execute backup task");
249 PVE
::QemuServer
::vm_start
($self->{storecfg
}, $vmid, undef,
250 $skiplock, undef, 1);
251 if ($self->{vm_was_running
}) {
252 $resume_on_backup = 1;
254 $stop_after_backup = 1;
263 my $interrupt_msg = "interrupted by signal\n";
265 $SIG{INT
} = $SIG{TERM
} = $SIG{QUIT
} = $SIG{HUP
} = $SIG{PIPE
} = sub {
269 my $qmpclient = PVE
::QMPClient-
>new();
273 my $backup_cb = sub {
274 my ($vmid, $resp) = @_;
275 $uuid = $resp->{return};
279 if ($opts->{stdout
}) {
280 $outfh = $opts->{stdout
};
282 $outfh = IO
::File-
>new($filename, "w") ||
283 die "unable to open file '$filename' - $!\n";
288 my @pipefd = POSIX
::pipe();
290 die "unable to fork worker - $!" if !defined($cpid);
293 POSIX
::close($pipefd[1]);
295 my $fd = fileno(STDIN
);
297 POSIX
::close(0) if $fd != 0;
298 die "unable to redirect STDIN - $!"
299 if !open(STDIN
, "<&", $pipefd[0]);
302 $fd = fileno(STDOUT
);
304 POSIX
::close (1) if $fd != 1;
306 die "unable to redirect STDOUT - $!"
307 if !open(STDOUT
, ">&", fileno($outfh));
310 die "fork compressor '$comp' failed\n";
319 POSIX
::close($pipefd[0]);
320 $outfileno = $pipefd[1];
323 $outfileno = fileno($outfh);
326 my $add_fd_cb = sub {
327 my ($vmid, $resp) = @_;
329 $qmpclient->queue_cmd($vmid, $backup_cb, 'backup',
330 backupfile
=> "/dev/fdname/backup",
332 'config-filename' => $conffile,
333 devlist
=> $devlist);
337 $qmpclient->queue_cmd($vmid, $add_fd_cb, 'getfd',
338 fd
=> $outfileno, fdname
=> "backup");
339 $qmpclient->queue_execute();
341 die $qmpclient->{errors
}->{$vmid} if $qmpclient->{errors
}->{$vmid};
344 POSIX
::close($outfileno) == 0 ||
345 die "close output file handle failed\n";
348 die "got no uuid for backup task\n" if !$uuid;
350 $self->loginfo("started backup task '$uuid'");
352 if ($resume_on_backup) {
353 $self->loginfo("resume VM");
354 PVE
::QemuServer
::vm_mon_cmd
($vmid, 'cont');
358 my $starttime = time ();
362 my $last_transferred = 0;
363 my $last_time = time();
367 $status = PVE
::QemuServer
::vm_mon_cmd
($vmid, 'query-backup');
368 my $total = $status->{total
};
369 $transferred = $status->{transferred
};
370 my $per = $total ?
int(($transferred * 100)/$total) : 0;
371 my $zero = $status->{'zero-bytes'} || 0;
372 my $zero_per = $total ?
int(($zero * 100)/$total) : 0;
374 die "got unexpected uuid\n" if $status->{uuid
} ne $uuid;
377 my $duration = $ctime - $starttime;
379 my $rbytes = $transferred - $last_transferred;
380 my $wbytes = $rbytes - ($zero - $last_zero);
382 my $timediff = ($ctime - $last_time) || 1; # fixme
383 my $mbps_read = ($rbytes > 0) ?
384 int(($rbytes/$timediff)/(1000*1000)) : 0;
385 my $mbps_write = ($wbytes > 0) ?
386 int(($wbytes/$timediff)/(1000*1000)) : 0;
388 my $statusline = "status: $per% ($transferred/$total), " .
389 "sparse ${zero_per}% ($zero), duration $duration, " .
390 "$mbps_read/$mbps_write MB/s";
391 if ($status->{status
} ne 'active') {
392 $self->loginfo($statusline);
393 die(($status->{errmsg
} || "unknown error") . "\n")
394 if $status->{status
} eq 'error';
397 if ($per != $last_per && ($timediff > 2)) {
398 $self->loginfo($statusline);
400 $last_total = $total if $total;
401 $last_zero = $zero if $zero;
402 $last_transferred = $transferred if $transferred;
408 my $duration = time() - $starttime;
409 if ($transferred && $duration) {
410 my $mb = int($transferred/(1000*1000));
411 my $mbps = int(($transferred/$duration)/(1000*1000));
412 $self->loginfo("transferred $mb MB in $duration seconds ($mbps MB/s)");
418 $self->loginfo("aborting backup job");
419 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, 'backup_cancel'); };
423 if ($stop_after_backup) {
424 # stop if not running
426 my $resp = PVE
::QemuServer
::vm_mon_cmd
($vmid, 'query-status');
427 my $status = $resp && $resp->{status
} ?
$resp->{status
} : 'unknown';
428 if ($status eq 'prelaunch') {
429 $self->loginfo("stopping kvm after backup task");
430 PVE
::QemuServer
::vm_stop
($self->{storecfg
}, $vmid, $skiplock);
432 $self->loginfo("kvm status changed after backup ('$status')" .
433 " - keep VM running");
446 if ($cpid && (waitpid($cpid, 0) > 0)) {
449 my $signal = $stat & 127;
450 if ($ec || $signal) {
451 die "$comp failed - wrong exit status $ec" .
452 ($signal ?
" (signal $signal)\n" : "\n");
458 my ($self, $task, $vmid) = @_;
464 my ($self, $task, $vmid) = @_;