]>
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->{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
::QemuConfig-
>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'";
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) {
195 next if $found_snapshot; # skip all snapshots data
196 next if $found_pending; # skip all pending changes
198 if ($line =~ m/^unused\d+:\s*(\S+)\s*/) {
199 $self->loginfo("skip unused drive '$1' (not included into backup)");
202 next if $line =~ m/^lock:/ || $line =~ m/^parent:/;
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";
213 die "internal error";
217 if ($found_snapshot) {
218 $self->loginfo("snapshots found (not included into backup)");
221 if ($found_pending) {
222 $self->loginfo("pending configuration changes found (not included into backup)");
225 PVE
::Tools
::file_copy
($firewall_src, $firewall_dest) if -f
$firewall_src;
229 my ($self, $task, $vmid, $filename, $comp) = @_;
231 my $conffile = "$task->{tmpdir}/qemu-server.conf";
232 my $firewall = "$task->{tmpdir}/qemu-server.fw";
234 my $opts = $self->{vzdump
}->{opts
};
236 my $starttime = time ();
239 if ($opts->{bwlimit
}) {
240 $speed = $opts->{bwlimit
}*1024;
243 my $diskcount = scalar(@{$task->{disks
}});
245 if (PVE
::QemuConfig-
>is_template($self->{vmlist
}->{$vmid}) || !$diskcount) {
247 foreach my $di (@{$task->{disks
}}) {
248 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
249 push @pathlist, "$di->{qmdevice}=$di->{path}";
256 $self->loginfo("backup contains no disks");
261 $outcmd = "exec:$comp";
263 $outcmd = "exec:cat";
266 $outcmd .= ">$filename" if !$opts->{stdout
};
268 my $cmd = ['/usr/bin/vma', 'create', '-v', '-c', $conffile];
269 push @$cmd, '-c', $firewall if -e
$firewall;
270 push @$cmd, $outcmd, @pathlist;
272 $self->loginfo("starting template backup");
273 $self->loginfo(join(' ', @$cmd));
275 if ($opts->{stdout
}) {
276 $self->cmd($cmd, output
=> ">&=" . fileno($opts->{stdout
}));
286 foreach my $di (@{$task->{disks
}}) {
287 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
288 $devlist .= $devlist ?
",$di->{qmdevice}" : $di->{qmdevice
};
294 my $stop_after_backup;
295 my $resume_on_backup;
298 my $vm_is_running = PVE
::QemuServer
::check_running
($vmid);
299 if (!$vm_is_running) {
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;
307 $stop_after_backup = 1;
316 my $interrupt_msg = "interrupted by signal\n";
318 $SIG{INT
} = $SIG{TERM
} = $SIG{QUIT
} = $SIG{HUP
} = $SIG{PIPE
} = sub {
322 my $qmpclient = PVE
::QMPClient-
>new();
326 my $backup_cb = sub {
327 my ($vmid, $resp) = @_;
328 $uuid = $resp->{return}->{UUID
};
332 if ($opts->{stdout
}) {
333 $outfh = $opts->{stdout
};
335 $outfh = IO
::File-
>new($filename, "w") ||
336 die "unable to open file '$filename' - $!\n";
341 my @pipefd = POSIX
::pipe();
343 die "unable to fork worker - $!" if !defined($cpid);
346 POSIX
::close($pipefd[1]);
348 my $fd = fileno(STDIN
);
350 POSIX
::close(0) if $fd != 0;
351 die "unable to redirect STDIN - $!"
352 if !open(STDIN
, "<&", $pipefd[0]);
355 $fd = fileno(STDOUT
);
357 POSIX
::close (1) if $fd != 1;
359 die "unable to redirect STDOUT - $!"
360 if !open(STDOUT
, ">&", fileno($outfh));
363 die "fork compressor '$comp' failed\n";
372 POSIX
::close($pipefd[0]);
373 $outfileno = $pipefd[1];
376 $outfileno = fileno($outfh);
379 my $add_fd_cb = sub {
380 my ($vmid, $resp) = @_;
383 'backup-file' => "/dev/fdname/backup",
385 'config-file' => $conffile,
389 $params->{'firewall-file'} = $firewall if -e
$firewall;
390 $qmpclient->queue_cmd($vmid, $backup_cb, 'backup', %$params);
393 $qmpclient->queue_cmd($vmid, $add_fd_cb, 'getfd',
394 fd
=> $outfileno, fdname
=> "backup");
396 if ($self->{vmlist
}->{$vmid}->{agent
} && $vm_is_running){
397 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-freeze"); };
403 $qmpclient->queue_execute();
405 if ($self->{vmlist
}->{$vmid}->{agent
} && $vm_is_running ){
406 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-thaw"); };
411 die $qmpclient->{errors
}->{$vmid} if $qmpclient->{errors
}->{$vmid};
414 POSIX
::close($outfileno) == 0 ||
415 die "close output file handle failed\n";
418 die "got no uuid for backup task\n" if !$uuid;
420 $self->loginfo("started backup task '$uuid'");
422 if ($resume_on_backup) {
423 $self->loginfo("resume VM");
424 PVE
::QemuServer
::vm_mon_cmd
($vmid, 'cont');
428 my $starttime = time ();
432 my $last_transferred = 0;
433 my $last_time = time();
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;
444 die "got unexpected uuid\n" if !$status->{uuid
} || ($status->{uuid
} ne $uuid);
447 my $duration = $ctime - $starttime;
449 my $rbytes = $transferred - $last_transferred;
450 my $wbytes = $rbytes - ($zero - $last_zero);
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;
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")
466 die "got unexpected status '$res'\n"
468 die "got wrong number of transfered bytes ($total != $transferred)\n"
469 if ($res eq 'done') && ($total != $transferred);
473 if ($per != $last_per && ($timediff > 2)) {
474 $self->loginfo($statusline);
476 $last_total = $total if $total;
477 $last_zero = $zero if $zero;
478 $last_transferred = $transferred if $transferred;
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)");
495 $self->loginfo("aborting backup job");
496 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, 'backup-cancel'); };
498 $self->logerr($err1);
502 if ($stop_after_backup) {
503 # stop if not running
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);
511 $self->loginfo("kvm status changed after backup ('$status')" .
512 " - keep VM running");
525 if ($cpid && (waitpid($cpid, 0) > 0)) {
528 my $signal = $stat & 127;
529 if ($ec || $signal) {
530 die "$comp failed - wrong exit status $ec" .
531 ($signal ?
" (signal $signal)\n" : "\n");
537 my ($self, $task, $vmid) = @_;
543 my ($self, $task, $vmid) = @_;