]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/API2/Qemu.pm
api: deletion: check also pending values for serial/usb
[qemu-server.git] / PVE / API2 / Qemu.pm
index 1c4e07ba9a3532a1e8ac2ecae8d5cd033579950d..5814f941dc4967710c762529f5268e14ab99e1c0 100644 (file)
@@ -260,6 +260,7 @@ my $hwtypeoptions = {
     'tablet' => 1,
     'vga' => 1,
     'watchdog' => 1,
+    'audio0' => 1,
 };
 
 my $generaloptions = {
@@ -1201,7 +1202,7 @@ my $update_vm_api  = sub {
                    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 ($conf->{$opt} eq 'socket' || (!$conf->{$opt} && $conf->{pending}->{$opt} 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 +1210,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 ($conf->{$opt} =~ m/spice/ || (!$conf->{$opt} && $conf->{pending}->{$opt} =~ 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 +1437,6 @@ __PACKAGE__->register_method({
     }
 });
 
-
 __PACKAGE__->register_method({
     name => 'destroy_vm',
     path => '{vmid}',
@@ -1462,9 +1462,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 +1471,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 +1488,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 +1907,10 @@ __PACKAGE__->register_method({
            { subdir => 'current' },
            { subdir => 'start' },
            { subdir => 'stop' },
+           { subdir => 'reset' },
+           { subdir => 'shutdown' },
+           { subdir => 'suspend' },
+           { subdir => 'reboot' },
            ];
 
        return $res;
@@ -2047,7 +2043,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 +2334,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',
@@ -3211,12 +3265,11 @@ __PACKAGE__->register_method({
        if (!$res->{running}) {
            $res->{allowed_nodes} = [];
            my $checked_nodes = PVE::QemuServer::check_local_storage_availability($vmconf, $storecfg);
+           delete $checked_nodes->{$localnode};
 
-           delete $checked_nodes->{$localnode} if $checked_nodes->{$localnode};
            foreach my $node (keys %$checked_nodes) {
-               if (!defined $checked_nodes->{$node}->{not_available_storages}){
+               if (!defined $checked_nodes->{$node}->{unavailable_storages}) {
                    push @{$res->{allowed_nodes}}, $node;
-                   delete $checked_nodes->{$node};
                }
 
            }