2 * QEMU disk image utility
4 * Copyright (c) 2003-2008 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 static void __attribute__((noreturn)) error(const char *fmt, ...)
37 fprintf(stderr, "qemu-img: ");
38 vfprintf(stderr, fmt, ap);
39 fprintf(stderr, "\n");
44 static void format_print(void *opaque, const char *name)
49 static void help(void)
51 printf("qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2008 Fabrice Bellard\n"
52 "usage: qemu-img command [command options]\n"
53 "QEMU disk image utility\n"
56 " create [-e] [-6] [-b base_image] [-f fmt] filename [size]\n"
57 " commit [-f fmt] filename\n"
58 " convert [-c] [-e] [-6] [-f fmt] [-O output_fmt] filename [filename2 [...]] output_filename\n"
59 " info [-f fmt] filename\n"
61 "Command parameters:\n"
62 " 'filename' is a disk image filename\n"
63 " 'base_image' is the read-only disk image which is used as base for a copy on\n"
64 " write image; the copy on write image only stores the modified data\n"
65 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
66 " 'size' is the disk image size in kilobytes. Optional suffixes 'M' (megabyte)\n"
67 " and 'G' (gigabyte) are supported\n"
68 " 'output_filename' is the destination disk image filename\n"
69 " 'output_fmt' is the destination format\n"
70 " '-c' indicates that target image must be compressed (qcow format only)\n"
71 " '-e' indicates that the target image must be encrypted (qcow format only)\n"
72 " '-6' indicates that the target image must use compatibility level 6 (vmdk format only)\n"
74 printf("\nSupported format:");
75 bdrv_iterate_format(format_print, NULL);
81 /* XXX: put correct support for win32 */
82 static int read_password(char *buf, int buf_size)
92 if (i < (buf_size - 1))
103 static struct termios oldtty;
105 static void term_exit(void)
107 tcsetattr (0, TCSANOW, &oldtty);
110 static void term_init(void)
117 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
118 |INLCR|IGNCR|ICRNL|IXON);
119 tty.c_oflag |= OPOST;
120 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
121 tty.c_cflag &= ~(CSIZE|PARENB);
126 tcsetattr (0, TCSANOW, &tty);
131 static int read_password(char *buf, int buf_size)
136 printf("password: ");
141 ret = read(0, &ch, 1);
143 if (errno == EAGAIN || errno == EINTR) {
149 } else if (ret == 0) {
157 if (i < (buf_size - 1))
168 static BlockDriverState *bdrv_new_open(const char *filename,
171 BlockDriverState *bs;
177 error("Not enough memory");
179 drv = bdrv_find_format(fmt);
181 error("Unknown file format '%s'", fmt);
185 if (bdrv_open2(bs, filename, 0, drv) < 0) {
186 error("Could not open '%s'", filename);
188 if (bdrv_is_encrypted(bs)) {
189 printf("Disk image '%s' is encrypted.\n", filename);
190 if (read_password(password, sizeof(password)) < 0)
191 error("No password given");
192 if (bdrv_set_key(bs, password) < 0)
193 error("invalid password");
198 static int img_create(int argc, char **argv)
201 const char *fmt = "raw";
202 const char *filename;
203 const char *base_filename = NULL;
210 c = getopt(argc, argv, "b:f:he6");
218 base_filename = optarg;
224 flags |= BLOCK_FLAG_ENCRYPT;
227 flags |= BLOCK_FLAG_COMPAT6;
233 filename = argv[optind++];
236 BlockDriverState *bs;
237 bs = bdrv_new_open(base_filename, NULL);
238 bdrv_get_geometry(bs, &size);
245 size = strtoul(p, (char **)&p, 0);
248 } else if (*p == 'G') {
249 size *= 1024 * 1024 * 1024;
250 } else if (*p == 'k' || *p == 'K' || *p == '\0') {
256 drv = bdrv_find_format(fmt);
258 error("Unknown file format '%s'", fmt);
259 printf("Formatting '%s', fmt=%s",
261 if (flags & BLOCK_FLAG_ENCRYPT)
262 printf(", encrypted");
263 if (flags & BLOCK_FLAG_COMPAT6)
264 printf(", compatibility level=6");
266 printf(", backing_file=%s",
269 printf(", size=%" PRIu64 " kB\n", size / 1024);
270 ret = bdrv_create(drv, filename, size / 512, base_filename, flags);
272 if (ret == -ENOTSUP) {
273 error("Formatting or formatting option not supported for file format '%s'", fmt);
275 error("Error while formatting");
281 static int img_commit(int argc, char **argv)
284 const char *filename, *fmt;
286 BlockDriverState *bs;
290 c = getopt(argc, argv, "f:h");
304 filename = argv[optind++];
308 error("Not enough memory");
310 drv = bdrv_find_format(fmt);
312 error("Unknown file format '%s'", fmt);
316 if (bdrv_open2(bs, filename, 0, drv) < 0) {
317 error("Could not open '%s'", filename);
319 ret = bdrv_commit(bs);
322 printf("Image committed.\n");
325 error("No disk inserted");
328 error("Image is read-only");
331 error("Image is already committed");
334 error("Error while committing image");
342 static int is_not_zero(const uint8_t *sector, int len)
346 for(i = 0;i < len; i++) {
347 if (((uint32_t *)sector)[i] != 0)
353 static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
361 v = is_not_zero(buf, 512);
362 for(i = 1; i < n; i++) {
364 if (v != is_not_zero(buf, 512))
371 #define IO_BUF_SIZE 65536
373 static int img_convert(int argc, char **argv)
375 int c, ret, n, n1, bs_n, bs_i, flags, cluster_size, cluster_sectors;
376 const char *fmt, *out_fmt, *out_filename;
378 BlockDriverState **bs, *out_bs;
379 int64_t total_sectors, nb_sectors, sector_num, bs_offset;
381 uint8_t buf[IO_BUF_SIZE];
389 c = getopt(argc, argv, "f:O:hce6");
403 flags |= BLOCK_FLAG_COMPRESS;
406 flags |= BLOCK_FLAG_ENCRYPT;
409 flags |= BLOCK_FLAG_COMPAT6;
414 bs_n = argc - optind - 1;
415 if (bs_n < 1) help();
417 out_filename = argv[argc - 1];
419 bs = calloc(bs_n, sizeof(BlockDriverState *));
421 error("Out of memory");
424 for (bs_i = 0; bs_i < bs_n; bs_i++) {
425 bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt);
427 error("Could not open '%s'", argv[optind + bs_i]);
428 bdrv_get_geometry(bs[bs_i], &bs_sectors);
429 total_sectors += bs_sectors;
432 drv = bdrv_find_format(out_fmt);
434 error("Unknown file format '%s'", out_fmt);
435 if (flags & BLOCK_FLAG_COMPRESS && drv != &bdrv_qcow && drv != &bdrv_qcow2)
436 error("Compression not supported for this file format");
437 if (flags & BLOCK_FLAG_ENCRYPT && drv != &bdrv_qcow && drv != &bdrv_qcow2)
438 error("Encryption not supported for this file format");
439 if (flags & BLOCK_FLAG_COMPAT6 && drv != &bdrv_vmdk)
440 error("Alternative compatibility level not supported for this file format");
441 if (flags & BLOCK_FLAG_ENCRYPT && flags & BLOCK_FLAG_COMPRESS)
442 error("Compression and encryption not supported at the same time");
444 ret = bdrv_create(drv, out_filename, total_sectors, NULL, flags);
446 if (ret == -ENOTSUP) {
447 error("Formatting not supported for file format '%s'", fmt);
449 error("Error while formatting '%s'", out_filename);
453 out_bs = bdrv_new_open(out_filename, out_fmt);
457 bdrv_get_geometry(bs[0], &bs_sectors);
459 if (flags & BLOCK_FLAG_COMPRESS) {
460 if (bdrv_get_info(out_bs, &bdi) < 0)
461 error("could not get block driver info");
462 cluster_size = bdi.cluster_size;
463 if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE)
464 error("invalid cluster size");
465 cluster_sectors = cluster_size >> 9;
472 nb_sectors = total_sectors - sector_num;
475 if (nb_sectors >= cluster_sectors)
480 bs_num = sector_num - bs_offset;
481 assert (bs_num >= 0);
484 while (remainder > 0) {
486 while (bs_num == bs_sectors) {
488 assert (bs_i < bs_n);
489 bs_offset += bs_sectors;
490 bdrv_get_geometry(bs[bs_i], &bs_sectors);
492 /* printf("changing part: sector_num=%lld, "
493 "bs_i=%d, bs_offset=%lld, bs_sectors=%lld\n",
494 sector_num, bs_i, bs_offset, bs_sectors); */
496 assert (bs_num < bs_sectors);
498 nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder;
500 if (bdrv_read(bs[bs_i], bs_num, buf2, nlow) < 0)
501 error("error while reading");
508 assert (remainder == 0);
510 if (n < cluster_sectors)
511 memset(buf + n * 512, 0, cluster_size - n * 512);
512 if (is_not_zero(buf, cluster_size)) {
513 if (bdrv_write_compressed(out_bs, sector_num, buf,
514 cluster_sectors) != 0)
515 error("error while compressing sector %" PRId64,
520 /* signal EOF to align */
521 bdrv_write_compressed(out_bs, 0, NULL, 0);
525 nb_sectors = total_sectors - sector_num;
528 if (nb_sectors >= (IO_BUF_SIZE / 512))
529 n = (IO_BUF_SIZE / 512);
533 while (sector_num - bs_offset >= bs_sectors) {
535 assert (bs_i < bs_n);
536 bs_offset += bs_sectors;
537 bdrv_get_geometry(bs[bs_i], &bs_sectors);
538 /* printf("changing part: sector_num=%lld, bs_i=%d, "
539 "bs_offset=%lld, bs_sectors=%lld\n",
540 sector_num, bs_i, bs_offset, bs_sectors); */
543 if (n > bs_offset + bs_sectors - sector_num)
544 n = bs_offset + bs_sectors - sector_num;
546 if (bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n) < 0)
547 error("error while reading");
548 /* NOTE: at the same time we convert, we do not write zero
549 sectors to have a chance to compress the image. Ideally, we
550 should add a specific call to have the info to go faster */
553 if (is_allocated_sectors(buf1, n, &n1)) {
554 if (bdrv_write(out_bs, sector_num, buf1, n1) < 0)
555 error("error while writing");
564 for (bs_i = 0; bs_i < bs_n; bs_i++)
565 bdrv_delete(bs[bs_i]);
571 static int64_t get_allocated_file_size(const char *filename)
573 typedef DWORD (WINAPI * get_compressed_t)(const char *filename, DWORD *high);
574 get_compressed_t get_compressed;
577 /* WinNT support GetCompressedFileSize to determine allocate size */
578 get_compressed = (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"), "GetCompressedFileSizeA");
579 if (get_compressed) {
581 low = get_compressed(filename, &high);
582 if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR)
583 return (((int64_t) high) << 32) + low;
586 if (_stati64(filename, &st) < 0)
591 static int64_t get_allocated_file_size(const char *filename)
594 if (stat(filename, &st) < 0)
596 return (int64_t)st.st_blocks * 512;
600 static void dump_snapshots(BlockDriverState *bs)
602 QEMUSnapshotInfo *sn_tab, *sn;
606 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
609 printf("Snapshot list:\n");
610 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
611 for(i = 0; i < nb_sns; i++) {
613 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
618 static int img_info(int argc, char **argv)
621 const char *filename, *fmt;
623 BlockDriverState *bs;
624 char fmt_name[128], size_buf[128], dsize_buf[128];
625 uint64_t total_sectors;
626 int64_t allocated_size;
627 char backing_filename[1024];
628 char backing_filename2[1024];
633 c = getopt(argc, argv, "f:h");
647 filename = argv[optind++];
651 error("Not enough memory");
653 drv = bdrv_find_format(fmt);
655 error("Unknown file format '%s'", fmt);
659 if (bdrv_open2(bs, filename, 0, drv) < 0) {
660 error("Could not open '%s'", filename);
662 bdrv_get_format(bs, fmt_name, sizeof(fmt_name));
663 bdrv_get_geometry(bs, &total_sectors);
664 get_human_readable_size(size_buf, sizeof(size_buf), total_sectors * 512);
665 allocated_size = get_allocated_file_size(filename);
666 if (allocated_size < 0)
667 sprintf(dsize_buf, "unavailable");
669 get_human_readable_size(dsize_buf, sizeof(dsize_buf),
673 "virtual size: %s (%" PRId64 " bytes)\n"
675 filename, fmt_name, size_buf,
676 (total_sectors * 512),
678 if (bdrv_is_encrypted(bs))
679 printf("encrypted: yes\n");
680 if (bdrv_get_info(bs, &bdi) >= 0) {
681 if (bdi.cluster_size != 0)
682 printf("cluster_size: %d\n", bdi.cluster_size);
684 bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
685 if (backing_filename[0] != '\0') {
686 path_combine(backing_filename2, sizeof(backing_filename2),
687 filename, backing_filename);
688 printf("backing file: %s (actual path: %s)\n",
697 int main(int argc, char **argv)
706 if (!strcmp(cmd, "create")) {
707 img_create(argc, argv);
708 } else if (!strcmp(cmd, "commit")) {
709 img_commit(argc, argv);
710 } else if (!strcmp(cmd, "convert")) {
711 img_convert(argc, argv);
712 } else if (!strcmp(cmd, "info")) {
713 img_info(argc, argv);