]> git.proxmox.com Git - qemu-server.git/blob - PVE/VZDump/QemuServer.pm
fix backup parameters for pve-qemu-kvm 1.4-4
[qemu-server.git] / PVE / VZDump / QemuServer.pm
1 package PVE::VZDump::QemuServer;
2
3 use strict;
4 use warnings;
5 use File::Path;
6 use File::Basename;
7 use PVE::INotify;
8 use PVE::VZDump;
9 use PVE::IPCC;
10 use PVE::Cluster qw(cfs_read_file);
11 use PVE::Tools;
12 use PVE::Storage::Plugin;
13 use PVE::Storage;
14 use PVE::QemuServer;
15 use IO::File;
16 use IPC::Open3;
17
18 use base qw (PVE::VZDump::Plugin);
19
20 sub new {
21 my ($class, $vzdump) = @_;
22
23 PVE::VZDump::check_bin('qm');
24
25 my $self = bless { vzdump => $vzdump };
26
27 $self->{vmlist} = PVE::QemuServer::vzlist();
28 $self->{storecfg} = PVE::Storage::config();
29
30 return $self;
31 };
32
33
34 sub type {
35 return 'qemu';
36 }
37
38 sub vmlist {
39 my ($self) = @_;
40
41 return [ keys %{$self->{vmlist}} ];
42 }
43
44 sub prepare {
45 my ($self, $task, $vmid, $mode) = @_;
46
47 $task->{disks} = [];
48
49 my $conf = $self->{vmlist}->{$vmid} = PVE::QemuServer::load_config($vmid);
50
51 $self->{vm_was_running} = 1;
52 if (!PVE::QemuServer::check_running($vmid)) {
53 $self->{vm_was_running} = 0;
54 }
55
56 $task->{hostname} = $conf->{name};
57
58 my $hostname = PVE::INotify::nodename();
59
60 my $vollist = [];
61 my $drivehash = {};
62 PVE::QemuServer::foreach_drive($conf, sub {
63 my ($ds, $drive) = @_;
64
65 return if PVE::QemuServer::drive_is_cdrom($drive);
66
67 if (defined($drive->{backup}) && $drive->{backup} eq "no") {
68 $self->loginfo("exclude disk '$ds' (backup=no)");
69 return;
70 }
71
72 my $volid = $drive->{file};
73
74 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
75 push @$vollist, $volid if $storeid;
76 $drivehash->{$ds} = $drive;
77 });
78
79 PVE::Storage::activate_volumes($self->{storecfg}, $vollist);
80
81 foreach my $ds (sort keys %$drivehash) {
82 my $drive = $drivehash->{$ds};
83
84 my $volid = $drive->{file};
85
86 my $path;
87
88 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
89 if ($storeid) {
90 $path = PVE::Storage::path($self->{storecfg}, $volid);
91 } else {
92 $path = $volid;
93 }
94
95 next if !$path;
96
97 my $format = undef;
98 my $size = undef;
99
100 eval{
101 ($size, $format) = PVE::Storage::volume_size_info($self->{storecfg}, $volid, 5);
102 };
103 die "no such volume '$volid'\n" if $@;
104
105 my $diskinfo = { path => $path , volid => $volid, storeid => $storeid,
106 format => $format, virtdev => $ds, qmdevice => "drive-$ds" };
107
108 if (-b $path) {
109 $diskinfo->{type} = 'block';
110 } else {
111 $diskinfo->{type} = 'file';
112 }
113
114 push @{$task->{disks}}, $diskinfo;
115 }
116 }
117
118 sub vm_status {
119 my ($self, $vmid) = @_;
120
121 my $running = PVE::QemuServer::check_running($vmid) ? 1 : 0;
122
123 return wantarray ? ($running, $running ? 'running' : 'stopped') : $running;
124 }
125
126 sub lock_vm {
127 my ($self, $vmid) = @_;
128
129 $self->cmd ("qm set $vmid --lock backup");
130 }
131
132 sub unlock_vm {
133 my ($self, $vmid) = @_;
134
135 $self->cmd ("qm unlock $vmid");
136 }
137
138 sub stop_vm {
139 my ($self, $task, $vmid) = @_;
140
141 my $opts = $self->{vzdump}->{opts};
142
143 my $wait = $opts->{stopwait} * 60;
144 # send shutdown and wait
145 $self->cmd ("qm shutdown $vmid --skiplock --keepActive --timeout $wait");
146 }
147
148 sub start_vm {
149 my ($self, $task, $vmid) = @_;
150
151 $self->cmd ("qm start $vmid --skiplock");
152 }
153
154 sub suspend_vm {
155 my ($self, $task, $vmid) = @_;
156
157 $self->cmd ("qm suspend $vmid --skiplock");
158 }
159
160 sub resume_vm {
161 my ($self, $task, $vmid) = @_;
162
163 $self->cmd ("qm resume $vmid --skiplock");
164 }
165
166 sub assemble {
167 my ($self, $task, $vmid) = @_;
168
169 my $conffile = PVE::QemuServer::config_file ($vmid);
170
171 my $outfile = "$task->{tmpdir}/qemu-server.conf";
172
173 my $outfd;
174 my $conffd;
175
176 eval {
177
178 $outfd = IO::File->new (">$outfile") ||
179 die "unable to open '$outfile'";
180 $conffd = IO::File->new ($conffile, 'r') ||
181 die "unable open '$conffile'";
182
183 my $found_snapshot;
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*$/) {
188 $found_snapshot = 1;
189 }
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)");
193 next;
194 }
195 next if $line =~ m/^lock:/ || $line =~ m/^parent:/;
196
197 print $outfd $line;
198 }
199
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";
205 } else {
206 die "internal error";
207 }
208 }
209
210 if ($found_snapshot) {
211 $self->loginfo("snapshots found (not included into backup)");
212 }
213 };
214 my $err = $@;
215
216 close ($outfd) if $outfd;
217 close ($conffd) if $conffd;
218
219 die $err if $err;
220 }
221
222 sub archive {
223 my ($self, $task, $vmid, $filename, $comp) = @_;
224
225 my $conffile = "$task->{tmpdir}/qemu-server.conf";
226
227 my $opts = $self->{vzdump}->{opts};
228
229 my $starttime = time ();
230
231 my $speed = 0;
232 if ($opts->{bwlimit}) {
233 $speed = $opts->{bwlimit}*1024;
234 }
235
236 my $devlist = '';
237 foreach my $di (@{$task->{disks}}) {
238 if ($di->{type} eq 'block' || $di->{type} eq 'file') {
239 $devlist .= $devlist ? ",$di->{qmdevice}" : $di->{qmdevice};
240 } else {
241 die "implement me";
242 }
243 }
244
245 my $stop_after_backup;
246 my $resume_on_backup;
247
248 my $skiplock = 1;
249
250 if (!PVE::QemuServer::check_running($vmid)) {
251 eval {
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;
257 } else {
258 $stop_after_backup = 1;
259 }
260 };
261 if (my $err = $@) {
262 die $err;
263 }
264 }
265
266 my $cpid;
267 my $interrupt_msg = "interrupted by signal\n";
268 eval {
269 $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub {
270 die $interrupt_msg;
271 };
272
273 my $qmpclient = PVE::QMPClient->new();
274
275 my $uuid;
276
277 my $backup_cb = sub {
278 my ($vmid, $resp) = @_;
279 $uuid = $resp->{return};
280 };
281
282 my $outfh;
283 if ($opts->{stdout}) {
284 $outfh = $opts->{stdout};
285 } else {
286 $outfh = IO::File->new($filename, "w") ||
287 die "unable to open file '$filename' - $!\n";
288 }
289
290 my $outfileno;
291 if ($comp) {
292 my @pipefd = POSIX::pipe();
293 $cpid = fork();
294 die "unable to fork worker - $!" if !defined($cpid);
295 if ($cpid == 0) {
296 eval {
297 POSIX::close($pipefd[1]);
298 # redirect STDIN
299 my $fd = fileno(STDIN);
300 close STDIN;
301 POSIX::close(0) if $fd != 0;
302 die "unable to redirect STDIN - $!"
303 if !open(STDIN, "<&", $pipefd[0]);
304
305 # redirect STDOUT
306 $fd = fileno(STDOUT);
307 close STDOUT;
308 POSIX::close (1) if $fd != 1;
309
310 die "unable to redirect STDOUT - $!"
311 if !open(STDOUT, ">&", fileno($outfh));
312
313 exec($comp);
314 die "fork compressor '$comp' failed\n";
315 };
316 if (my $err = $@) {
317 warn $err;
318 POSIX::_exit(1);
319 }
320 POSIX::_exit(0);
321 kill(-9, $$);
322 } else {
323 POSIX::close($pipefd[0]);
324 $outfileno = $pipefd[1];
325 }
326 } else {
327 $outfileno = fileno($outfh);
328 }
329
330 my $add_fd_cb = sub {
331 my ($vmid, $resp) = @_;
332
333 $qmpclient->queue_cmd($vmid, $backup_cb, 'backup',
334 'backup-file' => "/dev/fdname/backup",
335 speed => $speed,
336 'config-file' => $conffile,
337 devlist => $devlist);
338 };
339
340
341 $qmpclient->queue_cmd($vmid, $add_fd_cb, 'getfd',
342 fd => $outfileno, fdname => "backup");
343 $qmpclient->queue_execute();
344
345 die $qmpclient->{errors}->{$vmid} if $qmpclient->{errors}->{$vmid};
346
347 if ($cpid) {
348 POSIX::close($outfileno) == 0 ||
349 die "close output file handle failed\n";
350 }
351
352 die "got no uuid for backup task\n" if !$uuid;
353
354 $self->loginfo("started backup task '$uuid'");
355
356 if ($resume_on_backup) {
357 $self->loginfo("resume VM");
358 PVE::QemuServer::vm_mon_cmd($vmid, 'cont');
359 }
360
361 my $status;
362 my $starttime = time ();
363 my $last_per = -1;
364 my $last_total = 0;
365 my $last_zero = 0;
366 my $last_transferred = 0;
367 my $last_time = time();
368 my $transferred;
369
370 while(1) {
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;
377
378 die "got unexpected uuid\n" if $status->{uuid} ne $uuid;
379
380 my $ctime = time();
381 my $duration = $ctime - $starttime;
382
383 my $rbytes = $transferred - $last_transferred;
384 my $wbytes = $rbytes - ($zero - $last_zero);
385
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;
391
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';
399 last;
400 }
401 if ($per != $last_per && ($timediff > 2)) {
402 $self->loginfo($statusline);
403 $last_per = $per;
404 $last_total = $total if $total;
405 $last_zero = $zero if $zero;
406 $last_transferred = $transferred if $transferred;
407 $last_time = $ctime;
408 }
409 sleep(1);
410 }
411
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)");
417 }
418 };
419 my $err = $@;
420
421 if ($err) {
422 $self->loginfo("aborting backup job");
423 eval { PVE::QemuServer::vm_mon_cmd($vmid, 'backup-cancel'); };
424 warn $@ if $@;
425 }
426
427 if ($stop_after_backup) {
428 # stop if not running
429 eval {
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);
435 } else {
436 $self->loginfo("kvm status changed after backup ('$status')" .
437 " - keep VM running");
438 }
439 }
440 }
441
442 if ($err) {
443 if ($cpid) {
444 kill(-9, $cpid);
445 waitpid($cpid, 0);
446 }
447 die $err;
448 }
449
450 if ($cpid && (waitpid($cpid, 0) > 0)) {
451 my $stat = $?;
452 my $ec = $stat >> 8;
453 my $signal = $stat & 127;
454 if ($ec || $signal) {
455 die "$comp failed - wrong exit status $ec" .
456 ($signal ? " (signal $signal)\n" : "\n");
457 }
458 }
459 }
460
461 sub snapshot {
462 my ($self, $task, $vmid) = @_;
463
464 # nothing to do
465 }
466
467 sub cleanup {
468 my ($self, $task, $vmid) = @_;
469
470 # nothing to do ?
471 }
472
473 1;