[101] | 1 | # -*- mode: python; -*- |
---|
[829] | 2 | from invirt.database import models, connect |
---|
[842] | 3 | from invirt.config import structs as config |
---|
[109] | 4 | import re |
---|
[24] | 5 | |
---|
[829] | 6 | connect() |
---|
[146] | 7 | prefix = "d_" |
---|
[24] | 8 | |
---|
[109] | 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: |
---|
[117] | 14 | import sys |
---|
| 15 | sys.exit(1) |
---|
[109] | 16 | |
---|
[1016] | 17 | machine = models.Machine.query().filter_by(name=machine_name).one() |
---|
[109] | 18 | check(machine is not None) |
---|
[1090] | 19 | machine_type = machine.type |
---|
| 20 | cdrom = None |
---|
| 21 | if 'cdrom_image' in locals(): |
---|
[1101] | 22 | cdrom = models.CDROM.query().filter_by(cdrom_id=cdrom_image).one() |
---|
[1090] | 23 | check(cdrom is not None) |
---|
[24] | 24 | |
---|
| 25 | memory = machine.memory |
---|
[1635] | 26 | maxmem = '2048' |
---|
[109] | 27 | check(re.match('^[A-Za-z0-9][A-Za-z0-9._-]*$', machine.name)) |
---|
[146] | 28 | name = prefix + machine.name |
---|
[109] | 29 | check(re.match('^[0-9a-f-]+$', machine.uuid)) |
---|
[24] | 30 | uuid = machine.uuid |
---|
| 31 | |
---|
| 32 | vcpus = machine.cpus |
---|
| 33 | |
---|
[65] | 34 | diskioemu = "" |
---|
| 35 | viftype = "" |
---|
[24] | 36 | |
---|
[762] | 37 | pae = machine_type.pae |
---|
| 38 | acpi = machine_type.acpi |
---|
| 39 | apic = machine_type.apic |
---|
| 40 | |
---|
| 41 | vif = [] |
---|
| 42 | |
---|
| 43 | disk = [] |
---|
| 44 | |
---|
| 45 | if machine_type.hvm: |
---|
| 46 | codepath = 'hvm' |
---|
| 47 | else: |
---|
| 48 | codepath = 'paravm' |
---|
| 49 | |
---|
[1091] | 50 | if 'installer_options' in locals(): #Installer |
---|
[1079] | 51 | disk.append('phy:/dev/xenvg/s_install_hda,hdb,r') |
---|
[762] | 52 | |
---|
[1680] | 53 | import os |
---|
| 54 | release = os.uname()[2] |
---|
| 55 | kernel = '/boot/vmlinuz-%s' % release |
---|
| 56 | ramdisk = '/boot/initrd.img-%s' % release |
---|
[762] | 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' |
---|
[842] | 62 | extra += (' ip=%s::%s:%s:%s:eth0:off' |
---|
[1079] | 63 | % (n.ip, config.dhcp.gateway, config.dhcp.netmask, machine.name)) |
---|
[1098] | 64 | extra += ' %s' % installer_options |
---|
[762] | 65 | root = '/dev/hdb1' |
---|
| 66 | codepath = None |
---|
[2367] | 67 | |
---|
| 68 | memory = max(memory, 512) |
---|
[1090] | 69 | elif cdrom is not None: |
---|
[1126] | 70 | disk.append('phy:/dev/xenvg/image_' + cdrom.cdrom_id + ',hdc:cdrom,r') |
---|
[762] | 71 | boot = 'd' |
---|
| 72 | codepath = 'hvm' |
---|
| 73 | |
---|
| 74 | if codepath == 'hvm': |
---|
[65] | 75 | ioemu = "ioemu:" |
---|
[101] | 76 | viftype = "type=ioemu, " |
---|
[1016] | 77 | kernel = '/usr/lib/xen/boot/hvmloader' |
---|
[65] | 78 | builder = 'hvm' |
---|
| 79 | vnc = 1 |
---|
[1262] | 80 | device_model = '/usr/sbin/qemu-dm-invirt' |
---|
[762] | 81 | serial = "pty" |
---|
| 82 | elif codepath == 'paravm': |
---|
[1107] | 83 | bootloader = '/usr/bin/pygrub' |
---|
[65] | 84 | |
---|
| 85 | |
---|
| 86 | for n in machine.nics: |
---|
[109] | 87 | check(re.match('^[0-9a-fA-F:]+$', n.mac_addr) and re.match('^[0-9.]*$', n.ip)) |
---|
[2055] | 88 | d = ('%smac=%s, ip=%s, script=vif-invirtroute netdev=%s' |
---|
[2895] | 89 | % (viftype, n.mac_addr, n.ip, config.xen.iface)) |
---|
[84] | 90 | vif.append(d) |
---|
[65] | 91 | |
---|
[24] | 92 | for d in machine.disks: |
---|
[109] | 93 | check(re.match('^[A-Za-z0-9]+$', d.guest_device_name)) |
---|
[146] | 94 | device = '/dev/xenvg/' + prefix + machine.name + '_' + d.guest_device_name |
---|
[65] | 95 | dspec = 'phy:%s,%s%s,w' % (device, diskioemu, d.guest_device_name) |
---|
[24] | 96 | disk.append(dspec) |
---|
| 97 | |
---|
[433] | 98 | usbdevice = 'tablet' |
---|
| 99 | |
---|
[24] | 100 | on_poweroff = 'destroy' |
---|
| 101 | on_reboot = 'restart' |
---|
[109] | 102 | on_crash = 'destroy' |
---|
| 103 | if machine.autorestart: |
---|
| 104 | on_crash = 'restart' |
---|