2 * qcow2.c --- Functions to generate qcow2 formatted disk images. This
3 * format is used originally by QEMU for virtual machines, and stores the
4 * filesystem data on disk in a packed format to avoid creating sparse
5 * image files that need lots of seeking to read and write.
7 * The qcow2 format supports zlib compression, but that is not yet
10 * It is possible to directly mount a qcow2 image using qemu-nbd:
12 * [root]# modprobe nbd max_part=63
13 * [root]# qemu-nbd -c /dev/nbd0 image.img
14 * [root]# mount /dev/nbd0p1 /mnt/qemu
16 * Format details at http://people.gnome.org/~markmc/qcow-image-format.html
18 * Copyright (C) 2010 Red Hat, Inc., Lukas Czerner <lczerner@redhat.com>
21 * This file may be redistributed under the terms of the GNU Public
26 #define _LARGEFILE_SOURCE
27 #define _LARGEFILE64_SOURCE
43 #include <sys/types.h>
46 #include "ext2fs/ext2fs.h"
49 /* Functions for converting qcow2 image into raw image */
51 struct ext2_qcow2_hdr *qcow2_read_header(int fd)
54 struct ext2_qcow2_hdr *hdr = NULL;
58 ret = ext2fs_get_mem(sizeof(struct ext2_qcow2_hdr), &buffer);
61 memset(buffer, 0, sizeof(struct ext2_qcow2_hdr));
63 if (ext2fs_llseek(fd, 0, SEEK_SET < 0)) {
64 ext2fs_free_mem(&buffer);
68 size = read(fd, buffer, sizeof(struct ext2_qcow2_hdr));
69 if (size != sizeof(struct ext2_qcow2_hdr)) {
70 ext2fs_free_mem(&buffer);
74 hdr = (struct ext2_qcow2_hdr *)(buffer);
76 if ((ext2fs_be32_to_cpu(hdr->magic) != QCOW_MAGIC) ||
77 (ext2fs_be32_to_cpu(hdr->version) != 2)) {
78 ext2fs_free_mem(&hdr);
85 static int qcow2_read_l1_table(struct ext2_qcow2_image *img)
88 size_t size, l1_size = img->l1_size * sizeof(blk64_t);
92 ret = ext2fs_get_memzero(l1_size, &table);
96 if (ext2fs_llseek(fd, img->l1_offset, SEEK_SET) < 0) {
97 ext2fs_free_mem(&table);
101 size = read(fd, table, l1_size);
102 if (size != l1_size) {
103 ext2fs_free_mem(&table);
107 img->l1_table = table;
112 static int qcow2_read_l2_table(struct ext2_qcow2_image *img,
113 ext2_off64_t offset, blk64_t **l2_table)
120 if (ext2fs_llseek(fd, offset, SEEK_SET) < 0)
123 size = read(fd, *l2_table, img->cluster_size);
124 if (size != img->cluster_size)
130 static int qcow2_copy_data(int fdin, int fdout, ext2_off64_t off_in,
131 ext2_off64_t off_out, void *buf, size_t count)
137 if (ext2fs_llseek(fdout, off_out, SEEK_SET) < 0)
140 if (ext2fs_llseek(fdin, off_in, SEEK_SET) < 0)
143 size = read(fdin, buf, count);
147 size = write(fdout, buf, count);
155 int qcow2_write_raw_image(int qcow2_fd, int raw_fd,
156 struct ext2_qcow2_hdr *hdr)
158 struct ext2_qcow2_image img;
160 unsigned int l1_index, l2_index;
162 blk64_t *l1_table, *l2_table = NULL;
163 void *copy_buf = NULL;
166 if (hdr->crypt_method)
167 return -QCOW_ENCRYPTED;
173 img.cluster_bits = ext2fs_be32_to_cpu(hdr->cluster_bits);
174 img.cluster_size = 1 << img.cluster_bits;
175 img.l1_size = ext2fs_be32_to_cpu(hdr->l1_size);
176 img.l1_offset = ext2fs_be64_to_cpu(hdr->l1_table_offset);
177 img.l2_size = 1 << (img.cluster_bits - 3);
178 img.image_size = ext2fs_be64_to_cpu(hdr->size);
181 ret = ext2fs_get_memzero(img.cluster_size, &l2_table);
185 ret = ext2fs_get_memzero(1 << img.cluster_bits, ©_buf);
189 if (ext2fs_llseek(raw_fd, 0, SEEK_SET) < 0) {
194 ret = qcow2_read_l1_table(&img);
198 l1_table = img.l1_table;
199 /* Walk through l1 table */
200 for (l1_index = 0; l1_index < img.l1_size; l1_index++) {
201 ext2_off64_t off_out;
203 offset = ext2fs_be64_to_cpu(l1_table[l1_index]) &
206 if ((offset > img.image_size) ||
210 if (offset & QCOW_OFLAG_COMPRESSED) {
211 ret = -QCOW_COMPRESSED;
215 ret = qcow2_read_l2_table(&img, offset, &l2_table);
219 /* Walk through l2 table and copy data blocks into raw image */
220 for (l2_index = 0; l2_index < img.l2_size; l2_index++) {
221 offset = ext2fs_be64_to_cpu(l2_table[l2_index]) &
227 off_out = (l1_index * img.l2_size) +
229 off_out <<= img.cluster_bits;
230 ret = qcow2_copy_data(qcow2_fd, raw_fd, offset,
231 off_out, copy_buf, img.cluster_size);
237 /* Resize the output image to the filesystem size */
238 if (ext2fs_llseek(raw_fd, img.image_size - 1, SEEK_SET) < 0) {
243 ((char *)copy_buf)[0] = 0;
244 size = write(raw_fd, copy_buf, 1);
252 ext2fs_free_mem(©_buf);
254 ext2fs_free_mem(&img.l1_table);
256 ext2fs_free_mem(&l2_table);