From patchwork Thu Jul 25 14:23:11 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 261724 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id BEC7B2C00D1 for ; Fri, 26 Jul 2013 00:31:44 +1000 (EST) Received: from localhost ([::1]:60895 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V2MZy-0006XY-Ni for incoming@patchwork.ozlabs.org; Thu, 25 Jul 2013 10:31:42 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36002) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V2MT2-00044I-Bt for qemu-devel@nongnu.org; Thu, 25 Jul 2013 10:24:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1V2MSx-0007yN-ES for qemu-devel@nongnu.org; Thu, 25 Jul 2013 10:24:32 -0400 Received: from mail-ye0-x235.google.com ([2607:f8b0:4002:c04::235]:62598) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V2MSx-0007yG-9s for qemu-devel@nongnu.org; Thu, 25 Jul 2013 10:24:27 -0400 Received: by mail-ye0-f181.google.com with SMTP id q2so199946yen.12 for ; Thu, 25 Jul 2013 07:24:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=eXn1mOSAaAwFdKlHEx2LkaC1ca/cvv5iCxnnxnkhwMs=; b=eWmaKTlPCq1knBx78oCzBuLpmx6g1uYXREPl3yCSn2qdOJ1Tlox0WVBx1TwlL8k3jx BIzBJ4q5ABBA+zfyTFtoHdZCq4yEpA+VM9KhlxDxkaVrypjXdnqtu/ccZd69F2ICN/em T6bkH7CXV6uLtA9YeLe/nl+sRtqkO3A+adKr1EGdKPhUA/bfZzDkGxDYKaX1O9y52hGi iidXQIbRFSPlw/LtWYTNUK9y6ACV9grN5LZgnjklcjtw7+NGG/ESrmf0iheP/YppU8wT CRBO+EaiPCH18rrGb/AgEliOl53rTrR03Sk46+Fd0xEnfpUSie//nbltpLFbqAuyog7S AwYw== X-Received: by 10.236.30.65 with SMTP id j41mr19342576yha.147.1374762266927; Thu, 25 Jul 2013 07:24:26 -0700 (PDT) Received: from yakj.usersys.redhat.com (net-2-39-8-162.cust.dsl.vodafone.it. [2.39.8.162]) by mx.google.com with ESMTPSA id b48sm58469338yhc.8.2013.07.25.07.24.24 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Thu, 25 Jul 2013 07:24:26 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Thu, 25 Jul 2013 16:23:11 +0200 Message-Id: <1374762197-7261-14-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1374762197-7261-1-git-send-email-pbonzini@redhat.com> References: <1374762197-7261-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:4002:c04::235 Cc: kwolf@redhat.com, pl@kamp.de, stefanha@redhat.com Subject: [Qemu-devel] [PATCH v3 13/19] qemu-img: add a "map" subcommand X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This command dumps the metadata of an entire chain, in either tabular or JSON format. Signed-off-by: Paolo Bonzini --- qemu-img-cmds.hx | 6 ++ qemu-img.c | 186 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 192 insertions(+) diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx index 4ca7e95..c97a1f4 100644 --- a/qemu-img-cmds.hx +++ b/qemu-img-cmds.hx @@ -45,6 +45,12 @@ STEXI @item info [-f @var{fmt}] [--output=@var{ofmt}] [--backing-chain] @var{filename} ETEXI +DEF("map", img_map, + "map [-f fmt] [--output=ofmt] filename") +STEXI +@item map [-f @var{fmt}] [--output=@var{ofmt}] @var{filename} +ETEXI + DEF("snapshot", img_snapshot, "snapshot [-q] [-l | -a snapshot | -c snapshot | -d snapshot] filename") STEXI diff --git a/qemu-img.c b/qemu-img.c index c5c8ebc..b28d388 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -1768,6 +1768,192 @@ static int img_info(int argc, char **argv) return 0; } + +typedef struct MapEntry { + int flags; + int depth; + int64_t start; + int64_t length; + int64_t offset; +} MapEntry; + +static void dump_map_entry(OutputFormat output_format, MapEntry *e, + MapEntry *next) +{ + switch (output_format) { + case OFORMAT_HUMAN: + if ((e->flags & BDRV_BLOCK_DATA) && + !(e->flags & BDRV_BLOCK_OFFSET_VALID)) { + error_report("File contains external, encrypted or compressed clusters."); + exit(1); + } + if ((e->flags & (BDRV_BLOCK_DATA|BDRV_BLOCK_ZERO)) == BDRV_BLOCK_DATA) { + printf("%"PRId64" %"PRId64" %d %"PRId64"\n", + e->start, e->length, e->depth, e->offset); + } + /* This format ignores the distinction between 0, ZERO and ZERO|DATA. + * Modify the flags here to allow more coalescing. + */ + if (next && + (next->flags & (BDRV_BLOCK_DATA|BDRV_BLOCK_ZERO)) != BDRV_BLOCK_DATA) { + next->flags &= ~BDRV_BLOCK_DATA; + next->flags |= BDRV_BLOCK_ZERO; + } + break; + case OFORMAT_JSON: + printf("%s{ 'start': %"PRId64", 'length': %"PRId64", 'depth': %d, " + "'zero': %s, 'data': %s", + (e->start == 0 ? "[" : ",\n"), + e->start, e->length, e->depth, + (e->flags & BDRV_BLOCK_ZERO) ? "true" : "false", + (e->flags & BDRV_BLOCK_DATA) ? "true" : "false"); + if (e->flags & BDRV_BLOCK_OFFSET_VALID) { + printf(", 'offset': %"PRId64"", e->offset); + } + putchar('}'); + + if (!next) { + printf("]\n"); + } + break; + } +} + +static int64_t get_block_status(BlockDriverState *bs, int64_t sector_num, + int *nb_sectors, int *depth) +{ + int64_t ret; + + /* As an optimization, we could cache the current range of unallocated + * clusters in each file of the chain, and avoid querying the same + * range repeatedly. + */ + + *depth = 0; + for (;;) { + int orig_nb_sectors = *nb_sectors; + + ret = bdrv_get_block_status(bs, sector_num, *nb_sectors, nb_sectors); + if (ret < 0) { + return ret; + } + if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) { + return ret; + } + if (!*nb_sectors) { + /* Beyond the end of this image. The extra data is read as zeroes. + * We're in the range of the BlockDriverState above this one, so + * adjust depth. + */ + *nb_sectors = orig_nb_sectors; + (*depth)--; + return BDRV_BLOCK_ZERO; + } + + bs = bs->backing_hd; + if (bs == NULL) { + return 0; + } + + (*depth)++; + } +} + +static int img_map(int argc, char **argv) +{ + int c; + OutputFormat output_format = OFORMAT_HUMAN; + BlockDriverState *bs; + const char *filename, *fmt, *output; + int64_t length; + MapEntry curr = { .length = 0 }, next; + + fmt = NULL; + output = NULL; + for (;;) { + int option_index = 0; + static const struct option long_options[] = { + {"help", no_argument, 0, 'h'}, + {"format", required_argument, 0, 'f'}, + {"output", required_argument, 0, OPTION_OUTPUT}, + {0, 0, 0, 0} + }; + c = getopt_long(argc, argv, "f:h", + long_options, &option_index); + if (c == -1) { + break; + } + switch (c) { + case '?': + case 'h': + help(); + break; + case 'f': + fmt = optarg; + break; + case OPTION_OUTPUT: + output = optarg; + break; + } + } + if (optind >= argc) { + help(); + } + filename = argv[optind++]; + + if (output && !strcmp(output, "json")) { + output_format = OFORMAT_JSON; + } else if (output && !strcmp(output, "human")) { + output_format = OFORMAT_HUMAN; + } else if (output) { + error_report("--output must be used with human or json as argument."); + return 1; + } + + bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS, true, false); + if (!bs) { + return 1; + } + + length = bdrv_getlength(bs); + while (curr.start + curr.length < length) { + int64_t nsectors_left, ret; + int64_t sector_num; + int n, depth, flags; + + /* Probe up to 1 G at a time. */ + sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS; + nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num; + n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left); + ret = get_block_status(bs, sector_num, &n, &depth); + + if (ret < 0) { + error_report("Could not read file metadata: %s", strerror(-ret)); + return 1; + } + + flags = ret & ~BDRV_BLOCK_OFFSET_MASK; + ret &= BDRV_BLOCK_OFFSET_MASK; + if (curr.length == 0 || curr.flags != flags || curr.depth != depth || + ((curr.flags & BDRV_BLOCK_OFFSET_VALID) && + curr.offset + curr.length != ret)) { + next.flags = flags; + next.depth = depth; + next.start = sector_num << BDRV_SECTOR_BITS; + next.offset = ret; + next.length = 0; + if (curr.length > 0) { + dump_map_entry(output_format, &curr, &next); + } + curr = next; + } + curr.length += n << BDRV_SECTOR_BITS; + } + + dump_map_entry(output_format, &curr, NULL); + return 0; +} + #define SNAPSHOT_LIST 1 #define SNAPSHOT_CREATE 2 #define SNAPSHOT_APPLY 3