]>
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;
237 foreach my $di (@{$task->{disks
}}) {
238 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
239 $devlist .= $devlist ?
",$di->{qmdevice}" : $di->{qmdevice
};
245 my $stop_after_backup;
246 my $resume_on_backup;
250 if (!PVE
::QemuServer
::check_running
($vmid)) {
252 $self->loginfo("starting kvm to execute backup task");
253 PVE
::QemuServer
::vm_start
($self->{storecfg
}, $vmid, undef,
254 $skiplock, undef, 1);
255 if ($self->{vm_was_running
}) {
256 $resume_on_backup = 1;
258 $stop_after_backup = 1;
267 my $interrupt_msg = "interrupted by signal\n";
269 $SIG{INT
} = $SIG{TERM
} = $SIG{QUIT
} = $SIG{HUP
} = $SIG{PIPE
} = sub {
273 my $qmpclient = PVE
::QMPClient-
>new();
277 my $backup_cb = sub {
278 my ($vmid, $resp) = @_;
279 $uuid = $resp->{return};
283 if ($opts->{stdout
}) {
284 $outfh = $opts->{stdout
};
286 $outfh = IO
::File-
>new($filename, "w") ||
287 die "unable to open file '$filename' - $!\n";
292 my @pipefd = POSIX
::pipe();
294 die "unable to fork worker - $!" if !defined($cpid);
297 POSIX
::close($pipefd[1]);
299 my $fd = fileno(STDIN
);
301 POSIX
::close(0) if $fd != 0;
302 die "unable to redirect STDIN - $!"
303 if !open(STDIN
, "<&", $pipefd[0]);
306 $fd = fileno(STDOUT
);
308 POSIX
::close (1) if $fd != 1;
310 die "unable to redirect STDOUT - $!"
311 if !open(STDOUT
, ">&", fileno($outfh));
314 die "fork compressor '$comp' failed\n";
323 POSIX
::close($pipefd[0]);
324 $outfileno = $pipefd[1];
327 $outfileno = fileno($outfh);
330 my $add_fd_cb = sub {
331 my ($vmid, $resp) = @_;
333 $qmpclient->queue_cmd($vmid, $backup_cb, 'backup',
334 'backup-file' => "/dev/fdname/backup",
336 'config-file' => $conffile,
337 devlist
=> $devlist);
341 $qmpclient->queue_cmd($vmid, $add_fd_cb, 'getfd',
342 fd
=> $outfileno, fdname
=> "backup");
343 $qmpclient->queue_execute();
345 die $qmpclient->{errors
}->{$vmid} if $qmpclient->{errors
}->{$vmid};
348 POSIX
::close($outfileno) == 0 ||
349 die "close output file handle failed\n";
352 die "got no uuid for backup task\n" if !$uuid;
354 $self->loginfo("started backup task '$uuid'");
356 if ($resume_on_backup) {
357 $self->loginfo("resume VM");
358 PVE
::QemuServer
::vm_mon_cmd
($vmid, 'cont');
362 my $starttime = time ();
366 my $last_transferred = 0;
367 my $last_time = time();
371 $status = PVE
::QemuServer
::vm_mon_cmd
($vmid, 'query-backup');
372 my $total = $status->{total
};
373 $transferred = $status->{transferred
};
374 my $per = $total ?
int(($transferred * 100)/$total) : 0;
375 my $zero = $status->{'zero-bytes'} || 0;
376 my $zero_per = $total ?
int(($zero * 100)/$total) : 0;
378 die "got unexpected uuid\n" if $status->{uuid
} ne $uuid;
381 my $duration = $ctime - $starttime;
383 my $rbytes = $transferred - $last_transferred;
384 my $wbytes = $rbytes - ($zero - $last_zero);
386 my $timediff = ($ctime - $last_time) || 1; # fixme
387 my $mbps_read = ($rbytes > 0) ?
388 int(($rbytes/$timediff)/(1000*1000)) : 0;
389 my $mbps_write = ($wbytes > 0) ?
390 int(($wbytes/$timediff)/(1000*1000)) : 0;
392 my $statusline = "status: $per% ($transferred/$total), " .
393 "sparse ${zero_per}% ($zero), duration $duration, " .
394 "$mbps_read/$mbps_write MB/s";
395 if ($status->{status
} ne 'active') {
396 $self->loginfo($statusline);
397 die(($status->{errmsg
} || "unknown error") . "\n")
398 if $status->{status
} eq 'error';
401 if ($per != $last_per && ($timediff > 2)) {
402 $self->loginfo($statusline);
404 $last_total = $total if $total;
405 $last_zero = $zero if $zero;
406 $last_transferred = $transferred if $transferred;
412 my $duration = time() - $starttime;
413 if ($transferred && $duration) {
414 my $mb = int($transferred/(1000*1000));
415 my $mbps = int(($transferred/$duration)/(1000*1000));
416 $self->loginfo("transferred $mb MB in $duration seconds ($mbps MB/s)");
422 $self->loginfo("aborting backup job");
423 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, 'backup-cancel'); };
427 if ($stop_after_backup) {
428 # stop if not running
430 my $resp = PVE
::QemuServer
::vm_mon_cmd
($vmid, 'query-status');
431 my $status = $resp && $resp->{status
} ?
$resp->{status
} : 'unknown';
432 if ($status eq 'prelaunch') {
433 $self->loginfo("stopping kvm after backup task");
434 PVE
::QemuServer
::vm_stop
($self->{storecfg
}, $vmid, $skiplock);
436 $self->loginfo("kvm status changed after backup ('$status')" .
437 " - keep VM running");
450 if ($cpid && (waitpid($cpid, 0) > 0)) {
453 my $signal = $stat & 127;
454 if ($ec || $signal) {
455 die "$comp failed - wrong exit status $ec" .
456 ($signal ?
" (signal $signal)\n" : "\n");
462 my ($self, $task, $vmid) = @_;
468 my ($self, $task, $vmid) = @_;