Fix autoinstalls to work around a modern d-i bug
[invirt/packages/invirt-xen-config.git] / invirt-database
1 # -*- mode: python; -*-
2 from invirt.database import models, connect, session
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 = max(memory, 4096)
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
54 if 'installer_options' in locals(): #Installer
55     import shlex
56     install = dict(x.split("=",1) for x in shlex.split(installer_options))
57     if 'ks' in install or 'preseed' in install:
58         if 'ks' in install: # anaconda based installer
59             baseurl = install['mirror']+"/releases/"+install['dist']+"/Everything/"+install['arch']+"/os"
60             kernelurl = baseurl + "/images/pxeboot/vmlinuz"
61             ramdiskurl = baseurl + "/images/pxeboot/initrd.img"
62             extras = ["inst.text",
63                       "inst.repo="+baseurl,
64                       "inst.ks="+install['ks'],
65                       "--", "console=hvc0",
66                       ]
67         elif 'preseed' in install: # d-i based installer
68             baseurl = install['mirror']+"/dists/"+install['dist']+"/main/installer-"+install['arch']+"/current/images"
69             kernelurl = baseurl + "/netboot/xen/vmlinuz"
70             ramdiskurl = baseurl + "/netboot/xen/initrd.gz"
71
72             # For debugging, add "DEBCONF_DEBUG=5" to the arguments.
73             extras = ["auto=true",
74                       "debconf/priority=critical",
75                       'preseed/early_command="grep -v tty0 /etc/inittab >/etc/inittab.new; mv -f /etc/inittab.new /etc/inittab"',
76                       "debian-installer/locale=en_US.UTF-8",
77                       "debian-installer/exit/always_halt=true",
78                       "url="+install['preseed'],
79                       "--", "console=hvc0"]
80         else:
81             raise RuntimeError('unknown new-style autoinstall')
82
83         import urllib
84         class MyUrlOpener(urllib.FancyURLopener):
85             def http_error_default(self, req, fp, code, msg, hdrs):
86                 raise IOError("%s %s" % (code, msg))
87         urlopener = MyUrlOpener()
88
89         try:
90             print "Fetching %s" % kernelurl
91             kernel, _ = urlopener.retrieve(kernelurl)
92             print "Fetching %s" % ramdiskurl
93             ramdisk, _ = urlopener.retrieve(ramdiskurl)
94         except IOError, _:
95             raise
96
97         extra = str.join(" ", extras)
98     else: # Traditional debootstrap-based install
99         disk.append('phy:/dev/xenvg/s_install_hda,hdb,r')
100
101         import os
102         release = os.uname()[2]
103         kernel = '/boot/vmlinuz-%s' % release
104         ramdisk = '/boot/initrd.img-%s' % release
105
106         if not machine.nics:
107             raise RuntimeError('You must have a nic to autoinstall')
108         n = machine.nics[0]
109         extra = 'ro noresume'
110         extra += (' ip=%s::%s:%s:%s:eth0:off'
111                   % (n.ip, n.gateway, n.netmask, machine.name))
112         extra += ' %s' % installer_options
113         root = '/dev/hdb1'
114     codepath = None
115
116     memory = max(memory, 768)
117     maxmem = memory
118 elif cdrom is not None:
119     disk.append('phy:/dev/xenvg/image_' + cdrom.cdrom_id + ',hdc:cdrom,r')
120     boot = 'd'
121     codepath = 'hvm'
122
123 if codepath == 'hvm':
124     xen_platform_pci = 1
125     ioemu = "ioemu:"
126     viftype = "model=pcnet, "
127     kernel = '/usr/lib/xen-4.1/boot/hvmloader'
128     builder = 'hvm'
129     vnc = 1
130     device_model = '/usr/sbin/qemu-dm-invirt'
131     serial = "pty"
132 elif codepath == 'paravm':
133     bootloader = '/usr/bin/pygrub'
134
135
136 for n in machine.nics:
137     other_netparams = ''
138     other_action = n.other_action if n.other_action else ''
139     if other_action == 'renumber':
140         (n.ip, n.netmask, n.gateway,
141          n.other_ip, n.other_netmask, n.other_gateway) = (
142          n.other_ip, n.other_netmask, n.other_gateway,
143          n.ip, n.netmask, n.gateway)
144         other_action = n.other_action = 'dnat'
145         session.add(n)
146         session.flush()
147     if other_action == 'dnat':
148         other_netparams = ('other_ip=%s other_gateway=%s'
149                            % (n.other_ip, n.other_gateway))
150     if other_action == 'remove':
151         n.other_ip = n.other_netmask = n.other_gateway = n.other_action = None
152         session.add(n)
153         session.flush()
154     check(re.match('^[0-9a-fA-F:]+$', n.mac_addr) and re.match('^[0-9.]*$', n.ip))
155     if n.nic_type:
156         viftype = viftype.replace("pcnet", n.nic_type)
157     d = ('%smac=%s, ip=%s, script=vif-invirtroute netdev=%s gateway=%s %s'
158          % (viftype, n.mac_addr, n.ip, config.xen.iface, n.gateway, other_netparams))
159     vif.append(d)
160
161 for d in machine.disks:
162     check(re.match('^[A-Za-z0-9]+$', d.guest_device_name))
163     device = '/dev/xenvg/' + prefix + machine.name + '_' + d.guest_device_name
164     dspec = 'phy:%s,%s%s,w' % (device, diskioemu, d.guest_device_name)
165     disk.append(dspec)
166
167 usbdevice = 'tablet'
168
169 on_poweroff = 'destroy'
170 on_reboot = 'restart'
171 on_crash = 'destroy'
172 if machine.autorestart:
173     on_crash = 'restart'