Pin libxen3 on the hosts until we're ready to upgrade to Xen 3.3
[invirt/packages/invirt-xen-config.git] / invirt-database
1 # -*- mode: python; -*-
2 from invirt.database import models, connect
3 from invirt.config import structs as config
4 import re
5 import tempfile
6 from subprocess import call
7
8 connect()
9 prefix = "d_"
10
11 # 'machine_name', and optionally 'cdrom_image', should get passed in
12 # from the xm create call
13
14 def check(b):
15     if not b:
16         import sys
17         sys.exit(1)
18
19 machine = models.Machine.query().filter_by(name=machine_name).one()
20 check(machine is not None)
21 machine_type = machine.type
22 cdrom = None
23 if 'cdrom_image' in locals():
24     cdrom = models.CDROM.query().filter_by(cdrom_id=cdrom_image).one()
25     check(cdrom is not None)
26
27 memory = machine.memory
28 maxmem = '2048'
29 check(re.match('^[A-Za-z0-9][A-Za-z0-9._-]*$', machine.name))
30 name = prefix + machine.name
31 check(re.match('^[0-9a-f-]+$', machine.uuid))
32 uuid = machine.uuid
33
34 vcpus = machine.cpus
35
36 diskioemu = ""
37 viftype = ""
38
39 pae = machine_type.pae
40 acpi = machine_type.acpi
41 apic = machine_type.apic
42
43 vif = []
44
45 disk = []
46
47 if machine_type.hvm:
48     codepath = 'hvm'
49 else:
50     codepath = 'paravm'
51
52 if 'installer_options' in locals(): #Installer
53     disk.append('phy:/dev/xenvg/s_install_hda,hdb,r')
54
55     import os
56     release = os.uname()[2]
57     kernel = '/boot/vmlinuz-%s' % release
58     ramdisk = '/boot/initrd.img-%s' % release
59
60     if not machine.nics:
61         raise RuntimeError('You must have a nic to autoinstall')
62     n = machine.nics[0]
63     extra = 'ro noresume'
64     extra += (' ip=%s::%s:%s:%s:eth0:off'
65          % (n.ip, config.dhcp.gateway, config.dhcp.netmask, machine.name))
66     extra += ' %s' % installer_options
67     root = '/dev/hdb1'
68     codepath = None
69 elif cdrom is not None:
70     disk.append('phy:/dev/xenvg/image_' + cdrom.cdrom_id + ',hdc:cdrom,r')
71     boot = 'd'
72     codepath = 'hvm'
73
74 if codepath == 'hvm':
75     ioemu = "ioemu:"
76     viftype = "type=ioemu, "
77     kernel = '/usr/lib/xen/boot/hvmloader'
78     builder = 'hvm'
79     vnc = 1
80     vncpasswd = 'moocow'
81     device_model = '/usr/sbin/qemu-dm-invirt'
82     serial = "pty"
83 elif codepath == 'paravm':
84     bootloader = '/usr/bin/pygrub'
85
86
87 for n in machine.nics:
88     check(re.match('^[0-9a-fA-F:]+$', n.mac_addr) and re.match('^[0-9.]*$', n.ip))
89     d = '%smac=%s, ip=%s, script=vif-invirtroute netdev=eth2' % (viftype, n.mac_addr, n.ip)
90     vif.append(d)
91
92 for d in machine.disks:
93     check(re.match('^[A-Za-z0-9]+$', d.guest_device_name))
94     device = '/dev/xenvg/' + prefix + machine.name + '_' + d.guest_device_name
95     dspec = 'phy:%s,%s%s,w' % (device, diskioemu, d.guest_device_name)
96     disk.append(dspec)
97
98 usbdevice = 'tablet'
99
100 on_poweroff = 'destroy'
101 on_reboot = 'restart'
102 on_crash = 'destroy'
103 if machine.autorestart:
104     on_crash = 'restart'