When building a guest, give it a fixed (but random) MAC address.
[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 random
21 import sys
22
23 # Generate libvirt XML.  Would be nice to use virt-install here, but
24 # it doesn't work: RHBZ#1095789
25 def generate_libvirt_xml (vm_name, memory, vcpus, virtio, output):
26     network_model = "virtio"
27     if not virtio:
28         network_model = "e1000"
29
30     # Give the network a fixed MAC address, otherwise libvirt will
31     # generate a random one on every guest restart.
32     network_mac = "52:54:00:%02x:%02x:%02x" % (random.randint (0, 0xff),
33                                                random.randint (0, 0xff),
34                                                random.randint (0, 0xff))
35
36     # XXX Quoting, and we should use a real XML generator.
37     xml = """
38 <domain type='kvm'>
39   <name>%s</name>
40   <memory unit='MiB'>%d</memory>
41   <currentMemory unit='MiB'>%d</currentMemory>
42   <vcpu>%d</vcpu>
43   <os>
44     <type>hvm</type>
45     <boot dev='hd'/>
46   </os>
47   <features>
48     <acpi/>
49     <apic/>
50     <pae/>
51   </features>
52   <cpu mode='host-model' fallback='allow' />
53   <clock offset='utc'>
54     <timer name='rtc' tickpolicy='catchup'/>
55     <timer name='pit' tickpolicy='delay'/>
56     <timer name='hpet' present='no'/>
57   </clock>
58   <on_poweroff>destroy</on_poweroff>
59   <on_reboot>restart</on_reboot>
60   <on_crash>restart</on_crash>
61   <devices>
62     <interface type='bridge'>
63       <mac address='%s'/>
64       <source bridge='br0'/>
65       <model type='%s'/>
66     </interface>
67     <console type='pty'>
68       <target type='virtio' port='0'/>
69     </console>
70     <input type='tablet' bus='usb'/>
71     <input type='mouse' bus='ps2'/>
72     <!-- <input type='keyboard' bus='ps2'/> -->
73     <graphics type='vnc' autoport='yes'/>
74     <video>
75       <model type='cirrus' vram='9216' heads='1'/>
76     </video>
77 """ % (vm_name, memory, memory, vcpus, network_mac, network_model)
78
79     # virtio-scsi or IDE disk:
80     if virtio:
81         xml += """
82     <controller type="scsi" index="0" model="virtio-scsi"/>
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='scsi'/>
87     </disk>
88 """ % output
89     else:
90         xml += """
91     <disk type='file' device='disk'>
92       <driver name='qemu' type='qcow2' cache='none' io='native'/>
93       <source file='%s'/>
94       <target dev='sda' bus='ide'/>
95     </disk>
96 """ % output
97
98     xml += """
99   </devices>
100 </domain>
101 """
102
103     return xml