1 # -*- mode: python; -*-
2 from invirt.database import models, connect, session
3 from invirt.config import structs as config
9 # 'machine_name', and optionally 'cdrom_image', should get passed in
10 # from the xm create call
17 machine = models.Machine.query.filter_by(name=machine_name).one()
18 check(machine is not None)
19 machine_type = machine.type
21 if 'cdrom_image' in locals():
22 cdrom = models.CDROM.query.filter_by(cdrom_id=cdrom_image).one()
23 check(cdrom is not None)
25 memory = machine.memory
27 # Disable populate-on-demand
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)
41 pae = machine_type.pae
42 acpi = machine_type.acpi
43 apic = machine_type.apic
54 if 'installer_options' in locals(): #Installer
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",
64 "inst.ks="+install['ks'],
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"
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'],
81 raise RuntimeError('unknown new-style autoinstall')
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()
90 print "Fetching %s" % kernelurl
91 kernel, _ = urlopener.retrieve(kernelurl)
92 print "Fetching %s" % ramdiskurl
93 ramdisk, _ = urlopener.retrieve(ramdiskurl)
97 extra = str.join(" ", extras)
98 else: # Traditional debootstrap-based install
99 disk.append('phy:/dev/xenvg/s_install_hda,hdb,r')
102 release = os.uname()[2]
103 kernel = '/boot/vmlinuz-%s' % release
104 ramdisk = '/boot/initrd.img-%s' % release
107 raise RuntimeError('You must have a nic to autoinstall')
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
116 memory = max(memory, 768)
118 elif cdrom is not None:
119 disk.append('phy:/dev/xenvg/image_' + cdrom.cdrom_id + ',hdc:cdrom,r')
123 if codepath == 'hvm':
126 viftype = "model=pcnet, "
127 kernel = '/usr/lib/xen-4.1/boot/hvmloader'
130 device_model = '/usr/sbin/qemu-dm-invirt'
132 elif codepath == 'paravm':
133 bootloader = '/usr/bin/pygrub'
136 for n in machine.nics:
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'
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
154 check(re.match('^[0-9a-fA-F:]+$', n.mac_addr) and re.match('^[0-9.]*$', n.ip))
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))
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)
169 on_poweroff = 'destroy'
170 on_reboot = 'restart'
172 if machine.autorestart: