2012-02-15 15:36:03 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2018-06-08 12:29:43 +00:00
|
|
|
from __future__ import print_function
|
2012-02-15 15:36:03 +00:00
|
|
|
import sys
|
|
|
|
import struct
|
|
|
|
import string
|
|
|
|
|
|
|
|
class QcowHeaderExtension:
|
|
|
|
|
|
|
|
def __init__(self, magic, length, data):
|
2014-11-25 17:12:41 +00:00
|
|
|
if length % 8 != 0:
|
|
|
|
padding = 8 - (length % 8)
|
2018-10-22 13:53:01 +00:00
|
|
|
data += b"\0" * padding
|
2014-11-25 17:12:41 +00:00
|
|
|
|
2012-02-15 15:36:03 +00:00
|
|
|
self.magic = magic
|
|
|
|
self.length = length
|
|
|
|
self.data = data
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create(cls, magic, data):
|
|
|
|
return QcowHeaderExtension(magic, len(data), data)
|
|
|
|
|
|
|
|
class QcowHeader:
|
|
|
|
|
|
|
|
uint32_t = 'I'
|
|
|
|
uint64_t = 'Q'
|
|
|
|
|
|
|
|
fields = [
|
|
|
|
# Version 2 header fields
|
|
|
|
[ uint32_t, '%#x', 'magic' ],
|
|
|
|
[ uint32_t, '%d', 'version' ],
|
|
|
|
[ uint64_t, '%#x', 'backing_file_offset' ],
|
|
|
|
[ uint32_t, '%#x', 'backing_file_size' ],
|
|
|
|
[ uint32_t, '%d', 'cluster_bits' ],
|
|
|
|
[ uint64_t, '%d', 'size' ],
|
|
|
|
[ uint32_t, '%d', 'crypt_method' ],
|
|
|
|
[ uint32_t, '%d', 'l1_size' ],
|
|
|
|
[ uint64_t, '%#x', 'l1_table_offset' ],
|
|
|
|
[ uint64_t, '%#x', 'refcount_table_offset' ],
|
|
|
|
[ uint32_t, '%d', 'refcount_table_clusters' ],
|
|
|
|
[ uint32_t, '%d', 'nb_snapshots' ],
|
|
|
|
[ uint64_t, '%#x', 'snapshot_offset' ],
|
2012-04-12 15:21:44 +00:00
|
|
|
|
|
|
|
# Version 3 header fields
|
|
|
|
[ uint64_t, '%#x', 'incompatible_features' ],
|
|
|
|
[ uint64_t, '%#x', 'compatible_features' ],
|
|
|
|
[ uint64_t, '%#x', 'autoclear_features' ],
|
|
|
|
[ uint32_t, '%d', 'refcount_order' ],
|
|
|
|
[ uint32_t, '%d', 'header_length' ],
|
2012-02-15 15:36:03 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
fmt = '>' + ''.join(field[0] for field in fields)
|
|
|
|
|
|
|
|
def __init__(self, fd):
|
|
|
|
|
|
|
|
buf_size = struct.calcsize(QcowHeader.fmt)
|
|
|
|
|
|
|
|
fd.seek(0)
|
|
|
|
buf = fd.read(buf_size)
|
|
|
|
|
|
|
|
header = struct.unpack(QcowHeader.fmt, buf)
|
|
|
|
self.__dict__ = dict((field[2], header[i])
|
|
|
|
for i, field in enumerate(QcowHeader.fields))
|
|
|
|
|
2012-04-12 15:21:44 +00:00
|
|
|
self.set_defaults()
|
2012-02-15 15:36:03 +00:00
|
|
|
self.cluster_size = 1 << self.cluster_bits
|
|
|
|
|
2012-04-12 15:21:44 +00:00
|
|
|
fd.seek(self.header_length)
|
2012-02-15 15:36:03 +00:00
|
|
|
self.load_extensions(fd)
|
|
|
|
|
|
|
|
if self.backing_file_offset:
|
|
|
|
fd.seek(self.backing_file_offset)
|
|
|
|
self.backing_file = fd.read(self.backing_file_size)
|
|
|
|
else:
|
|
|
|
self.backing_file = None
|
|
|
|
|
2012-04-12 15:21:44 +00:00
|
|
|
def set_defaults(self):
|
2012-02-15 15:36:03 +00:00
|
|
|
if self.version == 2:
|
2012-04-12 15:21:44 +00:00
|
|
|
self.incompatible_features = 0
|
|
|
|
self.compatible_features = 0
|
|
|
|
self.autoclear_features = 0
|
|
|
|
self.refcount_order = 4
|
|
|
|
self.header_length = 72
|
2012-02-15 15:36:03 +00:00
|
|
|
|
|
|
|
def load_extensions(self, fd):
|
|
|
|
self.extensions = []
|
|
|
|
|
|
|
|
if self.backing_file_offset != 0:
|
|
|
|
end = min(self.cluster_size, self.backing_file_offset)
|
|
|
|
else:
|
|
|
|
end = self.cluster_size
|
|
|
|
|
|
|
|
while fd.tell() < end:
|
|
|
|
(magic, length) = struct.unpack('>II', fd.read(8))
|
|
|
|
if magic == 0:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
padded = (length + 7) & ~7
|
|
|
|
data = fd.read(padded)
|
|
|
|
self.extensions.append(QcowHeaderExtension(magic, length, data))
|
|
|
|
|
|
|
|
def update_extensions(self, fd):
|
|
|
|
|
2012-04-12 15:21:44 +00:00
|
|
|
fd.seek(self.header_length)
|
2012-02-15 15:36:03 +00:00
|
|
|
extensions = self.extensions
|
2018-10-22 13:53:01 +00:00
|
|
|
extensions.append(QcowHeaderExtension(0, 0, b""))
|
2012-02-15 15:36:03 +00:00
|
|
|
for ex in extensions:
|
|
|
|
buf = struct.pack('>II', ex.magic, ex.length)
|
|
|
|
fd.write(buf)
|
|
|
|
fd.write(ex.data)
|
|
|
|
|
|
|
|
if self.backing_file != None:
|
|
|
|
self.backing_file_offset = fd.tell()
|
|
|
|
fd.write(self.backing_file)
|
|
|
|
|
|
|
|
if fd.tell() > self.cluster_size:
|
|
|
|
raise Exception("I think I just broke the image...")
|
|
|
|
|
|
|
|
|
|
|
|
def update(self, fd):
|
2012-04-12 15:21:44 +00:00
|
|
|
header_bytes = self.header_length
|
2012-02-15 15:36:03 +00:00
|
|
|
|
|
|
|
self.update_extensions(fd)
|
|
|
|
|
|
|
|
fd.seek(0)
|
|
|
|
header = tuple(self.__dict__[f] for t, p, f in QcowHeader.fields)
|
|
|
|
buf = struct.pack(QcowHeader.fmt, *header)
|
|
|
|
buf = buf[0:header_bytes-1]
|
|
|
|
fd.write(buf)
|
|
|
|
|
|
|
|
def dump(self):
|
|
|
|
for f in QcowHeader.fields:
|
2018-06-08 12:29:43 +00:00
|
|
|
print("%-25s" % f[2], f[1] % self.__dict__[f[2]])
|
|
|
|
print("")
|
2012-02-15 15:36:03 +00:00
|
|
|
|
|
|
|
def dump_extensions(self):
|
|
|
|
for ex in self.extensions:
|
|
|
|
|
|
|
|
data = ex.data[:ex.length]
|
2018-10-22 13:53:01 +00:00
|
|
|
if all(c in string.printable.encode('ascii') for c in data):
|
|
|
|
data = "'%s'" % data.decode('ascii')
|
2012-02-15 15:36:03 +00:00
|
|
|
else:
|
|
|
|
data = "<binary>"
|
|
|
|
|
2018-06-08 12:29:43 +00:00
|
|
|
print("Header extension:")
|
|
|
|
print("%-25s %#x" % ("magic", ex.magic))
|
|
|
|
print("%-25s %d" % ("length", ex.length))
|
|
|
|
print("%-25s %s" % ("data", data))
|
|
|
|
print("")
|
2012-02-15 15:36:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def cmd_dump_header(fd):
|
|
|
|
h = QcowHeader(fd)
|
|
|
|
h.dump()
|
|
|
|
h.dump_extensions()
|
|
|
|
|
2013-05-13 12:00:15 +00:00
|
|
|
def cmd_set_header(fd, name, value):
|
|
|
|
try:
|
|
|
|
value = int(value, 0)
|
|
|
|
except:
|
2018-06-08 12:29:43 +00:00
|
|
|
print("'%s' is not a valid number" % value)
|
2013-05-13 12:00:15 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
fields = (field[2] for field in QcowHeader.fields)
|
|
|
|
if not name in fields:
|
2018-06-08 12:29:43 +00:00
|
|
|
print("'%s' is not a known header field" % name)
|
2013-05-13 12:00:15 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
h = QcowHeader(fd)
|
|
|
|
h.__dict__[name] = value
|
|
|
|
h.update(fd)
|
|
|
|
|
2012-02-15 15:36:03 +00:00
|
|
|
def cmd_add_header_ext(fd, magic, data):
|
|
|
|
try:
|
|
|
|
magic = int(magic, 0)
|
|
|
|
except:
|
2018-06-08 12:29:43 +00:00
|
|
|
print("'%s' is not a valid magic number" % magic)
|
2012-02-15 15:36:03 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
h = QcowHeader(fd)
|
2018-10-22 13:53:01 +00:00
|
|
|
h.extensions.append(QcowHeaderExtension.create(magic, data.encode('ascii')))
|
2012-02-15 15:36:03 +00:00
|
|
|
h.update(fd)
|
|
|
|
|
2014-07-17 09:41:53 +00:00
|
|
|
def cmd_add_header_ext_stdio(fd, magic):
|
|
|
|
data = sys.stdin.read()
|
|
|
|
cmd_add_header_ext(fd, magic, data)
|
|
|
|
|
2012-02-15 15:36:03 +00:00
|
|
|
def cmd_del_header_ext(fd, magic):
|
|
|
|
try:
|
|
|
|
magic = int(magic, 0)
|
|
|
|
except:
|
2018-06-08 12:29:43 +00:00
|
|
|
print("'%s' is not a valid magic number" % magic)
|
2012-02-15 15:36:03 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
h = QcowHeader(fd)
|
|
|
|
found = False
|
|
|
|
|
|
|
|
for ex in h.extensions:
|
|
|
|
if ex.magic == magic:
|
|
|
|
found = True
|
|
|
|
h.extensions.remove(ex)
|
|
|
|
|
|
|
|
if not found:
|
2018-06-08 12:29:43 +00:00
|
|
|
print("No such header extension")
|
2012-02-15 15:36:03 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
h.update(fd)
|
|
|
|
|
2012-06-14 12:58:17 +00:00
|
|
|
def cmd_set_feature_bit(fd, group, bit):
|
|
|
|
try:
|
|
|
|
bit = int(bit, 0)
|
|
|
|
if bit < 0 or bit >= 64:
|
|
|
|
raise ValueError
|
|
|
|
except:
|
2018-06-08 12:29:43 +00:00
|
|
|
print("'%s' is not a valid bit number in range [0, 64)" % bit)
|
2012-06-14 12:58:17 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
h = QcowHeader(fd)
|
|
|
|
if group == 'incompatible':
|
|
|
|
h.incompatible_features |= 1 << bit
|
|
|
|
elif group == 'compatible':
|
|
|
|
h.compatible_features |= 1 << bit
|
|
|
|
elif group == 'autoclear':
|
|
|
|
h.autoclear_features |= 1 << bit
|
|
|
|
else:
|
2018-06-08 12:29:43 +00:00
|
|
|
print("'%s' is not a valid group, try 'incompatible', 'compatible', or 'autoclear'" % group)
|
2012-06-14 12:58:17 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
h.update(fd)
|
|
|
|
|
2012-02-15 15:36:03 +00:00
|
|
|
cmds = [
|
2014-07-17 09:41:53 +00:00
|
|
|
[ 'dump-header', cmd_dump_header, 0, 'Dump image header and header extensions' ],
|
|
|
|
[ 'set-header', cmd_set_header, 2, 'Set a field in the header'],
|
|
|
|
[ 'add-header-ext', cmd_add_header_ext, 2, 'Add a header extension' ],
|
|
|
|
[ 'add-header-ext-stdio', cmd_add_header_ext_stdio, 1, 'Add a header extension, data from stdin' ],
|
|
|
|
[ 'del-header-ext', cmd_del_header_ext, 1, 'Delete a header extension' ],
|
|
|
|
[ 'set-feature-bit', cmd_set_feature_bit, 2, 'Set a feature bit'],
|
2012-02-15 15:36:03 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def main(filename, cmd, args):
|
|
|
|
fd = open(filename, "r+b")
|
|
|
|
try:
|
|
|
|
for name, handler, num_args, desc in cmds:
|
|
|
|
if name != cmd:
|
|
|
|
continue
|
|
|
|
elif len(args) != num_args:
|
|
|
|
usage()
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
handler(fd, *args)
|
|
|
|
return
|
2018-06-08 12:29:43 +00:00
|
|
|
print("Unknown command '%s'" % cmd)
|
2012-02-15 15:36:03 +00:00
|
|
|
finally:
|
|
|
|
fd.close()
|
|
|
|
|
|
|
|
def usage():
|
2018-06-08 12:29:43 +00:00
|
|
|
print("Usage: %s <file> <cmd> [<arg>, ...]" % sys.argv[0])
|
|
|
|
print("")
|
|
|
|
print("Supported commands:")
|
2012-02-15 15:36:03 +00:00
|
|
|
for name, handler, num_args, desc in cmds:
|
2018-06-08 12:29:43 +00:00
|
|
|
print(" %-20s - %s" % (name, desc))
|
2012-02-15 15:36:03 +00:00
|
|
|
|
2012-10-26 18:31:15 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
if len(sys.argv) < 3:
|
|
|
|
usage()
|
|
|
|
sys.exit(1)
|
2012-02-15 15:36:03 +00:00
|
|
|
|
2012-10-26 18:31:15 +00:00
|
|
|
main(sys.argv[1], sys.argv[2], sys.argv[3:])
|