7 class QcowHeaderExtension
:
9 def __init__(self
, magic
, length
, data
):
15 def create(cls
, magic
, data
):
16 return QcowHeaderExtension(magic
, len(data
), data
)
24 # Version 2 header fields
25 [ uint32_t
, '%#x', 'magic' ],
26 [ uint32_t
, '%d', 'version' ],
27 [ uint64_t
, '%#x', 'backing_file_offset' ],
28 [ uint32_t
, '%#x', 'backing_file_size' ],
29 [ uint32_t
, '%d', 'cluster_bits' ],
30 [ uint64_t
, '%d', 'size' ],
31 [ uint32_t
, '%d', 'crypt_method' ],
32 [ uint32_t
, '%d', 'l1_size' ],
33 [ uint64_t
, '%#x', 'l1_table_offset' ],
34 [ uint64_t
, '%#x', 'refcount_table_offset' ],
35 [ uint32_t
, '%d', 'refcount_table_clusters' ],
36 [ uint32_t
, '%d', 'nb_snapshots' ],
37 [ uint64_t
, '%#x', 'snapshot_offset' ],
39 # Version 3 header fields
40 [ uint64_t
, '%#x', 'incompatible_features' ],
41 [ uint64_t
, '%#x', 'compatible_features' ],
42 [ uint64_t
, '%#x', 'autoclear_features' ],
43 [ uint32_t
, '%d', 'refcount_order' ],
44 [ uint32_t
, '%d', 'header_length' ],
47 fmt
= '>' + ''.join(field
[0] for field
in fields
)
49 def __init__(self
, fd
):
51 buf_size
= struct
.calcsize(QcowHeader
.fmt
)
54 buf
= fd
.read(buf_size
)
56 header
= struct
.unpack(QcowHeader
.fmt
, buf
)
57 self
.__dict
__ = dict((field
[2], header
[i
])
58 for i
, field
in enumerate(QcowHeader
.fields
))
61 self
.cluster_size
= 1 << self
.cluster_bits
63 fd
.seek(self
.header_length
)
64 self
.load_extensions(fd
)
66 if self
.backing_file_offset
:
67 fd
.seek(self
.backing_file_offset
)
68 self
.backing_file
= fd
.read(self
.backing_file_size
)
70 self
.backing_file
= None
72 def set_defaults(self
):
74 self
.incompatible_features
= 0
75 self
.compatible_features
= 0
76 self
.autoclear_features
= 0
77 self
.refcount_order
= 4
78 self
.header_length
= 72
80 def load_extensions(self
, fd
):
83 if self
.backing_file_offset
!= 0:
84 end
= min(self
.cluster_size
, self
.backing_file_offset
)
86 end
= self
.cluster_size
88 while fd
.tell() < end
:
89 (magic
, length
) = struct
.unpack('>II', fd
.read(8))
93 padded
= (length
+ 7) & ~
7
94 data
= fd
.read(padded
)
95 self
.extensions
.append(QcowHeaderExtension(magic
, length
, data
))
97 def update_extensions(self
, fd
):
99 fd
.seek(self
.header_length
)
100 extensions
= self
.extensions
101 extensions
.append(QcowHeaderExtension(0, 0, ""))
102 for ex
in extensions
:
103 buf
= struct
.pack('>II', ex
.magic
, ex
.length
)
107 if self
.backing_file
!= None:
108 self
.backing_file_offset
= fd
.tell()
109 fd
.write(self
.backing_file
)
111 if fd
.tell() > self
.cluster_size
:
112 raise Exception("I think I just broke the image...")
115 def update(self
, fd
):
116 header_bytes
= self
.header_length
118 self
.update_extensions(fd
)
121 header
= tuple(self
.__dict
__[f
] for t
, p
, f
in QcowHeader
.fields
)
122 buf
= struct
.pack(QcowHeader
.fmt
, *header
)
123 buf
= buf
[0:header_bytes
-1]
127 for f
in QcowHeader
.fields
:
128 print "%-25s" % f
[2], f
[1] % self
.__dict
__[f
[2]]
131 def dump_extensions(self
):
132 for ex
in self
.extensions
:
134 data
= ex
.data
[:ex
.length
]
135 if all(c
in string
.printable
for c
in data
):
140 print "Header extension:"
141 print "%-25s %#x" % ("magic", ex
.magic
)
142 print "%-25s %d" % ("length", ex
.length
)
143 print "%-25s %s" % ("data", data
)
147 def cmd_dump_header(fd
):
152 def cmd_set_header(fd
, name
, value
):
154 value
= int(value
, 0)
156 print "'%s' is not a valid number" % value
159 fields
= (field
[2] for field
in QcowHeader
.fields
)
160 if not name
in fields
:
161 print "'%s' is not a known header field" % name
165 h
.__dict
__[name
] = value
168 def cmd_add_header_ext(fd
, magic
, data
):
170 magic
= int(magic
, 0)
172 print "'%s' is not a valid magic number" % magic
176 h
.extensions
.append(QcowHeaderExtension
.create(magic
, data
))
179 def cmd_del_header_ext(fd
, magic
):
181 magic
= int(magic
, 0)
183 print "'%s' is not a valid magic number" % magic
189 for ex
in h
.extensions
:
190 if ex
.magic
== magic
:
192 h
.extensions
.remove(ex
)
195 print "No such header extension"
200 def cmd_set_feature_bit(fd
, group
, bit
):
203 if bit
< 0 or bit
>= 64:
206 print "'%s' is not a valid bit number in range [0, 64)" % bit
210 if group
== 'incompatible':
211 h
.incompatible_features |
= 1 << bit
212 elif group
== 'compatible':
213 h
.compatible_features |
= 1 << bit
214 elif group
== 'autoclear':
215 h
.autoclear_features |
= 1 << bit
217 print "'%s' is not a valid group, try 'incompatible', 'compatible', or 'autoclear'" % group
223 [ 'dump-header', cmd_dump_header
, 0, 'Dump image header and header extensions' ],
224 [ 'set-header', cmd_set_header
, 2, 'Set a field in the header'],
225 [ 'add-header-ext', cmd_add_header_ext
, 2, 'Add a header extension' ],
226 [ 'del-header-ext', cmd_del_header_ext
, 1, 'Delete a header extension' ],
227 [ 'set-feature-bit', cmd_set_feature_bit
, 2, 'Set a feature bit'],
230 def main(filename
, cmd
, args
):
231 fd
= open(filename
, "r+b")
233 for name
, handler
, num_args
, desc
in cmds
:
236 elif len(args
) != num_args
:
242 print "Unknown command '%s'" % cmd
247 print "Usage: %s <file> <cmd> [<arg>, ...]" % sys
.argv
[0]
249 print "Supported commands:"
250 for name
, handler
, num_args
, desc
in cmds
:
251 print " %-20s - %s" % (name
, desc
)
253 if __name__
== '__main__':
254 if len(sys
.argv
) < 3:
258 main(sys
.argv
[1], sys
.argv
[2], sys
.argv
[3:])