7 class QcowHeaderExtension
:
9 def __init__(self
, magic
, length
, data
):
11 padding
= 8 - (length
% 8)
12 data
+= "\0" * padding
19 def create(cls
, magic
, data
):
20 return QcowHeaderExtension(magic
, len(data
), data
)
28 # Version 2 header fields
29 [ uint32_t
, '%#x', 'magic' ],
30 [ uint32_t
, '%d', 'version' ],
31 [ uint64_t
, '%#x', 'backing_file_offset' ],
32 [ uint32_t
, '%#x', 'backing_file_size' ],
33 [ uint32_t
, '%d', 'cluster_bits' ],
34 [ uint64_t
, '%d', 'size' ],
35 [ uint32_t
, '%d', 'crypt_method' ],
36 [ uint32_t
, '%d', 'l1_size' ],
37 [ uint64_t
, '%#x', 'l1_table_offset' ],
38 [ uint64_t
, '%#x', 'refcount_table_offset' ],
39 [ uint32_t
, '%d', 'refcount_table_clusters' ],
40 [ uint32_t
, '%d', 'nb_snapshots' ],
41 [ uint64_t
, '%#x', 'snapshot_offset' ],
43 # Version 3 header fields
44 [ uint64_t
, '%#x', 'incompatible_features' ],
45 [ uint64_t
, '%#x', 'compatible_features' ],
46 [ uint64_t
, '%#x', 'autoclear_features' ],
47 [ uint32_t
, '%d', 'refcount_order' ],
48 [ uint32_t
, '%d', 'header_length' ],
51 fmt
= '>' + ''.join(field
[0] for field
in fields
)
53 def __init__(self
, fd
):
55 buf_size
= struct
.calcsize(QcowHeader
.fmt
)
58 buf
= fd
.read(buf_size
)
60 header
= struct
.unpack(QcowHeader
.fmt
, buf
)
61 self
.__dict
__ = dict((field
[2], header
[i
])
62 for i
, field
in enumerate(QcowHeader
.fields
))
65 self
.cluster_size
= 1 << self
.cluster_bits
67 fd
.seek(self
.header_length
)
68 self
.load_extensions(fd
)
70 if self
.backing_file_offset
:
71 fd
.seek(self
.backing_file_offset
)
72 self
.backing_file
= fd
.read(self
.backing_file_size
)
74 self
.backing_file
= None
76 def set_defaults(self
):
78 self
.incompatible_features
= 0
79 self
.compatible_features
= 0
80 self
.autoclear_features
= 0
81 self
.refcount_order
= 4
82 self
.header_length
= 72
84 def load_extensions(self
, fd
):
87 if self
.backing_file_offset
!= 0:
88 end
= min(self
.cluster_size
, self
.backing_file_offset
)
90 end
= self
.cluster_size
92 while fd
.tell() < end
:
93 (magic
, length
) = struct
.unpack('>II', fd
.read(8))
97 padded
= (length
+ 7) & ~
7
98 data
= fd
.read(padded
)
99 self
.extensions
.append(QcowHeaderExtension(magic
, length
, data
))
101 def update_extensions(self
, fd
):
103 fd
.seek(self
.header_length
)
104 extensions
= self
.extensions
105 extensions
.append(QcowHeaderExtension(0, 0, ""))
106 for ex
in extensions
:
107 buf
= struct
.pack('>II', ex
.magic
, ex
.length
)
111 if self
.backing_file
!= None:
112 self
.backing_file_offset
= fd
.tell()
113 fd
.write(self
.backing_file
)
115 if fd
.tell() > self
.cluster_size
:
116 raise Exception("I think I just broke the image...")
119 def update(self
, fd
):
120 header_bytes
= self
.header_length
122 self
.update_extensions(fd
)
125 header
= tuple(self
.__dict
__[f
] for t
, p
, f
in QcowHeader
.fields
)
126 buf
= struct
.pack(QcowHeader
.fmt
, *header
)
127 buf
= buf
[0:header_bytes
-1]
131 for f
in QcowHeader
.fields
:
132 print "%-25s" % f
[2], f
[1] % self
.__dict
__[f
[2]]
135 def dump_extensions(self
):
136 for ex
in self
.extensions
:
138 data
= ex
.data
[:ex
.length
]
139 if all(c
in string
.printable
for c
in data
):
144 print "Header extension:"
145 print "%-25s %#x" % ("magic", ex
.magic
)
146 print "%-25s %d" % ("length", ex
.length
)
147 print "%-25s %s" % ("data", data
)
151 def cmd_dump_header(fd
):
156 def cmd_set_header(fd
, name
, value
):
158 value
= int(value
, 0)
160 print "'%s' is not a valid number" % value
163 fields
= (field
[2] for field
in QcowHeader
.fields
)
164 if not name
in fields
:
165 print "'%s' is not a known header field" % name
169 h
.__dict
__[name
] = value
172 def cmd_add_header_ext(fd
, magic
, data
):
174 magic
= int(magic
, 0)
176 print "'%s' is not a valid magic number" % magic
180 h
.extensions
.append(QcowHeaderExtension
.create(magic
, data
))
183 def cmd_add_header_ext_stdio(fd
, magic
):
184 data
= sys
.stdin
.read()
185 cmd_add_header_ext(fd
, magic
, data
)
187 def cmd_del_header_ext(fd
, magic
):
189 magic
= int(magic
, 0)
191 print "'%s' is not a valid magic number" % magic
197 for ex
in h
.extensions
:
198 if ex
.magic
== magic
:
200 h
.extensions
.remove(ex
)
203 print "No such header extension"
208 def cmd_set_feature_bit(fd
, group
, bit
):
211 if bit
< 0 or bit
>= 64:
214 print "'%s' is not a valid bit number in range [0, 64)" % bit
218 if group
== 'incompatible':
219 h
.incompatible_features |
= 1 << bit
220 elif group
== 'compatible':
221 h
.compatible_features |
= 1 << bit
222 elif group
== 'autoclear':
223 h
.autoclear_features |
= 1 << bit
225 print "'%s' is not a valid group, try 'incompatible', 'compatible', or 'autoclear'" % group
231 [ 'dump-header', cmd_dump_header
, 0, 'Dump image header and header extensions' ],
232 [ 'set-header', cmd_set_header
, 2, 'Set a field in the header'],
233 [ 'add-header-ext', cmd_add_header_ext
, 2, 'Add a header extension' ],
234 [ 'add-header-ext-stdio', cmd_add_header_ext_stdio
, 1, 'Add a header extension, data from stdin' ],
235 [ 'del-header-ext', cmd_del_header_ext
, 1, 'Delete a header extension' ],
236 [ 'set-feature-bit', cmd_set_feature_bit
, 2, 'Set a feature bit'],
239 def main(filename
, cmd
, args
):
240 fd
= open(filename
, "r+b")
242 for name
, handler
, num_args
, desc
in cmds
:
245 elif len(args
) != num_args
:
251 print "Unknown command '%s'" % cmd
256 print "Usage: %s <file> <cmd> [<arg>, ...]" % sys
.argv
[0]
258 print "Supported commands:"
259 for name
, handler
, num_args
, desc
in cmds
:
260 print " %-20s - %s" % (name
, desc
)
262 if __name__
== '__main__':
263 if len(sys
.argv
) < 3:
267 main(sys
.argv
[1], sys
.argv
[2], sys
.argv
[3:])