2 * QEMU disk image utility
4 * Copyright (c) 2003-2006 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
30 void *get_mmap_addr(unsigned long size)
35 void qemu_free(void *ptr)
40 void *qemu_malloc(size_t size)
45 void *qemu_mallocz(size_t size)
48 ptr = qemu_malloc(size);
55 char *qemu_strdup(const char *str)
58 ptr = qemu_malloc(strlen(str) + 1);
65 void pstrcpy(char *buf, int buf_size, const char *str)
75 if (c == 0 || q >= buf + buf_size - 1)
82 /* strcat and truncate. */
83 char *pstrcat(char *buf, int buf_size, const char *s)
88 pstrcpy(buf + len, buf_size - len, s);
92 int strstart(const char *str, const char *val, const char **ptr)
108 void term_printf(const char *fmt, ...)
116 void term_print_filename(const char *filename)
118 term_printf(filename);
121 void __attribute__((noreturn)) error(const char *fmt, ...)
125 fprintf(stderr, "qemu-img: ");
126 vfprintf(stderr, fmt, ap);
127 fprintf(stderr, "\n");
132 static void format_print(void *opaque, const char *name)
139 printf("qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2006 Fabrice Bellard\n"
140 "usage: qemu-img command [command options]\n"
141 "QEMU disk image utility\n"
144 " create [-e] [-b base_image] [-f fmt] filename [size]\n"
145 " commit [-f fmt] filename\n"
146 " convert [-c] [-e] [-f fmt] filename [-O output_fmt] output_filename\n"
147 " info [-f fmt] filename\n"
149 "Command parameters:\n"
150 " 'filename' is a disk image filename\n"
151 " 'base_image' is the read-only disk image which is used as base for a copy on\n"
152 " write image; the copy on write image only stores the modified data\n"
153 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
154 " 'size' is the disk image size in kilobytes. Optional suffixes 'M' (megabyte)\n"
155 " and 'G' (gigabyte) are supported\n"
156 " 'output_filename' is the destination disk image filename\n"
157 " 'output_fmt' is the destination format\n"
158 " '-c' indicates that target image must be compressed (qcow format only)\n"
159 " '-e' indicates that the target image must be encrypted (qcow format only)\n"
161 printf("\nSupported format:");
162 bdrv_iterate_format(format_print, NULL);
168 /* XXX: put correct support for win32 */
169 static int read_password(char *buf, int buf_size)
172 printf("Password: ");
179 if (i < (buf_size - 1))
190 static struct termios oldtty;
192 static void term_exit(void)
194 tcsetattr (0, TCSANOW, &oldtty);
197 static void term_init(void)
204 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
205 |INLCR|IGNCR|ICRNL|IXON);
206 tty.c_oflag |= OPOST;
207 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
208 tty.c_cflag &= ~(CSIZE|PARENB);
213 tcsetattr (0, TCSANOW, &tty);
218 int read_password(char *buf, int buf_size)
223 printf("password: ");
228 ret = read(0, &ch, 1);
230 if (errno == EAGAIN || errno == EINTR) {
236 } else if (ret == 0) {
244 if (i < (buf_size - 1))
255 static BlockDriverState *bdrv_new_open(const char *filename,
258 BlockDriverState *bs;
264 error("Not enough memory");
266 drv = bdrv_find_format(fmt);
268 error("Unknown file format '%s'", fmt);
272 if (bdrv_open2(bs, filename, 0, drv) < 0) {
273 error("Could not open '%s'", filename);
275 if (bdrv_is_encrypted(bs)) {
276 printf("Disk image '%s' is encrypted.\n", filename);
277 if (read_password(password, sizeof(password)) < 0)
278 error("No password given");
279 if (bdrv_set_key(bs, password) < 0)
280 error("invalid password");
285 static int img_create(int argc, char **argv)
287 int c, ret, encrypted;
288 const char *fmt = "raw";
289 const char *filename;
290 const char *base_filename = NULL;
297 c = getopt(argc, argv, "b:f:he");
305 base_filename = optarg;
317 filename = argv[optind++];
320 BlockDriverState *bs;
321 bs = bdrv_new_open(base_filename, NULL);
322 bdrv_get_geometry(bs, &size);
329 size = strtoul(p, (char **)&p, 0);
332 } else if (*p == 'G') {
333 size *= 1024 * 1024 * 1024;
334 } else if (*p == 'k' || *p == 'K' || *p == '\0') {
340 drv = bdrv_find_format(fmt);
342 error("Unknown file format '%s'", fmt);
343 printf("Formating '%s', fmt=%s",
346 printf(", encrypted");
348 printf(", backing_file=%s",
351 printf(", size=%" PRId64 " kB\n", (int64_t) (size / 1024));
352 ret = bdrv_create(drv, filename, size / 512, base_filename, encrypted);
354 if (ret == -ENOTSUP) {
355 error("Formatting or formatting option not supported for file format '%s'", fmt);
357 error("Error while formatting");
363 static int img_commit(int argc, char **argv)
366 const char *filename, *fmt;
368 BlockDriverState *bs;
372 c = getopt(argc, argv, "f:h");
386 filename = argv[optind++];
390 error("Not enough memory");
392 drv = bdrv_find_format(fmt);
394 error("Unknown file format '%s'", fmt);
398 if (bdrv_open2(bs, filename, 0, drv) < 0) {
399 error("Could not open '%s'", filename);
401 ret = bdrv_commit(bs);
404 printf("Image committed.\n");
407 error("No disk inserted");
410 error("Image is read-only");
413 error("Image is already committed");
416 error("Error while committing image");
424 static int is_not_zero(const uint8_t *sector, int len)
428 for(i = 0;i < len; i++) {
429 if (((uint32_t *)sector)[i] != 0)
435 static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
443 v = is_not_zero(buf, 512);
444 for(i = 1; i < n; i++) {
446 if (v != is_not_zero(buf, 512))
453 #define IO_BUF_SIZE 65536
455 static int img_convert(int argc, char **argv)
457 int c, ret, n, n1, compress, cluster_size, cluster_sectors, encrypt;
458 const char *filename, *fmt, *out_fmt, *out_filename;
460 BlockDriverState *bs, *out_bs;
461 int64_t total_sectors, nb_sectors, sector_num;
462 uint8_t buf[IO_BUF_SIZE];
471 c = getopt(argc, argv, "f:O:hce");
494 filename = argv[optind++];
497 out_filename = argv[optind++];
499 bs = bdrv_new_open(filename, fmt);
501 drv = bdrv_find_format(out_fmt);
503 error("Unknown file format '%s'", fmt);
504 if (compress && drv != &bdrv_qcow && drv != &bdrv_qcow2)
505 error("Compression not supported for this file format");
506 if (encrypt && drv != &bdrv_qcow && drv != &bdrv_qcow2)
507 error("Encryption not supported for this file format");
508 if (compress && encrypt)
509 error("Compression and encryption not supported at the same time");
510 bdrv_get_geometry(bs, &total_sectors);
511 ret = bdrv_create(drv, out_filename, total_sectors, NULL, encrypt);
513 if (ret == -ENOTSUP) {
514 error("Formatting not supported for file format '%s'", fmt);
516 error("Error while formatting '%s'", out_filename);
520 out_bs = bdrv_new_open(out_filename, out_fmt);
523 if (bdrv_get_info(out_bs, &bdi) < 0)
524 error("could not get block driver info");
525 cluster_size = bdi.cluster_size;
526 if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE)
527 error("invalid cluster size");
528 cluster_sectors = cluster_size >> 9;
531 nb_sectors = total_sectors - sector_num;
534 if (nb_sectors >= cluster_sectors)
538 if (bdrv_read(bs, sector_num, buf, n) < 0)
539 error("error while reading");
540 if (n < cluster_sectors)
541 memset(buf + n * 512, 0, cluster_size - n * 512);
542 if (is_not_zero(buf, cluster_size)) {
543 if (bdrv_write_compressed(out_bs, sector_num, buf,
544 cluster_sectors) != 0)
545 error("error while compressing sector %" PRId64,
550 /* signal EOF to align */
551 bdrv_write_compressed(out_bs, 0, NULL, 0);
555 nb_sectors = total_sectors - sector_num;
558 if (nb_sectors >= (IO_BUF_SIZE / 512))
559 n = (IO_BUF_SIZE / 512);
562 if (bdrv_read(bs, sector_num, buf, n) < 0)
563 error("error while reading");
564 /* NOTE: at the same time we convert, we do not write zero
565 sectors to have a chance to compress the image. Ideally, we
566 should add a specific call to have the info to go faster */
569 if (is_allocated_sectors(buf1, n, &n1)) {
570 if (bdrv_write(out_bs, sector_num, buf1, n1) < 0)
571 error("error while writing");
585 static int64_t get_allocated_file_size(const char *filename)
587 typedef DWORD (WINAPI * get_compressed_t)(const char *filename, DWORD *high);
588 get_compressed_t get_compressed;
591 /* WinNT support GetCompressedFileSize to determine allocate size */
592 get_compressed = (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"), "GetCompressedFileSizeA");
593 if (get_compressed) {
595 low = get_compressed(filename, &high);
596 if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR)
597 return (((int64_t) high) << 32) + low;
600 if (_stati64(filename, &st) < 0)
605 static int64_t get_allocated_file_size(const char *filename)
608 if (stat(filename, &st) < 0)
610 return (int64_t)st.st_blocks * 512;
614 static void dump_snapshots(BlockDriverState *bs)
616 QEMUSnapshotInfo *sn_tab, *sn;
620 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
623 printf("Snapshot list:\n");
624 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
625 for(i = 0; i < nb_sns; i++) {
627 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
632 static int img_info(int argc, char **argv)
635 const char *filename, *fmt;
637 BlockDriverState *bs;
638 char fmt_name[128], size_buf[128], dsize_buf[128];
639 int64_t total_sectors, allocated_size;
640 char backing_filename[1024];
641 char backing_filename2[1024];
646 c = getopt(argc, argv, "f:h");
660 filename = argv[optind++];
664 error("Not enough memory");
666 drv = bdrv_find_format(fmt);
668 error("Unknown file format '%s'", fmt);
672 if (bdrv_open2(bs, filename, 0, drv) < 0) {
673 error("Could not open '%s'", filename);
675 bdrv_get_format(bs, fmt_name, sizeof(fmt_name));
676 bdrv_get_geometry(bs, &total_sectors);
677 get_human_readable_size(size_buf, sizeof(size_buf), total_sectors * 512);
678 allocated_size = get_allocated_file_size(filename);
679 if (allocated_size < 0)
680 sprintf(dsize_buf, "unavailable");
682 get_human_readable_size(dsize_buf, sizeof(dsize_buf),
686 "virtual size: %s (%" PRId64 " bytes)\n"
688 filename, fmt_name, size_buf,
689 (total_sectors * 512),
691 if (bdrv_is_encrypted(bs))
692 printf("encrypted: yes\n");
693 if (bdrv_get_info(bs, &bdi) >= 0) {
694 if (bdi.cluster_size != 0)
695 printf("cluster_size: %d\n", bdi.cluster_size);
697 bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
698 if (backing_filename[0] != '\0') {
699 path_combine(backing_filename2, sizeof(backing_filename2),
700 filename, backing_filename);
701 printf("backing file: %s (actual path: %s)\n",
710 int main(int argc, char **argv)
719 if (!strcmp(cmd, "create")) {
720 img_create(argc, argv);
721 } else if (!strcmp(cmd, "commit")) {
722 img_commit(argc, argv);
723 } else if (!strcmp(cmd, "convert")) {
724 img_convert(argc, argv);
725 } else if (!strcmp(cmd, "info")) {
726 img_info(argc, argv);