]>
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
::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 if (defined($drive->{backup
}) && !$drive->{backup
}) {
71 $self->loginfo("exclude disk '$ds' (backup=no)");
73 } elsif ($drive->{iothread
}) {
74 die "disk '$ds' (iothread=on) can't use backup feature currently. Please set backup=no for this drive";
77 my $volid = $drive->{file
};
79 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
80 push @$vollist, $volid if $storeid;
81 $drivehash->{$ds} = $drive;
84 PVE
::Storage
::activate_volumes
($self->{storecfg
}, $vollist);
86 foreach my $ds (sort keys %$drivehash) {
87 my $drive = $drivehash->{$ds};
89 my $volid = $drive->{file
};
93 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
95 $path = PVE
::Storage
::path
($self->{storecfg
}, $volid);
106 ($size, $format) = PVE
::Storage
::volume_size_info
($self->{storecfg
}, $volid, 5);
108 die "no such volume '$volid'\n" if $@;
110 my $diskinfo = { path
=> $path , volid
=> $volid, storeid
=> $storeid,
111 format
=> $format, virtdev
=> $ds, qmdevice
=> "drive-$ds" };
114 $diskinfo->{type
} = 'block';
116 $diskinfo->{type
} = 'file';
119 push @{$task->{disks
}}, $diskinfo;
124 my ($self, $vmid) = @_;
126 my $running = PVE
::QemuServer
::check_running
($vmid) ?
1 : 0;
128 return wantarray ?
($running, $running ?
'running' : 'stopped') : $running;
132 my ($self, $vmid) = @_;
134 $self->cmd ("qm set $vmid --lock backup");
138 my ($self, $vmid) = @_;
140 $self->cmd ("qm unlock $vmid");
144 my ($self, $task, $vmid) = @_;
146 my $opts = $self->{vzdump
}->{opts
};
148 my $wait = $opts->{stopwait
} * 60;
149 # send shutdown and wait
150 $self->cmd ("qm shutdown $vmid --skiplock --keepActive --timeout $wait");
154 my ($self, $task, $vmid) = @_;
156 $self->cmd ("qm start $vmid --skiplock");
160 my ($self, $task, $vmid) = @_;
162 $self->cmd ("qm suspend $vmid --skiplock");
166 my ($self, $task, $vmid) = @_;
168 $self->cmd ("qm resume $vmid --skiplock");
172 my ($self, $task, $vmid) = @_;
174 my $conffile = PVE
::QemuConfig-
>config_file($vmid);
176 my $outfile = "$task->{tmpdir}/qemu-server.conf";
177 my $firewall_src = "/etc/pve/firewall/$vmid.fw";
178 my $firewall_dest = "$task->{tmpdir}/qemu-server.fw";
180 my $outfd = IO
::File-
>new (">$outfile") ||
181 die "unable to open '$outfile'";
182 my $conffd = IO
::File-
>new ($conffile, 'r') ||
183 die "unable open '$conffile'";
187 while (defined (my $line = <$conffd>)) {
188 next if $line =~ m/^\#vzdump\#/; # just to be sure
189 next if $line =~ m/^\#qmdump\#/; # just to be sure
190 if ($line =~ m/^\[(.*)\]\s*$/) {
191 if ($1 =~ m/PENDING/i) {
198 next if $found_snapshot; # skip all snapshots data
199 next if $found_pending; # skip all pending changes
201 if ($line =~ m/^unused\d+:\s*(\S+)\s*/) {
202 $self->loginfo("skip unused drive '$1' (not included into backup)");
205 next if $line =~ m/^lock:/ || $line =~ m/^parent:/;
210 foreach my $di (@{$task->{disks
}}) {
211 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
212 my $storeid = $di->{storeid
} || '';
213 my $format = $di->{format
} || '';
214 print $outfd "#qmdump#map:$di->{virtdev}:$di->{qmdevice}:$storeid:$format:\n";
216 die "internal error";
220 if ($found_snapshot) {
221 $self->loginfo("snapshots found (not included into backup)");
224 if ($found_pending) {
225 $self->loginfo("pending configuration changes found (not included into backup)");
228 PVE
::Tools
::file_copy
($firewall_src, $firewall_dest) if -f
$firewall_src;
232 my ($self, $task, $vmid, $filename, $comp) = @_;
234 my $conffile = "$task->{tmpdir}/qemu-server.conf";
235 my $firewall = "$task->{tmpdir}/qemu-server.fw";
237 my $opts = $self->{vzdump
}->{opts
};
239 my $starttime = time ();
242 if ($opts->{bwlimit
}) {
243 $speed = $opts->{bwlimit
}*1024;
246 my $diskcount = scalar(@{$task->{disks
}});
248 if (PVE
::QemuConfig-
>is_template($self->{vmlist
}->{$vmid}) || !$diskcount) {
250 foreach my $di (@{$task->{disks
}}) {
251 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
252 push @pathlist, "$di->{qmdevice}=$di->{path}";
259 $self->loginfo("backup contains no disks");
264 $outcmd = "exec:$comp";
266 $outcmd = "exec:cat";
269 $outcmd .= ">$filename" if !$opts->{stdout
};
271 my $cmd = ['/usr/bin/vma', 'create', '-v', '-c', $conffile];
272 push @$cmd, '-c', $firewall if -e
$firewall;
273 push @$cmd, $outcmd, @pathlist;
275 $self->loginfo("starting template backup");
276 $self->loginfo(join(' ', @$cmd));
278 if ($opts->{stdout
}) {
279 $self->cmd($cmd, output
=> ">&=" . fileno($opts->{stdout
}));
289 foreach my $di (@{$task->{disks
}}) {
290 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
291 $devlist .= $devlist ?
",$di->{qmdevice}" : $di->{qmdevice
};
297 my $stop_after_backup;
298 my $resume_on_backup;
301 my $vm_is_running = PVE
::QemuServer
::check_running
($vmid);
302 if (!$vm_is_running) {
304 $self->loginfo("starting kvm to execute backup task");
305 PVE
::QemuServer
::vm_start
($self->{storecfg
}, $vmid, undef,
306 $skiplock, undef, 1);
307 if ($self->{vm_was_running
}) {
308 $resume_on_backup = 1;
310 $stop_after_backup = 1;
319 my $interrupt_msg = "interrupted by signal\n";
321 $SIG{INT
} = $SIG{TERM
} = $SIG{QUIT
} = $SIG{HUP
} = $SIG{PIPE
} = sub {
325 my $qmpclient = PVE
::QMPClient-
>new();
329 my $backup_cb = sub {
330 my ($vmid, $resp) = @_;
331 $uuid = $resp->{return}->{UUID
};
335 if ($opts->{stdout
}) {
336 $outfh = $opts->{stdout
};
338 $outfh = IO
::File-
>new($filename, "w") ||
339 die "unable to open file '$filename' - $!\n";
344 my @pipefd = POSIX
::pipe();
346 die "unable to fork worker - $!" if !defined($cpid);
349 POSIX
::close($pipefd[1]);
351 my $fd = fileno(STDIN
);
353 POSIX
::close(0) if $fd != 0;
354 die "unable to redirect STDIN - $!"
355 if !open(STDIN
, "<&", $pipefd[0]);
358 $fd = fileno(STDOUT
);
360 POSIX
::close (1) if $fd != 1;
362 die "unable to redirect STDOUT - $!"
363 if !open(STDOUT
, ">&", fileno($outfh));
366 die "fork compressor '$comp' failed\n";
375 POSIX
::close($pipefd[0]);
376 $outfileno = $pipefd[1];
379 $outfileno = fileno($outfh);
382 my $add_fd_cb = sub {
383 my ($vmid, $resp) = @_;
386 'backup-file' => "/dev/fdname/backup",
388 'config-file' => $conffile,
392 $params->{'firewall-file'} = $firewall if -e
$firewall;
393 $qmpclient->queue_cmd($vmid, $backup_cb, 'backup', %$params);
396 $qmpclient->queue_cmd($vmid, $add_fd_cb, 'getfd',
397 fd
=> $outfileno, fdname
=> "backup");
399 if ($self->{vmlist
}->{$vmid}->{agent
} && $vm_is_running){
400 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-freeze"); };
406 $qmpclient->queue_execute();
408 if ($self->{vmlist
}->{$vmid}->{agent
} && $vm_is_running ){
409 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-thaw"); };
414 die $qmpclient->{errors
}->{$vmid} if $qmpclient->{errors
}->{$vmid};
417 POSIX
::close($outfileno) == 0 ||
418 die "close output file handle failed\n";
421 die "got no uuid for backup task\n" if !$uuid;
423 $self->loginfo("started backup task '$uuid'");
425 if ($resume_on_backup) {
426 $self->loginfo("resume VM");
427 PVE
::QemuServer
::vm_mon_cmd
($vmid, 'cont');
431 my $starttime = time ();
435 my $last_transferred = 0;
436 my $last_time = time();
440 $status = PVE
::QemuServer
::vm_mon_cmd
($vmid, 'query-backup');
441 my $total = $status->{total
} || 0;
442 $transferred = $status->{transferred
} || 0;
443 my $per = $total ?
int(($transferred * 100)/$total) : 0;
444 my $zero = $status->{'zero-bytes'} || 0;
445 my $zero_per = $total ?
int(($zero * 100)/$total) : 0;
447 die "got unexpected uuid\n" if !$status->{uuid
} || ($status->{uuid
} ne $uuid);
450 my $duration = $ctime - $starttime;
452 my $rbytes = $transferred - $last_transferred;
453 my $wbytes = $rbytes - ($zero - $last_zero);
455 my $timediff = ($ctime - $last_time) || 1; # fixme
456 my $mbps_read = ($rbytes > 0) ?
457 int(($rbytes/$timediff)/(1000*1000)) : 0;
458 my $mbps_write = ($wbytes > 0) ?
459 int(($wbytes/$timediff)/(1000*1000)) : 0;
461 my $statusline = "status: $per% ($transferred/$total), " .
462 "sparse ${zero_per}% ($zero), duration $duration, " .
463 "$mbps_read/$mbps_write MB/s";
464 my $res = $status->{status
} || 'unknown';
465 if ($res ne 'active') {
466 $self->loginfo($statusline);
467 die(($status->{errmsg
} || "unknown error") . "\n")
469 die "got unexpected status '$res'\n"
471 die "got wrong number of transfered bytes ($total != $transferred)\n"
472 if ($res eq 'done') && ($total != $transferred);
476 if ($per != $last_per && ($timediff > 2)) {
477 $self->loginfo($statusline);
479 $last_total = $total if $total;
480 $last_zero = $zero if $zero;
481 $last_transferred = $transferred if $transferred;
487 my $duration = time() - $starttime;
488 if ($transferred && $duration) {
489 my $mb = int($transferred/(1000*1000));
490 my $mbps = int(($transferred/$duration)/(1000*1000));
491 $self->loginfo("transferred $mb MB in $duration seconds ($mbps MB/s)");
498 $self->loginfo("aborting backup job");
499 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, 'backup-cancel'); };
501 $self->logerr($err1);
505 if ($stop_after_backup) {
506 # stop if not running
508 my $resp = PVE
::QemuServer
::vm_mon_cmd
($vmid, 'query-status');
509 my $status = $resp && $resp->{status
} ?
$resp->{status
} : 'unknown';
510 if ($status eq 'prelaunch') {
511 $self->loginfo("stopping kvm after backup task");
512 PVE
::QemuServer
::vm_stop
($self->{storecfg
}, $vmid, $skiplock);
514 $self->loginfo("kvm status changed after backup ('$status')" .
515 " - keep VM running");
528 if ($cpid && (waitpid($cpid, 0) > 0)) {
531 my $signal = $stat & 127;
532 if ($ec || $signal) {
533 die "$comp failed - wrong exit status $ec" .
534 ($signal ?
" (signal $signal)\n" : "\n");
540 my ($self, $task, $vmid) = @_;
546 my ($self, $task, $vmid) = @_;