blob: 5d1e480fcb4605f0e3b4e1321b7a8968b1759dda [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"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080033#include "block/qapi.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020034#include <getopt.h>
aliguori9230eaf2009-03-28 17:55:19 +000035#include <stdio.h>
Miroslav Rezaninaf382d432013-02-13 09:09:40 +010036#include <stdarg.h>
bellardea2384d2004-08-01 21:59:26 +000037
bellarde8445332006-06-14 15:32:10 +000038#ifdef _WIN32
39#include <windows.h>
40#endif
41
Anthony Liguoric227f092009-10-01 16:12:16 -050042typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010043 const char *name;
44 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050045} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010046
Federico Simoncelli8599ea42013-01-28 06:59:47 -050047enum {
48 OPTION_OUTPUT = 256,
49 OPTION_BACKING_CHAIN = 257,
50};
51
52typedef enum OutputFormat {
53 OFORMAT_JSON,
54 OFORMAT_HUMAN,
55} OutputFormat;
56
aurel32137519c2008-11-30 19:12:49 +000057/* Default to cache=writeback as data integrity is not important for qemu-tcg. */
Stefan Hajnocziadfe0782010-04-13 10:29:35 +010058#define BDRV_O_FLAGS BDRV_O_CACHE_WB
Federico Simoncelli661a0f72011-06-20 12:48:19 -040059#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000060
bellardea2384d2004-08-01 21:59:26 +000061static void format_print(void *opaque, const char *name)
62{
63 printf(" %s", name);
64}
65
blueswir1d2c639d2009-01-24 18:19:25 +000066/* Please keep in synch with qemu-img.texi */
pbrook3f379ab2007-11-11 03:33:13 +000067static void help(void)
bellardea2384d2004-08-01 21:59:26 +000068{
Paolo Bonzinie00291c2010-02-04 16:49:56 +010069 const char *help_msg =
70 "qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2008 Fabrice Bellard\n"
malc3f020d72010-02-08 12:04:56 +030071 "usage: qemu-img command [command options]\n"
72 "QEMU disk image utility\n"
73 "\n"
74 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +010075#define DEF(option, callback, arg_string) \
76 " " arg_string "\n"
77#include "qemu-img-cmds.h"
78#undef DEF
79#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +030080 "\n"
81 "Command parameters:\n"
82 " 'filename' is a disk image filename\n"
83 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -040084 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +080085 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
86 " 'directsync' and 'unsafe' (default for convert)\n"
malc3f020d72010-02-08 12:04:56 +030087 " 'size' is the disk image size in bytes. Optional suffixes\n"
88 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M)\n"
89 " and T (terabyte, 1024G) are supported. 'b' is ignored.\n"
90 " 'output_filename' is the destination disk image filename\n"
91 " 'output_fmt' is the destination format\n"
92 " 'options' is a comma separated list of format specific options in a\n"
93 " name=value format. Use -o ? for an overview of the options supported by the\n"
94 " used format\n"
95 " '-c' indicates that target image must be compressed (qcow format only)\n"
96 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
97 " match exactly. The image doesn't need a working backing file before\n"
98 " rebasing in this case (useful for renaming the backing file)\n"
99 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200100 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100101 " '-q' use Quiet mode - do not print any output (except errors)\n"
Kevin Wolfa22f1232011-08-26 15:27:13 +0200102 " '-S' indicates the consecutive number of bytes that must contain only zeros\n"
103 " for qemu-img to create a sparse image during conversion\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200104 " '--output' takes the format in which the output must be done (human or json)\n"
malc3f020d72010-02-08 12:04:56 +0300105 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200106 "Parameters to check subcommand:\n"
107 " '-r' tries to repair any inconsistencies that are found during the check.\n"
108 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
109 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200110 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200111 "\n"
malc3f020d72010-02-08 12:04:56 +0300112 "Parameters to snapshot subcommand:\n"
113 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
114 " '-a' applies a snapshot (revert disk to saved state)\n"
115 " '-c' creates a snapshot\n"
116 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100117 " '-l' lists all snapshots in the given image\n"
118 "\n"
119 "Parameters to compare subcommand:\n"
120 " '-f' first image format\n"
121 " '-F' second image format\n"
122 " '-s' run in Strict mode - fail on different image size or sector allocation\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100123
124 printf("%s\nSupported formats:", help_msg);
bellardea2384d2004-08-01 21:59:26 +0000125 bdrv_iterate_format(format_print, NULL);
126 printf("\n");
127 exit(1);
128}
129
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100130static int qprintf(bool quiet, const char *fmt, ...)
131{
132 int ret = 0;
133 if (!quiet) {
134 va_list args;
135 va_start(args, fmt);
136 ret = vprintf(fmt, args);
137 va_end(args);
138 }
139 return ret;
140}
141
bellardea2384d2004-08-01 21:59:26 +0000142#if defined(WIN32)
143/* XXX: put correct support for win32 */
144static int read_password(char *buf, int buf_size)
145{
146 int c, i;
147 printf("Password: ");
148 fflush(stdout);
149 i = 0;
150 for(;;) {
151 c = getchar();
152 if (c == '\n')
153 break;
154 if (i < (buf_size - 1))
155 buf[i++] = c;
156 }
157 buf[i] = '\0';
158 return 0;
159}
160
161#else
162
163#include <termios.h>
164
165static struct termios oldtty;
166
167static void term_exit(void)
168{
169 tcsetattr (0, TCSANOW, &oldtty);
170}
171
172static void term_init(void)
173{
174 struct termios tty;
175
176 tcgetattr (0, &tty);
177 oldtty = tty;
178
179 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
180 |INLCR|IGNCR|ICRNL|IXON);
181 tty.c_oflag |= OPOST;
182 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
183 tty.c_cflag &= ~(CSIZE|PARENB);
184 tty.c_cflag |= CS8;
185 tty.c_cc[VMIN] = 1;
186 tty.c_cc[VTIME] = 0;
ths3b46e622007-09-17 08:09:54 +0000187
bellardea2384d2004-08-01 21:59:26 +0000188 tcsetattr (0, TCSANOW, &tty);
189
190 atexit(term_exit);
191}
192
pbrook3f379ab2007-11-11 03:33:13 +0000193static int read_password(char *buf, int buf_size)
bellardea2384d2004-08-01 21:59:26 +0000194{
195 uint8_t ch;
196 int i, ret;
197
198 printf("password: ");
199 fflush(stdout);
200 term_init();
201 i = 0;
202 for(;;) {
203 ret = read(0, &ch, 1);
204 if (ret == -1) {
205 if (errno == EAGAIN || errno == EINTR) {
206 continue;
207 } else {
208 ret = -1;
209 break;
210 }
211 } else if (ret == 0) {
212 ret = -1;
213 break;
214 } else {
215 if (ch == '\r') {
216 ret = 0;
217 break;
218 }
219 if (i < (buf_size - 1))
220 buf[i++] = ch;
221 }
222 }
223 term_exit();
224 buf[i] = '\0';
225 printf("\n");
226 return ret;
227}
228#endif
229
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100230static int print_block_option_help(const char *filename, const char *fmt)
231{
232 BlockDriver *drv, *proto_drv;
233 QEMUOptionParameter *create_options = NULL;
234
235 /* Find driver and parse its options */
236 drv = bdrv_find_format(fmt);
237 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100238 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100239 return 1;
240 }
241
242 proto_drv = bdrv_find_protocol(filename);
243 if (!proto_drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100244 error_report("Unknown protocol '%s'", filename);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100245 return 1;
246 }
247
248 create_options = append_option_parameters(create_options,
249 drv->create_options);
250 create_options = append_option_parameters(create_options,
251 proto_drv->create_options);
252 print_option_help(create_options);
253 free_option_parameters(create_options);
254 return 0;
255}
256
bellard75c23802004-08-27 21:28:58 +0000257static BlockDriverState *bdrv_new_open(const char *filename,
Sheng Yang9bc378c2010-01-29 10:15:06 +0800258 const char *fmt,
Daniel P. Berrangef0536bb2012-09-10 12:11:31 +0100259 int flags,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100260 bool require_io,
261 bool quiet)
bellard75c23802004-08-27 21:28:58 +0000262{
263 BlockDriverState *bs;
264 BlockDriver *drv;
265 char password[256];
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100266 int ret;
bellard75c23802004-08-27 21:28:58 +0000267
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100268 bs = bdrv_new("image");
Kevin Wolfad717132010-12-16 15:37:41 +0100269
bellard75c23802004-08-27 21:28:58 +0000270 if (fmt) {
271 drv = bdrv_find_format(fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900272 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100273 error_report("Unknown file format '%s'", fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900274 goto fail;
275 }
bellard75c23802004-08-27 21:28:58 +0000276 } else {
277 drv = NULL;
278 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100279
Kevin Wolfde9c0ce2013-03-15 10:35:02 +0100280 ret = bdrv_open(bs, filename, NULL, flags, drv);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100281 if (ret < 0) {
282 error_report("Could not open '%s': %s", filename, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900283 goto fail;
bellard75c23802004-08-27 21:28:58 +0000284 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100285
Daniel P. Berrangef0536bb2012-09-10 12:11:31 +0100286 if (bdrv_is_encrypted(bs) && require_io) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100287 qprintf(quiet, "Disk image '%s' is encrypted.\n", filename);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900288 if (read_password(password, sizeof(password)) < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100289 error_report("No password given");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900290 goto fail;
291 }
292 if (bdrv_set_key(bs, password) < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100293 error_report("invalid password");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900294 goto fail;
295 }
bellard75c23802004-08-27 21:28:58 +0000296 }
297 return bs;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900298fail:
299 if (bs) {
300 bdrv_delete(bs);
301 }
302 return NULL;
bellard75c23802004-08-27 21:28:58 +0000303}
304
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900305static int add_old_style_options(const char *fmt, QEMUOptionParameter *list,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100306 const char *base_filename,
307 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200308{
Kevin Wolfefa84d42009-05-18 16:42:12 +0200309 if (base_filename) {
310 if (set_option_parameter(list, BLOCK_OPT_BACKING_FILE, base_filename)) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100311 error_report("Backing file not supported for file format '%s'",
312 fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900313 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200314 }
315 }
316 if (base_fmt) {
317 if (set_option_parameter(list, BLOCK_OPT_BACKING_FMT, base_fmt)) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100318 error_report("Backing file format not supported for file "
319 "format '%s'", fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900320 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200321 }
322 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900323 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200324}
325
bellardea2384d2004-08-01 21:59:26 +0000326static int img_create(int argc, char **argv)
327{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200328 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100329 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000330 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000331 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000332 const char *filename;
333 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200334 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200335 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100336 bool quiet = false;
ths3b46e622007-09-17 08:09:54 +0000337
bellardea2384d2004-08-01 21:59:26 +0000338 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100339 c = getopt(argc, argv, "F:b:f:he6o:q");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100340 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000341 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100342 }
bellardea2384d2004-08-01 21:59:26 +0000343 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100344 case '?':
bellardea2384d2004-08-01 21:59:26 +0000345 case 'h':
346 help();
347 break;
aliguori9230eaf2009-03-28 17:55:19 +0000348 case 'F':
349 base_fmt = optarg;
350 break;
bellardea2384d2004-08-01 21:59:26 +0000351 case 'b':
352 base_filename = optarg;
353 break;
354 case 'f':
355 fmt = optarg;
356 break;
357 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200358 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100359 "encryption\' instead!");
360 return 1;
thsd8871c52007-10-24 16:11:42 +0000361 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200362 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100363 "compat6\' instead!");
364 return 1;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200365 case 'o':
366 options = optarg;
367 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100368 case 'q':
369 quiet = true;
370 break;
bellardea2384d2004-08-01 21:59:26 +0000371 }
372 }
aliguori9230eaf2009-03-28 17:55:19 +0000373
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900374 /* Get the filename */
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100375 if (optind >= argc) {
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900376 help();
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100377 }
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900378 filename = argv[optind++];
379
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100380 /* Get image size, if specified */
381 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100382 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +0100383 char *end;
384 sval = strtosz_suffix(argv[optind++], &end, STRTOSZ_DEFSUFFIX_B);
385 if (sval < 0 || *end) {
liguang79443392012-12-17 09:49:23 +0800386 if (sval == -ERANGE) {
387 error_report("Image size must be less than 8 EiB!");
388 } else {
389 error_report("Invalid image size specified! You may use k, M, "
390 "G or T suffixes for ");
391 error_report("kilobytes, megabytes, gigabytes and terabytes.");
392 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200393 return 1;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100394 }
395 img_size = (uint64_t)sval;
396 }
397
Peter Maydellc8057f92012-08-02 13:45:54 +0100398 if (options && is_help_option(options)) {
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200399 return print_block_option_help(filename, fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100400 }
401
Luiz Capitulino9b375252012-11-30 10:52:05 -0200402 bdrv_img_create(filename, fmt, base_filename, base_fmt,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100403 options, img_size, BDRV_O_FLAGS, &local_err, quiet);
Luiz Capitulino9b375252012-11-30 10:52:05 -0200404 if (error_is_set(&local_err)) {
405 error_report("%s", error_get_pretty(local_err));
406 error_free(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900407 return 1;
408 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200409
bellardea2384d2004-08-01 21:59:26 +0000410 return 0;
411}
412
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100413static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500414{
415 Error *errp = NULL;
416 QString *str;
417 QmpOutputVisitor *ov = qmp_output_visitor_new();
418 QObject *obj;
419 visit_type_ImageCheck(qmp_output_get_visitor(ov),
420 &check, NULL, &errp);
421 obj = qmp_output_get_qobject(ov);
422 str = qobject_to_json_pretty(obj);
423 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100424 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500425 qobject_decref(obj);
426 qmp_output_visitor_cleanup(ov);
427 QDECREF(str);
428}
429
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100430static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500431{
432 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100433 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500434 } else {
435 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100436 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
437 "Data may be corrupted, or further writes to the image "
438 "may corrupt it.\n",
439 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500440 }
441
442 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100443 qprintf(quiet,
444 "\n%" PRId64 " leaked clusters were found on the image.\n"
445 "This means waste of disk space, but no harm to data.\n",
446 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500447 }
448
449 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100450 qprintf(quiet,
451 "\n%" PRId64
452 " internal errors have occurred during the check.\n",
453 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500454 }
455 }
456
457 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100458 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
459 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
460 check->allocated_clusters, check->total_clusters,
461 check->allocated_clusters * 100.0 / check->total_clusters,
462 check->fragmented_clusters * 100.0 / check->allocated_clusters,
463 check->compressed_clusters * 100.0 /
464 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500465 }
466
467 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100468 qprintf(quiet,
469 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500470 }
471}
472
473static int collect_image_check(BlockDriverState *bs,
474 ImageCheck *check,
475 const char *filename,
476 const char *fmt,
477 int fix)
478{
479 int ret;
480 BdrvCheckResult result;
481
482 ret = bdrv_check(bs, &result, fix);
483 if (ret < 0) {
484 return ret;
485 }
486
487 check->filename = g_strdup(filename);
488 check->format = g_strdup(bdrv_get_format_name(bs));
489 check->check_errors = result.check_errors;
490 check->corruptions = result.corruptions;
491 check->has_corruptions = result.corruptions != 0;
492 check->leaks = result.leaks;
493 check->has_leaks = result.leaks != 0;
494 check->corruptions_fixed = result.corruptions_fixed;
495 check->has_corruptions_fixed = result.corruptions != 0;
496 check->leaks_fixed = result.leaks_fixed;
497 check->has_leaks_fixed = result.leaks != 0;
498 check->image_end_offset = result.image_end_offset;
499 check->has_image_end_offset = result.image_end_offset != 0;
500 check->total_clusters = result.bfi.total_clusters;
501 check->has_total_clusters = result.bfi.total_clusters != 0;
502 check->allocated_clusters = result.bfi.allocated_clusters;
503 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
504 check->fragmented_clusters = result.bfi.fragmented_clusters;
505 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100506 check->compressed_clusters = result.bfi.compressed_clusters;
507 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500508
509 return 0;
510}
511
Kevin Wolfe076f332010-06-29 11:43:13 +0200512/*
513 * Checks an image for consistency. Exit codes:
514 *
515 * 0 - Check completed, image is good
516 * 1 - Check not completed because of internal errors
517 * 2 - Check completed, image is corrupted
518 * 3 - Check completed, image has leaked clusters, but is good otherwise
519 */
aliguori15859692009-04-21 23:11:53 +0000520static int img_check(int argc, char **argv)
521{
522 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500523 OutputFormat output_format = OFORMAT_HUMAN;
524 const char *filename, *fmt, *output;
aliguori15859692009-04-21 23:11:53 +0000525 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200526 int fix = 0;
Stefan Hajnoczi058f8f12012-08-09 13:05:56 +0100527 int flags = BDRV_O_FLAGS | BDRV_O_CHECK;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500528 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100529 bool quiet = false;
aliguori15859692009-04-21 23:11:53 +0000530
531 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500532 output = NULL;
aliguori15859692009-04-21 23:11:53 +0000533 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500534 int option_index = 0;
535 static const struct option long_options[] = {
536 {"help", no_argument, 0, 'h'},
537 {"format", required_argument, 0, 'f'},
538 {"repair", no_argument, 0, 'r'},
539 {"output", required_argument, 0, OPTION_OUTPUT},
540 {0, 0, 0, 0}
541 };
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100542 c = getopt_long(argc, argv, "f:hr:q",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500543 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100544 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000545 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100546 }
aliguori15859692009-04-21 23:11:53 +0000547 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100548 case '?':
aliguori15859692009-04-21 23:11:53 +0000549 case 'h':
550 help();
551 break;
552 case 'f':
553 fmt = optarg;
554 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200555 case 'r':
556 flags |= BDRV_O_RDWR;
557
558 if (!strcmp(optarg, "leaks")) {
559 fix = BDRV_FIX_LEAKS;
560 } else if (!strcmp(optarg, "all")) {
561 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
562 } else {
563 help();
564 }
565 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500566 case OPTION_OUTPUT:
567 output = optarg;
568 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100569 case 'q':
570 quiet = true;
571 break;
aliguori15859692009-04-21 23:11:53 +0000572 }
573 }
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100574 if (optind >= argc) {
aliguori15859692009-04-21 23:11:53 +0000575 help();
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100576 }
aliguori15859692009-04-21 23:11:53 +0000577 filename = argv[optind++];
578
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500579 if (output && !strcmp(output, "json")) {
580 output_format = OFORMAT_JSON;
581 } else if (output && !strcmp(output, "human")) {
582 output_format = OFORMAT_HUMAN;
583 } else if (output) {
584 error_report("--output must be used with human or json as argument.");
585 return 1;
586 }
587
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100588 bs = bdrv_new_open(filename, fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900589 if (!bs) {
590 return 1;
591 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500592
593 check = g_new0(ImageCheck, 1);
594 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200595
596 if (ret == -ENOTSUP) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500597 if (output_format == OFORMAT_HUMAN) {
598 error_report("This image format does not support checks");
599 }
600 ret = 1;
601 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200602 }
603
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500604 if (check->corruptions_fixed || check->leaks_fixed) {
605 int corruptions_fixed, leaks_fixed;
606
607 leaks_fixed = check->leaks_fixed;
608 corruptions_fixed = check->corruptions_fixed;
609
610 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100611 qprintf(quiet,
612 "The following inconsistencies were found and repaired:\n\n"
613 " %" PRId64 " leaked clusters\n"
614 " %" PRId64 " corruptions\n\n"
615 "Double checking the fixed image now...\n",
616 check->leaks_fixed,
617 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500618 }
619
620 ret = collect_image_check(bs, check, filename, fmt, 0);
621
622 check->leaks_fixed = leaks_fixed;
623 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200624 }
625
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500626 switch (output_format) {
627 case OFORMAT_HUMAN:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100628 dump_human_image_check(check, quiet);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500629 break;
630 case OFORMAT_JSON:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100631 dump_json_image_check(check, quiet);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500632 break;
633 }
634
635 if (ret || check->check_errors) {
636 ret = 1;
637 goto fail;
638 }
639
640 if (check->corruptions) {
641 ret = 2;
642 } else if (check->leaks) {
643 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200644 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500645 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000646 }
647
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500648fail:
649 qapi_free_ImageCheck(check);
aliguori15859692009-04-21 23:11:53 +0000650 bdrv_delete(bs);
Kevin Wolfe076f332010-06-29 11:43:13 +0200651
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500652 return ret;
aliguori15859692009-04-21 23:11:53 +0000653}
654
bellardea2384d2004-08-01 21:59:26 +0000655static int img_commit(int argc, char **argv)
656{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400657 int c, ret, flags;
658 const char *filename, *fmt, *cache;
bellardea2384d2004-08-01 21:59:26 +0000659 BlockDriverState *bs;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100660 bool quiet = false;
bellardea2384d2004-08-01 21:59:26 +0000661
662 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400663 cache = BDRV_DEFAULT_CACHE;
bellardea2384d2004-08-01 21:59:26 +0000664 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100665 c = getopt(argc, argv, "f:ht:q");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100666 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000667 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100668 }
bellardea2384d2004-08-01 21:59:26 +0000669 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100670 case '?':
bellardea2384d2004-08-01 21:59:26 +0000671 case 'h':
672 help();
673 break;
674 case 'f':
675 fmt = optarg;
676 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400677 case 't':
678 cache = optarg;
679 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100680 case 'q':
681 quiet = true;
682 break;
bellardea2384d2004-08-01 21:59:26 +0000683 }
684 }
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100685 if (optind >= argc) {
bellardea2384d2004-08-01 21:59:26 +0000686 help();
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100687 }
bellardea2384d2004-08-01 21:59:26 +0000688 filename = argv[optind++];
689
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400690 flags = BDRV_O_RDWR;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100691 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400692 if (ret < 0) {
693 error_report("Invalid cache option: %s", cache);
694 return -1;
695 }
696
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100697 bs = bdrv_new_open(filename, fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900698 if (!bs) {
699 return 1;
700 }
bellardea2384d2004-08-01 21:59:26 +0000701 ret = bdrv_commit(bs);
702 switch(ret) {
703 case 0:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100704 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +0000705 break;
706 case -ENOENT:
Jes Sorensen15654a62010-12-16 14:31:53 +0100707 error_report("No disk inserted");
bellardea2384d2004-08-01 21:59:26 +0000708 break;
709 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +0100710 error_report("Image is read-only");
bellardea2384d2004-08-01 21:59:26 +0000711 break;
712 case -ENOTSUP:
Jes Sorensen15654a62010-12-16 14:31:53 +0100713 error_report("Image is already committed");
bellardea2384d2004-08-01 21:59:26 +0000714 break;
715 default:
Jes Sorensen15654a62010-12-16 14:31:53 +0100716 error_report("Error while committing image");
bellardea2384d2004-08-01 21:59:26 +0000717 break;
718 }
719
720 bdrv_delete(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900721 if (ret) {
722 return 1;
723 }
bellardea2384d2004-08-01 21:59:26 +0000724 return 0;
725}
726
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +0400727/*
thsf58c7b32008-06-05 21:53:49 +0000728 * Returns true iff the first sector pointed to by 'buf' contains at least
729 * a non-NUL byte.
730 *
731 * 'pnum' is set to the number of sectors (including and immediately following
732 * the first one) that are known to be in the same allocated/unallocated state.
733 */
bellardea2384d2004-08-01 21:59:26 +0000734static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
735{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000736 bool is_zero;
737 int i;
bellardea2384d2004-08-01 21:59:26 +0000738
739 if (n <= 0) {
740 *pnum = 0;
741 return 0;
742 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000743 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +0000744 for(i = 1; i < n; i++) {
745 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000746 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +0000747 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000748 }
bellardea2384d2004-08-01 21:59:26 +0000749 }
750 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000751 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +0000752}
753
Kevin Wolf3e85c6f2010-01-12 12:55:18 +0100754/*
Kevin Wolfa22f1232011-08-26 15:27:13 +0200755 * Like is_allocated_sectors, but if the buffer starts with a used sector,
756 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
757 * breaking up write requests for only small sparse areas.
758 */
759static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
760 int min)
761{
762 int ret;
763 int num_checked, num_used;
764
765 if (n < min) {
766 min = n;
767 }
768
769 ret = is_allocated_sectors(buf, n, pnum);
770 if (!ret) {
771 return ret;
772 }
773
774 num_used = *pnum;
775 buf += BDRV_SECTOR_SIZE * *pnum;
776 n -= *pnum;
777 num_checked = num_used;
778
779 while (n > 0) {
780 ret = is_allocated_sectors(buf, n, pnum);
781
782 buf += BDRV_SECTOR_SIZE * *pnum;
783 n -= *pnum;
784 num_checked += *pnum;
785 if (ret) {
786 num_used = num_checked;
787 } else if (*pnum >= min) {
788 break;
789 }
790 }
791
792 *pnum = num_used;
793 return 1;
794}
795
796/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +0100797 * Compares two buffers sector by sector. Returns 0 if the first sector of both
798 * buffers matches, non-zero otherwise.
799 *
800 * pnum is set to the number of sectors (including and immediately following
801 * the first one) that are known to have the same comparison result
802 */
803static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
804 int *pnum)
805{
806 int res, i;
807
808 if (n <= 0) {
809 *pnum = 0;
810 return 0;
811 }
812
813 res = !!memcmp(buf1, buf2, 512);
814 for(i = 1; i < n; i++) {
815 buf1 += 512;
816 buf2 += 512;
817
818 if (!!memcmp(buf1, buf2, 512) != res) {
819 break;
820 }
821 }
822
823 *pnum = i;
824 return res;
825}
826
Kevin Wolf80ee15a2009-09-15 12:30:43 +0200827#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +0000828
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100829static int64_t sectors_to_bytes(int64_t sectors)
830{
831 return sectors << BDRV_SECTOR_BITS;
832}
833
834static int64_t sectors_to_process(int64_t total, int64_t from)
835{
836 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
837}
838
839/*
840 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
841 *
842 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
843 * data and negative value on error.
844 *
845 * @param bs: Driver used for accessing file
846 * @param sect_num: Number of first sector to check
847 * @param sect_count: Number of sectors to check
848 * @param filename: Name of disk file we are checking (logging purpose)
849 * @param buffer: Allocated buffer for storing read data
850 * @param quiet: Flag for quiet mode
851 */
852static int check_empty_sectors(BlockDriverState *bs, int64_t sect_num,
853 int sect_count, const char *filename,
854 uint8_t *buffer, bool quiet)
855{
856 int pnum, ret = 0;
857 ret = bdrv_read(bs, sect_num, buffer, sect_count);
858 if (ret < 0) {
859 error_report("Error while reading offset %" PRId64 " of %s: %s",
860 sectors_to_bytes(sect_num), filename, strerror(-ret));
861 return ret;
862 }
863 ret = is_allocated_sectors(buffer, sect_count, &pnum);
864 if (ret || pnum != sect_count) {
865 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
866 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
867 return 1;
868 }
869
870 return 0;
871}
872
873/*
874 * Compares two images. Exit codes:
875 *
876 * 0 - Images are identical
877 * 1 - Images differ
878 * >1 - Error occurred
879 */
880static int img_compare(int argc, char **argv)
881{
882 const char *fmt1 = NULL, *fmt2 = NULL, *filename1, *filename2;
883 BlockDriverState *bs1, *bs2;
884 int64_t total_sectors1, total_sectors2;
885 uint8_t *buf1 = NULL, *buf2 = NULL;
886 int pnum1, pnum2;
887 int allocated1, allocated2;
888 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
889 bool progress = false, quiet = false, strict = false;
890 int64_t total_sectors;
891 int64_t sector_num = 0;
892 int64_t nb_sectors;
893 int c, pnum;
894 uint64_t bs_sectors;
895 uint64_t progress_base;
896
897 for (;;) {
898 c = getopt(argc, argv, "hpf:F:sq");
899 if (c == -1) {
900 break;
901 }
902 switch (c) {
903 case '?':
904 case 'h':
905 help();
906 break;
907 case 'f':
908 fmt1 = optarg;
909 break;
910 case 'F':
911 fmt2 = optarg;
912 break;
913 case 'p':
914 progress = true;
915 break;
916 case 'q':
917 quiet = true;
918 break;
919 case 's':
920 strict = true;
921 break;
922 }
923 }
924
925 /* Progress is not shown in Quiet mode */
926 if (quiet) {
927 progress = false;
928 }
929
930
931 if (optind > argc - 2) {
932 help();
933 }
934 filename1 = argv[optind++];
935 filename2 = argv[optind++];
936
937 /* Initialize before goto out */
938 qemu_progress_init(progress, 2.0);
939
940 bs1 = bdrv_new_open(filename1, fmt1, BDRV_O_FLAGS, true, quiet);
941 if (!bs1) {
942 error_report("Can't open file %s", filename1);
943 ret = 2;
944 goto out3;
945 }
946
947 bs2 = bdrv_new_open(filename2, fmt2, BDRV_O_FLAGS, true, quiet);
948 if (!bs2) {
949 error_report("Can't open file %s", filename2);
950 ret = 2;
951 goto out2;
952 }
953
954 buf1 = qemu_blockalign(bs1, IO_BUF_SIZE);
955 buf2 = qemu_blockalign(bs2, IO_BUF_SIZE);
956 bdrv_get_geometry(bs1, &bs_sectors);
957 total_sectors1 = bs_sectors;
958 bdrv_get_geometry(bs2, &bs_sectors);
959 total_sectors2 = bs_sectors;
960 total_sectors = MIN(total_sectors1, total_sectors2);
961 progress_base = MAX(total_sectors1, total_sectors2);
962
963 qemu_progress_print(0, 100);
964
965 if (strict && total_sectors1 != total_sectors2) {
966 ret = 1;
967 qprintf(quiet, "Strict mode: Image size mismatch!\n");
968 goto out;
969 }
970
971 for (;;) {
972 nb_sectors = sectors_to_process(total_sectors, sector_num);
973 if (nb_sectors <= 0) {
974 break;
975 }
976 allocated1 = bdrv_is_allocated_above(bs1, NULL, sector_num, nb_sectors,
977 &pnum1);
978 if (allocated1 < 0) {
979 ret = 3;
980 error_report("Sector allocation test failed for %s", filename1);
981 goto out;
982 }
983
984 allocated2 = bdrv_is_allocated_above(bs2, NULL, sector_num, nb_sectors,
985 &pnum2);
986 if (allocated2 < 0) {
987 ret = 3;
988 error_report("Sector allocation test failed for %s", filename2);
989 goto out;
990 }
991 nb_sectors = MIN(pnum1, pnum2);
992
993 if (allocated1 == allocated2) {
994 if (allocated1) {
995 ret = bdrv_read(bs1, sector_num, buf1, nb_sectors);
996 if (ret < 0) {
997 error_report("Error while reading offset %" PRId64 " of %s:"
998 " %s", sectors_to_bytes(sector_num), filename1,
999 strerror(-ret));
1000 ret = 4;
1001 goto out;
1002 }
1003 ret = bdrv_read(bs2, sector_num, buf2, nb_sectors);
1004 if (ret < 0) {
1005 error_report("Error while reading offset %" PRId64
1006 " of %s: %s", sectors_to_bytes(sector_num),
1007 filename2, strerror(-ret));
1008 ret = 4;
1009 goto out;
1010 }
1011 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1012 if (ret || pnum != nb_sectors) {
1013 ret = 1;
1014 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1015 sectors_to_bytes(
1016 ret ? sector_num : sector_num + pnum));
1017 goto out;
1018 }
1019 }
1020 } else {
1021 if (strict) {
1022 ret = 1;
1023 qprintf(quiet, "Strict mode: Offset %" PRId64
1024 " allocation mismatch!\n",
1025 sectors_to_bytes(sector_num));
1026 goto out;
1027 }
1028
1029 if (allocated1) {
1030 ret = check_empty_sectors(bs1, sector_num, nb_sectors,
1031 filename1, buf1, quiet);
1032 } else {
1033 ret = check_empty_sectors(bs2, sector_num, nb_sectors,
1034 filename2, buf1, quiet);
1035 }
1036 if (ret) {
1037 if (ret < 0) {
1038 ret = 4;
1039 error_report("Error while reading offset %" PRId64 ": %s",
1040 sectors_to_bytes(sector_num), strerror(-ret));
1041 }
1042 goto out;
1043 }
1044 }
1045 sector_num += nb_sectors;
1046 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1047 }
1048
1049 if (total_sectors1 != total_sectors2) {
1050 BlockDriverState *bs_over;
1051 int64_t total_sectors_over;
1052 const char *filename_over;
1053
1054 qprintf(quiet, "Warning: Image size mismatch!\n");
1055 if (total_sectors1 > total_sectors2) {
1056 total_sectors_over = total_sectors1;
1057 bs_over = bs1;
1058 filename_over = filename1;
1059 } else {
1060 total_sectors_over = total_sectors2;
1061 bs_over = bs2;
1062 filename_over = filename2;
1063 }
1064
1065 for (;;) {
1066 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1067 if (nb_sectors <= 0) {
1068 break;
1069 }
1070 ret = bdrv_is_allocated_above(bs_over, NULL, sector_num,
1071 nb_sectors, &pnum);
1072 if (ret < 0) {
1073 ret = 3;
1074 error_report("Sector allocation test failed for %s",
1075 filename_over);
1076 goto out;
1077
1078 }
1079 nb_sectors = pnum;
1080 if (ret) {
1081 ret = check_empty_sectors(bs_over, sector_num, nb_sectors,
1082 filename_over, buf1, quiet);
1083 if (ret) {
1084 if (ret < 0) {
1085 ret = 4;
1086 error_report("Error while reading offset %" PRId64
1087 " of %s: %s", sectors_to_bytes(sector_num),
1088 filename_over, strerror(-ret));
1089 }
1090 goto out;
1091 }
1092 }
1093 sector_num += nb_sectors;
1094 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1095 }
1096 }
1097
1098 qprintf(quiet, "Images are identical.\n");
1099 ret = 0;
1100
1101out:
1102 bdrv_delete(bs2);
1103 qemu_vfree(buf1);
1104 qemu_vfree(buf2);
1105out2:
1106 bdrv_delete(bs1);
1107out3:
1108 qemu_progress_end();
1109 return ret;
1110}
1111
bellardea2384d2004-08-01 21:59:26 +00001112static int img_convert(int argc, char **argv)
1113{
Jes Sorenseneec77d92010-12-07 17:44:34 +01001114 int c, ret = 0, n, n1, bs_n, bs_i, compress, cluster_size, cluster_sectors;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001115 int progress = 0, flags;
1116 const char *fmt, *out_fmt, *cache, *out_baseimg, *out_filename;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001117 BlockDriver *drv, *proto_drv;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001118 BlockDriverState **bs = NULL, *out_bs = NULL;
ths96b8f132007-12-17 01:35:20 +00001119 int64_t total_sectors, nb_sectors, sector_num, bs_offset;
1120 uint64_t bs_sectors;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001121 uint8_t * buf = NULL;
bellardea2384d2004-08-01 21:59:26 +00001122 const uint8_t *buf1;
bellardfaea38e2006-08-05 21:31:00 +00001123 BlockDriverInfo bdi;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001124 QEMUOptionParameter *param = NULL, *create_options = NULL;
Kevin Wolfa18953f2010-10-14 15:46:04 +02001125 QEMUOptionParameter *out_baseimg_param;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001126 char *options = NULL;
edison51ef6722010-09-21 19:58:41 -07001127 const char *snapshot_name = NULL;
Kevin Wolf1f710492012-10-12 14:29:18 +02001128 float local_progress = 0;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001129 int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001130 bool quiet = false;
bellardea2384d2004-08-01 21:59:26 +00001131
1132 fmt = NULL;
1133 out_fmt = "raw";
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001134 cache = "unsafe";
thsf58c7b32008-06-05 21:53:49 +00001135 out_baseimg = NULL;
Jes Sorenseneec77d92010-12-07 17:44:34 +01001136 compress = 0;
bellardea2384d2004-08-01 21:59:26 +00001137 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001138 c = getopt(argc, argv, "f:O:B:s:hce6o:pS:t:q");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001139 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001140 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001141 }
bellardea2384d2004-08-01 21:59:26 +00001142 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001143 case '?':
bellardea2384d2004-08-01 21:59:26 +00001144 case 'h':
1145 help();
1146 break;
1147 case 'f':
1148 fmt = optarg;
1149 break;
1150 case 'O':
1151 out_fmt = optarg;
1152 break;
thsf58c7b32008-06-05 21:53:49 +00001153 case 'B':
1154 out_baseimg = optarg;
1155 break;
bellardea2384d2004-08-01 21:59:26 +00001156 case 'c':
Jes Sorenseneec77d92010-12-07 17:44:34 +01001157 compress = 1;
bellardea2384d2004-08-01 21:59:26 +00001158 break;
1159 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001160 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001161 "encryption\' instead!");
1162 return 1;
thsec36ba12007-09-16 21:59:02 +00001163 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001164 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001165 "compat6\' instead!");
1166 return 1;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001167 case 'o':
1168 options = optarg;
1169 break;
edison51ef6722010-09-21 19:58:41 -07001170 case 's':
1171 snapshot_name = optarg;
1172 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001173 case 'S':
1174 {
1175 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +01001176 char *end;
1177 sval = strtosz_suffix(optarg, &end, STRTOSZ_DEFSUFFIX_B);
1178 if (sval < 0 || *end) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02001179 error_report("Invalid minimum zero buffer size for sparse output specified");
1180 return 1;
1181 }
1182
1183 min_sparse = sval / BDRV_SECTOR_SIZE;
1184 break;
1185 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001186 case 'p':
1187 progress = 1;
1188 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001189 case 't':
1190 cache = optarg;
1191 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001192 case 'q':
1193 quiet = true;
1194 break;
bellardea2384d2004-08-01 21:59:26 +00001195 }
1196 }
ths3b46e622007-09-17 08:09:54 +00001197
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001198 if (quiet) {
1199 progress = 0;
1200 }
1201
balrog926c2d22007-10-31 01:11:44 +00001202 bs_n = argc - optind - 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001203 if (bs_n < 1) {
1204 help();
1205 }
balrog926c2d22007-10-31 01:11:44 +00001206
1207 out_filename = argv[argc - 1];
thsf58c7b32008-06-05 21:53:49 +00001208
Charles Arnoldfa170c12012-05-11 10:57:54 -06001209 /* Initialize before goto out */
1210 qemu_progress_init(progress, 2.0);
1211
Peter Maydellc8057f92012-08-02 13:45:54 +01001212 if (options && is_help_option(options)) {
Jes Sorensen4ac8aac2010-12-06 15:25:38 +01001213 ret = print_block_option_help(out_filename, out_fmt);
1214 goto out;
1215 }
1216
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001217 if (bs_n > 1 && out_baseimg) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001218 error_report("-B makes no sense when concatenating multiple input "
1219 "images");
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001220 ret = -1;
1221 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001222 }
Dong Xu Wangf8111c22012-03-15 20:13:31 +08001223
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001224 qemu_progress_print(0, 100);
1225
Anthony Liguori7267c092011-08-20 22:09:37 -05001226 bs = g_malloc0(bs_n * sizeof(BlockDriverState *));
balrog926c2d22007-10-31 01:11:44 +00001227
1228 total_sectors = 0;
1229 for (bs_i = 0; bs_i < bs_n; bs_i++) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001230 bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt, BDRV_O_FLAGS, true,
1231 quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001232 if (!bs[bs_i]) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001233 error_report("Could not open '%s'", argv[optind + bs_i]);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001234 ret = -1;
1235 goto out;
1236 }
balrog926c2d22007-10-31 01:11:44 +00001237 bdrv_get_geometry(bs[bs_i], &bs_sectors);
1238 total_sectors += bs_sectors;
1239 }
bellardea2384d2004-08-01 21:59:26 +00001240
edison51ef6722010-09-21 19:58:41 -07001241 if (snapshot_name != NULL) {
1242 if (bs_n > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02001243 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07001244 ret = -1;
1245 goto out;
1246 }
1247 if (bdrv_snapshot_load_tmp(bs[0], snapshot_name) < 0) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02001248 error_report("Failed to load snapshot");
edison51ef6722010-09-21 19:58:41 -07001249 ret = -1;
1250 goto out;
1251 }
1252 }
1253
Kevin Wolfefa84d42009-05-18 16:42:12 +02001254 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +00001255 drv = bdrv_find_format(out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001256 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001257 error_report("Unknown file format '%s'", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001258 ret = -1;
1259 goto out;
1260 }
balrog926c2d22007-10-31 01:11:44 +00001261
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001262 proto_drv = bdrv_find_protocol(out_filename);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001263 if (!proto_drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001264 error_report("Unknown protocol '%s'", out_filename);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001265 ret = -1;
1266 goto out;
1267 }
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001268
1269 create_options = append_option_parameters(create_options,
1270 drv->create_options);
1271 create_options = append_option_parameters(create_options,
1272 proto_drv->create_options);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02001273
Kevin Wolfefa84d42009-05-18 16:42:12 +02001274 if (options) {
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001275 param = parse_option_parameters(options, create_options, param);
Kevin Wolfefa84d42009-05-18 16:42:12 +02001276 if (param == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001277 error_report("Invalid options for file format '%s'.", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001278 ret = -1;
1279 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001280 }
1281 } else {
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001282 param = parse_option_parameters("", create_options, param);
Kevin Wolfefa84d42009-05-18 16:42:12 +02001283 }
1284
1285 set_option_parameter_int(param, BLOCK_OPT_SIZE, total_sectors * 512);
Jes Sorenseneec77d92010-12-07 17:44:34 +01001286 ret = add_old_style_options(out_fmt, param, out_baseimg, NULL);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001287 if (ret < 0) {
1288 goto out;
1289 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001290
Kevin Wolfa18953f2010-10-14 15:46:04 +02001291 /* Get backing file name if -o backing_file was used */
1292 out_baseimg_param = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
1293 if (out_baseimg_param) {
1294 out_baseimg = out_baseimg_param->value.s;
1295 }
1296
Kevin Wolfefa84d42009-05-18 16:42:12 +02001297 /* Check if compression is supported */
Jes Sorenseneec77d92010-12-07 17:44:34 +01001298 if (compress) {
Kevin Wolfefa84d42009-05-18 16:42:12 +02001299 QEMUOptionParameter *encryption =
1300 get_option_parameter(param, BLOCK_OPT_ENCRYPT);
Kevin Wolf41521fa2011-10-18 16:19:42 +02001301 QEMUOptionParameter *preallocation =
1302 get_option_parameter(param, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02001303
1304 if (!drv->bdrv_write_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001305 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001306 ret = -1;
1307 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001308 }
1309
1310 if (encryption && encryption->value.n) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001311 error_report("Compression and encryption not supported at "
1312 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001313 ret = -1;
1314 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001315 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02001316
1317 if (preallocation && preallocation->value.s
1318 && strcmp(preallocation->value.s, "off"))
1319 {
1320 error_report("Compression and preallocation not supported at "
1321 "the same time");
1322 ret = -1;
1323 goto out;
1324 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001325 }
1326
1327 /* Create the new image */
1328 ret = bdrv_create(drv, out_filename, param);
bellardea2384d2004-08-01 21:59:26 +00001329 if (ret < 0) {
1330 if (ret == -ENOTSUP) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001331 error_report("Formatting not supported for file format '%s'",
1332 out_fmt);
aurel326e9ea0c2009-04-15 14:42:46 +00001333 } else if (ret == -EFBIG) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001334 error_report("The image size is too large for file format '%s'",
1335 out_fmt);
bellardea2384d2004-08-01 21:59:26 +00001336 } else {
Jes Sorensen15654a62010-12-16 14:31:53 +01001337 error_report("%s: error while converting %s: %s",
1338 out_filename, out_fmt, strerror(-ret));
bellardea2384d2004-08-01 21:59:26 +00001339 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001340 goto out;
bellardea2384d2004-08-01 21:59:26 +00001341 }
ths3b46e622007-09-17 08:09:54 +00001342
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001343 flags = BDRV_O_RDWR;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +01001344 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001345 if (ret < 0) {
1346 error_report("Invalid cache option: %s", cache);
1347 return -1;
1348 }
1349
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001350 out_bs = bdrv_new_open(out_filename, out_fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001351 if (!out_bs) {
1352 ret = -1;
1353 goto out;
1354 }
bellardea2384d2004-08-01 21:59:26 +00001355
balrog926c2d22007-10-31 01:11:44 +00001356 bs_i = 0;
1357 bs_offset = 0;
1358 bdrv_get_geometry(bs[0], &bs_sectors);
Kevin Wolfbb1c0592011-08-08 14:09:12 +02001359 buf = qemu_blockalign(out_bs, IO_BUF_SIZE);
balrog926c2d22007-10-31 01:11:44 +00001360
Jes Sorenseneec77d92010-12-07 17:44:34 +01001361 if (compress) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001362 ret = bdrv_get_info(out_bs, &bdi);
1363 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001364 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001365 goto out;
1366 }
bellardfaea38e2006-08-05 21:31:00 +00001367 cluster_size = bdi.cluster_size;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001368 if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001369 error_report("invalid cluster size");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001370 ret = -1;
1371 goto out;
1372 }
bellardea2384d2004-08-01 21:59:26 +00001373 cluster_sectors = cluster_size >> 9;
1374 sector_num = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001375
1376 nb_sectors = total_sectors;
Kevin Wolf1f710492012-10-12 14:29:18 +02001377 if (nb_sectors != 0) {
1378 local_progress = (float)100 /
1379 (nb_sectors / MIN(nb_sectors, cluster_sectors));
1380 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001381
bellardea2384d2004-08-01 21:59:26 +00001382 for(;;) {
balrog926c2d22007-10-31 01:11:44 +00001383 int64_t bs_num;
1384 int remainder;
1385 uint8_t *buf2;
1386
bellardea2384d2004-08-01 21:59:26 +00001387 nb_sectors = total_sectors - sector_num;
1388 if (nb_sectors <= 0)
1389 break;
1390 if (nb_sectors >= cluster_sectors)
1391 n = cluster_sectors;
1392 else
1393 n = nb_sectors;
balrog926c2d22007-10-31 01:11:44 +00001394
1395 bs_num = sector_num - bs_offset;
1396 assert (bs_num >= 0);
1397 remainder = n;
1398 buf2 = buf;
1399 while (remainder > 0) {
1400 int nlow;
1401 while (bs_num == bs_sectors) {
1402 bs_i++;
1403 assert (bs_i < bs_n);
1404 bs_offset += bs_sectors;
1405 bdrv_get_geometry(bs[bs_i], &bs_sectors);
1406 bs_num = 0;
Blue Swirl0bfcd592010-05-22 08:02:12 +00001407 /* printf("changing part: sector_num=%" PRId64 ", "
1408 "bs_i=%d, bs_offset=%" PRId64 ", bs_sectors=%" PRId64
1409 "\n", sector_num, bs_i, bs_offset, bs_sectors); */
balrog926c2d22007-10-31 01:11:44 +00001410 }
1411 assert (bs_num < bs_sectors);
1412
1413 nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder;
1414
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001415 ret = bdrv_read(bs[bs_i], bs_num, buf2, nlow);
1416 if (ret < 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001417 error_report("error while reading sector %" PRId64 ": %s",
1418 bs_num, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001419 goto out;
1420 }
balrog926c2d22007-10-31 01:11:44 +00001421
1422 buf2 += nlow * 512;
1423 bs_num += nlow;
1424
1425 remainder -= nlow;
1426 }
1427 assert (remainder == 0);
1428
Stefan Hajnoczi54f106d2013-04-15 17:17:33 +02001429 if (!buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)) {
1430 ret = bdrv_write_compressed(out_bs, sector_num, buf, n);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001431 if (ret != 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001432 error_report("error while compressing sector %" PRId64
1433 ": %s", sector_num, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001434 goto out;
1435 }
bellardea2384d2004-08-01 21:59:26 +00001436 }
1437 sector_num += n;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001438 qemu_progress_print(local_progress, 100);
bellardea2384d2004-08-01 21:59:26 +00001439 }
bellardfaea38e2006-08-05 21:31:00 +00001440 /* signal EOF to align */
1441 bdrv_write_compressed(out_bs, 0, NULL, 0);
bellardea2384d2004-08-01 21:59:26 +00001442 } else {
Kevin Wolff2feebb2010-04-14 17:30:35 +02001443 int has_zero_init = bdrv_has_zero_init(out_bs);
1444
thsf58c7b32008-06-05 21:53:49 +00001445 sector_num = 0; // total number of sectors converted so far
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001446 nb_sectors = total_sectors - sector_num;
Kevin Wolf1f710492012-10-12 14:29:18 +02001447 if (nb_sectors != 0) {
1448 local_progress = (float)100 /
1449 (nb_sectors / MIN(nb_sectors, IO_BUF_SIZE / 512));
1450 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001451
bellardea2384d2004-08-01 21:59:26 +00001452 for(;;) {
1453 nb_sectors = total_sectors - sector_num;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001454 if (nb_sectors <= 0) {
bellardea2384d2004-08-01 21:59:26 +00001455 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001456 }
1457 if (nb_sectors >= (IO_BUF_SIZE / 512)) {
bellardea2384d2004-08-01 21:59:26 +00001458 n = (IO_BUF_SIZE / 512);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001459 } else {
bellardea2384d2004-08-01 21:59:26 +00001460 n = nb_sectors;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001461 }
balrog926c2d22007-10-31 01:11:44 +00001462
1463 while (sector_num - bs_offset >= bs_sectors) {
1464 bs_i ++;
1465 assert (bs_i < bs_n);
1466 bs_offset += bs_sectors;
1467 bdrv_get_geometry(bs[bs_i], &bs_sectors);
Blue Swirl0bfcd592010-05-22 08:02:12 +00001468 /* printf("changing part: sector_num=%" PRId64 ", bs_i=%d, "
1469 "bs_offset=%" PRId64 ", bs_sectors=%" PRId64 "\n",
balrog926c2d22007-10-31 01:11:44 +00001470 sector_num, bs_i, bs_offset, bs_sectors); */
1471 }
1472
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001473 if (n > bs_offset + bs_sectors - sector_num) {
balrog926c2d22007-10-31 01:11:44 +00001474 n = bs_offset + bs_sectors - sector_num;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001475 }
balrog926c2d22007-10-31 01:11:44 +00001476
Kevin Wolff2feebb2010-04-14 17:30:35 +02001477 if (has_zero_init) {
Akkarit Sangpetchd0320442009-07-17 10:02:15 +02001478 /* If the output image is being created as a copy on write image,
1479 assume that sectors which are unallocated in the input image
1480 are present in both the output's and input's base images (no
1481 need to copy them). */
1482 if (out_baseimg) {
1483 if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset,
1484 n, &n1)) {
1485 sector_num += n1;
1486 continue;
1487 }
1488 /* The next 'n1' sectors are allocated in the input image. Copy
1489 only those as they may be followed by unallocated sectors. */
1490 n = n1;
aliguori93c65b42009-04-05 17:40:43 +00001491 }
aliguori93c65b42009-04-05 17:40:43 +00001492 } else {
1493 n1 = n;
thsf58c7b32008-06-05 21:53:49 +00001494 }
1495
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001496 ret = bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n);
1497 if (ret < 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001498 error_report("error while reading sector %" PRId64 ": %s",
1499 sector_num - bs_offset, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001500 goto out;
1501 }
bellardea2384d2004-08-01 21:59:26 +00001502 /* NOTE: at the same time we convert, we do not write zero
1503 sectors to have a chance to compress the image. Ideally, we
1504 should add a specific call to have the info to go faster */
1505 buf1 = buf;
1506 while (n > 0) {
thsf58c7b32008-06-05 21:53:49 +00001507 /* If the output image is being created as a copy on write image,
1508 copy all sectors even the ones containing only NUL bytes,
aliguori93c65b42009-04-05 17:40:43 +00001509 because they may differ from the sectors in the base image.
1510
1511 If the output is to a host device, we also write out
1512 sectors that are entirely 0, since whatever data was
1513 already there is garbage, not 0s. */
Kevin Wolff2feebb2010-04-14 17:30:35 +02001514 if (!has_zero_init || out_baseimg ||
Kevin Wolfa22f1232011-08-26 15:27:13 +02001515 is_allocated_sectors_min(buf1, n, &n1, min_sparse)) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001516 ret = bdrv_write(out_bs, sector_num, buf1, n1);
1517 if (ret < 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001518 error_report("error while writing sector %" PRId64
1519 ": %s", sector_num, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001520 goto out;
1521 }
bellardea2384d2004-08-01 21:59:26 +00001522 }
1523 sector_num += n1;
1524 n -= n1;
1525 buf1 += n1 * 512;
1526 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001527 qemu_progress_print(local_progress, 100);
bellardea2384d2004-08-01 21:59:26 +00001528 }
1529 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001530out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001531 qemu_progress_end();
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001532 free_option_parameters(create_options);
1533 free_option_parameters(param);
Kevin Wolfbb1c0592011-08-08 14:09:12 +02001534 qemu_vfree(buf);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001535 if (out_bs) {
1536 bdrv_delete(out_bs);
1537 }
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001538 if (bs) {
1539 for (bs_i = 0; bs_i < bs_n; bs_i++) {
1540 if (bs[bs_i]) {
1541 bdrv_delete(bs[bs_i]);
1542 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001543 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001544 g_free(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001545 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001546 if (ret) {
1547 return 1;
1548 }
bellardea2384d2004-08-01 21:59:26 +00001549 return 0;
1550}
1551
bellard57d1a2b2004-08-03 21:15:11 +00001552
bellardfaea38e2006-08-05 21:31:00 +00001553static void dump_snapshots(BlockDriverState *bs)
1554{
1555 QEMUSnapshotInfo *sn_tab, *sn;
1556 int nb_sns, i;
1557 char buf[256];
1558
1559 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1560 if (nb_sns <= 0)
1561 return;
1562 printf("Snapshot list:\n");
1563 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1564 for(i = 0; i < nb_sns; i++) {
1565 sn = &sn_tab[i];
1566 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
1567 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001568 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00001569}
1570
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001571static void dump_json_image_info_list(ImageInfoList *list)
1572{
1573 Error *errp = NULL;
1574 QString *str;
1575 QmpOutputVisitor *ov = qmp_output_visitor_new();
1576 QObject *obj;
1577 visit_type_ImageInfoList(qmp_output_get_visitor(ov),
1578 &list, NULL, &errp);
1579 obj = qmp_output_get_qobject(ov);
1580 str = qobject_to_json_pretty(obj);
1581 assert(str != NULL);
1582 printf("%s\n", qstring_get_str(str));
1583 qobject_decref(obj);
1584 qmp_output_visitor_cleanup(ov);
1585 QDECREF(str);
1586}
1587
Benoît Canetc054b3f2012-09-05 13:09:02 +02001588static void dump_json_image_info(ImageInfo *info)
1589{
1590 Error *errp = NULL;
1591 QString *str;
1592 QmpOutputVisitor *ov = qmp_output_visitor_new();
1593 QObject *obj;
1594 visit_type_ImageInfo(qmp_output_get_visitor(ov),
1595 &info, NULL, &errp);
1596 obj = qmp_output_get_qobject(ov);
1597 str = qobject_to_json_pretty(obj);
1598 assert(str != NULL);
1599 printf("%s\n", qstring_get_str(str));
1600 qobject_decref(obj);
1601 qmp_output_visitor_cleanup(ov);
1602 QDECREF(str);
1603}
1604
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001605static void dump_human_image_info_list(ImageInfoList *list)
1606{
1607 ImageInfoList *elem;
1608 bool delim = false;
1609
1610 for (elem = list; elem; elem = elem->next) {
1611 if (delim) {
1612 printf("\n");
1613 }
1614 delim = true;
1615
Wenchao Xiaf364ec62013-05-25 11:09:44 +08001616 bdrv_image_info_dump(elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001617 }
1618}
1619
1620static gboolean str_equal_func(gconstpointer a, gconstpointer b)
1621{
1622 return strcmp(a, b) == 0;
1623}
1624
1625/**
1626 * Open an image file chain and return an ImageInfoList
1627 *
1628 * @filename: topmost image filename
1629 * @fmt: topmost image format (may be NULL to autodetect)
1630 * @chain: true - enumerate entire backing file chain
1631 * false - only topmost image file
1632 *
1633 * Returns a list of ImageInfo objects or NULL if there was an error opening an
1634 * image file. If there was an error a message will have been printed to
1635 * stderr.
1636 */
1637static ImageInfoList *collect_image_info_list(const char *filename,
1638 const char *fmt,
1639 bool chain)
1640{
1641 ImageInfoList *head = NULL;
1642 ImageInfoList **last = &head;
1643 GHashTable *filenames;
1644
1645 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
1646
1647 while (filename) {
1648 BlockDriverState *bs;
1649 ImageInfo *info;
1650 ImageInfoList *elem;
1651
1652 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
1653 error_report("Backing file '%s' creates an infinite loop.",
1654 filename);
1655 goto err;
1656 }
1657 g_hash_table_insert(filenames, (gpointer)filename, NULL);
1658
1659 bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_NO_BACKING,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001660 false, false);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001661 if (!bs) {
1662 goto err;
1663 }
1664
1665 info = g_new0(ImageInfo, 1);
Wenchao Xiaf364ec62013-05-25 11:09:44 +08001666 bdrv_collect_image_info(bs, info, filename);
1667 bdrv_collect_snapshots(bs, info);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001668
1669 elem = g_new0(ImageInfoList, 1);
1670 elem->value = info;
1671 *last = elem;
1672 last = &elem->next;
1673
1674 bdrv_delete(bs);
1675
1676 filename = fmt = NULL;
1677 if (chain) {
1678 if (info->has_full_backing_filename) {
1679 filename = info->full_backing_filename;
1680 } else if (info->has_backing_filename) {
1681 filename = info->backing_filename;
1682 }
1683 if (info->has_backing_filename_format) {
1684 fmt = info->backing_filename_format;
1685 }
1686 }
1687 }
1688 g_hash_table_destroy(filenames);
1689 return head;
1690
1691err:
1692 qapi_free_ImageInfoList(head);
1693 g_hash_table_destroy(filenames);
1694 return NULL;
1695}
1696
Benoît Canetc054b3f2012-09-05 13:09:02 +02001697static int img_info(int argc, char **argv)
1698{
1699 int c;
1700 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001701 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001702 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001703 ImageInfoList *list;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001704
bellardea2384d2004-08-01 21:59:26 +00001705 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001706 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00001707 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02001708 int option_index = 0;
1709 static const struct option long_options[] = {
1710 {"help", no_argument, 0, 'h'},
1711 {"format", required_argument, 0, 'f'},
1712 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001713 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Benoît Canetc054b3f2012-09-05 13:09:02 +02001714 {0, 0, 0, 0}
1715 };
1716 c = getopt_long(argc, argv, "f:h",
1717 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001718 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001719 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001720 }
bellardea2384d2004-08-01 21:59:26 +00001721 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001722 case '?':
bellardea2384d2004-08-01 21:59:26 +00001723 case 'h':
1724 help();
1725 break;
1726 case 'f':
1727 fmt = optarg;
1728 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001729 case OPTION_OUTPUT:
1730 output = optarg;
1731 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001732 case OPTION_BACKING_CHAIN:
1733 chain = true;
1734 break;
bellardea2384d2004-08-01 21:59:26 +00001735 }
1736 }
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001737 if (optind >= argc) {
bellardea2384d2004-08-01 21:59:26 +00001738 help();
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001739 }
bellardea2384d2004-08-01 21:59:26 +00001740 filename = argv[optind++];
1741
Benoît Canetc054b3f2012-09-05 13:09:02 +02001742 if (output && !strcmp(output, "json")) {
1743 output_format = OFORMAT_JSON;
1744 } else if (output && !strcmp(output, "human")) {
1745 output_format = OFORMAT_HUMAN;
1746 } else if (output) {
1747 error_report("--output must be used with human or json as argument.");
1748 return 1;
1749 }
1750
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001751 list = collect_image_info_list(filename, fmt, chain);
1752 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001753 return 1;
1754 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001755
Benoît Canetc054b3f2012-09-05 13:09:02 +02001756 switch (output_format) {
1757 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001758 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02001759 break;
1760 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001761 if (chain) {
1762 dump_json_image_info_list(list);
1763 } else {
1764 dump_json_image_info(list->value);
1765 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001766 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001767 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001768
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001769 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00001770 return 0;
1771}
1772
aliguorif7b4a942009-01-07 17:40:15 +00001773#define SNAPSHOT_LIST 1
1774#define SNAPSHOT_CREATE 2
1775#define SNAPSHOT_APPLY 3
1776#define SNAPSHOT_DELETE 4
1777
Stuart Brady153859b2009-06-07 00:42:17 +01001778static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00001779{
1780 BlockDriverState *bs;
1781 QEMUSnapshotInfo sn;
1782 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001783 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00001784 int action = 0;
1785 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001786 bool quiet = false;
aliguorif7b4a942009-01-07 17:40:15 +00001787
Kevin Wolf710da702011-01-10 12:33:02 +01001788 bdrv_oflags = BDRV_O_FLAGS | BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00001789 /* Parse commandline parameters */
1790 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001791 c = getopt(argc, argv, "la:c:d:hq");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001792 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00001793 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001794 }
aliguorif7b4a942009-01-07 17:40:15 +00001795 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001796 case '?':
aliguorif7b4a942009-01-07 17:40:15 +00001797 case 'h':
1798 help();
Stuart Brady153859b2009-06-07 00:42:17 +01001799 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00001800 case 'l':
1801 if (action) {
1802 help();
Stuart Brady153859b2009-06-07 00:42:17 +01001803 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00001804 }
1805 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02001806 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00001807 break;
1808 case 'a':
1809 if (action) {
1810 help();
Stuart Brady153859b2009-06-07 00:42:17 +01001811 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00001812 }
1813 action = SNAPSHOT_APPLY;
1814 snapshot_name = optarg;
1815 break;
1816 case 'c':
1817 if (action) {
1818 help();
Stuart Brady153859b2009-06-07 00:42:17 +01001819 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00001820 }
1821 action = SNAPSHOT_CREATE;
1822 snapshot_name = optarg;
1823 break;
1824 case 'd':
1825 if (action) {
1826 help();
Stuart Brady153859b2009-06-07 00:42:17 +01001827 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00001828 }
1829 action = SNAPSHOT_DELETE;
1830 snapshot_name = optarg;
1831 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001832 case 'q':
1833 quiet = true;
1834 break;
aliguorif7b4a942009-01-07 17:40:15 +00001835 }
1836 }
1837
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001838 if (optind >= argc) {
aliguorif7b4a942009-01-07 17:40:15 +00001839 help();
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001840 }
aliguorif7b4a942009-01-07 17:40:15 +00001841 filename = argv[optind++];
1842
1843 /* Open the image */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001844 bs = bdrv_new_open(filename, NULL, bdrv_oflags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001845 if (!bs) {
1846 return 1;
1847 }
aliguorif7b4a942009-01-07 17:40:15 +00001848
1849 /* Perform the requested action */
1850 switch(action) {
1851 case SNAPSHOT_LIST:
1852 dump_snapshots(bs);
1853 break;
1854
1855 case SNAPSHOT_CREATE:
1856 memset(&sn, 0, sizeof(sn));
1857 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
1858
1859 qemu_gettimeofday(&tv);
1860 sn.date_sec = tv.tv_sec;
1861 sn.date_nsec = tv.tv_usec * 1000;
1862
1863 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001864 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001865 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00001866 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001867 }
aliguorif7b4a942009-01-07 17:40:15 +00001868 break;
1869
1870 case SNAPSHOT_APPLY:
1871 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001872 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001873 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00001874 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001875 }
aliguorif7b4a942009-01-07 17:40:15 +00001876 break;
1877
1878 case SNAPSHOT_DELETE:
1879 ret = bdrv_snapshot_delete(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001880 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001881 error_report("Could not delete snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00001882 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001883 }
aliguorif7b4a942009-01-07 17:40:15 +00001884 break;
1885 }
1886
1887 /* Cleanup */
1888 bdrv_delete(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001889 if (ret) {
1890 return 1;
1891 }
Stuart Brady153859b2009-06-07 00:42:17 +01001892 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00001893}
1894
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001895static int img_rebase(int argc, char **argv)
1896{
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001897 BlockDriverState *bs, *bs_old_backing = NULL, *bs_new_backing = NULL;
Stefan Hajnoczif163d072010-04-13 10:29:34 +01001898 BlockDriver *old_backing_drv, *new_backing_drv;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001899 char *filename;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001900 const char *fmt, *cache, *out_basefmt, *out_baseimg;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001901 int c, flags, ret;
1902 int unsafe = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001903 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001904 bool quiet = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001905
1906 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01001907 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001908 cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001909 out_baseimg = NULL;
1910 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001911 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001912 c = getopt(argc, argv, "uhf:F:b:pt:q");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001913 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001914 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001915 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001916 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001917 case '?':
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001918 case 'h':
1919 help();
1920 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01001921 case 'f':
1922 fmt = optarg;
1923 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001924 case 'F':
1925 out_basefmt = optarg;
1926 break;
1927 case 'b':
1928 out_baseimg = optarg;
1929 break;
1930 case 'u':
1931 unsafe = 1;
1932 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001933 case 'p':
1934 progress = 1;
1935 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001936 case 't':
1937 cache = optarg;
1938 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001939 case 'q':
1940 quiet = true;
1941 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001942 }
1943 }
1944
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001945 if (quiet) {
1946 progress = 0;
1947 }
1948
Anthony Liguori9a9d9db2011-04-13 15:51:47 +01001949 if ((optind >= argc) || (!unsafe && !out_baseimg)) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001950 help();
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001951 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001952 filename = argv[optind++];
1953
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001954 qemu_progress_init(progress, 2.0);
1955 qemu_progress_print(0, 100);
1956
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001957 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +01001958 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001959 if (ret < 0) {
1960 error_report("Invalid cache option: %s", cache);
1961 return -1;
1962 }
1963
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001964 /*
1965 * Open the images.
1966 *
1967 * Ignore the old backing file for unsafe rebase in case we want to correct
1968 * the reference to a renamed or moved backing file.
1969 */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001970 bs = bdrv_new_open(filename, fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001971 if (!bs) {
1972 return 1;
1973 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001974
1975 /* Find the right drivers for the backing files */
1976 old_backing_drv = NULL;
1977 new_backing_drv = NULL;
1978
1979 if (!unsafe && bs->backing_format[0] != '\0') {
1980 old_backing_drv = bdrv_find_format(bs->backing_format);
1981 if (old_backing_drv == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001982 error_report("Invalid format name: '%s'", bs->backing_format);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001983 ret = -1;
1984 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001985 }
1986 }
1987
1988 if (out_basefmt != NULL) {
1989 new_backing_drv = bdrv_find_format(out_basefmt);
1990 if (new_backing_drv == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001991 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001992 ret = -1;
1993 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001994 }
1995 }
1996
1997 /* For safe rebasing we need to compare old and new backing file */
1998 if (unsafe) {
1999 /* Make the compiler happy */
2000 bs_old_backing = NULL;
2001 bs_new_backing = NULL;
2002 } else {
2003 char backing_name[1024];
2004
2005 bs_old_backing = bdrv_new("old_backing");
2006 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01002007 ret = bdrv_open(bs_old_backing, backing_name, NULL, BDRV_O_FLAGS,
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002008 old_backing_drv);
2009 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002010 error_report("Could not open old backing file '%s'", backing_name);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002011 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002012 }
Alex Bligha6166732012-10-16 13:46:18 +01002013 if (out_baseimg[0]) {
2014 bs_new_backing = bdrv_new("new_backing");
Kevin Wolfde9c0ce2013-03-15 10:35:02 +01002015 ret = bdrv_open(bs_new_backing, out_baseimg, NULL, BDRV_O_FLAGS,
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002016 new_backing_drv);
Alex Bligha6166732012-10-16 13:46:18 +01002017 if (ret) {
2018 error_report("Could not open new backing file '%s'",
2019 out_baseimg);
2020 goto out;
2021 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002022 }
2023 }
2024
2025 /*
2026 * Check each unallocated cluster in the COW file. If it is unallocated,
2027 * accesses go to the backing file. We must therefore compare this cluster
2028 * in the old and new backing file, and if they differ we need to copy it
2029 * from the old backing file into the COW file.
2030 *
2031 * If qemu-img crashes during this step, no harm is done. The content of
2032 * the image is the same as the original one at any time.
2033 */
2034 if (!unsafe) {
2035 uint64_t num_sectors;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002036 uint64_t old_backing_num_sectors;
Alex Bligha6166732012-10-16 13:46:18 +01002037 uint64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002038 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02002039 int n;
TeLeMand6771bf2010-02-08 16:20:00 +08002040 uint8_t * buf_old;
2041 uint8_t * buf_new;
Kevin Wolf1f710492012-10-12 14:29:18 +02002042 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08002043
Kevin Wolfbb1c0592011-08-08 14:09:12 +02002044 buf_old = qemu_blockalign(bs, IO_BUF_SIZE);
2045 buf_new = qemu_blockalign(bs, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002046
2047 bdrv_get_geometry(bs, &num_sectors);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002048 bdrv_get_geometry(bs_old_backing, &old_backing_num_sectors);
Alex Bligha6166732012-10-16 13:46:18 +01002049 if (bs_new_backing) {
2050 bdrv_get_geometry(bs_new_backing, &new_backing_num_sectors);
2051 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002052
Kevin Wolf1f710492012-10-12 14:29:18 +02002053 if (num_sectors != 0) {
2054 local_progress = (float)100 /
2055 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
2056 }
2057
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002058 for (sector = 0; sector < num_sectors; sector += n) {
2059
2060 /* How many sectors can we handle with the next read? */
2061 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
2062 n = (IO_BUF_SIZE / 512);
2063 } else {
2064 n = num_sectors - sector;
2065 }
2066
2067 /* If the cluster is allocated, we don't need to take action */
Kevin Wolfcc60e322010-04-29 14:47:48 +02002068 ret = bdrv_is_allocated(bs, sector, n, &n);
2069 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002070 continue;
2071 }
2072
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002073 /*
2074 * Read old and new backing file and take into consideration that
2075 * backing files may be smaller than the COW image.
2076 */
2077 if (sector >= old_backing_num_sectors) {
2078 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
2079 } else {
2080 if (sector + n > old_backing_num_sectors) {
2081 n = old_backing_num_sectors - sector;
2082 }
2083
2084 ret = bdrv_read(bs_old_backing, sector, buf_old, n);
2085 if (ret < 0) {
2086 error_report("error while reading from old backing file");
2087 goto out;
2088 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002089 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002090
Alex Bligha6166732012-10-16 13:46:18 +01002091 if (sector >= new_backing_num_sectors || !bs_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002092 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
2093 } else {
2094 if (sector + n > new_backing_num_sectors) {
2095 n = new_backing_num_sectors - sector;
2096 }
2097
2098 ret = bdrv_read(bs_new_backing, sector, buf_new, n);
2099 if (ret < 0) {
2100 error_report("error while reading from new backing file");
2101 goto out;
2102 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002103 }
2104
2105 /* If they differ, we need to write to the COW file */
2106 uint64_t written = 0;
2107
2108 while (written < n) {
2109 int pnum;
2110
2111 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01002112 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002113 {
2114 ret = bdrv_write(bs, sector + written,
2115 buf_old + written * 512, pnum);
2116 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002117 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002118 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002119 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002120 }
2121 }
2122
2123 written += pnum;
2124 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002125 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002126 }
TeLeMand6771bf2010-02-08 16:20:00 +08002127
Kevin Wolfbb1c0592011-08-08 14:09:12 +02002128 qemu_vfree(buf_old);
2129 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002130 }
2131
2132 /*
2133 * Change the backing file. All clusters that are different from the old
2134 * backing file are overwritten in the COW file now, so the visible content
2135 * doesn't change when we switch the backing file.
2136 */
Alex Bligha6166732012-10-16 13:46:18 +01002137 if (out_baseimg && *out_baseimg) {
2138 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
2139 } else {
2140 ret = bdrv_change_backing_file(bs, NULL, NULL);
2141 }
2142
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002143 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002144 error_report("Could not change the backing file to '%s': No "
2145 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002146 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002147 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002148 out_baseimg, strerror(-ret));
2149 }
2150
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002151 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002152 /*
2153 * TODO At this point it is possible to check if any clusters that are
2154 * allocated in the COW file are the same in the backing file. If so, they
2155 * could be dropped from the COW file. Don't do this before switching the
2156 * backing file, in case of a crash this would lead to corruption.
2157 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002158out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002159 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002160 /* Cleanup */
2161 if (!unsafe) {
Kevin Wolfeb863ad2011-03-31 12:39:51 +02002162 if (bs_old_backing != NULL) {
2163 bdrv_delete(bs_old_backing);
2164 }
2165 if (bs_new_backing != NULL) {
2166 bdrv_delete(bs_new_backing);
2167 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002168 }
2169
2170 bdrv_delete(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002171 if (ret) {
2172 return 1;
2173 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002174 return 0;
2175}
2176
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002177static int img_resize(int argc, char **argv)
2178{
2179 int c, ret, relative;
2180 const char *filename, *fmt, *size;
2181 int64_t n, total_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002182 bool quiet = false;
Jes Sorensen2a819982010-12-06 17:08:31 +01002183 BlockDriverState *bs = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002184 QemuOpts *param;
2185 static QemuOptsList resize_options = {
2186 .name = "resize_options",
2187 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
2188 .desc = {
2189 {
2190 .name = BLOCK_OPT_SIZE,
2191 .type = QEMU_OPT_SIZE,
2192 .help = "Virtual disk size"
2193 }, {
2194 /* end of list */
2195 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002196 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002197 };
2198
Kevin Wolfe80fec72011-04-29 10:58:12 +02002199 /* Remove size from argv manually so that negative numbers are not treated
2200 * as options by getopt. */
2201 if (argc < 3) {
2202 help();
2203 return 1;
2204 }
2205
2206 size = argv[--argc];
2207
2208 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002209 fmt = NULL;
2210 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002211 c = getopt(argc, argv, "f:hq");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002212 if (c == -1) {
2213 break;
2214 }
2215 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002216 case '?':
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002217 case 'h':
2218 help();
2219 break;
2220 case 'f':
2221 fmt = optarg;
2222 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002223 case 'q':
2224 quiet = true;
2225 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002226 }
2227 }
Kevin Wolfe80fec72011-04-29 10:58:12 +02002228 if (optind >= argc) {
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002229 help();
2230 }
2231 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002232
2233 /* Choose grow, shrink, or absolute resize mode */
2234 switch (size[0]) {
2235 case '+':
2236 relative = 1;
2237 size++;
2238 break;
2239 case '-':
2240 relative = -1;
2241 size++;
2242 break;
2243 default:
2244 relative = 0;
2245 break;
2246 }
2247
2248 /* Parse size */
Dong Xu Wange478b442012-12-06 14:47:22 +08002249 param = qemu_opts_create_nofail(&resize_options);
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002250 if (qemu_opt_set(param, BLOCK_OPT_SIZE, size)) {
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002251 /* Error message already printed when size parsing fails */
Jes Sorensen2a819982010-12-06 17:08:31 +01002252 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002253 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01002254 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002255 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002256 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
2257 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002258
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002259 bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002260 if (!bs) {
Jes Sorensen2a819982010-12-06 17:08:31 +01002261 ret = -1;
2262 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002263 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002264
2265 if (relative) {
2266 total_size = bdrv_getlength(bs) + n * relative;
2267 } else {
2268 total_size = n;
2269 }
2270 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002271 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002272 ret = -1;
2273 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002274 }
2275
2276 ret = bdrv_truncate(bs, total_size);
2277 switch (ret) {
2278 case 0:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002279 qprintf(quiet, "Image resized.\n");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002280 break;
2281 case -ENOTSUP:
Kevin Wolf259b2172012-03-06 12:44:45 +01002282 error_report("This image does not support resize");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002283 break;
2284 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +01002285 error_report("Image is read-only");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002286 break;
2287 default:
Jes Sorensen15654a62010-12-16 14:31:53 +01002288 error_report("Error resizing image (%d)", -ret);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002289 break;
2290 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002291out:
Jes Sorensen2a819982010-12-06 17:08:31 +01002292 if (bs) {
2293 bdrv_delete(bs);
2294 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002295 if (ret) {
2296 return 1;
2297 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002298 return 0;
2299}
2300
Anthony Liguoric227f092009-10-01 16:12:16 -05002301static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01002302#define DEF(option, callback, arg_string) \
2303 { option, callback },
2304#include "qemu-img-cmds.h"
2305#undef DEF
2306#undef GEN_DOCS
2307 { NULL, NULL, },
2308};
2309
bellardea2384d2004-08-01 21:59:26 +00002310int main(int argc, char **argv)
2311{
Anthony Liguoric227f092009-10-01 16:12:16 -05002312 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01002313 const char *cmdname;
bellardea2384d2004-08-01 21:59:26 +00002314
Kevin Wolf53f76e52010-12-16 15:10:32 +01002315 error_set_progname(argv[0]);
2316
Paolo Bonzini2592c592012-11-03 18:10:17 +01002317 qemu_init_main_loop();
bellardea2384d2004-08-01 21:59:26 +00002318 bdrv_init();
2319 if (argc < 2)
2320 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002321 cmdname = argv[1];
aurel328f9b1572009-02-09 18:14:31 +00002322 argc--; argv++;
Stuart Brady153859b2009-06-07 00:42:17 +01002323
2324 /* find the command */
2325 for(cmd = img_cmds; cmd->name != NULL; cmd++) {
2326 if (!strcmp(cmdname, cmd->name)) {
2327 return cmd->handler(argc, argv);
2328 }
bellardea2384d2004-08-01 21:59:26 +00002329 }
Stuart Brady153859b2009-06-07 00:42:17 +01002330
2331 /* not found */
2332 help();
bellardea2384d2004-08-01 21:59:26 +00002333 return 0;
2334}