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