Move libvirt XML generation to separate module.
[mclu.git] / libvirt_xml.py
1 #!/usr/bin/python
2 # mclu (mini cluster)
3 # Copyright (C) 2014 Red Hat Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 import os
20 import sys
21
22 # Generate libvirt XML.  Would be nice to use virt-install here, but
23 # it doesn't work: RHBZ#1095789
24 def generate_libvirt_xml (vm_name, memory, vcpus, virtio, output):
25     network_model = "virtio"
26     if not virtio:
27         network_model = "e1000"
28
29     # XXX Quoting, and we should use a real XML generator.
30     xml = """
31 <domain type='kvm'>
32   <name>%s</name>
33   <memory unit='MiB'>%d</memory>
34   <currentMemory unit='MiB'>%d</currentMemory>
35   <vcpu>%d</vcpu>
36   <os>
37     <type>hvm</type>
38     <boot dev='hd'/>
39   </os>
40   <features>
41     <acpi/>
42     <apic/>
43     <pae/>
44   </features>
45   <cpu mode='host-model' fallback='allow' />
46   <clock offset='utc'>
47     <timer name='rtc' tickpolicy='catchup'/>
48     <timer name='pit' tickpolicy='delay'/>
49     <timer name='hpet' present='no'/>
50   </clock>
51   <on_poweroff>destroy</on_poweroff>
52   <on_reboot>restart</on_reboot>
53   <on_crash>restart</on_crash>
54   <devices>
55     <interface type='bridge'>
56       <source bridge='br0'/>
57       <model type='%s'/>
58     </interface>
59     <console type='pty'>
60       <target type='virtio' port='0'/>
61     </console>
62     <input type='tablet' bus='usb'/>
63     <input type='mouse' bus='ps2'/>
64     <!-- <input type='keyboard' bus='ps2'/> -->
65     <graphics type='vnc' autoport='yes'/>
66     <video>
67       <model type='cirrus' vram='9216' heads='1'/>
68     </video>
69 """ % (vm_name, memory, memory, vcpus, network_model)
70
71     # virtio-scsi or IDE disk:
72     if virtio:
73         xml += """
74     <controller type="scsi" index="0" model="virtio-scsi"/>
75     <disk type='file' device='disk'>
76       <driver name='qemu' type='qcow2' cache='none' io='native'/>
77       <source file='%s'/>
78       <target dev='sda' bus='scsi'/>
79     </disk>
80 """ % output
81     else:
82         xml += """
83     <disk type='file' device='disk'>
84       <driver name='qemu' type='qcow2' cache='none' io='native'/>
85       <source file='%s'/>
86       <target dev='sda' bus='ide'/>
87     </disk>
88 """ % output
89
90     xml += """
91   </devices>
92 </domain>
93 """
94
95     return xml