]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/QemuServer.pm
set template flag earlier
[qemu-server.git] / PVE / QemuServer.pm
index 7cdd64794644b8448ae382a3a0b5146b16c53b63..194b678d12f1edfa5ceb5f64a319c3b6b57889d7 100644 (file)
@@ -166,7 +166,7 @@ my $confdesc = {
     hotplug => {
         optional => 1,
         type => 'boolean',
-        description => "Activate hotplug for disk and network device",
+        description => "Allow hotplug for disk and network device",
         default => 0,
     },
     reboot => {
@@ -358,6 +358,12 @@ EODESC
        typetext => '[[order=]\d+] [,up=\d+] [,down=\d+] ',
        description => "Startup and shutdown behavior. Order is a non-negative number defining the general startup order. Shutdown in done with reverse ordering. Additionally you can set the 'up' or 'down' delay in seconds, which specifies a delay to wait before the next VM is started or stopped.",
     },
+    template => {
+       optional => 1,
+       type => 'boolean',
+       description => "Enable/disable Template.",
+       default => 0,
+    },
     args => {
        optional => 1,
        type => 'string',
@@ -382,10 +388,10 @@ EODESCR
     },
     migrate_downtime => {
        optional => 1,
-       type => 'integer',
+       type => 'number',
        description => "Set maximum tolerated downtime (in seconds) for migrations.",
        minimum => 0,
-       default => 1,
+       default => 0.1,
     },
     cdrom => {
        optional => 1,
@@ -398,7 +404,7 @@ EODESCR
        description => "Emulated CPU type.",
        type => 'string',
        enum => [ qw(486 athlon pentium pentium2 pentium3 coreduo core2duo kvm32 kvm64 qemu32 qemu64 phenom Conroe Penryn Nehalem Westmere SandyBridge Haswell Opteron_G1 Opteron_G2 Opteron_G3 Opteron_G4 Opteron_G5 host) ],
-       default => 'qemu64',
+       default => 'kvm64',
     },
     parent => get_standard_option('pve-snapshot-name', {
        optional => 1,
@@ -1104,11 +1110,10 @@ sub print_drive_full {
        } else {
            $path = PVE::Storage::path($storecfg, $volid);
        }
-       if (!$drive->{cache} && ($path =~ m|^/dev/| || $path =~ m|\.raw$|)) {
-           $opts .= ",cache=none";
-       }
     }
 
+    $opts .= ",cache=none" if !$drive->{cache} && !drive_is_cdrom($drive);
+
     my $pathinfo = $path ? "file=$path," : '';
 
     return "${pathinfo}if=none,id=drive-$drive->{interface}$drive->{index}$opts";
@@ -1194,7 +1199,7 @@ sub parse_net {
 
        if ($kvp =~ m/^(ne2k_pci|e1000|rtl8139|pcnet|virtio|ne2k_isa|i82551|i82557b|i82559er)(=([0-9a-f]{2}(:[0-9a-f]{2}){5}))?$/i) {
            my $model = lc($1);
-           my $mac = uc($3) || PVE::Tools::random_ether_addr();
+           my $mac = defined($3) ? uc($3) : PVE::Tools::random_ether_addr();
            $res->{model} = $model;
            $res->{macaddr} = $mac;
        } elsif ($kvp =~ m/^bridge=(\S+)$/) {
@@ -1441,6 +1446,9 @@ sub check_type {
     } elsif ($type eq 'integer') {
        return int($1) if $value =~ m/^(\d+)$/;
        die "type check ('integer') failed - got '$value'\n";
+    } elsif ($type eq 'number') {
+        return $value if $value =~ m/^(\d+)(\.\d+)?$/;
+        die "type check ('number') failed - got '$value'\n";
     } elsif ($type eq 'string') {
        if (my $fmt = $confdesc->{$key}->{format}) {
            if ($fmt eq 'pve-qm-drive') {
@@ -1529,6 +1537,7 @@ sub destroy_vm {
        return if drive_is_cdrom($drive);
 
        my $volid = $drive->{file};
+
        return if !$volid || $volid =~ m|^/|;
 
        my ($path, $owner) = PVE::Storage::path($storecfg, $volid);
@@ -1684,7 +1693,7 @@ sub write_vm_config {
            $cref->{$key} = $value;
 
            if (valid_drivename($key)) {
-               my $drive = PVE::QemuServer::parse_drive($key, $value);
+               my $drive = parse_drive($key, $value);
                $used_volids->{$drive->{file}} = 1 if $drive && $drive->{file};
            }
        }
@@ -1832,7 +1841,7 @@ sub check_cmdline {
        my @param = split(/\0/, $line);
 
        my $cmd = $param[0];
-       return if !$cmd || ($cmd !~ m|kvm$|);
+       return if !$cmd || ($cmd !~ m|kvm$| && $cmd !~ m|qemu-system-x86_64$|);
 
        for (my $i = 0; $i < scalar (@param); $i++) {
            my $p = $param[$i];
@@ -1963,8 +1972,8 @@ sub vmstatus {
        $d->{maxmem} = $conf->{memory} ? $conf->{memory}*(1024*1024) : 0;
 
        if ($conf->{balloon}) {
-           $d->{balloon_min} = $conf->{balloon};
-           $d->{shares} = $conf->{shares} || 1000;
+           $d->{balloon_min} = $conf->{balloon}*(1024*1024);
+           $d->{shares} = defined($conf->{shares}) ? $conf->{shares} : 1000;
        }
 
        $d->{uptime} = 0;
@@ -1977,6 +1986,8 @@ sub vmstatus {
        $d->{diskread} = 0;
        $d->{diskwrite} = 0;
 
+        $d->{template} = is_template($conf);
+
        $res->{$vmid} = $d;
     }
 
@@ -2131,7 +2142,7 @@ sub foreach_volid {
        $volhash->{$volid} = $is_cdrom || 0;
     };
 
-    PVE::QemuServer::foreach_drive($conf, sub {
+    foreach_drive($conf, sub {
        my ($ds, $drive) = @_;
        &$test_volid($drive->{file}, drive_is_cdrom($drive));
     });
@@ -2139,7 +2150,7 @@ sub foreach_volid {
     foreach my $snapname (keys %{$conf->{snapshots}}) {
        my $snap = $conf->{snapshots}->{$snapname};
        &$test_volid($snap->{vmstate}, 0);
-       PVE::QemuServer::foreach_drive($snap, sub {
+       foreach_drive($snap, sub {
            my ($ds, $drive) = @_;
            &$test_volid($drive->{file}, drive_is_cdrom($drive));
         });
@@ -2189,6 +2200,9 @@ sub config_to_command {
 
     push @$cmd, '-daemonize';
 
+    $pciaddr = print_pci_addr("piix3", $bridges);
+    push @$devices, '-device', "piix3-usb-uhci,id=uhci$pciaddr.0x2";
+
     my $use_usb2 = 0;
     for (my $i = 0; $i < $MAX_USB_DEVICES; $i++)  {
        next if !$conf->{"usb$i"};
@@ -2199,13 +2213,7 @@ sub config_to_command {
 
     # enable absolute mouse coordinates (needed by vnc)
     my $tablet = defined($conf->{tablet}) ? $conf->{tablet} : $defaults->{tablet};
-    if ($tablet) {
-       if ($use_usb2) {
-           push @$devices, '-device', 'usb-tablet,bus=ehci.0,port=6';
-       } else {
-           push @$devices, '-usbdevice', 'tablet';
-       }
-    }
+    push @$devices, '-device', 'usb-tablet,id=tablet,bus=uhci.0,port=1' if $tablet;
 
     # host pci devices
     for (my $i = 0; $i < $MAX_HOSTPCI_DEVICES; $i++)  {
@@ -2520,7 +2528,15 @@ sub vm_devices_list {
 sub vm_deviceplug {
     my ($storecfg, $conf, $vmid, $deviceid, $device) = @_;
 
-    return 1 if !check_running($vmid) || !$conf->{hotplug};
+    return 1 if !check_running($vmid);
+
+    if ($deviceid eq 'tablet') {
+       my $devicefull = "usb-tablet,id=tablet,bus=uhci.0,port=1";
+       qemu_deviceadd($vmid, $devicefull);
+       return 1;
+    }
+
+    return 1 if !$conf->{hotplug};
 
     my $devices_list = vm_devices_list($vmid);
     return 1 if defined($devices_list->{$deviceid});
@@ -2580,7 +2596,14 @@ sub vm_deviceplug {
 sub vm_deviceunplug {
     my ($vmid, $conf, $deviceid) = @_;
 
-    return 1 if !check_running ($vmid) || !$conf->{hotplug};
+    return 1 if !check_running ($vmid);
+
+    if ($deviceid eq 'tablet') {
+       qemu_devicedel($vmid, $deviceid);
+       return 1;
+    }
+
+    return 1 if !$conf->{hotplug};
 
     my $devices_list = vm_devices_list($vmid);
     return 1 if !defined($devices_list->{$deviceid});
@@ -2588,9 +2611,9 @@ sub vm_deviceunplug {
     die "can't unplug bootdisk" if $conf->{bootdisk} && $conf->{bootdisk} eq $deviceid;
 
     if ($deviceid =~ m/^(virtio)(\d+)$/) {
-        return undef if !qemu_drivedel($vmid, $deviceid);
         qemu_devicedel($vmid, $deviceid);
         return undef if !qemu_devicedelverify($vmid, $deviceid);
+        return undef if !qemu_drivedel($vmid, $deviceid);
     }
 
     if ($deviceid =~ m/^(lsi)(\d+)$/) {
@@ -2603,9 +2626,9 @@ sub vm_deviceunplug {
     }
 
     if ($deviceid =~ m/^(net)(\d+)$/) {
-        return undef if !qemu_netdevdel($vmid, $deviceid);
         qemu_devicedel($vmid, $deviceid);
         return undef if !qemu_devicedelverify($vmid, $deviceid);
+        return undef if !qemu_netdevdel($vmid, $deviceid);
     }
 
     return 1;
@@ -2614,23 +2637,17 @@ sub vm_deviceunplug {
 sub qemu_deviceadd {
     my ($vmid, $devicefull) = @_;
 
-    my $ret = vm_human_monitor_command($vmid, "device_add $devicefull");
-    $ret =~ s/^\s+//;
-    # Otherwise, if the command succeeds, no output is sent. So any non-empty string shows an error
-    return 1 if $ret eq "";
-    syslog("err", "error on hotplug device : $ret");
-    return undef;
+    $devicefull = "driver=".$devicefull;
+    my %options =  split(/[=,]/, $devicefull);
 
+    vm_mon_cmd($vmid, "device_add" , %options);
+    return 1;
 }
 
 sub qemu_devicedel {
     my($vmid, $deviceid) = @_;
-
-    my $ret = vm_human_monitor_command($vmid, "device_del $deviceid");
-    $ret =~ s/^\s+//;
-    return 1 if $ret eq "";
-    syslog("err", "detaching device $deviceid failed : $ret");
-    return undef;
+    my $ret = vm_mon_cmd($vmid, "device_del", id => $deviceid);
+    return 1;
 }
 
 sub qemu_driveadd {
@@ -2725,24 +2742,17 @@ sub qemu_netdevadd {
     my ($vmid, $conf, $device, $deviceid) = @_;
 
     my $netdev = print_netdev_full($vmid, $conf, $device, $deviceid);
-    my $ret = vm_human_monitor_command($vmid, "netdev_add $netdev");
-    $ret =~ s/^\s+//;
+    my %options =  split(/[=,]/, $netdev);
 
-    #if the command succeeds, no output is sent. So any non-empty string shows an error
-    return 1 if $ret eq "";
-    syslog("err", "adding netdev failed: $ret");
-    return undef;
+    vm_mon_cmd($vmid, "netdev_add",  %options);
+    return 1;
 }
 
 sub qemu_netdevdel {
     my ($vmid, $deviceid) = @_;
 
-    my $ret = vm_human_monitor_command($vmid, "netdev_del $deviceid");
-    $ret =~ s/^\s+//;
-    #if the command succeeds, no output is sent. So any non-empty string shows an error
-    return 1 if $ret eq "";
-    syslog("err", "deleting netdev failed: $ret");
-    return undef;
+    vm_mon_cmd($vmid, "netdev_del", id => $deviceid);
+    return 1;
 }
 
 sub qemu_block_set_io_throttle {
@@ -2872,7 +2882,7 @@ sub vm_monitor_command {
 sub qemu_block_resize {
     my ($vmid, $deviceid, $storecfg, $volid, $size) = @_;
 
-    my $running = PVE::QemuServer::check_running($vmid);
+    my $running = check_running($vmid);
 
     return if !PVE::Storage::volume_resize($storecfg, $volid, $size, $running);
 
@@ -2885,7 +2895,7 @@ sub qemu_block_resize {
 sub qemu_volume_snapshot {
     my ($vmid, $deviceid, $storecfg, $volid, $snap) = @_;
 
-    my $running = PVE::QemuServer::check_running($vmid);
+    my $running = check_running($vmid);
 
     return if !PVE::Storage::volume_snapshot($storecfg, $volid, $snap, $running);
 
@@ -2898,7 +2908,7 @@ sub qemu_volume_snapshot {
 sub qemu_volume_snapshot_delete {
     my ($vmid, $deviceid, $storecfg, $volid, $snap) = @_;
 
-    my $running = PVE::QemuServer::check_running($vmid);
+    my $running = check_running($vmid);
 
     return if !PVE::Storage::volume_snapshot_delete($storecfg, $volid, $snap, $running);
 
@@ -2925,6 +2935,8 @@ sub vm_start {
     lock_config($vmid, sub {
        my $conf = load_config($vmid, $migratedfrom);
 
+       die "you can't start a vm if it's a template\n" if is_template($conf);
+
        check_lock($conf) if !$skiplock;
 
        die "VM $vmid already running\n" if check_running($vmid, undef, $migratedfrom);
@@ -2972,41 +2984,26 @@ sub vm_start {
        print "migration listens on port $migrate_port\n" if $migrate_port;
 
        if ($statefile && $statefile ne 'tcp')  {
-           eval { vm_mon_cmd($vmid, "cont"); };
+           eval { vm_mon_cmd_nocheck($vmid, "cont"); };
            warn $@ if $@;
        }
 
-       # always set migrate speed (overwrite kvm default of 32m)
-       # we set a very hight default of 8192m which is basically unlimited
-       my $migrate_speed = $defaults->{migrate_speed} || 8192;
-       $migrate_speed = $conf->{migrate_speed} || $migrate_speed;
-       $migrate_speed = $migrate_speed * 1048576;
-       eval {
-           vm_mon_cmd($vmid, "migrate_set_speed", value => $migrate_speed);
-       };
-
-       my $migrate_downtime = $defaults->{migrate_downtime};
-       $migrate_downtime = $conf->{migrate_downtime} if defined($conf->{migrate_downtime});
-       if (defined($migrate_downtime)) {
-           eval { vm_mon_cmd($vmid, "migrate_set_downtime", value => $migrate_downtime); };
-       }
-
        if($migratedfrom) {
            my $capabilities = {};
            $capabilities->{capability} =  "xbzrle";
            $capabilities->{state} = JSON::true;
-           eval { PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate-set-capabilities", capabilities => [$capabilities]); };
+           eval { vm_mon_cmd_nocheck($vmid, "migrate-set-capabilities", capabilities => [$capabilities]); };
        }
-
-       # fixme: how do we handle that on migration?
-
-       if (!defined($conf->{balloon}) || $conf->{balloon}) {
-           vm_mon_cmd($vmid, "balloon", value => $conf->{balloon}*1024*1024) 
-               if $conf->{balloon};
-           vm_mon_cmd($vmid, 'qom-set'
-                      path => "machine/peripheral/balloon0", 
-                      property => "stats-polling-interval", 
-                      value => 2);
+       else{
+
+           if (!defined($conf->{balloon}) || $conf->{balloon}) {
+               vm_mon_cmd_nocheck($vmid, "balloon", value => $conf->{balloon}*1024*1024) 
+                   if $conf->{balloon};
+               vm_mon_cmd_nocheck($vmid, 'qom-set', 
+                           path => "machine/peripheral/balloon0"
+                           property => "guest-stats-polling-interval", 
+                           value => 2);
+           }
        }
     });
 }
@@ -3038,7 +3035,7 @@ sub vm_qmp_command {
  
     eval {
        die "VM $vmid not running\n" if !check_running($vmid, $nocheck);
-       my $sname = PVE::QemuServer::qmp_socket($vmid);
+       my $sname = qmp_socket($vmid);
        if (-e $sname) {
            my $qmpclient = PVE::QMPClient->new();
 
@@ -3229,7 +3226,7 @@ sub vm_suspend {
 
        my $conf = load_config($vmid);
 
-       check_lock($conf) if !$skiplock;
+       check_lock($conf) if !($skiplock || ($conf->{lock} && $conf->{lock} eq 'backup'));
 
        vm_mon_cmd($vmid, "stop");
     });
@@ -3242,7 +3239,7 @@ sub vm_resume {
 
        my $conf = load_config($vmid);
 
-       check_lock($conf) if !$skiplock;
+       check_lock($conf) if !($skiplock || ($conf->{lock} && $conf->{lock} eq 'backup'));
 
        vm_mon_cmd($vmid, "cont");
     });
@@ -3364,7 +3361,7 @@ sub print_pci_addr {
 
     my $res = '';
     my $devices = {
-       #addr1 : ide,parallel,serial (motherboard)
+       piix3 => { bus => 0, addr => 1 },
        #addr2 : first videocard
        balloon0 => { bus => 0, addr => 3 },
        watchdog => { bus => 0, addr => 4 },
@@ -3439,7 +3436,7 @@ sub print_pci_addr {
 
 # vzdump restore implementaion
 
-sub archive_read_firstfile {
+sub tar_archive_read_firstfile {
     my $archive = shift;
 
     die "ERROR: file '$archive' does not exist\n" if ! -f $archive;
@@ -3457,8 +3454,8 @@ sub archive_read_firstfile {
     return $firstfile;
 }
 
-sub restore_cleanup {
-    my $statfile = shift;
+sub tar_restore_cleanup {
+    my ($storecfg, $statfile) = @_;
 
     print STDERR "starting cleanup\n";
 
@@ -3470,8 +3467,7 @@ sub restore_cleanup {
                    if ($volid =~ m|^/|) {
                        unlink $volid || die 'unlink failed\n';
                    } else {
-                       my $cfg = cfs_read_file('storage.cfg');
-                       PVE::Storage::vdisk_free($cfg, $volid);
+                       PVE::Storage::vdisk_free($storecfg, $volid);
                    }
                    print STDERR "temporary volume '$volid' sucessfuly removed\n";
                };
@@ -3551,13 +3547,13 @@ sub restore_update_config_line {
        print $outfd "$id: $netstr\n";
     } elsif ($line =~ m/^((ide|scsi|virtio|sata)\d+):\s*(\S+)\s*$/) {
        my $virtdev = $1;
-       my $value = $2;
+       my $value = $3;
        if ($line =~ m/backup=no/) {
            print $outfd "#$line";
        } elsif ($virtdev && $map->{$virtdev}) {
-           my $di = PVE::QemuServer::parse_drive($virtdev, $value);
+           my $di = parse_drive($virtdev, $value);
            $di->{file} = $map->{$virtdev};
-           $value = PVE::QemuServer::print_drive($vmid, $di);
+           $value = print_drive($vmid, $di);
            print $outfd "$virtdev: $value\n";
        } else {
            print $outfd $line;
@@ -3592,19 +3588,19 @@ sub update_disksize {
 
     # update size info
     foreach my $opt (keys %$conf) {
-       if (PVE::QemuServer::valid_drivename($opt)) {
-           my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
+       if (valid_drivename($opt)) {
+           my $drive = parse_drive($opt, $conf->{$opt});
            my $volid = $drive->{file};
            next if !$volid;
 
            $used->{$volid} = 1;
 
-           next if PVE::QemuServer::drive_is_cdrom($drive);
+           next if drive_is_cdrom($drive);
            next if !$volid_hash->{$volid};
 
            $drive->{size} = $volid_hash->{$volid}->{size};
            $changes = 1;
-           $conf->{$opt} = PVE::QemuServer::print_drive($vmid, $drive);
+           $conf->{$opt} = print_drive($vmid, $drive);
        }
     }
 
@@ -3612,7 +3608,7 @@ sub update_disksize {
        next if $volid =~ m/vm-$vmid-state-/;
        next if $used->{$volid};
        $changes = 1;
-       PVE::QemuServer::add_unused_volume($conf, $volid);
+       add_unused_volume($conf, $volid);
     }
 
     return $changes;
@@ -3628,20 +3624,26 @@ sub rescan {
     my $updatefn =  sub {
        my ($vmid) = @_;
 
-       my $conf = PVE::QemuServer::load_config($vmid);
+       my $conf = load_config($vmid);
            
-       PVE::QemuServer::check_lock($conf);
+       check_lock($conf);
+
+       my $vm_volids = {};
+       foreach my $volid (keys %$volid_hash) {
+           my $info = $volid_hash->{$volid};
+           $vm_volids->{$volid} = $info if $info->{vmid} && $info->{vmid} == $vmid;
+       }
 
-       my $changes = PVE::QemuServer::update_disksize($vmid, $conf, $volid_hash);
+       my $changes = update_disksize($vmid, $conf, $vm_volids);
 
-       PVE::QemuServer::update_config_nolock($vmid, $conf, 1) if $changes;
+       update_config_nolock($vmid, $conf, 1) if $changes;
     };
 
     if (defined($vmid)) {
        if ($nolock) {
            &$updatefn($vmid);
        } else {
-           PVE::QemuServer::lock_config($vmid, $updatefn, $vmid);
+           lock_config($vmid, $updatefn, $vmid);
        }
     } else {
        my $vmlist = config_list();
@@ -3649,7 +3651,7 @@ sub rescan {
            if ($nolock) {
                &$updatefn($vmid);
            } else {
-               PVE::QemuServer::lock_config($vmid, $updatefn, $vmid);
+               lock_config($vmid, $updatefn, $vmid);
            }    
        }
     }
@@ -3700,9 +3702,12 @@ sub restore_vma_archive {
 
     my $rpcenv = PVE::RPCEnvironment::get();
 
-    my $conffile = PVE::QemuServer::config_file($vmid);
+    my $conffile = config_file($vmid);
     my $tmpfn = "$conffile.$$.tmp";
 
+    # Note: $oldconf is undef if VM does not exists
+    my $oldconf = PVE::Cluster::cfs_read_file(cfs_config_path($vmid));
+
     my $print_devmap = sub {
        my $virtdev_hash = {};
 
@@ -3743,8 +3748,32 @@ sub restore_vma_archive {
                if !$devinfo->{$devname}->{virtdev};        
        }
 
-       my $map = {};
        my $cfg = cfs_read_file('storage.cfg');
+
+       # create empty/temp config
+       if ($oldconf) { 
+           PVE::Tools::file_set_contents($conffile, "memory: 128\n");
+           foreach_drive($oldconf, sub {
+               my ($ds, $drive) = @_;
+
+               return if drive_is_cdrom($drive);
+
+               my $volid = $drive->{file};
+
+               return if !$volid || $volid =~ m|^/|;
+
+               my ($path, $owner) = PVE::Storage::path($cfg, $volid);
+               return if !$path || !$owner || ($owner != $vmid);
+
+               # Note: only delete disk we want to restore
+               # other volumes will become unused
+               if ($virtdev_hash->{$ds}) {
+                   PVE::Storage::vdisk_free($cfg, $volid);
+               }
+           });
+       }
+
+       my $map = {};
        foreach my $virtdev (sort keys %$virtdev_hash) {
            my $d = $virtdev_hash->{$virtdev};
            my $alloc_size = int(($d->{size} + 1024 - 1)/1024);
@@ -3757,7 +3786,8 @@ sub restore_vma_archive {
 
            my $write_zeros = 1;
            # fixme: what other storages types initialize volumes with zero?
-           if ($scfg->{type} eq 'dir' || $scfg->{type} eq 'nfs') {
+           if ($scfg->{type} eq 'dir' || $scfg->{type} eq 'nfs' || 
+               $scfg->{type} eq 'sheepdog' || $scfg->{type} eq 'rbd') {
                $write_zeros = 0;
            }
 
@@ -3839,10 +3869,12 @@ sub restore_vma_archive {
     }
 
     rmtree $tmpdir;
-    
-    rename $tmpfn, $conffile ||
+
+    rename($tmpfn, $conffile) ||
        die "unable to commit configuration file '$conffile'\n";
 
+    PVE::Cluster::cfs_update(); # make sure we read new file
+
     eval { rescan($vmid, 1); };
     warn $@ if $@;
 }
@@ -3851,11 +3883,17 @@ sub restore_tar_archive {
     my ($archive, $vmid, $user, $opts) = @_;
 
     if ($archive ne '-') {
-       my $firstfile = archive_read_firstfile($archive);
+       my $firstfile = tar_archive_read_firstfile($archive);
        die "ERROR: file '$archive' dos not lock like a QemuServer vzdump backup\n"
            if $firstfile ne 'qemu-server.conf';
     }
 
+    my $storecfg = cfs_read_file('storage.cfg');
+
+    # destroy existing data - keep empty config
+    my $vmcfgfn = PVE::QemuServer::config_file($vmid);
+    destroy_vm($storecfg, $vmid, 1) if -f $vmcfgfn;
+
     my $tocmd = "/usr/lib/qemu-server/qmextract";
 
     $tocmd .= " --storage " . PVE::Tools::shellquote($opts->{storage}) if $opts->{storage};
@@ -3875,7 +3913,7 @@ sub restore_tar_archive {
     local $ENV{VZDUMP_VMID} = $vmid;
     local $ENV{VZDUMP_USER} = $user;
 
-    my $conffile = PVE::QemuServer::config_file($vmid);
+    my $conffile = config_file($vmid);
     my $tmpfn = "$conffile.$$.tmp";
 
     # disable interrupts (always do cleanups)
@@ -3935,7 +3973,7 @@ sub restore_tar_archive {
 
        unlink $tmpfn;
 
-       restore_cleanup("$tmpdir/qmrestore.stat") if !$opts->{info};
+       tar_restore_cleanup($storecfg, "$tmpdir/qmrestore.stat") if !$opts->{info};
 
        die $err;
     }
@@ -3945,6 +3983,8 @@ sub restore_tar_archive {
     rename $tmpfn, $conffile ||
        die "unable to commit configuration file '$conffile'\n";
 
+    PVE::Cluster::cfs_update(); # make sure we read new file
+
     eval { rescan($vmid, 1); };
     warn $@ if $@;
 };
@@ -4066,6 +4106,9 @@ my $snapshot_prepare = sub {
 
        my $conf = load_config($vmid);
 
+       die "you can't take a snapshot if it's a template\n" 
+           if is_template($conf);
+
        check_lock($conf);
 
        $conf->{lock} = 'snapshot';
@@ -4074,28 +4117,7 @@ my $snapshot_prepare = sub {
            if defined($conf->{snapshots}->{$snapname}); 
 
        my $storecfg = PVE::Storage::config();
-
-       foreach_drive($conf, sub {
-           my ($ds, $drive) = @_;
-
-           return if drive_is_cdrom($drive);
-           my $volid = $drive->{file};
-
-           my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
-           if ($storeid) {
-               my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
-               die "can't snapshot volume '$volid'\n"          
-                   if !(($scfg->{path} && $volname =~ m/\.qcow2$/) ||
-                        ($scfg->{type} eq 'nexenta') || 
-                        ($scfg->{type} eq 'rbd') || 
-                        ($scfg->{type} eq 'sheepdog'));
-           } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
-               die "snapshot device '$volid' is not possible\n";
-           } else {
-               die "can't snapshot volume '$volid'\n";
-           }
-       });
-
+       die "snapshot feature is not available" if !has_feature('snapshot', $conf, $storecfg);
 
        $snap = $conf->{snapshots}->{$snapname} = {};
 
@@ -4160,6 +4182,8 @@ sub snapshot_rollback {
 
        my $conf = load_config($vmid);
 
+       die "you can't rollback if vm is a template\n" if is_template($conf);
+
        $snap = $conf->{snapshots}->{$snapname};
 
        die "snapshot '$snapname' does not exist\n" if !defined($snap); 
@@ -4217,7 +4241,7 @@ my $savevm_wait = sub {
     my ($vmid) = @_;
 
     for(;;) {
-       my $stat = PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "query-savevm");
+       my $stat = vm_mon_cmd_nocheck($vmid, "query-savevm");
        if (!$stat->{status}) {
            die "savevm not active\n";
        } elsif ($stat->{status} eq 'active') {
@@ -4315,7 +4339,11 @@ sub snapshot_delete {
 
        my $conf = load_config($vmid);
 
-       check_lock($conf) if !$drivehash;
+       if (!$drivehash) {
+           check_lock($conf);
+           die "you can't delete a snapshot if vm is a template\n" 
+               if is_template($conf);
+       }
 
        $snap = $conf->{snapshots}->{$snapname};
 
@@ -4395,4 +4423,49 @@ sub snapshot_delete {
     lock_config($vmid, $updatefn);
 }
 
+sub has_feature {
+    my ($feature, $conf, $storecfg, $snapname, $running) = @_;
+
+    my $err = undef;
+    foreach_drive($conf, sub {
+       my ($ds, $drive) = @_;
+
+       return if drive_is_cdrom($drive);
+       my $volid = $drive->{file};
+       $err = 1 if !PVE::Storage::volume_has_feature($storecfg, $feature, $volid, $snapname, $running);
+    });
+
+    return 1 if !$err;
+}
+
+sub template_create {
+    my ($vmid, $conf, $disk) = @_;
+
+    my $running = check_running($vmid);
+    die "you can't convert a vm to template if vm is running vm\n" if $running;
+
+    my $storecfg = PVE::Storage::config();
+
+    foreach_drive($conf, sub {
+       my ($ds, $drive) = @_;
+
+       return if drive_is_cdrom($drive);
+       return if $disk && $ds ne $disk;
+
+       my $volid = $drive->{file};
+       return if !PVE::Storage::volume_has_feature($storecfg, 'template', $volid);
+
+       my $voliddst = PVE::Storage::vdisk_create_base($storecfg, $volid);
+       $drive->{file} = $voliddst;
+       $conf->{$ds} = PVE::QemuServer::print_drive($vmid, $drive);
+       PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
+    });
+}
+
+sub is_template {
+    my ($conf) = @_;
+
+    return 1 if defined $conf->{template} && $conf->{template} == 1;
+}
+
 1;