]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/VZDump/QemuServer.pm
fix #1779: vzdump: ensure guest-fsfreeze-thaw is called on error
[qemu-server.git] / PVE / VZDump / QemuServer.pm
index 745ed635f480cf4fadd836fbeb0fb7ae6b9b99e2..70abe994b67dc1b4d4985e19b30356d9af80e7cc 100644 (file)
@@ -6,20 +6,24 @@ use File::Path;
 use File::Basename;
 use PVE::INotify;
 use PVE::VZDump;
+use PVE::IPCC;
 use PVE::Cluster qw(cfs_read_file);
 use PVE::Tools;
+use PVE::Storage::Plugin;
 use PVE::Storage;
 use PVE::QemuServer;
+use PVE::JSONSchema;
 use IO::File;
+use IPC::Open3;
 
 use base qw (PVE::VZDump::Plugin);
 
 sub new {
     my ($class, $vzdump) = @_;
-    
+
     PVE::VZDump::check_bin('qm');
 
-    my $self = bless { vzdump => $vzdump };
+    my $self = bless { vzdump => $vzdump }, $class;
 
     $self->{vmlist} = PVE::QemuServer::vzlist();
     $self->{storecfg} = PVE::Storage::config();
@@ -43,135 +47,93 @@ sub prepare {
 
     $task->{disks} = [];
 
-    my $conf = $self->{vmlist}->{$vmid} = PVE::QemuServer::load_config($vmid);
-
-    $task->{hostname} = $conf->{name};
+    my $conf = $self->{vmlist}->{$vmid} = PVE::QemuConfig->load_config($vmid);
 
-    my $lvmmap = PVE::VZDump::get_lvm_mapping();
+    $self->loginfo("VM Name: $conf->{name}")
+       if defined($conf->{name});
 
-    my $hostname = PVE::INotify::nodename(); 
+    $self->{vm_was_running} = 1;
+    if (!PVE::QemuServer::check_running($vmid)) {
+       $self->{vm_was_running} = 0;
+    }
 
-    my $ind = {};
-    my $mountinfo = {};
-    my $mountind = 0;
+    $task->{hostname} = $conf->{name};
 
-    my $snapshot_count = 0;
+    my $hostname = PVE::INotify::nodename();
 
+    my $vollist = [];
+    my $drivehash = {};
     PVE::QemuServer::foreach_drive($conf, sub {
        my ($ds, $drive) = @_;
 
-       return if PVE::QemuServer::drive_is_cdrom ($drive);
+       return if PVE::QemuServer::drive_is_cdrom($drive);
 
-       if (defined($drive->{backup}) && $drive->{backup} eq "no") {
-           $self->loginfo("exclude disk '$ds' (backup=no)");
+       my $volid = $drive->{file};
+
+       if (defined($drive->{backup}) && !$drive->{backup}) {
+           $self->loginfo("exclude disk '$ds' '$volid' (backup=no)");
            return;
-       }          
+       } elsif ($drive->{iothread}) {
+           die "disk '$ds' '$volid' (iothread=on) can't use backup feature currently. Please set backup=no for this drive";
+       } else {
+           my $log = "include disk '$ds' '$volid'";
+          if (defined $drive->{size}) {
+               my $readable_size = PVE::JSONSchema::format_size($drive->{size});
+               $log .= " $readable_size";
+          }
+           $self->loginfo($log);
+       }
+
+       my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
+       push @$vollist, $volid if $storeid;
+       $drivehash->{$ds} = $drive;
+    });
+
+    PVE::Storage::activate_volumes($self->{storecfg}, $vollist);
+
+    foreach my $ds (sort keys %$drivehash) {
+       my $drive = $drivehash->{$ds};
+
        my $volid = $drive->{file};
 
        my $path;
 
-       my ($storeid, $volname) = PVE::Storage::parse_volume_id ($volid, 1);
+       my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
        if ($storeid) {
-           PVE::Storage::activate_storage ($self->{storecfg}, $storeid);
-           $path = PVE::Storage::path ($self->{storecfg}, $volid);
+           $path = PVE::Storage::path($self->{storecfg}, $volid);
        } else {
            $path = $volid;
        }
 
-       return if !$path;
+       next if !$path;
 
-       die "no such volume '$volid'\n" if ! -e $path;
+       my $format = undef;
+       my $size = undef;
 
-       my $diskinfo = { path => $path , volid => $volid, storeid => $storeid, 
-                        snappath => $path, virtdev => $ds };
+       eval{
+           ($size, $format) = PVE::Storage::volume_size_info($self->{storecfg}, $volid, 5);
+       };
+       die "no such volume '$volid'\n" if $@;
 
-       if (-b $path) {
+       my $diskinfo = { path => $path , volid => $volid, storeid => $storeid,
+                        format => $format, virtdev => $ds, qmdevice => "drive-$ds" };
 
+       if (-b $path) {
            $diskinfo->{type} = 'block';
-
-           $diskinfo->{filename} = "vm-disk-$ds.raw";
-
-           if ($mode eq 'snapshot') {
-               my ($lvmvg, $lvmlv) = @{$lvmmap->{$path}} if defined ($lvmmap->{$path});
-               die ("mode failure - unable to detect lvm volume group\n") if !$lvmvg;
-
-               $ind->{$lvmvg} = 0 if !defined $ind->{$lvmvg};
-               $diskinfo->{snapname} = "vzsnap-$hostname-$ind->{$lvmvg}";
-               $diskinfo->{snapdev} = "/dev/$lvmvg/$diskinfo->{snapname}";
-               $diskinfo->{lvmvg} = $lvmvg;
-               $diskinfo->{lvmlv} = $lvmlv;
-               $diskinfo->{snappath} = $diskinfo->{snapdev};
-               $ind->{$lvmvg}++;
-
-               $snapshot_count++;
-           }
-
        } else {
-
            $diskinfo->{type} = 'file';
-
-           my (undef, $dir, $ext) = fileparse ($path, qr/\.[^.]*/);
-
-           $diskinfo->{filename} = "vm-disk-$ds$ext";
-
-           if ($mode eq 'snapshot') {
-           
-               my ($srcdev, $lvmpath, $lvmvg, $lvmlv, $fstype) =
-                   PVE::VZDump::get_lvm_device ($dir, $lvmmap);
-
-               my $targetdev = PVE::VZDump::get_lvm_device ($task->{dumpdir}, $lvmmap);
-
-               die ("mode failure - unable to detect lvm volume group\n") if !$lvmvg;
-               die ("mode failure - wrong lvm mount point '$lvmpath'\n") if $dir !~ m|/?$lvmpath/?|;
-               die ("mode failure - unable to dump into snapshot (use option --dumpdir)\n") 
-                   if $targetdev eq $srcdev;
-               
-               $ind->{$lvmvg} = 0 if !defined $ind->{$lvmvg};
-                   
-               my $info = $mountinfo->{$lvmpath};
-               if (!$info) {
-                   my $snapname = "vzsnap-$hostname-$ind->{$lvmvg}";
-                   my $snapdev = "/dev/$lvmvg/$snapname";
-                   $mountinfo->{$lvmpath} = $info = {
-                       snapdev => $snapdev,
-                       snapname => $snapname,
-                       mountpoint => "/mnt/vzsnap$mountind",
-                   };
-                   $ind->{$lvmvg}++;
-                   $mountind++;
-
-                   $snapshot_count++;
-               } 
-
-               $diskinfo->{snapdev} = $info->{snapdev};
-               $diskinfo->{snapname} = $info->{snapname};
-               $diskinfo->{mountpoint} = $info->{mountpoint};
-               
-               $diskinfo->{lvmvg} = $lvmvg;
-               $diskinfo->{lvmlv} = $lvmlv;
-               
-               $diskinfo->{fstype}  = $fstype;
-               $diskinfo->{lvmpath} = $lvmpath;
-
-               $diskinfo->{snappath} = $path;
-               $diskinfo->{snappath} =~ s|/?$lvmpath/?|$diskinfo->{mountpoint}/|;
-           }
        }
 
        push @{$task->{disks}}, $diskinfo;
-
-    });
-
-    $task->{snapshot_count} = $snapshot_count;
+    }
 }
 
 sub vm_status {
     my ($self, $vmid) = @_;
 
     my $running = PVE::QemuServer::check_running($vmid) ? 1 : 0;
-  
-    return wantarray ? ($running, $running ? 'running' : 'stopped') : $running; 
+
+    return wantarray ? ($running, $running ? 'running' : 'stopped') : $running;
 }
 
 sub lock_vm {
@@ -214,216 +176,392 @@ sub resume_vm {
     $self->cmd ("qm resume $vmid --skiplock");
 }
 
-sub snapshot_alloc {
-    my ($self, $storeid, $name, $size, $srcdev) = @_;
+sub assemble {
+    my ($self, $task, $vmid) = @_;
 
-    my $cmd = "lvcreate --size ${size}M --snapshot --name '$name' '$srcdev'";
+    my $conffile = PVE::QemuConfig->config_file($vmid);
 
-    if ($storeid) {
+    my $outfile = "$task->{tmpdir}/qemu-server.conf";
+    my $firewall_src = "/etc/pve/firewall/$vmid.fw";
+    my $firewall_dest = "$task->{tmpdir}/qemu-server.fw";
+
+    my $outfd = IO::File->new (">$outfile") ||
+       die "unable to open '$outfile'";
+    my $conffd = IO::File->new ($conffile, 'r') ||
+       die "unable open '$conffile'";
+
+    my $found_snapshot;
+    my $found_pending;
+    while (defined (my $line = <$conffd>)) {
+       next if $line =~ m/^\#vzdump\#/; # just to be sure
+       next if $line =~ m/^\#qmdump\#/; # just to be sure
+       if ($line =~ m/^\[(.*)\]\s*$/) {
+           if ($1 =~ m/PENDING/i) {
+               $found_pending = 1;
+           } else {
+               $found_snapshot = 1;
+           }
+       }
 
-       my $scfg = PVE::Storage::storage_config ($self->{storecfg}, $storeid);
+       next if $found_snapshot; # skip all snapshots data
+       next if $found_pending; # skip all pending changes
 
-       # lock shared storage
-       return PVE::Storage::cluster_lock_storage ($storeid, $scfg->{shared}, undef, sub {
-               $self->cmd ($cmd);
-       });
-    } else {
-       $self->cmd ($cmd);
+       if ($line =~ m/^unused\d+:\s*(\S+)\s*/) {
+           $self->loginfo("skip unused drive '$1' (not included into backup)");
+           next;
+       }
+       next if $line =~ m/^lock:/ || $line =~ m/^parent:/;
+
+       print $outfd $line;
     }
-}
 
-sub snapshot_free {
-    my ($self, $storeid, $name, $snapdev, $noerr) = @_;
+    foreach my $di (@{$task->{disks}}) {
+       if ($di->{type} eq 'block' || $di->{type} eq 'file') {
+           my $storeid = $di->{storeid} || '';
+           my $format = $di->{format} || '';
+           print $outfd "#qmdump#map:$di->{virtdev}:$di->{qmdevice}:$storeid:$format:\n";
+       } else {
+           die "internal error";
+       }
+    }
 
-    my $cmd = ['lvremove', '-f', $snapdev];
+    if ($found_snapshot) {
+       $self->loginfo("snapshots found (not included into backup)");
+    }
 
-    # loop, because we often get 'LV in use: not deactivating'
-    # we use run_command() because we do not want to log errors here
-    my $wait = 1;
-    while(-b $snapdev) {
-       eval {
-           if ($storeid) {
-               my $scfg = PVE::Storage::storage_config ($self->{storecfg}, $storeid);
-               # lock shared storage
-               return PVE::Storage::cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
-                   PVE::Tools::run_command($cmd, outfunc => sub {}, errfunc => {});
-               });
-           } else {
-               PVE::Tools::run_command($cmd, outfunc => sub {}, errfunc => {});
-           }
-       };
-       my $err = $@;
-       last if !$err;
-       if ($wait >= 64) {
-           $self->logerr($err);
-           die $@ if !$noerr;
-           last;
-       }
-       $self->loginfo("lvremove failed - trying again in $wait seconds") if $wait >= 8;
-       sleep($wait);
-       $wait = $wait*2;
+    if ($found_pending) {
+       $self->loginfo("pending configuration changes found (not included into backup)");
     }
+
+    PVE::Tools::file_copy($firewall_src, $firewall_dest) if -f $firewall_src;
 }
 
-sub snapshot {
-    my ($self, $task, $vmid) = @_;
+sub archive {
+    my ($self, $task, $vmid, $filename, $comp) = @_;
+
+    my $conffile = "$task->{tmpdir}/qemu-server.conf";
+    my $firewall = "$task->{tmpdir}/qemu-server.fw";
 
     my $opts = $self->{vzdump}->{opts};
 
-    my $mounts = {};
+    my $starttime = time ();
 
-    foreach my $di (@{$task->{disks}}) {
-       if ($di->{type} eq 'block') {
+    my $speed = 0;
+    if ($opts->{bwlimit}) {
+       $speed = $opts->{bwlimit}*1024;
+    }
 
-           if (-b $di->{snapdev}) {
-               $self->loginfo ("trying to remove stale snapshot '$di->{snapdev}'");
-               $self->snapshot_free ($di->{storeid}, $di->{snapname}, $di->{snapdev}, 1); 
-           }
+    my $diskcount = scalar(@{$task->{disks}});
 
-           $di->{cleanup_lvm} = 1;
-           $self->snapshot_alloc ($di->{storeid}, $di->{snapname}, $opts->{size},
-                                  "/dev/$di->{lvmvg}/$di->{lvmlv}"); 
+    if (PVE::QemuConfig->is_template($self->{vmlist}->{$vmid}) || !$diskcount) {
+       my @pathlist;
+       foreach my $di (@{$task->{disks}}) {
+           if ($di->{type} eq 'block' || $di->{type} eq 'file') {
+               push @pathlist, "$di->{qmdevice}=$di->{path}";
+           } else {
+               die "implement me";
+           }
+       }
 
-       } elsif ($di->{type} eq 'file') {
+       if (!$diskcount) {
+           $self->loginfo("backup contains no disks");
+       }
 
-           next if defined ($mounts->{$di->{mountpoint}}); # already mounted
+       my $outcmd;
+       if ($comp) {
+           $outcmd = "exec:$comp";
+       } else {
+           $outcmd = "exec:cat";
+       }
 
-           if (-b $di->{snapdev}) {
-               $self->loginfo ("trying to remove stale snapshot '$di->{snapdev}'");        
-           
-               $self->cmd_noerr ("umount $di->{mountpoint}");
-               $self->snapshot_free ($di->{storeid}, $di->{snapname}, $di->{snapdev}, 1); 
-           }
+       $outcmd .= " > $filename" if !$opts->{stdout};
 
-           mkpath $di->{mountpoint}; # create mount point for lvm snapshot
+       my $cmd = ['/usr/bin/vma', 'create', '-v', '-c', $conffile];
+       push @$cmd, '-c', $firewall if -e $firewall;
+       push @$cmd, $outcmd, @pathlist;
 
-           $di->{cleanup_lvm} = 1;
+       $self->loginfo("starting template backup");
+       $self->loginfo(join(' ', @$cmd));
 
-           $self->snapshot_alloc ($di->{storeid}, $di->{snapname}, $opts->{size},
-                                  "/dev/$di->{lvmvg}/$di->{lvmlv}"); 
-           
-           my $mopts = $di->{fstype} eq 'xfs' ? "-o nouuid" : '';
-
-           $di->{snapshot_mount} = 1;
+       if ($opts->{stdout}) {
+           $self->cmd($cmd, output => ">&=" . fileno($opts->{stdout}));
+       } else {
+           $self->cmd($cmd);
+       }
 
-           $self->cmd ("mount -n -t $di->{fstype} $mopts $di->{snapdev} $di->{mountpoint}");
+       return;
+    }
 
-           $mounts->{$di->{mountpoint}} = 1;
 
+    my $devlist = '';
+    foreach my $di (@{$task->{disks}}) {
+       if ($di->{type} eq 'block' || $di->{type} eq 'file') {
+           $devlist .= $devlist ? ",$di->{qmdevice}" : $di->{qmdevice};
        } else {
            die "implement me";
        }
     }
-}
 
-sub get_size {
-    my $path = shift;
-
-    if (-f $path) {
-       return -s $path;
-    } elsif (-b $path) {
-       my $fh = IO::File->new ($path, "r");
-       die "unable to open '$path' to detect device size\n" if !$fh;
-       my $size = sysseek $fh, 0, 2;
-       $fh->close();
-       die "unable to detect device size for '$path'\n" if !$size;
-       return $size;
+    my $stop_after_backup;
+    my $resume_on_backup;
+
+    my $skiplock = 1;
+    my $vm_is_running = PVE::QemuServer::check_running($vmid);
+    if (!$vm_is_running) {
+       eval {
+           $self->loginfo("starting kvm to execute backup task");
+           PVE::QemuServer::vm_start($self->{storecfg}, $vmid, undef,
+                                     $skiplock, undef, 1);
+           if ($self->{vm_was_running}) {
+               $resume_on_backup = 1;
+           } else {
+               $stop_after_backup = 1;
+           }
+       };
+       if (my $err = $@) {
+           die $err;
+       }
     }
-}
 
-sub assemble {
-    my ($self, $task, $vmid) = @_;
+    my $cpid;
+    my $interrupt_msg = "interrupted by signal\n";
+    eval {
+       $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub {
+           die $interrupt_msg;
+       };
 
-    my $conffile = PVE::QemuServer::config_file ($vmid);
+       my $qmpclient = PVE::QMPClient->new();
 
-    my $outfile = "$task->{tmpdir}/qemu-server.conf";
+       my $uuid;
 
-    my $outfd;
-    my $conffd;
+       my $backup_cb = sub {
+           my ($vmid, $resp) = @_;
+           $uuid = $resp->{return}->{UUID};
+       };
 
-    eval {
+       my $outfh;
+       if ($opts->{stdout}) {
+           $outfh = $opts->{stdout};
+       } else {
+           $outfh = IO::File->new($filename, "w") ||
+               die "unable to open file '$filename' - $!\n";
+       }
+
+       my $outfileno;
+       if ($comp) {
+           my @pipefd = POSIX::pipe();
+           $cpid = fork();
+           die "unable to fork worker - $!" if !defined($cpid);
+           if ($cpid == 0) {
+               eval {
+                   POSIX::close($pipefd[1]);
+                   # redirect STDIN
+                   my $fd = fileno(STDIN);
+                   close STDIN;
+                   POSIX::close(0) if $fd != 0;
+                   die "unable to redirect STDIN - $!"
+                       if !open(STDIN, "<&", $pipefd[0]);
+
+                   # redirect STDOUT
+                   $fd = fileno(STDOUT);
+                   close STDOUT;
+                   POSIX::close (1) if $fd != 1;
+
+                   die "unable to redirect STDOUT - $!"
+                       if !open(STDOUT, ">&", fileno($outfh));
+
+                   exec($comp);
+                   die "fork compressor '$comp' failed\n";
+               };
+               if (my $err = $@) {
+                   $self->logerr($err);
+                   POSIX::_exit(1);
+               }
+               POSIX::_exit(0);
+               kill(-9, $$);
+           } else {
+               POSIX::close($pipefd[0]);
+               $outfileno = $pipefd[1];
+           }
+       } else {
+           $outfileno = fileno($outfh);
+       }
 
-       $outfd = IO::File->new (">$outfile") ||
-           die "unable to open '$outfile'";
-       $conffd = IO::File->new ($conffile, 'r') ||
-           die "unable open '$conffile'";
+       my $add_fd_cb = sub {
+           my ($vmid, $resp) = @_;
 
-       while (defined (my $line = <$conffd>)) {
-           next if $line =~ m/^\#vzdump\#/; # just to be sure
-           print $outfd $line;
+           my $params = {
+               'backup-file' => "/dev/fdname/backup",
+               speed => $speed,
+               'config-file' => $conffile,
+               devlist => $devlist
+           };
+           
+           $params->{'firewall-file'} = $firewall if -e $firewall;
+           $qmpclient->queue_cmd($vmid, $backup_cb, 'backup', %$params);
+       };
+
+       $qmpclient->queue_cmd($vmid, $add_fd_cb, 'getfd',
+                             fd => $outfileno, fdname => "backup");
+
+       my $agent_running = 0;
+
+       if ($self->{vmlist}->{$vmid}->{agent} && $vm_is_running) {
+           $agent_running = PVE::QemuServer::qga_check_running($vmid);
        }
 
-       foreach my $di (@{$task->{disks}}) {
-           if ($di->{type} eq 'block' || $di->{type} eq 'file') {
-               my $size = get_size ($di->{snappath});
-               my $storeid = $di->{storeid} || '';
-               print $outfd "#vzdump#map:$di->{virtdev}:$di->{filename}:$size:$storeid:\n";
-           } else {
-               die "internal error";
+       if ($agent_running){
+           eval { PVE::QemuServer::vm_mon_cmd($vmid, "guest-fsfreeze-freeze"); };
+           if (my $err = $@) {
+               $self->logerr($err);
            }
        }
-    };
-    my $err = $@;
 
-    close ($outfd) if $outfd;
-    close ($conffd) if $conffd;
-    
-    die $err if $err;
-}
+       eval { $qmpclient->queue_execute() };
+       my $qmperr = $@;
 
-sub archive {
-    my ($self, $task, $vmid, $filename, $comp) = @_;
+       if ($agent_running){
+           eval { PVE::QemuServer::vm_mon_cmd($vmid, "guest-fsfreeze-thaw"); };
+           if (my $err = $@) {
+               $self->logerr($err);
+           }
+       }
+       die $qmperr if $qmperr;
+       die $qmpclient->{errors}->{$vmid} if $qmpclient->{errors}->{$vmid};
 
-    my $conffile = "$task->{tmpdir}/qemu-server.conf";
+       if ($cpid) {
+           POSIX::close($outfileno) == 0 ||
+               die "close output file handle failed\n";
+       }
 
-    my $opts = $self->{vzdump}->{opts};
+       die "got no uuid for backup task\n" if !$uuid;
 
-    my $starttime = time ();
+       $self->loginfo("started backup task '$uuid'");
 
-    my $fh;
+       if ($resume_on_backup) {
+           $self->loginfo("resume VM");
+           PVE::QemuServer::vm_mon_cmd($vmid, 'cont');
+       }
 
-    my @filea = ($conffile, 'qemu-server.conf'); # always first file in tar
-    foreach my $di (@{$task->{disks}}) {
-       if ($di->{type} eq 'block' || $di->{type} eq 'file') {
-           push @filea, $di->{snappath}, $di->{filename};
-       } else {
-           die "implement me";
+       my $status;
+       my $starttime = time ();
+       my $last_per = -1;
+       my $last_total = 0;
+       my $last_zero = 0;
+       my $last_transferred = 0;
+       my $last_time = time();
+       my $transferred;
+
+       while(1) {
+           $status = PVE::QemuServer::vm_mon_cmd($vmid, 'query-backup');
+           my $total = $status->{total} || 0;
+           $transferred = $status->{transferred} || 0;
+           my $per = $total ? int(($transferred * 100)/$total) : 0;
+           my $zero = $status->{'zero-bytes'} || 0;
+           my $zero_per = $total ? int(($zero * 100)/$total) : 0;
+
+           die "got unexpected uuid\n" if !$status->{uuid} || ($status->{uuid} ne $uuid);
+
+           my $ctime = time();
+           my $duration = $ctime - $starttime;
+
+           my $rbytes = $transferred - $last_transferred;
+           my $wbytes = $rbytes - ($zero - $last_zero);
+
+           my $timediff = ($ctime - $last_time) || 1; # fixme
+           my $mbps_read = ($rbytes > 0) ?
+               int(($rbytes/$timediff)/(1000*1000)) : 0;
+           my $mbps_write = ($wbytes > 0) ?
+               int(($wbytes/$timediff)/(1000*1000)) : 0;
+
+           my $statusline = "status: $per% ($transferred/$total), " .
+               "sparse ${zero_per}% ($zero), duration $duration, " .
+               "read/write $mbps_read/$mbps_write MB/s";
+           my $res = $status->{status} || 'unknown';
+           if ($res ne 'active') {
+               $self->loginfo($statusline);
+               die(($status->{errmsg} || "unknown error") . "\n")
+                   if $res eq 'error';
+               die "got unexpected status '$res'\n"
+                   if $res ne 'done';
+               die "got wrong number of transfered bytes ($total != $transferred)\n"
+                   if ($res eq 'done') && ($total != $transferred);
+
+               last;
+           }
+           if ($per != $last_per && ($timediff > 2)) {
+               $self->loginfo($statusline);
+               $last_per = $per;
+               $last_total = $total if $total;
+               $last_zero = $zero if $zero;
+               $last_transferred = $transferred if $transferred;
+               $last_time = $ctime;
+           }
+           sleep(1);
+       }
+
+       my $duration = time() - $starttime;
+       if ($transferred && $duration) {
+           my $mb = int($transferred/(1000*1000));
+           my $mbps = int(($transferred/$duration)/(1000*1000));
+           $self->loginfo("transferred $mb MB in $duration seconds ($mbps MB/s)");
+       }
+    };
+    my $err = $@;
+
+    if ($err) {
+       $self->logerr($err);
+       $self->loginfo("aborting backup job");
+       eval { PVE::QemuServer::vm_mon_cmd($vmid, 'backup-cancel'); };
+       if (my $err1 = $@) {
+           $self->logerr($err1);
+       }
+    }
+
+    if ($stop_after_backup) {
+       # stop if not running
+       eval {
+           my $resp = PVE::QemuServer::vm_mon_cmd($vmid, 'query-status');
+           my $status = $resp && $resp->{status} ?  $resp->{status} : 'unknown';
+           if ($status eq 'prelaunch') {
+               $self->loginfo("stopping kvm after backup task");
+               PVE::QemuServer::vm_stop($self->{storecfg}, $vmid, $skiplock);
+           } else {
+               $self->loginfo("kvm status changed after backup ('$status')" .
+                              " - keep VM running");
+           }
        }
     }
 
-    my $files = join (' ', map { "'$_'" } @filea);
-    
-    my $cmd = "/usr/lib/qemu-server/vmtar $files";
-    my $bwl = $opts->{bwlimit}*1024; # bandwidth limit for cstream
-    $cmd .= "|cstream -t $bwl" if $opts->{bwlimit};
-    $cmd .= "|$comp" if $comp;
-
-    if ($opts->{stdout}) {
-       $self->cmd ($cmd, output => ">&=" . fileno($opts->{stdout}));
-    } else {
-       $self->cmd ("$cmd >$filename");
+    if ($err) {
+       if ($cpid) {
+           kill(9, $cpid);
+           waitpid($cpid, 0);
+       }
+       die $err;
+    }
+
+    if ($cpid && (waitpid($cpid, 0) > 0)) {
+       my $stat = $?;
+       my $ec = $stat >> 8;
+       my $signal = $stat & 127;
+       if ($ec || $signal) {
+           die "$comp failed - wrong exit status $ec" .
+               ($signal ? " (signal $signal)\n" : "\n");
+       }
     }
 }
 
+sub snapshot {
+    my ($self, $task, $vmid) = @_;
+
+    # nothing to do
+}
+
 sub cleanup {
     my ($self, $task, $vmid) = @_;
 
-   foreach my $di (@{$task->{disks}}) {
-       
-       if ($di->{snapshot_mount}) {
-          $self->cmd_noerr ("umount $di->{mountpoint}");
-       }
-
-       if ($di->{cleanup_lvm}) {
-          if (-b $di->{snapdev}) {
-              if ($di->{type} eq 'block') {
-                  $self->snapshot_free ($di->{storeid}, $di->{snapname}, $di->{snapdev}, 1);
-              } elsif ($di->{type} eq 'file') {
-                  $self->snapshot_free ($di->{storeid}, $di->{snapname}, $di->{snapdev}, 1);
-              }
-          }
-       }
-   }
+    # nothing to do ?
 }
 
 1;