]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/API2/Qemu.pm
followup: reorder error so that $volid is last (potential long)
[qemu-server.git] / PVE / API2 / Qemu.pm
index 65a192f36dc7ed996a275c325197eaa698cdbd05..1e8d3500c72c397523f0688713e17d78684b91f4 100644 (file)
@@ -260,6 +260,7 @@ my $hwtypeoptions = {
     'tablet' => 1,
     'vga' => 1,
     'watchdog' => 1,
+    'audio0' => 1,
 };
 
 my $generaloptions = {
@@ -1179,14 +1180,18 @@ my $update_vm_api  = sub {
            foreach my $opt (@delete) {
                $modified->{$opt} = 1;
                $conf = PVE::QemuConfig->load_config($vmid); # update/reload
-               if (!defined($conf->{$opt}) && !defined($conf->{pending}->{$opt})) {
+
+               # value of what we want to delete, independent if pending or not
+               my $val = $conf->{$opt} // $conf->{pending}->{$opt};
+               if (!defined($val)) {
                    warn "cannot delete '$opt' - not set in current configuration!\n";
                    $modified->{$opt} = 0;
                    next;
                }
+               my $is_pending_val = defined($conf->{pending}->{$opt});
 
                if ($opt =~ m/^unused/) {
-                   my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
+                   my $drive = PVE::QemuServer::parse_drive($opt, $val);
                    PVE::QemuConfig->check_protection($conf, "can't remove unused disk '$drive->{file}'");
                    $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
                    if (PVE::QemuServer::try_deallocate_drive($storecfg, $vmid, $conf, $opt, $drive, $rpcenv, $authuser)) {
@@ -1196,12 +1201,12 @@ my $update_vm_api  = sub {
                } elsif (PVE::QemuServer::is_valid_drivename($opt)) {
                    PVE::QemuConfig->check_protection($conf, "can't remove drive '$opt'");
                    $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
-                   PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $conf->{pending}->{$opt}))
-                       if defined($conf->{pending}->{$opt});
+                   PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $val))
+                       if $is_pending_val;
                    PVE::QemuServer::vmconfig_delete_pending_option($conf, $opt, $force);
                    PVE::QemuConfig->write_config($vmid, $conf);
                } elsif ($opt =~ m/^serial\d+$/) {
-                   if ($conf->{$opt} eq 'socket') {
+                   if ($val eq 'socket') {
                        $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']);
                    } elsif ($authuser ne 'root@pam') {
                        die "only root can delete '$opt' config for real devices\n";
@@ -1209,7 +1214,7 @@ my $update_vm_api  = sub {
                    PVE::QemuServer::vmconfig_delete_pending_option($conf, $opt, $force);
                    PVE::QemuConfig->write_config($vmid, $conf);
                } elsif ($opt =~ m/^usb\d+$/) {
-                   if ($conf->{$opt} =~ m/spice/) {
+                   if ($val =~ m/spice/) {
                        $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']);
                    } elsif ($authuser ne 'root@pam') {
                        die "only root can delete '$opt' config for real devices\n";
@@ -1436,7 +1441,6 @@ __PACKAGE__->register_method({
     }
 });
 
-
 __PACKAGE__->register_method({
     name => 'destroy_vm',
     path => '{vmid}',
@@ -1462,9 +1466,7 @@ __PACKAGE__->register_method({
        my ($param) = @_;
 
        my $rpcenv = PVE::RPCEnvironment::get();
-
        my $authuser = $rpcenv->get_user();
-
        my $vmid = $param->{vmid};
 
        my $skiplock = $param->{skiplock};
@@ -1473,11 +1475,8 @@ __PACKAGE__->register_method({
 
        # test if VM exists
        my $conf = PVE::QemuConfig->load_config($vmid);
-
        my $storecfg = PVE::Storage::config();
-
        PVE::QemuConfig->check_protection($conf, "can't remove VM $vmid");
-
        die "unable to remove VM $vmid - used in HA resources\n"
            if PVE::HA::Config::vm_is_ha_managed($vmid);
 
@@ -1493,11 +1492,8 @@ __PACKAGE__->register_method({
            my $upid = shift;
 
            syslog('info', "destroy VM $vmid: $upid\n");
-
            PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock);
-
            PVE::AccessControl::remove_vm_access($vmid);
-
             PVE::Firewall::remove_vmfw_conf($vmid);
        };
 
@@ -1915,6 +1911,10 @@ __PACKAGE__->register_method({
            { subdir => 'current' },
            { subdir => 'start' },
            { subdir => 'stop' },
+           { subdir => 'reset' },
+           { subdir => 'shutdown' },
+           { subdir => 'suspend' },
+           { subdir => 'reboot' },
            ];
 
        return $res;
@@ -2047,7 +2047,7 @@ __PACKAGE__->register_method({
 
        # read spice ticket from STDIN
        my $spice_ticket;
-       if ($stateuri && ($stateuri eq 'tcp') && $migratedfrom && ($rpcenv->{type} eq 'cli')) {
+       if ($stateuri && ($stateuri eq 'tcp' || $stateuri eq 'unix') && $migratedfrom && ($rpcenv->{type} eq 'cli')) {
            if (defined(my $line = <STDIN>)) {
                chomp $line;
                $spice_ticket = $line;
@@ -2338,6 +2338,64 @@ __PACKAGE__->register_method({
        }
     }});
 
+__PACKAGE__->register_method({
+    name => 'vm_reboot',
+    path => '{vmid}/status/reboot',
+    method => 'POST',
+    protected => 1,
+    proxyto => 'node',
+    description => "Reboot the VM by shutting it down, and starting it again. Applies pending changes.",
+    permissions => {
+       check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
+    },
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           vmid => get_standard_option('pve-vmid',
+                                       { completion => \&PVE::QemuServer::complete_vmid_running }),
+           timeout => {
+               description => "Wait maximal timeout seconds for the shutdown.",
+               type => 'integer',
+               minimum => 0,
+               optional => 1,
+           },
+       },
+    },
+    returns => {
+       type => 'string',
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+       my $authuser = $rpcenv->get_user();
+
+       my $node = extract_param($param, 'node');
+       my $vmid = extract_param($param, 'vmid');
+
+       my $qmpstatus = eval {
+           PVE::QemuServer::vm_qmp_command($vmid, { execute => "query-status" }, 0);
+       };
+       my $err = $@ if $@;
+
+       if (!$err && $qmpstatus->{status} eq "paused") {
+           die "VM is paused - cannot shutdown\n";
+       }
+
+       die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
+
+       my $realcmd = sub {
+           my $upid = shift;
+
+           syslog('info', "requesting reboot of VM $vmid: $upid\n");
+           PVE::QemuServer::vm_reboot($vmid, $param->{timeout});
+           return;
+       };
+
+       return $rpcenv->fork_worker('qmreboot', $vmid, $authuser, $realcmd);
+    }});
+
 __PACKAGE__->register_method({
     name => 'vm_suspend',
     path => '{vmid}/status/suspend',
@@ -3103,6 +3161,138 @@ __PACKAGE__->register_method({
        return PVE::QemuConfig->lock_config($vmid, $updatefn);
     }});
 
+my $check_vm_disks_local = sub {
+    my ($storecfg, $vmconf, $vmid) = @_;
+
+    my $local_disks = {};
+
+    # add some more information to the disks e.g. cdrom
+    PVE::QemuServer::foreach_volid($vmconf, sub {
+       my ($volid, $attr) = @_;
+
+       my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
+       if ($storeid) {
+           my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
+           return if $scfg->{shared};
+       }
+       # The shared attr here is just a special case where the vdisk
+       # is marked as shared manually
+       return if $attr->{shared};
+       return if $attr->{cdrom} and $volid eq "none";
+
+       if (exists $local_disks->{$volid}) {
+           @{$local_disks->{$volid}}{keys %$attr} = values %$attr
+       } else {
+           $local_disks->{$volid} = $attr;
+           # ensure volid is present in case it's needed
+           $local_disks->{$volid}->{volid} = $volid;
+       }
+    });
+
+    return $local_disks;
+};
+
+__PACKAGE__->register_method({
+    name => 'migrate_vm_precondition',
+    path => '{vmid}/migrate',
+    method => 'GET',
+    protected => 1,
+    proxyto => 'node',
+    description => "Get preconditions for migration.",
+    permissions => {
+       check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
+    },
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
+           target => get_standard_option('pve-node', {
+               description => "Target node.",
+               completion =>  \&PVE::Cluster::complete_migration_target,
+               optional => 1,
+           }),
+       },
+    },
+    returns => {
+       type => "object",
+       properties => {
+           running => { type => 'boolean' },
+           allowed_nodes => {
+               type => 'array',
+               optional => 1,
+               description => "List nodes allowed for offline migration, only passed if VM is offline"
+           },
+           not_allowed_nodes => {
+               type => 'object',
+               optional => 1,
+               description => "List not allowed nodes with additional informations, only passed if VM is offline"
+           },
+           local_disks => {
+               type => 'array',
+               description => "List local disks including CD-Rom, unsused and not referenced disks"
+           },
+           local_resources => {
+               type => 'array',
+               description => "List local resources e.g. pci, usb"
+           }
+       },
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+
+       my $authuser = $rpcenv->get_user();
+
+       PVE::Cluster::check_cfs_quorum();
+
+       my $res = {};
+
+       my $vmid = extract_param($param, 'vmid');
+       my $target = extract_param($param, 'target');
+       my $localnode = PVE::INotify::nodename();
+
+
+       # test if VM exists
+       my $vmconf = PVE::QemuConfig->load_config($vmid);
+       my $storecfg = PVE::Storage::config();
+
+
+       # try to detect errors early
+       PVE::QemuConfig->check_lock($vmconf);
+
+       $res->{running} = PVE::QemuServer::check_running($vmid) ? 1:0;
+
+       # if vm is not running, return target nodes where local storage is available
+       # for offline migration
+       if (!$res->{running}) {
+           $res->{allowed_nodes} = [];
+           my $checked_nodes = PVE::QemuServer::check_local_storage_availability($vmconf, $storecfg);
+           delete $checked_nodes->{$localnode};
+
+           foreach my $node (keys %$checked_nodes) {
+               if (!defined $checked_nodes->{$node}->{unavailable_storages}) {
+                   push @{$res->{allowed_nodes}}, $node;
+               }
+
+           }
+           $res->{not_allowed_nodes} = $checked_nodes;
+       }
+
+
+       my $local_disks = &$check_vm_disks_local($storecfg, $vmconf, $vmid);
+       $res->{local_disks} = [ values %$local_disks ];;
+
+       my $local_resources =  PVE::QemuServer::check_local_resources($vmconf, 1);
+
+       $res->{local_resources} = $local_resources;
+
+       return $res;
+
+
+    }});
+
 __PACKAGE__->register_method({
     name => 'migrate_vm',
     path => '{vmid}/migrate',
@@ -3124,7 +3314,7 @@ __PACKAGE__->register_method({
             }),
            online => {
                type => 'boolean',
-               description => "Use online/live migration.",
+               description => "Use online/live migration if VM is running. Ignored if VM is stopped.",
                optional => 1,
            },
            force => {
@@ -3185,9 +3375,6 @@ __PACKAGE__->register_method({
 
        my $vmid = extract_param($param, 'vmid');
 
-       raise_param_exc({ targetstorage => "Live storage migration can only be done online." })
-           if !$param->{online} && $param->{targetstorage};
-
        raise_param_exc({ force => "Only root may use this option." })
            if $param->{force} && $authuser ne 'root@pam';
 
@@ -3206,10 +3393,15 @@ __PACKAGE__->register_method({
        PVE::QemuConfig->check_lock($conf);
 
        if (PVE::QemuServer::check_running($vmid)) {
-           die "cant migrate running VM without --online\n"
-               if !$param->{online};
+           die "can't migrate running VM without --online\n" if !$param->{online};
+       } else {
+           warn "VM isn't running. Doing offline migration instead\n." if $param->{online};
+           $param->{online} = 0;
        }
 
+       raise_param_exc({ targetstorage => "Live storage migration can only be done online." })
+           if !$param->{online} && $param->{targetstorage};
+
        my $storecfg = PVE::Storage::config();
 
        if( $param->{targetstorage}) {
@@ -3387,6 +3579,8 @@ __PACKAGE__->register_method({
            PVE::Storage::activate_volumes($storecfg, [$volid]);
            my $size = PVE::Storage::volume_size_info($storecfg, $volid, 5);
 
+           die "Could not determine current size of volume '$volid'\n" if !defined($size);
+
            die "internal error" if $sizestr !~ m/^(\+)?(\d+(\.\d+)?)([KMGT])?$/;
            my ($ext, $newsize, $unit) = ($1, $2, $4);
            if ($unit) {
@@ -3859,4 +4053,36 @@ __PACKAGE__->register_method({
        return undef;
     }});
 
+__PACKAGE__->register_method({
+    name => 'cloudinit_generated_config_dump',
+    path => '{vmid}/cloudinit/dump',
+    method => 'GET',
+    proxyto => 'node',
+    description => "Get automatically generated cloudinit config.",
+    permissions => {
+       check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
+    },
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
+           type => {
+               description => 'Config type.',
+               type => 'string',
+               enum => ['user', 'network', 'meta'],
+           },
+       },
+    },
+    returns => {
+       type => 'string',
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $conf = PVE::QemuConfig->load_config($param->{vmid});
+
+       return PVE::QemuServer::Cloudinit::dump_cloudinit_config($conf, $param->{vmid}, $param->{type});
+    }});
+
 1;