{%- import 'libvirt_disks.jinja' as libvirt_disks -%}
<domain type='{{ hypervisor }}'>
        <name>{{ name }}</name>
        <vcpu>{{ cpu }}</vcpu>
        {%- if mem.max %}
        <maxMemory {{ mem.slots }} unit='KiB'> {{ mem.max }}</maxMemory>
        {%- endif %}
        {%- if mem.boot %}
        <memory unit='KiB'>{{ mem.boot }}</memory>
        {%- endif %}
        {%- if mem.current %}
        <currentMemory unit='KiB'>{{ mem.current }}</currentMemory>
        {%- endif %}
        {%- if mem %}
        <memtune>
            {%- if 'hard_limit' in mem and mem.hard_limit %}
                <hard_limit unit="KiB">{{ mem.hard_limit }}</hard_limit>
            {%- endif %}
            {%- if 'soft_limit' in mem and mem.soft_limit %}
                <soft_limit unit="KiB">{{ mem.soft_limit }}</soft_limit>
            {%- endif %}
            {%- if 'swap_hard_limit' in mem and mem.swap_hard_limit %}
                <swap_hard_limit unit="KiB">{{ mem.swap_hard_limit }}</swap_hard_limit>
            {%- endif %}
            {%- if 'min_guarantee' in mem and mem.min_guarantee %}
                <min_guarantee unit="KiB">{{ mem.min_guarantee }}</min_guarantee>
            {%- endif %}
        </memtune>
        {%- endif %}
        <os {{ boot.os_attrib }}>
                <type arch='{{ arch }}'>{{ os_type }}</type>
                {% if boot %}
                  {% if 'kernel' in boot %}
                    <kernel>{{ boot.kernel }}</kernel>
                  {% endif %}
                  {% if 'initrd' in boot %}
                    <initrd>{{ boot.initrd }}</initrd>
                  {% endif %}
                  {% if 'cmdline' in boot %}
                    <cmdline>{{ boot.cmdline }}</cmdline>
                  {% endif %}
                  {% if 'loader' in boot %}
                    <loader readonly='yes' type='pflash'>{{ boot.loader }}</loader>
                  {% endif %}
                  {% if 'nvram' in boot %}
                    <nvram template='{{boot.nvram}}'></nvram>
                  {% endif %}
                {% endif %}
                {% for dev in boot_dev %}
                <boot dev='{{ dev }}' />
                {% endfor %}
        </os>
        <devices>
                {% for disk in disks %}
                <disk type='{{ disk.type }}' device='{{ disk.device }}'>
                        {% if disk.type == 'file' and 'source_file' in disk -%}
                        <source file='{{ disk.source_file }}' />
                        {% endif %}
                        {% if disk.type == 'block' -%}
                        <source dev='{{ disk.source_file }}' />
                        {% endif %}
                        {% if disk.type == 'volume' and 'pool' in disk -%}
                        <source pool='{{ disk.pool }}' volume='{{ disk.volume }}' />
                        {% endif %}
                        {%- if disk.type == 'network' %}{{ libvirt_disks.network_source(disk) }}{%- endif %}
                        <target dev='{{ disk.target_dev }}' bus='{{ disk.disk_bus }}' />
                        {% if disk.address -%}
                        <address type='drive' controller='0' bus='0' target='0' unit='{{ disk.index }}' />
                        {% endif %}
                        {% if disk.driver -%}
                        <driver name='qemu' type='{{ disk.format}}' cache='none' io='native'/>
                        {% endif %}
                </disk>
                {% endfor %}

                {% if controller_model %}
                <controller type='scsi' index='0' model='{{ controller_model }}' />
                {% endif %}

                {% for nic in nics %}
                <interface type='{{ nic.type }}'>
                    <source {{ nic.type }}='{{ nic.source }}'/>
                    {% if nic.get('mac') -%}
                    <mac address='{{ nic.mac }}'/>
                    {%- endif %}
                    {% if nic.model %}<model type='{{ nic.model }}'/>{% endif %}
                </interface>
                {% endfor %}
                {% if graphics %}
                <graphics type='{{ graphics.type }}'
                          {% if graphics.listen.address %}
                          listen='{{ graphics.listen.address }}'
                          {% endif %}
                          {% if graphics.port %}
                          port='{{ graphics.port }}'
                          {% endif %}
                          {% if graphics.type == 'spice' and graphics.tls_port %}
                          tlsPort='{{ graphics.tls_port }}'
                          {% endif %}
                          autoport='{{ 'no' if graphics.port or graphics.tls_port else 'yes' }}'>
                    <listen type='{{ graphics.listen.type }}'
                            {% if graphics.listen.address %}
                            address='{{ graphics.listen.address }}'
                            {% endif %}/>
                </graphics>
                {% endif %}
                {% if serial_type == 'pty' %}
                <serial type='pty'>
                        <target port='0'/>
                </serial>
                {% if console %}
                <console type='pty'>
                        <target type='serial' port='0'/>
                </console>
                {% endif %}
                {% endif %}

                {% if serial_type == 'tcp' %}
                <serial type='tcp'>
                        <source mode='bind' host='' service='{{ telnet_port }}'/>
                        <protocol type='telnet'/>
                        <target port='0'/>
                </serial>
                {% if console %}
                <console type='tcp'>
                        <source mode='bind' host='' service='{{ telnet_port }}'/>
                        <protocol type='telnet'/>
                        <target type='serial' port='0'/>
                </console>
                {% endif %}
                {% endif %}

        </devices>
        <features>
                <acpi />
        </features>
</domain>
