2 * create a COW disk image
4 * Copyright (c) 2003 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
26 void *get_mmap_addr(unsigned long size
)
31 void qemu_free(void *ptr
)
36 void *qemu_malloc(size_t size
)
41 void *qemu_mallocz(size_t size
)
44 ptr
= qemu_malloc(size
);
51 char *qemu_strdup(const char *str
)
54 ptr
= qemu_malloc(strlen(str
) + 1);
61 void pstrcpy(char *buf
, int buf_size
, const char *str
)
71 if (c
== 0 || q
>= buf
+ buf_size
- 1)
78 /* strcat and truncate. */
79 char *pstrcat(char *buf
, int buf_size
, const char *s
)
84 pstrcpy(buf
+ len
, buf_size
- len
, s
);
88 int strstart(const char *str
, const char *val
, const char **ptr
)
104 void term_printf(const char *fmt
, ...)
112 void __attribute__((noreturn
)) error(const char *fmt
, ...)
116 fprintf(stderr
, "qemu-img: ");
117 vfprintf(stderr
, fmt
, ap
);
118 fprintf(stderr
, "\n");
123 static void format_print(void *opaque
, const char *name
)
130 printf("qemu-img version " QEMU_VERSION
", Copyright (c) 2004-2005 Fabrice Bellard\n"
131 "usage: qemu-img command [command options]\n"
132 "QEMU disk image utility\n"
135 " create [-e] [-b base_image] [-f fmt] filename [size]\n"
136 " commit [-f fmt] filename\n"
137 " convert [-c] [-e] [-f fmt] filename [-O output_fmt] output_filename\n"
138 " info [-f fmt] filename\n"
140 "Command parameters:\n"
141 " 'filename' is a disk image filename\n"
142 " 'base_image' is the read-only disk image which is used as base for a copy on\n"
143 " write image; the copy on write image only stores the modified data\n"
144 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
145 " 'size' is the disk image size in kilobytes. Optional suffixes 'M' (megabyte)\n"
146 " and 'G' (gigabyte) are supported\n"
147 " 'output_filename' is the destination disk image filename\n"
148 " 'output_fmt' is the destination format\n"
149 " '-c' indicates that target image must be compressed (qcow format only)\n"
150 " '-e' indicates that the target image must be encrypted (qcow format only)\n"
152 printf("\nSupported format:");
153 bdrv_iterate_format(format_print
, NULL
);
159 #define NB_SUFFIXES 4
161 static void get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
163 char suffixes
[NB_SUFFIXES
] = "KMGT";
168 snprintf(buf
, buf_size
, "%lld", (long long) size
);
171 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
172 if (size
< (10 * base
)) {
173 snprintf(buf
, buf_size
, "%0.1f%c",
177 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
178 snprintf(buf
, buf_size
, "%lld%c",
179 (long long) ((size
+ (base
>> 1)) / base
),
189 /* XXX: put correct support for win32 */
190 static int read_password(char *buf
, int buf_size
)
193 printf("Password: ");
200 if (i
< (buf_size
- 1))
211 static struct termios oldtty
;
213 static void term_exit(void)
215 tcsetattr (0, TCSANOW
, &oldtty
);
218 static void term_init(void)
225 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
226 |INLCR
|IGNCR
|ICRNL
|IXON
);
227 tty
.c_oflag
|= OPOST
;
228 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
229 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
234 tcsetattr (0, TCSANOW
, &tty
);
239 int read_password(char *buf
, int buf_size
)
244 printf("password: ");
249 ret
= read(0, &ch
, 1);
251 if (errno
== EAGAIN
|| errno
== EINTR
) {
257 } else if (ret
== 0) {
265 if (i
< (buf_size
- 1))
276 static BlockDriverState
*bdrv_new_open(const char *filename
,
279 BlockDriverState
*bs
;
285 error("Not enough memory");
287 drv
= bdrv_find_format(fmt
);
289 error("Unknown file format '%s'", fmt
);
293 if (bdrv_open2(bs
, filename
, 0, drv
) < 0) {
294 error("Could not open '%s'", filename
);
296 if (bdrv_is_encrypted(bs
)) {
297 printf("Disk image '%s' is encrypted.\n", filename
);
298 if (read_password(password
, sizeof(password
)) < 0)
299 error("No password given");
300 if (bdrv_set_key(bs
, password
) < 0)
301 error("invalid password");
306 static int img_create(int argc
, char **argv
)
308 int c
, ret
, encrypted
;
309 const char *fmt
= "raw";
310 const char *filename
;
311 const char *base_filename
= NULL
;
318 c
= getopt(argc
, argv
, "b:f:he");
326 base_filename
= optarg
;
338 filename
= argv
[optind
++];
341 BlockDriverState
*bs
;
342 bs
= bdrv_new_open(base_filename
, NULL
);
343 bdrv_get_geometry(bs
, &size
);
350 size
= strtoul(p
, (char **)&p
, 0);
353 } else if (*p
== 'G') {
354 size
*= 1024 * 1024 * 1024;
355 } else if (*p
== 'k' || *p
== 'K' || *p
== '\0') {
361 drv
= bdrv_find_format(fmt
);
363 error("Unknown file format '%s'", fmt
);
364 printf("Formating '%s', fmt=%s",
367 printf(", encrypted");
369 printf(", backing_file=%s",
372 printf(", size=%lld kB\n", (long long) (size
/ 1024));
373 ret
= bdrv_create(drv
, filename
, size
/ 512, base_filename
, encrypted
);
375 if (ret
== -ENOTSUP
) {
376 error("Formatting or formatting option not supported for file format '%s'", fmt
);
378 error("Error while formatting");
384 static int img_commit(int argc
, char **argv
)
387 const char *filename
, *fmt
;
389 BlockDriverState
*bs
;
393 c
= getopt(argc
, argv
, "f:h");
407 filename
= argv
[optind
++];
411 error("Not enough memory");
413 drv
= bdrv_find_format(fmt
);
415 error("Unknown file format '%s'", fmt
);
419 if (bdrv_open2(bs
, filename
, 0, drv
) < 0) {
420 error("Could not open '%s'", filename
);
422 ret
= bdrv_commit(bs
);
425 printf("Image committed.\n");
428 error("No disk inserted");
431 error("Image is read-only");
434 error("Image is already committed");
437 error("Error while committing image");
445 static int is_not_zero(const uint8_t *sector
, int len
)
449 for(i
= 0;i
< len
; i
++) {
450 if (((uint32_t *)sector
)[i
] != 0)
456 static int is_allocated_sectors(const uint8_t *buf
, int n
, int *pnum
)
464 v
= is_not_zero(buf
, 512);
465 for(i
= 1; i
< n
; i
++) {
467 if (v
!= is_not_zero(buf
, 512))
474 #define IO_BUF_SIZE 65536
476 static int img_convert(int argc
, char **argv
)
478 int c
, ret
, n
, n1
, compress
, cluster_size
, cluster_sectors
, encrypt
;
479 const char *filename
, *fmt
, *out_fmt
, *out_filename
;
481 BlockDriverState
*bs
, *out_bs
;
482 int64_t total_sectors
, nb_sectors
, sector_num
;
483 uint8_t buf
[IO_BUF_SIZE
];
491 c
= getopt(argc
, argv
, "f:O:hce");
514 filename
= argv
[optind
++];
517 out_filename
= argv
[optind
++];
519 bs
= bdrv_new_open(filename
, fmt
);
521 drv
= bdrv_find_format(out_fmt
);
523 error("Unknown file format '%s'", fmt
);
524 if (compress
&& drv
!= &bdrv_qcow
)
525 error("Compression not supported for this file format");
526 if (encrypt
&& drv
!= &bdrv_qcow
)
527 error("Encryption not supported for this file format");
528 if (compress
&& encrypt
)
529 error("Compression and encryption not supported at the same time");
530 bdrv_get_geometry(bs
, &total_sectors
);
531 ret
= bdrv_create(drv
, out_filename
, total_sectors
, NULL
, encrypt
);
533 if (ret
== -ENOTSUP
) {
534 error("Formatting not supported for file format '%s'", fmt
);
536 error("Error while formatting '%s'", out_filename
);
540 out_bs
= bdrv_new_open(out_filename
, out_fmt
);
543 cluster_size
= qcow_get_cluster_size(out_bs
);
544 if (cluster_size
<= 0 || cluster_size
> IO_BUF_SIZE
)
545 error("invalid cluster size");
546 cluster_sectors
= cluster_size
>> 9;
549 nb_sectors
= total_sectors
- sector_num
;
552 if (nb_sectors
>= cluster_sectors
)
556 if (bdrv_read(bs
, sector_num
, buf
, n
) < 0)
557 error("error while reading");
558 if (n
< cluster_sectors
)
559 memset(buf
+ n
* 512, 0, cluster_size
- n
* 512);
560 if (is_not_zero(buf
, cluster_size
)) {
561 if (qcow_compress_cluster(out_bs
, sector_num
, buf
) != 0)
562 error("error while compressing sector %lld", sector_num
);
569 nb_sectors
= total_sectors
- sector_num
;
572 if (nb_sectors
>= (IO_BUF_SIZE
/ 512))
573 n
= (IO_BUF_SIZE
/ 512);
576 if (bdrv_read(bs
, sector_num
, buf
, n
) < 0)
577 error("error while reading");
578 /* NOTE: at the same time we convert, we do not write zero
579 sectors to have a chance to compress the image. Ideally, we
580 should add a specific call to have the info to go faster */
583 if (is_allocated_sectors(buf1
, n
, &n1
)) {
584 if (bdrv_write(out_bs
, sector_num
, buf1
, n1
) < 0)
585 error("error while writing");
599 static int64_t get_allocated_file_size(const char *filename
)
602 if (_stati64(filename
, &st
) < 0)
607 static int64_t get_allocated_file_size(const char *filename
)
610 if (stat(filename
, &st
) < 0)
612 return (int64_t)st
.st_blocks
* 512;
616 static int img_info(int argc
, char **argv
)
619 const char *filename
, *fmt
;
621 BlockDriverState
*bs
;
622 char fmt_name
[128], size_buf
[128], dsize_buf
[128];
623 int64_t total_sectors
, allocated_size
;
627 c
= getopt(argc
, argv
, "f:h");
641 filename
= argv
[optind
++];
645 error("Not enough memory");
647 drv
= bdrv_find_format(fmt
);
649 error("Unknown file format '%s'", fmt
);
653 if (bdrv_open2(bs
, filename
, 0, drv
) < 0) {
654 error("Could not open '%s'", filename
);
656 bdrv_get_format(bs
, fmt_name
, sizeof(fmt_name
));
657 bdrv_get_geometry(bs
, &total_sectors
);
658 get_human_readable_size(size_buf
, sizeof(size_buf
), total_sectors
* 512);
659 allocated_size
= get_allocated_file_size(filename
);
660 if (allocated_size
< 0)
661 sprintf(dsize_buf
, "unavailable");
663 get_human_readable_size(dsize_buf
, sizeof(dsize_buf
),
667 "virtual size: %s (%lld bytes)\n"
669 filename
, fmt_name
, size_buf
,
670 (long long) (total_sectors
* 512),
672 if (bdrv_is_encrypted(bs
))
673 printf("encrypted: yes\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
);