os-cmpi-xen

view src/Xen_VirtualSystemManagementService.c @ 88:9a96ebba2b60

Added support for localtime, stdvga, OnPoweroff, OnReboot, and OnCrash to Xen_ComputerSystemSettingData.

Signed-off-by: Jim Fehlig <jfehlig@novell.com>
author Jim Fehlig <jfehlig@novell.com>
date Mon Feb 26 14:52:36 2007 -0700 (2007-02-26)
parents 4ae953dc7f33
children 93600f4355d8
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 // Description:
20 // ============================================================================
22 /**
23 * TODO:
24 * 1. Currently not checking if incoming embedded settings are valid in
25 * the various extrinsic method code paths within InvokeMethod().
26 */
28 #include <string.h>
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <assert.h>
33 #include <xen_vm.h>
34 #include <xen_vif.h>
35 #include <xen_vbd.h>
36 #include <xen_console.h>
38 /* Include utility functions */
39 #include "cmpiutil.h"
41 /* Include _SBLIM_TRACE() logging support */
42 #include "cmpitrace.h"
44 /* Include Xen utilities */
45 #include "xen_utils.h"
47 /* Include the abstract resource access functions and abstracted _RESOURCES and _RESOURCE data types. */
48 #include "Xen_VirtualSystemManagementService_Resource.h"
50 /* Include the required CMPI data types, function headers, and macros */
51 #include "cmpidt.h"
52 #include "cmpift.h"
53 #include "cmpimacs.h"
55 #include "provider_common.h"
56 #include "xen_utils.h"
59 // ----------------------------------------------------------------------------
60 // COMMON GLOBAL VARIABLES
61 // ----------------------------------------------------------------------------
63 /* Handle to the CIM broker. Initialized when the provider lib is loaded. */
64 static const CMPIBroker *_BROKER;
66 /* Xen session object. Initialize when the provider is loaded, close when
67 * provider unloaded. */
68 static xen_utils_session *session = NULL;
71 // ============================================================================
72 // CMPI INSTANCE PROVIDER FUNCTION TABLE
73 // ============================================================================
75 // ----------------------------------------------------------------------------
76 // Info for the class supported by the instance provider
77 // ----------------------------------------------------------------------------
79 /* Name of the class implemented by this instance provider. */
80 /*** CUSTOMIZE FOR EACH PROVIDER ***/
81 static char * _CLASSNAME = "Xen_VirtualSystemManagementService";
83 /* NULL terminated list of key properties of this class. */
84 /*** CUSTOMIZE FOR EACH PROVIDER ***/
85 const static char * _KEYNAMES[] = {"SystemName", "SystemCreationClassName", "Name", "CreationClassName", NULL};
88 static int create_vm(char *vsSettings, CMPIArray *resourceSettings,
89 xen_vm *result, CMPIStatus *status);
90 static int add_resource_to_vm(xen_vm_record *vmRec,
91 char *sourceSetting,
92 CMPIObjectPath **resultSetting,
93 char *namespace,
94 CMPIStatus *status);
95 static void remove_vm(xen_vm vm);
96 static int vssd2xenconfig(CMPIInstance *vssd, xen_vm_record *vm_rec, CMPIStatus *status);
97 static int proc_rasd2vmconfig(CMPIInstance *proc_rasd, xen_vm_record *vm_rec,
98 CMPIStatus *status);
99 static int mem_rasd2vmconfig(CMPIInstance *instance, xen_vm_record *vm_rec,
100 CMPIStatus *status);
101 static int disk_rasd2vmconfig(CMPIInstance *instance, xen_vbd_record **vbd_rec,
102 CMPIStatus *status);
103 static int nic_rasd2vmconfig(CMPIInstance *instance, xen_vif_record **vif_rec,
104 CMPIStatus *status);
105 static int con_rasd2vmconfig(CMPIInstance *instance, xen_console_record *con_rec,
106 CMPIStatus *status);
107 static CMPIInstance *parse_embedded_instance(char *instanceStr);
111 // ----------------------------------------------------------------------------
112 // Cleanup()
113 // Perform any necessary cleanup immediately before this provider is unloaded.
114 // ----------------------------------------------------------------------------
115 static CMPIStatus Cleanup(
116 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
117 const CMPIContext * context, /* [in] Additional context info, if any. */
118 CMPIBoolean terminating) /* [in] True if MB is terminating */
119 {
120 CMPIStatus status = { CMPI_RC_OK, NULL }; /* Return status of CIM operations. */
122 _SBLIM_ENTER("Cleanup");
123 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
124 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
126 if (session) {
127 xen_utils_xen_close(session);
128 session = NULL;
129 }
130 _SBLIM_RETURNSTATUS(status);
131 }
133 // ----------------------------------------------------------------------------
134 // EnumInstanceNames()
135 // Return a list of all the instances names (return their object paths only).
136 // ----------------------------------------------------------------------------
137 static CMPIStatus EnumInstanceNames(
138 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
139 const CMPIContext * context, /* [in] Additional context info, if any. */
140 const CMPIResult * results, /* [out] Results of this operation. */
141 const CMPIObjectPath * reference) /* [in] Contains target namespace and classname. */
142 {
143 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
144 _RESOURCES * resources = NULL; /* Handle to the list of system resources. */
145 _RESOURCE * resource; /* Handle to each system resource. */
146 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
147 int found = 0; /* Found any instances? */
149 _SBLIM_ENTER("EnumInstanceNames");
150 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
151 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
152 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
153 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
155 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
156 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
157 goto exit;
158 }
160 /* Get a handle to the list of system resources. */
161 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
162 _SBLIM_TRACE(1,("--- _getResources() failed"));
163 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
164 goto exit;
165 }
167 /* Enumerate thru the list of system resources and return a CMPIInstance for each. */
168 while (Xen_VirtualSystemManagementService_getNextResource(session, resources, &resource)) {
169 /* Create a new CMPIInstance to store this resource. */
170 CMPIInstance * instance = _CMNewInstance(_BROKER, namespace, _CLASSNAME, &status);
171 if ((status.rc != CMPI_RC_OK) || CMIsNullObject(instance)) {
172 _SBLIM_TRACE(1,("--- CMNewInstance() failed - %s", CMGetCharPtr(status.msg)));
173 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERROR, "Cannot create new CMPIInstance");
174 goto exit;
175 }
177 /* Set the instance property values from the resource data. */
178 if (!Xen_VirtualSystemManagementService_setInstanceFromResource(resource, instance, _BROKER)) {
179 _SBLIM_TRACE(1,("--- _setInstanceFromResource() failed"));
180 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to set property values from resource data");
181 goto exit;
182 }
184 /* Free the resource data. */
185 if (!Xen_VirtualSystemManagementService_freeResource(resource)) {
186 _SBLIM_TRACE(1,("--- _freeResource() failed"));
187 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free resource data");
188 goto exit;
189 }
191 /* Return the CMPIObjectPath for this instance. */
192 CMPIObjectPath * objectpath = CMGetObjectPath(instance, &status);
193 if ((status.rc != CMPI_RC_OK) || CMIsNullObject(objectpath)) {
194 _SBLIM_TRACE(1,("--- CMGetObjectPath() failed - %s", CMGetCharPtr(status.msg)));
195 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERROR, "Cannot get CMPIObjectPath for instance");
196 goto exit;
197 }
198 CMSetNameSpace(objectpath, namespace); /* Note - CMGetObjectPath() does not preserve the namespace! */
200 _SBLIM_TRACE(3,("--- objectpath=\"%s\"", CMGetCharPtr(CDToString(_BROKER, objectpath, NULL))));
201 CMReturnObjectPath(results, objectpath);
202 found++;
203 }
205 _SBLIM_TRACE(2,("--- %d object paths found", found));
206 CMReturnDone(results);
208 exit:
209 /* Free the list of system resources. */
210 if (!Xen_VirtualSystemManagementService_freeResources(resources)) {
211 _SBLIM_TRACE(1,("--- _freeResources() failed"));
212 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free list of system resources");
213 }
215 _SBLIM_RETURNSTATUS(status);
216 }
218 // ----------------------------------------------------------------------------
219 // EnumInstances()
220 // Return a list of all the instances (return all the instance data).
221 // ----------------------------------------------------------------------------
222 static CMPIStatus EnumInstances(
223 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
224 const CMPIContext * context, /* [in] Additional context info, if any. */
225 const CMPIResult * results, /* [out] Results of this operation. */
226 const CMPIObjectPath * reference, /* [in] Contains target namespace and classname. */
227 const char ** properties) /* [in] List of desired properties (NULL=all). */
228 {
229 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
230 _RESOURCES * resources = NULL; /* Handle to the list of system resources. */
231 _RESOURCE * resource; /* Handle to each system resource. */
232 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
233 int found = 0; /* Found any resource instances? */
235 _SBLIM_ENTER("EnumInstances");
236 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
237 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
238 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
239 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
241 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
242 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
243 goto exit;
244 }
246 /* Get a handle to the list of system resources. */
247 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
248 _SBLIM_TRACE(1,("--- _getResources() failed"));
249 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
250 goto exit;
251 }
253 /* Enumerate thru the list of system resources and return a CMPIInstance for each. */
254 while (Xen_VirtualSystemManagementService_getNextResource(session, resources, &resource)) {
255 /* Create a new CMPIInstance to store this resource. */
256 CMPIInstance * instance = _CMNewInstance(_BROKER, namespace, _CLASSNAME, &status);
257 if ((status.rc != CMPI_RC_OK) || CMIsNullObject(instance)) {
258 _SBLIM_TRACE(1,("--- CMNewInstance() failed - %s", CMGetCharPtr(status.msg)));
259 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERROR, "Cannot create new CMPIInstance");
260 goto exit;
261 }
263 /* Setup a filter to only return the desired properties. */
264 status = CMSetPropertyFilter(instance, properties, _KEYNAMES);
265 if (status.rc != CMPI_RC_OK) {
266 _SBLIM_TRACE(1, ("--- CMSetPropertyFilter() failed - %s", CMGetCharPtr(status.msg)));
267 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Cannot set property filter");
268 goto exit;
269 }
271 /* Set the instance property values from the resource data. */
272 if (!Xen_VirtualSystemManagementService_setInstanceFromResource(resource, instance, _BROKER)) {
273 _SBLIM_TRACE(1,("--- _setInstanceFromResource() failed"));
274 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to set property values from resource data");
275 goto exit;
276 }
278 /* Free the resource data. */
279 if (!Xen_VirtualSystemManagementService_freeResource(resource)) {
280 _SBLIM_TRACE(1,("--- _freeResource() failed"));
281 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free resource data");
282 goto exit;
283 }
285 /* Return the CMPIInstance for this instance. */
286 _SBLIM_TRACE(3,("--- instance=\"%s\"", CMGetCharPtr(CDToString(_BROKER, instance, NULL))));
287 CMReturnInstance(results, instance);
288 found++;
289 }
291 _SBLIM_TRACE(2,("--- %d instances found", found));
292 CMReturnDone(results);
294 exit:
295 /* Free the list of system resources. */
296 if (!Xen_VirtualSystemManagementService_freeResources(resources)) {
297 _SBLIM_TRACE(1,("--- _freeResources() failed"));
298 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free list of system resources");
299 }
301 _SBLIM_RETURNSTATUS(status);
302 }
304 // ----------------------------------------------------------------------------
305 // GetInstance()
306 // Return the instance data for the specified instance only.
307 // ----------------------------------------------------------------------------
308 static CMPIStatus GetInstance(
309 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
310 const CMPIContext * context, /* [in] Additional context info, if any. */
311 const CMPIResult * results, /* [out] Results of this operation. */
312 const CMPIObjectPath * reference, /* [in] Contains the target namespace, classname and object path. */
313 const char ** properties) /* [in] List of desired properties (NULL=all). */
314 {
315 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
316 _RESOURCES * resources = NULL; /* Handle to the list of system resources. */
317 _RESOURCE * resource; /* Handle to the system resource. */
318 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
319 int found = 0; /* Found the target instance? */
321 _SBLIM_ENTER("GetInstance");
322 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
323 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
324 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
325 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
327 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
328 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
329 goto exit;
330 }
332 /* Get a handle to the list of system resources. */
333 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
334 _SBLIM_TRACE(1,("--- _getResources() failed"));
335 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
336 goto exit;
337 }
339 /* Get the target resource. */
340 found = Xen_VirtualSystemManagementService_getResourceForObjectPath(session, resources, &resource, reference);
341 if (!found || (resource == NULL)) {
342 _SBLIM_TRACE(1,("--- Target instance not found"));
343 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_FOUND, "Target instance not found");
344 goto exit;
345 }
347 /* Create a new CMPIInstance to store this resource. */
348 CMPIInstance * instance = _CMNewInstance(_BROKER, namespace, _CLASSNAME, &status);
349 if ((status.rc != CMPI_RC_OK) || CMIsNullObject(instance)) {
350 _SBLIM_TRACE(1,("--- CMNewInstance() failed - %s", CMGetCharPtr(status.msg)));
351 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERROR, "Cannot create new CMPIInstance");
352 goto exit;
353 }
355 /* Setup a filter to only return the desired properties. */
356 status = CMSetPropertyFilter(instance, properties, _KEYNAMES);
357 if (status.rc != CMPI_RC_OK) {
358 _SBLIM_TRACE(1, ("--- CMSetPropertyFilter() failed - %s", CMGetCharPtr(status.msg)));
359 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Cannot set property filter");
360 goto exit;
361 }
363 /* Set the instance property values from the resource data. */
364 if (!Xen_VirtualSystemManagementService_setInstanceFromResource(resource, instance, _BROKER)) {
365 _SBLIM_TRACE(1,("--- _setInstanceFromResource() failed"));
366 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to set property values from resource data");
367 goto exit;
368 }
370 /* Free the resource data. */
371 if (!Xen_VirtualSystemManagementService_freeResource(resource)) {
372 _SBLIM_TRACE(1,("--- _freeResource() failed"));
373 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free resource data");
374 goto exit;
375 }
377 /* Return the CMPIInstance for this instance. */
378 _SBLIM_TRACE(3,("--- instance=\"%s\"", CMGetCharPtr(CDToString(_BROKER, instance, NULL))));
379 CMReturnInstance(results, instance);
381 _SBLIM_TRACE(2,("--- instance found"));
382 CMReturnDone(results);
384 exit:
385 /* Free the list of system resources. */
386 if (!Xen_VirtualSystemManagementService_freeResources(resources)) {
387 _SBLIM_TRACE(1,("--- _freeResources() failed"));
388 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free list of system resources");
389 }
391 _SBLIM_RETURNSTATUS(status);
392 }
394 // ----------------------------------------------------------------------------
395 // SetInstance()
396 // Save modified instance data for the specified instance.
397 // ----------------------------------------------------------------------------
398 static CMPIStatus SetInstance(
399 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
400 const CMPIContext * context, /* [in] Additional context info, if any. */
401 const CMPIResult * results, /* [out] Results of this operation. */
402 const CMPIObjectPath * reference, /* [in] Contains the target namespace, classname and object path. */
403 const CMPIInstance * newinstance, /* [in] Contains the new instance data. */
404 const char **properties)
405 {
406 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
407 _RESOURCES * resources = NULL; /* Handle to the list of system resources. */
408 _RESOURCE * resource; /* Handle to the system resource. */
409 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
410 int found = 0; /* Found the target instance? */
412 _SBLIM_ENTER("SetInstance");
413 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
414 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
415 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
416 _SBLIM_TRACE(2, ("--- newinstance=\"%s\"", CMGetCharPtr(CDToString(_BROKER, newinstance, NULL))));
417 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
419 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
420 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
421 goto exit;
422 }
424 /* Get a handle to the list of system resources. */
425 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
426 _SBLIM_TRACE(1,("--- _getResources() failed"));
427 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
428 goto exit;
429 }
431 /* Get the target resource. */
432 found = Xen_VirtualSystemManagementService_getResourceForObjectPath(session, resources, &resource, reference);
433 if (!found || (resource == NULL)) {
434 _SBLIM_TRACE(1,("--- Target instance not found"));
435 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_FOUND, "Target instance not found");
436 goto exit;
437 }
439 _SBLIM_TRACE(2,("--- instance found"));
441 /* Update the target resource data with the new instance property values. */
442 int rc = Xen_VirtualSystemManagementService_setResourceFromInstance(resource, newinstance, _BROKER);
444 /* Free the resource data. */
445 if (!Xen_VirtualSystemManagementService_freeResource(resource)) {
446 _SBLIM_TRACE(1,("--- _freeResource() failed"));
447 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free resource data");
448 goto exit;
449 }
451 if (rc != 1) {
452 if (rc == -1) {
453 _SBLIM_TRACE(1,("--- _setResourceFromInstance() unsupported"));
454 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
455 } else {
456 _SBLIM_TRACE(1,("--- _setResourceFromInstance() failed"));
457 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to set resource data from instance properties");
458 }
459 goto exit;
460 }
462 exit:
463 /* Free the list of system resources. */
464 if (!Xen_VirtualSystemManagementService_freeResources(resources)) {
465 _SBLIM_TRACE(1,("--- _freeResources() failed"));
466 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free list of system resources");
467 }
469 _SBLIM_RETURNSTATUS(status);
470 }
472 // ----------------------------------------------------------------------------
473 // CreateInstance()
474 // Create a new instance from the specified instance data.
475 // ----------------------------------------------------------------------------
476 static CMPIStatus CreateInstance(
477 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
478 const CMPIContext * context, /* [in] Additional context info, if any. */
479 const CMPIResult * results, /* [out] Results of this operation. */
480 const CMPIObjectPath * reference, /* [in] Contains the target namespace, classname and object path. */
481 const CMPIInstance * newinstance) /* [in] Contains the new instance data. */
482 {
483 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
484 _RESOURCES * resources = NULL; /* Handle to the list of system resources. */
485 _RESOURCE * resource; /* Handle to the system resource. */
486 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
487 int found = 0; /* Found the target instance? */
489 _SBLIM_ENTER("CreateInstance");
490 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
491 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
492 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
493 _SBLIM_TRACE(2, ("--- newinstance=\"%s\"", CMGetCharPtr(CDToString(_BROKER, newinstance, NULL))));
494 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
496 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
497 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
498 goto exit;
499 }
501 /* WORKAROUND FOR PEGASUS BUG?! reference does not contain object path, only namespace & classname. */
502 reference = CMGetObjectPath(newinstance, NULL);
504 /* Get a handle to the list of system resources. */
505 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
506 _SBLIM_TRACE(1,("--- _getResources() failed"));
507 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
508 goto exit;
509 }
511 /* Get the target resource. */
512 found = Xen_VirtualSystemManagementService_getResourceForObjectPath(session, resources, &resource, reference);
514 /* Free the resource data. */
515 if (!Xen_VirtualSystemManagementService_freeResource(resource)) {
516 _SBLIM_TRACE(1,("--- _freeResource() failed"));
517 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free resource data");
518 goto exit;
519 }
521 if (found) {
522 _SBLIM_TRACE(1,("--- Target instance already exists"));
523 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_ALREADY_EXISTS, "Target instance already exists");
524 goto exit;
525 }
527 /* Create a new resource with the new instance property values. */
528 int rc = Xen_VirtualSystemManagementService_createResourceFromInstance(resources, &resource, newinstance, _BROKER);
529 if (rc != 1) {
530 if (rc == -1) {
531 _SBLIM_TRACE(1,("--- _createResourceFromInstance() unsupported"));
532 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
533 } else {
534 _SBLIM_TRACE(1,("--- _createResourceFromInstance() failed"));
535 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to create resource data from instance properties");
536 }
537 goto exit;
538 }
540 /* Return the object path for the newly created instance. */
541 CMPIObjectPath * objectpath = CMGetObjectPath(newinstance, NULL);
542 CMSetNameSpace(objectpath, namespace);
543 CMReturnObjectPath(results, objectpath);
544 CMReturnDone(results);
546 exit:
547 /* Free the list of system resources. */
548 if (!Xen_VirtualSystemManagementService_freeResources(resources)) {
549 _SBLIM_TRACE(1,("--- _freeResources() failed"));
550 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free list of system resources");
551 }
553 _SBLIM_RETURNSTATUS(status);
554 }
556 // ----------------------------------------------------------------------------
557 // DeleteInstance()
558 // Delete or remove the specified instance from the system.
559 // ----------------------------------------------------------------------------
560 static CMPIStatus DeleteInstance(
561 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
562 const CMPIContext * context, /* [in] Additional context info, if any. */
563 const CMPIResult * results, /* [out] Results of this operation. */
564 const CMPIObjectPath * reference) /* [in] Contains the target namespace, classname and object path. */
565 {
566 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
567 _RESOURCES * resources = NULL; /* Handle to the list of system resources. */
568 _RESOURCE * resource; /* Handle to the system resource. */
569 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
570 int found = 0; /* Found the target instance? */
572 _SBLIM_ENTER("DeleteInstance");
573 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
574 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
575 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
576 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
578 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
579 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
580 goto exit;
581 }
583 /* Get a handle to the list of system resources. */
584 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
585 _SBLIM_TRACE(1,("--- _getResources() failed"));
586 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
587 goto exit;
588 }
590 /* Get the target resource. */
591 found = Xen_VirtualSystemManagementService_getResourceForObjectPath(session, resources, &resource, reference);
592 if (!found || (resource == NULL)) {
593 _SBLIM_TRACE(1,("--- Target instance not found"));
594 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_FOUND, "Target instance not found");
595 goto exit;
596 }
598 _SBLIM_TRACE(2,("--- instance found"));
600 /* Delete the target resource. */
601 int rc = Xen_VirtualSystemManagementService_deleteResource(resources, resource);
603 /* Free the resource data. */
604 if (!Xen_VirtualSystemManagementService_freeResource(resource)) {
605 _SBLIM_TRACE(1,("--- _freeResource() failed"));
606 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free resource data");
607 goto exit;
608 }
610 if (rc != 1) {
611 if (rc == -1) {
612 _SBLIM_TRACE(1,("--- __deleteResource() unsupported"));
613 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
614 } else {
615 _SBLIM_TRACE(1,("--- _deleteResource() failed"));
616 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to delete resource");
617 }
618 goto exit;
619 }
621 exit:
622 /* Free the list of system resources. */
623 if (!Xen_VirtualSystemManagementService_freeResources(resources)) {
624 _SBLIM_TRACE(1,("--- _freeResources() failed"));
625 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free list of system resources");
626 }
628 _SBLIM_RETURNSTATUS(status);
629 }
632 // ----------------------------------------------------------------------------
633 // ExecQuery()
634 // Return a list of all the instances that satisfy the specified query filter.
635 // ----------------------------------------------------------------------------
636 static CMPIStatus ExecQuery(
637 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
638 const CMPIContext * context, /* [in] Additional context info, if any. */
639 const CMPIResult * results, /* [out] Results of this operation. */
640 const CMPIObjectPath * reference, /* [in] Contains the target namespace and classname. */
641 const char * language, /* [in] Name of the query language. */
642 const char * query) /* [in] Text of the query written in the query language. */
643 {
644 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
645 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
647 _SBLIM_ENTER("ExecQuery");
648 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
649 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
650 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
651 _SBLIM_TRACE(2, ("--- language=\"%s\"", language));
652 _SBLIM_TRACE(2, ("--- query=\"%s\"", query));
653 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
655 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
656 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
657 goto exit;
658 }
660 /* EXECQUERY() IS NOT YET SUPPORTED FOR THIS CLASS */
661 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
663 CMReturnDone(results);
665 exit:
666 _SBLIM_RETURNSTATUS(status);
667 }
669 // ----------------------------------------------------------------------------
670 // InstInitialize()
671 // Perform any necessary initialization immediately after the instance provider
672 // is first loaded.
673 // ----------------------------------------------------------------------------
674 static void InstInitialize(
675 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
676 const CMPIContext * context) /* [in] Additional context info, if any. */
677 {
678 _SBLIM_ENTER("InstInitialize");
679 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
680 // _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
682 /* Initialized Xen session object. */
683 if (session == NULL)
684 xen_utils_xen_init(&session);
686 _SBLIM_RETURN();
687 }
690 // ============================================================================
691 // CMPI INSTANCE PROVIDER FUNCTION TABLE SETUP
692 // ============================================================================
693 CMInstanceMIStub( , Xen_VirtualSystemManagementService, _BROKER, InstInitialize(&mi, ctx));
696 // ============================================================================
697 // CMPI METHOD PROVIDER FUNCTION TABLE
698 // ============================================================================
700 // ----------------------------------------------------------------------------
701 // MethodCleanup()
702 // Perform any necessary cleanup immediately before this provider is unloaded.
703 // ----------------------------------------------------------------------------
704 static CMPIStatus MethodCleanup(
705 CMPIMethodMI * self, /* [in] Handle to this provider (i.e. 'self'). */
706 const CMPIContext * context, /* [in] Additional context info, if any. */
707 CMPIBoolean terminating) /* [in] True if MB is terminating */
708 {
709 CMPIStatus status = { CMPI_RC_OK, NULL }; /* Return status of CIM operations. */
711 _SBLIM_ENTER("MethodCleanup");
712 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
713 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
715 if (session) {
716 xen_utils_xen_close(session);
717 session = NULL;
718 }
719 _SBLIM_RETURNSTATUS(status);
720 }
722 // ----------------------------------------------------------------------------
723 // InvokeMethod()
724 // Execute an extrinsic method on the specified instance.
725 // ----------------------------------------------------------------------------
726 static CMPIStatus InvokeMethod(
727 CMPIMethodMI * self, /* [in] Handle to this provider (i.e. 'self') */
728 const CMPIContext * context, /* [in] Additional context info, if any */
729 const CMPIResult * results, /* [out] Results of this operation */
730 const CMPIObjectPath * reference, /* [in] Contains the CIM namespace, classname and desired object path */
731 const char * methodname, /* [in] Name of the method to apply against the reference object */
732 const CMPIArgs * argsin, /* [in] Method input arguments */
733 CMPIArgs * argsout) /* [in] Method output arguments */
734 {
735 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations */
736 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
737 int rc = 0;
738 int argsInCount;
739 _RESOURCES * resources; /* Handle to the list of system resources. */
740 CMPIData argdata;
741 char error_msg[XEN_UTILS_ERROR_BUF_LEN];
743 _SBLIM_ENTER("InvokeMethod");
744 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
745 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
746 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
747 _SBLIM_TRACE(2, ("--- methodname=\"%s\"", methodname));
748 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
750 if (!xen_utils_validate_session(&session)) {
751 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_METHOD_NOT_AVAILABLE, "Unable to connect to xen daemon");
752 goto Exit;
753 }
755 argsInCount = CMGetArgCount(argsin, NULL);
756 _SBLIM_TRACE(2, ("--- # argsin=%d", argsInCount));
758 /* Get a handle to the list of system resources. */
759 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
760 _SBLIM_TRACE(1,("--- _getResources() failed"));
761 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
762 rc = 2;
763 goto Exit;
764 }
766 /* Start/stop the service. */
767 if (strcmp(methodname, "StartService") == 0) {
768 if (!Xen_VirtualSystemManagementService_enableResource(session, resources, NULL)) {
769 _SBLIM_TRACE(1,("--- StartService() failed"));
770 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Method operation failed");
771 rc = 2;
772 goto Exit;
773 }
774 }
775 else if (strcmp(methodname, "StopService") == 0) {
776 if (!Xen_VirtualSystemManagementService_disableResource(session, resources, NULL)) {
777 _SBLIM_TRACE(1,("--- StopService() 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, "DefineSystem") == 0) {
784 if (argsInCount != 3) {
785 _SBLIM_TRACE(1,("--- Incorrect number of method arguments"));
786 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Incorrect number of method arguments");
787 rc = 5;
788 goto Exit;
789 }
791 /* Get embedded instance of VirtualSystemSettingData. */
792 argdata = CMGetArgAt(argsin, 0, NULL, &status);
793 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(argdata)) {
794 _SBLIM_TRACE(1,("--- CMGetArgAt(0) failed"));
795 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Cannot get method input argument");
796 rc = 5;
797 goto Exit;
798 }
800 if (argdata.type != CMPI_string) {
801 _SBLIM_TRACE(1,("--- Invalid setting type - %s", _CMPITypeName(argdata.type)));
802 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid setting type");
803 rc = 5;
804 goto Exit;
805 }
807 char *vsSettingData = CMGetCharPtr(argdata.value.string);
808 _SBLIM_TRACE(2,("--- vsSettingData=\"%s\"", vsSettingData));
810 /* Get input array of ResourceAllocationSettingData instances. */
811 argdata = CMGetArgAt(argsin, 1, NULL, &status);
812 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(argdata)) {
813 _SBLIM_TRACE(1,("--- CMGetArgAt(1) failed"));
814 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Cannot get method input argument");
815 rc = 5;
816 goto Exit;
817 }
819 /* Check that the input arg is an array. */
820 if (!CMIsArray(argdata) || CMIsNullObject(argdata.value.array)) {
821 _SBLIM_TRACE(1,("--- Invalid method argument type. Array expected"));
822 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid method argument type. Array expected");
823 rc = 5;
824 goto Exit;
825 }
827 CMPIArray *resourceSettings = argdata.value.array;
828 xen_vm vm;
829 if (!create_vm(vsSettingData, resourceSettings, &vm, &status))
830 {
831 _SBLIM_TRACE(1,("--- create_vm() failed"));
832 /* status set in create_vm */
833 rc = 3;
834 goto Exit;
835 }
837 /* Return the objectpath for the resulting DomU in the output args */
838 CMPIObjectPath * op = CMNewObjectPath(_BROKER, namespace, "Xen_ComputerSystem", NULL);
839 CMAddKey(op, "CreationClassName", (CMPIValue *)"Xen_ComputerSystem", CMPI_chars);
840 char *vm_name;
841 if (!xen_vm_get_name_label(session->xen, &vm_name, vm)) {
842 /* Can't get the name after all of this work! Unwind this mess. */
843 remove_vm(vm);
844 xen_vm_free(vm);
845 _SBLIM_TRACE(1,("--- Unable to retrieve name of new VM"));
846 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to create the virtual system");
847 rc = 3;
848 goto Exit;
849 }
851 CMAddKey(op, "Name", (CMPIValue *)vm_name, CMPI_chars);
852 /*
853 * WARNING: J. Fehlig
854 * What does this do?
855 *
856 * CMSetHostAndNameSpaceFromObjectPath(op, reference);
857 *
858 */
859 _SBLIM_TRACE(2,("--- VirtualSystem=%s", CMGetCharPtr(CDToString(_BROKER, op, NULL))));
860 CMAddArg(argsout, "ResultingSystem", (CMPIValue*)&op, CMPI_ref);
861 xen_vm_free(vm);
863 _SBLIM_TRACE(2, ("--- DefineSystem completed with success"));
864 }
865 else if (strcmp(methodname, "AddResourceSettings") == 0) {
866 /* TODO */
867 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_METHOD_NOT_AVAILABLE, NULL);
868 rc = 1;
869 goto Exit;
870 }
871 else if (strcmp(methodname, "AddResourceSetting") == 0) {
872 if (argsInCount != 2) {
873 _SBLIM_TRACE(1,("--- Incorrect number of method arguments"));
874 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Incorrect number of method arguments");
875 rc = 5;
876 goto Exit;
877 }
879 /* Get vm record based on VirtualSystemSettingData object path. */
880 argdata = CMGetArgAt(argsin, 0, NULL, &status);
881 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(argdata)) {
882 _SBLIM_TRACE(1,("--- CMGetArgAt(0) failed"));
883 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Cannot get method input argument");
884 rc = 5;
885 goto Exit;
886 }
888 if (argdata.type != CMPI_ref) {
889 _SBLIM_TRACE(1,("--- Invalid setting type - %s", _CMPITypeName(argdata.type)));
890 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid setting type");
891 rc = 5;
892 goto Exit;
893 }
895 _SBLIM_TRACE(2, ("--- domain object path=\"%s\"", CMGetCharPtr(CDToString(_BROKER, argdata.value.ref, NULL))));
897 xen_vm_record *vmRec;
898 if (!xen_utils_get_domain_from_sd_OP(session, &vmRec, argdata.value.ref)) {
899 _SBLIM_TRACE(1,("--- Unable to retrieve domain record from object path"));
900 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Unable to retrieve domain record from object path");
901 rc = 5;
902 goto Exit;
903 }
905 /*
906 * Get input ResourceAllocationSettingData instance
907 * that will be added to domain.
908 */
909 argdata = CMGetArgAt(argsin, 1, NULL, &status);
910 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(argdata)) {
911 _SBLIM_TRACE(1,("--- CMGetArgAt(1) failed"));
912 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Cannot get method input argument");
913 xen_vm_record_free(vmRec);
914 rc = 5;
915 goto Exit;
916 }
918 if (argdata.type != CMPI_string) {
919 _SBLIM_TRACE(1,("--- Invalid setting type - %s", _CMPITypeName(argdata.type)));
920 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid setting type");
921 rc = 5;
922 goto Exit;
923 }
925 char *newSetting = CMGetCharPtr(argdata.value.string);
926 _SBLIM_TRACE(2,("--- newSetting=\"%s\"", newSetting));
929 CMPIObjectPath *resultSetting;
930 if (!add_resource_to_vm(vmRec, newSetting, &resultSetting, namespace, &status))
931 {
932 _SBLIM_TRACE(1,("--- add_resource_to_vm() failed"));
933 /* status set in add_resource_to_vm */
934 xen_vm_record_free(vmRec);
935 rc = 3;
936 goto Exit;
937 }
939 CMAddArg(argsout, "ResultingResourceSetting",
940 (CMPIValue*)&resultSetting, CMPI_ref);
941 xen_vm_record_free(vmRec);
943 _SBLIM_TRACE(2, ("--- AddResourceSetting completed with success"));
944 }
945 else if (strcmp(methodname, "ModifyResourceSettings") == 0) {
946 /* TODO */
947 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
948 rc = 1;
949 goto Exit;
950 }
951 else if (strcmp(methodname, "RemoveResourceSettings") == 0) {
952 /* TODO */
953 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
954 rc = 1;
955 goto Exit;
956 }
957 else if (strcmp(methodname, "ModifySystemSettings") == 0) {
958 /* TODO */
959 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
960 rc = 1;
961 goto Exit;
962 }
963 else if (strcmp(methodname, "DestroySystem") == 0) {
964 /* Check that the method has the correct number of arguments. */
965 if (argsInCount != 1) {
966 _SBLIM_TRACE(1,("--- Incorrect number of method arguments"));
967 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Incorrect number of method arguments");
968 rc = 5;
969 goto Exit;
970 }
972 /* Get object path of domain */
973 argdata = CMGetArgAt(argsin, 0, NULL, &status);
974 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(argdata)) {
975 _SBLIM_TRACE(1,("--- CMGetArgAt(0) failed"));
976 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Cannot get method input argument");
977 rc = 5;
978 goto Exit;
979 }
981 if ((argdata.type != CMPI_ref) || CMIsNullObject(argdata.value.ref)) {
982 _SBLIM_TRACE(1,("--- Invalid parameter type - %s", _CMPITypeName(argdata.type)));
983 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid method input argument");
984 rc = 5;
985 goto Exit;
986 }
988 CMPIData namedata = CMGetKey(argdata.value.ref, "Name", &status);
989 char *domainname = CMGetCharPtr(namedata.value.string);
990 _SBLIM_TRACE(1,("--- domainname = %s", domainname));
991 xen_vm_set *vms;
992 if (!xen_vm_get_by_name_label(session->xen, &vms, domainname)) {
993 _SBLIM_TRACE(1,("--- Target instance not found"));
994 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_FOUND, "Target instance not found");
995 rc = 3;
996 goto Exit;
997 }
998 assert(vms->size == 1);
999 xen_vm vm = vms->contents[0];
1000 enum xen_vm_power_state power_state;
1001 if (!xen_vm_get_power_state(session->xen, &power_state, vm)) {
1002 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1003 _SBLIM_TRACE(1,("--- Unable to retrieve power state of vm - error = %s", error_msg));
1004 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Unable to determine power state of virtual system");
1005 rc = 3;
1006 goto Exit;
1009 _SBLIM_TRACE(1,("--- retrieved power state of vm - %", power_state));
1011 if (power_state != XEN_VM_POWER_STATE_HALTED) {
1012 _SBLIM_TRACE(1,("--- calling xen_vm_clean_shutdown"));
1013 if (!xen_vm_clean_shutdown(session->xen, vm)) {
1014 _SBLIM_TRACE(1, ("--- xen_vm_clean_shutdown() failed"));
1015 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to shutdown VirtualSystem");
1016 rc = 3;
1017 goto Exit;
1021 _SBLIM_TRACE(1,("--- calling xen_vm_destroy"));
1022 if (!xen_vm_destroy(session->xen, vm)) {
1023 _SBLIM_TRACE(1,("--- xen_vm_destroy failed"));
1024 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to destroy the virtual system");
1025 rc = 3;
1026 goto Exit;
1030 else {
1031 _SBLIM_TRACE(1,("--- Method \"%s\" is not supported", methodname));
1032 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_METHOD_NOT_AVAILABLE, NULL);
1033 rc = 5;
1034 goto Exit;
1037 Exit:
1039 CMReturnData(results, (CMPIValue *)&rc, CMPI_uint32);
1040 CMReturnDone(results);
1041 _SBLIM_RETURNSTATUS(status);
1045 // ----------------------------------------------------------------------------
1046 // MethodInitialize()
1047 // Perform any necessary initialization immediately after the method provider
1048 // is first loaded.
1049 // ----------------------------------------------------------------------------
1050 static void MethodInitialize(
1051 CMPIMethodMI * self, /* [in] Handle to this provider (i.e. 'self'). */
1052 const CMPIContext * context) /* [in] Additional context info, if any. */
1054 _SBLIM_ENTER("MethodInitialize");
1055 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
1056 // _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
1058 /* Initialized Xen session object. */
1059 if (session == NULL)
1060 xen_utils_xen_init(&session);
1062 _SBLIM_RETURN();
1065 // ============================================================================
1066 // CMPI METHOD PROVIDER FUNCTION TABLE SETUP
1067 // ============================================================================
1068 CMMethodMIStub( , Xen_VirtualSystemManagementService, _BROKER, MethodInitialize(&mi, ctx));
1071 /*
1072 * Wild macro to support adding devices (vbd, vif) to as list of such
1073 * devices. To be used only in create_vm where list of devices is
1074 * collected from the incoming RASDs.
1075 */
1076 #define ADD_DEVICE_TO_LIST(list, device, type) \
1077 { \
1078 if (list == NULL) { \
1079 list = type ## _set_alloc(1); \
1080 if (list == NULL) { \
1081 _SBLIM_TRACE(1, \
1082 ("Cannot malloc memory for xen device settings list")); \
1083 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, \
1084 "Unable to malloc memory"); \
1085 type ## _free(device); \
1086 goto Error; \
1087 } \
1088 list->size = 1; \
1089 list->contents[0] = device; \
1090 } \
1091 /* List is not empty. Grow the list and add the new device */ \
1092 else { \
1093 int new_len = sizeof(type ## _set) + ((list->size + 1) * sizeof(type)); \
1094 list = realloc(list, new_len); \
1095 if (list == NULL) { \
1096 _SBLIM_TRACE(1, \
1097 ("Cannot malloc memory for xen device settings list")); \
1098 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, \
1099 "Unable to malloc memory"); \
1100 type ## _free(device); \
1101 goto Error; \
1102 } \
1103 list->contents[list->size] = device; \
1104 list->size++; \
1105 } \
1106 } \
1109 static int create_vm(char *vsSettings, CMPIArray *resourceSettings,
1110 xen_vm *result, CMPIStatus *status)
1112 CMPIInstance *instance;
1113 CMPIObjectPath *objectpath;
1114 char *settingclassname;
1115 xen_vm_record *vm_rec;
1116 xen_vbd_record_set *vbds = NULL;
1117 xen_vif_record_set *vifs = NULL;
1118 xen_console_record *con_rec = NULL;
1119 xen_vm vm = NULL;
1120 int i;
1121 int ccode;
1122 char error_msg[XEN_UTILS_ERROR_BUF_LEN];
1124 _SBLIM_TRACE(2,("--- vsSettings=\"%s\"", vsSettings));
1126 vm_rec = xen_vm_record_alloc();
1127 if (vm_rec == NULL) {
1128 _SBLIM_TRACE(1,("--- Cannot malloc memory for vm record"));
1129 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, "Unable to malloc memory");
1130 return 0;
1133 instance = parse_embedded_instance(vsSettings);
1134 if (instance == NULL) { /* parser returns zero for success, non-zero for error */
1135 _SBLIM_TRACE(1,("--- Error parsing VirtualSystemSettingData: \"%s\"", vsSettings));
1136 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Cannot parse VirtualSystemSettingData");
1138 return 0;
1141 objectpath = CMGetObjectPath(instance, NULL);
1142 _SBLIM_TRACE(2,("--- objectpath=%s", CMGetCharPtr(CDToString(_BROKER, objectpath, NULL))));
1144 /* Get the class type of the setting data instance */
1145 settingclassname = CMGetCharPtr(CMGetClassName(objectpath, NULL));
1146 _SBLIM_TRACE(2,("--- settingclassname=%s", settingclassname));
1148 /* Ensure we have a Xen_ComputerSystemSettingData */
1149 if (strcmp(settingclassname,"Xen_ComputerSystemSettingData") != 0) {
1150 _SBLIM_TRACE(1,("--- Unrecognized setting data class - %s", settingclassname));
1151 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Unrecognized setting data class");
1152 return 0;
1155 /* Convert the domain settings to respective xen settings. */
1156 if (!vssd2xenconfig(instance, vm_rec, status)) {
1157 _SBLIM_TRACE(1,("--- failed to convert VSSD to xen_vmrecord"));
1158 xen_vm_record_free(vm_rec);
1159 return 0;
1163 /* Convert resource settings to their respecitve xen settings. */
1164 char *setting;
1165 for (i = 0; i < CMGetArrayCount(resourceSettings, NULL); i++) {
1166 CMPIData settingdata = CMGetArrayElementAt(resourceSettings, i, status);
1167 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(settingdata)) {
1168 _SBLIM_TRACE(1,("--- CMGetArrayElementAt(%d) failed", i));
1169 goto Error;
1172 /* Check that the array element is an (embedded) instance */
1173 // if (settingdata.type != CMPI_instance) {
1174 if (settingdata.type != CMPI_string) {
1175 _SBLIM_TRACE(1,("--- Invalid setting type - %s", _CMPITypeName(settingdata.type)));
1176 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid resource setting data parameter");
1177 goto Error;
1180 setting = CMGetCharPtr(settingdata.value.string);
1181 _SBLIM_TRACE(2,("--- setting=\"%s\"", setting));
1183 instance = parse_embedded_instance(setting);
1185 if (instance == NULL) { /* Fail if we have an unparsable setting data? */
1186 _SBLIM_TRACE(1,("--- Error parsing RASD: \"%s\"", setting));
1187 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Unable to parse resource setting data parameter");
1188 goto Error;
1191 _SBLIM_TRACE(2,("--- instance=%s", CMGetCharPtr(CDToString(_BROKER, instance, NULL))));
1192 objectpath = CMGetObjectPath(instance, NULL);
1193 _SBLIM_TRACE(2,("--- objectpath=%s", CMGetCharPtr(CDToString(_BROKER, objectpath, NULL))));
1195 /* Get the class type of the setting data instance */
1196 settingclassname = CMGetCharPtr(CMGetClassName(objectpath, NULL));
1197 _SBLIM_TRACE(2,("--- settingclassname=%s", settingclassname));
1199 /* Populate config with instance data from the virtual device. */
1200 if (strcmp(settingclassname,"Xen_ProcessorSettingData") == 0) {
1201 _SBLIM_TRACE(2,("--- adding Xen_ProcessorSettingData to configuration"));
1203 if (!proc_rasd2vmconfig(instance, vm_rec, status)) {
1204 _SBLIM_TRACE(1,("--- Error parsing processor settings"));
1205 goto Error;
1208 else if (strcmp(settingclassname,"Xen_MemorySettingData") == 0) {
1209 _SBLIM_TRACE(2,("--- adding Xen_MemorySettingData to configuration"));
1211 /* Fail if duplicate memory rasd's? */
1212 if (vm_rec->memory_dynamic_max > 0) {
1213 _SBLIM_TRACE(1,("--- More than one memroy setting data specified"));
1214 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER,
1215 "More than one memory setting data specified");
1216 goto Error;
1219 if (!mem_rasd2vmconfig(instance, vm_rec, status)) {
1220 _SBLIM_TRACE(1,("--- Error parsing memory settings"));
1221 goto Error;
1224 else if (strcmp(settingclassname,"Xen_DiskSettingData") == 0) {
1225 _SBLIM_TRACE(2,("--- adding Xen_DiskSettingData to configuration"));
1227 xen_vbd_record *vbd_rec;
1228 if (!disk_rasd2vmconfig(instance, &vbd_rec, status)) {
1229 _SBLIM_TRACE(1,("--- Error parsing disk settings"));
1230 goto Error;
1233 ADD_DEVICE_TO_LIST(vbds, vbd_rec, xen_vbd_record);
1235 else if (strcmp(settingclassname,"Xen_NetworkPortSettingData") == 0) {
1236 _SBLIM_TRACE(2,("--- adding Xen_NetworkPortSettingData to configuration"));
1238 xen_vif_record *vif_rec;
1239 if (!nic_rasd2vmconfig(instance, &vif_rec, status)) {
1240 _SBLIM_TRACE(1,("--- Error parsing network port settings"));
1241 goto Error;
1244 ADD_DEVICE_TO_LIST(vifs, vif_rec, xen_vif_record);
1246 else if (strcmp(settingclassname,"Xen_ConsoleSettingData") == 0) {
1247 _SBLIM_TRACE(2,("--- adding Xen_ConsoleSettingData to configuration"));
1249 con_rec = xen_console_record_alloc();
1250 if (con_rec == NULL) {
1251 _SBLIM_TRACE(1,("--- Cannot malloc memory for console record"));
1252 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, "Unable to malloc memory");
1253 goto Error;
1256 if (!con_rasd2vmconfig(instance, con_rec, status)) {
1257 _SBLIM_TRACE(1,("--- Error parsing console settings"));
1258 goto Error;
1261 else {
1262 _SBLIM_TRACE(1,("--- Unrecognized setting data class - %s", settingclassname));
1263 goto Error;
1267 /* We have all of the settings. First create the vm. */
1268 if (!xen_vm_create(session->xen, &vm, vm_rec)) {
1269 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1270 _SBLIM_TRACE(1,("--- xen_vm_create failed: %s", error_msg));
1271 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1272 goto Error;
1275 xen_vm_record_opt vm_record_opt =
1277 .u.handle = vm
1278 };
1280 /* Add all of the virutal block devices. */
1281 for (i = 0; i < vbds->size; i++) {
1283 xen_vbd_record *vbd_rec = vbds->contents[i];
1284 vbd_rec->vm = &vm_record_opt;
1285 xen_vbd new_vbd;
1286 ccode = xen_vbd_create(session->xen, &new_vbd, vbd_rec);
1287 /* Set vm field of vbd record to NULL so it is not freed */
1288 vbd_rec->vm = NULL;
1289 if (!ccode)
1291 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1292 _SBLIM_TRACE(1,("--- xen_vdb_create failed: %s", error_msg));
1293 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1294 goto Error;
1296 xen_vbd_free(new_vbd);
1299 /* Add all of the virutal network devices. */
1300 for (i = 0; i < vifs->size; i++) {
1301 xen_vif_record *vif_rec = vifs->contents[i];
1302 vif_rec->vm = &vm_record_opt;
1303 xen_vif new_vif;
1304 ccode = xen_vif_create(session->xen, &new_vif, vif_rec);
1305 /* Set vm field of vif record to NULL so it is not freed */
1306 vif_rec->vm = NULL;
1307 if (!ccode)
1309 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1310 _SBLIM_TRACE(1,("--- xen_vif_create failed %s", error_msg));
1311 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1312 goto Error;
1314 xen_vif_free(new_vif);
1317 /* Add console device if specified */
1318 if (con_rec) {
1319 con_rec->vm = &vm_record_opt;
1320 xen_console new_con;
1321 ccode = xen_console_create(session->xen, &new_con, con_rec);
1322 /* Set vm field of console record to NULL so it is not freed */
1323 con_rec->vm = NULL;
1324 if (!ccode)
1326 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1327 _SBLIM_TRACE(1,("--- xen_console_create failed %s", error_msg));
1328 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1329 goto Error;
1331 xen_console_free(new_con);
1334 xen_console_record_free(con_rec);
1335 xen_vbd_record_set_free(vbds);
1336 xen_vif_record_set_free(vifs);
1337 xen_vm_record_free(vm_rec);
1339 *result = vm;
1341 return 1;
1343 /* Unwind if we failed.
1344 * remove_vm will nuke any vifs / vbds created and the vm.
1345 */
1346 Error:
1347 if (vm) {
1348 remove_vm(vm);
1349 xen_vm_free(vm);
1351 xen_console_record_free(con_rec);
1352 xen_vbd_record_set_free(vbds);
1353 xen_vif_record_set_free(vifs);
1354 xen_vm_record_free(vm_rec);
1355 *result = NULL;
1357 return 0;
1361 static int add_resource_to_vm(xen_vm_record *vmRec,
1362 char *sourceSetting,
1363 CMPIObjectPath **resultSetting,
1364 char *namespace,
1365 CMPIStatus *status)
1367 CMPIInstance *instance;
1368 CMPIObjectPath *op;
1369 char *settingclassname;
1370 CMPIData propertyvalue;
1371 int ccode;
1372 char instId[MAX_INSTANCEID_LEN];
1373 char error_msg[XEN_UTILS_ERROR_BUF_LEN];
1374 xen_vm_record_opt vm_record_opt =
1376 .u.handle = vmRec->handle
1378 };
1381 /* Parse sourceSetting - creating a CIM instance */
1382 instance = parse_embedded_instance(sourceSetting);
1384 if (instance == NULL) { /* Fail if we have an unparsable setting data? */
1385 _SBLIM_TRACE(1,("--- Error parsing input RASD: \"%s\"", sourceSetting));
1386 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Unable to parse ResourceSetting parameter");
1387 return 0;
1390 _SBLIM_TRACE(2,("--- instance=%s", CMGetCharPtr(CDToString(_BROKER, instance, NULL))));
1391 op = CMGetObjectPath(instance, NULL);
1392 _SBLIM_TRACE(2,("--- objectpath=%s", CMGetCharPtr(CDToString(_BROKER, op, NULL))));
1394 /* Get the class type of the setting data instance */
1395 settingclassname = CMGetCharPtr(CMGetClassName(op, NULL));
1396 _SBLIM_TRACE(2,("--- settingclassname=%s", settingclassname));
1398 /* Add the resource to the domain. */
1399 if (strcmp(settingclassname,"Xen_ProcessorSettingData") == 0) {
1400 #if 1
1401 _SBLIM_TRACE(1,("--- Adding processors not currently supported"));
1402 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, "Adding processors not currently supported");
1403 return 0;
1405 #else
1406 /* Code for adding processors -- when Xen API supports it. */
1407 _SBLIM_TRACE(2,("--- adding Xen_ProcessorSettingData to configuration"));
1408 propertyvalue = CMGetProperty(instance, "VirtualQuantity", status);
1409 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1410 _SBLIM_TRACE(1,("--- Unable to determine VirtualQuantity in Processor RASD"));
1411 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "VirtualQuantity not specified in ResourceSetting");
1412 return 0;
1415 /* Adding cpus means just that - adding to existing amount */
1416 uint64_t cpus = propertyvalue.value.uint64 + vmRec->vcpus_number;
1417 /*
1418 * TODO:
1419 * 1. CPU weights and other scheduling parameters?
1420 */
1421 if (!xen_vm_set_vcpus_number(session->xen,
1422 vmRec->handle,
1423 cpus)) {
1424 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1425 _SBLIM_TRACE(1,("--- xen_vm_set_vcpus_number failed: %s", error_msg));
1426 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1427 return 0;
1430 /* Create new object path for setting */
1431 *resultSetting = CMNewObjectPath(_BROKER, namespace, "Xen_ProcessorSettingData", NULL);
1432 snprintf(instId, MAX_INSTANCEID_LEN, "Xen:%s:Processor", vmRec->name_label);
1433 CMAddKey(*resultSetting, "InstanceID", (CMPIValue *)instId, CMPI_chars);
1434 #endif
1436 else if (strcmp(settingclassname,"Xen_MemorySettingData") == 0) {
1437 #if 1
1438 _SBLIM_TRACE(1,("--- Adding memory not currently supported"));
1439 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, "Adding memory not currently supported");
1440 return 0;
1442 #else
1443 /* Code for adding memory -- when Xen API supports it. */
1444 _SBLIM_TRACE(2,("--- adding Xen_MemorySettingData to configuration"));
1445 propertyvalue = CMGetProperty(instance, "VirtualQuantity", status);
1446 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1447 _SBLIM_TRACE(1,("--- Unable to determine VirtualQuantity in Memory RASD"));
1448 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "VirtualQuantity not specified in ResourceSetting");
1449 return 0;
1452 /* Adding memory means just that - adding to existing amount */
1453 uint64_t mem = propertyvalue.value.uint64 + vmRec->memory_dynamic_max;
1454 if (!xen_vm_set_memory_dynamic_max(session->xen,
1455 vmRec->handle,
1456 mem)) {
1457 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1458 _SBLIM_TRACE(1,("--- xen_vm_set_memory_dynamic_max failed: %s", error_msg));
1459 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1460 return 0;
1463 /* Add object path to output array */
1464 *resultSetting = CMNewObjectPath(_BROKER, namespace, "Xen_MemorySettingData", NULL);
1465 snprintf(instId, MAX_INSTANCEID_LEN, "Xen:%s:Memory1", vmRec->name_label);
1466 CMAddKey(*resultSetting, "InstanceID", (CMPIValue *)instId, CMPI_chars);
1467 #endif
1469 else if (strcmp(settingclassname,"Xen_DiskSettingData") == 0) {
1470 _SBLIM_TRACE(2,("--- adding Xen_DiskSettingData to configuration"));
1472 xen_vbd_record *vbd_rec;
1473 if (!disk_rasd2vmconfig(instance, &vbd_rec, status)) {
1474 /* status set in disk_rasd2vmconfig */
1475 _SBLIM_TRACE(1,("--- Error parsing disk settings"));
1476 return 0;
1479 vbd_rec->vm = &vm_record_opt;
1480 xen_vbd new_vbd;
1481 ccode = xen_vbd_create(session->xen, &new_vbd, vbd_rec);
1482 /* Set vm field of vbd record to NULL so it is not freed */
1483 vbd_rec->vm = NULL;
1484 xen_vbd_record_free(vbd_rec);
1485 if (!ccode)
1487 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1488 _SBLIM_TRACE(1,("--- xen_vdb_create failed: %s", error_msg));
1489 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1490 return 0;
1492 xen_vbd_free(new_vbd);
1494 /* Add object path to output array */
1495 *resultSetting = CMNewObjectPath(_BROKER, namespace, "Xen_DiskSettingData", NULL);
1496 snprintf(instId, MAX_INSTANCEID_LEN, "Xen:%s:Disk", vmRec->name_label);
1497 CMAddKey(*resultSetting, "InstanceID", (CMPIValue *)instId, CMPI_chars);
1499 else if (strcmp(settingclassname,"Xen_NetworkPortSettingData") == 0) {
1500 _SBLIM_TRACE(2,("--- adding Xen_NetworkPortSettingData to configuration"));
1501 xen_vif_record *vif_rec;
1502 if (!nic_rasd2vmconfig(instance, &vif_rec, status)) {
1503 /* status set in disk_rasd2vmconfig */
1504 _SBLIM_TRACE(1,("--- Error parsing network port settings"));
1505 return 0;
1508 vif_rec->vm = &vm_record_opt;
1509 xen_vif new_vif;
1510 ccode = xen_vif_create(session->xen, &new_vif, vif_rec);
1511 /* Set vm field of vif record to NULL so it is not freed */
1512 vif_rec->vm = NULL;
1513 xen_vif_record_free(vif_rec);
1514 if (!ccode)
1516 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1517 _SBLIM_TRACE(1,("--- xen_vif_create failed %s", error_msg));
1518 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1519 return 0;
1521 xen_vif_free(new_vif);
1523 /* Add object path to output array */
1524 *resultSetting = CMNewObjectPath(_BROKER, namespace, "Xen_NetworkPortSettingData", NULL);
1525 snprintf(instId, MAX_INSTANCEID_LEN, "Xen:%s:Vif", vmRec->name_label);
1526 CMAddKey(*resultSetting, "InstanceID", (CMPIValue *)instId, CMPI_chars);
1528 else {
1529 _SBLIM_TRACE(1,("--- Unrecognized setting data class - %s", settingclassname));
1530 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Unrecognized ResourceSetting");
1531 return 0;
1534 return 1;
1538 static void remove_vm(xen_vm vm)
1540 xen_vbd_set *vbds;
1541 xen_vif_set *vifs;
1542 int i;
1544 if (xen_vm_get_vbds(session->xen, &vbds, vm)) {
1545 for (i = 0; i < vbds->size; i++)
1546 xen_vbd_destroy(session->xen, vbds->contents[i]);
1549 if (xen_vm_get_vifs(session->xen, &vifs, vm)) {
1550 for (i = 0; i < vifs->size; i++)
1551 xen_vif_destroy(session->xen, vifs->contents[i]);
1554 xen_vm_destroy(session->xen, vm);
1558 static int vssd2xenconfig(CMPIInstance *vssd, xen_vm_record *vm_rec, CMPIStatus *status)
1560 CMPIData propertyvalue;
1561 char *vsType;;
1563 /*
1564 * Get domain name.
1565 * WARNING!
1566 * Do we fail if VirtualSystemIdentifier is not specified?
1567 */
1568 propertyvalue = CMGetProperty(vssd, "VirtualSystemIdentifier", status);
1569 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1570 _SBLIM_TRACE(1,("--- failed to retrieve VirtualSystemIndentifier property value"));
1571 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Unable to retrieve VirtualSystemIdentifier (name) property from virtual system setting data");
1572 return 0;
1574 vm_rec->name_label = strdup(CMGetCharPtr(propertyvalue.value.string));
1576 /* Get UUID, if specified */
1577 propertyvalue = CMGetProperty(vssd, "UUID", status);
1578 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue))
1579 vm_rec->uuid = strdup(CMGetCharPtr(propertyvalue.value.string));
1581 propertyvalue = CMGetProperty(vssd, "localtime", status);
1582 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue))
1583 vm_rec->platform_localtime = propertyvalue.value.boolean;
1585 propertyvalue = CMGetProperty(vssd, "OnPoweroff", status);
1586 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue))
1587 vm_rec->actions_after_shutdown = propertyvalue.value.uint16;
1589 propertyvalue = CMGetProperty(vssd, "OnReboot", status);
1590 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue))
1591 vm_rec->actions_after_reboot = propertyvalue.value.uint16;
1593 propertyvalue = CMGetProperty(vssd, "OnCrash", status);
1594 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue))
1595 vm_rec->actions_after_crash = propertyvalue.value.uint16;
1597 /* Paravirtual or HVM domain? */
1598 propertyvalue = CMGetProperty(vssd, "VirtualSystemType", status);
1599 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1600 _SBLIM_TRACE(1,("--- VirtualSystemType not specified"));
1601 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER,
1602 "VirtualSystemType not specified");
1603 return 0;
1606 vsType = CMGetCharPtr(propertyvalue.value.string);
1607 if (vsType == NULL) {
1608 _SBLIM_TRACE(1,("--- VirtualSystemType not specified"));
1609 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER,
1610 "VirtualSystemType not specified");
1611 return 0;
1614 if (strcasecmp(vsType, "Xen Paravirtual") == 0) {
1616 propertyvalue = CMGetProperty(vssd, "Bootloader", status);
1617 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1618 vm_rec->pv_bootloader = strdup(CMGetCharPtr(propertyvalue.value.string));
1620 /* If Bootloader specified, get options. */
1621 /* WARNING!
1622 * Should we ensure that BootloaderOptions is specified if
1623 * Bootloader is specified? For domUloader probably but
1624 * pygrub should work with no options.
1625 */
1626 propertyvalue = CMGetProperty(vssd, "BootloaderOptions", status);
1627 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1628 vm_rec->pv_bootloader_args = strdup(CMGetCharPtr(propertyvalue.value.string));
1632 /* Only honor Kernel if Bootloader not specified. */
1633 propertyvalue = CMGetProperty(vssd, "Kernel", status);
1634 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue) &&
1635 vm_rec->pv_bootloader == NULL) {
1636 vm_rec->pv_kernel = strdup(CMGetCharPtr(propertyvalue.value.string));
1638 /* If Kernel specified, get RAMDisk. */
1639 /* WARNING!
1640 * Should we ensure that RAMDisk is specified if
1641 * Kernel is specified? For Unix'ish guests we probably need
1642 * RAMDisk but what about OS's with no notion of RAMDisk?
1643 */
1644 propertyvalue = CMGetProperty(vssd, "RAMDisk", status);
1645 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1646 vm_rec->pv_ramdisk = strdup(CMGetCharPtr(propertyvalue.value.string));
1650 propertyvalue = CMGetProperty(vssd, "KernelOptions", status);
1651 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1652 vm_rec->pv_args = strdup(CMGetCharPtr(propertyvalue.value.string));
1655 else if (strcasecmp(vsType, "Xen HVM") == 0) {
1656 propertyvalue = CMGetProperty(vssd, "BootOrder", status);
1657 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1658 _SBLIM_TRACE(1,("--- No BootOrder specified for HVM guest"));
1659 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER,
1660 "No BootOrder specified for HVM guest");
1661 return 0;
1663 vm_rec->hvm_boot = strdup(CMGetCharPtr(propertyvalue.value.string));
1665 propertyvalue = CMGetProperty(vssd, "stdvga", status);
1666 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue))
1667 vm_rec->platform_std_vga = propertyvalue.value.boolean;
1669 else {
1670 _SBLIM_TRACE(1,("--- Invalide VirtualSystemType %s specified", vsType));
1671 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER,
1672 "Invalid VirtualSystemType specified");
1673 return 0;
1676 return 1;
1680 static int proc_rasd2vmconfig(CMPIInstance *proc_rasd, xen_vm_record *vm_rec,
1681 CMPIStatus *status)
1683 CMPIData propertyvalue;
1685 propertyvalue = CMGetProperty(proc_rasd, "VirtualQuantity", status);
1686 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1687 /*
1688 * TODO:
1689 * 1. Handle defaults if processors not specified.
1690 * 2. CPU weights and other scheduling parameters?
1691 */
1692 vm_rec->vcpus_number = propertyvalue.value.uint64;
1694 return 1;
1697 return 0;
1701 static int mem_rasd2vmconfig(CMPIInstance *mem_rasd, xen_vm_record *vm_rec,
1702 CMPIStatus *status)
1704 CMPIData propertyvalue;
1706 propertyvalue = CMGetProperty(mem_rasd, "VirtualQuantity", status);
1707 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1708 /*
1709 * TODO:
1710 * 1. Handle defaults if memory is not specified.
1711 * 2. What is semantics of these fields? For now
1712 * set them all to the requested value.
1713 */
1714 vm_rec->memory_static_max = propertyvalue.value.uint64;
1715 vm_rec->memory_dynamic_max = propertyvalue.value.uint64;
1716 vm_rec->memory_static_min = propertyvalue.value.uint64;
1717 vm_rec->memory_dynamic_min = propertyvalue.value.uint64;
1719 return 1;
1722 return 0;
1726 static int disk_rasd2vmconfig(CMPIInstance *disk_rasd, xen_vbd_record **vbd_rec,
1727 CMPIStatus *status)
1729 CMPIData propertyvalue;
1731 propertyvalue = CMGetProperty(disk_rasd, "DiskConfigInfo", status);
1732 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1733 *vbd_rec = xen_vbd_record_alloc();
1734 if (*vbd_rec == NULL) {
1735 _SBLIM_TRACE(1,("--- Cannot malloc memory for virtual block device"));
1736 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, "Unable to malloc memory");
1737 return 0;
1740 /* Extract the image, dev and mode params from the DiskConfigInfo string */
1741 char *tok;
1742 char *next_tok;
1743 char *string = strdup(CMGetCharPtr(propertyvalue.value.string));
1744 tok = strtok_r(string, ",", &next_tok);
1745 if (tok == NULL) {
1746 _SBLIM_TRACE(1,("--- Malformed DiskConfigInfo property in disk setting data"));
1747 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalide disk setting data");
1748 goto Error;
1750 (*vbd_rec)->image = strdup(tok);
1752 if ((tok = strtok_r(NULL, ",", &next_tok)) == NULL) {
1753 _SBLIM_TRACE(1,("--- Malformed DiskConfigInfo property in disk setting data"));
1754 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalide disk setting data");
1755 goto Error;
1757 (*vbd_rec)->device = strdup(tok);
1759 if ((tok = strtok_r(NULL, ",", &next_tok)) == NULL) {
1760 _SBLIM_TRACE(1,("--- Malformed DiskConfigInfo property in disk setting data"));
1761 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalide disk setting data");
1762 goto Error;
1764 if (strcmp(tok, "w") == 0)
1765 (*vbd_rec)->mode = XEN_VBD_MODE_RW;
1766 else
1767 (*vbd_rec)->mode = XEN_VBD_MODE_RO;
1769 free(string);
1770 _SBLIM_TRACE(2,("--- uname = %s", (*vbd_rec)->image));
1771 _SBLIM_TRACE(2,("--- dev = %s", (*vbd_rec)->device));
1772 _SBLIM_TRACE(2,("--- mode = %s",
1773 (*vbd_rec)->mode == XEN_VBD_MODE_RW ? "RW" : "RO"));
1775 return 1;
1778 Error:
1779 /* frees fields as well */
1780 xen_vbd_record_free(*vbd_rec);
1781 *vbd_rec = NULL;
1783 return 0;
1787 static int nic_rasd2vmconfig(CMPIInstance *nic_rasd, xen_vif_record **vif_rec,
1788 CMPIStatus *status)
1790 CMPIData propertyvalue;
1792 /* Set the domain config data from the Xen_NetworkPortSettingData. */
1793 propertyvalue = CMGetProperty(nic_rasd, "NICConfigInfo", status);
1794 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1795 *vif_rec = xen_vif_record_alloc();
1796 if (*vif_rec == NULL) {
1797 _SBLIM_TRACE(1,("--- Cannot malloc memory for virtual network device"));
1798 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, "Unable to malloc memory");
1799 return 0;
1802 /* Extract the type, mac and network params from the NicConfigInfo
1803 * string. Expected syntax is "key=value,key=value" */
1804 char * next_tok;
1805 char * string = strdup(CMGetCharPtr(propertyvalue.value.string));
1806 char *tok = strtok_r(string, ",", &next_tok);
1807 /* If tok is NULL, then string contains only 1 key/value pair */
1808 if (tok == NULL)
1809 tok = string;
1810 while (tok) {
1811 if (strncmp(tok, "mac", 3) == 0)
1812 (*vif_rec)->mac = strdup(tok + 4);
1813 /*
1814 * TODO:
1815 * Map other config items in the NIC info to xen_vif_record struct.
1816 */
1818 tok = strtok_r(NULL, ",", &next_tok);
1821 free(string);
1822 _SBLIM_TRACE(2,("--- mac = %s",
1823 (*vif_rec)->mac ? (*vif_rec)->mac : "not specified"));
1825 return 1;
1828 *vif_rec = NULL;
1829 return 0;
1833 static int con_rasd2vmconfig(CMPIInstance *con_rasd, xen_console_record *con_rec,
1834 CMPIStatus *status)
1836 CMPIData propertyvalue;
1838 propertyvalue = CMGetProperty(con_rasd, "Protocol", status);
1839 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1840 _SBLIM_TRACE(1,("--- No protocol field specified in console setting data"));
1841 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "No protocol specified in console setting data");
1842 return 0;
1845 switch (propertyvalue.value.uint16) {
1846 case 0:
1847 con_rec->protocol = XEN_CONSOLE_PROTOCOL_VT100;
1848 break;
1849 case 1:
1850 con_rec->protocol = XEN_CONSOLE_PROTOCOL_RFB;
1851 break;
1852 case 2:
1853 con_rec->protocol = XEN_CONSOLE_PROTOCOL_RDP;
1854 break;
1855 default:
1856 _SBLIM_TRACE(1,("--- Invalid protocol specified in console setting data"));
1857 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid protocol specified in console setting data");
1858 return 0;
1861 return 1;
1866 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1867 // THE FOLLOWING CODE IS REQUIRED UNTIL EMBEDDEDOBJECT SUPPORT IS WORKING!!!
1868 //
1869 static CMPIInstance *parse_embedded_instance(char *instanceStr)
1871 char filename[L_tmpnam];
1872 FILE *fd;
1873 int rc;
1874 CMPIInstance *instance;
1876 _SBLIM_TRACE(2,("--- parsing instance: \"%s\"", instanceStr));
1878 /* Store the embedded Xen_*SettingData string data in a file for parsing */
1879 tmpnam(filename);
1880 fd = fopen(filename, "w");
1881 fprintf(fd, "%s", instanceStr);
1882 fclose(fd);
1884 fd = fopen(filename, "r");
1885 Xen_SettingDatayyrestart(fd);
1887 /* Parse the embedded Xen_*SettingData string data into a CMPIInstance */
1888 rc = Xen_SettingDatayyparseinstance(_BROKER, &instance);
1889 fclose(fd);
1890 remove(filename);
1891 if (rc != 0) { /* parser returns zero for success, non-zero for error */
1892 _SBLIM_TRACE(1,("--- error parsing instance"));
1893 return NULL;
1896 _SBLIM_TRACE(2,("--- parsed instance: \"%s\"",
1897 CMGetCharPtr(CDToString(_BROKER, instance, NULL))));
1898 return instance;