From patchwork Thu Dec 1 17:00:43 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 128730 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 82113B6F68 for ; Fri, 2 Dec 2011 04:15:09 +1100 (EST) Received: from localhost ([::1]:56464 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RWADr-0004BF-Dh for incoming@patchwork.ozlabs.org; Thu, 01 Dec 2011 12:14:59 -0500 Received: from eggs.gnu.org ([140.186.70.92]:51400) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RWADg-0002L7-59 for qemu-devel@nongnu.org; Thu, 01 Dec 2011 12:14:52 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RWA0j-0002Ml-7v for qemu-devel@nongnu.org; Thu, 01 Dec 2011 12:01:35 -0500 Received: from e06smtp15.uk.ibm.com ([195.75.94.111]:54850) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RWA0i-0002J5-Ox for qemu-devel@nongnu.org; Thu, 01 Dec 2011 12:01:25 -0500 Received: from /spool/local by e06smtp15.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 1 Dec 2011 17:00:52 -0000 Received: from d06nrmr1307.portsmouth.uk.ibm.com ([9.149.38.129]) by e06smtp15.uk.ibm.com ([192.168.101.145]) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 1 Dec 2011 17:00:51 -0000 Received: from d06av12.portsmouth.uk.ibm.com (d06av12.portsmouth.uk.ibm.com [9.149.37.247]) by d06nrmr1307.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id pB1H0oNp1986774 for ; Thu, 1 Dec 2011 17:00:50 GMT Received: from d06av12.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av12.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id pB1H0oxY028309 for ; Thu, 1 Dec 2011 10:00:50 -0700 Received: from localhost (stefanha-thinkpad.manchester-maybrook.uk.ibm.com [9.174.219.31]) by d06av12.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id pB1H0nRF028306; Thu, 1 Dec 2011 10:00:49 -0700 From: Stefan Hajnoczi To: Date: Thu, 1 Dec 2011 17:00:43 +0000 Message-Id: <1322758843-19809-1-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.7.3 x-cbid: 11120117-0342-0000-0000-0000003B0C87 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 195.75.94.111 Cc: Kevin Wolf , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH] qed: add qed-tool.py image manipulation utility 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 The qed-tool.py utility can inspect and manipulate QED image files. It can be used for testing to see the state of image metadata and also to inject corruptions into the image file. It also has a scrubbing feature to copy just the metadata out of an image file, allowing users to share broken image files without revealing data in bug reports. This has lived in my local repo for a long time but could be useful to others. Signed-off-by: Stefan Hajnoczi --- scripts/qed-tool.py | 250 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 250 insertions(+), 0 deletions(-) create mode 100755 scripts/qed-tool.py diff --git a/scripts/qed-tool.py b/scripts/qed-tool.py new file mode 100755 index 0000000..90cc375 --- /dev/null +++ b/scripts/qed-tool.py @@ -0,0 +1,250 @@ +#!/usr/bin/env python +# +# Tool to manipulate QED image files +# +# Copyright (C) 2010 IBM, Corp. +# +# Authors: +# Stefan Hajnoczi +# +# This work is licensed under the terms of the GNU GPL, version 2. See +# the COPYING file in the top-level directory. + +import sys +import struct +import random +import optparse + +QED_F_NEED_CHECK = 0x02 + +header_fmt = ' [] - Zero data clusters''' + pos, n = int(pos), 1 + if args: + if len(args) != 1: + err('expected one argument') + n = int(args[0]) + + for i in xrange(n): + l1_index = pos / qed.header['cluster_size'] / len(qed.l1_table) + if qed.l1_table[l1_index] == 0: + err('no l2 table allocated') + + l2_offset = qed.l1_table[l1_index] + l2_table = qed.read_table(l2_offset) + + l2_index = (pos / qed.header['cluster_size']) % len(qed.l1_table) + l2_table[l2_index] = 1 # zero the data cluster + qed.write_table(l2_offset, l2_table) + pos += qed.header['cluster_size'] + +def cmd_copy_metadata(qed, outfile): + '''copy_metadata - Copy metadata only (for scrubbing corrupted images)''' + out = open(outfile, 'wb') + + # Match file size + out.seek(qed.filesize - 1) + out.write('\0') + + # Copy header clusters + out.seek(0) + header_size_bytes = qed.header['header_size'] * qed.header['cluster_size'] + out.write(qed.raw_pread(0, header_size_bytes)) + + # Copy L1 table + out.seek(qed.header['l1_table_offset']) + s = ''.join(pack_table_elem(x) for x in qed.l1_table) + out.write(s) + + # Copy L2 tables + for l2_offset in qed.l1_table: + if l2_offset == 0: + continue + l2_table = qed.read_table(l2_offset) + out.seek(l2_offset) + s = ''.join(pack_table_elem(x) for x in l2_table) + out.write(s) + + out.close() + +def usage(): + sys.stderr.write('usage: %s []\n\n' % sys.argv[0]) + for cmd in sorted(x for x in globals() if x.startswith('cmd_')): + sys.stderr.write(globals()[cmd].__doc__ + '\n') + sys.exit(1) + +if len(sys.argv) < 3: + usage() +filename, cmd = sys.argv[1:3] + +cmd = 'cmd_' + cmd +if cmd not in globals(): + usage() + +qed = QED(open(filename, 'r+b')) +try: + globals()[cmd](qed, *sys.argv[3:]) +except TypeError: + sys.stderr.write(globals()[cmd].__doc__ + '\n') + sys.exit(1)