1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker * qcow2.c --- Functions to generate qcow2 formatted disk images. This
3*6a54128fSAndroid Build Coastguard Worker * format is used originally by QEMU for virtual machines, and stores the
4*6a54128fSAndroid Build Coastguard Worker * filesystem data on disk in a packed format to avoid creating sparse
5*6a54128fSAndroid Build Coastguard Worker * image files that need lots of seeking to read and write.
6*6a54128fSAndroid Build Coastguard Worker *
7*6a54128fSAndroid Build Coastguard Worker * The qcow2 format supports zlib compression, but that is not yet
8*6a54128fSAndroid Build Coastguard Worker * implemented.
9*6a54128fSAndroid Build Coastguard Worker *
10*6a54128fSAndroid Build Coastguard Worker * It is possible to directly mount a qcow2 image using qemu-nbd:
11*6a54128fSAndroid Build Coastguard Worker *
12*6a54128fSAndroid Build Coastguard Worker * [root]# modprobe nbd max_part=63
13*6a54128fSAndroid Build Coastguard Worker * [root]# qemu-nbd -c /dev/nbd0 image.img
14*6a54128fSAndroid Build Coastguard Worker * [root]# mount /dev/nbd0p1 /mnt/qemu
15*6a54128fSAndroid Build Coastguard Worker *
16*6a54128fSAndroid Build Coastguard Worker * Format details at http://people.gnome.org/~markmc/qcow-image-format.html
17*6a54128fSAndroid Build Coastguard Worker *
18*6a54128fSAndroid Build Coastguard Worker * Copyright (C) 2010 Red Hat, Inc., Lukas Czerner <[email protected]>
19*6a54128fSAndroid Build Coastguard Worker *
20*6a54128fSAndroid Build Coastguard Worker * %Begin-Header%
21*6a54128fSAndroid Build Coastguard Worker * This file may be redistributed under the terms of the GNU Public
22*6a54128fSAndroid Build Coastguard Worker * License.
23*6a54128fSAndroid Build Coastguard Worker * %End-Header%
24*6a54128fSAndroid Build Coastguard Worker */
25*6a54128fSAndroid Build Coastguard Worker
26*6a54128fSAndroid Build Coastguard Worker #ifndef _LARGEFILE_SOURCE
27*6a54128fSAndroid Build Coastguard Worker #define _LARGEFILE_SOURCE
28*6a54128fSAndroid Build Coastguard Worker #endif
29*6a54128fSAndroid Build Coastguard Worker #ifndef _LARGEFILE64_SOURCE
30*6a54128fSAndroid Build Coastguard Worker #define _LARGEFILE64_SOURCE
31*6a54128fSAndroid Build Coastguard Worker #endif
32*6a54128fSAndroid Build Coastguard Worker
33*6a54128fSAndroid Build Coastguard Worker #include "config.h"
34*6a54128fSAndroid Build Coastguard Worker #include <fcntl.h>
35*6a54128fSAndroid Build Coastguard Worker #include <grp.h>
36*6a54128fSAndroid Build Coastguard Worker #include <pwd.h>
37*6a54128fSAndroid Build Coastguard Worker #include <stdio.h>
38*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_STDLIB_H
39*6a54128fSAndroid Build Coastguard Worker #include <stdlib.h>
40*6a54128fSAndroid Build Coastguard Worker #endif
41*6a54128fSAndroid Build Coastguard Worker #include <string.h>
42*6a54128fSAndroid Build Coastguard Worker #include <time.h>
43*6a54128fSAndroid Build Coastguard Worker #include <unistd.h>
44*6a54128fSAndroid Build Coastguard Worker #include <fcntl.h>
45*6a54128fSAndroid Build Coastguard Worker #include <errno.h>
46*6a54128fSAndroid Build Coastguard Worker #include <sys/stat.h>
47*6a54128fSAndroid Build Coastguard Worker #include <sys/types.h>
48*6a54128fSAndroid Build Coastguard Worker #include <assert.h>
49*6a54128fSAndroid Build Coastguard Worker
50*6a54128fSAndroid Build Coastguard Worker #include "ext2fs/ext2fs.h"
51*6a54128fSAndroid Build Coastguard Worker #include "qcow2.h"
52*6a54128fSAndroid Build Coastguard Worker
53*6a54128fSAndroid Build Coastguard Worker /* Functions for converting qcow2 image into raw image */
54*6a54128fSAndroid Build Coastguard Worker
qcow2_read_header(int fd)55*6a54128fSAndroid Build Coastguard Worker struct ext2_qcow2_hdr *qcow2_read_header(int fd)
56*6a54128fSAndroid Build Coastguard Worker {
57*6a54128fSAndroid Build Coastguard Worker void *buffer = NULL;
58*6a54128fSAndroid Build Coastguard Worker struct ext2_qcow2_hdr *hdr = NULL;
59*6a54128fSAndroid Build Coastguard Worker size_t size;
60*6a54128fSAndroid Build Coastguard Worker errcode_t ret;
61*6a54128fSAndroid Build Coastguard Worker
62*6a54128fSAndroid Build Coastguard Worker ret = ext2fs_get_mem(sizeof(struct ext2_qcow2_hdr), &buffer);
63*6a54128fSAndroid Build Coastguard Worker if (ret)
64*6a54128fSAndroid Build Coastguard Worker return NULL;
65*6a54128fSAndroid Build Coastguard Worker memset(buffer, 0, sizeof(struct ext2_qcow2_hdr));
66*6a54128fSAndroid Build Coastguard Worker
67*6a54128fSAndroid Build Coastguard Worker if (ext2fs_llseek(fd, 0, SEEK_SET < 0)) {
68*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&buffer);
69*6a54128fSAndroid Build Coastguard Worker return NULL;
70*6a54128fSAndroid Build Coastguard Worker }
71*6a54128fSAndroid Build Coastguard Worker
72*6a54128fSAndroid Build Coastguard Worker size = read(fd, buffer, sizeof(struct ext2_qcow2_hdr));
73*6a54128fSAndroid Build Coastguard Worker if (size != sizeof(struct ext2_qcow2_hdr)) {
74*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&buffer);
75*6a54128fSAndroid Build Coastguard Worker return NULL;
76*6a54128fSAndroid Build Coastguard Worker }
77*6a54128fSAndroid Build Coastguard Worker
78*6a54128fSAndroid Build Coastguard Worker hdr = (struct ext2_qcow2_hdr *)(buffer);
79*6a54128fSAndroid Build Coastguard Worker
80*6a54128fSAndroid Build Coastguard Worker if ((ext2fs_be32_to_cpu(hdr->magic) != QCOW_MAGIC) ||
81*6a54128fSAndroid Build Coastguard Worker (ext2fs_be32_to_cpu(hdr->version) != 2)) {
82*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&hdr);
83*6a54128fSAndroid Build Coastguard Worker return NULL;
84*6a54128fSAndroid Build Coastguard Worker }
85*6a54128fSAndroid Build Coastguard Worker
86*6a54128fSAndroid Build Coastguard Worker return hdr;
87*6a54128fSAndroid Build Coastguard Worker }
88*6a54128fSAndroid Build Coastguard Worker
qcow2_read_l1_table(struct ext2_qcow2_image * img)89*6a54128fSAndroid Build Coastguard Worker static int qcow2_read_l1_table(struct ext2_qcow2_image *img)
90*6a54128fSAndroid Build Coastguard Worker {
91*6a54128fSAndroid Build Coastguard Worker int fd = img->fd;
92*6a54128fSAndroid Build Coastguard Worker size_t size, l1_size = img->l1_size * sizeof(blk64_t);
93*6a54128fSAndroid Build Coastguard Worker blk64_t *table;
94*6a54128fSAndroid Build Coastguard Worker errcode_t ret;
95*6a54128fSAndroid Build Coastguard Worker
96*6a54128fSAndroid Build Coastguard Worker ret = ext2fs_get_memzero(l1_size, &table);
97*6a54128fSAndroid Build Coastguard Worker if (ret)
98*6a54128fSAndroid Build Coastguard Worker return ret;
99*6a54128fSAndroid Build Coastguard Worker
100*6a54128fSAndroid Build Coastguard Worker if (ext2fs_llseek(fd, img->l1_offset, SEEK_SET) < 0) {
101*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&table);
102*6a54128fSAndroid Build Coastguard Worker return errno;
103*6a54128fSAndroid Build Coastguard Worker }
104*6a54128fSAndroid Build Coastguard Worker
105*6a54128fSAndroid Build Coastguard Worker size = read(fd, table, l1_size);
106*6a54128fSAndroid Build Coastguard Worker if (size != l1_size) {
107*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&table);
108*6a54128fSAndroid Build Coastguard Worker return errno;
109*6a54128fSAndroid Build Coastguard Worker }
110*6a54128fSAndroid Build Coastguard Worker
111*6a54128fSAndroid Build Coastguard Worker img->l1_table = table;
112*6a54128fSAndroid Build Coastguard Worker
113*6a54128fSAndroid Build Coastguard Worker return 0;
114*6a54128fSAndroid Build Coastguard Worker }
115*6a54128fSAndroid Build Coastguard Worker
qcow2_read_l2_table(struct ext2_qcow2_image * img,__u64 offset,blk64_t ** l2_table)116*6a54128fSAndroid Build Coastguard Worker static int qcow2_read_l2_table(struct ext2_qcow2_image *img,
117*6a54128fSAndroid Build Coastguard Worker __u64 offset, blk64_t **l2_table)
118*6a54128fSAndroid Build Coastguard Worker {
119*6a54128fSAndroid Build Coastguard Worker int fd = img->fd;
120*6a54128fSAndroid Build Coastguard Worker size_t size;
121*6a54128fSAndroid Build Coastguard Worker
122*6a54128fSAndroid Build Coastguard Worker assert(*l2_table);
123*6a54128fSAndroid Build Coastguard Worker
124*6a54128fSAndroid Build Coastguard Worker if (ext2fs_llseek(fd, offset, SEEK_SET) < 0)
125*6a54128fSAndroid Build Coastguard Worker return errno;
126*6a54128fSAndroid Build Coastguard Worker
127*6a54128fSAndroid Build Coastguard Worker size = read(fd, *l2_table, img->cluster_size);
128*6a54128fSAndroid Build Coastguard Worker if (size != img->cluster_size)
129*6a54128fSAndroid Build Coastguard Worker return errno;
130*6a54128fSAndroid Build Coastguard Worker
131*6a54128fSAndroid Build Coastguard Worker return 0;
132*6a54128fSAndroid Build Coastguard Worker }
133*6a54128fSAndroid Build Coastguard Worker
qcow2_copy_data(int fdin,int fdout,__u64 off_in,__u64 off_out,void * buf,size_t count)134*6a54128fSAndroid Build Coastguard Worker static int qcow2_copy_data(int fdin, int fdout, __u64 off_in,
135*6a54128fSAndroid Build Coastguard Worker __u64 off_out, void *buf, size_t count)
136*6a54128fSAndroid Build Coastguard Worker {
137*6a54128fSAndroid Build Coastguard Worker size_t size;
138*6a54128fSAndroid Build Coastguard Worker
139*6a54128fSAndroid Build Coastguard Worker assert(buf);
140*6a54128fSAndroid Build Coastguard Worker
141*6a54128fSAndroid Build Coastguard Worker if (ext2fs_llseek(fdout, off_out, SEEK_SET) < 0)
142*6a54128fSAndroid Build Coastguard Worker return errno;
143*6a54128fSAndroid Build Coastguard Worker
144*6a54128fSAndroid Build Coastguard Worker if (ext2fs_llseek(fdin, off_in, SEEK_SET) < 0)
145*6a54128fSAndroid Build Coastguard Worker return errno;
146*6a54128fSAndroid Build Coastguard Worker
147*6a54128fSAndroid Build Coastguard Worker size = read(fdin, buf, count);
148*6a54128fSAndroid Build Coastguard Worker if (size != count)
149*6a54128fSAndroid Build Coastguard Worker return errno;
150*6a54128fSAndroid Build Coastguard Worker
151*6a54128fSAndroid Build Coastguard Worker size = write(fdout, buf, count);
152*6a54128fSAndroid Build Coastguard Worker if (size != count)
153*6a54128fSAndroid Build Coastguard Worker return errno;
154*6a54128fSAndroid Build Coastguard Worker
155*6a54128fSAndroid Build Coastguard Worker return 0;
156*6a54128fSAndroid Build Coastguard Worker }
157*6a54128fSAndroid Build Coastguard Worker
158*6a54128fSAndroid Build Coastguard Worker
qcow2_write_raw_image(int qcow2_fd,int raw_fd,struct ext2_qcow2_hdr * hdr)159*6a54128fSAndroid Build Coastguard Worker int qcow2_write_raw_image(int qcow2_fd, int raw_fd,
160*6a54128fSAndroid Build Coastguard Worker struct ext2_qcow2_hdr *hdr)
161*6a54128fSAndroid Build Coastguard Worker {
162*6a54128fSAndroid Build Coastguard Worker struct ext2_qcow2_image img;
163*6a54128fSAndroid Build Coastguard Worker errcode_t ret = 0;
164*6a54128fSAndroid Build Coastguard Worker unsigned int l1_index, l2_index;
165*6a54128fSAndroid Build Coastguard Worker __u64 offset;
166*6a54128fSAndroid Build Coastguard Worker blk64_t *l1_table, *l2_table = NULL;
167*6a54128fSAndroid Build Coastguard Worker void *copy_buf = NULL;
168*6a54128fSAndroid Build Coastguard Worker size_t size;
169*6a54128fSAndroid Build Coastguard Worker unsigned int max_l1_size;
170*6a54128fSAndroid Build Coastguard Worker
171*6a54128fSAndroid Build Coastguard Worker if (hdr->crypt_method)
172*6a54128fSAndroid Build Coastguard Worker return -QCOW_ENCRYPTED;
173*6a54128fSAndroid Build Coastguard Worker
174*6a54128fSAndroid Build Coastguard Worker img.fd = qcow2_fd;
175*6a54128fSAndroid Build Coastguard Worker img.hdr = hdr;
176*6a54128fSAndroid Build Coastguard Worker img.l2_cache = NULL;
177*6a54128fSAndroid Build Coastguard Worker img.l1_table = NULL;
178*6a54128fSAndroid Build Coastguard Worker img.cluster_bits = ext2fs_be32_to_cpu(hdr->cluster_bits);
179*6a54128fSAndroid Build Coastguard Worker if (img.cluster_bits < 9 || img.cluster_bits > 31)
180*6a54128fSAndroid Build Coastguard Worker return -QCOW_CORRUPTED;
181*6a54128fSAndroid Build Coastguard Worker img.cluster_size = 1 << img.cluster_bits;
182*6a54128fSAndroid Build Coastguard Worker img.l1_size = ext2fs_be32_to_cpu(hdr->l1_size);
183*6a54128fSAndroid Build Coastguard Worker img.l1_offset = ext2fs_be64_to_cpu(hdr->l1_table_offset);
184*6a54128fSAndroid Build Coastguard Worker img.l2_size = 1 << (img.cluster_bits - 3);
185*6a54128fSAndroid Build Coastguard Worker img.image_size = ext2fs_be64_to_cpu(hdr->size);
186*6a54128fSAndroid Build Coastguard Worker
187*6a54128fSAndroid Build Coastguard Worker if (img.l1_offset & (img.cluster_size - 1))
188*6a54128fSAndroid Build Coastguard Worker return -QCOW_CORRUPTED;
189*6a54128fSAndroid Build Coastguard Worker
190*6a54128fSAndroid Build Coastguard Worker max_l1_size = (img.image_size >> ((2 * img.cluster_bits) - 3)) +
191*6a54128fSAndroid Build Coastguard Worker img.cluster_size;
192*6a54128fSAndroid Build Coastguard Worker if (img.l1_size > max_l1_size)
193*6a54128fSAndroid Build Coastguard Worker return -QCOW_CORRUPTED;
194*6a54128fSAndroid Build Coastguard Worker
195*6a54128fSAndroid Build Coastguard Worker ret = ext2fs_get_memzero(img.cluster_size, &l2_table);
196*6a54128fSAndroid Build Coastguard Worker if (ret)
197*6a54128fSAndroid Build Coastguard Worker goto out;
198*6a54128fSAndroid Build Coastguard Worker
199*6a54128fSAndroid Build Coastguard Worker ret = ext2fs_get_memzero(1 << img.cluster_bits, ©_buf);
200*6a54128fSAndroid Build Coastguard Worker if (ret)
201*6a54128fSAndroid Build Coastguard Worker goto out;
202*6a54128fSAndroid Build Coastguard Worker
203*6a54128fSAndroid Build Coastguard Worker if (ext2fs_llseek(raw_fd, 0, SEEK_SET) < 0) {
204*6a54128fSAndroid Build Coastguard Worker ret = errno;
205*6a54128fSAndroid Build Coastguard Worker goto out;
206*6a54128fSAndroid Build Coastguard Worker }
207*6a54128fSAndroid Build Coastguard Worker
208*6a54128fSAndroid Build Coastguard Worker ret = qcow2_read_l1_table(&img);
209*6a54128fSAndroid Build Coastguard Worker if (ret)
210*6a54128fSAndroid Build Coastguard Worker goto out;
211*6a54128fSAndroid Build Coastguard Worker
212*6a54128fSAndroid Build Coastguard Worker l1_table = img.l1_table;
213*6a54128fSAndroid Build Coastguard Worker /* Walk through l1 table */
214*6a54128fSAndroid Build Coastguard Worker for (l1_index = 0; l1_index < img.l1_size; l1_index++) {
215*6a54128fSAndroid Build Coastguard Worker __u64 off_out;
216*6a54128fSAndroid Build Coastguard Worker
217*6a54128fSAndroid Build Coastguard Worker offset = ext2fs_be64_to_cpu(l1_table[l1_index]) &
218*6a54128fSAndroid Build Coastguard Worker ~QCOW_OFLAG_COPIED;
219*6a54128fSAndroid Build Coastguard Worker
220*6a54128fSAndroid Build Coastguard Worker if ((offset > img.image_size) ||
221*6a54128fSAndroid Build Coastguard Worker (offset <= 0))
222*6a54128fSAndroid Build Coastguard Worker continue;
223*6a54128fSAndroid Build Coastguard Worker
224*6a54128fSAndroid Build Coastguard Worker if (offset & QCOW_OFLAG_COMPRESSED) {
225*6a54128fSAndroid Build Coastguard Worker ret = -QCOW_COMPRESSED;
226*6a54128fSAndroid Build Coastguard Worker goto out;
227*6a54128fSAndroid Build Coastguard Worker }
228*6a54128fSAndroid Build Coastguard Worker
229*6a54128fSAndroid Build Coastguard Worker ret = qcow2_read_l2_table(&img, offset, &l2_table);
230*6a54128fSAndroid Build Coastguard Worker if (ret)
231*6a54128fSAndroid Build Coastguard Worker break;
232*6a54128fSAndroid Build Coastguard Worker
233*6a54128fSAndroid Build Coastguard Worker /* Walk through l2 table and copy data blocks into raw image */
234*6a54128fSAndroid Build Coastguard Worker for (l2_index = 0; l2_index < img.l2_size; l2_index++) {
235*6a54128fSAndroid Build Coastguard Worker offset = ext2fs_be64_to_cpu(l2_table[l2_index]) &
236*6a54128fSAndroid Build Coastguard Worker ~QCOW_OFLAG_COPIED;
237*6a54128fSAndroid Build Coastguard Worker
238*6a54128fSAndroid Build Coastguard Worker if (offset == 0)
239*6a54128fSAndroid Build Coastguard Worker continue;
240*6a54128fSAndroid Build Coastguard Worker
241*6a54128fSAndroid Build Coastguard Worker off_out = ((__u64)l1_index * img.l2_size) +
242*6a54128fSAndroid Build Coastguard Worker l2_index;
243*6a54128fSAndroid Build Coastguard Worker off_out <<= img.cluster_bits;
244*6a54128fSAndroid Build Coastguard Worker ret = qcow2_copy_data(qcow2_fd, raw_fd, offset,
245*6a54128fSAndroid Build Coastguard Worker off_out, copy_buf, img.cluster_size);
246*6a54128fSAndroid Build Coastguard Worker if (ret)
247*6a54128fSAndroid Build Coastguard Worker goto out;
248*6a54128fSAndroid Build Coastguard Worker }
249*6a54128fSAndroid Build Coastguard Worker }
250*6a54128fSAndroid Build Coastguard Worker
251*6a54128fSAndroid Build Coastguard Worker /* Resize the output image to the filesystem size */
252*6a54128fSAndroid Build Coastguard Worker if (ext2fs_llseek(raw_fd, img.image_size - 1, SEEK_SET) < 0) {
253*6a54128fSAndroid Build Coastguard Worker ret = errno;
254*6a54128fSAndroid Build Coastguard Worker goto out;
255*6a54128fSAndroid Build Coastguard Worker }
256*6a54128fSAndroid Build Coastguard Worker
257*6a54128fSAndroid Build Coastguard Worker ((char *)copy_buf)[0] = 0;
258*6a54128fSAndroid Build Coastguard Worker size = write(raw_fd, copy_buf, 1);
259*6a54128fSAndroid Build Coastguard Worker if (size != 1) {
260*6a54128fSAndroid Build Coastguard Worker ret = errno;
261*6a54128fSAndroid Build Coastguard Worker goto out;
262*6a54128fSAndroid Build Coastguard Worker }
263*6a54128fSAndroid Build Coastguard Worker
264*6a54128fSAndroid Build Coastguard Worker out:
265*6a54128fSAndroid Build Coastguard Worker if (copy_buf)
266*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(©_buf);
267*6a54128fSAndroid Build Coastguard Worker if (img.l1_table)
268*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&img.l1_table);
269*6a54128fSAndroid Build Coastguard Worker if (l2_table)
270*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&l2_table);
271*6a54128fSAndroid Build Coastguard Worker return ret;
272*6a54128fSAndroid Build Coastguard Worker }
273