[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) |
---|
[671] | 21 | hvm = machine.type.hvm |
---|
| 22 | if 'cdrom_image' in locals(): |
---|
| 23 | hvm = True |
---|
[24] | 24 | |
---|
| 25 | memory = machine.memory |
---|
| 26 | maxmem = memory |
---|
[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 | |
---|
[671] | 37 | if hvm: |
---|
[65] | 38 | ioemu = "ioemu:" |
---|
[101] | 39 | viftype = "type=ioemu, " |
---|
[65] | 40 | |
---|
| 41 | kernel = 'hvmloader' |
---|
| 42 | builder = 'hvm' |
---|
| 43 | vnc = 1 |
---|
[104] | 44 | vncpasswd = 'moocow' |
---|
| 45 | device_model = '/usr/sbin/qemu-dm-sipb' |
---|
[490] | 46 | serial = 'pty' |
---|
[65] | 47 | else: |
---|
[255] | 48 | kernel = '/boot/vmlinuz-2.6.18-5-xen-amd64' |
---|
| 49 | ramdisk = '/boot/initrd.img-2.6.18-5-xen-amd64' |
---|
[65] | 50 | builder = 'linux' |
---|
| 51 | |
---|
[671] | 52 | pae = machine.type.pae |
---|
| 53 | acpi = machine.type.acpi |
---|
| 54 | apic = machine.type.apic |
---|
[65] | 55 | |
---|
| 56 | vif = [] |
---|
| 57 | |
---|
| 58 | for n in machine.nics: |
---|
[109] | 59 | check(re.match('^[0-9a-fA-F:]+$', n.mac_addr) and re.match('^[0-9.]*$', n.ip)) |
---|
[101] | 60 | d = '%smac=%s, ip=%s, bridge=xenbr0, script=vif-sipbroute' % (viftype, n.mac_addr, n.ip) |
---|
[84] | 61 | vif.append(d) |
---|
[65] | 62 | |
---|
[24] | 63 | disk = [] |
---|
| 64 | |
---|
| 65 | for d in machine.disks: |
---|
[109] | 66 | check(re.match('^[A-Za-z0-9]+$', d.guest_device_name)) |
---|
[146] | 67 | device = '/dev/xenvg/' + prefix + machine.name + '_' + d.guest_device_name |
---|
[65] | 68 | dspec = 'phy:%s,%s%s,w' % (device, diskioemu, d.guest_device_name) |
---|
[24] | 69 | disk.append(dspec) |
---|
| 70 | |
---|
[223] | 71 | if 'installer' in locals(): |
---|
| 72 | check(re.match('^[A-Za-z0-9][A-Za-z0-9_.-]*$', installer)) |
---|
| 73 | tmptree = tempfile.mkdtemp('', 'auto-install.', '/tmp') |
---|
| 74 | call(['/usr/sbin/sipb-xen-make-iso', installer, tmptree] |
---|
| 75 | + installer_options.split(' ')) |
---|
| 76 | disk.append('file:'+tmptree+'/install.iso,hdc:cdrom,r') |
---|
| 77 | boot = 'd' |
---|
[245] | 78 | |
---|
| 79 | elif 'disks' in locals(): # for the copying installer's use |
---|
| 80 | disk = disks.split(' ') |
---|
| 81 | boot = 'c' |
---|
| 82 | |
---|
[223] | 83 | elif 'cdrom_image' in locals(): |
---|
[109] | 84 | check(re.match('^[A-Za-z0-9][A-Za-z0-9_.-]*$', cdrom_image)) |
---|
| 85 | disk.append('file:/srv/images/' + cdrom_image + '.iso,hdc:cdrom,r') |
---|
| 86 | boot = 'd' |
---|
[24] | 87 | |
---|
[433] | 88 | usbdevice = 'tablet' |
---|
| 89 | |
---|
[24] | 90 | on_poweroff = 'destroy' |
---|
| 91 | on_reboot = 'restart' |
---|
[109] | 92 | on_crash = 'destroy' |
---|
| 93 | if machine.autorestart: |
---|
| 94 | on_crash = 'restart' |
---|