]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/QemuServer.pm
use aio=native only with O_DIRECT (cache=none|directsync)
[qemu-server.git] / PVE / QemuServer.pm
index 9ee770a9fd2bb9696cb835c272683bd4d7ca4d28..baf0e64df9b8fcb5563a7b98b31beba9cd4635fb 100644 (file)
@@ -549,7 +549,7 @@ PVE::JSONSchema::register_standard_option("pve-qm-ide", $idedesc);
 my $scsidesc = {
     optional => 1,
     type => 'string', format => 'pve-qm-drive',
-    typetext => '[volume=]volume,] [,media=cdrom|disk] [,cyls=c,heads=h,secs=s[,trans=t]] [,snapshot=on|off] [,cache=none|writethrough|writeback|unsafe|directsync] [,format=f] [,backup=yes|no] [,rerror=ignore|report|stop] [,werror=enospc|ignore|report|stop] [,aio=native|threads] [,discard=ignore|on]',
+    typetext => '[volume=]volume,] [,media=cdrom|disk] [,cyls=c,heads=h,secs=s[,trans=t]] [,snapshot=on|off] [,cache=none|writethrough|writeback|unsafe|directsync] [,format=f] [,backup=yes|no] [,rerror=ignore|report|stop] [,werror=enospc|ignore|report|stop] [,aio=native|threads] [,discard=ignore|on] [,iothread=on]',
     description => "Use volume as SCSI hard disk or CD-ROM (n is 0 to " . ($MAX_SCSI_DISKS - 1) . ").",
 };
 PVE::JSONSchema::register_standard_option("pve-qm-scsi", $scsidesc);
@@ -873,7 +873,7 @@ sub parse_hotplug_features {
     my $res = {};
 
     return $res if $data eq '0';
-    
+
     $data = $confdesc->{hotplug}->{default} if $data eq '1';
 
     foreach my $feature (PVE::Tools::split_list($data)) {
@@ -1145,17 +1145,8 @@ sub print_drivedevice_full {
        $device = "virtio-blk-pci,drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}$pciaddr";
        $device .= ",iothread=iothread-$drive->{interface}$drive->{index}" if $drive->{iothread};
     } elsif ($drive->{interface} eq 'scsi') {
-       if ($conf->{scsihw} && ($conf->{scsihw} =~ m/^lsi/)) {
-           $maxdev = 7;
-       } elsif ($conf->{scsihw} && ($conf->{scsihw} =~ m/^virtio-scsi-single/)) {
-           $maxdev = 1;
-       } else {
-           $maxdev = 256;
-       }
-
-       my $controller = int($drive->{index} / $maxdev);
-        my $controller_prefix = ($conf->{scsihw} && $conf->{scsihw} =~ m/^virtio-scsi-single/) ? "virtioscsi" : "scsihw";
 
+       my ($maxdev, $controller, $controller_prefix) = scsihw_infos($conf, $drive);
        my $unit = $drive->{index} % $maxdev;
        my $devicetype = 'hd';
         my $path = '';
@@ -1238,8 +1229,15 @@ sub print_drive_full {
        $opts .= ",$o=" . int($v*1024*1024) if $v;
     }
 
-    # use linux-aio by default (qemu default is threads)
-    $opts .= ",aio=native" if !$drive->{aio};
+    # aio native works only with O_DIRECT
+    if (!$drive->{aio}) {
+       if(!$drive->{cache} || $drive->{cache} eq 'none' || $drive->{cache} eq 'directsync') {
+           $opts .= ",aio=native";
+       } else {
+           $opts .= ",aio=threads";
+       }
+    }
+
 
     my $path;
     my $volid = $drive->{file};
@@ -2872,7 +2870,7 @@ sub config_to_command {
                push @$cpuFlags , 'hv_vapic' if !$nokvm;
                push @$cpuFlags , 'hv_time' if !$nokvm;
 
-           } else { 
+           } else {
                push @$cpuFlags , 'hv_spinlocks=0xffff' if !$nokvm;
            }
        }
@@ -3119,20 +3117,18 @@ sub config_to_command {
 
         if ($drive->{interface} eq 'scsi') {
 
-           my $maxdev = 0;
-           if ($scsihw =~ m/^lsi/) {
-               $maxdev = 7;
-           } elsif ($scsihw =~ m/^virtio-scsi-single/) {
-               $maxdev = 1;
-           } else {
-               $maxdev = 256;
-           }
+           my ($maxdev, $controller, $controller_prefix) = scsihw_infos($conf, $drive);
 
-           my $controller = int($drive->{index} / $maxdev);
-           my $controller_prefix = $scsihw =~ m/^virtio-scsi-single/ ? "virtioscsi" : "scsihw";
            $pciaddr = print_pci_addr("$controller_prefix$controller", $bridges);
-           my $scsihw_type = $scsihw =~ m/^virtio-scsi-single/ ? "virtio-scsi-pci" : $scsihw; 
-           push @$devices, '-device', "$scsihw_type,id=$controller_prefix$controller$pciaddr" if !$scsicontroller->{$controller};
+           my $scsihw_type = $scsihw =~ m/^virtio-scsi-single/ ? "virtio-scsi-pci" : $scsihw;
+
+           my $iothread = '';
+           if($conf->{scsihw} && $conf->{scsihw} eq "virtio-scsi-single" && $drive->{iothread}){
+               $iothread .= ",iothread=iothread-$controller_prefix$controller";
+               push @$cmd, '-object', "iothread,id=iothread-$controller_prefix$controller";
+           }
+
+           push @$devices, '-device', "$scsihw_type,id=$controller_prefix$controller$pciaddr$iothread" if !$scsicontroller->{$controller};
            $scsicontroller->{$controller}=1;
         }
 
@@ -3306,12 +3302,18 @@ sub vm_deviceplug {
 
     } elsif ($deviceid =~ m/^(virtioscsi|scsihw)(\d+)$/) {
 
+
         my $scsihw = defined($conf->{scsihw}) ? $conf->{scsihw} : "lsi";
         my $pciaddr = print_pci_addr($deviceid);
-       my $scsihw_type = $scsihw =~ m/^virtio-scsi-single/ ? "virtio-scsi-pci" : $scsihw; 
+       my $scsihw_type = $scsihw eq 'virtio-scsi-single' ? "virtio-scsi-pci" : $scsihw;
 
         my $devicefull = "$scsihw_type,id=$deviceid$pciaddr";
 
+       if($deviceid =~ m/^virtioscsi(\d+)$/ && $device->{iothread}) {
+           qemu_iothread_add($vmid, $deviceid, $device);
+           $devicefull .= ",iothread=iothread-$deviceid";
+       }
+
         qemu_deviceadd($vmid, $devicefull);
         qemu_deviceaddverify($vmid, $deviceid);
 
@@ -3319,7 +3321,7 @@ sub vm_deviceplug {
 
         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 = $@) {
@@ -3345,12 +3347,12 @@ sub vm_deviceplug {
        my $bridgeid = $2;
        my $pciaddr = print_pci_addr($deviceid);
        my $devicefull = "pci-bridge,id=pci.$bridgeid,chassis_nr=$bridgeid$pciaddr";
-       
+
        qemu_deviceadd($vmid, $devicefull);
        qemu_deviceaddverify($vmid, $deviceid);
 
     } else {
-       die "can't hotplug device '$deviceid'\n";       
+       die "can't hotplug device '$deviceid'\n";
     }
 
     return 1;
@@ -3377,15 +3379,20 @@ sub vm_deviceunplug {
        qemu_iothread_del($conf, $vmid, $deviceid);
 
     } elsif ($deviceid =~ m/^(virtioscsi|scsihw)(\d+)$/) {
-    
+
        qemu_devicedel($vmid, $deviceid);
        qemu_devicedelverify($vmid, $deviceid);
-    
+       qemu_iothread_del($conf, $vmid, $deviceid);
+
     } elsif ($deviceid =~ m/^(scsi)(\d+)$/) {
 
+       #qemu 2.3 segfault on drive_del with virtioscsi + iothread
+       my $device = parse_drive($deviceid, $conf->{$deviceid});
+       die "virtioscsi with iothread is not hot-unplugglable currently" if $device->{iothread};
+
         qemu_devicedel($vmid, $deviceid);
         qemu_drivedel($vmid, $deviceid);
-       qemu_deletescsihw($conf, $vmid, $deviceid);  
+       qemu_deletescsihw($conf, $vmid, $deviceid);
 
     } elsif ($deviceid =~ m/^(net)(\d+)$/) {
 
@@ -3468,12 +3475,12 @@ sub qemu_drivedel {
 
     my $ret = vm_human_monitor_command($vmid, "drive_del drive-$deviceid");
     $ret =~ s/^\s+//;
-    
+
     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; 
-    
+    return 1 if $ret =~ m/Device \'.*?\' not found/s;
+
     die "deleting drive $deviceid failed : $ret\n";
 }
 
@@ -3493,7 +3500,7 @@ sub qemu_deviceaddverify {
 sub qemu_devicedelverify {
     my ($vmid, $deviceid) = @_;
 
-    # need to verify that the device is correctly removed as device_del 
+    # need to verify that the device is correctly removed as device_del
     # is async and empty return is not reliable
 
     for (my $i = 0; $i <= 5; $i++) {
@@ -3508,23 +3515,13 @@ sub qemu_devicedelverify {
 sub qemu_findorcreatescsihw {
     my ($storecfg, $conf, $vmid, $device) = @_;
 
-    my $maxdev = 0;
-    if ($conf->{scsihw} && ($conf->{scsihw} =~ m/^lsi/)) {
-        $maxdev = 7;
-    } elsif ($conf->{scsihw} && ($conf->{scsihw} =~ m/^virtio-scsi-single/)) {
-        $maxdev = 1;
-    } else {
-        $maxdev = 256;
-    }
-
-    my $controller = int($device->{index} / $maxdev);
-    my $controller_prefix = ($conf->{scsihw} && $conf->{scsihw} =~ m/^virtio-scsi-single/) ? "virtioscsi" : "scsihw";
+    my ($maxdev, $controller, $controller_prefix) = scsihw_infos($conf, $device);
 
     my $scsihwid="$controller_prefix$controller";
     my $devices_list = vm_devices_list($vmid);
 
     if(!defined($devices_list->{$scsihwid})) {
-       vm_deviceplug($storecfg, $conf, $vmid, $scsihwid);
+       vm_deviceplug($storecfg, $conf, $vmid, $scsihwid, $device);
     }
 
     return 1;
@@ -3535,13 +3532,12 @@ sub qemu_deletescsihw {
 
     my $device = parse_drive($opt, $conf->{$opt});
 
-    if ($conf->{scsihw} && ($conf->{scsihw} =~ m/^virtio-scsi-single/)) {
+    if ($conf->{scsihw} && ($conf->{scsihw} eq 'virtio-scsi-single')) {
        vm_deviceunplug($vmid, $conf, "virtioscsi$device->{index}");
        return 1;
     }
 
-    my $maxdev = ($conf->{scsihw} && ($conf->{scsihw} !~ m/^lsi/)) ? 256 : 7;
-    my $controller = int($device->{index} / $maxdev);
+    my ($maxdev, $controller, $controller_prefix) = scsihw_infos($conf, $device);
 
     my $devices_list = vm_devices_list($vmid);
     foreach my $opt (keys %{$devices_list}) {
@@ -3587,7 +3583,7 @@ sub qemu_add_pci_bridge {
 sub qemu_set_link_status {
     my ($vmid, $device, $up) = @_;
 
-    vm_mon_cmd($vmid, "set_link", name => $device, 
+    vm_mon_cmd($vmid, "set_link", name => $device,
               up => $up ? JSON::true : JSON::false);
 }
 
@@ -3638,9 +3634,9 @@ sub qemu_memory_hotplug {
     my ($vmid, $conf, $defaults, $opt, $value) = @_;
 
     return $value if !check_running($vmid);
+
     my $memory = $conf->{memory} || $defaults->{memory};
-    $value = $defaults->{memory} if !$value; 
+    $value = $defaults->{memory} if !$value;
     return $value if $value == $memory;
 
     my $static_memory = $STATICMEM;
@@ -3858,7 +3854,7 @@ sub set_migration_caps {
 my $fast_plug_option = {
     'lock' => 1,
     'name' => 1,
-    'onboot' => 1, 
+    'onboot' => 1,
     'shares' => 1,
     'startup' => 1,
 };
@@ -3962,7 +3958,7 @@ sub vmconfig_hotplug_pending {
            } 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});            
+               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
@@ -3970,9 +3966,9 @@ sub vmconfig_hotplug_pending {
                    my $balloon = $conf->{pending}->{balloon} || $conf->{memory} || $defaults->{memory};
                    vm_mon_cmd($vmid, "balloon", value => $balloon*1024*1024);
                }
-           } elsif ($opt =~ m/^net(\d+)$/) { 
+           } elsif ($opt =~ m/^net(\d+)$/) {
                # some changes can be done without hotplug
-               vmconfig_update_net($storecfg, $conf, $hotplug_features->{network}, 
+               vmconfig_update_net($storecfg, $conf, $hotplug_features->{network},
                                    $vmid, $opt, $value);
            } elsif (valid_drivename($opt)) {
                # some changes can be done without hotplug
@@ -4081,7 +4077,7 @@ sub vmconfig_update_net {
 
            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});
            }
@@ -4100,7 +4096,7 @@ sub vmconfig_update_net {
            return 1;
        }
     }
-    
+
     if ($hotplug) {
        vm_deviceplug($storecfg, $conf, $vmid, $opt, $newnet);
     } else {
@@ -4125,19 +4121,19 @@ sub vmconfig_update_disk {
 
            if (!drive_is_cdrom($old_drive)) {
 
-               if ($drive->{file} ne $old_drive->{file}) {  
+               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}) || 
+                   if (&$safe_num_ne($drive->{discard}, $old_drive->{discard}) ||
                        &$safe_string_ne($drive->{iothread}, $old_drive->{iothread}) ||
                        &$safe_string_ne($drive->{cache}, $old_drive->{cache})) {
                        die "skip\n";
@@ -4156,7 +4152,7 @@ sub vmconfig_update_disk {
                        &$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,
@@ -4172,12 +4168,12 @@ sub vmconfig_update_disk {
                                                   $drive->{iops_wr_max} || 0);
 
                    }
-                   
+
                    return 1;
                }
 
            } else { # cdrom
-               
+
                if ($drive->{file} eq 'none') {
                    vm_mon_cmd($vmid, "eject",force => JSON::true,device => "drive-$opt");
                } else {
@@ -4185,13 +4181,13 @@ sub vmconfig_update_disk {
                    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;
                }
-               
+
                return 1;
            }
        }
     }
 
-    die "skip\n" if !$hotplug || $opt =~ m/(ide|sata)(\d+)/;   
+    die "skip\n" if !$hotplug || $opt =~ m/(ide|sata)(\d+)/;
     # hotplug new disks
     vm_deviceplug($storecfg, $conf, $vmid, $opt, $drive);
 }
@@ -4305,7 +4301,7 @@ sub vm_start {
                qemu_set_link_status($vmid, $opt, 0) if $nicconf->{link_down};
            }
        }
-       
+
        vm_mon_cmd_nocheck($vmid, 'qom-set',
                    path => "machine/peripheral/balloon0",
                    property => "guest-stats-polling-interval",
@@ -4428,11 +4424,11 @@ sub vm_stop_cleanup {
            my $vollist = get_vm_volumes($conf);
            PVE::Storage::deactivate_volumes($storecfg, $vollist);
        }
-       
+
        foreach my $ext (qw(mon qmp pid vnc qga)) {
            unlink "/var/run/qemu-server/${vmid}.$ext";
        }
-       
+
        vmconfig_apply_pending($vmid, $conf, $storecfg) if $apply_pending_changes;
     };
     warn $@ if $@; # avoid errors - just warn
@@ -4736,7 +4732,7 @@ sub print_pci_addr {
        #addr2 : first videocard
        balloon0 => { bus => 0, addr => 3 },
        watchdog => { bus => 0, addr => 4 },
-       scsihw0 => { bus => 0, addr => 5 }, 
+       scsihw0 => { bus => 0, addr => 5 },
        'pci.3' => { bus => 0, addr => 5 }, #can also be used for virtio-scsi-single bridge
        scsihw1 => { bus => 0, addr => 6 },
        ahci0 => { bus => 0, addr => 7 },
@@ -6298,4 +6294,23 @@ sub vm_iothreads_list {
     return $iothreads;
 }
 
+sub scsihw_infos {
+    my ($conf, $drive) = @_;
+
+    my $maxdev = 0;
+
+    if ($conf->{scsihw} && ($conf->{scsihw} =~ m/^lsi/)) {
+        $maxdev = 7;
+    } elsif ($conf->{scsihw} && ($conf->{scsihw} eq 'virtio-scsi-single')) {
+        $maxdev = 1;
+    } else {
+        $maxdev = 256;
+    }
+
+    my $controller = int($drive->{index} / $maxdev);
+    my $controller_prefix = ($conf->{scsihw} && $conf->{scsihw} eq 'virtio-scsi-single') ? "virtioscsi" : "scsihw";
+
+    return ($maxdev, $controller, $controller_prefix);
+}
+
 1;