mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-21 20:15:17 +00:00
list: Add helpers for listing storage pool objects
src/conf/storage_conf.c: Add virStoragePoolMatch to filter the pools; Add virStoragePoolList to iterate over the pool objects with filter. src/conf/storage_conf.h: Declare virStoragePoolMatch, virStoragePoolList, and the macros for filters. src/libvirt_private.syms: Export helper virStoragePoolList.
This commit is contained in:
parent
075c754af0
commit
84208a4a8b
@ -1883,3 +1883,119 @@ void virStoragePoolObjUnlock(virStoragePoolObjPtr obj)
|
||||
{
|
||||
virMutexUnlock(&obj->lock);
|
||||
}
|
||||
|
||||
#define MATCH(FLAG) (flags & (FLAG))
|
||||
static bool
|
||||
virStoragePoolMatch(virStoragePoolObjPtr poolobj,
|
||||
unsigned int flags)
|
||||
{
|
||||
/* filter by active state */
|
||||
if (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ACTIVE) &&
|
||||
!((MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE) &&
|
||||
virStoragePoolObjIsActive(poolobj)) ||
|
||||
(MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_INACTIVE) &&
|
||||
!virStoragePoolObjIsActive(poolobj))))
|
||||
return false;
|
||||
|
||||
/* filter by persistence */
|
||||
if (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_PERSISTENT) &&
|
||||
!((MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_PERSISTENT) &&
|
||||
poolobj->configFile) ||
|
||||
(MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_TRANSIENT) &&
|
||||
!poolobj->configFile)))
|
||||
return false;
|
||||
|
||||
/* filter by autostart option */
|
||||
if (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_AUTOSTART) &&
|
||||
!((MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_AUTOSTART) &&
|
||||
poolobj->autostart) ||
|
||||
(MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_NO_AUTOSTART) &&
|
||||
!poolobj->autostart)))
|
||||
return false;
|
||||
|
||||
/* filter by pool type */
|
||||
if (MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_POOL_TYPE)) {
|
||||
if (!((MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_DIR) &&
|
||||
(poolobj->def->type == VIR_STORAGE_POOL_DIR)) ||
|
||||
(MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_FS) &&
|
||||
(poolobj->def->type == VIR_STORAGE_POOL_FS)) ||
|
||||
(MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_NETFS) &&
|
||||
(poolobj->def->type == VIR_STORAGE_POOL_NETFS)) ||
|
||||
(MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_LOGICAL) &&
|
||||
(poolobj->def->type == VIR_STORAGE_POOL_LOGICAL)) ||
|
||||
(MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_DISK) &&
|
||||
(poolobj->def->type == VIR_STORAGE_POOL_DISK)) ||
|
||||
(MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_ISCSI) &&
|
||||
(poolobj->def->type == VIR_STORAGE_POOL_ISCSI)) ||
|
||||
(MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_SCSI) &&
|
||||
(poolobj->def->type == VIR_STORAGE_POOL_SCSI)) ||
|
||||
(MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_MPATH) &&
|
||||
(poolobj->def->type == VIR_STORAGE_POOL_MPATH)) ||
|
||||
(MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_RBD) &&
|
||||
(poolobj->def->type == VIR_STORAGE_POOL_RBD)) ||
|
||||
(MATCH(VIR_CONNECT_LIST_STORAGE_POOLS_SHEEPDOG) &&
|
||||
(poolobj->def->type == VIR_STORAGE_POOL_SHEEPDOG))))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
#undef MATCH
|
||||
|
||||
int
|
||||
virStoragePoolList(virConnectPtr conn,
|
||||
virStoragePoolObjList poolobjs,
|
||||
virStoragePoolPtr **pools,
|
||||
unsigned int flags)
|
||||
{
|
||||
virStoragePoolPtr *tmp_pools = NULL;
|
||||
virStoragePoolPtr pool = NULL;
|
||||
int npools = 0;
|
||||
int ret = -1;
|
||||
int i;
|
||||
|
||||
if (pools) {
|
||||
if (VIR_ALLOC_N(tmp_pools, poolobjs.count + 1) < 0) {
|
||||
virReportOOMError();
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < poolobjs.count; i++) {
|
||||
virStoragePoolObjPtr poolobj = poolobjs.objs[i];
|
||||
virStoragePoolObjLock(poolobj);
|
||||
if (virStoragePoolMatch(poolobj, flags)) {
|
||||
if (pools) {
|
||||
if (!(pool = virGetStoragePool(conn,
|
||||
poolobj->def->name,
|
||||
poolobj->def->uuid))) {
|
||||
virStoragePoolObjUnlock(poolobj);
|
||||
goto cleanup;
|
||||
}
|
||||
tmp_pools[npools] = pool;
|
||||
}
|
||||
npools++;
|
||||
}
|
||||
virStoragePoolObjUnlock(poolobj);
|
||||
}
|
||||
|
||||
if (tmp_pools) {
|
||||
/* trim the array to the final size */
|
||||
ignore_value(VIR_REALLOC_N(tmp_pools, npools + 1));
|
||||
*pools = tmp_pools;
|
||||
tmp_pools = NULL;
|
||||
}
|
||||
|
||||
ret = npools;
|
||||
|
||||
cleanup:
|
||||
if (tmp_pools) {
|
||||
for (i = 0; i < npools; i++) {
|
||||
if (tmp_pools[i])
|
||||
virStoragePoolFree(tmp_pools[i]);
|
||||
}
|
||||
}
|
||||
|
||||
VIR_FREE(tmp_pools);
|
||||
return ret;
|
||||
}
|
||||
|
@ -518,4 +518,39 @@ enum virStoragePartedFsType {
|
||||
};
|
||||
VIR_ENUM_DECL(virStoragePartedFsType)
|
||||
|
||||
# define VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ACTIVE \
|
||||
(VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE | \
|
||||
VIR_CONNECT_LIST_STORAGE_POOLS_INACTIVE)
|
||||
|
||||
# define VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_PERSISTENT \
|
||||
(VIR_CONNECT_LIST_STORAGE_POOLS_PERSISTENT | \
|
||||
VIR_CONNECT_LIST_STORAGE_POOLS_TRANSIENT)
|
||||
|
||||
# define VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_AUTOSTART \
|
||||
(VIR_CONNECT_LIST_STORAGE_POOLS_AUTOSTART | \
|
||||
VIR_CONNECT_LIST_STORAGE_POOLS_NO_AUTOSTART)
|
||||
|
||||
# define VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_POOL_TYPE \
|
||||
(VIR_CONNECT_LIST_STORAGE_POOLS_DIR | \
|
||||
VIR_CONNECT_LIST_STORAGE_POOLS_FS | \
|
||||
VIR_CONNECT_LIST_STORAGE_POOLS_NETFS | \
|
||||
VIR_CONNECT_LIST_STORAGE_POOLS_LOGICAL | \
|
||||
VIR_CONNECT_LIST_STORAGE_POOLS_DISK | \
|
||||
VIR_CONNECT_LIST_STORAGE_POOLS_ISCSI | \
|
||||
VIR_CONNECT_LIST_STORAGE_POOLS_SCSI | \
|
||||
VIR_CONNECT_LIST_STORAGE_POOLS_MPATH | \
|
||||
VIR_CONNECT_LIST_STORAGE_POOLS_RBD | \
|
||||
VIR_CONNECT_LIST_STORAGE_POOLS_SHEEPDOG)
|
||||
|
||||
# define VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ALL \
|
||||
(VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_ACTIVE | \
|
||||
VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_PERSISTENT | \
|
||||
VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_AUTOSTART | \
|
||||
VIR_CONNECT_LIST_STORAGE_POOLS_FILTERS_POOL_TYPE)
|
||||
|
||||
int virStoragePoolList(virConnectPtr conn,
|
||||
virStoragePoolObjList poolobjs,
|
||||
virStoragePoolPtr **pools,
|
||||
unsigned int flags);
|
||||
|
||||
#endif /* __VIR_STORAGE_CONF_H__ */
|
||||
|
@ -1058,6 +1058,7 @@ virStoragePoolDefParseString;
|
||||
virStoragePoolFormatDiskTypeToString;
|
||||
virStoragePoolFormatFileSystemNetTypeToString;
|
||||
virStoragePoolFormatFileSystemTypeToString;
|
||||
virStoragePoolList;
|
||||
virStoragePoolLoadAllConfigs;
|
||||
virStoragePoolObjAssignDef;
|
||||
virStoragePoolObjClearVols;
|
||||
|
Loading…
x
Reference in New Issue
Block a user