ia64/xen-unstable

view tools/libxen/test/test_bindings.c @ 13046:a407f14478ba

Updated test to match new booting arrangement.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author Ewan Mellor <ewan@xensource.com>
date Thu Dec 14 18:30:35 2006 +0000 (2006-12-14)
parents de7d7b223db2
children 671cc928183d
line source
1 /*
2 * Copyright (c) 2006 XenSource, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
19 #define _GNU_SOURCE
20 #include <inttypes.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
25 #include <libxml/parser.h>
26 #include <curl/curl.h>
28 #include "xen_host.h"
29 #include "xen_sr.h"
30 #include "xen_vbd.h"
31 #include "xen_vdi.h"
32 #include "xen_vm.h"
35 static void usage()
36 {
37 fprintf(stderr,
38 "Usage:\n"
39 "\n"
40 " test_bindings <url> <username> <password>\n"
41 "\n"
42 "where\n"
43 " <url> is a fragment of the server's URL, e.g. localhost:8005/RPC2;\n"
44 " <username> is the username to use at the server; and\n"
45 " <password> is the password.\n");
47 exit(EXIT_FAILURE);
48 }
51 static char *url;
54 typedef struct
55 {
56 xen_result_func func;
57 void *handle;
58 } xen_comms;
61 static void create_new_vm(xen_session *session);
64 static size_t
65 write_func(void *ptr, size_t size, size_t nmemb, xen_comms *comms)
66 {
67 size_t n = size * nmemb;
68 return comms->func(ptr, n, comms->handle) ? n : 0;
69 }
72 static int
73 call_func(const void *data, size_t len, void *user_handle,
74 void *result_handle, xen_result_func result_func)
75 {
76 (void)user_handle;
78 CURL *curl = curl_easy_init();
79 if (!curl) {
80 return -1;
81 }
83 xen_comms comms = {
84 .func = result_func,
85 .handle = result_handle
86 };
88 curl_easy_setopt(curl, CURLOPT_URL, url);
89 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
90 curl_easy_setopt(curl, CURLOPT_MUTE, 1);
91 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_func);
92 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &comms);
93 curl_easy_setopt(curl, CURLOPT_POST, 1);
94 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
95 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, len);
97 CURLcode result = curl_easy_perform(curl);
99 curl_easy_cleanup(curl);
101 return result;
102 }
105 static void print_error(xen_session *session)
106 {
107 fprintf(stderr, "Error: %d", session->error_description_count);
108 for (int i = 0; i < session->error_description_count; i++)
109 {
110 fprintf(stderr, "%s ", session->error_description[i]);
111 }
112 fprintf(stderr, "\n");
113 }
116 int main(int argc, char **argv)
117 {
118 if (argc != 4)
119 {
120 usage();
121 }
123 url = argv[1];
124 char *username = argv[2];
125 char *password = argv[3];
127 xmlInitParser();
128 xen_init();
129 curl_global_init(CURL_GLOBAL_ALL);
131 #define CLEANUP \
132 do { \
133 xen_session_logout(session); \
134 curl_global_cleanup(); \
135 xen_fini(); \
136 xmlCleanupParser(); \
137 } while(0) \
140 xen_session *session =
141 xen_session_login_with_password(call_func, NULL, username, password);
143 xen_vm vm;
144 if (!xen_vm_get_by_uuid(session, &vm,
145 "00000000-0000-0000-0000-000000000000"))
146 {
147 print_error(session);
148 CLEANUP;
149 return 1;
150 }
152 char *vm_uuid;
153 if (!xen_vm_get_uuid(session, &vm_uuid, vm))
154 {
155 print_error(session);
156 xen_vm_free(vm);
157 CLEANUP;
158 return 1;
159 }
161 char *vm_uuid_bytes;
162 if (!xen_uuid_string_to_bytes(vm_uuid, &vm_uuid_bytes))
163 {
164 fprintf(stderr, "xen_uuid_string_to_bytes failed.\n");
165 xen_uuid_free(vm_uuid);
166 xen_vm_free(vm);
167 CLEANUP;
168 return 1;
169 }
171 xen_vm_record *vm_record;
172 if (!xen_vm_get_record(session, &vm_record, vm))
173 {
174 print_error(session);
175 xen_uuid_bytes_free(vm_uuid_bytes);
176 xen_uuid_free(vm_uuid);
177 xen_vm_free(vm);
178 CLEANUP;
179 return 1;
180 }
182 xen_host host;
183 if (!xen_session_get_this_host(session, &host))
184 {
185 print_error(session);
186 xen_vm_record_free(vm_record);
187 xen_uuid_bytes_free(vm_uuid_bytes);
188 xen_uuid_free(vm_uuid);
189 xen_vm_free(vm);
190 CLEANUP;
191 return 1;
192 }
194 xen_string_string_map *versions;
195 if (!xen_host_get_software_version(session, &versions, host))
196 {
197 print_error(session);
198 xen_host_free(host);
199 xen_vm_record_free(vm_record);
200 xen_uuid_bytes_free(vm_uuid_bytes);
201 xen_uuid_free(vm_uuid);
202 xen_vm_free(vm);
203 CLEANUP;
204 return 1;
205 }
207 printf("%s.\n", vm_uuid);
209 fprintf(stderr, "In bytes, the VM UUID is ");
210 for (int i = 0; i < 15; i++)
211 {
212 fprintf(stderr, "%x, ", (unsigned int)vm_uuid_bytes[i]);
213 }
214 fprintf(stderr, "%x.\n", (unsigned int)vm_uuid_bytes[15]);
216 printf("%zd.\n", versions->size);
218 for (size_t i = 0; i < versions->size; i++)
219 {
220 printf("%s -> %s.\n", versions->contents[i].key,
221 versions->contents[i].val);
222 }
224 printf("%s.\n", vm_record->uuid);
226 printf("Resident on %s.\n", (char *)vm_record->resident_on->u.handle);
228 printf("%s.\n", xen_vm_power_state_to_string(vm_record->power_state));
230 for (size_t i = 0; i < vm_record->vcpus_utilisation->size; i++)
231 {
232 printf("%"PRId64" -> %lf.\n",
233 vm_record->vcpus_utilisation->contents[i].key,
234 vm_record->vcpus_utilisation->contents[i].val);
235 }
237 xen_uuid_bytes_free(vm_uuid_bytes);
238 xen_uuid_free(vm_uuid);
239 xen_vm_free(vm);
241 xen_vm_record_free(vm_record);
243 xen_host_free(host);
244 xen_string_string_map_free(versions);
247 create_new_vm(session);
248 if (!session->ok)
249 {
250 /* Error has been logged, just clean up. */
251 CLEANUP;
252 return 1;
253 }
255 CLEANUP;
257 return 0;
258 }
261 /**
262 * Creation of a new VM, using the Named Parameters idiom. Allocate the
263 * xen_vm_record here, but the sets through the library. Either
264 * allocation patterns can be used, as long as the allocation and free are
265 * paired correctly.
266 */
267 static void create_new_vm(xen_session *session)
268 {
269 xen_cpu_feature_set *empty_cpu_feature_set =
270 xen_cpu_feature_set_alloc(0);
272 xen_cpu_feature_set *force_off_cpu_feature_set =
273 xen_cpu_feature_set_alloc(1);
274 force_off_cpu_feature_set->contents[0] = XEN_CPU_FEATURE_MMX;
276 xen_vm_record vm_record =
277 {
278 .name_label = "NewVM",
279 .name_description = "New VM Description",
280 .user_version = 1,
281 .is_a_template = false,
282 .memory_static_max = 256,
283 .memory_dynamic_max = 256,
284 .memory_dynamic_min = 128,
285 .memory_static_min = 128,
286 .vcpus_policy = "credit",
287 .vcpus_params = "",
288 .vcpus_number = 2,
289 .vcpus_features_required = empty_cpu_feature_set,
290 .vcpus_features_can_use = empty_cpu_feature_set,
291 .vcpus_features_force_on = empty_cpu_feature_set,
292 .vcpus_features_force_off = force_off_cpu_feature_set,
293 .actions_after_shutdown = XEN_ON_NORMAL_EXIT_DESTROY,
294 .actions_after_reboot = XEN_ON_NORMAL_EXIT_RESTART,
295 .actions_after_suspend = XEN_ON_NORMAL_EXIT_DESTROY,
296 .actions_after_crash = XEN_ON_CRASH_BEHAVIOUR_PRESERVE,
297 .hvm_boot = "",
298 .pv_bootloader = "pygrub",
299 .pv_kernel = "/boot/vmlinuz-2.6.16.33-xen",
300 .pv_ramdisk = "",
301 .pv_args = "",
302 .pv_bootloader_args = ""
303 };
306 xen_vm vm;
307 xen_vm_create(session, &vm, &vm_record);
309 xen_cpu_feature_set_free(empty_cpu_feature_set);
310 xen_cpu_feature_set_free(force_off_cpu_feature_set);
312 if (!session->ok)
313 {
314 fprintf(stderr, "VM creation failed.\n");
315 print_error(session);
316 return;
317 }
320 /*
321 * Create a new disk for the new VM.
322 */
323 xen_sr_set *srs;
324 if (!xen_sr_get_by_name_label(session, &srs, "Local") ||
325 srs->size < 1)
326 {
327 fprintf(stderr, "SR lookup failed.\n");
328 print_error(session);
329 xen_vm_free(vm);
330 return;
331 }
333 xen_sr_record_opt sr_record =
334 {
335 .u.handle = srs->contents[0]
336 };
337 xen_vdi_record vdi0_record =
338 {
339 .name_label = "MyRootFS",
340 .name_description = "MyRootFS description",
341 .sr = &sr_record,
342 .virtual_size = (1 << 21), // 1GiB / 512 bytes/sector
343 .sector_size = 512,
344 .type = XEN_VDI_TYPE_SYSTEM,
345 .sharable = false,
346 .read_only = false
347 };
349 xen_vdi vdi0;
350 if (!xen_vdi_create(session, &vdi0, &vdi0_record))
351 {
352 fprintf(stderr, "VDI creation failed.\n");
353 print_error(session);
355 xen_sr_set_free(srs);
356 xen_vm_free(vm);
357 return;
358 }
361 xen_vm_record_opt vm_record_opt =
362 {
363 .u.handle = vm
364 };
365 xen_vdi_record_opt vdi0_record_opt =
366 {
367 .u.handle = vdi0
368 };
369 xen_vbd_record vbd0_record =
370 {
371 .vm = &vm_record_opt,
372 .vdi = &vdi0_record_opt,
373 .device = "xvda1",
374 .mode = XEN_VBD_MODE_RW,
375 .driver = XEN_DRIVER_TYPE_PARAVIRTUALISED
376 };
378 xen_vbd vbd0;
379 if (!xen_vbd_create(session, &vbd0, &vbd0_record))
380 {
381 fprintf(stderr, "VBD creation failed.\n");
382 print_error(session);
384 xen_vdi_free(vdi0);
385 xen_sr_set_free(srs);
386 xen_vm_free(vm);
387 return;
388 }
390 char *vm_uuid;
391 char *vdi0_uuid;
392 char *vbd0_uuid;
394 xen_vm_get_uuid(session, &vm_uuid, vm);
395 xen_vdi_get_uuid(session, &vdi0_uuid, vdi0);
396 xen_vbd_get_uuid(session, &vbd0_uuid, vbd0);
398 if (!session->ok)
399 {
400 fprintf(stderr, "get_uuid call failed.\n");
401 print_error(session);
403 xen_uuid_free(vm_uuid);
404 xen_uuid_free(vdi0_uuid);
405 xen_uuid_free(vbd0_uuid);
406 xen_vbd_free(vbd0);
407 xen_vdi_free(vdi0);
408 xen_sr_set_free(srs);
409 xen_vm_free(vm);
410 return;
411 }
413 fprintf(stderr,
414 "Created a new VM, with UUID %s, VDI UUID %s, and VBD UUID %s.\n",
415 vm_uuid, vdi0_uuid, vbd0_uuid);
417 xen_uuid_free(vm_uuid);
418 xen_uuid_free(vdi0_uuid);
419 xen_uuid_free(vbd0_uuid);
420 xen_vbd_free(vbd0);
421 xen_vdi_free(vdi0);
422 xen_sr_set_free(srs);
423 xen_vm_free(vm);
424 }