| 1 | # -*- mode: python; -*- |
|---|
| 2 | from invirt.database import models, connect |
|---|
| 3 | from invirt.config import structs as config |
|---|
| 4 | import re |
|---|
| 5 | import tempfile |
|---|
| 6 | from subprocess import call |
|---|
| 7 | |
|---|
| 8 | connect() |
|---|
| 9 | prefix = "d_" |
|---|
| 10 | |
|---|
| 11 | # 'machine_name', and optionally 'cdrom_image', should get passed in |
|---|
| 12 | # from the xm create call |
|---|
| 13 | |
|---|
| 14 | def check(b): |
|---|
| 15 | if not b: |
|---|
| 16 | import sys |
|---|
| 17 | sys.exit(1) |
|---|
| 18 | |
|---|
| 19 | machine = models.Machine.query().filter_by(name=machine_name).one() |
|---|
| 20 | check(machine is not None) |
|---|
| 21 | machine_type = machine.type |
|---|
| 22 | cdrom = None |
|---|
| 23 | if 'cdrom_image' in locals(): |
|---|
| 24 | cdrom = models.CDROM.query().filter_by(cdrom_id=cdrom_image).one() |
|---|
| 25 | check(cdrom is not None) |
|---|
| 26 | |
|---|
| 27 | memory = machine.memory |
|---|
| 28 | maxmem = memory |
|---|
| 29 | check(re.match('^[A-Za-z0-9][A-Za-z0-9._-]*$', machine.name)) |
|---|
| 30 | name = prefix + machine.name |
|---|
| 31 | check(re.match('^[0-9a-f-]+$', machine.uuid)) |
|---|
| 32 | uuid = machine.uuid |
|---|
| 33 | |
|---|
| 34 | vcpus = machine.cpus |
|---|
| 35 | |
|---|
| 36 | diskioemu = "" |
|---|
| 37 | viftype = "" |
|---|
| 38 | |
|---|
| 39 | pae = machine_type.pae |
|---|
| 40 | acpi = machine_type.acpi |
|---|
| 41 | apic = machine_type.apic |
|---|
| 42 | |
|---|
| 43 | vif = [] |
|---|
| 44 | |
|---|
| 45 | disk = [] |
|---|
| 46 | |
|---|
| 47 | if machine_type.hvm: |
|---|
| 48 | codepath = 'hvm' |
|---|
| 49 | else: |
|---|
| 50 | codepath = 'paravm' |
|---|
| 51 | |
|---|
| 52 | if 'installer_options' in locals(): #Installer |
|---|
| 53 | disk.append('phy:/dev/xenvg/s_install_hda,hdb,r') |
|---|
| 54 | |
|---|
| 55 | kernel = '/boot/vmlinuz-2.6.24-19-xen' #From hardy |
|---|
| 56 | ramdisk = '/boot/initrd.img-2.6.24-19-xen' |
|---|
| 57 | |
|---|
| 58 | if not machine.nics: |
|---|
| 59 | raise RuntimeError('You must have a nic to autoinstall') |
|---|
| 60 | n = machine.nics[0] |
|---|
| 61 | extra = 'ro noresume' |
|---|
| 62 | extra += (' ip=%s::%s:%s:%s:eth0:off' |
|---|
| 63 | % (n.ip, config.dhcp.gateway, config.dhcp.netmask, machine.name)) |
|---|
| 64 | extra += ' %s' % installer_options |
|---|
| 65 | root = '/dev/hdb1' |
|---|
| 66 | codepath = None |
|---|
| 67 | elif cdrom is not None: |
|---|
| 68 | disk.append('phy:/dev/xenvg/image_' + cdrom.cdrom_id + ',hdc:cdrom,r') |
|---|
| 69 | boot = 'd' |
|---|
| 70 | codepath = 'hvm' |
|---|
| 71 | |
|---|
| 72 | if codepath == 'hvm': |
|---|
| 73 | ioemu = "ioemu:" |
|---|
| 74 | viftype = "type=ioemu, " |
|---|
| 75 | kernel = '/usr/lib/xen/boot/hvmloader' |
|---|
| 76 | builder = 'hvm' |
|---|
| 77 | vnc = 1 |
|---|
| 78 | vncpasswd = 'moocow' |
|---|
| 79 | device_model = '/usr/sbin/qemu-dm-invirt' |
|---|
| 80 | serial = "pty" |
|---|
| 81 | elif codepath == 'paravm': |
|---|
| 82 | bootloader = '/usr/bin/pygrub' |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | for n in machine.nics: |
|---|
| 86 | check(re.match('^[0-9a-fA-F:]+$', n.mac_addr) and re.match('^[0-9.]*$', n.ip)) |
|---|
| 87 | d = '%smac=%s, ip=%s, script=vif-invirtroute' % (viftype, n.mac_addr, n.ip) |
|---|
| 88 | vif.append(d) |
|---|
| 89 | |
|---|
| 90 | for d in machine.disks: |
|---|
| 91 | check(re.match('^[A-Za-z0-9]+$', d.guest_device_name)) |
|---|
| 92 | device = '/dev/xenvg/' + prefix + machine.name + '_' + d.guest_device_name |
|---|
| 93 | dspec = 'phy:%s,%s%s,w' % (device, diskioemu, d.guest_device_name) |
|---|
| 94 | disk.append(dspec) |
|---|
| 95 | |
|---|
| 96 | usbdevice = 'tablet' |
|---|
| 97 | |
|---|
| 98 | on_poweroff = 'destroy' |
|---|
| 99 | on_reboot = 'restart' |
|---|
| 100 | on_crash = 'destroy' |
|---|
| 101 | if machine.autorestart: |
|---|
| 102 | on_crash = 'restart' |
|---|