blob: f1a8aebf5e1ac783eba596e6052760dc997678e3 [file] [log] [blame]
bellardea2384d2004-08-01 21:59:26 +00001/*
bellardfb43f4d2006-08-07 21:34:46 +00002 * QEMU disk image utility
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard84f2e8e2007-02-05 20:21:32 +00004 * Copyright (c) 2003-2007 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardea2384d2004-08-01 21:59:26 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
pbrookfaf07962007-11-11 02:51:17 +000024#include "qemu-common.h"
thsec36ba12007-09-16 21:59:02 +000025#include "block_int.h"
balrog926c2d22007-10-31 01:11:44 +000026#include <assert.h>
bellardea2384d2004-08-01 21:59:26 +000027
bellarde8445332006-06-14 15:32:10 +000028#ifdef _WIN32
29#include <windows.h>
30#endif
31
bellardea2384d2004-08-01 21:59:26 +000032void *get_mmap_addr(unsigned long size)
33{
34 return NULL;
35}
36
37void qemu_free(void *ptr)
38{
39 free(ptr);
40}
41
42void *qemu_malloc(size_t size)
43{
44 return malloc(size);
45}
46
47void *qemu_mallocz(size_t size)
48{
49 void *ptr;
50 ptr = qemu_malloc(size);
51 if (!ptr)
52 return NULL;
53 memset(ptr, 0, size);
54 return ptr;
55}
56
57char *qemu_strdup(const char *str)
58{
59 char *ptr;
60 ptr = qemu_malloc(strlen(str) + 1);
61 if (!ptr)
62 return NULL;
63 strcpy(ptr, str);
64 return ptr;
65}
66
pbrook3f379ab2007-11-11 03:33:13 +000067static void __attribute__((noreturn)) error(const char *fmt, ...)
bellardea2384d2004-08-01 21:59:26 +000068{
69 va_list ap;
70 va_start(ap, fmt);
bellard57d1a2b2004-08-03 21:15:11 +000071 fprintf(stderr, "qemu-img: ");
bellardea2384d2004-08-01 21:59:26 +000072 vfprintf(stderr, fmt, ap);
73 fprintf(stderr, "\n");
74 exit(1);
75 va_end(ap);
76}
77
78static void format_print(void *opaque, const char *name)
79{
80 printf(" %s", name);
81}
82
pbrook3f379ab2007-11-11 03:33:13 +000083static void help(void)
bellardea2384d2004-08-01 21:59:26 +000084{
bellard84f2e8e2007-02-05 20:21:32 +000085 printf("qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2007 Fabrice Bellard\n"
bellard57d1a2b2004-08-03 21:15:11 +000086 "usage: qemu-img command [command options]\n"
bellardea2384d2004-08-01 21:59:26 +000087 "QEMU disk image utility\n"
88 "\n"
89 "Command syntax:\n"
thsec36ba12007-09-16 21:59:02 +000090 " create [-e] [-6] [-b base_image] [-f fmt] filename [size]\n"
bellardea2384d2004-08-01 21:59:26 +000091 " commit [-f fmt] filename\n"
balrog926c2d22007-10-31 01:11:44 +000092 " convert [-c] [-e] [-6] [-f fmt] filename [filename2 [...]] [-O output_fmt] output_filename\n"
bellardea2384d2004-08-01 21:59:26 +000093 " info [-f fmt] filename\n"
94 "\n"
95 "Command parameters:\n"
96 " 'filename' is a disk image filename\n"
97 " 'base_image' is the read-only disk image which is used as base for a copy on\n"
98 " write image; the copy on write image only stores the modified data\n"
99 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
100 " 'size' is the disk image size in kilobytes. Optional suffixes 'M' (megabyte)\n"
101 " and 'G' (gigabyte) are supported\n"
102 " 'output_filename' is the destination disk image filename\n"
103 " 'output_fmt' is the destination format\n"
104 " '-c' indicates that target image must be compressed (qcow format only)\n"
105 " '-e' indicates that the target image must be encrypted (qcow format only)\n"
thsec36ba12007-09-16 21:59:02 +0000106 " '-6' indicates that the target image must use compatibility level 6 (vmdk format only)\n"
bellardea2384d2004-08-01 21:59:26 +0000107 );
108 printf("\nSupported format:");
109 bdrv_iterate_format(format_print, NULL);
110 printf("\n");
111 exit(1);
112}
113
bellardea2384d2004-08-01 21:59:26 +0000114#if defined(WIN32)
115/* XXX: put correct support for win32 */
116static int read_password(char *buf, int buf_size)
117{
118 int c, i;
119 printf("Password: ");
120 fflush(stdout);
121 i = 0;
122 for(;;) {
123 c = getchar();
124 if (c == '\n')
125 break;
126 if (i < (buf_size - 1))
127 buf[i++] = c;
128 }
129 buf[i] = '\0';
130 return 0;
131}
132
133#else
134
135#include <termios.h>
136
137static struct termios oldtty;
138
139static void term_exit(void)
140{
141 tcsetattr (0, TCSANOW, &oldtty);
142}
143
144static void term_init(void)
145{
146 struct termios tty;
147
148 tcgetattr (0, &tty);
149 oldtty = tty;
150
151 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
152 |INLCR|IGNCR|ICRNL|IXON);
153 tty.c_oflag |= OPOST;
154 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
155 tty.c_cflag &= ~(CSIZE|PARENB);
156 tty.c_cflag |= CS8;
157 tty.c_cc[VMIN] = 1;
158 tty.c_cc[VTIME] = 0;
ths3b46e622007-09-17 08:09:54 +0000159
bellardea2384d2004-08-01 21:59:26 +0000160 tcsetattr (0, TCSANOW, &tty);
161
162 atexit(term_exit);
163}
164
pbrook3f379ab2007-11-11 03:33:13 +0000165static int read_password(char *buf, int buf_size)
bellardea2384d2004-08-01 21:59:26 +0000166{
167 uint8_t ch;
168 int i, ret;
169
170 printf("password: ");
171 fflush(stdout);
172 term_init();
173 i = 0;
174 for(;;) {
175 ret = read(0, &ch, 1);
176 if (ret == -1) {
177 if (errno == EAGAIN || errno == EINTR) {
178 continue;
179 } else {
180 ret = -1;
181 break;
182 }
183 } else if (ret == 0) {
184 ret = -1;
185 break;
186 } else {
187 if (ch == '\r') {
188 ret = 0;
189 break;
190 }
191 if (i < (buf_size - 1))
192 buf[i++] = ch;
193 }
194 }
195 term_exit();
196 buf[i] = '\0';
197 printf("\n");
198 return ret;
199}
200#endif
201
bellard75c23802004-08-27 21:28:58 +0000202static BlockDriverState *bdrv_new_open(const char *filename,
203 const char *fmt)
204{
205 BlockDriverState *bs;
206 BlockDriver *drv;
207 char password[256];
208
209 bs = bdrv_new("");
210 if (!bs)
211 error("Not enough memory");
212 if (fmt) {
213 drv = bdrv_find_format(fmt);
214 if (!drv)
215 error("Unknown file format '%s'", fmt);
216 } else {
217 drv = NULL;
218 }
219 if (bdrv_open2(bs, filename, 0, drv) < 0) {
220 error("Could not open '%s'", filename);
221 }
222 if (bdrv_is_encrypted(bs)) {
223 printf("Disk image '%s' is encrypted.\n", filename);
224 if (read_password(password, sizeof(password)) < 0)
225 error("No password given");
226 if (bdrv_set_key(bs, password) < 0)
227 error("invalid password");
228 }
229 return bs;
230}
231
bellardea2384d2004-08-01 21:59:26 +0000232static int img_create(int argc, char **argv)
233{
thsec36ba12007-09-16 21:59:02 +0000234 int c, ret, flags;
bellardea2384d2004-08-01 21:59:26 +0000235 const char *fmt = "raw";
236 const char *filename;
237 const char *base_filename = NULL;
238 int64_t size;
239 const char *p;
240 BlockDriver *drv;
ths3b46e622007-09-17 08:09:54 +0000241
thsec36ba12007-09-16 21:59:02 +0000242 flags = 0;
bellardea2384d2004-08-01 21:59:26 +0000243 for(;;) {
thsec36ba12007-09-16 21:59:02 +0000244 c = getopt(argc, argv, "b:f:he6");
bellardea2384d2004-08-01 21:59:26 +0000245 if (c == -1)
246 break;
247 switch(c) {
248 case 'h':
249 help();
250 break;
251 case 'b':
252 base_filename = optarg;
253 break;
254 case 'f':
255 fmt = optarg;
256 break;
257 case 'e':
thsec36ba12007-09-16 21:59:02 +0000258 flags |= BLOCK_FLAG_ENCRYPT;
bellardea2384d2004-08-01 21:59:26 +0000259 break;
thsd8871c52007-10-24 16:11:42 +0000260 case '6':
thsec36ba12007-09-16 21:59:02 +0000261 flags |= BLOCK_FLAG_COMPAT6;
thsd8871c52007-10-24 16:11:42 +0000262 break;
bellardea2384d2004-08-01 21:59:26 +0000263 }
264 }
ths5fafdf22007-09-16 21:08:06 +0000265 if (optind >= argc)
bellardea2384d2004-08-01 21:59:26 +0000266 help();
267 filename = argv[optind++];
268 size = 0;
bellard75c23802004-08-27 21:28:58 +0000269 if (base_filename) {
270 BlockDriverState *bs;
271 bs = bdrv_new_open(base_filename, NULL);
272 bdrv_get_geometry(bs, &size);
273 size *= 512;
274 bdrv_delete(bs);
275 } else {
bellardea2384d2004-08-01 21:59:26 +0000276 if (optind >= argc)
277 help();
278 p = argv[optind];
279 size = strtoul(p, (char **)&p, 0);
280 if (*p == 'M') {
281 size *= 1024 * 1024;
282 } else if (*p == 'G') {
283 size *= 1024 * 1024 * 1024;
284 } else if (*p == 'k' || *p == 'K' || *p == '\0') {
285 size *= 1024;
286 } else {
287 help();
288 }
289 }
290 drv = bdrv_find_format(fmt);
291 if (!drv)
292 error("Unknown file format '%s'", fmt);
ths0cfec832007-06-23 16:02:43 +0000293 printf("Formatting '%s', fmt=%s",
bellardea2384d2004-08-01 21:59:26 +0000294 filename, fmt);
thsec36ba12007-09-16 21:59:02 +0000295 if (flags & BLOCK_FLAG_ENCRYPT)
bellardea2384d2004-08-01 21:59:26 +0000296 printf(", encrypted");
thsec36ba12007-09-16 21:59:02 +0000297 if (flags & BLOCK_FLAG_COMPAT6)
298 printf(", compatibility level=6");
bellard75c23802004-08-27 21:28:58 +0000299 if (base_filename) {
300 printf(", backing_file=%s",
bellardea2384d2004-08-01 21:59:26 +0000301 base_filename);
bellard75c23802004-08-27 21:28:58 +0000302 }
bellardec3757d2006-06-14 15:50:07 +0000303 printf(", size=%" PRId64 " kB\n", (int64_t) (size / 1024));
thsec36ba12007-09-16 21:59:02 +0000304 ret = bdrv_create(drv, filename, size / 512, base_filename, flags);
bellardea2384d2004-08-01 21:59:26 +0000305 if (ret < 0) {
306 if (ret == -ENOTSUP) {
bellard3c565212004-09-29 21:29:14 +0000307 error("Formatting or formatting option not supported for file format '%s'", fmt);
bellardea2384d2004-08-01 21:59:26 +0000308 } else {
309 error("Error while formatting");
310 }
311 }
312 return 0;
313}
314
315static int img_commit(int argc, char **argv)
316{
317 int c, ret;
318 const char *filename, *fmt;
319 BlockDriver *drv;
320 BlockDriverState *bs;
321
322 fmt = NULL;
323 for(;;) {
324 c = getopt(argc, argv, "f:h");
325 if (c == -1)
326 break;
327 switch(c) {
328 case 'h':
329 help();
330 break;
331 case 'f':
332 fmt = optarg;
333 break;
334 }
335 }
ths5fafdf22007-09-16 21:08:06 +0000336 if (optind >= argc)
bellardea2384d2004-08-01 21:59:26 +0000337 help();
338 filename = argv[optind++];
339
340 bs = bdrv_new("");
341 if (!bs)
342 error("Not enough memory");
343 if (fmt) {
344 drv = bdrv_find_format(fmt);
345 if (!drv)
346 error("Unknown file format '%s'", fmt);
347 } else {
348 drv = NULL;
349 }
350 if (bdrv_open2(bs, filename, 0, drv) < 0) {
351 error("Could not open '%s'", filename);
352 }
353 ret = bdrv_commit(bs);
354 switch(ret) {
355 case 0:
356 printf("Image committed.\n");
357 break;
358 case -ENOENT:
359 error("No disk inserted");
360 break;
361 case -EACCES:
362 error("Image is read-only");
363 break;
364 case -ENOTSUP:
365 error("Image is already committed");
366 break;
367 default:
368 error("Error while committing image");
369 break;
370 }
371
372 bdrv_delete(bs);
373 return 0;
374}
375
376static int is_not_zero(const uint8_t *sector, int len)
377{
378 int i;
379 len >>= 2;
380 for(i = 0;i < len; i++) {
381 if (((uint32_t *)sector)[i] != 0)
382 return 1;
383 }
384 return 0;
385}
386
387static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
388{
389 int v, i;
390
391 if (n <= 0) {
392 *pnum = 0;
393 return 0;
394 }
395 v = is_not_zero(buf, 512);
396 for(i = 1; i < n; i++) {
397 buf += 512;
398 if (v != is_not_zero(buf, 512))
399 break;
400 }
401 *pnum = i;
402 return v;
403}
404
bellardea2384d2004-08-01 21:59:26 +0000405#define IO_BUF_SIZE 65536
406
407static int img_convert(int argc, char **argv)
408{
balrog926c2d22007-10-31 01:11:44 +0000409 int c, ret, n, n1, bs_n, bs_i, flags, cluster_size, cluster_sectors;
410 const char *fmt, *out_fmt, *out_filename;
bellardea2384d2004-08-01 21:59:26 +0000411 BlockDriver *drv;
balrog926c2d22007-10-31 01:11:44 +0000412 BlockDriverState **bs, *out_bs;
413 int64_t total_sectors, nb_sectors, sector_num, bs_offset, bs_sectors;
bellardea2384d2004-08-01 21:59:26 +0000414 uint8_t buf[IO_BUF_SIZE];
415 const uint8_t *buf1;
bellardfaea38e2006-08-05 21:31:00 +0000416 BlockDriverInfo bdi;
bellardea2384d2004-08-01 21:59:26 +0000417
418 fmt = NULL;
419 out_fmt = "raw";
thsec36ba12007-09-16 21:59:02 +0000420 flags = 0;
bellardea2384d2004-08-01 21:59:26 +0000421 for(;;) {
thsec36ba12007-09-16 21:59:02 +0000422 c = getopt(argc, argv, "f:O:hce6");
bellardea2384d2004-08-01 21:59:26 +0000423 if (c == -1)
424 break;
425 switch(c) {
426 case 'h':
427 help();
428 break;
429 case 'f':
430 fmt = optarg;
431 break;
432 case 'O':
433 out_fmt = optarg;
434 break;
435 case 'c':
thsec36ba12007-09-16 21:59:02 +0000436 flags |= BLOCK_FLAG_COMPRESS;
bellardea2384d2004-08-01 21:59:26 +0000437 break;
438 case 'e':
thsec36ba12007-09-16 21:59:02 +0000439 flags |= BLOCK_FLAG_ENCRYPT;
440 break;
441 case '6':
442 flags |= BLOCK_FLAG_COMPAT6;
bellardea2384d2004-08-01 21:59:26 +0000443 break;
444 }
445 }
ths3b46e622007-09-17 08:09:54 +0000446
balrog926c2d22007-10-31 01:11:44 +0000447 bs_n = argc - optind - 1;
448 if (bs_n < 1) help();
449
450 out_filename = argv[argc - 1];
451
452 bs = calloc(bs_n, sizeof(BlockDriverState *));
453 if (!bs)
454 error("Out of memory");
455
456 total_sectors = 0;
457 for (bs_i = 0; bs_i < bs_n; bs_i++) {
458 bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt);
459 if (!bs[bs_i])
460 error("Could not open '%s'", argv[optind + bs_i]);
461 bdrv_get_geometry(bs[bs_i], &bs_sectors);
462 total_sectors += bs_sectors;
463 }
bellardea2384d2004-08-01 21:59:26 +0000464
465 drv = bdrv_find_format(out_fmt);
466 if (!drv)
thsd34dda52007-02-10 22:59:40 +0000467 error("Unknown file format '%s'", out_fmt);
thsec36ba12007-09-16 21:59:02 +0000468 if (flags & BLOCK_FLAG_COMPRESS && drv != &bdrv_qcow && drv != &bdrv_qcow2)
bellardea2384d2004-08-01 21:59:26 +0000469 error("Compression not supported for this file format");
thsec36ba12007-09-16 21:59:02 +0000470 if (flags & BLOCK_FLAG_ENCRYPT && drv != &bdrv_qcow && drv != &bdrv_qcow2)
bellardea2384d2004-08-01 21:59:26 +0000471 error("Encryption not supported for this file format");
thsd8871c52007-10-24 16:11:42 +0000472 if (flags & BLOCK_FLAG_COMPAT6 && drv != &bdrv_vmdk)
thsec36ba12007-09-16 21:59:02 +0000473 error("Alternative compatibility level not supported for this file format");
474 if (flags & BLOCK_FLAG_ENCRYPT && flags & BLOCK_FLAG_COMPRESS)
bellardea2384d2004-08-01 21:59:26 +0000475 error("Compression and encryption not supported at the same time");
balrog926c2d22007-10-31 01:11:44 +0000476
thsec36ba12007-09-16 21:59:02 +0000477 ret = bdrv_create(drv, out_filename, total_sectors, NULL, flags);
bellardea2384d2004-08-01 21:59:26 +0000478 if (ret < 0) {
479 if (ret == -ENOTSUP) {
bellard3c565212004-09-29 21:29:14 +0000480 error("Formatting not supported for file format '%s'", fmt);
bellardea2384d2004-08-01 21:59:26 +0000481 } else {
482 error("Error while formatting '%s'", out_filename);
483 }
484 }
ths3b46e622007-09-17 08:09:54 +0000485
bellardea2384d2004-08-01 21:59:26 +0000486 out_bs = bdrv_new_open(out_filename, out_fmt);
487
balrog926c2d22007-10-31 01:11:44 +0000488 bs_i = 0;
489 bs_offset = 0;
490 bdrv_get_geometry(bs[0], &bs_sectors);
491
492 if (flags & BLOCK_FLAG_COMPRESS) {
bellardfaea38e2006-08-05 21:31:00 +0000493 if (bdrv_get_info(out_bs, &bdi) < 0)
494 error("could not get block driver info");
495 cluster_size = bdi.cluster_size;
bellardea2384d2004-08-01 21:59:26 +0000496 if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE)
497 error("invalid cluster size");
498 cluster_sectors = cluster_size >> 9;
499 sector_num = 0;
500 for(;;) {
balrog926c2d22007-10-31 01:11:44 +0000501 int64_t bs_num;
502 int remainder;
503 uint8_t *buf2;
504
bellardea2384d2004-08-01 21:59:26 +0000505 nb_sectors = total_sectors - sector_num;
506 if (nb_sectors <= 0)
507 break;
508 if (nb_sectors >= cluster_sectors)
509 n = cluster_sectors;
510 else
511 n = nb_sectors;
balrog926c2d22007-10-31 01:11:44 +0000512
513 bs_num = sector_num - bs_offset;
514 assert (bs_num >= 0);
515 remainder = n;
516 buf2 = buf;
517 while (remainder > 0) {
518 int nlow;
519 while (bs_num == bs_sectors) {
520 bs_i++;
521 assert (bs_i < bs_n);
522 bs_offset += bs_sectors;
523 bdrv_get_geometry(bs[bs_i], &bs_sectors);
524 bs_num = 0;
525 /* printf("changing part: sector_num=%lld, "
526 "bs_i=%d, bs_offset=%lld, bs_sectors=%lld\n",
527 sector_num, bs_i, bs_offset, bs_sectors); */
528 }
529 assert (bs_num < bs_sectors);
530
531 nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder;
532
533 if (bdrv_read(bs[bs_i], bs_num, buf2, nlow) < 0)
534 error("error while reading");
535
536 buf2 += nlow * 512;
537 bs_num += nlow;
538
539 remainder -= nlow;
540 }
541 assert (remainder == 0);
542
bellardea2384d2004-08-01 21:59:26 +0000543 if (n < cluster_sectors)
544 memset(buf + n * 512, 0, cluster_size - n * 512);
545 if (is_not_zero(buf, cluster_size)) {
ths5fafdf22007-09-16 21:08:06 +0000546 if (bdrv_write_compressed(out_bs, sector_num, buf,
bellardfaea38e2006-08-05 21:31:00 +0000547 cluster_sectors) != 0)
bellardec3757d2006-06-14 15:50:07 +0000548 error("error while compressing sector %" PRId64,
549 sector_num);
bellardea2384d2004-08-01 21:59:26 +0000550 }
551 sector_num += n;
552 }
bellardfaea38e2006-08-05 21:31:00 +0000553 /* signal EOF to align */
554 bdrv_write_compressed(out_bs, 0, NULL, 0);
bellardea2384d2004-08-01 21:59:26 +0000555 } else {
556 sector_num = 0;
557 for(;;) {
558 nb_sectors = total_sectors - sector_num;
559 if (nb_sectors <= 0)
560 break;
561 if (nb_sectors >= (IO_BUF_SIZE / 512))
562 n = (IO_BUF_SIZE / 512);
563 else
564 n = nb_sectors;
balrog926c2d22007-10-31 01:11:44 +0000565
566 while (sector_num - bs_offset >= bs_sectors) {
567 bs_i ++;
568 assert (bs_i < bs_n);
569 bs_offset += bs_sectors;
570 bdrv_get_geometry(bs[bs_i], &bs_sectors);
571 /* printf("changing part: sector_num=%lld, bs_i=%d, "
572 "bs_offset=%lld, bs_sectors=%lld\n",
573 sector_num, bs_i, bs_offset, bs_sectors); */
574 }
575
576 if (n > bs_offset + bs_sectors - sector_num)
577 n = bs_offset + bs_sectors - sector_num;
578
579 if (bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n) < 0)
bellardea2384d2004-08-01 21:59:26 +0000580 error("error while reading");
581 /* NOTE: at the same time we convert, we do not write zero
582 sectors to have a chance to compress the image. Ideally, we
583 should add a specific call to have the info to go faster */
584 buf1 = buf;
585 while (n > 0) {
586 if (is_allocated_sectors(buf1, n, &n1)) {
ths5fafdf22007-09-16 21:08:06 +0000587 if (bdrv_write(out_bs, sector_num, buf1, n1) < 0)
bellardea2384d2004-08-01 21:59:26 +0000588 error("error while writing");
589 }
590 sector_num += n1;
591 n -= n1;
592 buf1 += n1 * 512;
593 }
594 }
595 }
596 bdrv_delete(out_bs);
balrog926c2d22007-10-31 01:11:44 +0000597 for (bs_i = 0; bs_i < bs_n; bs_i++)
598 bdrv_delete(bs[bs_i]);
599 free(bs);
bellardea2384d2004-08-01 21:59:26 +0000600 return 0;
601}
602
bellard57d1a2b2004-08-03 21:15:11 +0000603#ifdef _WIN32
604static int64_t get_allocated_file_size(const char *filename)
605{
bellarde8445332006-06-14 15:32:10 +0000606 typedef DWORD (WINAPI * get_compressed_t)(const char *filename, DWORD *high);
607 get_compressed_t get_compressed;
bellard57d1a2b2004-08-03 21:15:11 +0000608 struct _stati64 st;
bellarde8445332006-06-14 15:32:10 +0000609
610 /* WinNT support GetCompressedFileSize to determine allocate size */
611 get_compressed = (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"), "GetCompressedFileSizeA");
612 if (get_compressed) {
613 DWORD high, low;
614 low = get_compressed(filename, &high);
615 if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR)
616 return (((int64_t) high) << 32) + low;
617 }
618
ths5fafdf22007-09-16 21:08:06 +0000619 if (_stati64(filename, &st) < 0)
bellard57d1a2b2004-08-03 21:15:11 +0000620 return -1;
621 return st.st_size;
622}
623#else
624static int64_t get_allocated_file_size(const char *filename)
625{
626 struct stat st;
ths5fafdf22007-09-16 21:08:06 +0000627 if (stat(filename, &st) < 0)
bellard57d1a2b2004-08-03 21:15:11 +0000628 return -1;
629 return (int64_t)st.st_blocks * 512;
630}
631#endif
632
bellardfaea38e2006-08-05 21:31:00 +0000633static void dump_snapshots(BlockDriverState *bs)
634{
635 QEMUSnapshotInfo *sn_tab, *sn;
636 int nb_sns, i;
637 char buf[256];
638
639 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
640 if (nb_sns <= 0)
641 return;
642 printf("Snapshot list:\n");
643 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
644 for(i = 0; i < nb_sns; i++) {
645 sn = &sn_tab[i];
646 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
647 }
648 qemu_free(sn_tab);
649}
650
bellardea2384d2004-08-01 21:59:26 +0000651static int img_info(int argc, char **argv)
652{
653 int c;
654 const char *filename, *fmt;
655 BlockDriver *drv;
656 BlockDriverState *bs;
657 char fmt_name[128], size_buf[128], dsize_buf[128];
bellard57d1a2b2004-08-03 21:15:11 +0000658 int64_t total_sectors, allocated_size;
bellard93b6b2a2006-08-01 15:51:11 +0000659 char backing_filename[1024];
660 char backing_filename2[1024];
bellardfaea38e2006-08-05 21:31:00 +0000661 BlockDriverInfo bdi;
bellardea2384d2004-08-01 21:59:26 +0000662
663 fmt = NULL;
664 for(;;) {
665 c = getopt(argc, argv, "f:h");
666 if (c == -1)
667 break;
668 switch(c) {
669 case 'h':
670 help();
671 break;
672 case 'f':
673 fmt = optarg;
674 break;
675 }
676 }
ths5fafdf22007-09-16 21:08:06 +0000677 if (optind >= argc)
bellardea2384d2004-08-01 21:59:26 +0000678 help();
679 filename = argv[optind++];
680
681 bs = bdrv_new("");
682 if (!bs)
683 error("Not enough memory");
684 if (fmt) {
685 drv = bdrv_find_format(fmt);
686 if (!drv)
687 error("Unknown file format '%s'", fmt);
688 } else {
689 drv = NULL;
690 }
691 if (bdrv_open2(bs, filename, 0, drv) < 0) {
692 error("Could not open '%s'", filename);
693 }
694 bdrv_get_format(bs, fmt_name, sizeof(fmt_name));
695 bdrv_get_geometry(bs, &total_sectors);
696 get_human_readable_size(size_buf, sizeof(size_buf), total_sectors * 512);
bellard57d1a2b2004-08-03 21:15:11 +0000697 allocated_size = get_allocated_file_size(filename);
698 if (allocated_size < 0)
bellardde167e42005-04-28 21:15:08 +0000699 sprintf(dsize_buf, "unavailable");
700 else
ths5fafdf22007-09-16 21:08:06 +0000701 get_human_readable_size(dsize_buf, sizeof(dsize_buf),
bellardde167e42005-04-28 21:15:08 +0000702 allocated_size);
bellardea2384d2004-08-01 21:59:26 +0000703 printf("image: %s\n"
704 "file format: %s\n"
bellardec3757d2006-06-14 15:50:07 +0000705 "virtual size: %s (%" PRId64 " bytes)\n"
bellardea2384d2004-08-01 21:59:26 +0000706 "disk size: %s\n",
ths5fafdf22007-09-16 21:08:06 +0000707 filename, fmt_name, size_buf,
bellardec3757d2006-06-14 15:50:07 +0000708 (total_sectors * 512),
bellardea2384d2004-08-01 21:59:26 +0000709 dsize_buf);
710 if (bdrv_is_encrypted(bs))
711 printf("encrypted: yes\n");
bellardfaea38e2006-08-05 21:31:00 +0000712 if (bdrv_get_info(bs, &bdi) >= 0) {
ths5fafdf22007-09-16 21:08:06 +0000713 if (bdi.cluster_size != 0)
bellardfaea38e2006-08-05 21:31:00 +0000714 printf("cluster_size: %d\n", bdi.cluster_size);
715 }
bellard93b6b2a2006-08-01 15:51:11 +0000716 bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
bellardfaea38e2006-08-05 21:31:00 +0000717 if (backing_filename[0] != '\0') {
bellard93b6b2a2006-08-01 15:51:11 +0000718 path_combine(backing_filename2, sizeof(backing_filename2),
719 filename, backing_filename);
ths5fafdf22007-09-16 21:08:06 +0000720 printf("backing file: %s (actual path: %s)\n",
bellard93b6b2a2006-08-01 15:51:11 +0000721 backing_filename,
722 backing_filename2);
bellardfaea38e2006-08-05 21:31:00 +0000723 }
724 dump_snapshots(bs);
bellardea2384d2004-08-01 21:59:26 +0000725 bdrv_delete(bs);
726 return 0;
727}
728
729int main(int argc, char **argv)
730{
731 const char *cmd;
732
733 bdrv_init();
734 if (argc < 2)
735 help();
736 cmd = argv[1];
bellarde3888182004-10-09 16:44:06 +0000737 optind++;
bellardea2384d2004-08-01 21:59:26 +0000738 if (!strcmp(cmd, "create")) {
739 img_create(argc, argv);
740 } else if (!strcmp(cmd, "commit")) {
741 img_commit(argc, argv);
742 } else if (!strcmp(cmd, "convert")) {
743 img_convert(argc, argv);
744 } else if (!strcmp(cmd, "info")) {
745 img_info(argc, argv);
746 } else {
747 help();
748 }
749 return 0;
750}