mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-24 22:55:23 +00:00
storage: Introduce APIs to search/scan storage pool volumes list
Introduce virStoragePoolObjForEachVolume to scan each volume calling the passed callback function until all volumes have been processed in the storage pool volume list, unless the callback function returns an error. Introduce virStoragePoolObjSearchVolume to search each volume calling the passed callback function until it returns true indicating that the desired volume was found. Signed-off-by: John Ferlan <jferlan@redhat.com>
This commit is contained in:
parent
40630a8e45
commit
5bf9b65501
@ -319,6 +319,38 @@ virStoragePoolObjGetVolumesCount(virStoragePoolObjPtr obj)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
virStoragePoolObjForEachVolume(virStoragePoolObjPtr obj,
|
||||||
|
virStorageVolObjListIterator iter,
|
||||||
|
const void *opaque)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
for (i = 0; i < obj->volumes.count; i++) {
|
||||||
|
if (iter(obj->volumes.objs[i], opaque) < 0)
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
virStorageVolDefPtr
|
||||||
|
virStoragePoolObjSearchVolume(virStoragePoolObjPtr obj,
|
||||||
|
virStorageVolObjListSearcher iter,
|
||||||
|
const void *opaque)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
for (i = 0; i < obj->volumes.count; i++) {
|
||||||
|
if (iter(obj->volumes.objs[i], opaque))
|
||||||
|
return obj->volumes.objs[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
virStorageVolDefPtr
|
virStorageVolDefPtr
|
||||||
virStorageVolDefFindByKey(virStoragePoolObjPtr obj,
|
virStorageVolDefFindByKey(virStoragePoolObjPtr obj,
|
||||||
const char *key)
|
const char *key)
|
||||||
|
@ -147,6 +147,24 @@ virStoragePoolObjRemoveVol(virStoragePoolObjPtr obj,
|
|||||||
size_t
|
size_t
|
||||||
virStoragePoolObjGetVolumesCount(virStoragePoolObjPtr obj);
|
virStoragePoolObjGetVolumesCount(virStoragePoolObjPtr obj);
|
||||||
|
|
||||||
|
typedef int
|
||||||
|
(*virStorageVolObjListIterator)(virStorageVolDefPtr voldef,
|
||||||
|
const void *opaque);
|
||||||
|
|
||||||
|
int
|
||||||
|
virStoragePoolObjForEachVolume(virStoragePoolObjPtr obj,
|
||||||
|
virStorageVolObjListIterator iter,
|
||||||
|
const void *opaque);
|
||||||
|
|
||||||
|
typedef bool
|
||||||
|
(*virStorageVolObjListSearcher)(virStorageVolDefPtr voldef,
|
||||||
|
const void *opaque);
|
||||||
|
|
||||||
|
virStorageVolDefPtr
|
||||||
|
virStoragePoolObjSearchVolume(virStoragePoolObjPtr obj,
|
||||||
|
virStorageVolObjListSearcher iter,
|
||||||
|
const void *opaque);
|
||||||
|
|
||||||
virStorageVolDefPtr
|
virStorageVolDefPtr
|
||||||
virStorageVolDefFindByKey(virStoragePoolObjPtr obj,
|
virStorageVolDefFindByKey(virStoragePoolObjPtr obj,
|
||||||
const char *key);
|
const char *key);
|
||||||
|
@ -1061,6 +1061,7 @@ virStoragePoolObjDefUseNewDef;
|
|||||||
virStoragePoolObjDeleteDef;
|
virStoragePoolObjDeleteDef;
|
||||||
virStoragePoolObjFindByName;
|
virStoragePoolObjFindByName;
|
||||||
virStoragePoolObjFindByUUID;
|
virStoragePoolObjFindByUUID;
|
||||||
|
virStoragePoolObjForEachVolume;
|
||||||
virStoragePoolObjGetAsyncjobs;
|
virStoragePoolObjGetAsyncjobs;
|
||||||
virStoragePoolObjGetAutostartLink;
|
virStoragePoolObjGetAutostartLink;
|
||||||
virStoragePoolObjGetConfigFile;
|
virStoragePoolObjGetConfigFile;
|
||||||
@ -1083,6 +1084,7 @@ virStoragePoolObjNumOfVolumes;
|
|||||||
virStoragePoolObjRemove;
|
virStoragePoolObjRemove;
|
||||||
virStoragePoolObjRemoveVol;
|
virStoragePoolObjRemoveVol;
|
||||||
virStoragePoolObjSaveDef;
|
virStoragePoolObjSaveDef;
|
||||||
|
virStoragePoolObjSearchVolume;
|
||||||
virStoragePoolObjSetActive;
|
virStoragePoolObjSetActive;
|
||||||
virStoragePoolObjSetAutostart;
|
virStoragePoolObjSetAutostart;
|
||||||
virStoragePoolObjSetConfigFile;
|
virStoragePoolObjSetConfigFile;
|
||||||
|
@ -43,6 +43,17 @@ VIR_LOG_INIT("storage.storage_backend_disk");
|
|||||||
|
|
||||||
#define SECTOR_SIZE 512
|
#define SECTOR_SIZE 512
|
||||||
|
|
||||||
|
static bool
|
||||||
|
virStorageVolPartFindExtended(virStorageVolDefPtr def,
|
||||||
|
const void *opaque ATTRIBUTE_UNUSED)
|
||||||
|
{
|
||||||
|
if (def->source.partType == VIR_STORAGE_VOL_DISK_TYPE_EXTENDED)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
virStorageBackendDiskMakeDataVol(virStoragePoolObjPtr pool,
|
virStorageBackendDiskMakeDataVol(virStoragePoolObjPtr pool,
|
||||||
char **const groups,
|
char **const groups,
|
||||||
@ -191,16 +202,13 @@ virStorageBackendDiskMakeDataVol(virStoragePoolObjPtr pool,
|
|||||||
|
|
||||||
/* Find the extended partition and increase the allocation value */
|
/* Find the extended partition and increase the allocation value */
|
||||||
if (vol->source.partType == VIR_STORAGE_VOL_DISK_TYPE_LOGICAL) {
|
if (vol->source.partType == VIR_STORAGE_VOL_DISK_TYPE_LOGICAL) {
|
||||||
size_t i;
|
virStorageVolDefPtr voldef;
|
||||||
|
|
||||||
for (i = 0; i < pool->volumes.count; i++) {
|
voldef = virStoragePoolObjSearchVolume(pool,
|
||||||
if (pool->volumes.objs[i]->source.partType ==
|
virStorageVolPartFindExtended,
|
||||||
VIR_STORAGE_VOL_DISK_TYPE_EXTENDED) {
|
NULL);
|
||||||
pool->volumes.objs[i]->target.allocation +=
|
if (voldef)
|
||||||
vol->target.allocation;
|
voldef->target.allocation += vol->target.allocation;
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (STRNEQ(groups[2], "metadata"))
|
if (STRNEQ(groups[2], "metadata"))
|
||||||
@ -523,6 +531,25 @@ virStorageBackendDiskBuildPool(virConnectPtr conn ATTRIBUTE_UNUSED,
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
struct virStorageVolNumData {
|
||||||
|
int count;
|
||||||
|
};
|
||||||
|
|
||||||
|
static int
|
||||||
|
virStorageVolNumOfPartTypes(virStorageVolDefPtr def,
|
||||||
|
const void *opaque)
|
||||||
|
{
|
||||||
|
struct virStorageVolNumData *data = (struct virStorageVolNumData *)opaque;
|
||||||
|
|
||||||
|
if (def->source.partType == VIR_STORAGE_VOL_DISK_TYPE_PRIMARY ||
|
||||||
|
def->source.partType == VIR_STORAGE_VOL_DISK_TYPE_EXTENDED)
|
||||||
|
data->count++;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decides what kind of partition type that should be created.
|
* Decides what kind of partition type that should be created.
|
||||||
* Important when the partition table is of msdos type
|
* Important when the partition table is of msdos type
|
||||||
@ -530,19 +557,16 @@ virStorageBackendDiskBuildPool(virConnectPtr conn ATTRIBUTE_UNUSED,
|
|||||||
static int
|
static int
|
||||||
virStorageBackendDiskPartTypeToCreate(virStoragePoolObjPtr pool)
|
virStorageBackendDiskPartTypeToCreate(virStoragePoolObjPtr pool)
|
||||||
{
|
{
|
||||||
|
struct virStorageVolNumData data = { .count = 0 };
|
||||||
|
|
||||||
if (pool->def->source.format == VIR_STORAGE_POOL_DISK_DOS) {
|
if (pool->def->source.format == VIR_STORAGE_POOL_DISK_DOS) {
|
||||||
/* count primary and extended partitions,
|
/* count primary and extended partitions,
|
||||||
can't be more than 3 to create a new primary partition */
|
can't be more than 3 to create a new primary partition */
|
||||||
size_t i;
|
if (virStoragePoolObjForEachVolume(pool, virStorageVolNumOfPartTypes,
|
||||||
int count = 0;
|
&data) == 0) {
|
||||||
for (i = 0; i < pool->volumes.count; i++) {
|
if (data.count >= 4)
|
||||||
int partType = pool->volumes.objs[i]->source.partType;
|
return VIR_STORAGE_VOL_DISK_TYPE_LOGICAL;
|
||||||
if (partType == VIR_STORAGE_VOL_DISK_TYPE_PRIMARY ||
|
|
||||||
partType == VIR_STORAGE_VOL_DISK_TYPE_EXTENDED)
|
|
||||||
count++;
|
|
||||||
}
|
}
|
||||||
if (count >= 4)
|
|
||||||
return VIR_STORAGE_VOL_DISK_TYPE_LOGICAL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* for all other cases, all partitions are primary */
|
/* for all other cases, all partitions are primary */
|
||||||
@ -554,7 +578,6 @@ virStorageBackendDiskPartFormat(virStoragePoolObjPtr pool,
|
|||||||
virStorageVolDefPtr vol,
|
virStorageVolDefPtr vol,
|
||||||
char** partFormat)
|
char** partFormat)
|
||||||
{
|
{
|
||||||
size_t i;
|
|
||||||
if (pool->def->source.format == VIR_STORAGE_POOL_DISK_DOS) {
|
if (pool->def->source.format == VIR_STORAGE_POOL_DISK_DOS) {
|
||||||
const char *partedFormat;
|
const char *partedFormat;
|
||||||
partedFormat = virStoragePartedFsTypeToString(vol->target.format);
|
partedFormat = virStoragePartedFsTypeToString(vol->target.format);
|
||||||
@ -565,13 +588,12 @@ virStorageBackendDiskPartFormat(virStoragePoolObjPtr pool,
|
|||||||
}
|
}
|
||||||
if (vol->target.format == VIR_STORAGE_VOL_DISK_EXTENDED) {
|
if (vol->target.format == VIR_STORAGE_VOL_DISK_EXTENDED) {
|
||||||
/* make sure we don't have an extended partition already */
|
/* make sure we don't have an extended partition already */
|
||||||
for (i = 0; i < pool->volumes.count; i++) {
|
if (virStoragePoolObjSearchVolume(pool,
|
||||||
if (pool->volumes.objs[i]->source.partType ==
|
virStorageVolPartFindExtended,
|
||||||
VIR_STORAGE_VOL_DISK_TYPE_EXTENDED) {
|
NULL)) {
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||||
_("extended partition already exists"));
|
_("extended partition already exists"));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (VIR_STRDUP(*partFormat, partedFormat) < 0)
|
if (VIR_STRDUP(*partFormat, partedFormat) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
@ -587,18 +609,16 @@ virStorageBackendDiskPartFormat(virStoragePoolObjPtr pool,
|
|||||||
break;
|
break;
|
||||||
case VIR_STORAGE_VOL_DISK_TYPE_LOGICAL:
|
case VIR_STORAGE_VOL_DISK_TYPE_LOGICAL:
|
||||||
/* make sure we have an extended partition */
|
/* make sure we have an extended partition */
|
||||||
for (i = 0; i < pool->volumes.count; i++) {
|
if (virStoragePoolObjSearchVolume(pool,
|
||||||
if (pool->volumes.objs[i]->source.partType ==
|
virStorageVolPartFindExtended,
|
||||||
VIR_STORAGE_VOL_DISK_TYPE_EXTENDED) {
|
NULL)) {
|
||||||
if (virAsprintf(partFormat, "logical %s",
|
if (virAsprintf(partFormat, "logical %s",
|
||||||
partedFormat) < 0)
|
partedFormat) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
break;
|
} else {
|
||||||
}
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||||
}
|
_("no extended partition found and no "
|
||||||
if (i == virStoragePoolObjGetVolumesCount(pool)) {
|
"primary partition available"));
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
||||||
"%s", _("no extended partition found and no primary partition available"));
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
Loading…
Reference in New Issue
Block a user