Disable proxy_arp on the 'all' interface
[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 maxmem = '2048'
27 check(re.match('^[A-Za-z0-9][A-Za-z0-9._-]*$', machine.name))
28 name = prefix + machine.name
29 check(re.match('^[0-9a-f-]+$', machine.uuid))
30 uuid = machine.uuid
31
32 vcpus = machine.cpus
33
34 diskioemu = ""
35 viftype = ""
36
37 pae = machine_type.pae
38 acpi = machine_type.acpi
39 apic = machine_type.apic
40
41 vif = []
42
43 disk = []
44
45 if machine_type.hvm:
46     codepath = 'hvm'
47 else:
48     codepath = 'paravm'
49
50 if 'installer_options' in locals(): #Installer
51     import shlex
52     install = dict(x.split("=",1) for x in shlex.split(installer_options))
53     if 'preseed' in install: # d-i based installer
54         baseurl = install['mirror']+"/dists/"+install['dist']+"/main/installer-"+install['arch']+"/current/images"
55         kernelurl = baseurl + "/netboot/xen/vmlinuz"
56         ramdiskurl = baseurl + "/netboot/xen/initrd.gz"
57
58         import urllib
59         class MyUrlOpener(urllib.FancyURLopener):
60             def http_error_default(self, req, fp, code, msg, hdrs):
61                 raise IOError("%s %s" % (code, msg))
62         urlopener = MyUrlOpener()
63
64         try:
65             print "Fetching %s" % kernelurl
66             kernel, _ = urlopener.retrieve(kernelurl)
67             print "Fetching %s" % ramdiskurl
68             ramdisk, _ = urlopener.retrieve(ramdiskurl)
69         except IOError, _:
70             raise
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
80         extra = str.join(" ", extras)
81     else: # Traditional debootstrap-based install
82         disk.append('phy:/dev/xenvg/s_install_hda,hdb,r')
83
84         import os
85         release = os.uname()[2]
86         kernel = '/boot/vmlinuz-%s' % release
87         ramdisk = '/boot/initrd.img-%s' % release
88
89         if not machine.nics:
90             raise RuntimeError('You must have a nic to autoinstall')
91         n = machine.nics[0]
92         extra = 'ro noresume'
93         extra += (' ip=%s::%s:%s:%s:eth0:off'
94                   % (n.ip, config.dhcp.gateway, config.dhcp.netmask, machine.name))
95         extra += ' %s' % installer_options
96         root = '/dev/hdb1'
97     codepath = None
98
99     memory = max(memory, 512)
100 elif cdrom is not None:
101     disk.append('phy:/dev/xenvg/image_' + cdrom.cdrom_id + ',hdc:cdrom,r')
102     boot = 'd'
103     codepath = 'hvm'
104
105 if codepath == 'hvm':
106     ioemu = "ioemu:"
107     viftype = "type=ioemu, "
108     kernel = '/usr/lib/xen/boot/hvmloader'
109     builder = 'hvm'
110     vnc = 1
111     device_model = '/usr/sbin/qemu-dm-invirt'
112     serial = "pty"
113 elif codepath == 'paravm':
114     bootloader = '/usr/bin/pygrub'
115
116
117 for n in machine.nics:
118     check(re.match('^[0-9a-fA-F:]+$', n.mac_addr) and re.match('^[0-9.]*$', n.ip))
119     d = ('%smac=%s, ip=%s, script=vif-invirtroute netdev=%s'
120          % (viftype, n.mac_addr, n.ip, config.xen.iface))
121     vif.append(d)
122
123 for d in machine.disks:
124     check(re.match('^[A-Za-z0-9]+$', d.guest_device_name))
125     device = '/dev/xenvg/' + prefix + machine.name + '_' + d.guest_device_name
126     dspec = 'phy:%s,%s%s,w' % (device, diskioemu, d.guest_device_name)
127     disk.append(dspec)
128
129 usbdevice = 'tablet'
130
131 on_poweroff = 'destroy'
132 on_reboot = 'restart'
133 on_crash = 'destroy'
134 if machine.autorestart:
135     on_crash = 'restart'