]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/QemuServer.pm
add downtime && expected_downtime query-migrate info
[qemu-server.git] / PVE / QemuServer.pm
index 8f536e3d058dd3754aad0bbc709c9ec0ddb241ae..a9dd544629bd28e840c93758a41b10eee19bd07b 100644 (file)
@@ -235,7 +235,7 @@ my $confdesc = {
     ostype => {
        optional => 1,
        type => 'string',
-        enum => [qw(other wxp w2k w2k3 w2k8 wvista win7 l24 l26)],
+        enum => [qw(other wxp w2k w2k3 w2k8 wvista win7 win8 l24 l26)],
        description => <<EODESC,
 Used to enable special optimization/features for specific
 operating systems:
@@ -247,11 +247,12 @@ w2k3   => Microsoft Windows 2003
 w2k8   => Microsoft Windows 2008
 wvista => Microsoft Windows Vista
 win7   => Microsoft Windows 7
+win8   => Microsoft Windows 8/2012
 l24    => Linux 2.4 Kernel
 l26    => Linux 2.6/3.X Kernel
 
-other|l24|l26                  ... no special behaviour
-wxp|w2k|w2k3|w2k8|wvista|win7  ... use --localtime switch
+other|l24|l26                       ... no special behaviour
+wxp|w2k|w2k3|w2k8|wvista|win7|win8  ... use --localtime switch
 EODESC
     },
     boot => {
@@ -309,8 +310,8 @@ EODESC
     tdf => {
        optional => 1,
        type => 'boolean',
-       description => "Enable/disable time drift fix. This is ignored for kvm versions newer that 1.0 (not needed anymore).",
-       default => 1,
+       description => "Enable/disable time drift fix.",
+       default => 0,
     },
     localtime => {
        optional => 1,
@@ -325,7 +326,7 @@ EODESC
     vga => {
        optional => 1,
        type => 'string',
-       description => "Select VGA type. If you want to use high resolution modes (>= 1280x1024x16) then you should use option 'std' or 'vmware'. Default is 'std' for win7/w2k8, and 'cirrur' for other OS types",
+       description => "Select VGA type. If you want to use high resolution modes (>= 1280x1024x16) then you should use option 'std' or 'vmware'. Default is 'std' for win8/win7/w2k8, and 'cirrur' for other OS types",
        enum => [qw(std cirrus vmware)],
     },
     watchdog => {
@@ -387,7 +388,7 @@ EODESCR
        optional => 1,
        description => "Emulated CPU type.",
        type => 'string',
-       enum => [ qw(486 athlon pentium pentium2 pentium3 coreduo core2duo kvm32 kvm64 qemu32 qemu64 phenom cpu64-rhel6 cpu64-rhel5 Conroe Penryn Nehalem Westmere Opteron_G1 Opteron_G2 Opteron_G3 host) ],
+       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',
     },
     parent => get_standard_option('pve-snapshot-name', {
@@ -400,6 +401,11 @@ EODESCR
        type => 'integer',
        minimum => 0,
     },
+    vmstate => {
+       optional => 1,
+       type => 'string', format => 'pve-volume-id',
+       description => "Reference to a volume which stores the VM state. This is used internally for snapshots.",
+    },
 };
 
 # what about other qemu settings ?
@@ -642,7 +648,7 @@ sub kvm_user_version {
 
     my $tmp = `kvm -help 2>/dev/null`;
 
-    if ($tmp =~ m/^QEMU( PC)? emulator version (\d+\.\d+(\.\d+)?) /) {
+    if ($tmp =~ m/^QEMU( PC)? emulator version (\d+\.\d+(\.\d+)?)[,\s]/) {
        $kvm_user_version = $2;
     }
 
@@ -685,6 +691,7 @@ sub os_list_description {
        w2k8 => 'Windows 2008',
        wvista => 'Windows Vista',
        win7 => 'Windows 7',
+       win8 => 'Windows 8/2012',
        l24 => 'Linux 2.4',
        l26 => 'Linux 2.6',
     };
@@ -1399,7 +1406,7 @@ sub json_config_properties {
     my $prop = shift;
 
     foreach my $opt (keys %$confdesc) {
-       next if $opt eq 'parent' || $opt eq 'snaptime';
+       next if $opt eq 'parent' || $opt eq 'snaptime' || $opt eq 'vmstate';
        $prop->{$opt} = $confdesc->{$opt};
     }
 
@@ -2077,10 +2084,45 @@ sub foreach_drive {
     }
 }
 
+sub foreach_volid {
+    my ($conf, $func) = @_;
+    
+    my $volhash = {};
+
+    my $test_volid = sub {
+       my ($volid, $is_cdrom) = @_;
+
+       return if !$volid;
+       
+       $volhash->{$volid} = $is_cdrom || 0;
+    };
+
+    PVE::QemuServer::foreach_drive($conf, sub {
+       my ($ds, $drive) = @_;
+       &$test_volid($drive->{file}, drive_is_cdrom($drive));
+    });
+
+    foreach my $snapname (keys %{$conf->{snapshots}}) {
+       my $snap = $conf->{snapshots}->{$snapname};
+       &$test_volid($snap->{vmstate}, 0);
+       PVE::QemuServer::foreach_drive($snap, sub {
+           my ($ds, $drive) = @_;
+           &$test_volid($drive->{file}, drive_is_cdrom($drive));
+        });
+    }
+
+    foreach my $volid (keys %$volhash) {
+       &$func($volid, $volhash->{$volid});     
+    }
+}
+
 sub config_to_command {
-    my ($storecfg, $vmid, $conf, $defaults, $migrate_uri) = @_;
+    my ($storecfg, $vmid, $conf, $defaults) = @_;
 
     my $cmd = [];
+    my $globalFlags = [];
+    my $machineFlags = [];
+    my $rtcFlags = [];
     my $devices = [];
     my $pciaddr = '';
     my $bridges = {};
@@ -2113,10 +2155,6 @@ sub config_to_command {
 
     push @$cmd, '-daemonize';
 
-    push @$cmd, '-incoming', $migrate_uri if $migrate_uri;
-
-    push @$cmd, '-S' if $migrate_uri;
-
     my $use_usb2 = 0;
     for (my $i = 0; $i < $MAX_USB_DEVICES; $i++)  {
        next if !$conf->{"usb$i"};
@@ -2205,7 +2243,7 @@ sub config_to_command {
 
     my $vga = $conf->{vga};
     if (!$vga) {
-       if ($conf->{ostype} && ($conf->{ostype} eq 'win7' || $conf->{ostype} eq 'w2k8')) {
+       if ($conf->{ostype} && ($conf->{ostype} eq 'win8' || $conf->{ostype} eq 'win7' || $conf->{ostype} eq 'w2k8')) {
            $vga = 'std';
        } else {
            $vga = 'cirrus';
@@ -2216,43 +2254,42 @@ sub config_to_command {
 
     # time drift fix
     my $tdf = defined($conf->{tdf}) ? $conf->{tdf} : $defaults->{tdf};
-    # ignore - no longer supported by newer kvm
-    # push @$cmd, '-tdf' if $tdf;
 
     my $nokvm = defined($conf->{kvm}) && $conf->{kvm} == 0 ? 1 : 0;
+    my $useLocaltime = $conf->{localtime};
 
     if (my $ost = $conf->{ostype}) {
-       # other, wxp, w2k, w2k3, w2k8, wvista, win7, l24, l26
+       # other, wxp, w2k, w2k3, w2k8, wvista, win7, win8, l24, l26
 
        if ($ost =~ m/^w/) { # windows
-           push @$cmd, '-localtime' if !defined($conf->{localtime});
+           $useLocaltime = 1 if !defined($conf->{localtime});
 
-           # use rtc-td-hack when acpi is enabled
+           # use time drift fix when acpi is enabled
            if (!(defined($conf->{acpi}) && $conf->{acpi} == 0)) {
-               push @$cmd, '-rtc-td-hack';
+               $tdf = 1 if !defined($conf->{tdf});
            }
        }
 
-       if ($ost eq 'win7' || $ost eq 'w2k8' || $ost eq 'wvista') {
-           push @$cmd, '-no-kvm-pit-reinjection';
+       if ($ost eq 'win7' || $ost eq 'win8' || $ost eq 'w2k8' || 
+           $ost eq 'wvista') {
+           push @$globalFlags, 'kvm-pit.lost_tick_policy=discard';
            push @$cmd, '-no-hpet';
        }
-
-       # -tdf ?
-       # -no-acpi
-       # -no-kvm
-       # -win2k-hack ?
     }
 
+    push @$rtcFlags, 'driftfix=slew' if $tdf;
+
     if ($nokvm) {
-       push @$cmd, '-no-kvm';
+       push @$machineFlags, 'accel=tcg';
     } else {
        die "No accelerator found!\n" if !$cpuinfo->{hvm};
     }
 
-    push @$cmd, '-localtime' if $conf->{localtime};
-
-    push @$cmd, '-startdate', $conf->{startdate} if $conf->{startdate};
+    if ($conf->{startdate}) {
+       push @$rtcFlags, "base=$conf->{startdate}";
+    } elsif ($useLocaltime) {
+       push @$rtcFlags, 'base=localtime';
+    }
 
     push @$cmd, '-S' if $conf->{freeze};
 
@@ -2378,6 +2415,13 @@ sub config_to_command {
     }
 
     push @$cmd, @$devices;
+    push @$cmd, '-rtc', join(',', @$rtcFlags) 
+       if scalar(@$rtcFlags);
+    push @$cmd, '-machine', join(',', @$machineFlags) 
+       if scalar(@$machineFlags);
+    push @$cmd, '-global', join(',', @$globalFlags)
+       if scalar(@$globalFlags);
+
     return wantarray ? ($cmd, $vollist) : $cmd;
 }
 
@@ -2817,36 +2861,13 @@ sub qemu_volume_snapshot {
 sub qemu_volume_snapshot_delete {
     my ($vmid, $deviceid, $storecfg, $volid, $snap) = @_;
 
-     #need to implement statefile location
-    my $statefile="/tmp/$vmid-$snap";
-
-    unlink $statefile if -e $statefile;
-
     my $running = PVE::QemuServer::check_running($vmid);
 
     return if !PVE::Storage::volume_snapshot_delete($storecfg, $volid, $snap, $running);
 
     return if !$running;
 
-    #need to split delvm monitor command like savevm
-
-}
-
-sub qemu_snapshot_start {
-    my ($vmid, $snap) = @_;
-
-    #need to implement statefile location
-    my $statefile="/tmp/$vmid-$snap";
-
-    vm_mon_cmd($vmid, "snapshot-start", statefile => $statefile);
-
-}
-
-sub qemu_snapshot_end {
-    my ($vmid) = @_;
-
-    vm_mon_cmd($vmid, "snapshot-end");
-
+    vm_mon_cmd($vmid, "delete-drive-snapshot", device => $deviceid, name => $snap);
 }
 
 sub qga_freezefs {
@@ -2871,28 +2892,26 @@ sub vm_start {
 
        die "VM $vmid already running\n" if check_running($vmid, undef, $migratedfrom);
 
-       my $migrate_uri;
+       my $defaults = load_defaults();
+
+       # set environment variable useful inside network script
+       $ENV{PVE_MIGRATED_FROM} = $migratedfrom if $migratedfrom;
+
+       my ($cmd, $vollist) = config_to_command($storecfg, $vmid, $conf, $defaults);
+
        my $migrate_port = 0;
 
        if ($statefile) {
            if ($statefile eq 'tcp') {
                $migrate_port = next_migrate_port();
-               $migrate_uri = "tcp:localhost:${migrate_port}";
+               my $migrate_uri = "tcp:localhost:${migrate_port}";
+               push @$cmd, '-incoming', $migrate_uri;
+               push @$cmd, '-S';
            } else {
-               if (-f $statefile) {
-                   $migrate_uri = "exec:cat $statefile";
-               } else {
-                   warn "state file '$statefile' does not exist - doing normal startup\n";
-               }
+               push @$cmd, '-loadstate', $statefile;
            }
        }
 
-       my $defaults = load_defaults();
-
-       # set environment variable useful inside network script
-       $ENV{PVE_MIGRATED_FROM} = $migratedfrom if $migratedfrom;
-
-       my ($cmd, $vollist) = config_to_command($storecfg, $vmid, $conf, $defaults, $migrate_uri);
        # host pci devices
         for (my $i = 0; $i < $MAX_HOSTPCI_DEVICES; $i++)  {
           my $d = parse_hostpci($conf->{"hostpci$i"});
@@ -2906,34 +2925,16 @@ sub vm_start {
 
        PVE::Storage::activate_volumes($storecfg, $vollist);
 
-       eval  { run_command($cmd, timeout => $migrate_uri ? undef : 30); };
+       eval  { run_command($cmd, timeout => $statefile ? undef : 30,
+                   umask => 0077); };
        my $err = $@;
        die "start failed: $err" if $err;
 
-       if ($statefile) {
+       print "migration listens on port $migrate_port\n" if $migrate_port;
 
-           if ($statefile eq 'tcp') {
-               print "migration listens on port $migrate_port\n";
-           } else {
-               unlink $statefile;
-               # fixme: send resume - is that necessary ?
-               eval { vm_mon_cmd($vmid, "cont"); };
-           }
-       }
-
-       # 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 ($statefile && $statefile ne 'tcp')  {
+           eval { vm_mon_cmd_nocheck($vmid, "cont"); };
+           warn $@ if $@;
        }
 
        if($migratedfrom) {
@@ -2943,8 +2944,8 @@ sub vm_start {
            eval { PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate-set-capabilities", capabilities => [$capabilities]); };
        }
 
-       vm_balloonset($vmid, $conf->{balloon}) if $conf->{balloon};
-
+       vm_mon_cmd_nocheck($vmid, "balloon", value => $conf->{balloon}*1024*1024) 
+           if $conf->{balloon};
     });
 }
 
@@ -3038,14 +3039,13 @@ sub get_vm_volumes {
     my ($conf) = @_;
 
     my $vollist = [];
-    foreach_drive($conf, sub {
-       my ($ds, $drive) = @_;
+    foreach_volid($conf, sub {
+       my ($volid, $is_cdrom) = @_;
 
-       my ($sid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1);
-       return if !$sid;
+       return if $volid =~ m|^/|;
 
-       my $volid = $drive->{file};
-       return if !$volid || $volid =~ m|^/|;
+       my ($sid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
+       return if !$sid;
 
        push @$vollist, $volid;
     });
@@ -3378,7 +3378,7 @@ sub print_pci_addr {
 sub vm_balloonset {
     my ($vmid, $value) = @_;
 
-    vm_mon_cmd($vmid, "balloon", value => $value);
+    vm_mon_cmd($vmid, "balloon", value => $value*1024*1024);
 }
 
 # vzdump restore implementaion
@@ -3530,7 +3530,7 @@ sub restore_archive {
                $net->{macaddr} = PVE::Tools::random_ether_addr() if $net->{macaddr};
                $netstr = print_net($net);
                print $outfd "$id: $netstr\n";
-           } elsif ($line =~ m/^((ide|scsi|virtio)\d+):\s*(\S+)\s*$/) {
+           } elsif ($line =~ m/^((ide|scsi|virtio|sata)\d+):\s*(\S+)\s*$/) {
                my $virtdev = $1;
                my $value = $2;
                if ($line =~ m/backup=no/) {
@@ -3583,6 +3583,7 @@ my $snapshot_copy_config = sub {
        next if $k eq 'snapshots';
        next if $k eq 'snapstate';
        next if $k eq 'snaptime';
+       next if $k eq 'vmstate';
        next if $k eq 'lock';
        next if $k eq 'digest';
        next if $k eq 'description';
@@ -3611,8 +3612,72 @@ my $snapshot_apply_config = sub {
     return $newconf;
 };
 
+sub foreach_writable_storage {
+    my ($conf, $func) = @_;
+
+    my $sidhash = {};
+
+    foreach my $ds (keys %$conf) {
+       next if !valid_drivename($ds);
+
+       my $drive = parse_drive($ds, $conf->{$ds});
+       next if !$drive;
+       next if drive_is_cdrom($drive);
+
+       my $volid = $drive->{file};
+
+       my ($sid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
+       $sidhash->{$sid} = $sid if $sid;        
+    }
+
+    foreach my $sid (sort keys %$sidhash) {
+       &$func($sid);
+    }
+}
+
+my $alloc_vmstate_volid = sub {
+    my ($storecfg, $vmid, $conf, $snapname) = @_;
+    
+    # Note: we try to be smart when selecting a $target storage
+
+    my $target;
+
+    # search shared storage first
+    foreach_writable_storage($conf, sub {
+       my ($sid) = @_;
+       my $scfg = PVE::Storage::storage_config($storecfg, $sid);
+       return if !$scfg->{shared};
+
+       $target = $sid if !$target || $scfg->{path}; # prefer file based storage
+    });
+
+    if (!$target) {
+       # now search local storage
+       foreach_writable_storage($conf, sub {
+           my ($sid) = @_;
+           my $scfg = PVE::Storage::storage_config($storecfg, $sid);
+           return if $scfg->{shared};
+
+           $target = $sid if !$target || $scfg->{path}; # prefer file based storage;
+       });
+    }
+
+    $target = 'local' if !$target;
+
+    my $driver_state_size = 500; # assume 32MB is enough to safe all driver state;
+    # we abort live save after $conf->{memory}, so we need at max twice that space
+    my $size = $conf->{memory}*2 + $driver_state_size;
+
+    my $name = "vm-$vmid-state-$snapname";
+    my $scfg = PVE::Storage::storage_config($storecfg, $target);
+    $name .= ".raw" if $scfg->{path}; # add filename extension for file base storage
+    my $volid = PVE::Storage::vdisk_alloc($storecfg, $target, $vmid, 'raw', $name, $size*1024);
+
+    return $volid;
+};
+
 my $snapshot_prepare = sub {
-    my ($vmid, $snapname, $comment) = @_;
+    my ($vmid, $snapname, $save_vmstate, $comment) = @_;
 
     my $snap;
 
@@ -3629,7 +3694,7 @@ my $snapshot_prepare = sub {
 
        my $storecfg = PVE::Storage::config();
 
-       PVE::QemuServer::foreach_drive($conf, sub {
+       foreach_drive($conf, sub {
            my ($ds, $drive) = @_;
 
            return if drive_is_cdrom($drive);
@@ -3640,6 +3705,7 @@ my $snapshot_prepare = sub {
                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) {
@@ -3649,8 +3715,13 @@ my $snapshot_prepare = sub {
            }
        });
 
+
        $snap = $conf->{snapshots}->{$snapname} = {};
 
+       if ($save_vmstate && check_running($vmid)) {
+           $snap->{vmstate} = &$alloc_vmstate_volid($storecfg, $vmid, $conf, $snapname);
+       }
+
        &$snapshot_copy_config($conf, $snap);
 
        $snap->{snapstate} = "prepare";
@@ -3702,11 +3773,23 @@ sub snapshot_rollback {
 
     my $prepare = 1;
 
+    my $storecfg = PVE::Storage::config();
     my $updatefn = sub {
 
        my $conf = load_config($vmid);
 
-       check_lock($conf) if $prepare;
+       $snap = $conf->{snapshots}->{$snapname};
+
+       die "snapshot '$snapname' does not exist\n" if !defined($snap); 
+
+       die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n" 
+           if $snap->{snapstate};
+
+       if ($prepare) {
+           check_lock($conf);
+           vm_stop($storecfg, $vmid, undef, undef, 5, undef, undef);
+       }
 
        die "unable to rollback vm $vmid: vm is running\n"
            if check_running($vmid);
@@ -3718,13 +3801,6 @@ sub snapshot_rollback {
            delete $conf->{lock};
        }
 
-       $snap = $conf->{snapshots}->{$snapname};
-
-       die "snapshot '$snapname' does not exist\n" if !defined($snap); 
-
-       die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n" 
-           if $snap->{snapstate};
-
        if (!$prepare) {
            # copy snapshot config to current config
            $conf = &$snapshot_apply_config($conf, $snap);
@@ -3732,11 +3808,16 @@ sub snapshot_rollback {
        }
 
        update_config_nolock($vmid, $conf, 1);
+
+       if (!$prepare && $snap->{vmstate}) {
+           my $statefile = PVE::Storage::path($storecfg, $snap->{vmstate});
+           # fixme: this only forws for files currently
+           vm_start($storecfg, $vmid, $statefile);
+       }
+
     };
 
     lock_config($vmid, $updatefn);
-
-    my $storecfg = PVE::Storage::config();
     
     foreach_drive($snap, sub {
        my ($ds, $drive) = @_;
@@ -3753,23 +3834,51 @@ sub snapshot_rollback {
     lock_config($vmid, $updatefn);
 }
 
+my $savevm_wait = sub {
+    my ($vmid) = @_;
+
+    for(;;) {
+       my $stat = PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "query-savevm");
+       if (!$stat->{status}) {
+           die "savevm not active\n";
+       } elsif ($stat->{status} eq 'active') {
+           sleep(1);
+           next;
+       } elsif ($stat->{status} eq 'completed') {
+           last;
+       } else {
+           die "query-savevm returned status '$stat->{status}'\n";
+       }
+    }
+};
+
 sub snapshot_create {
-    my ($vmid, $snapname, $vmstate, $freezefs, $comment) = @_;
+    my ($vmid, $snapname, $save_vmstate, $freezefs, $comment) = @_;
 
-    my $snap = &$snapshot_prepare($vmid, $snapname, $comment);
+    my $snap = &$snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
 
-    $freezefs = $vmstate = 0 if !check_running($vmid);
+    $freezefs = $save_vmstate = 0 if !$snap->{vmstate}; # vm is not running
 
     my $drivehash = {};
 
+    my $running = check_running($vmid);
+
     eval {
        # create internal snapshots of all drives
-       
-       qemu_snapshot_start($vmid, $snapname) if $vmstate;
-
-       qga_freezefs($vmid) if $freezefs;
 
        my $storecfg = PVE::Storage::config();
+
+       if ($running) {
+           if ($snap->{vmstate}) {
+               my $path = PVE::Storage::path($storecfg, $snap->{vmstate});     
+               vm_mon_cmd($vmid, "savevm-start", statefile => $path);
+               &$savevm_wait($vmid);
+           } else {
+               vm_mon_cmd($vmid, "savevm-start");
+           }
+       };
+
+       qga_freezefs($vmid) if $running && $freezefs;
  
        foreach_drive($snap, sub {
            my ($ds, $drive) = @_;
@@ -3785,10 +3894,10 @@ sub snapshot_create {
     };
     my $err = $@;
 
-    eval { gqa_unfreezefs($vmid) if $freezefs; };
+    eval { gqa_unfreezefs($vmid) if $running && $freezefs; };
     warn $@ if $@;
 
-    eval { qemu_snapshot_end($vmid) if $vmstate; };
+    eval { vm_mon_cmd($vmid, "savevm-end") if $running; };
     warn $@ if $@;
 
     if ($err) {
@@ -3841,10 +3950,14 @@ sub snapshot_delete {
        }
 
        if ($remove_drive) {
-           my $drive = parse_drive($remove_drive, $snap->{$remove_drive});
-           my $volid = $drive->{file};
-           delete $snap->{$remove_drive};
-           add_unused_volume($conf, $volid);
+           if ($remove_drive eq 'vmstate') {
+               delete $snap->{$remove_drive};
+           } else {
+               my $drive = parse_drive($remove_drive, $snap->{$remove_drive});
+               my $volid = $drive->{file};
+               delete $snap->{$remove_drive};
+               add_unused_volume($conf, $volid);
+           }
        }
 
        if ($prepare) {
@@ -3862,11 +3975,22 @@ sub snapshot_delete {
 
     lock_config($vmid, $updatefn);
 
-    # now remove all internal snapshots
+    # now remove vmstate file
 
     my $storecfg = PVE::Storage::config();
 
-    PVE::QemuServer::foreach_drive($snap, sub {
+    if ($snap->{vmstate}) {
+       eval {  PVE::Storage::vdisk_free($storecfg, $snap->{vmstate}); };
+       if (my $err = $@) {
+           die $err if !$force;
+           warn $err;
+       }
+       # save changes (remove vmstate from snapshot)
+       lock_config($vmid, $updatefn, 'vmstate') if !$force;
+    };
+
+    # now remove all internal snapshots
+    foreach_drive($snap, sub {
        my ($ds, $drive) = @_;
 
        return if drive_is_cdrom($drive);