提交 982c8c5e 编写于 作者: K Kevin Wolf 提交者: Michael Roth

qcow2: Zero-initialise first cluster for new images

Strictly speaking, this is only required for has_zero_init() == false,
but it's easy enough to just do a cluster-aligned write that is padded
with zeros after the header.

This fixes that after 'qemu-img create' header extensions are attempted
to be parsed that are really just random leftover data.

Cc: qemu-stable@nongnu.org
Signed-off-by: NKevin Wolf <kwolf@redhat.com>
Reviewed-by: NFam Zheng <famz@redhat.com>
Reviewed-by: NPaolo Bonzini <pbonzini@redhat.com>
Signed-off-by: NStefan Hajnoczi <stefanha@redhat.com>
(cherry picked from commit f8413b3c)
Signed-off-by: NMichael Roth <mdroth@linux.vnet.ibm.com>
上级 d90ff19d
...@@ -1285,7 +1285,7 @@ static int qcow2_create2(const char *filename, int64_t total_size, ...@@ -1285,7 +1285,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,
* size for any qcow2 image. * size for any qcow2 image.
*/ */
BlockDriverState* bs; BlockDriverState* bs;
QCowHeader header; QCowHeader *header;
uint8_t* refcount_table; uint8_t* refcount_table;
int ret; int ret;
...@@ -1300,30 +1300,34 @@ static int qcow2_create2(const char *filename, int64_t total_size, ...@@ -1300,30 +1300,34 @@ static int qcow2_create2(const char *filename, int64_t total_size,
} }
/* Write the header */ /* Write the header */
memset(&header, 0, sizeof(header)); QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS) < sizeof(*header));
header.magic = cpu_to_be32(QCOW_MAGIC); header = g_malloc0(cluster_size);
header.version = cpu_to_be32(version); *header = (QCowHeader) {
header.cluster_bits = cpu_to_be32(cluster_bits); .magic = cpu_to_be32(QCOW_MAGIC),
header.size = cpu_to_be64(0); .version = cpu_to_be32(version),
header.l1_table_offset = cpu_to_be64(0); .cluster_bits = cpu_to_be32(cluster_bits),
header.l1_size = cpu_to_be32(0); .size = cpu_to_be64(0),
header.refcount_table_offset = cpu_to_be64(cluster_size); .l1_table_offset = cpu_to_be64(0),
header.refcount_table_clusters = cpu_to_be32(1); .l1_size = cpu_to_be32(0),
header.refcount_order = cpu_to_be32(3 + REFCOUNT_SHIFT); .refcount_table_offset = cpu_to_be64(cluster_size),
header.header_length = cpu_to_be32(sizeof(header)); .refcount_table_clusters = cpu_to_be32(1),
.refcount_order = cpu_to_be32(3 + REFCOUNT_SHIFT),
.header_length = cpu_to_be32(sizeof(*header)),
};
if (flags & BLOCK_FLAG_ENCRYPT) { if (flags & BLOCK_FLAG_ENCRYPT) {
header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES); header->crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
} else { } else {
header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE); header->crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
} }
if (flags & BLOCK_FLAG_LAZY_REFCOUNTS) { if (flags & BLOCK_FLAG_LAZY_REFCOUNTS) {
header.compatible_features |= header->compatible_features |=
cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS); cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS);
} }
ret = bdrv_pwrite(bs, 0, &header, sizeof(header)); ret = bdrv_pwrite(bs, 0, header, cluster_size);
g_free(header);
if (ret < 0) { if (ret < 0) {
goto out; goto out;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册