Set the arp_notify sysctl on new interfaces, and set all sysctls before the 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 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 'preseed' in install: # d-i based installer
58         baseurl = install['mirror']+"/dists/"+install['dist']+"/main/installer-"+install['arch']+"/current/images"
59         kernelurl = baseurl + "/netboot/xen/vmlinuz"
60         ramdiskurl = baseurl + "/netboot/xen/initrd.gz"
61
62         import urllib
63         class MyUrlOpener(urllib.FancyURLopener):
64             def http_error_default(self, req, fp, code, msg, hdrs):
65                 raise IOError("%s %s" % (code, msg))
66         urlopener = MyUrlOpener()
67
68         try:
69             print "Fetching %s" % kernelurl
70             kernel, _ = urlopener.retrieve(kernelurl)
71             print "Fetching %s" % ramdiskurl
72             ramdisk, _ = urlopener.retrieve(ramdiskurl)
73         except IOError, _:
74             raise
75
76         # For debugging, add "DEBCONF_DEBUG=5" to the arguments.
77         extras = ["auto=true",
78                   "debconf/priority=critical",
79                   "debian-installer/locale=en_US.UTF-8",
80                   "debian-installer/exit/always_halt=true",
81                   "url="+install['preseed'],
82                   "--", "console=hvc0"]
83
84         extra = str.join(" ", extras)
85     else: # Traditional debootstrap-based install
86         disk.append('phy:/dev/xenvg/s_install_hda,hdb,r')
87
88         import os
89         release = os.uname()[2]
90         kernel = '/boot/vmlinuz-%s' % release
91         ramdisk = '/boot/initrd.img-%s' % release
92
93         if not machine.nics:
94             raise RuntimeError('You must have a nic to autoinstall')
95         n = machine.nics[0]
96         extra = 'ro noresume'
97         extra += (' ip=%s::%s:%s:%s:eth0:off'
98                   % (n.ip, config.dhcp.gateway, config.dhcp.netmask, machine.name))
99         extra += ' %s' % installer_options
100         root = '/dev/hdb1'
101     codepath = None
102
103     memory = max(memory, 512)
104 elif cdrom is not None:
105     disk.append('phy:/dev/xenvg/image_' + cdrom.cdrom_id + ',hdc:cdrom,r')
106     boot = 'd'
107     codepath = 'hvm'
108
109 if codepath == 'hvm':
110     xen_platform_pci = 1
111     ioemu = "ioemu:"
112     viftype = "model=pcnet, "
113     kernel = '/usr/lib/xen-4.1/boot/hvmloader'
114     builder = 'hvm'
115     vnc = 1
116     device_model = '/usr/sbin/qemu-dm-invirt'
117     serial = "pty"
118 elif codepath == 'paravm':
119     bootloader = '/usr/bin/pygrub'
120
121
122 for n in machine.nics:
123     check(re.match('^[0-9a-fA-F:]+$', n.mac_addr) and re.match('^[0-9.]*$', n.ip))
124     if n.nic_type:
125         viftype = viftype.replace("pcnet", n.nic_type)
126     d = ('%smac=%s, ip=%s, script=vif-invirtroute netdev=%s'
127          % (viftype, n.mac_addr, n.ip, config.xen.iface))
128     vif.append(d)
129
130 for d in machine.disks:
131     check(re.match('^[A-Za-z0-9]+$', d.guest_device_name))
132     device = '/dev/xenvg/' + prefix + machine.name + '_' + d.guest_device_name
133     dspec = 'phy:%s,%s%s,w' % (device, diskioemu, d.guest_device_name)
134     disk.append(dspec)
135
136 usbdevice = 'tablet'
137
138 on_poweroff = 'destroy'
139 on_reboot = 'restart'
140 on_crash = 'destroy'
141 if machine.autorestart:
142     on_crash = 'restart'