1*6a54128fSAndroid Build Coastguard Worker /* 2*6a54128fSAndroid Build Coastguard Worker * qcow2.h --- structures and function prototypes for qcow2.c to generate 3*6a54128fSAndroid Build Coastguard Worker * qcow2 formatted disk images. This format is used originally by QEMU 4*6a54128fSAndroid Build Coastguard Worker * for virtual machines, and stores the filesystem data on disk in a 5*6a54128fSAndroid Build Coastguard Worker * packed format to avoid creating sparse image files that need lots of 6*6a54128fSAndroid Build Coastguard Worker * seeking to read and write. 7*6a54128fSAndroid Build Coastguard Worker * 8*6a54128fSAndroid Build Coastguard Worker * The qcow2 format supports zlib compression, but that is not yet 9*6a54128fSAndroid Build Coastguard Worker * implemented. 10*6a54128fSAndroid Build Coastguard Worker * 11*6a54128fSAndroid Build Coastguard Worker * It is possible to directly mount a qcow2 image using qemu-nbd: 12*6a54128fSAndroid Build Coastguard Worker * 13*6a54128fSAndroid Build Coastguard Worker * [root]# modprobe nbd max_part=63 14*6a54128fSAndroid Build Coastguard Worker * [root]# qemu-nbd -c /dev/nbd0 image.img 15*6a54128fSAndroid Build Coastguard Worker * [root]# mount /dev/nbd0p1 /mnt/qemu 16*6a54128fSAndroid Build Coastguard Worker * 17*6a54128fSAndroid Build Coastguard Worker * Format details at http://people.gnome.org/~markmc/qcow-image-format.html 18*6a54128fSAndroid Build Coastguard Worker * 19*6a54128fSAndroid Build Coastguard Worker * Copyright (C) 2010 Red Hat, Inc., Lukas Czerner <[email protected]> 20*6a54128fSAndroid Build Coastguard Worker * 21*6a54128fSAndroid Build Coastguard Worker * %Begin-Header% 22*6a54128fSAndroid Build Coastguard Worker * This file may be redistributed under the terms of the GNU Public 23*6a54128fSAndroid Build Coastguard Worker * License. 24*6a54128fSAndroid Build Coastguard Worker * %End-Header% 25*6a54128fSAndroid Build Coastguard Worker */ 26*6a54128fSAndroid Build Coastguard Worker 27*6a54128fSAndroid Build Coastguard Worker /* Number of l2 tables in memory before writeback */ 28*6a54128fSAndroid Build Coastguard Worker #define L2_CACHE_PREALLOC 512 29*6a54128fSAndroid Build Coastguard Worker 30*6a54128fSAndroid Build Coastguard Worker 31*6a54128fSAndroid Build Coastguard Worker #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb) 32*6a54128fSAndroid Build Coastguard Worker #define QCOW_VERSION 2 33*6a54128fSAndroid Build Coastguard Worker #define QCOW_OFLAG_COPIED (1ULL << 63) 34*6a54128fSAndroid Build Coastguard Worker #define QCOW_OFLAG_COMPRESSED (1ULL << 62) 35*6a54128fSAndroid Build Coastguard Worker 36*6a54128fSAndroid Build Coastguard Worker #define QCOW_COMPRESSED 1 37*6a54128fSAndroid Build Coastguard Worker #define QCOW_ENCRYPTED 2 38*6a54128fSAndroid Build Coastguard Worker #define QCOW_CORRUPTED 3 39*6a54128fSAndroid Build Coastguard Worker 40*6a54128fSAndroid Build Coastguard Worker struct ext2_qcow2_hdr { 41*6a54128fSAndroid Build Coastguard Worker __u32 magic; 42*6a54128fSAndroid Build Coastguard Worker __u32 version; 43*6a54128fSAndroid Build Coastguard Worker 44*6a54128fSAndroid Build Coastguard Worker __u64 backing_file_offset; 45*6a54128fSAndroid Build Coastguard Worker __u32 backing_file_size; 46*6a54128fSAndroid Build Coastguard Worker 47*6a54128fSAndroid Build Coastguard Worker __u32 cluster_bits; 48*6a54128fSAndroid Build Coastguard Worker __u64 size; 49*6a54128fSAndroid Build Coastguard Worker __u32 crypt_method; 50*6a54128fSAndroid Build Coastguard Worker 51*6a54128fSAndroid Build Coastguard Worker __u32 l1_size; 52*6a54128fSAndroid Build Coastguard Worker __u64 l1_table_offset; 53*6a54128fSAndroid Build Coastguard Worker 54*6a54128fSAndroid Build Coastguard Worker __u64 refcount_table_offset; 55*6a54128fSAndroid Build Coastguard Worker __u32 refcount_table_clusters; 56*6a54128fSAndroid Build Coastguard Worker 57*6a54128fSAndroid Build Coastguard Worker __u32 nb_snapshots; 58*6a54128fSAndroid Build Coastguard Worker __u64 snapshots_offset; 59*6a54128fSAndroid Build Coastguard Worker }; 60*6a54128fSAndroid Build Coastguard Worker 61*6a54128fSAndroid Build Coastguard Worker typedef struct ext2_qcow2_l2_table L2_CACHE_HEAD; 62*6a54128fSAndroid Build Coastguard Worker 63*6a54128fSAndroid Build Coastguard Worker struct ext2_qcow2_l2_table { 64*6a54128fSAndroid Build Coastguard Worker __u32 l1_index; 65*6a54128fSAndroid Build Coastguard Worker __u64 offset; 66*6a54128fSAndroid Build Coastguard Worker __u64 *data; 67*6a54128fSAndroid Build Coastguard Worker L2_CACHE_HEAD *next; 68*6a54128fSAndroid Build Coastguard Worker }; 69*6a54128fSAndroid Build Coastguard Worker 70*6a54128fSAndroid Build Coastguard Worker struct ext2_qcow2_l2_cache { 71*6a54128fSAndroid Build Coastguard Worker L2_CACHE_HEAD *used_head; 72*6a54128fSAndroid Build Coastguard Worker L2_CACHE_HEAD *used_tail; 73*6a54128fSAndroid Build Coastguard Worker L2_CACHE_HEAD *free_head; 74*6a54128fSAndroid Build Coastguard Worker __u32 free; 75*6a54128fSAndroid Build Coastguard Worker __u32 count; 76*6a54128fSAndroid Build Coastguard Worker __u64 next_offset; 77*6a54128fSAndroid Build Coastguard Worker }; 78*6a54128fSAndroid Build Coastguard Worker 79*6a54128fSAndroid Build Coastguard Worker struct ext2_qcow2_refcount { 80*6a54128fSAndroid Build Coastguard Worker __u64 *refcount_table; 81*6a54128fSAndroid Build Coastguard Worker __u64 refcount_table_offset; 82*6a54128fSAndroid Build Coastguard Worker __u64 refcount_block_offset; 83*6a54128fSAndroid Build Coastguard Worker 84*6a54128fSAndroid Build Coastguard Worker __u32 refcount_table_clusters; 85*6a54128fSAndroid Build Coastguard Worker __u32 refcount_table_index; 86*6a54128fSAndroid Build Coastguard Worker __u32 refcount_block_index; 87*6a54128fSAndroid Build Coastguard Worker 88*6a54128fSAndroid Build Coastguard Worker __u16 *refcount_block; 89*6a54128fSAndroid Build Coastguard Worker }; 90*6a54128fSAndroid Build Coastguard Worker 91*6a54128fSAndroid Build Coastguard Worker struct ext2_qcow2_image { 92*6a54128fSAndroid Build Coastguard Worker int fd; 93*6a54128fSAndroid Build Coastguard Worker struct ext2_qcow2_hdr *hdr; 94*6a54128fSAndroid Build Coastguard Worker struct ext2_qcow2_l2_cache *l2_cache; 95*6a54128fSAndroid Build Coastguard Worker struct ext2_qcow2_refcount refcount; 96*6a54128fSAndroid Build Coastguard Worker __u32 cluster_size; 97*6a54128fSAndroid Build Coastguard Worker __u32 cluster_bits; 98*6a54128fSAndroid Build Coastguard Worker __u32 l1_size; 99*6a54128fSAndroid Build Coastguard Worker __u32 l2_size; 100*6a54128fSAndroid Build Coastguard Worker 101*6a54128fSAndroid Build Coastguard Worker __u64 *l1_table; 102*6a54128fSAndroid Build Coastguard Worker __u64 l2_offset; 103*6a54128fSAndroid Build Coastguard Worker __u64 l1_offset; 104*6a54128fSAndroid Build Coastguard Worker __u64 image_size; 105*6a54128fSAndroid Build Coastguard Worker }; 106*6a54128fSAndroid Build Coastguard Worker 107*6a54128fSAndroid Build Coastguard Worker /* Function prototypes */ 108*6a54128fSAndroid Build Coastguard Worker 109*6a54128fSAndroid Build Coastguard Worker /* qcow2.c */ 110*6a54128fSAndroid Build Coastguard Worker 111*6a54128fSAndroid Build Coastguard Worker /* Functions for converting qcow2 image into raw image */ 112*6a54128fSAndroid Build Coastguard Worker struct ext2_qcow2_hdr *qcow2_read_header(int); 113*6a54128fSAndroid Build Coastguard Worker int qcow2_write_raw_image(int, int, struct ext2_qcow2_hdr *); 114*6a54128fSAndroid Build Coastguard Worker 115