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