Пример #1
0
// automatically generated. Do not modify
static void testVDIs(xen_session *session) {
	printf("\nGet all the  VDI Records\n");

	xen_vdi_set *vdi_set;
	bool d = xen_vdi_get_all(session, &vdi_set);
	if (d) {
		size_t i = 0;
		for (; i < vdi_set->size; ++i) {
			xen_vdi_record *vdi_record;
			xen_vdi_get_record(session, &vdi_record, vdi_set->contents[i]);
			char *handle = (char*) vdi_record->handle;
			char *opaque = (char*) vdi_set->contents[i];
			if (strcmp(handle, opaque) == 0)
				printf("identical: %s\n", opaque);
			else
				printf("record->handle[%s] v.s. set->contents[%d][%s]\n",
						opaque, i, handle);
			xen_vdi_record_free(vdi_record);
		}
	} else {
		print_error(session);
	}
	xen_vdi_set_free(vdi_set);

	printf("\n==============================================\n\n");
}
void
xen_vdi_xen_vdi_record_map_free(xen_vdi_xen_vdi_record_map *map)
{
    if (map == NULL)
    {
        return;
    }

    size_t n = map->size;
    for (size_t i = 0; i < n; i++)
    {
        xen_vdi_record_opt_free(map->contents[i].key);
        xen_vdi_record_free(map->contents[i].val);
    }

    free(map);
}
Пример #3
0
/******************************************************************************
* disk_rasd_to_vbd
*
* This function attempts to parse a Xen_Disk CIM instance and populate a new
* VBD record. It also gets the record to the VDI that the VBD attaches to (could
* be a newly created VDI, if requested) and a handle to an existing SR as
* pointed to by the PoolID field in the CIM instance
*
* Returns 1 on Success and 0 on failure.
*******************************************************************************/
int disk_rasd_to_vbd(
    const CMPIBroker *broker,
    xen_utils_session* session,
    CMPIInstance *disk_rasd,
    xen_vbd_record **vbd_rec,
    xen_vdi_record **vdi_rec,
    xen_sr  *sr,
    CMPIStatus *status)
{
    CMPIData propertyvalue;
    char *error_msg = "ERROR: Unknown error";
    char *sr_label = NULL, *vdi_name_label = NULL, *vdi_name_desc = NULL;
    char *vbd_device = NULL, *vbd_uuid = NULL, *vdi_uuid = NULL;
    enum xen_vbd_type vbd_type = XEN_VBD_TYPE_DISK; /* default with Disk (as opposed to CDRom) */
    enum xen_vdi_type vdi_type = XEN_VDI_TYPE_USER;
    bool vbd_readonly = false, vbd_bootable = false;/* defaults for Disk type */
    int vbd_mode= XEN_VBD_MODE_RW;                  /* defaults for Disk type */
    int64_t disk_size = -1;                         /* for CDRoms - this is the size expected */
    char buf[MAX_INSTANCEID_LEN];

    *vbd_rec  = NULL;
    *vdi_rec  = NULL;

    /* only resource types of 15,16 and 19 are currently supported */
    int rc = CMPI_RC_ERR_INVALID_PARAMETER;
    propertyvalue = CMGetProperty(disk_rasd, "ResourceType", status);
    if ((status->rc != CMPI_RC_OK) || CMIsNullValue(propertyvalue)) {
        error_msg = "ERROR: Xen_DiskSettingData has no ResourceType";
        goto Error;
    }
    int res_type = propertyvalue.value.uint16;
    if ((res_type == DMTF_ResourceType_CD_Drive) || (res_type == DMTF_ResourceType_DVD_drive)) {
        vbd_mode = XEN_VBD_MODE_RO;
        vbd_type = XEN_VBD_TYPE_CD;
    }
    else if ((res_type == DMTF_ResourceType_Storage_Extent) || (res_type == DMTF_ResourceType_Disk_Drive))
        vbd_type = XEN_VBD_TYPE_DISK;
    else {
        error_msg = "ERROR: Xen_DiskSettingData has unsupported ResourceType";
        goto Error;
    }

    /* Get the instance ID which has the device's UUID in it, if its available
      This will be usd during deletes and not used during create*/
    propertyvalue = CMGetProperty(disk_rasd, "InstanceID", status);
    if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
        _CMPIStrncpyDeviceNameFromID(buf, CMGetCharPtr(propertyvalue.value.string),
            sizeof(buf)/sizeof(buf[0]));
        vbd_uuid = strdup(buf);
    }

    /* The HostResource property is used to identify the VDI, if available */
    propertyvalue = CMGetProperty(disk_rasd, "HostResource", status);
    if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
        /* HostResource is always in WBEM URI format and it points to a 
          Xen_DiskImage object reference. Convert it to CMPIObjectPath */
        CMPIData data = CMGetArrayElementAt(propertyvalue.value.array, 0, NULL);
        CMPIObjectPath *obj_path = xen_utils_WBEM_URI_to_CMPIObjectPath(
                                       broker, 
                                       CMGetCharPtr(data.value.string));
        if (obj_path) {
            /* this should be a Xen_DiskImage object reference */
            /* Get the DeviceID key */
            propertyvalue = CMGetKey(obj_path, "DeviceID", status);
            if ((status->rc == CMPI_RC_OK) && 
                !CMIsNullValue(propertyvalue) && 
                propertyvalue.type == CMPI_string) {
                memset(buf, 0, sizeof(buf));
                _CMPIStrncpyDeviceNameFromID(buf, CMGetCharPtr(propertyvalue.value.string),
                                             sizeof(buf)/sizeof(buf[0]));
                vdi_uuid = strdup(buf);
            }
        }
    }
    if (!vdi_uuid) {
        /* second chance, Try the HostExtentName */
        propertyvalue = CMGetProperty(disk_rasd, "HostExtentName", status);
        if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue))
            vdi_uuid = strdup(CMGetCharPtr(propertyvalue.value.string));
    }
    /* Device is specified as the address on the host bus */
    propertyvalue = CMGetProperty(disk_rasd, "AddressOnParent", status);
    if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue))
        vbd_device = strdup(CMGetCharPtr(propertyvalue.value.string));

    propertyvalue = CMGetProperty(disk_rasd, "Bootable", status);
    if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue))
        vbd_bootable = propertyvalue.value.boolean;

    propertyvalue = CMGetProperty(disk_rasd, "Access", status);
    if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
        if (propertyvalue.value.uint16 == Access_Readable)
            vbd_mode = XEN_VBD_MODE_RO;
    }

    /*
    * The Pool ID property is used to identify the Xen SR
    * This could be NULL if VDIs are reused or if
    * the VBD has to be created on the default SR.
    */
    propertyvalue = CMGetProperty(disk_rasd, "PoolID", status);
    if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
        char *poolid = CMGetCharPtr(propertyvalue.value.string);
        if (poolid && (*poolid != '\0'))
            sr_label = strdup(poolid);
    }

    propertyvalue = CMGetProperty(disk_rasd, "ElementName", status);
    if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue))
        vdi_name_label = strdup(CMGetCharPtr(propertyvalue.value.string));

    propertyvalue = CMGetProperty(disk_rasd, "Description", status);
    if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue))
        vdi_name_desc = strdup(CMGetCharPtr(propertyvalue.value.string));

    /* Get the disk size from the CIM instance -
    * If this is a new disk, this property is required
    * If we are instantiating a CDRom VDI, this is not required */
    int64_t multiplier = 1; /* default to bytes */
    propertyvalue = CMGetProperty(disk_rasd, "AllocationUnits", status);
    if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue)) {
        char *units = CMGetCharPtr(propertyvalue.value.string);
        if ((multiplier = xen_utils_get_alloc_units(units)) == 0) {
            error_msg = "ERROR: Xen_DiskSettingData has unsupported AllocationUnits";
            goto Error;
        }
    }

    /* Limit and VirtualQuantity properties mean the same when we create the VBD */
    propertyvalue = CMGetProperty(disk_rasd, "VirtualQuantity", status);
    if ((status->rc == CMPI_RC_OK) && !CMIsNullValue(propertyvalue))
        disk_size = (propertyvalue.value.uint64) * multiplier;

    if (vbd_type == XEN_VBD_TYPE_CD)
        vdi_type = XEN_VDI_TYPE_USER;

    /* We have enough information... create the Xen device records so we can */
    /* create new devices or identify existing ones */
    /* Create the VDI device record */
    rc = CMPI_RC_ERR_FAILED;
    *vdi_rec = xen_vdi_record_alloc();
    if (*vdi_rec == NULL) {
        error_msg = "ERROR: Unable to malloc memory";
        goto Error;
    }

    (*vdi_rec)->virtual_size  = disk_size;
#if OSS_XENAPI
    (*vdi_rec)->other_config  = xen_string_string_map_alloc(0);
    vdi_location = "";
    (*vdi_rec)->other_config->contents[0].key= strdup("location");
    (*vdi_rec)->other_config->contents[0].val = strdup(vdi_location);
    (*vdi_rec)->other_config->size = 1;
#else
    (*vdi_rec)->other_config  = xen_string_string_map_alloc(0);
    (*vdi_rec)->other_config->size = 0;
#endif
    (*vdi_rec)->type          = vdi_type;
    (*vdi_rec)->read_only     = vbd_readonly;
    (*vdi_rec)->name_label    = vdi_name_label;
    (*vdi_rec)->name_description = vdi_name_desc;

#if XENAPI_VERSION > 400
    (*vdi_rec)->managed = true;
#endif

    /* If VDI has already been created, use it */
    if (vdi_uuid) {
        _SBLIM_TRACE(_SBLIM_TRACE_LEVEL_INFO, ("VDI specified in the RASD: -%s-", vdi_uuid));
        (*vdi_rec)->uuid = vdi_uuid;
    }

    /* create the VBD record */
    *vbd_rec = xen_vbd_record_alloc();
    if (*vbd_rec == NULL) {
        error_msg = "ERROR: Unable to malloc memory";
        goto Error;
    }

    if (vbd_uuid)
        (*vbd_rec)->uuid = vbd_uuid;

#if XENAPI_VERSION > 400
    if (vbd_device)
        (*vbd_rec)->userdevice = vbd_device;
    (*vbd_rec)->other_config = xen_string_string_map_alloc(0);
#endif
    (*vbd_rec)->bootable = vbd_bootable;
    (*vbd_rec)->mode = vbd_mode;
    (*vbd_rec)->type = vbd_type;
    (*vbd_rec)->qos_algorithm_params = xen_string_string_map_alloc(0);

    if (vbd_type == XEN_VBD_TYPE_CD)
        (*vdi_rec)->sharable = true;
    else
        (*vdi_rec)->sharable = false;

    /* Identify the Storage Repository where this VDI will be created (if its being created) */
    if (sr_label) {
        if (!_get_sr(broker, session, sr_label, true, sr, status)) {
            _SBLIM_TRACE(_SBLIM_TRACE_LEVEL_ERROR,
                ("--- getting SR for %s failed", sr_label));
            goto Error;
        }
        free(sr_label);
    }
    return 1;

    Error:
    if (sr_label)
        free(sr_label);
    if (vdi_name_label) {
        free(vdi_name_label);
        if (*vdi_rec)
            (*vdi_rec)->name_label = NULL;
    }
    if(vdi_name_desc) {
        free(vdi_name_desc);
    }
    if(vbd_device) {
        free(vbd_device);
    }
    if(vbd_uuid) {
        free(vbd_uuid);
    }

    /* frees fields as well */
    if (*vbd_rec) {
        xen_vbd_record_free(*vbd_rec);
        *vbd_rec = NULL;
    }
    if (*vdi_rec) {
        xen_vdi_record_free(*vdi_rec);
        *vdi_rec = NULL;
    }
    xen_utils_set_status(broker, status, rc, error_msg, session->xen);
    return 0;
}