]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/QemuConfig.pm
vzdump: log 100% percent in case $target is 0
[qemu-server.git] / PVE / QemuConfig.pm
index e9796a3925a133ae94df3c30bfed3d0a4d59a520..7f350b1e0eab535d2e2204afa6470e5fe4423102 100644 (file)
@@ -5,7 +5,13 @@ use warnings;
 
 use PVE::AbstractConfig;
 use PVE::INotify;
+use PVE::JSONSchema;
+use PVE::QemuServer::CPUConfig;
+use PVE::QemuServer::Drive;
+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 +20,23 @@ 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 = 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
 
@@ -31,7 +47,7 @@ sub guest_type {
 sub __config_max_unused_disks {
     my ($class) = @_;
 
-    return $MAX_UNUSED_DISKS;
+    return $PVE::QemuServer::Drive::MAX_UNUSED_DISKS;
 }
 
 sub config_file_lock {
@@ -51,7 +67,7 @@ sub has_feature {
     my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
 
     my $err;
-    PVE::QemuServer::foreach_drive($conf, sub {
+    $class->foreach_volume($conf, sub {
        my ($ds, $drive) = @_;
 
        return if PVE::QemuServer::drive_is_cdrom($drive);
@@ -63,6 +79,47 @@ sub has_feature {
     return $err ? 0 : 1;
 }
 
+sub valid_volume_keys {
+    my ($class, $reverse) = @_;
+
+    my @keys = PVE::QemuServer::Drive::valid_drive_names();
+
+    return $reverse ? reverse @keys : @keys;
+}
+
+# FIXME: adapt parse_drive to use $noerr for better error messages
+sub parse_volume {
+    my ($class, $key, $volume_string, $noerr) = @_;
+
+    my $volume;
+    if ($key eq 'vmstate') {
+       eval { PVE::JSONSchema::check_format('pve-volume-id', $volume_string) };
+       if (my $err = $@) {
+           return undef if $noerr;
+           die $err;
+       }
+       $volume = { 'file' => $volume_string };
+    } else {
+       $volume = PVE::QemuServer::Drive::parse_drive($key, $volume_string);
+    }
+
+    die "unable to parse volume\n" if !defined($volume) && !$noerr;
+
+    return $volume;
+}
+
+sub print_volume {
+    my ($class, $key, $volume) = @_;
+
+    return PVE::QemuServer::Drive::print_drive($volume);
+}
+
+sub volid_key {
+    my ($class) = @_;
+
+    return 'file';
+}
+
 sub get_replicatable_volumes {
     my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;
 
@@ -105,36 +162,49 @@ sub get_replicatable_volumes {
 
     PVE::QemuServer::foreach_volid($conf, $test_volid);
 
-    # add 'unusedX' volumes to volhash
-    foreach my $key (keys %$conf) {
-       if ($key =~ m/^unused/) {
-           $test_volid->($conf->{$key}, { replicate => 1 });
+    return $volhash;
+}
+
+sub get_backup_volumes {
+    my ($class, $conf) = @_;
+
+    my $return_volumes = [];
+
+    my $test_volume = sub {
+       my ($key, $drive) = @_;
+
+       return if PVE::QemuServer::drive_is_cdrom($drive);
+
+       my $included = $drive->{backup} // 1;
+       my $reason = "backup=";
+       $reason .= defined($drive->{backup}) ? 'no' : 'yes';
+
+       if ($key =~ m/^efidisk/ && (!defined($conf->{bios}) || $conf->{bios} ne 'ovmf')) {
+           $included = 0;
+           $reason = "efidisk but no OMVF BIOS";
        }
-    }
 
-    return $volhash;
+       push @$return_volumes, {
+           key => $key,
+           included => $included,
+           reason => $reason,
+           volume_config => $drive,
+       };
+    };
+
+    PVE::QemuConfig->foreach_volume($conf, $test_volume);
+
+    return $return_volumes;
 }
 
 sub __snapshot_save_vmstate {
     my ($class, $vmid, $conf, $snapname, $storecfg, $statestorage, $suspend) = @_;
 
-    # first, use explicitly configured storage
-    # either directly via API, or via conf
-    my $target = $statestorage // $conf->{vmstatestorage};
+    # use given storage or search for one from the config
+    my $target = $statestorage;
 
     if (!$target) {
-       my ($shared, $local);
-       PVE::QemuServer::foreach_storage_used_by_vm($conf, sub {
-           my ($sid) = @_;
-           my $scfg = PVE::Storage::storage_config($storecfg, $sid);
-           my $dst = $scfg->{shared} ? \$shared : \$local;
-           $$dst = $sid if !$$dst || $scfg->{path}; # prefer file based storage
-       });
-
-       # second, use shared storage where VM has at least one disk
-       # third, use local storage where VM has at least one disk
-       # fall back to local storage
-       $target = $shared // $local // 'local';
+       $target = PVE::QemuServer::find_vmstate_storage($conf, $storecfg);
     }
 
     my $defaults = PVE::QemuServer::load_defaults();
@@ -151,23 +221,28 @@ sub __snapshot_save_vmstate {
     $name .= ".raw" if $scfg->{path}; # add filename extension for file base storage
 
     my $statefile = PVE::Storage::vdisk_alloc($storecfg, $target, $vmid, 'raw', $name, $size*1024);
-    my $runningmachine = PVE::QemuServer::get_current_qemu_machine($vmid);
+    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;
+    # get current QEMU -cpu argument to ensure consistency of custom CPU models
+    my $runningcpu;
+    if (my $pid = PVE::QemuServer::check_running($vmid)) {
+       $runningcpu = PVE::QemuServer::CPUConfig::get_cpu_from_running_vm($pid);
+    }
+
+    if (!$suspend) {
+       $conf = $conf->{snapshots}->{$snapname};
     }
 
+    $conf->{vmstate} = $statefile;
+    $conf->{runningmachine} = $runningmachine;
+    $conf->{runningcpu} = $runningcpu;
+
     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 {
@@ -185,10 +260,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 $@;
     }
 }
@@ -204,9 +279,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') {
@@ -219,18 +294,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");
+               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 {
@@ -307,6 +382,11 @@ sub __snapshot_rollback_hook {
        if (defined($conf->{runningmachine})) {
            $data->{forcemachine} = $conf->{runningmachine};
            delete $conf->{runningmachine};
+
+           # runningcpu is newer than runningmachine, so assume it only exists
+           # here, if at all
+           $data->{forcecpu} = delete $conf->{runningcpu}
+               if defined($conf->{runningcpu});
        } 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).
@@ -359,7 +439,12 @@ sub __snapshot_rollback_vm_start {
     my ($class, $vmid, $vmstate, $data) = @_;
 
     my $storecfg = PVE::Storage::config();
-    PVE::QemuServer::vm_start($storecfg, $vmid, $vmstate, undef, undef, undef, $data->{forcemachine});
+    my $params = {
+       statefile => $vmstate,
+       forcemachine => $data->{forcemachine},
+       forcecpu => $data->{forcecpu},
+    };
+    PVE::QemuServer::vm_start($storecfg, $vmid, $params);
 }
 
 sub __snapshot_rollback_get_unused {
@@ -367,7 +452,7 @@ sub __snapshot_rollback_get_unused {
 
     my $unused = [];
 
-    $class->__snapshot_foreach_volume($conf, sub {
+    $class->foreach_volume($conf, sub {
        my ($vs, $volume) = @_;
 
        return if PVE::QemuServer::drive_is_cdrom($volume);
@@ -375,7 +460,7 @@ sub __snapshot_rollback_get_unused {
        my $found = 0;
        my $volid = $volume->{file};
 
-       $class->__snapshot_foreach_volume($snap, sub {
+       $class->foreach_volume($snap, sub {
            my ($ds, $drive) = @_;
 
            return if $found;
@@ -391,11 +476,6 @@ sub __snapshot_rollback_get_unused {
     return $unused;
 }
 
-sub __snapshot_foreach_volume {
-    my ($class, $conf, $func) = @_;
-
-    PVE::QemuServer::foreach_drive($conf, $func);
-}
 # END implemented abstract methods from PVE::AbstractConfig
 
 1;