os-cmpi-xen

view src/Xen_VirtualSystemManagementService.c @ 121:4868ace2726b

Add initial consistence checks in test suite for Xen_MemoryPool, Xen_ProcessorPool and Xen_VirtualSystemManagementService.

Signed-off-by: Luke Szymanski <Lukasz.Szymanski@Unisys.com>
author Jim Fehlig <jfehlig@novell.com>
date Fri Jun 08 10:22:05 2007 -0600 (2007-06-08)
parents 1903faaf4d37
children 3e8654ff41d1
line source
1 // Copyright (C) 2006 IBM Corporation
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 // ============================================================================
17 // Authors: Dr. Gareth S. Bestor, <bestor@us.ibm.com>
18 // Contributors: Jim Fehlig, <jfehlig@novell.com>
19 // Raj Subrahmanian <raj.subrahmanian@unisys.com>
20 // Description:
21 // ============================================================================
23 /**
24 * TODO:
25 * 1. Currently not checking if incoming embedded settings are valid in
26 * the various extrinsic method code paths within InvokeMethod().
27 */
29 #include <string.h>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <assert.h>
34 #include <xen_vm.h>
35 #include <xen_vif.h>
36 #include <xen_vbd.h>
37 #include <xen_vdi.h>
38 #include <xen_sr.h>
39 #include <xen_console.h>
40 #include <xen_vm_metrics.h>
41 #include <xen_string_string_map.h>
43 /* Include utility functions */
44 #include "cmpiutil.h"
46 /* Include _SBLIM_TRACE() logging support */
47 #include "cmpitrace.h"
49 /* Include Xen utilities */
50 #include "xen_utils.h"
52 /* Include the abstract resource access functions and abstracted _RESOURCES and _RESOURCE data types. */
53 #include "Xen_VirtualSystemManagementService_Resource.h"
55 /* Include the required CMPI data types, function headers, and macros */
56 #include "cmpidt.h"
57 #include "cmpift.h"
58 #include "cmpimacs.h"
60 #include "provider_common.h"
61 #include "xen_utils.h"
64 // ----------------------------------------------------------------------------
65 // COMMON GLOBAL VARIABLES
66 // ----------------------------------------------------------------------------
68 /* Handle to the CIM broker. Initialized when the provider lib is loaded. */
69 static const CMPIBroker *_BROKER;
71 /* Xen session object. Initialize when the provider is loaded, close when
72 * provider unloaded. */
73 static xen_utils_session *session = NULL;
76 // ============================================================================
77 // CMPI INSTANCE PROVIDER FUNCTION TABLE
78 // ============================================================================
80 // ----------------------------------------------------------------------------
81 // Info for the class supported by the instance provider
82 // ----------------------------------------------------------------------------
84 /* Name of the class implemented by this instance provider. */
85 /*** CUSTOMIZE FOR EACH PROVIDER ***/
86 static char * _CLASSNAME = "Xen_VirtualSystemManagementService";
88 /* NULL terminated list of key properties of this class. */
89 /*** CUSTOMIZE FOR EACH PROVIDER ***/
90 const static char * _KEYNAMES[] = {"SystemName", "SystemCreationClassName", "Name", "CreationClassName", NULL};
93 static int create_vm(CMPIInstance* vsSettingsInst,
94 CMPIArray *resourceSettings,
95 xen_vm *result, CMPIStatus *status);
96 static int add_resource_to_vm(xen_vm_record *vmRec,
97 CMPIInstance *sourceSettingInst,
98 CMPIObjectPath **resultSetting,
99 char *namespace,
100 CMPIStatus *status);
101 static void remove_vm(xen_vm vm);
102 static int vssd2xenconfig(CMPIInstance *vssd, xen_vm_record *vm_rec, CMPIStatus *status);
103 static int proc_rasd2vmconfig(CMPIInstance *proc_rasd, xen_vm_record *vm_rec,
104 CMPIStatus *status);
105 static int mem_rasd2vmconfig(CMPIInstance *instance, xen_vm_record *vm_rec,
106 CMPIStatus *status);
107 static int disk_rasd2vmconfig(CMPIInstance *instance, xen_vbd_record **vbd_rec,
108 xen_vdi_record **vdi_rec, CMPIStatus *status);
109 static int nic_rasd2vmconfig(CMPIInstance *instance, xen_vif_record **vif_rec,
110 CMPIStatus *status);
111 static int con_rasd2vmconfig(CMPIInstance *instance, xen_console_record *con_rec,
112 CMPIStatus *status);
113 static int add_strings_to_map(const char *key, const char *val,
114 xen_string_string_map **map);
115 static CMPIInstance *parse_embedded_instance(char *instanceStr);
119 // ----------------------------------------------------------------------------
120 // Cleanup()
121 // Perform any necessary cleanup immediately before this provider is unloaded.
122 // ----------------------------------------------------------------------------
123 static CMPIStatus Cleanup(
124 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
125 const CMPIContext * context, /* [in] Additional context info, if any. */
126 CMPIBoolean terminating) /* [in] True if MB is terminating */
127 {
128 CMPIStatus status = { CMPI_RC_OK, NULL }; /* Return status of CIM operations. */
130 _SBLIM_ENTER("Cleanup");
131 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
132 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
134 if (session) {
135 xen_utils_xen_close(session);
136 session = NULL;
137 }
138 _SBLIM_RETURNSTATUS(status);
139 }
141 // ----------------------------------------------------------------------------
142 // EnumInstanceNames()
143 // Return a list of all the instances names (return their object paths only).
144 // ----------------------------------------------------------------------------
145 static CMPIStatus EnumInstanceNames(
146 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
147 const CMPIContext * context, /* [in] Additional context info, if any. */
148 const CMPIResult * results, /* [out] Results of this operation. */
149 const CMPIObjectPath * reference) /* [in] Contains target namespace and classname. */
150 {
151 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
152 _RESOURCES * resources = NULL; /* Handle to the list of system resources. */
153 _RESOURCE * resource; /* Handle to each system resource. */
154 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
155 int found = 0; /* Found any instances? */
157 _SBLIM_ENTER("EnumInstanceNames");
158 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
159 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
160 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
161 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
163 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
164 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
165 goto exit;
166 }
168 /* Get a handle to the list of system resources. */
169 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
170 _SBLIM_TRACE(1,("--- _getResources() failed"));
171 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
172 goto exit;
173 }
175 /* Enumerate thru the list of system resources and return a CMPIInstance for each. */
176 while (Xen_VirtualSystemManagementService_getNextResource(session, resources, &resource)) {
177 /* Create a new CMPIInstance to store this resource. */
178 CMPIInstance * instance = _CMNewInstance(_BROKER, namespace, _CLASSNAME, &status);
179 if ((status.rc != CMPI_RC_OK) || CMIsNullObject(instance)) {
180 _SBLIM_TRACE(1,("--- CMNewInstance() failed - %s", CMGetCharPtr(status.msg)));
181 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERROR, "Cannot create new CMPIInstance");
182 goto exit;
183 }
185 /* Set the instance property values from the resource data. */
186 if (!Xen_VirtualSystemManagementService_setInstanceFromResource(resource, instance, _BROKER)) {
187 _SBLIM_TRACE(1,("--- _setInstanceFromResource() failed"));
188 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to set property values from resource data");
189 goto exit;
190 }
192 /* Free the resource data. */
193 if (!Xen_VirtualSystemManagementService_freeResource(resource)) {
194 _SBLIM_TRACE(1,("--- _freeResource() failed"));
195 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free resource data");
196 goto exit;
197 }
199 /* Return the CMPIObjectPath for this instance. */
200 CMPIObjectPath * objectpath = CMGetObjectPath(instance, &status);
201 if ((status.rc != CMPI_RC_OK) || CMIsNullObject(objectpath)) {
202 _SBLIM_TRACE(1,("--- CMGetObjectPath() failed - %s", CMGetCharPtr(status.msg)));
203 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERROR, "Cannot get CMPIObjectPath for instance");
204 goto exit;
205 }
206 CMSetNameSpace(objectpath, namespace); /* Note - CMGetObjectPath() does not preserve the namespace! */
208 _SBLIM_TRACE(3,("--- objectpath=\"%s\"", CMGetCharPtr(CDToString(_BROKER, objectpath, NULL))));
209 CMReturnObjectPath(results, objectpath);
210 found++;
211 }
213 _SBLIM_TRACE(2,("--- %d object paths found", found));
214 CMReturnDone(results);
216 exit:
217 /* Free the list of system resources. */
218 if (!Xen_VirtualSystemManagementService_freeResources(resources)) {
219 _SBLIM_TRACE(1,("--- _freeResources() failed"));
220 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free list of system resources");
221 }
223 _SBLIM_RETURNSTATUS(status);
224 }
226 // ----------------------------------------------------------------------------
227 // EnumInstances()
228 // Return a list of all the instances (return all the instance data).
229 // ----------------------------------------------------------------------------
230 static CMPIStatus EnumInstances(
231 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
232 const CMPIContext * context, /* [in] Additional context info, if any. */
233 const CMPIResult * results, /* [out] Results of this operation. */
234 const CMPIObjectPath * reference, /* [in] Contains target namespace and classname. */
235 const char ** properties) /* [in] List of desired properties (NULL=all). */
236 {
237 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
238 _RESOURCES * resources = NULL; /* Handle to the list of system resources. */
239 _RESOURCE * resource; /* Handle to each system resource. */
240 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
241 int found = 0; /* Found any resource instances? */
243 _SBLIM_ENTER("EnumInstances");
244 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
245 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
246 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
247 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
249 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
250 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
251 goto exit;
252 }
254 /* Get a handle to the list of system resources. */
255 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
256 _SBLIM_TRACE(1,("--- _getResources() failed"));
257 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
258 goto exit;
259 }
261 /* Enumerate thru the list of system resources and return a CMPIInstance for each. */
262 while (Xen_VirtualSystemManagementService_getNextResource(session, resources, &resource)) {
263 /* Create a new CMPIInstance to store this resource. */
264 CMPIInstance * instance = _CMNewInstance(_BROKER, namespace, _CLASSNAME, &status);
265 if ((status.rc != CMPI_RC_OK) || CMIsNullObject(instance)) {
266 _SBLIM_TRACE(1,("--- CMNewInstance() failed - %s", CMGetCharPtr(status.msg)));
267 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERROR, "Cannot create new CMPIInstance");
268 goto exit;
269 }
271 /* Setup a filter to only return the desired properties. */
272 status = CMSetPropertyFilter(instance, properties, _KEYNAMES);
273 if (status.rc != CMPI_RC_OK) {
274 _SBLIM_TRACE(1, ("--- CMSetPropertyFilter() failed - %s", CMGetCharPtr(status.msg)));
275 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Cannot set property filter");
276 goto exit;
277 }
279 /* Set the instance property values from the resource data. */
280 if (!Xen_VirtualSystemManagementService_setInstanceFromResource(resource, instance, _BROKER)) {
281 _SBLIM_TRACE(1,("--- _setInstanceFromResource() failed"));
282 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to set property values from resource data");
283 goto exit;
284 }
286 /* Free the resource data. */
287 if (!Xen_VirtualSystemManagementService_freeResource(resource)) {
288 _SBLIM_TRACE(1,("--- _freeResource() failed"));
289 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free resource data");
290 goto exit;
291 }
293 /* Return the CMPIInstance for this instance. */
294 _SBLIM_TRACE(3,("--- instance=\"%s\"", CMGetCharPtr(CDToString(_BROKER, instance, NULL))));
295 CMReturnInstance(results, instance);
296 found++;
297 }
299 _SBLIM_TRACE(2,("--- %d instances found", found));
300 CMReturnDone(results);
302 exit:
303 /* Free the list of system resources. */
304 if (!Xen_VirtualSystemManagementService_freeResources(resources)) {
305 _SBLIM_TRACE(1,("--- _freeResources() failed"));
306 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free list of system resources");
307 }
309 _SBLIM_RETURNSTATUS(status);
310 }
312 // ----------------------------------------------------------------------------
313 // GetInstance()
314 // Return the instance data for the specified instance only.
315 // ----------------------------------------------------------------------------
316 static CMPIStatus GetInstance(
317 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
318 const CMPIContext * context, /* [in] Additional context info, if any. */
319 const CMPIResult * results, /* [out] Results of this operation. */
320 const CMPIObjectPath * reference, /* [in] Contains the target namespace, classname and object path. */
321 const char ** properties) /* [in] List of desired properties (NULL=all). */
322 {
323 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
324 _RESOURCES * resources = NULL; /* Handle to the list of system resources. */
325 _RESOURCE * resource; /* Handle to the system resource. */
326 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
327 int found = 0; /* Found the target instance? */
329 _SBLIM_ENTER("GetInstance");
330 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
331 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
332 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
333 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
335 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
336 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
337 goto exit;
338 }
340 /* Get a handle to the list of system resources. */
341 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
342 _SBLIM_TRACE(1,("--- _getResources() failed"));
343 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
344 goto exit;
345 }
347 /* Get the target resource. */
348 found = Xen_VirtualSystemManagementService_getResourceForObjectPath(session, resources, &resource, reference);
349 if (!found || (resource == NULL)) {
350 _SBLIM_TRACE(1,("--- Target instance not found"));
351 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_FOUND, "Target instance not found");
352 goto exit;
353 }
355 /* Create a new CMPIInstance to store this resource. */
356 CMPIInstance * instance = _CMNewInstance(_BROKER, namespace, _CLASSNAME, &status);
357 if ((status.rc != CMPI_RC_OK) || CMIsNullObject(instance)) {
358 _SBLIM_TRACE(1,("--- CMNewInstance() failed - %s", CMGetCharPtr(status.msg)));
359 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERROR, "Cannot create new CMPIInstance");
360 goto exit;
361 }
363 /* Setup a filter to only return the desired properties. */
364 status = CMSetPropertyFilter(instance, properties, _KEYNAMES);
365 if (status.rc != CMPI_RC_OK) {
366 _SBLIM_TRACE(1, ("--- CMSetPropertyFilter() failed - %s", CMGetCharPtr(status.msg)));
367 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Cannot set property filter");
368 goto exit;
369 }
371 /* Set the instance property values from the resource data. */
372 if (!Xen_VirtualSystemManagementService_setInstanceFromResource(resource, instance, _BROKER)) {
373 _SBLIM_TRACE(1,("--- _setInstanceFromResource() failed"));
374 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to set property values from resource data");
375 goto exit;
376 }
378 /* Free the resource data. */
379 if (!Xen_VirtualSystemManagementService_freeResource(resource)) {
380 _SBLIM_TRACE(1,("--- _freeResource() failed"));
381 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free resource data");
382 goto exit;
383 }
385 /* Return the CMPIInstance for this instance. */
386 _SBLIM_TRACE(3,("--- instance=\"%s\"", CMGetCharPtr(CDToString(_BROKER, instance, NULL))));
387 CMReturnInstance(results, instance);
389 _SBLIM_TRACE(2,("--- instance found"));
390 CMReturnDone(results);
392 exit:
393 /* Free the list of system resources. */
394 if (!Xen_VirtualSystemManagementService_freeResources(resources)) {
395 _SBLIM_TRACE(1,("--- _freeResources() failed"));
396 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free list of system resources");
397 }
399 _SBLIM_RETURNSTATUS(status);
400 }
402 // ----------------------------------------------------------------------------
403 // SetInstance()
404 // Save modified instance data for the specified instance.
405 // ----------------------------------------------------------------------------
406 static CMPIStatus SetInstance(
407 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
408 const CMPIContext * context, /* [in] Additional context info, if any. */
409 const CMPIResult * results, /* [out] Results of this operation. */
410 const CMPIObjectPath * reference, /* [in] Contains the target namespace, classname and object path. */
411 const CMPIInstance * newinstance, /* [in] Contains the new instance data. */
412 const char **properties)
413 {
414 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
415 _RESOURCES * resources = NULL; /* Handle to the list of system resources. */
416 _RESOURCE * resource; /* Handle to the system resource. */
417 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
418 int found = 0; /* Found the target instance? */
420 _SBLIM_ENTER("SetInstance");
421 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
422 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
423 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
424 _SBLIM_TRACE(2, ("--- newinstance=\"%s\"", CMGetCharPtr(CDToString(_BROKER, newinstance, NULL))));
425 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
427 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
428 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
429 goto exit;
430 }
432 /* Get a handle to the list of system resources. */
433 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
434 _SBLIM_TRACE(1,("--- _getResources() failed"));
435 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
436 goto exit;
437 }
439 /* Get the target resource. */
440 found = Xen_VirtualSystemManagementService_getResourceForObjectPath(session, resources, &resource, reference);
441 if (!found || (resource == NULL)) {
442 _SBLIM_TRACE(1,("--- Target instance not found"));
443 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_FOUND, "Target instance not found");
444 goto exit;
445 }
447 _SBLIM_TRACE(2,("--- instance found"));
449 /* Update the target resource data with the new instance property values. */
450 int rc = Xen_VirtualSystemManagementService_setResourceFromInstance(resource, newinstance, _BROKER);
452 /* Free the resource data. */
453 if (!Xen_VirtualSystemManagementService_freeResource(resource)) {
454 _SBLIM_TRACE(1,("--- _freeResource() failed"));
455 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free resource data");
456 goto exit;
457 }
459 if (rc != 1) {
460 if (rc == -1) {
461 _SBLIM_TRACE(1,("--- _setResourceFromInstance() unsupported"));
462 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
463 } else {
464 _SBLIM_TRACE(1,("--- _setResourceFromInstance() failed"));
465 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to set resource data from instance properties");
466 }
467 goto exit;
468 }
470 exit:
471 /* Free the list of system resources. */
472 if (!Xen_VirtualSystemManagementService_freeResources(resources)) {
473 _SBLIM_TRACE(1,("--- _freeResources() failed"));
474 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free list of system resources");
475 }
477 _SBLIM_RETURNSTATUS(status);
478 }
480 // ----------------------------------------------------------------------------
481 // CreateInstance()
482 // Create a new instance from the specified instance data.
483 // ----------------------------------------------------------------------------
484 static CMPIStatus CreateInstance(
485 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
486 const CMPIContext * context, /* [in] Additional context info, if any. */
487 const CMPIResult * results, /* [out] Results of this operation. */
488 const CMPIObjectPath * reference, /* [in] Contains the target namespace, classname and object path. */
489 const CMPIInstance * newinstance) /* [in] Contains the new instance data. */
490 {
491 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
492 _RESOURCES * resources = NULL; /* Handle to the list of system resources. */
493 _RESOURCE * resource; /* Handle to the system resource. */
494 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
495 int found = 0; /* Found the target instance? */
497 _SBLIM_ENTER("CreateInstance");
498 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
499 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
500 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
501 _SBLIM_TRACE(2, ("--- newinstance=\"%s\"", CMGetCharPtr(CDToString(_BROKER, newinstance, NULL))));
502 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
504 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
505 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
506 goto exit;
507 }
509 /* WORKAROUND FOR PEGASUS BUG?! reference does not contain object path, only namespace & classname. */
510 reference = CMGetObjectPath(newinstance, NULL);
512 /* Get a handle to the list of system resources. */
513 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
514 _SBLIM_TRACE(1,("--- _getResources() failed"));
515 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
516 goto exit;
517 }
519 /* Get the target resource. */
520 found = Xen_VirtualSystemManagementService_getResourceForObjectPath(session, resources, &resource, reference);
522 /* Free the resource data. */
523 if (!Xen_VirtualSystemManagementService_freeResource(resource)) {
524 _SBLIM_TRACE(1,("--- _freeResource() failed"));
525 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free resource data");
526 goto exit;
527 }
529 if (found) {
530 _SBLIM_TRACE(1,("--- Target instance already exists"));
531 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_ALREADY_EXISTS, "Target instance already exists");
532 goto exit;
533 }
535 /* Create a new resource with the new instance property values. */
536 int rc = Xen_VirtualSystemManagementService_createResourceFromInstance(resources, &resource, newinstance, _BROKER);
537 if (rc != 1) {
538 if (rc == -1) {
539 _SBLIM_TRACE(1,("--- _createResourceFromInstance() unsupported"));
540 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
541 } else {
542 _SBLIM_TRACE(1,("--- _createResourceFromInstance() failed"));
543 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to create resource data from instance properties");
544 }
545 goto exit;
546 }
548 /* Return the object path for the newly created instance. */
549 CMPIObjectPath * objectpath = CMGetObjectPath(newinstance, NULL);
550 CMSetNameSpace(objectpath, namespace);
551 CMReturnObjectPath(results, objectpath);
552 CMReturnDone(results);
554 exit:
555 /* Free the list of system resources. */
556 if (!Xen_VirtualSystemManagementService_freeResources(resources)) {
557 _SBLIM_TRACE(1,("--- _freeResources() failed"));
558 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free list of system resources");
559 }
561 _SBLIM_RETURNSTATUS(status);
562 }
564 // ----------------------------------------------------------------------------
565 // DeleteInstance()
566 // Delete or remove the specified instance from the system.
567 // ----------------------------------------------------------------------------
568 static CMPIStatus DeleteInstance(
569 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
570 const CMPIContext * context, /* [in] Additional context info, if any. */
571 const CMPIResult * results, /* [out] Results of this operation. */
572 const CMPIObjectPath * reference) /* [in] Contains the target namespace, classname and object path. */
573 {
574 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
575 _RESOURCES * resources = NULL; /* Handle to the list of system resources. */
576 _RESOURCE * resource; /* Handle to the system resource. */
577 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
578 int found = 0; /* Found the target instance? */
580 _SBLIM_ENTER("DeleteInstance");
581 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
582 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
583 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
584 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
586 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
587 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
588 goto exit;
589 }
591 /* Get a handle to the list of system resources. */
592 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
593 _SBLIM_TRACE(1,("--- _getResources() failed"));
594 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
595 goto exit;
596 }
598 /* Get the target resource. */
599 found = Xen_VirtualSystemManagementService_getResourceForObjectPath(session, resources, &resource, reference);
600 if (!found || (resource == NULL)) {
601 _SBLIM_TRACE(1,("--- Target instance not found"));
602 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_FOUND, "Target instance not found");
603 goto exit;
604 }
606 _SBLIM_TRACE(2,("--- instance found"));
608 /* Delete the target resource. */
609 int rc = Xen_VirtualSystemManagementService_deleteResource(resources, resource);
611 /* Free the resource data. */
612 if (!Xen_VirtualSystemManagementService_freeResource(resource)) {
613 _SBLIM_TRACE(1,("--- _freeResource() failed"));
614 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free resource data");
615 goto exit;
616 }
618 if (rc != 1) {
619 if (rc == -1) {
620 _SBLIM_TRACE(1,("--- __deleteResource() unsupported"));
621 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
622 } else {
623 _SBLIM_TRACE(1,("--- _deleteResource() failed"));
624 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to delete resource");
625 }
626 goto exit;
627 }
629 exit:
630 /* Free the list of system resources. */
631 if (!Xen_VirtualSystemManagementService_freeResources(resources)) {
632 _SBLIM_TRACE(1,("--- _freeResources() failed"));
633 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free list of system resources");
634 }
636 _SBLIM_RETURNSTATUS(status);
637 }
640 // ----------------------------------------------------------------------------
641 // ExecQuery()
642 // Return a list of all the instances that satisfy the specified query filter.
643 // ----------------------------------------------------------------------------
644 static CMPIStatus ExecQuery(
645 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
646 const CMPIContext * context, /* [in] Additional context info, if any. */
647 const CMPIResult * results, /* [out] Results of this operation. */
648 const CMPIObjectPath * reference, /* [in] Contains the target namespace and classname. */
649 const char * language, /* [in] Name of the query language. */
650 const char * query) /* [in] Text of the query written in the query language. */
651 {
652 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
653 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
655 _SBLIM_ENTER("ExecQuery");
656 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
657 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
658 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
659 _SBLIM_TRACE(2, ("--- language=\"%s\"", language));
660 _SBLIM_TRACE(2, ("--- query=\"%s\"", query));
661 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
663 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
664 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
665 goto exit;
666 }
668 /* EXECQUERY() IS NOT YET SUPPORTED FOR THIS CLASS */
669 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
671 CMReturnDone(results);
673 exit:
674 _SBLIM_RETURNSTATUS(status);
675 }
677 // ----------------------------------------------------------------------------
678 // InstInitialize()
679 // Perform any necessary initialization immediately after the instance provider
680 // is first loaded.
681 // ----------------------------------------------------------------------------
682 static void InstInitialize(
683 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
684 const CMPIContext * context) /* [in] Additional context info, if any. */
685 {
686 _SBLIM_ENTER("InstInitialize");
687 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
688 // _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
690 /* Initialized Xen session object. */
691 if (session == NULL)
692 xen_utils_xen_init(&session);
694 _SBLIM_RETURN();
695 }
698 // ============================================================================
699 // CMPI INSTANCE PROVIDER FUNCTION TABLE SETUP
700 // ============================================================================
701 CMInstanceMIStub( , Xen_VirtualSystemManagementService, _BROKER, InstInitialize(&mi, ctx));
704 // ============================================================================
705 // CMPI METHOD PROVIDER FUNCTION TABLE
706 // ============================================================================
708 // ----------------------------------------------------------------------------
709 // MethodCleanup()
710 // Perform any necessary cleanup immediately before this provider is unloaded.
711 // ----------------------------------------------------------------------------
712 static CMPIStatus MethodCleanup(
713 CMPIMethodMI * self, /* [in] Handle to this provider (i.e. 'self'). */
714 const CMPIContext * context, /* [in] Additional context info, if any. */
715 CMPIBoolean terminating) /* [in] True if MB is terminating */
716 {
717 CMPIStatus status = { CMPI_RC_OK, NULL }; /* Return status of CIM operations. */
719 _SBLIM_ENTER("MethodCleanup");
720 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
721 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
723 if (session) {
724 xen_utils_xen_close(session);
725 session = NULL;
726 }
727 _SBLIM_RETURNSTATUS(status);
728 }
730 // ----------------------------------------------------------------------------
731 // InvokeMethod()
732 // Execute an extrinsic method on the specified instance.
733 // ----------------------------------------------------------------------------
734 static CMPIStatus InvokeMethod(
735 CMPIMethodMI * self, /* [in] Handle to this provider (i.e. 'self') */
736 const CMPIContext * context, /* [in] Additional context info, if any */
737 const CMPIResult * results, /* [out] Results of this operation */
738 const CMPIObjectPath * reference, /* [in] Contains the CIM namespace, classname and desired object path */
739 const char * methodname, /* [in] Name of the method to apply against the reference object */
740 const CMPIArgs * argsin, /* [in] Method input arguments */
741 CMPIArgs * argsout) /* [in] Method output arguments */
742 {
743 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations */
744 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
745 int rc = 0;
746 int argsInCount;
747 _RESOURCES * resources; /* Handle to the list of system resources. */
748 CMPIData argdata;
749 char error_msg[XEN_UTILS_ERROR_BUF_LEN];
751 _SBLIM_ENTER("InvokeMethod");
752 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
753 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
754 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
755 _SBLIM_TRACE(2, ("--- methodname=\"%s\"", methodname));
756 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
758 if (!xen_utils_validate_session(&session)) {
759 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_METHOD_NOT_AVAILABLE, "Unable to connect to xen daemon");
760 goto Exit;
761 }
763 argsInCount = CMGetArgCount(argsin, NULL);
764 _SBLIM_TRACE(2, ("--- # argsin=%d", argsInCount));
766 /* Get a handle to the list of system resources. */
767 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
768 _SBLIM_TRACE(1,("--- _getResources() failed"));
769 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
770 rc = 2;
771 goto Exit;
772 }
774 /* Start/stop the service. */
775 if (strcmp(methodname, "StartService") == 0) {
776 if (!Xen_VirtualSystemManagementService_enableResource(session, resources, NULL)) {
777 _SBLIM_TRACE(1,("--- StartService() failed"));
778 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Method operation failed");
779 rc = 2;
780 goto Exit;
781 }
782 }
783 else if (strcmp(methodname, "StopService") == 0) {
784 if (!Xen_VirtualSystemManagementService_disableResource(session, resources, NULL)) {
785 _SBLIM_TRACE(1,("--- StopService() failed"));
786 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Method operation failed");
787 rc = 2;
788 goto Exit;
789 }
790 }
791 else if (strcmp(methodname, "DefineSystem") == 0) {
792 if (argsInCount != 3) {
793 _SBLIM_TRACE(1,("--- Incorrect number of method arguments"));
794 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Incorrect number of method arguments");
795 rc = 5;
796 goto Exit;
797 }
799 /* Get embedded instance of VirtualSystemSettingData. */
800 argdata = CMGetArgAt(argsin, 0, NULL, &status);
801 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(argdata)) {
802 _SBLIM_TRACE(1,("--- CMGetArgAt(0) failed"));
803 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Cannot get method input argument");
804 rc = 5;
805 goto Exit;
806 }
808 CMPIInstance* vsSettingDataInst;
809 if (argdata.type == CMPI_string)
810 {
811 char *vsSettingData = CMGetCharPtr(argdata.value.string);
812 _SBLIM_TRACE(2,("--- Got string with embedded instance =\"%s\"", vsSettingData));
814 vsSettingDataInst = parse_embedded_instance(vsSettingData);
815 if (vsSettingDataInst == NULL) { /* parser returns zero for success, non-zero for error */
816 _SBLIM_TRACE(1,("--- Error parsing VirtualSystemSettingData: \"%s\"",vsSettingData));
817 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Cannot parse VirtualSystemSettingData");
818 rc = 5;
819 goto Exit;
820 }
821 }
822 else if (argdata.type == CMPI_instance)
823 {
824 vsSettingDataInst = argdata.value.inst;
825 _SBLIM_TRACE(2,("--- Got embedded instance for vsSettingData"));
826 }
827 else
828 {
829 _SBLIM_TRACE(1,("--- Invalid setting type - %s", _CMPITypeName(argdata.type)));
830 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid setting type");
831 rc = 5;
832 goto Exit;
833 }
835 /* Get input array of ResourceAllocationSettingData instances. */
836 argdata = CMGetArgAt(argsin, 1, NULL, &status);
837 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(argdata)) {
838 _SBLIM_TRACE(1,("--- CMGetArgAt(1) failed"));
839 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Cannot get method input argument");
840 rc = 5;
841 goto Exit;
842 }
844 /* Check that the input arg is an array. */
845 if (!CMIsArray(argdata) || CMIsNullObject(argdata.value.array)) {
846 _SBLIM_TRACE(1,("--- Invalid method argument type. Array expected"));
847 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid method argument type. Array expected");
848 rc = 5;
849 goto Exit;
850 }
852 CMPIArray *resourceSettings = argdata.value.array;
853 xen_vm vm;
854 if (!create_vm(vsSettingDataInst, resourceSettings, &vm, &status))
855 {
856 _SBLIM_TRACE(1,("--- create_vm() failed"));
857 /* status set in create_vm */
858 rc = 3;
859 goto Exit;
860 }
862 /* Return the objectpath for the resulting DomU in the output args */
863 CMPIObjectPath * op = CMNewObjectPath(_BROKER, namespace, "Xen_ComputerSystem", NULL);
864 CMAddKey(op, "CreationClassName", (CMPIValue *)"Xen_ComputerSystem", CMPI_chars);
865 char *vm_name;
866 if (!xen_vm_get_name_label(session->xen, &vm_name, vm)) {
867 /* Can't get the name after all of this work! Unwind this mess. */
868 remove_vm(vm);
869 xen_vm_free(vm);
870 _SBLIM_TRACE(1,("--- Unable to retrieve name of new VM"));
871 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to create the virtual system");
872 rc = 3;
873 goto Exit;
874 }
876 CMAddKey(op, "Name", (CMPIValue *)vm_name, CMPI_chars);
877 /*
878 * WARNING: J. Fehlig
879 * What does this do?
880 *
881 * CMSetHostAndNameSpaceFromObjectPath(op, reference);
882 *
883 */
884 _SBLIM_TRACE(2,("--- VirtualSystem=%s", CMGetCharPtr(CDToString(_BROKER, op, NULL))));
885 CMAddArg(argsout, "ResultingSystem", (CMPIValue*)&op, CMPI_ref);
886 xen_vm_free(vm);
888 _SBLIM_TRACE(2, ("--- DefineSystem completed with success"));
889 }
890 else if (strcmp(methodname, "AddResourceSettings") == 0) {
891 /* TODO */
892 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_METHOD_NOT_AVAILABLE, NULL);
893 rc = 1;
894 goto Exit;
895 }
896 else if (strcmp(methodname, "AddResourceSetting") == 0) {
897 if (argsInCount != 2) {
898 _SBLIM_TRACE(1,("--- Incorrect number of method arguments"));
899 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Incorrect number of method arguments");
900 rc = 5;
901 goto Exit;
902 }
904 /* Get vm record based on VirtualSystemSettingData object path. */
905 argdata = CMGetArgAt(argsin, 0, NULL, &status);
906 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(argdata)) {
907 _SBLIM_TRACE(1,("--- CMGetArgAt(0) failed"));
908 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Cannot get method input argument");
909 rc = 5;
910 goto Exit;
911 }
913 if (argdata.type != CMPI_ref) {
914 _SBLIM_TRACE(1,("--- Invalid setting type - %s", _CMPITypeName(argdata.type)));
915 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid setting type");
916 rc = 5;
917 goto Exit;
918 }
920 _SBLIM_TRACE(2, ("--- domain object path=\"%s\"", CMGetCharPtr(CDToString(_BROKER, argdata.value.ref, NULL))));
922 xen_vm_record *vmRec;
923 if (!xen_utils_get_domain_from_sd_OP(session, &vmRec, argdata.value.ref)) {
924 _SBLIM_TRACE(1,("--- Unable to retrieve domain record from object path"));
925 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Unable to retrieve domain record from object path");
926 rc = 5;
927 goto Exit;
928 }
930 /*
931 * Get input ResourceAllocationSettingData instance
932 * that will be added to domain.
933 */
934 argdata = CMGetArgAt(argsin, 1, NULL, &status);
935 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(argdata)) {
936 _SBLIM_TRACE(1,("--- CMGetArgAt(1) failed"));
937 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Cannot get method input argument");
938 xen_vm_record_free(vmRec);
939 rc = 5;
940 goto Exit;
941 }
943 CMPIInstance* newSettingInst;
944 if (argdata.type == CMPI_string){
945 char* newSetting = CMGetCharPtr(argdata.value.string);
946 _SBLIM_TRACE(2,("--- newSetting=\"%s\"", newSetting));
947 newSettingInst = parse_embedded_instance(newSetting);
948 if (newSettingInst == NULL) { /* parser returns zero for success, non-zero for error */
949 _SBLIM_TRACE(1,("--- Error parsing ResourceSettingData: \"%s\"",newSetting));
950 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Cannot parse ResourceSettingData");
951 rc = 5;
952 goto Exit;
953 }
954 }
955 else if (argdata.type == CMPI_instance) {
956 newSettingInst = argdata.value.inst;
957 }
958 else {
959 _SBLIM_TRACE(1,("--- Invalid setting type - %s", _CMPITypeName(argdata.type)));
960 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid setting type");
961 rc = 5;
962 goto Exit;
963 }
966 CMPIObjectPath *resultSetting;
967 if (!add_resource_to_vm(vmRec, newSettingInst, &resultSetting, namespace, &status))
968 {
969 _SBLIM_TRACE(1,("--- add_resource_to_vm() failed"));
970 /* status set in add_resource_to_vm */
971 xen_vm_record_free(vmRec);
972 rc = 3;
973 goto Exit;
974 }
976 CMAddArg(argsout, "ResultingResourceSetting",
977 (CMPIValue*)&resultSetting, CMPI_ref);
978 xen_vm_record_free(vmRec);
980 _SBLIM_TRACE(2, ("--- AddResourceSetting completed with success"));
981 }
982 else if (strcmp(methodname, "ModifyResourceSettings") == 0) {
983 /* TODO */
984 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
985 rc = 1;
986 goto Exit;
987 }
988 else if (strcmp(methodname, "RemoveResourceSettings") == 0) {
989 /* TODO */
990 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
991 rc = 1;
992 goto Exit;
993 }
994 else if (strcmp(methodname, "ModifySystemSettings") == 0) {
995 /* TODO */
996 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
997 rc = 1;
998 goto Exit;
999 }
1000 else if (strcmp(methodname, "DestroySystem") == 0) {
1001 /* Check that the method has the correct number of arguments. */
1002 if (argsInCount != 1) {
1003 _SBLIM_TRACE(1,("--- Incorrect number of method arguments"));
1004 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Incorrect number of method arguments");
1005 rc = 5;
1006 goto Exit;
1009 /* Get object path of domain */
1010 argdata = CMGetArgAt(argsin, 0, NULL, &status);
1011 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(argdata)) {
1012 _SBLIM_TRACE(1,("--- CMGetArgAt(0) failed"));
1013 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Cannot get method input argument");
1014 rc = 5;
1015 goto Exit;
1018 if ((argdata.type != CMPI_ref) || CMIsNullObject(argdata.value.ref)) {
1019 _SBLIM_TRACE(1,("--- Invalid parameter type - %s", _CMPITypeName(argdata.type)));
1020 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid method input argument");
1021 rc = 5;
1022 goto Exit;
1025 CMPIData namedata = CMGetKey(argdata.value.ref, "Name", &status);
1026 char *domainname = CMGetCharPtr(namedata.value.string);
1027 _SBLIM_TRACE(1,("--- domainname = %s", domainname));
1028 xen_vm_set *vms;
1029 if (!xen_vm_get_by_name_label(session->xen, &vms, domainname)) {
1030 _SBLIM_TRACE(1,("--- Target instance not found"));
1031 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_FOUND, "Target instance not found");
1032 rc = 3;
1033 goto Exit;
1035 assert(vms->size == 1);
1036 xen_vm vm = vms->contents[0];
1037 enum xen_vm_power_state power_state;
1038 if (!xen_vm_get_power_state(session->xen, &power_state, vm)) {
1039 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1040 _SBLIM_TRACE(1,("--- Unable to retrieve power state of vm - error = %s", error_msg));
1041 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Unable to determine power state of virtual system");
1042 rc = 3;
1043 goto Exit;
1046 _SBLIM_TRACE(1,("--- retrieved power state of vm - %", power_state));
1048 if (power_state != XEN_VM_POWER_STATE_HALTED) {
1049 _SBLIM_TRACE(1,("--- calling xen_vm_clean_shutdown"));
1050 if (!xen_vm_clean_shutdown(session->xen, vm)) {
1051 _SBLIM_TRACE(1, ("--- xen_vm_clean_shutdown() failed"));
1052 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to shutdown VirtualSystem");
1053 rc = 3;
1054 goto Exit;
1058 _SBLIM_TRACE(1,("--- calling xen_vm_destroy"));
1059 if (!xen_vm_destroy(session->xen, vm)) {
1060 _SBLIM_TRACE(1,("--- xen_vm_destroy failed"));
1061 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to destroy the virtual system");
1062 rc = 3;
1063 goto Exit;
1067 else {
1068 _SBLIM_TRACE(1,("--- Method \"%s\" is not supported", methodname));
1069 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_METHOD_NOT_AVAILABLE, NULL);
1070 rc = 5;
1071 goto Exit;
1074 Exit:
1076 CMReturnData(results, (CMPIValue *)&rc, CMPI_uint32);
1077 CMReturnDone(results);
1078 _SBLIM_RETURNSTATUS(status);
1082 // ----------------------------------------------------------------------------
1083 // MethodInitialize()
1084 // Perform any necessary initialization immediately after the method provider
1085 // is first loaded.
1086 // ----------------------------------------------------------------------------
1087 static void MethodInitialize(
1088 CMPIMethodMI * self, /* [in] Handle to this provider (i.e. 'self'). */
1089 const CMPIContext * context) /* [in] Additional context info, if any. */
1091 _SBLIM_ENTER("MethodInitialize");
1092 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
1093 // _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
1095 /* Initialized Xen session object. */
1096 if (session == NULL)
1097 xen_utils_xen_init(&session);
1099 _SBLIM_RETURN();
1102 // ============================================================================
1103 // CMPI METHOD PROVIDER FUNCTION TABLE SETUP
1104 // ============================================================================
1105 CMMethodMIStub( , Xen_VirtualSystemManagementService, _BROKER, MethodInitialize(&mi, ctx));
1108 /*
1109 * Wild macro to support adding devices (vbd, vif) to as list of such
1110 * devices. To be used only in create_vm where list of devices is
1111 * collected from the incoming RASDs.
1112 */
1113 #define ADD_DEVICE_TO_LIST(list, device, type) \
1114 { \
1115 if (list == NULL) { \
1116 list = type ## _set_alloc(1); \
1117 if (list == NULL) { \
1118 _SBLIM_TRACE(1, \
1119 ("Cannot malloc memory for xen device settings list")); \
1120 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, \
1121 "Unable to malloc memory"); \
1122 type ## _free(device); \
1123 goto Error; \
1124 } \
1125 list->size = 1; \
1126 list->contents[0] = device; \
1127 } \
1128 /* List is not empty. Grow the list and add the new device */ \
1129 else { \
1130 int new_len = sizeof(type ## _set) + ((list->size + 1) * sizeof(type)); \
1131 list = realloc(list, new_len); \
1132 if (list == NULL) { \
1133 _SBLIM_TRACE(1, \
1134 ("Cannot malloc memory for xen device settings list")); \
1135 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, \
1136 "Unable to malloc memory"); \
1137 type ## _free(device); \
1138 goto Error; \
1139 } \
1140 list->contents[list->size] = device; \
1141 list->size++; \
1142 } \
1146 static int create_vm(CMPIInstance* vsSettingsInst,
1147 CMPIArray *resourceSettings,
1148 xen_vm *result, CMPIStatus *status)
1150 CMPIObjectPath *objectpath;
1151 char *settingclassname;
1152 xen_vm_record *vm_rec;
1153 xen_vdi_record_set *vdis = NULL;
1154 xen_vbd_record_set *vbds = NULL;
1155 xen_vif_record_set *vifs = NULL;
1156 xen_console_record *con_rec = NULL;
1157 xen_vm vm = NULL;
1158 xen_sr_set *srs = NULL;
1159 int i;
1160 int ccode;
1161 char error_msg[XEN_UTILS_ERROR_BUF_LEN];
1163 vm_rec = xen_vm_record_alloc();
1164 if (vm_rec == NULL) {
1165 _SBLIM_TRACE(1,("--- Cannot malloc memory for vm record"));
1166 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, "Unable to malloc memory");
1167 return 0;
1170 objectpath = CMGetObjectPath(vsSettingsInst, NULL);
1171 _SBLIM_TRACE(2,("--- objectpath=%s", CMGetCharPtr(CDToString(_BROKER, objectpath, NULL))));
1173 /* Get the class type of the setting data instance */
1174 settingclassname = CMGetCharPtr(CMGetClassName(objectpath, NULL));
1175 _SBLIM_TRACE(2,("--- settingclassname=%s", settingclassname));
1177 /* Ensure we have a Xen_ComputerSystemSettingData */
1178 if (strcmp(settingclassname,"Xen_ComputerSystemSettingData") != 0) {
1179 _SBLIM_TRACE(1,("--- Unrecognized setting data class - %s", settingclassname));
1180 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Unrecognized setting data class");
1181 return 0;
1184 /* Convert the domain settings to respective xen settings. */
1185 if (!vssd2xenconfig(vsSettingsInst, vm_rec, status)) {
1186 _SBLIM_TRACE(1,("--- failed to convert VSSD to xen_vmrecord"));
1187 xen_vm_record_free(vm_rec);
1188 return 0;
1191 /* Convert resource settings to their respecitve xen settings. */
1192 CMPIInstance *instance;
1193 char *setting;
1194 for (i = 0; i < CMGetArrayCount(resourceSettings, NULL); i++) {
1195 CMPIData settingdata = CMGetArrayElementAt(resourceSettings, i, status);
1196 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(settingdata)) {
1197 _SBLIM_TRACE(1,("--- CMGetArrayElementAt(%d) failed", i));
1198 goto Error;
1201 /* Check that the array element is an (embedded) instance */
1202 if (settingdata.type == CMPI_string) {
1203 setting = CMGetCharPtr(settingdata.value.string);
1204 _SBLIM_TRACE(2,("--- Got string with embedded instance = %s", setting));
1206 instance = parse_embedded_instance(setting);
1207 if (instance == NULL) {
1208 _SBLIM_TRACE(1,("--- unable to parse setting data %s", setting));
1209 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Unable to parse resource setting data parameter");
1210 goto Error;
1213 else if (settingdata.type == CMPI_instance) {
1214 instance = settingdata.value.inst;
1215 _SBLIM_TRACE(2,("--- Got embedded instance for setting"));
1217 else {
1218 _SBLIM_TRACE(1,("--- Invalid setting type - %s", _CMPITypeName(settingdata.type)));
1219 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid resource setting data parameter");
1220 goto Error;
1223 _SBLIM_TRACE(2,("--- instance=%s", CMGetCharPtr(CDToString(_BROKER, instance, NULL))));
1224 objectpath = CMGetObjectPath(instance, NULL);
1225 _SBLIM_TRACE(2,("--- objectpath=%s", CMGetCharPtr(CDToString(_BROKER, objectpath, NULL))));
1227 /* Get the class type of the setting data instance */
1228 settingclassname = CMGetCharPtr(CMGetClassName(objectpath, NULL));
1229 _SBLIM_TRACE(2,("--- settingclassname=%s", settingclassname));
1231 /* Populate config with instance data from the virtual device. */
1232 if (strcmp(settingclassname,"Xen_ProcessorSettingData") == 0) {
1233 _SBLIM_TRACE(2,("--- adding Xen_ProcessorSettingData to configuration"));
1235 if (!proc_rasd2vmconfig(instance, vm_rec, status)) {
1236 _SBLIM_TRACE(1,("--- Error parsing processor settings"));
1237 goto Error;
1240 else if (strcmp(settingclassname,"Xen_MemorySettingData") == 0) {
1241 _SBLIM_TRACE(2,("--- adding Xen_MemorySettingData to configuration"));
1243 /* Fail if duplicate memory rasd's? */
1244 if (vm_rec->memory_dynamic_max > 0) {
1245 _SBLIM_TRACE(1,("--- More than one memroy setting data specified"));
1246 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER,
1247 "More than one memory setting data specified");
1248 goto Error;
1251 if (!mem_rasd2vmconfig(instance, vm_rec, status)) {
1252 _SBLIM_TRACE(1,("--- Error parsing memory settings"));
1253 goto Error;
1256 else if (strcmp(settingclassname,"Xen_DiskSettingData") == 0) {
1257 _SBLIM_TRACE(2,("--- adding Xen_DiskSettingData to configuration"));
1259 xen_vbd_record *vbd_rec;
1260 xen_vdi_record *vdi_rec;
1261 if (!disk_rasd2vmconfig(instance, &vbd_rec, &vdi_rec, status)) {
1262 _SBLIM_TRACE(1,("--- Error parsing disk settings"));
1263 goto Error;
1266 ADD_DEVICE_TO_LIST(vdis, vdi_rec, xen_vdi_record);
1267 ADD_DEVICE_TO_LIST(vbds, vbd_rec, xen_vbd_record);
1269 else if (strcmp(settingclassname,"Xen_NetworkPortSettingData") == 0) {
1270 _SBLIM_TRACE(2,("--- adding Xen_NetworkPortSettingData to configuration"));
1272 xen_vif_record *vif_rec;
1273 if (!nic_rasd2vmconfig(instance, &vif_rec, status)) {
1274 _SBLIM_TRACE(1,("--- Error parsing network port settings"));
1275 goto Error;
1278 ADD_DEVICE_TO_LIST(vifs, vif_rec, xen_vif_record);
1280 else if (strcmp(settingclassname,"Xen_ConsoleSettingData") == 0) {
1281 _SBLIM_TRACE(2,("--- adding Xen_ConsoleSettingData to configuration"));
1283 con_rec = xen_console_record_alloc();
1284 if (con_rec == NULL) {
1285 _SBLIM_TRACE(1,("--- Cannot malloc memory for console record"));
1286 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, "Unable to malloc memory");
1287 goto Error;
1290 if (!con_rasd2vmconfig(instance, con_rec, status)) {
1291 _SBLIM_TRACE(1,("--- Error parsing console settings"));
1292 goto Error;
1295 else {
1296 _SBLIM_TRACE(1,("--- Unrecognized setting data class - %s", settingclassname));
1297 goto Error;
1301 /* We have all of the settings. First create the vm. */
1302 if (!xen_vm_create(session->xen, &vm, vm_rec)) {
1303 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1304 _SBLIM_TRACE(1,("--- xen_vm_create failed: %s", error_msg));
1305 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1306 goto Error;
1309 /* Create devices. Need a Storage Repository (sr) for VDI. */
1310 xen_vm_record_opt vm_record_opt =
1312 .u.handle = vm
1313 };
1315 if (!xen_sr_get_by_name_label(session->xen, &srs, "Local")) {
1316 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1317 _SBLIM_TRACE(1,("--- xen_sr_get_by_name_label failed: %s", error_msg));
1318 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1319 goto Error;
1321 assert(srs->size == 1);
1323 xen_sr_record_opt sr_record =
1325 .u.handle = srs->contents[0]
1326 };
1328 /* Add all of the virutal block devices. */
1329 for (i = 0; i < vdis->size; i++) {
1331 xen_vdi_record *vdi_rec = vdis->contents[i];
1332 vdi_rec->sr = &sr_record;
1333 xen_vdi new_vdi;
1334 ccode = xen_vdi_create(session->xen, &new_vdi, vdi_rec);
1335 /* Set sr field of vdi record to NULL so it is not freed */
1336 vdi_rec->sr = NULL;
1337 if (!ccode)
1339 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1340 _SBLIM_TRACE(1,("--- xen_vdi_create failed: %s", error_msg));
1341 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1342 goto Error;
1345 xen_vdi_record_opt vdi_record_opt =
1347 .u.handle = new_vdi
1348 };
1350 xen_vbd_record *vbd_rec = vbds->contents[i];
1351 vbd_rec->vm = &vm_record_opt;
1352 vbd_rec->vdi = &vdi_record_opt;
1353 xen_vbd new_vbd;
1354 ccode = xen_vbd_create(session->xen, &new_vbd, vbd_rec);
1355 /* Set vm and vdi fields of vbd record to NULL so they are not freed */
1356 vbd_rec->vm = NULL;
1357 vbd_rec->vdi = NULL;
1358 xen_vdi_free(new_vdi);
1359 if (!ccode)
1361 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1362 _SBLIM_TRACE(1,("--- xen_vdb_create failed: %s", error_msg));
1363 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1364 goto Error;
1366 xen_vbd_free(new_vbd);
1369 /* Add all of the virutal network devices. */
1370 for (i = 0; i < vifs->size; i++) {
1371 xen_vif_record *vif_rec = vifs->contents[i];
1372 vif_rec->vm = &vm_record_opt;
1373 xen_vif new_vif;
1374 ccode = xen_vif_create(session->xen, &new_vif, vif_rec);
1375 /* Set vm field of vif record to NULL so it is not freed */
1376 vif_rec->vm = NULL;
1377 if (!ccode)
1379 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1380 _SBLIM_TRACE(1,("--- xen_vif_create failed %s", error_msg));
1381 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1382 goto Error;
1384 xen_vif_free(new_vif);
1387 /* Add console device if specified */
1388 if (con_rec) {
1389 con_rec->vm = &vm_record_opt;
1390 xen_console new_con;
1391 ccode = xen_console_create(session->xen, &new_con, con_rec);
1392 /* Set vm field of console record to NULL so it is not freed */
1393 con_rec->vm = NULL;
1394 if (!ccode)
1396 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1397 _SBLIM_TRACE(1,("--- xen_console_create failed %s", error_msg));
1398 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1399 goto Error;
1401 xen_console_free(new_con);
1404 xen_console_record_free(con_rec);
1405 xen_vif_record_set_free(vifs);
1406 xen_vbd_record_set_free(vbds);
1407 xen_vdi_record_set_free(vdis);
1408 xen_sr_set_free(srs);
1409 xen_vm_record_free(vm_rec);
1411 *result = vm;
1413 return 1;
1415 /* Unwind if we failed.
1416 * remove_vm will nuke any vifs / vbds created and the vm.
1417 */
1418 Error:
1419 if (vm) {
1420 remove_vm(vm);
1421 xen_vm_free(vm);
1423 xen_console_record_free(con_rec);
1424 xen_vif_record_set_free(vifs);
1425 xen_vbd_record_set_free(vbds);
1426 xen_vdi_record_set_free(vdis);
1427 xen_sr_set_free(srs);
1428 xen_vm_record_free(vm_rec);
1429 *result = NULL;
1431 return 0;
1435 static int add_resource_to_vm(xen_vm_record *vmRec,
1436 CMPIInstance *sourceSettingInst,
1437 CMPIObjectPath **resultSetting,
1438 char *namespace,
1439 CMPIStatus *status)
1441 CMPIObjectPath *op;
1442 char *settingclassname;
1443 CMPIData propertyvalue;
1444 int ccode;
1445 char instId[MAX_INSTANCEID_LEN];
1446 char error_msg[XEN_UTILS_ERROR_BUF_LEN];
1447 xen_vm_record_opt vm_record_opt =
1449 .u.handle = vmRec->handle
1451 };
1453 _SBLIM_TRACE(2,("--- instance=%s", CMGetCharPtr(CDToString(_BROKER, sourceSettingInst, NULL))));
1454 op = CMGetObjectPath(sourceSettingInst, NULL);
1455 _SBLIM_TRACE(2,("--- objectpath=%s", CMGetCharPtr(CDToString(_BROKER, op, NULL))));
1457 /* Get the class type of the setting data instance */
1458 settingclassname = CMGetCharPtr(CMGetClassName(op, NULL));
1459 _SBLIM_TRACE(2,("--- settingclassname=%s", settingclassname));
1461 /* Add the resource to the domain. */
1462 if (strcmp(settingclassname,"Xen_ProcessorSettingData") == 0) {
1463 #if 1
1464 _SBLIM_TRACE(1,("--- Adding processors not currently supported"));
1465 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, "Adding processors not currently supported");
1466 return 0;
1468 #else
1469 /* Code for adding processors -- when Xen API supports it. */
1470 _SBLIM_TRACE(2,("--- adding Xen_ProcessorSettingData to configuration"));
1471 propertyvalue = CMGetProperty(sourceSettingInst, "VirtualQuantity", status);
1472 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1473 _SBLIM_TRACE(1,("--- Unable to determine VirtualQuantity in Processor RASD"));
1474 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "VirtualQuantity not specified in ResourceSetting");
1475 return 0;
1478 /* Adding cpus means just that - adding to existing amount */
1479 uint64_t cpus = propertyvalue.value.uint64 + vmRec->vcpus_number;
1480 /*
1481 * TODO:
1482 * 1. CPU weights and other scheduling parameters?
1483 */
1484 if (!xen_vm_set_vcpus_number(session->xen,
1485 vmRec->handle,
1486 cpus)) {
1487 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1488 _SBLIM_TRACE(1,("--- xen_vm_set_vcpus_number failed: %s", error_msg));
1489 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1490 return 0;
1493 /* Create new object path for setting */
1494 *resultSetting = CMNewObjectPath(_BROKER, namespace, "Xen_ProcessorSettingData", NULL);
1495 snprintf(instId, MAX_INSTANCEID_LEN, "Xen:%s:Processor", vmRec->name_label);
1496 CMAddKey(*resultSetting, "InstanceID", (CMPIValue *)instId, CMPI_chars);
1497 #endif
1499 else if (strcmp(settingclassname,"Xen_MemorySettingData") == 0) {
1500 #if 1
1501 _SBLIM_TRACE(1,("--- Adding memory not currently supported"));
1502 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, "Adding memory not currently supported");
1503 return 0;
1505 #else
1506 /* Code for adding memory -- when Xen API supports it. */
1507 _SBLIM_TRACE(2,("--- adding Xen_MemorySettingData to configuration"));
1508 propertyvalue = CMGetProperty(sourceSettingInst, "VirtualQuantity", status);
1509 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1510 _SBLIM_TRACE(1,("--- Unable to determine VirtualQuantity in Memory RASD"));
1511 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "VirtualQuantity not specified in ResourceSetting");
1512 return 0;
1515 /* Adding memory means just that - adding to existing amount */
1516 uint64_t mem = propertyvalue.value.uint64 + vmRec->memory_dynamic_max;
1517 if (!xen_vm_set_memory_dynamic_max(session->xen,
1518 vmRec->handle,
1519 mem)) {
1520 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1521 _SBLIM_TRACE(1,("--- xen_vm_set_memory_dynamic_max failed: %s", error_msg));
1522 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1523 return 0;
1526 /* Add object path to output array */
1527 *resultSetting = CMNewObjectPath(_BROKER, namespace, "Xen_MemorySettingData", NULL);
1528 snprintf(instId, MAX_INSTANCEID_LEN, "Xen:%s:Memory1", vmRec->name_label);
1529 CMAddKey(*resultSetting, "InstanceID", (CMPIValue *)instId, CMPI_chars);
1530 #endif
1532 else if (strcmp(settingclassname,"Xen_DiskSettingData") == 0) {
1533 _SBLIM_TRACE(2,("--- adding Xen_DiskSettingData to configuration"));
1535 xen_vbd_record *vbd_rec;
1536 xen_vdi_record *vdi_rec;
1537 if (!disk_rasd2vmconfig(sourceSettingInst, &vbd_rec, &vdi_rec, status)) {
1538 /* status set in disk_rasd2vmconfig */
1539 _SBLIM_TRACE(1,("--- Error parsing disk settings"));
1540 return 0;
1543 xen_sr_set *srs;
1544 if (!xen_sr_get_by_name_label(session->xen, &srs, "Local")) {
1545 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1546 _SBLIM_TRACE(1,("--- xen_sr_get_by_name_label failed: %s", error_msg));
1547 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1548 xen_vbd_record_free(vbd_rec);
1549 xen_vdi_record_free(vdi_rec);
1550 return 0;
1552 assert(srs->size == 1);
1554 xen_sr_record_opt sr_record =
1556 .u.handle = srs->contents[0]
1557 };
1559 vdi_rec->sr = &sr_record;
1560 xen_vdi new_vdi;
1561 ccode = xen_vdi_create(session->xen, &new_vdi, vdi_rec);
1562 /* Set sr field of vdi record to NULL so it is not freed */
1563 vdi_rec->sr = NULL;
1564 if (!ccode)
1566 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1567 _SBLIM_TRACE(1,("--- xen_vdi_create failed: %s", error_msg));
1568 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1569 xen_sr_set_free(srs);
1570 xen_vbd_record_free(vbd_rec);
1571 xen_vdi_record_free(vdi_rec);
1572 return 0;
1575 xen_vdi_record_opt vdi_record_opt =
1577 .u.handle = new_vdi
1578 };
1580 vbd_rec->vm = &vm_record_opt;
1581 vbd_rec->vdi = &vdi_record_opt;
1582 xen_vbd new_vbd;
1583 ccode = xen_vbd_create(session->xen, &new_vbd, vbd_rec);
1584 /* Set vm and vdi fields of vbd record to NULL so they are not freed */
1585 vbd_rec->vm = NULL;
1586 vbd_rec->vdi = NULL;
1587 /* Free allocated objects. */
1588 xen_vdi_free(new_vdi);
1589 xen_sr_set_free(srs);
1590 xen_vbd_record_free(vbd_rec);
1591 xen_vdi_record_free(vdi_rec);
1592 if (!ccode)
1594 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1595 _SBLIM_TRACE(1,("--- xen_vdb_create failed: %s", error_msg));
1596 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1597 return 0;
1599 xen_vbd_free(new_vbd);
1601 /* Add object path to output array */
1602 *resultSetting = CMNewObjectPath(_BROKER, namespace, "Xen_DiskSettingData", NULL);
1603 snprintf(instId, MAX_INSTANCEID_LEN, "Xen:%s:Disk", vmRec->name_label);
1604 CMAddKey(*resultSetting, "InstanceID", (CMPIValue *)instId, CMPI_chars);
1606 else if (strcmp(settingclassname,"Xen_NetworkPortSettingData") == 0) {
1607 _SBLIM_TRACE(2,("--- adding Xen_NetworkPortSettingData to configuration"));
1608 xen_vif_record *vif_rec;
1609 if (!nic_rasd2vmconfig(sourceSettingInst, &vif_rec, status)) {
1610 /* status set in nic_rasd2vmconfig */
1611 _SBLIM_TRACE(1,("--- Error parsing network port settings"));
1612 return 0;
1615 vif_rec->vm = &vm_record_opt;
1616 xen_vif new_vif;
1617 ccode = xen_vif_create(session->xen, &new_vif, vif_rec);
1618 /* Set vm field of vif record to NULL so it is not freed */
1619 vif_rec->vm = NULL;
1620 xen_vif_record_free(vif_rec);
1621 if (!ccode)
1623 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1624 _SBLIM_TRACE(1,("--- xen_vif_create failed %s", error_msg));
1625 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1626 return 0;
1628 xen_vif_free(new_vif);
1630 /* Add object path to output array */
1631 *resultSetting = CMNewObjectPath(_BROKER, namespace, "Xen_NetworkPortSettingData", NULL);
1632 snprintf(instId, MAX_INSTANCEID_LEN, "Xen:%s:Vif", vmRec->name_label);
1633 CMAddKey(*resultSetting, "InstanceID", (CMPIValue *)instId, CMPI_chars);
1635 else {
1636 _SBLIM_TRACE(1,("--- Unrecognized setting data class - %s", settingclassname));
1637 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Unrecognized ResourceSetting");
1638 return 0;
1641 return 1;
1645 static void remove_vm(xen_vm vm)
1647 /*
1648 * TODO:
1649 * We want to destroy the VM, so reset any previous failures on the
1650 * session object. But do we want to be touching internal fields in
1651 * this manner? Revisit once we move to the new resource abstraction
1652 * interface.
1653 */
1654 session->xen->ok = true;
1655 xen_vm_destroy(session->xen, vm);
1659 static int vssd2xenconfig(CMPIInstance *vssd, xen_vm_record *vm_rec, CMPIStatus *status)
1661 CMPIData propertyvalue;
1662 char *vsType;
1663 const char *plat_val;
1665 /*
1666 * Get domain name.
1667 * WARNING!
1668 * Do we fail if VirtualSystemIdentifier is not specified?
1669 */
1670 propertyvalue = CMGetProperty(vssd, "VirtualSystemIdentifier", status);
1671 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1672 _SBLIM_TRACE(1,("--- failed to retrieve VirtualSystemIndentifier property value"));
1673 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Unable to retrieve VirtualSystemIdentifier (name) property from virtual system setting data");
1674 return 0;
1676 vm_rec->name_label = strdup(CMGetCharPtr(propertyvalue.value.string));
1678 /* Get UUID, if specified */
1679 propertyvalue = CMGetProperty(vssd, "UUID", status);
1680 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue))
1681 vm_rec->uuid = strdup(CMGetCharPtr(propertyvalue.value.string));
1683 propertyvalue = CMGetProperty(vssd, "localtime", status);
1684 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1685 if (propertyvalue.value.boolean)
1686 plat_val = "1";
1687 else
1688 plat_val = "0";
1690 if (!add_strings_to_map("localtime", plat_val, &(vm_rec->platform))) {
1691 _SBLIM_TRACE(1,
1692 ("Cannot malloc memory for xend platform settings list"));
1693 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM,
1694 "Unable to malloc memory");
1695 return 0;
1699 propertyvalue = CMGetProperty(vssd, "OnPoweroff", status);
1700 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue))
1701 vm_rec->actions_after_shutdown = propertyvalue.value.uint16;
1703 propertyvalue = CMGetProperty(vssd, "OnReboot", status);
1704 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue))
1705 vm_rec->actions_after_reboot = propertyvalue.value.uint16;
1707 propertyvalue = CMGetProperty(vssd, "OnCrash", status);
1708 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue))
1709 vm_rec->actions_after_crash = propertyvalue.value.uint16;
1711 /* Paravirtual or HVM domain? */
1712 propertyvalue = CMGetProperty(vssd, "VirtualSystemType", status);
1713 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1714 _SBLIM_TRACE(1,("--- VirtualSystemType not specified"));
1715 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER,
1716 "VirtualSystemType not specified");
1717 return 0;
1720 vsType = CMGetCharPtr(propertyvalue.value.string);
1721 if (vsType == NULL) {
1722 _SBLIM_TRACE(1,("--- VirtualSystemType not specified"));
1723 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER,
1724 "VirtualSystemType not specified");
1725 return 0;
1728 if (strstr(vsType, "xen")) {
1730 propertyvalue = CMGetProperty(vssd, "Bootloader", status);
1731 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1732 vm_rec->pv_bootloader = strdup(CMGetCharPtr(propertyvalue.value.string));
1734 /* If Bootloader specified, get options. */
1735 /* WARNING!
1736 * Should we ensure that BootloaderOptions is specified if
1737 * Bootloader is specified? For domUloader probably but
1738 * pygrub should work with no options.
1739 */
1740 propertyvalue = CMGetProperty(vssd, "BootloaderOptions", status);
1741 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1742 vm_rec->pv_bootloader_args = strdup(CMGetCharPtr(propertyvalue.value.string));
1746 /* Only honor Kernel if Bootloader not specified. */
1747 propertyvalue = CMGetProperty(vssd, "Kernel", status);
1748 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue) &&
1749 vm_rec->pv_bootloader == NULL) {
1750 vm_rec->pv_kernel = strdup(CMGetCharPtr(propertyvalue.value.string));
1752 /* If Kernel specified, get RAMDisk. */
1753 /* WARNING!
1754 * Should we ensure that RAMDisk is specified if
1755 * Kernel is specified? For Unix'ish guests we probably need
1756 * RAMDisk but what about OS's with no notion of RAMDisk?
1757 */
1758 propertyvalue = CMGetProperty(vssd, "RAMDisk", status);
1759 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1760 vm_rec->pv_ramdisk = strdup(CMGetCharPtr(propertyvalue.value.string));
1764 propertyvalue = CMGetProperty(vssd, "KernelOptions", status);
1765 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1766 vm_rec->pv_args = strdup(CMGetCharPtr(propertyvalue.value.string));
1769 else if (strstr(vsType, "hvm")) {
1770 propertyvalue = CMGetProperty(vssd, "BootOrder", status);
1771 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1772 _SBLIM_TRACE(1,("--- No BootOrder specified for HVM guest"));
1773 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER,
1774 "No BootOrder specified for HVM guest");
1775 return 0;
1777 //vm_rec->hvm_boot_params = strdup(CMGetCharPtr(propertyvalue.value.string));
1778 add_strings_to_map("order", CMGetCharPtr(propertyvalue.value.string),&(vm_rec->hvm_boot_params ));
1779 /* Default stdvga to 0 */
1780 plat_val = "0";
1781 propertyvalue = CMGetProperty(vssd, "stdvga", status);
1782 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1783 if (propertyvalue.value.boolean)
1784 plat_val = "1";
1785 else
1786 plat_val = "0";
1788 if (!add_strings_to_map("stdvga", plat_val, &(vm_rec->platform))) {
1789 _SBLIM_TRACE(1,
1790 ("Cannot malloc memory for xend platform settings list"));
1791 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM,
1792 "Unable to malloc memory");
1793 return 0;
1796 propertyvalue = CMGetProperty(vssd, "apic", status);
1797 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1798 if (propertyvalue.value.boolean)
1799 plat_val = "1";
1800 else
1801 plat_val = "0";
1803 if (!add_strings_to_map("apic", plat_val, &(vm_rec->platform))) {
1804 _SBLIM_TRACE(1,
1805 ("Cannot malloc memory for xend platform settings list"));
1806 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM,
1807 "Unable to malloc memory");
1808 return 0;
1812 propertyvalue = CMGetProperty(vssd, "acpi", status);
1813 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1814 if (propertyvalue.value.boolean)
1815 plat_val = "1";
1816 else
1817 plat_val = "0";
1819 if (!add_strings_to_map("acpi", plat_val, &(vm_rec->platform))) {
1820 _SBLIM_TRACE(1,
1821 ("Cannot malloc memory for xend platform settings list"));
1822 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM,
1823 "Unable to malloc memory");
1824 return 0;
1828 propertyvalue = CMGetProperty(vssd, "pae", status);
1829 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1830 if (propertyvalue.value.boolean)
1831 plat_val = "1";
1832 else
1833 plat_val = "0";
1835 if (!add_strings_to_map("pae", plat_val, &(vm_rec->platform))) {
1836 _SBLIM_TRACE(1,
1837 ("Cannot malloc memory for xend platform settings list"));
1838 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM,
1839 "Unable to malloc memory");
1840 return 0;
1844 propertyvalue = CMGetProperty(vssd, "usb", status);
1845 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1846 if (propertyvalue.value.boolean)
1847 plat_val = "1";
1848 else
1849 plat_val = "0";
1851 if (!add_strings_to_map("usb", plat_val, &(vm_rec->platform))) {
1852 _SBLIM_TRACE(1,
1853 ("Cannot malloc memory for xend platform settings list"));
1854 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM,
1855 "Unable to malloc memory");
1856 return 0;
1860 propertyvalue = CMGetProperty(vssd, "usbdevice", status);
1861 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1862 plat_val = CMGetCharPtr(propertyvalue.value.string);
1864 if (!add_strings_to_map("usbdevice", plat_val, &(vm_rec->platform))) {
1865 _SBLIM_TRACE(1,
1866 ("Cannot malloc memory for xend platform settings list"));
1867 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM,
1868 "Unable to malloc memory");
1869 return 0;
1873 else {
1874 _SBLIM_TRACE(1,("--- Invalid VirtualSystemType %s specified", vsType));
1875 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER,
1876 "Invalid VirtualSystemType specified");
1877 return 0;
1880 return 1;
1884 static int proc_rasd2vmconfig(CMPIInstance *proc_rasd, xen_vm_record *vm_rec,
1885 CMPIStatus *status)
1887 CMPIData propertyvalue;
1889 propertyvalue = CMGetProperty(proc_rasd, "VirtualQuantity", status);
1890 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1891 /*
1892 * TODO:
1893 * 1. Handle defaults if processors not specified.
1894 * 2. CPU weights and other scheduling parameters?
1895 * 3. Handle VM.vcpus_max properly. 0 is not good so
1896 * we'll set it to same value as number of vcpus set
1897 * in VirtualQuantity for now.
1898 */
1899 vm_rec->vcpus_at_startup = propertyvalue.value.uint64;
1900 vm_rec->vcpus_max = propertyvalue.value.uint64;
1901 return 1;
1904 return 0;
1908 static int mem_rasd2vmconfig(CMPIInstance *mem_rasd, xen_vm_record *vm_rec,
1909 CMPIStatus *status)
1911 CMPIData propertyvalue;
1913 propertyvalue = CMGetProperty(mem_rasd, "AllocationUnits", status);
1914 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1915 return 0;
1918 char *units = CMGetCharPtr(propertyvalue.value.string);
1920 propertyvalue = CMGetProperty(mem_rasd, "VirtualQuantity", status);
1921 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1922 return 0;
1925 uint64_t mem_size = propertyvalue.value.uint64;
1926 if (strstr(units, "Mega") || strstr(units, "M"))
1927 mem_size = mem_size * 1024 * 1024;
1929 /*
1930 * TODO:
1931 * 1. Handle defaults if memory is not specified.
1932 */
1933 vm_rec->memory_static_max = mem_size;
1934 vm_rec->memory_dynamic_max = mem_size;
1935 vm_rec->memory_dynamic_min = mem_size;
1937 return 1;
1941 static int disk_rasd2vmconfig(CMPIInstance *disk_rasd, xen_vbd_record **vbd_rec,
1942 xen_vdi_record **vdi_rec, CMPIStatus *status)
1944 CMPIData propertyvalue;
1945 char *tmp_str = NULL;
1946 xen_string_string_map *vdi_params = NULL;
1948 propertyvalue = CMGetProperty(disk_rasd, "DiskConfigInfo", status);
1949 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1950 *vbd_rec = xen_vbd_record_alloc();
1951 if (*vbd_rec == NULL) {
1952 _SBLIM_TRACE(1,("--- Cannot malloc memory for virtual block device"));
1953 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, "Unable to malloc memory");
1954 return 0;
1957 *vdi_rec = xen_vdi_record_alloc();
1958 if (*vdi_rec == NULL) {
1959 _SBLIM_TRACE(1,("--- Cannot malloc memory for virtual disk image"));
1960 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, "Unable to malloc memory");
1961 goto Error;
1964 /* Extract the image, dev and mode params from the DiskConfigInfo string */
1965 char *tok;
1966 char *next_tok;
1967 tmp_str = strdup(CMGetCharPtr(propertyvalue.value.string));
1968 tok = strtok_r(tmp_str, ",", &next_tok);
1969 if (tok == NULL) {
1970 _SBLIM_TRACE(1,("--- Malformed DiskConfigInfo property in disk setting data"));
1971 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid disk setting data");
1972 goto Error;
1975 vdi_params = xen_string_string_map_alloc(1);
1976 if (vdi_params == NULL) {
1977 _SBLIM_TRACE(1,("--- Cannot malloc memory for VDI options"));
1978 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, "Unable to malloc memory");
1979 goto Error;
1982 vdi_params->contents[0].key = strdup("location");
1983 vdi_params->contents[0].val = strdup(tok);
1984 (*vdi_rec)->other_config = vdi_params;
1986 /*
1987 * TODO:
1988 * Need to handle specifying some of the VDI fields. For now
1989 * we'll hardcode to something reasonable.
1990 */
1991 (*vdi_rec)->type = XEN_VDI_TYPE_SYSTEM;
1992 (*vdi_rec)->sharable = false;
1994 if ((tok = strtok_r(NULL, ",", &next_tok)) == NULL) {
1995 _SBLIM_TRACE(1,("--- Malformed DiskConfigInfo property in disk setting data"));
1996 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid disk setting data");
1997 goto Error;
1999 (*vbd_rec)->device = strdup(tok);
2001 if ((tok = strtok_r(NULL, ",", &next_tok)) == NULL) {
2002 _SBLIM_TRACE(1,("--- Malformed DiskConfigInfo property in disk setting data"));
2003 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid disk setting data");
2004 goto Error;
2006 if (strcmp(tok, "w") == 0) {
2007 (*vbd_rec)->mode = XEN_VBD_MODE_RW;
2008 (*vdi_rec)->read_only = false;
2010 else {
2011 (*vbd_rec)->mode = XEN_VBD_MODE_RO;
2012 (*vdi_rec)->read_only = true;
2015 /*
2016 * TODO:
2017 * 1. Handle specifying type. For now just say its a disk.
2018 */
2019 (*vbd_rec)->type = XEN_VBD_TYPE_DISK;
2021 free(tmp_str);
2022 _SBLIM_TRACE(2,("--- uname = %s", vdi_params->contents[0].val));
2023 _SBLIM_TRACE(2,("--- dev = %s", (*vbd_rec)->device));
2024 _SBLIM_TRACE(2,("--- mode = %s",
2025 (*vbd_rec)->mode == XEN_VBD_MODE_RW ? "RW" : "RO"));
2027 return 1;
2030 Error:
2031 free(tmp_str);
2032 /* frees fields as well */
2033 xen_vbd_record_free(*vbd_rec);
2034 *vbd_rec = NULL;
2035 xen_vdi_record_free(*vdi_rec);
2036 *vdi_rec = NULL;
2038 return 0;
2042 static int nic_rasd2vmconfig(CMPIInstance *nic_rasd, xen_vif_record **vif_rec,
2043 CMPIStatus *status)
2045 CMPIData propertyvalue;
2047 /* Set the domain config data from the Xen_NetworkPortSettingData. */
2048 propertyvalue = CMGetProperty(nic_rasd, "NICConfigInfo", status);
2049 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
2050 _SBLIM_TRACE(1,("--- No NICConfigInfo specified in network port setting data"));
2051 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "No NICConfigInfo specified in network port setting data");
2053 *vif_rec = NULL;
2054 return 0;
2057 *vif_rec = xen_vif_record_alloc();
2058 if (*vif_rec == NULL) {
2059 _SBLIM_TRACE(1,("--- Cannot malloc memory for virtual network device"));
2060 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, "Unable to malloc memory");
2061 return 0;
2064 /* Extract the type, mac and network params from the NicConfigInfo
2065 * string. Expected syntax is "key=value,key=value" */
2066 char * next_tok;
2067 char * string = strdup(CMGetCharPtr(propertyvalue.value.string));
2068 char *tok = strtok_r(string, ",", &next_tok);
2069 while (tok) {
2070 if (strncmp(tok, "mac", 3) == 0)
2071 (*vif_rec)->mac = strdup(tok + 4);
2072 /* else if (strncmp(tok, "model", 5) == 0)
2073 (*vif_rec)->model = strdup(tok + 6); Commented off to compile the code*/
2075 tok = strtok_r(NULL, ",", &next_tok);
2078 free(string);
2080 return 1;
2084 static int con_rasd2vmconfig(CMPIInstance *con_rasd, xen_console_record *con_rec,
2085 CMPIStatus *status)
2087 CMPIStatus local_status = {CMPI_RC_OK, NULL};
2088 CMPIData propertyvalue;
2090 propertyvalue = CMGetProperty(con_rasd, "Protocol", &local_status);
2091 if ((local_status.rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
2092 _SBLIM_TRACE(1,("--- No protocol field specified in console setting data"));
2093 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "No protocol specified in console setting data");
2094 return 0;
2097 switch (propertyvalue.value.uint16) {
2098 case 0:
2099 con_rec->protocol = XEN_CONSOLE_PROTOCOL_VT100;
2100 break;
2101 case 1:
2102 con_rec->protocol = XEN_CONSOLE_PROTOCOL_RFB;
2103 break;
2104 case 2:
2105 con_rec->protocol = XEN_CONSOLE_PROTOCOL_RDP;
2106 break;
2107 default:
2108 _SBLIM_TRACE(1,("--- Invalid protocol specified in console setting data"));
2109 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid protocol specified in console setting data");
2110 return 0;
2113 /*
2114 * Get any additional config from ConsoleConfigInfo.
2115 * Expected syntax is "key=value,key=value"
2116 */
2117 propertyvalue = CMGetProperty(con_rasd, "ConsoleConfigInfo", &local_status);
2118 if ((local_status.rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
2119 /* Count number of config items */
2120 int num_items = 0;
2121 char *next_tok;
2122 char *string = strdup(CMGetCharPtr(propertyvalue.value.string));
2123 char *tok = strtok_r(string, ",", &next_tok);
2124 while (tok) {
2125 num_items++;
2126 tok = strtok_r(NULL, ",", &next_tok);
2128 free(string);
2130 xen_string_string_map *con_params = xen_string_string_map_alloc(num_items);
2131 if (con_params == NULL) {
2132 _SBLIM_TRACE(1,("--- Cannot malloc memory for console options"));
2133 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, "Unable to malloc memory");
2134 return 0;
2137 /*
2138 * Go back through the options and populate the string map.
2139 */
2140 string = strdup(CMGetCharPtr(propertyvalue.value.string));
2141 tok = strtok_r(string, ",", &next_tok);
2142 /* If tok is NULL, then string contains only 1 key/value pair */
2143 if (tok == NULL)
2144 tok = string;
2145 int i = 0;
2146 while (tok) {
2147 char *val = strchr(tok, '=');
2148 if (val == NULL) {
2149 _SBLIM_TRACE(1,("--- Invalid console option specified in console setting data"));
2150 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid console option specified in console setting data");
2151 xen_string_string_map_free(con_params);
2152 free(string);
2153 return 0;
2155 *val = '\0';
2156 val++;
2157 con_params->contents[i].key = strdup(tok);
2158 con_params->contents[i].val = strdup(val);
2159 i++;
2160 tok = strtok_r(NULL, ",", &next_tok);
2163 con_rec->other_config = con_params;
2164 free(string);
2167 return 1;
2171 /*
2172 * Private function to support adding strings to a string map.
2173 */
2174 static int add_strings_to_map(const char *key, const char *val,
2175 xen_string_string_map **map)
2177 if (*map == NULL) {
2178 *map = xen_string_string_map_alloc(1);
2179 if (*map == NULL)
2180 return 0;
2182 (*map)->size = 1;
2183 (*map)->contents[0].key = strdup(key);
2184 (*map)->contents[0].val = strdup(val);
2185 return 1;
2188 /* Map is not empty. Grow the map and add the new strings */
2189 int new_len = sizeof(xen_string_string_map) +
2190 (((*map)->size + 1) * sizeof(xen_string_string_map_contents));
2191 *map = realloc(*map, new_len);
2192 if (*map == NULL)
2193 return 0;
2195 (*map)->contents[(*map)->size].key = strdup(key);
2196 (*map)->contents[(*map)->size].val = strdup(val);
2197 (*map)->size++;
2199 return 1;
2204 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2205 // THE FOLLOWING CODE IS REQUIRED UNTIL EMBEDDEDOBJECT SUPPORT IS WORKING!!!
2206 //
2207 static CMPIInstance *parse_embedded_instance(char *instanceStr)
2209 char filename[L_tmpnam];
2210 FILE *fd;
2211 int rc;
2212 CMPIInstance *instance;
2214 _SBLIM_TRACE(2,("--- parsing instance: \"%s\"", instanceStr));
2216 /* Store the embedded Xen_*SettingData string data in a file for parsing */
2217 tmpnam(filename);
2218 fd = fopen(filename, "w");
2219 fprintf(fd, "%s", instanceStr);
2220 fclose(fd);
2222 fd = fopen(filename, "r");
2223 Xen_SettingDatayyrestart(fd);
2225 /* Parse the embedded Xen_*SettingData string data into a CMPIInstance */
2226 rc = Xen_SettingDatayyparseinstance(_BROKER, &instance);
2227 fclose(fd);
2228 remove(filename);
2229 if (rc != 0) { /* parser returns zero for success, non-zero for error */
2230 _SBLIM_TRACE(1,("--- error parsing instance"));
2231 return NULL;
2234 _SBLIM_TRACE(2,("--- parsed instance: \"%s\"",
2235 CMGetCharPtr(CDToString(_BROKER, instance, NULL))));
2236 return instance;