]>
git.proxmox.com Git - qemu-server.git/blob - PVE/VZDump/QemuServer.pm
6a58a79e30cafad760dc659c2e92465976b2dbed
1 package PVE
::VZDump
::QemuServer
;
10 use PVE
::Cluster
qw(cfs_read_file);
12 use PVE
::Storage
::Plugin
;
18 use base qw
(PVE
::VZDump
::Plugin
);
21 my ($class, $vzdump) = @_;
23 PVE
::VZDump
::check_bin
('qm');
25 my $self = bless { vzdump
=> $vzdump }, $class;
27 $self->{vmlist
} = PVE
::QemuServer
::vzlist
();
28 $self->{storecfg
} = PVE
::Storage
::config
();
41 return [ keys %{$self->{vmlist
}} ];
45 my ($self, $task, $vmid, $mode) = @_;
49 my $conf = $self->{vmlist
}->{$vmid} = PVE
::QemuConfig-
>load_config($vmid);
51 $self->loginfo("VM Name: $conf->{name}")
52 if defined($conf->{name
});
54 $self->{vm_was_running
} = 1;
55 if (!PVE
::QemuServer
::check_running
($vmid)) {
56 $self->{vm_was_running
} = 0;
59 $task->{hostname
} = $conf->{name
};
61 my $hostname = PVE
::INotify
::nodename
();
65 PVE
::QemuServer
::foreach_drive
($conf, sub {
66 my ($ds, $drive) = @_;
68 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
70 my $volid = $drive->{file
};
72 if (defined($drive->{backup
}) && !$drive->{backup
}) {
73 $self->loginfo("exclude disk '$ds' '$volid' (backup=no)");
75 } elsif ($drive->{iothread
}) {
76 die "disk '$ds' '$volid' (iothread=on) can't use backup feature currently. Please set backup=no for this drive";
78 $self->loginfo("include disk '$ds' '$volid'");
81 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
82 push @$vollist, $volid if $storeid;
83 $drivehash->{$ds} = $drive;
86 PVE
::Storage
::activate_volumes
($self->{storecfg
}, $vollist);
88 foreach my $ds (sort keys %$drivehash) {
89 my $drive = $drivehash->{$ds};
91 my $volid = $drive->{file
};
95 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
97 $path = PVE
::Storage
::path
($self->{storecfg
}, $volid);
108 ($size, $format) = PVE
::Storage
::volume_size_info
($self->{storecfg
}, $volid, 5);
110 die "no such volume '$volid'\n" if $@;
112 my $diskinfo = { path
=> $path , volid
=> $volid, storeid
=> $storeid,
113 format
=> $format, virtdev
=> $ds, qmdevice
=> "drive-$ds" };
116 $diskinfo->{type
} = 'block';
118 $diskinfo->{type
} = 'file';
121 push @{$task->{disks
}}, $diskinfo;
126 my ($self, $vmid) = @_;
128 my $running = PVE
::QemuServer
::check_running
($vmid) ?
1 : 0;
130 return wantarray ?
($running, $running ?
'running' : 'stopped') : $running;
134 my ($self, $vmid) = @_;
136 $self->cmd ("qm set $vmid --lock backup");
140 my ($self, $vmid) = @_;
142 $self->cmd ("qm unlock $vmid");
146 my ($self, $task, $vmid) = @_;
148 my $opts = $self->{vzdump
}->{opts
};
150 my $wait = $opts->{stopwait
} * 60;
151 # send shutdown and wait
152 $self->cmd ("qm shutdown $vmid --skiplock --keepActive --timeout $wait");
156 my ($self, $task, $vmid) = @_;
158 $self->cmd ("qm start $vmid --skiplock");
162 my ($self, $task, $vmid) = @_;
164 $self->cmd ("qm suspend $vmid --skiplock");
168 my ($self, $task, $vmid) = @_;
170 $self->cmd ("qm resume $vmid --skiplock");
174 my ($self, $task, $vmid) = @_;
176 my $conffile = PVE
::QemuConfig-
>config_file($vmid);
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";
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'";
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) {
200 next if $found_snapshot; # skip all snapshots data
201 next if $found_pending; # skip all pending changes
203 if ($line =~ m/^unused\d+:\s*(\S+)\s*/) {
204 $self->loginfo("skip unused drive '$1' (not included into backup)");
207 next if $line =~ m/^lock:/ || $line =~ m/^parent:/;
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";
218 die "internal error";
222 if ($found_snapshot) {
223 $self->loginfo("snapshots found (not included into backup)");
226 if ($found_pending) {
227 $self->loginfo("pending configuration changes found (not included into backup)");
230 PVE
::Tools
::file_copy
($firewall_src, $firewall_dest) if -f
$firewall_src;
234 my ($self, $task, $vmid, $filename, $comp) = @_;
236 my $conffile = "$task->{tmpdir}/qemu-server.conf";
237 my $firewall = "$task->{tmpdir}/qemu-server.fw";
239 my $opts = $self->{vzdump
}->{opts
};
241 my $starttime = time ();
244 if ($opts->{bwlimit
}) {
245 $speed = $opts->{bwlimit
}*1024;
248 my $diskcount = scalar(@{$task->{disks
}});
250 if (PVE
::QemuConfig-
>is_template($self->{vmlist
}->{$vmid}) || !$diskcount) {
252 foreach my $di (@{$task->{disks
}}) {
253 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
254 push @pathlist, "$di->{qmdevice}=$di->{path}";
261 $self->loginfo("backup contains no disks");
266 $outcmd = "exec:$comp";
268 $outcmd = "exec:cat";
271 $outcmd .= ">$filename" if !$opts->{stdout
};
273 my $cmd = ['/usr/bin/vma', 'create', '-v', '-c', $conffile];
274 push @$cmd, '-c', $firewall if -e
$firewall;
275 push @$cmd, $outcmd, @pathlist;
277 $self->loginfo("starting template backup");
278 $self->loginfo(join(' ', @$cmd));
280 if ($opts->{stdout
}) {
281 $self->cmd($cmd, output
=> ">&=" . fileno($opts->{stdout
}));
291 foreach my $di (@{$task->{disks
}}) {
292 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
293 $devlist .= $devlist ?
",$di->{qmdevice}" : $di->{qmdevice
};
299 my $stop_after_backup;
300 my $resume_on_backup;
303 my $vm_is_running = PVE
::QemuServer
::check_running
($vmid);
304 if (!$vm_is_running) {
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;
312 $stop_after_backup = 1;
321 my $interrupt_msg = "interrupted by signal\n";
323 $SIG{INT
} = $SIG{TERM
} = $SIG{QUIT
} = $SIG{HUP
} = $SIG{PIPE
} = sub {
327 my $qmpclient = PVE
::QMPClient-
>new();
331 my $backup_cb = sub {
332 my ($vmid, $resp) = @_;
333 $uuid = $resp->{return}->{UUID
};
337 if ($opts->{stdout
}) {
338 $outfh = $opts->{stdout
};
340 $outfh = IO
::File-
>new($filename, "w") ||
341 die "unable to open file '$filename' - $!\n";
346 my @pipefd = POSIX
::pipe();
348 die "unable to fork worker - $!" if !defined($cpid);
351 POSIX
::close($pipefd[1]);
353 my $fd = fileno(STDIN
);
355 POSIX
::close(0) if $fd != 0;
356 die "unable to redirect STDIN - $!"
357 if !open(STDIN
, "<&", $pipefd[0]);
360 $fd = fileno(STDOUT
);
362 POSIX
::close (1) if $fd != 1;
364 die "unable to redirect STDOUT - $!"
365 if !open(STDOUT
, ">&", fileno($outfh));
368 die "fork compressor '$comp' failed\n";
377 POSIX
::close($pipefd[0]);
378 $outfileno = $pipefd[1];
381 $outfileno = fileno($outfh);
384 my $add_fd_cb = sub {
385 my ($vmid, $resp) = @_;
388 'backup-file' => "/dev/fdname/backup",
390 'config-file' => $conffile,
394 $params->{'firewall-file'} = $firewall if -e
$firewall;
395 $qmpclient->queue_cmd($vmid, $backup_cb, 'backup', %$params);
398 $qmpclient->queue_cmd($vmid, $add_fd_cb, 'getfd',
399 fd
=> $outfileno, fdname
=> "backup");
401 if ($self->{vmlist
}->{$vmid}->{agent
} && $vm_is_running){
402 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-freeze"); };
408 $qmpclient->queue_execute();
410 if ($self->{vmlist
}->{$vmid}->{agent
} && $vm_is_running ){
411 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-thaw"); };
416 die $qmpclient->{errors
}->{$vmid} if $qmpclient->{errors
}->{$vmid};
419 POSIX
::close($outfileno) == 0 ||
420 die "close output file handle failed\n";
423 die "got no uuid for backup task\n" if !$uuid;
425 $self->loginfo("started backup task '$uuid'");
427 if ($resume_on_backup) {
428 $self->loginfo("resume VM");
429 PVE
::QemuServer
::vm_mon_cmd
($vmid, 'cont');
433 my $starttime = time ();
437 my $last_transferred = 0;
438 my $last_time = time();
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;
449 die "got unexpected uuid\n" if !$status->{uuid
} || ($status->{uuid
} ne $uuid);
452 my $duration = $ctime - $starttime;
454 my $rbytes = $transferred - $last_transferred;
455 my $wbytes = $rbytes - ($zero - $last_zero);
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;
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")
471 die "got unexpected status '$res'\n"
473 die "got wrong number of transfered bytes ($total != $transferred)\n"
474 if ($res eq 'done') && ($total != $transferred);
478 if ($per != $last_per && ($timediff > 2)) {
479 $self->loginfo($statusline);
481 $last_total = $total if $total;
482 $last_zero = $zero if $zero;
483 $last_transferred = $transferred if $transferred;
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)");
500 $self->loginfo("aborting backup job");
501 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, 'backup-cancel'); };
503 $self->logerr($err1);
507 if ($stop_after_backup) {
508 # stop if not running
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);
516 $self->loginfo("kvm status changed after backup ('$status')" .
517 " - keep VM running");
530 if ($cpid && (waitpid($cpid, 0) > 0)) {
533 my $signal = $stat & 127;
534 if ($ec || $signal) {
535 die "$comp failed - wrong exit status $ec" .
536 ($signal ?
" (signal $signal)\n" : "\n");
542 my ($self, $task, $vmid) = @_;
548 my ($self, $task, $vmid) = @_;