os-cmpi-xen

view src/Xen_VirtualSystemManagementService.c @ 89:93600f4355d8

Added support for Xen_ConsoleSettingData.ConsoleConfigInfo
in Xen_VirtualSystemManagementService.DefineSystem(). This
allows specifying additional console configuration such as
vncunused=1, vncpasswd=foobar, etc.

Signed-off-by: Jim Fehlig <jfehlig@novell.com>
author Jim Fehlig <jfehlig@novell.com>
date Wed Feb 28 16:28:14 2007 -0700 (2007-02-28)
parents 9a96ebba2b60
children 66600aae1e53
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>
37 #include <xen_string_string_map.h>
39 /* Include utility functions */
40 #include "cmpiutil.h"
42 /* Include _SBLIM_TRACE() logging support */
43 #include "cmpitrace.h"
45 /* Include Xen utilities */
46 #include "xen_utils.h"
48 /* Include the abstract resource access functions and abstracted _RESOURCES and _RESOURCE data types. */
49 #include "Xen_VirtualSystemManagementService_Resource.h"
51 /* Include the required CMPI data types, function headers, and macros */
52 #include "cmpidt.h"
53 #include "cmpift.h"
54 #include "cmpimacs.h"
56 #include "provider_common.h"
57 #include "xen_utils.h"
60 // ----------------------------------------------------------------------------
61 // COMMON GLOBAL VARIABLES
62 // ----------------------------------------------------------------------------
64 /* Handle to the CIM broker. Initialized when the provider lib is loaded. */
65 static const CMPIBroker *_BROKER;
67 /* Xen session object. Initialize when the provider is loaded, close when
68 * provider unloaded. */
69 static xen_utils_session *session = NULL;
72 // ============================================================================
73 // CMPI INSTANCE PROVIDER FUNCTION TABLE
74 // ============================================================================
76 // ----------------------------------------------------------------------------
77 // Info for the class supported by the instance provider
78 // ----------------------------------------------------------------------------
80 /* Name of the class implemented by this instance provider. */
81 /*** CUSTOMIZE FOR EACH PROVIDER ***/
82 static char * _CLASSNAME = "Xen_VirtualSystemManagementService";
84 /* NULL terminated list of key properties of this class. */
85 /*** CUSTOMIZE FOR EACH PROVIDER ***/
86 const static char * _KEYNAMES[] = {"SystemName", "SystemCreationClassName", "Name", "CreationClassName", NULL};
89 static int create_vm(char *vsSettings, CMPIArray *resourceSettings,
90 xen_vm *result, CMPIStatus *status);
91 static int add_resource_to_vm(xen_vm_record *vmRec,
92 char *sourceSetting,
93 CMPIObjectPath **resultSetting,
94 char *namespace,
95 CMPIStatus *status);
96 static void remove_vm(xen_vm vm);
97 static int vssd2xenconfig(CMPIInstance *vssd, xen_vm_record *vm_rec, CMPIStatus *status);
98 static int proc_rasd2vmconfig(CMPIInstance *proc_rasd, xen_vm_record *vm_rec,
99 CMPIStatus *status);
100 static int mem_rasd2vmconfig(CMPIInstance *instance, xen_vm_record *vm_rec,
101 CMPIStatus *status);
102 static int disk_rasd2vmconfig(CMPIInstance *instance, xen_vbd_record **vbd_rec,
103 CMPIStatus *status);
104 static int nic_rasd2vmconfig(CMPIInstance *instance, xen_vif_record **vif_rec,
105 CMPIStatus *status);
106 static int con_rasd2vmconfig(CMPIInstance *instance, xen_console_record *con_rec,
107 CMPIStatus *status);
108 static CMPIInstance *parse_embedded_instance(char *instanceStr);
112 // ----------------------------------------------------------------------------
113 // Cleanup()
114 // Perform any necessary cleanup immediately before this provider is unloaded.
115 // ----------------------------------------------------------------------------
116 static CMPIStatus Cleanup(
117 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
118 const CMPIContext * context, /* [in] Additional context info, if any. */
119 CMPIBoolean terminating) /* [in] True if MB is terminating */
120 {
121 CMPIStatus status = { CMPI_RC_OK, NULL }; /* Return status of CIM operations. */
123 _SBLIM_ENTER("Cleanup");
124 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
125 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
127 if (session) {
128 xen_utils_xen_close(session);
129 session = NULL;
130 }
131 _SBLIM_RETURNSTATUS(status);
132 }
134 // ----------------------------------------------------------------------------
135 // EnumInstanceNames()
136 // Return a list of all the instances names (return their object paths only).
137 // ----------------------------------------------------------------------------
138 static CMPIStatus EnumInstanceNames(
139 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
140 const CMPIContext * context, /* [in] Additional context info, if any. */
141 const CMPIResult * results, /* [out] Results of this operation. */
142 const CMPIObjectPath * reference) /* [in] Contains target namespace and classname. */
143 {
144 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
145 _RESOURCES * resources = NULL; /* Handle to the list of system resources. */
146 _RESOURCE * resource; /* Handle to each system resource. */
147 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
148 int found = 0; /* Found any instances? */
150 _SBLIM_ENTER("EnumInstanceNames");
151 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
152 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
153 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
154 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
156 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
157 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
158 goto exit;
159 }
161 /* Get a handle to the list of system resources. */
162 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
163 _SBLIM_TRACE(1,("--- _getResources() failed"));
164 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
165 goto exit;
166 }
168 /* Enumerate thru the list of system resources and return a CMPIInstance for each. */
169 while (Xen_VirtualSystemManagementService_getNextResource(session, resources, &resource)) {
170 /* Create a new CMPIInstance to store this resource. */
171 CMPIInstance * instance = _CMNewInstance(_BROKER, namespace, _CLASSNAME, &status);
172 if ((status.rc != CMPI_RC_OK) || CMIsNullObject(instance)) {
173 _SBLIM_TRACE(1,("--- CMNewInstance() failed - %s", CMGetCharPtr(status.msg)));
174 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERROR, "Cannot create new CMPIInstance");
175 goto exit;
176 }
178 /* Set the instance property values from the resource data. */
179 if (!Xen_VirtualSystemManagementService_setInstanceFromResource(resource, instance, _BROKER)) {
180 _SBLIM_TRACE(1,("--- _setInstanceFromResource() failed"));
181 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to set property values from resource data");
182 goto exit;
183 }
185 /* Free the resource data. */
186 if (!Xen_VirtualSystemManagementService_freeResource(resource)) {
187 _SBLIM_TRACE(1,("--- _freeResource() failed"));
188 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free resource data");
189 goto exit;
190 }
192 /* Return the CMPIObjectPath for this instance. */
193 CMPIObjectPath * objectpath = CMGetObjectPath(instance, &status);
194 if ((status.rc != CMPI_RC_OK) || CMIsNullObject(objectpath)) {
195 _SBLIM_TRACE(1,("--- CMGetObjectPath() failed - %s", CMGetCharPtr(status.msg)));
196 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERROR, "Cannot get CMPIObjectPath for instance");
197 goto exit;
198 }
199 CMSetNameSpace(objectpath, namespace); /* Note - CMGetObjectPath() does not preserve the namespace! */
201 _SBLIM_TRACE(3,("--- objectpath=\"%s\"", CMGetCharPtr(CDToString(_BROKER, objectpath, NULL))));
202 CMReturnObjectPath(results, objectpath);
203 found++;
204 }
206 _SBLIM_TRACE(2,("--- %d object paths found", found));
207 CMReturnDone(results);
209 exit:
210 /* Free the list of system resources. */
211 if (!Xen_VirtualSystemManagementService_freeResources(resources)) {
212 _SBLIM_TRACE(1,("--- _freeResources() failed"));
213 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free list of system resources");
214 }
216 _SBLIM_RETURNSTATUS(status);
217 }
219 // ----------------------------------------------------------------------------
220 // EnumInstances()
221 // Return a list of all the instances (return all the instance data).
222 // ----------------------------------------------------------------------------
223 static CMPIStatus EnumInstances(
224 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
225 const CMPIContext * context, /* [in] Additional context info, if any. */
226 const CMPIResult * results, /* [out] Results of this operation. */
227 const CMPIObjectPath * reference, /* [in] Contains target namespace and classname. */
228 const char ** properties) /* [in] List of desired properties (NULL=all). */
229 {
230 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
231 _RESOURCES * resources = NULL; /* Handle to the list of system resources. */
232 _RESOURCE * resource; /* Handle to each system resource. */
233 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
234 int found = 0; /* Found any resource instances? */
236 _SBLIM_ENTER("EnumInstances");
237 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
238 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
239 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
240 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
242 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
243 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
244 goto exit;
245 }
247 /* Get a handle to the list of system resources. */
248 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
249 _SBLIM_TRACE(1,("--- _getResources() failed"));
250 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
251 goto exit;
252 }
254 /* Enumerate thru the list of system resources and return a CMPIInstance for each. */
255 while (Xen_VirtualSystemManagementService_getNextResource(session, resources, &resource)) {
256 /* Create a new CMPIInstance to store this resource. */
257 CMPIInstance * instance = _CMNewInstance(_BROKER, namespace, _CLASSNAME, &status);
258 if ((status.rc != CMPI_RC_OK) || CMIsNullObject(instance)) {
259 _SBLIM_TRACE(1,("--- CMNewInstance() failed - %s", CMGetCharPtr(status.msg)));
260 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERROR, "Cannot create new CMPIInstance");
261 goto exit;
262 }
264 /* Setup a filter to only return the desired properties. */
265 status = CMSetPropertyFilter(instance, properties, _KEYNAMES);
266 if (status.rc != CMPI_RC_OK) {
267 _SBLIM_TRACE(1, ("--- CMSetPropertyFilter() failed - %s", CMGetCharPtr(status.msg)));
268 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Cannot set property filter");
269 goto exit;
270 }
272 /* Set the instance property values from the resource data. */
273 if (!Xen_VirtualSystemManagementService_setInstanceFromResource(resource, instance, _BROKER)) {
274 _SBLIM_TRACE(1,("--- _setInstanceFromResource() failed"));
275 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to set property values from resource data");
276 goto exit;
277 }
279 /* Free the resource data. */
280 if (!Xen_VirtualSystemManagementService_freeResource(resource)) {
281 _SBLIM_TRACE(1,("--- _freeResource() failed"));
282 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free resource data");
283 goto exit;
284 }
286 /* Return the CMPIInstance for this instance. */
287 _SBLIM_TRACE(3,("--- instance=\"%s\"", CMGetCharPtr(CDToString(_BROKER, instance, NULL))));
288 CMReturnInstance(results, instance);
289 found++;
290 }
292 _SBLIM_TRACE(2,("--- %d instances found", found));
293 CMReturnDone(results);
295 exit:
296 /* Free the list of system resources. */
297 if (!Xen_VirtualSystemManagementService_freeResources(resources)) {
298 _SBLIM_TRACE(1,("--- _freeResources() failed"));
299 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free list of system resources");
300 }
302 _SBLIM_RETURNSTATUS(status);
303 }
305 // ----------------------------------------------------------------------------
306 // GetInstance()
307 // Return the instance data for the specified instance only.
308 // ----------------------------------------------------------------------------
309 static CMPIStatus GetInstance(
310 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
311 const CMPIContext * context, /* [in] Additional context info, if any. */
312 const CMPIResult * results, /* [out] Results of this operation. */
313 const CMPIObjectPath * reference, /* [in] Contains the target namespace, classname and object path. */
314 const char ** properties) /* [in] List of desired properties (NULL=all). */
315 {
316 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
317 _RESOURCES * resources = NULL; /* Handle to the list of system resources. */
318 _RESOURCE * resource; /* Handle to the system resource. */
319 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
320 int found = 0; /* Found the target instance? */
322 _SBLIM_ENTER("GetInstance");
323 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
324 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
325 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
326 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
328 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
329 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
330 goto exit;
331 }
333 /* Get a handle to the list of system resources. */
334 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
335 _SBLIM_TRACE(1,("--- _getResources() failed"));
336 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
337 goto exit;
338 }
340 /* Get the target resource. */
341 found = Xen_VirtualSystemManagementService_getResourceForObjectPath(session, resources, &resource, reference);
342 if (!found || (resource == NULL)) {
343 _SBLIM_TRACE(1,("--- Target instance not found"));
344 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_FOUND, "Target instance not found");
345 goto exit;
346 }
348 /* Create a new CMPIInstance to store this resource. */
349 CMPIInstance * instance = _CMNewInstance(_BROKER, namespace, _CLASSNAME, &status);
350 if ((status.rc != CMPI_RC_OK) || CMIsNullObject(instance)) {
351 _SBLIM_TRACE(1,("--- CMNewInstance() failed - %s", CMGetCharPtr(status.msg)));
352 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERROR, "Cannot create new CMPIInstance");
353 goto exit;
354 }
356 /* Setup a filter to only return the desired properties. */
357 status = CMSetPropertyFilter(instance, properties, _KEYNAMES);
358 if (status.rc != CMPI_RC_OK) {
359 _SBLIM_TRACE(1, ("--- CMSetPropertyFilter() failed - %s", CMGetCharPtr(status.msg)));
360 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Cannot set property filter");
361 goto exit;
362 }
364 /* Set the instance property values from the resource data. */
365 if (!Xen_VirtualSystemManagementService_setInstanceFromResource(resource, instance, _BROKER)) {
366 _SBLIM_TRACE(1,("--- _setInstanceFromResource() failed"));
367 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to set property values from resource data");
368 goto exit;
369 }
371 /* Free the resource data. */
372 if (!Xen_VirtualSystemManagementService_freeResource(resource)) {
373 _SBLIM_TRACE(1,("--- _freeResource() failed"));
374 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free resource data");
375 goto exit;
376 }
378 /* Return the CMPIInstance for this instance. */
379 _SBLIM_TRACE(3,("--- instance=\"%s\"", CMGetCharPtr(CDToString(_BROKER, instance, NULL))));
380 CMReturnInstance(results, instance);
382 _SBLIM_TRACE(2,("--- instance found"));
383 CMReturnDone(results);
385 exit:
386 /* Free the list of system resources. */
387 if (!Xen_VirtualSystemManagementService_freeResources(resources)) {
388 _SBLIM_TRACE(1,("--- _freeResources() failed"));
389 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free list of system resources");
390 }
392 _SBLIM_RETURNSTATUS(status);
393 }
395 // ----------------------------------------------------------------------------
396 // SetInstance()
397 // Save modified instance data for the specified instance.
398 // ----------------------------------------------------------------------------
399 static CMPIStatus SetInstance(
400 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
401 const CMPIContext * context, /* [in] Additional context info, if any. */
402 const CMPIResult * results, /* [out] Results of this operation. */
403 const CMPIObjectPath * reference, /* [in] Contains the target namespace, classname and object path. */
404 const CMPIInstance * newinstance, /* [in] Contains the new instance data. */
405 const char **properties)
406 {
407 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
408 _RESOURCES * resources = NULL; /* Handle to the list of system resources. */
409 _RESOURCE * resource; /* Handle to the system resource. */
410 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
411 int found = 0; /* Found the target instance? */
413 _SBLIM_ENTER("SetInstance");
414 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
415 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
416 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
417 _SBLIM_TRACE(2, ("--- newinstance=\"%s\"", CMGetCharPtr(CDToString(_BROKER, newinstance, NULL))));
418 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
420 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
421 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
422 goto exit;
423 }
425 /* Get a handle to the list of system resources. */
426 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
427 _SBLIM_TRACE(1,("--- _getResources() failed"));
428 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
429 goto exit;
430 }
432 /* Get the target resource. */
433 found = Xen_VirtualSystemManagementService_getResourceForObjectPath(session, resources, &resource, reference);
434 if (!found || (resource == NULL)) {
435 _SBLIM_TRACE(1,("--- Target instance not found"));
436 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_FOUND, "Target instance not found");
437 goto exit;
438 }
440 _SBLIM_TRACE(2,("--- instance found"));
442 /* Update the target resource data with the new instance property values. */
443 int rc = Xen_VirtualSystemManagementService_setResourceFromInstance(resource, newinstance, _BROKER);
445 /* Free the resource data. */
446 if (!Xen_VirtualSystemManagementService_freeResource(resource)) {
447 _SBLIM_TRACE(1,("--- _freeResource() failed"));
448 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free resource data");
449 goto exit;
450 }
452 if (rc != 1) {
453 if (rc == -1) {
454 _SBLIM_TRACE(1,("--- _setResourceFromInstance() unsupported"));
455 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
456 } else {
457 _SBLIM_TRACE(1,("--- _setResourceFromInstance() failed"));
458 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to set resource data from instance properties");
459 }
460 goto exit;
461 }
463 exit:
464 /* Free the list of system resources. */
465 if (!Xen_VirtualSystemManagementService_freeResources(resources)) {
466 _SBLIM_TRACE(1,("--- _freeResources() failed"));
467 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free list of system resources");
468 }
470 _SBLIM_RETURNSTATUS(status);
471 }
473 // ----------------------------------------------------------------------------
474 // CreateInstance()
475 // Create a new instance from the specified instance data.
476 // ----------------------------------------------------------------------------
477 static CMPIStatus CreateInstance(
478 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
479 const CMPIContext * context, /* [in] Additional context info, if any. */
480 const CMPIResult * results, /* [out] Results of this operation. */
481 const CMPIObjectPath * reference, /* [in] Contains the target namespace, classname and object path. */
482 const CMPIInstance * newinstance) /* [in] Contains the new instance data. */
483 {
484 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
485 _RESOURCES * resources = NULL; /* Handle to the list of system resources. */
486 _RESOURCE * resource; /* Handle to the system resource. */
487 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
488 int found = 0; /* Found the target instance? */
490 _SBLIM_ENTER("CreateInstance");
491 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
492 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
493 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
494 _SBLIM_TRACE(2, ("--- newinstance=\"%s\"", CMGetCharPtr(CDToString(_BROKER, newinstance, NULL))));
495 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
497 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
498 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
499 goto exit;
500 }
502 /* WORKAROUND FOR PEGASUS BUG?! reference does not contain object path, only namespace & classname. */
503 reference = CMGetObjectPath(newinstance, NULL);
505 /* Get a handle to the list of system resources. */
506 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
507 _SBLIM_TRACE(1,("--- _getResources() failed"));
508 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
509 goto exit;
510 }
512 /* Get the target resource. */
513 found = Xen_VirtualSystemManagementService_getResourceForObjectPath(session, resources, &resource, reference);
515 /* Free the resource data. */
516 if (!Xen_VirtualSystemManagementService_freeResource(resource)) {
517 _SBLIM_TRACE(1,("--- _freeResource() failed"));
518 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free resource data");
519 goto exit;
520 }
522 if (found) {
523 _SBLIM_TRACE(1,("--- Target instance already exists"));
524 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_ALREADY_EXISTS, "Target instance already exists");
525 goto exit;
526 }
528 /* Create a new resource with the new instance property values. */
529 int rc = Xen_VirtualSystemManagementService_createResourceFromInstance(resources, &resource, newinstance, _BROKER);
530 if (rc != 1) {
531 if (rc == -1) {
532 _SBLIM_TRACE(1,("--- _createResourceFromInstance() unsupported"));
533 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
534 } else {
535 _SBLIM_TRACE(1,("--- _createResourceFromInstance() failed"));
536 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to create resource data from instance properties");
537 }
538 goto exit;
539 }
541 /* Return the object path for the newly created instance. */
542 CMPIObjectPath * objectpath = CMGetObjectPath(newinstance, NULL);
543 CMSetNameSpace(objectpath, namespace);
544 CMReturnObjectPath(results, objectpath);
545 CMReturnDone(results);
547 exit:
548 /* Free the list of system resources. */
549 if (!Xen_VirtualSystemManagementService_freeResources(resources)) {
550 _SBLIM_TRACE(1,("--- _freeResources() failed"));
551 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free list of system resources");
552 }
554 _SBLIM_RETURNSTATUS(status);
555 }
557 // ----------------------------------------------------------------------------
558 // DeleteInstance()
559 // Delete or remove the specified instance from the system.
560 // ----------------------------------------------------------------------------
561 static CMPIStatus DeleteInstance(
562 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
563 const CMPIContext * context, /* [in] Additional context info, if any. */
564 const CMPIResult * results, /* [out] Results of this operation. */
565 const CMPIObjectPath * reference) /* [in] Contains the target namespace, classname and object path. */
566 {
567 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
568 _RESOURCES * resources = NULL; /* Handle to the list of system resources. */
569 _RESOURCE * resource; /* Handle to the system resource. */
570 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
571 int found = 0; /* Found the target instance? */
573 _SBLIM_ENTER("DeleteInstance");
574 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
575 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
576 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
577 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
579 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
580 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
581 goto exit;
582 }
584 /* Get a handle to the list of system resources. */
585 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
586 _SBLIM_TRACE(1,("--- _getResources() failed"));
587 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
588 goto exit;
589 }
591 /* Get the target resource. */
592 found = Xen_VirtualSystemManagementService_getResourceForObjectPath(session, resources, &resource, reference);
593 if (!found || (resource == NULL)) {
594 _SBLIM_TRACE(1,("--- Target instance not found"));
595 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_FOUND, "Target instance not found");
596 goto exit;
597 }
599 _SBLIM_TRACE(2,("--- instance found"));
601 /* Delete the target resource. */
602 int rc = Xen_VirtualSystemManagementService_deleteResource(resources, resource);
604 /* Free the resource data. */
605 if (!Xen_VirtualSystemManagementService_freeResource(resource)) {
606 _SBLIM_TRACE(1,("--- _freeResource() failed"));
607 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free resource data");
608 goto exit;
609 }
611 if (rc != 1) {
612 if (rc == -1) {
613 _SBLIM_TRACE(1,("--- __deleteResource() unsupported"));
614 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
615 } else {
616 _SBLIM_TRACE(1,("--- _deleteResource() failed"));
617 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to delete resource");
618 }
619 goto exit;
620 }
622 exit:
623 /* Free the list of system resources. */
624 if (!Xen_VirtualSystemManagementService_freeResources(resources)) {
625 _SBLIM_TRACE(1,("--- _freeResources() failed"));
626 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to free list of system resources");
627 }
629 _SBLIM_RETURNSTATUS(status);
630 }
633 // ----------------------------------------------------------------------------
634 // ExecQuery()
635 // Return a list of all the instances that satisfy the specified query filter.
636 // ----------------------------------------------------------------------------
637 static CMPIStatus ExecQuery(
638 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
639 const CMPIContext * context, /* [in] Additional context info, if any. */
640 const CMPIResult * results, /* [out] Results of this operation. */
641 const CMPIObjectPath * reference, /* [in] Contains the target namespace and classname. */
642 const char * language, /* [in] Name of the query language. */
643 const char * query) /* [in] Text of the query written in the query language. */
644 {
645 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
646 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
648 _SBLIM_ENTER("ExecQuery");
649 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
650 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
651 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
652 _SBLIM_TRACE(2, ("--- language=\"%s\"", language));
653 _SBLIM_TRACE(2, ("--- query=\"%s\"", query));
654 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
656 if (strcmp(namespace, HOST_INSTRUMENTATION_NS) == 0) {
657 _SBLIM_TRACE(1,("--- \"%s\" is not a valid namespace for %s", namespace, _CLASSNAME));
658 goto exit;
659 }
661 /* EXECQUERY() IS NOT YET SUPPORTED FOR THIS CLASS */
662 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
664 CMReturnDone(results);
666 exit:
667 _SBLIM_RETURNSTATUS(status);
668 }
670 // ----------------------------------------------------------------------------
671 // InstInitialize()
672 // Perform any necessary initialization immediately after the instance provider
673 // is first loaded.
674 // ----------------------------------------------------------------------------
675 static void InstInitialize(
676 CMPIInstanceMI * self, /* [in] Handle to this provider (i.e. 'self'). */
677 const CMPIContext * context) /* [in] Additional context info, if any. */
678 {
679 _SBLIM_ENTER("InstInitialize");
680 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
681 // _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
683 /* Initialized Xen session object. */
684 if (session == NULL)
685 xen_utils_xen_init(&session);
687 _SBLIM_RETURN();
688 }
691 // ============================================================================
692 // CMPI INSTANCE PROVIDER FUNCTION TABLE SETUP
693 // ============================================================================
694 CMInstanceMIStub( , Xen_VirtualSystemManagementService, _BROKER, InstInitialize(&mi, ctx));
697 // ============================================================================
698 // CMPI METHOD PROVIDER FUNCTION TABLE
699 // ============================================================================
701 // ----------------------------------------------------------------------------
702 // MethodCleanup()
703 // Perform any necessary cleanup immediately before this provider is unloaded.
704 // ----------------------------------------------------------------------------
705 static CMPIStatus MethodCleanup(
706 CMPIMethodMI * self, /* [in] Handle to this provider (i.e. 'self'). */
707 const CMPIContext * context, /* [in] Additional context info, if any. */
708 CMPIBoolean terminating) /* [in] True if MB is terminating */
709 {
710 CMPIStatus status = { CMPI_RC_OK, NULL }; /* Return status of CIM operations. */
712 _SBLIM_ENTER("MethodCleanup");
713 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
714 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
716 if (session) {
717 xen_utils_xen_close(session);
718 session = NULL;
719 }
720 _SBLIM_RETURNSTATUS(status);
721 }
723 // ----------------------------------------------------------------------------
724 // InvokeMethod()
725 // Execute an extrinsic method on the specified instance.
726 // ----------------------------------------------------------------------------
727 static CMPIStatus InvokeMethod(
728 CMPIMethodMI * self, /* [in] Handle to this provider (i.e. 'self') */
729 const CMPIContext * context, /* [in] Additional context info, if any */
730 const CMPIResult * results, /* [out] Results of this operation */
731 const CMPIObjectPath * reference, /* [in] Contains the CIM namespace, classname and desired object path */
732 const char * methodname, /* [in] Name of the method to apply against the reference object */
733 const CMPIArgs * argsin, /* [in] Method input arguments */
734 CMPIArgs * argsout) /* [in] Method output arguments */
735 {
736 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations */
737 char * namespace = CMGetCharPtr(CMGetNameSpace(reference, NULL)); /* Target namespace. */
738 int rc = 0;
739 int argsInCount;
740 _RESOURCES * resources; /* Handle to the list of system resources. */
741 CMPIData argdata;
742 char error_msg[XEN_UTILS_ERROR_BUF_LEN];
744 _SBLIM_ENTER("InvokeMethod");
745 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
746 _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
747 _SBLIM_TRACE(2, ("--- reference=\"%s\"", CMGetCharPtr(CDToString(_BROKER, reference, NULL))));
748 _SBLIM_TRACE(2, ("--- methodname=\"%s\"", methodname));
749 _SBLIM_TRACE(2, ("--- namespace=\"%s\"", namespace));
751 if (!xen_utils_validate_session(&session)) {
752 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_METHOD_NOT_AVAILABLE, "Unable to connect to xen daemon");
753 goto Exit;
754 }
756 argsInCount = CMGetArgCount(argsin, NULL);
757 _SBLIM_TRACE(2, ("--- # argsin=%d", argsInCount));
759 /* Get a handle to the list of system resources. */
760 if (!Xen_VirtualSystemManagementService_getResources(&resources)) {
761 _SBLIM_TRACE(1,("--- _getResources() failed"));
762 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to get list of system resources");
763 rc = 2;
764 goto Exit;
765 }
767 /* Start/stop the service. */
768 if (strcmp(methodname, "StartService") == 0) {
769 if (!Xen_VirtualSystemManagementService_enableResource(session, resources, NULL)) {
770 _SBLIM_TRACE(1,("--- StartService() failed"));
771 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Method operation failed");
772 rc = 2;
773 goto Exit;
774 }
775 }
776 else if (strcmp(methodname, "StopService") == 0) {
777 if (!Xen_VirtualSystemManagementService_disableResource(session, resources, NULL)) {
778 _SBLIM_TRACE(1,("--- StopService() failed"));
779 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Method operation failed");
780 rc = 2;
781 goto Exit;
782 }
783 }
784 else if (strcmp(methodname, "DefineSystem") == 0) {
785 if (argsInCount != 3) {
786 _SBLIM_TRACE(1,("--- Incorrect number of method arguments"));
787 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Incorrect number of method arguments");
788 rc = 5;
789 goto Exit;
790 }
792 /* Get embedded instance of VirtualSystemSettingData. */
793 argdata = CMGetArgAt(argsin, 0, NULL, &status);
794 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(argdata)) {
795 _SBLIM_TRACE(1,("--- CMGetArgAt(0) failed"));
796 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Cannot get method input argument");
797 rc = 5;
798 goto Exit;
799 }
801 if (argdata.type != CMPI_string) {
802 _SBLIM_TRACE(1,("--- Invalid setting type - %s", _CMPITypeName(argdata.type)));
803 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid setting type");
804 rc = 5;
805 goto Exit;
806 }
808 char *vsSettingData = CMGetCharPtr(argdata.value.string);
809 _SBLIM_TRACE(2,("--- vsSettingData=\"%s\"", vsSettingData));
811 /* Get input array of ResourceAllocationSettingData instances. */
812 argdata = CMGetArgAt(argsin, 1, NULL, &status);
813 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(argdata)) {
814 _SBLIM_TRACE(1,("--- CMGetArgAt(1) failed"));
815 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Cannot get method input argument");
816 rc = 5;
817 goto Exit;
818 }
820 /* Check that the input arg is an array. */
821 if (!CMIsArray(argdata) || CMIsNullObject(argdata.value.array)) {
822 _SBLIM_TRACE(1,("--- Invalid method argument type. Array expected"));
823 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid method argument type. Array expected");
824 rc = 5;
825 goto Exit;
826 }
828 CMPIArray *resourceSettings = argdata.value.array;
829 xen_vm vm;
830 if (!create_vm(vsSettingData, resourceSettings, &vm, &status))
831 {
832 _SBLIM_TRACE(1,("--- create_vm() failed"));
833 /* status set in create_vm */
834 rc = 3;
835 goto Exit;
836 }
838 /* Return the objectpath for the resulting DomU in the output args */
839 CMPIObjectPath * op = CMNewObjectPath(_BROKER, namespace, "Xen_ComputerSystem", NULL);
840 CMAddKey(op, "CreationClassName", (CMPIValue *)"Xen_ComputerSystem", CMPI_chars);
841 char *vm_name;
842 if (!xen_vm_get_name_label(session->xen, &vm_name, vm)) {
843 /* Can't get the name after all of this work! Unwind this mess. */
844 remove_vm(vm);
845 xen_vm_free(vm);
846 _SBLIM_TRACE(1,("--- Unable to retrieve name of new VM"));
847 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to create the virtual system");
848 rc = 3;
849 goto Exit;
850 }
852 CMAddKey(op, "Name", (CMPIValue *)vm_name, CMPI_chars);
853 /*
854 * WARNING: J. Fehlig
855 * What does this do?
856 *
857 * CMSetHostAndNameSpaceFromObjectPath(op, reference);
858 *
859 */
860 _SBLIM_TRACE(2,("--- VirtualSystem=%s", CMGetCharPtr(CDToString(_BROKER, op, NULL))));
861 CMAddArg(argsout, "ResultingSystem", (CMPIValue*)&op, CMPI_ref);
862 xen_vm_free(vm);
864 _SBLIM_TRACE(2, ("--- DefineSystem completed with success"));
865 }
866 else if (strcmp(methodname, "AddResourceSettings") == 0) {
867 /* TODO */
868 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_METHOD_NOT_AVAILABLE, NULL);
869 rc = 1;
870 goto Exit;
871 }
872 else if (strcmp(methodname, "AddResourceSetting") == 0) {
873 if (argsInCount != 2) {
874 _SBLIM_TRACE(1,("--- Incorrect number of method arguments"));
875 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Incorrect number of method arguments");
876 rc = 5;
877 goto Exit;
878 }
880 /* Get vm record based on VirtualSystemSettingData object path. */
881 argdata = CMGetArgAt(argsin, 0, NULL, &status);
882 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(argdata)) {
883 _SBLIM_TRACE(1,("--- CMGetArgAt(0) failed"));
884 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Cannot get method input argument");
885 rc = 5;
886 goto Exit;
887 }
889 if (argdata.type != CMPI_ref) {
890 _SBLIM_TRACE(1,("--- Invalid setting type - %s", _CMPITypeName(argdata.type)));
891 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid setting type");
892 rc = 5;
893 goto Exit;
894 }
896 _SBLIM_TRACE(2, ("--- domain object path=\"%s\"", CMGetCharPtr(CDToString(_BROKER, argdata.value.ref, NULL))));
898 xen_vm_record *vmRec;
899 if (!xen_utils_get_domain_from_sd_OP(session, &vmRec, argdata.value.ref)) {
900 _SBLIM_TRACE(1,("--- Unable to retrieve domain record from object path"));
901 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Unable to retrieve domain record from object path");
902 rc = 5;
903 goto Exit;
904 }
906 /*
907 * Get input ResourceAllocationSettingData instance
908 * that will be added to domain.
909 */
910 argdata = CMGetArgAt(argsin, 1, NULL, &status);
911 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(argdata)) {
912 _SBLIM_TRACE(1,("--- CMGetArgAt(1) failed"));
913 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Cannot get method input argument");
914 xen_vm_record_free(vmRec);
915 rc = 5;
916 goto Exit;
917 }
919 if (argdata.type != CMPI_string) {
920 _SBLIM_TRACE(1,("--- Invalid setting type - %s", _CMPITypeName(argdata.type)));
921 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid setting type");
922 rc = 5;
923 goto Exit;
924 }
926 char *newSetting = CMGetCharPtr(argdata.value.string);
927 _SBLIM_TRACE(2,("--- newSetting=\"%s\"", newSetting));
930 CMPIObjectPath *resultSetting;
931 if (!add_resource_to_vm(vmRec, newSetting, &resultSetting, namespace, &status))
932 {
933 _SBLIM_TRACE(1,("--- add_resource_to_vm() failed"));
934 /* status set in add_resource_to_vm */
935 xen_vm_record_free(vmRec);
936 rc = 3;
937 goto Exit;
938 }
940 CMAddArg(argsout, "ResultingResourceSetting",
941 (CMPIValue*)&resultSetting, CMPI_ref);
942 xen_vm_record_free(vmRec);
944 _SBLIM_TRACE(2, ("--- AddResourceSetting completed with success"));
945 }
946 else if (strcmp(methodname, "ModifyResourceSettings") == 0) {
947 /* TODO */
948 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
949 rc = 1;
950 goto Exit;
951 }
952 else if (strcmp(methodname, "RemoveResourceSettings") == 0) {
953 /* TODO */
954 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
955 rc = 1;
956 goto Exit;
957 }
958 else if (strcmp(methodname, "ModifySystemSettings") == 0) {
959 /* TODO */
960 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_SUPPORTED, NULL);
961 rc = 1;
962 goto Exit;
963 }
964 else if (strcmp(methodname, "DestroySystem") == 0) {
965 /* Check that the method has the correct number of arguments. */
966 if (argsInCount != 1) {
967 _SBLIM_TRACE(1,("--- Incorrect number of method arguments"));
968 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Incorrect number of method arguments");
969 rc = 5;
970 goto Exit;
971 }
973 /* Get object path of domain */
974 argdata = CMGetArgAt(argsin, 0, NULL, &status);
975 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(argdata)) {
976 _SBLIM_TRACE(1,("--- CMGetArgAt(0) failed"));
977 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Cannot get method input argument");
978 rc = 5;
979 goto Exit;
980 }
982 if ((argdata.type != CMPI_ref) || CMIsNullObject(argdata.value.ref)) {
983 _SBLIM_TRACE(1,("--- Invalid parameter type - %s", _CMPITypeName(argdata.type)));
984 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid method input argument");
985 rc = 5;
986 goto Exit;
987 }
989 CMPIData namedata = CMGetKey(argdata.value.ref, "Name", &status);
990 char *domainname = CMGetCharPtr(namedata.value.string);
991 _SBLIM_TRACE(1,("--- domainname = %s", domainname));
992 xen_vm_set *vms;
993 if (!xen_vm_get_by_name_label(session->xen, &vms, domainname)) {
994 _SBLIM_TRACE(1,("--- Target instance not found"));
995 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_NOT_FOUND, "Target instance not found");
996 rc = 3;
997 goto Exit;
998 }
999 assert(vms->size == 1);
1000 xen_vm vm = vms->contents[0];
1001 enum xen_vm_power_state power_state;
1002 if (!xen_vm_get_power_state(session->xen, &power_state, vm)) {
1003 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1004 _SBLIM_TRACE(1,("--- Unable to retrieve power state of vm - error = %s", error_msg));
1005 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Unable to determine power state of virtual system");
1006 rc = 3;
1007 goto Exit;
1010 _SBLIM_TRACE(1,("--- retrieved power state of vm - %", power_state));
1012 if (power_state != XEN_VM_POWER_STATE_HALTED) {
1013 _SBLIM_TRACE(1,("--- calling xen_vm_clean_shutdown"));
1014 if (!xen_vm_clean_shutdown(session->xen, vm)) {
1015 _SBLIM_TRACE(1, ("--- xen_vm_clean_shutdown() failed"));
1016 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to shutdown VirtualSystem");
1017 rc = 3;
1018 goto Exit;
1022 _SBLIM_TRACE(1,("--- calling xen_vm_destroy"));
1023 if (!xen_vm_destroy(session->xen, vm)) {
1024 _SBLIM_TRACE(1,("--- xen_vm_destroy failed"));
1025 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_FAILED, "Failed to destroy the virtual system");
1026 rc = 3;
1027 goto Exit;
1031 else {
1032 _SBLIM_TRACE(1,("--- Method \"%s\" is not supported", methodname));
1033 CMSetStatusWithChars(_BROKER, &status, CMPI_RC_ERR_METHOD_NOT_AVAILABLE, NULL);
1034 rc = 5;
1035 goto Exit;
1038 Exit:
1040 CMReturnData(results, (CMPIValue *)&rc, CMPI_uint32);
1041 CMReturnDone(results);
1042 _SBLIM_RETURNSTATUS(status);
1046 // ----------------------------------------------------------------------------
1047 // MethodInitialize()
1048 // Perform any necessary initialization immediately after the method provider
1049 // is first loaded.
1050 // ----------------------------------------------------------------------------
1051 static void MethodInitialize(
1052 CMPIMethodMI * self, /* [in] Handle to this provider (i.e. 'self'). */
1053 const CMPIContext * context) /* [in] Additional context info, if any. */
1055 _SBLIM_ENTER("MethodInitialize");
1056 _SBLIM_TRACE(2, ("--- self=\"%s\"", self->ft->miName));
1057 // _SBLIM_TRACE(2, ("--- context=\"%s\"", CMGetCharPtr(CDToString(_BROKER, context, NULL))));
1059 /* Initialized Xen session object. */
1060 if (session == NULL)
1061 xen_utils_xen_init(&session);
1063 _SBLIM_RETURN();
1066 // ============================================================================
1067 // CMPI METHOD PROVIDER FUNCTION TABLE SETUP
1068 // ============================================================================
1069 CMMethodMIStub( , Xen_VirtualSystemManagementService, _BROKER, MethodInitialize(&mi, ctx));
1072 /*
1073 * Wild macro to support adding devices (vbd, vif) to as list of such
1074 * devices. To be used only in create_vm where list of devices is
1075 * collected from the incoming RASDs.
1076 */
1077 #define ADD_DEVICE_TO_LIST(list, device, type) \
1078 { \
1079 if (list == NULL) { \
1080 list = type ## _set_alloc(1); \
1081 if (list == NULL) { \
1082 _SBLIM_TRACE(1, \
1083 ("Cannot malloc memory for xen device settings list")); \
1084 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, \
1085 "Unable to malloc memory"); \
1086 type ## _free(device); \
1087 goto Error; \
1088 } \
1089 list->size = 1; \
1090 list->contents[0] = device; \
1091 } \
1092 /* List is not empty. Grow the list and add the new device */ \
1093 else { \
1094 int new_len = sizeof(type ## _set) + ((list->size + 1) * sizeof(type)); \
1095 list = realloc(list, new_len); \
1096 if (list == NULL) { \
1097 _SBLIM_TRACE(1, \
1098 ("Cannot malloc memory for xen device settings list")); \
1099 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, \
1100 "Unable to malloc memory"); \
1101 type ## _free(device); \
1102 goto Error; \
1103 } \
1104 list->contents[list->size] = device; \
1105 list->size++; \
1106 } \
1107 } \
1110 static int create_vm(char *vsSettings, CMPIArray *resourceSettings,
1111 xen_vm *result, CMPIStatus *status)
1113 CMPIInstance *instance;
1114 CMPIObjectPath *objectpath;
1115 char *settingclassname;
1116 xen_vm_record *vm_rec;
1117 xen_vbd_record_set *vbds = NULL;
1118 xen_vif_record_set *vifs = NULL;
1119 xen_console_record *con_rec = NULL;
1120 xen_vm vm = NULL;
1121 int i;
1122 int ccode;
1123 char error_msg[XEN_UTILS_ERROR_BUF_LEN];
1125 _SBLIM_TRACE(2,("--- vsSettings=\"%s\"", vsSettings));
1127 vm_rec = xen_vm_record_alloc();
1128 if (vm_rec == NULL) {
1129 _SBLIM_TRACE(1,("--- Cannot malloc memory for vm record"));
1130 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, "Unable to malloc memory");
1131 return 0;
1134 instance = parse_embedded_instance(vsSettings);
1135 if (instance == NULL) { /* parser returns zero for success, non-zero for error */
1136 _SBLIM_TRACE(1,("--- Error parsing VirtualSystemSettingData: \"%s\"", vsSettings));
1137 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Cannot parse VirtualSystemSettingData");
1139 return 0;
1142 objectpath = CMGetObjectPath(instance, NULL);
1143 _SBLIM_TRACE(2,("--- objectpath=%s", CMGetCharPtr(CDToString(_BROKER, objectpath, NULL))));
1145 /* Get the class type of the setting data instance */
1146 settingclassname = CMGetCharPtr(CMGetClassName(objectpath, NULL));
1147 _SBLIM_TRACE(2,("--- settingclassname=%s", settingclassname));
1149 /* Ensure we have a Xen_ComputerSystemSettingData */
1150 if (strcmp(settingclassname,"Xen_ComputerSystemSettingData") != 0) {
1151 _SBLIM_TRACE(1,("--- Unrecognized setting data class - %s", settingclassname));
1152 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Unrecognized setting data class");
1153 return 0;
1156 /* Convert the domain settings to respective xen settings. */
1157 if (!vssd2xenconfig(instance, vm_rec, status)) {
1158 _SBLIM_TRACE(1,("--- failed to convert VSSD to xen_vmrecord"));
1159 xen_vm_record_free(vm_rec);
1160 return 0;
1164 /* Convert resource settings to their respecitve xen settings. */
1165 char *setting;
1166 for (i = 0; i < CMGetArrayCount(resourceSettings, NULL); i++) {
1167 CMPIData settingdata = CMGetArrayElementAt(resourceSettings, i, status);
1168 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(settingdata)) {
1169 _SBLIM_TRACE(1,("--- CMGetArrayElementAt(%d) failed", i));
1170 goto Error;
1173 /* Check that the array element is an (embedded) instance */
1174 // if (settingdata.type != CMPI_instance) {
1175 if (settingdata.type != CMPI_string) {
1176 _SBLIM_TRACE(1,("--- Invalid setting type - %s", _CMPITypeName(settingdata.type)));
1177 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid resource setting data parameter");
1178 goto Error;
1181 setting = CMGetCharPtr(settingdata.value.string);
1182 _SBLIM_TRACE(2,("--- setting=\"%s\"", setting));
1184 instance = parse_embedded_instance(setting);
1186 if (instance == NULL) { /* Fail if we have an unparsable setting data? */
1187 _SBLIM_TRACE(1,("--- Error parsing RASD: \"%s\"", setting));
1188 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Unable to parse resource setting data parameter");
1189 goto Error;
1192 _SBLIM_TRACE(2,("--- instance=%s", CMGetCharPtr(CDToString(_BROKER, instance, NULL))));
1193 objectpath = CMGetObjectPath(instance, NULL);
1194 _SBLIM_TRACE(2,("--- objectpath=%s", CMGetCharPtr(CDToString(_BROKER, objectpath, NULL))));
1196 /* Get the class type of the setting data instance */
1197 settingclassname = CMGetCharPtr(CMGetClassName(objectpath, NULL));
1198 _SBLIM_TRACE(2,("--- settingclassname=%s", settingclassname));
1200 /* Populate config with instance data from the virtual device. */
1201 if (strcmp(settingclassname,"Xen_ProcessorSettingData") == 0) {
1202 _SBLIM_TRACE(2,("--- adding Xen_ProcessorSettingData to configuration"));
1204 if (!proc_rasd2vmconfig(instance, vm_rec, status)) {
1205 _SBLIM_TRACE(1,("--- Error parsing processor settings"));
1206 goto Error;
1209 else if (strcmp(settingclassname,"Xen_MemorySettingData") == 0) {
1210 _SBLIM_TRACE(2,("--- adding Xen_MemorySettingData to configuration"));
1212 /* Fail if duplicate memory rasd's? */
1213 if (vm_rec->memory_dynamic_max > 0) {
1214 _SBLIM_TRACE(1,("--- More than one memroy setting data specified"));
1215 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER,
1216 "More than one memory setting data specified");
1217 goto Error;
1220 if (!mem_rasd2vmconfig(instance, vm_rec, status)) {
1221 _SBLIM_TRACE(1,("--- Error parsing memory settings"));
1222 goto Error;
1225 else if (strcmp(settingclassname,"Xen_DiskSettingData") == 0) {
1226 _SBLIM_TRACE(2,("--- adding Xen_DiskSettingData to configuration"));
1228 xen_vbd_record *vbd_rec;
1229 if (!disk_rasd2vmconfig(instance, &vbd_rec, status)) {
1230 _SBLIM_TRACE(1,("--- Error parsing disk settings"));
1231 goto Error;
1234 ADD_DEVICE_TO_LIST(vbds, vbd_rec, xen_vbd_record);
1236 else if (strcmp(settingclassname,"Xen_NetworkPortSettingData") == 0) {
1237 _SBLIM_TRACE(2,("--- adding Xen_NetworkPortSettingData to configuration"));
1239 xen_vif_record *vif_rec;
1240 if (!nic_rasd2vmconfig(instance, &vif_rec, status)) {
1241 _SBLIM_TRACE(1,("--- Error parsing network port settings"));
1242 goto Error;
1245 ADD_DEVICE_TO_LIST(vifs, vif_rec, xen_vif_record);
1247 else if (strcmp(settingclassname,"Xen_ConsoleSettingData") == 0) {
1248 _SBLIM_TRACE(2,("--- adding Xen_ConsoleSettingData to configuration"));
1250 con_rec = xen_console_record_alloc();
1251 if (con_rec == NULL) {
1252 _SBLIM_TRACE(1,("--- Cannot malloc memory for console record"));
1253 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, "Unable to malloc memory");
1254 goto Error;
1257 if (!con_rasd2vmconfig(instance, con_rec, status)) {
1258 _SBLIM_TRACE(1,("--- Error parsing console settings"));
1259 goto Error;
1262 else {
1263 _SBLIM_TRACE(1,("--- Unrecognized setting data class - %s", settingclassname));
1264 goto Error;
1268 /* We have all of the settings. First create the vm. */
1269 if (!xen_vm_create(session->xen, &vm, vm_rec)) {
1270 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1271 _SBLIM_TRACE(1,("--- xen_vm_create failed: %s", error_msg));
1272 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1273 goto Error;
1276 xen_vm_record_opt vm_record_opt =
1278 .u.handle = vm
1279 };
1281 /* Add all of the virutal block devices. */
1282 for (i = 0; i < vbds->size; i++) {
1284 xen_vbd_record *vbd_rec = vbds->contents[i];
1285 vbd_rec->vm = &vm_record_opt;
1286 xen_vbd new_vbd;
1287 ccode = xen_vbd_create(session->xen, &new_vbd, vbd_rec);
1288 /* Set vm field of vbd record to NULL so it is not freed */
1289 vbd_rec->vm = NULL;
1290 if (!ccode)
1292 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1293 _SBLIM_TRACE(1,("--- xen_vdb_create failed: %s", error_msg));
1294 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1295 goto Error;
1297 xen_vbd_free(new_vbd);
1300 /* Add all of the virutal network devices. */
1301 for (i = 0; i < vifs->size; i++) {
1302 xen_vif_record *vif_rec = vifs->contents[i];
1303 vif_rec->vm = &vm_record_opt;
1304 xen_vif new_vif;
1305 ccode = xen_vif_create(session->xen, &new_vif, vif_rec);
1306 /* Set vm field of vif record to NULL so it is not freed */
1307 vif_rec->vm = NULL;
1308 if (!ccode)
1310 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1311 _SBLIM_TRACE(1,("--- xen_vif_create failed %s", error_msg));
1312 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1313 goto Error;
1315 xen_vif_free(new_vif);
1318 /* Add console device if specified */
1319 if (con_rec) {
1320 con_rec->vm = &vm_record_opt;
1321 xen_console new_con;
1322 ccode = xen_console_create(session->xen, &new_con, con_rec);
1323 /* Set vm field of console record to NULL so it is not freed */
1324 con_rec->vm = NULL;
1325 if (!ccode)
1327 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1328 _SBLIM_TRACE(1,("--- xen_console_create failed %s", error_msg));
1329 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1330 goto Error;
1332 xen_console_free(new_con);
1335 xen_console_record_free(con_rec);
1336 xen_vbd_record_set_free(vbds);
1337 xen_vif_record_set_free(vifs);
1338 xen_vm_record_free(vm_rec);
1340 *result = vm;
1342 return 1;
1344 /* Unwind if we failed.
1345 * remove_vm will nuke any vifs / vbds created and the vm.
1346 */
1347 Error:
1348 if (vm) {
1349 remove_vm(vm);
1350 xen_vm_free(vm);
1352 xen_console_record_free(con_rec);
1353 xen_vbd_record_set_free(vbds);
1354 xen_vif_record_set_free(vifs);
1355 xen_vm_record_free(vm_rec);
1356 *result = NULL;
1358 return 0;
1362 static int add_resource_to_vm(xen_vm_record *vmRec,
1363 char *sourceSetting,
1364 CMPIObjectPath **resultSetting,
1365 char *namespace,
1366 CMPIStatus *status)
1368 CMPIInstance *instance;
1369 CMPIObjectPath *op;
1370 char *settingclassname;
1371 CMPIData propertyvalue;
1372 int ccode;
1373 char instId[MAX_INSTANCEID_LEN];
1374 char error_msg[XEN_UTILS_ERROR_BUF_LEN];
1375 xen_vm_record_opt vm_record_opt =
1377 .u.handle = vmRec->handle
1379 };
1382 /* Parse sourceSetting - creating a CIM instance */
1383 instance = parse_embedded_instance(sourceSetting);
1385 if (instance == NULL) { /* Fail if we have an unparsable setting data? */
1386 _SBLIM_TRACE(1,("--- Error parsing input RASD: \"%s\"", sourceSetting));
1387 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Unable to parse ResourceSetting parameter");
1388 return 0;
1391 _SBLIM_TRACE(2,("--- instance=%s", CMGetCharPtr(CDToString(_BROKER, instance, NULL))));
1392 op = CMGetObjectPath(instance, NULL);
1393 _SBLIM_TRACE(2,("--- objectpath=%s", CMGetCharPtr(CDToString(_BROKER, op, NULL))));
1395 /* Get the class type of the setting data instance */
1396 settingclassname = CMGetCharPtr(CMGetClassName(op, NULL));
1397 _SBLIM_TRACE(2,("--- settingclassname=%s", settingclassname));
1399 /* Add the resource to the domain. */
1400 if (strcmp(settingclassname,"Xen_ProcessorSettingData") == 0) {
1401 #if 1
1402 _SBLIM_TRACE(1,("--- Adding processors not currently supported"));
1403 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, "Adding processors not currently supported");
1404 return 0;
1406 #else
1407 /* Code for adding processors -- when Xen API supports it. */
1408 _SBLIM_TRACE(2,("--- adding Xen_ProcessorSettingData to configuration"));
1409 propertyvalue = CMGetProperty(instance, "VirtualQuantity", status);
1410 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1411 _SBLIM_TRACE(1,("--- Unable to determine VirtualQuantity in Processor RASD"));
1412 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "VirtualQuantity not specified in ResourceSetting");
1413 return 0;
1416 /* Adding cpus means just that - adding to existing amount */
1417 uint64_t cpus = propertyvalue.value.uint64 + vmRec->vcpus_number;
1418 /*
1419 * TODO:
1420 * 1. CPU weights and other scheduling parameters?
1421 */
1422 if (!xen_vm_set_vcpus_number(session->xen,
1423 vmRec->handle,
1424 cpus)) {
1425 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1426 _SBLIM_TRACE(1,("--- xen_vm_set_vcpus_number failed: %s", error_msg));
1427 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1428 return 0;
1431 /* Create new object path for setting */
1432 *resultSetting = CMNewObjectPath(_BROKER, namespace, "Xen_ProcessorSettingData", NULL);
1433 snprintf(instId, MAX_INSTANCEID_LEN, "Xen:%s:Processor", vmRec->name_label);
1434 CMAddKey(*resultSetting, "InstanceID", (CMPIValue *)instId, CMPI_chars);
1435 #endif
1437 else if (strcmp(settingclassname,"Xen_MemorySettingData") == 0) {
1438 #if 1
1439 _SBLIM_TRACE(1,("--- Adding memory not currently supported"));
1440 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, "Adding memory not currently supported");
1441 return 0;
1443 #else
1444 /* Code for adding memory -- when Xen API supports it. */
1445 _SBLIM_TRACE(2,("--- adding Xen_MemorySettingData to configuration"));
1446 propertyvalue = CMGetProperty(instance, "VirtualQuantity", status);
1447 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1448 _SBLIM_TRACE(1,("--- Unable to determine VirtualQuantity in Memory RASD"));
1449 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "VirtualQuantity not specified in ResourceSetting");
1450 return 0;
1453 /* Adding memory means just that - adding to existing amount */
1454 uint64_t mem = propertyvalue.value.uint64 + vmRec->memory_dynamic_max;
1455 if (!xen_vm_set_memory_dynamic_max(session->xen,
1456 vmRec->handle,
1457 mem)) {
1458 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1459 _SBLIM_TRACE(1,("--- xen_vm_set_memory_dynamic_max failed: %s", error_msg));
1460 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1461 return 0;
1464 /* Add object path to output array */
1465 *resultSetting = CMNewObjectPath(_BROKER, namespace, "Xen_MemorySettingData", NULL);
1466 snprintf(instId, MAX_INSTANCEID_LEN, "Xen:%s:Memory1", vmRec->name_label);
1467 CMAddKey(*resultSetting, "InstanceID", (CMPIValue *)instId, CMPI_chars);
1468 #endif
1470 else if (strcmp(settingclassname,"Xen_DiskSettingData") == 0) {
1471 _SBLIM_TRACE(2,("--- adding Xen_DiskSettingData to configuration"));
1473 xen_vbd_record *vbd_rec;
1474 if (!disk_rasd2vmconfig(instance, &vbd_rec, status)) {
1475 /* status set in disk_rasd2vmconfig */
1476 _SBLIM_TRACE(1,("--- Error parsing disk settings"));
1477 return 0;
1480 vbd_rec->vm = &vm_record_opt;
1481 xen_vbd new_vbd;
1482 ccode = xen_vbd_create(session->xen, &new_vbd, vbd_rec);
1483 /* Set vm field of vbd record to NULL so it is not freed */
1484 vbd_rec->vm = NULL;
1485 xen_vbd_record_free(vbd_rec);
1486 if (!ccode)
1488 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1489 _SBLIM_TRACE(1,("--- xen_vdb_create failed: %s", error_msg));
1490 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1491 return 0;
1493 xen_vbd_free(new_vbd);
1495 /* Add object path to output array */
1496 *resultSetting = CMNewObjectPath(_BROKER, namespace, "Xen_DiskSettingData", NULL);
1497 snprintf(instId, MAX_INSTANCEID_LEN, "Xen:%s:Disk", vmRec->name_label);
1498 CMAddKey(*resultSetting, "InstanceID", (CMPIValue *)instId, CMPI_chars);
1500 else if (strcmp(settingclassname,"Xen_NetworkPortSettingData") == 0) {
1501 _SBLIM_TRACE(2,("--- adding Xen_NetworkPortSettingData to configuration"));
1502 xen_vif_record *vif_rec;
1503 if (!nic_rasd2vmconfig(instance, &vif_rec, status)) {
1504 /* status set in disk_rasd2vmconfig */
1505 _SBLIM_TRACE(1,("--- Error parsing network port settings"));
1506 return 0;
1509 vif_rec->vm = &vm_record_opt;
1510 xen_vif new_vif;
1511 ccode = xen_vif_create(session->xen, &new_vif, vif_rec);
1512 /* Set vm field of vif record to NULL so it is not freed */
1513 vif_rec->vm = NULL;
1514 xen_vif_record_free(vif_rec);
1515 if (!ccode)
1517 XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen);
1518 _SBLIM_TRACE(1,("--- xen_vif_create failed %s", error_msg));
1519 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_FAILED, error_msg);
1520 return 0;
1522 xen_vif_free(new_vif);
1524 /* Add object path to output array */
1525 *resultSetting = CMNewObjectPath(_BROKER, namespace, "Xen_NetworkPortSettingData", NULL);
1526 snprintf(instId, MAX_INSTANCEID_LEN, "Xen:%s:Vif", vmRec->name_label);
1527 CMAddKey(*resultSetting, "InstanceID", (CMPIValue *)instId, CMPI_chars);
1529 else {
1530 _SBLIM_TRACE(1,("--- Unrecognized setting data class - %s", settingclassname));
1531 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Unrecognized ResourceSetting");
1532 return 0;
1535 return 1;
1539 static void remove_vm(xen_vm vm)
1541 xen_vbd_set *vbds;
1542 xen_vif_set *vifs;
1543 int i;
1545 if (xen_vm_get_vbds(session->xen, &vbds, vm)) {
1546 for (i = 0; i < vbds->size; i++)
1547 xen_vbd_destroy(session->xen, vbds->contents[i]);
1550 if (xen_vm_get_vifs(session->xen, &vifs, vm)) {
1551 for (i = 0; i < vifs->size; i++)
1552 xen_vif_destroy(session->xen, vifs->contents[i]);
1555 xen_vm_destroy(session->xen, vm);
1559 static int vssd2xenconfig(CMPIInstance *vssd, xen_vm_record *vm_rec, CMPIStatus *status)
1561 CMPIData propertyvalue;
1562 char *vsType;;
1564 /*
1565 * Get domain name.
1566 * WARNING!
1567 * Do we fail if VirtualSystemIdentifier is not specified?
1568 */
1569 propertyvalue = CMGetProperty(vssd, "VirtualSystemIdentifier", status);
1570 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1571 _SBLIM_TRACE(1,("--- failed to retrieve VirtualSystemIndentifier property value"));
1572 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Unable to retrieve VirtualSystemIdentifier (name) property from virtual system setting data");
1573 return 0;
1575 vm_rec->name_label = strdup(CMGetCharPtr(propertyvalue.value.string));
1577 /* Get UUID, if specified */
1578 propertyvalue = CMGetProperty(vssd, "UUID", status);
1579 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue))
1580 vm_rec->uuid = strdup(CMGetCharPtr(propertyvalue.value.string));
1582 propertyvalue = CMGetProperty(vssd, "localtime", status);
1583 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue))
1584 vm_rec->platform_localtime = propertyvalue.value.boolean;
1586 propertyvalue = CMGetProperty(vssd, "OnPoweroff", status);
1587 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue))
1588 vm_rec->actions_after_shutdown = propertyvalue.value.uint16;
1590 propertyvalue = CMGetProperty(vssd, "OnReboot", status);
1591 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue))
1592 vm_rec->actions_after_reboot = propertyvalue.value.uint16;
1594 propertyvalue = CMGetProperty(vssd, "OnCrash", status);
1595 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue))
1596 vm_rec->actions_after_crash = propertyvalue.value.uint16;
1598 /* Paravirtual or HVM domain? */
1599 propertyvalue = CMGetProperty(vssd, "VirtualSystemType", status);
1600 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1601 _SBLIM_TRACE(1,("--- VirtualSystemType not specified"));
1602 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER,
1603 "VirtualSystemType not specified");
1604 return 0;
1607 vsType = CMGetCharPtr(propertyvalue.value.string);
1608 if (vsType == NULL) {
1609 _SBLIM_TRACE(1,("--- VirtualSystemType not specified"));
1610 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER,
1611 "VirtualSystemType not specified");
1612 return 0;
1615 if (strcasecmp(vsType, "Xen Paravirtual") == 0) {
1617 propertyvalue = CMGetProperty(vssd, "Bootloader", status);
1618 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1619 vm_rec->pv_bootloader = strdup(CMGetCharPtr(propertyvalue.value.string));
1621 /* If Bootloader specified, get options. */
1622 /* WARNING!
1623 * Should we ensure that BootloaderOptions is specified if
1624 * Bootloader is specified? For domUloader probably but
1625 * pygrub should work with no options.
1626 */
1627 propertyvalue = CMGetProperty(vssd, "BootloaderOptions", status);
1628 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1629 vm_rec->pv_bootloader_args = strdup(CMGetCharPtr(propertyvalue.value.string));
1633 /* Only honor Kernel if Bootloader not specified. */
1634 propertyvalue = CMGetProperty(vssd, "Kernel", status);
1635 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue) &&
1636 vm_rec->pv_bootloader == NULL) {
1637 vm_rec->pv_kernel = strdup(CMGetCharPtr(propertyvalue.value.string));
1639 /* If Kernel specified, get RAMDisk. */
1640 /* WARNING!
1641 * Should we ensure that RAMDisk is specified if
1642 * Kernel is specified? For Unix'ish guests we probably need
1643 * RAMDisk but what about OS's with no notion of RAMDisk?
1644 */
1645 propertyvalue = CMGetProperty(vssd, "RAMDisk", status);
1646 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1647 vm_rec->pv_ramdisk = strdup(CMGetCharPtr(propertyvalue.value.string));
1651 propertyvalue = CMGetProperty(vssd, "KernelOptions", status);
1652 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1653 vm_rec->pv_args = strdup(CMGetCharPtr(propertyvalue.value.string));
1656 else if (strcasecmp(vsType, "Xen HVM") == 0) {
1657 propertyvalue = CMGetProperty(vssd, "BootOrder", status);
1658 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1659 _SBLIM_TRACE(1,("--- No BootOrder specified for HVM guest"));
1660 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER,
1661 "No BootOrder specified for HVM guest");
1662 return 0;
1664 vm_rec->hvm_boot = strdup(CMGetCharPtr(propertyvalue.value.string));
1666 propertyvalue = CMGetProperty(vssd, "stdvga", status);
1667 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue))
1668 vm_rec->platform_std_vga = propertyvalue.value.boolean;
1670 else {
1671 _SBLIM_TRACE(1,("--- Invalid VirtualSystemType %s specified", vsType));
1672 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER,
1673 "Invalid VirtualSystemType specified");
1674 return 0;
1677 return 1;
1681 static int proc_rasd2vmconfig(CMPIInstance *proc_rasd, xen_vm_record *vm_rec,
1682 CMPIStatus *status)
1684 CMPIData propertyvalue;
1686 propertyvalue = CMGetProperty(proc_rasd, "VirtualQuantity", status);
1687 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1688 /*
1689 * TODO:
1690 * 1. Handle defaults if processors not specified.
1691 * 2. CPU weights and other scheduling parameters?
1692 */
1693 vm_rec->vcpus_number = propertyvalue.value.uint64;
1695 return 1;
1698 return 0;
1702 static int mem_rasd2vmconfig(CMPIInstance *mem_rasd, xen_vm_record *vm_rec,
1703 CMPIStatus *status)
1705 CMPIData propertyvalue;
1707 propertyvalue = CMGetProperty(mem_rasd, "VirtualQuantity", status);
1708 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1709 /*
1710 * TODO:
1711 * 1. Handle defaults if memory is not specified.
1712 * 2. What is semantics of these fields? For now
1713 * set them all to the requested value.
1714 */
1715 vm_rec->memory_static_max = propertyvalue.value.uint64;
1716 vm_rec->memory_dynamic_max = propertyvalue.value.uint64;
1717 vm_rec->memory_static_min = propertyvalue.value.uint64;
1718 vm_rec->memory_dynamic_min = propertyvalue.value.uint64;
1720 return 1;
1723 return 0;
1727 static int disk_rasd2vmconfig(CMPIInstance *disk_rasd, xen_vbd_record **vbd_rec,
1728 CMPIStatus *status)
1730 CMPIData propertyvalue;
1732 propertyvalue = CMGetProperty(disk_rasd, "DiskConfigInfo", status);
1733 if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1734 *vbd_rec = xen_vbd_record_alloc();
1735 if (*vbd_rec == NULL) {
1736 _SBLIM_TRACE(1,("--- Cannot malloc memory for virtual block device"));
1737 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, "Unable to malloc memory");
1738 return 0;
1741 /* Extract the image, dev and mode params from the DiskConfigInfo string */
1742 char *tok;
1743 char *next_tok;
1744 char *string = strdup(CMGetCharPtr(propertyvalue.value.string));
1745 tok = strtok_r(string, ",", &next_tok);
1746 if (tok == NULL) {
1747 _SBLIM_TRACE(1,("--- Malformed DiskConfigInfo property in disk setting data"));
1748 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid disk setting data");
1749 free(string);
1750 goto Error;
1752 (*vbd_rec)->image = strdup(tok);
1754 if ((tok = strtok_r(NULL, ",", &next_tok)) == NULL) {
1755 _SBLIM_TRACE(1,("--- Malformed DiskConfigInfo property in disk setting data"));
1756 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid disk setting data");
1757 free(string);
1758 goto Error;
1760 (*vbd_rec)->device = strdup(tok);
1762 if ((tok = strtok_r(NULL, ",", &next_tok)) == NULL) {
1763 _SBLIM_TRACE(1,("--- Malformed DiskConfigInfo property in disk setting data"));
1764 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid disk setting data");
1765 free(string);
1766 goto Error;
1768 if (strcmp(tok, "w") == 0)
1769 (*vbd_rec)->mode = XEN_VBD_MODE_RW;
1770 else
1771 (*vbd_rec)->mode = XEN_VBD_MODE_RO;
1773 free(string);
1774 _SBLIM_TRACE(2,("--- uname = %s", (*vbd_rec)->image));
1775 _SBLIM_TRACE(2,("--- dev = %s", (*vbd_rec)->device));
1776 _SBLIM_TRACE(2,("--- mode = %s",
1777 (*vbd_rec)->mode == XEN_VBD_MODE_RW ? "RW" : "RO"));
1779 return 1;
1782 Error:
1783 /* frees fields as well */
1784 xen_vbd_record_free(*vbd_rec);
1785 *vbd_rec = NULL;
1787 return 0;
1791 static int nic_rasd2vmconfig(CMPIInstance *nic_rasd, xen_vif_record **vif_rec,
1792 CMPIStatus *status)
1794 CMPIData propertyvalue;
1796 /* Set the domain config data from the Xen_NetworkPortSettingData. */
1797 propertyvalue = CMGetProperty(nic_rasd, "NICConfigInfo", status);
1798 if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1799 _SBLIM_TRACE(1,("--- No NICConfigInfo specified in network port setting data"));
1800 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "No NICConfigInfo specified in network port setting data");
1802 *vif_rec = NULL;
1803 return 0;
1806 *vif_rec = xen_vif_record_alloc();
1807 if (*vif_rec == NULL) {
1808 _SBLIM_TRACE(1,("--- Cannot malloc memory for virtual network device"));
1809 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, "Unable to malloc memory");
1810 return 0;
1813 /* Extract the type, mac and network params from the NicConfigInfo
1814 * string. Expected syntax is "key=value,key=value" */
1815 char * next_tok;
1816 char * string = strdup(CMGetCharPtr(propertyvalue.value.string));
1817 char *tok = strtok_r(string, ",", &next_tok);
1818 while (tok) {
1819 if (strncmp(tok, "mac", 3) == 0)
1820 (*vif_rec)->mac = strdup(tok + 4);
1821 /*
1822 * TODO:
1823 * Map other config items in the NIC info to xen_vif_record struct.
1824 */
1826 tok = strtok_r(NULL, ",", &next_tok);
1829 free(string);
1830 _SBLIM_TRACE(2,("--- mac = %s",
1831 (*vif_rec)->mac ? (*vif_rec)->mac : "not specified"));
1833 return 1;
1837 static int con_rasd2vmconfig(CMPIInstance *con_rasd, xen_console_record *con_rec,
1838 CMPIStatus *status)
1840 CMPIStatus local_status = {CMPI_RC_OK, NULL};
1841 CMPIData propertyvalue;
1843 propertyvalue = CMGetProperty(con_rasd, "Protocol", &local_status);
1844 if ((local_status.rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
1845 _SBLIM_TRACE(1,("--- No protocol field specified in console setting data"));
1846 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "No protocol specified in console setting data");
1847 return 0;
1850 switch (propertyvalue.value.uint16) {
1851 case 0:
1852 con_rec->protocol = XEN_CONSOLE_PROTOCOL_VT100;
1853 break;
1854 case 1:
1855 con_rec->protocol = XEN_CONSOLE_PROTOCOL_RFB;
1856 break;
1857 case 2:
1858 con_rec->protocol = XEN_CONSOLE_PROTOCOL_RDP;
1859 break;
1860 default:
1861 _SBLIM_TRACE(1,("--- Invalid protocol specified in console setting data"));
1862 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid protocol specified in console setting data");
1863 return 0;
1866 /*
1867 * Get any additional config from ConsoleConfigInfo.
1868 * Expected syntax is "key=value,key=value"
1869 */
1870 propertyvalue = CMGetProperty(con_rasd, "ConsoleConfigInfo", &local_status);
1871 if ((local_status.rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
1872 /* Count number of config items */
1873 int num_items = 0;
1874 char *next_tok;
1875 char *string = strdup(CMGetCharPtr(propertyvalue.value.string));
1876 char *tok = strtok_r(string, ",", &next_tok);
1877 while (tok) {
1878 num_items++;
1879 tok = strtok_r(NULL, ",", &next_tok);
1881 free(string);
1883 xen_string_string_map *con_params = xen_string_string_map_alloc(num_items);
1884 if (con_params == NULL) {
1885 _SBLIM_TRACE(1,("--- Cannot malloc memory for console options"));
1886 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERROR_SYSTEM, "Unable to malloc memory");
1887 return 0;
1890 /*
1891 * Go back through the options and populate the string map.
1892 */
1893 string = strdup(CMGetCharPtr(propertyvalue.value.string));
1894 tok = strtok_r(string, ",", &next_tok);
1895 /* If tok is NULL, then string contains only 1 key/value pair */
1896 if (tok == NULL)
1897 tok = string;
1898 int i = 0;
1899 while (tok) {
1900 char *val = strchr(tok, '=');
1901 if (val == NULL) {
1902 _SBLIM_TRACE(1,("--- Invalid console option specified in console setting data"));
1903 CMSetStatusWithChars(_BROKER, status, CMPI_RC_ERR_INVALID_PARAMETER, "Invalid console option specified in console setting data");
1904 xen_string_string_map_free(con_params);
1905 free(string);
1906 return 0;
1908 *val = '\0';
1909 val++;
1910 con_params->contents[i].key = strdup(tok);
1911 con_params->contents[i].val = strdup(val);
1912 i++;
1913 tok = strtok_r(NULL, ",", &next_tok);
1916 con_rec->other_config = con_params;
1917 free(string);
1920 return 1;
1925 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1926 // THE FOLLOWING CODE IS REQUIRED UNTIL EMBEDDEDOBJECT SUPPORT IS WORKING!!!
1927 //
1928 static CMPIInstance *parse_embedded_instance(char *instanceStr)
1930 char filename[L_tmpnam];
1931 FILE *fd;
1932 int rc;
1933 CMPIInstance *instance;
1935 _SBLIM_TRACE(2,("--- parsing instance: \"%s\"", instanceStr));
1937 /* Store the embedded Xen_*SettingData string data in a file for parsing */
1938 tmpnam(filename);
1939 fd = fopen(filename, "w");
1940 fprintf(fd, "%s", instanceStr);
1941 fclose(fd);
1943 fd = fopen(filename, "r");
1944 Xen_SettingDatayyrestart(fd);
1946 /* Parse the embedded Xen_*SettingData string data into a CMPIInstance */
1947 rc = Xen_SettingDatayyparseinstance(_BROKER, &instance);
1948 fclose(fd);
1949 remove(filename);
1950 if (rc != 0) { /* parser returns zero for success, non-zero for error */
1951 _SBLIM_TRACE(1,("--- error parsing instance"));
1952 return NULL;
1955 _SBLIM_TRACE(2,("--- parsed instance: \"%s\"",
1956 CMGetCharPtr(CDToString(_BROKER, instance, NULL))));
1957 return instance;