]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/QemuServer.pm
enable hotplug by default
[qemu-server.git] / PVE / QemuServer.pm
index 0104c2748e6473b07a5e5c456d9a04973bac39be..dea7b6c084c711b777be771e6b03692901813a55 100644 (file)
@@ -22,7 +22,7 @@ use PVE::SafeSyslog;
 use Storable qw(dclone);
 use PVE::Exception qw(raise raise_param_exc);
 use PVE::Storage;
-use PVE::Tools qw(run_command lock_file lock_file_full file_read_firstline);
+use PVE::Tools qw(run_command lock_file lock_file_full file_read_firstline dir_glob_foreach);
 use PVE::JSONSchema qw(get_standard_option);
 use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file);
 use PVE::INotify;
@@ -152,6 +152,12 @@ mkdir $lock_dir;
 my $pcisysfs = "/sys/bus/pci";
 
 my $confdesc = {
+    iothread => {
+       optional => 1,
+       type => 'boolean',
+       description => "Enable iothread dataplane.",
+       default => 0,
+    },
     onboot => {
        optional => 1,
        type => 'boolean',
@@ -168,7 +174,7 @@ my $confdesc = {
         optional => 1,
         type => 'boolean',
         description => "Allow hotplug for disk and network device",
-        default => 0,
+        default => 1,
     },
     reboot => {
        optional => 1,
@@ -300,6 +306,19 @@ EODESC
        minimum => 1,
        default => 1,
     },
+    numa => {
+       optional => 1,
+       type => 'boolean',
+       description => "Enable/disable Numa.",
+       default => 0,
+    },
+    maxcpus => {
+       optional => 1,
+       type => 'integer',
+       description => "Maximum cpus for hotplug.",
+       minimum => 1,
+       default => 1,
+    },
     acpi => {
        optional => 1,
        type => 'boolean',
@@ -405,7 +424,7 @@ EODESCR
        optional => 1,
        description => "Emulated CPU type.",
        type => 'string',
-       enum => [ qw(486 athlon pentium pentium2 pentium3 coreduo core2duo kvm32 kvm64 qemu32 qemu64 phenom Conroe Penryn Nehalem Westmere SandyBridge Haswell Opteron_G1 Opteron_G2 Opteron_G3 Opteron_G4 Opteron_G5 host) ],
+       enum => [ qw(486 athlon pentium pentium2 pentium3 coreduo core2duo kvm32 kvm64 qemu32 qemu64 phenom Conroe Penryn Nehalem Westmere SandyBridge Haswell Broadwell Opteron_G1 Opteron_G2 Opteron_G3 Opteron_G4 Opteron_G5 host) ],
        default => 'kvm64',
     },
     parent => get_standard_option('pve-snapshot-name', {
@@ -430,6 +449,13 @@ EODESCR
        maxLength => 40,
        optional => 1,
     },
+    smbios1 => {
+       description => "Specify SMBIOS type 1 fields.",
+       type => 'string', format => 'pve-qm-smbios1',
+       typetext => "[manufacturer=str][,product=str][,version=str][,serial=str] [,uuid=uuid][,sku=str][,family=str]",
+       maxLength => 256,
+       optional => 1,
+    },
 };
 
 # what about other qemu settings ?
@@ -460,18 +486,32 @@ my $MAX_SATA_DISKS = 6;
 my $MAX_USB_DEVICES = 5;
 my $MAX_NETS = 32;
 my $MAX_UNUSED_DISKS = 8;
-my $MAX_HOSTPCI_DEVICES = 2;
+my $MAX_HOSTPCI_DEVICES = 4;
 my $MAX_SERIAL_PORTS = 4;
 my $MAX_PARALLEL_PORTS = 3;
+my $MAX_NUMA = 8;
+
+my $numadesc = {
+    optional => 1,
+    type => 'string', format => 'pve-qm-numanode',
+    typetext => "cpus=<id[-id],memory=<mb>[[,hostnodes=<id[-id]>] [,policy=<preferred|bind|interleave>]]",
+    description => "numa topology",
+};
+PVE::JSONSchema::register_standard_option("pve-qm-numanode", $numadesc);
+
+for (my $i = 0; $i < $MAX_NUMA; $i++)  {
+    $confdesc->{"numa$i"} = $numadesc;
+}
 
 my $nic_model_list = ['rtl8139', 'ne2k_pci', 'e1000',  'pcnet',  'virtio',
-                     'ne2k_isa', 'i82551', 'i82557b', 'i82559er', 'vmxnet3'];
+                     'ne2k_isa', 'i82551', 'i82557b', 'i82559er', 'vmxnet3',
+                     'e1000-82540em', 'e1000-82544gc', 'e1000-82545em'];
 my $nic_model_list_txt = join(' ', sort @$nic_model_list);
 
 my $netdesc = {
     optional => 1,
     type => 'string', format => 'pve-qm-net',
-    typetext => "MODEL=XX:XX:XX:XX:XX:XX [,bridge=<dev>][,rate=<mbps>][,tag=<vlanid>]",
+    typetext => "MODEL=XX:XX:XX:XX:XX:XX [,bridge=<dev>][,queues=<nbqueues>][,rate=<mbps>] [,tag=<vlanid>][,firewall=0|1],link_down=0|1]",
     description => <<EODESCR,
 Specify network devices.
 
@@ -558,7 +598,7 @@ PVE::JSONSchema::register_standard_option("pve-qm-usb", $usbdesc);
 my $hostpcidesc = {
         optional => 1,
         type => 'string', format => 'pve-qm-hostpci',
-        typetext => "HOSTPCIDEVICE",
+        typetext => "[host=]HOSTPCIDEVICE [,driver=kvm|vfio] [,rombar=on|off] [,pcie=0|1] [,x-vga=on|off]",
         description => <<EODESCR,
 Map host pci devices. HOSTPCIDEVICE syntax is:
 
@@ -566,6 +606,8 @@ Map host pci devices. HOSTPCIDEVICE syntax is:
 
 You can us the 'lspci' command to list existing pci devices.
 
+The 'rombar' option determines whether or not the device's ROM will be visible in the guest's memory map (default is 'on').
+
 Note: This option allows direct access to host hardware. So it is no longer possible to migrate such machines - use with special care.
 
 Experimental: user reported problems with this option.
@@ -576,9 +618,9 @@ PVE::JSONSchema::register_standard_option("pve-qm-hostpci", $hostpcidesc);
 my $serialdesc = {
        optional => 1,
        type => 'string',
-       pattern => '(/dev/ttyS\d+|socket)',
+       pattern => '(/dev/.+|socket)',
        description =>  <<EODESCR,
-Create a serial device inside the VM (n is 0 to 3), and pass through a host serial device, or create a unix socket on the host side (use 'qm terminal' to open a terminal connection).
+Create a serial device inside the VM (n is 0 to 3), and pass through a host serial device (i.e. /dev/ttyS0), or create a unix socket on the host side (use 'qm terminal' to open a terminal connection).
 
 Note: This option allows direct access to host hardware. So it is no longer possible to migrate such machines - use with special care.
 
@@ -887,7 +929,7 @@ sub parse_drive {
     foreach my $p (split (/,/, $data)) {
        next if $p =~ m/^\s*$/;
 
-       if ($p =~ m/^(file|volume|cyls|heads|secs|trans|media|snapshot|cache|format|rerror|werror|backup|aio|bps|mbps|bps_rd|mbps_rd|bps_wr|mbps_wr|iops|iops_rd|iops_wr|size|discard)=(.+)$/) {
+       if ($p =~ m/^(file|volume|cyls|heads|secs|trans|media|snapshot|cache|format|rerror|werror|backup|aio|bps|mbps|mbps_max|bps_rd|mbps_rd|mbps_rd_max|bps_wr|mbps_wr|mbps_wr_max|iops|iops_max|iops_rd|iops_rd_max|iops_wr|iops_wr_max|size|discard)=(.+)$/) {
            my ($k, $v) = ($1, $2);
 
            $k = 'file' if $k eq 'volume';
@@ -934,14 +976,22 @@ sub parse_drive {
     return undef if $res->{mbps_wr} && $res->{mbps};
 
     return undef if $res->{mbps} && $res->{mbps} !~ m/^\d+(\.\d+)?$/;
+    return undef if $res->{mbps_max} && $res->{mbps_max} !~ m/^\d+(\.\d+)?$/;
     return undef if $res->{mbps_rd} && $res->{mbps_rd} !~ m/^\d+(\.\d+)?$/;
+    return undef if $res->{mbps_rd_max} && $res->{mbps_rd_max} !~ m/^\d+(\.\d+)?$/;
     return undef if $res->{mbps_wr} && $res->{mbps_wr} !~ m/^\d+(\.\d+)?$/;
+    return undef if $res->{mbps_wr_max} && $res->{mbps_wr_max} !~ m/^\d+(\.\d+)?$/;
 
     return undef if $res->{iops_rd} && $res->{iops};
     return undef if $res->{iops_wr} && $res->{iops};
+
+
     return undef if $res->{iops} && $res->{iops} !~ m/^\d+$/;
+    return undef if $res->{iops_max} && $res->{iops_max} !~ m/^\d+$/;
     return undef if $res->{iops_rd} && $res->{iops_rd} !~ m/^\d+$/;
+    return undef if $res->{iops_rd_max} && $res->{iops_rd_max} !~ m/^\d+$/;
     return undef if $res->{iops_wr} && $res->{iops_wr} !~ m/^\d+$/;
+    return undef if $res->{iops_wr_max} && $res->{iops_wr_max} !~ m/^\d+$/;
 
 
     if ($res->{size}) {
@@ -962,13 +1012,13 @@ sub parse_drive {
     return $res;
 }
 
-my @qemu_drive_options = qw(heads secs cyls trans media format cache snapshot rerror werror aio discard iops iops_rd iops_wr);
+my @qemu_drive_options = qw(heads secs cyls trans media format cache snapshot rerror werror aio discard iops iops_rd iops_wr iops_max iops_rd_max iops_wr_max);
 
 sub print_drive {
     my ($vmid, $drive) = @_;
 
     my $opts = '';
-    foreach my $o (@qemu_drive_options, 'mbps', 'mbps_rd', 'mbps_wr', 'backup') {
+    foreach my $o (@qemu_drive_options, 'mbps', 'mbps_rd', 'mbps_wr', 'mbps_max', 'mbps_rd_max', 'mbps_wr_max', 'backup') {
        $opts .= ",$o=$drive->{$o}" if $drive->{$o};
     }
 
@@ -1040,6 +1090,23 @@ sub path_is_scsi {
     return $res;
 }
 
+sub machine_type_is_q35 {
+    my ($conf) = @_;
+
+    return $conf->{machine} && ($conf->{machine} =~ m/q35/) ? 1 : 0;
+}
+
+sub print_tabletdevice_full {
+    my ($conf) = @_;
+
+    my $q35 = machine_type_is_q35($conf);
+
+    # we use uhci for old VMs because tablet driver was buggy in older qemu
+    my $usbbus = $q35 ? "ehci" : "uhci";
+
+    return "usb-tablet,id=tablet,bus=$usbbus.0,port=1";
+}
+
 sub print_drivedevice_full {
     my ($storecfg, $conf, $vmid, $drive, $bridges) = @_;
 
@@ -1049,6 +1116,7 @@ sub print_drivedevice_full {
     if ($drive->{interface} eq 'virtio') {
        my $pciaddr = print_pci_addr("$drive->{interface}$drive->{index}", $bridges);
        $device = "virtio-blk-pci,drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}$pciaddr";
+       $device .= ",iothread=iothread0" if $conf->{iothread};
     } elsif ($drive->{interface} eq 'scsi') {
        $maxdev = ($conf->{scsihw} && ($conf->{scsihw} !~ m/^lsi/)) ? 256 : 7;
        my $controller = int($drive->{index} / $maxdev);
@@ -1106,6 +1174,20 @@ sub print_drivedevice_full {
     return $device;
 }
 
+sub get_initiator_name {
+    my $initiator;
+
+    my $fh = IO::File->new('/etc/iscsi/initiatorname.iscsi') || return undef;
+    while (defined(my $line = <$fh>)) {
+       next if $line !~ m/^\s*InitiatorName\s*=\s*([\.\-:\w]+)/;
+       $initiator = $1;
+       last;
+    }
+    $fh->close();
+
+    return $initiator;
+}
+
 sub print_drive_full {
     my ($storecfg, $vmid, $drive) = @_;
 
@@ -1137,6 +1219,9 @@ sub print_drive_full {
 
     $opts .= ",cache=none" if !$drive->{cache} && !drive_is_cdrom($drive);
 
+    my $detectzeroes = $drive->{discard} ? "unmap" : "on";
+    $opts .= ",detect-zeroes=$detectzeroes" if !drive_is_cdrom($drive);
+
     my $pathinfo = $path ? "file=$path," : '';
 
     return "${pathinfo}if=none,id=drive-$drive->{interface}$drive->{index}$opts";
@@ -1152,11 +1237,13 @@ sub print_netdevice_full {
          $device = 'virtio-net-pci';
      };
 
-    # qemu > 0.15 always try to boot from network - we disable that by
-    # not loading the pxe rom file
-    my $extra = ($bootorder !~ m/n/) ? "romfile=," : '';
     my $pciaddr = print_pci_addr("$netid", $bridges);
-    my $tmpstr = "$device,${extra}mac=$net->{macaddr},netdev=$netid$pciaddr,id=$netid";
+    my $tmpstr = "$device,mac=$net->{macaddr},netdev=$netid$pciaddr,id=$netid";
+    if ($net->{queues} && $net->{queues} > 1 && $net->{model} eq 'virtio'){
+       #Consider we have N queues, the number of vectors needed is 2*N + 2 (plus one config interrupt and control vq)
+       my $vectors = $net->{queues} * 2 + 2;
+       $tmpstr .= ",vectors=$vectors,mq=on";
+    }
     $tmpstr .= ",bootindex=$net->{bootindex}" if $net->{bootindex} ;
     return $tmpstr;
 }
@@ -1182,11 +1269,17 @@ sub print_netdev_full {
 
     my $vmname = $conf->{name} || "vm$vmid";
 
+    my $netdev = "";
+
     if ($net->{bridge}) {
-        return "type=tap,id=$netid,ifname=${ifname},script=/var/lib/qemu-server/pve-bridge$vhostparam";
+        $netdev = "type=tap,id=$netid,ifname=${ifname},script=/var/lib/qemu-server/pve-bridge,downscript=/var/lib/qemu-server/pve-bridgedown$vhostparam";
     } else {
-        return "type=user,id=$netid,hostname=$vmname";
+        $netdev = "type=user,id=$netid,hostname=$vmname";
     }
+
+    $netdev .= ",queues=$net->{queues}" if ($net->{queues} && $net->{model} eq 'virtio');
+
+    return $netdev;
 }
 
 sub drive_is_cdrom {
@@ -1196,19 +1289,67 @@ sub drive_is_cdrom {
 
 }
 
+sub parse_numa {
+    my ($data) = @_;
+
+    my $res = {};
+
+    foreach my $kvp (split(/,/, $data)) {
+
+       if ($kvp =~ m/^memory=(\S+)$/) {
+           $res->{memory} = $1;
+       } elsif ($kvp =~ m/^policy=(preferred|bind|interleave)$/) {
+           $res->{policy} = $1;
+       } elsif ($kvp =~ m/^cpus=(\d+)(-(\d+))?$/) {
+           $res->{cpus}->{start} = $1;
+           $res->{cpus}->{end} = $3;
+       } elsif ($kvp =~ m/^hostnodes=(\d+)(-(\d+))?$/) {
+           $res->{hostnodes}->{start} = $1;
+           $res->{hostnodes}->{end} = $3;
+       } else {
+           return undef;
+       }
+    }
+
+    return $res;
+}
+
 sub parse_hostpci {
     my ($value) = @_;
 
     return undef if !$value;
 
+
+    my @list = split(/,/, $value);
+    my $found;
+
     my $res = {};
+    foreach my $kv (@list) {
 
-    if ($value =~ m/^[a-f0-9]{2}:[a-f0-9]{2}\.[a-f0-9]$/) {
-       $res->{pciid} = $value;
-    } else {
-       return undef;
+       if ($kv =~ m/^(host=)?([a-f0-9]{2}:[a-f0-9]{2})(\.([a-f0-9]))?$/) {
+           $found = 1;
+           if(defined($4)){
+               push @{$res->{pciid}}, { id => $2 , function => $4};
+
+           }else{
+               my $pcidevices = lspci($2);
+               $res->{pciid} = $pcidevices->{$2};
+           }
+       } elsif ($kv =~ m/^driver=(kvm|vfio)$/) {
+           $res->{driver} = $1;
+       } elsif ($kv =~ m/^rombar=(on|off)$/) {
+           $res->{rombar} = $1;
+       } elsif ($kv =~ m/^x-vga=(on|off)$/) {
+           $res->{'x-vga'} = $1;
+       } elsif ($kv =~ m/^pcie=(\d+)$/) {
+           $res->{pcie} = 1 if $1 == 1;
+       } else {
+           warn "unknown hostpci setting '$kv'\n";
+       }
     }
 
+    return undef if !$found;
+
     return $res;
 }
 
@@ -1220,17 +1361,23 @@ sub parse_net {
 
     foreach my $kvp (split(/,/, $data)) {
 
-       if ($kvp =~ m/^(ne2k_pci|e1000|rtl8139|pcnet|virtio|ne2k_isa|i82551|i82557b|i82559er|vmxnet3)(=([0-9a-f]{2}(:[0-9a-f]{2}){5}))?$/i) {
+       if ($kvp =~ m/^(ne2k_pci|e1000|e1000-82540em|e1000-82544gc|e1000-82545em|rtl8139|pcnet|virtio|ne2k_isa|i82551|i82557b|i82559er|vmxnet3)(=([0-9a-f]{2}(:[0-9a-f]{2}){5}))?$/i) {
            my $model = lc($1);
            my $mac = defined($3) ? uc($3) : PVE::Tools::random_ether_addr();
            $res->{model} = $model;
            $res->{macaddr} = $mac;
        } elsif ($kvp =~ m/^bridge=(\S+)$/) {
            $res->{bridge} = $1;
+       } elsif ($kvp =~ m/^queues=(\d+)$/) {
+           $res->{queues} = $1;
        } elsif ($kvp =~ m/^rate=(\d+(\.\d+)?)$/) {
            $res->{rate} = $1;
         } elsif ($kvp =~ m/^tag=(\d+)$/) {
             $res->{tag} = $1;
+        } elsif ($kvp =~ m/^firewall=([01])$/) {
+           $res->{firewall} = $1;
+       } elsif ($kvp =~ m/^link_down=([01])$/) {
+           $res->{link_down} = $1;
        } else {
            return undef;
        }
@@ -1250,6 +1397,8 @@ sub print_net {
     $res .= ",bridge=$net->{bridge}" if $net->{bridge};
     $res .= ",rate=$net->{rate}" if $net->{rate};
     $res .= ",tag=$net->{tag}" if $net->{tag};
+    $res .= ",firewall=1" if $net->{firewall};
+    $res .= ",link_down=1" if $net->{link_down};
 
     return $res;
 }
@@ -1285,6 +1434,143 @@ sub add_unused_volume {
     return $key;
 }
 
+sub vm_is_volid_owner {
+    my ($storecfg, $vmid, $volid) = @_;
+
+    if ($volid !~  m|^/|) {
+       my ($path, $owner);
+       eval { ($path, $owner) = PVE::Storage::path($storecfg, $volid); };
+       if ($owner && ($owner == $vmid)) {
+           return 1;
+       }
+    }
+
+    return undef;
+}
+
+sub vmconfig_delete_pending_option {
+    my ($conf, $key) = @_;
+
+    delete $conf->{pending}->{$key};
+    my $pending_delete_hash = { $key => 1 };
+    foreach my $opt (PVE::Tools::split_list($conf->{pending}->{delete})) {
+       $pending_delete_hash->{$opt} = 1;
+    }
+    $conf->{pending}->{delete} = join(',', keys %$pending_delete_hash);
+}
+
+sub vmconfig_undelete_pending_option {
+    my ($conf, $key) = @_;
+
+    my $pending_delete_hash = {};
+    foreach my $opt (PVE::Tools::split_list($conf->{pending}->{delete})) {
+       $pending_delete_hash->{$opt} = 1;
+    }
+    delete $pending_delete_hash->{$key};
+
+    my @keylist = keys %$pending_delete_hash;
+    if (scalar(@keylist)) {
+       $conf->{pending}->{delete} = join(',', @keylist);
+    } else {
+       delete $conf->{pending}->{delete};
+    }
+}
+
+sub vmconfig_register_unused_drive {
+    my ($storecfg, $vmid, $conf, $drive) = @_;
+
+    if (!drive_is_cdrom($drive)) {
+       my $volid = $drive->{file};
+       if (vm_is_volid_owner($storecfg, $vmid, $volid)) {
+           add_unused_volume($conf, $volid, $vmid);
+       }
+    }
+}
+
+sub vmconfig_cleanup_pending {
+    my ($conf) = @_;
+
+    # remove pending changes when nothing changed
+    my $changes;
+    foreach my $opt (keys %{$conf->{pending}}) {
+       if (defined($conf->{$opt}) && ($conf->{pending}->{$opt} eq  $conf->{$opt})) {
+           $changes = 1;
+           delete $conf->{pending}->{$opt};
+       }
+    }
+
+    # remove delete if option is not set
+    my $pending_delete_hash = {};
+    foreach my $opt (PVE::Tools::split_list($conf->{pending}->{delete})) {
+       if (defined($conf->{$opt})) {
+           $pending_delete_hash->{$opt} = 1;
+       } else {
+           $changes = 1;
+       }
+    }
+
+    my @keylist = keys %$pending_delete_hash;
+    if (scalar(@keylist)) {
+       $conf->{pending}->{delete} = join(',', @keylist);
+    } else {
+       delete $conf->{pending}->{delete};
+    }
+
+    return $changes;
+}
+
+my $valid_smbios1_options = {
+    manufacturer => '\S+',
+    product => '\S+',
+    version => '\S+',
+    serial => '\S+',
+    uuid => '[a-fA-F0-9]{8}(?:-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}',
+    sku => '\S+',
+    family => '\S+',
+};
+
+# smbios: [manufacturer=str][,product=str][,version=str][,serial=str][,uuid=uuid][,sku=str][,family=str]
+sub parse_smbios1 {
+    my ($data) = @_;
+
+    my $res = {};
+
+    foreach my $kvp (split(/,/, $data)) {
+       return undef if $kvp !~ m/^(\S+)=(.+)$/;
+       my ($k, $v) = split(/=/, $kvp);
+       return undef if !defined($k) || !defined($v);
+       return undef if !$valid_smbios1_options->{$k};
+       return undef if $v !~ m/^$valid_smbios1_options->{$k}$/;
+       $res->{$k} = $v;
+    }
+
+    return $res;
+}
+
+sub print_smbios1 {
+    my ($smbios1) = @_;
+
+    my $data = '';
+    foreach my $k (keys %$smbios1) {
+       next if !defined($smbios1->{$k});
+       next if !$valid_smbios1_options->{$k};
+       $data .= ',' if $data;
+       $data .= "$k=$smbios1->{$k}";
+    }
+    return $data;
+}
+
+PVE::JSONSchema::register_format('pve-qm-smbios1', \&verify_smbios1);
+sub verify_smbios1 {
+    my ($value, $noerr) = @_;
+
+    return $value if parse_smbios1($value);
+
+    return undef if $noerr;
+
+    die "unable to parse smbios (type 1) options\n";
+}
+
 PVE::JSONSchema::register_format('pve-qm-bootdisk', \&verify_bootdisk);
 sub verify_bootdisk {
     my ($value, $noerr) = @_;
@@ -1296,6 +1582,17 @@ sub verify_bootdisk {
     die "invalid boot disk '$value'\n";
 }
 
+PVE::JSONSchema::register_format('pve-qm-numanode', \&verify_numa);
+sub verify_numa {
+    my ($value, $noerr) = @_;
+
+    return $value if parse_numa($value);
+
+    return undef if $noerr;
+
+    die "unable to parse numa options\n";
+}
+
 PVE::JSONSchema::register_format('pve-qm-net', \&verify_net);
 sub verify_net {
     my ($value, $noerr) = @_;
@@ -1626,6 +1923,7 @@ sub parse_vm_config {
     my $res = {
        digest => Digest::SHA::sha1_hex($raw),
        snapshots => {},
+       pending => {},
     };
 
     $filename =~ m|/qemu-server/(\d+)\.conf$|
@@ -1635,16 +1933,24 @@ sub parse_vm_config {
 
     my $conf = $res;
     my $descr = '';
+    my $section = '';
 
     my @lines = split(/\n/, $raw);
     foreach my $line (@lines) {
        next if $line =~ m/^\s*$/;
 
-       if ($line =~ m/^\[([a-z][a-z0-9_\-]+)\]\s*$/i) {
-           my $snapname = $1;
+       if ($line =~ m/^\[PENDING\]\s*$/i) {
+           $section = 'pending';
+           $conf->{description} = $descr if $descr;
+           $descr = '';
+           $conf = $res->{$section} = {};
+           next;
+
+       } elsif ($line =~ m/^\[([a-z][a-z0-9_\-]+)\]\s*$/i) {
+           $section = $1;
            $conf->{description} = $descr if $descr;
            $descr = '';
-           $conf = $res->{snapshots}->{$snapname} = {};
+           $conf = $res->{snapshots}->{$section} = {};
            next;
        }
 
@@ -1661,6 +1967,13 @@ sub parse_vm_config {
            my $key = $1;
            my $value = $2;
            $conf->{$key} = $value;
+       } elsif ($line =~ m/^delete:\s*(.*\S)\s*$/) {
+           my $value = $1;
+           if ($section eq 'pending') {
+               $conf->{delete} = $value; # we parse this later
+           } else {
+               warn "vm $vmid - propertry 'delete' is only allowed in [PENDING]\n";
+           }
        } elsif ($line =~ m/^([a-z][a-z_]*\d*):\s*(\S+)\s*$/) {
            my $key = $1;
            my $value = $2;
@@ -1716,15 +2029,25 @@ sub write_vm_config {
        delete $conf->{smp};
     }
 
+    if ($conf->{maxcpus} && $conf->{sockets}) {
+       delete $conf->{sockets};
+    }
+
     my $used_volids = {};
 
     my $cleanup_config = sub {
-       my ($cref, $snapname) = @_;
+       my ($cref, $pending, $snapname) = @_;
 
        foreach my $key (keys %$cref) {
            next if $key eq 'digest' || $key eq 'description' || $key eq 'snapshots' ||
-               $key eq 'snapstate';
+               $key eq 'snapstate' || $key eq 'pending';
            my $value = $cref->{$key};
+           if ($key eq 'delete') {
+               die "propertry 'delete' is only allowed in [PENDING]\n"
+                   if !$pending;
+               # fixme: check syntax?
+               next;
+           }
            eval { $value = check_type($key, $value); };
            die "unable to parse value of '$key' - $@" if $@;
 
@@ -1738,8 +2061,12 @@ sub write_vm_config {
     };
 
     &$cleanup_config($conf);
+
+    &$cleanup_config($conf->{pending}, 1);
+
     foreach my $snapname (keys %{$conf->{snapshots}}) {
-       &$cleanup_config($conf->{snapshots}->{$snapname}, $snapname);
+       die "internal error" if $snapname eq 'pending';
+       &$cleanup_config($conf->{snapshots}->{$snapname}, undef, $snapname);
     }
 
     # remove 'unusedX' settings if we re-add a volume
@@ -1762,13 +2089,19 @@ sub write_vm_config {
        }
 
        foreach my $key (sort keys %$conf) {
-           next if $key eq 'digest' || $key eq 'description' || $key eq 'snapshots';
+           next if $key eq 'digest' || $key eq 'description' || $key eq 'pending' || $key eq 'snapshots';
            $raw .= "$key: $conf->{$key}\n";
        }
        return $raw;
     };
 
     my $raw = &$generate_raw_config($conf);
+
+    if (scalar(keys %{$conf->{pending}})){
+       $raw .= "\n[PENDING]\n";
+       $raw .= &$generate_raw_config($conf->{pending});
+    }
+
     foreach my $snapname (sort keys %{$conf->{snapshots}}) {
        $raw .= "\n[$snapname]\n";
        $raw .= &$generate_raw_config($conf->{snapshots}->{$snapname});
@@ -2179,7 +2512,7 @@ sub vmstatus {
        $qmpclient->queue_cmd($vmid, $statuscb, 'query-status');
     }
 
-    $qmpclient->queue_execute();
+    $qmpclient->queue_execute(undef, 1);
 
     foreach my $vmid (keys %$list) {
        next if $opt_vmid && ($vmid ne $opt_vmid);
@@ -2265,6 +2598,8 @@ sub config_to_command {
 
     my $have_ovz = -f '/proc/vz/vestat';
 
+    my $q35 = machine_type_is_q35($conf);
+
     push @$cmd, '/usr/bin/kvm';
 
     push @$cmd, '-id', $vmid;
@@ -2282,16 +2617,28 @@ sub config_to_command {
 
     push @$cmd, '-daemonize';
 
-    $pciaddr = print_pci_addr("piix3", $bridges);
-    push @$devices, '-device', "piix3-usb-uhci,id=uhci$pciaddr.0x2";
+    if ($conf->{smbios1}) {
+       push @$cmd, '-smbios', "type=1,$conf->{smbios1}";
+    }
 
-    my $use_usb2 = 0;
-    for (my $i = 0; $i < $MAX_USB_DEVICES; $i++)  {
-       next if !$conf->{"usb$i"};
-       $use_usb2 = 1;
+    push @$cmd, '-object', "iothread,id=iothread0" if $conf->{iothread};
+
+    if ($q35) {
+       # the q35 chipset support native usb2, so we enable usb controller
+       # by default for this machine type
+        push @$devices, '-readconfig', '/usr/share/qemu-server/pve-q35.cfg';
+    } else {
+        $pciaddr = print_pci_addr("piix3", $bridges);
+        push @$devices, '-device', "piix3-usb-uhci,id=uhci$pciaddr.0x2";
+
+        my $use_usb2 = 0;
+       for (my $i = 0; $i < $MAX_USB_DEVICES; $i++)  {
+           next if !$conf->{"usb$i"};
+           $use_usb2 = 1;
+       }
+       # include usb device config
+       push @$devices, '-readconfig', '/usr/share/qemu-server/pve-usb.cfg' if $use_usb2;
     }
-    # include usb device config
-    push @$devices, '-readconfig', '/usr/share/qemu-server/pve-usb.cfg' if $use_usb2;
 
     my $vga = $conf->{vga};
 
@@ -2299,8 +2646,8 @@ sub config_to_command {
     $vga = 'qxl' if $qxlnum;
 
     if (!$vga) {
-       if ($conf->{ostype} && ($conf->{ostype} eq 'win8' || 
-                               $conf->{ostype} eq 'win7' || 
+       if ($conf->{ostype} && ($conf->{ostype} eq 'win8' ||
+                               $conf->{ostype} eq 'win7' ||
                                $conf->{ostype} eq 'w2k8')) {
            $vga = 'std';
        } else {
@@ -2318,14 +2665,49 @@ sub config_to_command {
        $tablet = 0 if $vga =~ m/^serial\d+$/; # disable if we use serial terminal (no vga card)
     }
 
-    push @$devices, '-device', 'usb-tablet,id=tablet,bus=uhci.0,port=1' if $tablet;
+    push @$devices, '-device', print_tabletdevice_full($conf) if $tablet;
 
     # host pci devices
     for (my $i = 0; $i < $MAX_HOSTPCI_DEVICES; $i++)  {
-          my $d = parse_hostpci($conf->{"hostpci$i"});
-          next if !$d;
-         $pciaddr = print_pci_addr("hostpci$i", $bridges);
-          push @$devices, '-device', "pci-assign,host=$d->{pciid},id=hostpci$i$pciaddr";
+       my $d = parse_hostpci($conf->{"hostpci$i"});
+       next if !$d;
+
+       my $pcie = $d->{pcie};
+       if($pcie){
+           die "q35 machine model is not enabled" if !$q35;
+           $pciaddr = print_pcie_addr("hostpci$i");
+       }else{
+           $pciaddr = print_pci_addr("hostpci$i", $bridges);
+       }
+
+       my $rombar = $d->{rombar} && $d->{rombar} eq 'off' ? ",rombar=0" : "";
+       my $driver = $d->{driver} && $d->{driver} eq 'vfio' ? "vfio-pci" : "pci-assign";
+       my $xvga = $d->{'x-vga'} && $d->{'x-vga'} eq 'on' ? ",x-vga=on" : "";
+       if ($xvga && $xvga ne '') {
+           push @$cpuFlags, 'kvm=off';
+           $vga = 'none';
+       }
+       $driver = "vfio-pci" if $xvga ne '';
+       my $pcidevices = $d->{pciid};
+       my $multifunction = 1 if @$pcidevices > 1;
+
+       my $j=0;
+        foreach my $pcidevice (@$pcidevices) {
+
+           my $id = "hostpci$i";
+           $id .= ".$j" if $multifunction;
+           my $addr = $pciaddr;
+           $addr .= ".$j" if $multifunction;
+           my $devicestr = "$driver,host=$pcidevice->{id}.$pcidevice->{function},id=$id$addr";
+
+           if($j == 0){
+               $devicestr .= "$rombar$xvga";
+               $devicestr .= ",multifunction=on" if $multifunction;
+           }
+
+           push @$devices, '-device', $devicestr;
+           $j++;
+       }
     }
 
     # usb devices
@@ -2377,7 +2759,19 @@ sub config_to_command {
     $sockets = $conf->{sockets} if  $conf->{sockets};
 
     my $cores = $conf->{cores} || 1;
-    push @$cmd, '-smp', "sockets=$sockets,cores=$cores";
+    my $maxcpus = $conf->{maxcpus} if $conf->{maxcpus};
+
+    my $total_cores = $sockets * $cores;
+    my $allowed_cores = $cpuinfo->{cpus};
+
+    die "MAX $allowed_cores cores allowed per VM on this node\n"
+       if ($allowed_cores < $total_cores);
+
+    if ($maxcpus) {
+       push @$cmd, '-smp', "cpus=$cores,maxcpus=$maxcpus";
+    } else {
+       push @$cmd, '-smp', "sockets=$sockets,cores=$cores";
+    }
 
     push @$cmd, '-nodefaults';
 
@@ -2390,7 +2784,7 @@ sub config_to_command {
        $i++;
     }
 
-    push @$cmd, '-boot', "menu=on";
+    push @$cmd, '-boot', "menu=on,strict=on,reboot-timeout=1000";
 
     push @$cmd, '-no-acpi' if defined($conf->{acpi}) && $conf->{acpi} == 0;
 
@@ -2461,8 +2855,82 @@ sub config_to_command {
 
     $cpu .= "," . join(',', @$cpuFlags) if scalar(@$cpuFlags);
 
+    # Note: enforce needs kernel 3.10, so we do not use it for now
+    # push @$cmd, '-cpu', "$cpu,enforce";
     push @$cmd, '-cpu', $cpu;
 
+    my $memory =  $conf->{memory} || $defaults->{memory};
+    push @$cmd, '-m', $memory;
+
+    if ($conf->{numa}) {
+
+       my $numa_totalmemory = undef;
+       for (my $i = 0; $i < $MAX_NUMA; $i++) {
+           next if !$conf->{"numa$i"};
+           my $numa = parse_numa($conf->{"numa$i"});
+           next if !$numa;
+           # memory
+           die "missing numa node$i memory value\n" if !$numa->{memory};
+           my $numa_memory = $numa->{memory};
+           $numa_totalmemory += $numa_memory;
+           my $numa_object = "memory-backend-ram,id=ram-node$i,size=$numa_memory"."M";
+
+           # cpus
+           my $cpus_start = $numa->{cpus}->{start};
+           die "missing numa node$i cpus\n" if !defined($cpus_start);
+           my $cpus_end = $numa->{cpus}->{end} if defined($numa->{cpus}->{end});
+           my $cpus = $cpus_start;
+           if (defined($cpus_end)) {
+               $cpus .= "-$cpus_end";
+               die "numa node$i :  cpu range $cpus is incorrect\n" if $cpus_end <= $cpus_start;
+           }
+
+           # hostnodes
+           my $hostnodes_start = $numa->{hostnodes}->{start};
+           if (defined($hostnodes_start)) {
+               my $hostnodes_end = $numa->{hostnodes}->{end} if defined($numa->{hostnodes}->{end});
+               my $hostnodes = $hostnodes_start;
+               if (defined($hostnodes_end)) {
+                   $hostnodes .= "-$hostnodes_end";
+                   die "host node $hostnodes range is incorrect\n" if $hostnodes_end <= $hostnodes_start;
+               }
+
+               my $hostnodes_end_range = defined($hostnodes_end) ? $hostnodes_end : $hostnodes_start;
+               for (my $i = $hostnodes_start; $i <= $hostnodes_end_range; $i++ ) {
+                   die "host numa node$i don't exist\n" if ! -d "/sys/devices/system/node/node$i/";
+               }
+
+               # policy
+               my $policy = $numa->{policy};
+               die "you need to define a policy for hostnode $hostnodes\n" if !$policy;
+               $numa_object .= ",host-nodes=$hostnodes,policy=$policy";
+           }
+
+           push @$cmd, '-object', $numa_object;
+           push @$cmd, '-numa', "node,nodeid=$i,cpus=$cpus,memdev=ram-node$i";
+       }
+
+       die "total memory for NUMA nodes must be equal to vm memory\n"
+           if $numa_totalmemory && $numa_totalmemory != $memory;
+
+       #if no custom tology, we split memory and cores across numa nodes
+       if(!$numa_totalmemory) {
+
+           my $numa_memory = ($memory / $sockets) . "M";
+
+           for (my $i = 0; $i < $sockets; $i++)  {
+
+               my $cpustart = ($cores * $i);
+               my $cpuend = ($cpustart + $cores - 1) if $cores && $cores > 1;
+               my $cpus = $cpustart;
+               $cpus .= "-$cpuend" if $cpuend;
+
+               push @$cmd, '-object', "memory-backend-ram,size=$numa_memory,id=ram-node$i";
+               push @$cmd, '-numa', "node,nodeid=$i,cpus=$cpus,memdev=ram-node$i";
+           }
+       }
+    }
+
     push @$cmd, '-S' if $conf->{freeze};
 
     # set keyboard layout
@@ -2475,7 +2943,7 @@ sub config_to_command {
     #push @$cmd, '-soundhw', $soundhw if $soundhw;
 
     if($conf->{agent}) {
-       my $qgasocket = qga_socket($vmid);
+       my $qgasocket = qmp_socket($vmid, 1);
        my $pciaddr = print_pci_addr("qga0", $bridges);
        push @$devices, '-chardev', "socket,path=$qgasocket,server,nowait,id=qga0";
        push @$devices, '-device', "virtio-serial,id=qga0$pciaddr";
@@ -2500,13 +2968,13 @@ sub config_to_command {
 
        my $pciaddr = print_pci_addr("spice", $bridges);
 
-       $spice_port = PVE::Tools::next_unused_port(61000, 61099);
+       $spice_port = PVE::Tools::next_spice_port();
 
-       push @$cmd, '-spice', "tls-port=${spice_port},addr=127.0.0.1,tls-ciphers=DES-CBC3-SHA,seamless-migration=on";
+       push @$devices, '-spice', "tls-port=${spice_port},addr=127.0.0.1,tls-ciphers=DES-CBC3-SHA,seamless-migration=on";
 
-       push @$cmd, '-device', "virtio-serial,id=spice$pciaddr";
-       push @$cmd, '-chardev', "spicevmc,id=vdagent,name=vdagent";
-       push @$cmd, '-device', "virtserialport,chardev=vdagent,name=com.redhat.spice.0";
+       push @$devices, '-device', "virtio-serial,id=spice$pciaddr";
+       push @$devices, '-chardev', "spicevmc,id=vdagent,name=vdagent";
+       push @$devices, '-device', "virtserialport,chardev=vdagent,name=com.redhat.spice.0";
     }
 
     # enable balloon by default, unless explicitly disabled
@@ -2528,6 +2996,11 @@ sub config_to_command {
     my $ahcicontroller = {};
     my $scsihw = defined($conf->{scsihw}) ? $conf->{scsihw} : $defaults->{scsihw};
 
+    # Add iscsi initiator name if available
+    if (my $initiator = get_initiator_name()) {
+       push @$devices, '-iscsi', "initiator-name=$initiator";
+    }
+
     foreach_drive($conf, sub {
        my ($ds, $drive) = @_;
 
@@ -2565,12 +3038,11 @@ sub config_to_command {
            $ahcicontroller->{$controller}=1;
         }
 
-       push @$devices, '-drive',print_drive_full($storecfg, $vmid, $drive);
-       push @$devices, '-device',print_drivedevice_full($storecfg, $conf, $vmid, $drive, $bridges);
+       my $drive_cmd = print_drive_full($storecfg, $vmid, $drive);
+       push @$devices, '-drive',$drive_cmd;
+       push @$devices, '-device', print_drivedevice_full($storecfg, $conf, $vmid, $drive, $bridges);
     });
 
-    push @$cmd, '-m', $conf->{memory} || $defaults->{memory};
-
     for (my $i = 0; $i < $MAX_NETS; $i++) {
          next if !$conf->{"net$i"};
          my $d = parse_net($conf->{"net$i"});
@@ -2590,13 +3062,14 @@ sub config_to_command {
          push @$devices, '-device', $netdevicefull;
     }
 
-    #bridges
-    while (my ($k, $v) = each %$bridges) {
-       $pciaddr = print_pci_addr("pci.$k");
-       unshift @$devices, '-device', "pci-bridge,id=pci.$k,chassis_nr=$k$pciaddr" if $k > 0;
+    if (!$q35) {
+       # add pci bridges
+       while (my ($k, $v) = each %$bridges) {
+           $pciaddr = print_pci_addr("pci.$k");
+           unshift @$devices, '-device', "pci-bridge,id=pci.$k,chassis_nr=$k$pciaddr" if $k > 0;
+       }
     }
 
-
     # hack: virtio with fairsched is unreliable, so we do not use fairsched
     # when the VM uses virtio devices.
     if (!$use_virtio && $have_ovz) {
@@ -2641,13 +3114,9 @@ sub spice_port {
 }
 
 sub qmp_socket {
-    my ($vmid) = @_;
-    return "${var_run_tmpdir}/$vmid.qmp";
-}
-
-sub qga_socket {
-    my ($vmid) = @_;
-    return "${var_run_tmpdir}/$vmid.qga";
+    my ($vmid, $qga) = @_;
+    my $sockettype = $qga ? 'qga' : 'qmp';
+    return "${var_run_tmpdir}/$vmid.$sockettype";
 }
 
 sub pidfile_name {
@@ -2659,122 +3128,157 @@ sub vm_devices_list {
     my ($vmid) = @_;
 
     my $res = vm_mon_cmd($vmid, 'query-pci');
-
     my $devices = {};
     foreach my $pcibus (@$res) {
        foreach my $device (@{$pcibus->{devices}}) {
            next if !$device->{'qdev_id'};
-           $devices->{$device->{'qdev_id'}} = $device;
+           $devices->{$device->{'qdev_id'}} = 1;
+       }
+    }
+
+    my $resblock = vm_mon_cmd($vmid, 'query-block');
+    foreach my $block (@$resblock) {
+       if($block->{device} =~ m/^drive-(\S+)/){
+               $devices->{$1} = 1;
+       }
+    }
+
+    my $resmice = vm_mon_cmd($vmid, 'query-mice');
+    foreach my $mice (@$resmice) {
+       if ($mice->{name} eq 'QEMU HID Tablet') {
+           $devices->{tablet} = 1;
+           last;
        }
     }
 
     return $devices;
 }
 
+sub hotplug_enabled {
+    my ($conf) = @_;
+
+    my $default = $confdesc->{'hotplug'}->{default};
+
+    return defined($conf->{hotplug}) ? $conf->{hotplug} : $default;
+}
+
 sub vm_deviceplug {
     my ($storecfg, $conf, $vmid, $deviceid, $device) = @_;
 
-    return 1 if !check_running($vmid);
-
-    if ($deviceid eq 'tablet') {
-       my $devicefull = "usb-tablet,id=tablet,bus=uhci.0,port=1";
-       qemu_deviceadd($vmid, $devicefull);
-       return 1;
-    }
+    die "internal error" if !hotplug_enabled($conf);
 
-    return 1 if !$conf->{hotplug};
+    my $q35 = machine_type_is_q35($conf);
 
     my $devices_list = vm_devices_list($vmid);
     return 1 if defined($devices_list->{$deviceid});
 
-    qemu_bridgeadd($storecfg, $conf, $vmid, $deviceid); #add bridge if we need it for the device
+    qemu_add_pci_bridge($storecfg, $conf, $vmid, $deviceid); # add PCI bridge if we need it for the device
+
+    if ($deviceid eq 'tablet') {
+
+       qemu_deviceadd($vmid, print_tabletdevice_full($conf));
 
-    if ($deviceid =~ m/^(virtio)(\d+)$/) {
-        return undef if !qemu_driveadd($storecfg, $vmid, $device);
+    } elsif ($deviceid =~ m/^(virtio)(\d+)$/) {
+
+        qemu_driveadd($storecfg, $vmid, $device);
         my $devicefull = print_drivedevice_full($storecfg, $conf, $vmid, $device);
+
         qemu_deviceadd($vmid, $devicefull);
-        if(!qemu_deviceaddverify($vmid, $deviceid)) {
-           qemu_drivedel($vmid, $deviceid);
-           return undef;
+       eval { qemu_deviceaddverify($vmid, $deviceid); };
+       if (my $err = $@) {
+           eval { qemu_drivedel($vmid, $deviceid); };
+           warn $@ if $@;
+           die $err;
         }
-    }
 
-    if ($deviceid =~ m/^(scsihw)(\d+)$/) {
+    } elsif ($deviceid =~ m/^(scsihw)(\d+)$/) {
+
         my $scsihw = defined($conf->{scsihw}) ? $conf->{scsihw} : "lsi";
         my $pciaddr = print_pci_addr($deviceid);
         my $devicefull = "$scsihw,id=$deviceid$pciaddr";
+
         qemu_deviceadd($vmid, $devicefull);
-        return undef if(!qemu_deviceaddverify($vmid, $deviceid));
-    }
+        qemu_deviceaddverify($vmid, $deviceid);
 
-    if ($deviceid =~ m/^(scsi)(\d+)$/) {
-        return 1 if ($conf->{scsihw} && ($conf->{scsihw} !~ m/^lsi/)); #virtio-scsi not yet support hotplug
-        return undef if !qemu_findorcreatescsihw($storecfg,$conf, $vmid, $device);
-        return undef if !qemu_driveadd($storecfg, $vmid, $device);
-        my $devicefull = print_drivedevice_full($storecfg, $conf, $vmid, $device);
-        if(!qemu_deviceadd($vmid, $devicefull)) {
-           qemu_drivedel($vmid, $deviceid);
-           return undef;
+    } elsif ($deviceid =~ m/^(scsi)(\d+)$/) {
+
+        qemu_findorcreatescsihw($storecfg,$conf, $vmid, $device);
+        qemu_driveadd($storecfg, $vmid, $device);
+        
+       my $devicefull = print_drivedevice_full($storecfg, $conf, $vmid, $device);
+       eval { qemu_deviceadd($vmid, $devicefull); };
+       if (my $err = $@) {
+           eval { qemu_drivedel($vmid, $deviceid); };
+           warn $@ if $@;
+           die $err;
         }
-    }
 
-    if ($deviceid =~ m/^(net)(\d+)$/) {
+    } elsif ($deviceid =~ m/^(net)(\d+)$/) {
+
         return undef if !qemu_netdevadd($vmid, $conf, $device, $deviceid);
         my $netdevicefull = print_netdevice_full($vmid, $conf, $device, $deviceid);
         qemu_deviceadd($vmid, $netdevicefull);
-        if(!qemu_deviceaddverify($vmid, $deviceid)) {
-           qemu_netdevdel($vmid, $deviceid);
-           return undef;
+        eval { qemu_deviceaddverify($vmid, $deviceid); };
+       if (my $err = $@) {
+           eval { qemu_netdevdel($vmid, $deviceid); };
+           warn $@ if $@;
+           die $err;
         }
-    }
 
-    if ($deviceid =~ m/^(pci\.)(\d+)$/) {
+    } elsif (!$q35 && $deviceid =~ m/^(pci\.)(\d+)$/) {
+
        my $bridgeid = $2;
        my $pciaddr = print_pci_addr($deviceid);
        my $devicefull = "pci-bridge,id=pci.$bridgeid,chassis_nr=$bridgeid$pciaddr";
+       
        qemu_deviceadd($vmid, $devicefull);
-       return undef if !qemu_deviceaddverify($vmid, $deviceid);
+       qemu_deviceaddverify($vmid, $deviceid);
+
+    } else {
+       die "can't hotplug device '$deviceid'\n";       
     }
 
     return 1;
 }
 
+# fixme: this should raise exceptions on error!
 sub vm_deviceunplug {
     my ($vmid, $conf, $deviceid) = @_;
 
-    return 1 if !check_running ($vmid);
-
-    if ($deviceid eq 'tablet') {
-       qemu_devicedel($vmid, $deviceid);
-       return 1;
-    }
-
-    return 1 if !$conf->{hotplug};
+    die "internal error" if !hotplug_enabled($conf);
 
     my $devices_list = vm_devices_list($vmid);
     return 1 if !defined($devices_list->{$deviceid});
 
     die "can't unplug bootdisk" if $conf->{bootdisk} && $conf->{bootdisk} eq $deviceid;
 
-    if ($deviceid =~ m/^(virtio)(\d+)$/) {
-        qemu_devicedel($vmid, $deviceid);
-        return undef if !qemu_devicedelverify($vmid, $deviceid);
-        return undef if !qemu_drivedel($vmid, $deviceid);
-    }
+    if ($deviceid eq 'tablet') {
 
-    if ($deviceid =~ m/^(lsi)(\d+)$/) {
-        return undef if !qemu_devicedel($vmid, $deviceid);
-    }
+       qemu_devicedel($vmid, $deviceid);
 
-    if ($deviceid =~ m/^(scsi)(\d+)$/) {
-        return undef if !qemu_devicedel($vmid, $deviceid);
-        return undef if !qemu_drivedel($vmid, $deviceid);
-    }
+    } elsif ($deviceid =~ m/^(virtio)(\d+)$/) {
 
-    if ($deviceid =~ m/^(net)(\d+)$/) {
         qemu_devicedel($vmid, $deviceid);
-        return undef if !qemu_devicedelverify($vmid, $deviceid);
-        return undef if !qemu_netdevdel($vmid, $deviceid);
+        qemu_devicedelverify($vmid, $deviceid);
+        qemu_drivedel($vmid, $deviceid);
+   
+    } elsif ($deviceid =~ m/^(lsi)(\d+)$/) {
+    
+       qemu_devicedel($vmid, $deviceid);
+    
+    } elsif ($deviceid =~ m/^(scsi)(\d+)$/) {
+
+        qemu_devicedel($vmid, $deviceid);
+        qemu_drivedel($vmid, $deviceid);
+    
+    } elsif ($deviceid =~ m/^(net)(\d+)$/) {
+
+        qemu_devicedel($vmid, $deviceid);
+        qemu_devicedelverify($vmid, $deviceid);
+        qemu_netdevdel($vmid, $deviceid);
+
+    } else {
+       die "can't unplug device '$deviceid'\n";
     }
 
     return 1;
@@ -2787,26 +3291,24 @@ sub qemu_deviceadd {
     my %options =  split(/[=,]/, $devicefull);
 
     vm_mon_cmd($vmid, "device_add" , %options);
-    return 1;
 }
 
 sub qemu_devicedel {
-    my($vmid, $deviceid) = @_;
+    my ($vmid, $deviceid) = @_;
+
     my $ret = vm_mon_cmd($vmid, "device_del", id => $deviceid);
-    return 1;
 }
 
 sub qemu_driveadd {
-    my($storecfg, $vmid, $device) = @_;
+    my ($storecfg, $vmid, $device) = @_;
 
     my $drive = print_drive_full($storecfg, $vmid, $device);
     my $ret = vm_human_monitor_command($vmid, "drive_add auto $drive");
+
     # If the command succeeds qemu prints: "OK"
-    if ($ret !~ m/OK/s) {
-        syslog("err", "adding drive failed: $ret");
-        return undef;
-    }
-    return 1;
+    return 1 if $ret =~ m/OK/s;
+
+    die "adding drive failed: $ret\n";
 }
 
 sub qemu_drivedel {
@@ -2814,40 +3316,41 @@ sub qemu_drivedel {
 
     my $ret = vm_human_monitor_command($vmid, "drive_del drive-$deviceid");
     $ret =~ s/^\s+//;
-    if ($ret =~ m/Device \'.*?\' not found/s) {
-        # NB: device not found errors mean the drive was auto-deleted and we ignore the error
-    }
-    elsif ($ret ne "") {
-      syslog("err", "deleting drive $deviceid failed : $ret");
-      return undef;
-    }
-    return 1;
+    
+    return 1 if $ret eq "";
+  
+    # NB: device not found errors mean the drive was auto-deleted and we ignore the error
+    return 1 if $ret =~ m/Device \'.*?\' not found/s; 
+    
+    die "deleting drive $deviceid failed : $ret\n";
 }
 
 sub qemu_deviceaddverify {
-    my ($vmid,$deviceid) = @_;
+    my ($vmid, $deviceid) = @_;
 
     for (my $i = 0; $i <= 5; $i++) {
          my $devices_list = vm_devices_list($vmid);
          return 1 if defined($devices_list->{$deviceid});
          sleep 1;
     }
-    syslog("err", "error on hotplug device $deviceid");
-    return undef;
+
+    die "error on hotplug device '$deviceid'\n";
 }
 
 
 sub qemu_devicedelverify {
-    my ($vmid,$deviceid) = @_;
+    my ($vmid, $deviceid) = @_;
+
+    # need to verify that the device is correctly removed as device_del 
+    # is async and empty return is not reliable
 
-    #need to verify the device is correctly remove as device_del is async and empty return is not reliable
     for (my $i = 0; $i <= 5; $i++) {
          my $devices_list = vm_devices_list($vmid);
          return 1 if !defined($devices_list->{$deviceid});
          sleep 1;
     }
-    syslog("err", "error on hot-unplugging device $deviceid");
-    return undef;
+
+    die "error on hot-unplugging device '$deviceid'\n";
 }
 
 sub qemu_findorcreatescsihw {
@@ -2859,31 +3362,43 @@ sub qemu_findorcreatescsihw {
     my $devices_list = vm_devices_list($vmid);
 
     if(!defined($devices_list->{$scsihwid})) {
-       return undef if !vm_deviceplug($storecfg, $conf, $vmid, $scsihwid);
+       vm_deviceplug($storecfg, $conf, $vmid, $scsihwid);
     }
+
     return 1;
 }
 
-sub qemu_bridgeadd {
+sub qemu_add_pci_bridge {
     my ($storecfg, $conf, $vmid, $device) = @_;
 
     my $bridges = {};
-    my $bridgeid = undef;
+
+    my $bridgeid;
+
     print_pci_addr($device, $bridges);
 
     while (my ($k, $v) = each %$bridges) {
        $bridgeid = $k;
     }
-    return if !$bridgeid || $bridgeid < 1;
+    return 1 if !defined($bridgeid) || $bridgeid < 1;
+
     my $bridge = "pci.$bridgeid";
     my $devices_list = vm_devices_list($vmid);
 
-    if(!defined($devices_list->{$bridge})) {
-       return undef if !vm_deviceplug($storecfg, $conf, $vmid, $bridge);
+    if (!defined($devices_list->{$bridge})) {
+       vm_deviceplug($storecfg, $conf, $vmid, $bridge);
     }
+
     return 1;
 }
 
+sub qemu_set_link_status {
+    my ($vmid, $device, $up) = @_;
+
+    vm_mon_cmd($vmid, "set_link", name => $device, 
+              up => $up ? JSON::true : JSON::false);
+}
+
 sub qemu_netdevadd {
     my ($vmid, $conf, $device, $deviceid) = @_;
 
@@ -2898,7 +3413,32 @@ sub qemu_netdevdel {
     my ($vmid, $deviceid) = @_;
 
     vm_mon_cmd($vmid, "netdev_del", id => $deviceid);
-    return 1;
+}
+
+sub qemu_cpu_hotplug {
+    my ($vmid, $conf, $cores) = @_;
+
+    my $sockets = $conf->{sockets} || 1;
+    die "cpu hotplug only works with one socket\n"
+       if $sockets > 1;
+
+    die "maxcpus is not defined\n"
+       if !$conf->{maxcpus};
+
+    die "you can't add more cores than maxcpus\n"
+       if $cores > $conf->{maxcpus};
+
+    my $currentcores = $conf->{cores} || 1;
+    die "online cpu unplug is not yet possible\n"
+       if $cores < $currentcores;
+
+    my $currentrunningcores = vm_mon_cmd($vmid, "query-cpus");
+    die "cores number if running vm is different than configuration\n"
+       if scalar(@{$currentrunningcores}) != $currentcores;
+
+    for (my $i = $currentcores; $i < $cores; $i++) {
+       vm_mon_cmd($vmid, "cpu-add", id => int($i));
+    }
 }
 
 sub qemu_block_set_io_throttle {
@@ -3056,16 +3596,360 @@ sub qemu_volume_snapshot_delete {
     vm_mon_cmd($vmid, "delete-drive-snapshot", device => $deviceid, name => $snap);
 }
 
-sub qga_freezefs {
+sub set_migration_caps {
     my ($vmid) = @_;
 
-    #need to impplement call to qemu-ga
+    my $cap_ref = [];
+
+    my $enabled_cap = {
+       "auto-converge" => 1,
+       "xbzrle" => 0,
+       "x-rdma-pin-all" => 0,
+       "zero-blocks" => 0,
+    };
+
+    my $supported_capabilities = vm_mon_cmd_nocheck($vmid, "query-migrate-capabilities");
+
+    for my $supported_capability (@$supported_capabilities) {
+       push @$cap_ref, {
+           capability => $supported_capability->{capability},
+           state => $enabled_cap->{$supported_capability->{capability}} ? JSON::true : JSON::false,
+       };
+    }
+
+    vm_mon_cmd_nocheck($vmid, "migrate-set-capabilities", capabilities => $cap_ref);
 }
 
-sub qga_unfreezefs {
-    my ($vmid) = @_;
+my $fast_plug_option = {
+    'name' => 1,
+    'hotplug' => 1,
+    'onboot' => 1, 
+    'shares' => 1,
+    'startup' => 1,
+};
+
+# hotplug changes in [PENDING]
+# $selection hash can be used to only apply specified options, for
+# example: { cores => 1 } (only apply changed 'cores')
+# $errors ref is used to return error messages
+sub vmconfig_hotplug_pending {
+    my ($vmid, $conf, $storecfg, $selection, $errors) = @_;
+
+    my $defaults = load_defaults();
+
+    # commit values which do not have any impact on running VM first
+    # Note: those option cannot raise errors, we we do not care about
+    # $selection and always apply them.
+
+    my $add_error = sub {
+       my ($opt, $msg) = @_;
+       $errors->{$opt} = "hotplug problem - $msg";
+    };
+
+    my $changes = 0;
+    foreach my $opt (keys %{$conf->{pending}}) { # add/change
+       if ($fast_plug_option->{$opt}) {
+           $conf->{$opt} = $conf->{pending}->{$opt};
+           delete $conf->{pending}->{$opt};
+           $changes = 1;
+       }
+    }
+
+    if ($changes) {
+       update_config_nolock($vmid, $conf, 1);
+       $conf = load_config($vmid); # update/reload
+    }
+
+    my $hotplug = hotplug_enabled($conf);
+
+    my @delete = PVE::Tools::split_list($conf->{pending}->{delete});
+    foreach my $opt (@delete) {
+       next if $selection && !$selection->{$opt};
+       eval {
+           if ($opt eq 'tablet') {
+               die "skip\n" if !$hotplug;
+               if ($defaults->{tablet}) {
+                   vm_deviceplug($storecfg, $conf, $vmid, $opt);
+               } else {
+                   vm_deviceunplug($vmid, $conf, $opt);
+               }
+           } elsif ($opt eq 'cores') {
+               die "skip\n" if !$hotplug;
+               qemu_cpu_hotplug($vmid, $conf, 1);
+            } elsif ($opt eq 'balloon') {
+               # enable balloon device is not hotpluggable
+               die "skip\n" if !defined($conf->{balloon}) || $conf->{balloon};
+           } elsif ($fast_plug_option->{$opt}) {
+               # do nothing
+           } elsif ($opt =~ m/^net(\d+)$/) {
+               die "skip\n" if !$hotplug;
+               vm_deviceunplug($vmid, $conf, $opt);
+           } elsif (valid_drivename($opt)) {
+               die "skip\n" if !$hotplug || $opt =~ m/(ide|sata)(\d+)/;
+               vm_deviceunplug($vmid, $conf, $opt);
+               vmconfig_register_unused_drive($storecfg, $vmid, $conf, parse_drive($opt, $conf->{$opt}));
+           } else {
+               die "skip\n";
+           }
+       };
+       if (my $err = $@) {
+           &$add_error($opt, $err) if $err ne "skip\n";
+       } else {
+           # save new config if hotplug was successful
+           delete $conf->{$opt};
+           vmconfig_undelete_pending_option($conf, $opt);
+           update_config_nolock($vmid, $conf, 1);
+           $conf = load_config($vmid); # update/reload
+       }
+    }
+
+    foreach my $opt (keys %{$conf->{pending}}) {
+       next if $selection && !$selection->{$opt};
+       my $value = $conf->{pending}->{$opt};
+       eval {
+           if ($opt eq 'tablet') {
+               die "skip\n" if !$hotplug;
+               if ($value == 1) {
+                   vm_deviceplug($storecfg, $conf, $vmid, $opt);
+               } elsif ($value == 0) {
+                   vm_deviceunplug($vmid, $conf, $opt);
+               }
+           } elsif ($opt eq 'cores') {
+               die "skip\n" if !$hotplug;
+               qemu_cpu_hotplug($vmid, $conf, $value);
+           } elsif ($opt eq 'balloon') {
+               # enable/disable balloning device is not hotpluggable
+               my $old_balloon_enabled =  !!(!defined($conf->{balloon}) || $conf->{balloon});
+               my $new_balloon_enabled =  !!(!defined($conf->{pending}->{balloon}) || $conf->{pending}->{balloon});            
+               die "skip\n" if $old_balloon_enabled != $new_balloon_enabled;
+
+               # allow manual ballooning if shares is set to zero
+               if (!(defined($conf->{shares}) && ($conf->{shares} == 0))) {
+                   my $balloon = $conf->{pending}->{balloon} || $conf->{memory} || $defaults->{memory};
+                   vm_mon_cmd($vmid, "balloon", value => $balloon*1024*1024);
+               }
+           } elsif ($opt =~ m/^net(\d+)$/) { 
+               # some changes can be done without hotplug
+               vmconfig_update_net($storecfg, $conf, $vmid, $opt, $value);
+           } elsif (valid_drivename($opt)) {
+               # some changes can be done without hotplug
+               vmconfig_update_disk($storecfg, $conf, $vmid, $opt, $value, 1);
+           } else {
+               die "skip\n";  # skip non-hot-pluggable options
+           }
+       };
+       if (my $err = $@) {
+           &$add_error($opt, $err) if $err ne "skip\n";
+       } else {
+           # save new config if hotplug was successful
+           $conf->{$opt} = $value;
+           delete $conf->{pending}->{$opt};
+           update_config_nolock($vmid, $conf, 1);
+           $conf = load_config($vmid); # update/reload
+       }
+    }
+}
+
+sub vmconfig_apply_pending {
+    my ($vmid, $conf, $storecfg) = @_;
+
+    # cold plug
+
+    my @delete = PVE::Tools::split_list($conf->{pending}->{delete});
+    foreach my $opt (@delete) { # delete
+       die "internal error" if $opt =~ m/^unused/;
+       $conf = load_config($vmid); # update/reload
+       if (!defined($conf->{$opt})) {
+           vmconfig_undelete_pending_option($conf, $opt);
+           update_config_nolock($vmid, $conf, 1);
+       } elsif (valid_drivename($opt)) {
+           vmconfig_register_unused_drive($storecfg, $vmid, $conf, parse_drive($opt, $conf->{$opt}));
+           vmconfig_undelete_pending_option($conf, $opt);
+           delete $conf->{$opt};
+           update_config_nolock($vmid, $conf, 1);
+       } else {
+           vmconfig_undelete_pending_option($conf, $opt);
+           delete $conf->{$opt};
+           update_config_nolock($vmid, $conf, 1);
+       }
+    }
+
+    $conf = load_config($vmid); # update/reload
+
+    foreach my $opt (keys %{$conf->{pending}}) { # add/change
+       $conf = load_config($vmid); # update/reload
+
+       if (defined($conf->{$opt}) && ($conf->{$opt} eq $conf->{pending}->{$opt})) {
+           # skip if nothing changed
+       } elsif (valid_drivename($opt)) {
+           vmconfig_register_unused_drive($storecfg, $vmid, $conf, parse_drive($opt, $conf->{$opt}))
+               if defined($conf->{$opt});
+           $conf->{$opt} = $conf->{pending}->{$opt};
+       } else {
+           $conf->{$opt} = $conf->{pending}->{$opt};
+       }
+
+       delete $conf->{pending}->{$opt};
+       update_config_nolock($vmid, $conf, 1);
+    }
+}
+
+my $safe_num_ne = sub {
+    my ($a, $b) = @_;
+
+    return 0 if !defined($a) && !defined($b);
+    return 1 if !defined($a);
+    return 1 if !defined($b);
+
+    return $a != $b;
+};
+
+my $safe_string_ne = sub {
+    my ($a, $b) = @_;
+
+    return 0 if !defined($a) && !defined($b);
+    return 1 if !defined($a);
+    return 1 if !defined($b);
+
+    return $a ne $b;
+};
+
+sub vmconfig_update_net {
+    my ($storecfg, $conf, $vmid, $opt, $value) = @_;
+
+    my $newnet = parse_net($value);
+
+    my $hotplug = hotplug_enabled($conf);
+
+    if ($conf->{$opt}) {
+       my $oldnet = parse_net($conf->{$opt});
+
+       if (&$safe_string_ne($oldnet->{model}, $newnet->{model}) ||
+           &$safe_string_ne($oldnet->{macaddr}, $newnet->{macaddr}) ||
+           &$safe_num_ne($oldnet->{queues}, $newnet->{queues}) ||
+           !($newnet->{bridge} && $oldnet->{bridge})) { # bridge/nat mode change
+
+            # for non online change, we try to hot-unplug
+           die "skip\n" if !$hotplug;
+           vm_deviceunplug($vmid, $conf, $opt);
+       } else {
+
+           die "internal error" if $opt !~ m/net(\d+)/;
+           my $iface = "tap${vmid}i$1";
+               
+           if (&$safe_num_ne($oldnet->{rate}, $newnet->{rate})) {
+               PVE::Network::tap_rate_limit($iface, $newnet->{rate});
+           }
+
+           if (&$safe_string_ne($oldnet->{bridge}, $newnet->{bridge}) ||
+               &$safe_num_ne($oldnet->{tag}, $newnet->{tag}) ||
+               &$safe_num_ne($oldnet->{firewall}, $newnet->{firewall})) {
+               PVE::Network::tap_unplug($iface);
+               PVE::Network::tap_plug($iface, $newnet->{bridge}, $newnet->{tag}, $newnet->{firewall});
+           }
+
+           if (&$safe_string_ne($oldnet->{link_down}, $newnet->{link_down})) {
+               qemu_set_link_status($vmid, $opt, !$newnet->{link_down});
+           }
+
+           return 1;
+       }
+    }
+    
+    if ($hotplug) {
+       vm_deviceplug($storecfg, $conf, $vmid, $opt, $newnet);
+    } else {
+       die "skip\n";
+    }
+}
+
+sub vmconfig_update_disk {
+    my ($storecfg, $conf, $vmid, $opt, $value, $force) = @_;
+
+    # fixme: do we need force?
+
+    my $drive = parse_drive($opt, $value);
+
+    my $hotplug = hotplug_enabled($conf);
+
+    if ($conf->{$opt}) {
 
-    #need to impplement call to qemu-ga
+       if (my $old_drive = parse_drive($opt, $conf->{$opt}))  {
+
+           my $media = $drive->{media} || 'disk';
+           my $oldmedia = $old_drive->{media} || 'disk';
+           die "unable to change media type\n" if $media ne $oldmedia;
+
+           if (!drive_is_cdrom($old_drive)) {
+
+               if ($drive->{file} ne $old_drive->{file}) {  
+
+                   die "skip\n" if !$hotplug;
+
+                   # unplug and register as unused
+                   vm_deviceunplug($vmid, $conf, $opt);
+                   vmconfig_register_unused_drive($storecfg, $vmid, $conf, $old_drive)
+       
+               } else {
+                   # update existing disk
+
+                   # skip non hotpluggable value
+                   if (&$safe_num_ne($drive->{discard}, $old_drive->{discard}) || 
+                       &$safe_string_ne($drive->{cache}, $old_drive->{cache})) {
+                       die "skip\n";
+                   }
+
+                   # apply throttle
+                   if (&$safe_num_ne($drive->{mbps}, $old_drive->{mbps}) ||
+                       &$safe_num_ne($drive->{mbps_rd}, $old_drive->{mbps_rd}) ||
+                       &$safe_num_ne($drive->{mbps_wr}, $old_drive->{mbps_wr}) ||
+                       &$safe_num_ne($drive->{iops}, $old_drive->{iops}) ||
+                       &$safe_num_ne($drive->{iops_rd}, $old_drive->{iops_rd}) ||
+                       &$safe_num_ne($drive->{iops_wr}, $old_drive->{iops_wr}) ||
+                       &$safe_num_ne($drive->{mbps_max}, $old_drive->{mbps_max}) ||
+                       &$safe_num_ne($drive->{mbps_rd_max}, $old_drive->{mbps_rd_max}) ||
+                       &$safe_num_ne($drive->{mbps_wr_max}, $old_drive->{mbps_wr_max}) ||
+                       &$safe_num_ne($drive->{iops_max}, $old_drive->{iops_max}) ||
+                       &$safe_num_ne($drive->{iops_rd_max}, $old_drive->{iops_rd_max}) ||
+                       &$safe_num_ne($drive->{iops_wr_max}, $old_drive->{iops_wr_max})) {
+                       
+                       qemu_block_set_io_throttle($vmid,"drive-$opt",
+                                                  ($drive->{mbps} || 0)*1024*1024,
+                                                  ($drive->{mbps_rd} || 0)*1024*1024,
+                                                  ($drive->{mbps_wr} || 0)*1024*1024,
+                                                  $drive->{iops} || 0,
+                                                  $drive->{iops_rd} || 0,
+                                                  $drive->{iops_wr} || 0,
+                                                  ($drive->{mbps_max} || 0)*1024*1024,
+                                                  ($drive->{mbps_rd_max} || 0)*1024*1024,
+                                                  ($drive->{mbps_wr_max} || 0)*1024*1024,
+                                                  $drive->{iops_max} || 0,
+                                                  $drive->{iops_rd_max} || 0,
+                                                  $drive->{iops_wr_max} || 0);
+
+                   }
+                   
+                   return 1;
+               }
+           }
+       }
+    }
+
+    if (drive_is_cdrom($drive)) { # cdrom
+
+       if ($drive->{file} eq 'none') {
+           vm_mon_cmd($vmid, "eject",force => JSON::true,device => "drive-$opt");
+       } else {
+           my $path = get_iso_path($storecfg, $vmid, $drive->{file});
+           vm_mon_cmd($vmid, "eject", force => JSON::true,device => "drive-$opt"); # force eject if locked
+           vm_mon_cmd($vmid, "change", device => "drive-$opt",target => "$path") if $path;
+       }
+
+    } else { 
+       die "skip\n" if !$hotplug || $opt =~ m/(ide|sata)(\d+)/;   
+       # hotplug new disks
+       vm_deviceplug($storecfg, $conf, $vmid, $opt, $drive);
+    }
 }
 
 sub vm_start {
@@ -3080,6 +3964,11 @@ sub vm_start {
 
        die "VM $vmid already running\n" if check_running($vmid, undef, $migratedfrom);
 
+       if (!$statefile && scalar(keys %{$conf->{pending}})) {
+           vmconfig_apply_pending($vmid, $conf, $storecfg);
+           $conf = load_config($vmid); # update/reload
+       }
+
        my $defaults = load_defaults();
 
        # set environment variable useful inside network script
@@ -3112,11 +4001,22 @@ sub vm_start {
         for (my $i = 0; $i < $MAX_HOSTPCI_DEVICES; $i++)  {
           my $d = parse_hostpci($conf->{"hostpci$i"});
           next if !$d;
-          my $info = pci_device_info("0000:$d->{pciid}");
-          die "IOMMU not present\n" if !check_iommu_support();
-          die "no pci device info for device '$d->{pciid}'\n" if !$info;
-          die "can't unbind pci device '$d->{pciid}'\n" if !pci_dev_bind_to_stub($info);
-          die "can't reset pci device '$d->{pciid}'\n" if !pci_dev_reset($info);
+         my $pcidevices = $d->{pciid};
+         foreach my $pcidevice (@$pcidevices) {
+               my $pciid = $pcidevice->{id}.".".$pcidevice->{function};
+
+               my $info = pci_device_info("0000:$pciid");
+               die "IOMMU not present\n" if !check_iommu_support();
+               die "no pci device info for device '$pciid'\n" if !$info;
+
+               if ($d->{driver} && $d->{driver} eq "vfio") {
+                   die "can't unbind/bind pci group to vfio '$pciid'\n" if !pci_dev_group_bind_to_vfio($pciid);
+               } else {
+                   die "can't unbind/bind to stub pci device '$pciid'\n" if !pci_dev_bind_to_stub($info);
+               }
+
+               die "can't reset pci device '$pciid'\n" if $info->{has_fl_reset} and !pci_dev_reset($info);
+         }
         }
 
        PVE::Storage::activate_volumes($storecfg, $vollist);
@@ -3134,17 +4034,17 @@ sub vm_start {
        }
 
        if ($migratedfrom) {
-           my $capabilities = {};
-           $capabilities->{capability} =  "xbzrle";
-           $capabilities->{state} = JSON::true;
-           eval { vm_mon_cmd_nocheck($vmid, "migrate-set-capabilities", capabilities => [$capabilities]); };
+
+           eval {
+               set_migration_caps($vmid);
+           };
            warn $@ if $@;
-           
+
            if ($spice_port) {
                print "spice listens on port $spice_port\n";
                if ($spice_ticket) {
-                   PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "set_password", protocol => 'spice', password => $spice_ticket);
-                   PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "expire_password", protocol => 'spice', time => "+30");
+                   vm_mon_cmd_nocheck($vmid, "set_password", protocol => 'spice', password => $spice_ticket);
+                   vm_mon_cmd_nocheck($vmid, "expire_password", protocol => 'spice', time => "+30");
                }
            }
 
@@ -3158,6 +4058,12 @@ sub vm_start {
                            property => "guest-stats-polling-interval",
                            value => 2);
            }
+
+           foreach my $opt (keys %$conf) {
+               next if $opt !~  m/^net\d+$/;
+               my $nicconf = parse_net($conf->{$opt});
+               qemu_set_link_status($vmid, $opt, 0) if $nicconf->{link_down};
+           }
        }
     });
 }
@@ -3190,7 +4096,7 @@ sub vm_qmp_command {
     eval {
        die "VM $vmid not running\n" if !check_running($vmid, $nocheck);
        my $sname = qmp_socket($vmid);
-       if (-e $sname) {
+       if (-e $sname) { # test if VM is reasonambe new and supports qmp/qga
            my $qmpclient = PVE::QMPClient->new();
 
            $res = $qmpclient->cmd($vmid, $cmd, $timeout);
@@ -3319,10 +4225,13 @@ sub vm_stop {
 
        eval {
            if ($shutdown) {
-               $nocheck ? vm_mon_cmd_nocheck($vmid, "system_powerdown") : vm_mon_cmd($vmid, "system_powerdown");
-
+               if (defined($conf) && $conf->{agent}) {
+                   vm_qmp_command($vmid, { execute => "guest-shutdown" }, $nocheck);
+               } else {
+                   vm_qmp_command($vmid, { execute => "system_powerdown" }, $nocheck);
+               }
            } else {
-               $nocheck ? vm_mon_cmd_nocheck($vmid, "quit") : vm_mon_cmd($vmid, "quit");
+               vm_qmp_command($vmid, { execute => "quit" }, $nocheck);
            }
        };
        my $err = $@;
@@ -3510,6 +4419,61 @@ sub pci_dev_bind_to_stub {
     return -d $testdir;
 }
 
+sub pci_dev_bind_to_vfio {
+    my ($dev) = @_;
+
+    my $name = $dev->{name};
+
+    my $vfio_basedir = "$pcisysfs/drivers/vfio-pci";
+
+    if (!-d $vfio_basedir) {
+       system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null");
+    }
+    die "Cannot find vfio-pci module!\n" if !-d $vfio_basedir;
+
+    my $testdir = "$vfio_basedir/$name";
+    return 1 if -d $testdir;
+
+    my $data = "$dev->{vendor} $dev->{product}";
+    return undef if !file_write("$vfio_basedir/new_id", $data);
+
+    my $fn = "$pcisysfs/devices/$name/driver/unbind";
+    if (!file_write($fn, $name)) {
+       return undef if -f $fn;
+    }
+
+    $fn = "$vfio_basedir/bind";
+    if (! -d $testdir) {
+       return undef if !file_write($fn, $name);
+    }
+
+    return -d $testdir;
+}
+
+sub pci_dev_group_bind_to_vfio {
+    my ($pciid) = @_;
+
+    my $vfio_basedir = "$pcisysfs/drivers/vfio-pci";
+
+    if (!-d $vfio_basedir) {
+       system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null");
+    }
+    die "Cannot find vfio-pci module!\n" if !-d $vfio_basedir;
+
+    # get IOMMU group devices
+    opendir(my $D, "$pcisysfs/devices/0000:$pciid/iommu_group/devices/") || die "Cannot open iommu_group: $!\n";
+      my @devs = grep /^0000:/, readdir($D);
+    closedir($D);
+
+    foreach my $pciid (@devs) {
+       $pciid =~ m/^([:\.\da-f]+)$/ or die "PCI ID $pciid not valid!\n";
+       my $info = pci_device_info($1);
+       pci_dev_bind_to_vfio($info) || die "Cannot bind $pciid to vfio\n";
+    }
+
+    return 1;
+}
+
 sub print_pci_addr {
     my ($id, $bridges) = @_;
 
@@ -3541,6 +4505,8 @@ sub print_pci_addr {
        vga1 => { bus => 0, addr => 24 },
        vga2 => { bus => 0, addr => 25 },
        vga3 => { bus => 0, addr => 26 },
+       hostpci2 => { bus => 0, addr => 27 },
+       hostpci3 => { bus => 0, addr => 28 },
        #addr29 : usb-host (pve-usb.cfg)
        'pci.1' => { bus => 0, addr => 30 },
        'pci.2' => { bus => 0, addr => 31 },
@@ -3592,6 +4558,26 @@ sub print_pci_addr {
 
 }
 
+sub print_pcie_addr {
+    my ($id) = @_;
+
+    my $res = '';
+    my $devices = {
+       hostpci0 => { bus => "ich9-pcie-port-1", addr => 0 },
+       hostpci1 => { bus => "ich9-pcie-port-2", addr => 0 },
+       hostpci2 => { bus => "ich9-pcie-port-3", addr => 0 },
+       hostpci3 => { bus => "ich9-pcie-port-4", addr => 0 },
+    };
+
+    if (defined($devices->{$id}->{bus}) && defined($devices->{$id}->{addr})) {
+          my $addr = sprintf("0x%x", $devices->{$id}->{addr});
+          my $bus = $devices->{$id}->{bus};
+          $res = ",bus=$bus,addr=$addr";
+    }
+    return $res;
+
+}
+
 # vzdump restore implementaion
 
 sub tar_archive_read_firstfile {
@@ -4065,6 +5051,10 @@ sub restore_vma_archive {
                my ($dev_id, $size, $devname) = ($1, $2, $3);
                $devinfo->{$devname} = { size => $size, dev_id => $dev_id };
            } elsif ($line =~ m/^CTIME: /) {
+               # we correctly received the vma config, so we can disable
+               # the timeout now for disk allocation (set to 10 minutes, so
+               # that we always timeout if something goes wrong)
+               alarm(600);
                &$print_devmap();
                print $fifofh "done\n";
                my $tmp = $oldtimeout || 0;
@@ -4127,7 +5117,7 @@ sub restore_tar_archive {
     my $storecfg = cfs_read_file('storage.cfg');
 
     # destroy existing data - keep empty config
-    my $vmcfgfn = PVE::QemuServer::config_file($vmid);
+    my $vmcfgfn = config_file($vmid);
     destroy_vm($storecfg, $vmid, 1) if -f $vmcfgfn;
 
     my $tocmd = "/usr/lib/qemu-server/qmextract";
@@ -4389,6 +5379,8 @@ my $snapshot_commit = sub {
        die "missing snapshot lock\n"
            if !($conf->{lock} && $conf->{lock} eq 'snapshot');
 
+       my $has_machine_config = defined($conf->{machine});
+
        my $snap = $conf->{snapshots}->{$snapname};
 
        die "snapshot '$snapname' does not exist\n" if !defined($snap);
@@ -4401,6 +5393,8 @@ my $snapshot_commit = sub {
 
        my $newconf = &$snapshot_apply_config($conf, $snap);
 
+       delete $newconf->{machine} if !$has_machine_config;
+
        $newconf->{parent} = $snapname;
 
        update_config_nolock($vmid, $newconf, 1);
@@ -4507,16 +5501,26 @@ my $savevm_wait = sub {
 };
 
 sub snapshot_create {
-    my ($vmid, $snapname, $save_vmstate, $freezefs, $comment) = @_;
+    my ($vmid, $snapname, $save_vmstate, $comment) = @_;
 
     my $snap = &$snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
 
-    $freezefs = $save_vmstate = 0 if !$snap->{vmstate}; # vm is not running
+    $save_vmstate = 0 if !$snap->{vmstate}; # vm is not running
 
-    my $drivehash = {};
+    my $config = load_config($vmid);
 
     my $running = check_running($vmid);
 
+    my $freezefs = $running && $config->{agent};
+    $freezefs = 0 if $snap->{vmstate}; # not needed if we save RAM
+
+    my $drivehash = {};
+
+    if ($freezefs) {
+       eval { vm_mon_cmd($vmid, "guest-fsfreeze-freeze"); };
+       warn "guest-fsfreeze-freeze problems - $@" if $@;
+    }
+
     eval {
        # create internal snapshots of all drives
 
@@ -4532,8 +5536,6 @@ sub snapshot_create {
            }
        };
 
-       qga_freezefs($vmid) if $running && $freezefs;
-
        foreach_drive($snap, sub {
            my ($ds, $drive) = @_;
 
@@ -4548,11 +5550,27 @@ sub snapshot_create {
     };
     my $err = $@;
 
-    eval { qga_unfreezefs($vmid) if $running && $freezefs; };
-    warn $@ if $@;
+    if ($running) {
+       eval { vm_mon_cmd($vmid, "savevm-end")  };
+       warn $@ if $@;
 
-    eval { vm_mon_cmd($vmid, "savevm-end") if $running; };
-    warn $@ if $@;
+       if ($freezefs) {
+           eval { vm_mon_cmd($vmid, "guest-fsfreeze-thaw"); };
+           warn "guest-fsfreeze-thaw problems - $@" if $@;
+       }
+
+       # savevm-end is async, we need to wait
+       for (;;) {
+           my $stat = vm_mon_cmd_nocheck($vmid, "query-savevm");
+           if (!$stat->{bytes}) {
+               last;
+           } else {
+               print "savevm not yet finished\n";
+               sleep(1);
+               next;
+           }
+       }
+    }
 
     if ($err) {
        warn "snapshot create failed: starting cleanup\n";
@@ -4601,10 +5619,12 @@ sub snapshot_delete {
        die "snapshot '$snapname' does not exist\n" if !defined($snap);
 
        # remove parent refs
-       &$unlink_parent($conf, $snap->{parent});
-       foreach my $sn (keys %{$conf->{snapshots}}) {
-           next if $sn eq $snapname;
-           &$unlink_parent($conf->{snapshots}->{$sn}, $snap->{parent});
+       if (!$prepare) {
+           &$unlink_parent($conf, $snap->{parent});
+           foreach my $sn (keys %{$conf->{snapshots}}) {
+               next if $sn eq $snapname;
+               &$unlink_parent($conf->{snapshots}->{$sn}, $snap->{parent});
+           }
        }
 
        if ($remove_drive) {
@@ -4769,82 +5789,96 @@ sub qemu_img_format {
 }
 
 sub qemu_drive_mirror {
-    my ($vmid, $drive, $dst_volid, $vmiddst, $maxwait) = @_;
+    my ($vmid, $drive, $dst_volid, $vmiddst) = @_;
 
-    my $count = 1;
+    my $count = 0;
     my $old_len = 0;
     my $frozen = undef;
+    my $maxwait = 120;
 
     my $storecfg = PVE::Storage::config();
-    my ($dst_storeid, $dst_volname) = PVE::Storage::parse_volume_id($dst_volid, 1);
+    my ($dst_storeid, $dst_volname) = PVE::Storage::parse_volume_id($dst_volid);
 
-    if ($dst_storeid) {
-       my $dst_scfg = PVE::Storage::storage_config($storecfg, $dst_storeid);
+    my $dst_scfg = PVE::Storage::storage_config($storecfg, $dst_storeid);
 
-       my $format;
-        if ($dst_volname =~ m/\.(raw|qcow2)$/){
-           $format = $1;
-       }
+    my $format;
+    if ($dst_volname =~ m/\.(raw|qcow2)$/){
+       $format = $1;
+    }
 
-       my $dst_path = PVE::Storage::path($storecfg, $dst_volid);
+    my $dst_path = PVE::Storage::path($storecfg, $dst_volid);
 
-       if ($format) {
-           #fixme : sometime drive-mirror timeout, but works fine after.
-           # (I have see the problem with big volume > 200GB), so we need to eval
-           eval { vm_mon_cmd($vmid, "drive-mirror", timeout => 10, device => "drive-$drive", mode => "existing",
-                             sync => "full", target => $dst_path, format => $format); };
-       } else {
-           eval { vm_mon_cmd($vmid, "drive-mirror", timeout => 10, device => "drive-$drive", mode => "existing",
-                             sync => "full", target => $dst_path); };
-       }
+    my $opts = { timeout => 10, device => "drive-$drive", mode => "existing", sync => "full", target => $dst_path };
+    $opts->{format} = $format if $format;
 
-       eval {
-           while (1) {
-               my $stats = vm_mon_cmd($vmid, "query-block-jobs");
-               my $stat = @$stats[0];
-               die "mirroring job seem to have die. Maybe do you have bad sectors?" if !$stat;
-               die "error job is not mirroring" if $stat->{type} ne "mirror";
-
-               my $transferred = $stat->{offset};
-               my $total = $stat->{len};
+    #fixme : sometime drive-mirror timeout, but works fine after.
+    # (I have see the problem with big volume > 200GB), so we need to eval
+    eval { vm_mon_cmd($vmid, "drive-mirror", %$opts); };
+    # ignore errors here
+
+    eval {
+       while (1) {
+           my $stats = vm_mon_cmd($vmid, "query-block-jobs");
+           my $stat = @$stats[0];
+           die "mirroring job seem to have die. Maybe do you have bad sectors?" if !$stat;
+           die "error job is not mirroring" if $stat->{type} ne "mirror";
+
+           my $busy = $stat->{busy};
+
+           if (my $total = $stat->{len}) {
+               my $transferred = $stat->{offset} || 0;
                my $remaining = $total - $transferred;
                my $percent = sprintf "%.2f", ($transferred * 100 / $total);
 
-                print "transferred: $transferred bytes remaining: $remaining bytes total: $total bytes progression: $percent %\n";
+               print "transferred: $transferred bytes remaining: $remaining bytes total: $total bytes progression: $percent % busy: $busy\n";
+           }
 
-               last if ($stat->{len} == $stat->{offset});
-               if ($old_len == $stat->{offset}) {
-                   if ($maxwait && $count > $maxwait) {
-                   # if writes to disk occurs the disk needs to be freezed
-                   # to be able to complete the migration
-                       vm_suspend($vmid,1);
-                       $count = 0;
-                       $frozen = 1;
-                   } else {
-                       $count++ unless $frozen;
-                   }
-               } elsif ($frozen) {
-                   vm_resume($vmid,1);
-                   $count = 0;
+           if ($stat->{len} == $stat->{offset}) {
+               if ($busy eq 'false') {
+
+                   last if $vmiddst != $vmid;
+
+                   # try to switch the disk if source and destination are on the same guest
+                   eval { vm_mon_cmd($vmid, "block-job-complete", device => "drive-$drive") };
+                   last if !$@;
+                   die $@ if $@ !~ m/cannot be completed/;
                }
-               $old_len = $stat->{offset};
-               sleep 1;
-           }
 
-           if ($vmiddst == $vmid) {
-               # switch the disk if source and destination are on the same guest
-               vm_mon_cmd($vmid, "block-job-complete", device => "drive-$drive");
+               if ($count > $maxwait) {
+                   # if too much writes to disk occurs at the end of migration
+                   #the disk needs to be freezed to be able to complete the migration
+                   vm_suspend($vmid,1);
+                   $frozen = 1;
+               }
+               $count ++
            }
-       };
-       if (my $err = $@) {
-           eval { vm_mon_cmd($vmid, "block-job-cancel", device => "drive-$drive"); };
-           die "mirroring error: $err";
+           $old_len = $stat->{offset};
+           sleep 1;
        }
 
-       if ($vmiddst != $vmid) {
-           # if we clone a disk for a new target vm, we don't switch the disk
-           vm_mon_cmd($vmid, "block-job-cancel", device => "drive-$drive");
+       vm_resume($vmid, 1) if $frozen;
+
+    };
+    my $err = $@;
+
+    my $cancel_job = sub {
+       vm_mon_cmd($vmid, "block-job-cancel", device => "drive-$drive");
+       while (1) {
+           my $stats = vm_mon_cmd($vmid, "query-block-jobs");
+           my $stat = @$stats[0];
+           last if !$stat;
+           sleep 1;
        }
+    };
+
+    if ($err) {
+       eval { &$cancel_job(); };
+       die "mirroring error: $err";
+    }
+
+    if ($vmiddst != $vmid) {
+       # if we clone a disk for a new target vm, we don't switch the disk
+       &$cancel_job(); # so we call block-job-cancel
     }
 }
 
@@ -4856,7 +5890,7 @@ sub clone_disk {
 
     if (!$full) {
        print "create linked clone of drive $drivename ($drive->{file})\n";
-       $newvolid = PVE::Storage::vdisk_clone($storecfg,  $drive->{file}, $newvmid);
+       $newvolid = PVE::Storage::vdisk_clone($storecfg,  $drive->{file}, $newvmid, $snapname);
        push @$newvollist, $newvolid;
     } else {
        my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file});
@@ -4899,7 +5933,7 @@ sub get_current_qemu_machine {
     my ($vmid) = @_;
 
     my $cmd = { execute => 'query-machines', arguments => {} };
-    my $res = PVE::QemuServer::vm_qmp_command($vmid, $cmd);
+    my $res = vm_qmp_command($vmid, $cmd);
 
     my ($current, $default);
     foreach my $e (@$res) {
@@ -4911,21 +5945,17 @@ sub get_current_qemu_machine {
     return $current || $default || 'pc';
 }
 
-sub read_x509_subject_spice {
-    my ($filename) = @_;
+sub lspci {
 
-    # read x509 subject
-    my $bio = Net::SSLeay::BIO_new_file($filename, 'r');
-    my $x509 = Net::SSLeay::PEM_read_bio_X509($bio);
-    Net::SSLeay::BIO_free($bio);
-    my $nameobj = Net::SSLeay::X509_get_subject_name($x509);
-    my $subject = Net::SSLeay::X509_NAME_oneline($nameobj);
-    Net::SSLeay::X509_free($x509);
-  
-    # remote-viewer wants comma as seperator (not '/')
-    $subject =~ s!^/!!;
-    $subject =~ s!/(\w+=)!,$1!g;
+    my $devices = {};
 
-    return $subject;
+    dir_glob_foreach("$pcisysfs/devices", '[a-f0-9]{4}:([a-f0-9]{2}:[a-f0-9]{2})\.([0-9])', sub {
+            my (undef, $id, $function) = @_;
+           my $res = { id => $id, function => $function};
+           push @{$devices->{$id}}, $res;
+    });
+
+    return $devices;
 }
+
 1;