blob: cd096a136153811f05a7e9268ebdb08164e5ddf3 [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 */
Benoît Canetc054b3f2012-09-05 13:09:02 +020024#include "qapi-visit.h"
25#include "qapi/qmp-output-visitor.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010026#include "qapi/qmp/qjson.h"
pbrookfaf07962007-11-11 02:51:17 +000027#include "qemu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010028#include "qemu/option.h"
29#include "qemu/error-report.h"
30#include "qemu/osdep.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010031#include "sysemu/sysemu.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010032#include "block/block_int.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020033#include <getopt.h>
aliguori9230eaf2009-03-28 17:55:19 +000034#include <stdio.h>
Miroslav Rezaninaf382d432013-02-13 09:09:40 +010035#include <stdarg.h>
bellardea2384d2004-08-01 21:59:26 +000036
bellarde8445332006-06-14 15:32:10 +000037#ifdef _WIN32
38#include <windows.h>
39#endif
40
Anthony Liguoric227f092009-10-01 16:12:16 -050041typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010042 const char *name;
43 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050044} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010045
Federico Simoncelli8599ea42013-01-28 06:59:47 -050046enum {
47 OPTION_OUTPUT = 256,
48 OPTION_BACKING_CHAIN = 257,
49};
50
51typedef enum OutputFormat {
52 OFORMAT_JSON,
53 OFORMAT_HUMAN,
54} OutputFormat;
55
aurel32137519c2008-11-30 19:12:49 +000056/* Default to cache=writeback as data integrity is not important for qemu-tcg. */
Stefan Hajnocziadfe0782010-04-13 10:29:35 +010057#define BDRV_O_FLAGS BDRV_O_CACHE_WB
Federico Simoncelli661a0f72011-06-20 12:48:19 -040058#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000059
bellardea2384d2004-08-01 21:59:26 +000060static void format_print(void *opaque, const char *name)
61{
62 printf(" %s", name);
63}
64
blueswir1d2c639d2009-01-24 18:19:25 +000065/* Please keep in synch with qemu-img.texi */
pbrook3f379ab2007-11-11 03:33:13 +000066static void help(void)
bellardea2384d2004-08-01 21:59:26 +000067{
Paolo Bonzinie00291c2010-02-04 16:49:56 +010068 const char *help_msg =
69 "qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2008 Fabrice Bellard\n"
malc3f020d72010-02-08 12:04:56 +030070 "usage: qemu-img command [command options]\n"
71 "QEMU disk image utility\n"
72 "\n"
73 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +010074#define DEF(option, callback, arg_string) \
75 " " arg_string "\n"
76#include "qemu-img-cmds.h"
77#undef DEF
78#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +030079 "\n"
80 "Command parameters:\n"
81 " 'filename' is a disk image filename\n"
82 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -040083 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +080084 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
85 " 'directsync' and 'unsafe' (default for convert)\n"
malc3f020d72010-02-08 12:04:56 +030086 " 'size' is the disk image size in bytes. Optional suffixes\n"
87 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M)\n"
88 " and T (terabyte, 1024G) are supported. 'b' is ignored.\n"
89 " 'output_filename' is the destination disk image filename\n"
90 " 'output_fmt' is the destination format\n"
91 " 'options' is a comma separated list of format specific options in a\n"
92 " name=value format. Use -o ? for an overview of the options supported by the\n"
93 " used format\n"
94 " '-c' indicates that target image must be compressed (qcow format only)\n"
95 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
96 " match exactly. The image doesn't need a working backing file before\n"
97 " rebasing in this case (useful for renaming the backing file)\n"
98 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +020099 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100100 " '-q' use Quiet mode - do not print any output (except errors)\n"
Kevin Wolfa22f1232011-08-26 15:27:13 +0200101 " '-S' indicates the consecutive number of bytes that must contain only zeros\n"
102 " for qemu-img to create a sparse image during conversion\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200103 " '--output' takes the format in which the output must be done (human or json)\n"
malc3f020d72010-02-08 12:04:56 +0300104 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200105 "Parameters to check subcommand:\n"
106 " '-r' tries to repair any inconsistencies that are found during the check.\n"
107 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
108 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200109 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200110 "\n"
malc3f020d72010-02-08 12:04:56 +0300111 "Parameters to snapshot subcommand:\n"
112 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
113 " '-a' applies a snapshot (revert disk to saved state)\n"
114 " '-c' creates a snapshot\n"
115 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100116 " '-l' lists all snapshots in the given image\n"
117 "\n"
118 "Parameters to compare subcommand:\n"
119 " '-f' first image format\n"
120 " '-F' second image format\n"
121 " '-s' run in Strict mode - fail on different image size or sector allocation\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100122
123 printf("%s\nSupported formats:", help_msg);
bellardea2384d2004-08-01 21:59:26 +0000124 bdrv_iterate_format(format_print, NULL);
125 printf("\n");
126 exit(1);
127}
128
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100129static int qprintf(bool quiet, const char *fmt, ...)
130{
131 int ret = 0;
132 if (!quiet) {
133 va_list args;
134 va_start(args, fmt);
135 ret = vprintf(fmt, args);
136 va_end(args);
137 }
138 return ret;
139}
140
bellardea2384d2004-08-01 21:59:26 +0000141#if defined(WIN32)
142/* XXX: put correct support for win32 */
143static int read_password(char *buf, int buf_size)
144{
145 int c, i;
146 printf("Password: ");
147 fflush(stdout);
148 i = 0;
149 for(;;) {
150 c = getchar();
151 if (c == '\n')
152 break;
153 if (i < (buf_size - 1))
154 buf[i++] = c;
155 }
156 buf[i] = '\0';
157 return 0;
158}
159
160#else
161
162#include <termios.h>
163
164static struct termios oldtty;
165
166static void term_exit(void)
167{
168 tcsetattr (0, TCSANOW, &oldtty);
169}
170
171static void term_init(void)
172{
173 struct termios tty;
174
175 tcgetattr (0, &tty);
176 oldtty = tty;
177
178 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
179 |INLCR|IGNCR|ICRNL|IXON);
180 tty.c_oflag |= OPOST;
181 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
182 tty.c_cflag &= ~(CSIZE|PARENB);
183 tty.c_cflag |= CS8;
184 tty.c_cc[VMIN] = 1;
185 tty.c_cc[VTIME] = 0;
ths3b46e622007-09-17 08:09:54 +0000186
bellardea2384d2004-08-01 21:59:26 +0000187 tcsetattr (0, TCSANOW, &tty);
188
189 atexit(term_exit);
190}
191
pbrook3f379ab2007-11-11 03:33:13 +0000192static int read_password(char *buf, int buf_size)
bellardea2384d2004-08-01 21:59:26 +0000193{
194 uint8_t ch;
195 int i, ret;
196
197 printf("password: ");
198 fflush(stdout);
199 term_init();
200 i = 0;
201 for(;;) {
202 ret = read(0, &ch, 1);
203 if (ret == -1) {
204 if (errno == EAGAIN || errno == EINTR) {
205 continue;
206 } else {
207 ret = -1;
208 break;
209 }
210 } else if (ret == 0) {
211 ret = -1;
212 break;
213 } else {
214 if (ch == '\r') {
215 ret = 0;
216 break;
217 }
218 if (i < (buf_size - 1))
219 buf[i++] = ch;
220 }
221 }
222 term_exit();
223 buf[i] = '\0';
224 printf("\n");
225 return ret;
226}
227#endif
228
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100229static int print_block_option_help(const char *filename, const char *fmt)
230{
231 BlockDriver *drv, *proto_drv;
232 QEMUOptionParameter *create_options = NULL;
233
234 /* Find driver and parse its options */
235 drv = bdrv_find_format(fmt);
236 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100237 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100238 return 1;
239 }
240
241 proto_drv = bdrv_find_protocol(filename);
242 if (!proto_drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100243 error_report("Unknown protocol '%s'", filename);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100244 return 1;
245 }
246
247 create_options = append_option_parameters(create_options,
248 drv->create_options);
249 create_options = append_option_parameters(create_options,
250 proto_drv->create_options);
251 print_option_help(create_options);
252 free_option_parameters(create_options);
253 return 0;
254}
255
bellard75c23802004-08-27 21:28:58 +0000256static BlockDriverState *bdrv_new_open(const char *filename,
Sheng Yang9bc378c2010-01-29 10:15:06 +0800257 const char *fmt,
Daniel P. Berrangef0536bb2012-09-10 12:11:31 +0100258 int flags,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100259 bool require_io,
260 bool quiet)
bellard75c23802004-08-27 21:28:58 +0000261{
262 BlockDriverState *bs;
263 BlockDriver *drv;
264 char password[256];
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100265 int ret;
bellard75c23802004-08-27 21:28:58 +0000266
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100267 bs = bdrv_new("image");
Kevin Wolfad717132010-12-16 15:37:41 +0100268
bellard75c23802004-08-27 21:28:58 +0000269 if (fmt) {
270 drv = bdrv_find_format(fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900271 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100272 error_report("Unknown file format '%s'", fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900273 goto fail;
274 }
bellard75c23802004-08-27 21:28:58 +0000275 } else {
276 drv = NULL;
277 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100278
Kevin Wolfde9c0ce2013-03-15 10:35:02 +0100279 ret = bdrv_open(bs, filename, NULL, flags, drv);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100280 if (ret < 0) {
281 error_report("Could not open '%s': %s", filename, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900282 goto fail;
bellard75c23802004-08-27 21:28:58 +0000283 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100284
Daniel P. Berrangef0536bb2012-09-10 12:11:31 +0100285 if (bdrv_is_encrypted(bs) && require_io) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100286 qprintf(quiet, "Disk image '%s' is encrypted.\n", filename);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900287 if (read_password(password, sizeof(password)) < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100288 error_report("No password given");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900289 goto fail;
290 }
291 if (bdrv_set_key(bs, password) < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100292 error_report("invalid password");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900293 goto fail;
294 }
bellard75c23802004-08-27 21:28:58 +0000295 }
296 return bs;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900297fail:
298 if (bs) {
299 bdrv_delete(bs);
300 }
301 return NULL;
bellard75c23802004-08-27 21:28:58 +0000302}
303
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900304static int add_old_style_options(const char *fmt, QEMUOptionParameter *list,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100305 const char *base_filename,
306 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200307{
Kevin Wolfefa84d42009-05-18 16:42:12 +0200308 if (base_filename) {
309 if (set_option_parameter(list, BLOCK_OPT_BACKING_FILE, base_filename)) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100310 error_report("Backing file not supported for file format '%s'",
311 fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900312 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200313 }
314 }
315 if (base_fmt) {
316 if (set_option_parameter(list, BLOCK_OPT_BACKING_FMT, base_fmt)) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100317 error_report("Backing file format not supported for file "
318 "format '%s'", fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900319 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200320 }
321 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900322 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200323}
324
bellardea2384d2004-08-01 21:59:26 +0000325static int img_create(int argc, char **argv)
326{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200327 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100328 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000329 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000330 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000331 const char *filename;
332 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200333 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200334 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100335 bool quiet = false;
ths3b46e622007-09-17 08:09:54 +0000336
bellardea2384d2004-08-01 21:59:26 +0000337 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100338 c = getopt(argc, argv, "F:b:f:he6o:q");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100339 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000340 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100341 }
bellardea2384d2004-08-01 21:59:26 +0000342 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100343 case '?':
bellardea2384d2004-08-01 21:59:26 +0000344 case 'h':
345 help();
346 break;
aliguori9230eaf2009-03-28 17:55:19 +0000347 case 'F':
348 base_fmt = optarg;
349 break;
bellardea2384d2004-08-01 21:59:26 +0000350 case 'b':
351 base_filename = optarg;
352 break;
353 case 'f':
354 fmt = optarg;
355 break;
356 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200357 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100358 "encryption\' instead!");
359 return 1;
thsd8871c52007-10-24 16:11:42 +0000360 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200361 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100362 "compat6\' instead!");
363 return 1;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200364 case 'o':
365 options = optarg;
366 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100367 case 'q':
368 quiet = true;
369 break;
bellardea2384d2004-08-01 21:59:26 +0000370 }
371 }
aliguori9230eaf2009-03-28 17:55:19 +0000372
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900373 /* Get the filename */
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100374 if (optind >= argc) {
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900375 help();
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100376 }
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900377 filename = argv[optind++];
378
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100379 /* Get image size, if specified */
380 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100381 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +0100382 char *end;
383 sval = strtosz_suffix(argv[optind++], &end, STRTOSZ_DEFSUFFIX_B);
384 if (sval < 0 || *end) {
liguang79443392012-12-17 09:49:23 +0800385 if (sval == -ERANGE) {
386 error_report("Image size must be less than 8 EiB!");
387 } else {
388 error_report("Invalid image size specified! You may use k, M, "
389 "G or T suffixes for ");
390 error_report("kilobytes, megabytes, gigabytes and terabytes.");
391 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200392 return 1;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100393 }
394 img_size = (uint64_t)sval;
395 }
396
Peter Maydellc8057f92012-08-02 13:45:54 +0100397 if (options && is_help_option(options)) {
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200398 return print_block_option_help(filename, fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100399 }
400
Luiz Capitulino9b375252012-11-30 10:52:05 -0200401 bdrv_img_create(filename, fmt, base_filename, base_fmt,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100402 options, img_size, BDRV_O_FLAGS, &local_err, quiet);
Luiz Capitulino9b375252012-11-30 10:52:05 -0200403 if (error_is_set(&local_err)) {
404 error_report("%s", error_get_pretty(local_err));
405 error_free(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900406 return 1;
407 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200408
bellardea2384d2004-08-01 21:59:26 +0000409 return 0;
410}
411
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100412static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500413{
414 Error *errp = NULL;
415 QString *str;
416 QmpOutputVisitor *ov = qmp_output_visitor_new();
417 QObject *obj;
418 visit_type_ImageCheck(qmp_output_get_visitor(ov),
419 &check, NULL, &errp);
420 obj = qmp_output_get_qobject(ov);
421 str = qobject_to_json_pretty(obj);
422 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100423 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500424 qobject_decref(obj);
425 qmp_output_visitor_cleanup(ov);
426 QDECREF(str);
427}
428
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100429static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500430{
431 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100432 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500433 } else {
434 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100435 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
436 "Data may be corrupted, or further writes to the image "
437 "may corrupt it.\n",
438 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500439 }
440
441 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100442 qprintf(quiet,
443 "\n%" PRId64 " leaked clusters were found on the image.\n"
444 "This means waste of disk space, but no harm to data.\n",
445 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500446 }
447
448 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100449 qprintf(quiet,
450 "\n%" PRId64
451 " internal errors have occurred during the check.\n",
452 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500453 }
454 }
455
456 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100457 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
458 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
459 check->allocated_clusters, check->total_clusters,
460 check->allocated_clusters * 100.0 / check->total_clusters,
461 check->fragmented_clusters * 100.0 / check->allocated_clusters,
462 check->compressed_clusters * 100.0 /
463 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500464 }
465
466 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100467 qprintf(quiet,
468 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500469 }
470}
471
472static int collect_image_check(BlockDriverState *bs,
473 ImageCheck *check,
474 const char *filename,
475 const char *fmt,
476 int fix)
477{
478 int ret;
479 BdrvCheckResult result;
480
481 ret = bdrv_check(bs, &result, fix);
482 if (ret < 0) {
483 return ret;
484 }
485
486 check->filename = g_strdup(filename);
487 check->format = g_strdup(bdrv_get_format_name(bs));
488 check->check_errors = result.check_errors;
489 check->corruptions = result.corruptions;
490 check->has_corruptions = result.corruptions != 0;
491 check->leaks = result.leaks;
492 check->has_leaks = result.leaks != 0;
493 check->corruptions_fixed = result.corruptions_fixed;
494 check->has_corruptions_fixed = result.corruptions != 0;
495 check->leaks_fixed = result.leaks_fixed;
496 check->has_leaks_fixed = result.leaks != 0;
497 check->image_end_offset = result.image_end_offset;
498 check->has_image_end_offset = result.image_end_offset != 0;
499 check->total_clusters = result.bfi.total_clusters;
500 check->has_total_clusters = result.bfi.total_clusters != 0;
501 check->allocated_clusters = result.bfi.allocated_clusters;
502 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
503 check->fragmented_clusters = result.bfi.fragmented_clusters;
504 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100505 check->compressed_clusters = result.bfi.compressed_clusters;
506 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500507
508 return 0;
509}
510
Kevin Wolfe076f332010-06-29 11:43:13 +0200511/*
512 * Checks an image for consistency. Exit codes:
513 *
514 * 0 - Check completed, image is good
515 * 1 - Check not completed because of internal errors
516 * 2 - Check completed, image is corrupted
517 * 3 - Check completed, image has leaked clusters, but is good otherwise
518 */
aliguori15859692009-04-21 23:11:53 +0000519static int img_check(int argc, char **argv)
520{
521 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500522 OutputFormat output_format = OFORMAT_HUMAN;
523 const char *filename, *fmt, *output;
aliguori15859692009-04-21 23:11:53 +0000524 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200525 int fix = 0;
Stefan Hajnoczi058f8f12012-08-09 13:05:56 +0100526 int flags = BDRV_O_FLAGS | BDRV_O_CHECK;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500527 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100528 bool quiet = false;
aliguori15859692009-04-21 23:11:53 +0000529
530 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500531 output = NULL;
aliguori15859692009-04-21 23:11:53 +0000532 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500533 int option_index = 0;
534 static const struct option long_options[] = {
535 {"help", no_argument, 0, 'h'},
536 {"format", required_argument, 0, 'f'},
537 {"repair", no_argument, 0, 'r'},
538 {"output", required_argument, 0, OPTION_OUTPUT},
539 {0, 0, 0, 0}
540 };
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100541 c = getopt_long(argc, argv, "f:hr:q",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500542 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100543 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000544 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100545 }
aliguori15859692009-04-21 23:11:53 +0000546 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100547 case '?':
aliguori15859692009-04-21 23:11:53 +0000548 case 'h':
549 help();
550 break;
551 case 'f':
552 fmt = optarg;
553 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200554 case 'r':
555 flags |= BDRV_O_RDWR;
556
557 if (!strcmp(optarg, "leaks")) {
558 fix = BDRV_FIX_LEAKS;
559 } else if (!strcmp(optarg, "all")) {
560 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
561 } else {
562 help();
563 }
564 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500565 case OPTION_OUTPUT:
566 output = optarg;
567 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100568 case 'q':
569 quiet = true;
570 break;
aliguori15859692009-04-21 23:11:53 +0000571 }
572 }
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100573 if (optind >= argc) {
aliguori15859692009-04-21 23:11:53 +0000574 help();
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100575 }
aliguori15859692009-04-21 23:11:53 +0000576 filename = argv[optind++];
577
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500578 if (output && !strcmp(output, "json")) {
579 output_format = OFORMAT_JSON;
580 } else if (output && !strcmp(output, "human")) {
581 output_format = OFORMAT_HUMAN;
582 } else if (output) {
583 error_report("--output must be used with human or json as argument.");
584 return 1;
585 }
586
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100587 bs = bdrv_new_open(filename, fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900588 if (!bs) {
589 return 1;
590 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500591
592 check = g_new0(ImageCheck, 1);
593 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200594
595 if (ret == -ENOTSUP) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500596 if (output_format == OFORMAT_HUMAN) {
597 error_report("This image format does not support checks");
598 }
599 ret = 1;
600 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200601 }
602
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500603 if (check->corruptions_fixed || check->leaks_fixed) {
604 int corruptions_fixed, leaks_fixed;
605
606 leaks_fixed = check->leaks_fixed;
607 corruptions_fixed = check->corruptions_fixed;
608
609 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100610 qprintf(quiet,
611 "The following inconsistencies were found and repaired:\n\n"
612 " %" PRId64 " leaked clusters\n"
613 " %" PRId64 " corruptions\n\n"
614 "Double checking the fixed image now...\n",
615 check->leaks_fixed,
616 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500617 }
618
619 ret = collect_image_check(bs, check, filename, fmt, 0);
620
621 check->leaks_fixed = leaks_fixed;
622 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200623 }
624
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500625 switch (output_format) {
626 case OFORMAT_HUMAN:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100627 dump_human_image_check(check, quiet);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500628 break;
629 case OFORMAT_JSON:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100630 dump_json_image_check(check, quiet);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500631 break;
632 }
633
634 if (ret || check->check_errors) {
635 ret = 1;
636 goto fail;
637 }
638
639 if (check->corruptions) {
640 ret = 2;
641 } else if (check->leaks) {
642 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200643 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500644 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000645 }
646
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500647fail:
648 qapi_free_ImageCheck(check);
aliguori15859692009-04-21 23:11:53 +0000649 bdrv_delete(bs);
Kevin Wolfe076f332010-06-29 11:43:13 +0200650
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500651 return ret;
aliguori15859692009-04-21 23:11:53 +0000652}
653
bellardea2384d2004-08-01 21:59:26 +0000654static int img_commit(int argc, char **argv)
655{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400656 int c, ret, flags;
657 const char *filename, *fmt, *cache;
bellardea2384d2004-08-01 21:59:26 +0000658 BlockDriverState *bs;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100659 bool quiet = false;
bellardea2384d2004-08-01 21:59:26 +0000660
661 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400662 cache = BDRV_DEFAULT_CACHE;
bellardea2384d2004-08-01 21:59:26 +0000663 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100664 c = getopt(argc, argv, "f:ht:q");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100665 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000666 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100667 }
bellardea2384d2004-08-01 21:59:26 +0000668 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100669 case '?':
bellardea2384d2004-08-01 21:59:26 +0000670 case 'h':
671 help();
672 break;
673 case 'f':
674 fmt = optarg;
675 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400676 case 't':
677 cache = optarg;
678 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100679 case 'q':
680 quiet = true;
681 break;
bellardea2384d2004-08-01 21:59:26 +0000682 }
683 }
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100684 if (optind >= argc) {
bellardea2384d2004-08-01 21:59:26 +0000685 help();
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100686 }
bellardea2384d2004-08-01 21:59:26 +0000687 filename = argv[optind++];
688
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400689 flags = BDRV_O_RDWR;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100690 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400691 if (ret < 0) {
692 error_report("Invalid cache option: %s", cache);
693 return -1;
694 }
695
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100696 bs = bdrv_new_open(filename, fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900697 if (!bs) {
698 return 1;
699 }
bellardea2384d2004-08-01 21:59:26 +0000700 ret = bdrv_commit(bs);
701 switch(ret) {
702 case 0:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100703 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +0000704 break;
705 case -ENOENT:
Jes Sorensen15654a62010-12-16 14:31:53 +0100706 error_report("No disk inserted");
bellardea2384d2004-08-01 21:59:26 +0000707 break;
708 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +0100709 error_report("Image is read-only");
bellardea2384d2004-08-01 21:59:26 +0000710 break;
711 case -ENOTSUP:
Jes Sorensen15654a62010-12-16 14:31:53 +0100712 error_report("Image is already committed");
bellardea2384d2004-08-01 21:59:26 +0000713 break;
714 default:
Jes Sorensen15654a62010-12-16 14:31:53 +0100715 error_report("Error while committing image");
bellardea2384d2004-08-01 21:59:26 +0000716 break;
717 }
718
719 bdrv_delete(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900720 if (ret) {
721 return 1;
722 }
bellardea2384d2004-08-01 21:59:26 +0000723 return 0;
724}
725
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +0400726/*
thsf58c7b32008-06-05 21:53:49 +0000727 * Returns true iff the first sector pointed to by 'buf' contains at least
728 * a non-NUL byte.
729 *
730 * 'pnum' is set to the number of sectors (including and immediately following
731 * the first one) that are known to be in the same allocated/unallocated state.
732 */
bellardea2384d2004-08-01 21:59:26 +0000733static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
734{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000735 bool is_zero;
736 int i;
bellardea2384d2004-08-01 21:59:26 +0000737
738 if (n <= 0) {
739 *pnum = 0;
740 return 0;
741 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000742 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +0000743 for(i = 1; i < n; i++) {
744 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000745 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +0000746 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000747 }
bellardea2384d2004-08-01 21:59:26 +0000748 }
749 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000750 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +0000751}
752
Kevin Wolf3e85c6f2010-01-12 12:55:18 +0100753/*
Kevin Wolfa22f1232011-08-26 15:27:13 +0200754 * Like is_allocated_sectors, but if the buffer starts with a used sector,
755 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
756 * breaking up write requests for only small sparse areas.
757 */
758static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
759 int min)
760{
761 int ret;
762 int num_checked, num_used;
763
764 if (n < min) {
765 min = n;
766 }
767
768 ret = is_allocated_sectors(buf, n, pnum);
769 if (!ret) {
770 return ret;
771 }
772
773 num_used = *pnum;
774 buf += BDRV_SECTOR_SIZE * *pnum;
775 n -= *pnum;
776 num_checked = num_used;
777
778 while (n > 0) {
779 ret = is_allocated_sectors(buf, n, pnum);
780
781 buf += BDRV_SECTOR_SIZE * *pnum;
782 n -= *pnum;
783 num_checked += *pnum;
784 if (ret) {
785 num_used = num_checked;
786 } else if (*pnum >= min) {
787 break;
788 }
789 }
790
791 *pnum = num_used;
792 return 1;
793}
794
795/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +0100796 * Compares two buffers sector by sector. Returns 0 if the first sector of both
797 * buffers matches, non-zero otherwise.
798 *
799 * pnum is set to the number of sectors (including and immediately following
800 * the first one) that are known to have the same comparison result
801 */
802static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
803 int *pnum)
804{
805 int res, i;
806
807 if (n <= 0) {
808 *pnum = 0;
809 return 0;
810 }
811
812 res = !!memcmp(buf1, buf2, 512);
813 for(i = 1; i < n; i++) {
814 buf1 += 512;
815 buf2 += 512;
816
817 if (!!memcmp(buf1, buf2, 512) != res) {
818 break;
819 }
820 }
821
822 *pnum = i;
823 return res;
824}
825
Kevin Wolf80ee15a2009-09-15 12:30:43 +0200826#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +0000827
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100828static int64_t sectors_to_bytes(int64_t sectors)
829{
830 return sectors << BDRV_SECTOR_BITS;
831}
832
833static int64_t sectors_to_process(int64_t total, int64_t from)
834{
835 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
836}
837
838/*
839 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
840 *
841 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
842 * data and negative value on error.
843 *
844 * @param bs: Driver used for accessing file
845 * @param sect_num: Number of first sector to check
846 * @param sect_count: Number of sectors to check
847 * @param filename: Name of disk file we are checking (logging purpose)
848 * @param buffer: Allocated buffer for storing read data
849 * @param quiet: Flag for quiet mode
850 */
851static int check_empty_sectors(BlockDriverState *bs, int64_t sect_num,
852 int sect_count, const char *filename,
853 uint8_t *buffer, bool quiet)
854{
855 int pnum, ret = 0;
856 ret = bdrv_read(bs, sect_num, buffer, sect_count);
857 if (ret < 0) {
858 error_report("Error while reading offset %" PRId64 " of %s: %s",
859 sectors_to_bytes(sect_num), filename, strerror(-ret));
860 return ret;
861 }
862 ret = is_allocated_sectors(buffer, sect_count, &pnum);
863 if (ret || pnum != sect_count) {
864 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
865 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
866 return 1;
867 }
868
869 return 0;
870}
871
872/*
873 * Compares two images. Exit codes:
874 *
875 * 0 - Images are identical
876 * 1 - Images differ
877 * >1 - Error occurred
878 */
879static int img_compare(int argc, char **argv)
880{
881 const char *fmt1 = NULL, *fmt2 = NULL, *filename1, *filename2;
882 BlockDriverState *bs1, *bs2;
883 int64_t total_sectors1, total_sectors2;
884 uint8_t *buf1 = NULL, *buf2 = NULL;
885 int pnum1, pnum2;
886 int allocated1, allocated2;
887 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
888 bool progress = false, quiet = false, strict = false;
889 int64_t total_sectors;
890 int64_t sector_num = 0;
891 int64_t nb_sectors;
892 int c, pnum;
893 uint64_t bs_sectors;
894 uint64_t progress_base;
895
896 for (;;) {
897 c = getopt(argc, argv, "hpf:F:sq");
898 if (c == -1) {
899 break;
900 }
901 switch (c) {
902 case '?':
903 case 'h':
904 help();
905 break;
906 case 'f':
907 fmt1 = optarg;
908 break;
909 case 'F':
910 fmt2 = optarg;
911 break;
912 case 'p':
913 progress = true;
914 break;
915 case 'q':
916 quiet = true;
917 break;
918 case 's':
919 strict = true;
920 break;
921 }
922 }
923
924 /* Progress is not shown in Quiet mode */
925 if (quiet) {
926 progress = false;
927 }
928
929
930 if (optind > argc - 2) {
931 help();
932 }
933 filename1 = argv[optind++];
934 filename2 = argv[optind++];
935
936 /* Initialize before goto out */
937 qemu_progress_init(progress, 2.0);
938
939 bs1 = bdrv_new_open(filename1, fmt1, BDRV_O_FLAGS, true, quiet);
940 if (!bs1) {
941 error_report("Can't open file %s", filename1);
942 ret = 2;
943 goto out3;
944 }
945
946 bs2 = bdrv_new_open(filename2, fmt2, BDRV_O_FLAGS, true, quiet);
947 if (!bs2) {
948 error_report("Can't open file %s", filename2);
949 ret = 2;
950 goto out2;
951 }
952
953 buf1 = qemu_blockalign(bs1, IO_BUF_SIZE);
954 buf2 = qemu_blockalign(bs2, IO_BUF_SIZE);
955 bdrv_get_geometry(bs1, &bs_sectors);
956 total_sectors1 = bs_sectors;
957 bdrv_get_geometry(bs2, &bs_sectors);
958 total_sectors2 = bs_sectors;
959 total_sectors = MIN(total_sectors1, total_sectors2);
960 progress_base = MAX(total_sectors1, total_sectors2);
961
962 qemu_progress_print(0, 100);
963
964 if (strict && total_sectors1 != total_sectors2) {
965 ret = 1;
966 qprintf(quiet, "Strict mode: Image size mismatch!\n");
967 goto out;
968 }
969
970 for (;;) {
971 nb_sectors = sectors_to_process(total_sectors, sector_num);
972 if (nb_sectors <= 0) {
973 break;
974 }
975 allocated1 = bdrv_is_allocated_above(bs1, NULL, sector_num, nb_sectors,
976 &pnum1);
977 if (allocated1 < 0) {
978 ret = 3;
979 error_report("Sector allocation test failed for %s", filename1);
980 goto out;
981 }
982
983 allocated2 = bdrv_is_allocated_above(bs2, NULL, sector_num, nb_sectors,
984 &pnum2);
985 if (allocated2 < 0) {
986 ret = 3;
987 error_report("Sector allocation test failed for %s", filename2);
988 goto out;
989 }
990 nb_sectors = MIN(pnum1, pnum2);
991
992 if (allocated1 == allocated2) {
993 if (allocated1) {
994 ret = bdrv_read(bs1, sector_num, buf1, nb_sectors);
995 if (ret < 0) {
996 error_report("Error while reading offset %" PRId64 " of %s:"
997 " %s", sectors_to_bytes(sector_num), filename1,
998 strerror(-ret));
999 ret = 4;
1000 goto out;
1001 }
1002 ret = bdrv_read(bs2, sector_num, buf2, nb_sectors);
1003 if (ret < 0) {
1004 error_report("Error while reading offset %" PRId64
1005 " of %s: %s", sectors_to_bytes(sector_num),
1006 filename2, strerror(-ret));
1007 ret = 4;
1008 goto out;
1009 }
1010 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1011 if (ret || pnum != nb_sectors) {
1012 ret = 1;
1013 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1014 sectors_to_bytes(
1015 ret ? sector_num : sector_num + pnum));
1016 goto out;
1017 }
1018 }
1019 } else {
1020 if (strict) {
1021 ret = 1;
1022 qprintf(quiet, "Strict mode: Offset %" PRId64
1023 " allocation mismatch!\n",
1024 sectors_to_bytes(sector_num));
1025 goto out;
1026 }
1027
1028 if (allocated1) {
1029 ret = check_empty_sectors(bs1, sector_num, nb_sectors,
1030 filename1, buf1, quiet);
1031 } else {
1032 ret = check_empty_sectors(bs2, sector_num, nb_sectors,
1033 filename2, buf1, quiet);
1034 }
1035 if (ret) {
1036 if (ret < 0) {
1037 ret = 4;
1038 error_report("Error while reading offset %" PRId64 ": %s",
1039 sectors_to_bytes(sector_num), strerror(-ret));
1040 }
1041 goto out;
1042 }
1043 }
1044 sector_num += nb_sectors;
1045 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1046 }
1047
1048 if (total_sectors1 != total_sectors2) {
1049 BlockDriverState *bs_over;
1050 int64_t total_sectors_over;
1051 const char *filename_over;
1052
1053 qprintf(quiet, "Warning: Image size mismatch!\n");
1054 if (total_sectors1 > total_sectors2) {
1055 total_sectors_over = total_sectors1;
1056 bs_over = bs1;
1057 filename_over = filename1;
1058 } else {
1059 total_sectors_over = total_sectors2;
1060 bs_over = bs2;
1061 filename_over = filename2;
1062 }
1063
1064 for (;;) {
1065 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1066 if (nb_sectors <= 0) {
1067 break;
1068 }
1069 ret = bdrv_is_allocated_above(bs_over, NULL, sector_num,
1070 nb_sectors, &pnum);
1071 if (ret < 0) {
1072 ret = 3;
1073 error_report("Sector allocation test failed for %s",
1074 filename_over);
1075 goto out;
1076
1077 }
1078 nb_sectors = pnum;
1079 if (ret) {
1080 ret = check_empty_sectors(bs_over, sector_num, nb_sectors,
1081 filename_over, buf1, quiet);
1082 if (ret) {
1083 if (ret < 0) {
1084 ret = 4;
1085 error_report("Error while reading offset %" PRId64
1086 " of %s: %s", sectors_to_bytes(sector_num),
1087 filename_over, strerror(-ret));
1088 }
1089 goto out;
1090 }
1091 }
1092 sector_num += nb_sectors;
1093 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1094 }
1095 }
1096
1097 qprintf(quiet, "Images are identical.\n");
1098 ret = 0;
1099
1100out:
1101 bdrv_delete(bs2);
1102 qemu_vfree(buf1);
1103 qemu_vfree(buf2);
1104out2:
1105 bdrv_delete(bs1);
1106out3:
1107 qemu_progress_end();
1108 return ret;
1109}
1110
bellardea2384d2004-08-01 21:59:26 +00001111static int img_convert(int argc, char **argv)
1112{
Jes Sorenseneec77d92010-12-07 17:44:34 +01001113 int c, ret = 0, n, n1, bs_n, bs_i, compress, cluster_size, cluster_sectors;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001114 int progress = 0, flags;
1115 const char *fmt, *out_fmt, *cache, *out_baseimg, *out_filename;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001116 BlockDriver *drv, *proto_drv;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001117 BlockDriverState **bs = NULL, *out_bs = NULL;
ths96b8f132007-12-17 01:35:20 +00001118 int64_t total_sectors, nb_sectors, sector_num, bs_offset;
1119 uint64_t bs_sectors;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001120 uint8_t * buf = NULL;
bellardea2384d2004-08-01 21:59:26 +00001121 const uint8_t *buf1;
bellardfaea38e2006-08-05 21:31:00 +00001122 BlockDriverInfo bdi;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001123 QEMUOptionParameter *param = NULL, *create_options = NULL;
Kevin Wolfa18953f2010-10-14 15:46:04 +02001124 QEMUOptionParameter *out_baseimg_param;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001125 char *options = NULL;
edison51ef6722010-09-21 19:58:41 -07001126 const char *snapshot_name = NULL;
Kevin Wolf1f710492012-10-12 14:29:18 +02001127 float local_progress = 0;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001128 int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001129 bool quiet = false;
bellardea2384d2004-08-01 21:59:26 +00001130
1131 fmt = NULL;
1132 out_fmt = "raw";
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001133 cache = "unsafe";
thsf58c7b32008-06-05 21:53:49 +00001134 out_baseimg = NULL;
Jes Sorenseneec77d92010-12-07 17:44:34 +01001135 compress = 0;
bellardea2384d2004-08-01 21:59:26 +00001136 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001137 c = getopt(argc, argv, "f:O:B:s:hce6o:pS:t:q");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001138 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001139 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001140 }
bellardea2384d2004-08-01 21:59:26 +00001141 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001142 case '?':
bellardea2384d2004-08-01 21:59:26 +00001143 case 'h':
1144 help();
1145 break;
1146 case 'f':
1147 fmt = optarg;
1148 break;
1149 case 'O':
1150 out_fmt = optarg;
1151 break;
thsf58c7b32008-06-05 21:53:49 +00001152 case 'B':
1153 out_baseimg = optarg;
1154 break;
bellardea2384d2004-08-01 21:59:26 +00001155 case 'c':
Jes Sorenseneec77d92010-12-07 17:44:34 +01001156 compress = 1;
bellardea2384d2004-08-01 21:59:26 +00001157 break;
1158 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001159 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001160 "encryption\' instead!");
1161 return 1;
thsec36ba12007-09-16 21:59:02 +00001162 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001163 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001164 "compat6\' instead!");
1165 return 1;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001166 case 'o':
1167 options = optarg;
1168 break;
edison51ef6722010-09-21 19:58:41 -07001169 case 's':
1170 snapshot_name = optarg;
1171 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001172 case 'S':
1173 {
1174 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +01001175 char *end;
1176 sval = strtosz_suffix(optarg, &end, STRTOSZ_DEFSUFFIX_B);
1177 if (sval < 0 || *end) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02001178 error_report("Invalid minimum zero buffer size for sparse output specified");
1179 return 1;
1180 }
1181
1182 min_sparse = sval / BDRV_SECTOR_SIZE;
1183 break;
1184 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001185 case 'p':
1186 progress = 1;
1187 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001188 case 't':
1189 cache = optarg;
1190 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001191 case 'q':
1192 quiet = true;
1193 break;
bellardea2384d2004-08-01 21:59:26 +00001194 }
1195 }
ths3b46e622007-09-17 08:09:54 +00001196
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001197 if (quiet) {
1198 progress = 0;
1199 }
1200
balrog926c2d22007-10-31 01:11:44 +00001201 bs_n = argc - optind - 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001202 if (bs_n < 1) {
1203 help();
1204 }
balrog926c2d22007-10-31 01:11:44 +00001205
1206 out_filename = argv[argc - 1];
thsf58c7b32008-06-05 21:53:49 +00001207
Charles Arnoldfa170c12012-05-11 10:57:54 -06001208 /* Initialize before goto out */
1209 qemu_progress_init(progress, 2.0);
1210
Peter Maydellc8057f92012-08-02 13:45:54 +01001211 if (options && is_help_option(options)) {
Jes Sorensen4ac8aac2010-12-06 15:25:38 +01001212 ret = print_block_option_help(out_filename, out_fmt);
1213 goto out;
1214 }
1215
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001216 if (bs_n > 1 && out_baseimg) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001217 error_report("-B makes no sense when concatenating multiple input "
1218 "images");
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001219 ret = -1;
1220 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001221 }
Dong Xu Wangf8111c22012-03-15 20:13:31 +08001222
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001223 qemu_progress_print(0, 100);
1224
Anthony Liguori7267c092011-08-20 22:09:37 -05001225 bs = g_malloc0(bs_n * sizeof(BlockDriverState *));
balrog926c2d22007-10-31 01:11:44 +00001226
1227 total_sectors = 0;
1228 for (bs_i = 0; bs_i < bs_n; bs_i++) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001229 bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt, BDRV_O_FLAGS, true,
1230 quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001231 if (!bs[bs_i]) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001232 error_report("Could not open '%s'", argv[optind + bs_i]);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001233 ret = -1;
1234 goto out;
1235 }
balrog926c2d22007-10-31 01:11:44 +00001236 bdrv_get_geometry(bs[bs_i], &bs_sectors);
1237 total_sectors += bs_sectors;
1238 }
bellardea2384d2004-08-01 21:59:26 +00001239
edison51ef6722010-09-21 19:58:41 -07001240 if (snapshot_name != NULL) {
1241 if (bs_n > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02001242 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07001243 ret = -1;
1244 goto out;
1245 }
1246 if (bdrv_snapshot_load_tmp(bs[0], snapshot_name) < 0) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02001247 error_report("Failed to load snapshot");
edison51ef6722010-09-21 19:58:41 -07001248 ret = -1;
1249 goto out;
1250 }
1251 }
1252
Kevin Wolfefa84d42009-05-18 16:42:12 +02001253 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +00001254 drv = bdrv_find_format(out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001255 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001256 error_report("Unknown file format '%s'", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001257 ret = -1;
1258 goto out;
1259 }
balrog926c2d22007-10-31 01:11:44 +00001260
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001261 proto_drv = bdrv_find_protocol(out_filename);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001262 if (!proto_drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001263 error_report("Unknown protocol '%s'", out_filename);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001264 ret = -1;
1265 goto out;
1266 }
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001267
1268 create_options = append_option_parameters(create_options,
1269 drv->create_options);
1270 create_options = append_option_parameters(create_options,
1271 proto_drv->create_options);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02001272
Kevin Wolfefa84d42009-05-18 16:42:12 +02001273 if (options) {
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001274 param = parse_option_parameters(options, create_options, param);
Kevin Wolfefa84d42009-05-18 16:42:12 +02001275 if (param == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001276 error_report("Invalid options for file format '%s'.", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001277 ret = -1;
1278 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001279 }
1280 } else {
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001281 param = parse_option_parameters("", create_options, param);
Kevin Wolfefa84d42009-05-18 16:42:12 +02001282 }
1283
1284 set_option_parameter_int(param, BLOCK_OPT_SIZE, total_sectors * 512);
Jes Sorenseneec77d92010-12-07 17:44:34 +01001285 ret = add_old_style_options(out_fmt, param, out_baseimg, NULL);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001286 if (ret < 0) {
1287 goto out;
1288 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001289
Kevin Wolfa18953f2010-10-14 15:46:04 +02001290 /* Get backing file name if -o backing_file was used */
1291 out_baseimg_param = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
1292 if (out_baseimg_param) {
1293 out_baseimg = out_baseimg_param->value.s;
1294 }
1295
Kevin Wolfefa84d42009-05-18 16:42:12 +02001296 /* Check if compression is supported */
Jes Sorenseneec77d92010-12-07 17:44:34 +01001297 if (compress) {
Kevin Wolfefa84d42009-05-18 16:42:12 +02001298 QEMUOptionParameter *encryption =
1299 get_option_parameter(param, BLOCK_OPT_ENCRYPT);
Kevin Wolf41521fa2011-10-18 16:19:42 +02001300 QEMUOptionParameter *preallocation =
1301 get_option_parameter(param, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02001302
1303 if (!drv->bdrv_write_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001304 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001305 ret = -1;
1306 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001307 }
1308
1309 if (encryption && encryption->value.n) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001310 error_report("Compression and encryption not supported at "
1311 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001312 ret = -1;
1313 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001314 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02001315
1316 if (preallocation && preallocation->value.s
1317 && strcmp(preallocation->value.s, "off"))
1318 {
1319 error_report("Compression and preallocation not supported at "
1320 "the same time");
1321 ret = -1;
1322 goto out;
1323 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001324 }
1325
1326 /* Create the new image */
1327 ret = bdrv_create(drv, out_filename, param);
bellardea2384d2004-08-01 21:59:26 +00001328 if (ret < 0) {
1329 if (ret == -ENOTSUP) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001330 error_report("Formatting not supported for file format '%s'",
1331 out_fmt);
aurel326e9ea0c2009-04-15 14:42:46 +00001332 } else if (ret == -EFBIG) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001333 error_report("The image size is too large for file format '%s'",
1334 out_fmt);
bellardea2384d2004-08-01 21:59:26 +00001335 } else {
Jes Sorensen15654a62010-12-16 14:31:53 +01001336 error_report("%s: error while converting %s: %s",
1337 out_filename, out_fmt, strerror(-ret));
bellardea2384d2004-08-01 21:59:26 +00001338 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001339 goto out;
bellardea2384d2004-08-01 21:59:26 +00001340 }
ths3b46e622007-09-17 08:09:54 +00001341
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001342 flags = BDRV_O_RDWR;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +01001343 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001344 if (ret < 0) {
1345 error_report("Invalid cache option: %s", cache);
1346 return -1;
1347 }
1348
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001349 out_bs = bdrv_new_open(out_filename, out_fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001350 if (!out_bs) {
1351 ret = -1;
1352 goto out;
1353 }
bellardea2384d2004-08-01 21:59:26 +00001354
balrog926c2d22007-10-31 01:11:44 +00001355 bs_i = 0;
1356 bs_offset = 0;
1357 bdrv_get_geometry(bs[0], &bs_sectors);
Kevin Wolfbb1c0592011-08-08 14:09:12 +02001358 buf = qemu_blockalign(out_bs, IO_BUF_SIZE);
balrog926c2d22007-10-31 01:11:44 +00001359
Jes Sorenseneec77d92010-12-07 17:44:34 +01001360 if (compress) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001361 ret = bdrv_get_info(out_bs, &bdi);
1362 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001363 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001364 goto out;
1365 }
bellardfaea38e2006-08-05 21:31:00 +00001366 cluster_size = bdi.cluster_size;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001367 if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001368 error_report("invalid cluster size");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001369 ret = -1;
1370 goto out;
1371 }
bellardea2384d2004-08-01 21:59:26 +00001372 cluster_sectors = cluster_size >> 9;
1373 sector_num = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001374
1375 nb_sectors = total_sectors;
Kevin Wolf1f710492012-10-12 14:29:18 +02001376 if (nb_sectors != 0) {
1377 local_progress = (float)100 /
1378 (nb_sectors / MIN(nb_sectors, cluster_sectors));
1379 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001380
bellardea2384d2004-08-01 21:59:26 +00001381 for(;;) {
balrog926c2d22007-10-31 01:11:44 +00001382 int64_t bs_num;
1383 int remainder;
1384 uint8_t *buf2;
1385
bellardea2384d2004-08-01 21:59:26 +00001386 nb_sectors = total_sectors - sector_num;
1387 if (nb_sectors <= 0)
1388 break;
1389 if (nb_sectors >= cluster_sectors)
1390 n = cluster_sectors;
1391 else
1392 n = nb_sectors;
balrog926c2d22007-10-31 01:11:44 +00001393
1394 bs_num = sector_num - bs_offset;
1395 assert (bs_num >= 0);
1396 remainder = n;
1397 buf2 = buf;
1398 while (remainder > 0) {
1399 int nlow;
1400 while (bs_num == bs_sectors) {
1401 bs_i++;
1402 assert (bs_i < bs_n);
1403 bs_offset += bs_sectors;
1404 bdrv_get_geometry(bs[bs_i], &bs_sectors);
1405 bs_num = 0;
Blue Swirl0bfcd592010-05-22 08:02:12 +00001406 /* printf("changing part: sector_num=%" PRId64 ", "
1407 "bs_i=%d, bs_offset=%" PRId64 ", bs_sectors=%" PRId64
1408 "\n", sector_num, bs_i, bs_offset, bs_sectors); */
balrog926c2d22007-10-31 01:11:44 +00001409 }
1410 assert (bs_num < bs_sectors);
1411
1412 nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder;
1413
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001414 ret = bdrv_read(bs[bs_i], bs_num, buf2, nlow);
1415 if (ret < 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001416 error_report("error while reading sector %" PRId64 ": %s",
1417 bs_num, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001418 goto out;
1419 }
balrog926c2d22007-10-31 01:11:44 +00001420
1421 buf2 += nlow * 512;
1422 bs_num += nlow;
1423
1424 remainder -= nlow;
1425 }
1426 assert (remainder == 0);
1427
Stefan Hajnoczi54f106d2013-04-15 17:17:33 +02001428 if (!buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)) {
1429 ret = bdrv_write_compressed(out_bs, sector_num, buf, n);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001430 if (ret != 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001431 error_report("error while compressing sector %" PRId64
1432 ": %s", sector_num, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001433 goto out;
1434 }
bellardea2384d2004-08-01 21:59:26 +00001435 }
1436 sector_num += n;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001437 qemu_progress_print(local_progress, 100);
bellardea2384d2004-08-01 21:59:26 +00001438 }
bellardfaea38e2006-08-05 21:31:00 +00001439 /* signal EOF to align */
1440 bdrv_write_compressed(out_bs, 0, NULL, 0);
bellardea2384d2004-08-01 21:59:26 +00001441 } else {
Kevin Wolff2feebb2010-04-14 17:30:35 +02001442 int has_zero_init = bdrv_has_zero_init(out_bs);
1443
thsf58c7b32008-06-05 21:53:49 +00001444 sector_num = 0; // total number of sectors converted so far
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001445 nb_sectors = total_sectors - sector_num;
Kevin Wolf1f710492012-10-12 14:29:18 +02001446 if (nb_sectors != 0) {
1447 local_progress = (float)100 /
1448 (nb_sectors / MIN(nb_sectors, IO_BUF_SIZE / 512));
1449 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001450
bellardea2384d2004-08-01 21:59:26 +00001451 for(;;) {
1452 nb_sectors = total_sectors - sector_num;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001453 if (nb_sectors <= 0) {
bellardea2384d2004-08-01 21:59:26 +00001454 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001455 }
1456 if (nb_sectors >= (IO_BUF_SIZE / 512)) {
bellardea2384d2004-08-01 21:59:26 +00001457 n = (IO_BUF_SIZE / 512);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001458 } else {
bellardea2384d2004-08-01 21:59:26 +00001459 n = nb_sectors;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001460 }
balrog926c2d22007-10-31 01:11:44 +00001461
1462 while (sector_num - bs_offset >= bs_sectors) {
1463 bs_i ++;
1464 assert (bs_i < bs_n);
1465 bs_offset += bs_sectors;
1466 bdrv_get_geometry(bs[bs_i], &bs_sectors);
Blue Swirl0bfcd592010-05-22 08:02:12 +00001467 /* printf("changing part: sector_num=%" PRId64 ", bs_i=%d, "
1468 "bs_offset=%" PRId64 ", bs_sectors=%" PRId64 "\n",
balrog926c2d22007-10-31 01:11:44 +00001469 sector_num, bs_i, bs_offset, bs_sectors); */
1470 }
1471
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001472 if (n > bs_offset + bs_sectors - sector_num) {
balrog926c2d22007-10-31 01:11:44 +00001473 n = bs_offset + bs_sectors - sector_num;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001474 }
balrog926c2d22007-10-31 01:11:44 +00001475
Kevin Wolff2feebb2010-04-14 17:30:35 +02001476 if (has_zero_init) {
Akkarit Sangpetchd0320442009-07-17 10:02:15 +02001477 /* If the output image is being created as a copy on write image,
1478 assume that sectors which are unallocated in the input image
1479 are present in both the output's and input's base images (no
1480 need to copy them). */
1481 if (out_baseimg) {
1482 if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset,
1483 n, &n1)) {
1484 sector_num += n1;
1485 continue;
1486 }
1487 /* The next 'n1' sectors are allocated in the input image. Copy
1488 only those as they may be followed by unallocated sectors. */
1489 n = n1;
aliguori93c65b42009-04-05 17:40:43 +00001490 }
aliguori93c65b42009-04-05 17:40:43 +00001491 } else {
1492 n1 = n;
thsf58c7b32008-06-05 21:53:49 +00001493 }
1494
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001495 ret = bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n);
1496 if (ret < 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001497 error_report("error while reading sector %" PRId64 ": %s",
1498 sector_num - bs_offset, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001499 goto out;
1500 }
bellardea2384d2004-08-01 21:59:26 +00001501 /* NOTE: at the same time we convert, we do not write zero
1502 sectors to have a chance to compress the image. Ideally, we
1503 should add a specific call to have the info to go faster */
1504 buf1 = buf;
1505 while (n > 0) {
thsf58c7b32008-06-05 21:53:49 +00001506 /* If the output image is being created as a copy on write image,
1507 copy all sectors even the ones containing only NUL bytes,
aliguori93c65b42009-04-05 17:40:43 +00001508 because they may differ from the sectors in the base image.
1509
1510 If the output is to a host device, we also write out
1511 sectors that are entirely 0, since whatever data was
1512 already there is garbage, not 0s. */
Kevin Wolff2feebb2010-04-14 17:30:35 +02001513 if (!has_zero_init || out_baseimg ||
Kevin Wolfa22f1232011-08-26 15:27:13 +02001514 is_allocated_sectors_min(buf1, n, &n1, min_sparse)) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001515 ret = bdrv_write(out_bs, sector_num, buf1, n1);
1516 if (ret < 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001517 error_report("error while writing sector %" PRId64
1518 ": %s", sector_num, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001519 goto out;
1520 }
bellardea2384d2004-08-01 21:59:26 +00001521 }
1522 sector_num += n1;
1523 n -= n1;
1524 buf1 += n1 * 512;
1525 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001526 qemu_progress_print(local_progress, 100);
bellardea2384d2004-08-01 21:59:26 +00001527 }
1528 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001529out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001530 qemu_progress_end();
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001531 free_option_parameters(create_options);
1532 free_option_parameters(param);
Kevin Wolfbb1c0592011-08-08 14:09:12 +02001533 qemu_vfree(buf);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001534 if (out_bs) {
1535 bdrv_delete(out_bs);
1536 }
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001537 if (bs) {
1538 for (bs_i = 0; bs_i < bs_n; bs_i++) {
1539 if (bs[bs_i]) {
1540 bdrv_delete(bs[bs_i]);
1541 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001542 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001543 g_free(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001544 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001545 if (ret) {
1546 return 1;
1547 }
bellardea2384d2004-08-01 21:59:26 +00001548 return 0;
1549}
1550
bellard57d1a2b2004-08-03 21:15:11 +00001551
bellardfaea38e2006-08-05 21:31:00 +00001552static void dump_snapshots(BlockDriverState *bs)
1553{
1554 QEMUSnapshotInfo *sn_tab, *sn;
1555 int nb_sns, i;
1556 char buf[256];
1557
1558 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1559 if (nb_sns <= 0)
1560 return;
1561 printf("Snapshot list:\n");
1562 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1563 for(i = 0; i < nb_sns; i++) {
1564 sn = &sn_tab[i];
1565 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
1566 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001567 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00001568}
1569
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001570static void dump_json_image_info_list(ImageInfoList *list)
1571{
1572 Error *errp = NULL;
1573 QString *str;
1574 QmpOutputVisitor *ov = qmp_output_visitor_new();
1575 QObject *obj;
1576 visit_type_ImageInfoList(qmp_output_get_visitor(ov),
1577 &list, NULL, &errp);
1578 obj = qmp_output_get_qobject(ov);
1579 str = qobject_to_json_pretty(obj);
1580 assert(str != NULL);
1581 printf("%s\n", qstring_get_str(str));
1582 qobject_decref(obj);
1583 qmp_output_visitor_cleanup(ov);
1584 QDECREF(str);
1585}
1586
Benoît Canetc054b3f2012-09-05 13:09:02 +02001587static void collect_snapshots(BlockDriverState *bs , ImageInfo *info)
bellardea2384d2004-08-01 21:59:26 +00001588{
Benoît Canetc054b3f2012-09-05 13:09:02 +02001589 int i, sn_count;
1590 QEMUSnapshotInfo *sn_tab = NULL;
1591 SnapshotInfoList *info_list, *cur_item = NULL;
1592 sn_count = bdrv_snapshot_list(bs, &sn_tab);
1593
1594 for (i = 0; i < sn_count; i++) {
1595 info->has_snapshots = true;
1596 info_list = g_new0(SnapshotInfoList, 1);
1597
1598 info_list->value = g_new0(SnapshotInfo, 1);
1599 info_list->value->id = g_strdup(sn_tab[i].id_str);
1600 info_list->value->name = g_strdup(sn_tab[i].name);
1601 info_list->value->vm_state_size = sn_tab[i].vm_state_size;
1602 info_list->value->date_sec = sn_tab[i].date_sec;
1603 info_list->value->date_nsec = sn_tab[i].date_nsec;
1604 info_list->value->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000;
1605 info_list->value->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
1606
1607 /* XXX: waiting for the qapi to support qemu-queue.h types */
1608 if (!cur_item) {
1609 info->snapshots = cur_item = info_list;
1610 } else {
1611 cur_item->next = info_list;
1612 cur_item = info_list;
1613 }
1614
1615 }
1616
1617 g_free(sn_tab);
1618}
1619
1620static void dump_json_image_info(ImageInfo *info)
1621{
1622 Error *errp = NULL;
1623 QString *str;
1624 QmpOutputVisitor *ov = qmp_output_visitor_new();
1625 QObject *obj;
1626 visit_type_ImageInfo(qmp_output_get_visitor(ov),
1627 &info, NULL, &errp);
1628 obj = qmp_output_get_qobject(ov);
1629 str = qobject_to_json_pretty(obj);
1630 assert(str != NULL);
1631 printf("%s\n", qstring_get_str(str));
1632 qobject_decref(obj);
1633 qmp_output_visitor_cleanup(ov);
1634 QDECREF(str);
1635}
1636
1637static void collect_image_info(BlockDriverState *bs,
1638 ImageInfo *info,
1639 const char *filename,
1640 const char *fmt)
1641{
ths96b8f132007-12-17 01:35:20 +00001642 uint64_t total_sectors;
bellard93b6b2a2006-08-01 15:51:11 +00001643 char backing_filename[1024];
1644 char backing_filename2[1024];
bellardfaea38e2006-08-05 21:31:00 +00001645 BlockDriverInfo bdi;
bellardea2384d2004-08-01 21:59:26 +00001646
Benoît Canetc054b3f2012-09-05 13:09:02 +02001647 bdrv_get_geometry(bs, &total_sectors);
1648
1649 info->filename = g_strdup(filename);
1650 info->format = g_strdup(bdrv_get_format_name(bs));
1651 info->virtual_size = total_sectors * 512;
1652 info->actual_size = bdrv_get_allocated_file_size(bs);
1653 info->has_actual_size = info->actual_size >= 0;
1654 if (bdrv_is_encrypted(bs)) {
1655 info->encrypted = true;
1656 info->has_encrypted = true;
1657 }
1658 if (bdrv_get_info(bs, &bdi) >= 0) {
1659 if (bdi.cluster_size != 0) {
1660 info->cluster_size = bdi.cluster_size;
1661 info->has_cluster_size = true;
1662 }
1663 info->dirty_flag = bdi.is_dirty;
1664 info->has_dirty_flag = true;
1665 }
1666 bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
1667 if (backing_filename[0] != '\0') {
1668 info->backing_filename = g_strdup(backing_filename);
1669 info->has_backing_filename = true;
1670 bdrv_get_full_backing_filename(bs, backing_filename2,
1671 sizeof(backing_filename2));
1672
1673 if (strcmp(backing_filename, backing_filename2) != 0) {
1674 info->full_backing_filename =
1675 g_strdup(backing_filename2);
1676 info->has_full_backing_filename = true;
1677 }
1678
1679 if (bs->backing_format[0]) {
1680 info->backing_filename_format = g_strdup(bs->backing_format);
1681 info->has_backing_filename_format = true;
1682 }
1683 }
1684}
1685
1686static void dump_human_image_info(ImageInfo *info)
1687{
1688 char size_buf[128], dsize_buf[128];
1689 if (!info->has_actual_size) {
1690 snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
1691 } else {
1692 get_human_readable_size(dsize_buf, sizeof(dsize_buf),
1693 info->actual_size);
1694 }
1695 get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
1696 printf("image: %s\n"
1697 "file format: %s\n"
1698 "virtual size: %s (%" PRId64 " bytes)\n"
1699 "disk size: %s\n",
1700 info->filename, info->format, size_buf,
1701 info->virtual_size,
1702 dsize_buf);
1703
1704 if (info->has_encrypted && info->encrypted) {
1705 printf("encrypted: yes\n");
1706 }
1707
1708 if (info->has_cluster_size) {
1709 printf("cluster_size: %" PRId64 "\n", info->cluster_size);
1710 }
1711
1712 if (info->has_dirty_flag && info->dirty_flag) {
1713 printf("cleanly shut down: no\n");
1714 }
1715
1716 if (info->has_backing_filename) {
1717 printf("backing file: %s", info->backing_filename);
1718 if (info->has_full_backing_filename) {
1719 printf(" (actual path: %s)", info->full_backing_filename);
1720 }
1721 putchar('\n');
1722 if (info->has_backing_filename_format) {
1723 printf("backing file format: %s\n", info->backing_filename_format);
1724 }
1725 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001726
1727 if (info->has_snapshots) {
1728 SnapshotInfoList *elem;
1729 char buf[256];
1730
1731 printf("Snapshot list:\n");
1732 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1733
1734 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
1735 * we convert to the block layer's native QEMUSnapshotInfo for now.
1736 */
1737 for (elem = info->snapshots; elem; elem = elem->next) {
1738 QEMUSnapshotInfo sn = {
1739 .vm_state_size = elem->value->vm_state_size,
1740 .date_sec = elem->value->date_sec,
1741 .date_nsec = elem->value->date_nsec,
1742 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
1743 elem->value->vm_clock_nsec,
1744 };
1745
1746 pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
1747 pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
1748 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), &sn));
1749 }
1750 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001751}
1752
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001753static void dump_human_image_info_list(ImageInfoList *list)
1754{
1755 ImageInfoList *elem;
1756 bool delim = false;
1757
1758 for (elem = list; elem; elem = elem->next) {
1759 if (delim) {
1760 printf("\n");
1761 }
1762 delim = true;
1763
1764 dump_human_image_info(elem->value);
1765 }
1766}
1767
1768static gboolean str_equal_func(gconstpointer a, gconstpointer b)
1769{
1770 return strcmp(a, b) == 0;
1771}
1772
1773/**
1774 * Open an image file chain and return an ImageInfoList
1775 *
1776 * @filename: topmost image filename
1777 * @fmt: topmost image format (may be NULL to autodetect)
1778 * @chain: true - enumerate entire backing file chain
1779 * false - only topmost image file
1780 *
1781 * Returns a list of ImageInfo objects or NULL if there was an error opening an
1782 * image file. If there was an error a message will have been printed to
1783 * stderr.
1784 */
1785static ImageInfoList *collect_image_info_list(const char *filename,
1786 const char *fmt,
1787 bool chain)
1788{
1789 ImageInfoList *head = NULL;
1790 ImageInfoList **last = &head;
1791 GHashTable *filenames;
1792
1793 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
1794
1795 while (filename) {
1796 BlockDriverState *bs;
1797 ImageInfo *info;
1798 ImageInfoList *elem;
1799
1800 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
1801 error_report("Backing file '%s' creates an infinite loop.",
1802 filename);
1803 goto err;
1804 }
1805 g_hash_table_insert(filenames, (gpointer)filename, NULL);
1806
1807 bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_NO_BACKING,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001808 false, false);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001809 if (!bs) {
1810 goto err;
1811 }
1812
1813 info = g_new0(ImageInfo, 1);
1814 collect_image_info(bs, info, filename, fmt);
1815 collect_snapshots(bs, info);
1816
1817 elem = g_new0(ImageInfoList, 1);
1818 elem->value = info;
1819 *last = elem;
1820 last = &elem->next;
1821
1822 bdrv_delete(bs);
1823
1824 filename = fmt = NULL;
1825 if (chain) {
1826 if (info->has_full_backing_filename) {
1827 filename = info->full_backing_filename;
1828 } else if (info->has_backing_filename) {
1829 filename = info->backing_filename;
1830 }
1831 if (info->has_backing_filename_format) {
1832 fmt = info->backing_filename_format;
1833 }
1834 }
1835 }
1836 g_hash_table_destroy(filenames);
1837 return head;
1838
1839err:
1840 qapi_free_ImageInfoList(head);
1841 g_hash_table_destroy(filenames);
1842 return NULL;
1843}
1844
Benoît Canetc054b3f2012-09-05 13:09:02 +02001845static int img_info(int argc, char **argv)
1846{
1847 int c;
1848 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001849 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001850 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001851 ImageInfoList *list;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001852
bellardea2384d2004-08-01 21:59:26 +00001853 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001854 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00001855 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02001856 int option_index = 0;
1857 static const struct option long_options[] = {
1858 {"help", no_argument, 0, 'h'},
1859 {"format", required_argument, 0, 'f'},
1860 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001861 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Benoît Canetc054b3f2012-09-05 13:09:02 +02001862 {0, 0, 0, 0}
1863 };
1864 c = getopt_long(argc, argv, "f:h",
1865 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001866 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001867 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001868 }
bellardea2384d2004-08-01 21:59:26 +00001869 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001870 case '?':
bellardea2384d2004-08-01 21:59:26 +00001871 case 'h':
1872 help();
1873 break;
1874 case 'f':
1875 fmt = optarg;
1876 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001877 case OPTION_OUTPUT:
1878 output = optarg;
1879 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001880 case OPTION_BACKING_CHAIN:
1881 chain = true;
1882 break;
bellardea2384d2004-08-01 21:59:26 +00001883 }
1884 }
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001885 if (optind >= argc) {
bellardea2384d2004-08-01 21:59:26 +00001886 help();
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001887 }
bellardea2384d2004-08-01 21:59:26 +00001888 filename = argv[optind++];
1889
Benoît Canetc054b3f2012-09-05 13:09:02 +02001890 if (output && !strcmp(output, "json")) {
1891 output_format = OFORMAT_JSON;
1892 } else if (output && !strcmp(output, "human")) {
1893 output_format = OFORMAT_HUMAN;
1894 } else if (output) {
1895 error_report("--output must be used with human or json as argument.");
1896 return 1;
1897 }
1898
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001899 list = collect_image_info_list(filename, fmt, chain);
1900 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001901 return 1;
1902 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001903
Benoît Canetc054b3f2012-09-05 13:09:02 +02001904 switch (output_format) {
1905 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001906 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02001907 break;
1908 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001909 if (chain) {
1910 dump_json_image_info_list(list);
1911 } else {
1912 dump_json_image_info(list->value);
1913 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001914 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001915 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001916
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001917 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00001918 return 0;
1919}
1920
aliguorif7b4a942009-01-07 17:40:15 +00001921#define SNAPSHOT_LIST 1
1922#define SNAPSHOT_CREATE 2
1923#define SNAPSHOT_APPLY 3
1924#define SNAPSHOT_DELETE 4
1925
Stuart Brady153859b2009-06-07 00:42:17 +01001926static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00001927{
1928 BlockDriverState *bs;
1929 QEMUSnapshotInfo sn;
1930 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001931 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00001932 int action = 0;
1933 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001934 bool quiet = false;
aliguorif7b4a942009-01-07 17:40:15 +00001935
Kevin Wolf710da702011-01-10 12:33:02 +01001936 bdrv_oflags = BDRV_O_FLAGS | BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00001937 /* Parse commandline parameters */
1938 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001939 c = getopt(argc, argv, "la:c:d:hq");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001940 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00001941 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001942 }
aliguorif7b4a942009-01-07 17:40:15 +00001943 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001944 case '?':
aliguorif7b4a942009-01-07 17:40:15 +00001945 case 'h':
1946 help();
Stuart Brady153859b2009-06-07 00:42:17 +01001947 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00001948 case 'l':
1949 if (action) {
1950 help();
Stuart Brady153859b2009-06-07 00:42:17 +01001951 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00001952 }
1953 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02001954 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00001955 break;
1956 case 'a':
1957 if (action) {
1958 help();
Stuart Brady153859b2009-06-07 00:42:17 +01001959 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00001960 }
1961 action = SNAPSHOT_APPLY;
1962 snapshot_name = optarg;
1963 break;
1964 case 'c':
1965 if (action) {
1966 help();
Stuart Brady153859b2009-06-07 00:42:17 +01001967 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00001968 }
1969 action = SNAPSHOT_CREATE;
1970 snapshot_name = optarg;
1971 break;
1972 case 'd':
1973 if (action) {
1974 help();
Stuart Brady153859b2009-06-07 00:42:17 +01001975 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00001976 }
1977 action = SNAPSHOT_DELETE;
1978 snapshot_name = optarg;
1979 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001980 case 'q':
1981 quiet = true;
1982 break;
aliguorif7b4a942009-01-07 17:40:15 +00001983 }
1984 }
1985
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001986 if (optind >= argc) {
aliguorif7b4a942009-01-07 17:40:15 +00001987 help();
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001988 }
aliguorif7b4a942009-01-07 17:40:15 +00001989 filename = argv[optind++];
1990
1991 /* Open the image */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001992 bs = bdrv_new_open(filename, NULL, bdrv_oflags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001993 if (!bs) {
1994 return 1;
1995 }
aliguorif7b4a942009-01-07 17:40:15 +00001996
1997 /* Perform the requested action */
1998 switch(action) {
1999 case SNAPSHOT_LIST:
2000 dump_snapshots(bs);
2001 break;
2002
2003 case SNAPSHOT_CREATE:
2004 memset(&sn, 0, sizeof(sn));
2005 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2006
2007 qemu_gettimeofday(&tv);
2008 sn.date_sec = tv.tv_sec;
2009 sn.date_nsec = tv.tv_usec * 1000;
2010
2011 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002012 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002013 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002014 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002015 }
aliguorif7b4a942009-01-07 17:40:15 +00002016 break;
2017
2018 case SNAPSHOT_APPLY:
2019 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002020 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002021 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002022 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002023 }
aliguorif7b4a942009-01-07 17:40:15 +00002024 break;
2025
2026 case SNAPSHOT_DELETE:
2027 ret = bdrv_snapshot_delete(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002028 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002029 error_report("Could not delete snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002030 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002031 }
aliguorif7b4a942009-01-07 17:40:15 +00002032 break;
2033 }
2034
2035 /* Cleanup */
2036 bdrv_delete(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002037 if (ret) {
2038 return 1;
2039 }
Stuart Brady153859b2009-06-07 00:42:17 +01002040 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002041}
2042
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002043static int img_rebase(int argc, char **argv)
2044{
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002045 BlockDriverState *bs, *bs_old_backing = NULL, *bs_new_backing = NULL;
Stefan Hajnoczif163d072010-04-13 10:29:34 +01002046 BlockDriver *old_backing_drv, *new_backing_drv;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002047 char *filename;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002048 const char *fmt, *cache, *out_basefmt, *out_baseimg;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002049 int c, flags, ret;
2050 int unsafe = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002051 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002052 bool quiet = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002053
2054 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002055 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002056 cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002057 out_baseimg = NULL;
2058 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002059 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002060 c = getopt(argc, argv, "uhf:F:b:pt:q");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002061 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002062 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002063 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002064 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002065 case '?':
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002066 case 'h':
2067 help();
2068 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002069 case 'f':
2070 fmt = optarg;
2071 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002072 case 'F':
2073 out_basefmt = optarg;
2074 break;
2075 case 'b':
2076 out_baseimg = optarg;
2077 break;
2078 case 'u':
2079 unsafe = 1;
2080 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002081 case 'p':
2082 progress = 1;
2083 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002084 case 't':
2085 cache = optarg;
2086 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002087 case 'q':
2088 quiet = true;
2089 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002090 }
2091 }
2092
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002093 if (quiet) {
2094 progress = 0;
2095 }
2096
Anthony Liguori9a9d9db2011-04-13 15:51:47 +01002097 if ((optind >= argc) || (!unsafe && !out_baseimg)) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002098 help();
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002099 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002100 filename = argv[optind++];
2101
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002102 qemu_progress_init(progress, 2.0);
2103 qemu_progress_print(0, 100);
2104
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002105 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +01002106 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002107 if (ret < 0) {
2108 error_report("Invalid cache option: %s", cache);
2109 return -1;
2110 }
2111
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002112 /*
2113 * Open the images.
2114 *
2115 * Ignore the old backing file for unsafe rebase in case we want to correct
2116 * the reference to a renamed or moved backing file.
2117 */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002118 bs = bdrv_new_open(filename, fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002119 if (!bs) {
2120 return 1;
2121 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002122
2123 /* Find the right drivers for the backing files */
2124 old_backing_drv = NULL;
2125 new_backing_drv = NULL;
2126
2127 if (!unsafe && bs->backing_format[0] != '\0') {
2128 old_backing_drv = bdrv_find_format(bs->backing_format);
2129 if (old_backing_drv == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002130 error_report("Invalid format name: '%s'", bs->backing_format);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002131 ret = -1;
2132 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002133 }
2134 }
2135
2136 if (out_basefmt != NULL) {
2137 new_backing_drv = bdrv_find_format(out_basefmt);
2138 if (new_backing_drv == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002139 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002140 ret = -1;
2141 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002142 }
2143 }
2144
2145 /* For safe rebasing we need to compare old and new backing file */
2146 if (unsafe) {
2147 /* Make the compiler happy */
2148 bs_old_backing = NULL;
2149 bs_new_backing = NULL;
2150 } else {
2151 char backing_name[1024];
2152
2153 bs_old_backing = bdrv_new("old_backing");
2154 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01002155 ret = bdrv_open(bs_old_backing, backing_name, NULL, BDRV_O_FLAGS,
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002156 old_backing_drv);
2157 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002158 error_report("Could not open old backing file '%s'", backing_name);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002159 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002160 }
Alex Bligha6166732012-10-16 13:46:18 +01002161 if (out_baseimg[0]) {
2162 bs_new_backing = bdrv_new("new_backing");
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01002163 ret = bdrv_open(bs_new_backing, out_baseimg, NULL, BDRV_O_FLAGS,
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002164 new_backing_drv);
Alex Bligha6166732012-10-16 13:46:18 +01002165 if (ret) {
2166 error_report("Could not open new backing file '%s'",
2167 out_baseimg);
2168 goto out;
2169 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002170 }
2171 }
2172
2173 /*
2174 * Check each unallocated cluster in the COW file. If it is unallocated,
2175 * accesses go to the backing file. We must therefore compare this cluster
2176 * in the old and new backing file, and if they differ we need to copy it
2177 * from the old backing file into the COW file.
2178 *
2179 * If qemu-img crashes during this step, no harm is done. The content of
2180 * the image is the same as the original one at any time.
2181 */
2182 if (!unsafe) {
2183 uint64_t num_sectors;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002184 uint64_t old_backing_num_sectors;
Alex Bligha6166732012-10-16 13:46:18 +01002185 uint64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002186 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02002187 int n;
TeLeMand6771bf2010-02-08 16:20:00 +08002188 uint8_t * buf_old;
2189 uint8_t * buf_new;
Kevin Wolf1f710492012-10-12 14:29:18 +02002190 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08002191
Kevin Wolfbb1c0592011-08-08 14:09:12 +02002192 buf_old = qemu_blockalign(bs, IO_BUF_SIZE);
2193 buf_new = qemu_blockalign(bs, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002194
2195 bdrv_get_geometry(bs, &num_sectors);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002196 bdrv_get_geometry(bs_old_backing, &old_backing_num_sectors);
Alex Bligha6166732012-10-16 13:46:18 +01002197 if (bs_new_backing) {
2198 bdrv_get_geometry(bs_new_backing, &new_backing_num_sectors);
2199 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002200
Kevin Wolf1f710492012-10-12 14:29:18 +02002201 if (num_sectors != 0) {
2202 local_progress = (float)100 /
2203 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
2204 }
2205
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002206 for (sector = 0; sector < num_sectors; sector += n) {
2207
2208 /* How many sectors can we handle with the next read? */
2209 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
2210 n = (IO_BUF_SIZE / 512);
2211 } else {
2212 n = num_sectors - sector;
2213 }
2214
2215 /* If the cluster is allocated, we don't need to take action */
Kevin Wolfcc60e322010-04-29 14:47:48 +02002216 ret = bdrv_is_allocated(bs, sector, n, &n);
2217 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002218 continue;
2219 }
2220
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002221 /*
2222 * Read old and new backing file and take into consideration that
2223 * backing files may be smaller than the COW image.
2224 */
2225 if (sector >= old_backing_num_sectors) {
2226 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
2227 } else {
2228 if (sector + n > old_backing_num_sectors) {
2229 n = old_backing_num_sectors - sector;
2230 }
2231
2232 ret = bdrv_read(bs_old_backing, sector, buf_old, n);
2233 if (ret < 0) {
2234 error_report("error while reading from old backing file");
2235 goto out;
2236 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002237 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002238
Alex Bligha6166732012-10-16 13:46:18 +01002239 if (sector >= new_backing_num_sectors || !bs_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002240 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
2241 } else {
2242 if (sector + n > new_backing_num_sectors) {
2243 n = new_backing_num_sectors - sector;
2244 }
2245
2246 ret = bdrv_read(bs_new_backing, sector, buf_new, n);
2247 if (ret < 0) {
2248 error_report("error while reading from new backing file");
2249 goto out;
2250 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002251 }
2252
2253 /* If they differ, we need to write to the COW file */
2254 uint64_t written = 0;
2255
2256 while (written < n) {
2257 int pnum;
2258
2259 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01002260 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002261 {
2262 ret = bdrv_write(bs, sector + written,
2263 buf_old + written * 512, pnum);
2264 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002265 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002266 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002267 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002268 }
2269 }
2270
2271 written += pnum;
2272 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002273 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002274 }
TeLeMand6771bf2010-02-08 16:20:00 +08002275
Kevin Wolfbb1c0592011-08-08 14:09:12 +02002276 qemu_vfree(buf_old);
2277 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002278 }
2279
2280 /*
2281 * Change the backing file. All clusters that are different from the old
2282 * backing file are overwritten in the COW file now, so the visible content
2283 * doesn't change when we switch the backing file.
2284 */
Alex Bligha6166732012-10-16 13:46:18 +01002285 if (out_baseimg && *out_baseimg) {
2286 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
2287 } else {
2288 ret = bdrv_change_backing_file(bs, NULL, NULL);
2289 }
2290
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002291 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002292 error_report("Could not change the backing file to '%s': No "
2293 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002294 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002295 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002296 out_baseimg, strerror(-ret));
2297 }
2298
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002299 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002300 /*
2301 * TODO At this point it is possible to check if any clusters that are
2302 * allocated in the COW file are the same in the backing file. If so, they
2303 * could be dropped from the COW file. Don't do this before switching the
2304 * backing file, in case of a crash this would lead to corruption.
2305 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002306out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002307 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002308 /* Cleanup */
2309 if (!unsafe) {
Kevin Wolfeb863ad2011-03-31 12:39:51 +02002310 if (bs_old_backing != NULL) {
2311 bdrv_delete(bs_old_backing);
2312 }
2313 if (bs_new_backing != NULL) {
2314 bdrv_delete(bs_new_backing);
2315 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002316 }
2317
2318 bdrv_delete(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002319 if (ret) {
2320 return 1;
2321 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002322 return 0;
2323}
2324
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002325static int img_resize(int argc, char **argv)
2326{
2327 int c, ret, relative;
2328 const char *filename, *fmt, *size;
2329 int64_t n, total_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002330 bool quiet = false;
Jes Sorensen2a819982010-12-06 17:08:31 +01002331 BlockDriverState *bs = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002332 QemuOpts *param;
2333 static QemuOptsList resize_options = {
2334 .name = "resize_options",
2335 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
2336 .desc = {
2337 {
2338 .name = BLOCK_OPT_SIZE,
2339 .type = QEMU_OPT_SIZE,
2340 .help = "Virtual disk size"
2341 }, {
2342 /* end of list */
2343 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002344 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002345 };
2346
Kevin Wolfe80fec72011-04-29 10:58:12 +02002347 /* Remove size from argv manually so that negative numbers are not treated
2348 * as options by getopt. */
2349 if (argc < 3) {
2350 help();
2351 return 1;
2352 }
2353
2354 size = argv[--argc];
2355
2356 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002357 fmt = NULL;
2358 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002359 c = getopt(argc, argv, "f:hq");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002360 if (c == -1) {
2361 break;
2362 }
2363 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002364 case '?':
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002365 case 'h':
2366 help();
2367 break;
2368 case 'f':
2369 fmt = optarg;
2370 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002371 case 'q':
2372 quiet = true;
2373 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002374 }
2375 }
Kevin Wolfe80fec72011-04-29 10:58:12 +02002376 if (optind >= argc) {
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002377 help();
2378 }
2379 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002380
2381 /* Choose grow, shrink, or absolute resize mode */
2382 switch (size[0]) {
2383 case '+':
2384 relative = 1;
2385 size++;
2386 break;
2387 case '-':
2388 relative = -1;
2389 size++;
2390 break;
2391 default:
2392 relative = 0;
2393 break;
2394 }
2395
2396 /* Parse size */
Dong Xu Wange478b442012-12-06 14:47:22 +08002397 param = qemu_opts_create_nofail(&resize_options);
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002398 if (qemu_opt_set(param, BLOCK_OPT_SIZE, size)) {
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002399 /* Error message already printed when size parsing fails */
Jes Sorensen2a819982010-12-06 17:08:31 +01002400 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002401 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01002402 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002403 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002404 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
2405 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002406
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002407 bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002408 if (!bs) {
Jes Sorensen2a819982010-12-06 17:08:31 +01002409 ret = -1;
2410 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002411 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002412
2413 if (relative) {
2414 total_size = bdrv_getlength(bs) + n * relative;
2415 } else {
2416 total_size = n;
2417 }
2418 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002419 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002420 ret = -1;
2421 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002422 }
2423
2424 ret = bdrv_truncate(bs, total_size);
2425 switch (ret) {
2426 case 0:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002427 qprintf(quiet, "Image resized.\n");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002428 break;
2429 case -ENOTSUP:
Kevin Wolf259b2172012-03-06 12:44:45 +01002430 error_report("This image does not support resize");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002431 break;
2432 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +01002433 error_report("Image is read-only");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002434 break;
2435 default:
Jes Sorensen15654a62010-12-16 14:31:53 +01002436 error_report("Error resizing image (%d)", -ret);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002437 break;
2438 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002439out:
Jes Sorensen2a819982010-12-06 17:08:31 +01002440 if (bs) {
2441 bdrv_delete(bs);
2442 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002443 if (ret) {
2444 return 1;
2445 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002446 return 0;
2447}
2448
Anthony Liguoric227f092009-10-01 16:12:16 -05002449static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01002450#define DEF(option, callback, arg_string) \
2451 { option, callback },
2452#include "qemu-img-cmds.h"
2453#undef DEF
2454#undef GEN_DOCS
2455 { NULL, NULL, },
2456};
2457
bellardea2384d2004-08-01 21:59:26 +00002458int main(int argc, char **argv)
2459{
Anthony Liguoric227f092009-10-01 16:12:16 -05002460 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01002461 const char *cmdname;
bellardea2384d2004-08-01 21:59:26 +00002462
Kevin Wolf53f76e52010-12-16 15:10:32 +01002463 error_set_progname(argv[0]);
2464
Paolo Bonzini2592c592012-11-03 18:10:17 +01002465 qemu_init_main_loop();
bellardea2384d2004-08-01 21:59:26 +00002466 bdrv_init();
2467 if (argc < 2)
2468 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002469 cmdname = argv[1];
aurel328f9b1572009-02-09 18:14:31 +00002470 argc--; argv++;
Stuart Brady153859b2009-06-07 00:42:17 +01002471
2472 /* find the command */
2473 for(cmd = img_cmds; cmd->name != NULL; cmd++) {
2474 if (!strcmp(cmdname, cmd->name)) {
2475 return cmd->handler(argc, argv);
2476 }
bellardea2384d2004-08-01 21:59:26 +00002477 }
Stuart Brady153859b2009-06-07 00:42:17 +01002478
2479 /* not found */
2480 help();
bellardea2384d2004-08-01 21:59:26 +00002481 return 0;
2482}