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