os-cmpi-xen

view src/Xen_OperatingSystem_Resource.c @ 121:4868ace2726b

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

Signed-off-by: Luke Szymanski <Lukasz.Szymanski@Unisys.com>
author Jim Fehlig <jfehlig@novell.com>
date Fri Jun 08 10:22:05 2007 -0600 (2007-06-08)
parents b66e845be7e4
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 // Rajagopalan Subrahmanian <raj.subrahmanian@unisys.com>
20 // Description:
21 // ============================================================================
23 #include <stdlib.h>
24 #include <strings.h>
25 #include <assert.h>
27 #include <xen_common.h>
28 #include <xen_vm.h>
29 #include <xen_vm_metrics.h>
31 #include "Xen_OperatingSystem_Resource.h"
33 /* Include the required CMPI data types, function headers, and macros. */
34 #include "cmpidt.h"
35 #include "cmpift.h"
36 #include "cmpimacs.h"
38 #include "xen_utils.h"
40 // ----------------------------------------------------------------------------
42 /* Get a handle to the list of all system resources for this class. */
43 int Xen_OperatingSystem_getResources(xen_utils_session *session,
44 _RESOURCES ** resources)
45 {
46 return xen_utils_get_domain_resources(session, resources);
47 }
49 // ----------------------------------------------------------------------------
51 /* Free/deallocate/cleanup the resources list after use. */
52 int Xen_OperatingSystem_freeResources(_RESOURCES * resources)
53 {
54 return xen_utils_free_domain_resources(resources);
55 }
57 // ----------------------------------------------------------------------------
59 /* Iterator to get the next resource from the resources list. */
60 int Xen_OperatingSystem_getNextResource(xen_utils_session *session,
61 _RESOURCES * resources,
62 _RESOURCE ** resource)
63 {
64 return xen_utils_get_next_domain_resource(session, resources, resource);
65 }
67 // ----------------------------------------------------------------------------
69 /* Get the specific resource that matches the CMPI object path. */
70 int Xen_OperatingSystem_getResourceForObjectPath(xen_utils_session *session,
71 _RESOURCES * resources,
72 _RESOURCE ** resource,
73 const CMPIObjectPath * objectpath)
74 {
75 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
77 /* Check input params are valid. */
78 if (resources == NULL) return 0;
79 if (CMIsNullObject(objectpath)) return 0;
81 /* Obtain the target resource identifier from the CMPIObjectPath key(s). */
82 CMPIData iddata = CMGetKey(objectpath, "CSName", &status);
83 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(iddata)) return 0;
85 /* Extract the id string from the CMPIString. */
86 char * id = CMGetCharPtr(iddata.value.string);
87 if ((id == NULL) || (*id == '\0')) return 0;
89 /* Get the domain data for the target domain name. */
90 xen_vm_set *vms;
91 if (!xen_vm_get_by_name_label(session->xen, &vms, id)) {
92 /* Error is in session object! */
93 *resource = NULL;
94 return 0;
95 }
97 assert(vms->size == 1);
98 if (!xen_vm_get_record(session->xen, resource, vms->contents[0])) {
99 /* Error description in session object! */
100 xen_vm_set_free(vms);
101 return 0;
102 }
104 xen_vm_set_free(vms);
106 return 1;
107 }
109 // ----------------------------------------------------------------------------
111 /* Free/deallocate/cleanup the resource after use. */
112 int Xen_OperatingSystem_freeResource(_RESOURCE * resource)
113 {
114 return xen_utils_free_domain_resource(resource);
115 }
117 // ----------------------------------------------------------------------------
119 /* Set the property values of a CMPI instance from a specific resource. */
120 int Xen_OperatingSystem_setInstanceFromResource(xen_utils_session *session,
121 _RESOURCE * resource,
122 const CMPIInstance * instance,
123 const CMPIBroker * broker)
124 {
125 CMPIArray * status = CMNewArray(broker, 1, CMPI_uint16, NULL);
126 int statusvalue;
128 if (resource == NULL) return 0;
129 if (CMIsNullObject(instance)) return 0;
131 /* Set the CMPIInstance properties from the resource data. */
132 CMSetProperty(instance, "CreationClassName",(CMPIValue *)"Xen_OperatingSystem", CMPI_chars);
133 CMSetProperty(instance, "Name",(CMPIValue *)"Linux", CMPI_chars);
134 CMSetProperty(instance, "CSCreationClassName",(CMPIValue *)"Xen_ComputerSystem", CMPI_chars);
135 CMSetProperty(instance, "CSName",(CMPIValue *)resource->name_label, CMPI_chars);
137 /* Set "OSType" = "LINUX" */
138 unsigned int ostype = 36;
139 CMSetProperty(instance, "OSType", (CMPIValue *)&ostype, CMPI_uint16);
141 /* Guess the OS version from the kernal filename, if available. */
142 if (resource->pv_kernel != NULL) {
143 char *version;
145 version = (char *)(rindex(resource->pv_kernel,'/'));
146 if (version == NULL)
147 version = resource->pv_kernel;
148 else
149 version++;
150 CMSetProperty(instance, "Version", (CMPIValue *)version, CMPI_chars);
151 }
153 /* Set the "Status" and "OperationalStatus" properties from the Xen domain state. */
154 switch (resource->power_state)
155 {
156 case XEN_VM_POWER_STATE_HALTED:
157 statusvalue = 15; // "Dormant"
158 CMSetProperty(instance, "Status",(CMPIValue *)"Stopped", CMPI_chars);
159 break;
161 case XEN_VM_POWER_STATE_PAUSED:
162 statusvalue = 15; // "Dormant"
163 CMSetProperty(instance, "Status",(CMPIValue *)"Stopped", CMPI_chars);
164 break;
166 case XEN_VM_POWER_STATE_RUNNING:
167 statusvalue = 2; // "OK"
168 CMSetProperty(instance, "Status",(CMPIValue *)"OK", CMPI_chars);
169 break;
171 case XEN_VM_POWER_STATE_SUSPENDED:
172 statusvalue = 10; // "Stopped"
173 // Status is just a string (and deprecated), so just say Suspended
174 CMSetProperty(instance, "Status",(CMPIValue *)"Suspended", CMPI_chars);
175 break;
176 /*
177 case XEN_VM_POWER_STATE_SHUTTINGDOWN:
178 statusvalue = 9; // "Stopping"
179 CMSetProperty(instance, "Status",(CMPIValue *)"Stopping", CMPI_chars);
180 break;
181 */
182 default:
183 statusvalue = 0; // "Unknown"
184 CMSetProperty(instance, "Status",(CMPIValue *)"No Contact", CMPI_chars);
185 break;
186 }
188 CMSetArrayElementAt(status, 0, (CMPIValue *)&statusvalue, CMPI_uint16);
189 CMSetProperty(instance, "OperationalStatus",(CMPIValue *)&status, CMPI_uint16A);
191 int distributed = 0;
192 CMSetProperty(instance, "Distributed", (CMPIValue *)&distributed, CMPI_boolean);
194 /*
195 * TODO:
196 * What memory_* field of xen_vm_record should we use here?
197 */
198 int64_t memory=0;
199 // Get the memory_actual
200 if (resource->metrics->is_record)
201 memory=resource->metrics->u.record->memory_actual;
202 else
203 xen_vm_metrics_get_memory_actual(session->xen, &memory, resource->metrics->u.handle);
205 CMSetProperty(instance, "TotalVisibleMemorySize", (CMPIValue *)&(memory), CMPI_uint64);
207 return 1;
208 }
210 // ----------------------------------------------------------------------------
212 /* Delete the specified resource from the system. */
213 int Xen_OperatingSystem_deleteResource(_RESOURCES * resources,
214 _RESOURCE * resource)
215 {
216 /*
217 * TODO:
218 * Should we support this? The Computer System Profile or similar
219 * should specify whether DeleteInstance on OperatingSystem is
220 * permitted.
221 *
222 * Do not support until this is researched.
223 */
225 /* Unsupported. */
226 return -1;
228 #if 0
229 if (resources == NULL) return 0;
230 if (resource == NULL) return 0;
232 /* First stop/destroy the running Xen domain, if necessary. */
233 if (resource->info != NULL) {
234 if (xm_destroy(resources->xen, resource->name) != 0) return 0;
235 }
237 /* Remove the Xen domain configuration from the system. */
238 if (xm_delete(resources->xen, resource->name) != 0) return 0;
240 return 1;
241 #endif
242 }
244 // ----------------------------------------------------------------------------
246 /* Modify the specified resource using the property values of a CMPI instance. */
247 int Xen_OperatingSystem_setResourceFromInstance(_RESOURCE * resource,
248 const CMPIInstance * instance,
249 const CMPIBroker * broker)
250 {
251 /* Unsupported. */
252 return -1;
253 }
255 // ----------------------------------------------------------------------------
257 /* Create a new resource using the property values of a CMPI instance. */
258 int Xen_OperatingSystem_createResourceFromInstance(_RESOURCES * resources,
259 _RESOURCE ** resource,
260 const CMPIInstance * instance,
261 const CMPIBroker * broker)
262 {
263 /*
264 * TODO:
265 * Should we support this? The Computer System Profile or similar
266 * should specify whether CreateInstance on OperatingSystem is
267 * permitted.
268 *
269 * Do not support until this is researched.
270 */
272 /* Unsupported. */
273 return -1;
275 #if 0
276 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
277 CMPIData propertyvalue;
279 /* malloc a new resource to store the domain config data. */
280 _RESOURCE * newresource = xen_vm_record_alloc();
281 if (newresource == NULL) return 0;
283 /* Set the resource values from the CMPIInstance properties. */
284 propertyvalue = CMGetProperty(instance, "Name", &status);
285 if ((status.rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
286 newresource->name_label = CMGetCharPtr(propertyvalue.value.string);
287 }
289 propertyvalue = CMGetProperty(instance, "Kernel", &status);
290 if ((status.rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
291 newresource->pv_kernel = CMGetCharPtr(propertyvalue.value.string);
292 }
294 propertyvalue = CMGetProperty(instance, "RAMDisk", &status);
295 if ((status.rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
296 newresource->pv_ramdisk = CMGetCharPtr(propertyvalue.value.string);
297 }
299 propertyvalue = CMGetProperty(instance, "Memory", &status);
300 if ((status.rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
301 newresource->memory_actual = propertyvalue.value.uint64;
302 }
304 propertyvalue = CMGetProperty(instance, "VCPUs", &status);
305 if ((status.rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
306 newresource->vcpus_number = (int)propertyvalue.value.uint32;
307 }
309 /*
310 propertyvalue = CMGetProperty(instance, "DHCP", &status);
311 if ((status.rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
312 newresource->dhcp = (bool)propertyvalue.value.boolean;
313 }
315 propertyvalue = CMGetProperty(instance, "Netmask", &status);
316 if ((status.rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
317 newresource->netmask = CMGetCharPtr(propertyvalue.value.string);
318 }
320 propertyvalue = CMGetProperty(instance, "Gateway", &status);
321 if ((status.rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
322 newresource->gateway = CMGetCharPtr(propertyvalue.value.string);
323 }
325 propertyvalue = CMGetProperty(instance, "Hostname", &status);
326 if ((status.rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
327 newresource->hostname = CMGetCharPtr(propertyvalue.value.string);
328 }
329 */
331 propertyvalue = CMGetProperty(instance, "KernelOptions", &status);
332 if ((status.rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
333 newresource->pv_args = CMGetCharPtr(propertyvalue.value.string);
334 }
336 /*
337 propertyvalue = CMGetProperty(instance, "Root", &status);
338 if ((status.rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
339 newresource->root = CMGetCharPtr(propertyvalue.value.string);
340 }
341 */
343 /* Create a new DomU entry in libxm from the config info. */
344 xen_vm vm;
345 if (!xen_vm_create(resources->xen, &vm, newresource)) {
346 xen_vm_record_free(newresource);
347 return 0;
348 }
350 xen_vm_record_free(newresource);
352 /* Get the actual registered domain data back for this domain for subsequent processing. */
353 *resource = NULL;
354 if (!xen_vm_get_record(resources->xen, resource, vm)) {
355 /* Error description in session object! */
356 xen_vm_free(vm);
357 return 0;
358 }
360 xen_vm_free(vm);
362 if (*resource == NULL) return 0;
364 return 1;
365 #endif
366 }