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