]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/VZDump/QemuServer.pm
avoid backup command timeout with pbs
[qemu-server.git] / PVE / VZDump / QemuServer.pm
index 11e2ec0faccd1e3cc910616613f112e8ef98f21d..8513d89743ccefbb1c875af1a2f5d30607cfcd82 100644 (file)
@@ -1,44 +1,37 @@
 package PVE::VZDump::QemuServer;
 
-#    Copyright (C) 2007-2009 Proxmox Server Solutions GmbH
-#
-#    Copyright: vzdump is under GNU GPL, the GNU General Public License.
-#
-#    This program is free software; you can redistribute it and/or modify
-#    it under the terms of the GNU General Public License as published by
-#    the Free Software Foundation; version 2 dated June, 1991.
-#
-#    This program is distributed in the hope that it will be useful,
-#    but WITHOUT ANY WARRANTY; without even the implied warranty of
-#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-#    GNU General Public License for more details.
-#
-#    You should have received a copy of the GNU General Public License
-#    along with this program; if not, write to the
-#    Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
-#    MA 02110-1301, USA.
-#
-#    Author: Dietmar Maurer <dietmar@proxmox.com>
-
 use strict;
 use warnings;
-use File::Path;
+
 use File::Basename;
-use PVE::VZDump;
-use PVE::Cluster;
+use File::Path;
+use IO::File;
+use IPC::Open3;
+
+use PVE::Cluster qw(cfs_read_file);
+use PVE::INotify;
+use PVE::IPCC;
+use PVE::JSONSchema;
+use PVE::QMPClient;
+use PVE::Storage::Plugin;
+use PVE::Storage::PBSPlugin;
 use PVE::Storage;
+use PVE::Tools;
+use PVE::VZDump;
+
+use PVE::QemuConfig;
 use PVE::QemuServer;
-use Sys::Hostname;
-use IO::File;
+use PVE::QemuServer::Machine;
+use PVE::QemuServer::Monitor qw(mon_cmd);
 
 use base qw (PVE::VZDump::Plugin);
 
 sub new {
     my ($class, $vzdump) = @_;
-    
-    PVE::VZDump::check_bin ('qm');
 
-    my $self = bless { vzdump => $vzdump };
+    PVE::VZDump::check_bin('qm');
+
+    my $self = bless { vzdump => $vzdump }, $class;
 
     $self->{vmlist} = PVE::QemuServer::vzlist();
     $self->{storecfg} = PVE::Storage::config();
@@ -46,14 +39,12 @@ sub new {
     return $self;
 };
 
-
 sub type {
     return 'qemu';
 }
 
 sub vmlist {
     my ($self) = @_;
-
     return [ keys %{$self->{vmlist}} ];
 }
 
@@ -62,150 +53,105 @@ sub prepare {
 
     $task->{disks} = [];
 
-    my $conf = $self->{vmlist}->{$vmid} = PVE::QemuServer::load_config ($vmid);
+    my $conf = $self->{vmlist}->{$vmid} = PVE::QemuConfig->load_config($vmid);
+
+    $self->loginfo("VM Name: $conf->{name}")
+       if defined($conf->{name});
+
+    $self->{vm_was_running} = 1;
+    if (!PVE::QemuServer::check_running($vmid)) {
+       $self->{vm_was_running} = 0;
+    }
 
     $task->{hostname} = $conf->{name};
 
-    my $lvmmap = PVE::VZDump::get_lvm_mapping();
+    my $hostname = PVE::INotify::nodename();
 
-    my $hostname = hostname(); 
+    my $vollist = [];
+    my $drivehash = {};
+    my $backup_volumes = PVE::QemuConfig->get_backup_volumes($conf);
 
-    my $ind = {};
-    my $mountinfo = {};
-    my $mountind = 0;
+    foreach my $volume (@{$backup_volumes}) {
+       my $name = $volume->{key};
+       my $volume_config = $volume->{volume_config};
+       my $volid = $volume_config->{file};
 
-    my $snapshot_count = 0;
+       if (!$volume->{included}) {
+           $self->loginfo("exclude disk '$name' '$volid' ($volume->{reason})");
+           next;
+       } elsif ($self->{vm_was_running} && $volume_config->{iothread}) {
+           if (!PVE::QemuServer::Machine::runs_at_least_qemu_version($vmid, 4, 0, 1)) {
+               die "disk '$name' '$volid' (iothread=on) can't use backup feature with running QEMU " .
+                   "version < 4.0.1! Either set backup=no for this drive or upgrade QEMU and restart VM\n";
+           }
+       } else {
+           my $log = "include disk '$name' '$volid'";
+           if (defined(my $size = $volume_config->{size})) {
+               my $readable_size = PVE::JSONSchema::format_size($size);
+               $log .= " $readable_size";
+           }
+           $self->loginfo($log);
+       }
 
-    PVE::QemuServer::foreach_drive($conf, sub {
-       my ($ds, $drive) = @_;
+       my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
+       push @$vollist, $volid if $storeid;
+       $drivehash->{$name} = $volume->{volume_config};
+    }
 
-       return if PVE::QemuServer::drive_is_cdrom ($drive);
+    PVE::Storage::activate_volumes($self->{storecfg}, $vollist);
 
-       if (defined($drive->{backup}) && $drive->{backup} eq "no") {
-           $self->loginfo("exclude disk '$ds' (backup=no)");
-           return;
-       }          
-       my $volid = $drive->{file};
+    foreach my $ds (sort keys %$drivehash) {
+       my $drive = $drivehash->{$ds};
 
-       my $path;
+       my $volid = $drive->{file};
+       my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
 
-       my ($storeid, $volname) = PVE::Storage::parse_volume_id ($volid, 1);
+       my $path = $volid;
        if ($storeid) {
-           PVE::Storage::activate_storage ($self->{storecfg}, $storeid);
-           $path = PVE::Storage::path ($self->{storecfg}, $volid);
-       } else {
-           $path = $volid;
+           $path = PVE::Storage::path($self->{storecfg}, $volid);
        }
+       next if !$path;
 
-       return if !$path;
+       my ($size, $format) = eval { PVE::Storage::volume_size_info($self->{storecfg}, $volid, 5) };
+       die "no such volume '$volid'\n" if $@;
 
-       die "no such volume '$volid'\n" if ! -e $path;
-
-       my $diskinfo = { path => $path , volid => $volid, storeid => $storeid, 
-                        snappath => $path, virtdev => $ds };
+       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 $status_text = $self->cmd ("qm status $vmid");
-    chomp $status_text;
+    my $running = PVE::QemuServer::check_running($vmid) ? 1 : 0;
 
-    my $running = $status_text =~ m/running/ ? 1 : 0;
-   
-    return wantarray ? ($running, $status_text) : $running; 
+    return wantarray ? ($running, $running ? 'running' : 'stopped') : $running;
 }
 
 sub lock_vm {
     my ($self, $vmid) = @_;
 
-    $self->cmd ("qm set $vmid --lock backup");
+    PVE::QemuConfig->set_lock($vmid, 'backup');
 }
 
 sub unlock_vm {
     my ($self, $vmid) = @_;
 
-    $self->cmd ("qm --skiplock set $vmid --lock ''");
+    PVE::QemuConfig->remove_lock($vmid, 'backup');
 }
 
 sub stop_vm {
@@ -215,246 +161,584 @@ sub stop_vm {
 
     my $wait = $opts->{stopwait} * 60;
     # send shutdown and wait
-    $self->cmd ("qm --skiplock shutdown $vmid && qm wait $vmid $wait");
+    $self->cmd ("qm shutdown $vmid --skiplock --keepActive --timeout $wait");
 }
 
 sub start_vm {
     my ($self, $task, $vmid) = @_;
 
-    $self->cmd ("qm --skiplock start $vmid");
+    $self->cmd ("qm start $vmid --skiplock");
 }
 
 sub suspend_vm {
     my ($self, $task, $vmid) = @_;
 
-    $self->cmd ("qm --skiplock suspend $vmid");
+    $self->cmd ("qm suspend $vmid --skiplock");
 }
 
 sub resume_vm {
     my ($self, $task, $vmid) = @_;
 
-    $self->cmd ("qm --skiplock resume $vmid");
+    $self->cmd ("qm resume $vmid --skiplock");
 }
 
-sub snapshot_alloc {
-    my ($self, $volid, $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);
 
-    my ($storeid, $volname) = PVE::Storage::parse_volume_id ($volid, 1);
-    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;
+           }
+       }
+       next if $found_snapshot || $found_pending; # skip all snapshots and pending changes config data
+
+       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;
+    }
+
+    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 $scfg = PVE::Storage::storage_config ($self->{storecfg}, $storeid);
+    if ($found_snapshot) {
+       $self->loginfo("snapshots found (not included into backup)");
+    }
+    if ($found_pending) {
+       $self->loginfo("pending configuration changes found (not included into backup)");
+    }
 
-       # lock shared storage
-       return PVE::Storage::cluster_lock_storage ($storeid, $scfg->{shared}, undef, sub {
+    PVE::Tools::file_copy($firewall_src, $firewall_dest) if -f $firewall_src;
+}
 
-           if ($scfg->{type} eq 'lvm') {
-               my $vg = $scfg->{vgname};
+sub archive {
+    my ($self, $task, $vmid, $filename, $comp) = @_;
 
-               $self->cmd ($cmd);
+    my $opts = $self->{vzdump}->{opts};
+    my $scfg = $opts->{scfg};
 
-           } else {
-               die "can't allocate snapshot on storage type '$scfg->{type}'\n";
-           }
-       });
+    if ($self->{vzdump}->{opts}->{pbs}) {
+       $self->archive_pbs($task, $vmid);
     } else {
-       $self->cmd ($cmd);
+       $self->archive_vma($task, $vmid, $filename, $comp);
     }
 }
 
-sub snapshot_free {
-    my ($self, $volid, $name, $snapdev, $noerr) = @_;
+my $query_backup_status_loop = sub {
+    my ($self, $vmid, $job_uuid) = @_;
 
-    my $cmd = "lvremove -f '$snapdev'";
+    my $starttime = time ();
+    my $last_time = $starttime;
+    my ($last_percent, $last_total, $last_zero, $last_transferred) = (-1, 0, 0, 0);
+    my $transferred;
 
-    eval {
-       my ($storeid, $volname) = PVE::Storage::parse_volume_id ($volid, 1);
-       if ($storeid) {
+    my $get_mbps = sub {
+       my ($mb, $delta) = @_;
+       return ($mb > 0) ? int(($mb / $delta) / (1000 * 1000)) : 0;
+    };
 
-           my $scfg = PVE::Storage::storage_config ($self->{storecfg}, $storeid);
+    while(1) {
+       my $status = mon_cmd($vmid, 'query-backup');
 
-           # lock shared storage
-           return PVE::Storage::cluster_lock_storage ($storeid, $scfg->{shared}, undef, sub {
+       my $total = $status->{total} || 0;
+       $transferred = $status->{transferred} || 0;
+       my $percent = $total ? int(($transferred * 100)/$total) : 0;
+       my $zero = $status->{'zero-bytes'} || 0;
+       my $zero_per = $total ? int(($zero * 100)/$total) : 0;
 
-               if ($scfg->{type} eq 'lvm') {
-                   my $vg = $scfg->{vgname};
+       die "got unexpected uuid\n" if !$status->{uuid} || ($status->{uuid} ne $job_uuid);
 
-                   $self->cmd ($cmd);
+       my $ctime = time();
+       my $duration = $ctime - $starttime;
 
-               } else {
-                   die "can't allocate snapshot on storage type '$scfg->{type}'\n";
-               }
-           });
-       } else {
-           $self->cmd ($cmd);
+       my $rbytes = $transferred - $last_transferred;
+       my $wbytes = $rbytes - ($zero - $last_zero);
+
+       my $timediff = ($ctime - $last_time) || 1; # fixme
+       my $mbps_read = $get_mbps->($rbytes, $timediff);
+       my $mbps_write = $get_mbps->($wbytes, $timediff);
+
+       my $statusline = "status: $percent% ($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);
+           if ($res ne 'done') {
+               die (($status->{errmsg} || "unknown error") . "\n") if $res eq 'error';
+               die "got unexpected status '$res'\n";
+           } elsif ($total != $transferred) {
+               die "got wrong number of transfered bytes ($total != $transferred)\n";
+           }
+           last;
        }
-    };
-    die $@ if !$noerr;
-    $self->logerr ($@) if $@;
-}
+       if ($percent != $last_percent && ($timediff > 2)) {
+           $self->loginfo($statusline);
+           $last_percent = $percent;
+           $last_total = $total if $total;
+           $last_zero = $zero if $zero;
+           $last_transferred = $transferred if $transferred;
+           $last_time = $ctime;
+       }
+       sleep(1);
+    }
 
-sub snapshot {
+    my $duration = time() - $starttime;
+    if ($transferred && $duration) {
+       my $mb = int($transferred / (1000 * 1000));
+       my $mbps = $get_mbps->($transferred, $duration);
+       $self->loginfo("transferred $mb MB in $duration seconds ($mbps MB/s)");
+    }
+};
+
+sub archive_pbs {
     my ($self, $task, $vmid) = @_;
 
+    my $conffile = "$task->{tmpdir}/qemu-server.conf";
+    my $firewall = "$task->{tmpdir}/qemu-server.fw";
+
     my $opts = $self->{vzdump}->{opts};
+    my $scfg = $opts->{scfg};
 
-    my $mounts = {};
+    my $starttime = time();
 
-    foreach my $di (@{$task->{disks}}) {
-       if ($di->{type} eq 'block') {
+    my $server = $scfg->{server};
+    my $datastore = $scfg->{datastore};
+    my $username = $scfg->{username} // 'root@pam';
+    my $fingerprint = $scfg->{fingerprint};
+
+    my $repo = "$username\@$server:$datastore";
+    my $password = PVE::Storage::PBSPlugin::pbs_get_password($scfg, $opts->{storage});
 
-           if (-b $di->{snapdev}) {
-               $self->loginfo ("trying to remove stale snapshot '$di->{snapdev}'");
-               $self->snapshot_free ($di->{volid}, $di->{snapname}, $di->{snapdev}, 1); 
+    my $diskcount = scalar(@{$task->{disks}});
+    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}.img:$di->{path}";
+           } else {
+               die "implement me (type $di->{type})";
            }
+       }
 
-           $di->{cleanup_lvm} = 1;
-           $self->snapshot_alloc ($di->{volid}, $di->{snapname}, $opts->{size},
-                                  "/dev/$di->{lvmvg}/$di->{lvmlv}"); 
+       if (!$diskcount) {
+           $self->loginfo("backup contains no disks");
+       }
 
-       } elsif ($di->{type} eq 'file') {
+       local $ENV{PBS_PASSWORD} = $password;
+       local $ENV{PBS_FINGERPRINT} = $fingerprint if defined($fingerprint);
+       my $cmd = [
+           '/usr/bin/proxmox-backup-client',
+           'backup',
+           '--repository', $repo,
+           '--backup-type', 'vm',
+           '--backup-id', "$vmid",
+           '--backup-time', $task->{backup_time},
+       ];
 
-           next if defined ($mounts->{$di->{mountpoint}}); # already mounted
+       push @$cmd, "qemu-server.conf:$conffile";
+       push @$cmd, "fw.conf:$firewall" if -e $firewall;
+       push @$cmd, @pathlist if scalar(@pathlist);
 
-           # note: files are never on shared storage, so we use $di->{path} instead
-           # of $di->{volid} (avoid PVE:Storage calls because path start with /)
+       $self->loginfo("starting template backup");
+       $self->loginfo(join(' ', @$cmd));
 
-           if (-b $di->{snapdev}) {
-               $self->loginfo ("trying to remove stale snapshot '$di->{snapdev}'");        
-           
-               $self->cmd_noerr ("umount $di->{mountpoint}");
+       $self->cmd($cmd);
 
-               $self->snapshot_free ($di->{path}, $di->{snapname}, $di->{snapdev}, 1); 
-           }
+       return;
+    }
 
-           mkpath $di->{mountpoint}; # create mount point for lvm snapshot
+    # get list early so we die on unkown drive types before doing anything
+    my $devlist = _get_task_devlist($task);
 
-           $di->{cleanup_lvm} = 1;
+    $self->enforce_vm_running_for_backup($vmid);
 
-           $self->snapshot_alloc ($di->{path}, $di->{snapname}, $opts->{size},
-                                  "/dev/$di->{lvmvg}/$di->{lvmlv}"); 
-           
-           my $mopts = $di->{fstype} eq 'xfs' ? "-o nouuid" : '';
+    my $backup_job_uuid;
+    eval {
+       $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub {
+           die "interrupted by signal\n";
+       };
+
+       my $fs_frozen = $self->qga_fs_freeze($task, $vmid);
+
+       my $params = {
+           format => "pbs",
+           'backup-file' => $repo,
+           'backup-id' => "$vmid",
+           'backup-time' => $task->{backup_time},
+           password => $password,
+           devlist => $devlist,
+           'config-file' => $conffile,
+       };
+       $params->{fingerprint} = $fingerprint if defined($fingerprint);
+       $params->{'firewall-file'} = $firewall if -e $firewall;
+
+       $params->{timeout} = 60; # give some time to connect to the backup server
+
+       my $res = eval { mon_cmd($vmid, "backup", %$params) };
+       my $qmperr = $@;
+       $backup_job_uuid = $res->{UUID} if $res;
+
+       if ($fs_frozen) {
+           $self->qga_fs_thaw($vmid);
+       }
 
-           $di->{snapshot_mount} = 1;
+       die $qmperr if $qmperr;
+       die "got no uuid for backup task\n" if !defined($backup_job_uuid);
 
-           $self->cmd ("mount -t $di->{fstype} $mopts $di->{snapdev} $di->{mountpoint}");
+       $self->loginfo("started backup task '$backup_job_uuid'");
 
-           $mounts->{$di->{mountpoint}} = 1;
+       $self->resume_vm_after_job_start($task, $vmid);
 
-       } else {
-           die "implement me";
-       }
+       $query_backup_status_loop->($self, $vmid, $backup_job_uuid);
+    };
+    my $err = $@;
+    if ($err) {
+       $self->logerr($err);
+       $self->mon_backup_cancel($vmid) if defined($backup_job_uuid);
     }
-}
+    $self->restore_vm_power_state($vmid);
 
-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;
-    }
+    die $err if $err;
 }
 
-sub assemble {
-    my ($self, $task, $vmid) = @_;
+my $fork_compressor_pipe = sub {
+    my ($self, $comp, $outfileno) = @_;
+
+    my @pipefd = POSIX::pipe();
+    my $cpid = fork();
+    die "unable to fork worker - $!" if !defined($cpid) || $cpid < 0;
+    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, ">&", $outfileno);
+
+           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];
+    }
 
-    my $conffile = PVE::QemuServer::config_file ($vmid);
+    return ($cpid, $outfileno);
+};
 
-    my $outfile = "$task->{tmpdir}/qemu-server.conf";
+sub archive_vma {
+    my ($self, $task, $vmid, $filename, $comp) = @_;
 
-    my $outfd;
-    my $conffd;
+    my $conffile = "$task->{tmpdir}/qemu-server.conf";
+    my $firewall = "$task->{tmpdir}/qemu-server.fw";
 
-    eval {
+    my $opts = $self->{vzdump}->{opts};
 
-       $outfd = IO::File->new (">$outfile") ||
-           die "unable to open '$outfile'";
-       $conffd = IO::File->new ($conffile, 'r') ||
-           die "unable open '$conffile'";
+    my $starttime = time();
 
-       while (defined (my $line = <$conffd>)) {
-           next if $line =~ m/^\#vzdump\#/; # just to be sure
-           print $outfd $line;
-       }
+    my $speed = 0;
+    if ($opts->{bwlimit}) {
+       $speed = $opts->{bwlimit}*1024;
+    }
 
+    my $diskcount = scalar(@{$task->{disks}});
+    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') {
-               my $size = get_size ($di->{snappath});
-               my $storeid = $di->{storeid} || '';
-               print $outfd "#vzdump#map:$di->{virtdev}:$di->{filename}:$size:$storeid:\n";
+               push @pathlist, "$di->{qmdevice}=$di->{path}";
            } else {
-               die "internal error";
+               die "implement me";
            }
        }
-    };
-    my $err = $@;
 
-    close ($outfd) if $outfd;
-    close ($conffd) if $conffd;
-    
-    die $err if $err;
-}
+       if (!$diskcount) {
+           $self->loginfo("backup contains no disks");
+       }
 
-sub archive {
-    my ($self, $task, $vmid, $filename) = @_;
+       my $outcmd;
+       if ($comp) {
+           $outcmd = "exec:$comp";
+       } else {
+           $outcmd = "exec:cat";
+       }
 
-    my $conffile = "$task->{tmpdir}/qemu-server.conf";
+       $outcmd .= " > $filename" if !$opts->{stdout};
 
-    my $opts = $self->{vzdump}->{opts};
+       my $cmd = ['/usr/bin/vma', 'create', '-v', '-c', $conffile];
+       push @$cmd, '-c', $firewall if -e $firewall;
+       push @$cmd, $outcmd, @pathlist;
 
-    my $starttime = time ();
+       $self->loginfo("starting template backup");
+       $self->loginfo(join(' ', @$cmd));
 
-    my $fh;
+       if ($opts->{stdout}) {
+           $self->cmd($cmd, output => ">&" . fileno($opts->{stdout}));
+       } else {
+           $self->cmd($cmd);
+       }
 
-    my $bwl = $opts->{bwlimit}*1024; # bandwidth limit for cstream
+       return;
+    }
+
+    my $devlist = _get_task_devlist($task);
+
+    $self->enforce_vm_running_for_backup($vmid);
+
+    my $cpid;
+    my $backup_job_uuid;
+
+    eval {
+       $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub {
+           die "interrupted by signal\n";
+       };
 
-    my @filea = ($conffile, 'qemu-server.conf'); # always first file in tar
+       my $outfh;
+       if ($opts->{stdout}) {
+           $outfh = $opts->{stdout};
+       } else {
+           $outfh = IO::File->new($filename, "w") ||
+               die "unable to open file '$filename' - $!\n";
+       }
+       my $outfileno = fileno($outfh);
+
+       if ($comp) {
+           ($cpid, $outfileno) = $fork_compressor_pipe->($self, $comp, $outfileno);
+       }
+
+       my $qmpclient = PVE::QMPClient->new();
+       my $backup_cb = sub {
+           my ($vmid, $resp) = @_;
+           $backup_job_uuid = $resp->{return}->{UUID};
+       };
+       my $add_fd_cb = sub {
+           my ($vmid, $resp) = @_;
+
+           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 $fs_frozen = $self->qga_fs_freeze($task, $vmid);
+
+       eval { $qmpclient->queue_execute(30) };
+       my $qmperr = $@;
+
+       if ($fs_frozen) {
+           $self->qga_fs_thaw($vmid);
+       }
+
+       die $qmperr if $qmperr;
+       die $qmpclient->{errors}->{$vmid} if $qmpclient->{errors}->{$vmid};
+
+       if ($cpid) {
+           POSIX::close($outfileno) == 0 ||
+               die "close output file handle failed\n";
+       }
+
+       die "got no uuid for backup task\n" if !defined($backup_job_uuid);
+
+       $self->loginfo("started backup task '$backup_job_uuid'");
+
+       $self->resume_vm_after_job_start($task, $vmid);
+
+       $query_backup_status_loop->($self, $vmid, $backup_job_uuid);
+    };
+    my $err = $@;
+    if ($err) {
+       $self->logerr($err);
+       $self->mon_backup_cancel($vmid) if defined($backup_job_uuid);
+    }
+
+    $self->restore_vm_power_state($vmid);
+
+    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 _get_task_devlist {
+    my ($task) = @_;
+
+    my $devlist = '';
     foreach my $di (@{$task->{disks}}) {
        if ($di->{type} eq 'block' || $di->{type} eq 'file') {
-           push @filea, $di->{snappath}, $di->{filename};
+           $devlist .= ',' if $devlist;
+           $devlist .= $di->{qmdevice};
        } else {
-           die "implement me";
+           die "implement me (type '$di->{type}')";
        }
     }
+    return $devlist;
+}
+
+sub qga_fs_freeze {
+    my ($self, $task, $vmid) = @_;
+    return if !$self->{vmlist}->{$vmid}->{agent} || $task->{mode} eq 'stop' || !$self->{vm_was_running};
 
-    my $out = ">$filename";
-    $out = "|cstream -t $bwl $out" if $opts->{bwlimit};
-    $out = "|gzip $out" if $opts->{compress};
+    if (!PVE::QemuServer::qga_check_running($vmid, 1)) {
+       $self->loginfo("skipping guest-agent 'fs-freeze', agent configured but not running?");
+       return;
+    }
+
+    $self->loginfo("issuing guest-agent 'fs-freeze' command");
+    eval { mon_cmd($vmid, "guest-fsfreeze-freeze") };
+    $self->logerr($@) if $@;
 
-    my $files = join (' ', map { "'$_'" } @filea);
-    
-    $self->cmd("/usr/lib/qemu-server/vmtar $files $out");
+    return 1; # even on mon command error, ensure we always thaw again
 }
 
-sub cleanup {
+# only call if fs_freeze return 1
+sub qga_fs_thaw {
+    my ($self, $vmid) = @_;
+
+    $self->loginfo("issuing guest-agent 'fs-thaw' command");
+    eval { mon_cmd($vmid, "guest-fsfreeze-thaw") };
+    $self->logerr($@) if $@;
+}
+
+# we need a running QEMU/KVM process for backup, starts a paused (prelaunch)
+# one if VM isn't already running
+sub enforce_vm_running_for_backup {
+    my ($self, $vmid) = @_;
+
+    if (PVE::QemuServer::check_running($vmid)) {
+       $self->{vm_was_running} = 1;
+       return;
+    }
+
+    eval {
+       $self->loginfo("starting kvm to execute backup task");
+       # start with skiplock
+       my $params = {
+           skiplock => 1,
+           paused => 1,
+       };
+       PVE::QemuServer::vm_start($self->{storecfg}, $vmid, $params);
+    };
+    die $@ if $@;
+}
+
+# resume VM againe once we got in a clear state (stop mode backup of running VM)
+sub resume_vm_after_job_start {
     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->{volid}, $di->{snapname}, $di->{snapdev}, 1);
-              } elsif ($di->{type} eq 'file') {
-                  $self->snapshot_free ($di->{path}, $di->{snapname}, $di->{snapdev}, 1);
-              }
-          }
-       }
-   }
+    return if !$self->{vm_was_running};
+
+    if (my $stoptime = $task->{vmstoptime}) {
+       my $delay = time() - $task->{vmstoptime};
+       $task->{vmstoptime} = undef; # avoid printing 'online after ..' twice
+       $self->loginfo("resuming VM again after $delay seconds");
+    } else {
+       $self->loginfo("resuming VM again");
+    }
+    mon_cmd($vmid, 'cont');
+}
+
+# stop again if VM was not running before
+sub restore_vm_power_state {
+    my ($self, $vmid) = @_;
+
+    # we always let VMs keep running
+    return if $self->{vm_was_running};
+
+    eval {
+       my $resp = 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, 1);
+       } else {
+           $self->loginfo("kvm status changed after backup ('$status') - keep VM running");
+       }
+    };
+    warn $@ if $@;
+}
+
+sub mon_backup_cancel {
+    my ($self, $vmid) = @_;
+
+    $self->loginfo("aborting backup job");
+    eval { mon_cmd($vmid, 'backup-cancel') };
+    $self->logerr($@) if $@;
+}
+
+sub snapshot {
+    my ($self, $task, $vmid) = @_;
+
+    # nothing to do
+}
+
+sub cleanup {
+    my ($self, $task, $vmid) = @_;
 
+    # nothing to do ?
 }
 
 1;