]> git.proxmox.com Git - qemu-server.git/blob - PVE/VZDump/QemuServer.pm
567088cb2003cfebd24cc0a644293488dd20f902
[qemu-server.git] / PVE / VZDump / QemuServer.pm
1 package PVE::VZDump::QemuServer;
2
3 use strict;
4 use warnings;
5
6 use File::Basename;
7 use File::Path;
8 use IO::File;
9 use IPC::Open3;
10 use JSON;
11
12 use PVE::Cluster qw(cfs_read_file);
13 use PVE::INotify;
14 use PVE::IPCC;
15 use PVE::JSONSchema;
16 use PVE::QMPClient;
17 use PVE::Storage::Plugin;
18 use PVE::Storage::PBSPlugin;
19 use PVE::Storage;
20 use PVE::Tools;
21 use PVE::VZDump;
22
23 use PVE::QemuConfig;
24 use PVE::QemuServer;
25 use PVE::QemuServer::Machine;
26 use PVE::QemuServer::Monitor qw(mon_cmd);
27
28 use base qw (PVE::VZDump::Plugin);
29
30 sub new {
31 my ($class, $vzdump) = @_;
32
33 PVE::VZDump::check_bin('qm');
34
35 my $self = bless { vzdump => $vzdump }, $class;
36
37 $self->{vmlist} = PVE::QemuServer::vzlist();
38 $self->{storecfg} = PVE::Storage::config();
39
40 return $self;
41 };
42
43 sub type {
44 return 'qemu';
45 }
46
47 sub vmlist {
48 my ($self) = @_;
49 return [ keys %{$self->{vmlist}} ];
50 }
51
52 sub prepare {
53 my ($self, $task, $vmid, $mode) = @_;
54
55 $task->{disks} = [];
56
57 my $conf = $self->{vmlist}->{$vmid} = PVE::QemuConfig->load_config($vmid);
58
59 $self->loginfo("VM Name: $conf->{name}")
60 if defined($conf->{name});
61
62 $self->{vm_was_running} = 1;
63 if (!PVE::QemuServer::check_running($vmid)) {
64 $self->{vm_was_running} = 0;
65 }
66
67 $task->{hostname} = $conf->{name};
68
69 my $hostname = PVE::INotify::nodename();
70
71 my $vollist = [];
72 my $drivehash = {};
73 my $backup_volumes = PVE::QemuConfig->get_backup_volumes($conf);
74
75 foreach my $volume (@{$backup_volumes}) {
76 my $name = $volume->{key};
77 my $volume_config = $volume->{volume_config};
78 my $volid = $volume_config->{file};
79
80 if (!$volume->{included}) {
81 $self->loginfo("exclude disk '$name' '$volid' ($volume->{reason})");
82 next;
83 } elsif ($self->{vm_was_running} && $volume_config->{iothread}) {
84 if (!PVE::QemuServer::Machine::runs_at_least_qemu_version($vmid, 4, 0, 1)) {
85 die "disk '$name' '$volid' (iothread=on) can't use backup feature with running QEMU " .
86 "version < 4.0.1! Either set backup=no for this drive or upgrade QEMU and restart VM\n";
87 }
88 } else {
89 my $log = "include disk '$name' '$volid'";
90 if (defined(my $size = $volume_config->{size})) {
91 my $readable_size = PVE::JSONSchema::format_size($size);
92 $log .= " $readable_size";
93 }
94 $self->loginfo($log);
95 }
96
97 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
98 push @$vollist, $volid if $storeid;
99 $drivehash->{$name} = $volume->{volume_config};
100 }
101
102 PVE::Storage::activate_volumes($self->{storecfg}, $vollist);
103
104 foreach my $ds (sort keys %$drivehash) {
105 my $drive = $drivehash->{$ds};
106
107 my $volid = $drive->{file};
108 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
109
110 my $path = $volid;
111 if ($storeid) {
112 $path = PVE::Storage::path($self->{storecfg}, $volid);
113 }
114 next if !$path;
115
116 my ($size, $format) = eval { PVE::Storage::volume_size_info($self->{storecfg}, $volid, 5) };
117 die "no such volume '$volid'\n" if $@;
118
119 my $diskinfo = {
120 path => $path,
121 volid => $volid,
122 storeid => $storeid,
123 format => $format,
124 virtdev => $ds,
125 qmdevice => "drive-$ds",
126 };
127
128 if (-b $path) {
129 $diskinfo->{type} = 'block';
130 } else {
131 $diskinfo->{type} = 'file';
132 }
133
134 push @{$task->{disks}}, $diskinfo;
135 }
136 }
137
138 sub vm_status {
139 my ($self, $vmid) = @_;
140
141 my $running = PVE::QemuServer::check_running($vmid) ? 1 : 0;
142
143 return wantarray ? ($running, $running ? 'running' : 'stopped') : $running;
144 }
145
146 sub lock_vm {
147 my ($self, $vmid) = @_;
148
149 PVE::QemuConfig->set_lock($vmid, 'backup');
150 }
151
152 sub unlock_vm {
153 my ($self, $vmid) = @_;
154
155 PVE::QemuConfig->remove_lock($vmid, 'backup');
156 }
157
158 sub stop_vm {
159 my ($self, $task, $vmid) = @_;
160
161 my $opts = $self->{vzdump}->{opts};
162
163 my $wait = $opts->{stopwait} * 60;
164 # send shutdown and wait
165 $self->cmd ("qm shutdown $vmid --skiplock --keepActive --timeout $wait");
166 }
167
168 sub start_vm {
169 my ($self, $task, $vmid) = @_;
170
171 $self->cmd ("qm start $vmid --skiplock");
172 }
173
174 sub suspend_vm {
175 my ($self, $task, $vmid) = @_;
176
177 $self->cmd ("qm suspend $vmid --skiplock");
178 }
179
180 sub resume_vm {
181 my ($self, $task, $vmid) = @_;
182
183 $self->cmd ("qm resume $vmid --skiplock");
184 }
185
186 sub assemble {
187 my ($self, $task, $vmid) = @_;
188
189 my $conffile = PVE::QemuConfig->config_file($vmid);
190
191 my $outfile = "$task->{tmpdir}/qemu-server.conf";
192 my $firewall_src = "/etc/pve/firewall/$vmid.fw";
193 my $firewall_dest = "$task->{tmpdir}/qemu-server.fw";
194
195 my $outfd = IO::File->new (">$outfile") ||
196 die "unable to open '$outfile'";
197 my $conffd = IO::File->new ($conffile, 'r') ||
198 die "unable open '$conffile'";
199
200 my $found_snapshot;
201 my $found_pending;
202 while (defined (my $line = <$conffd>)) {
203 next if $line =~ m/^\#vzdump\#/; # just to be sure
204 next if $line =~ m/^\#qmdump\#/; # just to be sure
205 if ($line =~ m/^\[(.*)\]\s*$/) {
206 if ($1 =~ m/PENDING/i) {
207 $found_pending = 1;
208 } else {
209 $found_snapshot = 1;
210 }
211 }
212 next if $found_snapshot || $found_pending; # skip all snapshots and pending changes config data
213
214 if ($line =~ m/^unused\d+:\s*(\S+)\s*/) {
215 $self->loginfo("skip unused drive '$1' (not included into backup)");
216 next;
217 }
218 next if $line =~ m/^lock:/ || $line =~ m/^parent:/;
219
220 print $outfd $line;
221 }
222
223 foreach my $di (@{$task->{disks}}) {
224 if ($di->{type} eq 'block' || $di->{type} eq 'file') {
225 my $storeid = $di->{storeid} || '';
226 my $format = $di->{format} || '';
227 print $outfd "#qmdump#map:$di->{virtdev}:$di->{qmdevice}:$storeid:$format:\n";
228 } else {
229 die "internal error";
230 }
231 }
232
233 if ($found_snapshot) {
234 $self->loginfo("snapshots found (not included into backup)");
235 }
236 if ($found_pending) {
237 $self->loginfo("pending configuration changes found (not included into backup)");
238 }
239
240 PVE::Tools::file_copy($firewall_src, $firewall_dest) if -f $firewall_src;
241 }
242
243 sub archive {
244 my ($self, $task, $vmid, $filename, $comp) = @_;
245
246 my $opts = $self->{vzdump}->{opts};
247 my $scfg = $opts->{scfg};
248
249 if ($self->{vzdump}->{opts}->{pbs}) {
250 $self->archive_pbs($task, $vmid);
251 } else {
252 $self->archive_vma($task, $vmid, $filename, $comp);
253 }
254 }
255
256 # number, [precision=1]
257 my $num2str = sub {
258 return sprintf( "%." . ( $_[1] || 1 ) . "f", $_[0] );
259 };
260 sub bytes_to_human {
261 my ($bytes, $precission) = @_;
262
263 return $num2str->($bytes, $precission) . ' B' if $bytes < 1024;
264 my $kb = $bytes/1024;
265
266 return $num2str->($kb, $precission) . " KiB" if $kb < 1024;
267 my $mb = $kb/1024;
268
269 return $num2str->($mb, $precission) . " MiB" if $mb < 1024;
270 my $gb = $mb/1024;
271
272 return $num2str->($gb, $precission) . " GiB" if $gb < 1024;
273 my $tb = $gb/1024;
274
275 return $num2str->($tb, $precission) . " TiB";
276 };
277
278 my $query_backup_status_loop = sub {
279 my ($self, $vmid, $job_uuid) = @_;
280
281 my $starttime = time ();
282 my $last_time = $starttime;
283 my ($last_percent, $last_total, $last_target, $last_zero, $last_transferred) = (-1, 0, 0, 0, 0);
284 my ($transferred, $reused);
285 my $first_round = 1;
286
287 my $get_mbps = sub {
288 my ($mb, $delta) = @_;
289 return "0 B/s" if $mb <= 0;
290 my $bw = int(($mb / $delta));
291 return bytes_to_human($bw) . "/s";
292 };
293
294 while(1) {
295 my $status = mon_cmd($vmid, 'query-backup');
296
297 my $total = $status->{total} || 0;
298 my $dirty = $status->{dirty};
299 my $target = (defined($dirty) && $dirty < $total) ? $dirty : $total;
300 $transferred = $status->{transferred} || 0;
301 $reused = $status->{reused};
302 my $percent = $target ? int(($transferred * 100)/$target) : 0;
303 my $zero = $status->{'zero-bytes'} || 0;
304
305 die "got unexpected uuid\n" if !$status->{uuid} || ($status->{uuid} ne $job_uuid);
306
307 my $ctime = time();
308 my $duration = $ctime - $starttime;
309
310 my $rbytes = $transferred - $last_transferred;
311 my $wbytes = $rbytes - ($zero - $last_zero);
312
313 my $timediff = ($ctime - $last_time) || 1; # fixme
314 my $mbps_read = $get_mbps->($rbytes, $timediff);
315 my $mbps_write = $get_mbps->($wbytes, $timediff);
316 my $target_h = bytes_to_human($target);
317 my $transferred_h = bytes_to_human($transferred);
318
319 if ($first_round && $target != $total) {
320 my $total_h = bytes_to_human($total);
321 $self->loginfo("using fast incremental mode (dirty-bitmap), $target_h dirty of $total_h total");
322 }
323
324 my $statusline = "status: $percent% ($transferred_h of $target_h), duration $duration"
325 .", read: $mbps_read, write: $mbps_write";
326
327 my $res = $status->{status} || 'unknown';
328 if ($res ne 'active') {
329 $self->loginfo($statusline);
330 if ($res ne 'done') {
331 die (($status->{errmsg} || "unknown error") . "\n") if $res eq 'error';
332 die "got unexpected status '$res'\n";
333 }
334 $last_target = $target if $target;
335 $last_total = $total if $total;
336 $last_zero = $zero if $zero;
337 $last_transferred = $transferred if $transferred;
338 last;
339 }
340 if ($percent != $last_percent && ($timediff > 2)) {
341 $self->loginfo($statusline);
342 $last_percent = $percent;
343 $last_target = $target if $target;
344 $last_total = $total if $total;
345 $last_zero = $zero if $zero;
346 $last_transferred = $transferred if $transferred;
347 $last_time = $ctime;
348 }
349 sleep(1);
350 $first_round = 0;
351 }
352
353 my $duration = time() - $starttime;
354 if ($transferred && $duration) {
355 my $transferred_h = bytes_to_human($transferred, 2);
356 my $mbps = $get_mbps->($transferred, $duration);
357 if ($reused) {
358 my $reused_h = bytes_to_human($reused, 2);
359 my $reuse_per = int($reused * 100 / $last_total);
360 $self->loginfo("backup was done incrementally, reused $reused_h (${reuse_per}%)");
361 }
362 $self->loginfo("transferred $transferred_h in $duration seconds ($mbps)");
363 }
364
365 if ($last_zero) {
366 my $zero_per = $last_target ? int(($last_zero * 100)/$last_target) : 0;
367 my $zero_h = bytes_to_human($last_zero, 2);
368 $self->loginfo("Backup is sparse: ${zero_per}% ($zero_h) zero data");
369 }
370
371 return {
372 total => $last_total,
373 reused => $reused,
374 };
375 };
376
377 sub archive_pbs {
378 my ($self, $task, $vmid) = @_;
379
380 my $conffile = "$task->{tmpdir}/qemu-server.conf";
381 my $firewall = "$task->{tmpdir}/qemu-server.fw";
382
383 my $opts = $self->{vzdump}->{opts};
384 my $scfg = $opts->{scfg};
385
386 my $starttime = time();
387
388 my $server = $scfg->{server};
389 my $datastore = $scfg->{datastore};
390 my $username = $scfg->{username} // 'root@pam';
391 my $fingerprint = $scfg->{fingerprint};
392
393 my $repo = "$username\@$server:$datastore";
394 my $password = PVE::Storage::PBSPlugin::pbs_get_password($scfg, $opts->{storage});
395
396 my $diskcount = scalar(@{$task->{disks}});
397 if (PVE::QemuConfig->is_template($self->{vmlist}->{$vmid}) || !$diskcount) {
398 my @pathlist;
399 # FIXME: accumulate disk sizes to use for backup job (email) log
400 foreach my $di (@{$task->{disks}}) {
401 if ($di->{type} eq 'block' || $di->{type} eq 'file') {
402 push @pathlist, "$di->{qmdevice}.img:$di->{path}";
403 } else {
404 die "implement me (type $di->{type})";
405 }
406 }
407
408 if (!$diskcount) {
409 $self->loginfo("backup contains no disks");
410 }
411
412 local $ENV{PBS_PASSWORD} = $password;
413 local $ENV{PBS_FINGERPRINT} = $fingerprint if defined($fingerprint);
414 my $cmd = [
415 '/usr/bin/proxmox-backup-client',
416 'backup',
417 '--repository', $repo,
418 '--backup-type', 'vm',
419 '--backup-id', "$vmid",
420 '--backup-time', $task->{backup_time},
421 ];
422
423 push @$cmd, "qemu-server.conf:$conffile";
424 push @$cmd, "fw.conf:$firewall" if -e $firewall;
425 push @$cmd, @pathlist if scalar(@pathlist);
426
427 $self->loginfo("starting template backup");
428 $self->loginfo(join(' ', @$cmd));
429
430 $self->cmd($cmd);
431
432 return;
433 }
434
435 # get list early so we die on unkown drive types before doing anything
436 my $devlist = _get_task_devlist($task);
437
438 $self->enforce_vm_running_for_backup($vmid);
439
440 my $backup_job_uuid;
441 eval {
442 $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub {
443 die "interrupted by signal\n";
444 };
445
446 my $fs_frozen = $self->qga_fs_freeze($task, $vmid);
447
448 my $params = {
449 format => "pbs",
450 'backup-file' => $repo,
451 'backup-id' => "$vmid",
452 'backup-time' => $task->{backup_time},
453 password => $password,
454 devlist => $devlist,
455 'config-file' => $conffile,
456 'use-dirty-bitmap' => JSON::true,
457 };
458 $params->{fingerprint} = $fingerprint if defined($fingerprint);
459 $params->{'firewall-file'} = $firewall if -e $firewall;
460
461 $params->{timeout} = 60; # give some time to connect to the backup server
462
463 my $res = eval { mon_cmd($vmid, "backup", %$params) };
464 my $qmperr = $@;
465 $backup_job_uuid = $res->{UUID} if $res;
466
467 if ($fs_frozen) {
468 $self->qga_fs_thaw($vmid);
469 }
470
471 die $qmperr if $qmperr;
472 die "got no uuid for backup task\n" if !defined($backup_job_uuid);
473
474 $self->loginfo("started backup task '$backup_job_uuid'");
475
476 $self->resume_vm_after_job_start($task, $vmid);
477
478 my $stat = $query_backup_status_loop->($self, $vmid, $backup_job_uuid);
479 $task->{size} = $stat->{total};
480 };
481 my $err = $@;
482 if ($err) {
483 $self->logerr($err);
484 $self->mon_backup_cancel($vmid) if defined($backup_job_uuid);
485 }
486 $self->restore_vm_power_state($vmid);
487
488 die $err if $err;
489 }
490
491 my $fork_compressor_pipe = sub {
492 my ($self, $comp, $outfileno) = @_;
493
494 my @pipefd = POSIX::pipe();
495 my $cpid = fork();
496 die "unable to fork worker - $!" if !defined($cpid) || $cpid < 0;
497 if ($cpid == 0) {
498 eval {
499 POSIX::close($pipefd[1]);
500 # redirect STDIN
501 my $fd = fileno(STDIN);
502 close STDIN;
503 POSIX::close(0) if $fd != 0;
504 die "unable to redirect STDIN - $!"
505 if !open(STDIN, "<&", $pipefd[0]);
506
507 # redirect STDOUT
508 $fd = fileno(STDOUT);
509 close STDOUT;
510 POSIX::close (1) if $fd != 1;
511
512 die "unable to redirect STDOUT - $!"
513 if !open(STDOUT, ">&", $outfileno);
514
515 exec($comp);
516 die "fork compressor '$comp' failed\n";
517 };
518 if (my $err = $@) {
519 $self->logerr($err);
520 POSIX::_exit(1);
521 }
522 POSIX::_exit(0);
523 kill(-9, $$);
524 } else {
525 POSIX::close($pipefd[0]);
526 $outfileno = $pipefd[1];
527 }
528
529 return ($cpid, $outfileno);
530 };
531
532 sub archive_vma {
533 my ($self, $task, $vmid, $filename, $comp) = @_;
534
535 my $conffile = "$task->{tmpdir}/qemu-server.conf";
536 my $firewall = "$task->{tmpdir}/qemu-server.fw";
537
538 my $opts = $self->{vzdump}->{opts};
539
540 my $starttime = time();
541
542 my $speed = 0;
543 if ($opts->{bwlimit}) {
544 $speed = $opts->{bwlimit}*1024;
545 }
546
547 my $diskcount = scalar(@{$task->{disks}});
548 if (PVE::QemuConfig->is_template($self->{vmlist}->{$vmid}) || !$diskcount) {
549 my @pathlist;
550 foreach my $di (@{$task->{disks}}) {
551 if ($di->{type} eq 'block' || $di->{type} eq 'file') {
552 push @pathlist, "$di->{qmdevice}=$di->{path}";
553 } else {
554 die "implement me";
555 }
556 }
557
558 if (!$diskcount) {
559 $self->loginfo("backup contains no disks");
560 }
561
562 my $outcmd;
563 if ($comp) {
564 $outcmd = "exec:$comp";
565 } else {
566 $outcmd = "exec:cat";
567 }
568
569 $outcmd .= " > $filename" if !$opts->{stdout};
570
571 my $cmd = ['/usr/bin/vma', 'create', '-v', '-c', $conffile];
572 push @$cmd, '-c', $firewall if -e $firewall;
573 push @$cmd, $outcmd, @pathlist;
574
575 $self->loginfo("starting template backup");
576 $self->loginfo(join(' ', @$cmd));
577
578 if ($opts->{stdout}) {
579 $self->cmd($cmd, output => ">&" . fileno($opts->{stdout}));
580 } else {
581 $self->cmd($cmd);
582 }
583
584 return;
585 }
586
587 my $devlist = _get_task_devlist($task);
588
589 $self->enforce_vm_running_for_backup($vmid);
590
591 my $cpid;
592 my $backup_job_uuid;
593
594 eval {
595 $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub {
596 die "interrupted by signal\n";
597 };
598
599 my $outfh;
600 if ($opts->{stdout}) {
601 $outfh = $opts->{stdout};
602 } else {
603 $outfh = IO::File->new($filename, "w") ||
604 die "unable to open file '$filename' - $!\n";
605 }
606 my $outfileno = fileno($outfh);
607
608 if ($comp) {
609 ($cpid, $outfileno) = $fork_compressor_pipe->($self, $comp, $outfileno);
610 }
611
612 my $qmpclient = PVE::QMPClient->new();
613 my $backup_cb = sub {
614 my ($vmid, $resp) = @_;
615 $backup_job_uuid = $resp->{return}->{UUID};
616 };
617 my $add_fd_cb = sub {
618 my ($vmid, $resp) = @_;
619
620 my $params = {
621 'backup-file' => "/dev/fdname/backup",
622 speed => $speed,
623 'config-file' => $conffile,
624 devlist => $devlist
625 };
626 $params->{'firewall-file'} = $firewall if -e $firewall;
627
628 $qmpclient->queue_cmd($vmid, $backup_cb, 'backup', %$params);
629 };
630
631 $qmpclient->queue_cmd($vmid, $add_fd_cb, 'getfd', fd => $outfileno, fdname => "backup");
632
633 my $fs_frozen = $self->qga_fs_freeze($task, $vmid);
634
635 eval { $qmpclient->queue_execute(30) };
636 my $qmperr = $@;
637
638 if ($fs_frozen) {
639 $self->qga_fs_thaw($vmid);
640 }
641
642 die $qmperr if $qmperr;
643 die $qmpclient->{errors}->{$vmid} if $qmpclient->{errors}->{$vmid};
644
645 if ($cpid) {
646 POSIX::close($outfileno) == 0 ||
647 die "close output file handle failed\n";
648 }
649
650 die "got no uuid for backup task\n" if !defined($backup_job_uuid);
651
652 $self->loginfo("started backup task '$backup_job_uuid'");
653
654 $self->resume_vm_after_job_start($task, $vmid);
655
656 $query_backup_status_loop->($self, $vmid, $backup_job_uuid);
657 };
658 my $err = $@;
659 if ($err) {
660 $self->logerr($err);
661 $self->mon_backup_cancel($vmid) if defined($backup_job_uuid);
662 }
663
664 $self->restore_vm_power_state($vmid);
665
666 if ($err) {
667 if ($cpid) {
668 kill(9, $cpid);
669 waitpid($cpid, 0);
670 }
671 die $err;
672 }
673
674 if ($cpid && (waitpid($cpid, 0) > 0)) {
675 my $stat = $?;
676 my $ec = $stat >> 8;
677 my $signal = $stat & 127;
678 if ($ec || $signal) {
679 die "$comp failed - wrong exit status $ec" .
680 ($signal ? " (signal $signal)\n" : "\n");
681 }
682 }
683 }
684
685 sub _get_task_devlist {
686 my ($task) = @_;
687
688 my $devlist = '';
689 foreach my $di (@{$task->{disks}}) {
690 if ($di->{type} eq 'block' || $di->{type} eq 'file') {
691 $devlist .= ',' if $devlist;
692 $devlist .= $di->{qmdevice};
693 } else {
694 die "implement me (type '$di->{type}')";
695 }
696 }
697 return $devlist;
698 }
699
700 sub qga_fs_freeze {
701 my ($self, $task, $vmid) = @_;
702 return if !$self->{vmlist}->{$vmid}->{agent} || $task->{mode} eq 'stop' || !$self->{vm_was_running};
703
704 if (!PVE::QemuServer::qga_check_running($vmid, 1)) {
705 $self->loginfo("skipping guest-agent 'fs-freeze', agent configured but not running?");
706 return;
707 }
708
709 $self->loginfo("issuing guest-agent 'fs-freeze' command");
710 eval { mon_cmd($vmid, "guest-fsfreeze-freeze") };
711 $self->logerr($@) if $@;
712
713 return 1; # even on mon command error, ensure we always thaw again
714 }
715
716 # only call if fs_freeze return 1
717 sub qga_fs_thaw {
718 my ($self, $vmid) = @_;
719
720 $self->loginfo("issuing guest-agent 'fs-thaw' command");
721 eval { mon_cmd($vmid, "guest-fsfreeze-thaw") };
722 $self->logerr($@) if $@;
723 }
724
725 # we need a running QEMU/KVM process for backup, starts a paused (prelaunch)
726 # one if VM isn't already running
727 sub enforce_vm_running_for_backup {
728 my ($self, $vmid) = @_;
729
730 if (PVE::QemuServer::check_running($vmid)) {
731 $self->{vm_was_running} = 1;
732 return;
733 }
734
735 eval {
736 $self->loginfo("starting kvm to execute backup task");
737 # start with skiplock
738 my $params = {
739 skiplock => 1,
740 paused => 1,
741 };
742 PVE::QemuServer::vm_start($self->{storecfg}, $vmid, $params);
743 };
744 die $@ if $@;
745 }
746
747 # resume VM againe once we got in a clear state (stop mode backup of running VM)
748 sub resume_vm_after_job_start {
749 my ($self, $task, $vmid) = @_;
750
751 return if !$self->{vm_was_running};
752
753 if (my $stoptime = $task->{vmstoptime}) {
754 my $delay = time() - $task->{vmstoptime};
755 $task->{vmstoptime} = undef; # avoid printing 'online after ..' twice
756 $self->loginfo("resuming VM again after $delay seconds");
757 } else {
758 $self->loginfo("resuming VM again");
759 }
760 mon_cmd($vmid, 'cont');
761 }
762
763 # stop again if VM was not running before
764 sub restore_vm_power_state {
765 my ($self, $vmid) = @_;
766
767 # we always let VMs keep running
768 return if $self->{vm_was_running};
769
770 eval {
771 my $resp = mon_cmd($vmid, 'query-status');
772 my $status = $resp && $resp->{status} ? $resp->{status} : 'unknown';
773 if ($status eq 'prelaunch') {
774 $self->loginfo("stopping kvm after backup task");
775 PVE::QemuServer::vm_stop($self->{storecfg}, $vmid, 1);
776 } else {
777 $self->loginfo("kvm status changed after backup ('$status') - keep VM running");
778 }
779 };
780 warn $@ if $@;
781 }
782
783 sub mon_backup_cancel {
784 my ($self, $vmid) = @_;
785
786 $self->loginfo("aborting backup job");
787 eval { mon_cmd($vmid, 'backup-cancel') };
788 $self->logerr($@) if $@;
789 }
790
791 sub snapshot {
792 my ($self, $task, $vmid) = @_;
793
794 # nothing to do
795 }
796
797 sub cleanup {
798 my ($self, $task, $vmid) = @_;
799
800 # nothing to do ?
801 }
802
803 1;