]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/API2/Qemu.pm
removed trailing whitespace
[qemu-server.git] / PVE / API2 / Qemu.pm
index b8bd5dac239487fe353ab423620e551c4c4fd40f..fcb29cd8e25f235a1fb21dcdcb4d3faad75b1089 100644 (file)
@@ -2,6 +2,7 @@ package PVE::API2::Qemu;
 
 use strict;
 use warnings;
+use Cwd 'abs_path';
 
 use PVE::Cluster;
 use PVE::SafeSyslog;
@@ -33,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',
@@ -63,10 +64,10 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'create_vm', 
-    path => '', 
+    name => 'create_vm',
+    path => '',
     method => 'POST',
-    description => "Create new virtual machine.",
+    description => "Create or restore a virtual machine.",
     protected => 1,
     proxyto => 'node',
     parameters => {
@@ -75,9 +76,31 @@ __PACKAGE__->register_method({
            {
                node => get_standard_option('pve-node'),
                vmid => get_standard_option('pve-vmid'),
+               archive => {
+                   description => "The backup file.",
+                   type => 'string',
+                   optional => 1,
+                   maxLength => 255,
+               },
+               storage => get_standard_option('pve-storage-id', {
+                   description => "Default storage.",
+                   optional => 1,
+               }),
+               force => {
+                   optional => 1,
+                   type => 'boolean',
+                   description => "Allow to overwrite existing VM.",
+                   requires => 'archive',
+               },
+               unique => {
+                   optional => 1,
+                   type => 'boolean',
+                   description => "Assign a unique random ethernet address.",
+                   requires => 'archive',
+               },
            }),
     },
-    returns => { 
+    returns => {
        type => 'string',
     },
     code => sub {
@@ -89,35 +112,84 @@ __PACKAGE__->register_method({
 
        my $node = extract_param($param, 'node');
 
-       # fixme: fork worker?
-
        my $vmid = extract_param($param, 'vmid');
 
+       my $archive = extract_param($param, 'archive');
+
+       my $storage = extract_param($param, 'storage');
+
+       my $force = extract_param($param, 'force');
+
+       my $unique = extract_param($param, 'unique');
+
        my $filename = PVE::QemuServer::config_file($vmid);
-       # first test (befor locking)
-       die "unable to create vm $vmid: config file already exists\n" 
-           if -f $filename;
-       
-       my $storecfg = PVE::Storage::config(); 
 
-       &$resolve_cdrom_alias($param);
+       my $storecfg = PVE::Storage::config();
 
-       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::Cluster::check_cfs_quorum();
 
-               PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive);
-               $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive);
+       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);
+               }
+           }
+
+           PVE::QemuServer::add_random_macs($param);
+       } else {
+           my $keystr = join(' ', keys %$param);
+           raise_param_exc({ archive => "option conflicts with other options ($keystr)"}) if $keystr;
+
+           if ($archive eq '-') {
+               die "pipe requires cli environment\n"
+                   && $rpcenv->{type} ne 'cli';
+           } else {
+               my $path;
+               if (PVE::Storage::parse_volume_id($archive, 1)) {
+                   $path = PVE::Storage::path($storecfg, $archive);
+               } else {
+                   raise_param_exc({ archive => "Only root can pass arbitrary paths." })
+                       if $user ne 'root@pam';
+
+                   $path = abs_path($archive);
+               }
+               die "can't find archive file '$archive'\n" if !($path && -f $path);
+               $archive = $path;
            }
        }
 
-       PVE::QemuServer::add_random_macs($param);
+       my $restorefn = sub {
+
+           if (-f $filename) {
+               die "unable to restore vm $vmid: config file already exists\n"
+                   if !$force;
+
+               die "unable to restore vm $vmid: vm is running\n"
+                   if PVE::QemuServer::check_running($vmid);
+
+               # destroy existing data - keep empty config
+               PVE::QemuServer::destroy_vm($storecfg, $vmid, 1);
+           }
+
+           my $realcmd = sub {
+               PVE::QemuServer::restore_archive($archive, $vmid, {
+                   storage => $storage,
+                   unique => $unique });
+           };
+
+           return $rpcenv->fork_worker('qmrestore', $vmid, $user, $realcmd);
+       };
 
        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 {
@@ -125,7 +197,7 @@ __PACKAGE__->register_method({
                my $vollist = [];
 
                eval {
-                   $vollist = PVE::QemuServer::create_disks($storecfg, $vmid, $param);
+                   $vollist = PVE::QemuServer::create_disks($storecfg, $vmid, $param, $storage);
 
                    # try to be smart about bootdisk
                    my @disks = PVE::QemuServer::disknames();
@@ -138,7 +210,7 @@ __PACKAGE__->register_method({
                    }
 
                    if (!$param->{bootdisk} && $firstdisk) {
-                       $param->{bootdisk} = $firstdisk; 
+                       $param->{bootdisk} = $firstdisk;
                    }
 
                    PVE::QemuServer::create_conf_nolock($vmid, $param);
@@ -157,12 +229,12 @@ __PACKAGE__->register_method({
            return $rpcenv->fork_worker('qmcreate', $vmid, $user, $realcmd);
        };
 
-       return PVE::QemuServer::lock_config($vmid, $createfn);
+       return PVE::QemuServer::lock_config($vmid, $archive ? $restorefn : $createfn);
     }});
 
 __PACKAGE__->register_method({
     name => 'vmdiridx',
-    path => '{vmid}', 
+    path => '{vmid}',
     method => 'GET',
     proxyto => 'node',
     description => "Directory index",
@@ -194,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 => {
@@ -241,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 => {
@@ -290,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.",
@@ -302,7 +373,7 @@ __PACKAGE__->register_method({
            vmid => get_standard_option('pve-vmid'),
        },
     },
-    returns => { 
+    returns => {
        type => "object",
        properties => {
            digest => {
@@ -320,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',
@@ -348,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,
                }
            }),
     },
@@ -372,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');
@@ -380,131 +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)) {
-                   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);
+           $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;
@@ -512,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',
@@ -526,7 +593,7 @@ __PACKAGE__->register_method({
            skiplock => get_standard_option('skiplock'),
        },
     },
-    returns => { 
+    returns => {
        type => 'string',
     },
     code => sub {
@@ -539,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);
        };
 
@@ -555,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',
@@ -591,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 => {
@@ -607,7 +677,7 @@ __PACKAGE__->register_method({
            vmid => get_standard_option('pve-vmid'),
        },
     },
-    returns => { 
+    returns => {
        additionalProperties => 0,
        properties => {
            user => { type => 'string' },
@@ -623,19 +693,22 @@ __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 PVE::INotify::nodename()) {
+
+       if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
            $remip = PVE::Cluster::remote_node_ip($node);
        }
 
@@ -644,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;
@@ -668,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",
@@ -708,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',
@@ -733,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}};
 
-       return $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 $status;
     }});
 
 __PACKAGE__->register_method({
-    name => 'vm_start', 
+    name => 'vm_start',
     path => '{vmid}/status/start',
     method => 'POST',
     protected => 1,
@@ -754,7 +835,7 @@ __PACKAGE__->register_method({
            stateuri => get_standard_option('pve-qm-stateuri'),
        },
     },
-    returns => { 
+    returns => {
        type => 'string',
     },
     code => sub {
@@ -769,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;
@@ -792,7 +873,7 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'vm_stop', 
+    name => 'vm_stop',
     path => '{vmid}/status/stop',
     method => 'POST',
     protected => 1,
@@ -809,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 {
@@ -827,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;
        };
@@ -859,7 +938,7 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'vm_reset', 
+    name => 'vm_reset',
     path => '{vmid}/status/reset',
     method => 'POST',
     protected => 1,
@@ -873,7 +952,7 @@ __PACKAGE__->register_method({
            skiplock => get_standard_option('skiplock'),
        },
     },
-    returns => { 
+    returns => {
        type => 'string',
     },
     code => sub {
@@ -888,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;
@@ -905,7 +984,7 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'vm_shutdown', 
+    name => 'vm_shutdown',
     path => '{vmid}/status/shutdown',
     method => 'POST',
     protected => 1,
@@ -922,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 {
@@ -940,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;
        };
@@ -972,7 +1055,7 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'vm_suspend', 
+    name => 'vm_suspend',
     path => '{vmid}/status/suspend',
     method => 'POST',
     protected => 1,
@@ -986,7 +1069,7 @@ __PACKAGE__->register_method({
            skiplock => get_standard_option('skiplock'),
        },
     },
-    returns => { 
+    returns => {
        type => 'string',
     },
     code => sub {
@@ -1001,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;
 
@@ -1018,7 +1103,7 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'vm_resume', 
+    name => 'vm_resume',
     path => '{vmid}/status/resume',
     method => 'POST',
     protected => 1,
@@ -1032,7 +1117,7 @@ __PACKAGE__->register_method({
            skiplock => get_standard_option('skiplock'),
        },
     },
-    returns => { 
+    returns => {
        type => 'string',
     },
     code => sub {
@@ -1047,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;
 
@@ -1064,7 +1151,7 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'vm_sendkey', 
+    name => 'vm_sendkey',
     path => '{vmid}/sendkey',
     method => 'PUT',
     protected => 1,
@@ -1095,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});
@@ -1104,7 +1191,7 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'migrate_vm', 
+    name => 'migrate_vm',
     path => '{vmid}/migrate',
     method => 'POST',
     protected => 1,
@@ -1128,7 +1215,7 @@ __PACKAGE__->register_method({
            },
        },
     },
-    returns => { 
+    returns => {
        type => 'string',
        description => "the task ID.",
     },
@@ -1152,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);
@@ -1174,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;