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"
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 static void __attribute__((noreturn
)) error(const char *fmt
, ...)
71 fprintf(stderr
, "qemu-img: ");
72 vfprintf(stderr
, fmt
, ap
);
73 fprintf(stderr
, "\n");
78 static void format_print(void *opaque
, const char *name
)
83 static void help(void)
85 printf("qemu-img version " QEMU_VERSION
", Copyright (c) 2004-2007 Fabrice Bellard\n"
86 "usage: qemu-img command [command options]\n"
87 "QEMU disk image utility\n"
90 " create [-e] [-6] [-b base_image] [-f fmt] filename [size]\n"
91 " commit [-f fmt] filename\n"
92 " convert [-c] [-e] [-6] [-f fmt] filename [filename2 [...]] [-O output_fmt] output_filename\n"
93 " info [-f fmt] filename\n"
95 "Command parameters:\n"
96 " 'filename' is a disk image filename\n"
97 " 'base_image' is the read-only disk image which is used as base for a copy on\n"
98 " write image; the copy on write image only stores the modified data\n"
99 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
100 " 'size' is the disk image size in kilobytes. Optional suffixes 'M' (megabyte)\n"
101 " and 'G' (gigabyte) are supported\n"
102 " 'output_filename' is the destination disk image filename\n"
103 " 'output_fmt' is the destination format\n"
104 " '-c' indicates that target image must be compressed (qcow format only)\n"
105 " '-e' indicates that the target image must be encrypted (qcow format only)\n"
106 " '-6' indicates that the target image must use compatibility level 6 (vmdk format only)\n"
108 printf("\nSupported format:");
109 bdrv_iterate_format(format_print
, NULL
);
115 /* XXX: put correct support for win32 */
116 static int read_password(char *buf
, int buf_size
)
119 printf("Password: ");
126 if (i
< (buf_size
- 1))
137 static struct termios oldtty
;
139 static void term_exit(void)
141 tcsetattr (0, TCSANOW
, &oldtty
);
144 static void term_init(void)
151 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
152 |INLCR
|IGNCR
|ICRNL
|IXON
);
153 tty
.c_oflag
|= OPOST
;
154 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
155 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
160 tcsetattr (0, TCSANOW
, &tty
);
165 static int read_password(char *buf
, int buf_size
)
170 printf("password: ");
175 ret
= read(0, &ch
, 1);
177 if (errno
== EAGAIN
|| errno
== EINTR
) {
183 } else if (ret
== 0) {
191 if (i
< (buf_size
- 1))
202 static BlockDriverState
*bdrv_new_open(const char *filename
,
205 BlockDriverState
*bs
;
211 error("Not enough memory");
213 drv
= bdrv_find_format(fmt
);
215 error("Unknown file format '%s'", fmt
);
219 if (bdrv_open2(bs
, filename
, 0, drv
) < 0) {
220 error("Could not open '%s'", filename
);
222 if (bdrv_is_encrypted(bs
)) {
223 printf("Disk image '%s' is encrypted.\n", filename
);
224 if (read_password(password
, sizeof(password
)) < 0)
225 error("No password given");
226 if (bdrv_set_key(bs
, password
) < 0)
227 error("invalid password");
232 static int img_create(int argc
, char **argv
)
235 const char *fmt
= "raw";
236 const char *filename
;
237 const char *base_filename
= NULL
;
244 c
= getopt(argc
, argv
, "b:f:he6");
252 base_filename
= optarg
;
258 flags
|= BLOCK_FLAG_ENCRYPT
;
261 flags
|= BLOCK_FLAG_COMPAT6
;
267 filename
= argv
[optind
++];
270 BlockDriverState
*bs
;
271 bs
= bdrv_new_open(base_filename
, NULL
);
272 bdrv_get_geometry(bs
, &size
);
279 size
= strtoul(p
, (char **)&p
, 0);
282 } else if (*p
== 'G') {
283 size
*= 1024 * 1024 * 1024;
284 } else if (*p
== 'k' || *p
== 'K' || *p
== '\0') {
290 drv
= bdrv_find_format(fmt
);
292 error("Unknown file format '%s'", fmt
);
293 printf("Formatting '%s', fmt=%s",
295 if (flags
& BLOCK_FLAG_ENCRYPT
)
296 printf(", encrypted");
297 if (flags
& BLOCK_FLAG_COMPAT6
)
298 printf(", compatibility level=6");
300 printf(", backing_file=%s",
303 printf(", size=%" PRId64
" kB\n", (int64_t) (size
/ 1024));
304 ret
= bdrv_create(drv
, filename
, size
/ 512, base_filename
, flags
);
306 if (ret
== -ENOTSUP
) {
307 error("Formatting or formatting option not supported for file format '%s'", fmt
);
309 error("Error while formatting");
315 static int img_commit(int argc
, char **argv
)
318 const char *filename
, *fmt
;
320 BlockDriverState
*bs
;
324 c
= getopt(argc
, argv
, "f:h");
338 filename
= argv
[optind
++];
342 error("Not enough memory");
344 drv
= bdrv_find_format(fmt
);
346 error("Unknown file format '%s'", fmt
);
350 if (bdrv_open2(bs
, filename
, 0, drv
) < 0) {
351 error("Could not open '%s'", filename
);
353 ret
= bdrv_commit(bs
);
356 printf("Image committed.\n");
359 error("No disk inserted");
362 error("Image is read-only");
365 error("Image is already committed");
368 error("Error while committing image");
376 static int is_not_zero(const uint8_t *sector
, int len
)
380 for(i
= 0;i
< len
; i
++) {
381 if (((uint32_t *)sector
)[i
] != 0)
387 static int is_allocated_sectors(const uint8_t *buf
, int n
, int *pnum
)
395 v
= is_not_zero(buf
, 512);
396 for(i
= 1; i
< n
; i
++) {
398 if (v
!= is_not_zero(buf
, 512))
405 #define IO_BUF_SIZE 65536
407 static int img_convert(int argc
, char **argv
)
409 int c
, ret
, n
, n1
, bs_n
, bs_i
, flags
, cluster_size
, cluster_sectors
;
410 const char *fmt
, *out_fmt
, *out_filename
;
412 BlockDriverState
**bs
, *out_bs
;
413 int64_t total_sectors
, nb_sectors
, sector_num
, bs_offset
, bs_sectors
;
414 uint8_t buf
[IO_BUF_SIZE
];
422 c
= getopt(argc
, argv
, "f:O:hce6");
436 flags
|= BLOCK_FLAG_COMPRESS
;
439 flags
|= BLOCK_FLAG_ENCRYPT
;
442 flags
|= BLOCK_FLAG_COMPAT6
;
447 bs_n
= argc
- optind
- 1;
448 if (bs_n
< 1) help();
450 out_filename
= argv
[argc
- 1];
452 bs
= calloc(bs_n
, sizeof(BlockDriverState
*));
454 error("Out of memory");
457 for (bs_i
= 0; bs_i
< bs_n
; bs_i
++) {
458 bs
[bs_i
] = bdrv_new_open(argv
[optind
+ bs_i
], fmt
);
460 error("Could not open '%s'", argv
[optind
+ bs_i
]);
461 bdrv_get_geometry(bs
[bs_i
], &bs_sectors
);
462 total_sectors
+= bs_sectors
;
465 drv
= bdrv_find_format(out_fmt
);
467 error("Unknown file format '%s'", out_fmt
);
468 if (flags
& BLOCK_FLAG_COMPRESS
&& drv
!= &bdrv_qcow
&& drv
!= &bdrv_qcow2
)
469 error("Compression not supported for this file format");
470 if (flags
& BLOCK_FLAG_ENCRYPT
&& drv
!= &bdrv_qcow
&& drv
!= &bdrv_qcow2
)
471 error("Encryption not supported for this file format");
472 if (flags
& BLOCK_FLAG_COMPAT6
&& drv
!= &bdrv_vmdk
)
473 error("Alternative compatibility level not supported for this file format");
474 if (flags
& BLOCK_FLAG_ENCRYPT
&& flags
& BLOCK_FLAG_COMPRESS
)
475 error("Compression and encryption not supported at the same time");
477 ret
= bdrv_create(drv
, out_filename
, total_sectors
, NULL
, flags
);
479 if (ret
== -ENOTSUP
) {
480 error("Formatting not supported for file format '%s'", fmt
);
482 error("Error while formatting '%s'", out_filename
);
486 out_bs
= bdrv_new_open(out_filename
, out_fmt
);
490 bdrv_get_geometry(bs
[0], &bs_sectors
);
492 if (flags
& BLOCK_FLAG_COMPRESS
) {
493 if (bdrv_get_info(out_bs
, &bdi
) < 0)
494 error("could not get block driver info");
495 cluster_size
= bdi
.cluster_size
;
496 if (cluster_size
<= 0 || cluster_size
> IO_BUF_SIZE
)
497 error("invalid cluster size");
498 cluster_sectors
= cluster_size
>> 9;
505 nb_sectors
= total_sectors
- sector_num
;
508 if (nb_sectors
>= cluster_sectors
)
513 bs_num
= sector_num
- bs_offset
;
514 assert (bs_num
>= 0);
517 while (remainder
> 0) {
519 while (bs_num
== bs_sectors
) {
521 assert (bs_i
< bs_n
);
522 bs_offset
+= bs_sectors
;
523 bdrv_get_geometry(bs
[bs_i
], &bs_sectors
);
525 /* printf("changing part: sector_num=%lld, "
526 "bs_i=%d, bs_offset=%lld, bs_sectors=%lld\n",
527 sector_num, bs_i, bs_offset, bs_sectors); */
529 assert (bs_num
< bs_sectors
);
531 nlow
= (remainder
> bs_sectors
- bs_num
) ? bs_sectors
- bs_num
: remainder
;
533 if (bdrv_read(bs
[bs_i
], bs_num
, buf2
, nlow
) < 0)
534 error("error while reading");
541 assert (remainder
== 0);
543 if (n
< cluster_sectors
)
544 memset(buf
+ n
* 512, 0, cluster_size
- n
* 512);
545 if (is_not_zero(buf
, cluster_size
)) {
546 if (bdrv_write_compressed(out_bs
, sector_num
, buf
,
547 cluster_sectors
) != 0)
548 error("error while compressing sector %" PRId64
,
553 /* signal EOF to align */
554 bdrv_write_compressed(out_bs
, 0, NULL
, 0);
558 nb_sectors
= total_sectors
- sector_num
;
561 if (nb_sectors
>= (IO_BUF_SIZE
/ 512))
562 n
= (IO_BUF_SIZE
/ 512);
566 while (sector_num
- bs_offset
>= bs_sectors
) {
568 assert (bs_i
< bs_n
);
569 bs_offset
+= bs_sectors
;
570 bdrv_get_geometry(bs
[bs_i
], &bs_sectors
);
571 /* printf("changing part: sector_num=%lld, bs_i=%d, "
572 "bs_offset=%lld, bs_sectors=%lld\n",
573 sector_num, bs_i, bs_offset, bs_sectors); */
576 if (n
> bs_offset
+ bs_sectors
- sector_num
)
577 n
= bs_offset
+ bs_sectors
- sector_num
;
579 if (bdrv_read(bs
[bs_i
], sector_num
- bs_offset
, buf
, n
) < 0)
580 error("error while reading");
581 /* NOTE: at the same time we convert, we do not write zero
582 sectors to have a chance to compress the image. Ideally, we
583 should add a specific call to have the info to go faster */
586 if (is_allocated_sectors(buf1
, n
, &n1
)) {
587 if (bdrv_write(out_bs
, sector_num
, buf1
, n1
) < 0)
588 error("error while writing");
597 for (bs_i
= 0; bs_i
< bs_n
; bs_i
++)
598 bdrv_delete(bs
[bs_i
]);
604 static int64_t get_allocated_file_size(const char *filename
)
606 typedef DWORD (WINAPI
* get_compressed_t
)(const char *filename
, DWORD
*high
);
607 get_compressed_t get_compressed
;
610 /* WinNT support GetCompressedFileSize to determine allocate size */
611 get_compressed
= (get_compressed_t
) GetProcAddress(GetModuleHandle("kernel32"), "GetCompressedFileSizeA");
612 if (get_compressed
) {
614 low
= get_compressed(filename
, &high
);
615 if (low
!= 0xFFFFFFFFlu
|| GetLastError() == NO_ERROR
)
616 return (((int64_t) high
) << 32) + low
;
619 if (_stati64(filename
, &st
) < 0)
624 static int64_t get_allocated_file_size(const char *filename
)
627 if (stat(filename
, &st
) < 0)
629 return (int64_t)st
.st_blocks
* 512;
633 static void dump_snapshots(BlockDriverState
*bs
)
635 QEMUSnapshotInfo
*sn_tab
, *sn
;
639 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
642 printf("Snapshot list:\n");
643 printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
644 for(i
= 0; i
< nb_sns
; i
++) {
646 printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
651 static int img_info(int argc
, char **argv
)
654 const char *filename
, *fmt
;
656 BlockDriverState
*bs
;
657 char fmt_name
[128], size_buf
[128], dsize_buf
[128];
658 int64_t total_sectors
, allocated_size
;
659 char backing_filename
[1024];
660 char backing_filename2
[1024];
665 c
= getopt(argc
, argv
, "f:h");
679 filename
= argv
[optind
++];
683 error("Not enough memory");
685 drv
= bdrv_find_format(fmt
);
687 error("Unknown file format '%s'", fmt
);
691 if (bdrv_open2(bs
, filename
, 0, drv
) < 0) {
692 error("Could not open '%s'", filename
);
694 bdrv_get_format(bs
, fmt_name
, sizeof(fmt_name
));
695 bdrv_get_geometry(bs
, &total_sectors
);
696 get_human_readable_size(size_buf
, sizeof(size_buf
), total_sectors
* 512);
697 allocated_size
= get_allocated_file_size(filename
);
698 if (allocated_size
< 0)
699 sprintf(dsize_buf
, "unavailable");
701 get_human_readable_size(dsize_buf
, sizeof(dsize_buf
),
705 "virtual size: %s (%" PRId64
" bytes)\n"
707 filename
, fmt_name
, size_buf
,
708 (total_sectors
* 512),
710 if (bdrv_is_encrypted(bs
))
711 printf("encrypted: yes\n");
712 if (bdrv_get_info(bs
, &bdi
) >= 0) {
713 if (bdi
.cluster_size
!= 0)
714 printf("cluster_size: %d\n", bdi
.cluster_size
);
716 bdrv_get_backing_filename(bs
, backing_filename
, sizeof(backing_filename
));
717 if (backing_filename
[0] != '\0') {
718 path_combine(backing_filename2
, sizeof(backing_filename2
),
719 filename
, backing_filename
);
720 printf("backing file: %s (actual path: %s)\n",
729 int main(int argc
, char **argv
)
738 if (!strcmp(cmd
, "create")) {
739 img_create(argc
, argv
);
740 } else if (!strcmp(cmd
, "commit")) {
741 img_commit(argc
, argv
);
742 } else if (!strcmp(cmd
, "convert")) {
743 img_convert(argc
, argv
);
744 } else if (!strcmp(cmd
, "info")) {
745 img_info(argc
, argv
);