]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/QemuConfig.pm
create_efidisk: poll the real size after volume creation
[qemu-server.git] / PVE / QemuConfig.pm
index cd116bd46fd84790dca577554a6c5474f1a6a8ef..f13c7b05982940351a5e6029ba9b477113ed6765 100644 (file)
@@ -5,7 +5,10 @@ use warnings;
 
 use PVE::AbstractConfig;
 use PVE::INotify;
+use PVE::QemuServer::Helpers;
+use PVE::QemuServer::Monitor qw(mon_cmd);
 use PVE::QemuServer;
+use PVE::QemuServer::Machine;
 use PVE::Storage;
 use PVE::Tools;
 
@@ -14,13 +17,25 @@ use base qw(PVE::AbstractConfig);
 my $nodename = PVE::INotify::nodename();
 
 mkdir "/etc/pve/nodes/$nodename";
-my $confdir = "/etc/pve/nodes/$nodename/qemu-server";
-mkdir $confdir;
+mkdir "/etc/pve/nodes/$nodename/qemu-server";
 
 my $lock_dir = "/var/lock/qemu-server";
 mkdir $lock_dir;
 
-my $MAX_UNUSED_DISKS = 8;
+my $MAX_UNUSED_DISKS = 256;
+
+sub assert_config_exists_on_node {
+    my ($vmid, $node) = @_;
+
+    $node //= $nodename;
+
+    my $filename = __PACKAGE__->config_file($vmid, $node);
+    my $exists = -f $filename;
+
+    my $type = guest_type();
+    die "unable to find configuration file for $type $vmid on node '$node'\n"
+       if !$exists;
+}
 
 # BEGIN implemented abstract methods from PVE::AbstractConfig
 
@@ -116,12 +131,11 @@ 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, $statestorage, $suspend) = @_;
 
     # first, use explicitly configured storage
-    my $target = $conf->{vmstatestorage};
+    # either directly via API, or via conf
+    my $target = $statestorage // $conf->{vmstatestorage};
 
     if (!$target) {
        my ($shared, $local);
@@ -138,20 +152,37 @@ 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 $size = $conf->{memory}*2 + $driver_state_size;
+    my $defaults = PVE::QemuServer::load_defaults();
+    my $mem_size = $conf->{memory} // $defaults->{memory};
+    my $driver_state_size = 500; # assume 500MB is enough to safe all driver state;
+    # our savevm-start does live-save of the memory until the space left in the
+    # volume is just enough for the remaining memory content + internal state
+    # then it stops the vm and copies the rest so we reserve twice the
+    # memory content + state to minimize vm downtime
+    my $size = $mem_size*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);
-    $snap->{runningmachine} = PVE::QemuServer::get_current_qemu_machine($vmid);
+
+    my $statefile = PVE::Storage::vdisk_alloc($storecfg, $target, $vmid, 'raw', $name, $size*1024);
+    my $runningmachine = PVE::QemuServer::Machine::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 {
     my ($class, $vmid) = @_;
-    return PVE::QemuServer::check_running($vmid);
+    return PVE::QemuServer::Helpers::vm_running_locally($vmid);
 }
 
 sub __snapshot_check_freeze_needed {
@@ -169,10 +200,10 @@ sub __snapshot_freeze {
     my ($class, $vmid, $unfreeze) = @_;
 
     if ($unfreeze) {
-       eval { PVE::QemuServer::vm_mon_cmd($vmid, "guest-fsfreeze-thaw"); };
+       eval { mon_cmd($vmid, "guest-fsfreeze-thaw"); };
        warn "guest-fsfreeze-thaw problems - $@" if $@;
     } else {
-       eval { PVE::QemuServer::vm_mon_cmd($vmid, "guest-fsfreeze-freeze"); };
+       eval { mon_cmd($vmid, "guest-fsfreeze-freeze"); };
        warn "guest-fsfreeze-freeze problems - $@" if $@;
     }
 }
@@ -188,9 +219,9 @@ sub __snapshot_create_vol_snapshots_hook {
                my $path = PVE::Storage::path($storecfg, $snap->{vmstate});
                PVE::Storage::activate_volumes($storecfg, [$snap->{vmstate}]);
 
-               PVE::QemuServer::vm_mon_cmd($vmid, "savevm-start", statefile => $path);
+               mon_cmd($vmid, "savevm-start", statefile => $path);
                for(;;) {
-                   my $stat = PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "query-savevm");
+                   my $stat = mon_cmd($vmid, "query-savevm");
                    if (!$stat->{status}) {
                        die "savevm not active\n";
                    } elsif ($stat->{status} eq 'active') {
@@ -203,18 +234,18 @@ sub __snapshot_create_vol_snapshots_hook {
                    }
                }
            } else {
-               PVE::QemuServer::vm_mon_cmd($vmid, "savevm-start");
+               mon_cmd($vmid, "savevm-start");
            }
        } elsif ($hook eq "after") {
-           eval { 
-               PVE::QemuServer::vm_mon_cmd($vmid, "savevm-end");
+           eval {
+               mon_cmd($vmid, "savevm-end");
                PVE::Storage::deactivate_volumes($storecfg, [$snap->{vmstate}]) if $snap->{vmstate};
            };
            warn $@ if $@;
        } elsif ($hook eq "after-freeze") {
            # savevm-end is async, we need to wait
            for (;;) {
-               my $stat = PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "query-savevm");
+               my $stat = mon_cmd($vmid, "query-savevm");
                if (!$stat->{bytes}) {
                    last;
                } else {
@@ -300,6 +331,13 @@ sub __snapshot_rollback_hook {
            # 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;
@@ -336,8 +374,7 @@ sub __snapshot_rollback_vm_start {
     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, $data->{forcemachine});
+    PVE::QemuServer::vm_start($storecfg, $vmid, $vmstate, undef, undef, undef, $data->{forcemachine});
 }
 
 sub __snapshot_rollback_get_unused {