|
|
9ae3a8 |
From 43c680e82e94061bf1b522466054730d8f3d8c22 Mon Sep 17 00:00:00 2001
|
|
|
9ae3a8 |
From: Paolo Bonzini <pbonzini@redhat.com>
|
|
|
9ae3a8 |
Date: Fri, 18 Oct 2013 08:14:42 +0200
|
|
|
9ae3a8 |
Subject: [PATCH 17/81] qemu-img: add a "map" subcommand
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
|
|
|
9ae3a8 |
Message-id: <1382084091-16636-18-git-send-email-pbonzini@redhat.com>
|
|
|
9ae3a8 |
Patchwork-id: 55000
|
|
|
9ae3a8 |
O-Subject: [RHEL 7.0 qemu-kvm PATCH 17/26] qemu-img: add a "map" subcommand
|
|
|
9ae3a8 |
Bugzilla: 989646
|
|
|
9ae3a8 |
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
|
|
|
9ae3a8 |
RH-Acked-by: Max Reitz <mreitz@redhat.com>
|
|
|
9ae3a8 |
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
This command dumps the metadata of an entire chain, in either tabular or JSON
|
|
|
9ae3a8 |
format.
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
|
|
|
9ae3a8 |
(cherry picked from commit 4c93a13b5daf9bd5fca1a547661b0fb9a2f0ca52)
|
|
|
9ae3a8 |
---
|
|
|
9ae3a8 |
qemu-img-cmds.hx | 6 ++
|
|
|
9ae3a8 |
qemu-img.c | 192 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
9ae3a8 |
2 files changed, 198 insertions(+)
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
|
|
|
9ae3a8 |
---
|
|
|
9ae3a8 |
qemu-img-cmds.hx | 6 ++
|
|
|
9ae3a8 |
qemu-img.c | 192 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
9ae3a8 |
2 files changed, 198 insertions(+), 0 deletions(-)
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx
|
|
|
9ae3a8 |
index 4ca7e95..c97a1f4 100644
|
|
|
9ae3a8 |
--- a/qemu-img-cmds.hx
|
|
|
9ae3a8 |
+++ b/qemu-img-cmds.hx
|
|
|
9ae3a8 |
@@ -45,6 +45,12 @@ STEXI
|
|
|
9ae3a8 |
@item info [-f @var{fmt}] [--output=@var{ofmt}] [--backing-chain] @var{filename}
|
|
|
9ae3a8 |
ETEXI
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
+DEF("map", img_map,
|
|
|
9ae3a8 |
+ "map [-f fmt] [--output=ofmt] filename")
|
|
|
9ae3a8 |
+STEXI
|
|
|
9ae3a8 |
+@item map [-f @var{fmt}] [--output=@var{ofmt}] @var{filename}
|
|
|
9ae3a8 |
+ETEXI
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
DEF("snapshot", img_snapshot,
|
|
|
9ae3a8 |
"snapshot [-q] [-l | -a snapshot | -c snapshot | -d snapshot] filename")
|
|
|
9ae3a8 |
STEXI
|
|
|
9ae3a8 |
diff --git a/qemu-img.c b/qemu-img.c
|
|
|
9ae3a8 |
index a9aabd5..e58c052 100644
|
|
|
9ae3a8 |
--- a/qemu-img.c
|
|
|
9ae3a8 |
+++ b/qemu-img.c
|
|
|
9ae3a8 |
@@ -1919,6 +1919,198 @@ static int img_info(int argc, char **argv)
|
|
|
9ae3a8 |
return 0;
|
|
|
9ae3a8 |
}
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+typedef struct MapEntry {
|
|
|
9ae3a8 |
+ int flags;
|
|
|
9ae3a8 |
+ int depth;
|
|
|
9ae3a8 |
+ int64_t start;
|
|
|
9ae3a8 |
+ int64_t length;
|
|
|
9ae3a8 |
+ int64_t offset;
|
|
|
9ae3a8 |
+ BlockDriverState *bs;
|
|
|
9ae3a8 |
+} MapEntry;
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+static void dump_map_entry(OutputFormat output_format, MapEntry *e,
|
|
|
9ae3a8 |
+ MapEntry *next)
|
|
|
9ae3a8 |
+{
|
|
|
9ae3a8 |
+ switch (output_format) {
|
|
|
9ae3a8 |
+ case OFORMAT_HUMAN:
|
|
|
9ae3a8 |
+ if ((e->flags & BDRV_BLOCK_DATA) &&
|
|
|
9ae3a8 |
+ !(e->flags & BDRV_BLOCK_OFFSET_VALID)) {
|
|
|
9ae3a8 |
+ error_report("File contains external, encrypted or compressed clusters.");
|
|
|
9ae3a8 |
+ exit(1);
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ if ((e->flags & (BDRV_BLOCK_DATA|BDRV_BLOCK_ZERO)) == BDRV_BLOCK_DATA) {
|
|
|
9ae3a8 |
+ printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
|
|
|
9ae3a8 |
+ e->start, e->length, e->offset, e->bs->filename);
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
|
|
|
9ae3a8 |
+ * Modify the flags here to allow more coalescing.
|
|
|
9ae3a8 |
+ */
|
|
|
9ae3a8 |
+ if (next &&
|
|
|
9ae3a8 |
+ (next->flags & (BDRV_BLOCK_DATA|BDRV_BLOCK_ZERO)) != BDRV_BLOCK_DATA) {
|
|
|
9ae3a8 |
+ next->flags &= ~BDRV_BLOCK_DATA;
|
|
|
9ae3a8 |
+ next->flags |= BDRV_BLOCK_ZERO;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ break;
|
|
|
9ae3a8 |
+ case OFORMAT_JSON:
|
|
|
9ae3a8 |
+ printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64", \"depth\": %d,"
|
|
|
9ae3a8 |
+ " \"zero\": %s, \"data\": %s",
|
|
|
9ae3a8 |
+ (e->start == 0 ? "[" : ",\n"),
|
|
|
9ae3a8 |
+ e->start, e->length, e->depth,
|
|
|
9ae3a8 |
+ (e->flags & BDRV_BLOCK_ZERO) ? "true" : "false",
|
|
|
9ae3a8 |
+ (e->flags & BDRV_BLOCK_DATA) ? "true" : "false");
|
|
|
9ae3a8 |
+ if (e->flags & BDRV_BLOCK_OFFSET_VALID) {
|
|
|
9ae3a8 |
+ printf(", 'offset': %"PRId64"", e->offset);
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ putchar('}');
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ if (!next) {
|
|
|
9ae3a8 |
+ printf("]\n");
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ break;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+}
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+static int get_block_status(BlockDriverState *bs, int64_t sector_num,
|
|
|
9ae3a8 |
+ int nb_sectors, MapEntry *e)
|
|
|
9ae3a8 |
+{
|
|
|
9ae3a8 |
+ int64_t ret;
|
|
|
9ae3a8 |
+ int depth;
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ /* As an optimization, we could cache the current range of unallocated
|
|
|
9ae3a8 |
+ * clusters in each file of the chain, and avoid querying the same
|
|
|
9ae3a8 |
+ * range repeatedly.
|
|
|
9ae3a8 |
+ */
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ depth = 0;
|
|
|
9ae3a8 |
+ for (;;) {
|
|
|
9ae3a8 |
+ ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors);
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ return ret;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ assert(nb_sectors);
|
|
|
9ae3a8 |
+ if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
|
|
|
9ae3a8 |
+ break;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ bs = bs->backing_hd;
|
|
|
9ae3a8 |
+ if (bs == NULL) {
|
|
|
9ae3a8 |
+ ret = 0;
|
|
|
9ae3a8 |
+ break;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ depth++;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ e->start = sector_num * BDRV_SECTOR_SIZE;
|
|
|
9ae3a8 |
+ e->length = nb_sectors * BDRV_SECTOR_SIZE;
|
|
|
9ae3a8 |
+ e->flags = ret & ~BDRV_BLOCK_OFFSET_MASK;
|
|
|
9ae3a8 |
+ e->offset = ret & BDRV_BLOCK_OFFSET_MASK;
|
|
|
9ae3a8 |
+ e->depth = depth;
|
|
|
9ae3a8 |
+ e->bs = bs;
|
|
|
9ae3a8 |
+ return 0;
|
|
|
9ae3a8 |
+}
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+static int img_map(int argc, char **argv)
|
|
|
9ae3a8 |
+{
|
|
|
9ae3a8 |
+ int c;
|
|
|
9ae3a8 |
+ OutputFormat output_format = OFORMAT_HUMAN;
|
|
|
9ae3a8 |
+ BlockDriverState *bs;
|
|
|
9ae3a8 |
+ const char *filename, *fmt, *output;
|
|
|
9ae3a8 |
+ int64_t length;
|
|
|
9ae3a8 |
+ MapEntry curr = { .length = 0 }, next;
|
|
|
9ae3a8 |
+ int ret = 0;
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ fmt = NULL;
|
|
|
9ae3a8 |
+ output = NULL;
|
|
|
9ae3a8 |
+ for (;;) {
|
|
|
9ae3a8 |
+ int option_index = 0;
|
|
|
9ae3a8 |
+ static const struct option long_options[] = {
|
|
|
9ae3a8 |
+ {"help", no_argument, 0, 'h'},
|
|
|
9ae3a8 |
+ {"format", required_argument, 0, 'f'},
|
|
|
9ae3a8 |
+ {"output", required_argument, 0, OPTION_OUTPUT},
|
|
|
9ae3a8 |
+ {0, 0, 0, 0}
|
|
|
9ae3a8 |
+ };
|
|
|
9ae3a8 |
+ c = getopt_long(argc, argv, "f:h",
|
|
|
9ae3a8 |
+ long_options, &option_index);
|
|
|
9ae3a8 |
+ if (c == -1) {
|
|
|
9ae3a8 |
+ break;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ switch (c) {
|
|
|
9ae3a8 |
+ case '?':
|
|
|
9ae3a8 |
+ case 'h':
|
|
|
9ae3a8 |
+ help();
|
|
|
9ae3a8 |
+ break;
|
|
|
9ae3a8 |
+ case 'f':
|
|
|
9ae3a8 |
+ fmt = optarg;
|
|
|
9ae3a8 |
+ break;
|
|
|
9ae3a8 |
+ case OPTION_OUTPUT:
|
|
|
9ae3a8 |
+ output = optarg;
|
|
|
9ae3a8 |
+ break;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ if (optind >= argc) {
|
|
|
9ae3a8 |
+ help();
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ filename = argv[optind++];
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ if (output && !strcmp(output, "json")) {
|
|
|
9ae3a8 |
+ output_format = OFORMAT_JSON;
|
|
|
9ae3a8 |
+ } else if (output && !strcmp(output, "human")) {
|
|
|
9ae3a8 |
+ output_format = OFORMAT_HUMAN;
|
|
|
9ae3a8 |
+ } else if (output) {
|
|
|
9ae3a8 |
+ error_report("--output must be used with human or json as argument.");
|
|
|
9ae3a8 |
+ return 1;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS, true, false);
|
|
|
9ae3a8 |
+ if (!bs) {
|
|
|
9ae3a8 |
+ return 1;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ if (output_format == OFORMAT_HUMAN) {
|
|
|
9ae3a8 |
+ printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ length = bdrv_getlength(bs);
|
|
|
9ae3a8 |
+ while (curr.start + curr.length < length) {
|
|
|
9ae3a8 |
+ int64_t nsectors_left;
|
|
|
9ae3a8 |
+ int64_t sector_num;
|
|
|
9ae3a8 |
+ int n;
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ /* Probe up to 1 GiB at a time. */
|
|
|
9ae3a8 |
+ nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
|
|
|
9ae3a8 |
+ n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
|
|
|
9ae3a8 |
+ ret = get_block_status(bs, sector_num, n, &next;;
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ error_report("Could not read file metadata: %s", strerror(-ret));
|
|
|
9ae3a8 |
+ goto out;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ if (curr.length != 0 && curr.flags == next.flags &&
|
|
|
9ae3a8 |
+ curr.depth == next.depth &&
|
|
|
9ae3a8 |
+ ((curr.flags & BDRV_BLOCK_OFFSET_VALID) == 0 ||
|
|
|
9ae3a8 |
+ curr.offset + curr.length == next.offset)) {
|
|
|
9ae3a8 |
+ curr.length += next.length;
|
|
|
9ae3a8 |
+ continue;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ if (curr.length > 0) {
|
|
|
9ae3a8 |
+ dump_map_entry(output_format, &curr, &next;;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ curr = next;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ dump_map_entry(output_format, &curr, NULL);
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+out:
|
|
|
9ae3a8 |
+ bdrv_close(bs);
|
|
|
9ae3a8 |
+ bdrv_delete(bs);
|
|
|
9ae3a8 |
+ return ret < 0;
|
|
|
9ae3a8 |
+}
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
#define SNAPSHOT_LIST 1
|
|
|
9ae3a8 |
#define SNAPSHOT_CREATE 2
|
|
|
9ae3a8 |
#define SNAPSHOT_APPLY 3
|
|
|
9ae3a8 |
--
|
|
|
9ae3a8 |
1.7.1
|
|
|
9ae3a8 |
|