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
24 #include "qemu-common.h"
25 #include "block_int.h"
29 #define WIN32_LEAN_AND_MEAN
33 void *get_mmap_addr(unsigned long size)
38 void qemu_free(void *ptr)
43 void *qemu_malloc(size_t size)
48 void *qemu_mallocz(size_t size)
51 ptr = qemu_malloc(size);
58 char *qemu_strdup(const char *str)
61 ptr = qemu_malloc(strlen(str) + 1);
68 static void __attribute__((noreturn)) error(const char *fmt, ...)
72 fprintf(stderr, "qemu-img: ");
73 vfprintf(stderr, fmt, ap);
74 fprintf(stderr, "\n");
79 static void format_print(void *opaque, const char *name)
84 static void help(void)
86 printf("qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2007 Fabrice Bellard\n"
87 "usage: qemu-img command [command options]\n"
88 "QEMU disk image utility\n"
91 " create [-e] [-6] [-b base_image] [-f fmt] filename [size]\n"
92 " commit [-f fmt] filename\n"
93 " convert [-c] [-e] [-6] [-f fmt] filename [filename2 [...]] [-O output_fmt] output_filename\n"
94 " info [-f fmt] filename\n"
96 "Command parameters:\n"
97 " 'filename' is a disk image filename\n"
98 " 'base_image' is the read-only disk image which is used as base for a copy on\n"
99 " write image; the copy on write image only stores the modified data\n"
100 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
101 " 'size' is the disk image size in kilobytes. Optional suffixes 'M' (megabyte)\n"
102 " and 'G' (gigabyte) are supported\n"
103 " 'output_filename' is the destination disk image filename\n"
104 " 'output_fmt' is the destination format\n"
105 " '-c' indicates that target image must be compressed (qcow format only)\n"
106 " '-e' indicates that the target image must be encrypted (qcow format only)\n"
107 " '-6' indicates that the target image must use compatibility level 6 (vmdk format only)\n"
109 printf("\nSupported format:");
110 bdrv_iterate_format(format_print, NULL);
116 /* XXX: put correct support for win32 */
117 static int read_password(char *buf, int buf_size)
120 printf("Password: ");
127 if (i < (buf_size - 1))
138 static struct termios oldtty;
140 static void term_exit(void)
142 tcsetattr (0, TCSANOW, &oldtty);
145 static void term_init(void)
152 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
153 |INLCR|IGNCR|ICRNL|IXON);
154 tty.c_oflag |= OPOST;
155 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
156 tty.c_cflag &= ~(CSIZE|PARENB);
161 tcsetattr (0, TCSANOW, &tty);
166 static int read_password(char *buf, int buf_size)
171 printf("password: ");
176 ret = read(0, &ch, 1);
178 if (errno == EAGAIN || errno == EINTR) {
184 } else if (ret == 0) {
192 if (i < (buf_size - 1))
203 static BlockDriverState *bdrv_new_open(const char *filename,
206 BlockDriverState *bs;
212 error("Not enough memory");
214 drv = bdrv_find_format(fmt);
216 error("Unknown file format '%s'", fmt);
220 if (bdrv_open2(bs, filename, 0, drv) < 0) {
221 error("Could not open '%s'", filename);
223 if (bdrv_is_encrypted(bs)) {
224 printf("Disk image '%s' is encrypted.\n", filename);
225 if (read_password(password, sizeof(password)) < 0)
226 error("No password given");
227 if (bdrv_set_key(bs, password) < 0)
228 error("invalid password");
233 static int img_create(int argc, char **argv)
236 const char *fmt = "raw";
237 const char *filename;
238 const char *base_filename = NULL;
245 c = getopt(argc, argv, "b:f:he6");
253 base_filename = optarg;
259 flags |= BLOCK_FLAG_ENCRYPT;
262 flags |= BLOCK_FLAG_COMPAT6;
268 filename = argv[optind++];
271 BlockDriverState *bs;
272 bs = bdrv_new_open(base_filename, NULL);
273 bdrv_get_geometry(bs, &size);
280 size = strtoul(p, (char **)&p, 0);
283 } else if (*p == 'G') {
284 size *= 1024 * 1024 * 1024;
285 } else if (*p == 'k' || *p == 'K' || *p == '\0') {
291 drv = bdrv_find_format(fmt);
293 error("Unknown file format '%s'", fmt);
294 printf("Formatting '%s', fmt=%s",
296 if (flags & BLOCK_FLAG_ENCRYPT)
297 printf(", encrypted");
298 if (flags & BLOCK_FLAG_COMPAT6)
299 printf(", compatibility level=6");
301 printf(", backing_file=%s",
304 printf(", size=%" PRIu64 " kB\n", size / 1024);
305 ret = bdrv_create(drv, filename, size / 512, base_filename, flags);
307 if (ret == -ENOTSUP) {
308 error("Formatting or formatting option not supported for file format '%s'", fmt);
310 error("Error while formatting");
316 static int img_commit(int argc, char **argv)
319 const char *filename, *fmt;
321 BlockDriverState *bs;
325 c = getopt(argc, argv, "f:h");
339 filename = argv[optind++];
343 error("Not enough memory");
345 drv = bdrv_find_format(fmt);
347 error("Unknown file format '%s'", fmt);
351 if (bdrv_open2(bs, filename, 0, drv) < 0) {
352 error("Could not open '%s'", filename);
354 ret = bdrv_commit(bs);
357 printf("Image committed.\n");
360 error("No disk inserted");
363 error("Image is read-only");
366 error("Image is already committed");
369 error("Error while committing image");
377 static int is_not_zero(const uint8_t *sector, int len)
381 for(i = 0;i < len; i++) {
382 if (((uint32_t *)sector)[i] != 0)
388 static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
396 v = is_not_zero(buf, 512);
397 for(i = 1; i < n; i++) {
399 if (v != is_not_zero(buf, 512))
406 #define IO_BUF_SIZE 65536
408 static int img_convert(int argc, char **argv)
410 int c, ret, n, n1, bs_n, bs_i, flags, cluster_size, cluster_sectors;
411 const char *fmt, *out_fmt, *out_filename;
413 BlockDriverState **bs, *out_bs;
414 int64_t total_sectors, nb_sectors, sector_num, bs_offset;
416 uint8_t buf[IO_BUF_SIZE];
424 c = getopt(argc, argv, "f:O:hce6");
438 flags |= BLOCK_FLAG_COMPRESS;
441 flags |= BLOCK_FLAG_ENCRYPT;
444 flags |= BLOCK_FLAG_COMPAT6;
449 bs_n = argc - optind - 1;
450 if (bs_n < 1) help();
452 out_filename = argv[argc - 1];
454 bs = calloc(bs_n, sizeof(BlockDriverState *));
456 error("Out of memory");
459 for (bs_i = 0; bs_i < bs_n; bs_i++) {
460 bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt);
462 error("Could not open '%s'", argv[optind + bs_i]);
463 bdrv_get_geometry(bs[bs_i], &bs_sectors);
464 total_sectors += bs_sectors;
467 drv = bdrv_find_format(out_fmt);
469 error("Unknown file format '%s'", out_fmt);
470 if (flags & BLOCK_FLAG_COMPRESS && drv != &bdrv_qcow && drv != &bdrv_qcow2)
471 error("Compression not supported for this file format");
472 if (flags & BLOCK_FLAG_ENCRYPT && drv != &bdrv_qcow && drv != &bdrv_qcow2)
473 error("Encryption not supported for this file format");
474 if (flags & BLOCK_FLAG_COMPAT6 && drv != &bdrv_vmdk)
475 error("Alternative compatibility level not supported for this file format");
476 if (flags & BLOCK_FLAG_ENCRYPT && flags & BLOCK_FLAG_COMPRESS)
477 error("Compression and encryption not supported at the same time");
479 ret = bdrv_create(drv, out_filename, total_sectors, NULL, flags);
481 if (ret == -ENOTSUP) {
482 error("Formatting not supported for file format '%s'", fmt);
484 error("Error while formatting '%s'", out_filename);
488 out_bs = bdrv_new_open(out_filename, out_fmt);
492 bdrv_get_geometry(bs[0], &bs_sectors);
494 if (flags & BLOCK_FLAG_COMPRESS) {
495 if (bdrv_get_info(out_bs, &bdi) < 0)
496 error("could not get block driver info");
497 cluster_size = bdi.cluster_size;
498 if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE)
499 error("invalid cluster size");
500 cluster_sectors = cluster_size >> 9;
507 nb_sectors = total_sectors - sector_num;
510 if (nb_sectors >= cluster_sectors)
515 bs_num = sector_num - bs_offset;
516 assert (bs_num >= 0);
519 while (remainder > 0) {
521 while (bs_num == bs_sectors) {
523 assert (bs_i < bs_n);
524 bs_offset += bs_sectors;
525 bdrv_get_geometry(bs[bs_i], &bs_sectors);
527 /* printf("changing part: sector_num=%lld, "
528 "bs_i=%d, bs_offset=%lld, bs_sectors=%lld\n",
529 sector_num, bs_i, bs_offset, bs_sectors); */
531 assert (bs_num < bs_sectors);
533 nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder;
535 if (bdrv_read(bs[bs_i], bs_num, buf2, nlow) < 0)
536 error("error while reading");
543 assert (remainder == 0);
545 if (n < cluster_sectors)
546 memset(buf + n * 512, 0, cluster_size - n * 512);
547 if (is_not_zero(buf, cluster_size)) {
548 if (bdrv_write_compressed(out_bs, sector_num, buf,
549 cluster_sectors) != 0)
550 error("error while compressing sector %" PRId64,
555 /* signal EOF to align */
556 bdrv_write_compressed(out_bs, 0, NULL, 0);
560 nb_sectors = total_sectors - sector_num;
563 if (nb_sectors >= (IO_BUF_SIZE / 512))
564 n = (IO_BUF_SIZE / 512);
568 while (sector_num - bs_offset >= bs_sectors) {
570 assert (bs_i < bs_n);
571 bs_offset += bs_sectors;
572 bdrv_get_geometry(bs[bs_i], &bs_sectors);
573 /* printf("changing part: sector_num=%lld, bs_i=%d, "
574 "bs_offset=%lld, bs_sectors=%lld\n",
575 sector_num, bs_i, bs_offset, bs_sectors); */
578 if (n > bs_offset + bs_sectors - sector_num)
579 n = bs_offset + bs_sectors - sector_num;
581 if (bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n) < 0)
582 error("error while reading");
583 /* NOTE: at the same time we convert, we do not write zero
584 sectors to have a chance to compress the image. Ideally, we
585 should add a specific call to have the info to go faster */
588 if (is_allocated_sectors(buf1, n, &n1)) {
589 if (bdrv_write(out_bs, sector_num, buf1, n1) < 0)
590 error("error while writing");
599 for (bs_i = 0; bs_i < bs_n; bs_i++)
600 bdrv_delete(bs[bs_i]);
606 static int64_t get_allocated_file_size(const char *filename)
608 typedef DWORD (WINAPI * get_compressed_t)(const char *filename, DWORD *high);
609 get_compressed_t get_compressed;
612 /* WinNT support GetCompressedFileSize to determine allocate size */
613 get_compressed = (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"), "GetCompressedFileSizeA");
614 if (get_compressed) {
616 low = get_compressed(filename, &high);
617 if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR)
618 return (((int64_t) high) << 32) + low;
621 if (_stati64(filename, &st) < 0)
626 static int64_t get_allocated_file_size(const char *filename)
629 if (stat(filename, &st) < 0)
631 return (int64_t)st.st_blocks * 512;
635 static void dump_snapshots(BlockDriverState *bs)
637 QEMUSnapshotInfo *sn_tab, *sn;
641 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
644 printf("Snapshot list:\n");
645 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
646 for(i = 0; i < nb_sns; i++) {
648 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
653 static int img_info(int argc, char **argv)
656 const char *filename, *fmt;
658 BlockDriverState *bs;
659 char fmt_name[128], size_buf[128], dsize_buf[128];
660 uint64_t total_sectors;
661 int64_t allocated_size;
662 char backing_filename[1024];
663 char backing_filename2[1024];
668 c = getopt(argc, argv, "f:h");
682 filename = argv[optind++];
686 error("Not enough memory");
688 drv = bdrv_find_format(fmt);
690 error("Unknown file format '%s'", fmt);
694 if (bdrv_open2(bs, filename, 0, drv) < 0) {
695 error("Could not open '%s'", filename);
697 bdrv_get_format(bs, fmt_name, sizeof(fmt_name));
698 bdrv_get_geometry(bs, &total_sectors);
699 get_human_readable_size(size_buf, sizeof(size_buf), total_sectors * 512);
700 allocated_size = get_allocated_file_size(filename);
701 if (allocated_size < 0)
702 sprintf(dsize_buf, "unavailable");
704 get_human_readable_size(dsize_buf, sizeof(dsize_buf),
708 "virtual size: %s (%" PRId64 " bytes)\n"
710 filename, fmt_name, size_buf,
711 (total_sectors * 512),
713 if (bdrv_is_encrypted(bs))
714 printf("encrypted: yes\n");
715 if (bdrv_get_info(bs, &bdi) >= 0) {
716 if (bdi.cluster_size != 0)
717 printf("cluster_size: %d\n", bdi.cluster_size);
719 bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
720 if (backing_filename[0] != '\0') {
721 path_combine(backing_filename2, sizeof(backing_filename2),
722 filename, backing_filename);
723 printf("backing file: %s (actual path: %s)\n",
732 int main(int argc, char **argv)
741 if (!strcmp(cmd, "create")) {
742 img_create(argc, argv);
743 } else if (!strcmp(cmd, "commit")) {
744 img_commit(argc, argv);
745 } else if (!strcmp(cmd, "convert")) {
746 img_convert(argc, argv);
747 } else if (!strcmp(cmd, "info")) {
748 img_info(argc, argv);