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, "qemuimg: ");
117 vfprintf(stderr, fmt, ap);
118 fprintf(stderr, "\n");
123 static void format_print(void *opaque, const char *name)
130 printf("qemuimg version " QEMU_VERSION ", Copyright (c) 2004 Fabrice Bellard\n"
131 "usage: qemuimg 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", 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 (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 int img_create(int argc, char **argv)
278 int c, ret, encrypted;
279 const char *fmt = "raw";
280 const char *filename;
281 const char *base_filename = NULL;
288 c = getopt(argc, argv, "b:f:he");
296 base_filename = optarg;
309 filename = argv[optind++];
311 if (!base_filename) {
315 size = strtoul(p, (char **)&p, 0);
318 } else if (*p == 'G') {
319 size *= 1024 * 1024 * 1024;
320 } else if (*p == 'k' || *p == 'K' || *p == '\0') {
326 drv = bdrv_find_format(fmt);
328 error("Unknown file format '%s'", fmt);
329 printf("Formating '%s', fmt=%s",
332 printf(", encrypted");
334 printf(", backing_file=%s\n",
337 printf(", size=%lld kB\n", size / 1024);
338 ret = bdrv_create(drv, filename, size / 512, base_filename, encrypted);
340 if (ret == -ENOTSUP) {
341 error("Formatting or formatting option not suppored for file format '%s'", fmt);
343 error("Error while formatting");
349 static int img_commit(int argc, char **argv)
352 const char *filename, *fmt;
354 BlockDriverState *bs;
358 c = getopt(argc, argv, "f:h");
373 filename = argv[optind++];
377 error("Not enough memory");
379 drv = bdrv_find_format(fmt);
381 error("Unknown file format '%s'", fmt);
385 if (bdrv_open2(bs, filename, 0, drv) < 0) {
386 error("Could not open '%s'", filename);
388 ret = bdrv_commit(bs);
391 printf("Image committed.\n");
394 error("No disk inserted");
397 error("Image is read-only");
400 error("Image is already committed");
403 error("Error while committing image");
411 static int is_not_zero(const uint8_t *sector, int len)
415 for(i = 0;i < len; i++) {
416 if (((uint32_t *)sector)[i] != 0)
422 static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
430 v = is_not_zero(buf, 512);
431 for(i = 1; i < n; i++) {
433 if (v != is_not_zero(buf, 512))
440 static BlockDriverState *bdrv_new_open(const char *filename,
443 BlockDriverState *bs;
449 error("Not enough memory");
451 drv = bdrv_find_format(fmt);
453 error("Unknown file format '%s'", fmt);
457 if (bdrv_open2(bs, filename, 0, drv) < 0) {
458 error("Could not open '%s'", filename);
460 if (bdrv_is_encrypted(bs)) {
461 printf("Disk image '%s' is encrypted.\n", filename);
462 if (read_password(password, sizeof(password)) < 0)
463 error("No password given");
464 if (bdrv_set_key(bs, password) < 0)
465 error("invalid password");
470 #define IO_BUF_SIZE 65536
472 static int img_convert(int argc, char **argv)
474 int c, ret, n, n1, compress, cluster_size, cluster_sectors, encrypt;
475 const char *filename, *fmt, *out_fmt, *out_filename;
477 BlockDriverState *bs, *out_bs;
478 int64_t total_sectors, nb_sectors, sector_num;
479 uint8_t buf[IO_BUF_SIZE];
487 c = getopt(argc, argv, "f:O:hce");
511 filename = argv[optind++];
514 out_filename = argv[optind++];
516 bs = bdrv_new_open(filename, fmt);
518 drv = bdrv_find_format(out_fmt);
520 error("Unknown file format '%s'", fmt);
521 if (compress && drv != &bdrv_qcow)
522 error("Compression not supported for this file format");
523 if (encrypt && drv != &bdrv_qcow)
524 error("Encryption not supported for this file format");
525 if (compress && encrypt)
526 error("Compression and encryption not supported at the same time");
527 bdrv_get_geometry(bs, &total_sectors);
528 ret = bdrv_create(drv, out_filename, total_sectors, NULL, encrypt);
530 if (ret == -ENOTSUP) {
531 error("Formatting not suppored for file format '%s'", fmt);
533 error("Error while formatting '%s'", out_filename);
537 out_bs = bdrv_new_open(out_filename, out_fmt);
540 cluster_size = qcow_get_cluster_size(out_bs);
541 if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE)
542 error("invalid cluster size");
543 cluster_sectors = cluster_size >> 9;
546 nb_sectors = total_sectors - sector_num;
549 if (nb_sectors >= cluster_sectors)
553 if (bdrv_read(bs, sector_num, buf, n) < 0)
554 error("error while reading");
555 if (n < cluster_sectors)
556 memset(buf + n * 512, 0, cluster_size - n * 512);
557 if (is_not_zero(buf, cluster_size)) {
558 if (qcow_compress_cluster(out_bs, sector_num, buf) != 0)
559 error("error while compressing sector %lld", sector_num);
566 nb_sectors = total_sectors - sector_num;
569 if (nb_sectors >= (IO_BUF_SIZE / 512))
570 n = (IO_BUF_SIZE / 512);
573 if (bdrv_read(bs, sector_num, buf, n) < 0)
574 error("error while reading");
575 /* NOTE: at the same time we convert, we do not write zero
576 sectors to have a chance to compress the image. Ideally, we
577 should add a specific call to have the info to go faster */
580 if (is_allocated_sectors(buf1, n, &n1)) {
581 if (bdrv_write(out_bs, sector_num, buf1, n1) < 0)
582 error("error while writing");
595 static int img_info(int argc, char **argv)
598 const char *filename, *fmt;
600 BlockDriverState *bs;
601 char fmt_name[128], size_buf[128], dsize_buf[128];
602 int64_t total_sectors;
607 c = getopt(argc, argv, "f:h");
622 filename = argv[optind++];
626 error("Not enough memory");
628 drv = bdrv_find_format(fmt);
630 error("Unknown file format '%s'", fmt);
634 if (bdrv_open2(bs, filename, 0, drv) < 0) {
635 error("Could not open '%s'", filename);
637 bdrv_get_format(bs, fmt_name, sizeof(fmt_name));
638 bdrv_get_geometry(bs, &total_sectors);
639 get_human_readable_size(size_buf, sizeof(size_buf), total_sectors * 512);
640 if (stat(filename, &st) < 0)
641 error("Could not stat '%s'", filename);
642 get_human_readable_size(dsize_buf, sizeof(dsize_buf),
643 (int64_t)st.st_blocks * 512);
646 "virtual size: %s (%lld bytes)\n"
648 filename, fmt_name, size_buf,
651 if (bdrv_is_encrypted(bs))
652 printf("encrypted: yes\n");
657 int main(int argc, char **argv)
665 if (!strcmp(cmd, "create")) {
666 img_create(argc, argv);
667 } else if (!strcmp(cmd, "commit")) {
668 img_commit(argc, argv);
669 } else if (!strcmp(cmd, "convert")) {
670 img_convert(argc, argv);
671 } else if (!strcmp(cmd, "info")) {
672 img_info(argc, argv);