[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 |
---|
[24] | 5 | |
---|
[99] | 6 | connect('postgres://sipb-xen@sipb-xen-dev.mit.edu/sipb_xen') |
---|
[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 | |
---|
[85] | 17 | machine = models.Machine.get_by(name=machine_name) |
---|
[109] | 18 | check(machine is not None) |
---|
[65] | 19 | machine_type = models.Type.get_by(type_id=machine.type_id) |
---|
[24] | 20 | |
---|
| 21 | memory = machine.memory |
---|
| 22 | maxmem = memory |
---|
[109] | 23 | check(re.match('^[A-Za-z0-9][A-Za-z0-9._-]*$', machine.name)) |
---|
[146] | 24 | name = prefix + machine.name |
---|
[109] | 25 | check(re.match('^[0-9a-f-]+$', machine.uuid)) |
---|
[24] | 26 | uuid = machine.uuid |
---|
| 27 | |
---|
| 28 | vcpus = machine.cpus |
---|
| 29 | |
---|
[65] | 30 | diskioemu = "" |
---|
| 31 | viftype = "" |
---|
[24] | 32 | |
---|
[65] | 33 | if machine_type.hvm: |
---|
| 34 | ioemu = "ioemu:" |
---|
[101] | 35 | viftype = "type=ioemu, " |
---|
[65] | 36 | |
---|
| 37 | kernel = 'hvmloader' |
---|
| 38 | builder = 'hvm' |
---|
| 39 | vnc = 1 |
---|
[104] | 40 | vncpasswd = 'moocow' |
---|
| 41 | device_model = '/usr/sbin/qemu-dm-sipb' |
---|
[65] | 42 | else: |
---|
| 43 | kernel = '/boot/vmlinuz-2.6.18-4-xen-amd64' |
---|
| 44 | ramdisk = '/boot/initrd.img-2.6.18-4-xen-amd64' |
---|
| 45 | builder = 'linux' |
---|
| 46 | |
---|
| 47 | pae = machine_type.pae |
---|
| 48 | acpi = machine_type.acpi |
---|
| 49 | apic = machine_type.apic |
---|
| 50 | |
---|
| 51 | vif = [] |
---|
| 52 | |
---|
| 53 | for n in machine.nics: |
---|
[109] | 54 | check(re.match('^[0-9a-fA-F:]+$', n.mac_addr) and re.match('^[0-9.]*$', n.ip)) |
---|
[101] | 55 | d = '%smac=%s, ip=%s, bridge=xenbr0, script=vif-sipbroute' % (viftype, n.mac_addr, n.ip) |
---|
[84] | 56 | vif.append(d) |
---|
[65] | 57 | |
---|
[24] | 58 | disk = [] |
---|
| 59 | |
---|
| 60 | for d in machine.disks: |
---|
[109] | 61 | check(re.match('^[A-Za-z0-9]+$', d.guest_device_name)) |
---|
[146] | 62 | device = '/dev/xenvg/' + prefix + machine.name + '_' + d.guest_device_name |
---|
[65] | 63 | dspec = 'phy:%s,%s%s,w' % (device, diskioemu, d.guest_device_name) |
---|
[24] | 64 | disk.append(dspec) |
---|
| 65 | |
---|
[109] | 66 | if 'cdrom_image' in locals(): |
---|
| 67 | check(re.match('^[A-Za-z0-9][A-Za-z0-9_.-]*$', cdrom_image)) |
---|
| 68 | disk.append('file:/srv/images/' + cdrom_image + '.iso,hdc:cdrom,r') |
---|
| 69 | boot = 'd' |
---|
[24] | 70 | |
---|
| 71 | on_poweroff = 'destroy' |
---|
| 72 | on_reboot = 'restart' |
---|
[109] | 73 | on_crash = 'destroy' |
---|
| 74 | if machine.autorestart: |
---|
| 75 | on_crash = 'restart' |
---|