3 from __future__
import print_function
8 class QcowHeaderExtension
:
10 def __init__(self
, magic
, length
, data
):
12 padding
= 8 - (length
% 8)
13 data
+= b
"\0" * padding
20 def create(cls
, magic
, data
):
21 return QcowHeaderExtension(magic
, len(data
), data
)
29 # Version 2 header fields
30 [ uint32_t
, '%#x', 'magic' ],
31 [ uint32_t
, '%d', 'version' ],
32 [ uint64_t
, '%#x', 'backing_file_offset' ],
33 [ uint32_t
, '%#x', 'backing_file_size' ],
34 [ uint32_t
, '%d', 'cluster_bits' ],
35 [ uint64_t
, '%d', 'size' ],
36 [ uint32_t
, '%d', 'crypt_method' ],
37 [ uint32_t
, '%d', 'l1_size' ],
38 [ uint64_t
, '%#x', 'l1_table_offset' ],
39 [ uint64_t
, '%#x', 'refcount_table_offset' ],
40 [ uint32_t
, '%d', 'refcount_table_clusters' ],
41 [ uint32_t
, '%d', 'nb_snapshots' ],
42 [ uint64_t
, '%#x', 'snapshot_offset' ],
44 # Version 3 header fields
45 [ uint64_t
, '%#x', 'incompatible_features' ],
46 [ uint64_t
, '%#x', 'compatible_features' ],
47 [ uint64_t
, '%#x', 'autoclear_features' ],
48 [ uint32_t
, '%d', 'refcount_order' ],
49 [ uint32_t
, '%d', 'header_length' ],
52 fmt
= '>' + ''.join(field
[0] for field
in fields
)
54 def __init__(self
, fd
):
56 buf_size
= struct
.calcsize(QcowHeader
.fmt
)
59 buf
= fd
.read(buf_size
)
61 header
= struct
.unpack(QcowHeader
.fmt
, buf
)
62 self
.__dict
__ = dict((field
[2], header
[i
])
63 for i
, field
in enumerate(QcowHeader
.fields
))
66 self
.cluster_size
= 1 << self
.cluster_bits
68 fd
.seek(self
.header_length
)
69 self
.load_extensions(fd
)
71 if self
.backing_file_offset
:
72 fd
.seek(self
.backing_file_offset
)
73 self
.backing_file
= fd
.read(self
.backing_file_size
)
75 self
.backing_file
= None
77 def set_defaults(self
):
79 self
.incompatible_features
= 0
80 self
.compatible_features
= 0
81 self
.autoclear_features
= 0
82 self
.refcount_order
= 4
83 self
.header_length
= 72
85 def load_extensions(self
, fd
):
88 if self
.backing_file_offset
!= 0:
89 end
= min(self
.cluster_size
, self
.backing_file_offset
)
91 end
= self
.cluster_size
93 while fd
.tell() < end
:
94 (magic
, length
) = struct
.unpack('>II', fd
.read(8))
98 padded
= (length
+ 7) & ~
7
99 data
= fd
.read(padded
)
100 self
.extensions
.append(QcowHeaderExtension(magic
, length
, data
))
102 def update_extensions(self
, fd
):
104 fd
.seek(self
.header_length
)
105 extensions
= self
.extensions
106 extensions
.append(QcowHeaderExtension(0, 0, b
""))
107 for ex
in extensions
:
108 buf
= struct
.pack('>II', ex
.magic
, ex
.length
)
112 if self
.backing_file
!= None:
113 self
.backing_file_offset
= fd
.tell()
114 fd
.write(self
.backing_file
)
116 if fd
.tell() > self
.cluster_size
:
117 raise Exception("I think I just broke the image...")
120 def update(self
, fd
):
121 header_bytes
= self
.header_length
123 self
.update_extensions(fd
)
126 header
= tuple(self
.__dict
__[f
] for t
, p
, f
in QcowHeader
.fields
)
127 buf
= struct
.pack(QcowHeader
.fmt
, *header
)
128 buf
= buf
[0:header_bytes
-1]
132 for f
in QcowHeader
.fields
:
133 print("%-25s" % f
[2], f
[1] % self
.__dict
__[f
[2]])
136 def dump_extensions(self
):
137 for ex
in self
.extensions
:
139 data
= ex
.data
[:ex
.length
]
140 if all(c
in string
.printable
.encode('ascii') for c
in data
):
141 data
= "'%s'" % data
.decode('ascii')
145 print("Header extension:")
146 print("%-25s %#x" % ("magic", ex
.magic
))
147 print("%-25s %d" % ("length", ex
.length
))
148 print("%-25s %s" % ("data", data
))
152 def cmd_dump_header(fd
):
157 def cmd_set_header(fd
, name
, value
):
159 value
= int(value
, 0)
161 print("'%s' is not a valid number" % value
)
164 fields
= (field
[2] for field
in QcowHeader
.fields
)
165 if not name
in fields
:
166 print("'%s' is not a known header field" % name
)
170 h
.__dict
__[name
] = value
173 def cmd_add_header_ext(fd
, magic
, data
):
175 magic
= int(magic
, 0)
177 print("'%s' is not a valid magic number" % magic
)
181 h
.extensions
.append(QcowHeaderExtension
.create(magic
, data
.encode('ascii')))
184 def cmd_add_header_ext_stdio(fd
, magic
):
185 data
= sys
.stdin
.read()
186 cmd_add_header_ext(fd
, magic
, data
)
188 def cmd_del_header_ext(fd
, magic
):
190 magic
= int(magic
, 0)
192 print("'%s' is not a valid magic number" % magic
)
198 for ex
in h
.extensions
:
199 if ex
.magic
== magic
:
201 h
.extensions
.remove(ex
)
204 print("No such header extension")
209 def cmd_set_feature_bit(fd
, group
, bit
):
212 if bit
< 0 or bit
>= 64:
215 print("'%s' is not a valid bit number in range [0, 64)" % bit
)
219 if group
== 'incompatible':
220 h
.incompatible_features |
= 1 << bit
221 elif group
== 'compatible':
222 h
.compatible_features |
= 1 << bit
223 elif group
== 'autoclear':
224 h
.autoclear_features |
= 1 << bit
226 print("'%s' is not a valid group, try 'incompatible', 'compatible', or 'autoclear'" % group
)
232 [ 'dump-header', cmd_dump_header
, 0, 'Dump image header and header extensions' ],
233 [ 'set-header', cmd_set_header
, 2, 'Set a field in the header'],
234 [ 'add-header-ext', cmd_add_header_ext
, 2, 'Add a header extension' ],
235 [ 'add-header-ext-stdio', cmd_add_header_ext_stdio
, 1, 'Add a header extension, data from stdin' ],
236 [ 'del-header-ext', cmd_del_header_ext
, 1, 'Delete a header extension' ],
237 [ 'set-feature-bit', cmd_set_feature_bit
, 2, 'Set a feature bit'],
240 def main(filename
, cmd
, args
):
241 fd
= open(filename
, "r+b")
243 for name
, handler
, num_args
, desc
in cmds
:
246 elif len(args
) != num_args
:
252 print("Unknown command '%s'" % cmd
)
257 print("Usage: %s <file> <cmd> [<arg>, ...]" % sys
.argv
[0])
259 print("Supported commands:")
260 for name
, handler
, num_args
, desc
in cmds
:
261 print(" %-20s - %s" % (name
, desc
))
263 if __name__
== '__main__':
264 if len(sys
.argv
) < 3:
268 main(sys
.argv
[1], sys
.argv
[2], sys
.argv
[3:])