1903cb1bfSPhilippe Mathieu-Daudé#!/usr/bin/env python3 26e19b3c4SKevin Wolf 36e19b3c4SKevin Wolfimport sys 46e19b3c4SKevin Wolfimport struct 56e19b3c4SKevin Wolfimport string 66e19b3c4SKevin Wolf 7*02756054SVladimir Sementsov-Ogievskiy 86e19b3c4SKevin Wolfclass QcowHeaderExtension: 96e19b3c4SKevin Wolf 106e19b3c4SKevin Wolf def __init__(self, magic, length, data): 118884dd1bSKevin Wolf if length % 8 != 0: 128884dd1bSKevin Wolf padding = 8 - (length % 8) 138eb5e674SMax Reitz data += b"\0" * padding 148884dd1bSKevin Wolf 156e19b3c4SKevin Wolf self.magic = magic 166e19b3c4SKevin Wolf self.length = length 176e19b3c4SKevin Wolf self.data = data 186e19b3c4SKevin Wolf 196e19b3c4SKevin Wolf @classmethod 206e19b3c4SKevin Wolf def create(cls, magic, data): 216e19b3c4SKevin Wolf return QcowHeaderExtension(magic, len(data), data) 226e19b3c4SKevin Wolf 23*02756054SVladimir Sementsov-Ogievskiy 246e19b3c4SKevin Wolfclass QcowHeader: 256e19b3c4SKevin Wolf 266e19b3c4SKevin Wolf uint32_t = 'I' 276e19b3c4SKevin Wolf uint64_t = 'Q' 286e19b3c4SKevin Wolf 296e19b3c4SKevin Wolf fields = [ 306e19b3c4SKevin Wolf # Version 2 header fields 316e19b3c4SKevin Wolf [uint32_t, '%#x', 'magic'], 326e19b3c4SKevin Wolf [uint32_t, '%d', 'version'], 336e19b3c4SKevin Wolf [uint64_t, '%#x', 'backing_file_offset'], 346e19b3c4SKevin Wolf [uint32_t, '%#x', 'backing_file_size'], 356e19b3c4SKevin Wolf [uint32_t, '%d', 'cluster_bits'], 366e19b3c4SKevin Wolf [uint64_t, '%d', 'size'], 376e19b3c4SKevin Wolf [uint32_t, '%d', 'crypt_method'], 386e19b3c4SKevin Wolf [uint32_t, '%d', 'l1_size'], 396e19b3c4SKevin Wolf [uint64_t, '%#x', 'l1_table_offset'], 406e19b3c4SKevin Wolf [uint64_t, '%#x', 'refcount_table_offset'], 416e19b3c4SKevin Wolf [uint32_t, '%d', 'refcount_table_clusters'], 426e19b3c4SKevin Wolf [uint32_t, '%d', 'nb_snapshots'], 436e19b3c4SKevin Wolf [uint64_t, '%#x', 'snapshot_offset'], 441042ec94SKevin Wolf 451042ec94SKevin Wolf # Version 3 header fields 460485e6eeSMax Reitz [uint64_t, 'mask', 'incompatible_features'], 470485e6eeSMax Reitz [uint64_t, 'mask', 'compatible_features'], 480485e6eeSMax Reitz [uint64_t, 'mask', 'autoclear_features'], 491042ec94SKevin Wolf [uint32_t, '%d', 'refcount_order'], 501042ec94SKevin Wolf [uint32_t, '%d', 'header_length'], 51*02756054SVladimir Sementsov-Ogievskiy ] 526e19b3c4SKevin Wolf 536e19b3c4SKevin Wolf fmt = '>' + ''.join(field[0] for field in fields) 546e19b3c4SKevin Wolf 556e19b3c4SKevin Wolf def __init__(self, fd): 566e19b3c4SKevin Wolf 576e19b3c4SKevin Wolf buf_size = struct.calcsize(QcowHeader.fmt) 586e19b3c4SKevin Wolf 596e19b3c4SKevin Wolf fd.seek(0) 606e19b3c4SKevin Wolf buf = fd.read(buf_size) 616e19b3c4SKevin Wolf 626e19b3c4SKevin Wolf header = struct.unpack(QcowHeader.fmt, buf) 636e19b3c4SKevin Wolf self.__dict__ = dict((field[2], header[i]) 646e19b3c4SKevin Wolf for i, field in enumerate(QcowHeader.fields)) 656e19b3c4SKevin Wolf 661042ec94SKevin Wolf self.set_defaults() 676e19b3c4SKevin Wolf self.cluster_size = 1 << self.cluster_bits 686e19b3c4SKevin Wolf 691042ec94SKevin Wolf fd.seek(self.header_length) 706e19b3c4SKevin Wolf self.load_extensions(fd) 716e19b3c4SKevin Wolf 726e19b3c4SKevin Wolf if self.backing_file_offset: 736e19b3c4SKevin Wolf fd.seek(self.backing_file_offset) 746e19b3c4SKevin Wolf self.backing_file = fd.read(self.backing_file_size) 756e19b3c4SKevin Wolf else: 766e19b3c4SKevin Wolf self.backing_file = None 776e19b3c4SKevin Wolf 781042ec94SKevin Wolf def set_defaults(self): 796e19b3c4SKevin Wolf if self.version == 2: 801042ec94SKevin Wolf self.incompatible_features = 0 811042ec94SKevin Wolf self.compatible_features = 0 821042ec94SKevin Wolf self.autoclear_features = 0 831042ec94SKevin Wolf self.refcount_order = 4 841042ec94SKevin Wolf self.header_length = 72 856e19b3c4SKevin Wolf 866e19b3c4SKevin Wolf def load_extensions(self, fd): 876e19b3c4SKevin Wolf self.extensions = [] 886e19b3c4SKevin Wolf 896e19b3c4SKevin Wolf if self.backing_file_offset != 0: 906e19b3c4SKevin Wolf end = min(self.cluster_size, self.backing_file_offset) 916e19b3c4SKevin Wolf else: 926e19b3c4SKevin Wolf end = self.cluster_size 936e19b3c4SKevin Wolf 946e19b3c4SKevin Wolf while fd.tell() < end: 956e19b3c4SKevin Wolf (magic, length) = struct.unpack('>II', fd.read(8)) 966e19b3c4SKevin Wolf if magic == 0: 976e19b3c4SKevin Wolf break 986e19b3c4SKevin Wolf else: 996e19b3c4SKevin Wolf padded = (length + 7) & ~7 1006e19b3c4SKevin Wolf data = fd.read(padded) 101*02756054SVladimir Sementsov-Ogievskiy self.extensions.append(QcowHeaderExtension(magic, length, 102*02756054SVladimir Sementsov-Ogievskiy data)) 1036e19b3c4SKevin Wolf 1046e19b3c4SKevin Wolf def update_extensions(self, fd): 1056e19b3c4SKevin Wolf 1061042ec94SKevin Wolf fd.seek(self.header_length) 1076e19b3c4SKevin Wolf extensions = self.extensions 1088eb5e674SMax Reitz extensions.append(QcowHeaderExtension(0, 0, b"")) 1096e19b3c4SKevin Wolf for ex in extensions: 1106e19b3c4SKevin Wolf buf = struct.pack('>II', ex.magic, ex.length) 1116e19b3c4SKevin Wolf fd.write(buf) 1126e19b3c4SKevin Wolf fd.write(ex.data) 1136e19b3c4SKevin Wolf 114*02756054SVladimir Sementsov-Ogievskiy if self.backing_file is not None: 1156e19b3c4SKevin Wolf self.backing_file_offset = fd.tell() 1166e19b3c4SKevin Wolf fd.write(self.backing_file) 1176e19b3c4SKevin Wolf 1186e19b3c4SKevin Wolf if fd.tell() > self.cluster_size: 1196e19b3c4SKevin Wolf raise Exception("I think I just broke the image...") 1206e19b3c4SKevin Wolf 1216e19b3c4SKevin Wolf def update(self, fd): 1221042ec94SKevin Wolf header_bytes = self.header_length 1236e19b3c4SKevin Wolf 1246e19b3c4SKevin Wolf self.update_extensions(fd) 1256e19b3c4SKevin Wolf 1266e19b3c4SKevin Wolf fd.seek(0) 1276e19b3c4SKevin Wolf header = tuple(self.__dict__[f] for t, p, f in QcowHeader.fields) 1286e19b3c4SKevin Wolf buf = struct.pack(QcowHeader.fmt, *header) 1296e19b3c4SKevin Wolf buf = buf[0:header_bytes-1] 1306e19b3c4SKevin Wolf fd.write(buf) 1316e19b3c4SKevin Wolf 1326e19b3c4SKevin Wolf def dump(self): 1336e19b3c4SKevin Wolf for f in QcowHeader.fields: 1340485e6eeSMax Reitz value = self.__dict__[f[2]] 1350485e6eeSMax Reitz if f[1] == 'mask': 1360485e6eeSMax Reitz bits = [] 1370485e6eeSMax Reitz for bit in range(64): 1380485e6eeSMax Reitz if value & (1 << bit): 1390485e6eeSMax Reitz bits.append(bit) 1400485e6eeSMax Reitz value_str = str(bits) 1410485e6eeSMax Reitz else: 1420485e6eeSMax Reitz value_str = f[1] % value 1430485e6eeSMax Reitz 1440485e6eeSMax Reitz print("%-25s" % f[2], value_str) 145f03868bdSEduardo Habkost print("") 1466e19b3c4SKevin Wolf 1476e19b3c4SKevin Wolf def dump_extensions(self): 1486e19b3c4SKevin Wolf for ex in self.extensions: 1496e19b3c4SKevin Wolf 1506e19b3c4SKevin Wolf data = ex.data[:ex.length] 1518eb5e674SMax Reitz if all(c in string.printable.encode('ascii') for c in data): 1528eb5e674SMax Reitz data = "'%s'" % data.decode('ascii') 1536e19b3c4SKevin Wolf else: 1546e19b3c4SKevin Wolf data = "<binary>" 1556e19b3c4SKevin Wolf 156f03868bdSEduardo Habkost print("Header extension:") 157f03868bdSEduardo Habkost print("%-25s %#x" % ("magic", ex.magic)) 158f03868bdSEduardo Habkost print("%-25s %d" % ("length", ex.length)) 159f03868bdSEduardo Habkost print("%-25s %s" % ("data", data)) 160f03868bdSEduardo Habkost print("") 1616e19b3c4SKevin Wolf 1626e19b3c4SKevin Wolf 1636e19b3c4SKevin Wolfdef cmd_dump_header(fd): 1646e19b3c4SKevin Wolf h = QcowHeader(fd) 1656e19b3c4SKevin Wolf h.dump() 1666e19b3c4SKevin Wolf h.dump_extensions() 1676e19b3c4SKevin Wolf 168*02756054SVladimir Sementsov-Ogievskiy 1691aa6630eSMax Reitzdef cmd_dump_header_exts(fd): 1701aa6630eSMax Reitz h = QcowHeader(fd) 1711aa6630eSMax Reitz h.dump_extensions() 1721aa6630eSMax Reitz 173*02756054SVladimir Sementsov-Ogievskiy 174c93331c9SKevin Wolfdef cmd_set_header(fd, name, value): 175c93331c9SKevin Wolf try: 176c93331c9SKevin Wolf value = int(value, 0) 177*02756054SVladimir Sementsov-Ogievskiy except ValueError: 178f03868bdSEduardo Habkost print("'%s' is not a valid number" % value) 179c93331c9SKevin Wolf sys.exit(1) 180c93331c9SKevin Wolf 181c93331c9SKevin Wolf fields = (field[2] for field in QcowHeader.fields) 182*02756054SVladimir Sementsov-Ogievskiy if name not in fields: 183f03868bdSEduardo Habkost print("'%s' is not a known header field" % name) 184c93331c9SKevin Wolf sys.exit(1) 185c93331c9SKevin Wolf 186c93331c9SKevin Wolf h = QcowHeader(fd) 187c93331c9SKevin Wolf h.__dict__[name] = value 188c93331c9SKevin Wolf h.update(fd) 189c93331c9SKevin Wolf 190*02756054SVladimir Sementsov-Ogievskiy 1916e19b3c4SKevin Wolfdef cmd_add_header_ext(fd, magic, data): 1926e19b3c4SKevin Wolf try: 1936e19b3c4SKevin Wolf magic = int(magic, 0) 194*02756054SVladimir Sementsov-Ogievskiy except ValueError: 195f03868bdSEduardo Habkost print("'%s' is not a valid magic number" % magic) 1966e19b3c4SKevin Wolf sys.exit(1) 1976e19b3c4SKevin Wolf 1986e19b3c4SKevin Wolf h = QcowHeader(fd) 199*02756054SVladimir Sementsov-Ogievskiy h.extensions.append(QcowHeaderExtension.create(magic, 200*02756054SVladimir Sementsov-Ogievskiy data.encode('ascii'))) 2016e19b3c4SKevin Wolf h.update(fd) 2026e19b3c4SKevin Wolf 203*02756054SVladimir Sementsov-Ogievskiy 20412ac6d3dSKevin Wolfdef cmd_add_header_ext_stdio(fd, magic): 20512ac6d3dSKevin Wolf data = sys.stdin.read() 20612ac6d3dSKevin Wolf cmd_add_header_ext(fd, magic, data) 20712ac6d3dSKevin Wolf 208*02756054SVladimir Sementsov-Ogievskiy 2096e19b3c4SKevin Wolfdef cmd_del_header_ext(fd, magic): 2106e19b3c4SKevin Wolf try: 2116e19b3c4SKevin Wolf magic = int(magic, 0) 212*02756054SVladimir Sementsov-Ogievskiy except ValueError: 213f03868bdSEduardo Habkost print("'%s' is not a valid magic number" % magic) 2146e19b3c4SKevin Wolf sys.exit(1) 2156e19b3c4SKevin Wolf 2166e19b3c4SKevin Wolf h = QcowHeader(fd) 2176e19b3c4SKevin Wolf found = False 2186e19b3c4SKevin Wolf 2196e19b3c4SKevin Wolf for ex in h.extensions: 2206e19b3c4SKevin Wolf if ex.magic == magic: 2216e19b3c4SKevin Wolf found = True 2226e19b3c4SKevin Wolf h.extensions.remove(ex) 2236e19b3c4SKevin Wolf 2246e19b3c4SKevin Wolf if not found: 225f03868bdSEduardo Habkost print("No such header extension") 2266e19b3c4SKevin Wolf return 2276e19b3c4SKevin Wolf 2286e19b3c4SKevin Wolf h.update(fd) 2296e19b3c4SKevin Wolf 230*02756054SVladimir Sementsov-Ogievskiy 2311b2eff62SStefan Hajnoczidef cmd_set_feature_bit(fd, group, bit): 2321b2eff62SStefan Hajnoczi try: 2331b2eff62SStefan Hajnoczi bit = int(bit, 0) 2341b2eff62SStefan Hajnoczi if bit < 0 or bit >= 64: 2351b2eff62SStefan Hajnoczi raise ValueError 236*02756054SVladimir Sementsov-Ogievskiy except ValueError: 237f03868bdSEduardo Habkost print("'%s' is not a valid bit number in range [0, 64)" % bit) 2381b2eff62SStefan Hajnoczi sys.exit(1) 2391b2eff62SStefan Hajnoczi 2401b2eff62SStefan Hajnoczi h = QcowHeader(fd) 2411b2eff62SStefan Hajnoczi if group == 'incompatible': 2421b2eff62SStefan Hajnoczi h.incompatible_features |= 1 << bit 2431b2eff62SStefan Hajnoczi elif group == 'compatible': 2441b2eff62SStefan Hajnoczi h.compatible_features |= 1 << bit 2451b2eff62SStefan Hajnoczi elif group == 'autoclear': 2461b2eff62SStefan Hajnoczi h.autoclear_features |= 1 << bit 2471b2eff62SStefan Hajnoczi else: 248*02756054SVladimir Sementsov-Ogievskiy print("'%s' is not a valid group, try " 249*02756054SVladimir Sementsov-Ogievskiy "'incompatible', 'compatible', or 'autoclear'" % group) 2501b2eff62SStefan Hajnoczi sys.exit(1) 2511b2eff62SStefan Hajnoczi 2521b2eff62SStefan Hajnoczi h.update(fd) 2531b2eff62SStefan Hajnoczi 254*02756054SVladimir Sementsov-Ogievskiy 2556e19b3c4SKevin Wolfcmds = [ 256*02756054SVladimir Sementsov-Ogievskiy ['dump-header', cmd_dump_header, 0, 257*02756054SVladimir Sementsov-Ogievskiy 'Dump image header and header extensions'], 258*02756054SVladimir Sementsov-Ogievskiy ['dump-header-exts', cmd_dump_header_exts, 0, 259*02756054SVladimir Sementsov-Ogievskiy 'Dump image header extensions'], 260c93331c9SKevin Wolf ['set-header', cmd_set_header, 2, 'Set a field in the header'], 2616e19b3c4SKevin Wolf ['add-header-ext', cmd_add_header_ext, 2, 'Add a header extension'], 262*02756054SVladimir Sementsov-Ogievskiy ['add-header-ext-stdio', cmd_add_header_ext_stdio, 1, 263*02756054SVladimir Sementsov-Ogievskiy 'Add a header extension, data from stdin'], 2646e19b3c4SKevin Wolf ['del-header-ext', cmd_del_header_ext, 1, 'Delete a header extension'], 2651b2eff62SStefan Hajnoczi ['set-feature-bit', cmd_set_feature_bit, 2, 'Set a feature bit'], 2666e19b3c4SKevin Wolf] 2676e19b3c4SKevin Wolf 268*02756054SVladimir Sementsov-Ogievskiy 2696e19b3c4SKevin Wolfdef main(filename, cmd, args): 2706e19b3c4SKevin Wolf fd = open(filename, "r+b") 2716e19b3c4SKevin Wolf try: 2726e19b3c4SKevin Wolf for name, handler, num_args, desc in cmds: 2736e19b3c4SKevin Wolf if name != cmd: 2746e19b3c4SKevin Wolf continue 2756e19b3c4SKevin Wolf elif len(args) != num_args: 2766e19b3c4SKevin Wolf usage() 2776e19b3c4SKevin Wolf return 2786e19b3c4SKevin Wolf else: 2796e19b3c4SKevin Wolf handler(fd, *args) 2806e19b3c4SKevin Wolf return 281f03868bdSEduardo Habkost print("Unknown command '%s'" % cmd) 2826e19b3c4SKevin Wolf finally: 2836e19b3c4SKevin Wolf fd.close() 2846e19b3c4SKevin Wolf 285*02756054SVladimir Sementsov-Ogievskiy 2866e19b3c4SKevin Wolfdef usage(): 287f03868bdSEduardo Habkost print("Usage: %s <file> <cmd> [<arg>, ...]" % sys.argv[0]) 288f03868bdSEduardo Habkost print("") 289f03868bdSEduardo Habkost print("Supported commands:") 2906e19b3c4SKevin Wolf for name, handler, num_args, desc in cmds: 291f03868bdSEduardo Habkost print(" %-20s - %s" % (name, desc)) 2926e19b3c4SKevin Wolf 293*02756054SVladimir Sementsov-Ogievskiy 294d2ef210cSKevin Wolfif __name__ == '__main__': 2956e19b3c4SKevin Wolf if len(sys.argv) < 3: 2966e19b3c4SKevin Wolf usage() 2976e19b3c4SKevin Wolf sys.exit(1) 2986e19b3c4SKevin Wolf 2996e19b3c4SKevin Wolf main(sys.argv[1], sys.argv[2], sys.argv[3:]) 300