]> 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 8be0b7b10461902d1a3059c4cd8e3a71ef48021d..1e8d3500c72c397523f0688713e17d78684b91f4 100644 (file)
@@ -1180,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)) {
@@ -1197,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";
@@ -1210,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";
@@ -1437,7 +1441,6 @@ __PACKAGE__->register_method({
     }
 });
 
-
 __PACKAGE__->register_method({
     name => 'destroy_vm',
     path => '{vmid}',
@@ -1463,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};
@@ -1474,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);
 
@@ -1494,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);
        };
 
@@ -1916,6 +1911,10 @@ __PACKAGE__->register_method({
            { subdir => 'current' },
            { subdir => 'start' },
            { subdir => 'stop' },
+           { subdir => 'reset' },
+           { subdir => 'shutdown' },
+           { subdir => 'suspend' },
+           { subdir => 'reboot' },
            ];
 
        return $res;
@@ -2048,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;
@@ -2339,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',
@@ -3257,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 => {
@@ -3318,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';
 
@@ -3339,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}) {
@@ -3520,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) {