]> git.proxmox.com Git - qemu.git/blame - vmdk2raw.c
BMDMA support - CDROM fixes
[qemu.git] / vmdk2raw.c
CommitLineData
47cea614
FB
1/*
2 vmdk2raw: convert vmware images to raw disk images
3 Copyright (C) Net Integration Technologies 2004
4 Copyright (C) Matthew Chapman 2003
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21#include <stdlib.h>
22#include <stdio.h>
23#include <unistd.h>
24#include <fcntl.h>
25#include <inttypes.h>
26#include <sys/types.h>
27#include <string.h>
28#include <sys/stat.h>
29#include <errno.h>
30#include "vmdk.h"
31#include "config-host.h"
32
05efe46e
FB
33static struct cowdisk_header header;
34static struct vmdisk_header header4;
35static off64_t disk_limit;
36static unsigned int granule_size;
37static uint32_t *l1dir;
38
39static unsigned int cached_l2dir;
40static uint32_t l2dir[L2_SIZE];
41
42static struct vmdk_prm {
43 uint32_t grain_table_size;
44 uint32_t sectors_per_grain;
45 uint32_t sectors_per_table;
46 uint32_t directory_size;
47} vdsk;
48
49static size_t read_physical(int fd, off64_t offset, size_t length, void *buffer)
47cea614
FB
50{
51 size_t n;
52
05efe46e
FB
53 if (lseek64(fd, offset, SEEK_SET) == -1) {
54 printf(" error trying to seek lseek to %lld", offset);
47cea614
FB
55 return -1;
56 }
57
58 n = read(fd, buffer, length);
05efe46e
FB
59
60 if (n == -1) {
61 printf("read from disk %lld", offset);
47cea614
FB
62 return -1;
63 }
64
65 return n;
66}
67
05efe46e
FB
68static int read_l1dir(int fd, size_t offset, int num)
69{
70 l1dir = malloc(sizeof(*l1dir) * num);
71 if (!l1dir)
72 return -1;
73 return read_physical(fd, offset << SECTOR_BITS, sizeof(*l1dir) * num, (char *)l1dir) != (sizeof(*l1dir) * num);
74}
75
76static int read_l2dir(int fd, size_t offset, int num)
47cea614 77{
05efe46e
FB
78 return read_physical(fd, offset << SECTOR_BITS, sizeof(l2dir[0]) * num, (char *)l2dir) != sizeof(l2dir);
79}
47cea614 80
05efe46e
FB
81static size_t copy_virtual(struct vmdk_prm *dsk, int in_fd, int out_fd, off64_t offset, void *buffer, size_t length)
82{
83
84 unsigned int granule_offset;
85 unsigned int grain_index;
86 unsigned int sector_map_idx;
87
47cea614
FB
88 granule_offset = offset % granule_size;
89 length = MIN(length, granule_size - granule_offset);
90 length = MIN(length, disk_limit - offset);
91
05efe46e
FB
92 sector_map_idx = (offset >> SECTOR_BITS) / dsk->sectors_per_table;
93
94 if (sector_map_idx >= dsk->directory_size) {
95 fprintf(stderr, "cannot locate grain table for %d in %d\n", sector_map_idx, dsk->directory_size);
96 return -1;
97 }
47cea614 98
05efe46e 99 if (l1dir[sector_map_idx] == 0)
47cea614 100 goto zero_fill;
05efe46e
FB
101
102 if (sector_map_idx != cached_l2dir) {
103 if (read_l2dir(in_fd, l1dir[sector_map_idx], dsk->grain_table_size)) {
104 fprintf(stderr, "read failed\n");
105 return -1;
106 }
107 cached_l2dir = sector_map_idx;
108 }
47cea614 109
05efe46e
FB
110 grain_index = ((offset >> SECTOR_BITS) % dsk->sectors_per_table) / dsk->sectors_per_grain;
111
112 if (grain_index >= dsk->grain_table_size) {
113 fprintf(stderr, "grain to large");
114 return -1;
47cea614
FB
115 }
116
05efe46e 117 if (l2dir[grain_index] == 0)
47cea614
FB
118 goto zero_fill;
119
05efe46e
FB
120 if (read_physical(in_fd, (l2dir[grain_index] << SECTOR_BITS) + granule_offset, length, buffer) != length) {
121 fprintf(stderr, "read error 2\n");
122 return -1;
123 }
124
47cea614
FB
125 write(out_fd, buffer, length);
126 return length;
127
128zero_fill:
129 /* the last chunk of the file can not be sparse
130 * or the file will be truncated */
05efe46e
FB
131 if (offset + length >= disk_limit) {
132 if (lseek64(out_fd, length-1, SEEK_CUR) == (off_t)-1)
133 perror("lseek");
134 /* write the last NULL byte instead of seeking */
135 const char nil = 0;
136 write(out_fd, &nil, 1);
47cea614 137 } else {
05efe46e 138 if (lseek64(out_fd, length, SEEK_CUR) == (off_t)-1)
47cea614
FB
139 perror("lseek");
140 }
141 return length;
142}
143
05efe46e 144static int open_vmdk4(int fd)
47cea614 145{
05efe46e
FB
146 if (read(fd, &header4, sizeof(header4)) != sizeof(header4)) {
147 perror("read from disk");
47cea614
FB
148 return -1;
149 }
05efe46e
FB
150
151 granule_size = header4.granularity << SECTOR_BITS;
152 disk_limit = header4.capacity << SECTOR_BITS;
153
154 cached_l2dir = -1;
155 vdsk.grain_table_size = header4.num_gtes_per_gte;
156 vdsk.sectors_per_grain = header4.granularity;
157 vdsk.sectors_per_table = vdsk.grain_table_size * vdsk.sectors_per_grain;
158 vdsk.directory_size = (header4.capacity + vdsk.sectors_per_table - 1) / vdsk.sectors_per_table + 1;
47cea614 159
05efe46e 160 if (read_l1dir(fd, header4.rgd_offset, vdsk.directory_size))
47cea614 161 return -1;
05efe46e
FB
162
163 return 0;
164
165}
47cea614 166
05efe46e
FB
167static int open_vmdk3(int fd)
168{
169 if (read(fd, &header, sizeof(header)) != sizeof(header)) {
170 perror("read from disk\n");
47cea614
FB
171 return -1;
172 }
47cea614 173 granule_size = header.granularity << SECTOR_BITS;
05efe46e
FB
174 vdsk.sectors_per_grain = header.granularity;
175 vdsk.grain_table_size = L2_SIZE;
176 vdsk.sectors_per_table = vdsk.grain_table_size * vdsk.sectors_per_grain;
177 vdsk.directory_size = L1_SIZE;
178 if (read_l1dir(fd, header.l1dir_offset, L1_SIZE))
47cea614
FB
179 return -1;
180
181 disk_limit = header.disk_sectors << SECTOR_BITS;
182
05efe46e
FB
183 return fd;
184}
185
186static int open_vmdk(const char *filename)
187{
188 int fd = open(filename, O_RDONLY | O_LARGEFILE);
189 if (fd == -1) {
190 perror(filename);
191 return -1;
192 }
193
194 char magic[4];
195 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
196 perror("read from disk");
197 return -1;
198 }
199
200 if (!memcmp(magic, "KDMV", sizeof(magic))) {
201 open_vmdk4(fd);
202 } else if (!memcmp(magic, "COWD", sizeof(magic))) {
203 open_vmdk3(fd);
204 } else {
205 fprintf(stderr, "%s is not a VMware virtual disk image\n", filename);
206 return -1;
207 }
208
47cea614
FB
209 cached_l2dir = -1;
210 return fd;
211}
212
05efe46e 213static void help(void)
47cea614
FB
214{
215 printf("vmdk2raw\n"
216 "usage: vmdk2raw vmware_image output_image\n"
217 "\n"
05efe46e 218 "vmware_image a vmware cow image\n"
47cea614
FB
219 "output_image the created disk image\n"
220 );
221 exit(1);
222}
223
05efe46e
FB
224#define BUF_SIZE 0x10000
225static void copy_disk(in_fd, out_fd)
47cea614
FB
226{
227 char buf[BUF_SIZE];
228 off64_t i = 0;
05efe46e 229 int ret;
47cea614 230 while (i < disk_limit) {
05efe46e
FB
231 ret = copy_virtual(&vdsk, in_fd, out_fd, i, buf, sizeof(buf));
232 if (ret < 0) {
233 fprintf(stderr, "copying failed\n");
234 exit(-1);
235 }
236 i += ret;
47cea614
FB
237 }
238}
239
240int main(int argc, char **argv)
241{
242 int out_fd, in_fd;
243
244 if (argc < 3)
245 help();
246
247 in_fd = open_vmdk(argv[1]);
248 if (in_fd < 0) {
249 return -1;
250 }
251
05efe46e 252 out_fd = open(argv[2], O_WRONLY | O_LARGEFILE | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
47cea614
FB
253 if (out_fd < 0) {
254 perror(argv[2]);
255 return -1;
256 }
257
258 copy_disk(in_fd, out_fd);
259 close(out_fd);
260 return 0;
261}