]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/API2/Qemu.pm
config: define machine schema as property-string
[qemu-server.git] / PVE / API2 / Qemu.pm
index 3af21325796399dbf189d550984b3c68ce6b4043..f3ce83d6f9d4126967a5034e36621d2fae12948c 100644 (file)
@@ -4,11 +4,16 @@ use strict;
 use warnings;
 use Cwd 'abs_path';
 use Net::SSLeay;
-use POSIX;
 use IO::Socket::IP;
+use IO::Socket::UNIX;
+use IPC::Open3;
+use JSON;
 use URI::Escape;
 use Crypt::OpenSSL::Random;
+use Socket qw(SOCK_STREAM);
 
+use PVE::APIClient::LWP;
+use PVE::CGroup;
 use PVE::Cluster qw (cfs_read_file cfs_write_file);;
 use PVE::RRD;
 use PVE::SafeSyslog;
@@ -18,14 +23,18 @@ use PVE::Storage;
 use PVE::JSONSchema qw(get_standard_option);
 use PVE::RESTHandler;
 use PVE::ReplicationConfig;
-use PVE::GuestHelpers;
+use PVE::GuestHelpers qw(assert_tag_permissions);
 use PVE::QemuConfig;
 use PVE::QemuServer;
+use PVE::QemuServer::Cloudinit;
 use PVE::QemuServer::CPUConfig;
 use PVE::QemuServer::Drive;
 use PVE::QemuServer::ImportDisk;
 use PVE::QemuServer::Monitor qw(mon_cmd);
 use PVE::QemuServer::Machine;
+use PVE::QemuServer::Memory qw(get_current_memory);
+use PVE::QemuServer::PCI;
+use PVE::QemuServer::USB;
 use PVE::QemuMigrate;
 use PVE::RPCEnvironment;
 use PVE::AccessControl;
@@ -38,6 +47,8 @@ use PVE::VZDump::Plugin;
 use PVE::DataCenterConfig;
 use PVE::SSHInfo;
 use PVE::Replication;
+use PVE::StorageTunnel;
+use PVE::RESTEnvironment qw(log_warn);
 
 BEGIN {
     if (!$ENV{PVE_GENERATING_DOCS}) {
@@ -48,8 +59,6 @@ BEGIN {
     }
 }
 
-use Data::Dumper; # fixme: remove
-
 use base qw(PVE::RESTHandler);
 
 my $opt_force_description = "Force physical removal. Without this, we simple remove the disk from the config file and create an additional configuration entry called 'unused[n]', which contains the volume ID. Unlink of unused[n] always cause physical removal.";
@@ -78,8 +87,6 @@ my $foreach_volume_with_alloc = sub {
     }
 };
 
-my $NEW_DISK_RE = qr!^(([^/:\s]+):)?(\d+(\.\d+)?)$!;
-
 my $check_drive_param = sub {
     my ($param, $storecfg, $extra_checks) = @_;
 
@@ -90,7 +97,7 @@ my $check_drive_param = sub {
        raise_param_exc({ $opt => "unable to parse drive options" }) if !$drive;
 
        if ($drive->{'import-from'}) {
-           if ($drive->{file} !~ $NEW_DISK_RE || $3 != 0) {
+           if ($drive->{file} !~ $PVE::QemuServer::Drive::NEW_DISK_RE || $3 != 0) {
                raise_param_exc({
                    $opt => "'import-from' requires special syntax - ".
                        "use <storage ID>:0,import-from=<source>",
@@ -134,7 +141,7 @@ my $check_storage_access = sub {
            # nothing to check
        } elsif ($isCDROM && ($volid eq 'cdrom')) {
            $rpcenv->check($authuser, "/", ['Sys.Console']);
-       } elsif (!$isCDROM && ($volid =~ $NEW_DISK_RE)) {
+       } elsif (!$isCDROM && ($volid =~ $PVE::QemuServer::Drive::NEW_DISK_RE)) {
            my ($storeid, $size) = ($2 || $default_storage, $3);
            die "no storage ID specified (and no default storage)\n" if !$storeid;
            $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
@@ -142,14 +149,12 @@ my $check_storage_access = sub {
            raise_param_exc({ storage => "storage '$storeid' does not support vm images"})
                if !$scfg->{content}->{images};
        } else {
-           PVE::Storage::check_volume_access(
-               $rpcenv,
-               $authuser,
-               $storecfg,
-               $vmid,
-               $volid,
-               'images',
-           );
+           PVE::Storage::check_volume_access($rpcenv, $authuser, $storecfg, $vmid, $volid);
+           if ($storeid) {
+               my ($vtype) = PVE::Storage::parse_volname($storecfg, $volid);
+               raise_param_exc({ $ds => "content type needs to be 'images' or 'iso'" })
+                   if $vtype ne 'images' && $vtype ne 'iso';
+           }
        }
 
        if (my $src_image = $drive->{'import-from'}) {
@@ -311,13 +316,26 @@ my $import_from_volid = sub {
 
 # Note: $pool is only needed when creating a VM, because pool permissions
 # are automatically inherited if VM already exists inside a pool.
-my $create_disks = sub {
-    my ($rpcenv, $authuser, $conf, $arch, $storecfg, $vmid, $pool, $settings, $default_storage) = @_;
+my sub create_disks : prototype($$$$$$$$$$) {
+    my (
+       $rpcenv,
+       $authuser,
+       $conf,
+       $arch,
+       $storecfg,
+       $vmid,
+       $pool,
+       $settings,
+       $default_storage,
+       $is_live_import,
+    ) = @_;
 
     my $vollist = [];
 
     my $res = {};
 
+    my $live_import_mapping = {};
+
     my $code = sub {
        my ($ds, $disk) = @_;
 
@@ -330,6 +348,15 @@ my $create_disks = sub {
        } elsif (defined($volname) && $volname eq 'cloudinit') {
            $storeid = $storeid // $default_storage;
            die "no storage ID specified (and no default storage)\n" if !$storeid;
+
+           if (
+               my $ci_key = PVE::QemuConfig->has_cloudinit($conf, $ds)
+               || PVE::QemuConfig->has_cloudinit($conf->{pending} || {}, $ds)
+               || PVE::QemuConfig->has_cloudinit($res, $ds)
+           ) {
+               die "$ds - cloud-init drive is already attached at '$ci_key'\n";
+           }
+
            my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
            my $name = "vm-$vmid-cloudinit";
 
@@ -350,56 +377,86 @@ my $create_disks = sub {
            delete $disk->{format}; # no longer needed
            $res->{$ds} = PVE::QemuServer::print_drive($disk);
            print "$ds: successfully created disk '$res->{$ds}'\n";
-       } elsif ($volid =~ $NEW_DISK_RE) {
+       } elsif ($volid =~ $PVE::QemuServer::Drive::NEW_DISK_RE) {
            my ($storeid, $size) = ($2 || $default_storage, $3);
            die "no storage ID specified (and no default storage)\n" if !$storeid;
 
+           $size = PVE::Tools::convert_size($size, 'gb' => 'kb'); # vdisk_alloc uses kb
+
+           my $live_import = $is_live_import && $ds ne 'efidisk0';
+           my $needs_creation = 1;
+
            if (my $source = delete $disk->{'import-from'}) {
                my $dst_volid;
 
+               $needs_creation = $live_import;
+
                if (PVE::Storage::parse_volume_id($source, 1)) { # PVE-managed volume
-                   my $dest_info = {
-                       vmid => $vmid,
-                       drivename => $ds,
-                       storage => $storeid,
-                       format => $disk->{format},
-                   };
+                   if ($live_import && $ds ne 'efidisk0') {
+                       my $path = PVE::Storage::path($storecfg, $source)
+                           or die "failed to get a path for '$source'\n";
+                       $source = $path;
+                       ($size, my $source_format) = PVE::Storage::file_size_info($source);
+                       die "could not get file size of $source\n" if !$size;
+                       $live_import_mapping->{$ds} = {
+                           path => $source,
+                           format => $source_format,
+                       };
+                   } else {
+                       my $dest_info = {
+                           vmid => $vmid,
+                           drivename => $ds,
+                           storage => $storeid,
+                           format => $disk->{format},
+                       };
 
-                   $dest_info->{efisize} = PVE::QemuServer::get_efivars_size($conf, $disk)
-                       if $ds eq 'efidisk0';
+                       $dest_info->{efisize} = PVE::QemuServer::get_efivars_size($conf, $disk)
+                           if $ds eq 'efidisk0';
 
-                   ($dst_volid, $size) = eval {
-                       $import_from_volid->($storecfg, $source, $dest_info, $vollist);
-                   };
-                   die "cannot import from '$source' - $@" if $@;
+                       ($dst_volid, $size) = eval {
+                           $import_from_volid->($storecfg, $source, $dest_info, $vollist);
+                       };
+                       die "cannot import from '$source' - $@" if $@;
+                   }
                } else {
                    $source = PVE::Storage::abs_filesystem_path($storecfg, $source, 1);
-                   $size = PVE::Storage::file_size_info($source);
+                   ($size, my $source_format) = PVE::Storage::file_size_info($source);
                    die "could not get file size of $source\n" if !$size;
 
-                   (undef, $dst_volid) = PVE::QemuServer::ImportDisk::do_import(
-                       $source,
-                       $vmid,
-                       $storeid,
-                       {
-                           drive_name => $ds,
-                           format => $disk->{format},
-                           'skip-config-update' => 1,
-                       },
-                   );
-                   push @$vollist, $dst_volid;
+                   if ($live_import && $ds ne 'efidisk0') {
+                       $live_import_mapping->{$ds} = {
+                           path => $source,
+                           format => $source_format,
+                       };
+                   } else {
+                       (undef, $dst_volid) = PVE::QemuServer::ImportDisk::do_import(
+                           $source,
+                           $vmid,
+                           $storeid,
+                           {
+                               drive_name => $ds,
+                               format => $disk->{format},
+                               'skip-config-update' => 1,
+                           },
+                       );
+                       push @$vollist, $dst_volid;
+                   }
                }
 
-               $disk->{file} = $dst_volid;
-               $disk->{size} = $size;
-               delete $disk->{format}; # no longer needed
-               $res->{$ds} = PVE::QemuServer::print_drive($disk);
-           } else {
+               if ($needs_creation) {
+                   $size = PVE::Tools::convert_size($size, 'b' => 'kb'); # vdisk_alloc uses kb
+               } else {
+                   $disk->{file} = $dst_volid;
+                   $disk->{size} = $size;
+                   delete $disk->{format}; # no longer needed
+                   $res->{$ds} = PVE::QemuServer::print_drive($disk);
+               }
+           }
+
+           if ($needs_creation) {
                my $defformat = PVE::Storage::storage_default_format($storecfg, $storeid);
                my $fmt = $disk->{format} || $defformat;
 
-               $size = PVE::Tools::convert_size($size, 'gb' => 'kb'); # vdisk_alloc uses kb
-
                my $volid;
                if ($ds eq 'efidisk0') {
                    my $smm = PVE::QemuServer::Machine::machine_type_is_q35($conf);
@@ -421,14 +478,22 @@ my $create_disks = sub {
 
            print "$ds: successfully created disk '$res->{$ds}'\n";
        } else {
-           PVE::Storage::check_volume_access(
-               $rpcenv,
-               $authuser,
-               $storecfg,
-               $vmid,
-               $volid,
-               'images',
-           );
+           PVE::Storage::check_volume_access($rpcenv, $authuser, $storecfg, $vmid, $volid);
+           if ($storeid) {
+               my ($vtype) = PVE::Storage::parse_volname($storecfg, $volid);
+               die "cannot use volume $volid - content type needs to be 'images' or 'iso'"
+                   if $vtype ne 'images' && $vtype ne 'iso';
+
+               if (PVE::QemuServer::Drive::drive_is_cloudinit($disk)) {
+                   if (
+                       my $ci_key = PVE::QemuConfig->has_cloudinit($conf, $ds)
+                       || PVE::QemuConfig->has_cloudinit($conf->{pending} || {}, $ds)
+                       || PVE::QemuConfig->has_cloudinit($res, $ds)
+                   ) {
+                       die "$ds - cloud-init drive is already attached at '$ci_key'\n";
+                   }
+               }
+           }
 
            PVE::Storage::activate_volumes($storecfg, [ $volid ]) if $storeid;
 
@@ -452,7 +517,10 @@ my $create_disks = sub {
        die $err;
     }
 
-    return ($vollist, $res);
+    # don't return empty import mappings
+    $live_import_mapping = undef if !%$live_import_mapping;
+
+    return ($vollist, $res, $live_import_mapping);
 };
 
 my $check_cpu_model_access = sub {
@@ -523,7 +591,6 @@ my $generaloptions = {
     'startup' => 1,
     'tdf' => 1,
     'template' => 1,
-    'tags' => 1,
 };
 
 my $vmpoweroptions = {
@@ -541,6 +608,7 @@ my $cloudinitoptions = {
     cipassword => 1,
     citype => 1,
     ciuser => 1,
+    ciupgrade => 1,
     nameserver => 1,
     searchdomain => 1,
     sshkeys => 1,
@@ -564,19 +632,64 @@ my $check_vm_create_serial_perm = sub {
     return 1;
 };
 
-my $check_vm_create_usb_perm = sub {
+my sub check_usb_perm {
+    my ($rpcenv, $authuser, $vmid, $pool, $opt, $value) = @_;
+
+    return 1 if $authuser eq 'root@pam';
+
+    $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']);
+
+    my $device = PVE::JSONSchema::parse_property_string('pve-qm-usb', $value);
+    if ($device->{host} && $device->{host} !~ m/^spice$/i) {
+       die "only root can set '$opt' config for real devices\n";
+    } elsif ($device->{mapping}) {
+       $rpcenv->check_full($authuser, "/mapping/usb/$device->{mapping}", ['Mapping.Use']);
+    } else {
+       die "either 'host' or 'mapping' must be set.\n";
+    }
+
+    return 1;
+}
+
+my sub check_vm_create_usb_perm {
     my ($rpcenv, $authuser, $vmid, $pool, $param) = @_;
 
     return 1 if $authuser eq 'root@pam';
 
     foreach my $opt (keys %{$param}) {
        next if $opt !~ m/^usb\d+$/;
+       check_usb_perm($rpcenv, $authuser, $vmid, $pool, $opt, $param->{$opt});
+    }
 
-       if ($param->{$opt} =~ m/spice/) {
-           $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']);
-       } else {
-           die "only root can set '$opt' config for real devices\n";
-       }
+    return 1;
+};
+
+my sub check_hostpci_perm {
+    my ($rpcenv, $authuser, $vmid, $pool, $opt, $value) = @_;
+
+    return 1 if $authuser eq 'root@pam';
+
+    my $device = PVE::JSONSchema::parse_property_string('pve-qm-hostpci', $value);
+    if ($device->{host}) {
+       die "only root can set '$opt' config for non-mapped devices\n";
+    } elsif ($device->{mapping}) {
+       $rpcenv->check_full($authuser, "/mapping/pci/$device->{mapping}", ['Mapping.Use']);
+       $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.HWType']);
+    } else {
+       die "either 'host' or 'mapping' must be set.\n";
+    }
+
+    return 1;
+}
+
+my sub check_vm_create_hostpci_perm {
+    my ($rpcenv, $authuser, $vmid, $pool, $param) = @_;
+
+    return 1 if $authuser eq 'root@pam';
+
+    foreach my $opt (keys %{$param}) {
+       next if $opt !~ m/^hostpci\d+$/;
+       check_hostpci_perm($rpcenv, $authuser, $vmid, $pool, $opt, $param->{$opt});
     }
 
     return 1;
@@ -592,7 +705,8 @@ my $check_vm_modify_config_perm = sub {
        # 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|serial|usb)\d+$/;
+       next if $opt =~ m/^(?:unused|serial|usb|hostpci)\d+$/;
+       next if $opt eq 'tags';
 
 
        if ($cpuoptions->{$opt} || $opt =~ m/^numa\d+$/) {
@@ -611,16 +725,16 @@ my $check_vm_modify_config_perm = sub {
            $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.PowerMgmt']);
        } elsif ($diskoptions->{$opt}) {
            $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Disk']);
-       } elsif ($opt =~ m/^(?:net|ipconfig)\d+$/) {
+       } elsif ($opt =~ m/^net\d+$/) {
            $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Network']);
-       } elsif ($cloudinitoptions->{$opt}) {
+       } elsif ($cloudinitoptions->{$opt} || $opt =~ m/^ipconfig\d+$/) {
            $rpcenv->check_vm_perm($authuser, $vmid, $pool, ['VM.Config.Cloudinit', 'VM.Config.Network'], 1);
        } 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 hostpci\d+, args, lock, etc.
+           # catches args, lock, etc.
            # new options will be checked here
            die "only root can set '$opt' config\n";
        }
@@ -629,6 +743,33 @@ my $check_vm_modify_config_perm = sub {
     return 1;
 };
 
+sub assert_scsi_feature_compatibility {
+    my ($opt, $conf, $storecfg, $drive_attributes) = @_;
+
+    my $drive = PVE::QemuServer::Drive::parse_drive($opt, $drive_attributes, 1);
+
+    my $machine_type = PVE::QemuServer::get_vm_machine($conf, undef, $conf->{arch});
+    my $machine_version = PVE::QemuServer::Machine::extract_version(
+       $machine_type, PVE::QemuServer::kvm_user_version());
+    my $drivetype = PVE::QemuServer::Drive::get_scsi_device_type(
+       $drive, $storecfg, $machine_version);
+
+    if ($drivetype ne 'hd' && $drivetype ne 'cd') {
+       if ($drive->{product}) {
+           raise_param_exc({
+               $opt => "Passing of product information is only supported for 'scsi-hd' and "
+                   ."'scsi-cd' devices (e.g. not pass-through).",
+           });
+       }
+       if ($drive->{vendor}) {
+           raise_param_exc({
+               $opt => "Passing of vendor information is only supported for 'scsi-hd' and "
+                   ."'scsi-cd' devices (e.g. not pass-through).",
+           });
+       }
+    }
+}
+
 __PACKAGE__->register_method({
     name => 'vmlist',
     path => '',
@@ -683,23 +824,22 @@ my $parse_restore_archive = sub {
 
     my ($archive_storeid, $archive_volname) = PVE::Storage::parse_volume_id($archive, 1);
 
+    my $res = {};
+
     if (defined($archive_storeid)) {
        my $scfg =  PVE::Storage::storage_config($storecfg, $archive_storeid);
+       $res->{volid} = $archive;
        if ($scfg->{type} eq 'pbs') {
-           return {
-               type => 'pbs',
-               volid => $archive,
-           };
+           $res->{type} = 'pbs';
+           return $res;
        }
     }
     my $path = PVE::Storage::abs_filesystem_path($storecfg, $archive);
-    return {
-       type => 'file',
-       path => $path,
-    };
+    $res->{type} = 'file';
+    $res->{path} = $path;
+    return $res;
 };
 
-
 __PACKAGE__->register_method({
     name => 'create_vm',
     path => '',
@@ -708,7 +848,8 @@ __PACKAGE__->register_method({
     permissions => {
        description => "You need 'VM.Allocate' permissions on /vms/{vmid} or on the VM pool /pool/{pool}. " .
            "For restore (option 'archive'), it is enough if the user has 'VM.Backup' permission and the VM already exists. " .
-           "If you create disks you need 'Datastore.AllocateSpace' on any used storage.",
+           "If you create disks you need 'Datastore.AllocateSpace' on any used storage." .
+           "If you use a bridge/vlan, you need 'SDN.Use' on any used bridge/vlan.",
         user => 'all', # check inside
     },
     protected => 1,
@@ -746,8 +887,7 @@ __PACKAGE__->register_method({
                'live-restore' => {
                    optional => 1,
                    type => 'boolean',
-                   description => "Start the VM immediately from the backup and restore in background. PBS only.",
-                   requires => 'archive',
+                   description => "Start the VM immediately while importing or restoring in the background.",
                },
                pool => {
                    optional => 1,
@@ -799,6 +939,9 @@ __PACKAGE__->register_method({
                PVE::Tools::validate_ssh_public_keys($ssh_keys);
        }
 
+       $param->{cpuunits} = PVE::CGroup::clamp_cpu_shares($param->{cpuunits})
+           if defined($param->{cpuunits}); # clamp value depending on cgroup version
+
        PVE::Cluster::check_cfs_quorum();
 
        my $filename = PVE::QemuConfig->config_file($vmid);
@@ -817,29 +960,17 @@ __PACKAGE__->register_method({
            # OK
        } elsif ($archive && $force && (-f $filename) &&
                 $rpcenv->check($authuser, "/vms/$vmid", ['VM.Backup'], 1)) {
-           # OK: user has VM.Backup permissions, and want to restore an existing VM
+           # OK: user has VM.Backup permissions and wants to restore an existing VM
        } else {
            raise_perm_exc();
        }
 
-       if (!$archive) {
-           &$resolve_cdrom_alias($param);
-
-           &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param, $storage);
-
-           &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]);
-
-           &$check_vm_create_serial_perm($rpcenv, $authuser, $vmid, $pool, $param);
-           &$check_vm_create_usb_perm($rpcenv, $authuser, $vmid, $pool, $param);
-
-           &$check_cpu_model_access($rpcenv, $authuser, $param);
-
-           $check_drive_param->($param, $storecfg);
-
-           PVE::QemuServer::add_random_macs($param);
-       } else {
-           my $keystr = join(' ', keys %$param);
-           raise_param_exc({ archive => "option conflicts with other options ($keystr)"}) if $keystr;
+       if ($archive) {
+           for my $opt (sort keys $param->%*) {
+               if (PVE::QemuServer::Drive::is_valid_drivename($opt)) {
+                   raise_param_exc({ $opt => "option conflicts with option 'archive'" });
+               }
+           }
 
            if ($archive eq '-') {
                die "pipe requires cli environment\n" if $rpcenv->{type} ne 'cli';
@@ -858,6 +989,25 @@ __PACKAGE__->register_method({
            }
        }
 
+       if (scalar(keys $param->%*) > 0) {
+           &$resolve_cdrom_alias($param);
+
+           &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param, $storage);
+
+           &$check_vm_modify_config_perm($rpcenv, $authuser, $vmid, $pool, [ keys %$param]);
+
+           &$check_vm_create_serial_perm($rpcenv, $authuser, $vmid, $pool, $param);
+           check_vm_create_usb_perm($rpcenv, $authuser, $vmid, $pool, $param);
+           check_vm_create_hostpci_perm($rpcenv, $authuser, $vmid, $pool, $param);
+
+           PVE::QemuServer::check_bridge_access($rpcenv, $authuser, $param);
+           &$check_cpu_model_access($rpcenv, $authuser, $param);
+
+           $check_drive_param->($param, $storecfg);
+
+           PVE::QemuServer::add_random_macs($param);
+       }
+
        my $emsg = $is_restore ? "unable to restore VM $vmid -" : "unable to create VM $vmid -";
 
        eval { PVE::QemuConfig->create_and_lock_config($vmid, $force) };
@@ -878,7 +1028,21 @@ __PACKAGE__->register_method({
                    unique => $unique,
                    bwlimit => $bwlimit,
                    live => $live_restore,
+                   override_conf => $param,
                };
+               if (my $volid = $archive->{volid}) {
+                   # best effort, real check is after restoring!
+                   my $merged = eval {
+                       my $old_conf = PVE::Storage::extract_vzdump_config($storecfg, $volid);
+                       PVE::QemuServer::restore_merge_config("backup/qemu-server/$vmid.conf", $old_conf, $param);
+                   };
+                   if ($@) {
+                       warn "Could not extract backed up config: $@\n";
+                       warn "Skipping early checks!\n";
+                   } else {
+                       PVE::QemuServer::check_restore_permissions($rpcenv, $authuser, $merged);
+                   }
+               }
                if ($archive->{type} eq 'file' || $archive->{type} eq 'pipe') {
                    die "live-restore is only compatible with backup images from a Proxmox Backup Server\n"
                        if $live_restore;
@@ -897,6 +1061,8 @@ __PACKAGE__->register_method({
                    eval { PVE::QemuServer::template_create($vmid, $restored_conf) };
                    warn $@ if $@;
                }
+
+               PVE::QemuServer::create_ifaces_ipams_ips($restored_conf, $vmid) if $unique;
            };
 
            # ensure no old replication state are exists
@@ -912,6 +1078,8 @@ __PACKAGE__->register_method({
        };
 
        my $createfn = sub {
+           my $live_import_mapping = {};
+
            # ensure no old replication state are exists
            PVE::ReplicationState::delete_guest_states($vmid);
 
@@ -919,11 +1087,16 @@ __PACKAGE__->register_method({
                my $conf = $param;
                my $arch = PVE::QemuServer::get_vm_arch($conf);
 
+               for my $opt (sort keys $param->%*) {
+                   next if $opt !~ m/^scsi\d+$/;
+                   assert_scsi_feature_compatibility($opt, $conf, $storecfg, $param->{$opt});
+               }
+
                $conf->{meta} = PVE::QemuServer::new_meta_info_string();
 
                my $vollist = [];
                eval {
-                   ($vollist, my $created_opts) = $create_disks->(
+                   ($vollist, my $created_opts, $live_import_mapping) = create_disks(
                        $rpcenv,
                        $authuser,
                        $conf,
@@ -933,6 +1106,7 @@ __PACKAGE__->register_method({
                        $pool,
                        $param,
                        $storage,
+                       $live_restore,
                    );
                    $conf->{$_} = $created_opts->{$_} for keys $created_opts->%*;
 
@@ -941,6 +1115,9 @@ __PACKAGE__->register_method({
                        $conf->{boot} = PVE::QemuServer::print_bootorder($devs);
                    }
 
+                   my $vga = PVE::QemuServer::parse_vga($conf->{vga});
+                   PVE::QemuServer::assert_clipboard_config($vga);
+
                    # auto generate uuid if user did not specify smbios1 option
                    if (!$conf->{smbios1}) {
                        $conf->{smbios1} = PVE::QemuServer::generate_smbios1_uuid();
@@ -950,16 +1127,19 @@ __PACKAGE__->register_method({
                        $conf->{vmgenid} = PVE::QemuServer::generate_uuid();
                    }
 
-                   my $machine = $conf->{machine};
+                   my $machine_conf = PVE::QemuServer::Machine::parse_machine($conf->{machine});
+                   my $machine = $machine_conf->{type};
                    if (!$machine || $machine =~ m/^(?:pc|q35|virt)$/) {
                        # always pin Windows' machine version on create, they get to easily confused
-                       if (PVE::QemuServer::windows_version($conf->{ostype})) {
-                           $conf->{machine} = PVE::QemuServer::windows_get_pinned_machine_version($machine);
+                       if (PVE::QemuServer::Helpers::windows_version($conf->{ostype})) {
+                           $machine_conf->{type} = PVE::QemuServer::windows_get_pinned_machine_version($machine);
+                           $conf->{machine} = PVE::QemuServer::Machine::print_machine($machine_conf);
                        }
                    }
 
-                   PVE::QemuConfig->write_config($vmid, $conf);
+                   $conf->{lock} = 'import' if $live_import_mapping;
 
+                   PVE::QemuConfig->write_config($vmid, $conf);
                };
                my $err = $@;
 
@@ -972,14 +1152,19 @@ __PACKAGE__->register_method({
                }
 
                PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool;
+
+               PVE::QemuServer::create_ifaces_ipams_ips($conf, $vmid);
            };
 
            PVE::QemuConfig->lock_config_full($vmid, 1, $realcmd);
 
-           if ($start_after_create) {
+           if ($start_after_create && !$live_restore) {
                print "Execute autostart\n";
                eval { PVE::API2::Qemu->vm_start({vmid => $vmid, node => $node}) };
                warn $@ if $@;
+               return;
+           } else {
+               return $live_import_mapping;
            }
        };
 
@@ -1004,7 +1189,9 @@ __PACKAGE__->register_method({
        } else {
            $worker_name = 'qmcreate';
            $code = sub {
-               eval { $createfn->() };
+               # If a live import was requested the create function returns
+               # the mapping for the startup.
+               my $live_import_mapping = eval { $createfn->() };
                if (my $err = $@) {
                    eval {
                        my $conffile = PVE::QemuConfig->config_file($vmid);
@@ -1013,6 +1200,21 @@ __PACKAGE__->register_method({
                    warn $@ if $@;
                    die $err;
                }
+
+               if ($live_import_mapping) {
+                   my $import_options = {
+                       bwlimit => $bwlimit,
+                       live => 1,
+                   };
+
+                   my $conf = PVE::QemuConfig->load_config($vmid);
+                   PVE::QemuServer::live_import_from_files(
+                       $live_import_mapping,
+                       $vmid,
+                       $conf,
+                       $import_options,
+                   );
+               }
            };
        }
 
@@ -1050,6 +1252,7 @@ __PACKAGE__->register_method({
 
        my $res = [
            { subdir => 'config' },
+           { subdir => 'cloudinit' },
            { subdir => 'pending' },
            { subdir => 'status' },
            { subdir => 'unlink' },
@@ -1066,7 +1269,9 @@ __PACKAGE__->register_method({
            { subdir => 'spiceproxy' },
            { subdir => 'sendkey' },
            { subdir => 'firewall' },
-           ];
+           { subdir => 'mtunnel' },
+           { subdir => 'remote_migrate' },
+       ];
 
        return $res;
     }});
@@ -1287,6 +1492,133 @@ __PACKAGE__->register_method({
        return PVE::GuestHelpers::config_with_pending_array($conf, $pending_delete_hash);
    }});
 
+__PACKAGE__->register_method({
+    name => 'cloudinit_pending',
+    path => '{vmid}/cloudinit',
+    method => 'GET',
+    proxyto => 'node',
+    description => "Get the cloudinit configuration with both current and pending values.",
+    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 }),
+       },
+    },
+    returns => {
+       type => "array",
+       items => {
+           type => "object",
+           properties => {
+               key => {
+                   description => "Configuration option name.",
+                   type => 'string',
+               },
+               value => {
+                   description => "Value as it was used to generate the current cloudinit image.",
+                   type => 'string',
+                   optional => 1,
+               },
+               pending => {
+                   description => "The new pending value.",
+                   type => 'string',
+                   optional => 1,
+               },
+               delete => {
+                   description => "Indicates a pending delete request if present and not 0. ",
+                   type => 'integer',
+                   minimum => 0,
+                   maximum => 1,
+                   optional => 1,
+               },
+           },
+       },
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $vmid = $param->{vmid};
+       my $conf = PVE::QemuConfig->load_config($vmid);
+
+       my $ci = $conf->{cloudinit};
+
+       $conf->{cipassword} = '**********' if exists $conf->{cipassword};
+       $ci->{cipassword} = '**********' if exists $ci->{cipassword};
+
+       my $res = [];
+
+       # All the values that got added
+       my $added = delete($ci->{added}) // '';
+       for my $key (PVE::Tools::split_list($added)) {
+           push @$res, { key => $key, pending => $conf->{$key} };
+       }
+
+       # All already existing values (+ their new value, if it exists)
+       for my $opt (keys %$cloudinitoptions) {
+           next if !$conf->{$opt};
+           next if $added =~ m/$opt/;
+           my $item = {
+               key => $opt,
+           };
+
+           if (my $pending = $ci->{$opt}) {
+               $item->{value} = $pending;
+               $item->{pending} = $conf->{$opt};
+           } else {
+               $item->{value} = $conf->{$opt},
+           }
+
+           push @$res, $item;
+       }
+
+       # Now, we'll find the deleted ones
+       for my $opt (keys %$ci) {
+           next if $conf->{$opt};
+           push @$res, { key => $opt, delete => 1 };
+       }
+
+       return $res;
+   }});
+
+__PACKAGE__->register_method({
+    name => 'cloudinit_update',
+    path => '{vmid}/cloudinit',
+    method => 'PUT',
+    protected => 1,
+    proxyto => 'node',
+    description => "Regenerate and change cloudinit config drive.",
+    permissions => {
+       check => ['perm', '/vms/{vmid}', ['VM.Config.Cloudinit']],
+    },
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           vmid => get_standard_option('pve-vmid'),
+       },
+    },
+    returns => { type => 'null' },
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+       my $authuser = $rpcenv->get_user();
+
+       my $vmid = extract_param($param, 'vmid');
+
+       PVE::QemuConfig->lock_config($vmid, sub {
+           my $conf = PVE::QemuConfig->load_config($vmid);
+           PVE::QemuConfig->check_lock($conf);
+
+           my $storecfg = PVE::Storage::config();
+           PVE::QemuServer::vmconfig_update_cloudinit_drive($storecfg, $conf, $vmid);
+       });
+       return;
+    }});
+
 # POST/PUT {vmid}/config implementation
 #
 # The original API used PUT (idempotent) an we assumed that all operations
@@ -1314,6 +1646,8 @@ my $update_vm_api  = sub {
 
     my $background_delay = extract_param($param, 'background_delay');
 
+    my $skip_cloud_init = extract_param($param, 'skip_cloud_init');
+
     if (defined(my $cipassword = $param->{cipassword})) {
        # Same logic as in cloud-init (but with the regex fixed...)
        $param->{cipassword} = PVE::Tools::encrypt_pw($cipassword)
@@ -1341,12 +1675,13 @@ my $update_vm_api  = sub {
        PVE::Tools::validate_ssh_public_keys($ssh_keys);
     }
 
+    $param->{cpuunits} = PVE::CGroup::clamp_cpu_shares($param->{cpuunits})
+       if defined($param->{cpuunits}); # clamp value depending on cgroup version
+
     die "no options specified\n" if !$delete_str && !$revert_str && !scalar(keys %$param);
 
     my $storecfg = PVE::Storage::config();
 
-    my $defaults = PVE::QemuServer::load_defaults();
-
     &$resolve_cdrom_alias($param);
 
     # now try to verify all parameters
@@ -1399,7 +1734,7 @@ my $update_vm_api  = sub {
        return if defined($volname) && $volname eq 'cloudinit';
 
        my $format;
-       if ($volid =~ $NEW_DISK_RE) {
+       if ($volid =~ $PVE::QemuServer::Drive::NEW_DISK_RE) {
            $storeid = $2;
            $format = $drive->{format} || PVE::Storage::storage_default_format($storecfg, $storeid);
        } else {
@@ -1434,6 +1769,8 @@ my $update_vm_api  = sub {
 
     &$check_storage_access($rpcenv, $authuser, $storecfg, $vmid, $param);
 
+    PVE::QemuServer::check_bridge_access($rpcenv, $authuser, $param);
+
     my $updatefn =  sub {
 
        my $conf = PVE::QemuConfig->load_config($vmid);
@@ -1464,7 +1801,9 @@ my $update_vm_api  = sub {
        }
 
        if ($param->{memory} || defined($param->{balloon})) {
-           my $maxmem = $param->{memory} || $conf->{pending}->{memory} || $conf->{memory} || $defaults->{memory};
+
+           my $memory = $param->{memory} || $conf->{pending}->{memory} || $conf->{memory};
+           my $maxmem = get_current_memory($memory);
            my $balloon = defined($param->{balloon}) ? $param->{balloon} : $conf->{pending}->{balloon} || $conf->{balloon};
 
            die "balloon value too large (must be smaller than assigned memory)\n"
@@ -1491,11 +1830,13 @@ my $update_vm_api  = sub {
            my $check_drive_perms = sub {
                my ($opt, $val) = @_;
                my $drive = PVE::QemuServer::parse_drive($opt, $val, 1);
-               # FIXME: cloudinit: CDROM or Disk?
-               if (PVE::QemuServer::drive_is_cdrom($drive)) { # CDROM
+               if (PVE::QemuServer::drive_is_cloudinit($drive)) {
+                   $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Cloudinit', 'VM.Config.CDROM']);
+               } elsif (PVE::QemuServer::drive_is_cdrom($drive, 1)) { # CDROM
                    $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.CDROM']);
                } else {
                    $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
+
                }
            };
 
@@ -1550,14 +1891,28 @@ my $update_vm_api  = sub {
                    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";
-                   }
+                   check_usb_perm($rpcenv, $authuser, $vmid, undef, $opt, $val);
                    PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force);
                    PVE::QemuConfig->write_config($vmid, $conf);
-               } else {
+               } elsif ($opt =~ m/^hostpci\d+$/) {
+                   check_hostpci_perm($rpcenv, $authuser, $vmid, undef, $opt, $val);
+                   PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force);
+                   PVE::QemuConfig->write_config($vmid, $conf);
+               } elsif ($opt eq 'tags') {
+                   assert_tag_permissions($vmid, $val, '', $rpcenv, $authuser);
+                   delete $conf->{$opt};
+                   PVE::QemuConfig->write_config($vmid, $conf);
+               } elsif ($opt =~ m/^net\d+$/) {
+                   if ($conf->{$opt}) {
+                       PVE::QemuServer::check_bridge_access(
+                           $rpcenv,
+                           $authuser,
+                           { $opt => $conf->{$opt} },
+                       );
+                   }
+                   PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force);
+                   PVE::QemuConfig->write_config($vmid, $conf);
+               } else {
                    PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force);
                    PVE::QemuConfig->write_config($vmid, $conf);
                }
@@ -1581,7 +1936,10 @@ my $update_vm_api  = sub {
                    PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $conf->{pending}->{$opt}))
                        if defined($conf->{pending}->{$opt});
 
-                   my (undef, $created_opts) = $create_disks->(
+                   assert_scsi_feature_compatibility($opt, $conf, $storecfg, $param->{$opt})
+                       if $opt =~ m/^scsi\d+$/;
+
+                   my (undef, $created_opts) = create_disks(
                        $rpcenv,
                        $authuser,
                        $conf,
@@ -1590,6 +1948,8 @@ my $update_vm_api  = sub {
                        $vmid,
                        undef,
                        {$opt => $param->{$opt}},
+                       undef,
+                       undef,
                    );
                    $conf->{pending}->{$_} = $created_opts->{$_} for keys $created_opts->%*;
 
@@ -1610,12 +1970,36 @@ my $update_vm_api  = sub {
                        die "only root can modify '$opt' config for real devices\n";
                    }
                    $conf->{pending}->{$opt} = $param->{$opt};
+               } elsif ($opt eq 'vga') {
+                   my $vga = PVE::QemuServer::parse_vga($param->{$opt});
+                   PVE::QemuServer::assert_clipboard_config($vga);
+                   $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";
+                   if (my $olddevice = $conf->{$opt}) {
+                       check_usb_perm($rpcenv, $authuser, $vmid, undef, $opt, $conf->{$opt});
+                   }
+                   check_usb_perm($rpcenv, $authuser, $vmid, undef, $opt, $param->{$opt});
+                   $conf->{pending}->{$opt} = $param->{$opt};
+               } elsif ($opt =~ m/^hostpci\d+$/) {
+                   if (my $oldvalue = $conf->{$opt}) {
+                       check_hostpci_perm($rpcenv, $authuser, $vmid, undef, $opt, $oldvalue);
                    }
+                   check_hostpci_perm($rpcenv, $authuser, $vmid, undef, $opt, $param->{$opt});
+                   $conf->{pending}->{$opt} = $param->{$opt};
+               } elsif ($opt eq 'tags') {
+                   assert_tag_permissions($vmid, $conf->{$opt}, $param->{$opt}, $rpcenv, $authuser);
+                   $conf->{pending}->{$opt} = PVE::GuestHelpers::get_unique_tags($param->{$opt});
+               } elsif ($opt =~ m/^net\d+$/) {
+                   if ($conf->{$opt}) {
+                       PVE::QemuServer::check_bridge_access(
+                           $rpcenv,
+                           $authuser,
+                           { $opt => $conf->{$opt} },
+                       );
+                   }
+                   $conf->{pending}->{$opt} = $param->{$opt};
+               } elsif ($opt eq 'machine') {
+                   my $machine_conf = PVE::QemuServer::Machine::parse_machine($param->{$opt});
                    $conf->{pending}->{$opt} = $param->{$opt};
                } else {
                    $conf->{pending}->{$opt} = $param->{$opt};
@@ -1659,7 +2043,8 @@ my $update_vm_api  = sub {
            if ($running) {
                PVE::QemuServer::vmconfig_hotplug_pending($vmid, $conf, $storecfg, $modified, $errors);
            } else {
-               PVE::QemuServer::vmconfig_apply_pending($vmid, $conf, $storecfg, $errors);
+               # cloud_init must be skipped if we are in an incoming, remote live migration
+               PVE::QemuServer::vmconfig_apply_pending($vmid, $conf, $storecfg, $errors, $skip_cloud_init);
            }
            raise_param_exc($errors) if scalar(keys %$errors);
 
@@ -2002,7 +2387,8 @@ __PACKAGE__->register_method({
            websocket => {
                optional => 1,
                type => 'boolean',
-               description => "starts websockify instead of vncproxy",
+               description => "Prepare for websocket upgrade (only required when using "
+                   ."serial terminal, otherwise upgrade is always possible).",
            },
            'generate-password' => {
                optional => 1,
@@ -2100,7 +2486,7 @@ __PACKAGE__->register_method({
 
            } else {
 
-               $ENV{LC_PVE_TICKET} = $password if $websocket; # set ticket with "qm vncproxy"
+               $ENV{LC_PVE_TICKET} = $password; # set ticket with "qm vncproxy"
 
                $cmd = [@$remcmd, "/usr/sbin/qm", 'vncproxy', $vmid];
 
@@ -2417,15 +2803,22 @@ __PACKAGE__->register_method({
                type => 'object',
            },
            spice => {
-               description => "Qemu VGA configuration supports spice.",
+               description => "QEMU VGA configuration supports spice.",
                type => 'boolean',
                optional => 1,
            },
            agent => {
-               description => "Qemu GuestAgent enabled in config.",
+               description => "QEMU Guest Agent is enabled in config.",
                type => 'boolean',
                optional => 1,
            },
+           clipboard => {
+               description => 'Enable a specific clipboard. If not set, depending on'
+                   .' the display type the SPICE one will be added.',
+               type => 'string',
+               enum => ['vnc'],
+               optional => 1,
+           },
        },
     },
     code => sub {
@@ -2439,7 +2832,13 @@ __PACKAGE__->register_method({
 
        $status->{ha} = PVE::HA::Config::get_service_status("vm:$param->{vmid}");
 
-       $status->{spice} = 1 if PVE::QemuServer::vga_conf_has_spice($conf->{vga});
+       if ($conf->{vga}) {
+           my $vga = PVE::QemuServer::parse_vga($conf->{vga});
+           my $spice = defined($vga->{type}) && $vga->{type} =~ /^virtio/;
+           $spice ||= PVE::QemuServer::vga_conf_has_spice($conf->{vga});
+           $status->{spice} = 1 if $spice;
+           $status->{clipboard} = $vga->{clipboard};
+       }
        $status->{agent} = 1 if PVE::QemuServer::get_qga_key($conf, 'enabled');
 
        return $status;
@@ -2536,7 +2935,7 @@ __PACKAGE__->register_method({
        my $spice_ticket;
        my $nbd_protocol_version = 0;
        my $replicated_volumes = {};
-       my $tpmstate_vol;
+       my $offline_volumes = {};
        if ($stateuri && ($stateuri eq 'tcp' || $stateuri eq 'unix') && $migratedfrom && ($rpcenv->{type} eq 'cli')) {
            while (defined(my $line = <STDIN>)) {
                chomp $line;
@@ -2546,8 +2945,10 @@ __PACKAGE__->register_method({
                    $nbd_protocol_version = $1;
                } elsif ($line =~ m/^replicated_volume: (.*)$/) {
                    $replicated_volumes->{$1} = 1;
-               } elsif ($line =~ m/^tpmstate0: (.*)$/) {
-                   $tpmstate_vol = $1;
+               } elsif ($line =~ m/^tpmstate0: (.*)$/) { # Deprecated, use offline_volume instead
+                   $offline_volumes->{tpmstate0} = $1;
+               } elsif ($line =~ m/^offline_volume: ([^:]+): (.*)$/) {
+                   $offline_volumes->{$1} = $2;
                } elsif (!$spice_ticket) {
                    # fallback for old source node
                    $spice_ticket = $line;
@@ -2589,7 +2990,7 @@ __PACKAGE__->register_method({
                    storagemap => $storagemap,
                    nbd_proto_version => $nbd_protocol_version,
                    replicated_volumes => $replicated_volumes,
-                   tpmstate_vol => $tpmstate_vol,
+                   offline_volumes => $offline_volumes,
                };
 
                my $params = {
@@ -2809,13 +3210,9 @@ __PACKAGE__->register_method({
 
        my $shutdown = 1;
 
-       # if vm is paused, do not shutdown (but stop if forceStop = 1)
-       # otherwise, we will infer a shutdown command, but run into the timeout,
-       # then when the vm is resumed, it will instantly shutdown
-       #
-       # checking the qmp status here to get feedback to the gui/cli/api
-       # and the status query should not take too long
-       if (PVE::QemuServer::vm_is_paused($vmid)) {
+       # sending a graceful shutdown command to paused VMs runs into timeouts, and even worse, when
+       # the VM gets resumed later, it still gets the request delivered and powers off
+       if (PVE::QemuServer::vm_is_paused($vmid, 1)) {
            if ($param->{forceStop}) {
                warn "VM is paused - stop instead of shutdown\n";
                $shutdown = 0;
@@ -2891,7 +3288,7 @@ __PACKAGE__->register_method({
        my $node = extract_param($param, 'node');
        my $vmid = extract_param($param, 'vmid');
 
-       die "VM is paused - cannot shutdown\n" if PVE::QemuServer::vm_is_paused($vmid);
+       die "VM is paused - cannot shutdown\n" if PVE::QemuServer::vm_is_paused($vmid, 1);
 
        die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
 
@@ -2968,10 +3365,17 @@ __PACKAGE__->register_method({
        # early check for storage permission, for better user feedback
        if ($todisk) {
            $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
+           my $conf = PVE::QemuConfig->load_config($vmid);
+
+           # cannot save the state of a non-virtualized PCIe device, so resume cannot really work
+           for my $key (keys %$conf) {
+               next if $key !~ /^hostpci\d+/;
+               die "cannot suspend VM to disk due to passed-through PCI device(s), which lack the"
+                   ." possibility to save/restore their internal state\n";
+           }
 
            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);
            }
@@ -3032,6 +3436,8 @@ __PACKAGE__->register_method({
        raise_param_exc({ skiplock => "Only root may use this option." })
            if $skiplock && $authuser ne 'root@pam';
 
+       # nocheck is used as part of migration when config file might be still
+       # be on source node
        my $nocheck = extract_param($param, 'nocheck');
        raise_param_exc({ nocheck => "Only root may use this option." })
            if $nocheck && $authuser ne 'root@pam';
@@ -3185,7 +3591,7 @@ __PACKAGE__->register_method({
     permissions => {
        description => "You need 'VM.Clone' permissions on /vms/{vmid}, and 'VM.Allocate' permissions " .
            "on /vms/{newid} (or on the VM pool /pool/{pool}). You also need " .
-           "'Datastore.AllocateSpace' on any used storage.",
+           "'Datastore.AllocateSpace' on any used storage and 'SDN.Use' on any used bridge/vnet",
        check =>
        [ 'and',
          ['perm', '/vms/{vmid}', [ 'VM.Clone' ]],
@@ -3318,6 +3724,9 @@ __PACKAGE__->register_method({
            my $oldconf = $snapname ? $conf->{snapshots}->{$snapname} : $conf;
 
            my $sharedvm = &$check_storage_access_clone($rpcenv, $authuser, $storecfg, $oldconf, $storage);
+           PVE::QemuServer::check_mapping_access($rpcenv, $authuser, $oldconf);
+
+           PVE::QemuServer::check_bridge_access($rpcenv, $authuser, $oldconf);
 
            die "can't clone VM to node '$target' (VM uses local storage)\n"
                if $target && !$sharedvm;
@@ -3482,9 +3891,16 @@ __PACKAGE__->register_method({
 
                PVE::QemuConfig->write_config($newid, $newconf);
 
+               PVE::QemuServer::create_ifaces_ipams_ips($newconf, $newid);
+
                if ($target) {
-                   # always deactivate volumes - avoid lvm LVs to be active on several nodes
-                   PVE::Storage::deactivate_volumes($storecfg, $vollist, $snapname) if !$running;
+                   if (!$running) {
+                       # always deactivate volumes â€“ avoids that LVM LVs are active on several nodes
+                       eval { PVE::Storage::deactivate_volumes($storecfg, $vollist, $snapname) };
+                       # but only warn when that fails (e.g., parallel clones keeping them active)
+                       log_warn($@) if $@;
+                   }
+
                    PVE::Storage::deactivate_volumes($storecfg, $newvollist);
 
                    my $newconffile = PVE::QemuConfig->config_file($newid, $target);
@@ -3871,6 +4287,13 @@ __PACKAGE__->register_method({
 
                    $drive->{file} = $new_volid;
 
+                   my $boot_order = PVE::QemuServer::device_bootorder($source_conf);
+                   if (defined(delete $boot_order->{$disk})) {
+                       print "removing disk '$disk' from boot order config\n";
+                       my $boot_devs = [ sort { $boot_order->{$a} <=> $boot_order->{$b} } keys %$boot_order ];
+                       $source_conf->{boot} = PVE::QemuServer::print_bootorder($boot_devs);
+                   }
+
                    delete $source_conf->{$disk};
                    print "removing disk '${disk}' from VM '${vmid}' config\n";
                    PVE::QemuConfig->write_config($vmid, $source_conf);
@@ -4030,7 +4453,11 @@ __PACKAGE__->register_method({
            local_resources => {
                type => 'array',
                description => "List local resources e.g. pci, usb"
-           }
+           },
+           'mapped-resources' => {
+               type => 'array',
+               description => "List of mapped resources e.g. pci, usb"
+           },
        },
     },
     code => sub {
@@ -4059,7 +4486,16 @@ __PACKAGE__->register_method({
 
        $res->{running} = PVE::QemuServer::check_running($vmid) ? 1:0;
 
-       # if vm is not running, return target nodes where local storage is available
+       my ($local_resources, $mapped_resources, $missing_mappings_by_node) =
+           PVE::QemuServer::check_local_resources($vmconf, 1);
+       delete $missing_mappings_by_node->{$localnode};
+
+       my $vga = PVE::QemuServer::parse_vga($vmconf->{vga});
+       if ($res->{running} && $vga->{'clipboard'} && $vga->{'clipboard'} eq 'vnc') {
+           push $local_resources->@*, "clipboard=vnc";
+       }
+
+       # if vm is not running, return target nodes where local storage/mapped devices are available
        # for offline migration
        if (!$res->{running}) {
            $res->{allowed_nodes} = [];
@@ -4067,7 +4503,13 @@ __PACKAGE__->register_method({
            delete $checked_nodes->{$localnode};
 
            foreach my $node (keys %$checked_nodes) {
-               if (!defined $checked_nodes->{$node}->{unavailable_storages}) {
+               my $missing_mappings = $missing_mappings_by_node->{$node};
+               if (scalar($missing_mappings->@*)) {
+                   $checked_nodes->{$node}->{'unavailable-resources'} = $missing_mappings;
+                   next;
+               }
+
+               if (!defined($checked_nodes->{$node}->{unavailable_storages})) {
                    push @{$res->{allowed_nodes}}, $node;
                }
 
@@ -4075,13 +4517,11 @@ __PACKAGE__->register_method({
            $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;
+       $res->{'mapped-resources'} = $mapped_resources;
 
        return $res;
 
@@ -4253,13 +4693,178 @@ __PACKAGE__->register_method({
 
     }});
 
+__PACKAGE__->register_method({
+    name => 'remote_migrate_vm',
+    path => '{vmid}/remote_migrate',
+    method => 'POST',
+    protected => 1,
+    proxyto => 'node',
+    description => "Migrate virtual machine to a remote cluster. Creates a new migration task. EXPERIMENTAL feature!",
+    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-vmid' => get_standard_option('pve-vmid', { optional => 1 }),
+           'target-endpoint' => get_standard_option('proxmox-remote', {
+               description => "Remote target endpoint",
+           }),
+           online => {
+               type => 'boolean',
+               description => "Use online/live migration if VM is running. Ignored if VM is stopped.",
+               optional => 1,
+           },
+           delete => {
+               type => 'boolean',
+               description => "Delete the original VM and related data after successful migration. By default the original VM is kept on the source cluster in a stopped state.",
+               optional => 1,
+               default => 0,
+           },
+           'target-storage' => get_standard_option('pve-targetstorage', {
+               completion => \&PVE::QemuServer::complete_migration_storage,
+               optional => 0,
+           }),
+           'target-bridge' => {
+               type => 'string',
+               description => "Mapping from source to target bridges. Providing only a single bridge ID maps all source bridges to that bridge. Providing the special value '1' will map each source bridge to itself.",
+               format => 'bridge-pair-list',
+           },
+           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 => {
+       type => 'string',
+       description => "the task ID.",
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+       my $authuser = $rpcenv->get_user();
+
+       my $source_vmid = extract_param($param, 'vmid');
+       my $target_endpoint = extract_param($param, 'target-endpoint');
+       my $target_vmid = extract_param($param, 'target-vmid') // $source_vmid;
+
+       my $delete = extract_param($param, 'delete') // 0;
+
+       PVE::Cluster::check_cfs_quorum();
+
+       # test if VM exists
+       my $conf = PVE::QemuConfig->load_config($source_vmid);
+
+       PVE::QemuConfig->check_lock($conf);
+
+       raise_param_exc({ vmid => "cannot migrate HA-managed VM to remote cluster" })
+           if PVE::HA::Config::vm_is_ha_managed($source_vmid);
+
+       my $remote = PVE::JSONSchema::parse_property_string('proxmox-remote', $target_endpoint);
+
+       # TODO: move this as helper somewhere appropriate?
+       my $conn_args = {
+           protocol => 'https',
+           host => $remote->{host},
+           port => $remote->{port} // 8006,
+           apitoken => $remote->{apitoken},
+       };
+
+       my $fp;
+       if ($fp = $remote->{fingerprint}) {
+           $conn_args->{cached_fingerprints} = { uc($fp) => 1 };
+       }
+
+       print "Establishing API connection with remote at '$remote->{host}'\n";
+
+       my $api_client = PVE::APIClient::LWP->new(%$conn_args);
+
+       if (!defined($fp)) {
+           my $cert_info = $api_client->get("/nodes/localhost/certificates/info");
+           foreach my $cert (@$cert_info) {
+               my $filename = $cert->{filename};
+               next if $filename ne 'pveproxy-ssl.pem' && $filename ne 'pve-ssl.pem';
+               $fp = $cert->{fingerprint} if !$fp || $filename eq 'pveproxy-ssl.pem';
+           }
+           $conn_args->{cached_fingerprints} = { uc($fp) => 1 }
+               if defined($fp);
+       }
+
+       my $repl_conf = PVE::ReplicationConfig->new();
+       my $is_replicated = $repl_conf->check_for_existing_jobs($source_vmid, 1);
+       die "cannot remote-migrate replicated VM\n" if $is_replicated;
+
+       if (PVE::QemuServer::check_running($source_vmid)) {
+           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;
+       }
+
+       my $storecfg = PVE::Storage::config();
+       my $target_storage = extract_param($param, 'target-storage');
+       my $storagemap = eval { PVE::JSONSchema::parse_idmap($target_storage, 'pve-storage-id') };
+       raise_param_exc({ 'target-storage' => "failed to parse storage map: $@" })
+           if $@;
+
+       my $target_bridge = extract_param($param, 'target-bridge');
+       my $bridgemap = eval { PVE::JSONSchema::parse_idmap($target_bridge, 'pve-bridge-id') };
+       raise_param_exc({ 'target-bridge' => "failed to parse bridge map: $@" })
+           if $@;
+
+       die "remote migration requires explicit storage mapping!\n"
+           if $storagemap->{identity};
+
+       $param->{storagemap} = $storagemap;
+       $param->{bridgemap} = $bridgemap;
+       $param->{remote} = {
+           conn => $conn_args, # re-use fingerprint for tunnel
+           client => $api_client,
+           vmid => $target_vmid,
+       };
+       $param->{migration_type} = 'websocket';
+       $param->{'with-local-disks'} = 1;
+       $param->{delete} = $delete if $delete;
+
+       my $cluster_status = $api_client->get("/cluster/status");
+       my $target_node;
+       foreach my $entry (@$cluster_status) {
+           next if $entry->{type} ne 'node';
+           if ($entry->{local}) {
+               $target_node = $entry->{name};
+               last;
+           }
+       }
+
+       die "couldn't determine endpoint's node name\n"
+           if !defined($target_node);
+
+       my $realcmd = sub {
+           PVE::QemuMigrate->migrate($target_node, $remote->{host}, $source_vmid, $param);
+       };
+
+       my $worker = sub {
+           return PVE::GuestHelpers::guest_migration_lock($source_vmid, 10, $realcmd);
+       };
+
+       return $rpcenv->fork_worker('qmigrate', $source_vmid, $authuser, $worker);
+    }});
+
 __PACKAGE__->register_method({
     name => 'monitor',
     path => '{vmid}/monitor',
     method => 'POST',
     protected => 1,
     proxyto => 'node',
-    description => "Execute Qemu monitor commands.",
+    description => "Execute QEMU monitor commands.",
     permissions => {
        description => "Sys.Modify is required for (sub)commands which are not read-only ('info *' and 'help')",
         check => ['perm', '/vms/{vmid}', [ 'VM.Monitor' ]],
@@ -4338,7 +4943,10 @@ __PACKAGE__->register_method({
            },
        },
     },
-    returns => { type => 'null'},
+    returns => {
+       type => 'string',
+       description => "the task ID.",
+    },
     code => sub {
         my ($param) = @_;
 
@@ -4377,9 +4985,6 @@ __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"
-               if %{$conf->{snapshots}} && $format eq 'qcow2';
-
            my $volid = $drive->{file};
 
            die "disk '$disk' has no associated volume\n" if !$volid;
@@ -4425,8 +5030,11 @@ __PACKAGE__->register_method({
            PVE::QemuConfig->write_config($vmid, $conf);
        };
 
-        PVE::QemuConfig->lock_config($vmid, $updatefn);
-        return;
+       my $worker = sub {
+           PVE::QemuConfig->lock_config($vmid, $updatefn);
+       };
+
+       return $rpcenv->fork_worker('resize', $vmid, $authuser, $worker);
     }});
 
 __PACKAGE__->register_method({
@@ -4722,6 +5330,13 @@ __PACKAGE__->register_method({
            node => get_standard_option('pve-node'),
            vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
            snapname => get_standard_option('pve-snapshot-name'),
+           start => {
+               type => 'boolean',
+               description => "Whether the VM should get started after rolling back successfully."
+                   . " (Note: VMs will be automatically started if the snapshot includes RAM.)",
+               optional => 1,
+               default => 0,
+           },
        },
     },
     returns => {
@@ -4744,6 +5359,10 @@ __PACKAGE__->register_method({
        my $realcmd = sub {
            PVE::Cluster::log_msg('info', $authuser, "rollback snapshot VM $vmid: $snapname");
            PVE::QemuConfig->snapshot_rollback($vmid, $snapname);
+
+           if ($param->{start} && !PVE::QemuServer::Helpers::vm_running_locally($vmid)) {
+               PVE::API2::Qemu->vm_start({ vmid => $vmid, node => $node });
+           }
        };
 
        my $worker = sub {
@@ -4923,4 +5542,537 @@ __PACKAGE__->register_method({
        return PVE::QemuServer::Cloudinit::dump_cloudinit_config($conf, $param->{vmid}, $param->{type});
     }});
 
+__PACKAGE__->register_method({
+    name => 'mtunnel',
+    path => '{vmid}/mtunnel',
+    method => 'POST',
+    protected => 1,
+    description => 'Migration tunnel endpoint - only for internal use by VM migration.',
+    permissions => {
+       check =>
+       [ 'and',
+         ['perm', '/vms/{vmid}', [ 'VM.Allocate' ]],
+         ['perm', '/', [ 'Sys.Incoming' ]],
+       ],
+       description => "You need 'VM.Allocate' permissions on '/vms/{vmid}' and Sys.Incoming" .
+                      " on '/'. Further permission checks happen during the actual migration.",
+    },
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           vmid => get_standard_option('pve-vmid'),
+           storages => {
+               type => 'string',
+               format => 'pve-storage-id-list',
+               optional => 1,
+               description => 'List of storages to check permission and availability. Will be checked again for all actually used storages during migration.',
+           },
+           bridges => {
+               type => 'string',
+               format => 'pve-bridge-id-list',
+               optional => 1,
+               description => 'List of network bridges to check availability. Will be checked again for actually used bridges during migration.',
+           },
+       },
+    },
+    returns => {
+       additionalProperties => 0,
+       properties => {
+           upid => { type => 'string' },
+           ticket => { type => 'string' },
+           socket => { 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 $storages = extract_param($param, 'storages');
+       my $bridges = extract_param($param, 'bridges');
+
+       my $nodename = PVE::INotify::nodename();
+
+       raise_param_exc({ node => "node needs to be 'localhost' or local hostname '$nodename'" })
+           if $node ne 'localhost' && $node ne $nodename;
+
+       $node = $nodename;
+
+       my $storecfg = PVE::Storage::config();
+       foreach my $storeid (PVE::Tools::split_list($storages)) {
+           $check_storage_access_migrate->($rpcenv, $authuser, $storecfg, $storeid, $node);
+       }
+
+       foreach my $bridge (PVE::Tools::split_list($bridges)) {
+           PVE::Network::read_bridge_mtu($bridge);
+       }
+
+       PVE::Cluster::check_cfs_quorum();
+
+       my $lock = 'create';
+       eval { PVE::QemuConfig->create_and_lock_config($vmid, 0, $lock); };
+
+       raise_param_exc({ vmid => "unable to create empty VM config - $@"})
+           if $@;
+
+       my $realcmd = sub {
+           my $state = {
+               storecfg => PVE::Storage::config(),
+               lock => $lock,
+               vmid => $vmid,
+           };
+
+           my $run_locked = sub {
+               my ($code, $params) = @_;
+               return PVE::QemuConfig->lock_config($state->{vmid}, sub {
+                   my $conf = PVE::QemuConfig->load_config($state->{vmid});
+
+                   $state->{conf} = $conf;
+
+                   die "Encountered wrong lock - aborting mtunnel command handling.\n"
+                       if $state->{lock} && !PVE::QemuConfig->has_lock($conf, $state->{lock});
+
+                   return $code->($params);
+               });
+           };
+
+           my $cmd_desc = {
+               config => {
+                   conf => {
+                       type => 'string',
+                       description => 'Full VM config, adapted for target cluster/node',
+                   },
+                   'firewall-config' => {
+                       type => 'string',
+                       description => 'VM firewall config',
+                       optional => 1,
+                   },
+               },
+               disk => {
+                   format => PVE::JSONSchema::get_standard_option('pve-qm-image-format'),
+                   storage => {
+                       type => 'string',
+                       format => 'pve-storage-id',
+                   },
+                   drive => {
+                       type => 'object',
+                       description => 'parsed drive information without volid and format',
+                   },
+               },
+               start => {
+                   start_params => {
+                       type => 'object',
+                       description => 'params passed to vm_start_nolock',
+                   },
+                   migrate_opts => {
+                       type => 'object',
+                       description => 'migrate_opts passed to vm_start_nolock',
+                   },
+               },
+               ticket => {
+                   path => {
+                       type => 'string',
+                       description => 'socket path for which the ticket should be valid. must be known to current mtunnel instance.',
+                   },
+               },
+               quit => {
+                   cleanup => {
+                       type => 'boolean',
+                       description => 'remove VM config and disks, aborting migration',
+                       default => 0,
+                   },
+               },
+               'disk-import' => $PVE::StorageTunnel::cmd_schema->{'disk-import'},
+               'query-disk-import' => $PVE::StorageTunnel::cmd_schema->{'query-disk-import'},
+               bwlimit => $PVE::StorageTunnel::cmd_schema->{bwlimit},
+           };
+
+           my $cmd_handlers = {
+               'version' => sub {
+                   # compared against other end's version
+                   # bump/reset for breaking changes
+                   # bump/bump for opt-in changes
+                   return {
+                       api => $PVE::QemuMigrate::WS_TUNNEL_VERSION,
+                       age => 0,
+                   };
+               },
+               'config' => sub {
+                   my ($params) = @_;
+
+                   # parse and write out VM FW config if given
+                   if (my $fw_conf = $params->{'firewall-config'}) {
+                       my ($path, $fh) = PVE::Tools::tempfile_contents($fw_conf, 700);
+
+                       my $empty_conf = {
+                           rules => [],
+                           options => {},
+                           aliases => {},
+                           ipset => {} ,
+                           ipset_comments => {},
+                       };
+                       my $cluster_fw_conf = PVE::Firewall::load_clusterfw_conf();
+
+                       # TODO: add flag for strict parsing?
+                       # TODO: add import sub that does all this given raw content?
+                       my $vmfw_conf = PVE::Firewall::generic_fw_config_parser($path, $cluster_fw_conf, $empty_conf, 'vm');
+                       $vmfw_conf->{vmid} = $state->{vmid};
+                       PVE::Firewall::save_vmfw_conf($state->{vmid}, $vmfw_conf);
+
+                       $state->{cleanup}->{fw} = 1;
+                   }
+
+                   my $conf_fn = "incoming/qemu-server/$state->{vmid}.conf";
+                   my $new_conf = PVE::QemuServer::parse_vm_config($conf_fn, $params->{conf}, 1);
+                   delete $new_conf->{lock};
+                   delete $new_conf->{digest};
+
+                   # TODO handle properly?
+                   delete $new_conf->{snapshots};
+                   delete $new_conf->{parent};
+                   delete $new_conf->{pending};
+
+                   # not handled by update_vm_api
+                   my $vmgenid = delete $new_conf->{vmgenid};
+                   my $meta = delete $new_conf->{meta};
+                   my $cloudinit = delete $new_conf->{cloudinit}; # this is informational only
+                   $new_conf->{skip_cloud_init} = 1; # re-use image from source side
+
+                   $new_conf->{vmid} = $state->{vmid};
+                   $new_conf->{node} = $node;
+
+                   PVE::QemuConfig->remove_lock($state->{vmid}, 'create');
+
+                   eval {
+                       $update_vm_api->($new_conf, 1);
+                   };
+                   if (my $err = $@) {
+                       # revert to locked previous config
+                       my $conf = PVE::QemuConfig->load_config($state->{vmid});
+                       $conf->{lock} = 'create';
+                       PVE::QemuConfig->write_config($state->{vmid}, $conf);
+
+                       die $err;
+                   }
+
+                   my $conf = PVE::QemuConfig->load_config($state->{vmid});
+                   $conf->{lock} = 'migrate';
+                   $conf->{vmgenid} = $vmgenid if defined($vmgenid);
+                   $conf->{meta} = $meta if defined($meta);
+                   $conf->{cloudinit} = $cloudinit if defined($cloudinit);
+                   PVE::QemuConfig->write_config($state->{vmid}, $conf);
+
+                   $state->{lock} = 'migrate';
+
+                   return;
+               },
+               'bwlimit' => sub {
+                   my ($params) = @_;
+                   return PVE::StorageTunnel::handle_bwlimit($params);
+               },
+               'disk' => sub {
+                   my ($params) = @_;
+
+                   my $format = $params->{format};
+                   my $storeid = $params->{storage};
+                   my $drive = $params->{drive};
+
+                   $check_storage_access_migrate->($rpcenv, $authuser, $state->{storecfg}, $storeid, $node);
+
+                   my $storagemap = {
+                       default => $storeid,
+                   };
+
+                   my $source_volumes = {
+                       'disk' => [
+                           undef,
+                           $storeid,
+                           $drive,
+                           0,
+                           $format,
+                       ],
+                   };
+
+                   my $res = PVE::QemuServer::vm_migrate_alloc_nbd_disks($state->{storecfg}, $state->{vmid}, $source_volumes, $storagemap);
+                   if (defined($res->{disk})) {
+                       $state->{cleanup}->{volumes}->{$res->{disk}->{volid}} = 1;
+                       return $res->{disk};
+                   } else {
+                       die "failed to allocate NBD disk..\n";
+                   }
+               },
+               'disk-import' => sub {
+                   my ($params) = @_;
+
+                   $check_storage_access_migrate->(
+                       $rpcenv,
+                       $authuser,
+                       $state->{storecfg},
+                       $params->{storage},
+                       $node
+                   );
+
+                   $params->{unix} = "/run/qemu-server/$state->{vmid}.storage";
+
+                   return PVE::StorageTunnel::handle_disk_import($state, $params);
+               },
+               'query-disk-import' => sub {
+                   my ($params) = @_;
+
+                   return PVE::StorageTunnel::handle_query_disk_import($state, $params);
+               },
+               'start' => sub {
+                   my ($params) = @_;
+
+                   my $info = PVE::QemuServer::vm_start_nolock(
+                       $state->{storecfg},
+                       $state->{vmid},
+                       $state->{conf},
+                       $params->{start_params},
+                       $params->{migrate_opts},
+                   );
+
+
+                   if ($info->{migrate}->{proto} ne 'unix') {
+                       PVE::QemuServer::vm_stop(undef, $state->{vmid}, 1, 1);
+                       die "migration over non-UNIX sockets not possible\n";
+                   }
+
+                   my $socket = $info->{migrate}->{addr};
+                   chown $state->{socket_uid}, -1, $socket;
+                   $state->{sockets}->{$socket} = 1;
+
+                   my $unix_sockets = $info->{migrate}->{unix_sockets};
+                   foreach my $socket (@$unix_sockets) {
+                       chown $state->{socket_uid}, -1, $socket;
+                       $state->{sockets}->{$socket} = 1;
+                   }
+                   return $info;
+               },
+               'fstrim' => sub {
+                   if (PVE::QemuServer::qga_check_running($state->{vmid})) {
+                       eval { mon_cmd($state->{vmid}, "guest-fstrim") };
+                       warn "fstrim failed: $@\n" if $@;
+                   }
+                   return;
+               },
+               'stop' => sub {
+                   PVE::QemuServer::vm_stop(undef, $state->{vmid}, 1, 1);
+                   return;
+               },
+               'nbdstop' => sub {
+                   PVE::QemuServer::nbd_stop($state->{vmid});
+                   return;
+               },
+               'resume' => sub {
+                   if (PVE::QemuServer::Helpers::vm_running_locally($state->{vmid})) {
+                       PVE::QemuServer::vm_resume($state->{vmid}, 1, 1);
+                   } else {
+                       die "VM $state->{vmid} not running\n";
+                   }
+                   return;
+               },
+               'unlock' => sub {
+                   PVE::QemuConfig->remove_lock($state->{vmid}, $state->{lock});
+                   delete $state->{lock};
+                   return;
+               },
+               'ticket' => sub {
+                   my ($params) = @_;
+
+                   my $path = $params->{path};
+
+                   die "Not allowed to generate ticket for unknown socket '$path'\n"
+                       if !defined($state->{sockets}->{$path});
+
+                   return { ticket => PVE::AccessControl::assemble_tunnel_ticket($authuser, "/socket/$path") };
+               },
+               'quit' => sub {
+                   my ($params) = @_;
+
+                   if ($params->{cleanup}) {
+                       if ($state->{cleanup}->{fw}) {
+                           PVE::Firewall::remove_vmfw_conf($state->{vmid});
+                       }
+
+                       for my $volid (keys $state->{cleanup}->{volumes}->%*) {
+                           print "freeing volume '$volid' as part of cleanup\n";
+                           eval { PVE::Storage::vdisk_free($state->{storecfg}, $volid) };
+                           warn $@ if $@;
+                       }
+
+                       PVE::QemuServer::destroy_vm($state->{storecfg}, $state->{vmid}, 1);
+                   }
+
+                   print "switching to exit-mode, waiting for client to disconnect\n";
+                   $state->{exit} = 1;
+                   return;
+               },
+           };
+
+           $run_locked->(sub {
+               my $socket_addr = "/run/qemu-server/$state->{vmid}.mtunnel";
+               unlink $socket_addr;
+
+               $state->{socket} = IO::Socket::UNIX->new(
+                   Type => SOCK_STREAM(),
+                   Local => $socket_addr,
+                   Listen => 1,
+               );
+
+               $state->{socket_uid} = getpwnam('www-data')
+                   or die "Failed to resolve user 'www-data' to numeric UID\n";
+               chown $state->{socket_uid}, -1, $socket_addr;
+           });
+
+           print "mtunnel started\n";
+
+           my $conn = eval { PVE::Tools::run_with_timeout(300, sub { $state->{socket}->accept() }) };
+           if ($@) {
+               warn "Failed to accept tunnel connection - $@\n";
+
+               warn "Removing tunnel socket..\n";
+               unlink $state->{socket};
+
+               warn "Removing temporary VM config..\n";
+               $run_locked->(sub {
+                   PVE::QemuServer::destroy_vm($state->{storecfg}, $state->{vmid}, 1);
+               });
+
+               die "Exiting mtunnel\n";
+           }
+
+           $state->{conn} = $conn;
+
+           my $reply_err = sub {
+               my ($msg) = @_;
+
+               my $reply = JSON::encode_json({
+                   success => JSON::false,
+                   msg => $msg,
+               });
+               $conn->print("$reply\n");
+               $conn->flush();
+           };
+
+           my $reply_ok = sub {
+               my ($res) = @_;
+
+               $res->{success} = JSON::true;
+               my $reply = JSON::encode_json($res);
+               $conn->print("$reply\n");
+               $conn->flush();
+           };
+
+           while (my $line = <$conn>) {
+               chomp $line;
+
+               # untaint, we validate below if needed
+               ($line) = $line =~ /^(.*)$/;
+               my $parsed = eval { JSON::decode_json($line) };
+               if ($@) {
+                   $reply_err->("failed to parse command - $@");
+                   next;
+               }
+
+               my $cmd = delete $parsed->{cmd};
+               if (!defined($cmd)) {
+                   $reply_err->("'cmd' missing");
+               } elsif ($state->{exit}) {
+                   $reply_err->("tunnel is in exit-mode, processing '$cmd' cmd not possible");
+                   next;
+               } elsif (my $handler = $cmd_handlers->{$cmd}) {
+                   print "received command '$cmd'\n";
+                   eval {
+                       if ($cmd_desc->{$cmd}) {
+                           PVE::JSONSchema::validate($parsed, $cmd_desc->{$cmd});
+                       } else {
+                           $parsed = {};
+                       }
+                       my $res = $run_locked->($handler, $parsed);
+                       $reply_ok->($res);
+                   };
+                   $reply_err->("failed to handle '$cmd' command - $@")
+                       if $@;
+               } else {
+                   $reply_err->("unknown command '$cmd' given");
+               }
+           }
+
+           if ($state->{exit}) {
+               print "mtunnel exited\n";
+           } else {
+               die "mtunnel exited unexpectedly\n";
+           }
+       };
+
+       my $socket_addr = "/run/qemu-server/$vmid.mtunnel";
+       my $ticket = PVE::AccessControl::assemble_tunnel_ticket($authuser, "/socket/$socket_addr");
+       my $upid = $rpcenv->fork_worker('qmtunnel', $vmid, $authuser, $realcmd);
+
+       return {
+           ticket => $ticket,
+           upid => $upid,
+           socket => $socket_addr,
+       };
+    }});
+
+__PACKAGE__->register_method({
+    name => 'mtunnelwebsocket',
+    path => '{vmid}/mtunnelwebsocket',
+    method => 'GET',
+    permissions => {
+       description => "You need to pass a ticket valid for the selected socket. Tickets can be created via the mtunnel API call, which will check permissions accordingly.",
+        user => 'all', # check inside
+    },
+    description => 'Migration tunnel endpoint for websocket upgrade - only for internal use by VM migration.',
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           vmid => get_standard_option('pve-vmid'),
+           socket => {
+               type => "string",
+               description => "unix socket to forward to",
+           },
+           ticket => {
+               type => "string",
+               description => "ticket return by initial 'mtunnel' API call, or retrieved via 'ticket' tunnel command",
+           },
+       },
+    },
+    returns => {
+       type => "object",
+       properties => {
+           port => { type => 'string', optional => 1 },
+           socket => { type => 'string', optional => 1 },
+       },
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+       my $authuser = $rpcenv->get_user();
+
+       my $nodename = PVE::INotify::nodename();
+       my $node = extract_param($param, 'node');
+
+       raise_param_exc({ node => "node needs to be 'localhost' or local hostname '$nodename'" })
+           if $node ne 'localhost' && $node ne $nodename;
+
+       my $vmid = $param->{vmid};
+       # check VM exists
+       PVE::QemuConfig->load_config($vmid);
+
+       my $socket = $param->{socket};
+       PVE::AccessControl::verify_tunnel_ticket($param->{ticket}, $authuser, "/socket/$socket");
+
+       return { socket => $socket };
+    }});
+
 1;