2 * QEMU disk image utility
4 * Copyright (c) 2003-2007 Fabrice Bellard
6 * 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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "block_int.h"
32 void *get_mmap_addr(unsigned long size)
37 void qemu_free(void *ptr)
42 void *qemu_malloc(size_t size)
47 void *qemu_mallocz(size_t size)
50 ptr = qemu_malloc(size);
57 char *qemu_strdup(const char *str)
60 ptr = qemu_malloc(strlen(str) + 1);
67 void term_printf(const char *fmt, ...)
75 void term_print_filename(const char *filename)
77 term_printf(filename);
80 void __attribute__((noreturn)) error(const char *fmt, ...)
84 fprintf(stderr, "qemu-img: ");
85 vfprintf(stderr, fmt, ap);
86 fprintf(stderr, "\n");
91 static void format_print(void *opaque, const char *name)
98 printf("qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2007 Fabrice Bellard\n"
99 "usage: qemu-img command [command options]\n"
100 "QEMU disk image utility\n"
103 " create [-e] [-6] [-b base_image] [-f fmt] filename [size]\n"
104 " commit [-f fmt] filename\n"
105 " convert [-c] [-e] [-6] [-f fmt] filename [filename2 [...]] [-O output_fmt] output_filename\n"
106 " info [-f fmt] filename\n"
108 "Command parameters:\n"
109 " 'filename' is a disk image filename\n"
110 " 'base_image' is the read-only disk image which is used as base for a copy on\n"
111 " write image; the copy on write image only stores the modified data\n"
112 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
113 " 'size' is the disk image size in kilobytes. Optional suffixes 'M' (megabyte)\n"
114 " and 'G' (gigabyte) are supported\n"
115 " 'output_filename' is the destination disk image filename\n"
116 " 'output_fmt' is the destination format\n"
117 " '-c' indicates that target image must be compressed (qcow format only)\n"
118 " '-e' indicates that the target image must be encrypted (qcow format only)\n"
119 " '-6' indicates that the target image must use compatibility level 6 (vmdk format only)\n"
121 printf("\nSupported format:");
122 bdrv_iterate_format(format_print, NULL);
128 /* XXX: put correct support for win32 */
129 static int read_password(char *buf, int buf_size)
132 printf("Password: ");
139 if (i < (buf_size - 1))
150 static struct termios oldtty;
152 static void term_exit(void)
154 tcsetattr (0, TCSANOW, &oldtty);
157 static void term_init(void)
164 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
165 |INLCR|IGNCR|ICRNL|IXON);
166 tty.c_oflag |= OPOST;
167 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
168 tty.c_cflag &= ~(CSIZE|PARENB);
173 tcsetattr (0, TCSANOW, &tty);
178 int read_password(char *buf, int buf_size)
183 printf("password: ");
188 ret = read(0, &ch, 1);
190 if (errno == EAGAIN || errno == EINTR) {
196 } else if (ret == 0) {
204 if (i < (buf_size - 1))
215 static BlockDriverState *bdrv_new_open(const char *filename,
218 BlockDriverState *bs;
224 error("Not enough memory");
226 drv = bdrv_find_format(fmt);
228 error("Unknown file format '%s'", fmt);
232 if (bdrv_open2(bs, filename, 0, drv) < 0) {
233 error("Could not open '%s'", filename);
235 if (bdrv_is_encrypted(bs)) {
236 printf("Disk image '%s' is encrypted.\n", filename);
237 if (read_password(password, sizeof(password)) < 0)
238 error("No password given");
239 if (bdrv_set_key(bs, password) < 0)
240 error("invalid password");
245 static int img_create(int argc, char **argv)
248 const char *fmt = "raw";
249 const char *filename;
250 const char *base_filename = NULL;
257 c = getopt(argc, argv, "b:f:he6");
265 base_filename = optarg;
271 flags |= BLOCK_FLAG_ENCRYPT;
274 flags |= BLOCK_FLAG_COMPAT6;
280 filename = argv[optind++];
283 BlockDriverState *bs;
284 bs = bdrv_new_open(base_filename, NULL);
285 bdrv_get_geometry(bs, &size);
292 size = strtoul(p, (char **)&p, 0);
295 } else if (*p == 'G') {
296 size *= 1024 * 1024 * 1024;
297 } else if (*p == 'k' || *p == 'K' || *p == '\0') {
303 drv = bdrv_find_format(fmt);
305 error("Unknown file format '%s'", fmt);
306 printf("Formatting '%s', fmt=%s",
308 if (flags & BLOCK_FLAG_ENCRYPT)
309 printf(", encrypted");
310 if (flags & BLOCK_FLAG_COMPAT6)
311 printf(", compatibility level=6");
313 printf(", backing_file=%s",
316 printf(", size=%" PRId64 " kB\n", (int64_t) (size / 1024));
317 ret = bdrv_create(drv, filename, size / 512, base_filename, flags);
319 if (ret == -ENOTSUP) {
320 error("Formatting or formatting option not supported for file format '%s'", fmt);
322 error("Error while formatting");
328 static int img_commit(int argc, char **argv)
331 const char *filename, *fmt;
333 BlockDriverState *bs;
337 c = getopt(argc, argv, "f:h");
351 filename = argv[optind++];
355 error("Not enough memory");
357 drv = bdrv_find_format(fmt);
359 error("Unknown file format '%s'", fmt);
363 if (bdrv_open2(bs, filename, 0, drv) < 0) {
364 error("Could not open '%s'", filename);
366 ret = bdrv_commit(bs);
369 printf("Image committed.\n");
372 error("No disk inserted");
375 error("Image is read-only");
378 error("Image is already committed");
381 error("Error while committing image");
389 static int is_not_zero(const uint8_t *sector, int len)
393 for(i = 0;i < len; i++) {
394 if (((uint32_t *)sector)[i] != 0)
400 static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
408 v = is_not_zero(buf, 512);
409 for(i = 1; i < n; i++) {
411 if (v != is_not_zero(buf, 512))
418 #define IO_BUF_SIZE 65536
420 static int img_convert(int argc, char **argv)
422 int c, ret, n, n1, bs_n, bs_i, flags, cluster_size, cluster_sectors;
423 const char *fmt, *out_fmt, *out_filename;
425 BlockDriverState **bs, *out_bs;
426 int64_t total_sectors, nb_sectors, sector_num, bs_offset, bs_sectors;
427 uint8_t buf[IO_BUF_SIZE];
435 c = getopt(argc, argv, "f:O:hce6");
449 flags |= BLOCK_FLAG_COMPRESS;
452 flags |= BLOCK_FLAG_ENCRYPT;
455 flags |= BLOCK_FLAG_COMPAT6;
460 bs_n = argc - optind - 1;
461 if (bs_n < 1) help();
463 out_filename = argv[argc - 1];
465 bs = calloc(bs_n, sizeof(BlockDriverState *));
467 error("Out of memory");
470 for (bs_i = 0; bs_i < bs_n; bs_i++) {
471 bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt);
473 error("Could not open '%s'", argv[optind + bs_i]);
474 bdrv_get_geometry(bs[bs_i], &bs_sectors);
475 total_sectors += bs_sectors;
478 drv = bdrv_find_format(out_fmt);
480 error("Unknown file format '%s'", out_fmt);
481 if (flags & BLOCK_FLAG_COMPRESS && drv != &bdrv_qcow && drv != &bdrv_qcow2)
482 error("Compression not supported for this file format");
483 if (flags & BLOCK_FLAG_ENCRYPT && drv != &bdrv_qcow && drv != &bdrv_qcow2)
484 error("Encryption not supported for this file format");
485 if (flags & BLOCK_FLAG_COMPAT6 && drv != &bdrv_vmdk)
486 error("Alternative compatibility level not supported for this file format");
487 if (flags & BLOCK_FLAG_ENCRYPT && flags & BLOCK_FLAG_COMPRESS)
488 error("Compression and encryption not supported at the same time");
490 ret = bdrv_create(drv, out_filename, total_sectors, NULL, flags);
492 if (ret == -ENOTSUP) {
493 error("Formatting not supported for file format '%s'", fmt);
495 error("Error while formatting '%s'", out_filename);
499 out_bs = bdrv_new_open(out_filename, out_fmt);
503 bdrv_get_geometry(bs[0], &bs_sectors);
505 if (flags & BLOCK_FLAG_COMPRESS) {
506 if (bdrv_get_info(out_bs, &bdi) < 0)
507 error("could not get block driver info");
508 cluster_size = bdi.cluster_size;
509 if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE)
510 error("invalid cluster size");
511 cluster_sectors = cluster_size >> 9;
518 nb_sectors = total_sectors - sector_num;
521 if (nb_sectors >= cluster_sectors)
526 bs_num = sector_num - bs_offset;
527 assert (bs_num >= 0);
530 while (remainder > 0) {
532 while (bs_num == bs_sectors) {
534 assert (bs_i < bs_n);
535 bs_offset += bs_sectors;
536 bdrv_get_geometry(bs[bs_i], &bs_sectors);
538 /* printf("changing part: sector_num=%lld, "
539 "bs_i=%d, bs_offset=%lld, bs_sectors=%lld\n",
540 sector_num, bs_i, bs_offset, bs_sectors); */
542 assert (bs_num < bs_sectors);
544 nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder;
546 if (bdrv_read(bs[bs_i], bs_num, buf2, nlow) < 0)
547 error("error while reading");
554 assert (remainder == 0);
556 if (n < cluster_sectors)
557 memset(buf + n * 512, 0, cluster_size - n * 512);
558 if (is_not_zero(buf, cluster_size)) {
559 if (bdrv_write_compressed(out_bs, sector_num, buf,
560 cluster_sectors) != 0)
561 error("error while compressing sector %" PRId64,
566 /* signal EOF to align */
567 bdrv_write_compressed(out_bs, 0, NULL, 0);
571 nb_sectors = total_sectors - sector_num;
574 if (nb_sectors >= (IO_BUF_SIZE / 512))
575 n = (IO_BUF_SIZE / 512);
579 while (sector_num - bs_offset >= bs_sectors) {
581 assert (bs_i < bs_n);
582 bs_offset += bs_sectors;
583 bdrv_get_geometry(bs[bs_i], &bs_sectors);
584 /* printf("changing part: sector_num=%lld, bs_i=%d, "
585 "bs_offset=%lld, bs_sectors=%lld\n",
586 sector_num, bs_i, bs_offset, bs_sectors); */
589 if (n > bs_offset + bs_sectors - sector_num)
590 n = bs_offset + bs_sectors - sector_num;
592 if (bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n) < 0)
593 error("error while reading");
594 /* NOTE: at the same time we convert, we do not write zero
595 sectors to have a chance to compress the image. Ideally, we
596 should add a specific call to have the info to go faster */
599 if (is_allocated_sectors(buf1, n, &n1)) {
600 if (bdrv_write(out_bs, sector_num, buf1, n1) < 0)
601 error("error while writing");
610 for (bs_i = 0; bs_i < bs_n; bs_i++)
611 bdrv_delete(bs[bs_i]);
617 static int64_t get_allocated_file_size(const char *filename)
619 typedef DWORD (WINAPI * get_compressed_t)(const char *filename, DWORD *high);
620 get_compressed_t get_compressed;
623 /* WinNT support GetCompressedFileSize to determine allocate size */
624 get_compressed = (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"), "GetCompressedFileSizeA");
625 if (get_compressed) {
627 low = get_compressed(filename, &high);
628 if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR)
629 return (((int64_t) high) << 32) + low;
632 if (_stati64(filename, &st) < 0)
637 static int64_t get_allocated_file_size(const char *filename)
640 if (stat(filename, &st) < 0)
642 return (int64_t)st.st_blocks * 512;
646 static void dump_snapshots(BlockDriverState *bs)
648 QEMUSnapshotInfo *sn_tab, *sn;
652 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
655 printf("Snapshot list:\n");
656 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
657 for(i = 0; i < nb_sns; i++) {
659 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
664 static int img_info(int argc, char **argv)
667 const char *filename, *fmt;
669 BlockDriverState *bs;
670 char fmt_name[128], size_buf[128], dsize_buf[128];
671 int64_t total_sectors, allocated_size;
672 char backing_filename[1024];
673 char backing_filename2[1024];
678 c = getopt(argc, argv, "f:h");
692 filename = argv[optind++];
696 error("Not enough memory");
698 drv = bdrv_find_format(fmt);
700 error("Unknown file format '%s'", fmt);
704 if (bdrv_open2(bs, filename, 0, drv) < 0) {
705 error("Could not open '%s'", filename);
707 bdrv_get_format(bs, fmt_name, sizeof(fmt_name));
708 bdrv_get_geometry(bs, &total_sectors);
709 get_human_readable_size(size_buf, sizeof(size_buf), total_sectors * 512);
710 allocated_size = get_allocated_file_size(filename);
711 if (allocated_size < 0)
712 sprintf(dsize_buf, "unavailable");
714 get_human_readable_size(dsize_buf, sizeof(dsize_buf),
718 "virtual size: %s (%" PRId64 " bytes)\n"
720 filename, fmt_name, size_buf,
721 (total_sectors * 512),
723 if (bdrv_is_encrypted(bs))
724 printf("encrypted: yes\n");
725 if (bdrv_get_info(bs, &bdi) >= 0) {
726 if (bdi.cluster_size != 0)
727 printf("cluster_size: %d\n", bdi.cluster_size);
729 bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
730 if (backing_filename[0] != '\0') {
731 path_combine(backing_filename2, sizeof(backing_filename2),
732 filename, backing_filename);
733 printf("backing file: %s (actual path: %s)\n",
742 int main(int argc, char **argv)
751 if (!strcmp(cmd, "create")) {
752 img_create(argc, argv);
753 } else if (!strcmp(cmd, "commit")) {
754 img_commit(argc, argv);
755 } else if (!strcmp(cmd, "convert")) {
756 img_convert(argc, argv);
757 } else if (!strcmp(cmd, "info")) {
758 img_info(argc, argv);