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
53 if machine_type.type_id == 'linux-pvgrub':
58 if 'installer_options' in locals(): #Installer
60 install = dict(x.split("=",1) for x in shlex.split(installer_options))
61 if 'preseed' in install: # d-i based installer
62 baseurl = install['mirror']+"/dists/"+install['dist']+"/main/installer-"+install['arch']+"/current/images"
63 kernelurl = baseurl + "/netboot/xen/vmlinuz"
64 ramdiskurl = baseurl + "/netboot/xen/initrd.gz"
67 class MyUrlOpener(urllib.FancyURLopener):
68 def http_error_default(self, req, fp, code, msg, hdrs):
69 raise IOError("%s %s" % (code, msg))
70 urlopener = MyUrlOpener()
73 print "Fetching %s" % kernelurl
74 kernel, _ = urlopener.retrieve(kernelurl)
75 print "Fetching %s" % ramdiskurl
76 ramdisk, _ = urlopener.retrieve(ramdiskurl)
80 # For debugging, add "DEBCONF_DEBUG=5" to the arguments.
81 extras = ["auto=true",
82 "debconf/priority=critical",
83 "debian-installer/locale=en_US.UTF-8",
84 "debian-installer/exit/always_halt=true",
85 "url="+install['preseed'],
88 extra = str.join(" ", extras)
89 else: # Traditional debootstrap-based install
90 disk.append('phy:/dev/xenvg/s_install_hda,hdb,r')
93 release = os.uname()[2]
94 kernel = '/boot/vmlinuz-%s' % release
95 ramdisk = '/boot/initrd.img-%s' % release
98 raise RuntimeError('You must have a nic to autoinstall')
100 extra = 'ro noresume'
101 extra += (' ip=%s::%s:%s:%s:eth0:off'
102 % (n.ip, config.dhcp.gateway, config.dhcp.netmask, machine.name))
103 extra += ' %s' % installer_options
107 memory = max(memory, 512)
108 elif cdrom is not None:
109 disk.append('phy:/dev/xenvg/image_' + cdrom.cdrom_id + ',hdc:cdrom,r')
113 if codepath == 'hvm':
116 viftype = "model=pcnet, "
117 kernel = '/usr/lib/xen-4.1/boot/hvmloader'
120 device_model = '/usr/sbin/qemu-dm-invirt'
122 elif codepath == 'paravm':
124 kernel = '/usr/lib/grub/grub.xen'
126 bootloader = '/usr/bin/pygrub'
128 for n in machine.nics:
129 check(re.match('^[0-9a-fA-F:]+$', n.mac_addr) and re.match('^[0-9.]*$', n.ip))
131 viftype = viftype.replace("pcnet", n.nic_type)
132 d = ('%smac=%s, ip=%s, script=vif-invirtroute netdev=%s'
133 % (viftype, n.mac_addr, n.ip, config.xen.iface))
136 for d in machine.disks:
137 check(re.match('^[A-Za-z0-9]+$', d.guest_device_name))
138 device = '/dev/xenvg/' + prefix + machine.name + '_' + d.guest_device_name
139 dspec = 'phy:%s,%s%s,w' % (device, diskioemu, d.guest_device_name)
144 on_poweroff = 'destroy'
145 on_reboot = 'restart'
147 if machine.autorestart: