]> git.proxmox.com Git - qemu-server.git/blob - PVE/VZDump/QemuServer.pm
6a58a79e30cafad760dc659c2e92465976b2dbed
[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 }, $class;
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::QemuConfig->load_config($vmid);
50
51 $self->loginfo("VM Name: $conf->{name}")
52 if defined($conf->{name});
53
54 $self->{vm_was_running} = 1;
55 if (!PVE::QemuServer::check_running($vmid)) {
56 $self->{vm_was_running} = 0;
57 }
58
59 $task->{hostname} = $conf->{name};
60
61 my $hostname = PVE::INotify::nodename();
62
63 my $vollist = [];
64 my $drivehash = {};
65 PVE::QemuServer::foreach_drive($conf, sub {
66 my ($ds, $drive) = @_;
67
68 return if PVE::QemuServer::drive_is_cdrom($drive);
69
70 my $volid = $drive->{file};
71
72 if (defined($drive->{backup}) && !$drive->{backup}) {
73 $self->loginfo("exclude disk '$ds' '$volid' (backup=no)");
74 return;
75 } elsif ($drive->{iothread}) {
76 die "disk '$ds' '$volid' (iothread=on) can't use backup feature currently. Please set backup=no for this drive";
77 } else {
78 $self->loginfo("include disk '$ds' '$volid'");
79 }
80
81 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
82 push @$vollist, $volid if $storeid;
83 $drivehash->{$ds} = $drive;
84 });
85
86 PVE::Storage::activate_volumes($self->{storecfg}, $vollist);
87
88 foreach my $ds (sort keys %$drivehash) {
89 my $drive = $drivehash->{$ds};
90
91 my $volid = $drive->{file};
92
93 my $path;
94
95 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
96 if ($storeid) {
97 $path = PVE::Storage::path($self->{storecfg}, $volid);
98 } else {
99 $path = $volid;
100 }
101
102 next if !$path;
103
104 my $format = undef;
105 my $size = undef;
106
107 eval{
108 ($size, $format) = PVE::Storage::volume_size_info($self->{storecfg}, $volid, 5);
109 };
110 die "no such volume '$volid'\n" if $@;
111
112 my $diskinfo = { path => $path , volid => $volid, storeid => $storeid,
113 format => $format, virtdev => $ds, qmdevice => "drive-$ds" };
114
115 if (-b $path) {
116 $diskinfo->{type} = 'block';
117 } else {
118 $diskinfo->{type} = 'file';
119 }
120
121 push @{$task->{disks}}, $diskinfo;
122 }
123 }
124
125 sub vm_status {
126 my ($self, $vmid) = @_;
127
128 my $running = PVE::QemuServer::check_running($vmid) ? 1 : 0;
129
130 return wantarray ? ($running, $running ? 'running' : 'stopped') : $running;
131 }
132
133 sub lock_vm {
134 my ($self, $vmid) = @_;
135
136 $self->cmd ("qm set $vmid --lock backup");
137 }
138
139 sub unlock_vm {
140 my ($self, $vmid) = @_;
141
142 $self->cmd ("qm unlock $vmid");
143 }
144
145 sub stop_vm {
146 my ($self, $task, $vmid) = @_;
147
148 my $opts = $self->{vzdump}->{opts};
149
150 my $wait = $opts->{stopwait} * 60;
151 # send shutdown and wait
152 $self->cmd ("qm shutdown $vmid --skiplock --keepActive --timeout $wait");
153 }
154
155 sub start_vm {
156 my ($self, $task, $vmid) = @_;
157
158 $self->cmd ("qm start $vmid --skiplock");
159 }
160
161 sub suspend_vm {
162 my ($self, $task, $vmid) = @_;
163
164 $self->cmd ("qm suspend $vmid --skiplock");
165 }
166
167 sub resume_vm {
168 my ($self, $task, $vmid) = @_;
169
170 $self->cmd ("qm resume $vmid --skiplock");
171 }
172
173 sub assemble {
174 my ($self, $task, $vmid) = @_;
175
176 my $conffile = PVE::QemuConfig->config_file($vmid);
177
178 my $outfile = "$task->{tmpdir}/qemu-server.conf";
179 my $firewall_src = "/etc/pve/firewall/$vmid.fw";
180 my $firewall_dest = "$task->{tmpdir}/qemu-server.fw";
181
182 my $outfd = IO::File->new (">$outfile") ||
183 die "unable to open '$outfile'";
184 my $conffd = IO::File->new ($conffile, 'r') ||
185 die "unable open '$conffile'";
186
187 my $found_snapshot;
188 my $found_pending;
189 while (defined (my $line = <$conffd>)) {
190 next if $line =~ m/^\#vzdump\#/; # just to be sure
191 next if $line =~ m/^\#qmdump\#/; # just to be sure
192 if ($line =~ m/^\[(.*)\]\s*$/) {
193 if ($1 =~ m/PENDING/i) {
194 $found_pending = 1;
195 } else {
196 $found_snapshot = 1;
197 }
198 }
199
200 next if $found_snapshot; # skip all snapshots data
201 next if $found_pending; # skip all pending changes
202
203 if ($line =~ m/^unused\d+:\s*(\S+)\s*/) {
204 $self->loginfo("skip unused drive '$1' (not included into backup)");
205 next;
206 }
207 next if $line =~ m/^lock:/ || $line =~ m/^parent:/;
208
209 print $outfd $line;
210 }
211
212 foreach my $di (@{$task->{disks}}) {
213 if ($di->{type} eq 'block' || $di->{type} eq 'file') {
214 my $storeid = $di->{storeid} || '';
215 my $format = $di->{format} || '';
216 print $outfd "#qmdump#map:$di->{virtdev}:$di->{qmdevice}:$storeid:$format:\n";
217 } else {
218 die "internal error";
219 }
220 }
221
222 if ($found_snapshot) {
223 $self->loginfo("snapshots found (not included into backup)");
224 }
225
226 if ($found_pending) {
227 $self->loginfo("pending configuration changes found (not included into backup)");
228 }
229
230 PVE::Tools::file_copy($firewall_src, $firewall_dest) if -f $firewall_src;
231 }
232
233 sub archive {
234 my ($self, $task, $vmid, $filename, $comp) = @_;
235
236 my $conffile = "$task->{tmpdir}/qemu-server.conf";
237 my $firewall = "$task->{tmpdir}/qemu-server.fw";
238
239 my $opts = $self->{vzdump}->{opts};
240
241 my $starttime = time ();
242
243 my $speed = 0;
244 if ($opts->{bwlimit}) {
245 $speed = $opts->{bwlimit}*1024;
246 }
247
248 my $diskcount = scalar(@{$task->{disks}});
249
250 if (PVE::QemuConfig->is_template($self->{vmlist}->{$vmid}) || !$diskcount) {
251 my @pathlist;
252 foreach my $di (@{$task->{disks}}) {
253 if ($di->{type} eq 'block' || $di->{type} eq 'file') {
254 push @pathlist, "$di->{qmdevice}=$di->{path}";
255 } else {
256 die "implement me";
257 }
258 }
259
260 if (!$diskcount) {
261 $self->loginfo("backup contains no disks");
262 }
263
264 my $outcmd;
265 if ($comp) {
266 $outcmd = "exec:$comp";
267 } else {
268 $outcmd = "exec:cat";
269 }
270
271 $outcmd .= ">$filename" if !$opts->{stdout};
272
273 my $cmd = ['/usr/bin/vma', 'create', '-v', '-c', $conffile];
274 push @$cmd, '-c', $firewall if -e $firewall;
275 push @$cmd, $outcmd, @pathlist;
276
277 $self->loginfo("starting template backup");
278 $self->loginfo(join(' ', @$cmd));
279
280 if ($opts->{stdout}) {
281 $self->cmd($cmd, output => ">&=" . fileno($opts->{stdout}));
282 } else {
283 $self->cmd($cmd);
284 }
285
286 return;
287 }
288
289
290 my $devlist = '';
291 foreach my $di (@{$task->{disks}}) {
292 if ($di->{type} eq 'block' || $di->{type} eq 'file') {
293 $devlist .= $devlist ? ",$di->{qmdevice}" : $di->{qmdevice};
294 } else {
295 die "implement me";
296 }
297 }
298
299 my $stop_after_backup;
300 my $resume_on_backup;
301
302 my $skiplock = 1;
303 my $vm_is_running = PVE::QemuServer::check_running($vmid);
304 if (!$vm_is_running) {
305 eval {
306 $self->loginfo("starting kvm to execute backup task");
307 PVE::QemuServer::vm_start($self->{storecfg}, $vmid, undef,
308 $skiplock, undef, 1);
309 if ($self->{vm_was_running}) {
310 $resume_on_backup = 1;
311 } else {
312 $stop_after_backup = 1;
313 }
314 };
315 if (my $err = $@) {
316 die $err;
317 }
318 }
319
320 my $cpid;
321 my $interrupt_msg = "interrupted by signal\n";
322 eval {
323 $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub {
324 die $interrupt_msg;
325 };
326
327 my $qmpclient = PVE::QMPClient->new();
328
329 my $uuid;
330
331 my $backup_cb = sub {
332 my ($vmid, $resp) = @_;
333 $uuid = $resp->{return}->{UUID};
334 };
335
336 my $outfh;
337 if ($opts->{stdout}) {
338 $outfh = $opts->{stdout};
339 } else {
340 $outfh = IO::File->new($filename, "w") ||
341 die "unable to open file '$filename' - $!\n";
342 }
343
344 my $outfileno;
345 if ($comp) {
346 my @pipefd = POSIX::pipe();
347 $cpid = fork();
348 die "unable to fork worker - $!" if !defined($cpid);
349 if ($cpid == 0) {
350 eval {
351 POSIX::close($pipefd[1]);
352 # redirect STDIN
353 my $fd = fileno(STDIN);
354 close STDIN;
355 POSIX::close(0) if $fd != 0;
356 die "unable to redirect STDIN - $!"
357 if !open(STDIN, "<&", $pipefd[0]);
358
359 # redirect STDOUT
360 $fd = fileno(STDOUT);
361 close STDOUT;
362 POSIX::close (1) if $fd != 1;
363
364 die "unable to redirect STDOUT - $!"
365 if !open(STDOUT, ">&", fileno($outfh));
366
367 exec($comp);
368 die "fork compressor '$comp' failed\n";
369 };
370 if (my $err = $@) {
371 $self->logerr($err);
372 POSIX::_exit(1);
373 }
374 POSIX::_exit(0);
375 kill(-9, $$);
376 } else {
377 POSIX::close($pipefd[0]);
378 $outfileno = $pipefd[1];
379 }
380 } else {
381 $outfileno = fileno($outfh);
382 }
383
384 my $add_fd_cb = sub {
385 my ($vmid, $resp) = @_;
386
387 my $params = {
388 'backup-file' => "/dev/fdname/backup",
389 speed => $speed,
390 'config-file' => $conffile,
391 devlist => $devlist
392 };
393
394 $params->{'firewall-file'} = $firewall if -e $firewall;
395 $qmpclient->queue_cmd($vmid, $backup_cb, 'backup', %$params);
396 };
397
398 $qmpclient->queue_cmd($vmid, $add_fd_cb, 'getfd',
399 fd => $outfileno, fdname => "backup");
400
401 if ($self->{vmlist}->{$vmid}->{agent} && $vm_is_running){
402 eval { PVE::QemuServer::vm_mon_cmd($vmid, "guest-fsfreeze-freeze"); };
403 if (my $err = $@) {
404 $self->logerr($err);
405 }
406 }
407
408 $qmpclient->queue_execute();
409
410 if ($self->{vmlist}->{$vmid}->{agent} && $vm_is_running ){
411 eval { PVE::QemuServer::vm_mon_cmd($vmid, "guest-fsfreeze-thaw"); };
412 if (my $err = $@) {
413 $self->logerr($err);
414 }
415 }
416 die $qmpclient->{errors}->{$vmid} if $qmpclient->{errors}->{$vmid};
417
418 if ($cpid) {
419 POSIX::close($outfileno) == 0 ||
420 die "close output file handle failed\n";
421 }
422
423 die "got no uuid for backup task\n" if !$uuid;
424
425 $self->loginfo("started backup task '$uuid'");
426
427 if ($resume_on_backup) {
428 $self->loginfo("resume VM");
429 PVE::QemuServer::vm_mon_cmd($vmid, 'cont');
430 }
431
432 my $status;
433 my $starttime = time ();
434 my $last_per = -1;
435 my $last_total = 0;
436 my $last_zero = 0;
437 my $last_transferred = 0;
438 my $last_time = time();
439 my $transferred;
440
441 while(1) {
442 $status = PVE::QemuServer::vm_mon_cmd($vmid, 'query-backup');
443 my $total = $status->{total} || 0;
444 $transferred = $status->{transferred} || 0;
445 my $per = $total ? int(($transferred * 100)/$total) : 0;
446 my $zero = $status->{'zero-bytes'} || 0;
447 my $zero_per = $total ? int(($zero * 100)/$total) : 0;
448
449 die "got unexpected uuid\n" if !$status->{uuid} || ($status->{uuid} ne $uuid);
450
451 my $ctime = time();
452 my $duration = $ctime - $starttime;
453
454 my $rbytes = $transferred - $last_transferred;
455 my $wbytes = $rbytes - ($zero - $last_zero);
456
457 my $timediff = ($ctime - $last_time) || 1; # fixme
458 my $mbps_read = ($rbytes > 0) ?
459 int(($rbytes/$timediff)/(1000*1000)) : 0;
460 my $mbps_write = ($wbytes > 0) ?
461 int(($wbytes/$timediff)/(1000*1000)) : 0;
462
463 my $statusline = "status: $per% ($transferred/$total), " .
464 "sparse ${zero_per}% ($zero), duration $duration, " .
465 "$mbps_read/$mbps_write MB/s";
466 my $res = $status->{status} || 'unknown';
467 if ($res ne 'active') {
468 $self->loginfo($statusline);
469 die(($status->{errmsg} || "unknown error") . "\n")
470 if $res eq 'error';
471 die "got unexpected status '$res'\n"
472 if $res ne 'done';
473 die "got wrong number of transfered bytes ($total != $transferred)\n"
474 if ($res eq 'done') && ($total != $transferred);
475
476 last;
477 }
478 if ($per != $last_per && ($timediff > 2)) {
479 $self->loginfo($statusline);
480 $last_per = $per;
481 $last_total = $total if $total;
482 $last_zero = $zero if $zero;
483 $last_transferred = $transferred if $transferred;
484 $last_time = $ctime;
485 }
486 sleep(1);
487 }
488
489 my $duration = time() - $starttime;
490 if ($transferred && $duration) {
491 my $mb = int($transferred/(1000*1000));
492 my $mbps = int(($transferred/$duration)/(1000*1000));
493 $self->loginfo("transferred $mb MB in $duration seconds ($mbps MB/s)");
494 }
495 };
496 my $err = $@;
497
498 if ($err) {
499 $self->logerr($err);
500 $self->loginfo("aborting backup job");
501 eval { PVE::QemuServer::vm_mon_cmd($vmid, 'backup-cancel'); };
502 if (my $err1 = $@) {
503 $self->logerr($err1);
504 }
505 }
506
507 if ($stop_after_backup) {
508 # stop if not running
509 eval {
510 my $resp = PVE::QemuServer::vm_mon_cmd($vmid, 'query-status');
511 my $status = $resp && $resp->{status} ? $resp->{status} : 'unknown';
512 if ($status eq 'prelaunch') {
513 $self->loginfo("stopping kvm after backup task");
514 PVE::QemuServer::vm_stop($self->{storecfg}, $vmid, $skiplock);
515 } else {
516 $self->loginfo("kvm status changed after backup ('$status')" .
517 " - keep VM running");
518 }
519 }
520 }
521
522 if ($err) {
523 if ($cpid) {
524 kill(9, $cpid);
525 waitpid($cpid, 0);
526 }
527 die $err;
528 }
529
530 if ($cpid && (waitpid($cpid, 0) > 0)) {
531 my $stat = $?;
532 my $ec = $stat >> 8;
533 my $signal = $stat & 127;
534 if ($ec || $signal) {
535 die "$comp failed - wrong exit status $ec" .
536 ($signal ? " (signal $signal)\n" : "\n");
537 }
538 }
539 }
540
541 sub snapshot {
542 my ($self, $task, $vmid) = @_;
543
544 # nothing to do
545 }
546
547 sub cleanup {
548 my ($self, $task, $vmid) = @_;
549
550 # nothing to do ?
551 }
552
553 1;