[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH V5 05/13] block: add image info query function bdrv_
From: |
Wenchao Xia |
Subject: |
[Qemu-devel] [PATCH V5 05/13] block: add image info query function bdrv_query_image_info() |
Date: |
Thu, 24 Jan 2013 10:57:55 +0800 |
This patch add function bdrv_query_image_info(), which will return
image info in qmp object format. The implementation code are mostly
copied from qemu-img.c, but use block layer function to get snapshot
info.
A check with bdrv_can_read_snapshot(), was done before collecting
snapshot info.
Signed-off-by: Wenchao Xia <address@hidden>
Reviewed-by: Eric Blake <address@hidden>
---
block.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++
include/block/block.h | 1 +
2 files changed, 74 insertions(+), 0 deletions(-)
diff --git a/block.c b/block.c
index 7cdb6c6..14bf653 100644
--- a/block.c
+++ b/block.c
@@ -2902,6 +2902,79 @@ SnapshotInfoList
*bdrv_query_snapshot_infolist(BlockDriverState *bs,
return head;
}
+/* collect all internal snapshot info in a image for ImageInfo */
+static void collect_snapshots_info(BlockDriverState *bs,
+ ImageInfo *info,
+ Error **errp)
+{
+ SnapshotInfoList *info_list;
+
+ if (!bdrv_can_read_snapshot(bs)) {
+ return;
+ }
+ info_list = bdrv_query_snapshot_infolist(bs, NULL, NULL, errp);
+ if (info_list != NULL) {
+ info->has_snapshots = true;
+ info->snapshots = info_list;
+ }
+}
+
+static void collect_image_info(BlockDriverState *bs,
+ ImageInfo *info)
+{
+ uint64_t total_sectors;
+ char backing_filename[1024];
+ char backing_filename2[1024];
+ BlockDriverInfo bdi;
+ const char *filename;
+
+ filename = bdrv_get_filename(bs);
+ bdrv_get_geometry(bs, &total_sectors);
+
+ info->filename = g_strdup(filename);
+ info->format = g_strdup(bdrv_get_format_name(bs));
+ info->virtual_size = total_sectors * 512;
+ info->actual_size = bdrv_get_allocated_file_size(bs);
+ info->has_actual_size = info->actual_size >= 0;
+ if (bdrv_is_encrypted(bs)) {
+ info->encrypted = true;
+ info->has_encrypted = true;
+ }
+ if (bdrv_get_info(bs, &bdi) >= 0) {
+ if (bdi.cluster_size != 0) {
+ info->cluster_size = bdi.cluster_size;
+ info->has_cluster_size = true;
+ }
+ info->dirty_flag = bdi.is_dirty;
+ info->has_dirty_flag = true;
+ }
+ bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
+ if (backing_filename[0] != '\0') {
+ info->backing_filename = g_strdup(backing_filename);
+ info->has_backing_filename = true;
+ bdrv_get_full_backing_filename(bs, backing_filename2,
+ sizeof(backing_filename2));
+
+ if (strcmp(backing_filename, backing_filename2) != 0) {
+ info->full_backing_filename = g_strdup(backing_filename2);
+ info->has_full_backing_filename = true;
+ }
+
+ if (bs->backing_format[0]) {
+ info->backing_filename_format = g_strdup(bs->backing_format);
+ info->has_backing_filename_format = true;
+ }
+ }
+}
+
+ImageInfo *bdrv_query_image_info(BlockDriverState *bs, Error **errp)
+{
+ ImageInfo *info = g_new0(ImageInfo, 1);
+ collect_image_info(bs, info);
+ collect_snapshots_info(bs, info, errp);
+ return info;
+}
+
BlockInfo *bdrv_query_info(BlockDriverState *bs)
{
BlockInfo *info = g_malloc0(sizeof(*info));
diff --git a/include/block/block.h b/include/block/block.h
index ce18d4b..b6f80c3 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -323,6 +323,7 @@ SnapshotInfoList
*bdrv_query_snapshot_infolist(BlockDriverState *bs,
SnapshotFilterFunc filter,
void *opaque,
Error **errp);
+ImageInfo *bdrv_query_image_info(BlockDriverState *bs, Error **errp);
BlockInfo *bdrv_query_info(BlockDriverState *s);
BlockStats *bdrv_query_stats(const BlockDriverState *bs);
int bdrv_can_read_snapshot(BlockDriverState *bs);
--
1.7.1
[Qemu-devel] [PATCH V5 09/13] block: export function bdrv_find_snapshot(), Wenchao Xia, 2013/01/23