blob: 5ad88bf763d7a04782e916f3efa8e4353f866f49 [file] [log] [blame]
bellardea2384d2004-08-01 21:59:26 +00001/*
bellardfb43f4d2006-08-07 21:34:46 +00002 * QEMU disk image utility
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardea2384d2004-08-01 21:59:26 +00006 * 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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
pbrookfaf07962007-11-11 02:51:17 +000024#include "qemu-common.h"
Kevin Wolf9ea2ea72009-05-18 16:42:11 +020025#include "qemu-option.h"
aliguorif7b4a942009-01-07 17:40:15 +000026#include "osdep.h"
thsec36ba12007-09-16 21:59:02 +000027#include "block_int.h"
aliguori9230eaf2009-03-28 17:55:19 +000028#include <stdio.h>
bellardea2384d2004-08-01 21:59:26 +000029
bellarde8445332006-06-14 15:32:10 +000030#ifdef _WIN32
31#include <windows.h>
32#endif
33
Anthony Liguoric227f092009-10-01 16:12:16 -050034typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010035 const char *name;
36 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050037} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010038
aurel32137519c2008-11-30 19:12:49 +000039/* Default to cache=writeback as data integrity is not important for qemu-tcg. */
40#define BRDV_O_FLAGS BDRV_O_CACHE_WB
41
malca5e50b22009-02-01 22:19:27 +000042static void QEMU_NORETURN error(const char *fmt, ...)
bellardea2384d2004-08-01 21:59:26 +000043{
44 va_list ap;
45 va_start(ap, fmt);
bellard57d1a2b2004-08-03 21:15:11 +000046 fprintf(stderr, "qemu-img: ");
bellardea2384d2004-08-01 21:59:26 +000047 vfprintf(stderr, fmt, ap);
48 fprintf(stderr, "\n");
49 exit(1);
50 va_end(ap);
51}
52
53static void format_print(void *opaque, const char *name)
54{
55 printf(" %s", name);
56}
57
blueswir1d2c639d2009-01-24 18:19:25 +000058/* Please keep in synch with qemu-img.texi */
pbrook3f379ab2007-11-11 03:33:13 +000059static void help(void)
bellardea2384d2004-08-01 21:59:26 +000060{
bellard68d0f702008-01-06 17:21:48 +000061 printf("qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2008 Fabrice Bellard\n"
bellard57d1a2b2004-08-03 21:15:11 +000062 "usage: qemu-img command [command options]\n"
bellardea2384d2004-08-01 21:59:26 +000063 "QEMU disk image utility\n"
64 "\n"
65 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +010066#define DEF(option, callback, arg_string) \
67 " " arg_string "\n"
68#include "qemu-img-cmds.h"
69#undef DEF
70#undef GEN_DOCS
bellardea2384d2004-08-01 21:59:26 +000071 "\n"
72 "Command parameters:\n"
73 " 'filename' is a disk image filename\n"
bellardea2384d2004-08-01 21:59:26 +000074 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Pierre Riteau21eb3a22009-11-27 14:14:56 +010075 " 'size' is the disk image size in bytes. Optional suffixes\n"
76 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M)\n"
77 " and T (terabyte, 1024G) are supported. 'b' is ignored.\n"
bellardea2384d2004-08-01 21:59:26 +000078 " 'output_filename' is the destination disk image filename\n"
79 " 'output_fmt' is the destination format\n"
Kevin Wolfeff44262009-06-04 15:39:39 +020080 " 'options' is a comma separated list of format specific options in a\n"
81 " name=value format. Use -o ? for an overview of the options supported by the\n"
82 " used format\n"
bellardea2384d2004-08-01 21:59:26 +000083 " '-c' indicates that target image must be compressed (qcow format only)\n"
blueswir1d2c639d2009-01-24 18:19:25 +000084 " '-h' with or without a command shows this help and lists the supported formats\n"
aliguorif7b4a942009-01-07 17:40:15 +000085 "\n"
blueswir1d2c639d2009-01-24 18:19:25 +000086 "Parameters to snapshot subcommand:\n"
87 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
88 " '-a' applies a snapshot (revert disk to saved state)\n"
89 " '-c' creates a snapshot\n"
90 " '-d' deletes a snapshot\n"
91 " '-l' lists all snapshots in the given image\n"
bellardea2384d2004-08-01 21:59:26 +000092 );
blueswir1d2c639d2009-01-24 18:19:25 +000093 printf("\nSupported formats:");
bellardea2384d2004-08-01 21:59:26 +000094 bdrv_iterate_format(format_print, NULL);
95 printf("\n");
96 exit(1);
97}
98
bellardea2384d2004-08-01 21:59:26 +000099#if defined(WIN32)
100/* XXX: put correct support for win32 */
101static int read_password(char *buf, int buf_size)
102{
103 int c, i;
104 printf("Password: ");
105 fflush(stdout);
106 i = 0;
107 for(;;) {
108 c = getchar();
109 if (c == '\n')
110 break;
111 if (i < (buf_size - 1))
112 buf[i++] = c;
113 }
114 buf[i] = '\0';
115 return 0;
116}
117
118#else
119
120#include <termios.h>
121
122static struct termios oldtty;
123
124static void term_exit(void)
125{
126 tcsetattr (0, TCSANOW, &oldtty);
127}
128
129static void term_init(void)
130{
131 struct termios tty;
132
133 tcgetattr (0, &tty);
134 oldtty = tty;
135
136 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
137 |INLCR|IGNCR|ICRNL|IXON);
138 tty.c_oflag |= OPOST;
139 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
140 tty.c_cflag &= ~(CSIZE|PARENB);
141 tty.c_cflag |= CS8;
142 tty.c_cc[VMIN] = 1;
143 tty.c_cc[VTIME] = 0;
ths3b46e622007-09-17 08:09:54 +0000144
bellardea2384d2004-08-01 21:59:26 +0000145 tcsetattr (0, TCSANOW, &tty);
146
147 atexit(term_exit);
148}
149
pbrook3f379ab2007-11-11 03:33:13 +0000150static int read_password(char *buf, int buf_size)
bellardea2384d2004-08-01 21:59:26 +0000151{
152 uint8_t ch;
153 int i, ret;
154
155 printf("password: ");
156 fflush(stdout);
157 term_init();
158 i = 0;
159 for(;;) {
160 ret = read(0, &ch, 1);
161 if (ret == -1) {
162 if (errno == EAGAIN || errno == EINTR) {
163 continue;
164 } else {
165 ret = -1;
166 break;
167 }
168 } else if (ret == 0) {
169 ret = -1;
170 break;
171 } else {
172 if (ch == '\r') {
173 ret = 0;
174 break;
175 }
176 if (i < (buf_size - 1))
177 buf[i++] = ch;
178 }
179 }
180 term_exit();
181 buf[i] = '\0';
182 printf("\n");
183 return ret;
184}
185#endif
186
bellard75c23802004-08-27 21:28:58 +0000187static BlockDriverState *bdrv_new_open(const char *filename,
188 const char *fmt)
189{
190 BlockDriverState *bs;
191 BlockDriver *drv;
192 char password[256];
193
194 bs = bdrv_new("");
195 if (!bs)
196 error("Not enough memory");
197 if (fmt) {
198 drv = bdrv_find_format(fmt);
199 if (!drv)
200 error("Unknown file format '%s'", fmt);
201 } else {
202 drv = NULL;
203 }
aurel32137519c2008-11-30 19:12:49 +0000204 if (bdrv_open2(bs, filename, BRDV_O_FLAGS, drv) < 0) {
bellard75c23802004-08-27 21:28:58 +0000205 error("Could not open '%s'", filename);
206 }
207 if (bdrv_is_encrypted(bs)) {
208 printf("Disk image '%s' is encrypted.\n", filename);
209 if (read_password(password, sizeof(password)) < 0)
210 error("No password given");
211 if (bdrv_set_key(bs, password) < 0)
212 error("invalid password");
213 }
214 return bs;
215}
216
Kevin Wolfefa84d42009-05-18 16:42:12 +0200217static void add_old_style_options(const char *fmt, QEMUOptionParameter *list,
218 int flags, const char *base_filename, const char *base_fmt)
219{
220 if (flags & BLOCK_FLAG_ENCRYPT) {
221 if (set_option_parameter(list, BLOCK_OPT_ENCRYPT, "on")) {
222 error("Encryption not supported for file format '%s'", fmt);
223 }
224 }
225 if (flags & BLOCK_FLAG_COMPAT6) {
226 if (set_option_parameter(list, BLOCK_OPT_COMPAT6, "on")) {
227 error("VMDK version 6 not supported for file format '%s'", fmt);
228 }
229 }
230
231 if (base_filename) {
232 if (set_option_parameter(list, BLOCK_OPT_BACKING_FILE, base_filename)) {
233 error("Backing file not supported for file format '%s'", fmt);
234 }
235 }
236 if (base_fmt) {
237 if (set_option_parameter(list, BLOCK_OPT_BACKING_FMT, base_fmt)) {
238 error("Backing file format not supported for file format '%s'", fmt);
239 }
240 }
241}
242
bellardea2384d2004-08-01 21:59:26 +0000243static int img_create(int argc, char **argv)
244{
thsec36ba12007-09-16 21:59:02 +0000245 int c, ret, flags;
bellardea2384d2004-08-01 21:59:26 +0000246 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000247 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000248 const char *filename;
249 const char *base_filename = NULL;
bellardea2384d2004-08-01 21:59:26 +0000250 BlockDriver *drv;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200251 QEMUOptionParameter *param = NULL;
252 char *options = NULL;
ths3b46e622007-09-17 08:09:54 +0000253
thsec36ba12007-09-16 21:59:02 +0000254 flags = 0;
bellardea2384d2004-08-01 21:59:26 +0000255 for(;;) {
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200256 c = getopt(argc, argv, "F:b:f:he6o:");
bellardea2384d2004-08-01 21:59:26 +0000257 if (c == -1)
258 break;
259 switch(c) {
260 case 'h':
261 help();
262 break;
aliguori9230eaf2009-03-28 17:55:19 +0000263 case 'F':
264 base_fmt = optarg;
265 break;
bellardea2384d2004-08-01 21:59:26 +0000266 case 'b':
267 base_filename = optarg;
268 break;
269 case 'f':
270 fmt = optarg;
271 break;
272 case 'e':
thsec36ba12007-09-16 21:59:02 +0000273 flags |= BLOCK_FLAG_ENCRYPT;
bellardea2384d2004-08-01 21:59:26 +0000274 break;
thsd8871c52007-10-24 16:11:42 +0000275 case '6':
thsec36ba12007-09-16 21:59:02 +0000276 flags |= BLOCK_FLAG_COMPAT6;
thsd8871c52007-10-24 16:11:42 +0000277 break;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200278 case 'o':
279 options = optarg;
280 break;
bellardea2384d2004-08-01 21:59:26 +0000281 }
282 }
aliguori9230eaf2009-03-28 17:55:19 +0000283
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200284 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +0000285 drv = bdrv_find_format(fmt);
286 if (!drv)
287 error("Unknown file format '%s'", fmt);
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200288
Kevin Wolfdb08adf2009-06-04 15:39:38 +0200289 if (options && !strcmp(options, "?")) {
290 print_option_help(drv->create_options);
291 return 0;
292 }
293
Kevin Wolf9f566402009-10-28 11:36:07 +0100294 /* Create parameter list with default values */
295 param = parse_option_parameters("", drv->create_options, param);
296 set_option_parameter_int(param, BLOCK_OPT_SIZE, -1);
297
298 /* Parse -o options */
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200299 if (options) {
300 param = parse_option_parameters(options, drv->create_options, param);
301 if (param == NULL) {
302 error("Invalid options for file format '%s'.", fmt);
303 }
bellard75c23802004-08-27 21:28:58 +0000304 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200305
Kevin Wolfdb08adf2009-06-04 15:39:38 +0200306 /* Get the filename */
307 if (optind >= argc)
308 help();
309 filename = argv[optind++];
310
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200311 /* Add size to parameters */
312 if (optind < argc) {
313 set_option_parameter(param, BLOCK_OPT_SIZE, argv[optind++]);
314 }
315
316 /* Add old-style options to parameters */
Kevin Wolfefa84d42009-05-18 16:42:12 +0200317 add_old_style_options(fmt, param, flags, base_filename, base_fmt);
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200318
319 // The size for the image must always be specified, with one exception:
320 // If we are using a backing file, we can obtain the size from there
Kevin Wolf9f566402009-10-28 11:36:07 +0100321 if (get_option_parameter(param, BLOCK_OPT_SIZE)->value.n == -1) {
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200322
323 QEMUOptionParameter *backing_file =
324 get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
325 QEMUOptionParameter *backing_fmt =
326 get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
327
328 if (backing_file && backing_file->value.s) {
329 BlockDriverState *bs;
330 uint64_t size;
331 const char *fmt = NULL;
332 char buf[32];
333
334 if (backing_fmt && backing_fmt->value.s) {
335 if (bdrv_find_format(backing_fmt->value.s)) {
336 fmt = backing_fmt->value.s;
337 } else {
338 error("Unknown backing file format '%s'",
339 backing_fmt->value.s);
340 }
341 }
342
343 bs = bdrv_new_open(backing_file->value.s, fmt);
344 bdrv_get_geometry(bs, &size);
345 size *= 512;
346 bdrv_delete(bs);
347
348 snprintf(buf, sizeof(buf), "%" PRId64, size);
349 set_option_parameter(param, BLOCK_OPT_SIZE, buf);
350 } else {
351 error("Image creation needs a size parameter");
352 }
353 }
354
355 printf("Formatting '%s', fmt=%s ", filename, fmt);
356 print_option_parameters(param);
357 puts("");
358
359 ret = bdrv_create(drv, filename, param);
360 free_option_parameters(param);
361
bellardea2384d2004-08-01 21:59:26 +0000362 if (ret < 0) {
363 if (ret == -ENOTSUP) {
bellard3c565212004-09-29 21:29:14 +0000364 error("Formatting or formatting option not supported for file format '%s'", fmt);
aurel326e9ea0c2009-04-15 14:42:46 +0000365 } else if (ret == -EFBIG) {
366 error("The image size is too large for file format '%s'", fmt);
bellardea2384d2004-08-01 21:59:26 +0000367 } else {
368 error("Error while formatting");
369 }
370 }
371 return 0;
372}
373
aliguori15859692009-04-21 23:11:53 +0000374static int img_check(int argc, char **argv)
375{
376 int c, ret;
377 const char *filename, *fmt;
378 BlockDriver *drv;
379 BlockDriverState *bs;
380
381 fmt = NULL;
382 for(;;) {
383 c = getopt(argc, argv, "f:h");
384 if (c == -1)
385 break;
386 switch(c) {
387 case 'h':
388 help();
389 break;
390 case 'f':
391 fmt = optarg;
392 break;
393 }
394 }
395 if (optind >= argc)
396 help();
397 filename = argv[optind++];
398
399 bs = bdrv_new("");
400 if (!bs)
401 error("Not enough memory");
402 if (fmt) {
403 drv = bdrv_find_format(fmt);
404 if (!drv)
405 error("Unknown file format '%s'", fmt);
406 } else {
407 drv = NULL;
408 }
409 if (bdrv_open2(bs, filename, BRDV_O_FLAGS, drv) < 0) {
410 error("Could not open '%s'", filename);
411 }
412 ret = bdrv_check(bs);
413 switch(ret) {
414 case 0:
415 printf("No errors were found on the image.\n");
416 break;
417 case -ENOTSUP:
418 error("This image format does not support checks");
419 break;
420 default:
421 if (ret < 0) {
422 error("An error occurred during the check");
423 } else {
424 printf("%d errors were found on the image.\n", ret);
425 }
426 break;
427 }
428
429 bdrv_delete(bs);
430 return 0;
431}
432
bellardea2384d2004-08-01 21:59:26 +0000433static int img_commit(int argc, char **argv)
434{
435 int c, ret;
436 const char *filename, *fmt;
437 BlockDriver *drv;
438 BlockDriverState *bs;
439
440 fmt = NULL;
441 for(;;) {
442 c = getopt(argc, argv, "f:h");
443 if (c == -1)
444 break;
445 switch(c) {
446 case 'h':
447 help();
448 break;
449 case 'f':
450 fmt = optarg;
451 break;
452 }
453 }
ths5fafdf22007-09-16 21:08:06 +0000454 if (optind >= argc)
bellardea2384d2004-08-01 21:59:26 +0000455 help();
456 filename = argv[optind++];
457
458 bs = bdrv_new("");
459 if (!bs)
460 error("Not enough memory");
461 if (fmt) {
462 drv = bdrv_find_format(fmt);
463 if (!drv)
464 error("Unknown file format '%s'", fmt);
465 } else {
466 drv = NULL;
467 }
aurel32137519c2008-11-30 19:12:49 +0000468 if (bdrv_open2(bs, filename, BRDV_O_FLAGS, drv) < 0) {
bellardea2384d2004-08-01 21:59:26 +0000469 error("Could not open '%s'", filename);
470 }
471 ret = bdrv_commit(bs);
472 switch(ret) {
473 case 0:
474 printf("Image committed.\n");
475 break;
476 case -ENOENT:
477 error("No disk inserted");
478 break;
479 case -EACCES:
480 error("Image is read-only");
481 break;
482 case -ENOTSUP:
483 error("Image is already committed");
484 break;
485 default:
486 error("Error while committing image");
487 break;
488 }
489
490 bdrv_delete(bs);
491 return 0;
492}
493
494static int is_not_zero(const uint8_t *sector, int len)
495{
496 int i;
497 len >>= 2;
498 for(i = 0;i < len; i++) {
499 if (((uint32_t *)sector)[i] != 0)
500 return 1;
501 }
502 return 0;
503}
504
thsf58c7b32008-06-05 21:53:49 +0000505/*
506 * Returns true iff the first sector pointed to by 'buf' contains at least
507 * a non-NUL byte.
508 *
509 * 'pnum' is set to the number of sectors (including and immediately following
510 * the first one) that are known to be in the same allocated/unallocated state.
511 */
bellardea2384d2004-08-01 21:59:26 +0000512static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
513{
514 int v, i;
515
516 if (n <= 0) {
517 *pnum = 0;
518 return 0;
519 }
520 v = is_not_zero(buf, 512);
521 for(i = 1; i < n; i++) {
522 buf += 512;
523 if (v != is_not_zero(buf, 512))
524 break;
525 }
526 *pnum = i;
527 return v;
528}
529
Kevin Wolf80ee15a2009-09-15 12:30:43 +0200530#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +0000531
532static int img_convert(int argc, char **argv)
533{
balrog926c2d22007-10-31 01:11:44 +0000534 int c, ret, n, n1, bs_n, bs_i, flags, cluster_size, cluster_sectors;
thsf58c7b32008-06-05 21:53:49 +0000535 const char *fmt, *out_fmt, *out_baseimg, *out_filename;
bellardea2384d2004-08-01 21:59:26 +0000536 BlockDriver *drv;
balrog926c2d22007-10-31 01:11:44 +0000537 BlockDriverState **bs, *out_bs;
ths96b8f132007-12-17 01:35:20 +0000538 int64_t total_sectors, nb_sectors, sector_num, bs_offset;
539 uint64_t bs_sectors;
bellardea2384d2004-08-01 21:59:26 +0000540 uint8_t buf[IO_BUF_SIZE];
541 const uint8_t *buf1;
bellardfaea38e2006-08-05 21:31:00 +0000542 BlockDriverInfo bdi;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200543 QEMUOptionParameter *param = NULL;
544 char *options = NULL;
bellardea2384d2004-08-01 21:59:26 +0000545
546 fmt = NULL;
547 out_fmt = "raw";
thsf58c7b32008-06-05 21:53:49 +0000548 out_baseimg = NULL;
thsec36ba12007-09-16 21:59:02 +0000549 flags = 0;
bellardea2384d2004-08-01 21:59:26 +0000550 for(;;) {
Kevin Wolfefa84d42009-05-18 16:42:12 +0200551 c = getopt(argc, argv, "f:O:B:hce6o:");
bellardea2384d2004-08-01 21:59:26 +0000552 if (c == -1)
553 break;
554 switch(c) {
555 case 'h':
556 help();
557 break;
558 case 'f':
559 fmt = optarg;
560 break;
561 case 'O':
562 out_fmt = optarg;
563 break;
thsf58c7b32008-06-05 21:53:49 +0000564 case 'B':
565 out_baseimg = optarg;
566 break;
bellardea2384d2004-08-01 21:59:26 +0000567 case 'c':
thsec36ba12007-09-16 21:59:02 +0000568 flags |= BLOCK_FLAG_COMPRESS;
bellardea2384d2004-08-01 21:59:26 +0000569 break;
570 case 'e':
thsec36ba12007-09-16 21:59:02 +0000571 flags |= BLOCK_FLAG_ENCRYPT;
572 break;
573 case '6':
574 flags |= BLOCK_FLAG_COMPAT6;
bellardea2384d2004-08-01 21:59:26 +0000575 break;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200576 case 'o':
577 options = optarg;
578 break;
bellardea2384d2004-08-01 21:59:26 +0000579 }
580 }
ths3b46e622007-09-17 08:09:54 +0000581
balrog926c2d22007-10-31 01:11:44 +0000582 bs_n = argc - optind - 1;
583 if (bs_n < 1) help();
584
585 out_filename = argv[argc - 1];
thsf58c7b32008-06-05 21:53:49 +0000586
587 if (bs_n > 1 && out_baseimg)
588 error("-B makes no sense when concatenating multiple input images");
balrog926c2d22007-10-31 01:11:44 +0000589
590 bs = calloc(bs_n, sizeof(BlockDriverState *));
591 if (!bs)
592 error("Out of memory");
593
594 total_sectors = 0;
595 for (bs_i = 0; bs_i < bs_n; bs_i++) {
596 bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt);
597 if (!bs[bs_i])
598 error("Could not open '%s'", argv[optind + bs_i]);
599 bdrv_get_geometry(bs[bs_i], &bs_sectors);
600 total_sectors += bs_sectors;
601 }
bellardea2384d2004-08-01 21:59:26 +0000602
Kevin Wolfefa84d42009-05-18 16:42:12 +0200603 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +0000604 drv = bdrv_find_format(out_fmt);
605 if (!drv)
thsd34dda52007-02-10 22:59:40 +0000606 error("Unknown file format '%s'", out_fmt);
balrog926c2d22007-10-31 01:11:44 +0000607
Kevin Wolfdb08adf2009-06-04 15:39:38 +0200608 if (options && !strcmp(options, "?")) {
609 print_option_help(drv->create_options);
Kevin Wolf7078dea2009-11-18 10:48:01 +0100610 free(bs);
Kevin Wolfdb08adf2009-06-04 15:39:38 +0200611 return 0;
612 }
613
Kevin Wolfefa84d42009-05-18 16:42:12 +0200614 if (options) {
615 param = parse_option_parameters(options, drv->create_options, param);
616 if (param == NULL) {
617 error("Invalid options for file format '%s'.", out_fmt);
618 }
619 } else {
620 param = parse_option_parameters("", drv->create_options, param);
621 }
622
623 set_option_parameter_int(param, BLOCK_OPT_SIZE, total_sectors * 512);
624 add_old_style_options(out_fmt, param, flags, out_baseimg, NULL);
625
626 /* Check if compression is supported */
627 if (flags & BLOCK_FLAG_COMPRESS) {
628 QEMUOptionParameter *encryption =
629 get_option_parameter(param, BLOCK_OPT_ENCRYPT);
630
631 if (!drv->bdrv_write_compressed) {
632 error("Compression not supported for this file format");
633 }
634
635 if (encryption && encryption->value.n) {
636 error("Compression and encryption not supported at the same time");
637 }
638 }
639
640 /* Create the new image */
641 ret = bdrv_create(drv, out_filename, param);
642 free_option_parameters(param);
643
bellardea2384d2004-08-01 21:59:26 +0000644 if (ret < 0) {
645 if (ret == -ENOTSUP) {
aliguori93c65b42009-04-05 17:40:43 +0000646 error("Formatting not supported for file format '%s'", out_fmt);
aurel326e9ea0c2009-04-15 14:42:46 +0000647 } else if (ret == -EFBIG) {
648 error("The image size is too large for file format '%s'", out_fmt);
bellardea2384d2004-08-01 21:59:26 +0000649 } else {
650 error("Error while formatting '%s'", out_filename);
651 }
652 }
ths3b46e622007-09-17 08:09:54 +0000653
bellardea2384d2004-08-01 21:59:26 +0000654 out_bs = bdrv_new_open(out_filename, out_fmt);
655
balrog926c2d22007-10-31 01:11:44 +0000656 bs_i = 0;
657 bs_offset = 0;
658 bdrv_get_geometry(bs[0], &bs_sectors);
659
660 if (flags & BLOCK_FLAG_COMPRESS) {
bellardfaea38e2006-08-05 21:31:00 +0000661 if (bdrv_get_info(out_bs, &bdi) < 0)
662 error("could not get block driver info");
663 cluster_size = bdi.cluster_size;
bellardea2384d2004-08-01 21:59:26 +0000664 if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE)
665 error("invalid cluster size");
666 cluster_sectors = cluster_size >> 9;
667 sector_num = 0;
668 for(;;) {
balrog926c2d22007-10-31 01:11:44 +0000669 int64_t bs_num;
670 int remainder;
671 uint8_t *buf2;
672
bellardea2384d2004-08-01 21:59:26 +0000673 nb_sectors = total_sectors - sector_num;
674 if (nb_sectors <= 0)
675 break;
676 if (nb_sectors >= cluster_sectors)
677 n = cluster_sectors;
678 else
679 n = nb_sectors;
balrog926c2d22007-10-31 01:11:44 +0000680
681 bs_num = sector_num - bs_offset;
682 assert (bs_num >= 0);
683 remainder = n;
684 buf2 = buf;
685 while (remainder > 0) {
686 int nlow;
687 while (bs_num == bs_sectors) {
688 bs_i++;
689 assert (bs_i < bs_n);
690 bs_offset += bs_sectors;
691 bdrv_get_geometry(bs[bs_i], &bs_sectors);
692 bs_num = 0;
693 /* printf("changing part: sector_num=%lld, "
694 "bs_i=%d, bs_offset=%lld, bs_sectors=%lld\n",
695 sector_num, bs_i, bs_offset, bs_sectors); */
696 }
697 assert (bs_num < bs_sectors);
698
699 nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder;
700
701 if (bdrv_read(bs[bs_i], bs_num, buf2, nlow) < 0)
702 error("error while reading");
703
704 buf2 += nlow * 512;
705 bs_num += nlow;
706
707 remainder -= nlow;
708 }
709 assert (remainder == 0);
710
bellardea2384d2004-08-01 21:59:26 +0000711 if (n < cluster_sectors)
712 memset(buf + n * 512, 0, cluster_size - n * 512);
713 if (is_not_zero(buf, cluster_size)) {
ths5fafdf22007-09-16 21:08:06 +0000714 if (bdrv_write_compressed(out_bs, sector_num, buf,
bellardfaea38e2006-08-05 21:31:00 +0000715 cluster_sectors) != 0)
bellardec3757d2006-06-14 15:50:07 +0000716 error("error while compressing sector %" PRId64,
717 sector_num);
bellardea2384d2004-08-01 21:59:26 +0000718 }
719 sector_num += n;
720 }
bellardfaea38e2006-08-05 21:31:00 +0000721 /* signal EOF to align */
722 bdrv_write_compressed(out_bs, 0, NULL, 0);
bellardea2384d2004-08-01 21:59:26 +0000723 } else {
thsf58c7b32008-06-05 21:53:49 +0000724 sector_num = 0; // total number of sectors converted so far
bellardea2384d2004-08-01 21:59:26 +0000725 for(;;) {
726 nb_sectors = total_sectors - sector_num;
727 if (nb_sectors <= 0)
728 break;
729 if (nb_sectors >= (IO_BUF_SIZE / 512))
730 n = (IO_BUF_SIZE / 512);
731 else
732 n = nb_sectors;
balrog926c2d22007-10-31 01:11:44 +0000733
734 while (sector_num - bs_offset >= bs_sectors) {
735 bs_i ++;
736 assert (bs_i < bs_n);
737 bs_offset += bs_sectors;
738 bdrv_get_geometry(bs[bs_i], &bs_sectors);
739 /* printf("changing part: sector_num=%lld, bs_i=%d, "
740 "bs_offset=%lld, bs_sectors=%lld\n",
741 sector_num, bs_i, bs_offset, bs_sectors); */
742 }
743
744 if (n > bs_offset + bs_sectors - sector_num)
745 n = bs_offset + bs_sectors - sector_num;
746
Kevin Wolf12c09b82009-11-30 16:54:15 +0100747 if (!drv->no_zero_init) {
Akkarit Sangpetchd0320442009-07-17 10:02:15 +0200748 /* If the output image is being created as a copy on write image,
749 assume that sectors which are unallocated in the input image
750 are present in both the output's and input's base images (no
751 need to copy them). */
752 if (out_baseimg) {
753 if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset,
754 n, &n1)) {
755 sector_num += n1;
756 continue;
757 }
758 /* The next 'n1' sectors are allocated in the input image. Copy
759 only those as they may be followed by unallocated sectors. */
760 n = n1;
aliguori93c65b42009-04-05 17:40:43 +0000761 }
aliguori93c65b42009-04-05 17:40:43 +0000762 } else {
763 n1 = n;
thsf58c7b32008-06-05 21:53:49 +0000764 }
765
balrog926c2d22007-10-31 01:11:44 +0000766 if (bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n) < 0)
bellardea2384d2004-08-01 21:59:26 +0000767 error("error while reading");
768 /* NOTE: at the same time we convert, we do not write zero
769 sectors to have a chance to compress the image. Ideally, we
770 should add a specific call to have the info to go faster */
771 buf1 = buf;
772 while (n > 0) {
thsf58c7b32008-06-05 21:53:49 +0000773 /* If the output image is being created as a copy on write image,
774 copy all sectors even the ones containing only NUL bytes,
aliguori93c65b42009-04-05 17:40:43 +0000775 because they may differ from the sectors in the base image.
776
777 If the output is to a host device, we also write out
778 sectors that are entirely 0, since whatever data was
779 already there is garbage, not 0s. */
Kevin Wolf12c09b82009-11-30 16:54:15 +0100780 if (drv->no_zero_init || out_baseimg ||
aliguori93c65b42009-04-05 17:40:43 +0000781 is_allocated_sectors(buf1, n, &n1)) {
ths5fafdf22007-09-16 21:08:06 +0000782 if (bdrv_write(out_bs, sector_num, buf1, n1) < 0)
bellardea2384d2004-08-01 21:59:26 +0000783 error("error while writing");
784 }
785 sector_num += n1;
786 n -= n1;
787 buf1 += n1 * 512;
788 }
789 }
790 }
791 bdrv_delete(out_bs);
balrog926c2d22007-10-31 01:11:44 +0000792 for (bs_i = 0; bs_i < bs_n; bs_i++)
793 bdrv_delete(bs[bs_i]);
794 free(bs);
bellardea2384d2004-08-01 21:59:26 +0000795 return 0;
796}
797
bellard57d1a2b2004-08-03 21:15:11 +0000798#ifdef _WIN32
799static int64_t get_allocated_file_size(const char *filename)
800{
bellarde8445332006-06-14 15:32:10 +0000801 typedef DWORD (WINAPI * get_compressed_t)(const char *filename, DWORD *high);
802 get_compressed_t get_compressed;
bellard57d1a2b2004-08-03 21:15:11 +0000803 struct _stati64 st;
bellarde8445332006-06-14 15:32:10 +0000804
805 /* WinNT support GetCompressedFileSize to determine allocate size */
806 get_compressed = (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"), "GetCompressedFileSizeA");
807 if (get_compressed) {
808 DWORD high, low;
809 low = get_compressed(filename, &high);
810 if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR)
811 return (((int64_t) high) << 32) + low;
812 }
813
ths5fafdf22007-09-16 21:08:06 +0000814 if (_stati64(filename, &st) < 0)
bellard57d1a2b2004-08-03 21:15:11 +0000815 return -1;
816 return st.st_size;
817}
818#else
819static int64_t get_allocated_file_size(const char *filename)
820{
821 struct stat st;
ths5fafdf22007-09-16 21:08:06 +0000822 if (stat(filename, &st) < 0)
bellard57d1a2b2004-08-03 21:15:11 +0000823 return -1;
824 return (int64_t)st.st_blocks * 512;
825}
826#endif
827
bellardfaea38e2006-08-05 21:31:00 +0000828static void dump_snapshots(BlockDriverState *bs)
829{
830 QEMUSnapshotInfo *sn_tab, *sn;
831 int nb_sns, i;
832 char buf[256];
833
834 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
835 if (nb_sns <= 0)
836 return;
837 printf("Snapshot list:\n");
838 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
839 for(i = 0; i < nb_sns; i++) {
840 sn = &sn_tab[i];
841 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
842 }
843 qemu_free(sn_tab);
844}
845
bellardea2384d2004-08-01 21:59:26 +0000846static int img_info(int argc, char **argv)
847{
848 int c;
849 const char *filename, *fmt;
850 BlockDriver *drv;
851 BlockDriverState *bs;
852 char fmt_name[128], size_buf[128], dsize_buf[128];
ths96b8f132007-12-17 01:35:20 +0000853 uint64_t total_sectors;
854 int64_t allocated_size;
bellard93b6b2a2006-08-01 15:51:11 +0000855 char backing_filename[1024];
856 char backing_filename2[1024];
bellardfaea38e2006-08-05 21:31:00 +0000857 BlockDriverInfo bdi;
bellardea2384d2004-08-01 21:59:26 +0000858
859 fmt = NULL;
860 for(;;) {
861 c = getopt(argc, argv, "f:h");
862 if (c == -1)
863 break;
864 switch(c) {
865 case 'h':
866 help();
867 break;
868 case 'f':
869 fmt = optarg;
870 break;
871 }
872 }
ths5fafdf22007-09-16 21:08:06 +0000873 if (optind >= argc)
bellardea2384d2004-08-01 21:59:26 +0000874 help();
875 filename = argv[optind++];
876
877 bs = bdrv_new("");
878 if (!bs)
879 error("Not enough memory");
880 if (fmt) {
881 drv = bdrv_find_format(fmt);
882 if (!drv)
883 error("Unknown file format '%s'", fmt);
884 } else {
885 drv = NULL;
886 }
Kevin Wolfb783e402010-01-12 12:55:16 +0100887 if (bdrv_open2(bs, filename, BRDV_O_FLAGS | BDRV_O_NO_BACKING, drv) < 0) {
bellardea2384d2004-08-01 21:59:26 +0000888 error("Could not open '%s'", filename);
889 }
890 bdrv_get_format(bs, fmt_name, sizeof(fmt_name));
891 bdrv_get_geometry(bs, &total_sectors);
892 get_human_readable_size(size_buf, sizeof(size_buf), total_sectors * 512);
bellard57d1a2b2004-08-03 21:15:11 +0000893 allocated_size = get_allocated_file_size(filename);
894 if (allocated_size < 0)
blueswir1a10ea302008-08-24 10:30:33 +0000895 snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
bellardde167e42005-04-28 21:15:08 +0000896 else
ths5fafdf22007-09-16 21:08:06 +0000897 get_human_readable_size(dsize_buf, sizeof(dsize_buf),
bellardde167e42005-04-28 21:15:08 +0000898 allocated_size);
bellardea2384d2004-08-01 21:59:26 +0000899 printf("image: %s\n"
900 "file format: %s\n"
bellardec3757d2006-06-14 15:50:07 +0000901 "virtual size: %s (%" PRId64 " bytes)\n"
bellardea2384d2004-08-01 21:59:26 +0000902 "disk size: %s\n",
ths5fafdf22007-09-16 21:08:06 +0000903 filename, fmt_name, size_buf,
bellardec3757d2006-06-14 15:50:07 +0000904 (total_sectors * 512),
bellardea2384d2004-08-01 21:59:26 +0000905 dsize_buf);
906 if (bdrv_is_encrypted(bs))
907 printf("encrypted: yes\n");
bellardfaea38e2006-08-05 21:31:00 +0000908 if (bdrv_get_info(bs, &bdi) >= 0) {
ths5fafdf22007-09-16 21:08:06 +0000909 if (bdi.cluster_size != 0)
bellardfaea38e2006-08-05 21:31:00 +0000910 printf("cluster_size: %d\n", bdi.cluster_size);
911 }
bellard93b6b2a2006-08-01 15:51:11 +0000912 bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
bellardfaea38e2006-08-05 21:31:00 +0000913 if (backing_filename[0] != '\0') {
bellard93b6b2a2006-08-01 15:51:11 +0000914 path_combine(backing_filename2, sizeof(backing_filename2),
915 filename, backing_filename);
ths5fafdf22007-09-16 21:08:06 +0000916 printf("backing file: %s (actual path: %s)\n",
bellard93b6b2a2006-08-01 15:51:11 +0000917 backing_filename,
918 backing_filename2);
bellardfaea38e2006-08-05 21:31:00 +0000919 }
920 dump_snapshots(bs);
bellardea2384d2004-08-01 21:59:26 +0000921 bdrv_delete(bs);
922 return 0;
923}
924
aliguorif7b4a942009-01-07 17:40:15 +0000925#define SNAPSHOT_LIST 1
926#define SNAPSHOT_CREATE 2
927#define SNAPSHOT_APPLY 3
928#define SNAPSHOT_DELETE 4
929
Stuart Brady153859b2009-06-07 00:42:17 +0100930static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +0000931{
932 BlockDriverState *bs;
933 QEMUSnapshotInfo sn;
934 char *filename, *snapshot_name = NULL;
aliguori40a45392009-01-15 21:42:12 +0000935 int c, ret;
aliguorif7b4a942009-01-07 17:40:15 +0000936 int action = 0;
937 qemu_timeval tv;
938
939 /* Parse commandline parameters */
940 for(;;) {
941 c = getopt(argc, argv, "la:c:d:h");
942 if (c == -1)
943 break;
944 switch(c) {
945 case 'h':
946 help();
Stuart Brady153859b2009-06-07 00:42:17 +0100947 return 0;
aliguorif7b4a942009-01-07 17:40:15 +0000948 case 'l':
949 if (action) {
950 help();
Stuart Brady153859b2009-06-07 00:42:17 +0100951 return 0;
aliguorif7b4a942009-01-07 17:40:15 +0000952 }
953 action = SNAPSHOT_LIST;
954 break;
955 case 'a':
956 if (action) {
957 help();
Stuart Brady153859b2009-06-07 00:42:17 +0100958 return 0;
aliguorif7b4a942009-01-07 17:40:15 +0000959 }
960 action = SNAPSHOT_APPLY;
961 snapshot_name = optarg;
962 break;
963 case 'c':
964 if (action) {
965 help();
Stuart Brady153859b2009-06-07 00:42:17 +0100966 return 0;
aliguorif7b4a942009-01-07 17:40:15 +0000967 }
968 action = SNAPSHOT_CREATE;
969 snapshot_name = optarg;
970 break;
971 case 'd':
972 if (action) {
973 help();
Stuart Brady153859b2009-06-07 00:42:17 +0100974 return 0;
aliguorif7b4a942009-01-07 17:40:15 +0000975 }
976 action = SNAPSHOT_DELETE;
977 snapshot_name = optarg;
978 break;
979 }
980 }
981
982 if (optind >= argc)
983 help();
984 filename = argv[optind++];
985
986 /* Open the image */
987 bs = bdrv_new("");
988 if (!bs)
989 error("Not enough memory");
990
991 if (bdrv_open2(bs, filename, 0, NULL) < 0) {
992 error("Could not open '%s'", filename);
993 }
994
995 /* Perform the requested action */
996 switch(action) {
997 case SNAPSHOT_LIST:
998 dump_snapshots(bs);
999 break;
1000
1001 case SNAPSHOT_CREATE:
1002 memset(&sn, 0, sizeof(sn));
1003 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
1004
1005 qemu_gettimeofday(&tv);
1006 sn.date_sec = tv.tv_sec;
1007 sn.date_nsec = tv.tv_usec * 1000;
1008
1009 ret = bdrv_snapshot_create(bs, &sn);
1010 if (ret)
1011 error("Could not create snapshot '%s': %d (%s)",
1012 snapshot_name, ret, strerror(-ret));
1013 break;
1014
1015 case SNAPSHOT_APPLY:
1016 ret = bdrv_snapshot_goto(bs, snapshot_name);
1017 if (ret)
1018 error("Could not apply snapshot '%s': %d (%s)",
1019 snapshot_name, ret, strerror(-ret));
1020 break;
1021
1022 case SNAPSHOT_DELETE:
1023 ret = bdrv_snapshot_delete(bs, snapshot_name);
1024 if (ret)
1025 error("Could not delete snapshot '%s': %d (%s)",
1026 snapshot_name, ret, strerror(-ret));
1027 break;
1028 }
1029
1030 /* Cleanup */
1031 bdrv_delete(bs);
Stuart Brady153859b2009-06-07 00:42:17 +01001032
1033 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00001034}
1035
Anthony Liguoric227f092009-10-01 16:12:16 -05001036static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01001037#define DEF(option, callback, arg_string) \
1038 { option, callback },
1039#include "qemu-img-cmds.h"
1040#undef DEF
1041#undef GEN_DOCS
1042 { NULL, NULL, },
1043};
1044
bellardea2384d2004-08-01 21:59:26 +00001045int main(int argc, char **argv)
1046{
Anthony Liguoric227f092009-10-01 16:12:16 -05001047 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01001048 const char *cmdname;
bellardea2384d2004-08-01 21:59:26 +00001049
1050 bdrv_init();
1051 if (argc < 2)
1052 help();
Stuart Brady153859b2009-06-07 00:42:17 +01001053 cmdname = argv[1];
aurel328f9b1572009-02-09 18:14:31 +00001054 argc--; argv++;
Stuart Brady153859b2009-06-07 00:42:17 +01001055
1056 /* find the command */
1057 for(cmd = img_cmds; cmd->name != NULL; cmd++) {
1058 if (!strcmp(cmdname, cmd->name)) {
1059 return cmd->handler(argc, argv);
1060 }
bellardea2384d2004-08-01 21:59:26 +00001061 }
Stuart Brady153859b2009-06-07 00:42:17 +01001062
1063 /* not found */
1064 help();
bellardea2384d2004-08-01 21:59:26 +00001065 return 0;
1066}