~ubuntu-branches/ubuntu/utopic/xen/utopic

« back to all changes in this revision

Viewing changes to tools/python/scripts/test_hvm_create.py

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2010-05-06 15:47:38 UTC
  • mto: (1.3.1) (15.1.1 sid) (4.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20100506154738-agoz0rlafrh1fnq7
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
vm_cfg = {
 
4
    'name_label': 'API_HVM',
 
5
    'user_version': 1,
 
6
    'is_a_template': False,
 
7
    'auto_power_on': False, # TODO
 
8
 
 
9
    'memory_static_min': 64,    
 
10
    'memory_static_max': 128,
 
11
    #'memory_dynamic_min': 64,
 
12
    #'memory_dynamic_max': 128,
 
13
    
 
14
    
 
15
    'VCPUs_policy': 'credit',
 
16
    'VCPUs_params': {},
 
17
    'VCPUs_number': 2,
 
18
 
 
19
    'actions_after_shutdown': 'destroy',
 
20
    'actions_after_reboot': 'restart',
 
21
    'actions_after_crash': 'destroy',
 
22
    
 
23
    'PV_bootloader': '',
 
24
    'PV_bootloader_args': '',
 
25
    
 
26
    'PV_kernel': '',
 
27
    'PV_ramdisk': '',
 
28
    'PV_args': '',
 
29
 
 
30
    'HVM_boot': 'cda',
 
31
    'platform_std_VGA': False,
 
32
    'platform_serial': '',
 
33
    'platform_localtime': False,
 
34
    'platform_clock_offset': False,
 
35
    'platform_enable_audio': False,
 
36
    'PCI_bus': ''
 
37
}
 
38
 
 
39
local_vdi_cfg = {
 
40
    'name_label': 'gentoo.hvm',
 
41
    'name_description': '',
 
42
    'virtual_size': 0,
 
43
    'type': 'system',
 
44
    'parent': '',
 
45
    'SR_name': 'Local',
 
46
    'sharable': False,
 
47
    'read_only': False,
 
48
    'other_config': {'location': 'file:/root/gentoo.amd64.hvm.img'},
 
49
}    
 
50
 
 
51
local_vbd_cfg = {
 
52
    'VDI': '',
 
53
    'VM': '',
 
54
    'device': 'hda',
 
55
    'mode': 'RW',
 
56
    'type': 'disk',
 
57
    'driver': 'ioemu',
 
58
}
 
59
 
 
60
vif_cfg = {
 
61
    'name': 'API_VIF',
 
62
    'type': 'ioemu',
 
63
    'device': '',
 
64
    'network': '',
 
65
    'MAC': '',
 
66
    'MTU': 1500,
 
67
}    
 
68
 
 
69
console_cfg = {
 
70
    'protocol': 'rfb',
 
71
    'other_config': {'vncunused': 1, 'vncpasswd': 'testing'},
 
72
}
 
73
 
 
74
 
 
75
import sys
 
76
import time
 
77
 
 
78
from xapi import connect, execute
 
79
 
 
80
def test_vm_create():
 
81
    server, session = connect()
 
82
    vm_uuid = None
 
83
    local_vdi_uuid = None
 
84
    local_vbd_uuid = None
 
85
    vif_uuid = None
 
86
    
 
87
    # List all VMs
 
88
    vm_list = execute(server, 'VM.get_all', (session,))
 
89
    vm_names = []
 
90
    for vm_uuid in vm_list:
 
91
        vm_record = execute(server, 'VM.get_record', (session, vm_uuid))
 
92
        vm_names.append(vm_record['name_label'])
 
93
 
 
94
    # Get default SR
 
95
    local_sr_list = execute(server, 'SR.get_by_name_label',
 
96
                            (session, local_vdi_cfg['SR_name']))
 
97
    local_sr_uuid = local_sr_list[0]
 
98
 
 
99
    # Get default network
 
100
    net_list = execute(server, 'network.get_all', (session,))
 
101
    net_uuid = net_list[0]
 
102
 
 
103
    try:
 
104
        # Create a new VM
 
105
        print 'Create VM'
 
106
        vm_uuid = execute(server, 'VM.create', (session, vm_cfg))
 
107
 
 
108
        print 'Create VDI'
 
109
        # Create a new VDI (Local)
 
110
        local_vdi_cfg['SR'] = local_sr_uuid
 
111
        local_vdi_uuid = execute(server, 'VDI.create',
 
112
                                 (session, local_vdi_cfg))
 
113
 
 
114
        print 'Create VBD'
 
115
        # Create a new VBD (Local)
 
116
        local_vbd_cfg['VM'] = vm_uuid
 
117
        local_vbd_cfg['VDI'] = local_vdi_uuid
 
118
        local_vbd_uuid = execute(server, 'VBD.create',
 
119
                                 (session, local_vbd_cfg))
 
120
 
 
121
        print 'Craete VIF'
 
122
        # Create a new VIF
 
123
        vif_cfg['network'] = net_uuid
 
124
        vif_cfg['VM'] = vm_uuid
 
125
        vif_uuid = execute(server, 'VIF.create', (session, vif_cfg))
 
126
 
 
127
        # Create a console
 
128
        console_cfg['VM'] = vm_uuid
 
129
        console_uuid = execute(server, 'console.create',
 
130
                               (session, console_cfg))
 
131
        print console_uuid
 
132
 
 
133
        # Start the VM
 
134
        execute(server, 'VM.start', (session, vm_uuid, False))
 
135
 
 
136
        time.sleep(30)
 
137
 
 
138
        test_suspend = False
 
139
        if test_suspend:
 
140
            print 'Suspending VM..'
 
141
            execute(server, 'VM.suspend', (session, vm_uuid))
 
142
            print 'Suspended VM.'
 
143
            time.sleep(5)
 
144
            print 'Resuming VM ...'
 
145
            execute(server, 'VM.resume', (session, vm_uuid, False))
 
146
            print 'Resumed VM.'
 
147
 
 
148
        # Wait for user to say we're good to shut it down
 
149
        while True:
 
150
            destroy = raw_input('destroy VM? ')
 
151
            if destroy[0] in ('y', 'Y'):
 
152
                break
 
153
 
 
154
    finally:
 
155
        # Clean up
 
156
        if vif_uuid:
 
157
            execute(server, 'VIF.destroy', (session, vif_uuid))
 
158
            
 
159
        if local_vbd_uuid:
 
160
            execute(server, 'VBD.destroy', (session, local_vbd_uuid))
 
161
        if local_vdi_uuid:
 
162
            execute(server, 'VDI.destroy', (session, local_vdi_uuid))
 
163
            
 
164
        if vm_uuid:
 
165
            try:
 
166
                execute(server, 'VM.hard_shutdown', (session, vm_uuid))
 
167
                time.sleep(2)
 
168
            except:
 
169
                pass
 
170
            try:    
 
171
                execute(server, 'VM.destroy', (session, vm_uuid))
 
172
            except:
 
173
                pass
 
174
 
 
175
 
 
176
if __name__ == "__main__":
 
177
    test_vm_create()
 
178