[101] | 1 | # -*- mode: python; -*- |
---|
[24] | 2 | import sipb_xen_database.models as models |
---|
[85] | 3 | from sipb_xen_database import connect |
---|
[109] | 4 | import re |
---|
[223] | 5 | import tempfile |
---|
| 6 | from subprocess import call |
---|
[24] | 7 | |
---|
[99] | 8 | connect('postgres://sipb-xen@sipb-xen-dev.mit.edu/sipb_xen') |
---|
[146] | 9 | prefix = "d_" |
---|
[24] | 10 | |
---|
[109] | 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: |
---|
[117] | 16 | import sys |
---|
| 17 | sys.exit(1) |
---|
[109] | 18 | |
---|
[85] | 19 | machine = models.Machine.get_by(name=machine_name) |
---|
[109] | 20 | check(machine is not None) |
---|
[762] | 21 | machine_type = models.Type.get_by(type_id=machine.type_id) |
---|
[24] | 22 | |
---|
| 23 | memory = machine.memory |
---|
| 24 | maxmem = memory |
---|
[109] | 25 | check(re.match('^[A-Za-z0-9][A-Za-z0-9._-]*$', machine.name)) |
---|
[146] | 26 | name = prefix + machine.name |
---|
[109] | 27 | check(re.match('^[0-9a-f-]+$', machine.uuid)) |
---|
[24] | 28 | uuid = machine.uuid |
---|
| 29 | |
---|
| 30 | vcpus = machine.cpus |
---|
| 31 | |
---|
[65] | 32 | diskioemu = "" |
---|
| 33 | viftype = "" |
---|
[24] | 34 | |
---|
[762] | 35 | pae = machine_type.pae |
---|
| 36 | acpi = machine_type.acpi |
---|
| 37 | apic = machine_type.apic |
---|
| 38 | |
---|
| 39 | vif = [] |
---|
| 40 | |
---|
| 41 | disk = [] |
---|
| 42 | |
---|
| 43 | if machine_type.hvm: |
---|
| 44 | codepath = 'hvm' |
---|
| 45 | else: |
---|
| 46 | codepath = 'paravm' |
---|
| 47 | |
---|
| 48 | if 'mirror' in locals(): #Installer |
---|
[828] | 49 | disk.append('phy:/dev/xenvg/d_foobar3_hda,hdb,r') |
---|
[762] | 50 | |
---|
| 51 | kernel = '/boot/vmlinuz-2.6.24-19-xen' #From hardy |
---|
| 52 | ramdisk = '/boot/initrd.img-2.6.24-19-xen' |
---|
| 53 | |
---|
| 54 | if not machine.nics: |
---|
| 55 | raise RuntimeError('You must have a nic to autoinstall') |
---|
| 56 | n = machine.nics[0] |
---|
| 57 | extra = 'ro noresume' |
---|
| 58 | extra += ' ip=%s::18.181.0.1:255.255.0.0:%s:eth0:off' % (n.ip, machine.name) |
---|
| 59 | extra += ' mirror=%s dist=%s' % (mirror, dist) |
---|
| 60 | extra += ' imagesize=%s' % imagesize |
---|
[828] | 61 | if 'noinstall' in locals(): |
---|
| 62 | extra += ' noinstall' |
---|
[762] | 63 | root = '/dev/hdb1' |
---|
| 64 | codepath = None |
---|
| 65 | elif 'cdrom_image' in locals(): |
---|
| 66 | check(re.match('^[A-Za-z0-9][A-Za-z0-9_.-]*$', cdrom_image)) |
---|
| 67 | disk.append('file:/srv/images/' + cdrom_image + '.iso,hdc:cdrom,r') |
---|
| 68 | boot = 'd' |
---|
| 69 | codepath = 'hvm' |
---|
| 70 | |
---|
| 71 | if codepath == 'hvm': |
---|
[65] | 72 | ioemu = "ioemu:" |
---|
[101] | 73 | viftype = "type=ioemu, " |
---|
[65] | 74 | kernel = 'hvmloader' |
---|
| 75 | builder = 'hvm' |
---|
| 76 | vnc = 1 |
---|
[104] | 77 | vncpasswd = 'moocow' |
---|
| 78 | device_model = '/usr/sbin/qemu-dm-sipb' |
---|
[762] | 79 | serial = "pty" |
---|
| 80 | elif codepath == 'paravm': |
---|
| 81 | bootloader = '/usr/lib/xen-default/bin/pygrub' |
---|
[65] | 82 | |
---|
| 83 | |
---|
| 84 | for n in machine.nics: |
---|
[109] | 85 | check(re.match('^[0-9a-fA-F:]+$', n.mac_addr) and re.match('^[0-9.]*$', n.ip)) |
---|
[101] | 86 | d = '%smac=%s, ip=%s, bridge=xenbr0, script=vif-sipbroute' % (viftype, n.mac_addr, n.ip) |
---|
[84] | 87 | vif.append(d) |
---|
[65] | 88 | |
---|
[24] | 89 | for d in machine.disks: |
---|
[109] | 90 | check(re.match('^[A-Za-z0-9]+$', d.guest_device_name)) |
---|
[146] | 91 | device = '/dev/xenvg/' + prefix + machine.name + '_' + d.guest_device_name |
---|
[65] | 92 | dspec = 'phy:%s,%s%s,w' % (device, diskioemu, d.guest_device_name) |
---|
[24] | 93 | disk.append(dspec) |
---|
| 94 | |
---|
[433] | 95 | usbdevice = 'tablet' |
---|
| 96 | |
---|
[24] | 97 | on_poweroff = 'destroy' |
---|
| 98 | on_reboot = 'restart' |
---|
[109] | 99 | on_crash = 'destroy' |
---|
| 100 | if machine.autorestart: |
---|
| 101 | on_crash = 'restart' |
---|