16e19b3c4SKevin Wolf#!/usr/bin/env python 26e19b3c4SKevin Wolf 36e19b3c4SKevin Wolfimport sys 46e19b3c4SKevin Wolfimport struct 56e19b3c4SKevin Wolfimport string 66e19b3c4SKevin Wolf 76e19b3c4SKevin Wolfclass QcowHeaderExtension: 86e19b3c4SKevin Wolf 96e19b3c4SKevin Wolf def __init__(self, magic, length, data): 106e19b3c4SKevin Wolf self.magic = magic 116e19b3c4SKevin Wolf self.length = length 126e19b3c4SKevin Wolf self.data = data 136e19b3c4SKevin Wolf 146e19b3c4SKevin Wolf @classmethod 156e19b3c4SKevin Wolf def create(cls, magic, data): 166e19b3c4SKevin Wolf return QcowHeaderExtension(magic, len(data), data) 176e19b3c4SKevin Wolf 186e19b3c4SKevin Wolfclass QcowHeader: 196e19b3c4SKevin Wolf 206e19b3c4SKevin Wolf uint32_t = 'I' 216e19b3c4SKevin Wolf uint64_t = 'Q' 226e19b3c4SKevin Wolf 236e19b3c4SKevin Wolf fields = [ 246e19b3c4SKevin Wolf # Version 2 header fields 256e19b3c4SKevin Wolf [ uint32_t, '%#x', 'magic' ], 266e19b3c4SKevin Wolf [ uint32_t, '%d', 'version' ], 276e19b3c4SKevin Wolf [ uint64_t, '%#x', 'backing_file_offset' ], 286e19b3c4SKevin Wolf [ uint32_t, '%#x', 'backing_file_size' ], 296e19b3c4SKevin Wolf [ uint32_t, '%d', 'cluster_bits' ], 306e19b3c4SKevin Wolf [ uint64_t, '%d', 'size' ], 316e19b3c4SKevin Wolf [ uint32_t, '%d', 'crypt_method' ], 326e19b3c4SKevin Wolf [ uint32_t, '%d', 'l1_size' ], 336e19b3c4SKevin Wolf [ uint64_t, '%#x', 'l1_table_offset' ], 346e19b3c4SKevin Wolf [ uint64_t, '%#x', 'refcount_table_offset' ], 356e19b3c4SKevin Wolf [ uint32_t, '%d', 'refcount_table_clusters' ], 366e19b3c4SKevin Wolf [ uint32_t, '%d', 'nb_snapshots' ], 376e19b3c4SKevin Wolf [ uint64_t, '%#x', 'snapshot_offset' ], 381042ec94SKevin Wolf 391042ec94SKevin Wolf # Version 3 header fields 401042ec94SKevin Wolf [ uint64_t, '%#x', 'incompatible_features' ], 411042ec94SKevin Wolf [ uint64_t, '%#x', 'compatible_features' ], 421042ec94SKevin Wolf [ uint64_t, '%#x', 'autoclear_features' ], 431042ec94SKevin Wolf [ uint32_t, '%d', 'refcount_order' ], 441042ec94SKevin Wolf [ uint32_t, '%d', 'header_length' ], 456e19b3c4SKevin Wolf ]; 466e19b3c4SKevin Wolf 476e19b3c4SKevin Wolf fmt = '>' + ''.join(field[0] for field in fields) 486e19b3c4SKevin Wolf 496e19b3c4SKevin Wolf def __init__(self, fd): 506e19b3c4SKevin Wolf 516e19b3c4SKevin Wolf buf_size = struct.calcsize(QcowHeader.fmt) 526e19b3c4SKevin Wolf 536e19b3c4SKevin Wolf fd.seek(0) 546e19b3c4SKevin Wolf buf = fd.read(buf_size) 556e19b3c4SKevin Wolf 566e19b3c4SKevin Wolf header = struct.unpack(QcowHeader.fmt, buf) 576e19b3c4SKevin Wolf self.__dict__ = dict((field[2], header[i]) 586e19b3c4SKevin Wolf for i, field in enumerate(QcowHeader.fields)) 596e19b3c4SKevin Wolf 601042ec94SKevin Wolf self.set_defaults() 616e19b3c4SKevin Wolf self.cluster_size = 1 << self.cluster_bits 626e19b3c4SKevin Wolf 631042ec94SKevin Wolf fd.seek(self.header_length) 646e19b3c4SKevin Wolf self.load_extensions(fd) 656e19b3c4SKevin Wolf 666e19b3c4SKevin Wolf if self.backing_file_offset: 676e19b3c4SKevin Wolf fd.seek(self.backing_file_offset) 686e19b3c4SKevin Wolf self.backing_file = fd.read(self.backing_file_size) 696e19b3c4SKevin Wolf else: 706e19b3c4SKevin Wolf self.backing_file = None 716e19b3c4SKevin Wolf 721042ec94SKevin Wolf def set_defaults(self): 736e19b3c4SKevin Wolf if self.version == 2: 741042ec94SKevin Wolf self.incompatible_features = 0 751042ec94SKevin Wolf self.compatible_features = 0 761042ec94SKevin Wolf self.autoclear_features = 0 771042ec94SKevin Wolf self.refcount_order = 4 781042ec94SKevin Wolf self.header_length = 72 796e19b3c4SKevin Wolf 806e19b3c4SKevin Wolf def load_extensions(self, fd): 816e19b3c4SKevin Wolf self.extensions = [] 826e19b3c4SKevin Wolf 836e19b3c4SKevin Wolf if self.backing_file_offset != 0: 846e19b3c4SKevin Wolf end = min(self.cluster_size, self.backing_file_offset) 856e19b3c4SKevin Wolf else: 866e19b3c4SKevin Wolf end = self.cluster_size 876e19b3c4SKevin Wolf 886e19b3c4SKevin Wolf while fd.tell() < end: 896e19b3c4SKevin Wolf (magic, length) = struct.unpack('>II', fd.read(8)) 906e19b3c4SKevin Wolf if magic == 0: 916e19b3c4SKevin Wolf break 926e19b3c4SKevin Wolf else: 936e19b3c4SKevin Wolf padded = (length + 7) & ~7 946e19b3c4SKevin Wolf data = fd.read(padded) 956e19b3c4SKevin Wolf self.extensions.append(QcowHeaderExtension(magic, length, data)) 966e19b3c4SKevin Wolf 976e19b3c4SKevin Wolf def update_extensions(self, fd): 986e19b3c4SKevin Wolf 991042ec94SKevin Wolf fd.seek(self.header_length) 1006e19b3c4SKevin Wolf extensions = self.extensions 1016e19b3c4SKevin Wolf extensions.append(QcowHeaderExtension(0, 0, "")) 1026e19b3c4SKevin Wolf for ex in extensions: 1036e19b3c4SKevin Wolf buf = struct.pack('>II', ex.magic, ex.length) 1046e19b3c4SKevin Wolf fd.write(buf) 1056e19b3c4SKevin Wolf fd.write(ex.data) 1066e19b3c4SKevin Wolf 1076e19b3c4SKevin Wolf if self.backing_file != None: 1086e19b3c4SKevin Wolf self.backing_file_offset = fd.tell() 1096e19b3c4SKevin Wolf fd.write(self.backing_file) 1106e19b3c4SKevin Wolf 1116e19b3c4SKevin Wolf if fd.tell() > self.cluster_size: 1126e19b3c4SKevin Wolf raise Exception("I think I just broke the image...") 1136e19b3c4SKevin Wolf 1146e19b3c4SKevin Wolf 1156e19b3c4SKevin Wolf def update(self, fd): 1161042ec94SKevin Wolf header_bytes = self.header_length 1176e19b3c4SKevin Wolf 1186e19b3c4SKevin Wolf self.update_extensions(fd) 1196e19b3c4SKevin Wolf 1206e19b3c4SKevin Wolf fd.seek(0) 1216e19b3c4SKevin Wolf header = tuple(self.__dict__[f] for t, p, f in QcowHeader.fields) 1226e19b3c4SKevin Wolf buf = struct.pack(QcowHeader.fmt, *header) 1236e19b3c4SKevin Wolf buf = buf[0:header_bytes-1] 1246e19b3c4SKevin Wolf fd.write(buf) 1256e19b3c4SKevin Wolf 1266e19b3c4SKevin Wolf def dump(self): 1276e19b3c4SKevin Wolf for f in QcowHeader.fields: 1286e19b3c4SKevin Wolf print "%-25s" % f[2], f[1] % self.__dict__[f[2]] 1296e19b3c4SKevin Wolf print "" 1306e19b3c4SKevin Wolf 1316e19b3c4SKevin Wolf def dump_extensions(self): 1326e19b3c4SKevin Wolf for ex in self.extensions: 1336e19b3c4SKevin Wolf 1346e19b3c4SKevin Wolf data = ex.data[:ex.length] 1356e19b3c4SKevin Wolf if all(c in string.printable for c in data): 1366e19b3c4SKevin Wolf data = "'%s'" % data 1376e19b3c4SKevin Wolf else: 1386e19b3c4SKevin Wolf data = "<binary>" 1396e19b3c4SKevin Wolf 1406e19b3c4SKevin Wolf print "Header extension:" 1416e19b3c4SKevin Wolf print "%-25s %#x" % ("magic", ex.magic) 1426e19b3c4SKevin Wolf print "%-25s %d" % ("length", ex.length) 1436e19b3c4SKevin Wolf print "%-25s %s" % ("data", data) 1446e19b3c4SKevin Wolf print "" 1456e19b3c4SKevin Wolf 1466e19b3c4SKevin Wolf 1476e19b3c4SKevin Wolfdef cmd_dump_header(fd): 1486e19b3c4SKevin Wolf h = QcowHeader(fd) 1496e19b3c4SKevin Wolf h.dump() 1506e19b3c4SKevin Wolf h.dump_extensions() 1516e19b3c4SKevin Wolf 152*c93331c9SKevin Wolfdef cmd_set_header(fd, name, value): 153*c93331c9SKevin Wolf try: 154*c93331c9SKevin Wolf value = int(value, 0) 155*c93331c9SKevin Wolf except: 156*c93331c9SKevin Wolf print "'%s' is not a valid number" % value 157*c93331c9SKevin Wolf sys.exit(1) 158*c93331c9SKevin Wolf 159*c93331c9SKevin Wolf fields = (field[2] for field in QcowHeader.fields) 160*c93331c9SKevin Wolf if not name in fields: 161*c93331c9SKevin Wolf print "'%s' is not a known header field" % name 162*c93331c9SKevin Wolf sys.exit(1) 163*c93331c9SKevin Wolf 164*c93331c9SKevin Wolf h = QcowHeader(fd) 165*c93331c9SKevin Wolf h.__dict__[name] = value 166*c93331c9SKevin Wolf h.update(fd) 167*c93331c9SKevin Wolf 1686e19b3c4SKevin Wolfdef cmd_add_header_ext(fd, magic, data): 1696e19b3c4SKevin Wolf try: 1706e19b3c4SKevin Wolf magic = int(magic, 0) 1716e19b3c4SKevin Wolf except: 1726e19b3c4SKevin Wolf print "'%s' is not a valid magic number" % magic 1736e19b3c4SKevin Wolf sys.exit(1) 1746e19b3c4SKevin Wolf 1756e19b3c4SKevin Wolf h = QcowHeader(fd) 1766e19b3c4SKevin Wolf h.extensions.append(QcowHeaderExtension.create(magic, data)) 1776e19b3c4SKevin Wolf h.update(fd) 1786e19b3c4SKevin Wolf 1796e19b3c4SKevin Wolfdef cmd_del_header_ext(fd, magic): 1806e19b3c4SKevin Wolf try: 1816e19b3c4SKevin Wolf magic = int(magic, 0) 1826e19b3c4SKevin Wolf except: 1836e19b3c4SKevin Wolf print "'%s' is not a valid magic number" % magic 1846e19b3c4SKevin Wolf sys.exit(1) 1856e19b3c4SKevin Wolf 1866e19b3c4SKevin Wolf h = QcowHeader(fd) 1876e19b3c4SKevin Wolf found = False 1886e19b3c4SKevin Wolf 1896e19b3c4SKevin Wolf for ex in h.extensions: 1906e19b3c4SKevin Wolf if ex.magic == magic: 1916e19b3c4SKevin Wolf found = True 1926e19b3c4SKevin Wolf h.extensions.remove(ex) 1936e19b3c4SKevin Wolf 1946e19b3c4SKevin Wolf if not found: 1956e19b3c4SKevin Wolf print "No such header extension" 1966e19b3c4SKevin Wolf return 1976e19b3c4SKevin Wolf 1986e19b3c4SKevin Wolf h.update(fd) 1996e19b3c4SKevin Wolf 2001b2eff62SStefan Hajnoczidef cmd_set_feature_bit(fd, group, bit): 2011b2eff62SStefan Hajnoczi try: 2021b2eff62SStefan Hajnoczi bit = int(bit, 0) 2031b2eff62SStefan Hajnoczi if bit < 0 or bit >= 64: 2041b2eff62SStefan Hajnoczi raise ValueError 2051b2eff62SStefan Hajnoczi except: 2061b2eff62SStefan Hajnoczi print "'%s' is not a valid bit number in range [0, 64)" % bit 2071b2eff62SStefan Hajnoczi sys.exit(1) 2081b2eff62SStefan Hajnoczi 2091b2eff62SStefan Hajnoczi h = QcowHeader(fd) 2101b2eff62SStefan Hajnoczi if group == 'incompatible': 2111b2eff62SStefan Hajnoczi h.incompatible_features |= 1 << bit 2121b2eff62SStefan Hajnoczi elif group == 'compatible': 2131b2eff62SStefan Hajnoczi h.compatible_features |= 1 << bit 2141b2eff62SStefan Hajnoczi elif group == 'autoclear': 2151b2eff62SStefan Hajnoczi h.autoclear_features |= 1 << bit 2161b2eff62SStefan Hajnoczi else: 2171b2eff62SStefan Hajnoczi print "'%s' is not a valid group, try 'incompatible', 'compatible', or 'autoclear'" % group 2181b2eff62SStefan Hajnoczi sys.exit(1) 2191b2eff62SStefan Hajnoczi 2201b2eff62SStefan Hajnoczi h.update(fd) 2211b2eff62SStefan Hajnoczi 2226e19b3c4SKevin Wolfcmds = [ 2236e19b3c4SKevin Wolf [ 'dump-header', cmd_dump_header, 0, 'Dump image header and header extensions' ], 224*c93331c9SKevin Wolf [ 'set-header', cmd_set_header, 2, 'Set a field in the header'], 2256e19b3c4SKevin Wolf [ 'add-header-ext', cmd_add_header_ext, 2, 'Add a header extension' ], 2266e19b3c4SKevin Wolf [ 'del-header-ext', cmd_del_header_ext, 1, 'Delete a header extension' ], 2271b2eff62SStefan Hajnoczi [ 'set-feature-bit', cmd_set_feature_bit, 2, 'Set a feature bit'], 2286e19b3c4SKevin Wolf] 2296e19b3c4SKevin Wolf 2306e19b3c4SKevin Wolfdef main(filename, cmd, args): 2316e19b3c4SKevin Wolf fd = open(filename, "r+b") 2326e19b3c4SKevin Wolf try: 2336e19b3c4SKevin Wolf for name, handler, num_args, desc in cmds: 2346e19b3c4SKevin Wolf if name != cmd: 2356e19b3c4SKevin Wolf continue 2366e19b3c4SKevin Wolf elif len(args) != num_args: 2376e19b3c4SKevin Wolf usage() 2386e19b3c4SKevin Wolf return 2396e19b3c4SKevin Wolf else: 2406e19b3c4SKevin Wolf handler(fd, *args) 2416e19b3c4SKevin Wolf return 2426e19b3c4SKevin Wolf print "Unknown command '%s'" % cmd 2436e19b3c4SKevin Wolf finally: 2446e19b3c4SKevin Wolf fd.close() 2456e19b3c4SKevin Wolf 2466e19b3c4SKevin Wolfdef usage(): 2476e19b3c4SKevin Wolf print "Usage: %s <file> <cmd> [<arg>, ...]" % sys.argv[0] 2486e19b3c4SKevin Wolf print "" 2496e19b3c4SKevin Wolf print "Supported commands:" 2506e19b3c4SKevin Wolf for name, handler, num_args, desc in cmds: 2516e19b3c4SKevin Wolf print " %-20s - %s" % (name, desc) 2526e19b3c4SKevin Wolf 253d2ef210cSKevin Wolfif __name__ == '__main__': 2546e19b3c4SKevin Wolf if len(sys.argv) < 3: 2556e19b3c4SKevin Wolf usage() 2566e19b3c4SKevin Wolf sys.exit(1) 2576e19b3c4SKevin Wolf 2586e19b3c4SKevin Wolf main(sys.argv[1], sys.argv[2], sys.argv[3:]) 259