<domain type='{{ definition[1]['driver'] | default('kvm') }}'>
  <name>{{ libvirt_resource_name }}_{{ definition[2] }}</name>
  <memory unit='KiB'>{{ definition[1]['memory'] * 1024 | int }}</memory>
  <vcpu placement='static'>{{ definition[1]['vcpus'] }}</vcpu>
  <os>
    <type arch='{{ definition[1]['arch'] | default('x86_64') }}'>hvm</type>
    <boot dev='{{ definition[1]['boot_dev'] | default('hd') }}'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <pae/>
  </features>
  <cpu mode='host-model'>
    <model fallback='allow' />
  </cpu>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <devices>
{% if definition[2]['driver'] is defined and definition[0]['driver'] == 'qemu' %}
    <emulator>/usr/bin/qemu-system-x86_64</emulator>
{% else %}
    <emulator>{{ qemu_path }}qemu-kvm</emulator>
{% endif %}

{% if virt_type == 'cloud-init' %}
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='/tmp/vm-{{ libvirt_resource_name }}_{{ definition[2] }}.iso'/>
      <backingStore/>
      <target dev='hda' bus='ide'/>
      <readonly/>
    </disk>
{% endif %}
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='{{ definition[3] }}/{{ libvirt_resource_name }}_{{ definition[2] }}.{{ definition[4] }}'/>
      <target dev='vda' bus='virtio'/>
    </disk>
    {% if definition[1]['storage'] is defined %}
      {% for storage_def in definition[1]['storage'] %}
        <disk type='file' device='disk'>
          <driver name='qemu' type='qcow2'/>
          {% if storage_def['source'] is not defined %}
            <source file='/var/lib/libvirt/images/linchpin/{{ libvirt_resource_name }}-{{ storage_def['name'] }}.img' />
          {% elif storage_def['source']['file'] is defined %}
            {% if storage_def['source']['file'].startswith('/') %}
              <source file='{{ storage_def['source']['file'] }}'/>
            {% else %}
              <source file='{{ libvirt_image_path }}/{{ storage_def['source']['file'] }}'/>
            {% endif %}
          {% elif storage_def['source']['block'] is defined %}
            <source block='{{ storage_def['source']['block'] }}'/>
          {% elif storage_def['source']['dir'] is defined %}
            <source dir='{{ storage_def['source']['dir'] }}'/>
          {% elif storage_def['source']['volume'] is defined and sourage_def['source']['pool'] is defined %}
            <source pool='{{ storage_def['source']['pool'] }}' volume='{{ storage_def['source']['volume'] }}'/>
          {% elif storage_def['network_protocol'] is defined %}
            {% if storage_def['source']['network_protocol'] ==  'rbd' or storage_def['source']['network_protocol'] ==  'sheepdog' or storage_def['source']['network_protocol'] ==  'gluster' or storage_def['source']['network_protocol'] ==  'vxhs' %}
              <source protocol='{{ storage_def['source']['network_protocol'] }}' name='{{ storage_def['source']['network_name'] }}' />
            {% endif %}
          {% endif %}
          {% if storage_def['disk_type'] is not defined or storage_def['disk_type'] == 'virtio_blk' %}
            target dev='{{ storage_def['device'] | default('vda') }}' bus='virtio' />
          {% else %}
            <target dev='{{ storage_def['device'] | default('vda') }}' bus='scsi' />
            <address type='drive' controller='0' />
          {% endif %}
          {% if storage_def['shareable'] is defined %}
            <shareable />
          {% endif %}
        </disk>
        {% if storage_def['disk_type'] is defined and storage_def['disk_type'] == 'virtio_scsi' %}
          <controller type='scsi' model='virtio-scsi' />
        {% endif %}
      {% endfor %}
    {% endif %}
{% for network in definition[1]['networks'] %}
    <interface type='network'>
      <model type='virtio'/>
      <source network='{{ network['name'] }}'/>
      <guest dev='eth{{ loop.index0 }}'/>
  {% if network['mac'] is defined %}
    <mac address='{{ network['mac'] }}'/>
  {% endif %}
    </interface>
{% endfor %}
    <controller type='usb' index='0'/>
    <controller type='pci' index='0' model='pci-root'/>
    <serial type='pty'>
      <target port='0'/>
    </serial>
    <console type='pty' tty='/dev/pts/6'>
      <source path='/dev/pts/6'/>
      <target type='serial' port='0'/>
      <alias name='serial0'/>
    </console>
    <input type='mouse' bus='ps2'/>
    <input type='keyboard' bus='ps2'/>
    <graphics type='vnc' port='-1' autoport='yes' listen='127.0.0.1' keymap='en-us'>
      <listen type='address' address='127.0.0.1'/>
    </graphics>
    <video>
      <model type='cirrus' vram='16384' heads='1'/>
    </video>
  </devices>
</domain>
