1 | #!/usr/bin/python |
---|
2 | |
---|
3 | vm_cfg = { |
---|
4 | 'name_label': 'APIVM', |
---|
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': '/boot/vmlinuz-2.6.18-xenU', |
---|
27 | 'PV_ramdisk': '', |
---|
28 | 'PV_args': 'root=/dev/sda1 ro', |
---|
29 | |
---|
30 | #'HVM_boot': '', |
---|
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 | vdi_cfg = { |
---|
40 | 'name_label': 'API_VDI', |
---|
41 | 'name_description': '', |
---|
42 | 'virtual_size': 100 * 1024 * 1024 * 1024, |
---|
43 | 'type': 'system', |
---|
44 | 'parent': '', |
---|
45 | 'SR_name': 'QCoW', |
---|
46 | 'sharable': False, |
---|
47 | 'read_only': False, |
---|
48 | } |
---|
49 | |
---|
50 | vbd_cfg = { |
---|
51 | 'VDI': '', |
---|
52 | 'VM': '', |
---|
53 | 'device': 'sda2', |
---|
54 | 'mode': 'RW', |
---|
55 | 'type': 'disk', |
---|
56 | 'driver': 'paravirtualised', |
---|
57 | } |
---|
58 | |
---|
59 | local_vdi_cfg = { |
---|
60 | 'name_label': 'gentoo.amd64.img', |
---|
61 | 'name_description': '', |
---|
62 | 'virtual_size': 0, |
---|
63 | 'type': 'system', |
---|
64 | 'parent': '', |
---|
65 | 'SR_name': 'Local', |
---|
66 | 'sharable': False, |
---|
67 | 'read_only': False, |
---|
68 | 'other_config': {'location': 'file:/root/gentoo.amd64.img'}, |
---|
69 | } |
---|
70 | |
---|
71 | local_vbd_cfg = { |
---|
72 | 'VDI': '', |
---|
73 | 'VM': '', |
---|
74 | 'device': 'sda1', |
---|
75 | 'mode': 'RW', |
---|
76 | 'type': 'disk', |
---|
77 | 'driver': 'paravirtualised', |
---|
78 | } |
---|
79 | |
---|
80 | vif_cfg = { |
---|
81 | 'name': 'API_VIF', |
---|
82 | 'type': 'paravirtualised', |
---|
83 | 'device': '', |
---|
84 | 'network': '', |
---|
85 | 'MAC': '', |
---|
86 | 'MTU': 1500, |
---|
87 | } |
---|
88 | |
---|
89 | console_cfg = { |
---|
90 | 'protocol': 'rfb', |
---|
91 | 'other_config': {'vncunused': 1, 'vncpasswd': 'testing'}, |
---|
92 | } |
---|
93 | |
---|
94 | import sys |
---|
95 | import time |
---|
96 | sys.path.append('/usr/lib/python') |
---|
97 | |
---|
98 | from xapi import connect, execute |
---|
99 | |
---|
100 | def test_vm_create(): |
---|
101 | server, session = connect() |
---|
102 | vm_uuid = None |
---|
103 | vdi_uuid = None |
---|
104 | local_vdi_uuid = None |
---|
105 | local_vbd_uuid = None |
---|
106 | vbd_uuid = None |
---|
107 | vif_uuid = None |
---|
108 | |
---|
109 | # List all VMs |
---|
110 | vm_list = execute(server, 'VM.get_all', (session,)) |
---|
111 | vm_names = [] |
---|
112 | for vm_uuid in vm_list: |
---|
113 | vm_record = execute(server, 'VM.get_record', (session, vm_uuid)) |
---|
114 | vm_names.append(vm_record['name_label']) |
---|
115 | |
---|
116 | # Get default SR |
---|
117 | sr_list = execute(server, 'SR.get_by_name_label', (session, |
---|
118 | vdi_cfg['SR_name'])) |
---|
119 | sr_uuid = sr_list[0] |
---|
120 | |
---|
121 | local_sr_list = execute(server, 'SR.get_by_name_label', |
---|
122 | (session, local_vdi_cfg['SR_name'])) |
---|
123 | local_sr_uuid = local_sr_list[0] |
---|
124 | |
---|
125 | # Get default network |
---|
126 | net_list = execute(server, 'network.get_all', (session,)) |
---|
127 | net_uuid = net_list[0] |
---|
128 | |
---|
129 | try: |
---|
130 | # Create a new VM |
---|
131 | vm_uuid = execute(server, 'VM.create', (session, vm_cfg)) |
---|
132 | |
---|
133 | # Create a new VDI |
---|
134 | vdi_cfg['SR'] = sr_uuid |
---|
135 | vdi_uuid = execute(server, 'VDI.create', (session, vdi_cfg)) |
---|
136 | |
---|
137 | # Create a VDI backed VBD |
---|
138 | vbd_cfg['VM'] = vm_uuid |
---|
139 | vbd_cfg['VDI'] = vdi_uuid |
---|
140 | vbd_uuid = execute(server, 'VBD.create', (session, vbd_cfg)) |
---|
141 | |
---|
142 | # Create a new VDI (Local) |
---|
143 | local_vdi_cfg['SR'] = local_sr_uuid |
---|
144 | local_vdi_uuid = execute(server, 'VDI.create', |
---|
145 | (session, local_vdi_cfg)) |
---|
146 | |
---|
147 | # Create a new VBD (Local) |
---|
148 | local_vbd_cfg['VM'] = vm_uuid |
---|
149 | local_vbd_cfg['VDI'] = local_vdi_uuid |
---|
150 | local_vbd_uuid = execute(server, 'VBD.create', |
---|
151 | (session, local_vbd_cfg)) |
---|
152 | |
---|
153 | # Create a new VIF |
---|
154 | vif_cfg['network'] = net_uuid |
---|
155 | vif_cfg['VM'] = vm_uuid |
---|
156 | vif_uuid = execute(server, 'VIF.create', (session, vif_cfg)) |
---|
157 | |
---|
158 | # Create a console |
---|
159 | console_cfg['VM'] = vm_uuid |
---|
160 | console_uuid = execute(server, 'console.create', |
---|
161 | (session, console_cfg)) |
---|
162 | print console_uuid |
---|
163 | |
---|
164 | # Start the VM |
---|
165 | execute(server, 'VM.start', (session, vm_uuid, False)) |
---|
166 | |
---|
167 | time.sleep(30) |
---|
168 | |
---|
169 | test_suspend = False |
---|
170 | if test_suspend: |
---|
171 | print 'Suspending VM..' |
---|
172 | execute(server, 'VM.suspend', (session, vm_uuid)) |
---|
173 | print 'Suspended VM.' |
---|
174 | time.sleep(5) |
---|
175 | print 'Resuming VM ...' |
---|
176 | execute(server, 'VM.resume', (session, vm_uuid, False)) |
---|
177 | print 'Resumed VM.' |
---|
178 | |
---|
179 | finally: |
---|
180 | # Wait for user to say we're good to shut it down |
---|
181 | while True: |
---|
182 | destroy = raw_input('destroy VM? ') |
---|
183 | if destroy[0] in ('y', 'Y'): |
---|
184 | break |
---|
185 | |
---|
186 | # Clean up |
---|
187 | if vif_uuid: |
---|
188 | execute(server, 'VIF.destroy', (session, vif_uuid)) |
---|
189 | |
---|
190 | if local_vbd_uuid: |
---|
191 | execute(server, 'VBD.destroy', (session, local_vbd_uuid)) |
---|
192 | if local_vdi_uuid: |
---|
193 | execute(server, 'VDI.destroy', (session, local_vdi_uuid)) |
---|
194 | |
---|
195 | if vbd_uuid: |
---|
196 | execute(server, 'VBD.destroy', (session, vbd_uuid)) |
---|
197 | if vdi_uuid: |
---|
198 | execute(server, 'VDI.destroy', (session, vdi_uuid)) |
---|
199 | |
---|
200 | if vm_uuid: |
---|
201 | try: |
---|
202 | execute(server, 'VM.hard_shutdown', (session, vm_uuid)) |
---|
203 | time.sleep(2) |
---|
204 | except: |
---|
205 | pass |
---|
206 | |
---|
207 | execute(server, 'VM.destroy', (session, vm_uuid)) |
---|
208 | |
---|
209 | |
---|
210 | if __name__ == "__main__": |
---|
211 | test_vm_create() |
---|
212 | |
---|