os-cmpi-xen

view src/Xen_VirtualSystemManagementService.c @ 143:fc498265d293

Fixed warning (and bug) in Xen_DiskSettingData.

Signed-off-by: Jim Fehlig <jfehlig@novell.com>
author Jim Fehlig <jfehlig@novell.com>
date Thu Aug 16 17:49:08 2007 -0600 (2007-08-16)
parents d1dab4776fed
children
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 CMPIInstance *parse_embedded_instance(char *instanceStr);
117 // ----------------------------------------------------------------------------
118 // Cleanup()
119 // Perform any necessary cleanup immediately before this provider is unloaded.
120 // ----------------------------------------------------------------------------
121 static CMPIStatus Cleanup(
122 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
123 const CMPIContext * context, /* [in] Additional context info, if any. */
124 CMPIBoolean terminating) /* [in] True if MB is terminating */
125 {
126 CMPIStatus status = { CMPI_RC_OK, NULL }; /* Return status of CIM operations. */
128 _SBLIM_ENTER("Cleanup");
129 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
130 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
132 if (session) {
133 xen_utils_xen_close(session);
134 session = NULL;
135 }
136 _SBLIM_RETURNSTATUS(status);
137 }
139 // ----------------------------------------------------------------------------
140 // EnumInstanceNames()
141 // Return a list of all the instances names (return their object paths only).
142 // ----------------------------------------------------------------------------
143 static CMPIStatus EnumInstanceNames(
144 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
145 const CMPIContext * context, /* [in] Additional context info, if any. */
146 const CMPIResult * results, /* [out] Results of this operation. */
147 const CMPIObjectPath * reference) /* [in] Contains target namespace and classname. */
148 {
149 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
150 _RESOURCES * resources = NULL; /* Handle to the list of system resources. */
151 _RESOURCE * resource; /* Handle to each system resource. */
152 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
153 int found = 0; /* Found any instances? */
155 _SBLIM_ENTER("EnumInstanceNames");
156 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
157 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
158 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
159 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
161 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
162 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
163 goto exit;
164 }
166 /* Get a handle to the list of system resources. */
167 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
168 _SBLIM_TRACE(1,("--- _getResources() failed"));
169 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
170 goto exit;
171 }
173 /* Enumerate thru the list of system resources and return a CMPIInstance for each. */
174 while (Xen_VirtualSystemManagementService_getNextResource(session, resources, &resource)) {
175 /* Create a new CMPIInstance to store this resource. */
176 CMPIInstance * instance = _CMNewInstance(_BROKER, namespace, _CLASSNAME, &status);
177 if ((status.rc != CMPI_RC_OK) || CMIsNullObject(instance)) {
178 _SBLIM_TRACE(1,("--- CMNewInstance() failed - %s", CMGetCharPtr(status.msg)));
179 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERROR, "Cannot create new CMPIInstance");
180 goto exit;
181 }
183 /* Set the instance property values from the resource data. */
184 if (!Xen_VirtualSystemManagementService_setInstanceFromResource(resource, instance, _BROKER)) {
185 _SBLIM_TRACE(1,("--- _setInstanceFromResource() failed"));
186 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to set property values from resource data");
187 goto exit;
188 }
190 /* Free the resource data. */
191 if (!Xen_VirtualSystemManagementService_freeResource(resource)) {
192 _SBLIM_TRACE(1,("--- _freeResource() failed"));
193 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free resource data");
194 goto exit;
195 }
197 /* Return the CMPIObjectPath for this instance. */
198 CMPIObjectPath * objectpath = CMGetObjectPath(instance, &status);
199 if ((status.rc != CMPI_RC_OK) || CMIsNullObject(objectpath)) {
200 _SBLIM_TRACE(1,("--- CMGetObjectPath() failed - %s", CMGetCharPtr(status.msg)));
201 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERROR, "Cannot get CMPIObjectPath for instance");
202 goto exit;
203 }
204 CMSetNameSpace(objectpath, namespace); /* Note - CMGetObjectPath() does not preserve the namespace! */
206 _SBLIM_TRACE(3,("--- objectpath=\"%s\"", CMGetCharPtr(CDToString(_BROKER, objectpath, NULL))));
207 CMReturnObjectPath(results, objectpath);
208 found++;
209 }
211 _SBLIM_TRACE(2,("--- %d object paths found", found));
212 CMReturnDone(results);
214 exit:
215 /* Free the list of system resources. */
216 if (!Xen_VirtualSystemManagementService_freeResources(resources)) {
217 _SBLIM_TRACE(1,("--- _freeResources() failed"));
218 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free list of system resources");
219 }
221 _SBLIM_RETURNSTATUS(status);
222 }
224 // ----------------------------------------------------------------------------
225 // EnumInstances()
226 // Return a list of all the instances (return all the instance data).
227 // ----------------------------------------------------------------------------
228 static CMPIStatus EnumInstances(
229 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
230 const CMPIContext * context, /* [in] Additional context info, if any. */
231 const CMPIResult * results, /* [out] Results of this operation. */
232 const CMPIObjectPath * reference, /* [in] Contains target namespace and classname. */
233 const char ** properties) /* [in] List of desired properties (NULL=all). */
234 {
235 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
236 _RESOURCES * resources = NULL; /* Handle to the list of system resources. */
237 _RESOURCE * resource; /* Handle to each system resource. */
238 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
239 int found = 0; /* Found any resource instances? */
241 _SBLIM_ENTER("EnumInstances");
242 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
243 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
244 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
245 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
247 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
248 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
249 goto exit;
250 }
252 /* Get a handle to the list of system resources. */
253 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
254 _SBLIM_TRACE(1,("--- _getResources() failed"));
255 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
256 goto exit;
257 }
259 /* Enumerate thru the list of system resources and return a CMPIInstance for each. */
260 while (Xen_VirtualSystemManagementService_getNextResource(session, resources, &resource)) {
261 /* Create a new CMPIInstance to store this resource. */
262 CMPIInstance * instance = _CMNewInstance(_BROKER, namespace, _CLASSNAME, &status);
263 if ((status.rc != CMPI_RC_OK) || CMIsNullObject(instance)) {
264 _SBLIM_TRACE(1,("--- CMNewInstance() failed - %s", CMGetCharPtr(status.msg)));
265 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERROR, "Cannot create new CMPIInstance");
266 goto exit;
267 }
269 /* Setup a filter to only return the desired properties. */
270 status = CMSetPropertyFilter(instance, properties, _KEYNAMES);
271 if (status.rc != CMPI_RC_OK) {
272 _SBLIM_TRACE(1, ("--- CMSetPropertyFilter() failed - %s", CMGetCharPtr(status.msg)));
273 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Cannot set property filter");
274 goto exit;
275 }
277 /* Set the instance property values from the resource data. */
278 if (!Xen_VirtualSystemManagementService_setInstanceFromResource(resource, instance, _BROKER)) {
279 _SBLIM_TRACE(1,("--- _setInstanceFromResource() failed"));
280 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to set property values from resource data");
281 goto exit;
282 }
284 /* Free the resource data. */
285 if (!Xen_VirtualSystemManagementService_freeResource(resource)) {
286 _SBLIM_TRACE(1,("--- _freeResource() failed"));
287 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free resource data");
288 goto exit;
289 }
291 /* Return the CMPIInstance for this instance. */
292 _SBLIM_TRACE(3,("--- instance=\"%s\"", CMGetCharPtr(CDToString(_BROKER, instance, NULL))));
293 CMReturnInstance(results, instance);
294 found++;
295 }
297 _SBLIM_TRACE(2,("--- %d instances found", found));
298 CMReturnDone(results);
300 exit:
301 /* Free the list of system resources. */
302 if (!Xen_VirtualSystemManagementService_freeResources(resources)) {
303 _SBLIM_TRACE(1,("--- _freeResources() failed"));
304 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free list of system resources");
305 }
307 _SBLIM_RETURNSTATUS(status);
308 }
310 // ----------------------------------------------------------------------------
311 // GetInstance()
312 // Return the instance data for the specified instance only.
313 // ----------------------------------------------------------------------------
314 static CMPIStatus GetInstance(
315 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
316 const CMPIContext * context, /* [in] Additional context info, if any. */
317 const CMPIResult * results, /* [out] Results of this operation. */
318 const CMPIObjectPath * reference, /* [in] Contains the target namespace, classname and object path. */
319 const char ** properties) /* [in] List of desired properties (NULL=all). */
320 {
321 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
322 _RESOURCES * resources = NULL; /* Handle to the list of system resources. */
323 _RESOURCE * resource; /* Handle to the system resource. */
324 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
325 int found = 0; /* Found the target instance? */
327 _SBLIM_ENTER("GetInstance");
328 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
329 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
330 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
331 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
333 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
334 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
335 goto exit;
336 }
338 /* Get a handle to the list of system resources. */
339 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
340 _SBLIM_TRACE(1,("--- _getResources() failed"));
341 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
342 goto exit;
343 }
345 /* Get the target resource. */
346 found = Xen_VirtualSystemManagementService_getResourceForObjectPath(session, resources, &resource, reference);
347 if (!found || (resource == NULL)) {
348 _SBLIM_TRACE(1,("--- Target instance not found"));
349 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_FOUND, "Target instance not found");
350 goto exit;
351 }
353 /* Create a new CMPIInstance to store this resource. */
354 CMPIInstance * instance = _CMNewInstance(_BROKER, namespace, _CLASSNAME, &status);
355 if ((status.rc != CMPI_RC_OK) || CMIsNullObject(instance)) {
356 _SBLIM_TRACE(1,("--- CMNewInstance() failed - %s", CMGetCharPtr(status.msg)));
357 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERROR, "Cannot create new CMPIInstance");
358 goto exit;
359 }
361 /* Setup a filter to only return the desired properties. */
362 status = CMSetPropertyFilter(instance, properties, _KEYNAMES);
363 if (status.rc != CMPI_RC_OK) {
364 _SBLIM_TRACE(1, ("--- CMSetPropertyFilter() failed - %s", CMGetCharPtr(status.msg)));
365 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Cannot set property filter");
366 goto exit;
367 }
369 /* Set the instance property values from the resource data. */
370 if (!Xen_VirtualSystemManagementService_setInstanceFromResource(resource, instance, _BROKER)) {
371 _SBLIM_TRACE(1,("--- _setInstanceFromResource() failed"));
372 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to set property values from resource data");
373 goto exit;
374 }
376 /* Free the resource data. */
377 if (!Xen_VirtualSystemManagementService_freeResource(resource)) {
378 _SBLIM_TRACE(1,("--- _freeResource() failed"));
379 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free resource data");
380 goto exit;
381 }
383 /* Return the CMPIInstance for this instance. */
384 _SBLIM_TRACE(3,("--- instance=\"%s\"", CMGetCharPtr(CDToString(_BROKER, instance, NULL))));
385 CMReturnInstance(results, instance);
387 _SBLIM_TRACE(2,("--- instance found"));
388 CMReturnDone(results);
390 exit:
391 /* Free the list of system resources. */
392 if (!Xen_VirtualSystemManagementService_freeResources(resources)) {
393 _SBLIM_TRACE(1,("--- _freeResources() failed"));
394 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free list of system resources");
395 }
397 _SBLIM_RETURNSTATUS(status);
398 }
400 // ----------------------------------------------------------------------------
401 // SetInstance()
402 // Save modified instance data for the specified instance.
403 // ----------------------------------------------------------------------------
404 static CMPIStatus SetInstance(
405 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
406 const CMPIContext * context, /* [in] Additional context info, if any. */
407 const CMPIResult * results, /* [out] Results of this operation. */
408 const CMPIObjectPath * reference, /* [in] Contains the target namespace, classname and object path. */
409 const CMPIInstance * newinstance, /* [in] Contains the new instance data. */
410 const char **properties)
411 {
412 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
413 _RESOURCES * resources = NULL; /* Handle to the list of system resources. */
414 _RESOURCE * resource; /* Handle to the system resource. */
415 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
416 int found = 0; /* Found the target instance? */
418 _SBLIM_ENTER("SetInstance");
419 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
420 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
421 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
422 _SBLIM_TRACE(2, ("--- newinstance=\"%s\"", CMGetCharPtr(CDToString(_BROKER, newinstance, NULL))));
423 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
425 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
426 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
427 goto exit;
428 }
430 /* Get a handle to the list of system resources. */
431 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
432 _SBLIM_TRACE(1,("--- _getResources() failed"));
433 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
434 goto exit;
435 }
437 /* Get the target resource. */
438 found = Xen_VirtualSystemManagementService_getResourceForObjectPath(session, resources, &resource, reference);
439 if (!found || (resource == NULL)) {
440 _SBLIM_TRACE(1,("--- Target instance not found"));
441 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_FOUND, "Target instance not found");
442 goto exit;
443 }
445 _SBLIM_TRACE(2,("--- instance found"));
447 /* Update the target resource data with the new instance property values. */
448 int rc = Xen_VirtualSystemManagementService_setResourceFromInstance(resource, newinstance, _BROKER);
450 /* Free the resource data. */
451 if (!Xen_VirtualSystemManagementService_freeResource(resource)) {
452 _SBLIM_TRACE(1,("--- _freeResource() failed"));
453 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free resource data");
454 goto exit;
455 }
457 if (rc != 1) {
458 if (rc == -1) {
459 _SBLIM_TRACE(1,("--- _setResourceFromInstance() unsupported"));
460 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
461 } else {
462 _SBLIM_TRACE(1,("--- _setResourceFromInstance() failed"));
463 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to set resource data from instance properties");
464 }
465 goto exit;
466 }
468 exit:
469 /* Free the list of system resources. */
470 if (!Xen_VirtualSystemManagementService_freeResources(resources)) {
471 _SBLIM_TRACE(1,("--- _freeResources() failed"));
472 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free list of system resources");
473 }
475 _SBLIM_RETURNSTATUS(status);
476 }
478 // ----------------------------------------------------------------------------
479 // CreateInstance()
480 // Create a new instance from the specified instance data.
481 // ----------------------------------------------------------------------------
482 static CMPIStatus CreateInstance(
483 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
484 const CMPIContext * context, /* [in] Additional context info, if any. */
485 const CMPIResult * results, /* [out] Results of this operation. */
486 const CMPIObjectPath * reference, /* [in] Contains the target namespace, classname and object path. */
487 const CMPIInstance * newinstance) /* [in] Contains the new instance data. */
488 {
489 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
490 _RESOURCES * resources = NULL; /* Handle to the list of system resources. */
491 _RESOURCE * resource; /* Handle to the system resource. */
492 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
493 int found = 0; /* Found the target instance? */
495 _SBLIM_ENTER("CreateInstance");
496 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
497 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
498 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
499 _SBLIM_TRACE(2, ("--- newinstance=\"%s\"", CMGetCharPtr(CDToString(_BROKER, newinstance, NULL))));
500 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
502 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
503 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
504 goto exit;
505 }
507 /* WORKAROUND FOR PEGASUS BUG?! reference does not contain object path, only namespace & classname. */
508 reference = CMGetObjectPath(newinstance, NULL);
510 /* Get a handle to the list of system resources. */
511 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
512 _SBLIM_TRACE(1,("--- _getResources() failed"));
513 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
514 goto exit;
515 }
517 /* Get the target resource. */
518 found = Xen_VirtualSystemManagementService_getResourceForObjectPath(session, resources, &resource, reference);
520 /* Free the resource data. */
521 if (!Xen_VirtualSystemManagementService_freeResource(resource)) {
522 _SBLIM_TRACE(1,("--- _freeResource() failed"));
523 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free resource data");
524 goto exit;
525 }
527 if (found) {
528 _SBLIM_TRACE(1,("--- Target instance already exists"));
529 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_ALREADY_EXISTS, "Target instance already exists");
530 goto exit;
531 }
533 /* Create a new resource with the new instance property values. */
534 int rc = Xen_VirtualSystemManagementService_createResourceFromInstance(resources, &resource, newinstance, _BROKER);
535 if (rc != 1) {
536 if (rc == -1) {
537 _SBLIM_TRACE(1,("--- _createResourceFromInstance() unsupported"));
538 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
539 } else {
540 _SBLIM_TRACE(1,("--- _createResourceFromInstance() failed"));
541 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to create resource data from instance properties");
542 }
543 goto exit;
544 }
546 /* Return the object path for the newly created instance. */
547 CMPIObjectPath * objectpath = CMGetObjectPath(newinstance, NULL);
548 CMSetNameSpace(objectpath, namespace);
549 CMReturnObjectPath(results, objectpath);
550 CMReturnDone(results);
552 exit:
553 /* Free the list of system resources. */
554 if (!Xen_VirtualSystemManagementService_freeResources(resources)) {
555 _SBLIM_TRACE(1,("--- _freeResources() failed"));
556 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free list of system resources");
557 }
559 _SBLIM_RETURNSTATUS(status);
560 }
562 // ----------------------------------------------------------------------------
563 // DeleteInstance()
564 // Delete or remove the specified instance from the system.
565 // ----------------------------------------------------------------------------
566 static CMPIStatus DeleteInstance(
567 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
568 const CMPIContext * context, /* [in] Additional context info, if any. */
569 const CMPIResult * results, /* [out] Results of this operation. */
570 const CMPIObjectPath * reference) /* [in] Contains the target namespace, classname and object path. */
571 {
572 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
573 _RESOURCES * resources = NULL; /* Handle to the list of system resources. */
574 _RESOURCE * resource; /* Handle to the system resource. */
575 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
576 int found = 0; /* Found the target instance? */
578 _SBLIM_ENTER("DeleteInstance");
579 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
580 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
581 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
582 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
584 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
585 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
586 goto exit;
587 }
589 /* Get a handle to the list of system resources. */
590 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
591 _SBLIM_TRACE(1,("--- _getResources() failed"));
592 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
593 goto exit;
594 }
596 /* Get the target resource. */
597 found = Xen_VirtualSystemManagementService_getResourceForObjectPath(session, resources, &resource, reference);
598 if (!found || (resource == NULL)) {
599 _SBLIM_TRACE(1,("--- Target instance not found"));
600 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_FOUND, "Target instance not found");
601 goto exit;
602 }
604 _SBLIM_TRACE(2,("--- instance found"));
606 /* Delete the target resource. */
607 int rc = Xen_VirtualSystemManagementService_deleteResource(resources, resource);
609 /* Free the resource data. */
610 if (!Xen_VirtualSystemManagementService_freeResource(resource)) {
611 _SBLIM_TRACE(1,("--- _freeResource() failed"));
612 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free resource data");
613 goto exit;
614 }
616 if (rc != 1) {
617 if (rc == -1) {
618 _SBLIM_TRACE(1,("--- __deleteResource() unsupported"));
619 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
620 } else {
621 _SBLIM_TRACE(1,("--- _deleteResource() failed"));
622 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to delete resource");
623 }
624 goto exit;
625 }
627 exit:
628 /* Free the list of system resources. */
629 if (!Xen_VirtualSystemManagementService_freeResources(resources)) {
630 _SBLIM_TRACE(1,("--- _freeResources() failed"));
631 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free list of system resources");
632 }
634 _SBLIM_RETURNSTATUS(status);
635 }
638 // ----------------------------------------------------------------------------
639 // ExecQuery()
640 // Return a list of all the instances that satisfy the specified query filter.
641 // ----------------------------------------------------------------------------
642 static CMPIStatus ExecQuery(
643 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
644 const CMPIContext * context, /* [in] Additional context info, if any. */
645 const CMPIResult * results, /* [out] Results of this operation. */
646 const CMPIObjectPath * reference, /* [in] Contains the target namespace and classname. */
647 const char * language, /* [in] Name of the query language. */
648 const char * query) /* [in] Text of the query written in the query language. */
649 {
650 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
651 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
653 _SBLIM_ENTER("ExecQuery");
654 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
655 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
656 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
657 _SBLIM_TRACE(2, ("--- language=\"%s\"", language));
658 _SBLIM_TRACE(2, ("--- query=\"%s\"", query));
659 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
661 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
662 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
663 goto exit;
664 }
666 /* EXECQUERY() IS NOT YET SUPPORTED FOR THIS CLASS */
667 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
669 CMReturnDone(results);
671 exit:
672 _SBLIM_RETURNSTATUS(status);
673 }
675 // ----------------------------------------------------------------------------
676 // InstInitialize()
677 // Perform any necessary initialization immediately after the instance provider
678 // is first loaded.
679 // ----------------------------------------------------------------------------
680 static void InstInitialize(
681 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
682 const CMPIContext * context) /* [in] Additional context info, if any. */
683 {
684 _SBLIM_ENTER("InstInitialize");
685 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
686 // _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
688 /* Initialized Xen session object. */
689 if (session == NULL)
690 xen_utils_xen_init(&session);
692 _SBLIM_RETURN();
693 }
696 // ============================================================================
697 // CMPI INSTANCE PROVIDER FUNCTION TABLE SETUP
698 // ============================================================================
699 CMInstanceMIStub( , Xen_VirtualSystemManagementService, _BROKER, InstInitialize(&mi, ctx));
702 // ============================================================================
703 // CMPI METHOD PROVIDER FUNCTION TABLE
704 // ============================================================================
706 // ----------------------------------------------------------------------------
707 // MethodCleanup()
708 // Perform any necessary cleanup immediately before this provider is unloaded.
709 // ----------------------------------------------------------------------------
710 static CMPIStatus MethodCleanup(
711 CMPIMethodMI * self, /* [in] Handle to this provider (i.e. 'self'). */
712 const CMPIContext * context, /* [in] Additional context info, if any. */
713 CMPIBoolean terminating) /* [in] True if MB is terminating */
714 {
715 CMPIStatus status = { CMPI_RC_OK, NULL }; /* Return status of CIM operations. */
717 _SBLIM_ENTER("MethodCleanup");
718 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
719 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
721 if (session) {
722 xen_utils_xen_close(session);
723 session = NULL;
724 }
725 _SBLIM_RETURNSTATUS(status);
726 }
728 // ----------------------------------------------------------------------------
729 // InvokeMethod()
730 // Execute an extrinsic method on the specified instance.
731 // ----------------------------------------------------------------------------
732 static CMPIStatus InvokeMethod(
733 CMPIMethodMI * self, /* [in] Handle to this provider (i.e. 'self') */
734 const CMPIContext * context, /* [in] Additional context info, if any */
735 const CMPIResult * results, /* [out] Results of this operation */
736 const CMPIObjectPath * reference, /* [in] Contains the CIM namespace, classname and desired object path */
737 const char * methodname, /* [in] Name of the method to apply against the reference object */
738 const CMPIArgs * argsin, /* [in] Method input arguments */
739 CMPIArgs * argsout) /* [in] Method output arguments */
740 {
741 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations */
742 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
743 int rc = 0;
744 int argsInCount;
745 _RESOURCES * resources; /* Handle to the list of system resources. */
746 CMPIData argdata;
747 char error_msg[XEN_UTILS_ERROR_BUF_LEN];
749 _SBLIM_ENTER("InvokeMethod");
750 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
751 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
752 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
753 _SBLIM_TRACE(2, ("--- methodname=\"%s\"", methodname));
754 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
756 if (!xen_utils_validate_session(&session)) {
757 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_METHOD_NOT_AVAILABLE, "Unable to connect to xen daemon");
758 goto Exit;
759 }
761 argsInCount = CMGetArgCount(argsin, NULL);
762 _SBLIM_TRACE(2, ("--- # argsin=%d", argsInCount));
764 /* Get a handle to the list of system resources. */
765 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
766 _SBLIM_TRACE(1,("--- _getResources() failed"));
767 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
768 rc = 2;
769 goto Exit;
770 }
772 /* Start/stop the service. */
773 if (strcmp(methodname, "StartService") == 0) {
774 if (!Xen_VirtualSystemManagementService_enableResource(session, resources, NULL)) {
775 _SBLIM_TRACE(1,("--- StartService() failed"));
776 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Method operation failed");
777 rc = 2;
778 goto Exit;
779 }
780 }
781 else if (strcmp(methodname, "StopService") == 0) {
782 if (!Xen_VirtualSystemManagementService_disableResource(session, resources, NULL)) {
783 _SBLIM_TRACE(1,("--- StopService() failed"));
784 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Method operation failed");
785 rc = 2;
786 goto Exit;
787 }
788 }
789 else if (strcmp(methodname, "DefineSystem") == 0) {
790 if (argsInCount != 3) {
791 _SBLIM_TRACE(1,("--- Incorrect number of method arguments"));
792 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Incorrect number of method arguments");
793 rc = 5;
794 goto Exit;
795 }
797 /* Get embedded instance of VirtualSystemSettingData. */
798 argdata = CMGetArgAt(argsin, 0, NULL, &status);
799 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(argdata)) {
800 _SBLIM_TRACE(1,("--- CMGetArgAt(0) failed"));
801 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Cannot get method input argument");
802 rc = 5;
803 goto Exit;
804 }
806 CMPIInstance* vsSettingDataInst;
807 if (argdata.type == CMPI_string)
808 {
809 char *vsSettingData = CMGetCharPtr(argdata.value.string);
810 _SBLIM_TRACE(2,("--- Got string with embedded instance =\"%s\"", vsSettingData));
812 vsSettingDataInst = parse_embedded_instance(vsSettingData);
813 if (vsSettingDataInst == NULL) { /* parser returns zero for success, non-zero for error */
814 _SBLIM_TRACE(1,("--- Error parsing VirtualSystemSettingData: \"%s\"",vsSettingData));
815 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Cannot parse VirtualSystemSettingData");
816 rc = 5;
817 goto Exit;
818 }
819 }
820 else if (argdata.type == CMPI_instance)
821 {
822 vsSettingDataInst = argdata.value.inst;
823 _SBLIM_TRACE(2,("--- Got embedded instance for vsSettingData"));
824 }
825 else
826 {
827 _SBLIM_TRACE(1,("--- Invalid setting type - %s", _CMPITypeName(argdata.type)));
828 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid setting type");
829 rc = 5;
830 goto Exit;
831 }
833 /* Get input array of ResourceAllocationSettingData instances. */
834 argdata = CMGetArgAt(argsin, 1, NULL, &status);
835 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(argdata)) {
836 _SBLIM_TRACE(1,("--- CMGetArgAt(1) failed"));
837 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Cannot get method input argument");
838 rc = 5;
839 goto Exit;
840 }
842 /* Check that the input arg is an array. */
843 if (!CMIsArray(argdata) || CMIsNullObject(argdata.value.array)) {
844 _SBLIM_TRACE(1,("--- Invalid method argument type. Array expected"));
845 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid method argument type. Array expected");
846 rc = 5;
847 goto Exit;
848 }
850 CMPIArray *resourceSettings = argdata.value.array;
851 xen_vm vm;
852 if (!create_vm(vsSettingDataInst, resourceSettings, &vm, &status))
853 {
854 _SBLIM_TRACE(1,("--- create_vm() failed"));
855 /* status set in create_vm */
856 rc = 3;
857 goto Exit;
858 }
860 /* Return the objectpath for the resulting DomU in the output args */
861 CMPIObjectPath * op = CMNewObjectPath(_BROKER, namespace, "Xen_ComputerSystem", NULL);
862 CMAddKey(op, "CreationClassName", (CMPIValue *)"Xen_ComputerSystem", CMPI_chars);
863 char *vm_name;
864 if (!xen_vm_get_name_label(session->xen, &vm_name, vm)) {
865 /* Can't get the name after all of this work! Unwind this mess. */
866 remove_vm(vm);
867 xen_vm_free(vm);
868 _SBLIM_TRACE(1,("--- Unable to retrieve name of new VM"));
869 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to create the virtual system");
870 rc = 3;
871 goto Exit;
872 }
874 CMAddKey(op, "Name", (CMPIValue *)vm_name, CMPI_chars);
875 /*
876 * WARNING: J. Fehlig
877 * What does this do?
878 *
879 * CMSetHostAndNameSpaceFromObjectPath(op, reference);
880 *
881 */
882 _SBLIM_TRACE(2,("--- VirtualSystem=%s", CMGetCharPtr(CDToString(_BROKER, op, NULL))));
883 CMAddArg(argsout, "ResultingSystem", (CMPIValue*)&op, CMPI_ref);
884 xen_vm_free(vm);
886 _SBLIM_TRACE(2, ("--- DefineSystem completed with success"));
887 }
888 else if (strcmp(methodname, "AddResourceSettings") == 0) {
889 /* TODO */
890 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_METHOD_NOT_AVAILABLE, NULL);
891 rc = 1;
892 goto Exit;
893 }
894 else if (strcmp(methodname, "AddResourceSetting") == 0) {
895 if (argsInCount != 2) {
896 _SBLIM_TRACE(1,("--- Incorrect number of method arguments"));
897 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Incorrect number of method arguments");
898 rc = 5;
899 goto Exit;
900 }
902 /* Get vm record based on VirtualSystemSettingData object path. */
903 argdata = CMGetArgAt(argsin, 0, NULL, &status);
904 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(argdata)) {
905 _SBLIM_TRACE(1,("--- CMGetArgAt(0) failed"));
906 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Cannot get method input argument");
907 rc = 5;
908 goto Exit;
909 }
911 if (argdata.type != CMPI_ref) {
912 _SBLIM_TRACE(1,("--- Invalid setting type - %s", _CMPITypeName(argdata.type)));
913 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid setting type");
914 rc = 5;
915 goto Exit;
916 }
918 _SBLIM_TRACE(2, ("--- domain object path=\"%s\"", CMGetCharPtr(CDToString(_BROKER, argdata.value.ref, NULL))));
920 xen_vm_record *vmRec;
921 if (!xen_utils_get_domain_from_sd_OP(session, &vmRec, argdata.value.ref)) {
922 _SBLIM_TRACE(1,("--- Unable to retrieve domain record from object path"));
923 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Unable to retrieve domain record from object path");
924 rc = 5;
925 goto Exit;
926 }
928 /*
929 * Get input ResourceAllocationSettingData instance
930 * that will be added to domain.
931 */
932 argdata = CMGetArgAt(argsin, 1, NULL, &status);
933 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(argdata)) {
934 _SBLIM_TRACE(1,("--- CMGetArgAt(1) failed"));
935 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Cannot get method input argument");
936 xen_vm_record_free(vmRec);
937 rc = 5;
938 goto Exit;
939 }
941 CMPIInstance* newSettingInst;
942 if (argdata.type == CMPI_string){
943 char* newSetting = CMGetCharPtr(argdata.value.string);
944 _SBLIM_TRACE(2,("--- newSetting=\"%s\"", newSetting));
945 newSettingInst = parse_embedded_instance(newSetting);
946 if (newSettingInst == NULL) { /* parser returns zero for success, non-zero for error */
947 _SBLIM_TRACE(1,("--- Error parsing ResourceSettingData: \"%s\"",newSetting));
948 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Cannot parse ResourceSettingData");
949 rc = 5;
950 goto Exit;
951 }
952 }
953 else if (argdata.type == CMPI_instance) {
954 newSettingInst = argdata.value.inst;
955 }
956 else {
957 _SBLIM_TRACE(1,("--- Invalid setting type - %s", _CMPITypeName(argdata.type)));
958 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid setting type");
959 rc = 5;
960 goto Exit;
961 }
964 CMPIObjectPath *resultSetting;
965 if (!add_resource_to_vm(vmRec, newSettingInst, &resultSetting, namespace, &status))
966 {
967 _SBLIM_TRACE(1,("--- add_resource_to_vm() failed"));
968 /* status set in add_resource_to_vm */
969 xen_vm_record_free(vmRec);
970 rc = 3;
971 goto Exit;
972 }
974 CMAddArg(argsout, "ResultingResourceSetting",
975 (CMPIValue*)&resultSetting, CMPI_ref);
976 xen_vm_record_free(vmRec);
978 _SBLIM_TRACE(2, ("--- AddResourceSetting completed with success"));
979 }
980 else if (strcmp(methodname, "ModifyResourceSettings") == 0) {
981 /* TODO */
982 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
983 rc = 1;
984 goto Exit;
985 }
986 else if (strcmp(methodname, "RemoveResourceSettings") == 0) {
987 /* TODO */
988 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
989 rc = 1;
990 goto Exit;
991 }
992 else if (strcmp(methodname, "ModifySystemSettings") == 0) {
993 /* TODO */
994 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
995 rc = 1;
996 goto Exit;
997 }
998 else if (strcmp(methodname, "DestroySystem") == 0) {
999 /* Check that the method has the correct number of arguments. */
1000 if (argsInCount != 1) {
1001 _SBLIM_TRACE(1,("--- Incorrect number of method arguments"));
1002 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Incorrect number of method arguments");
1003 rc = 5;
1004 goto Exit;
1007 /* Get object path of domain */
1008 argdata = CMGetArgAt(argsin, 0, NULL, &status);
1009 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(argdata)) {
1010 _SBLIM_TRACE(1,("--- CMGetArgAt(0) failed"));
1011 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Cannot get method input argument");
1012 rc = 5;
1013 goto Exit;
1016 if ((argdata.type != CMPI_ref) || CMIsNullObject(argdata.value.ref)) {
1017 _SBLIM_TRACE(1,("--- Invalid parameter type - %s", _CMPITypeName(argdata.type)));
1018 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid method input argument");
1019 rc = 5;
1020 goto Exit;
1023 CMPIData namedata = CMGetKey(argdata.value.ref, "Name", &status);
1024 char *domainname = CMGetCharPtr(namedata.value.string);
1025 _SBLIM_TRACE(1,("--- domainname = %s", domainname));
1026 xen_vm_set *vms;
1027 if (!xen_vm_get_by_name_label(session->xen, &vms, domainname)) {
1028 _SBLIM_TRACE(1,("--- Target instance not found"));
1029 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_FOUND, "Target instance not found");
1030 rc = 3;
1031 goto Exit;
1033 assert(vms->size == 1);
1034 xen_vm vm = vms->contents[0];
1035 enum xen_vm_power_state power_state;
1036 if (!xen_vm_get_power_state(session->xen, &power_state, vm)) {
1037 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1038 _SBLIM_TRACE(1,("--- Unable to retrieve power state of vm - error = %s", error_msg));
1039 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Unable to determine power state of virtual system");
1040 rc = 3;
1041 goto Exit;
1044 _SBLIM_TRACE(1,("--- retrieved power state of vm - %", power_state));
1046 if (power_state != XEN_VM_POWER_STATE_HALTED) {
1047 _SBLIM_TRACE(1,("--- calling xen_vm_clean_shutdown"));
1048 if (!xen_vm_clean_shutdown(session->xen, vm)) {
1049 _SBLIM_TRACE(1, ("--- xen_vm_clean_shutdown() failed"));
1050 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to shutdown VirtualSystem");
1051 rc = 3;
1052 goto Exit;
1056 _SBLIM_TRACE(1,("--- calling xen_vm_destroy"));
1057 if (!xen_vm_destroy(session->xen, vm)) {
1058 _SBLIM_TRACE(1,("--- xen_vm_destroy failed"));
1059 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to destroy the virtual system");
1060 rc = 3;
1061 goto Exit;
1065 else {
1066 _SBLIM_TRACE(1,("--- Method \"%s\" is not supported", methodname));
1067 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_METHOD_NOT_AVAILABLE, NULL);
1068 rc = 5;
1069 goto Exit;
1072 Exit:
1074 CMReturnData(results, (CMPIValue *)&rc, CMPI_uint32);
1075 CMReturnDone(results);
1076 _SBLIM_RETURNSTATUS(status);
1080 // ----------------------------------------------------------------------------
1081 // MethodInitialize()
1082 // Perform any necessary initialization immediately after the method provider
1083 // is first loaded.
1084 // ----------------------------------------------------------------------------
1085 static void MethodInitialize(
1086 CMPIMethodMI * self, /* [in] Handle to this provider (i.e. 'self'). */
1087 const CMPIContext * context) /* [in] Additional context info, if any. */
1089 _SBLIM_ENTER("MethodInitialize");
1090 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
1091 // _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
1093 /* Initialized Xen session object. */
1094 if (session == NULL)
1095 xen_utils_xen_init(&session);
1097 _SBLIM_RETURN();
1100 // ============================================================================
1101 // CMPI METHOD PROVIDER FUNCTION TABLE SETUP
1102 // ============================================================================
1103 CMMethodMIStub( , Xen_VirtualSystemManagementService, _BROKER, MethodInitialize(&mi, ctx));
1106 /*
1107 * Wild macro to support adding devices (vbd, vif) to as list of such
1108 * devices. To be used only in create_vm where list of devices is
1109 * collected from the incoming RASDs.
1110 */
1111 #define ADD_DEVICE_TO_LIST(list, device, type) \
1112 { \
1113 if (list == NULL) { \
1114 list = type ## _set_alloc(1); \
1115 if (list == NULL) { \
1116 _SBLIM_TRACE(1, \
1117 ("Cannot malloc memory for xen device settings list")); \
1118 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, \
1119 "Unable to malloc memory"); \
1120 type ## _free(device); \
1121 goto Error; \
1122 } \
1123 list->size = 1; \
1124 list->contents[0] = device; \
1125 } \
1126 /* List is not empty. Grow the list and add the new device */ \
1127 else { \
1128 int new_len = sizeof(type ## _set) + ((list->size + 1) * sizeof(type)); \
1129 list = realloc(list, new_len); \
1130 if (list == NULL) { \
1131 _SBLIM_TRACE(1, \
1132 ("Cannot malloc memory for xen device settings list")); \
1133 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, \
1134 "Unable to malloc memory"); \
1135 type ## _free(device); \
1136 goto Error; \
1137 } \
1138 list->contents[list->size] = device; \
1139 list->size++; \
1140 } \
1144 static int create_vm(CMPIInstance* vsSettingsInst,
1145 CMPIArray *resourceSettings,
1146 xen_vm *result, CMPIStatus *status)
1148 CMPIObjectPath *objectpath;
1149 char *settingclassname;
1150 xen_vm_record *vm_rec;
1151 xen_vdi_record_set *vdis = NULL;
1152 xen_vbd_record_set *vbds = NULL;
1153 xen_vif_record_set *vifs = NULL;
1154 xen_console_record *con_rec = NULL;
1155 xen_vm vm = NULL;
1156 xen_sr_set *srs = NULL;
1157 int i;
1158 int ccode;
1159 char error_msg[XEN_UTILS_ERROR_BUF_LEN];
1161 vm_rec = xen_vm_record_alloc();
1162 if (vm_rec == NULL) {
1163 _SBLIM_TRACE(1,("--- Cannot malloc memory for vm record"));
1164 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, "Unable to malloc memory");
1165 return 0;
1168 objectpath = CMGetObjectPath(vsSettingsInst, NULL);
1169 _SBLIM_TRACE(2,("--- objectpath=%s", CMGetCharPtr(CDToString(_BROKER, objectpath, NULL))));
1171 /* Get the class type of the setting data instance */
1172 settingclassname = CMGetCharPtr(CMGetClassName(objectpath, NULL));
1173 _SBLIM_TRACE(2,("--- settingclassname=%s", settingclassname));
1175 /* Ensure we have a Xen_ComputerSystemSettingData */
1176 if (strcmp(settingclassname,"Xen_ComputerSystemSettingData") != 0) {
1177 _SBLIM_TRACE(1,("--- Unrecognized setting data class - %s", settingclassname));
1178 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Unrecognized setting data class");
1179 return 0;
1182 /* Convert the domain settings to respective xen settings. */
1183 if (!vssd2xenconfig(vsSettingsInst, vm_rec, status)) {
1184 _SBLIM_TRACE(1,("--- failed to convert VSSD to xen_vmrecord"));
1185 xen_vm_record_free(vm_rec);
1186 return 0;
1189 /* Convert resource settings to their respecitve xen settings. */
1190 CMPIInstance *instance;
1191 char *setting;
1192 for (i = 0; i < CMGetArrayCount(resourceSettings, NULL); i++) {
1193 CMPIData settingdata = CMGetArrayElementAt(resourceSettings, i, status);
1194 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(settingdata)) {
1195 _SBLIM_TRACE(1,("--- CMGetArrayElementAt(%d) failed", i));
1196 goto Error;
1199 /* Check that the array element is an (embedded) instance */
1200 if (settingdata.type == CMPI_string) {
1201 setting = CMGetCharPtr(settingdata.value.string);
1202 _SBLIM_TRACE(2,("--- Got string with embedded instance = %s", setting));
1204 instance = parse_embedded_instance(setting);
1205 if (instance == NULL) {
1206 _SBLIM_TRACE(1,("--- unable to parse setting data %s", setting));
1207 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Unable to parse resource setting data parameter");
1208 goto Error;
1211 else if (settingdata.type == CMPI_instance) {
1212 instance = settingdata.value.inst;
1213 _SBLIM_TRACE(2,("--- Got embedded instance for setting"));
1215 else {
1216 _SBLIM_TRACE(1,("--- Invalid setting type - %s", _CMPITypeName(settingdata.type)));
1217 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid resource setting data parameter");
1218 goto Error;
1221 _SBLIM_TRACE(2,("--- instance=%s", CMGetCharPtr(CDToString(_BROKER, instance, NULL))));
1222 objectpath = CMGetObjectPath(instance, NULL);
1223 _SBLIM_TRACE(2,("--- objectpath=%s", CMGetCharPtr(CDToString(_BROKER, objectpath, NULL))));
1225 /* Get the class type of the setting data instance */
1226 settingclassname = CMGetCharPtr(CMGetClassName(objectpath, NULL));
1227 _SBLIM_TRACE(2,("--- settingclassname=%s", settingclassname));
1229 /* Populate config with instance data from the virtual device. */
1230 if (strcmp(settingclassname,"Xen_ProcessorSettingData") == 0) {
1231 _SBLIM_TRACE(2,("--- adding Xen_ProcessorSettingData to configuration"));
1233 if (!proc_rasd2vmconfig(instance, vm_rec, status)) {
1234 _SBLIM_TRACE(1,("--- Error parsing processor settings"));
1235 goto Error;
1238 else if (strcmp(settingclassname,"Xen_MemorySettingData") == 0) {
1239 _SBLIM_TRACE(2,("--- adding Xen_MemorySettingData to configuration"));
1241 /* Fail if duplicate memory rasd's? */
1242 if (vm_rec->memory_dynamic_max > 0) {
1243 _SBLIM_TRACE(1,("--- More than one memroy setting data specified"));
1244 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER,
1245 "More than one memory setting data specified");
1246 goto Error;
1249 if (!mem_rasd2vmconfig(instance, vm_rec, status)) {
1250 _SBLIM_TRACE(1,("--- Error parsing memory settings"));
1251 goto Error;
1254 else if (strcmp(settingclassname,"Xen_DiskSettingData") == 0) {
1255 _SBLIM_TRACE(2,("--- adding Xen_DiskSettingData to configuration"));
1257 xen_vbd_record *vbd_rec;
1258 xen_vdi_record *vdi_rec;
1259 if (!disk_rasd2vmconfig(instance, &vbd_rec, &vdi_rec, status)) {
1260 _SBLIM_TRACE(1,("--- Error parsing disk settings"));
1261 goto Error;
1264 ADD_DEVICE_TO_LIST(vdis, vdi_rec, xen_vdi_record);
1265 ADD_DEVICE_TO_LIST(vbds, vbd_rec, xen_vbd_record);
1267 else if (strcmp(settingclassname,"Xen_NetworkPortSettingData") == 0) {
1268 _SBLIM_TRACE(2,("--- adding Xen_NetworkPortSettingData to configuration"));
1270 xen_vif_record *vif_rec;
1271 if (!nic_rasd2vmconfig(instance, &vif_rec, status)) {
1272 _SBLIM_TRACE(1,("--- Error parsing network port settings"));
1273 goto Error;
1276 ADD_DEVICE_TO_LIST(vifs, vif_rec, xen_vif_record);
1278 else if (strcmp(settingclassname,"Xen_ConsoleSettingData") == 0) {
1279 _SBLIM_TRACE(2,("--- adding Xen_ConsoleSettingData to configuration"));
1281 con_rec = xen_console_record_alloc();
1282 if (con_rec == NULL) {
1283 _SBLIM_TRACE(1,("--- Cannot malloc memory for console record"));
1284 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, "Unable to malloc memory");
1285 goto Error;
1288 if (!con_rasd2vmconfig(instance, con_rec, status)) {
1289 _SBLIM_TRACE(1,("--- Error parsing console settings"));
1290 goto Error;
1293 else {
1294 _SBLIM_TRACE(1,("--- Unrecognized setting data class - %s", settingclassname));
1295 goto Error;
1299 /* We have all of the settings. First create the vm. */
1300 if (!xen_vm_create(session->xen, &vm, vm_rec)) {
1301 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1302 _SBLIM_TRACE(1,("--- xen_vm_create failed: %s", error_msg));
1303 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1304 goto Error;
1307 /* Create devices. Need a Storage Repository (sr) for VDI. */
1308 xen_vm_record_opt vm_record_opt =
1310 .u.handle = vm
1311 };
1313 if (!xen_sr_get_by_name_label(session->xen, &srs, "Local")) {
1314 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1315 _SBLIM_TRACE(1,("--- xen_sr_get_by_name_label failed: %s", error_msg));
1316 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1317 goto Error;
1319 assert(srs->size == 1);
1321 xen_sr_record_opt sr_record =
1323 .u.handle = srs->contents[0]
1324 };
1326 /* Add all of the virutal block devices. */
1327 for (i = 0; i < vdis->size; i++) {
1329 xen_vdi_record *vdi_rec = vdis->contents[i];
1330 vdi_rec->sr = &sr_record;
1331 xen_vdi new_vdi;
1332 ccode = xen_vdi_create(session->xen, &new_vdi, vdi_rec);
1333 /* Set sr field of vdi record to NULL so it is not freed */
1334 vdi_rec->sr = NULL;
1335 if (!ccode)
1337 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1338 _SBLIM_TRACE(1,("--- xen_vdi_create failed: %s", error_msg));
1339 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1340 goto Error;
1343 xen_vdi_record_opt vdi_record_opt =
1345 .u.handle = new_vdi
1346 };
1348 xen_vbd_record *vbd_rec = vbds->contents[i];
1349 vbd_rec->vm = &vm_record_opt;
1350 vbd_rec->vdi = &vdi_record_opt;
1351 xen_vbd new_vbd;
1352 ccode = xen_vbd_create(session->xen, &new_vbd, vbd_rec);
1353 /* Set vm and vdi fields of vbd record to NULL so they are not freed */
1354 vbd_rec->vm = NULL;
1355 vbd_rec->vdi = NULL;
1356 xen_vdi_free(new_vdi);
1357 if (!ccode)
1359 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1360 _SBLIM_TRACE(1,("--- xen_vdb_create failed: %s", error_msg));
1361 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1362 goto Error;
1364 xen_vbd_free(new_vbd);
1367 /* Add all of the virutal network devices. */
1368 for (i = 0; i < vifs->size; i++) {
1369 xen_vif_record *vif_rec = vifs->contents[i];
1370 vif_rec->vm = &vm_record_opt;
1371 xen_vif new_vif;
1372 ccode = xen_vif_create(session->xen, &new_vif, vif_rec);
1373 /* Set vm field of vif record to NULL so it is not freed */
1374 vif_rec->vm = NULL;
1375 if (!ccode)
1377 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1378 _SBLIM_TRACE(1,("--- xen_vif_create failed %s", error_msg));
1379 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1380 goto Error;
1382 xen_vif_free(new_vif);
1385 /* Add console device if specified */
1386 if (con_rec) {
1387 con_rec->vm = &vm_record_opt;
1388 xen_console new_con;
1389 ccode = xen_console_create(session->xen, &new_con, con_rec);
1390 /* Set vm field of console record to NULL so it is not freed */
1391 con_rec->vm = NULL;
1392 if (!ccode)
1394 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1395 _SBLIM_TRACE(1,("--- xen_console_create failed %s", error_msg));
1396 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1397 goto Error;
1399 xen_console_free(new_con);
1402 xen_console_record_free(con_rec);
1403 xen_vif_record_set_free(vifs);
1404 xen_vbd_record_set_free(vbds);
1405 xen_vdi_record_set_free(vdis);
1406 xen_sr_set_free(srs);
1407 xen_vm_record_free(vm_rec);
1409 *result = vm;
1411 return 1;
1413 /* Unwind if we failed.
1414 * remove_vm will nuke any vifs / vbds created and the vm.
1415 */
1416 Error:
1417 if (vm) {
1418 remove_vm(vm);
1419 xen_vm_free(vm);
1421 xen_console_record_free(con_rec);
1422 xen_vif_record_set_free(vifs);
1423 xen_vbd_record_set_free(vbds);
1424 xen_vdi_record_set_free(vdis);
1425 xen_sr_set_free(srs);
1426 xen_vm_record_free(vm_rec);
1427 *result = NULL;
1429 return 0;
1433 static int add_resource_to_vm(xen_vm_record *vmRec,
1434 CMPIInstance *sourceSettingInst,
1435 CMPIObjectPath **resultSetting,
1436 char *namespace,
1437 CMPIStatus *status)
1439 CMPIObjectPath *op;
1440 char *settingclassname;
1441 CMPIData propertyvalue;
1442 int ccode;
1443 char instId[MAX_INSTANCEID_LEN];
1444 char error_msg[XEN_UTILS_ERROR_BUF_LEN];
1445 xen_vm_record_opt vm_record_opt =
1447 .u.handle = vmRec->handle
1449 };
1451 _SBLIM_TRACE(2,("--- instance=%s", CMGetCharPtr(CDToString(_BROKER, sourceSettingInst, NULL))));
1452 op = CMGetObjectPath(sourceSettingInst, NULL);
1453 _SBLIM_TRACE(2,("--- objectpath=%s", CMGetCharPtr(CDToString(_BROKER, op, NULL))));
1455 /* Get the class type of the setting data instance */
1456 settingclassname = CMGetCharPtr(CMGetClassName(op, NULL));
1457 _SBLIM_TRACE(2,("--- settingclassname=%s", settingclassname));
1459 /* Add the resource to the domain. */
1460 if (strcmp(settingclassname,"Xen_ProcessorSettingData") == 0) {
1461 #if 1
1462 _SBLIM_TRACE(1,("--- Adding processors not currently supported"));
1463 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, "Adding processors not currently supported");
1464 return 0;
1466 #else
1467 /* Code for adding processors -- when Xen API supports it. */
1468 _SBLIM_TRACE(2,("--- adding Xen_ProcessorSettingData to configuration"));
1469 propertyvalue = CMGetProperty(sourceSettingInst, "VirtualQuantity", status);
1470 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1471 _SBLIM_TRACE(1,("--- Unable to determine VirtualQuantity in Processor RASD"));
1472 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "VirtualQuantity not specified in ResourceSetting");
1473 return 0;
1476 /* Adding cpus means just that - adding to existing amount */
1477 uint64_t cpus = propertyvalue.value.uint64 + vmRec->vcpus_number;
1478 /*
1479 * TODO:
1480 * 1. CPU weights and other scheduling parameters?
1481 */
1482 if (!xen_vm_set_vcpus_number(session->xen,
1483 vmRec->handle,
1484 cpus)) {
1485 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1486 _SBLIM_TRACE(1,("--- xen_vm_set_vcpus_number failed: %s", error_msg));
1487 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1488 return 0;
1491 /* Create new object path for setting */
1492 *resultSetting = CMNewObjectPath(_BROKER, namespace, "Xen_ProcessorSettingData", NULL);
1493 snprintf(instId, MAX_INSTANCEID_LEN, "Xen:%s:Processor", vmRec->name_label);
1494 CMAddKey(*resultSetting, "InstanceID", (CMPIValue *)instId, CMPI_chars);
1495 #endif
1497 else if (strcmp(settingclassname,"Xen_MemorySettingData") == 0) {
1498 #if 1
1499 _SBLIM_TRACE(1,("--- Adding memory not currently supported"));
1500 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, "Adding memory not currently supported");
1501 return 0;
1503 #else
1504 /* Code for adding memory -- when Xen API supports it. */
1505 _SBLIM_TRACE(2,("--- adding Xen_MemorySettingData to configuration"));
1506 propertyvalue = CMGetProperty(sourceSettingInst, "VirtualQuantity", status);
1507 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1508 _SBLIM_TRACE(1,("--- Unable to determine VirtualQuantity in Memory RASD"));
1509 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "VirtualQuantity not specified in ResourceSetting");
1510 return 0;
1513 /* Adding memory means just that - adding to existing amount */
1514 uint64_t mem = propertyvalue.value.uint64 + vmRec->memory_dynamic_max;
1515 if (!xen_vm_set_memory_dynamic_max(session->xen,
1516 vmRec->handle,
1517 mem)) {
1518 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1519 _SBLIM_TRACE(1,("--- xen_vm_set_memory_dynamic_max failed: %s", error_msg));
1520 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1521 return 0;
1524 /* Add object path to output array */
1525 *resultSetting = CMNewObjectPath(_BROKER, namespace, "Xen_MemorySettingData", NULL);
1526 snprintf(instId, MAX_INSTANCEID_LEN, "Xen:%s:Memory1", vmRec->name_label);
1527 CMAddKey(*resultSetting, "InstanceID", (CMPIValue *)instId, CMPI_chars);
1528 #endif
1530 else if (strcmp(settingclassname,"Xen_DiskSettingData") == 0) {
1531 _SBLIM_TRACE(2,("--- adding Xen_DiskSettingData to configuration"));
1533 xen_vbd_record *vbd_rec;
1534 xen_vdi_record *vdi_rec;
1535 if (!disk_rasd2vmconfig(sourceSettingInst, &vbd_rec, &vdi_rec, status)) {
1536 /* status set in disk_rasd2vmconfig */
1537 _SBLIM_TRACE(1,("--- Error parsing disk settings"));
1538 return 0;
1541 xen_sr_set *srs;
1542 if (!xen_sr_get_by_name_label(session->xen, &srs, "Local")) {
1543 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1544 _SBLIM_TRACE(1,("--- xen_sr_get_by_name_label failed: %s", error_msg));
1545 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1546 xen_vbd_record_free(vbd_rec);
1547 xen_vdi_record_free(vdi_rec);
1548 return 0;
1550 assert(srs->size == 1);
1552 xen_sr_record_opt sr_record =
1554 .u.handle = srs->contents[0]
1555 };
1557 vdi_rec->sr = &sr_record;
1558 xen_vdi new_vdi;
1559 ccode = xen_vdi_create(session->xen, &new_vdi, vdi_rec);
1560 /* Set sr field of vdi record to NULL so it is not freed */
1561 vdi_rec->sr = NULL;
1562 if (!ccode)
1564 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1565 _SBLIM_TRACE(1,("--- xen_vdi_create failed: %s", error_msg));
1566 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1567 xen_sr_set_free(srs);
1568 xen_vbd_record_free(vbd_rec);
1569 xen_vdi_record_free(vdi_rec);
1570 return 0;
1573 xen_vdi_record_opt vdi_record_opt =
1575 .u.handle = new_vdi
1576 };
1578 vbd_rec->vm = &vm_record_opt;
1579 vbd_rec->vdi = &vdi_record_opt;
1580 xen_vbd new_vbd;
1581 ccode = xen_vbd_create(session->xen, &new_vbd, vbd_rec);
1582 /* Set vm and vdi fields of vbd record to NULL so they are not freed */
1583 vbd_rec->vm = NULL;
1584 vbd_rec->vdi = NULL;
1585 /* Free allocated objects. */
1586 xen_vdi_free(new_vdi);
1587 xen_sr_set_free(srs);
1588 xen_vbd_record_free(vbd_rec);
1589 xen_vdi_record_free(vdi_rec);
1590 if (!ccode)
1592 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1593 _SBLIM_TRACE(1,("--- xen_vdb_create failed: %s", error_msg));
1594 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1595 return 0;
1597 xen_vbd_free(new_vbd);
1599 /* Add object path to output array */
1600 *resultSetting = CMNewObjectPath(_BROKER, namespace, "Xen_DiskSettingData", NULL);
1601 snprintf(instId, MAX_INSTANCEID_LEN, "Xen:%s:Disk", vmRec->name_label);
1602 CMAddKey(*resultSetting, "InstanceID", (CMPIValue *)instId, CMPI_chars);
1604 else if (strcmp(settingclassname,"Xen_NetworkPortSettingData") == 0) {
1605 _SBLIM_TRACE(2,("--- adding Xen_NetworkPortSettingData to configuration"));
1606 xen_vif_record *vif_rec;
1607 if (!nic_rasd2vmconfig(sourceSettingInst, &vif_rec, status)) {
1608 /* status set in nic_rasd2vmconfig */
1609 _SBLIM_TRACE(1,("--- Error parsing network port settings"));
1610 return 0;
1613 vif_rec->vm = &vm_record_opt;
1614 xen_vif new_vif;
1615 ccode = xen_vif_create(session->xen, &new_vif, vif_rec);
1616 /* Set vm field of vif record to NULL so it is not freed */
1617 vif_rec->vm = NULL;
1618 xen_vif_record_free(vif_rec);
1619 if (!ccode)
1621 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1622 _SBLIM_TRACE(1,("--- xen_vif_create failed %s", error_msg));
1623 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1624 return 0;
1626 xen_vif_free(new_vif);
1628 /* Add object path to output array */
1629 *resultSetting = CMNewObjectPath(_BROKER, namespace, "Xen_NetworkPortSettingData", NULL);
1630 snprintf(instId, MAX_INSTANCEID_LEN, "Xen:%s:Vif", vmRec->name_label);
1631 CMAddKey(*resultSetting, "InstanceID", (CMPIValue *)instId, CMPI_chars);
1633 else {
1634 _SBLIM_TRACE(1,("--- Unrecognized setting data class - %s", settingclassname));
1635 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Unrecognized ResourceSetting");
1636 return 0;
1639 return 1;
1643 static void remove_vm(xen_vm vm)
1645 /*
1646 * TODO:
1647 * We want to destroy the VM, so reset any previous failures on the
1648 * session object. But do we want to be touching internal fields in
1649 * this manner? Revisit once we move to the new resource abstraction
1650 * interface.
1651 */
1652 session->xen->ok = true;
1653 xen_vm_destroy(session->xen, vm);
1657 static int vssd2xenconfig(CMPIInstance *vssd, xen_vm_record *vm_rec, CMPIStatus *status)
1659 CMPIData propertyvalue;
1660 char *vsType;
1661 const char *plat_val;
1663 /*
1664 * Get domain name.
1665 * WARNING!
1666 * Do we fail if VirtualSystemIdentifier is not specified?
1667 */
1668 propertyvalue = CMGetProperty(vssd, "VirtualSystemIdentifier", status);
1669 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1670 _SBLIM_TRACE(1,("--- failed to retrieve VirtualSystemIndentifier property value"));
1671 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Unable to retrieve VirtualSystemIdentifier (name) property from virtual system setting data");
1672 return 0;
1674 vm_rec->name_label = strdup(CMGetCharPtr(propertyvalue.value.string));
1676 /* Get UUID, if specified */
1677 propertyvalue = CMGetProperty(vssd, "UUID", status);
1678 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue))
1679 vm_rec->uuid = strdup(CMGetCharPtr(propertyvalue.value.string));
1681 propertyvalue = CMGetProperty(vssd, "localtime", status);
1682 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1683 if (propertyvalue.value.boolean)
1684 plat_val = "1";
1685 else
1686 plat_val = "0";
1688 if (!xen_utils_add_to_string_string_map("localtime", plat_val, &(vm_rec->platform))) {
1689 _SBLIM_TRACE(1,
1690 ("Cannot malloc memory for xend platform settings list"));
1691 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM,
1692 "Unable to malloc memory");
1693 return 0;
1697 propertyvalue = CMGetProperty(vssd, "OnPoweroff", status);
1698 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue))
1699 vm_rec->actions_after_shutdown = propertyvalue.value.uint16;
1701 propertyvalue = CMGetProperty(vssd, "OnReboot", status);
1702 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue))
1703 vm_rec->actions_after_reboot = propertyvalue.value.uint16;
1705 propertyvalue = CMGetProperty(vssd, "OnCrash", status);
1706 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue))
1707 vm_rec->actions_after_crash = propertyvalue.value.uint16;
1709 /* Paravirtual or HVM domain? */
1710 propertyvalue = CMGetProperty(vssd, "VirtualSystemType", status);
1711 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1712 _SBLIM_TRACE(1,("--- VirtualSystemType not specified"));
1713 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER,
1714 "VirtualSystemType not specified");
1715 return 0;
1718 vsType = CMGetCharPtr(propertyvalue.value.string);
1719 if (vsType == NULL) {
1720 _SBLIM_TRACE(1,("--- VirtualSystemType not specified"));
1721 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER,
1722 "VirtualSystemType not specified");
1723 return 0;
1726 if (strstr(vsType, "xen")) {
1728 propertyvalue = CMGetProperty(vssd, "Bootloader", status);
1729 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1730 vm_rec->pv_bootloader = strdup(CMGetCharPtr(propertyvalue.value.string));
1732 /* If Bootloader specified, get options. */
1733 /* WARNING!
1734 * Should we ensure that BootloaderOptions is specified if
1735 * Bootloader is specified? For domUloader probably but
1736 * pygrub should work with no options.
1737 */
1738 propertyvalue = CMGetProperty(vssd, "BootloaderOptions", status);
1739 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1740 vm_rec->pv_bootloader_args = strdup(CMGetCharPtr(propertyvalue.value.string));
1744 /* Only honor Kernel if Bootloader not specified. */
1745 propertyvalue = CMGetProperty(vssd, "Kernel", status);
1746 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue) &&
1747 vm_rec->pv_bootloader == NULL) {
1748 vm_rec->pv_kernel = strdup(CMGetCharPtr(propertyvalue.value.string));
1750 /* If Kernel specified, get RAMDisk. */
1751 /* WARNING!
1752 * Should we ensure that RAMDisk is specified if
1753 * Kernel is specified? For Unix'ish guests we probably need
1754 * RAMDisk but what about OS's with no notion of RAMDisk?
1755 */
1756 propertyvalue = CMGetProperty(vssd, "RAMDisk", status);
1757 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1758 vm_rec->pv_ramdisk = strdup(CMGetCharPtr(propertyvalue.value.string));
1762 propertyvalue = CMGetProperty(vssd, "KernelOptions", status);
1763 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1764 vm_rec->pv_args = strdup(CMGetCharPtr(propertyvalue.value.string));
1767 else if (strstr(vsType, "hvm")) {
1768 propertyvalue = CMGetProperty(vssd, "BootOrder", status);
1769 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1770 _SBLIM_TRACE(1,("--- No BootOrder specified for HVM guest"));
1771 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER,
1772 "No BootOrder specified for HVM guest");
1773 return 0;
1775 //vm_rec->hvm_boot_params = strdup(CMGetCharPtr(propertyvalue.value.string));
1776 xen_utils_add_to_string_string_map("order", CMGetCharPtr(propertyvalue.value.string),&(vm_rec->hvm_boot_params ));
1777 /* Default stdvga to 0 */
1778 plat_val = "0";
1779 propertyvalue = CMGetProperty(vssd, "stdvga", status);
1780 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1781 if (propertyvalue.value.boolean)
1782 plat_val = "1";
1783 else
1784 plat_val = "0";
1786 if (!xen_utils_add_to_string_string_map("stdvga", plat_val, &(vm_rec->platform))) {
1787 _SBLIM_TRACE(1,
1788 ("Cannot malloc memory for xend platform settings list"));
1789 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM,
1790 "Unable to malloc memory");
1791 return 0;
1794 propertyvalue = CMGetProperty(vssd, "apic", status);
1795 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1796 if (propertyvalue.value.boolean)
1797 plat_val = "1";
1798 else
1799 plat_val = "0";
1801 if (!xen_utils_add_to_string_string_map("apic", plat_val, &(vm_rec->platform))) {
1802 _SBLIM_TRACE(1,
1803 ("Cannot malloc memory for xend platform settings list"));
1804 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM,
1805 "Unable to malloc memory");
1806 return 0;
1810 propertyvalue = CMGetProperty(vssd, "acpi", status);
1811 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1812 if (propertyvalue.value.boolean)
1813 plat_val = "1";
1814 else
1815 plat_val = "0";
1817 if (!xen_utils_add_to_string_string_map("acpi", plat_val, &(vm_rec->platform))) {
1818 _SBLIM_TRACE(1,
1819 ("Cannot malloc memory for xend platform settings list"));
1820 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM,
1821 "Unable to malloc memory");
1822 return 0;
1826 propertyvalue = CMGetProperty(vssd, "pae", status);
1827 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1828 if (propertyvalue.value.boolean)
1829 plat_val = "1";
1830 else
1831 plat_val = "0";
1833 if (!xen_utils_add_to_string_string_map("pae", plat_val, &(vm_rec->platform))) {
1834 _SBLIM_TRACE(1,
1835 ("Cannot malloc memory for xend platform settings list"));
1836 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM,
1837 "Unable to malloc memory");
1838 return 0;
1842 propertyvalue = CMGetProperty(vssd, "usb", status);
1843 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1844 if (propertyvalue.value.boolean)
1845 plat_val = "1";
1846 else
1847 plat_val = "0";
1849 if (!xen_utils_add_to_string_string_map("usb", plat_val, &(vm_rec->platform))) {
1850 _SBLIM_TRACE(1,
1851 ("Cannot malloc memory for xend platform settings list"));
1852 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM,
1853 "Unable to malloc memory");
1854 return 0;
1858 propertyvalue = CMGetProperty(vssd, "usbdevice", status);
1859 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1860 plat_val = CMGetCharPtr(propertyvalue.value.string);
1862 if (!xen_utils_add_to_string_string_map("usbdevice", plat_val, &(vm_rec->platform))) {
1863 _SBLIM_TRACE(1,
1864 ("Cannot malloc memory for xend platform settings list"));
1865 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM,
1866 "Unable to malloc memory");
1867 return 0;
1871 else {
1872 _SBLIM_TRACE(1,("--- Invalid VirtualSystemType %s specified", vsType));
1873 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER,
1874 "Invalid VirtualSystemType specified");
1875 return 0;
1878 return 1;
1882 static int proc_rasd2vmconfig(CMPIInstance *proc_rasd, xen_vm_record *vm_rec,
1883 CMPIStatus *status)
1885 CMPIData propertyvalue;
1887 propertyvalue = CMGetProperty(proc_rasd, "VirtualQuantity", status);
1888 /* TODO:
1889 * How is vcpus_max represented in RASD? For now use VirtualQuantity.
1890 */
1891 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1892 vm_rec->vcpus_at_startup = propertyvalue.value.uint64;
1893 vm_rec->vcpus_max = propertyvalue.value.uint64;
1895 else {
1896 /*
1897 * Default to 1 vcpu if VirtualQuantity not specified.
1898 * Allocation capabilities should describe this default behavior.
1899 */
1900 vm_rec->vcpus_at_startup = 1;
1901 vm_rec->vcpus_max = 1;
1904 char buf[64];
1905 propertyvalue = CMGetProperty(proc_rasd, "Limit", status);
1906 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1907 snprintf(buf, 64, "%lld", propertyvalue.value.uint64);
1908 xen_utils_add_to_string_string_map("cap", buf, &(vm_rec->vcpus_params));
1910 propertyvalue = CMGetProperty(proc_rasd, "Weight", status);
1911 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1912 snprintf(buf, 64, "%d", propertyvalue.value.uint32);
1913 xen_utils_add_to_string_string_map("weight", buf, &(vm_rec->vcpus_params));
1916 return 1;
1920 static int mem_rasd2vmconfig(CMPIInstance *mem_rasd, xen_vm_record *vm_rec,
1921 CMPIStatus *status)
1923 CMPIData propertyvalue;
1925 propertyvalue = CMGetProperty(mem_rasd, "AllocationUnits", status);
1926 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1927 return 0;
1930 char *units = CMGetCharPtr(propertyvalue.value.string);
1932 propertyvalue = CMGetProperty(mem_rasd, "VirtualQuantity", status);
1933 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1934 return 0;
1937 uint64_t mem_size = propertyvalue.value.uint64;
1938 if (strstr(units, "Mega") || strstr(units, "M"))
1939 mem_size = mem_size * 1024 * 1024;
1941 /*
1942 * TODO:
1943 * 1. Handle defaults if memory is not specified.
1944 */
1945 vm_rec->memory_static_max = mem_size;
1946 vm_rec->memory_dynamic_max = mem_size;
1947 vm_rec->memory_dynamic_min = mem_size;
1949 return 1;
1953 static int disk_rasd2vmconfig(CMPIInstance *disk_rasd, xen_vbd_record **vbd_rec,
1954 xen_vdi_record **vdi_rec, CMPIStatus *status)
1956 CMPIData propertyvalue;
1957 char *tmp_str = NULL;
1958 xen_string_string_map *vdi_params = NULL;
1960 propertyvalue = CMGetProperty(disk_rasd, "DiskConfigInfo", status);
1961 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1962 *vbd_rec = xen_vbd_record_alloc();
1963 if (*vbd_rec == NULL) {
1964 _SBLIM_TRACE(1,("--- Cannot malloc memory for virtual block device"));
1965 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, "Unable to malloc memory");
1966 return 0;
1969 *vdi_rec = xen_vdi_record_alloc();
1970 if (*vdi_rec == NULL) {
1971 _SBLIM_TRACE(1,("--- Cannot malloc memory for virtual disk image"));
1972 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, "Unable to malloc memory");
1973 goto Error;
1976 /* Extract the image, dev and mode params from the DiskConfigInfo string */
1977 char *tok;
1978 char *next_tok;
1979 tmp_str = strdup(CMGetCharPtr(propertyvalue.value.string));
1980 tok = strtok_r(tmp_str, ",", &next_tok);
1981 if (tok == NULL) {
1982 _SBLIM_TRACE(1,("--- Malformed DiskConfigInfo property in disk setting data"));
1983 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid disk setting data");
1984 goto Error;
1987 vdi_params = xen_string_string_map_alloc(1);
1988 if (vdi_params == NULL) {
1989 _SBLIM_TRACE(1,("--- Cannot malloc memory for VDI options"));
1990 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, "Unable to malloc memory");
1991 goto Error;
1994 vdi_params->contents[0].key = strdup("location");
1995 vdi_params->contents[0].val = strdup(tok);
1996 (*vdi_rec)->other_config = vdi_params;
1998 /*
1999 * TODO:
2000 * Need to handle specifying some of the VDI fields. For now
2001 * we'll hardcode to something reasonable.
2002 */
2003 (*vdi_rec)->type = XEN_VDI_TYPE_SYSTEM;
2004 (*vdi_rec)->sharable = false;
2006 if ((tok = strtok_r(NULL, ",", &next_tok)) == NULL) {
2007 _SBLIM_TRACE(1,("--- Malformed DiskConfigInfo property in disk setting data"));
2008 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid disk setting data");
2009 goto Error;
2011 (*vbd_rec)->device = strdup(tok);
2013 if ((tok = strtok_r(NULL, ",", &next_tok)) == NULL) {
2014 _SBLIM_TRACE(1,("--- Malformed DiskConfigInfo property in disk setting data"));
2015 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid disk setting data");
2016 goto Error;
2018 if (strcmp(tok, "w") == 0) {
2019 (*vbd_rec)->mode = XEN_VBD_MODE_RW;
2020 (*vdi_rec)->read_only = false;
2022 else {
2023 (*vbd_rec)->mode = XEN_VBD_MODE_RO;
2024 (*vdi_rec)->read_only = true;
2027 /*
2028 * TODO:
2029 * 1. Handle specifying type. For now just say its a disk.
2030 * 2. What does it mean for a disk to be bootable? Particularly for a
2031 * paravirtualized domain? For now we'll set all disks created
2032 * through CIM to bootable.
2033 */
2034 (*vbd_rec)->type = XEN_VBD_TYPE_DISK;
2035 (*vbd_rec)->bootable = true;
2037 free(tmp_str);
2038 _SBLIM_TRACE(2,("--- uname = %s", vdi_params->contents[0].val));
2039 _SBLIM_TRACE(2,("--- dev = %s", (*vbd_rec)->device));
2040 _SBLIM_TRACE(2,("--- mode = %s",
2041 (*vbd_rec)->mode == XEN_VBD_MODE_RW ? "RW" : "RO"));
2043 return 1;
2046 Error:
2047 free(tmp_str);
2048 /* frees fields as well */
2049 xen_vbd_record_free(*vbd_rec);
2050 *vbd_rec = NULL;
2051 xen_vdi_record_free(*vdi_rec);
2052 *vdi_rec = NULL;
2054 return 0;
2058 static int nic_rasd2vmconfig(CMPIInstance *nic_rasd, xen_vif_record **vif_rec,
2059 CMPIStatus *status)
2061 CMPIData propertyvalue;
2063 /* Set the domain config data from the Xen_NetworkPortSettingData. */
2064 propertyvalue = CMGetProperty(nic_rasd, "NICConfigInfo", status);
2065 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
2066 _SBLIM_TRACE(1,("--- No NICConfigInfo specified in network port setting data"));
2067 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "No NICConfigInfo specified in network port setting data");
2069 *vif_rec = NULL;
2070 return 0;
2073 *vif_rec = xen_vif_record_alloc();
2074 if (*vif_rec == NULL) {
2075 _SBLIM_TRACE(1,("--- Cannot malloc memory for virtual network device"));
2076 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, "Unable to malloc memory");
2077 return 0;
2080 /* Extract the type, mac and network params from the NicConfigInfo
2081 * string. Expected syntax is "key=value,key=value" */
2082 char * next_tok;
2083 char * string = strdup(CMGetCharPtr(propertyvalue.value.string));
2084 char *tok = strtok_r(string, ",", &next_tok);
2085 while (tok) {
2086 if (strncmp(tok, "mac", 3) == 0)
2087 (*vif_rec)->mac = strdup(tok + 4);
2088 /* else if (strncmp(tok, "model", 5) == 0)
2089 (*vif_rec)->model = strdup(tok + 6); Commented off to compile the code*/
2091 tok = strtok_r(NULL, ",", &next_tok);
2094 free(string);
2096 return 1;
2100 static int con_rasd2vmconfig(CMPIInstance *con_rasd, xen_console_record *con_rec,
2101 CMPIStatus *status)
2103 CMPIStatus local_status = {CMPI_RC_OK, NULL};
2104 CMPIData propertyvalue;
2106 propertyvalue = CMGetProperty(con_rasd, "Protocol", &local_status);
2107 if ((local_status.rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
2108 _SBLIM_TRACE(1,("--- No protocol field specified in console setting data"));
2109 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "No protocol specified in console setting data");
2110 return 0;
2113 switch (propertyvalue.value.uint16) {
2114 case 0:
2115 con_rec->protocol = XEN_CONSOLE_PROTOCOL_VT100;
2116 break;
2117 case 1:
2118 con_rec->protocol = XEN_CONSOLE_PROTOCOL_RFB;
2119 break;
2120 case 2:
2121 con_rec->protocol = XEN_CONSOLE_PROTOCOL_RDP;
2122 break;
2123 default:
2124 _SBLIM_TRACE(1,("--- Invalid protocol specified in console setting data"));
2125 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid protocol specified in console setting data");
2126 return 0;
2129 /*
2130 * Get any additional config from ConsoleConfigInfo.
2131 * Expected syntax is "key=value,key=value"
2132 */
2133 propertyvalue = CMGetProperty(con_rasd, "ConsoleConfigInfo", &local_status);
2134 if ((local_status.rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
2135 /* Count number of config items */
2136 int num_items = 0;
2137 char *next_tok;
2138 char *string = strdup(CMGetCharPtr(propertyvalue.value.string));
2139 char *tok = strtok_r(string, ",", &next_tok);
2140 while (tok) {
2141 num_items++;
2142 tok = strtok_r(NULL, ",", &next_tok);
2144 free(string);
2146 xen_string_string_map *con_params = xen_string_string_map_alloc(num_items);
2147 if (con_params == NULL) {
2148 _SBLIM_TRACE(1,("--- Cannot malloc memory for console options"));
2149 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, "Unable to malloc memory");
2150 return 0;
2153 /*
2154 * Go back through the options and populate the string map.
2155 */
2156 string = strdup(CMGetCharPtr(propertyvalue.value.string));
2157 tok = strtok_r(string, ",", &next_tok);
2158 /* If tok is NULL, then string contains only 1 key/value pair */
2159 if (tok == NULL)
2160 tok = string;
2161 int i = 0;
2162 while (tok) {
2163 char *val = strchr(tok, '=');
2164 if (val == NULL) {
2165 _SBLIM_TRACE(1,("--- Invalid console option specified in console setting data"));
2166 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid console option specified in console setting data");
2167 xen_string_string_map_free(con_params);
2168 free(string);
2169 return 0;
2171 *val = '\0';
2172 val++;
2173 con_params->contents[i].key = strdup(tok);
2174 con_params->contents[i].val = strdup(val);
2175 i++;
2176 tok = strtok_r(NULL, ",", &next_tok);
2179 con_rec->other_config = con_params;
2180 free(string);
2183 return 1;
2188 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2189 // THE FOLLOWING CODE IS REQUIRED UNTIL EMBEDDEDOBJECT SUPPORT IS WORKING!!!
2190 //
2191 static CMPIInstance *parse_embedded_instance(char *instanceStr)
2193 char filename[L_tmpnam];
2194 FILE *fd;
2195 int rc;
2196 CMPIInstance *instance;
2198 _SBLIM_TRACE(2,("--- parsing instance: \"%s\"", instanceStr));
2200 /* Store the embedded Xen_*SettingData string data in a file for parsing */
2201 tmpnam(filename);
2202 fd = fopen(filename, "w");
2203 fprintf(fd, "%s", instanceStr);
2204 fclose(fd);
2206 fd = fopen(filename, "r");
2207 Xen_SettingDatayyrestart(fd);
2209 /* Parse the embedded Xen_*SettingData string data into a CMPIInstance */
2210 rc = Xen_SettingDatayyparseinstance(_BROKER, &instance);
2211 fclose(fd);
2212 remove(filename);
2213 if (rc != 0) { /* parser returns zero for success, non-zero for error */
2214 _SBLIM_TRACE(1,("--- error parsing instance"));
2215 return NULL;
2218 _SBLIM_TRACE(2,("--- parsed instance: \"%s\"",
2219 CMGetCharPtr(CDToString(_BROKER, instance, NULL))));
2220 return instance;