]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/QemuConfig.pm
implement suspend to disk for running vms
[qemu-server.git] / PVE / QemuConfig.pm
index b24773cc9e9052f312f1894fff1ba6992fb3a93e..6693585016fb7f53ce485d4aa8940aee6a85f581 100644 (file)
@@ -20,7 +20,7 @@ mkdir $confdir;
 my $lock_dir = "/var/lock/qemu-server";
 mkdir $lock_dir;
 
-my $MAX_UNUSED_DISKS = 8;
+my $MAX_UNUSED_DISKS = 256;
 
 # BEGIN implemented abstract methods from PVE::AbstractConfig
 
@@ -116,9 +116,7 @@ sub get_replicatable_volumes {
 }
 
 sub __snapshot_save_vmstate {
-    my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
-
-    my $snap = $conf->{snapshots}->{$snapname};
+    my ($class, $vmid, $conf, $snapname, $storecfg, $suspend) = @_;
 
     # first, use explicitly configured storage
     my $target = $conf->{vmstatestorage};
@@ -138,17 +136,26 @@ sub __snapshot_save_vmstate {
        $target = $shared // $local // 'local';
     }
 
-    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 $driver_state_size = 500; # assume 500MB is enough to safe all driver state;
     my $size = $conf->{memory}*2 + $driver_state_size;
+    my $scfg = PVE::Storage::storage_config($storecfg, $target);
 
     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
-    $snap->{vmstate} = PVE::Storage::vdisk_alloc($storecfg, $target, $vmid, 'raw', $name, $size*1024);
-    # always overwrite machine if we save vmstate. This makes sure we
-    # can restore it later using correct machine type
-    $snap->{machine} = PVE::QemuServer::get_current_qemu_machine($vmid);
+
+    my $statefile = PVE::Storage::vdisk_alloc($storecfg, $target, $vmid, 'raw', $name, $size*1024);
+    my $runningmachine = PVE::QemuServer::get_current_qemu_machine($vmid);
+
+    if ($suspend) {
+       $conf->{vmstate} = $statefile;
+       $conf->{runningmachine} = $runningmachine;
+    } else {
+       my $snap = $conf->{snapshots}->{$snapname};
+       $snap->{vmstate} = $statefile;
+       $snap->{runningmachine} = $runningmachine;
+    }
+
+    return $statefile;
 }
 
 sub __snapshot_check_running {
@@ -281,6 +288,39 @@ sub __snapshot_delete_vol_snapshot {
     push @$unused, $volid;
 }
 
+sub __snapshot_rollback_hook {
+    my ($class, $vmid, $conf, $snap, $prepare, $data) = @_;
+
+    if ($prepare) {
+       # we save the machine of the current config
+       $data->{oldmachine} = $conf->{machine};
+    } else {
+       # if we have a 'runningmachine' entry in the snapshot we use that
+       # for the forcemachine parameter, else we use the old logic
+       if (defined($conf->{runningmachine})) {
+           $data->{forcemachine} = $conf->{runningmachine};
+           delete $conf->{runningmachine};
+       } else {
+           # Note: old code did not store 'machine', so we try to be smart
+           # and guess the snapshot was generated with kvm 1.4 (pc-i440fx-1.4).
+           $data->{forcemachine} = $conf->{machine} || 'pc-i440fx-1.4';
+
+           # we remove the 'machine' configuration if not explicitly specified
+           # in the original config.
+           delete $conf->{machine} if $snap->{vmstate} && !defined($data->{oldmachine});
+       }
+
+       if ($conf->{vmgenid}) {
+           # tell the VM that it's another generation, so it can react
+           # appropriately, e.g. dirty-mark copies of distributed databases or
+           # re-initializing its random number generator
+           $conf->{vmgenid} = PVE::QemuServer::generate_uuid();
+       }
+    }
+
+    return;
+}
+
 sub __snapshot_rollback_vol_possible {
     my ($class, $drive, $snapname) = @_;
 
@@ -309,11 +349,11 @@ sub __snapshot_rollback_vm_stop {
 }
 
 sub __snapshot_rollback_vm_start {
-    my ($class, $vmid, $vmstate, $forcemachine) = @_;
+    my ($class, $vmid, $vmstate, $data) = @_;
 
     my $storecfg = PVE::Storage::config();
     my $statefile = PVE::Storage::path($storecfg, $vmstate);
-    PVE::QemuServer::vm_start($storecfg, $vmid, $statefile, undef, undef, undef, $forcemachine);
+    PVE::QemuServer::vm_start($storecfg, $vmid, $statefile, undef, undef, undef, $data->{forcemachine});
 }
 
 sub __snapshot_rollback_get_unused {