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