blob: 204f618c6e86820df5342f5a5398d5518ca0d2cc [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"
74 " 'base_image' is the read-only disk image which is used as base for a copy on\n"
75 " write image; the copy on write image only stores the modified data\n"
thsf58c7b32008-06-05 21:53:49 +000076 " 'output_base_image' forces the output image to be created as a copy on write\n"
77 " image of the specified base image; 'output_base_image' should have the same\n"
78 " content as the input's base image, however the path, image format, etc may\n"
79 " differ\n"
bellardea2384d2004-08-01 21:59:26 +000080 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
blueswir1d2c639d2009-01-24 18:19:25 +000081 " 'size' is the disk image size in kilobytes. Optional suffixes\n"
aurel322fbc4092009-03-08 19:49:51 +000082 " 'M' (megabyte, 1024 * 1024) and 'G' (gigabyte, 1024 * 1024 * 1024) are\n"
83 " supported any 'k' or 'K' is ignored\n"
bellardea2384d2004-08-01 21:59:26 +000084 " 'output_filename' is the destination disk image filename\n"
85 " 'output_fmt' is the destination format\n"
Kevin Wolfeff44262009-06-04 15:39:39 +020086 " 'options' is a comma separated list of format specific options in a\n"
87 " name=value format. Use -o ? for an overview of the options supported by the\n"
88 " used format\n"
bellardea2384d2004-08-01 21:59:26 +000089 " '-c' indicates that target image must be compressed (qcow format only)\n"
blueswir1d2c639d2009-01-24 18:19:25 +000090 " '-h' with or without a command shows this help and lists the supported formats\n"
aliguorif7b4a942009-01-07 17:40:15 +000091 "\n"
blueswir1d2c639d2009-01-24 18:19:25 +000092 "Parameters to snapshot subcommand:\n"
93 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
94 " '-a' applies a snapshot (revert disk to saved state)\n"
95 " '-c' creates a snapshot\n"
96 " '-d' deletes a snapshot\n"
97 " '-l' lists all snapshots in the given image\n"
bellardea2384d2004-08-01 21:59:26 +000098 );
blueswir1d2c639d2009-01-24 18:19:25 +000099 printf("\nSupported formats:");
bellardea2384d2004-08-01 21:59:26 +0000100 bdrv_iterate_format(format_print, NULL);
101 printf("\n");
102 exit(1);
103}
104
bellardea2384d2004-08-01 21:59:26 +0000105#if defined(WIN32)
106/* XXX: put correct support for win32 */
107static int read_password(char *buf, int buf_size)
108{
109 int c, i;
110 printf("Password: ");
111 fflush(stdout);
112 i = 0;
113 for(;;) {
114 c = getchar();
115 if (c == '\n')
116 break;
117 if (i < (buf_size - 1))
118 buf[i++] = c;
119 }
120 buf[i] = '\0';
121 return 0;
122}
123
124#else
125
126#include <termios.h>
127
128static struct termios oldtty;
129
130static void term_exit(void)
131{
132 tcsetattr (0, TCSANOW, &oldtty);
133}
134
135static void term_init(void)
136{
137 struct termios tty;
138
139 tcgetattr (0, &tty);
140 oldtty = tty;
141
142 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
143 |INLCR|IGNCR|ICRNL|IXON);
144 tty.c_oflag |= OPOST;
145 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
146 tty.c_cflag &= ~(CSIZE|PARENB);
147 tty.c_cflag |= CS8;
148 tty.c_cc[VMIN] = 1;
149 tty.c_cc[VTIME] = 0;
ths3b46e622007-09-17 08:09:54 +0000150
bellardea2384d2004-08-01 21:59:26 +0000151 tcsetattr (0, TCSANOW, &tty);
152
153 atexit(term_exit);
154}
155
pbrook3f379ab2007-11-11 03:33:13 +0000156static int read_password(char *buf, int buf_size)
bellardea2384d2004-08-01 21:59:26 +0000157{
158 uint8_t ch;
159 int i, ret;
160
161 printf("password: ");
162 fflush(stdout);
163 term_init();
164 i = 0;
165 for(;;) {
166 ret = read(0, &ch, 1);
167 if (ret == -1) {
168 if (errno == EAGAIN || errno == EINTR) {
169 continue;
170 } else {
171 ret = -1;
172 break;
173 }
174 } else if (ret == 0) {
175 ret = -1;
176 break;
177 } else {
178 if (ch == '\r') {
179 ret = 0;
180 break;
181 }
182 if (i < (buf_size - 1))
183 buf[i++] = ch;
184 }
185 }
186 term_exit();
187 buf[i] = '\0';
188 printf("\n");
189 return ret;
190}
191#endif
192
bellard75c23802004-08-27 21:28:58 +0000193static BlockDriverState *bdrv_new_open(const char *filename,
194 const char *fmt)
195{
196 BlockDriverState *bs;
197 BlockDriver *drv;
198 char password[256];
199
200 bs = bdrv_new("");
201 if (!bs)
202 error("Not enough memory");
203 if (fmt) {
204 drv = bdrv_find_format(fmt);
205 if (!drv)
206 error("Unknown file format '%s'", fmt);
207 } else {
208 drv = NULL;
209 }
aurel32137519c2008-11-30 19:12:49 +0000210 if (bdrv_open2(bs, filename, BRDV_O_FLAGS, drv) < 0) {
bellard75c23802004-08-27 21:28:58 +0000211 error("Could not open '%s'", filename);
212 }
213 if (bdrv_is_encrypted(bs)) {
214 printf("Disk image '%s' is encrypted.\n", filename);
215 if (read_password(password, sizeof(password)) < 0)
216 error("No password given");
217 if (bdrv_set_key(bs, password) < 0)
218 error("invalid password");
219 }
220 return bs;
221}
222
Kevin Wolfefa84d42009-05-18 16:42:12 +0200223static void add_old_style_options(const char *fmt, QEMUOptionParameter *list,
224 int flags, const char *base_filename, const char *base_fmt)
225{
226 if (flags & BLOCK_FLAG_ENCRYPT) {
227 if (set_option_parameter(list, BLOCK_OPT_ENCRYPT, "on")) {
228 error("Encryption not supported for file format '%s'", fmt);
229 }
230 }
231 if (flags & BLOCK_FLAG_COMPAT6) {
232 if (set_option_parameter(list, BLOCK_OPT_COMPAT6, "on")) {
233 error("VMDK version 6 not supported for file format '%s'", fmt);
234 }
235 }
236
237 if (base_filename) {
238 if (set_option_parameter(list, BLOCK_OPT_BACKING_FILE, base_filename)) {
239 error("Backing file not supported for file format '%s'", fmt);
240 }
241 }
242 if (base_fmt) {
243 if (set_option_parameter(list, BLOCK_OPT_BACKING_FMT, base_fmt)) {
244 error("Backing file format not supported for file format '%s'", fmt);
245 }
246 }
247}
248
bellardea2384d2004-08-01 21:59:26 +0000249static int img_create(int argc, char **argv)
250{
thsec36ba12007-09-16 21:59:02 +0000251 int c, ret, flags;
bellardea2384d2004-08-01 21:59:26 +0000252 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000253 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000254 const char *filename;
255 const char *base_filename = NULL;
bellardea2384d2004-08-01 21:59:26 +0000256 BlockDriver *drv;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200257 QEMUOptionParameter *param = NULL;
258 char *options = NULL;
ths3b46e622007-09-17 08:09:54 +0000259
thsec36ba12007-09-16 21:59:02 +0000260 flags = 0;
bellardea2384d2004-08-01 21:59:26 +0000261 for(;;) {
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200262 c = getopt(argc, argv, "F:b:f:he6o:");
bellardea2384d2004-08-01 21:59:26 +0000263 if (c == -1)
264 break;
265 switch(c) {
266 case 'h':
267 help();
268 break;
aliguori9230eaf2009-03-28 17:55:19 +0000269 case 'F':
270 base_fmt = optarg;
271 break;
bellardea2384d2004-08-01 21:59:26 +0000272 case 'b':
273 base_filename = optarg;
274 break;
275 case 'f':
276 fmt = optarg;
277 break;
278 case 'e':
thsec36ba12007-09-16 21:59:02 +0000279 flags |= BLOCK_FLAG_ENCRYPT;
bellardea2384d2004-08-01 21:59:26 +0000280 break;
thsd8871c52007-10-24 16:11:42 +0000281 case '6':
thsec36ba12007-09-16 21:59:02 +0000282 flags |= BLOCK_FLAG_COMPAT6;
thsd8871c52007-10-24 16:11:42 +0000283 break;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200284 case 'o':
285 options = optarg;
286 break;
bellardea2384d2004-08-01 21:59:26 +0000287 }
288 }
aliguori9230eaf2009-03-28 17:55:19 +0000289
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200290 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +0000291 drv = bdrv_find_format(fmt);
292 if (!drv)
293 error("Unknown file format '%s'", fmt);
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200294
Kevin Wolfdb08adf2009-06-04 15:39:38 +0200295 if (options && !strcmp(options, "?")) {
296 print_option_help(drv->create_options);
297 return 0;
298 }
299
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200300 if (options) {
301 param = parse_option_parameters(options, drv->create_options, param);
302 if (param == NULL) {
303 error("Invalid options for file format '%s'.", fmt);
304 }
305 } else {
306 param = parse_option_parameters("", drv->create_options, param);
bellard75c23802004-08-27 21:28:58 +0000307 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200308
Kevin Wolfdb08adf2009-06-04 15:39:38 +0200309 /* Get the filename */
310 if (optind >= argc)
311 help();
312 filename = argv[optind++];
313
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200314 /* Add size to parameters */
315 if (optind < argc) {
316 set_option_parameter(param, BLOCK_OPT_SIZE, argv[optind++]);
317 }
318
319 /* Add old-style options to parameters */
Kevin Wolfefa84d42009-05-18 16:42:12 +0200320 add_old_style_options(fmt, param, flags, base_filename, base_fmt);
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200321
322 // The size for the image must always be specified, with one exception:
323 // If we are using a backing file, we can obtain the size from there
324 if (get_option_parameter(param, BLOCK_OPT_SIZE)->value.n == 0) {
325
326 QEMUOptionParameter *backing_file =
327 get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
328 QEMUOptionParameter *backing_fmt =
329 get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
330
331 if (backing_file && backing_file->value.s) {
332 BlockDriverState *bs;
333 uint64_t size;
334 const char *fmt = NULL;
335 char buf[32];
336
337 if (backing_fmt && backing_fmt->value.s) {
338 if (bdrv_find_format(backing_fmt->value.s)) {
339 fmt = backing_fmt->value.s;
340 } else {
341 error("Unknown backing file format '%s'",
342 backing_fmt->value.s);
343 }
344 }
345
346 bs = bdrv_new_open(backing_file->value.s, fmt);
347 bdrv_get_geometry(bs, &size);
348 size *= 512;
349 bdrv_delete(bs);
350
351 snprintf(buf, sizeof(buf), "%" PRId64, size);
352 set_option_parameter(param, BLOCK_OPT_SIZE, buf);
353 } else {
354 error("Image creation needs a size parameter");
355 }
356 }
357
358 printf("Formatting '%s', fmt=%s ", filename, fmt);
359 print_option_parameters(param);
360 puts("");
361
362 ret = bdrv_create(drv, filename, param);
363 free_option_parameters(param);
364
bellardea2384d2004-08-01 21:59:26 +0000365 if (ret < 0) {
366 if (ret == -ENOTSUP) {
bellard3c565212004-09-29 21:29:14 +0000367 error("Formatting or formatting option not supported for file format '%s'", fmt);
aurel326e9ea0c2009-04-15 14:42:46 +0000368 } else if (ret == -EFBIG) {
369 error("The image size is too large for file format '%s'", fmt);
bellardea2384d2004-08-01 21:59:26 +0000370 } else {
371 error("Error while formatting");
372 }
373 }
374 return 0;
375}
376
aliguori15859692009-04-21 23:11:53 +0000377static int img_check(int argc, char **argv)
378{
379 int c, ret;
380 const char *filename, *fmt;
381 BlockDriver *drv;
382 BlockDriverState *bs;
383
384 fmt = NULL;
385 for(;;) {
386 c = getopt(argc, argv, "f:h");
387 if (c == -1)
388 break;
389 switch(c) {
390 case 'h':
391 help();
392 break;
393 case 'f':
394 fmt = optarg;
395 break;
396 }
397 }
398 if (optind >= argc)
399 help();
400 filename = argv[optind++];
401
402 bs = bdrv_new("");
403 if (!bs)
404 error("Not enough memory");
405 if (fmt) {
406 drv = bdrv_find_format(fmt);
407 if (!drv)
408 error("Unknown file format '%s'", fmt);
409 } else {
410 drv = NULL;
411 }
412 if (bdrv_open2(bs, filename, BRDV_O_FLAGS, drv) < 0) {
413 error("Could not open '%s'", filename);
414 }
415 ret = bdrv_check(bs);
416 switch(ret) {
417 case 0:
418 printf("No errors were found on the image.\n");
419 break;
420 case -ENOTSUP:
421 error("This image format does not support checks");
422 break;
423 default:
424 if (ret < 0) {
425 error("An error occurred during the check");
426 } else {
427 printf("%d errors were found on the image.\n", ret);
428 }
429 break;
430 }
431
432 bdrv_delete(bs);
433 return 0;
434}
435
bellardea2384d2004-08-01 21:59:26 +0000436static int img_commit(int argc, char **argv)
437{
438 int c, ret;
439 const char *filename, *fmt;
440 BlockDriver *drv;
441 BlockDriverState *bs;
442
443 fmt = NULL;
444 for(;;) {
445 c = getopt(argc, argv, "f:h");
446 if (c == -1)
447 break;
448 switch(c) {
449 case 'h':
450 help();
451 break;
452 case 'f':
453 fmt = optarg;
454 break;
455 }
456 }
ths5fafdf22007-09-16 21:08:06 +0000457 if (optind >= argc)
bellardea2384d2004-08-01 21:59:26 +0000458 help();
459 filename = argv[optind++];
460
461 bs = bdrv_new("");
462 if (!bs)
463 error("Not enough memory");
464 if (fmt) {
465 drv = bdrv_find_format(fmt);
466 if (!drv)
467 error("Unknown file format '%s'", fmt);
468 } else {
469 drv = NULL;
470 }
aurel32137519c2008-11-30 19:12:49 +0000471 if (bdrv_open2(bs, filename, BRDV_O_FLAGS, drv) < 0) {
bellardea2384d2004-08-01 21:59:26 +0000472 error("Could not open '%s'", filename);
473 }
474 ret = bdrv_commit(bs);
475 switch(ret) {
476 case 0:
477 printf("Image committed.\n");
478 break;
479 case -ENOENT:
480 error("No disk inserted");
481 break;
482 case -EACCES:
483 error("Image is read-only");
484 break;
485 case -ENOTSUP:
486 error("Image is already committed");
487 break;
488 default:
489 error("Error while committing image");
490 break;
491 }
492
493 bdrv_delete(bs);
494 return 0;
495}
496
497static int is_not_zero(const uint8_t *sector, int len)
498{
499 int i;
500 len >>= 2;
501 for(i = 0;i < len; i++) {
502 if (((uint32_t *)sector)[i] != 0)
503 return 1;
504 }
505 return 0;
506}
507
thsf58c7b32008-06-05 21:53:49 +0000508/*
509 * Returns true iff the first sector pointed to by 'buf' contains at least
510 * a non-NUL byte.
511 *
512 * 'pnum' is set to the number of sectors (including and immediately following
513 * the first one) that are known to be in the same allocated/unallocated state.
514 */
bellardea2384d2004-08-01 21:59:26 +0000515static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
516{
517 int v, i;
518
519 if (n <= 0) {
520 *pnum = 0;
521 return 0;
522 }
523 v = is_not_zero(buf, 512);
524 for(i = 1; i < n; i++) {
525 buf += 512;
526 if (v != is_not_zero(buf, 512))
527 break;
528 }
529 *pnum = i;
530 return v;
531}
532
Kevin Wolf80ee15a2009-09-15 12:30:43 +0200533#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +0000534
535static int img_convert(int argc, char **argv)
536{
balrog926c2d22007-10-31 01:11:44 +0000537 int c, ret, n, n1, bs_n, bs_i, flags, cluster_size, cluster_sectors;
thsf58c7b32008-06-05 21:53:49 +0000538 const char *fmt, *out_fmt, *out_baseimg, *out_filename;
bellardea2384d2004-08-01 21:59:26 +0000539 BlockDriver *drv;
balrog926c2d22007-10-31 01:11:44 +0000540 BlockDriverState **bs, *out_bs;
ths96b8f132007-12-17 01:35:20 +0000541 int64_t total_sectors, nb_sectors, sector_num, bs_offset;
542 uint64_t bs_sectors;
bellardea2384d2004-08-01 21:59:26 +0000543 uint8_t buf[IO_BUF_SIZE];
544 const uint8_t *buf1;
bellardfaea38e2006-08-05 21:31:00 +0000545 BlockDriverInfo bdi;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200546 QEMUOptionParameter *param = NULL;
547 char *options = NULL;
bellardea2384d2004-08-01 21:59:26 +0000548
549 fmt = NULL;
550 out_fmt = "raw";
thsf58c7b32008-06-05 21:53:49 +0000551 out_baseimg = NULL;
thsec36ba12007-09-16 21:59:02 +0000552 flags = 0;
bellardea2384d2004-08-01 21:59:26 +0000553 for(;;) {
Kevin Wolfefa84d42009-05-18 16:42:12 +0200554 c = getopt(argc, argv, "f:O:B:hce6o:");
bellardea2384d2004-08-01 21:59:26 +0000555 if (c == -1)
556 break;
557 switch(c) {
558 case 'h':
559 help();
560 break;
561 case 'f':
562 fmt = optarg;
563 break;
564 case 'O':
565 out_fmt = optarg;
566 break;
thsf58c7b32008-06-05 21:53:49 +0000567 case 'B':
568 out_baseimg = optarg;
569 break;
bellardea2384d2004-08-01 21:59:26 +0000570 case 'c':
thsec36ba12007-09-16 21:59:02 +0000571 flags |= BLOCK_FLAG_COMPRESS;
bellardea2384d2004-08-01 21:59:26 +0000572 break;
573 case 'e':
thsec36ba12007-09-16 21:59:02 +0000574 flags |= BLOCK_FLAG_ENCRYPT;
575 break;
576 case '6':
577 flags |= BLOCK_FLAG_COMPAT6;
bellardea2384d2004-08-01 21:59:26 +0000578 break;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200579 case 'o':
580 options = optarg;
581 break;
bellardea2384d2004-08-01 21:59:26 +0000582 }
583 }
ths3b46e622007-09-17 08:09:54 +0000584
balrog926c2d22007-10-31 01:11:44 +0000585 bs_n = argc - optind - 1;
586 if (bs_n < 1) help();
587
588 out_filename = argv[argc - 1];
thsf58c7b32008-06-05 21:53:49 +0000589
590 if (bs_n > 1 && out_baseimg)
591 error("-B makes no sense when concatenating multiple input images");
balrog926c2d22007-10-31 01:11:44 +0000592
593 bs = calloc(bs_n, sizeof(BlockDriverState *));
594 if (!bs)
595 error("Out of memory");
596
597 total_sectors = 0;
598 for (bs_i = 0; bs_i < bs_n; bs_i++) {
599 bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt);
600 if (!bs[bs_i])
601 error("Could not open '%s'", argv[optind + bs_i]);
602 bdrv_get_geometry(bs[bs_i], &bs_sectors);
603 total_sectors += bs_sectors;
604 }
bellardea2384d2004-08-01 21:59:26 +0000605
Kevin Wolfefa84d42009-05-18 16:42:12 +0200606 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +0000607 drv = bdrv_find_format(out_fmt);
608 if (!drv)
thsd34dda52007-02-10 22:59:40 +0000609 error("Unknown file format '%s'", out_fmt);
balrog926c2d22007-10-31 01:11:44 +0000610
Kevin Wolfdb08adf2009-06-04 15:39:38 +0200611 if (options && !strcmp(options, "?")) {
612 print_option_help(drv->create_options);
613 return 0;
614 }
615
Kevin Wolfefa84d42009-05-18 16:42:12 +0200616 if (options) {
617 param = parse_option_parameters(options, drv->create_options, param);
618 if (param == NULL) {
619 error("Invalid options for file format '%s'.", out_fmt);
620 }
621 } else {
622 param = parse_option_parameters("", drv->create_options, param);
623 }
624
625 set_option_parameter_int(param, BLOCK_OPT_SIZE, total_sectors * 512);
626 add_old_style_options(out_fmt, param, flags, out_baseimg, NULL);
627
628 /* Check if compression is supported */
629 if (flags & BLOCK_FLAG_COMPRESS) {
630 QEMUOptionParameter *encryption =
631 get_option_parameter(param, BLOCK_OPT_ENCRYPT);
632
633 if (!drv->bdrv_write_compressed) {
634 error("Compression not supported for this file format");
635 }
636
637 if (encryption && encryption->value.n) {
638 error("Compression and encryption not supported at the same time");
639 }
640 }
641
642 /* Create the new image */
643 ret = bdrv_create(drv, out_filename, param);
644 free_option_parameters(param);
645
bellardea2384d2004-08-01 21:59:26 +0000646 if (ret < 0) {
647 if (ret == -ENOTSUP) {
aliguori93c65b42009-04-05 17:40:43 +0000648 error("Formatting not supported for file format '%s'", out_fmt);
aurel326e9ea0c2009-04-15 14:42:46 +0000649 } else if (ret == -EFBIG) {
650 error("The image size is too large for file format '%s'", out_fmt);
bellardea2384d2004-08-01 21:59:26 +0000651 } else {
652 error("Error while formatting '%s'", out_filename);
653 }
654 }
ths3b46e622007-09-17 08:09:54 +0000655
bellardea2384d2004-08-01 21:59:26 +0000656 out_bs = bdrv_new_open(out_filename, out_fmt);
657
balrog926c2d22007-10-31 01:11:44 +0000658 bs_i = 0;
659 bs_offset = 0;
660 bdrv_get_geometry(bs[0], &bs_sectors);
661
662 if (flags & BLOCK_FLAG_COMPRESS) {
bellardfaea38e2006-08-05 21:31:00 +0000663 if (bdrv_get_info(out_bs, &bdi) < 0)
664 error("could not get block driver info");
665 cluster_size = bdi.cluster_size;
bellardea2384d2004-08-01 21:59:26 +0000666 if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE)
667 error("invalid cluster size");
668 cluster_sectors = cluster_size >> 9;
669 sector_num = 0;
670 for(;;) {
balrog926c2d22007-10-31 01:11:44 +0000671 int64_t bs_num;
672 int remainder;
673 uint8_t *buf2;
674
bellardea2384d2004-08-01 21:59:26 +0000675 nb_sectors = total_sectors - sector_num;
676 if (nb_sectors <= 0)
677 break;
678 if (nb_sectors >= cluster_sectors)
679 n = cluster_sectors;
680 else
681 n = nb_sectors;
balrog926c2d22007-10-31 01:11:44 +0000682
683 bs_num = sector_num - bs_offset;
684 assert (bs_num >= 0);
685 remainder = n;
686 buf2 = buf;
687 while (remainder > 0) {
688 int nlow;
689 while (bs_num == bs_sectors) {
690 bs_i++;
691 assert (bs_i < bs_n);
692 bs_offset += bs_sectors;
693 bdrv_get_geometry(bs[bs_i], &bs_sectors);
694 bs_num = 0;
695 /* printf("changing part: sector_num=%lld, "
696 "bs_i=%d, bs_offset=%lld, bs_sectors=%lld\n",
697 sector_num, bs_i, bs_offset, bs_sectors); */
698 }
699 assert (bs_num < bs_sectors);
700
701 nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder;
702
703 if (bdrv_read(bs[bs_i], bs_num, buf2, nlow) < 0)
704 error("error while reading");
705
706 buf2 += nlow * 512;
707 bs_num += nlow;
708
709 remainder -= nlow;
710 }
711 assert (remainder == 0);
712
bellardea2384d2004-08-01 21:59:26 +0000713 if (n < cluster_sectors)
714 memset(buf + n * 512, 0, cluster_size - n * 512);
715 if (is_not_zero(buf, cluster_size)) {
ths5fafdf22007-09-16 21:08:06 +0000716 if (bdrv_write_compressed(out_bs, sector_num, buf,
bellardfaea38e2006-08-05 21:31:00 +0000717 cluster_sectors) != 0)
bellardec3757d2006-06-14 15:50:07 +0000718 error("error while compressing sector %" PRId64,
719 sector_num);
bellardea2384d2004-08-01 21:59:26 +0000720 }
721 sector_num += n;
722 }
bellardfaea38e2006-08-05 21:31:00 +0000723 /* signal EOF to align */
724 bdrv_write_compressed(out_bs, 0, NULL, 0);
bellardea2384d2004-08-01 21:59:26 +0000725 } else {
thsf58c7b32008-06-05 21:53:49 +0000726 sector_num = 0; // total number of sectors converted so far
bellardea2384d2004-08-01 21:59:26 +0000727 for(;;) {
728 nb_sectors = total_sectors - sector_num;
729 if (nb_sectors <= 0)
730 break;
731 if (nb_sectors >= (IO_BUF_SIZE / 512))
732 n = (IO_BUF_SIZE / 512);
733 else
734 n = nb_sectors;
balrog926c2d22007-10-31 01:11:44 +0000735
736 while (sector_num - bs_offset >= bs_sectors) {
737 bs_i ++;
738 assert (bs_i < bs_n);
739 bs_offset += bs_sectors;
740 bdrv_get_geometry(bs[bs_i], &bs_sectors);
741 /* printf("changing part: sector_num=%lld, bs_i=%d, "
742 "bs_offset=%lld, bs_sectors=%lld\n",
743 sector_num, bs_i, bs_offset, bs_sectors); */
744 }
745
746 if (n > bs_offset + bs_sectors - sector_num)
747 n = bs_offset + bs_sectors - sector_num;
748
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500749 if (strcmp(drv->format_name, "host_device")) {
Akkarit Sangpetchd0320442009-07-17 10:02:15 +0200750 /* If the output image is being created as a copy on write image,
751 assume that sectors which are unallocated in the input image
752 are present in both the output's and input's base images (no
753 need to copy them). */
754 if (out_baseimg) {
755 if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset,
756 n, &n1)) {
757 sector_num += n1;
758 continue;
759 }
760 /* The next 'n1' sectors are allocated in the input image. Copy
761 only those as they may be followed by unallocated sectors. */
762 n = n1;
aliguori93c65b42009-04-05 17:40:43 +0000763 }
aliguori93c65b42009-04-05 17:40:43 +0000764 } else {
765 n1 = n;
thsf58c7b32008-06-05 21:53:49 +0000766 }
767
balrog926c2d22007-10-31 01:11:44 +0000768 if (bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n) < 0)
bellardea2384d2004-08-01 21:59:26 +0000769 error("error while reading");
770 /* NOTE: at the same time we convert, we do not write zero
771 sectors to have a chance to compress the image. Ideally, we
772 should add a specific call to have the info to go faster */
773 buf1 = buf;
774 while (n > 0) {
thsf58c7b32008-06-05 21:53:49 +0000775 /* If the output image is being created as a copy on write image,
776 copy all sectors even the ones containing only NUL bytes,
aliguori93c65b42009-04-05 17:40:43 +0000777 because they may differ from the sectors in the base image.
778
779 If the output is to a host device, we also write out
780 sectors that are entirely 0, since whatever data was
781 already there is garbage, not 0s. */
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500782 if (strcmp(drv->format_name, "host_device") == 0 || out_baseimg ||
aliguori93c65b42009-04-05 17:40:43 +0000783 is_allocated_sectors(buf1, n, &n1)) {
ths5fafdf22007-09-16 21:08:06 +0000784 if (bdrv_write(out_bs, sector_num, buf1, n1) < 0)
bellardea2384d2004-08-01 21:59:26 +0000785 error("error while writing");
786 }
787 sector_num += n1;
788 n -= n1;
789 buf1 += n1 * 512;
790 }
791 }
792 }
793 bdrv_delete(out_bs);
balrog926c2d22007-10-31 01:11:44 +0000794 for (bs_i = 0; bs_i < bs_n; bs_i++)
795 bdrv_delete(bs[bs_i]);
796 free(bs);
bellardea2384d2004-08-01 21:59:26 +0000797 return 0;
798}
799
bellard57d1a2b2004-08-03 21:15:11 +0000800#ifdef _WIN32
801static int64_t get_allocated_file_size(const char *filename)
802{
bellarde8445332006-06-14 15:32:10 +0000803 typedef DWORD (WINAPI * get_compressed_t)(const char *filename, DWORD *high);
804 get_compressed_t get_compressed;
bellard57d1a2b2004-08-03 21:15:11 +0000805 struct _stati64 st;
bellarde8445332006-06-14 15:32:10 +0000806
807 /* WinNT support GetCompressedFileSize to determine allocate size */
808 get_compressed = (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"), "GetCompressedFileSizeA");
809 if (get_compressed) {
810 DWORD high, low;
811 low = get_compressed(filename, &high);
812 if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR)
813 return (((int64_t) high) << 32) + low;
814 }
815
ths5fafdf22007-09-16 21:08:06 +0000816 if (_stati64(filename, &st) < 0)
bellard57d1a2b2004-08-03 21:15:11 +0000817 return -1;
818 return st.st_size;
819}
820#else
821static int64_t get_allocated_file_size(const char *filename)
822{
823 struct stat st;
ths5fafdf22007-09-16 21:08:06 +0000824 if (stat(filename, &st) < 0)
bellard57d1a2b2004-08-03 21:15:11 +0000825 return -1;
826 return (int64_t)st.st_blocks * 512;
827}
828#endif
829
bellardfaea38e2006-08-05 21:31:00 +0000830static void dump_snapshots(BlockDriverState *bs)
831{
832 QEMUSnapshotInfo *sn_tab, *sn;
833 int nb_sns, i;
834 char buf[256];
835
836 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
837 if (nb_sns <= 0)
838 return;
839 printf("Snapshot list:\n");
840 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
841 for(i = 0; i < nb_sns; i++) {
842 sn = &sn_tab[i];
843 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
844 }
845 qemu_free(sn_tab);
846}
847
bellardea2384d2004-08-01 21:59:26 +0000848static int img_info(int argc, char **argv)
849{
850 int c;
851 const char *filename, *fmt;
852 BlockDriver *drv;
853 BlockDriverState *bs;
854 char fmt_name[128], size_buf[128], dsize_buf[128];
ths96b8f132007-12-17 01:35:20 +0000855 uint64_t total_sectors;
856 int64_t allocated_size;
bellard93b6b2a2006-08-01 15:51:11 +0000857 char backing_filename[1024];
858 char backing_filename2[1024];
bellardfaea38e2006-08-05 21:31:00 +0000859 BlockDriverInfo bdi;
bellardea2384d2004-08-01 21:59:26 +0000860
861 fmt = NULL;
862 for(;;) {
863 c = getopt(argc, argv, "f:h");
864 if (c == -1)
865 break;
866 switch(c) {
867 case 'h':
868 help();
869 break;
870 case 'f':
871 fmt = optarg;
872 break;
873 }
874 }
ths5fafdf22007-09-16 21:08:06 +0000875 if (optind >= argc)
bellardea2384d2004-08-01 21:59:26 +0000876 help();
877 filename = argv[optind++];
878
879 bs = bdrv_new("");
880 if (!bs)
881 error("Not enough memory");
882 if (fmt) {
883 drv = bdrv_find_format(fmt);
884 if (!drv)
885 error("Unknown file format '%s'", fmt);
886 } else {
887 drv = NULL;
888 }
aurel32137519c2008-11-30 19:12:49 +0000889 if (bdrv_open2(bs, filename, BRDV_O_FLAGS, drv) < 0) {
bellardea2384d2004-08-01 21:59:26 +0000890 error("Could not open '%s'", filename);
891 }
892 bdrv_get_format(bs, fmt_name, sizeof(fmt_name));
893 bdrv_get_geometry(bs, &total_sectors);
894 get_human_readable_size(size_buf, sizeof(size_buf), total_sectors * 512);
bellard57d1a2b2004-08-03 21:15:11 +0000895 allocated_size = get_allocated_file_size(filename);
896 if (allocated_size < 0)
blueswir1a10ea302008-08-24 10:30:33 +0000897 snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
bellardde167e42005-04-28 21:15:08 +0000898 else
ths5fafdf22007-09-16 21:08:06 +0000899 get_human_readable_size(dsize_buf, sizeof(dsize_buf),
bellardde167e42005-04-28 21:15:08 +0000900 allocated_size);
bellardea2384d2004-08-01 21:59:26 +0000901 printf("image: %s\n"
902 "file format: %s\n"
bellardec3757d2006-06-14 15:50:07 +0000903 "virtual size: %s (%" PRId64 " bytes)\n"
bellardea2384d2004-08-01 21:59:26 +0000904 "disk size: %s\n",
ths5fafdf22007-09-16 21:08:06 +0000905 filename, fmt_name, size_buf,
bellardec3757d2006-06-14 15:50:07 +0000906 (total_sectors * 512),
bellardea2384d2004-08-01 21:59:26 +0000907 dsize_buf);
908 if (bdrv_is_encrypted(bs))
909 printf("encrypted: yes\n");
bellardfaea38e2006-08-05 21:31:00 +0000910 if (bdrv_get_info(bs, &bdi) >= 0) {
ths5fafdf22007-09-16 21:08:06 +0000911 if (bdi.cluster_size != 0)
bellardfaea38e2006-08-05 21:31:00 +0000912 printf("cluster_size: %d\n", bdi.cluster_size);
913 }
bellard93b6b2a2006-08-01 15:51:11 +0000914 bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
bellardfaea38e2006-08-05 21:31:00 +0000915 if (backing_filename[0] != '\0') {
bellard93b6b2a2006-08-01 15:51:11 +0000916 path_combine(backing_filename2, sizeof(backing_filename2),
917 filename, backing_filename);
ths5fafdf22007-09-16 21:08:06 +0000918 printf("backing file: %s (actual path: %s)\n",
bellard93b6b2a2006-08-01 15:51:11 +0000919 backing_filename,
920 backing_filename2);
bellardfaea38e2006-08-05 21:31:00 +0000921 }
922 dump_snapshots(bs);
bellardea2384d2004-08-01 21:59:26 +0000923 bdrv_delete(bs);
924 return 0;
925}
926
aliguorif7b4a942009-01-07 17:40:15 +0000927#define SNAPSHOT_LIST 1
928#define SNAPSHOT_CREATE 2
929#define SNAPSHOT_APPLY 3
930#define SNAPSHOT_DELETE 4
931
Stuart Brady153859b2009-06-07 00:42:17 +0100932static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +0000933{
934 BlockDriverState *bs;
935 QEMUSnapshotInfo sn;
936 char *filename, *snapshot_name = NULL;
aliguori40a45392009-01-15 21:42:12 +0000937 int c, ret;
aliguorif7b4a942009-01-07 17:40:15 +0000938 int action = 0;
939 qemu_timeval tv;
940
941 /* Parse commandline parameters */
942 for(;;) {
943 c = getopt(argc, argv, "la:c:d:h");
944 if (c == -1)
945 break;
946 switch(c) {
947 case 'h':
948 help();
Stuart Brady153859b2009-06-07 00:42:17 +0100949 return 0;
aliguorif7b4a942009-01-07 17:40:15 +0000950 case 'l':
951 if (action) {
952 help();
Stuart Brady153859b2009-06-07 00:42:17 +0100953 return 0;
aliguorif7b4a942009-01-07 17:40:15 +0000954 }
955 action = SNAPSHOT_LIST;
956 break;
957 case 'a':
958 if (action) {
959 help();
Stuart Brady153859b2009-06-07 00:42:17 +0100960 return 0;
aliguorif7b4a942009-01-07 17:40:15 +0000961 }
962 action = SNAPSHOT_APPLY;
963 snapshot_name = optarg;
964 break;
965 case 'c':
966 if (action) {
967 help();
Stuart Brady153859b2009-06-07 00:42:17 +0100968 return 0;
aliguorif7b4a942009-01-07 17:40:15 +0000969 }
970 action = SNAPSHOT_CREATE;
971 snapshot_name = optarg;
972 break;
973 case 'd':
974 if (action) {
975 help();
Stuart Brady153859b2009-06-07 00:42:17 +0100976 return 0;
aliguorif7b4a942009-01-07 17:40:15 +0000977 }
978 action = SNAPSHOT_DELETE;
979 snapshot_name = optarg;
980 break;
981 }
982 }
983
984 if (optind >= argc)
985 help();
986 filename = argv[optind++];
987
988 /* Open the image */
989 bs = bdrv_new("");
990 if (!bs)
991 error("Not enough memory");
992
993 if (bdrv_open2(bs, filename, 0, NULL) < 0) {
994 error("Could not open '%s'", filename);
995 }
996
997 /* Perform the requested action */
998 switch(action) {
999 case SNAPSHOT_LIST:
1000 dump_snapshots(bs);
1001 break;
1002
1003 case SNAPSHOT_CREATE:
1004 memset(&sn, 0, sizeof(sn));
1005 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
1006
1007 qemu_gettimeofday(&tv);
1008 sn.date_sec = tv.tv_sec;
1009 sn.date_nsec = tv.tv_usec * 1000;
1010
1011 ret = bdrv_snapshot_create(bs, &sn);
1012 if (ret)
1013 error("Could not create snapshot '%s': %d (%s)",
1014 snapshot_name, ret, strerror(-ret));
1015 break;
1016
1017 case SNAPSHOT_APPLY:
1018 ret = bdrv_snapshot_goto(bs, snapshot_name);
1019 if (ret)
1020 error("Could not apply snapshot '%s': %d (%s)",
1021 snapshot_name, ret, strerror(-ret));
1022 break;
1023
1024 case SNAPSHOT_DELETE:
1025 ret = bdrv_snapshot_delete(bs, snapshot_name);
1026 if (ret)
1027 error("Could not delete snapshot '%s': %d (%s)",
1028 snapshot_name, ret, strerror(-ret));
1029 break;
1030 }
1031
1032 /* Cleanup */
1033 bdrv_delete(bs);
Stuart Brady153859b2009-06-07 00:42:17 +01001034
1035 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00001036}
1037
Anthony Liguoric227f092009-10-01 16:12:16 -05001038static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01001039#define DEF(option, callback, arg_string) \
1040 { option, callback },
1041#include "qemu-img-cmds.h"
1042#undef DEF
1043#undef GEN_DOCS
1044 { NULL, NULL, },
1045};
1046
bellardea2384d2004-08-01 21:59:26 +00001047int main(int argc, char **argv)
1048{
Anthony Liguoric227f092009-10-01 16:12:16 -05001049 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01001050 const char *cmdname;
bellardea2384d2004-08-01 21:59:26 +00001051
1052 bdrv_init();
1053 if (argc < 2)
1054 help();
Stuart Brady153859b2009-06-07 00:42:17 +01001055 cmdname = argv[1];
aurel328f9b1572009-02-09 18:14:31 +00001056 argc--; argv++;
Stuart Brady153859b2009-06-07 00:42:17 +01001057
1058 /* find the command */
1059 for(cmd = img_cmds; cmd->name != NULL; cmd++) {
1060 if (!strcmp(cmdname, cmd->name)) {
1061 return cmd->handler(argc, argv);
1062 }
bellardea2384d2004-08-01 21:59:26 +00001063 }
Stuart Brady153859b2009-06-07 00:42:17 +01001064
1065 /* not found */
1066 help();
bellardea2384d2004-08-01 21:59:26 +00001067 return 0;
1068}