qemu: monitor: Introduce new interface to query-named-block-nodes

Retrieve data for individual block nodes in a hash table. Currently only
capacity and allocation data is extracted but this will be extended in
the future.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
ACKed-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Peter Krempa 2019-10-09 14:13:10 +02:00
parent 36d934e7ae
commit 86bf7ded3e
4 changed files with 98 additions and 0 deletions

View File

@ -2220,6 +2220,24 @@ qemuMonitorBlockStatsUpdateCapacityBlockdev(qemuMonitorPtr mon,
return qemuMonitorJSONBlockStatsUpdateCapacityBlockdev(mon, stats);
}
/**
* qemuMonitorBlockGetNamedNodeData:
* @mon: monitor object
*
* Uses 'query-named-block-nodes' to retrieve information about individual
* storage nodes and returns them in a hash table of qemuBlockNamedNodeDataPtrs
* filled with the data. The hash table keys are node names.
*/
virHashTablePtr
qemuMonitorBlockGetNamedNodeData(qemuMonitorPtr mon)
{
QEMU_CHECK_MONITOR_NULL(mon);
return qemuMonitorJSONBlockGetNamedNodeData(mon);
}
int
qemuMonitorBlockResize(qemuMonitorPtr mon,
const char *device,

View File

@ -676,6 +676,17 @@ int qemuMonitorBlockStatsUpdateCapacityBlockdev(qemuMonitorPtr mon,
virHashTablePtr stats)
ATTRIBUTE_NONNULL(2);
typedef struct _qemuBlockNamedNodeData qemuBlockNamedNodeData;
typedef qemuBlockNamedNodeData *qemuBlockNamedNodeDataPtr;
struct _qemuBlockNamedNodeData {
unsigned long long capacity;
unsigned long long physical;
};
virHashTablePtr
qemuMonitorBlockGetNamedNodeData(qemuMonitorPtr mon);
int qemuMonitorBlockResize(qemuMonitorPtr mon,
const char *device,
const char *nodename,

View File

@ -2875,6 +2875,72 @@ qemuMonitorJSONBlockStatsUpdateCapacityBlockdev(qemuMonitorPtr mon,
}
static void
qemuMonitorJSONBlockNamedNodeDataFree(qemuBlockNamedNodeDataPtr data)
{
g_free(data);
}
G_DEFINE_AUTOPTR_CLEANUP_FUNC(qemuBlockNamedNodeData, qemuMonitorJSONBlockNamedNodeDataFree);
static int
qemuMonitorJSONBlockGetNamedNodeDataWorker(size_t pos G_GNUC_UNUSED,
virJSONValuePtr val,
void *opaque)
{
virHashTablePtr nodes = opaque;
virJSONValuePtr img;
const char *nodename;
g_autoptr(qemuBlockNamedNodeData) ent = NULL;
ent = g_new0(qemuBlockNamedNodeData, 1);
if (!(nodename = virJSONValueObjectGetString(val, "node-name")) ||
!(img = virJSONValueObjectGetObject(val, "image")))
goto broken;
if (virJSONValueObjectGetNumberUlong(img, "virtual-size", &ent->capacity) < 0)
goto broken;
/* if actual-size is missing, image is not thin provisioned */
if (virJSONValueObjectGetNumberUlong(img, "actual-size", &ent->physical) < 0)
ent->physical = ent->capacity;
if (virHashAddEntry(nodes, nodename, ent) < 0)
return -1;
ent = NULL;
return 1; /* we don't want to steal the value from the JSON array */
broken:
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("query-named-block-nodes entry was not in expected format"));
return -1;
}
virHashTablePtr
qemuMonitorJSONBlockGetNamedNodeData(qemuMonitorPtr mon)
{
g_autoptr(virJSONValue) nodes = NULL;
g_autoptr(virHashTable) ret = NULL;
if (!(nodes = qemuMonitorJSONQueryNamedBlockNodes(mon)))
return NULL;
if (!(ret = virHashNew((virHashDataFreeSimple) qemuMonitorJSONBlockNamedNodeDataFree)))
return NULL;
if (virJSONValueArrayForeachSteal(nodes,
qemuMonitorJSONBlockGetNamedNodeDataWorker,
ret) < 0)
return NULL;
return g_steal_pointer(&ret);
}
int qemuMonitorJSONBlockResize(qemuMonitorPtr mon,
const char *device,
const char *nodename,

View File

@ -87,6 +87,9 @@ int qemuMonitorJSONBlockStatsUpdateCapacity(qemuMonitorPtr mon,
int qemuMonitorJSONBlockStatsUpdateCapacityBlockdev(qemuMonitorPtr mon,
virHashTablePtr stats);
virHashTablePtr
qemuMonitorJSONBlockGetNamedNodeData(qemuMonitorPtr mon);
int qemuMonitorJSONBlockResize(qemuMonitorPtr mon,
const char *device,
const char *nodename,