fix typo
[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
6 connect()
7 prefix = "d_"
8
9 # 'machine_name', and optionally 'cdrom_image', should get passed in
10 # from the xm create call
11
12 def check(b):
13     if not b:
14         import sys
15         sys.exit(1)
16
17 machine = models.Machine.query.filter_by(name=machine_name).one()
18 check(machine is not None)
19 machine_type = machine.type
20 cdrom = None
21 if 'cdrom_image' in locals():
22     cdrom = models.CDROM.query.filter_by(cdrom_id=cdrom_image).one()
23     check(cdrom is not None)
24
25 memory = machine.memory
26 if machine_type.hvm:
27     # Disable populate-on-demand
28     maxmem = memory
29 else:
30     maxmem = '2048'
31 check(re.match('^[A-Za-z0-9][A-Za-z0-9._-]*$', machine.name))
32 name = prefix + machine_name
33 check(re.match('^[0-9a-f-]+$', machine.uuid))
34 uuid = str(machine.uuid)
35
36 vcpus = machine.cpus
37
38 diskioemu = ""
39 viftype = ""
40
41 pae = machine_type.pae
42 acpi = machine_type.acpi
43 apic = machine_type.apic
44
45 vif = []
46
47 disk = []
48
49 if machine_type.hvm:
50     codepath = 'hvm'
51 else:
52     codepath = 'paravm'
53     if machine_type.type_id == 'linux-pvgrub':
54         use_pvgrub = True
55     else:
56         use_pvgrub = False
57
58 if 'installer_options' in locals(): #Installer
59     import shlex
60     install = dict(x.split("=",1) for x in shlex.split(installer_options))
61     if 'preseed' in install: # d-i based installer
62         baseurl = install['mirror']+"/dists/"+install['dist']+"/main/installer-"+install['arch']+"/current/images"
63         kernelurl = baseurl + "/netboot/xen/vmlinuz"
64         ramdiskurl = baseurl + "/netboot/xen/initrd.gz"
65
66         import urllib
67         class MyUrlOpener(urllib.FancyURLopener):
68             def http_error_default(self, req, fp, code, msg, hdrs):
69                 raise IOError("%s %s" % (code, msg))
70         urlopener = MyUrlOpener()
71
72         try:
73             print "Fetching %s" % kernelurl
74             kernel, _ = urlopener.retrieve(kernelurl)
75             print "Fetching %s" % ramdiskurl
76             ramdisk, _ = urlopener.retrieve(ramdiskurl)
77         except IOError, _:
78             raise
79
80         # For debugging, add "DEBCONF_DEBUG=5" to the arguments.
81         extras = ["auto=true",
82                   "debconf/priority=critical",
83                   "debian-installer/locale=en_US.UTF-8",
84                   "debian-installer/exit/always_halt=true",
85                   "url="+install['preseed'],
86                   "--", "console=hvc0"]
87
88         extra = str.join(" ", extras)
89     else: # Traditional debootstrap-based install
90         disk.append('phy:/dev/xenvg/s_install_hda,hdb,r')
91
92         import os
93         release = os.uname()[2]
94         kernel = '/boot/vmlinuz-%s' % release
95         ramdisk = '/boot/initrd.img-%s' % release
96
97         if not machine.nics:
98             raise RuntimeError('You must have a nic to autoinstall')
99         n = machine.nics[0]
100         extra = 'ro noresume'
101         extra += (' ip=%s::%s:%s:%s:eth0:off'
102                   % (n.ip, config.dhcp.gateway, config.dhcp.netmask, machine.name))
103         extra += ' %s' % installer_options
104         root = '/dev/hdb1'
105     codepath = None
106
107     memory = max(memory, 512)
108 elif cdrom is not None:
109     disk.append('phy:/dev/xenvg/image_' + cdrom.cdrom_id + ',hdc:cdrom,r')
110     boot = 'd'
111     codepath = 'hvm'
112
113 if codepath == 'hvm':
114     xen_platform_pci = 1
115     ioemu = "ioemu:"
116     viftype = "model=pcnet, "
117     kernel = '/usr/lib/xen-4.1/boot/hvmloader'
118     builder = 'hvm'
119     vnc = 1
120     device_model = '/usr/sbin/qemu-dm-invirt'
121     serial = "pty"
122 elif codepath == 'paravm':
123     if use_pvgrub:
124         kernel = '/usr/lib/grub/grub.xen'
125     else:
126         bootloader = '/usr/bin/pygrub'
127
128 for n in machine.nics:
129     check(re.match('^[0-9a-fA-F:]+$', n.mac_addr) and re.match('^[0-9.]*$', n.ip))
130     if n.nic_type:
131         viftype = viftype.replace("pcnet", n.nic_type)
132     d = ('%smac=%s, ip=%s, script=vif-invirtroute netdev=%s'
133          % (viftype, n.mac_addr, n.ip, config.xen.iface))
134     vif.append(d)
135
136 for d in machine.disks:
137     check(re.match('^[A-Za-z0-9]+$', d.guest_device_name))
138     device = '/dev/xenvg/' + prefix + machine.name + '_' + d.guest_device_name
139     dspec = 'phy:%s,%s%s,w' % (device, diskioemu, d.guest_device_name)
140     disk.append(dspec)
141
142 usbdevice = 'tablet'
143
144 on_poweroff = 'destroy'
145 on_reboot = 'restart'
146 on_crash = 'destroy'
147 if machine.autorestart:
148     on_crash = 'restart'