ia64/xen-unstable

changeset 13653:b8b75fdbd21f

[XENAPI] Add an example to start HVM guests via the Xen API.

Signed-off-by: Alastair Tse <atse@xensource.com>
author Alastair Tse <atse@xensource.com>
date Fri Jan 26 15:42:21 2007 +0000 (2007-01-26)
parents bb2b1b6662fa
children 93d3ff513609
files tools/python/scripts/test_hvm_create.py
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/python/scripts/test_hvm_create.py	Fri Jan 26 15:42:21 2007 +0000
     1.3 @@ -0,0 +1,173 @@
     1.4 +#!/usr/bin/python
     1.5 +
     1.6 +vm_cfg = {
     1.7 +    'name_label': 'API_HVM',
     1.8 +    'user_version': 1,
     1.9 +    'is_a_template': False,
    1.10 +    'auto_power_on': False, # TODO
    1.11 +
    1.12 +    'memory_static_min': 64,    
    1.13 +    'memory_static_max': 128,
    1.14 +    #'memory_dynamic_min': 64,
    1.15 +    #'memory_dynamic_max': 128,
    1.16 +    
    1.17 +    
    1.18 +    'VCPUs_policy': 'credit',
    1.19 +    'VCPUs_params': '',
    1.20 +    'VCPUs_number': 2,
    1.21 +    'VCPUs_features_required': '',
    1.22 +    'VCPUs_features_can_use': '',
    1.23 +    'VCPUs_features_force_on': '',
    1.24 +    'VCPUs_features_force_off': '',
    1.25 +
    1.26 +    'actions_after_shutdown': 'destroy',
    1.27 +    'actions_after_reboot': 'restart',
    1.28 +    'actions_after_suspend': 'destroy',
    1.29 +    'actions_after_crash': 'destroy',
    1.30 +    
    1.31 +    'PV_bootloader': '',
    1.32 +    'PV_bootloader_args': '',
    1.33 +    
    1.34 +    'PV_kernel': '',
    1.35 +    'PV_ramdisk': '',
    1.36 +    'PV_args': '',
    1.37 +
    1.38 +    'HVM_boot': 'cda',
    1.39 +    'platform_std_VGA': False,
    1.40 +    'platform_serial': '',
    1.41 +    'platform_localtime': False,
    1.42 +    'platform_clock_offset': False,
    1.43 +    'platform_enable_audio': False,
    1.44 +    'PCI_bus': ''
    1.45 +}
    1.46 +
    1.47 +local_vdi_cfg = {
    1.48 +    'name_label': 'gentoo.hvm',
    1.49 +    'name_description': '',
    1.50 +    'uri': 'file:/root/gentoo.amd64.hvm.img',
    1.51 +    'virtual_size': 0,
    1.52 +    'sector_size': 0,
    1.53 +    'type': 'system',
    1.54 +    'parent': '',
    1.55 +    'SR_name': 'Local',
    1.56 +    'sharable': False,
    1.57 +    'read_only': False,
    1.58 +}    
    1.59 +
    1.60 +local_vbd_cfg = {
    1.61 +    'VDI': '',
    1.62 +    'VM': '',
    1.63 +    'device': 'hda',
    1.64 +    'mode': 'RW',
    1.65 +    'type': 'disk',
    1.66 +    'driver': 'ioemu',
    1.67 +}
    1.68 +
    1.69 +vif_cfg = {
    1.70 +    'name': 'API_VIF',
    1.71 +    'type': 'ioemu',
    1.72 +    'device': '',
    1.73 +    'network': '',
    1.74 +    'MAC': '',
    1.75 +    'MTU': 1500,
    1.76 +}    
    1.77 +
    1.78 +import sys
    1.79 +import time
    1.80 +sys.path.append('/usr/lib/python')
    1.81 +
    1.82 +from xapi import connect, execute
    1.83 +
    1.84 +def test_vm_create():
    1.85 +    server, session = connect()
    1.86 +    vm_uuid = None
    1.87 +    local_vdi_uuid = None
    1.88 +    local_vbd_uuid = None
    1.89 +    vif_uuid = None
    1.90 +    
    1.91 +    # List all VMs
    1.92 +    vm_list = execute(server, 'VM.get_all', (session,))
    1.93 +    vm_names = []
    1.94 +    for vm_uuid in vm_list:
    1.95 +        vm_record = execute(server, 'VM.get_record', (session, vm_uuid))
    1.96 +        vm_names.append(vm_record['name_label'])
    1.97 +
    1.98 +    # Get default SR
    1.99 +    local_sr_list = execute(server, 'SR.get_by_name_label',
   1.100 +                            (session, local_vdi_cfg['SR_name']))
   1.101 +    local_sr_uuid = local_sr_list[0]
   1.102 +
   1.103 +    # Get default network
   1.104 +    net_list = execute(server, 'network.get_all', (session,))
   1.105 +    net_uuid = net_list[0]
   1.106 +
   1.107 +    try:
   1.108 +        # Create a new VM
   1.109 +        print 'Create VM'
   1.110 +        vm_uuid = execute(server, 'VM.create', (session, vm_cfg))
   1.111 +
   1.112 +        print 'Create VDI'
   1.113 +        # Create a new VDI (Local)
   1.114 +        local_vdi_cfg['SR'] = local_sr_uuid
   1.115 +        local_vdi_uuid = execute(server, 'VDI.create',
   1.116 +                                 (session, local_vdi_cfg))
   1.117 +
   1.118 +        print 'Create VBD'
   1.119 +        # Create a new VBD (Local)
   1.120 +        local_vbd_cfg['VM'] = vm_uuid
   1.121 +        local_vbd_cfg['VDI'] = local_vdi_uuid
   1.122 +        local_vbd_uuid = execute(server, 'VBD.create',
   1.123 +                                 (session, local_vbd_cfg))
   1.124 +
   1.125 +        print 'Craete VIF'
   1.126 +        # Create a new VIF
   1.127 +        vif_cfg['network'] = net_uuid
   1.128 +        vif_cfg['VM'] = vm_uuid
   1.129 +        vif_uuid = execute(server, 'VIF.create', (session, vif_cfg))
   1.130 +
   1.131 +        # Start the VM
   1.132 +        execute(server, 'VM.start', (session, vm_uuid, False))
   1.133 +
   1.134 +        time.sleep(30)
   1.135 +
   1.136 +        test_suspend = False
   1.137 +        if test_suspend:
   1.138 +            print 'Suspending VM..'
   1.139 +            execute(server, 'VM.suspend', (session, vm_uuid))
   1.140 +            print 'Suspended VM.'
   1.141 +            time.sleep(5)
   1.142 +            print 'Resuming VM ...'
   1.143 +            execute(server, 'VM.resume', (session, vm_uuid, False))
   1.144 +            print 'Resumed VM.'
   1.145 +
   1.146 +        # Wait for user to say we're good to shut it down
   1.147 +        while True:
   1.148 +            destroy = raw_input('destroy VM? ')
   1.149 +            if destroy[0] in ('y', 'Y'):
   1.150 +                break
   1.151 +
   1.152 +    finally:
   1.153 +        # Clean up
   1.154 +        if vif_uuid:
   1.155 +            execute(server, 'VIF.destroy', (session, vif_uuid))
   1.156 +            
   1.157 +        if local_vbd_uuid:
   1.158 +            execute(server, 'VBD.destroy', (session, local_vbd_uuid))
   1.159 +        if local_vdi_uuid:
   1.160 +            execute(server, 'VDI.destroy', (session, local_vdi_uuid))
   1.161 +            
   1.162 +        if vm_uuid:
   1.163 +            try:
   1.164 +                execute(server, 'VM.hard_shutdown', (session, vm_uuid))
   1.165 +                time.sleep(2)
   1.166 +            except:
   1.167 +                pass
   1.168 +            try:    
   1.169 +                execute(server, 'VM.destroy', (session, vm_uuid))
   1.170 +            except:
   1.171 +                pass
   1.172 +
   1.173 +
   1.174 +if __name__ == "__main__":
   1.175 +    test_vm_create()
   1.176 +