]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/API2/Qemu.pm
vmconfig_apply_pending: add error handling
[qemu-server.git] / PVE / API2 / Qemu.pm
index 49aaa48426f3c849cec77e32814e9bd51e586d5d..e853a8355f4456f662cb2b4cc619dffa47c77219 100644 (file)
@@ -4,12 +4,12 @@ use strict;
 use warnings;
 use Cwd 'abs_path';
 use Net::SSLeay;
-use UUID;
 use POSIX;
 use IO::Socket::IP;
 use URI::Escape;
 
 use PVE::Cluster qw (cfs_read_file cfs_write_file);;
+use PVE::RRD;
 use PVE::SafeSyslog;
 use PVE::Tools qw(extract_param);
 use PVE::Exception qw(raise raise_param_exc raise_perm_exc);
@@ -20,6 +20,7 @@ use PVE::ReplicationConfig;
 use PVE::GuestHelpers;
 use PVE::QemuConfig;
 use PVE::QemuServer;
+use PVE::QemuServer::Monitor qw(mon_cmd);
 use PVE::QemuMigrate;
 use PVE::RPCEnvironment;
 use PVE::AccessControl;
@@ -28,6 +29,9 @@ use PVE::Network;
 use PVE::Firewall;
 use PVE::API2::Firewall::VM;
 use PVE::API2::Qemu::Agent;
+use PVE::VZDump::Plugin;
+use PVE::DataCenterConfig;
+use PVE::SSHInfo;
 
 BEGIN {
     if (!$ENV{PVE_GENERATING_DOCS}) {
@@ -64,10 +68,9 @@ my $check_storage_access = sub {
        my $isCDROM = PVE::QemuServer::drive_is_cdrom($drive);
 
        my $volid = $drive->{file};
+       my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
 
-       if (!$volid || ($volid eq 'none' || $volid eq 'cloudinit')) {
-           # nothing to check
-       } elsif ($volid =~ m/^(([^:\s]+):)?(cloudinit)$/) {
+       if (!$volid || ($volid eq 'none' || $volid eq 'cloudinit' || (defined($volname) && $volname eq 'cloudinit'))) {
            # nothing to check
        } elsif ($isCDROM && ($volid eq 'cdrom')) {
            $rpcenv->check($authuser, "/", ['Sys.Console']);
@@ -140,31 +143,33 @@ my $create_disks = sub {
        my ($ds, $disk) = @_;
 
        my $volid = $disk->{file};
+       my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
 
        if (!$volid || $volid eq 'none' || $volid eq 'cdrom') {
            delete $disk->{size};
-           $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk);
-       } elsif ($volid =~ m!^(?:([^/:\s]+):)?cloudinit$!) {
-           my $storeid = $1 || $default_storage;
+           $res->{$ds} = PVE::QemuServer::print_drive($disk);
+       } elsif (defined($volname) && $volname eq 'cloudinit') {
+           $storeid = $storeid // $default_storage;
            die "no storage ID specified (and no default storage)\n" if !$storeid;
            my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
            my $name = "vm-$vmid-cloudinit";
+
            my $fmt = undef;
            if ($scfg->{path}) {
-               $name .= ".qcow2";
-               $fmt = 'qcow2';
-           }else{
-               $fmt = 'raw';
+               $fmt = $disk->{format} // "qcow2";
+               $name .= ".$fmt";
+           } else {
+               $fmt = $disk->{format} // "raw";
            }
-           # FIXME: Reasonable size? qcow2 shouldn't grow if the space isn't used anyway?
-           my $cloudinit_iso_size = 5; # in MB
-           my $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, 
-                                                 $fmt, $name, $cloudinit_iso_size*1024);
+
+           # Initial disk created with 4 MB and aligned to 4MB on regeneration
+           my $ci_size = PVE::QemuServer::Cloudinit::CLOUDINIT_DISK_SIZE;
+           my $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $fmt, $name, $ci_size/1024);
            $disk->{file} = $volid;
            $disk->{media} = 'cdrom';
            push @$vollist, $volid;
            delete $disk->{format}; # no longer needed
-           $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk);
+           $res->{$ds} = PVE::QemuServer::print_drive($disk);
        } elsif ($volid =~ $NEW_DISK_RE) {
            my ($storeid, $size) = ($2 || $default_storage, $3);
            die "no storage ID specified (and no default storage)\n" if !$storeid;
@@ -183,7 +188,7 @@ my $create_disks = sub {
            $disk->{file} = $volid;
            $disk->{size} = PVE::Tools::convert_size($size, 'kb' => 'b');
            delete $disk->{format}; # no longer needed
-           $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk);
+           $res->{$ds} = PVE::QemuServer::print_drive($disk);
        } else {
 
            PVE::Storage::check_volume_access($rpcenv, $authuser, $storecfg, $vmid, $volid);
@@ -197,18 +202,16 @@ my $create_disks = sub {
 
            if ($volid_is_new) {
 
-               my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
-
                PVE::Storage::activate_volumes($storecfg, [ $volid ]) if $storeid;
 
                my $size = PVE::Storage::volume_size_info($storecfg, $volid);
 
-               die "volume $volid does not exists\n" if !$size;
+               die "volume $volid does not exist\n" if !$size;
 
                $disk->{size} = $size;
            }
 
-           $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk);
+           $res->{$ds} = PVE::QemuServer::print_drive($disk);
        }
     };
 
@@ -259,6 +262,7 @@ my $hwtypeoptions = {
     'tablet' => 1,
     'vga' => 1,
     'watchdog' => 1,
+    'audio0' => 1,
 };
 
 my $generaloptions = {
@@ -279,6 +283,7 @@ my $generaloptions = {
     'startup' => 1,
     'tdf' => 1,
     'template' => 1,
+    'tags' => 1,
 };
 
 my $vmpoweroptions = {
@@ -307,10 +312,12 @@ my $check_vm_modify_config_perm = sub {
     return 1 if $authuser eq 'root@pam';
 
     foreach my $opt (@$key_list) {
-       # disk checks need to be done somewhere else
+       # some checks (e.g., disk, serial port, usb) need to be done somewhere
+       # else, as there the permission can be value dependend
        next if PVE::QemuServer::is_valid_drivename($opt);
        next if $opt eq 'cdrom';
-       next if $opt =~ m/^unused\d+$/;
+       next if $opt =~ m/^(?:unused|serial|usb)\d+$/;
+
 
        if ($cpuoptions->{$opt} || $opt =~ m/^numa\d+$/) {
            $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.CPU']);
@@ -330,8 +337,12 @@ my $check_vm_modify_config_perm = sub {
            $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']);
        } elsif ($cloudinitoptions->{$opt} || ($opt =~ m/^(?:net|ipconfig)\d+$/)) {
            $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']);
+       } elsif ($opt eq 'vmstate') {
+           # the user needs Disk and PowerMgmt privileges to change the vmstate
+           # also needs privileges on the storage, that will be checked later
+           $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk', 'VM.PowerMgmt' ]);
        } else {
-           # catches usb\d+, hostpci\d+, args, lock, etc.
+           # catches hostpci\d+, args, lock, etc.
            # new options will be checked here
            die "only root can set '$opt' config\n";
        }
@@ -440,10 +451,11 @@ __PACKAGE__->register_method({
                    description => "Add the VM to the specified pool.",
                },
                bwlimit => {
-                   description => "Override i/o bandwidth limit (in KiB/s).",
+                   description => "Override I/O bandwidth limit (in KiB/s).",
                    optional => 1,
                    type => 'integer',
                    minimum => '0',
+                   default => 'restore limit from datacenter or storage config',
                },
                start => {
                    optional => 1,
@@ -460,31 +472,20 @@ __PACKAGE__->register_method({
        my ($param) = @_;
 
        my $rpcenv = PVE::RPCEnvironment::get();
-
        my $authuser = $rpcenv->get_user();
 
        my $node = extract_param($param, 'node');
-
        my $vmid = extract_param($param, 'vmid');
 
        my $archive = extract_param($param, 'archive');
        my $is_restore = !!$archive;
 
-       my $storage = extract_param($param, 'storage');
-
+       my $bwlimit = extract_param($param, 'bwlimit');
        my $force = extract_param($param, 'force');
-
-       my $unique = extract_param($param, 'unique');
-
        my $pool = extract_param($param, 'pool');
-
-       my $bwlimit = extract_param($param, 'bwlimit');
-
        my $start_after_create = extract_param($param, 'start');
-
-       my $filename = PVE::QemuConfig->config_file($vmid);
-
-       my $storecfg = PVE::Storage::config();
+       my $storage = extract_param($param, 'storage');
+       my $unique = extract_param($param, 'unique');
 
        if (defined(my $ssh_keys = $param->{sshkeys})) {
                $ssh_keys = URI::Escape::uri_unescape($ssh_keys);
@@ -493,6 +494,9 @@ __PACKAGE__->register_method({
 
        PVE::Cluster::check_cfs_quorum();
 
+       my $filename = PVE::QemuConfig->config_file($vmid);
+       my $storecfg = PVE::Storage::config();
+
        if (defined($pool)) {
            $rpcenv->check_pool_exist($pool);
        }
@@ -524,7 +528,7 @@ __PACKAGE__->register_method({
                    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);
+                   $param->{$opt} = PVE::QemuServer::print_drive($drive);
                }
            }
 
@@ -553,27 +557,35 @@ __PACKAGE__->register_method({
            PVE::QemuConfig->check_protection($conf, $emsg);
 
            die "$emsg vm is running\n" if PVE::QemuServer::check_running($vmid);
-           die "$emsg vm is a template\n" if PVE::QemuConfig->is_template($conf);
 
            my $realcmd = sub {
                PVE::QemuServer::restore_archive($archive, $vmid, $authuser, {
                    storage => $storage,
                    pool => $pool,
                    unique => $unique,
-                   bwlimit => $bwlimit, });
-
-               PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool;
-
-               if ($start_after_create) {
-                   eval { PVE::API2::Qemu->vm_start({ vmid => $vmid, node => $node }) };
+                   bwlimit => $bwlimit,
+               });
+               my $restored_conf = PVE::QemuConfig->load_config($vmid);
+               # Convert restored VM to template if backup was VM template
+               if (PVE::QemuConfig->is_template($restored_conf)) {
+                   warn "Convert to template.\n";
+                   eval { PVE::QemuServer::template_create($vmid, $restored_conf) };
                    warn $@ if $@;
                }
+
+               PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool;
            };
 
            # ensure no old replication state are exists
            PVE::ReplicationState::delete_guest_states($vmid);
 
-           return PVE::QemuConfig->lock_config_full($vmid, 1, $realcmd);
+           PVE::QemuConfig->lock_config_full($vmid, 1, $realcmd);
+
+           if ($start_after_create) {
+               print "Execute autostart\n";
+               eval { PVE::API2::Qemu->vm_start({ vmid => $vmid, node => $node }) };
+               warn $@ if $@;
+           }
        };
 
        my $createfn = sub {
@@ -586,7 +598,7 @@ __PACKAGE__->register_method({
 
                my $conf = $param;
 
-               my ($arch, undef) = PVE::QemuServer::get_basic_machine_info($conf);
+               my $arch = PVE::QemuServer::get_vm_arch($conf);
 
                eval {
 
@@ -762,7 +774,7 @@ __PACKAGE__->register_method({
     code => sub {
        my ($param) = @_;
 
-       return PVE::Cluster::create_rrd_graph(
+       return PVE::RRD::create_rrd_graph(
            "pve2-vm/$param->{vmid}", $param->{timeframe},
            $param->{ds}, $param->{cf});
 
@@ -805,7 +817,7 @@ __PACKAGE__->register_method({
     code => sub {
        my ($param) = @_;
 
-       return PVE::Cluster::create_rrd_data(
+       return PVE::RRD::create_rrd_data(
            "pve2-vm/$param->{vmid}", $param->{timeframe}, $param->{cf});
     }});
 
@@ -853,40 +865,19 @@ __PACKAGE__->register_method({
     code => sub {
        my ($param) = @_;
 
-       my $conf = PVE::QemuConfig->load_config($param->{vmid});
-
-       if (my $snapname = $param->{snapshot}) {
-           my $snapshot = $conf->{snapshots}->{$snapname};
-           die "snapshot '$snapname' does not exist\n" if !defined($snapshot);
-
-           $snapshot->{digest} = $conf->{digest}; # keep file digest for API
-
-           $conf = $snapshot;
-       }
-
-       delete $conf->{snapshots};
-
-       if (!$param->{current}) {
-           foreach my $opt (keys %{$conf->{pending}}) {
-               next if $opt eq 'delete';
-               my $value = $conf->{pending}->{$opt};
-               next if ref($value); # just to be sure
-               $conf->{$opt} = $value;
-           }
-           my $pending_delete_hash = PVE::QemuServer::split_flagged_list($conf->{pending}->{delete});
-           foreach my $opt (keys %$pending_delete_hash) {
-               delete $conf->{$opt} if $conf->{$opt};
-           }
-       }
-
-       delete $conf->{pending};
+       raise_param_exc({ snapshot => "cannot use 'snapshot' parameter with 'current'",
+                         current => "cannot use 'snapshot' parameter with 'current'"})
+           if ($param->{snapshot} && $param->{current});
 
-       # hide cloudinit password
-       if ($conf->{cipassword}) {
-           $conf->{cipassword} = '**********';
+       my $conf;
+       if ($param->{snapshot}) {
+           $conf = PVE::QemuConfig->load_snapshot_config($param->{vmid}, $param->{snapshot});
+       } else {
+           $conf = PVE::QemuConfig->load_current_config($param->{vmid}, $param->{current});
        }
-
+       $conf->{cipassword} = '**********' if $conf->{cipassword};
        return $conf;
+
     }});
 
 __PACKAGE__->register_method({
@@ -940,49 +931,13 @@ __PACKAGE__->register_method({
 
        my $conf = PVE::QemuConfig->load_config($param->{vmid});
 
-       my $pending_delete_hash = PVE::QemuServer::split_flagged_list($conf->{pending}->{delete});
-
-       my $res = [];
+       my $pending_delete_hash = PVE::QemuConfig->parse_pending_delete($conf->{pending}->{delete});
 
-       foreach my $opt (keys %$conf) {
-           next if ref($conf->{$opt});
-           my $item = { key => $opt };
-           $item->{value} = $conf->{$opt} if defined($conf->{$opt});
-           $item->{pending} = $conf->{pending}->{$opt} if defined($conf->{pending}->{$opt});
-           $item->{delete} = ($pending_delete_hash->{$opt} ? 2 : 1) if exists $pending_delete_hash->{$opt};
-
-           # hide cloudinit password
-           if ($opt eq 'cipassword') {
-               $item->{value} = '**********' if defined($item->{value});
-               # the trailing space so that the pending string is different
-               $item->{pending} = '********** ' if defined($item->{pending});
-           }
-           push @$res, $item;
-       }
-
-       foreach my $opt (keys %{$conf->{pending}}) {
-           next if $opt eq 'delete';
-           next if ref($conf->{pending}->{$opt}); # just to be sure
-           next if defined($conf->{$opt});
-           my $item = { key => $opt };
-           $item->{pending} = $conf->{pending}->{$opt};
-
-           # hide cloudinit password
-           if ($opt eq 'cipassword') {
-               $item->{pending} = '**********' if defined($item->{pending});
-           }
-           push @$res, $item;
-       }
-
-       while (my ($opt, $force) = each %$pending_delete_hash) {
-           next if $conf->{pending}->{$opt}; # just to be sure
-           next if $conf->{$opt};
-           my $item = { key => $opt, delete => ($force ? 2 : 1)};
-           push @$res, $item;
-       }
+       $conf->{cipassword} = '**********' if defined($conf->{cipassword});
+       $conf->{pending}->{cipassword} = '********** ' if defined($conf->{pending}->{cipassword});
 
-       return $res;
-    }});
+       return PVE::GuestHelpers::config_with_pending_array($conf, $pending_delete_hash);
+   }});
 
 # POST/PUT {vmid}/config implementation
 #
@@ -1088,12 +1043,15 @@ my $update_vm_api  = sub {
        my $volid = $drive->{file};
        return if !$volid || !($drive->{replicate}//1);
        return if PVE::QemuServer::drive_is_cdrom($drive);
-       my ($storeid, $format);
+
+       my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
+       return if $volname eq 'cloudinit';
+
+       my $format;
        if ($volid =~ $NEW_DISK_RE) {
            $storeid = $2;
            $format = $drive->{format} || PVE::Storage::storage_default_format($storecfg, $storeid);
        } else {
-           ($storeid, undef) = PVE::Storage::parse_volume_id($volid, 1);
            $format = (PVE::Storage::parse_volname($storecfg, $volid))[6];
        }
        return if PVE::Storage::storage_can_replicate($storecfg, $storeid, $format);
@@ -1109,7 +1067,7 @@ my $update_vm_api  = sub {
            raise_param_exc({ $opt => "unable to parse drive options" }) if !$drive;
            PVE::QemuServer::cleanup_drive_path($opt, $storecfg, $drive);
            $check_replication->($drive);
-           $param->{$opt} = PVE::QemuServer::print_drive($vmid, $drive);
+           $param->{$opt} = PVE::QemuServer::print_drive($drive);
        } elsif ($opt =~ m/^net(\d+)$/) {
            # add macaddr
            my $net = PVE::QemuServer::parse_net($param->{$opt});
@@ -1137,6 +1095,15 @@ my $update_vm_api  = sub {
        die "checksum missmatch (file change by other user?)\n"
            if $digest && $digest ne $conf->{digest};
 
+       # FIXME: 'suspended' lock should probabyl be a state or "weak" lock?!
+       if (scalar(@delete) && grep { $_ eq 'vmstate'} @delete) {
+           if (defined($conf->{lock}) && $conf->{lock} eq 'suspended') {
+               delete $conf->{lock}; # for check lock check, not written out
+               push @delete, 'lock'; # this is the real deal to write it out
+           }
+           push @delete, 'runningmachine' if $conf->{runningmachine};
+       }
+
        PVE::QemuConfig->check_lock($conf) if !$skiplock;
 
        foreach my $opt (keys %$revert) {
@@ -1168,29 +1135,56 @@ my $update_vm_api  = sub {
            foreach my $opt (@delete) {
                $modified->{$opt} = 1;
                $conf = PVE::QemuConfig->load_config($vmid); # update/reload
-               if (!defined($conf->{$opt}) && !defined($conf->{pending}->{$opt})) {
+
+               # value of what we want to delete, independent if pending or not
+               my $val = $conf->{$opt} // $conf->{pending}->{$opt};
+               if (!defined($val)) {
                    warn "cannot delete '$opt' - not set in current configuration!\n";
                    $modified->{$opt} = 0;
                    next;
                }
+               my $is_pending_val = defined($conf->{pending}->{$opt});
+               delete $conf->{pending}->{$opt};
 
                if ($opt =~ m/^unused/) {
-                   my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
+                   my $drive = PVE::QemuServer::parse_drive($opt, $val);
                    PVE::QemuConfig->check_protection($conf, "can't remove unused disk '$drive->{file}'");
                    $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
                    if (PVE::QemuServer::try_deallocate_drive($storecfg, $vmid, $conf, $opt, $drive, $rpcenv, $authuser)) {
                        delete $conf->{$opt};
                        PVE::QemuConfig->write_config($vmid, $conf);
                    }
+               } elsif ($opt eq 'vmstate') {
+                   PVE::QemuConfig->check_protection($conf, "can't remove vmstate '$val'");
+                   if (PVE::QemuServer::try_deallocate_drive($storecfg, $vmid, $conf, $opt, { file => $val }, $rpcenv, $authuser, 1)) {
+                       delete $conf->{$opt};
+                       PVE::QemuConfig->write_config($vmid, $conf);
+                   }
                } elsif (PVE::QemuServer::is_valid_drivename($opt)) {
                    PVE::QemuConfig->check_protection($conf, "can't remove drive '$opt'");
                    $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
-                   PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $conf->{pending}->{$opt}))
-                       if defined($conf->{pending}->{$opt});
-                   PVE::QemuServer::vmconfig_delete_pending_option($conf, $opt, $force);
+                   PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $val))
+                       if $is_pending_val;
+                   PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force);
+                   PVE::QemuConfig->write_config($vmid, $conf);
+               } elsif ($opt =~ m/^serial\d+$/) {
+                   if ($val eq 'socket') {
+                       $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']);
+                   } elsif ($authuser ne 'root@pam') {
+                       die "only root can delete '$opt' config for real devices\n";
+                   }
+                   PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force);
+                   PVE::QemuConfig->write_config($vmid, $conf);
+               } elsif ($opt =~ m/^usb\d+$/) {
+                   if ($val =~ m/spice/) {
+                       $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']);
+                   } elsif ($authuser ne 'root@pam') {
+                       die "only root can delete '$opt' config for real devices\n";
+                   }
+                   PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force);
                    PVE::QemuConfig->write_config($vmid, $conf);
                } else {
-                   PVE::QemuServer::vmconfig_delete_pending_option($conf, $opt, $force);
+                   PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force);
                    PVE::QemuConfig->write_config($vmid, $conf);
                }
            }
@@ -1200,7 +1194,7 @@ my $update_vm_api  = sub {
                $conf = PVE::QemuConfig->load_config($vmid); # update/reload
                next if defined($conf->{pending}->{$opt}) && ($param->{$opt} eq $conf->{pending}->{$opt}); # skip if nothing changed
 
-               my ($arch, undef) = PVE::QemuServer::get_basic_machine_info($conf);
+               my $arch = PVE::QemuServer::get_vm_arch($conf);
 
                if (PVE::QemuServer::is_valid_drivename($opt)) {
                    my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
@@ -1214,16 +1208,30 @@ my $update_vm_api  = sub {
                        if defined($conf->{pending}->{$opt});
 
                    &$create_disks($rpcenv, $authuser, $conf->{pending}, $arch, $storecfg, $vmid, undef, {$opt => $param->{$opt}});
+               } elsif ($opt =~ m/^serial\d+/) {
+                   if ((!defined($conf->{$opt}) || $conf->{$opt} eq 'socket') && $param->{$opt} eq 'socket') {
+                       $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']);
+                   } elsif ($authuser ne 'root@pam') {
+                       die "only root can modify '$opt' config for real devices\n";
+                   }
+                   $conf->{pending}->{$opt} = $param->{$opt};
+               } elsif ($opt =~ m/^usb\d+/) {
+                   if ((!defined($conf->{$opt}) || $conf->{$opt} =~ m/spice/) && $param->{$opt} =~ m/spice/) {
+                       $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.HWType']);
+                   } elsif ($authuser ne 'root@pam') {
+                       die "only root can modify '$opt' config for real devices\n";
+                   }
+                   $conf->{pending}->{$opt} = $param->{$opt};
                } else {
                    $conf->{pending}->{$opt} = $param->{$opt};
                }
-               PVE::QemuServer::vmconfig_undelete_pending_option($conf, $opt);
+               PVE::QemuConfig->remove_from_pending_delete($conf, $opt);
                PVE::QemuConfig->write_config($vmid, $conf);
            }
 
            # remove pending changes when nothing changed
            $conf = PVE::QemuConfig->load_config($vmid); # update/reload
-           my $changes = PVE::QemuServer::vmconfig_cleanup_pending($conf);
+           my $changes = PVE::QemuConfig->cleanup_pending($conf);
            PVE::QemuConfig->write_config($vmid, $conf) if $changes;
 
            return if !scalar(keys %{$conf->{pending}});
@@ -1234,13 +1242,13 @@ my $update_vm_api  = sub {
 
            $conf = PVE::QemuConfig->load_config($vmid); # update/reload
 
+           my $errors = {};
            if ($running) {
-               my $errors = {};
                PVE::QemuServer::vmconfig_hotplug_pending($vmid, $conf, $storecfg, $modified, $errors);
-               raise_param_exc($errors) if scalar(keys %$errors);
            } else {
-               PVE::QemuServer::vmconfig_apply_pending($vmid, $conf, $storecfg, $running);
+               PVE::QemuServer::vmconfig_apply_pending($vmid, $conf, $storecfg, $running, $errors);
            }
+           raise_param_exc($errors) if scalar(keys %$errors);
 
            return;
        };
@@ -1395,7 +1403,6 @@ __PACKAGE__->register_method({
     }
 });
 
-
 __PACKAGE__->register_method({
     name => 'destroy_vm',
     path => '{vmid}',
@@ -1412,6 +1419,11 @@ __PACKAGE__->register_method({
            node => get_standard_option('pve-node'),
            vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_stopped }),
            skiplock => get_standard_option('skiplock'),
+           purge => {
+               type => 'boolean',
+               description => "Remove vmid from backup cron jobs.",
+               optional => 1,
+           },
        },
     },
     returns => {
@@ -1421,9 +1433,7 @@ __PACKAGE__->register_method({
        my ($param) = @_;
 
        my $rpcenv = PVE::RPCEnvironment::get();
-
        my $authuser = $rpcenv->get_user();
-
        my $vmid = $param->{vmid};
 
        my $skiplock = $param->{skiplock};
@@ -1432,17 +1442,16 @@ __PACKAGE__->register_method({
 
        # test if VM exists
        my $conf = PVE::QemuConfig->load_config($vmid);
-
        my $storecfg = PVE::Storage::config();
-
        PVE::QemuConfig->check_protection($conf, "can't remove VM $vmid");
-
        die "unable to remove VM $vmid - used in HA resources\n"
            if PVE::HA::Config::vm_is_ha_managed($vmid);
 
-       # do not allow destroy if there are replication jobs
-       my $repl_conf = PVE::ReplicationConfig->new();
-       $repl_conf->check_for_existing_jobs($vmid);
+       if (!$param->{purge}) {
+           # don't allow destroy if with replication jobs but no purge param
+           my $repl_conf = PVE::ReplicationConfig->new();
+           $repl_conf->check_for_existing_jobs($vmid);
+       }
 
        # early tests (repeat after locking)
        die "VM $vmid is running - destroy failed\n"
@@ -1452,12 +1461,22 @@ __PACKAGE__->register_method({
            my $upid = shift;
 
            syslog('info', "destroy VM $vmid: $upid\n");
+           PVE::QemuConfig->lock_config($vmid, sub {
+               die "VM $vmid is running - destroy failed\n"
+                   if (PVE::QemuServer::check_running($vmid));
 
-           PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock);
+               PVE::QemuServer::destroy_vm($storecfg, $vmid, $skiplock, { lock => 'destroyed' });
 
-           PVE::AccessControl::remove_vm_access($vmid);
+               PVE::AccessControl::remove_vm_access($vmid);
+               PVE::Firewall::remove_vmfw_conf($vmid);
+               if ($param->{purge}) {
+                   PVE::ReplicationConfig::remove_vmid_jobs($vmid);
+                   PVE::VZDump::Plugin::remove_vmid_from_backup_jobs($vmid);
+               }
 
-            PVE::Firewall::remove_vmfw_conf($vmid);
+               # only now remove the zombie config, else we can have reuse race
+               PVE::QemuConfig->destroy_config($vmid);
+           });
        };
 
        return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd);
@@ -1559,9 +1578,9 @@ __PACKAGE__->register_method({
 
        if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
            (undef, $family) = PVE::Cluster::remote_node_ip($node);
-           my $sshinfo = PVE::Cluster::get_ssh_info($node);
+           my $sshinfo = PVE::SSHInfo::get_ssh_info($node);
            # NOTE: kvm VNC traffic is already TLS encrypted or is known unsecure
-           $remcmd = PVE::Cluster::ssh_info_to_command($sshinfo, $use_serial ? '-t' : '-T');
+           $remcmd = PVE::SSHInfo::ssh_info_to_command($sshinfo, $use_serial ? '-t' : '-T');
        } else {
            $family = PVE::Tools::get_host_address_family($node);
        }
@@ -1699,8 +1718,8 @@ __PACKAGE__->register_method({
 
        if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
            (undef, $family) = PVE::Cluster::remote_node_ip($node);
-           my $sshinfo = PVE::Cluster::get_ssh_info($node);
-           $remcmd = PVE::Cluster::ssh_info_to_command($sshinfo, '-t');
+           my $sshinfo = PVE::SSHInfo::get_ssh_info($node);
+           $remcmd = PVE::SSHInfo::ssh_info_to_command($sshinfo, '-t');
            push @$remcmd, '--';
        } else {
            $family = PVE::Tools::get_host_address_family($node);
@@ -1832,8 +1851,8 @@ __PACKAGE__->register_method({
        my ($ticket, undef, $remote_viewer_config) =
            PVE::AccessControl::remote_viewer_config($authuser, $vmid, $node, $proxy, $title, $port);
 
-       PVE::QemuServer::vm_mon_cmd($vmid, "set_password", protocol => 'spice', password => $ticket);
-       PVE::QemuServer::vm_mon_cmd($vmid, "expire_password", protocol => 'spice', time => "+30");
+       mon_cmd($vmid, "set_password", protocol => 'spice', password => $ticket);
+       mon_cmd($vmid, "expire_password", protocol => 'spice', time => "+30");
 
        return $remote_viewer_config;
     }});
@@ -1874,6 +1893,10 @@ __PACKAGE__->register_method({
            { subdir => 'current' },
            { subdir => 'start' },
            { subdir => 'stop' },
+           { subdir => 'reset' },
+           { subdir => 'shutdown' },
+           { subdir => 'suspend' },
+           { subdir => 'reboot' },
            ];
 
        return $res;
@@ -1965,7 +1988,7 @@ __PACKAGE__->register_method({
                description => "CIDR of the (sub) network that is used for migration.",
                optional => 1,
            },
-           machine => get_standard_option('pve-qm-machine'),
+           machine => get_standard_option('pve-qemu-machine'),
            targetstorage => {
                description => "Target storage for the migration. (Can be '1' to use the same storage id as on the source node.)",
                type => 'string',
@@ -1980,45 +2003,33 @@ __PACKAGE__->register_method({
        my ($param) = @_;
 
        my $rpcenv = PVE::RPCEnvironment::get();
-
        my $authuser = $rpcenv->get_user();
 
        my $node = extract_param($param, 'node');
-
        my $vmid = extract_param($param, 'vmid');
 
        my $machine = extract_param($param, 'machine');
 
-       my $stateuri = extract_param($param, 'stateuri');
-       raise_param_exc({ stateuri => "Only root may use this option." })
-           if $stateuri && $authuser ne 'root@pam';
-
-       my $skiplock = extract_param($param, 'skiplock');
-       raise_param_exc({ skiplock => "Only root may use this option." })
-           if $skiplock && $authuser ne 'root@pam';
-
-       my $migratedfrom = extract_param($param, 'migratedfrom');
-       raise_param_exc({ migratedfrom => "Only root may use this option." })
-           if $migratedfrom && $authuser ne 'root@pam';
-
-       my $migration_type = extract_param($param, 'migration_type');
-       raise_param_exc({ migration_type => "Only root may use this option." })
-           if $migration_type && $authuser ne 'root@pam';
-
-       my $migration_network = extract_param($param, 'migration_network');
-       raise_param_exc({ migration_network => "Only root may use this option." })
-           if $migration_network && $authuser ne 'root@pam';
+       my $get_root_param = sub {
+           my $value = extract_param($param, $_[0]);
+           raise_param_exc({ "$_[0]" => "Only root may use this option." })
+               if $value && $authuser ne 'root@pam';
+           return $value;
+       };
 
-       my $targetstorage = extract_param($param, 'targetstorage');
-       raise_param_exc({ targetstorage => "Only root may use this option." })
-           if $targetstorage && $authuser ne 'root@pam';
+       my $stateuri = $get_root_param->('stateuri');
+       my $skiplock = $get_root_param->('skiplock');
+       my $migratedfrom = $get_root_param->('migratedfrom');
+       my $migration_type = $get_root_param->('migration_type');
+       my $migration_network = $get_root_param->('migration_network');
+       my $targetstorage = $get_root_param->('targetstorage');
 
        raise_param_exc({ targetstorage => "targetstorage can only by used with migratedfrom." })
            if $targetstorage && !$migratedfrom;
 
        # read spice ticket from STDIN
        my $spice_ticket;
-       if ($stateuri && ($stateuri eq 'tcp') && $migratedfrom && ($rpcenv->{type} eq 'cli')) {
+       if ($stateuri && ($stateuri eq 'tcp' || $stateuri eq 'unix') && $migratedfrom && ($rpcenv->{type} eq 'cli')) {
            if (defined(my $line = <STDIN>)) {
                chomp $line;
                $spice_ticket = $line;
@@ -2029,20 +2040,14 @@ __PACKAGE__->register_method({
 
        my $storecfg = PVE::Storage::config();
 
-       if (PVE::HA::Config::vm_is_ha_managed($vmid) && !$stateuri &&
-           $rpcenv->{type} ne 'ha') {
-
+       if (PVE::HA::Config::vm_is_ha_managed($vmid) && !$stateuri &&  $rpcenv->{type} ne 'ha') {
            my $hacmd = sub {
                my $upid = shift;
 
-               my $service = "vm:$vmid";
-
-               my $cmd = ['ha-manager', 'set', $service, '--state', 'started'];
-
                print "Requesting HA start for VM $vmid\n";
 
+               my $cmd = ['ha-manager', 'set',  "vm:$vmid", '--state', 'started'];
                PVE::Tools::run_command($cmd);
-
                return;
            };
 
@@ -2057,7 +2062,6 @@ __PACKAGE__->register_method({
 
                PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock, $migratedfrom, undef,
                                          $machine, $spice_ticket, $migration_network, $migration_type, $targetstorage);
-
                return;
            };
 
@@ -2105,11 +2109,9 @@ __PACKAGE__->register_method({
        my ($param) = @_;
 
        my $rpcenv = PVE::RPCEnvironment::get();
-
        my $authuser = $rpcenv->get_user();
 
        my $node = extract_param($param, 'node');
-
        my $vmid = extract_param($param, 'vmid');
 
        my $skiplock = extract_param($param, 'skiplock');
@@ -2132,14 +2134,10 @@ __PACKAGE__->register_method({
            my $hacmd = sub {
                my $upid = shift;
 
-               my $service = "vm:$vmid";
-
-               my $cmd = ['ha-manager', 'set', $service, '--state', 'stopped'];
-
                print "Requesting HA stop for VM $vmid\n";
 
+               my $cmd = ['ha-manager', 'crm-command', 'stop',  "vm:$vmid", '0'];
                PVE::Tools::run_command($cmd);
-
                return;
            };
 
@@ -2153,7 +2151,6 @@ __PACKAGE__->register_method({
 
                PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0,
                                         $param->{timeout}, 0, 1, $keepActive, $migratedfrom);
-
                return;
            };
 
@@ -2256,11 +2253,9 @@ __PACKAGE__->register_method({
        my ($param) = @_;
 
        my $rpcenv = PVE::RPCEnvironment::get();
-
        my $authuser = $rpcenv->get_user();
 
        my $node = extract_param($param, 'node');
-
        my $vmid = extract_param($param, 'vmid');
 
        my $skiplock = extract_param($param, 'skiplock');
@@ -2281,9 +2276,9 @@ __PACKAGE__->register_method({
        #
        # checking the qmp status here to get feedback to the gui/cli/api
        # and the status query should not take too long
-       my $qmpstatus;
-       eval {
-           $qmpstatus = PVE::QemuServer::vm_qmp_command($vmid, { execute => "query-status" }, 0);
+       my $qmpstatus = eval {
+           PVE::QemuConfig::assert_config_exists_on_node($vmid);
+           mon_cmd($vmid, "query-status");
        };
        my $err = $@ if $@;
 
@@ -2296,20 +2291,16 @@ __PACKAGE__->register_method({
            }
        }
 
-       if (PVE::HA::Config::vm_is_ha_managed($vmid) &&
-           ($rpcenv->{type} ne 'ha')) {
+       if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
 
+           my $timeout = $param->{timeout} // 60;
            my $hacmd = sub {
                my $upid = shift;
 
-               my $service = "vm:$vmid";
-
-               my $cmd = ['ha-manager', 'set', $service, '--state', 'stopped'];
-
                print "Requesting HA stop for VM $vmid\n";
 
+               my $cmd = ['ha-manager', 'crm-command', 'stop', "vm:$vmid", "$timeout"];
                PVE::Tools::run_command($cmd);
-
                return;
            };
 
@@ -2324,7 +2315,6 @@ __PACKAGE__->register_method({
 
                PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout},
                                         $shutdown, $param->{forceStop}, $keepActive);
-
                return;
            };
 
@@ -2332,6 +2322,65 @@ __PACKAGE__->register_method({
        }
     }});
 
+__PACKAGE__->register_method({
+    name => 'vm_reboot',
+    path => '{vmid}/status/reboot',
+    method => 'POST',
+    protected => 1,
+    proxyto => 'node',
+    description => "Reboot the VM by shutting it down, and starting it again. Applies pending changes.",
+    permissions => {
+       check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
+    },
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           vmid => get_standard_option('pve-vmid',
+                                       { completion => \&PVE::QemuServer::complete_vmid_running }),
+           timeout => {
+               description => "Wait maximal timeout seconds for the shutdown.",
+               type => 'integer',
+               minimum => 0,
+               optional => 1,
+           },
+       },
+    },
+    returns => {
+       type => 'string',
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+       my $authuser = $rpcenv->get_user();
+
+       my $node = extract_param($param, 'node');
+       my $vmid = extract_param($param, 'vmid');
+
+       my $qmpstatus = eval {
+           PVE::QemuConfig::assert_config_exists_on_node($vmid);
+           mon_cmd($vmid, "query-status");
+       };
+       my $err = $@ if $@;
+
+       if (!$err && $qmpstatus->{status} eq "paused") {
+           die "VM is paused - cannot shutdown\n";
+       }
+
+       die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
+
+       my $realcmd = sub {
+           my $upid = shift;
+
+           syslog('info', "requesting reboot of VM $vmid: $upid\n");
+           PVE::QemuServer::vm_reboot($vmid, $param->{timeout});
+           return;
+       };
+
+       return $rpcenv->fork_worker('qmreboot', $vmid, $authuser, $realcmd);
+    }});
+
 __PACKAGE__->register_method({
     name => 'vm_suspend',
     path => '{vmid}/status/suspend',
@@ -2340,6 +2389,9 @@ __PACKAGE__->register_method({
     proxyto => 'node',
     description => "Suspend virtual machine.",
     permissions => {
+       description => "You need 'VM.PowerMgmt' on /vms/{vmid}, and if you have set 'todisk',".
+           " you need also 'VM.Config.Disk' on /vms/{vmid} and 'Datastore.AllocateSpace'".
+           " on the storage for the vmstate.",
        check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
     },
     parameters => {
@@ -2349,6 +2401,18 @@ __PACKAGE__->register_method({
            vmid => get_standard_option('pve-vmid',
                                        { completion => \&PVE::QemuServer::complete_vmid_running }),
            skiplock => get_standard_option('skiplock'),
+           todisk => {
+               type => 'boolean',
+               default => 0,
+               optional => 1,
+               description => 'If set, suspends the VM to disk. Will be resumed on next VM start.',
+           },
+           statestorage => get_standard_option('pve-storage-id', {
+               description => "The storage for the VM state",
+               requires => 'todisk',
+               optional => 1,
+               completion => \&PVE::Storage::complete_storage_enabled,
+           }),
        },
     },
     returns => {
@@ -2358,30 +2422,50 @@ __PACKAGE__->register_method({
        my ($param) = @_;
 
        my $rpcenv = PVE::RPCEnvironment::get();
-
        my $authuser = $rpcenv->get_user();
 
        my $node = extract_param($param, 'node');
-
        my $vmid = extract_param($param, 'vmid');
 
+       my $todisk = extract_param($param, 'todisk') // 0;
+
+       my $statestorage = extract_param($param, 'statestorage');
+
        my $skiplock = extract_param($param, 'skiplock');
        raise_param_exc({ skiplock => "Only root may use this option." })
            if $skiplock && $authuser ne 'root@pam';
 
        die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
 
+       die "Cannot suspend HA managed VM to disk\n"
+           if $todisk && PVE::HA::Config::vm_is_ha_managed($vmid);
+
+       # early check for storage permission, for better user feedback
+       if ($todisk) {
+           $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
+
+           if (!$statestorage) {
+               # get statestorage from config if none is given
+               my $conf = PVE::QemuConfig->load_config($vmid);
+               my $storecfg = PVE::Storage::config();
+               $statestorage = PVE::QemuServer::find_vmstate_storage($conf, $storecfg);
+           }
+
+           $rpcenv->check($authuser, "/storage/$statestorage", ['Datastore.AllocateSpace']);
+       }
+
        my $realcmd = sub {
            my $upid = shift;
 
            syslog('info', "suspend VM $vmid: $upid\n");
 
-           PVE::QemuServer::vm_suspend($vmid, $skiplock);
+           PVE::QemuServer::vm_suspend($vmid, $skiplock, $todisk, $statestorage);
 
            return;
        };
 
-       return $rpcenv->fork_worker('qmsuspend', $vmid, $authuser, $realcmd);
+       my $taskname = $todisk ? 'qmsuspend' : 'qmpause';
+       return $rpcenv->fork_worker($taskname, $vmid, $authuser, $realcmd);
     }});
 
 __PACKAGE__->register_method({
@@ -2425,14 +2509,28 @@ __PACKAGE__->register_method({
 
        my $nocheck = extract_param($param, 'nocheck');
 
-       die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid, $nocheck);
+       my $to_disk_suspended;
+       eval {
+           PVE::QemuConfig->lock_config($vmid, sub {
+               my $conf = PVE::QemuConfig->load_config($vmid);
+               $to_disk_suspended = PVE::QemuConfig->has_lock($conf, 'suspended');
+           });
+       };
+
+       die "VM $vmid not running\n"
+           if !$to_disk_suspended && !PVE::QemuServer::check_running($vmid, $nocheck);
 
        my $realcmd = sub {
            my $upid = shift;
 
            syslog('info', "resume VM $vmid: $upid\n");
 
-           PVE::QemuServer::vm_resume($vmid, $skiplock, $nocheck);
+           if (!$to_disk_suspended) {
+               PVE::QemuServer::vm_resume($vmid, $skiplock, $nocheck);
+           } else {
+               my $storecfg = PVE::Storage::config();
+               PVE::QemuServer::vm_start($storecfg, $vmid, undef, $skiplock);
+           }
 
            return;
        };
@@ -2616,6 +2714,13 @@ __PACKAGE__->register_method({
                description => "Target node. Only allowed if the original VM is on shared storage.",
                optional => 1,
            }),
+           bwlimit => {
+               description => "Override I/O bandwidth limit (in KiB/s).",
+               optional => 1,
+               type => 'integer',
+               minimum => '0',
+               default => 'clone limit from datacenter or storage config',
+           },
         },
     },
     returns => {
@@ -2801,6 +2906,8 @@ __PACKAGE__->register_method({
 
                    PVE::Storage::activate_volumes($storecfg, $vollist, $snapname);
 
+                   my $bwlimit = extract_param($param, 'bwlimit');
+
                    my $total_jobs = scalar(keys %{$drives});
                    my $i = 1;
 
@@ -2808,11 +2915,16 @@ __PACKAGE__->register_method({
                        my $drive = $drives->{$opt};
                        my $skipcomplete = ($total_jobs != $i); # finish after last drive
 
+                       my $src_sid = PVE::Storage::parse_volume_id($drive->{file});
+                       my $storage_list = [ $src_sid ];
+                       push @$storage_list, $storage if defined($storage);
+                       my $clonelimit = PVE::Storage::get_bandwidth_limit('clone', $storage_list, $bwlimit);
+
                        my $newdrive = PVE::QemuServer::clone_disk($storecfg, $vmid, $running, $opt, $drive, $snapname,
                                                                   $newid, $storage, $format, $fullclone->{$opt}, $newvollist,
-                                                                  $jobs, $skipcomplete, $oldconf->{agent});
+                                                                  $jobs, $skipcomplete, $oldconf->{agent}, $clonelimit);
 
-                       $newconf->{$opt} = PVE::QemuServer::print_drive($vmid, $newdrive);
+                       $newconf->{$opt} = PVE::QemuServer::print_drive($newdrive);
 
                        PVE::QemuConfig->write_config($newid, $newconf);
                        $i++;
@@ -2916,6 +3028,13 @@ __PACKAGE__->register_method({
                maxLength => 40,
                optional => 1,
            },
+           bwlimit => {
+               description => "Override I/O bandwidth limit (in KiB/s).",
+               optional => 1,
+               type => 'integer',
+               minimum => '0',
+               default => 'move limit from datacenter or storage config',
+           },
        },
     },
     returns => {
@@ -2993,10 +3112,13 @@ __PACKAGE__->register_method({
                    warn "moving disk with snapshots, snapshots will not be moved!\n"
                        if $snapshotted;
 
+                   my $bwlimit = extract_param($param, 'bwlimit');
+                   my $movelimit = PVE::Storage::get_bandwidth_limit('move', [$oldstoreid, $storeid], $bwlimit);
+
                    my $newdrive = PVE::QemuServer::clone_disk($storecfg, $vmid, $running, $disk, $drive, undef,
-                                                              $vmid, $storeid, $format, 1, $newvollist);
+                                                              $vmid, $storeid, $format, 1, $newvollist, undef, undef, undef, $movelimit);
 
-                   $conf->{$disk} = PVE::QemuServer::print_drive($vmid, $newdrive);
+                   $conf->{$disk} = PVE::QemuServer::print_drive($newdrive);
 
                    PVE::QemuConfig->add_unused_volume($conf, $old_volid) if !$param->{delete};
 
@@ -3007,7 +3129,7 @@ __PACKAGE__->register_method({
                    PVE::QemuConfig->write_config($vmid, $conf);
 
                    if ($running && PVE::QemuServer::parse_guest_agent($conf)->{fstrim_cloned_disks} && PVE::QemuServer::qga_check_running($vmid)) {
-                       eval { PVE::QemuServer::vm_mon_cmd($vmid, "guest-fstrim"); };
+                       eval { mon_cmd($vmid, "guest-fstrim"); };
                    }
 
                    eval {
@@ -3041,6 +3163,138 @@ __PACKAGE__->register_method({
        return PVE::QemuConfig->lock_config($vmid, $updatefn);
     }});
 
+my $check_vm_disks_local = sub {
+    my ($storecfg, $vmconf, $vmid) = @_;
+
+    my $local_disks = {};
+
+    # add some more information to the disks e.g. cdrom
+    PVE::QemuServer::foreach_volid($vmconf, sub {
+       my ($volid, $attr) = @_;
+
+       my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
+       if ($storeid) {
+           my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
+           return if $scfg->{shared};
+       }
+       # The shared attr here is just a special case where the vdisk
+       # is marked as shared manually
+       return if $attr->{shared};
+       return if $attr->{cdrom} and $volid eq "none";
+
+       if (exists $local_disks->{$volid}) {
+           @{$local_disks->{$volid}}{keys %$attr} = values %$attr
+       } else {
+           $local_disks->{$volid} = $attr;
+           # ensure volid is present in case it's needed
+           $local_disks->{$volid}->{volid} = $volid;
+       }
+    });
+
+    return $local_disks;
+};
+
+__PACKAGE__->register_method({
+    name => 'migrate_vm_precondition',
+    path => '{vmid}/migrate',
+    method => 'GET',
+    protected => 1,
+    proxyto => 'node',
+    description => "Get preconditions for migration.",
+    permissions => {
+       check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
+    },
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
+           target => get_standard_option('pve-node', {
+               description => "Target node.",
+               completion =>  \&PVE::Cluster::complete_migration_target,
+               optional => 1,
+           }),
+       },
+    },
+    returns => {
+       type => "object",
+       properties => {
+           running => { type => 'boolean' },
+           allowed_nodes => {
+               type => 'array',
+               optional => 1,
+               description => "List nodes allowed for offline migration, only passed if VM is offline"
+           },
+           not_allowed_nodes => {
+               type => 'object',
+               optional => 1,
+               description => "List not allowed nodes with additional informations, only passed if VM is offline"
+           },
+           local_disks => {
+               type => 'array',
+               description => "List local disks including CD-Rom, unsused and not referenced disks"
+           },
+           local_resources => {
+               type => 'array',
+               description => "List local resources e.g. pci, usb"
+           }
+       },
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+
+       my $authuser = $rpcenv->get_user();
+
+       PVE::Cluster::check_cfs_quorum();
+
+       my $res = {};
+
+       my $vmid = extract_param($param, 'vmid');
+       my $target = extract_param($param, 'target');
+       my $localnode = PVE::INotify::nodename();
+
+
+       # test if VM exists
+       my $vmconf = PVE::QemuConfig->load_config($vmid);
+       my $storecfg = PVE::Storage::config();
+
+
+       # try to detect errors early
+       PVE::QemuConfig->check_lock($vmconf);
+
+       $res->{running} = PVE::QemuServer::check_running($vmid) ? 1:0;
+
+       # if vm is not running, return target nodes where local storage is available
+       # for offline migration
+       if (!$res->{running}) {
+           $res->{allowed_nodes} = [];
+           my $checked_nodes = PVE::QemuServer::check_local_storage_availability($vmconf, $storecfg);
+           delete $checked_nodes->{$localnode};
+
+           foreach my $node (keys %$checked_nodes) {
+               if (!defined $checked_nodes->{$node}->{unavailable_storages}) {
+                   push @{$res->{allowed_nodes}}, $node;
+               }
+
+           }
+           $res->{not_allowed_nodes} = $checked_nodes;
+       }
+
+
+       my $local_disks = &$check_vm_disks_local($storecfg, $vmconf, $vmid);
+       $res->{local_disks} = [ values %$local_disks ];;
+
+       my $local_resources =  PVE::QemuServer::check_local_resources($vmconf, 1);
+
+       $res->{local_resources} = $local_resources;
+
+       return $res;
+
+
+    }});
+
 __PACKAGE__->register_method({
     name => 'migrate_vm',
     path => '{vmid}/migrate',
@@ -3056,13 +3310,13 @@ __PACKAGE__->register_method({
        properties => {
            node => get_standard_option('pve-node'),
            vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
-           target => get_standard_option('pve-node', { 
+           target => get_standard_option('pve-node', {
                description => "Target node.",
                completion =>  \&PVE::Cluster::complete_migration_target,
             }),
            online => {
                type => 'boolean',
-               description => "Use online/live migration.",
+               description => "Use online/live migration if VM is running. Ignored if VM is stopped.",
                optional => 1,
            },
            force => {
@@ -3089,8 +3343,15 @@ __PACKAGE__->register_method({
             targetstorage => get_standard_option('pve-storage-id', {
                description => "Default target storage.",
                optional => 1,
-               completion => \&PVE::QemuServer::complete_storage,
+               completion => \&PVE::QemuServer::complete_migration_storage,
             }),
+           bwlimit => {
+               description => "Override I/O bandwidth limit (in KiB/s).",
+               optional => 1,
+               type => 'integer',
+               minimum => '0',
+               default => 'migrate limit from datacenter or storage config',
+           },
        },
     },
     returns => {
@@ -3101,7 +3362,6 @@ __PACKAGE__->register_method({
        my ($param) = @_;
 
        my $rpcenv = PVE::RPCEnvironment::get();
-
        my $authuser = $rpcenv->get_user();
 
        my $target = extract_param($param, 'target');
@@ -3117,9 +3377,6 @@ __PACKAGE__->register_method({
 
        my $vmid = extract_param($param, 'vmid');
 
-       raise_param_exc({ targetstorage => "Live storage migration can only be done online." })
-           if !$param->{online} && $param->{targetstorage};
-
        raise_param_exc({ force => "Only root may use this option." })
            if $param->{force} && $authuser ne 'root@pam';
 
@@ -3138,10 +3395,15 @@ __PACKAGE__->register_method({
        PVE::QemuConfig->check_lock($conf);
 
        if (PVE::QemuServer::check_running($vmid)) {
-           die "cant migrate running VM without --online\n"
-               if !$param->{online};
+           die "can't migrate running VM without --online\n" if !$param->{online};
+       } else {
+           warn "VM isn't running. Doing offline migration instead.\n" if $param->{online};
+           $param->{online} = 0;
        }
 
+       raise_param_exc({ targetstorage => "Live storage migration can only be done online." })
+           if !$param->{online} && $param->{targetstorage};
+
        my $storecfg = PVE::Storage::config();
 
        if( $param->{targetstorage}) {
@@ -3155,14 +3417,10 @@ __PACKAGE__->register_method({
            my $hacmd = sub {
                my $upid = shift;
 
-               my $service = "vm:$vmid";
-
-               my $cmd = ['ha-manager', 'migrate', $service, $target];
-
                print "Requesting HA migration for VM $vmid to node $target\n";
 
+               my $cmd = ['ha-manager', 'migrate', "vm:$vmid", $target];
                PVE::Tools::run_command($cmd);
-
                return;
            };
 
@@ -3227,7 +3485,7 @@ __PACKAGE__->register_method({
 
        my $res = '';
        eval {
-           $res = PVE::QemuServer::vm_human_monitor_command($vmid, $param->{command});
+           $res = PVE::QemuServer::Monitor::hmp_cmd($vmid, $param->{command});
        };
        $res = "ERROR: $@" if $@;
 
@@ -3307,7 +3565,7 @@ __PACKAGE__->register_method({
            my (undef, undef, undef, undef, undef, undef, $format) =
                PVE::Storage::parse_volname($storecfg, $drive->{file});
 
-           die "can't resize volume: $disk if snapshot exists\n" 
+           die "can't resize volume: $disk if snapshot exists\n"
                if %{$conf->{snapshots}} && $format eq 'qcow2';
 
            my $volid = $drive->{file};
@@ -3323,6 +3581,8 @@ __PACKAGE__->register_method({
            PVE::Storage::activate_volumes($storecfg, [$volid]);
            my $size = PVE::Storage::volume_size_info($storecfg, $volid, 5);
 
+           die "Could not determine current size of volume '$volid'\n" if !defined($size);
+
            die "internal error" if $sizestr !~ m/^(\+)?(\d+(\.\d+)?)([KMGT])?$/;
            my ($ext, $newsize, $unit) = ($1, $2, $4);
            if ($unit) {
@@ -3348,7 +3608,7 @@ __PACKAGE__->register_method({
            PVE::QemuServer::qemu_block_resize($vmid, "drive-$disk", $storecfg, $volid, $newsize);
 
            $drive->{size} = $newsize;
-           $conf->{$disk} = PVE::QemuServer::print_drive($vmid, $drive);
+           $conf->{$disk} = PVE::QemuServer::print_drive($drive);
 
            PVE::QemuConfig->write_config($vmid, $conf);
        };
@@ -3492,9 +3752,12 @@ __PACKAGE__->register_method({
        die "unable to use snapshot name 'current' (reserved name)\n"
            if $snapname eq 'current';
 
+       die "unable to use snapshot name 'pending' (reserved name)\n"
+           if lc($snapname) eq 'pending';
+
        my $realcmd = sub {
            PVE::Cluster::log_msg('info', $authuser, "snapshot VM $vmid: $snapname");
-           PVE::QemuConfig->snapshot_create($vmid, $snapname, $param->{vmstate}, 
+           PVE::QemuConfig->snapshot_create($vmid, $snapname, $param->{vmstate},
                                             $param->{description});
        };
 
@@ -3795,4 +4058,36 @@ __PACKAGE__->register_method({
        return undef;
     }});
 
+__PACKAGE__->register_method({
+    name => 'cloudinit_generated_config_dump',
+    path => '{vmid}/cloudinit/dump',
+    method => 'GET',
+    proxyto => 'node',
+    description => "Get automatically generated cloudinit config.",
+    permissions => {
+       check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
+    },
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
+           type => {
+               description => 'Config type.',
+               type => 'string',
+               enum => ['user', 'network', 'meta'],
+           },
+       },
+    },
+    returns => {
+       type => 'string',
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $conf = PVE::QemuConfig->load_config($param->{vmid});
+
+       return PVE::QemuServer::Cloudinit::dump_cloudinit_config($conf, $param->{vmid}, $param->{type});
+    }});
+
 1;