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"
31 void *get_mmap_addr(unsigned long size
)
36 void qemu_free(void *ptr
)
41 void *qemu_malloc(size_t size
)
46 void *qemu_mallocz(size_t size
)
49 ptr
= qemu_malloc(size
);
56 char *qemu_strdup(const char *str
)
59 ptr
= qemu_malloc(strlen(str
) + 1);
66 void term_printf(const char *fmt
, ...)
74 void term_print_filename(const char *filename
)
76 term_printf(filename
);
79 void __attribute__((noreturn
)) error(const char *fmt
, ...)
83 fprintf(stderr
, "qemu-img: ");
84 vfprintf(stderr
, fmt
, ap
);
85 fprintf(stderr
, "\n");
90 static void format_print(void *opaque
, const char *name
)
97 printf("qemu-img version " QEMU_VERSION
", Copyright (c) 2004-2007 Fabrice Bellard\n"
98 "usage: qemu-img command [command options]\n"
99 "QEMU disk image utility\n"
102 " create [-e] [-6] [-b base_image] [-f fmt] filename [size]\n"
103 " commit [-f fmt] filename\n"
104 " convert [-c] [-e] [-6] [-f fmt] filename [-O output_fmt] output_filename\n"
105 " info [-f fmt] filename\n"
107 "Command parameters:\n"
108 " 'filename' is a disk image filename\n"
109 " 'base_image' is the read-only disk image which is used as base for a copy on\n"
110 " write image; the copy on write image only stores the modified data\n"
111 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
112 " 'size' is the disk image size in kilobytes. Optional suffixes 'M' (megabyte)\n"
113 " and 'G' (gigabyte) are supported\n"
114 " 'output_filename' is the destination disk image filename\n"
115 " 'output_fmt' is the destination format\n"
116 " '-c' indicates that target image must be compressed (qcow format only)\n"
117 " '-e' indicates that the target image must be encrypted (qcow format only)\n"
118 " '-6' indicates that the target image must use compatibility level 6 (vmdk format only)\n"
120 printf("\nSupported format:");
121 bdrv_iterate_format(format_print
, NULL
);
127 /* XXX: put correct support for win32 */
128 static int read_password(char *buf
, int buf_size
)
131 printf("Password: ");
138 if (i
< (buf_size
- 1))
149 static struct termios oldtty
;
151 static void term_exit(void)
153 tcsetattr (0, TCSANOW
, &oldtty
);
156 static void term_init(void)
163 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
164 |INLCR
|IGNCR
|ICRNL
|IXON
);
165 tty
.c_oflag
|= OPOST
;
166 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
167 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
172 tcsetattr (0, TCSANOW
, &tty
);
177 int read_password(char *buf
, int buf_size
)
182 printf("password: ");
187 ret
= read(0, &ch
, 1);
189 if (errno
== EAGAIN
|| errno
== EINTR
) {
195 } else if (ret
== 0) {
203 if (i
< (buf_size
- 1))
214 static BlockDriverState
*bdrv_new_open(const char *filename
,
217 BlockDriverState
*bs
;
223 error("Not enough memory");
225 drv
= bdrv_find_format(fmt
);
227 error("Unknown file format '%s'", fmt
);
231 if (bdrv_open2(bs
, filename
, 0, drv
) < 0) {
232 error("Could not open '%s'", filename
);
234 if (bdrv_is_encrypted(bs
)) {
235 printf("Disk image '%s' is encrypted.\n", filename
);
236 if (read_password(password
, sizeof(password
)) < 0)
237 error("No password given");
238 if (bdrv_set_key(bs
, password
) < 0)
239 error("invalid password");
244 static int img_create(int argc
, char **argv
)
247 const char *fmt
= "raw";
248 const char *filename
;
249 const char *base_filename
= NULL
;
256 c
= getopt(argc
, argv
, "b:f:he6");
264 base_filename
= optarg
;
270 flags
|= BLOCK_FLAG_ENCRYPT
;
273 flags
|= BLOCK_FLAG_COMPAT6
;
279 filename
= argv
[optind
++];
282 BlockDriverState
*bs
;
283 bs
= bdrv_new_open(base_filename
, NULL
);
284 bdrv_get_geometry(bs
, &size
);
291 size
= strtoul(p
, (char **)&p
, 0);
294 } else if (*p
== 'G') {
295 size
*= 1024 * 1024 * 1024;
296 } else if (*p
== 'k' || *p
== 'K' || *p
== '\0') {
302 drv
= bdrv_find_format(fmt
);
304 error("Unknown file format '%s'", fmt
);
305 printf("Formatting '%s', fmt=%s",
307 if (flags
& BLOCK_FLAG_ENCRYPT
)
308 printf(", encrypted");
309 if (flags
& BLOCK_FLAG_COMPAT6
)
310 printf(", compatibility level=6");
312 printf(", backing_file=%s",
315 printf(", size=%" PRId64
" kB\n", (int64_t) (size
/ 1024));
316 ret
= bdrv_create(drv
, filename
, size
/ 512, base_filename
, flags
);
318 if (ret
== -ENOTSUP
) {
319 error("Formatting or formatting option not supported for file format '%s'", fmt
);
321 error("Error while formatting");
327 static int img_commit(int argc
, char **argv
)
330 const char *filename
, *fmt
;
332 BlockDriverState
*bs
;
336 c
= getopt(argc
, argv
, "f:h");
350 filename
= argv
[optind
++];
354 error("Not enough memory");
356 drv
= bdrv_find_format(fmt
);
358 error("Unknown file format '%s'", fmt
);
362 if (bdrv_open2(bs
, filename
, 0, drv
) < 0) {
363 error("Could not open '%s'", filename
);
365 ret
= bdrv_commit(bs
);
368 printf("Image committed.\n");
371 error("No disk inserted");
374 error("Image is read-only");
377 error("Image is already committed");
380 error("Error while committing image");
388 static int is_not_zero(const uint8_t *sector
, int len
)
392 for(i
= 0;i
< len
; i
++) {
393 if (((uint32_t *)sector
)[i
] != 0)
399 static int is_allocated_sectors(const uint8_t *buf
, int n
, int *pnum
)
407 v
= is_not_zero(buf
, 512);
408 for(i
= 1; i
< n
; i
++) {
410 if (v
!= is_not_zero(buf
, 512))
417 #define IO_BUF_SIZE 65536
419 static int img_convert(int argc
, char **argv
)
421 int c
, ret
, n
, n1
, flags
, cluster_size
, cluster_sectors
;
422 const char *filename
, *fmt
, *out_fmt
, *out_filename
;
424 BlockDriverState
*bs
, *out_bs
;
425 int64_t total_sectors
, nb_sectors
, sector_num
;
426 uint8_t buf
[IO_BUF_SIZE
];
434 c
= getopt(argc
, argv
, "f:O:hce6");
448 flags
|= BLOCK_FLAG_COMPRESS
;
451 flags
|= BLOCK_FLAG_ENCRYPT
;
454 flags
|= BLOCK_FLAG_COMPAT6
;
460 filename
= argv
[optind
++];
463 out_filename
= argv
[optind
++];
465 bs
= bdrv_new_open(filename
, fmt
);
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_COMPRESS
&& 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");
478 bdrv_get_geometry(bs
, &total_sectors
);
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
);
490 if (flags
&& BLOCK_FLAG_COMPRESS
) {
491 if (bdrv_get_info(out_bs
, &bdi
) < 0)
492 error("could not get block driver info");
493 cluster_size
= bdi
.cluster_size
;
494 if (cluster_size
<= 0 || cluster_size
> IO_BUF_SIZE
)
495 error("invalid cluster size");
496 cluster_sectors
= cluster_size
>> 9;
499 nb_sectors
= total_sectors
- sector_num
;
502 if (nb_sectors
>= cluster_sectors
)
506 if (bdrv_read(bs
, sector_num
, buf
, n
) < 0)
507 error("error while reading");
508 if (n
< cluster_sectors
)
509 memset(buf
+ n
* 512, 0, cluster_size
- n
* 512);
510 if (is_not_zero(buf
, cluster_size
)) {
511 if (bdrv_write_compressed(out_bs
, sector_num
, buf
,
512 cluster_sectors
) != 0)
513 error("error while compressing sector %" PRId64
,
518 /* signal EOF to align */
519 bdrv_write_compressed(out_bs
, 0, NULL
, 0);
523 nb_sectors
= total_sectors
- sector_num
;
526 if (nb_sectors
>= (IO_BUF_SIZE
/ 512))
527 n
= (IO_BUF_SIZE
/ 512);
530 if (bdrv_read(bs
, sector_num
, buf
, n
) < 0)
531 error("error while reading");
532 /* NOTE: at the same time we convert, we do not write zero
533 sectors to have a chance to compress the image. Ideally, we
534 should add a specific call to have the info to go faster */
537 if (is_allocated_sectors(buf1
, n
, &n1
)) {
538 if (bdrv_write(out_bs
, sector_num
, buf1
, n1
) < 0)
539 error("error while writing");
553 static int64_t get_allocated_file_size(const char *filename
)
555 typedef DWORD (WINAPI
* get_compressed_t
)(const char *filename
, DWORD
*high
);
556 get_compressed_t get_compressed
;
559 /* WinNT support GetCompressedFileSize to determine allocate size */
560 get_compressed
= (get_compressed_t
) GetProcAddress(GetModuleHandle("kernel32"), "GetCompressedFileSizeA");
561 if (get_compressed
) {
563 low
= get_compressed(filename
, &high
);
564 if (low
!= 0xFFFFFFFFlu
|| GetLastError() == NO_ERROR
)
565 return (((int64_t) high
) << 32) + low
;
568 if (_stati64(filename
, &st
) < 0)
573 static int64_t get_allocated_file_size(const char *filename
)
576 if (stat(filename
, &st
) < 0)
578 return (int64_t)st
.st_blocks
* 512;
582 static void dump_snapshots(BlockDriverState
*bs
)
584 QEMUSnapshotInfo
*sn_tab
, *sn
;
588 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
591 printf("Snapshot list:\n");
592 printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
593 for(i
= 0; i
< nb_sns
; i
++) {
595 printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
600 static int img_info(int argc
, char **argv
)
603 const char *filename
, *fmt
;
605 BlockDriverState
*bs
;
606 char fmt_name
[128], size_buf
[128], dsize_buf
[128];
607 int64_t total_sectors
, allocated_size
;
608 char backing_filename
[1024];
609 char backing_filename2
[1024];
614 c
= getopt(argc
, argv
, "f:h");
628 filename
= argv
[optind
++];
632 error("Not enough memory");
634 drv
= bdrv_find_format(fmt
);
636 error("Unknown file format '%s'", fmt
);
640 if (bdrv_open2(bs
, filename
, 0, drv
) < 0) {
641 error("Could not open '%s'", filename
);
643 bdrv_get_format(bs
, fmt_name
, sizeof(fmt_name
));
644 bdrv_get_geometry(bs
, &total_sectors
);
645 get_human_readable_size(size_buf
, sizeof(size_buf
), total_sectors
* 512);
646 allocated_size
= get_allocated_file_size(filename
);
647 if (allocated_size
< 0)
648 sprintf(dsize_buf
, "unavailable");
650 get_human_readable_size(dsize_buf
, sizeof(dsize_buf
),
654 "virtual size: %s (%" PRId64
" bytes)\n"
656 filename
, fmt_name
, size_buf
,
657 (total_sectors
* 512),
659 if (bdrv_is_encrypted(bs
))
660 printf("encrypted: yes\n");
661 if (bdrv_get_info(bs
, &bdi
) >= 0) {
662 if (bdi
.cluster_size
!= 0)
663 printf("cluster_size: %d\n", bdi
.cluster_size
);
665 bdrv_get_backing_filename(bs
, backing_filename
, sizeof(backing_filename
));
666 if (backing_filename
[0] != '\0') {
667 path_combine(backing_filename2
, sizeof(backing_filename2
),
668 filename
, backing_filename
);
669 printf("backing file: %s (actual path: %s)\n",
678 int main(int argc
, char **argv
)
687 if (!strcmp(cmd
, "create")) {
688 img_create(argc
, argv
);
689 } else if (!strcmp(cmd
, "commit")) {
690 img_commit(argc
, argv
);
691 } else if (!strcmp(cmd
, "convert")) {
692 img_convert(argc
, argv
);
693 } else if (!strcmp(cmd
, "info")) {
694 img_info(argc
, argv
);