]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/API2/Qemu.pm
removed trailing whitespace
[qemu-server.git] / PVE / API2 / Qemu.pm
index c9d6da7d77ed5e2918bf6a7b7a3e14e3f71aa5aa..fcb29cd8e25f235a1fb21dcdcb4d3faad75b1089 100644 (file)
@@ -34,8 +34,8 @@ my $resolve_cdrom_alias = sub {
 };
 
 __PACKAGE__->register_method({
-    name => 'vmlist', 
-    path => '', 
+    name => 'vmlist',
+    path => '',
     method => 'GET',
     description => "Virtual machine index (per node).",
     proxyto => 'node',
@@ -64,8 +64,8 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'create_vm', 
-    path => '', 
+    name => 'create_vm',
+    path => '',
     method => 'POST',
     description => "Create or restore a virtual machine.",
     protected => 1,
@@ -87,20 +87,20 @@ __PACKAGE__->register_method({
                    optional => 1,
                }),
                force => {
-                   optional => 1, 
+                   optional => 1,
                    type => 'boolean',
                    description => "Allow to overwrite existing VM.",
                    requires => 'archive',
                },
                unique => {
-                   optional => 1, 
+                   optional => 1,
                    type => 'boolean',
                    description => "Assign a unique random ethernet address.",
                    requires => 'archive',
                },
            }),
     },
-    returns => { 
+    returns => {
        type => 'string',
     },
     code => sub {
@@ -123,19 +123,19 @@ __PACKAGE__->register_method({
        my $unique = extract_param($param, 'unique');
 
        my $filename = PVE::QemuServer::config_file($vmid);
-       
-       my $storecfg = PVE::Storage::config(); 
+
+       my $storecfg = PVE::Storage::config();
 
        PVE::Cluster::check_cfs_quorum();
 
-       if (!$archive) { 
+       if (!$archive) {
            &$resolve_cdrom_alias($param);
 
            foreach my $opt (keys %$param) {
                if (PVE::QemuServer::valid_drivename($opt)) {
                    my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
                    raise_param_exc({ $opt => "unable to parse drive options" }) if !$drive;
-                   
+
                    PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive);
                    $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive);
                }
@@ -147,28 +147,30 @@ __PACKAGE__->register_method({
            raise_param_exc({ archive => "option conflicts with other options ($keystr)"}) if $keystr;
 
            if ($archive eq '-') {
-               die "pipe requires cli environment\n" 
+               die "pipe requires cli environment\n"
                    && $rpcenv->{type} ne 'cli';
            } else {
+               my $path;
                if (PVE::Storage::parse_volume_id($archive, 1)) {
-                   $archive = PVE::Storage::path($storecfg, $archive);
+                   $path = PVE::Storage::path($storecfg, $archive);
                } else {
-                   raise_param_exc({ archive => "Only root can pass arbitrary paths." }) 
+                   raise_param_exc({ archive => "Only root can pass arbitrary paths." })
                        if $user ne 'root@pam';
 
-                   $archive = abs_path($archive);
+                   $path = abs_path($archive);
                }
-               die "can't find file '$archive'\n" if ! -f $archive;
-           } 
+               die "can't find archive file '$archive'\n" if !($path && -f $path);
+               $archive = $path;
+           }
        }
 
        my $restorefn = sub {
 
            if (-f $filename) {
-               die "unable to restore vm $vmid: config file already exists\n" 
+               die "unable to restore vm $vmid: config file already exists\n"
                    if !$force;
 
-               die "unable to restore vm $vmid: vm is running\n" 
+               die "unable to restore vm $vmid: vm is running\n"
                    if PVE::QemuServer::check_running($vmid);
 
                # destroy existing data - keep empty config
@@ -176,7 +178,7 @@ __PACKAGE__->register_method({
            }
 
            my $realcmd = sub {
-               PVE::QemuServer::restore_archive($archive, $vmid, { 
+               PVE::QemuServer::restore_archive($archive, $vmid, {
                    storage => $storage,
                    unique => $unique });
            };
@@ -187,7 +189,7 @@ __PACKAGE__->register_method({
        my $createfn = sub {
 
            # second test (after locking test is accurate)
-           die "unable to create vm $vmid: config file already exists\n" 
+           die "unable to create vm $vmid: config file already exists\n"
                if -f $filename;
 
            my $realcmd = sub {
@@ -208,7 +210,7 @@ __PACKAGE__->register_method({
                    }
 
                    if (!$param->{bootdisk} && $firstdisk) {
-                       $param->{bootdisk} = $firstdisk; 
+                       $param->{bootdisk} = $firstdisk;
                    }
 
                    PVE::QemuServer::create_conf_nolock($vmid, $param);
@@ -232,7 +234,7 @@ __PACKAGE__->register_method({
 
 __PACKAGE__->register_method({
     name => 'vmdiridx',
-    path => '{vmid}', 
+    path => '{vmid}',
     method => 'GET',
     proxyto => 'node',
     description => "Directory index",
@@ -264,19 +266,19 @@ __PACKAGE__->register_method({
            { subdir => 'migrate' },
            { subdir => 'rrd' },
            { subdir => 'rrddata' },
+           { subdir => 'monitor' },
            ];
-       
+
        return $res;
     }});
 
 __PACKAGE__->register_method({
-    name => 'rrd', 
-    path => '{vmid}/rrd', 
+    name => 'rrd',
+    path => '{vmid}/rrd',
     method => 'GET',
     protected => 1, # fixme: can we avoid that?
     permissions => {
-       path => '/vms/{vmid}',
-       privs => [ 'VM.Audit' ],
+       check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
     },
     description => "Read VM RRD statistics (returns PNG)",
     parameters => {
@@ -311,19 +313,18 @@ __PACKAGE__->register_method({
        my ($param) = @_;
 
        return PVE::Cluster::create_rrd_graph(
-           "pve2-vm/$param->{vmid}", $param->{timeframe}, 
+           "pve2-vm/$param->{vmid}", $param->{timeframe},
            $param->{ds}, $param->{cf});
-                                             
+
     }});
 
 __PACKAGE__->register_method({
-    name => 'rrddata', 
-    path => '{vmid}/rrddata', 
+    name => 'rrddata',
+    path => '{vmid}/rrddata',
     method => 'GET',
     protected => 1, # fixme: can we avoid that?
     permissions => {
-       path => '/vms/{vmid}',
-       privs => [ 'VM.Audit' ],
+       check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
     },
     description => "Read VM RRD statistics",
     parameters => {
@@ -360,8 +361,8 @@ __PACKAGE__->register_method({
 
 
 __PACKAGE__->register_method({
-    name => 'vm_config', 
-    path => '{vmid}/config', 
+    name => 'vm_config',
+    path => '{vmid}/config',
     method => 'GET',
     proxyto => 'node',
     description => "Get virtual machine configuration.",
@@ -372,7 +373,7 @@ __PACKAGE__->register_method({
            vmid => get_standard_option('pve-vmid'),
        },
     },
-    returns => { 
+    returns => {
        type => "object",
        properties => {
            digest => {
@@ -390,8 +391,8 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'update_vm', 
-    path => '{vmid}/config', 
+    name => 'update_vm',
+    path => '{vmid}/config',
     method => 'PUT',
     protected => 1,
     proxyto => 'node',
@@ -418,7 +419,7 @@ __PACKAGE__->register_method({
                    type => 'string',
                    description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
                    maxLength => 40,
-                   optional => 1,                  
+                   optional => 1,
                }
            }),
     },
@@ -442,7 +443,7 @@ __PACKAGE__->register_method({
        }
 
        my $skiplock = extract_param($param, 'skiplock');
-       raise_param_exc({ skiplock => "Only root may use this option." }) 
+       raise_param_exc({ skiplock => "Only root may use this option." })
            if $skiplock && $user ne 'root@pam';
 
        my $delete = extract_param($param, 'delete');
@@ -450,132 +451,127 @@ __PACKAGE__->register_method({
 
        die "no options specified\n" if !$delete && !scalar(keys %$param);
 
-       my $storecfg = PVE::Storage::config(); 
+       my $storecfg = PVE::Storage::config();
 
        &$resolve_cdrom_alias($param);
 
-       my $eject = {};
-       my $cdchange = {};
+       my $conf = PVE::QemuServer::load_config($vmid);
 
-       foreach my $opt (keys %$param) {
-           if (PVE::QemuServer::valid_drivename($opt)) {
-               my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
-               raise_param_exc({ $opt => "unable to parse drive options" }) if !$drive;
-               if ($drive->{file} eq 'eject') {
-                   $eject->{$opt} = 1;
-                   delete $param->{$opt};
-                   next;
-               }
+       die "checksum missmatch (file change by other user?)\n"
+          if $digest && $digest ne $conf->{digest};
 
-               PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive);
-               $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive);
+       PVE::QemuServer::check_lock($conf) if !$skiplock;
 
-               if (PVE::QemuServer::drive_is_cdrom($drive)) {
-                   $cdchange->{$opt} = PVE::QemuServer::get_iso_path($storecfg, $vmid, $drive->{file});
-               }
-           }
-       }
+       PVE::Cluster::log_msg('info', $user, "update VM $vmid: " . join (' ', @paramarr));
 
+       #delete
        foreach my $opt (PVE::Tools::split_list($delete)) {
+
            $opt = 'ide2' if $opt eq 'cdrom';
            die "you can't use '-$opt' and '-delete $opt' at the same time\n"
                if defined($param->{$opt});
-       }
-
-       PVE::QemuServer::add_random_macs($param);
-
-       my $vollist = [];
 
-       my $updatefn =  sub {
-
-           my $conf = PVE::QemuServer::load_config($vmid);
-
-           die "checksum missmatch (file change by other user?)\n" 
-               if $digest && $digest ne $conf->{digest};
-
-           PVE::QemuServer::check_lock($conf) if !$skiplock;
-
-           PVE::Cluster::log_msg('info', $user, "update VM $vmid: " . join (' ', @paramarr));
+           my $unset = {};
 
-           foreach my $opt (keys %$eject) {
-               if ($conf->{$opt}) {
-                   my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
-                   $cdchange->{$opt} = undef if PVE::QemuServer::drive_is_cdrom($drive);
-               } else {
-                   raise_param_exc({ $opt => "eject failed - drive does not exist." });
-               }
+           if (!PVE::QemuServer::option_exists($opt)) {
+               raise_param_exc({ delete => "unknown option '$opt'" });
            }
 
-           foreach my $opt (keys %$param) {
-               next if !PVE::QemuServer::valid_drivename($opt);
-               next if !$conf->{$opt};
-               my $old_drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
-               next if PVE::QemuServer::drive_is_cdrom($old_drive);
-               my $new_drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
-               if ($new_drive->{file} ne $old_drive->{file}) {
-                   my ($path, $owner);
-                   eval { ($path, $owner) = PVE::Storage::path($storecfg, $old_drive->{file}); };
-                   if ($owner && ($owner == $vmid)) {
-                       PVE::QemuServer::add_unused_volume($conf, $param, $old_drive->{file});
-                   }
-               }
-           }
+           next if !defined($conf->{$opt});
 
-           my $unset = {};
+           die "error hot-unplug $opt" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
 
-           foreach my $opt (PVE::Tools::split_list($delete)) {
-               $opt = 'ide2' if $opt eq 'cdrom';
-               if (!PVE::QemuServer::option_exists($opt)) {
-                   raise_param_exc({ delete => "unknown option '$opt'" });
-               } 
-               next if !defined($conf->{$opt});
-               if (PVE::QemuServer::valid_drivename($opt)) {
-                   PVE::QemuServer::vm_devicedel($vmid, $conf, $opt);
-                   my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
-                   if (PVE::QemuServer::drive_is_cdrom($drive)) {
-                       $cdchange->{$opt} = undef;
-                   } else {
-                       my $volid = $drive->{file};
-
-                       if ($volid !~  m|^/|) {
-                           my ($path, $owner);
-                           eval { ($path, $owner) = PVE::Storage::path($storecfg, $volid); };
-                           if ($owner && ($owner == $vmid)) {
-                               if ($force) {
-                                   push @$vollist, $volid;
-                               } else {
-                                   PVE::QemuServer::add_unused_volume($conf, $param, $volid);
-                               }
+           #drive
+           if (PVE::QemuServer::valid_drivename($opt)) {
+               my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
+                #hdd
+               if (!PVE::QemuServer::drive_is_cdrom($drive)) {
+                   my $volid = $drive->{file};
+
+                   if ($volid !~  m|^/|) {
+                       my ($path, $owner);
+                       eval { ($path, $owner) = PVE::Storage::path($storecfg, $volid); };
+                       if ($owner && ($owner == $vmid)) {
+                           if ($force) {
+                               eval { PVE::Storage::vdisk_free($storecfg, $volid); };
+                               # fixme: log ?
+                               warn $@ if $@;
+                           } else {
+                               PVE::QemuServer::add_unused_volume($conf, $volid, $vmid);
                            }
                        }
                    }
-               } elsif ($opt =~ m/^unused/) {
-                   push @$vollist, $conf->{$opt};
-               }
-
-               $unset->{$opt} = 1;
+                 }
+           } elsif ($opt =~ m/^unused/) {
+                   my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
+                    my $volid = $drive->{file};
+                   eval { PVE::Storage::vdisk_free($storecfg, $volid); };
+                   # fixme: log ?
+                   warn $@ if $@;
            }
 
-           PVE::QemuServer::create_disks($storecfg, $vmid, $param, $conf);
+           $unset->{$opt} = 1;
+           PVE::QemuServer::change_config_nolock($vmid, {}, $unset, 1);
+       }
 
-           PVE::QemuServer::change_config_nolock($vmid, $param, $unset, 1);
 
-           return if !PVE::QemuServer::check_running($vmid);
 
-           foreach my $opt (keys %$cdchange) {
-               my $qdn = PVE::QemuServer::qemu_drive_name($opt, 'cdrom');
-               my $path = $cdchange->{$opt};
-               PVE::QemuServer::vm_monitor_command($vmid, "eject $qdn", 0);
-               PVE::QemuServer::vm_monitor_command($vmid, "change $qdn \"$path\"", 0) if $path;
-           }
-       };
+       #add
+       foreach my $opt (keys %$param) {
 
-       PVE::QemuServer::lock_config($vmid, $updatefn);
+            #drives
+           if (PVE::QemuServer::valid_drivename($opt)) {
+               my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
+               raise_param_exc({ $opt => "unable to parse drive options" }) if !$drive;
 
-       foreach my $volid (@$vollist) {
-           eval { PVE::Storage::vdisk_free($storecfg, $volid); };
-           # fixme: log ?
-           warn $@ if $@;
+               PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive);
+               $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive);
+
+                #cdrom
+               if (PVE::QemuServer::drive_is_cdrom($drive) && PVE::QemuServer::check_running($vmid)) {
+                   if ($drive->{file} eq 'none') {
+                        PVE::QemuServer::vm_monitor_command($vmid, "eject -f drive-$opt", 0);
+                       #delete $param->{$opt};
+                   }
+                   else {
+                       my $path = PVE::QemuServer::get_iso_path($storecfg, $vmid, $drive->{file});
+                        PVE::QemuServer::vm_monitor_command($vmid, "eject -f drive-$opt", 0); #force eject if locked
+                       PVE::QemuServer::vm_monitor_command($vmid, "change drive-$opt \"$path\"", 0) if $path;
+                    }
+               }
+               #hdd
+                else {
+                    #swap drive
+                    if ($conf->{$opt}){
+                       my $old_drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
+                       if ($drive->{file} ne $old_drive->{file} && !PVE::QemuServer::drive_is_cdrom($old_drive)) {
+
+                            my ($path, $owner);
+                            eval { ($path, $owner) = PVE::Storage::path($storecfg, $old_drive->{file}); };
+                            if ($owner && ($owner == $vmid)) {
+                               die "error hot-unplug $opt" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
+                                PVE::QemuServer::add_unused_volume($conf, $old_drive->{file}, $vmid);
+                            }
+                        }
+                    }
+                   my $settings = { $opt => $param->{$opt} };
+                   PVE::QemuServer::create_disks($storecfg, $vmid, $settings, $conf);
+                   $param->{$opt} = $settings->{$opt};
+                    #hotplug disks
+                    if(!PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $drive)) {
+                       PVE::QemuServer::add_unused_volume($conf,$drive->{file},$vmid);
+                       PVE::QemuServer::change_config_nolock($vmid, {}, { $opt => 1 }, 1);
+                       die "error hotplug $opt - put disk in unused";
+                    }
+                }
+          }
+           #nics
+           if ($opt =~ m/^net(\d+)$/) {
+                my $net = PVE::QemuServer::parse_net($param->{$opt});
+                $param->{$opt} = PVE::QemuServer::print_net($net);
+           }
+
+           PVE::QemuServer::change_config_nolock($vmid, { $opt => $param->{$opt} }, {}, 1);
        }
 
        return undef;
@@ -583,8 +579,8 @@ __PACKAGE__->register_method({
 
 
 __PACKAGE__->register_method({
-    name => 'destroy_vm', 
-    path => '{vmid}', 
+    name => 'destroy_vm',
+    path => '{vmid}',
     method => 'DELETE',
     protected => 1,
     proxyto => 'node',
@@ -597,7 +593,7 @@ __PACKAGE__->register_method({
            skiplock => get_standard_option('skiplock'),
        },
     },
-    returns => { 
+    returns => {
        type => 'string',
     },
     code => sub {
@@ -610,15 +606,19 @@ __PACKAGE__->register_method({
        my $vmid = $param->{vmid};
 
        my $skiplock = $param->{skiplock};
-       raise_param_exc({ skiplock => "Only root may use this option." }) 
+       raise_param_exc({ skiplock => "Only root may use this option." })
            if $skiplock && $user ne 'root@pam';
 
        # test if VM exists
        my $conf = PVE::QemuServer::load_config($vmid);
 
-       my $storecfg = PVE::Storage::config(); 
+       my $storecfg = PVE::Storage::config();
 
        my $realcmd = sub {
+           my $upid = shift;
+
+           syslog('info', "destroy VM $vmid: $upid\n");
+
            PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock);
        };
 
@@ -626,8 +626,8 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'unlink', 
-    path => '{vmid}/unlink', 
+    name => 'unlink',
+    path => '{vmid}/unlink',
     method => 'PUT',
     protected => 1,
     proxyto => 'node',
@@ -662,13 +662,12 @@ __PACKAGE__->register_method({
 my $sslcert;
 
 __PACKAGE__->register_method({
-    name => 'vncproxy', 
-    path => '{vmid}/vncproxy', 
+    name => 'vncproxy',
+    path => '{vmid}/vncproxy',
     method => 'POST',
     protected => 1,
     permissions => {
-       path => '/vms/{vmid}',
-       privs => [ 'VM.Console' ],
+       check => ['perm', '/vms/{vmid}', [ 'VM.Console' ]],
     },
     description => "Creates a TCP VNC proxy connections.",
     parameters => {
@@ -678,7 +677,7 @@ __PACKAGE__->register_method({
            vmid => get_standard_option('pve-vmid'),
        },
     },
-    returns => { 
+    returns => {
        additionalProperties => 0,
        properties => {
            user => { type => 'string' },
@@ -694,18 +693,21 @@ __PACKAGE__->register_method({
        my $rpcenv = PVE::RPCEnvironment::get();
 
        my $user = $rpcenv->get_user();
-       my $ticket = PVE::AccessControl::assemble_ticket($user);
 
        my $vmid = $param->{vmid};
        my $node = $param->{node};
 
+       my $authpath = "/vms/$vmid";
+
+       my $ticket = PVE::AccessControl::assemble_vnc_ticket($user, $authpath);
+
        $sslcert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192)
            if !$sslcert;
 
        my $port = PVE::Tools::next_vnc_port();
 
        my $remip;
-       
+
        if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
            $remip = PVE::Cluster::remote_node_ip($node);
        }
@@ -715,7 +717,7 @@ __PACKAGE__->register_method({
        my $remcmd = $remip ? ['/usr/bin/ssh', '-T', '-o', 'BatchMode=yes',
                               '-c', 'blowfish-cbc', $remip] : [];
 
-       my $timeout = 10; 
+       my $timeout = 10;
 
        my $realcmd = sub {
            my $upid = shift;
@@ -739,15 +741,15 @@ __PACKAGE__->register_method({
        return {
            user => $user,
            ticket => $ticket,
-           port => $port, 
-           upid => $upid, 
-           cert => $sslcert, 
+           port => $port,
+           upid => $upid,
+           cert => $sslcert,
        };
     }});
 
 __PACKAGE__->register_method({
     name => 'vmcmdidx',
-    path => '{vmid}/status', 
+    path => '{vmid}/status',
     method => 'GET',
     proxyto => 'node',
     description => "Directory index",
@@ -779,12 +781,12 @@ __PACKAGE__->register_method({
            { subdir => 'start' },
            { subdir => 'stop' },
            ];
-       
+
        return $res;
     }});
 
 __PACKAGE__->register_method({
-    name => 'vm_status', 
+    name => 'vm_status',
     path => '{vmid}/status/current',
     method => 'GET',
     proxyto => 'node',
@@ -804,13 +806,21 @@ __PACKAGE__->register_method({
        # test if VM exists
        my $conf = PVE::QemuServer::load_config($param->{vmid});
 
-       my $vmstatus =  PVE::QemuServer::vmstatus($param->{vmid});
+       my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid});
+       my $status = $vmstatus->{$param->{vmid}};
+
+       my $cc = PVE::Cluster::cfs_read_file('cluster.conf');
+       if (PVE::Cluster::cluster_conf_lookup_pvevm($cc, 0, $param->{vmid}, 1)) {
+           $status->{ha} = 1;
+       } else {
+           $status->{ha} = 0;
+       }
 
-       return $vmstatus->{$param->{vmid}};
+       return $status;
     }});
 
 __PACKAGE__->register_method({
-    name => 'vm_start', 
+    name => 'vm_start',
     path => '{vmid}/status/start',
     method => 'POST',
     protected => 1,
@@ -825,7 +835,7 @@ __PACKAGE__->register_method({
            stateuri => get_standard_option('pve-qm-stateuri'),
        },
     },
-    returns => { 
+    returns => {
        type => 'string',
     },
     code => sub {
@@ -840,14 +850,14 @@ __PACKAGE__->register_method({
        my $vmid = extract_param($param, 'vmid');
 
        my $stateuri = extract_param($param, 'stateuri');
-       raise_param_exc({ stateuri => "Only root may use this option." }) 
+       raise_param_exc({ stateuri => "Only root may use this option." })
            if $stateuri && $user ne 'root@pam';
 
        my $skiplock = extract_param($param, 'skiplock');
-       raise_param_exc({ skiplock => "Only root may use this option." }) 
+       raise_param_exc({ skiplock => "Only root may use this option." })
            if $skiplock && $user ne 'root@pam';
 
-       my $storecfg = PVE::Storage::config(); 
+       my $storecfg = PVE::Storage::config();
 
        my $realcmd = sub {
            my $upid = shift;
@@ -863,7 +873,7 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'vm_stop', 
+    name => 'vm_stop',
     path => '{vmid}/status/stop',
     method => 'POST',
     protected => 1,
@@ -880,10 +890,16 @@ __PACKAGE__->register_method({
                type => 'integer',
                minimum => 0,
                optional => 1,
+           },
+           keepActive => {
+               description => "Do not decativate storage volumes.",
+               type => 'boolean',
+               optional => 1,
+               default => 0,
            }
        },
     },
-    returns => { 
+    returns => {
        type => 'string',
     },
     code => sub {
@@ -898,30 +914,22 @@ __PACKAGE__->register_method({
        my $vmid = extract_param($param, 'vmid');
 
        my $skiplock = extract_param($param, 'skiplock');
-       raise_param_exc({ skiplock => "Only root may use this option." }) 
+       raise_param_exc({ skiplock => "Only root may use this option." })
            if $skiplock && $user ne 'root@pam';
 
+       my $keepActive = extract_param($param, 'keepActive');
+       raise_param_exc({ keepActive => "Only root may use this option." })
+           if $keepActive && $user ne 'root@pam';
+
+       my $storecfg = PVE::Storage::config();
+
        my $realcmd = sub {
            my $upid = shift;
 
            syslog('info', "stop VM $vmid: $upid\n");
 
-           PVE::QemuServer::vm_stop($vmid, $skiplock);
-
-           my $pid = PVE::QemuServer::check_running ($vmid);
-
-           if ($pid && $param->{timeout}) {
-               print "waiting until VM $vmid stopps (PID $pid)\n";
-
-               my $count = 0;
-               while (($count < $param->{timeout}) && 
-                      PVE::QemuServer::check_running($vmid)) {
-                   $count++;
-                   sleep 1;
-               }
-
-               die "wait failed - got timeout\n" if PVE::QemuServer::check_running($vmid);
-           }
+           PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0,
+                                    $param->{timeout}, 0, 1, $keepActive);
 
            return;
        };
@@ -930,7 +938,7 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'vm_reset', 
+    name => 'vm_reset',
     path => '{vmid}/status/reset',
     method => 'POST',
     protected => 1,
@@ -944,7 +952,7 @@ __PACKAGE__->register_method({
            skiplock => get_standard_option('skiplock'),
        },
     },
-    returns => { 
+    returns => {
        type => 'string',
     },
     code => sub {
@@ -959,14 +967,14 @@ __PACKAGE__->register_method({
        my $vmid = extract_param($param, 'vmid');
 
        my $skiplock = extract_param($param, 'skiplock');
-       raise_param_exc({ skiplock => "Only root may use this option." }) 
+       raise_param_exc({ skiplock => "Only root may use this option." })
            if $skiplock && $user ne 'root@pam';
 
+       die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
+
        my $realcmd = sub {
            my $upid = shift;
 
-           syslog('info', "reset VM $vmid: $upid\n");
-
            PVE::QemuServer::vm_reset($vmid, $skiplock);
 
            return;
@@ -976,7 +984,7 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'vm_shutdown', 
+    name => 'vm_shutdown',
     path => '{vmid}/status/shutdown',
     method => 'POST',
     protected => 1,
@@ -993,10 +1001,22 @@ __PACKAGE__->register_method({
                type => 'integer',
                minimum => 0,
                optional => 1,
+           },
+           forceStop => {
+               description => "Make sure the VM stops.",
+               type => 'boolean',
+               optional => 1,
+               default => 0,
+           },
+           keepActive => {
+               description => "Do not decativate storage volumes.",
+               type => 'boolean',
+               optional => 1,
+               default => 0,
            }
        },
     },
-    returns => { 
+    returns => {
        type => 'string',
     },
     code => sub {
@@ -1011,30 +1031,22 @@ __PACKAGE__->register_method({
        my $vmid = extract_param($param, 'vmid');
 
        my $skiplock = extract_param($param, 'skiplock');
-       raise_param_exc({ skiplock => "Only root may use this option." }) 
+       raise_param_exc({ skiplock => "Only root may use this option." })
            if $skiplock && $user ne 'root@pam';
 
+       my $keepActive = extract_param($param, 'keepActive');
+       raise_param_exc({ keepActive => "Only root may use this option." })
+           if $keepActive && $user ne 'root@pam';
+
+       my $storecfg = PVE::Storage::config();
+
        my $realcmd = sub {
            my $upid = shift;
 
            syslog('info', "shutdown VM $vmid: $upid\n");
 
-           PVE::QemuServer::vm_shutdown($vmid, $skiplock);
-
-           my $pid = PVE::QemuServer::check_running ($vmid);
-
-           if ($pid && $param->{timeout}) {
-               print "waiting until VM $vmid stopps (PID $pid)\n";
-
-               my $count = 0;
-               while (($count < $param->{timeout}) && 
-                      PVE::QemuServer::check_running($vmid)) {
-                   $count++;
-                   sleep 1;
-               }
-
-               die "wait failed - got timeout\n" if PVE::QemuServer::check_running($vmid);
-           }
+           PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout},
+                                    1, $param->{forceStop}, $keepActive);
 
            return;
        };
@@ -1043,7 +1055,7 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'vm_suspend', 
+    name => 'vm_suspend',
     path => '{vmid}/status/suspend',
     method => 'POST',
     protected => 1,
@@ -1057,7 +1069,7 @@ __PACKAGE__->register_method({
            skiplock => get_standard_option('skiplock'),
        },
     },
-    returns => { 
+    returns => {
        type => 'string',
     },
     code => sub {
@@ -1072,9 +1084,11 @@ __PACKAGE__->register_method({
        my $vmid = extract_param($param, 'vmid');
 
        my $skiplock = extract_param($param, 'skiplock');
-       raise_param_exc({ skiplock => "Only root may use this option." }) 
+       raise_param_exc({ skiplock => "Only root may use this option." })
            if $skiplock && $user ne 'root@pam';
 
+       die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
+
        my $realcmd = sub {
            my $upid = shift;
 
@@ -1089,7 +1103,7 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'vm_resume', 
+    name => 'vm_resume',
     path => '{vmid}/status/resume',
     method => 'POST',
     protected => 1,
@@ -1103,7 +1117,7 @@ __PACKAGE__->register_method({
            skiplock => get_standard_option('skiplock'),
        },
     },
-    returns => { 
+    returns => {
        type => 'string',
     },
     code => sub {
@@ -1118,9 +1132,11 @@ __PACKAGE__->register_method({
        my $vmid = extract_param($param, 'vmid');
 
        my $skiplock = extract_param($param, 'skiplock');
-       raise_param_exc({ skiplock => "Only root may use this option." }) 
+       raise_param_exc({ skiplock => "Only root may use this option." })
            if $skiplock && $user ne 'root@pam';
 
+       die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
+
        my $realcmd = sub {
            my $upid = shift;
 
@@ -1135,7 +1151,7 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'vm_sendkey', 
+    name => 'vm_sendkey',
     path => '{vmid}/sendkey',
     method => 'PUT',
     protected => 1,
@@ -1166,7 +1182,7 @@ __PACKAGE__->register_method({
        my $vmid = extract_param($param, 'vmid');
 
        my $skiplock = extract_param($param, 'skiplock');
-       raise_param_exc({ skiplock => "Only root may use this option." }) 
+       raise_param_exc({ skiplock => "Only root may use this option." })
            if $skiplock && $user ne 'root@pam';
 
        PVE::QemuServer::vm_sendkey($vmid, $skiplock, $param->{key});
@@ -1175,7 +1191,7 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'migrate_vm', 
+    name => 'migrate_vm',
     path => '{vmid}/migrate',
     method => 'POST',
     protected => 1,
@@ -1199,7 +1215,7 @@ __PACKAGE__->register_method({
            },
        },
     },
-    returns => { 
+    returns => {
        type => 'string',
        description => "the task ID.",
     },
@@ -1223,21 +1239,25 @@ __PACKAGE__->register_method({
 
        my $vmid = extract_param($param, 'vmid');
 
-       raise_param_exc({ force => "Only root may use this option." }) if $user ne 'root@pam';
+       raise_param_exc({ force => "Only root may use this option." })
+           if $param->{force} && $user ne 'root@pam';
 
        # test if VM exists
-       PVE::QemuServer::load_config($vmid);
+       my $conf = PVE::QemuServer::load_config($vmid);
 
        # try to detect errors early
+
+       PVE::QemuServer::check_lock($conf);
+
        if (PVE::QemuServer::check_running($vmid)) {
-           die "cant migrate running VM without --online\n" 
+           die "cant migrate running VM without --online\n"
                if !$param->{online};
        }
 
        my $realcmd = sub {
            my $upid = shift;
 
-           PVE::QemuMigrate::migrate($target, $targetip, $vmid, $param->{online}, $param->{force});
+           PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param);
        };
 
        my $upid = $rpcenv->fork_worker('qmigrate', $vmid, $user, $realcmd);
@@ -1245,4 +1265,39 @@ __PACKAGE__->register_method({
        return $upid;
     }});
 
+__PACKAGE__->register_method({
+    name => 'monitor',
+    path => '{vmid}/monitor',
+    method => 'POST',
+    protected => 1,
+    proxyto => 'node',
+    description => "Execute Qemu monitor commands.",
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           vmid => get_standard_option('pve-vmid'),
+           command => {
+               type => 'string',
+               description => "The monitor command.",
+           }
+       },
+    },
+    returns => { type => 'string'},
+    code => sub {
+       my ($param) = @_;
+
+       my $vmid = $param->{vmid};
+
+       my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists
+
+       my $res = '';
+       eval {
+           $res = PVE::QemuServer::vm_monitor_command($vmid, $param->{command});
+       };
+       $res = "ERROR: $@" if $@;
+
+       return $res;
+    }});
+
 1;