Skip to content

Commit 527247d

Browse files
author
Ali Polatel
committed
LibVirt: Initial support for virtio-scsi virtual drives (read: ssd trim)
1. Honour `driver_cache` and `driver_discard` keys in storage disk config. 2. Add a SCSI controller if any of the storage disks have SCSI as the target bus. The SCSI model defaults to `virtio-scsi` and may be overriden by the `scsi_model` parameter. The second change is also a bug fix since without a SCSI controller configuration a VM with a virtual disk attached to a SCSI bus won't boot.
1 parent 510f545 commit 527247d

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

lib/Rex/Virtualization/LibVirt/create.pm

+17-3
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,13 @@ sub _set_storage_defaults {
329329
function => "0x0",
330330
};
331331
}
332-
elsif ( $store->{"bus"} eq "ide" && !exists $store->{"address"} ) {
332+
elsif ( ($store->{"bus"} =~ /\Aide|scsi\Z/ && !exists $store->{"address"} ) ) {
333+
# The scsi conditional for the bus works around this error during virsh define:
334+
# error: internal error: SCSI controller only supports 1 bus
333335
$store->{"address"} = {
334336
type => "drive",
335337
controller => 0,
336-
bus => 1,
338+
bus => $store->{"bus"} eq "scsi" ? 0 : 1,
337339
unit => 0,
338340
};
339341
}
@@ -444,7 +446,14 @@ __DATA__
444446
445447
<% for my $disk (@{$::storage}) { %>
446448
<disk type="<%= $disk->{type} %>" device="<%= $disk->{device} %>">
447-
<driver name="qemu" type="<%= $disk->{driver_type} %>"/>
449+
<driver name="qemu" type="<%= $disk->{driver_type} %>"
450+
<% if(exists $disk->{driver_cache}) { %>
451+
cache="<%= $disk->{driver_cache} %>"
452+
<% } %>
453+
<% if(exists $disk->{driver_discard}) { %>
454+
discard="<%= $disk->{driver_discard} %>"
455+
<% } %>
456+
/>
448457
<% if ($disk->{type} eq "file") { %>
449458
<source file="<%= $disk->{file} %>"/>
450459
<% } elsif ($disk->{file} eq "block") { %>
@@ -460,6 +469,11 @@ __DATA__
460469
<controller type="ide" index="0">
461470
<address type="pci" domain="0x0000" bus="0x00" slot="0x01" function="0x1"/>
462471
</controller>
472+
<% if (grep { exists($_->{bus}) && $_->{bus} =~ /scsi/ } @{$::storage}) { %>
473+
<controller type='scsi' index='0' model='<%= $::scsi_model // 'virtio-scsi' %>'>
474+
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
475+
</controller>
476+
<% } %>
463477
<% for my $netdev (@{$::network}) { %>
464478
<interface type="<%= $netdev->{type} %>">
465479
<% if(exists $netdev->{mac}) { %>

0 commit comments

Comments
 (0)