]>
git.proxmox.com Git - qemu-server.git/blob - PVE/VZDump/QemuServer.pm
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 };
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
::QemuServer
::load_config
($vmid);
51 $self->{vm_was_running
} = 1;
52 if (!PVE
::QemuServer
::check_running
($vmid)) {
53 $self->{vm_was_running
} = 0;
56 $task->{hostname
} = $conf->{name
};
58 my $hostname = PVE
::INotify
::nodename
();
62 PVE
::QemuServer
::foreach_drive
($conf, sub {
63 my ($ds, $drive) = @_;
65 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
67 if (defined($drive->{backup
}) && !$drive->{backup
}) {
68 $self->loginfo("exclude disk '$ds' (backup=no)");
70 } elsif ($drive->{iothread
}) {
71 die "disk '$ds' (iothread=on) can't use backup feature currently. Please set backup=no for this drive";
74 my $volid = $drive->{file
};
76 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
77 push @$vollist, $volid if $storeid;
78 $drivehash->{$ds} = $drive;
81 PVE
::Storage
::activate_volumes
($self->{storecfg
}, $vollist);
83 foreach my $ds (sort keys %$drivehash) {
84 my $drive = $drivehash->{$ds};
86 my $volid = $drive->{file
};
90 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
92 $path = PVE
::Storage
::path
($self->{storecfg
}, $volid);
103 ($size, $format) = PVE
::Storage
::volume_size_info
($self->{storecfg
}, $volid, 5);
105 die "no such volume '$volid'\n" if $@;
107 my $diskinfo = { path
=> $path , volid
=> $volid, storeid
=> $storeid,
108 format
=> $format, virtdev
=> $ds, qmdevice
=> "drive-$ds" };
111 $diskinfo->{type
} = 'block';
113 $diskinfo->{type
} = 'file';
116 push @{$task->{disks
}}, $diskinfo;
121 my ($self, $vmid) = @_;
123 my $running = PVE
::QemuServer
::check_running
($vmid) ?
1 : 0;
125 return wantarray ?
($running, $running ?
'running' : 'stopped') : $running;
129 my ($self, $vmid) = @_;
131 $self->cmd ("qm set $vmid --lock backup");
135 my ($self, $vmid) = @_;
137 $self->cmd ("qm unlock $vmid");
141 my ($self, $task, $vmid) = @_;
143 my $opts = $self->{vzdump
}->{opts
};
145 my $wait = $opts->{stopwait
} * 60;
146 # send shutdown and wait
147 $self->cmd ("qm shutdown $vmid --skiplock --keepActive --timeout $wait");
151 my ($self, $task, $vmid) = @_;
153 $self->cmd ("qm start $vmid --skiplock");
157 my ($self, $task, $vmid) = @_;
159 $self->cmd ("qm suspend $vmid --skiplock");
163 my ($self, $task, $vmid) = @_;
165 $self->cmd ("qm resume $vmid --skiplock");
169 my ($self, $task, $vmid) = @_;
171 my $conffile = PVE
::QemuServer
::config_file
($vmid);
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";
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'";
183 while (defined (my $line = <$conffd>)) {
184 next if $line =~ m/^\#vzdump\#/; # just to be sure
185 next if $line =~ m/^\#qmdump\#/; # just to be sure
186 if ($line =~ m/^\[.*\]\s*$/) {
189 next if $found_snapshot; # skip all snapshots data
190 if ($line =~ m/^unused\d+:\s*(\S+)\s*/) {
191 $self->loginfo("skip unused drive '$1' (not included into backup)");
194 next if $line =~ m/^lock:/ || $line =~ m/^parent:/;
199 foreach my $di (@{$task->{disks
}}) {
200 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
201 my $storeid = $di->{storeid
} || '';
202 my $format = $di->{format
} || '';
203 print $outfd "#qmdump#map:$di->{virtdev}:$di->{qmdevice}:$storeid:$format:\n";
205 die "internal error";
209 if ($found_snapshot) {
210 $self->loginfo("snapshots found (not included into backup)");
213 PVE
::Tools
::file_copy
($firewall_src, $firewall_dest) if -f
$firewall_src;
217 my ($self, $task, $vmid, $filename, $comp) = @_;
219 my $conffile = "$task->{tmpdir}/qemu-server.conf";
220 my $firewall = "$task->{tmpdir}/qemu-server.fw";
222 my $opts = $self->{vzdump
}->{opts
};
224 my $starttime = time ();
227 if ($opts->{bwlimit
}) {
228 $speed = $opts->{bwlimit
}*1024;
231 my $diskcount = scalar(@{$task->{disks
}});
233 if (PVE
::QemuServer
::is_template
($self->{vmlist
}->{$vmid}) || !$diskcount) {
235 foreach my $di (@{$task->{disks
}}) {
236 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
237 push @pathlist, "$di->{qmdevice}=$di->{path}";
244 $self->loginfo("backup contains no disks");
249 $outcmd = "exec:$comp";
251 $outcmd = "exec:cat";
254 $outcmd .= ">$filename" if !$opts->{stdout
};
256 my $cmd = ['/usr/bin/vma', 'create', '-v', '-c', $conffile];
257 push @$cmd, '-c', $firewall if -e
$firewall;
258 push @$cmd, $outcmd, @pathlist;
260 $self->loginfo("starting template backup");
261 $self->loginfo(join(' ', @$cmd));
263 if ($opts->{stdout
}) {
264 $self->cmd($cmd, output
=> ">&=" . fileno($opts->{stdout
}));
274 foreach my $di (@{$task->{disks
}}) {
275 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
276 $devlist .= $devlist ?
",$di->{qmdevice}" : $di->{qmdevice
};
282 my $stop_after_backup;
283 my $resume_on_backup;
286 my $vm_is_running = PVE
::QemuServer
::check_running
($vmid);
287 if (!$vm_is_running) {
289 $self->loginfo("starting kvm to execute backup task");
290 PVE
::QemuServer
::vm_start
($self->{storecfg
}, $vmid, undef,
291 $skiplock, undef, 1);
292 if ($self->{vm_was_running
}) {
293 $resume_on_backup = 1;
295 $stop_after_backup = 1;
304 my $interrupt_msg = "interrupted by signal\n";
306 $SIG{INT
} = $SIG{TERM
} = $SIG{QUIT
} = $SIG{HUP
} = $SIG{PIPE
} = sub {
310 my $qmpclient = PVE
::QMPClient-
>new();
314 my $backup_cb = sub {
315 my ($vmid, $resp) = @_;
316 $uuid = $resp->{return}->{UUID
};
320 if ($opts->{stdout
}) {
321 $outfh = $opts->{stdout
};
323 $outfh = IO
::File-
>new($filename, "w") ||
324 die "unable to open file '$filename' - $!\n";
329 my @pipefd = POSIX
::pipe();
331 die "unable to fork worker - $!" if !defined($cpid);
334 POSIX
::close($pipefd[1]);
336 my $fd = fileno(STDIN
);
338 POSIX
::close(0) if $fd != 0;
339 die "unable to redirect STDIN - $!"
340 if !open(STDIN
, "<&", $pipefd[0]);
343 $fd = fileno(STDOUT
);
345 POSIX
::close (1) if $fd != 1;
347 die "unable to redirect STDOUT - $!"
348 if !open(STDOUT
, ">&", fileno($outfh));
351 die "fork compressor '$comp' failed\n";
360 POSIX
::close($pipefd[0]);
361 $outfileno = $pipefd[1];
364 $outfileno = fileno($outfh);
367 my $add_fd_cb = sub {
368 my ($vmid, $resp) = @_;
371 'backup-file' => "/dev/fdname/backup",
373 'config-file' => $conffile,
377 $params->{'firewall-file'} = $firewall if -e
$firewall;
378 $qmpclient->queue_cmd($vmid, $backup_cb, 'backup', %$params);
381 $qmpclient->queue_cmd($vmid, $add_fd_cb, 'getfd',
382 fd
=> $outfileno, fdname
=> "backup");
384 if ($self->{vmlist
}->{$vmid}->{agent
} && $vm_is_running){
385 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-freeze"); };
391 $qmpclient->queue_execute();
393 if ($self->{vmlist
}->{$vmid}->{agent
} && $vm_is_running ){
394 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-thaw"); };
399 die $qmpclient->{errors
}->{$vmid} if $qmpclient->{errors
}->{$vmid};
402 POSIX
::close($outfileno) == 0 ||
403 die "close output file handle failed\n";
406 die "got no uuid for backup task\n" if !$uuid;
408 $self->loginfo("started backup task '$uuid'");
410 if ($resume_on_backup) {
411 $self->loginfo("resume VM");
412 PVE
::QemuServer
::vm_mon_cmd
($vmid, 'cont');
416 my $starttime = time ();
420 my $last_transferred = 0;
421 my $last_time = time();
425 $status = PVE
::QemuServer
::vm_mon_cmd
($vmid, 'query-backup');
426 my $total = $status->{total
} || 0;
427 $transferred = $status->{transferred
} || 0;
428 my $per = $total ?
int(($transferred * 100)/$total) : 0;
429 my $zero = $status->{'zero-bytes'} || 0;
430 my $zero_per = $total ?
int(($zero * 100)/$total) : 0;
432 die "got unexpected uuid\n" if !$status->{uuid
} || ($status->{uuid
} ne $uuid);
435 my $duration = $ctime - $starttime;
437 my $rbytes = $transferred - $last_transferred;
438 my $wbytes = $rbytes - ($zero - $last_zero);
440 my $timediff = ($ctime - $last_time) || 1; # fixme
441 my $mbps_read = ($rbytes > 0) ?
442 int(($rbytes/$timediff)/(1000*1000)) : 0;
443 my $mbps_write = ($wbytes > 0) ?
444 int(($wbytes/$timediff)/(1000*1000)) : 0;
446 my $statusline = "status: $per% ($transferred/$total), " .
447 "sparse ${zero_per}% ($zero), duration $duration, " .
448 "$mbps_read/$mbps_write MB/s";
449 my $res = $status->{status
} || 'unknown';
450 if ($res ne 'active') {
451 $self->loginfo($statusline);
452 die(($status->{errmsg
} || "unknown error") . "\n")
454 die "got unexpected status '$res'\n"
456 die "got wrong number of transfered bytes ($total != $transferred)\n"
457 if ($res eq 'done') && ($total != $transferred);
461 if ($per != $last_per && ($timediff > 2)) {
462 $self->loginfo($statusline);
464 $last_total = $total if $total;
465 $last_zero = $zero if $zero;
466 $last_transferred = $transferred if $transferred;
472 my $duration = time() - $starttime;
473 if ($transferred && $duration) {
474 my $mb = int($transferred/(1000*1000));
475 my $mbps = int(($transferred/$duration)/(1000*1000));
476 $self->loginfo("transferred $mb MB in $duration seconds ($mbps MB/s)");
483 $self->loginfo("aborting backup job");
484 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, 'backup-cancel'); };
486 $self->logerr($err1);
490 if ($stop_after_backup) {
491 # stop if not running
493 my $resp = PVE
::QemuServer
::vm_mon_cmd
($vmid, 'query-status');
494 my $status = $resp && $resp->{status
} ?
$resp->{status
} : 'unknown';
495 if ($status eq 'prelaunch') {
496 $self->loginfo("stopping kvm after backup task");
497 PVE
::QemuServer
::vm_stop
($self->{storecfg
}, $vmid, $skiplock);
499 $self->loginfo("kvm status changed after backup ('$status')" .
500 " - keep VM running");
513 if ($cpid && (waitpid($cpid, 0) > 0)) {
516 my $signal = $stat & 127;
517 if ($ec || $signal) {
518 die "$comp failed - wrong exit status $ec" .
519 ($signal ?
" (signal $signal)\n" : "\n");
525 my ($self, $task, $vmid) = @_;
531 my ($self, $task, $vmid) = @_;