vbox: Rewrite vboxStorageVolLookupByKey

This commit is contained in:
Taowei Luo 2014-10-24 09:46:42 +08:00 committed by Michal Privoznik
parent 8a8fa504aa
commit f67ae174eb
3 changed files with 88 additions and 70 deletions

View File

@ -276,3 +276,70 @@ virStorageVolPtr vboxStorageVolLookupByName(virStoragePoolPtr pool, const char *
return ret;
}
virStorageVolPtr vboxStorageVolLookupByKey(virConnectPtr conn, const char *key)
{
vboxGlobalData *data = conn->privateData;
vboxIIDUnion hddIID;
unsigned char uuid[VIR_UUID_BUFLEN];
IHardDisk *hardDisk = NULL;
PRUnichar *hddNameUtf16 = NULL;
char *hddNameUtf8 = NULL;
PRUint32 hddstate;
nsresult rc;
virStorageVolPtr ret = NULL;
if (!data->vboxObj) {
return ret;
}
VBOX_IID_INITIALIZE(&hddIID);
if (!key)
return ret;
if (virUUIDParse(key, uuid) < 0) {
virReportError(VIR_ERR_INVALID_ARG,
_("Could not parse UUID from '%s'"), key);
return NULL;
}
vboxIIDFromUUID(&hddIID, uuid);
rc = gVBoxAPI.UIVirtualBox.GetHardDiskByIID(data->vboxObj, &hddIID, &hardDisk);
if (NS_FAILED(rc))
goto cleanup;
gVBoxAPI.UIMedium.GetState(hardDisk, &hddstate);
if (hddstate == MediaState_Inaccessible)
goto cleanup;
gVBoxAPI.UIMedium.GetName(hardDisk, &hddNameUtf16);
if (!hddNameUtf16)
goto cleanup;
VBOX_UTF16_TO_UTF8(hddNameUtf16, &hddNameUtf8);
if (!hddNameUtf8) {
VBOX_UTF16_FREE(hddNameUtf16);
goto cleanup;
}
if (vboxConnectNumOfStoragePools(conn) == 1) {
ret = virGetStorageVol(conn, "default-pool", hddNameUtf8, key,
NULL, NULL);
VIR_DEBUG("Storage Volume Pool: %s", "default-pool");
} else {
/* TODO: currently only one default pool and thus
* nothing here, change it when pools are supported
*/
}
VIR_DEBUG("Storage Volume Name: %s", key);
VIR_DEBUG("Storage Volume key : %s", hddNameUtf8);
VBOX_UTF8_FREE(hddNameUtf8);
VBOX_UTF16_FREE(hddNameUtf16);
cleanup:
VBOX_MEDIUM_RELEASE(hardDisk);
vboxIIDUnalloc(&hddIID);
return ret;
}

View File

@ -104,7 +104,10 @@ typedef IUSBDeviceFilters IUSBCommon;
#if VBOX_API_VERSION < 3001000
typedef IHardDiskAttachment IMediumAttachment;
#endif /* VBOX_API_VERSION < 3001000 */
#else /* VBOX_API_VERSION >= 3001000 */
typedef IMedium IHardDisk;
typedef IMediumAttachment IHardDiskAttachment;
#endif /* VBOX_API_VERSION >= 3001000 */
#include "vbox_uniformed_api.h"
@ -171,8 +174,6 @@ if (arg)\
#else /* VBOX_API_VERSION >= 3001000 */
typedef IMedium IHardDisk;
typedef IMediumAttachment IHardDiskAttachment;
# define MediaState_Inaccessible MediumState_Inaccessible
# define HardDiskVariant_Standard MediumVariant_Standard
# define HardDiskVariant_Fixed MediumVariant_Fixed
@ -2033,72 +2034,6 @@ _registerDomainEvent(virHypervisorDriverPtr driver)
* The Storage Functions here on
*/
static virStorageVolPtr
vboxStorageVolLookupByKey(virConnectPtr conn, const char *key)
{
VBOX_OBJECT_CHECK(conn, virStorageVolPtr, NULL);
vboxIID hddIID = VBOX_IID_INITIALIZER;
unsigned char uuid[VIR_UUID_BUFLEN];
IHardDisk *hardDisk = NULL;
nsresult rc;
if (!key)
return ret;
if (virUUIDParse(key, uuid) < 0) {
virReportError(VIR_ERR_INVALID_ARG,
_("Could not parse UUID from '%s'"), key);
return NULL;
}
vboxIIDFromUUID(&hddIID, uuid);
#if VBOX_API_VERSION < 4000000
rc = data->vboxObj->vtbl->GetHardDisk(data->vboxObj, hddIID.value, &hardDisk);
#elif VBOX_API_VERSION >= 4000000 && VBOX_API_VERSION < 4002000
rc = data->vboxObj->vtbl->FindMedium(data->vboxObj, hddIID.value,
DeviceType_HardDisk, &hardDisk);
#else
rc = data->vboxObj->vtbl->OpenMedium(data->vboxObj, hddIID.value,
DeviceType_HardDisk, AccessMode_ReadWrite,
PR_FALSE, &hardDisk);
#endif /* VBOX_API_VERSION >= 4000000 */
if (NS_SUCCEEDED(rc)) {
PRUint32 hddstate;
VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetState, &hddstate);
if (hddstate != MediaState_Inaccessible) {
PRUnichar *hddNameUtf16 = NULL;
char *hddNameUtf8 = NULL;
VBOX_MEDIUM_FUNC_ARG1(hardDisk, GetName, &hddNameUtf16);
VBOX_UTF16_TO_UTF8(hddNameUtf16, &hddNameUtf8);
if (hddNameUtf8) {
if (vboxConnectNumOfStoragePools(conn) == 1) {
ret = virGetStorageVol(conn, "default-pool", hddNameUtf8, key,
NULL, NULL);
VIR_DEBUG("Storage Volume Pool: %s", "default-pool");
} else {
/* TODO: currently only one default pool and thus
* nothing here, change it when pools are supported
*/
}
VIR_DEBUG("Storage Volume Name: %s", key);
VIR_DEBUG("Storage Volume key : %s", hddNameUtf8);
VBOX_UTF8_FREE(hddNameUtf8);
VBOX_UTF16_FREE(hddNameUtf16);
}
}
VBOX_MEDIUM_RELEASE(hardDisk);
}
vboxIIDUnalloc(&hddIID);
return ret;
}
static virStorageVolPtr
vboxStorageVolLookupByPath(virConnectPtr conn, const char *path)
{
@ -3660,6 +3595,20 @@ _virtualboxOpenMedium(IVirtualBox *vboxObj ATTRIBUTE_UNUSED,
#endif
}
static nsresult
_virtualboxGetHardDiskByIID(IVirtualBox *vboxObj, vboxIIDUnion *iidu, IHardDisk **hardDisk)
{
#if VBOX_API_VERSION < 4000000
return vboxObj->vtbl->GetHardDisk(vboxObj, IID_MEMBER(value), hardDisk);
#elif VBOX_API_VERSION >= 4000000 && VBOX_API_VERSION < 4002000
return vboxObj->vtbl->FindMedium(vboxObj, IID_MEMBER(value), DeviceType_HardDisk,
hardDisk);
#else /* VBOX_API_VERSION >= 4002000 */
return vboxObj->vtbl->OpenMedium(vboxObj, IID_MEMBER(value), DeviceType_HardDisk,
AccessMode_ReadWrite, PR_FALSE, hardDisk);
#endif /* VBOX_API_VERSION >= 4002000 */
}
static nsresult
_virtualboxFindDHCPServerByNetworkName(IVirtualBox *vboxObj, PRUnichar *name, IDHCPServer **server)
{
@ -5340,6 +5289,7 @@ static vboxUniformedIVirtualBox _UIVirtualBox = {
.RegisterMachine = _virtualboxRegisterMachine,
.FindMedium = _virtualboxFindMedium,
.OpenMedium = _virtualboxOpenMedium,
.GetHardDiskByIID = _virtualboxGetHardDiskByIID,
.FindDHCPServerByNetworkName = _virtualboxFindDHCPServerByNetworkName,
.CreateDHCPServer = _virtualboxCreateDHCPServer,
.RemoveDHCPServer = _virtualboxRemoveDHCPServer,

View File

@ -198,6 +198,7 @@ typedef struct {
nsresult (*RegisterMachine)(IVirtualBox *vboxObj, IMachine *machine);
nsresult (*FindMedium)(IVirtualBox *vboxObj, PRUnichar *location, PRUint32 deviceType, PRUint32 accessMode, IMedium **medium);
nsresult (*OpenMedium)(IVirtualBox *vboxObj, PRUnichar *location, PRUint32 deviceType, PRUint32 accessMode, IMedium **medium);
nsresult (*GetHardDiskByIID)(IVirtualBox *vboxObj, vboxIIDUnion *iidu, IHardDisk **hardDisk);
nsresult (*FindDHCPServerByNetworkName)(IVirtualBox *vboxObj, PRUnichar *name, IDHCPServer **server);
nsresult (*CreateDHCPServer)(IVirtualBox *vboxObj, PRUnichar *name, IDHCPServer **server);
nsresult (*RemoveDHCPServer)(IVirtualBox *vboxObj, IDHCPServer *server);
@ -598,7 +599,7 @@ virStoragePoolPtr vboxStoragePoolLookupByName(virConnectPtr conn, const char *na
int vboxStoragePoolNumOfVolumes(virStoragePoolPtr pool);
int vboxStoragePoolListVolumes(virStoragePoolPtr pool, char **const names, int nnames);
virStorageVolPtr vboxStorageVolLookupByName(virStoragePoolPtr pool, const char *name);
virStorageVolPtr vboxStorageVolLookupByKey(virConnectPtr conn, const char *key);
/* Version specified functions for installing uniformed API */
void vbox22InstallUniformedAPI(vboxUniformedAPI *pVBoxAPI);