Skip to content

Instantly share code, notes, and snippets.

@darkarnium
Created November 9, 2021 18:36
Show Gist options
  • Save darkarnium/4e1c28766c4c84c1b361a58d65b68fff to your computer and use it in GitHub Desktop.
Save darkarnium/4e1c28766c4c84c1b361a58d65b68fff to your computer and use it in GitHub Desktop.
Terraform Libvirt Patches
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:output method="xml" indent="yes" />
<xsl:param name="targetBusSata" select="'sata'"/>
<xsl:param name="targetBusVirtio" select="'virtio'"/>
<xsl:param name="vcpuPlacementAuto" select="'auto'"/>
<xsl:param name="diskCache" select="'none'"/>
<xsl:param name="diskIoQueues" select="'4'"/>
<xsl:param name="diskIo" select="'native'"/>
<xsl:param name="typeBlock" select="'block'"/>
<xsl:param name="typeVolume" select="'volume'"/>
<!-- Enables per disk and incremental allocation of IO Threads. -->
<xsl:param
name="ioThreadQcow"
select="count(//disk[@device='disk']/driver[@type='qcow2'])"/>
<xsl:param
name="ioThreadRaw"
select="count(//disk[@device='disk']/driver[@type='raw'])"/>
<!-- Start by cloning the whole document -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- Remove Clock, and WWN nodes from disk devices. -->
<xsl:template match="disk/wwn"/>
<!-- Configure vCPU and clock mapping -->
<xsl:template match="/domain/cpu">
<xsl:apply-templates select="node()|@*"/>
<iothreads>
<xsl:value-of select="$ioThreadQcow + $ioThreadRaw"/>
</iothreads>
</xsl:template>
<xsl:template match="vcpu/@placement">
<xsl:apply-templates select="node()|@*"/>
<xsl:attribute name="placement">
<xsl:value-of select="$vcpuPlacementAuto"/>
</xsl:attribute>
</xsl:template>
<!-- Tune cache and I/O settings per disk. -->
<xsl:strip-space elements="*"/>
<xsl:template match="disk[@device='disk']">
<xsl:variable name="n" select="position()"/>
<disk device='disk'>
<!-- Set to block device, or volume, depending on type (qemu vs raw). -->
<xsl:if test="driver[@type='raw']">
<xsl:attribute name="type">
<xsl:value-of select="$typeBlock"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="driver[@type='qcow2']">
<xsl:attribute name="type">
<xsl:value-of select="$typeVolume"/>
</xsl:attribute>
</xsl:if>
<driver name='qemu'>
<xsl:attribute name="type">
<xsl:value-of select="./driver/@type"/>
</xsl:attribute>
<xsl:for-each select="./driver">
<xsl:attribute name="iothread">
<xsl:value-of select="($n mod $ioThreadQcow + $ioThreadRaw) + 1"/>
</xsl:attribute>
<xsl:attribute name="cache">
<xsl:value-of select="$diskCache"/>
</xsl:attribute>
<xsl:attribute name="io">
<xsl:value-of select="$diskIo"/>
</xsl:attribute>
<xsl:attribute name="queues">
<xsl:value-of select="$diskIoQueues"/>
</xsl:attribute>
</xsl:for-each>
</driver>
<xsl:apply-templates select="*[not(self::driver)]"/>
</disk>
</xsl:template>
<!-- Swap ISO / CDROM devices to use sata instead of IDE. -->
<xsl:template match="disk[@device='cdrom']/target/@bus">
<xsl:apply-templates select="node()|@*"/>
<xsl:attribute name="bus">
<xsl:value-of select="$targetBusSata"/>
</xsl:attribute>
</xsl:template>
<!-- Swap disk devices to use virio instead of SCSI. -->
<xsl:template match="disk[@device='disk']/target/@bus">
<xsl:apply-templates select="node()|@*"/>
<xsl:attribute name="bus">
<xsl:value-of select="$targetBusVirtio"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:output method="xml" indent="yes" />
<xsl:param name="targetBusSata" select="'sata'"/>
<xsl:param name="targetBusVirtio" select="'virtio'"/>
<xsl:param name="osLoaderSecure" select="'yes'"/>
<xsl:param name="vcpuPlacementStatic" select="'static'"/>
<xsl:param name="diskCache" select="'none'"/>
<xsl:param name="diskIoQueues" select="'4'"/>
<xsl:param name="diskIo" select="'native'"/>
<!-- Enables per disk and incremental allocation of IO Threads. -->
<xsl:param
name="ioThreads"
select="count(//disk[@device='disk']/driver[@type='qcow2'])"/>
<!-- Start by cloning the whole document -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- Remove Clock, and WWN nodes from disk devices. -->
<xsl:template match="/domain/clock"/>
<xsl:template match="disk/wwn"/>
<!-- Configure vCPU and clock mapping -->
<xsl:template match="/domain/cpu">
<iothreads>
<xsl:value-of select="$ioThreads"/>
</iothreads>
<cpu mode='host-passthrough' check='none'>
<topology sockets='4' cores='2' threads='1'/>
</cpu>
<!-- TODO: All VMs shouldn't be on the same cores! -->
<cputune>
<vcpupin vcpu='0' cpuset='0'/>
<vcpupin vcpu='1' cpuset='1'/>
<vcpupin vcpu='2' cpuset='2'/>
<vcpupin vcpu='3' cpuset='3'/>
<vcpupin vcpu='4' cpuset='4'/>
<vcpupin vcpu='5' cpuset='5'/>
<vcpupin vcpu='6' cpuset='6'/>
<vcpupin vcpu='7' cpuset='7'/>
</cputune>
<clock offset='utc'>
<timer name='hypervclock' present='yes'/>
</clock>
</xsl:template>
<xsl:template match="vcpu/@placement">
<xsl:apply-templates select="node()|@*"/>
<xsl:attribute name="placement">
<xsl:value-of select="$vcpuPlacementStatic"/>
</xsl:attribute>
</xsl:template>
<!-- Tune cache and I/O settings per qcow2 disk. -->
<xsl:strip-space elements="*"/>
<xsl:template match="disk[@device='disk']">
<xsl:variable name="n" select="position()"/>
<disk type='volume' device='disk'>
<driver name='qemu' type='qcow2'>
<xsl:for-each select="./driver[@type='qcow2']">
<xsl:attribute name="iothread">
<xsl:value-of select="($n mod $ioThreads) + 1"/>
</xsl:attribute>
<xsl:attribute name="cache">
<xsl:value-of select="$diskCache"/>
</xsl:attribute>
<xsl:attribute name="io">
<xsl:value-of select="$diskIo"/>
</xsl:attribute>
<xsl:attribute name="queues">
<xsl:value-of select="$diskIoQueues"/>
</xsl:attribute>
</xsl:for-each>
</driver>
<xsl:apply-templates select="*[not(self::driver)]"/>
</disk>
</xsl:template>
<!-- Swap ISO / CDROM devices to use sata instead of IDE. -->
<xsl:template match="disk[@device='cdrom']/target/@bus">
<xsl:apply-templates select="node()|@*"/>
<xsl:attribute name="bus">
<xsl:value-of select="$targetBusSata"/>
</xsl:attribute>
</xsl:template>
<!-- Swap disk devices to use virio instead of SCSI. -->
<xsl:template match="disk[@device='disk']/target/@bus">
<xsl:apply-templates select="node()|@*"/>
<xsl:attribute name="bus">
<xsl:value-of select="$targetBusVirtio"/>
</xsl:attribute>
</xsl:template>
<!-- Ensure the QXL device is used for video - as it has Windows support -->
<xsl:template match="/domain/devices">
<devices>
<xsl:apply-templates select="node()|@*"/>
<video>
<model type="qxl" ram='131072' vram='65536' vgamem='65536' heads='1' primary='yes'></model>
</video>
</devices>
</xsl:template>
<!-- Enable Secure Boot -->
<xsl:template match="loader/@secure">
<xsl:apply-templates select="node()|@*"/>
<xsl:attribute name="secure">
<xsl:value-of select="$osLoaderSecure"/>
</xsl:attribute>
</xsl:template>
<!-- Enable bootmenu -->
<xsl:template match="/domain/os">
<os>
<xsl:apply-templates select="node()|@*"/>
<bootmenu enable='yes'/>
</os>
</xsl:template>
<!-- Enable Hyper-V Enlightenments -->
<xsl:template match="/domain/features">
<features>
<xsl:apply-templates select="node()|@*"/>
<smm state='on'/>
<hyperv>
<relaxed state='on'/>
<vapic state='on'/>
<spinlocks state='on' retries='8191'/>
</hyperv>
<kvm>
<hidden state='on'/>
</kvm>
</features>
</xsl:template>
</xsl:stylesheet>
resource "libvirt_domain" "domain" {
...
# Patch up XML to enable features not exposed by the terraform libvirt provider.
xml {
xslt = file(
var.patch_path != "" ? var.patch_path : "${path.module}/files/patches.xsl"
)
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment