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