1 # -*- mode: python; -*-
2 from invirt.database import models, connect
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
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 '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"
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()
69 print "Fetching %s" % kernelurl
70 kernel, _ = urlopener.retrieve(kernelurl)
71 print "Fetching %s" % ramdiskurl
72 ramdisk, _ = urlopener.retrieve(ramdiskurl)
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'],
84 extra = str.join(" ", extras)
85 else: # Traditional debootstrap-based install
86 disk.append('phy:/dev/xenvg/s_install_hda,hdb,r')
89 release = os.uname()[2]
90 kernel = '/boot/vmlinuz-%s' % release
91 ramdisk = '/boot/initrd.img-%s' % release
94 raise RuntimeError('You must have a nic to autoinstall')
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
103 memory = max(memory, 512)
104 elif cdrom is not None:
105 disk.append('phy:/dev/xenvg/image_' + cdrom.cdrom_id + ',hdc:cdrom,r')
109 if codepath == 'hvm':
112 viftype = "model=pcnet, "
113 kernel = '/usr/lib/xen-4.1/boot/hvmloader'
116 device_model = '/usr/sbin/qemu-dm-invirt'
118 elif codepath == 'paravm':
119 bootloader = '/usr/bin/pygrub'
122 for n in machine.nics:
123 check(re.match('^[0-9a-fA-F:]+$', n.mac_addr) and re.match('^[0-9.]*$', n.ip))
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))
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)
138 on_poweroff = 'destroy'
139 on_reboot = 'restart'
141 if machine.autorestart: