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 __attribute__((noreturn
)) error(const char *fmt
, ...)
120 fprintf(stderr
, "qemu-img: ");
121 vfprintf(stderr
, fmt
, ap
);
122 fprintf(stderr
, "\n");
127 static void format_print(void *opaque
, const char *name
)
134 printf("qemu-img version " QEMU_VERSION
", Copyright (c) 2004-2006 Fabrice Bellard\n"
135 "usage: qemu-img command [command options]\n"
136 "QEMU disk image utility\n"
139 " create [-e] [-b base_image] [-f fmt] filename [size]\n"
140 " commit [-f fmt] filename\n"
141 " convert [-c] [-e] [-f fmt] filename [-O output_fmt] output_filename\n"
142 " info [-f fmt] filename\n"
144 "Command parameters:\n"
145 " 'filename' is a disk image filename\n"
146 " 'base_image' is the read-only disk image which is used as base for a copy on\n"
147 " write image; the copy on write image only stores the modified data\n"
148 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
149 " 'size' is the disk image size in kilobytes. Optional suffixes 'M' (megabyte)\n"
150 " and 'G' (gigabyte) are supported\n"
151 " 'output_filename' is the destination disk image filename\n"
152 " 'output_fmt' is the destination format\n"
153 " '-c' indicates that target image must be compressed (qcow format only)\n"
154 " '-e' indicates that the target image must be encrypted (qcow format only)\n"
156 printf("\nSupported format:");
157 bdrv_iterate_format(format_print
, NULL
);
163 /* XXX: put correct support for win32 */
164 static int read_password(char *buf
, int buf_size
)
167 printf("Password: ");
174 if (i
< (buf_size
- 1))
185 static struct termios oldtty
;
187 static void term_exit(void)
189 tcsetattr (0, TCSANOW
, &oldtty
);
192 static void term_init(void)
199 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
200 |INLCR
|IGNCR
|ICRNL
|IXON
);
201 tty
.c_oflag
|= OPOST
;
202 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
203 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
208 tcsetattr (0, TCSANOW
, &tty
);
213 int read_password(char *buf
, int buf_size
)
218 printf("password: ");
223 ret
= read(0, &ch
, 1);
225 if (errno
== EAGAIN
|| errno
== EINTR
) {
231 } else if (ret
== 0) {
239 if (i
< (buf_size
- 1))
250 static BlockDriverState
*bdrv_new_open(const char *filename
,
253 BlockDriverState
*bs
;
259 error("Not enough memory");
261 drv
= bdrv_find_format(fmt
);
263 error("Unknown file format '%s'", fmt
);
267 if (bdrv_open2(bs
, filename
, 0, drv
) < 0) {
268 error("Could not open '%s'", filename
);
270 if (bdrv_is_encrypted(bs
)) {
271 printf("Disk image '%s' is encrypted.\n", filename
);
272 if (read_password(password
, sizeof(password
)) < 0)
273 error("No password given");
274 if (bdrv_set_key(bs
, password
) < 0)
275 error("invalid password");
280 static int img_create(int argc
, char **argv
)
282 int c
, ret
, encrypted
;
283 const char *fmt
= "raw";
284 const char *filename
;
285 const char *base_filename
= NULL
;
292 c
= getopt(argc
, argv
, "b:f:he");
300 base_filename
= optarg
;
312 filename
= argv
[optind
++];
315 BlockDriverState
*bs
;
316 bs
= bdrv_new_open(base_filename
, NULL
);
317 bdrv_get_geometry(bs
, &size
);
324 size
= strtoul(p
, (char **)&p
, 0);
327 } else if (*p
== 'G') {
328 size
*= 1024 * 1024 * 1024;
329 } else if (*p
== 'k' || *p
== 'K' || *p
== '\0') {
335 drv
= bdrv_find_format(fmt
);
337 error("Unknown file format '%s'", fmt
);
338 printf("Formating '%s', fmt=%s",
341 printf(", encrypted");
343 printf(", backing_file=%s",
346 printf(", size=%" PRId64
" kB\n", (int64_t) (size
/ 1024));
347 ret
= bdrv_create(drv
, filename
, size
/ 512, base_filename
, encrypted
);
349 if (ret
== -ENOTSUP
) {
350 error("Formatting or formatting option not supported for file format '%s'", fmt
);
352 error("Error while formatting");
358 static int img_commit(int argc
, char **argv
)
361 const char *filename
, *fmt
;
363 BlockDriverState
*bs
;
367 c
= getopt(argc
, argv
, "f:h");
381 filename
= argv
[optind
++];
385 error("Not enough memory");
387 drv
= bdrv_find_format(fmt
);
389 error("Unknown file format '%s'", fmt
);
393 if (bdrv_open2(bs
, filename
, 0, drv
) < 0) {
394 error("Could not open '%s'", filename
);
396 ret
= bdrv_commit(bs
);
399 printf("Image committed.\n");
402 error("No disk inserted");
405 error("Image is read-only");
408 error("Image is already committed");
411 error("Error while committing image");
419 static int is_not_zero(const uint8_t *sector
, int len
)
423 for(i
= 0;i
< len
; i
++) {
424 if (((uint32_t *)sector
)[i
] != 0)
430 static int is_allocated_sectors(const uint8_t *buf
, int n
, int *pnum
)
438 v
= is_not_zero(buf
, 512);
439 for(i
= 1; i
< n
; i
++) {
441 if (v
!= is_not_zero(buf
, 512))
448 #define IO_BUF_SIZE 65536
450 static int img_convert(int argc
, char **argv
)
452 int c
, ret
, n
, n1
, compress
, cluster_size
, cluster_sectors
, encrypt
;
453 const char *filename
, *fmt
, *out_fmt
, *out_filename
;
455 BlockDriverState
*bs
, *out_bs
;
456 int64_t total_sectors
, nb_sectors
, sector_num
;
457 uint8_t buf
[IO_BUF_SIZE
];
466 c
= getopt(argc
, argv
, "f:O:hce");
489 filename
= argv
[optind
++];
492 out_filename
= argv
[optind
++];
494 bs
= bdrv_new_open(filename
, fmt
);
496 drv
= bdrv_find_format(out_fmt
);
498 error("Unknown file format '%s'", fmt
);
499 if (compress
&& drv
!= &bdrv_qcow
&& drv
!= &bdrv_qcow2
)
500 error("Compression not supported for this file format");
501 if (encrypt
&& drv
!= &bdrv_qcow
&& drv
!= &bdrv_qcow2
)
502 error("Encryption not supported for this file format");
503 if (compress
&& encrypt
)
504 error("Compression and encryption not supported at the same time");
505 bdrv_get_geometry(bs
, &total_sectors
);
506 ret
= bdrv_create(drv
, out_filename
, total_sectors
, NULL
, encrypt
);
508 if (ret
== -ENOTSUP
) {
509 error("Formatting not supported for file format '%s'", fmt
);
511 error("Error while formatting '%s'", out_filename
);
515 out_bs
= bdrv_new_open(out_filename
, out_fmt
);
518 if (bdrv_get_info(out_bs
, &bdi
) < 0)
519 error("could not get block driver info");
520 cluster_size
= bdi
.cluster_size
;
521 if (cluster_size
<= 0 || cluster_size
> IO_BUF_SIZE
)
522 error("invalid cluster size");
523 cluster_sectors
= cluster_size
>> 9;
526 nb_sectors
= total_sectors
- sector_num
;
529 if (nb_sectors
>= cluster_sectors
)
533 if (bdrv_read(bs
, sector_num
, buf
, n
) < 0)
534 error("error while reading");
535 if (n
< cluster_sectors
)
536 memset(buf
+ n
* 512, 0, cluster_size
- n
* 512);
537 if (is_not_zero(buf
, cluster_size
)) {
538 if (bdrv_write_compressed(out_bs
, sector_num
, buf
,
539 cluster_sectors
) != 0)
540 error("error while compressing sector %" PRId64
,
545 /* signal EOF to align */
546 bdrv_write_compressed(out_bs
, 0, NULL
, 0);
550 nb_sectors
= total_sectors
- sector_num
;
553 if (nb_sectors
>= (IO_BUF_SIZE
/ 512))
554 n
= (IO_BUF_SIZE
/ 512);
557 if (bdrv_read(bs
, sector_num
, buf
, n
) < 0)
558 error("error while reading");
559 /* NOTE: at the same time we convert, we do not write zero
560 sectors to have a chance to compress the image. Ideally, we
561 should add a specific call to have the info to go faster */
564 if (is_allocated_sectors(buf1
, n
, &n1
)) {
565 if (bdrv_write(out_bs
, sector_num
, buf1
, n1
) < 0)
566 error("error while writing");
580 static int64_t get_allocated_file_size(const char *filename
)
582 typedef DWORD (WINAPI
* get_compressed_t
)(const char *filename
, DWORD
*high
);
583 get_compressed_t get_compressed
;
586 /* WinNT support GetCompressedFileSize to determine allocate size */
587 get_compressed
= (get_compressed_t
) GetProcAddress(GetModuleHandle("kernel32"), "GetCompressedFileSizeA");
588 if (get_compressed
) {
590 low
= get_compressed(filename
, &high
);
591 if (low
!= 0xFFFFFFFFlu
|| GetLastError() == NO_ERROR
)
592 return (((int64_t) high
) << 32) + low
;
595 if (_stati64(filename
, &st
) < 0)
600 static int64_t get_allocated_file_size(const char *filename
)
603 if (stat(filename
, &st
) < 0)
605 return (int64_t)st
.st_blocks
* 512;
609 static void dump_snapshots(BlockDriverState
*bs
)
611 QEMUSnapshotInfo
*sn_tab
, *sn
;
615 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
618 printf("Snapshot list:\n");
619 printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
620 for(i
= 0; i
< nb_sns
; i
++) {
622 printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
627 static int img_info(int argc
, char **argv
)
630 const char *filename
, *fmt
;
632 BlockDriverState
*bs
;
633 char fmt_name
[128], size_buf
[128], dsize_buf
[128];
634 int64_t total_sectors
, allocated_size
;
635 char backing_filename
[1024];
636 char backing_filename2
[1024];
641 c
= getopt(argc
, argv
, "f:h");
655 filename
= argv
[optind
++];
659 error("Not enough memory");
661 drv
= bdrv_find_format(fmt
);
663 error("Unknown file format '%s'", fmt
);
667 if (bdrv_open2(bs
, filename
, 0, drv
) < 0) {
668 error("Could not open '%s'", filename
);
670 bdrv_get_format(bs
, fmt_name
, sizeof(fmt_name
));
671 bdrv_get_geometry(bs
, &total_sectors
);
672 get_human_readable_size(size_buf
, sizeof(size_buf
), total_sectors
* 512);
673 allocated_size
= get_allocated_file_size(filename
);
674 if (allocated_size
< 0)
675 sprintf(dsize_buf
, "unavailable");
677 get_human_readable_size(dsize_buf
, sizeof(dsize_buf
),
681 "virtual size: %s (%" PRId64
" bytes)\n"
683 filename
, fmt_name
, size_buf
,
684 (total_sectors
* 512),
686 if (bdrv_is_encrypted(bs
))
687 printf("encrypted: yes\n");
688 if (bdrv_get_info(bs
, &bdi
) >= 0) {
689 if (bdi
.cluster_size
!= 0)
690 printf("cluster_size: %d\n", bdi
.cluster_size
);
692 bdrv_get_backing_filename(bs
, backing_filename
, sizeof(backing_filename
));
693 if (backing_filename
[0] != '\0') {
694 path_combine(backing_filename2
, sizeof(backing_filename2
),
695 filename
, backing_filename
);
696 printf("backing file: %s (actual path: %s)\n",
705 int main(int argc
, char **argv
)
714 if (!strcmp(cmd
, "create")) {
715 img_create(argc
, argv
);
716 } else if (!strcmp(cmd
, "commit")) {
717 img_commit(argc
, argv
);
718 } else if (!strcmp(cmd
, "convert")) {
719 img_convert(argc
, argv
);
720 } else if (!strcmp(cmd
, "info")) {
721 img_info(argc
, argv
);