From patchwork Thu Jun 11 21:19:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Shinkevich X-Patchwork-Id: 11600895 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 49C2B13B1 for ; Thu, 11 Jun 2020 21:23:54 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2A9902074B for ; Thu, 11 Jun 2020 21:23:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2A9902074B Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=virtuozzo.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:44384 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jjUfx-0000kL-A9 for patchwork-qemu-devel@patchwork.kernel.org; Thu, 11 Jun 2020 17:23:53 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:46736) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jjUbk-00037T-GR; Thu, 11 Jun 2020 17:19:32 -0400 Received: from relay.sw.ru ([185.231.240.75]:40346 helo=relay3.sw.ru) by eggs.gnu.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jjUbf-00057l-6h; Thu, 11 Jun 2020 17:19:32 -0400 Received: from [172.16.25.136] (helo=localhost.sw.ru) by relay3.sw.ru with esmtp (Exim 4.93) (envelope-from ) id 1jjUbW-0001xE-VJ; Fri, 12 Jun 2020 00:19:18 +0300 From: Andrey Shinkevich To: qemu-block@nongnu.org Subject: [PATCH v6 8/8] qcow2_format.py: support dumping metadata in JSON format Date: Fri, 12 Jun 2020 00:19:20 +0300 Message-Id: <1591910360-867499-9-git-send-email-andrey.shinkevich@virtuozzo.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1591910360-867499-1-git-send-email-andrey.shinkevich@virtuozzo.com> References: <1591910360-867499-1-git-send-email-andrey.shinkevich@virtuozzo.com> Received-SPF: pass client-ip=185.231.240.75; envelope-from=andrey.shinkevich@virtuozzo.com; helo=relay3.sw.ru X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/11 17:19:22 X-ACL-Warn: Detected OS = Linux 3.11 and newer X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, vsementsov@virtuozzo.com, qemu-devel@nongnu.org, mreitz@redhat.com, andrey.shinkevich@virtuozzo.com, den@openvz.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" Implementation of dumping QCOW2 image metadata. The sample output: { "Header_extensions": [ { "name": "Feature table", "magic": 1745090647, "length": 192, "data_str": "" }, { "name": "Bitmaps", "magic": 595929205, "length": 24, "data": { "nb_bitmaps": 2, "reserved32": 0, "bitmap_directory_size": 64, "bitmap_directory_offset": 1048576, "entries": [ { "name": "bitmap-1", "bitmap_table_offset": 589824, "bitmap_table_size": 1, "flags": [ "auto" ], "type": 1, "granularity_bits": 16, "name_size": 8, "extra_data_size": 0, "bitmap_table": { "table_entries": [ { "type": "serialized", "offset": 655360 }, ... Suggested-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Andrey Shinkevich --- tests/qemu-iotests/qcow2_format.py | 60 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/tests/qemu-iotests/qcow2_format.py b/tests/qemu-iotests/qcow2_format.py index bb2f13b..d87f8e0 100644 --- a/tests/qemu-iotests/qcow2_format.py +++ b/tests/qemu-iotests/qcow2_format.py @@ -18,6 +18,15 @@ import struct import string +import json + + +class ComplexEncoder(json.JSONEncoder): + def default(self, obj): + if hasattr(obj, 'get_fields_dict'): + return obj.get_fields_dict() + else: + return json.JSONEncoder.default(self, obj) class Qcow2Field: @@ -95,6 +104,11 @@ class Qcow2Struct(metaclass=Qcow2StructMeta): self.fields_dict = self.__dict__.copy() def dump(self, dump_json=None): + if dump_json: + print(json.dumps(self.get_fields_dict(), indent=4, + cls=ComplexEncoder)) + return + for f in self.fields: value = self.__dict__[f[2]] if isinstance(f[1], str): @@ -150,6 +164,9 @@ class Qcow2BitmapExt(Qcow2Struct): for bm in self.bitmaps: bm.dump_bitmap_dir_entry(dump_json) + def get_fields_dict(self): + return self.fields_dict + BME_FLAG_IN_USE = 1 << 0 BME_FLAG_AUTO = 1 << 1 @@ -202,6 +219,12 @@ class Qcow2BitmapDirEntry(Qcow2Struct): super().dump() self.bitmap_table.print_bitmap_table(self.cluster_size) + def get_fields_dict(self): + bmp_name = dict(name=self.name) + bme_dict = {**bmp_name, **self.fields_dict} + bme_dict['flags'] = self.bitmap_flags + return bme_dict + class Qcow2BitmapTableEntry: @@ -218,6 +241,9 @@ class Qcow2BitmapTableEntry: else: self.type = 'all-zeroes' + def get_fields_dict(self): + return dict(type=self.type, offset=self.offset) + class Qcow2BitmapTable: @@ -234,6 +260,18 @@ class Qcow2BitmapTable: cluster_size)) print("") + def get_fields_dict(self): + return dict(table_entries=self.entries) + + +class Qcow2HeaderExtensionsDoc: + + def __init__(self, extensions): + self.extensions = extensions + + def get_fields_dict(self): + return dict(Header_extensions=self.extensions) + QCOW2_EXT_MAGIC_BITMAPS = 0x23852875 @@ -249,6 +287,9 @@ class QcowHeaderExtension(Qcow2Struct): 0x44415441: 'Data file' } + def get_fields_dict(self): + return self.mapping.get(self.value, "") + fields = ( ('u32', Magic, 'magic'), ('u32', '{}', 'length') @@ -309,6 +350,16 @@ class QcowHeaderExtension(Qcow2Struct): else: self.obj.dump(dump_json) + def get_fields_dict(self): + ext_name = dict(name=self.Magic(self.magic)) + he_dict = {**ext_name, **self.fields_dict} + if self.obj is not None: + he_dict.update(data=self.obj) + else: + he_dict.update(data_str=self.data_str) + + return he_dict + @classmethod def create(cls, magic, data): return QcowHeaderExtension(magic, len(data), data) @@ -411,7 +462,16 @@ class QcowHeader(Qcow2Struct): fd.write(buf) def dump_extensions(self, dump_json=None): + if dump_json: + ext_doc = Qcow2HeaderExtensionsDoc(self.extensions) + print(json.dumps(ext_doc.get_fields_dict(), indent=4, + cls=ComplexEncoder)) + return + for ex in self.extensions: print('Header extension:') ex.dump(dump_json) print() + + def get_fields_dict(self): + return self.fields_dict