Skip to content

Commit a2e15c2

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 a2e15c2

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

lib/Rex/Virtualization/LibVirt/create.pm

+19-3
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,15 @@ sub _set_storage_defaults {
329329
function => "0x0",
330330
};
331331
}
332-
elsif ( $store->{"bus"} eq "ide" && !exists $store->{"address"} ) {
332+
elsif (
333+
( $store->{"bus"} =~ /\Aide|scsi\Z/ && !exists $store->{"address"} ) )
334+
{
335+
# The scsi conditional for the bus works around this error during virsh define:
336+
# error: internal error: SCSI controller only supports 1 bus
333337
$store->{"address"} = {
334338
type => "drive",
335339
controller => 0,
336-
bus => 1,
340+
bus => $store->{"bus"} eq "scsi" ? 0 : 1,
337341
unit => 0,
338342
};
339343
}
@@ -444,7 +448,14 @@ __DATA__
444448
445449
<% for my $disk (@{$::storage}) { %>
446450
<disk type="<%= $disk->{type} %>" device="<%= $disk->{device} %>">
447-
<driver name="qemu" type="<%= $disk->{driver_type} %>"/>
451+
<driver name="qemu" type="<%= $disk->{driver_type} %>"
452+
<% if(exists $disk->{driver_cache}) { %>
453+
cache="<%= $disk->{driver_cache} %>"
454+
<% } %>
455+
<% if(exists $disk->{driver_discard}) { %>
456+
discard="<%= $disk->{driver_discard} %>"
457+
<% } %>
458+
/>
448459
<% if ($disk->{type} eq "file") { %>
449460
<source file="<%= $disk->{file} %>"/>
450461
<% } elsif ($disk->{file} eq "block") { %>
@@ -460,6 +471,11 @@ __DATA__
460471
<controller type="ide" index="0">
461472
<address type="pci" domain="0x0000" bus="0x00" slot="0x01" function="0x1"/>
462473
</controller>
474+
<% if (grep { exists($_->{bus}) && $_->{bus} =~ /scsi/ } @{$::storage}) { %>
475+
<controller type='scsi' index='0' model='<%= $::scsi_model // 'virtio-scsi' %>'>
476+
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
477+
</controller>
478+
<% } %>
463479
<% for my $netdev (@{$::network}) { %>
464480
<interface type="<%= $netdev->{type} %>">
465481
<% if(exists $netdev->{mac}) { %>

0 commit comments

Comments
 (0)