]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/API2/Qemu.pm
removed trailing whitespace
[qemu-server.git] / PVE / API2 / Qemu.pm
index 464a1a466538d5b83074b52de7e0b19876969482..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,14 +147,14 @@ __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)) {
                    $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';
 
                    $path = abs_path($archive);
@@ -167,10 +167,10 @@ __PACKAGE__->register_method({
        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
@@ -178,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 });
            };
@@ -189,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 {
@@ -210,7 +210,7 @@ __PACKAGE__->register_method({
                    }
 
                    if (!$param->{bootdisk} && $firstdisk) {
-                       $param->{bootdisk} = $firstdisk; 
+                       $param->{bootdisk} = $firstdisk;
                    }
 
                    PVE::QemuServer::create_conf_nolock($vmid, $param);
@@ -234,7 +234,7 @@ __PACKAGE__->register_method({
 
 __PACKAGE__->register_method({
     name => 'vmdiridx',
-    path => '{vmid}', 
+    path => '{vmid}',
     method => 'GET',
     proxyto => 'node',
     description => "Directory index",
@@ -268,18 +268,17 @@ __PACKAGE__->register_method({
            { 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 => {
@@ -314,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 => {
@@ -363,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.",
@@ -375,7 +373,7 @@ __PACKAGE__->register_method({
            vmid => get_standard_option('pve-vmid'),
        },
     },
-    returns => { 
+    returns => {
        type => "object",
        properties => {
            digest => {
@@ -393,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',
@@ -421,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,
                }
            }),
     },
@@ -445,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');
@@ -453,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;
+
+               PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive);
+               $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive);
 
-       foreach my $volid (@$vollist) {
-           eval { PVE::Storage::vdisk_free($storecfg, $volid); };
-           # fixme: log ?
-           warn $@ if $@;
+                #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;
@@ -586,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',
@@ -600,7 +593,7 @@ __PACKAGE__->register_method({
            skiplock => get_standard_option('skiplock'),
        },
     },
-    returns => { 
+    returns => {
        type => 'string',
     },
     code => sub {
@@ -613,13 +606,13 @@ __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;
@@ -633,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',
@@ -669,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 => {
@@ -685,7 +677,7 @@ __PACKAGE__->register_method({
            vmid => get_standard_option('pve-vmid'),
        },
     },
-    returns => { 
+    returns => {
        additionalProperties => 0,
        properties => {
            user => { type => 'string' },
@@ -701,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);
        }
@@ -722,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;
@@ -746,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",
@@ -786,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',
@@ -812,12 +807,20 @@ __PACKAGE__->register_method({
        my $conf = PVE::QemuServer::load_config($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,
@@ -832,7 +835,7 @@ __PACKAGE__->register_method({
            stateuri => get_standard_option('pve-qm-stateuri'),
        },
     },
-    returns => { 
+    returns => {
        type => 'string',
     },
     code => sub {
@@ -847,16 +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';
 
-       die "VM $vmid already running\n" if PVE::QemuServer::check_running($vmid);
-
-       my $storecfg = PVE::Storage::config(); 
+       my $storecfg = PVE::Storage::config();
 
        my $realcmd = sub {
            my $upid = shift;
@@ -872,7 +873,7 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'vm_stop', 
+    name => 'vm_stop',
     path => '{vmid}/status/stop',
     method => 'POST',
     protected => 1,
@@ -889,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 {
@@ -907,10 +914,12 @@ __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 $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();
 
@@ -919,7 +928,8 @@ __PACKAGE__->register_method({
 
            syslog('info', "stop VM $vmid: $upid\n");
 
-           PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout});
+           PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0,
+                                    $param->{timeout}, 0, 1, $keepActive);
 
            return;
        };
@@ -928,7 +938,7 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'vm_reset', 
+    name => 'vm_reset',
     path => '{vmid}/status/reset',
     method => 'POST',
     protected => 1,
@@ -942,7 +952,7 @@ __PACKAGE__->register_method({
            skiplock => get_standard_option('skiplock'),
        },
     },
-    returns => { 
+    returns => {
        type => 'string',
     },
     code => sub {
@@ -957,7 +967,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';
 
        die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
@@ -974,7 +984,7 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'vm_shutdown', 
+    name => 'vm_shutdown',
     path => '{vmid}/status/shutdown',
     method => 'POST',
     protected => 1,
@@ -991,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 {
@@ -1009,10 +1031,12 @@ __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 $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();
 
@@ -1021,7 +1045,8 @@ __PACKAGE__->register_method({
 
            syslog('info', "shutdown VM $vmid: $upid\n");
 
-           PVE::QemuServer::vm_shutdown($storecfg, $vmid, $skiplock, $param->{timeout});
+           PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout},
+                                    1, $param->{forceStop}, $keepActive);
 
            return;
        };
@@ -1030,7 +1055,7 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'vm_suspend', 
+    name => 'vm_suspend',
     path => '{vmid}/status/suspend',
     method => 'POST',
     protected => 1,
@@ -1044,7 +1069,7 @@ __PACKAGE__->register_method({
            skiplock => get_standard_option('skiplock'),
        },
     },
-    returns => { 
+    returns => {
        type => 'string',
     },
     code => sub {
@@ -1059,7 +1084,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';
 
        die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
@@ -1078,7 +1103,7 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'vm_resume', 
+    name => 'vm_resume',
     path => '{vmid}/status/resume',
     method => 'POST',
     protected => 1,
@@ -1092,7 +1117,7 @@ __PACKAGE__->register_method({
            skiplock => get_standard_option('skiplock'),
        },
     },
-    returns => { 
+    returns => {
        type => 'string',
     },
     code => sub {
@@ -1107,7 +1132,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';
 
        die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
@@ -1126,7 +1151,7 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'vm_sendkey', 
+    name => 'vm_sendkey',
     path => '{vmid}/sendkey',
     method => 'PUT',
     protected => 1,
@@ -1157,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});
@@ -1166,7 +1191,7 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'migrate_vm', 
+    name => 'migrate_vm',
     path => '{vmid}/migrate',
     method => 'POST',
     protected => 1,
@@ -1190,7 +1215,7 @@ __PACKAGE__->register_method({
            },
        },
     },
-    returns => { 
+    returns => {
        type => 'string',
        description => "the task ID.",
     },
@@ -1214,7 +1239,7 @@ __PACKAGE__->register_method({
 
        my $vmid = extract_param($param, 'vmid');
 
-       raise_param_exc({ force => "Only root may use this option." }) 
+       raise_param_exc({ force => "Only root may use this option." })
            if $param->{force} && $user ne 'root@pam';
 
        # test if VM exists
@@ -1225,14 +1250,14 @@ __PACKAGE__->register_method({
        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);
@@ -1241,8 +1266,8 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
-    name => 'monitor', 
-    path => '{vmid}/monitor', 
+    name => 'monitor',
+    path => '{vmid}/monitor',
     method => 'POST',
     protected => 1,
     proxyto => 'node',