Update changelog
[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 = '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
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                       "debian-installer/locale=en_US.UTF-8",
76                       "debian-installer/exit/always_halt=true",
77                       "url="+install['preseed'],
78                       "--", "console=hvc0"]
79         else:
80             raise RuntimeError('unknown new-style autoinstall')
81
82         import urllib
83         class MyUrlOpener(urllib.FancyURLopener):
84             def http_error_default(self, req, fp, code, msg, hdrs):
85                 raise IOError("%s %s" % (code, msg))
86         urlopener = MyUrlOpener()
87
88         try:
89             print "Fetching %s" % kernelurl
90             kernel, _ = urlopener.retrieve(kernelurl)
91             print "Fetching %s" % ramdiskurl
92             ramdisk, _ = urlopener.retrieve(ramdiskurl)
93         except IOError, _:
94             raise
95
96         extra = str.join(" ", extras)
97     else: # Traditional debootstrap-based install
98         disk.append('phy:/dev/xenvg/s_install_hda,hdb,r')
99
100         import os
101         release = os.uname()[2]
102         kernel = '/boot/vmlinuz-%s' % release
103         ramdisk = '/boot/initrd.img-%s' % release
104
105         if not machine.nics:
106             raise RuntimeError('You must have a nic to autoinstall')
107         n = machine.nics[0]
108         extra = 'ro noresume'
109         extra += (' ip=%s::%s:%s:%s:eth0:off'
110                   % (n.ip, n.gateway, n.netmask, machine.name))
111         extra += ' %s' % installer_options
112         root = '/dev/hdb1'
113     codepath = None
114
115     memory = max(memory, 768)
116     maxmem = memory
117 elif cdrom is not None:
118     disk.append('phy:/dev/xenvg/image_' + cdrom.cdrom_id + ',hdc:cdrom,r')
119     boot = 'd'
120     codepath = 'hvm'
121
122 if codepath == 'hvm':
123     xen_platform_pci = 1
124     ioemu = "ioemu:"
125     viftype = "model=pcnet, "
126     kernel = '/usr/lib/xen-4.1/boot/hvmloader'
127     builder = 'hvm'
128     vnc = 1
129     device_model = '/usr/sbin/qemu-dm-invirt'
130     serial = "pty"
131 elif codepath == 'paravm':
132     bootloader = '/usr/bin/pygrub'
133
134
135 for n in machine.nics:
136     other_netparams = ''
137     other_action = n.other_action if n.other_action else ''
138     if other_action == 'renumber':
139         (n.ip, n.netmask, n.gateway,
140          n.other_ip, n.other_netmask, n.other_gateway) = (
141          n.other_ip, n.other_netmask, n.other_gateway,
142          n.ip, n.netmask, n.gateway)
143         other_action = n.other_action = 'dnat'
144         session.add(n)
145         session.flush()
146     if other_action == 'dnat':
147         other_netparams = ('other_ip=%s other_gateway=%s'
148                            % (n.other_ip, n.other_gateway))
149     if other_action == 'remove':
150         n.other_ip = n.other_netmask = n.other_gateway = n.other_action = None
151         session.add(n)
152         session.flush()
153     check(re.match('^[0-9a-fA-F:]+$', n.mac_addr) and re.match('^[0-9.]*$', n.ip))
154     if n.nic_type:
155         viftype = viftype.replace("pcnet", n.nic_type)
156     d = ('%smac=%s, ip=%s, script=vif-invirtroute netdev=%s gateway=%s %s'
157          % (viftype, n.mac_addr, n.ip, config.xen.iface, n.gateway, other_netparams))
158     vif.append(d)
159
160 for d in machine.disks:
161     check(re.match('^[A-Za-z0-9]+$', d.guest_device_name))
162     device = '/dev/xenvg/' + prefix + machine.name + '_' + d.guest_device_name
163     dspec = 'phy:%s,%s%s,w' % (device, diskioemu, d.guest_device_name)
164     disk.append(dspec)
165
166 usbdevice = 'tablet'
167
168 on_poweroff = 'destroy'
169 on_reboot = 'restart'
170 on_crash = 'destroy'
171 if machine.autorestart:
172     on_crash = 'restart'