blob: ff29ed1c6711f7926b63be376ef540728e587e62 [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>
bellarde8445332006-06-14 15:32:10 +000035
Jeff Cody5f6979c2014-04-28 14:37:18 -040036#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION \
37 ", Copyright (c) 2004-2008 Fabrice Bellard\n"
38
Anthony Liguoric227f092009-10-01 16:12:16 -050039typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010040 const char *name;
41 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050042} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010043
Federico Simoncelli8599ea42013-01-28 06:59:47 -050044enum {
45 OPTION_OUTPUT = 256,
46 OPTION_BACKING_CHAIN = 257,
47};
48
49typedef enum OutputFormat {
50 OFORMAT_JSON,
51 OFORMAT_HUMAN,
52} OutputFormat;
53
aurel32137519c2008-11-30 19:12:49 +000054/* Default to cache=writeback as data integrity is not important for qemu-tcg. */
Stefan Hajnocziadfe0782010-04-13 10:29:35 +010055#define BDRV_O_FLAGS BDRV_O_CACHE_WB
Federico Simoncelli661a0f72011-06-20 12:48:19 -040056#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000057
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010058static void format_print(void *opaque, const char *name)
bellardea2384d2004-08-01 21:59:26 +000059{
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +010060 printf(" %s", name);
bellardea2384d2004-08-01 21:59:26 +000061}
62
Fam Zhengac1307a2014-04-22 13:36:11 +080063static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
64{
65 va_list ap;
66
67 error_printf("qemu-img: ");
68
69 va_start(ap, fmt);
70 error_vprintf(fmt, ap);
71 va_end(ap);
72
73 error_printf("\nTry 'qemu-img --help' for more information\n");
74 exit(EXIT_FAILURE);
75}
76
blueswir1d2c639d2009-01-24 18:19:25 +000077/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +080078static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +000079{
Paolo Bonzinie00291c2010-02-04 16:49:56 +010080 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -040081 QEMU_IMG_VERSION
malc3f020d72010-02-08 12:04:56 +030082 "usage: qemu-img command [command options]\n"
83 "QEMU disk image utility\n"
84 "\n"
85 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +010086#define DEF(option, callback, arg_string) \
87 " " arg_string "\n"
88#include "qemu-img-cmds.h"
89#undef DEF
90#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +030091 "\n"
92 "Command parameters:\n"
93 " 'filename' is a disk image filename\n"
94 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -040095 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +080096 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
97 " 'directsync' and 'unsafe' (default for convert)\n"
Max Reitz40055952014-07-22 22:58:42 +020098 " 'src_cache' in contrast is the cache mode used to read input disk images\n"
malc3f020d72010-02-08 12:04:56 +030099 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +0200100 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
101 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
102 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300103 " 'output_filename' is the destination disk image filename\n"
104 " 'output_fmt' is the destination format\n"
105 " 'options' is a comma separated list of format specific options in a\n"
106 " name=value format. Use -o ? for an overview of the options supported by the\n"
107 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800108 " 'snapshot_param' is param used for internal snapshot, format\n"
109 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
110 " '[ID_OR_NAME]'\n"
111 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
112 " instead\n"
malc3f020d72010-02-08 12:04:56 +0300113 " '-c' indicates that target image must be compressed (qcow format only)\n"
114 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
115 " match exactly. The image doesn't need a working backing file before\n"
116 " rebasing in this case (useful for renaming the backing file)\n"
117 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200118 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100119 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200120 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
121 " contain only zeros for qemu-img to create a sparse image during\n"
122 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
123 " unallocated or zero sectors, and the destination image will always be\n"
124 " fully allocated\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200125 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100126 " '-n' skips the target volume creation (useful if the volume is created\n"
127 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300128 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200129 "Parameters to check subcommand:\n"
130 " '-r' tries to repair any inconsistencies that are found during the check.\n"
131 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
132 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200133 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200134 "\n"
malc3f020d72010-02-08 12:04:56 +0300135 "Parameters to snapshot subcommand:\n"
136 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
137 " '-a' applies a snapshot (revert disk to saved state)\n"
138 " '-c' creates a snapshot\n"
139 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100140 " '-l' lists all snapshots in the given image\n"
141 "\n"
142 "Parameters to compare subcommand:\n"
143 " '-f' first image format\n"
144 " '-F' second image format\n"
145 " '-s' run in Strict mode - fail on different image size or sector allocation\n";
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100146
147 printf("%s\nSupported formats:", help_msg);
Stefan Hajnoczi00c6d402014-08-27 12:08:56 +0100148 bdrv_iterate_format(format_print, NULL);
bellardea2384d2004-08-01 21:59:26 +0000149 printf("\n");
Fam Zhengac1307a2014-04-22 13:36:11 +0800150 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000151}
152
Stefan Weil7c30f652013-06-16 17:01:05 +0200153static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100154{
155 int ret = 0;
156 if (!quiet) {
157 va_list args;
158 va_start(args, fmt);
159 ret = vprintf(fmt, args);
160 va_end(args);
161 }
162 return ret;
163}
164
bellardea2384d2004-08-01 21:59:26 +0000165#if defined(WIN32)
166/* XXX: put correct support for win32 */
167static int read_password(char *buf, int buf_size)
168{
169 int c, i;
Chen Gangfdcf6e62014-07-06 16:43:33 +0800170
bellardea2384d2004-08-01 21:59:26 +0000171 printf("Password: ");
172 fflush(stdout);
173 i = 0;
174 for(;;) {
175 c = getchar();
Chen Gangfdcf6e62014-07-06 16:43:33 +0800176 if (c < 0) {
177 buf[i] = '\0';
178 return -1;
179 } else if (c == '\n') {
bellardea2384d2004-08-01 21:59:26 +0000180 break;
Chen Gangfdcf6e62014-07-06 16:43:33 +0800181 } else if (i < (buf_size - 1)) {
bellardea2384d2004-08-01 21:59:26 +0000182 buf[i++] = c;
Chen Gangfdcf6e62014-07-06 16:43:33 +0800183 }
bellardea2384d2004-08-01 21:59:26 +0000184 }
185 buf[i] = '\0';
186 return 0;
187}
188
189#else
190
191#include <termios.h>
192
193static struct termios oldtty;
194
195static void term_exit(void)
196{
197 tcsetattr (0, TCSANOW, &oldtty);
198}
199
200static void term_init(void)
201{
202 struct termios tty;
203
204 tcgetattr (0, &tty);
205 oldtty = tty;
206
207 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
208 |INLCR|IGNCR|ICRNL|IXON);
209 tty.c_oflag |= OPOST;
210 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
211 tty.c_cflag &= ~(CSIZE|PARENB);
212 tty.c_cflag |= CS8;
213 tty.c_cc[VMIN] = 1;
214 tty.c_cc[VTIME] = 0;
ths3b46e622007-09-17 08:09:54 +0000215
bellardea2384d2004-08-01 21:59:26 +0000216 tcsetattr (0, TCSANOW, &tty);
217
218 atexit(term_exit);
219}
220
pbrook3f379ab2007-11-11 03:33:13 +0000221static int read_password(char *buf, int buf_size)
bellardea2384d2004-08-01 21:59:26 +0000222{
223 uint8_t ch;
224 int i, ret;
225
226 printf("password: ");
227 fflush(stdout);
228 term_init();
229 i = 0;
230 for(;;) {
231 ret = read(0, &ch, 1);
232 if (ret == -1) {
233 if (errno == EAGAIN || errno == EINTR) {
234 continue;
235 } else {
bellardea2384d2004-08-01 21:59:26 +0000236 break;
237 }
238 } else if (ret == 0) {
239 ret = -1;
240 break;
241 } else {
242 if (ch == '\r') {
243 ret = 0;
244 break;
245 }
246 if (i < (buf_size - 1))
247 buf[i++] = ch;
248 }
249 }
250 term_exit();
251 buf[i] = '\0';
252 printf("\n");
253 return ret;
254}
255#endif
256
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100257static int print_block_option_help(const char *filename, const char *fmt)
258{
259 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800260 QemuOptsList *create_opts = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100261
262 /* Find driver and parse its options */
263 drv = bdrv_find_format(fmt);
264 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100265 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100266 return 1;
267 }
268
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800269 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100270 if (filename) {
271 proto_drv = bdrv_find_protocol(filename, true);
272 if (!proto_drv) {
273 error_report("Unknown protocol '%s'", filename);
Chunyan Liu83d05212014-06-05 17:20:51 +0800274 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100275 return 1;
276 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800277 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100278 }
279
Chunyan Liu83d05212014-06-05 17:20:51 +0800280 qemu_opts_print_help(create_opts);
281 qemu_opts_free(create_opts);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100282 return 0;
283}
284
Kevin Wolf9ffe3332014-04-17 16:57:13 +0200285static BlockDriverState *bdrv_new_open(const char *id,
286 const char *filename,
Sheng Yang9bc378c2010-01-29 10:15:06 +0800287 const char *fmt,
Daniel P. Berrangef0536bb2012-09-10 12:11:31 +0100288 int flags,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100289 bool require_io,
290 bool quiet)
bellard75c23802004-08-27 21:28:58 +0000291{
292 BlockDriverState *bs;
293 BlockDriver *drv;
294 char password[256];
Max Reitz34b5d2c2013-09-05 14:45:29 +0200295 Error *local_err = NULL;
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100296 int ret;
bellard75c23802004-08-27 21:28:58 +0000297
Kevin Wolf9ffe3332014-04-17 16:57:13 +0200298 bs = bdrv_new(id, &error_abort);
Kevin Wolfad717132010-12-16 15:37:41 +0100299
bellard75c23802004-08-27 21:28:58 +0000300 if (fmt) {
301 drv = bdrv_find_format(fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900302 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100303 error_report("Unknown file format '%s'", fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900304 goto fail;
305 }
bellard75c23802004-08-27 21:28:58 +0000306 } else {
307 drv = NULL;
308 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100309
Max Reitzddf56362014-02-18 18:33:06 +0100310 ret = bdrv_open(&bs, filename, NULL, NULL, flags, drv, &local_err);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100311 if (ret < 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200312 error_report("Could not open '%s': %s", filename,
313 error_get_pretty(local_err));
314 error_free(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900315 goto fail;
bellard75c23802004-08-27 21:28:58 +0000316 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100317
Daniel P. Berrangef0536bb2012-09-10 12:11:31 +0100318 if (bdrv_is_encrypted(bs) && require_io) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100319 qprintf(quiet, "Disk image '%s' is encrypted.\n", filename);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900320 if (read_password(password, sizeof(password)) < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100321 error_report("No password given");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900322 goto fail;
323 }
324 if (bdrv_set_key(bs, password) < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100325 error_report("invalid password");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900326 goto fail;
327 }
bellard75c23802004-08-27 21:28:58 +0000328 }
329 return bs;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900330fail:
Max Reitzf67503e2014-02-18 18:33:05 +0100331 bdrv_unref(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900332 return NULL;
bellard75c23802004-08-27 21:28:58 +0000333}
334
Chunyan Liu83d05212014-06-05 17:20:51 +0800335static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100336 const char *base_filename,
337 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200338{
Kevin Wolfefa84d42009-05-18 16:42:12 +0200339 if (base_filename) {
Chunyan Liu83d05212014-06-05 17:20:51 +0800340 if (qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename)) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100341 error_report("Backing file not supported for file format '%s'",
342 fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900343 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200344 }
345 }
346 if (base_fmt) {
Chunyan Liu83d05212014-06-05 17:20:51 +0800347 if (qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt)) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100348 error_report("Backing file format not supported for file "
349 "format '%s'", fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900350 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200351 }
352 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900353 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200354}
355
bellardea2384d2004-08-01 21:59:26 +0000356static int img_create(int argc, char **argv)
357{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200358 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100359 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000360 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000361 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000362 const char *filename;
363 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200364 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200365 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100366 bool quiet = false;
ths3b46e622007-09-17 08:09:54 +0000367
bellardea2384d2004-08-01 21:59:26 +0000368 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100369 c = getopt(argc, argv, "F:b:f:he6o:q");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100370 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000371 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100372 }
bellardea2384d2004-08-01 21:59:26 +0000373 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100374 case '?':
bellardea2384d2004-08-01 21:59:26 +0000375 case 'h':
376 help();
377 break;
aliguori9230eaf2009-03-28 17:55:19 +0000378 case 'F':
379 base_fmt = optarg;
380 break;
bellardea2384d2004-08-01 21:59:26 +0000381 case 'b':
382 base_filename = optarg;
383 break;
384 case 'f':
385 fmt = optarg;
386 break;
387 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200388 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100389 "encryption\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100390 goto fail;
thsd8871c52007-10-24 16:11:42 +0000391 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200392 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100393 "compat6\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100394 goto fail;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200395 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100396 if (!is_valid_option_list(optarg)) {
397 error_report("Invalid option list: %s", optarg);
398 goto fail;
399 }
400 if (!options) {
401 options = g_strdup(optarg);
402 } else {
403 char *old_options = options;
404 options = g_strdup_printf("%s,%s", options, optarg);
405 g_free(old_options);
406 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200407 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100408 case 'q':
409 quiet = true;
410 break;
bellardea2384d2004-08-01 21:59:26 +0000411 }
412 }
aliguori9230eaf2009-03-28 17:55:19 +0000413
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900414 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100415 filename = (optind < argc) ? argv[optind] : NULL;
416 if (options && has_help_option(options)) {
417 g_free(options);
418 return print_block_option_help(filename, fmt);
419 }
420
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100421 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800422 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100423 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100424 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900425
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100426 /* Get image size, if specified */
427 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100428 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +0100429 char *end;
430 sval = strtosz_suffix(argv[optind++], &end, STRTOSZ_DEFSUFFIX_B);
431 if (sval < 0 || *end) {
liguang79443392012-12-17 09:49:23 +0800432 if (sval == -ERANGE) {
433 error_report("Image size must be less than 8 EiB!");
434 } else {
435 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200436 "G, T, P or E suffixes for ");
437 error_report("kilobytes, megabytes, gigabytes, terabytes, "
438 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800439 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100440 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100441 }
442 img_size = (uint64_t)sval;
443 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200444 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800445 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200446 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100447
Luiz Capitulino9b375252012-11-30 10:52:05 -0200448 bdrv_img_create(filename, fmt, base_filename, base_fmt,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100449 options, img_size, BDRV_O_FLAGS, &local_err, quiet);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100450 if (local_err) {
Max Reitzb70d8c22013-09-06 16:51:03 +0200451 error_report("%s: %s", filename, error_get_pretty(local_err));
Luiz Capitulino9b375252012-11-30 10:52:05 -0200452 error_free(local_err);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100453 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900454 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200455
Kevin Wolf77386bf2014-02-21 16:24:04 +0100456 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000457 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100458
459fail:
460 g_free(options);
461 return 1;
bellardea2384d2004-08-01 21:59:26 +0000462}
463
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100464static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500465{
Markus Armbruster4399c432014-04-25 16:50:32 +0200466 Error *local_err = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500467 QString *str;
468 QmpOutputVisitor *ov = qmp_output_visitor_new();
469 QObject *obj;
470 visit_type_ImageCheck(qmp_output_get_visitor(ov),
Markus Armbruster4399c432014-04-25 16:50:32 +0200471 &check, NULL, &local_err);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500472 obj = qmp_output_get_qobject(ov);
473 str = qobject_to_json_pretty(obj);
474 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100475 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500476 qobject_decref(obj);
477 qmp_output_visitor_cleanup(ov);
478 QDECREF(str);
479}
480
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100481static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500482{
483 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100484 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500485 } else {
486 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100487 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
488 "Data may be corrupted, or further writes to the image "
489 "may corrupt it.\n",
490 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500491 }
492
493 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100494 qprintf(quiet,
495 "\n%" PRId64 " leaked clusters were found on the image.\n"
496 "This means waste of disk space, but no harm to data.\n",
497 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500498 }
499
500 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100501 qprintf(quiet,
502 "\n%" PRId64
503 " internal errors have occurred during the check.\n",
504 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500505 }
506 }
507
508 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100509 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
510 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
511 check->allocated_clusters, check->total_clusters,
512 check->allocated_clusters * 100.0 / check->total_clusters,
513 check->fragmented_clusters * 100.0 / check->allocated_clusters,
514 check->compressed_clusters * 100.0 /
515 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500516 }
517
518 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100519 qprintf(quiet,
520 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500521 }
522}
523
524static int collect_image_check(BlockDriverState *bs,
525 ImageCheck *check,
526 const char *filename,
527 const char *fmt,
528 int fix)
529{
530 int ret;
531 BdrvCheckResult result;
532
533 ret = bdrv_check(bs, &result, fix);
534 if (ret < 0) {
535 return ret;
536 }
537
538 check->filename = g_strdup(filename);
539 check->format = g_strdup(bdrv_get_format_name(bs));
540 check->check_errors = result.check_errors;
541 check->corruptions = result.corruptions;
542 check->has_corruptions = result.corruptions != 0;
543 check->leaks = result.leaks;
544 check->has_leaks = result.leaks != 0;
545 check->corruptions_fixed = result.corruptions_fixed;
546 check->has_corruptions_fixed = result.corruptions != 0;
547 check->leaks_fixed = result.leaks_fixed;
548 check->has_leaks_fixed = result.leaks != 0;
549 check->image_end_offset = result.image_end_offset;
550 check->has_image_end_offset = result.image_end_offset != 0;
551 check->total_clusters = result.bfi.total_clusters;
552 check->has_total_clusters = result.bfi.total_clusters != 0;
553 check->allocated_clusters = result.bfi.allocated_clusters;
554 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
555 check->fragmented_clusters = result.bfi.fragmented_clusters;
556 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100557 check->compressed_clusters = result.bfi.compressed_clusters;
558 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500559
560 return 0;
561}
562
Kevin Wolfe076f332010-06-29 11:43:13 +0200563/*
564 * Checks an image for consistency. Exit codes:
565 *
Max Reitzd6635c42014-06-02 22:15:21 +0200566 * 0 - Check completed, image is good
567 * 1 - Check not completed because of internal errors
568 * 2 - Check completed, image is corrupted
569 * 3 - Check completed, image has leaked clusters, but is good otherwise
570 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200571 */
aliguori15859692009-04-21 23:11:53 +0000572static int img_check(int argc, char **argv)
573{
574 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500575 OutputFormat output_format = OFORMAT_HUMAN;
Max Reitz40055952014-07-22 22:58:42 +0200576 const char *filename, *fmt, *output, *cache;
aliguori15859692009-04-21 23:11:53 +0000577 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200578 int fix = 0;
Stefan Hajnoczi058f8f12012-08-09 13:05:56 +0100579 int flags = BDRV_O_FLAGS | BDRV_O_CHECK;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500580 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100581 bool quiet = false;
aliguori15859692009-04-21 23:11:53 +0000582
583 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500584 output = NULL;
Max Reitz40055952014-07-22 22:58:42 +0200585 cache = BDRV_DEFAULT_CACHE;
aliguori15859692009-04-21 23:11:53 +0000586 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500587 int option_index = 0;
588 static const struct option long_options[] = {
589 {"help", no_argument, 0, 'h'},
590 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530591 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500592 {"output", required_argument, 0, OPTION_OUTPUT},
593 {0, 0, 0, 0}
594 };
Max Reitz40055952014-07-22 22:58:42 +0200595 c = getopt_long(argc, argv, "hf:r:T:q",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500596 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100597 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000598 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100599 }
aliguori15859692009-04-21 23:11:53 +0000600 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100601 case '?':
aliguori15859692009-04-21 23:11:53 +0000602 case 'h':
603 help();
604 break;
605 case 'f':
606 fmt = optarg;
607 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200608 case 'r':
609 flags |= BDRV_O_RDWR;
610
611 if (!strcmp(optarg, "leaks")) {
612 fix = BDRV_FIX_LEAKS;
613 } else if (!strcmp(optarg, "all")) {
614 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
615 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800616 error_exit("Unknown option value for -r "
617 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200618 }
619 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500620 case OPTION_OUTPUT:
621 output = optarg;
622 break;
Max Reitz40055952014-07-22 22:58:42 +0200623 case 'T':
624 cache = optarg;
625 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100626 case 'q':
627 quiet = true;
628 break;
aliguori15859692009-04-21 23:11:53 +0000629 }
630 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200631 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800632 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100633 }
aliguori15859692009-04-21 23:11:53 +0000634 filename = argv[optind++];
635
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500636 if (output && !strcmp(output, "json")) {
637 output_format = OFORMAT_JSON;
638 } else if (output && !strcmp(output, "human")) {
639 output_format = OFORMAT_HUMAN;
640 } else if (output) {
641 error_report("--output must be used with human or json as argument.");
642 return 1;
643 }
644
Max Reitz40055952014-07-22 22:58:42 +0200645 ret = bdrv_parse_cache_flags(cache, &flags);
646 if (ret < 0) {
647 error_report("Invalid source cache option: %s", cache);
648 return 1;
649 }
650
Kevin Wolf9ffe3332014-04-17 16:57:13 +0200651 bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900652 if (!bs) {
653 return 1;
654 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500655
656 check = g_new0(ImageCheck, 1);
657 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200658
659 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200660 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200661 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500662 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200663 }
664
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500665 if (check->corruptions_fixed || check->leaks_fixed) {
666 int corruptions_fixed, leaks_fixed;
667
668 leaks_fixed = check->leaks_fixed;
669 corruptions_fixed = check->corruptions_fixed;
670
671 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100672 qprintf(quiet,
673 "The following inconsistencies were found and repaired:\n\n"
674 " %" PRId64 " leaked clusters\n"
675 " %" PRId64 " corruptions\n\n"
676 "Double checking the fixed image now...\n",
677 check->leaks_fixed,
678 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500679 }
680
681 ret = collect_image_check(bs, check, filename, fmt, 0);
682
683 check->leaks_fixed = leaks_fixed;
684 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200685 }
686
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500687 switch (output_format) {
688 case OFORMAT_HUMAN:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100689 dump_human_image_check(check, quiet);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500690 break;
691 case OFORMAT_JSON:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100692 dump_json_image_check(check, quiet);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500693 break;
694 }
695
696 if (ret || check->check_errors) {
697 ret = 1;
698 goto fail;
699 }
700
701 if (check->corruptions) {
702 ret = 2;
703 } else if (check->leaks) {
704 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200705 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500706 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000707 }
708
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500709fail:
710 qapi_free_ImageCheck(check);
Fam Zheng4f6fd342013-08-23 09:14:47 +0800711 bdrv_unref(bs);
Kevin Wolfe076f332010-06-29 11:43:13 +0200712
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500713 return ret;
aliguori15859692009-04-21 23:11:53 +0000714}
715
bellardea2384d2004-08-01 21:59:26 +0000716static int img_commit(int argc, char **argv)
717{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400718 int c, ret, flags;
719 const char *filename, *fmt, *cache;
bellardea2384d2004-08-01 21:59:26 +0000720 BlockDriverState *bs;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100721 bool quiet = false;
bellardea2384d2004-08-01 21:59:26 +0000722
723 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400724 cache = BDRV_DEFAULT_CACHE;
bellardea2384d2004-08-01 21:59:26 +0000725 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100726 c = getopt(argc, argv, "f:ht:q");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100727 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000728 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100729 }
bellardea2384d2004-08-01 21:59:26 +0000730 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100731 case '?':
bellardea2384d2004-08-01 21:59:26 +0000732 case 'h':
733 help();
734 break;
735 case 'f':
736 fmt = optarg;
737 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400738 case 't':
739 cache = optarg;
740 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100741 case 'q':
742 quiet = true;
743 break;
bellardea2384d2004-08-01 21:59:26 +0000744 }
745 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200746 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800747 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100748 }
bellardea2384d2004-08-01 21:59:26 +0000749 filename = argv[optind++];
750
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400751 flags = BDRV_O_RDWR;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100752 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400753 if (ret < 0) {
754 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczia3981eb2014-08-26 19:17:54 +0100755 return 1;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400756 }
757
Kevin Wolf9ffe3332014-04-17 16:57:13 +0200758 bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900759 if (!bs) {
760 return 1;
761 }
bellardea2384d2004-08-01 21:59:26 +0000762 ret = bdrv_commit(bs);
763 switch(ret) {
764 case 0:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100765 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +0000766 break;
767 case -ENOENT:
Jes Sorensen15654a62010-12-16 14:31:53 +0100768 error_report("No disk inserted");
bellardea2384d2004-08-01 21:59:26 +0000769 break;
770 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +0100771 error_report("Image is read-only");
bellardea2384d2004-08-01 21:59:26 +0000772 break;
773 case -ENOTSUP:
Jes Sorensen15654a62010-12-16 14:31:53 +0100774 error_report("Image is already committed");
bellardea2384d2004-08-01 21:59:26 +0000775 break;
776 default:
Jes Sorensen15654a62010-12-16 14:31:53 +0100777 error_report("Error while committing image");
bellardea2384d2004-08-01 21:59:26 +0000778 break;
779 }
780
Fam Zheng4f6fd342013-08-23 09:14:47 +0800781 bdrv_unref(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900782 if (ret) {
783 return 1;
784 }
bellardea2384d2004-08-01 21:59:26 +0000785 return 0;
786}
787
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +0400788/*
thsf58c7b32008-06-05 21:53:49 +0000789 * Returns true iff the first sector pointed to by 'buf' contains at least
790 * a non-NUL byte.
791 *
792 * 'pnum' is set to the number of sectors (including and immediately following
793 * the first one) that are known to be in the same allocated/unallocated state.
794 */
bellardea2384d2004-08-01 21:59:26 +0000795static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
796{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000797 bool is_zero;
798 int i;
bellardea2384d2004-08-01 21:59:26 +0000799
800 if (n <= 0) {
801 *pnum = 0;
802 return 0;
803 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000804 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +0000805 for(i = 1; i < n; i++) {
806 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000807 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +0000808 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000809 }
bellardea2384d2004-08-01 21:59:26 +0000810 }
811 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000812 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +0000813}
814
Kevin Wolf3e85c6f2010-01-12 12:55:18 +0100815/*
Kevin Wolfa22f1232011-08-26 15:27:13 +0200816 * Like is_allocated_sectors, but if the buffer starts with a used sector,
817 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
818 * breaking up write requests for only small sparse areas.
819 */
820static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
821 int min)
822{
823 int ret;
824 int num_checked, num_used;
825
826 if (n < min) {
827 min = n;
828 }
829
830 ret = is_allocated_sectors(buf, n, pnum);
831 if (!ret) {
832 return ret;
833 }
834
835 num_used = *pnum;
836 buf += BDRV_SECTOR_SIZE * *pnum;
837 n -= *pnum;
838 num_checked = num_used;
839
840 while (n > 0) {
841 ret = is_allocated_sectors(buf, n, pnum);
842
843 buf += BDRV_SECTOR_SIZE * *pnum;
844 n -= *pnum;
845 num_checked += *pnum;
846 if (ret) {
847 num_used = num_checked;
848 } else if (*pnum >= min) {
849 break;
850 }
851 }
852
853 *pnum = num_used;
854 return 1;
855}
856
857/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +0100858 * Compares two buffers sector by sector. Returns 0 if the first sector of both
859 * buffers matches, non-zero otherwise.
860 *
861 * pnum is set to the number of sectors (including and immediately following
862 * the first one) that are known to have the same comparison result
863 */
864static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
865 int *pnum)
866{
867 int res, i;
868
869 if (n <= 0) {
870 *pnum = 0;
871 return 0;
872 }
873
874 res = !!memcmp(buf1, buf2, 512);
875 for(i = 1; i < n; i++) {
876 buf1 += 512;
877 buf2 += 512;
878
879 if (!!memcmp(buf1, buf2, 512) != res) {
880 break;
881 }
882 }
883
884 *pnum = i;
885 return res;
886}
887
Kevin Wolf80ee15a2009-09-15 12:30:43 +0200888#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +0000889
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100890static int64_t sectors_to_bytes(int64_t sectors)
891{
892 return sectors << BDRV_SECTOR_BITS;
893}
894
895static int64_t sectors_to_process(int64_t total, int64_t from)
896{
897 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
898}
899
900/*
901 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
902 *
903 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
904 * data and negative value on error.
905 *
906 * @param bs: Driver used for accessing file
907 * @param sect_num: Number of first sector to check
908 * @param sect_count: Number of sectors to check
909 * @param filename: Name of disk file we are checking (logging purpose)
910 * @param buffer: Allocated buffer for storing read data
911 * @param quiet: Flag for quiet mode
912 */
913static int check_empty_sectors(BlockDriverState *bs, int64_t sect_num,
914 int sect_count, const char *filename,
915 uint8_t *buffer, bool quiet)
916{
917 int pnum, ret = 0;
918 ret = bdrv_read(bs, sect_num, buffer, sect_count);
919 if (ret < 0) {
920 error_report("Error while reading offset %" PRId64 " of %s: %s",
921 sectors_to_bytes(sect_num), filename, strerror(-ret));
922 return ret;
923 }
924 ret = is_allocated_sectors(buffer, sect_count, &pnum);
925 if (ret || pnum != sect_count) {
926 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
927 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
928 return 1;
929 }
930
931 return 0;
932}
933
934/*
935 * Compares two images. Exit codes:
936 *
937 * 0 - Images are identical
938 * 1 - Images differ
939 * >1 - Error occurred
940 */
941static int img_compare(int argc, char **argv)
942{
Max Reitz40055952014-07-22 22:58:42 +0200943 const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100944 BlockDriverState *bs1, *bs2;
945 int64_t total_sectors1, total_sectors2;
946 uint8_t *buf1 = NULL, *buf2 = NULL;
947 int pnum1, pnum2;
948 int allocated1, allocated2;
949 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
950 bool progress = false, quiet = false, strict = false;
Max Reitz40055952014-07-22 22:58:42 +0200951 int flags;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100952 int64_t total_sectors;
953 int64_t sector_num = 0;
954 int64_t nb_sectors;
955 int c, pnum;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100956 uint64_t progress_base;
957
Max Reitz40055952014-07-22 22:58:42 +0200958 cache = BDRV_DEFAULT_CACHE;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100959 for (;;) {
Max Reitz40055952014-07-22 22:58:42 +0200960 c = getopt(argc, argv, "hf:F:T:pqs");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100961 if (c == -1) {
962 break;
963 }
964 switch (c) {
965 case '?':
966 case 'h':
967 help();
968 break;
969 case 'f':
970 fmt1 = optarg;
971 break;
972 case 'F':
973 fmt2 = optarg;
974 break;
Max Reitz40055952014-07-22 22:58:42 +0200975 case 'T':
976 cache = optarg;
977 break;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100978 case 'p':
979 progress = true;
980 break;
981 case 'q':
982 quiet = true;
983 break;
984 case 's':
985 strict = true;
986 break;
987 }
988 }
989
990 /* Progress is not shown in Quiet mode */
991 if (quiet) {
992 progress = false;
993 }
994
995
Kevin Wolffc11eb22013-08-05 10:53:04 +0200996 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800997 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100998 }
999 filename1 = argv[optind++];
1000 filename2 = argv[optind++];
1001
Stefan Hajnoczicbda0162014-08-26 19:17:55 +01001002 /* Initialize before goto out */
1003 qemu_progress_init(progress, 2.0);
1004
Max Reitz40055952014-07-22 22:58:42 +02001005 flags = BDRV_O_FLAGS;
1006 ret = bdrv_parse_cache_flags(cache, &flags);
1007 if (ret < 0) {
1008 error_report("Invalid source cache option: %s", cache);
1009 ret = 2;
1010 goto out3;
1011 }
1012
Max Reitz40055952014-07-22 22:58:42 +02001013 bs1 = bdrv_new_open("image 1", filename1, fmt1, flags, true, quiet);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001014 if (!bs1) {
1015 error_report("Can't open file %s", filename1);
1016 ret = 2;
1017 goto out3;
1018 }
1019
Max Reitz40055952014-07-22 22:58:42 +02001020 bs2 = bdrv_new_open("image 2", filename2, fmt2, flags, true, quiet);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001021 if (!bs2) {
1022 error_report("Can't open file %s", filename2);
1023 ret = 2;
1024 goto out2;
1025 }
1026
1027 buf1 = qemu_blockalign(bs1, IO_BUF_SIZE);
1028 buf2 = qemu_blockalign(bs2, IO_BUF_SIZE);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001029 total_sectors1 = bdrv_nb_sectors(bs1);
1030 if (total_sectors1 < 0) {
1031 error_report("Can't get size of %s: %s",
1032 filename1, strerror(-total_sectors1));
1033 ret = 4;
1034 goto out;
1035 }
1036 total_sectors2 = bdrv_nb_sectors(bs2);
1037 if (total_sectors2 < 0) {
1038 error_report("Can't get size of %s: %s",
1039 filename2, strerror(-total_sectors2));
1040 ret = 4;
1041 goto out;
1042 }
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001043 total_sectors = MIN(total_sectors1, total_sectors2);
1044 progress_base = MAX(total_sectors1, total_sectors2);
1045
1046 qemu_progress_print(0, 100);
1047
1048 if (strict && total_sectors1 != total_sectors2) {
1049 ret = 1;
1050 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1051 goto out;
1052 }
1053
1054 for (;;) {
1055 nb_sectors = sectors_to_process(total_sectors, sector_num);
1056 if (nb_sectors <= 0) {
1057 break;
1058 }
1059 allocated1 = bdrv_is_allocated_above(bs1, NULL, sector_num, nb_sectors,
1060 &pnum1);
1061 if (allocated1 < 0) {
1062 ret = 3;
1063 error_report("Sector allocation test failed for %s", filename1);
1064 goto out;
1065 }
1066
1067 allocated2 = bdrv_is_allocated_above(bs2, NULL, sector_num, nb_sectors,
1068 &pnum2);
1069 if (allocated2 < 0) {
1070 ret = 3;
1071 error_report("Sector allocation test failed for %s", filename2);
1072 goto out;
1073 }
1074 nb_sectors = MIN(pnum1, pnum2);
1075
1076 if (allocated1 == allocated2) {
1077 if (allocated1) {
1078 ret = bdrv_read(bs1, sector_num, buf1, nb_sectors);
1079 if (ret < 0) {
1080 error_report("Error while reading offset %" PRId64 " of %s:"
1081 " %s", sectors_to_bytes(sector_num), filename1,
1082 strerror(-ret));
1083 ret = 4;
1084 goto out;
1085 }
1086 ret = bdrv_read(bs2, sector_num, buf2, nb_sectors);
1087 if (ret < 0) {
1088 error_report("Error while reading offset %" PRId64
1089 " of %s: %s", sectors_to_bytes(sector_num),
1090 filename2, strerror(-ret));
1091 ret = 4;
1092 goto out;
1093 }
1094 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1095 if (ret || pnum != nb_sectors) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001096 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1097 sectors_to_bytes(
1098 ret ? sector_num : sector_num + pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001099 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001100 goto out;
1101 }
1102 }
1103 } else {
1104 if (strict) {
1105 ret = 1;
1106 qprintf(quiet, "Strict mode: Offset %" PRId64
1107 " allocation mismatch!\n",
1108 sectors_to_bytes(sector_num));
1109 goto out;
1110 }
1111
1112 if (allocated1) {
1113 ret = check_empty_sectors(bs1, sector_num, nb_sectors,
1114 filename1, buf1, quiet);
1115 } else {
1116 ret = check_empty_sectors(bs2, sector_num, nb_sectors,
1117 filename2, buf1, quiet);
1118 }
1119 if (ret) {
1120 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001121 error_report("Error while reading offset %" PRId64 ": %s",
1122 sectors_to_bytes(sector_num), strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001123 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001124 }
1125 goto out;
1126 }
1127 }
1128 sector_num += nb_sectors;
1129 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1130 }
1131
1132 if (total_sectors1 != total_sectors2) {
1133 BlockDriverState *bs_over;
1134 int64_t total_sectors_over;
1135 const char *filename_over;
1136
1137 qprintf(quiet, "Warning: Image size mismatch!\n");
1138 if (total_sectors1 > total_sectors2) {
1139 total_sectors_over = total_sectors1;
1140 bs_over = bs1;
1141 filename_over = filename1;
1142 } else {
1143 total_sectors_over = total_sectors2;
1144 bs_over = bs2;
1145 filename_over = filename2;
1146 }
1147
1148 for (;;) {
1149 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1150 if (nb_sectors <= 0) {
1151 break;
1152 }
1153 ret = bdrv_is_allocated_above(bs_over, NULL, sector_num,
1154 nb_sectors, &pnum);
1155 if (ret < 0) {
1156 ret = 3;
1157 error_report("Sector allocation test failed for %s",
1158 filename_over);
1159 goto out;
1160
1161 }
1162 nb_sectors = pnum;
1163 if (ret) {
1164 ret = check_empty_sectors(bs_over, sector_num, nb_sectors,
1165 filename_over, buf1, quiet);
1166 if (ret) {
1167 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001168 error_report("Error while reading offset %" PRId64
1169 " of %s: %s", sectors_to_bytes(sector_num),
1170 filename_over, strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001171 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001172 }
1173 goto out;
1174 }
1175 }
1176 sector_num += nb_sectors;
1177 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1178 }
1179 }
1180
1181 qprintf(quiet, "Images are identical.\n");
1182 ret = 0;
1183
1184out:
Fam Zheng4f6fd342013-08-23 09:14:47 +08001185 bdrv_unref(bs2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001186 qemu_vfree(buf1);
1187 qemu_vfree(buf2);
1188out2:
Fam Zheng4f6fd342013-08-23 09:14:47 +08001189 bdrv_unref(bs1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001190out3:
1191 qemu_progress_end();
1192 return ret;
1193}
1194
bellardea2384d2004-08-01 21:59:26 +00001195static int img_convert(int argc, char **argv)
1196{
Peter Lieven24f833c2013-11-27 11:07:07 +01001197 int c, n, n1, bs_n, bs_i, compress, cluster_sectors, skip_create;
Peter Lieven13c28af2013-11-27 11:07:01 +01001198 int64_t ret = 0;
Max Reitz40055952014-07-22 22:58:42 +02001199 int progress = 0, flags, src_flags;
1200 const char *fmt, *out_fmt, *cache, *src_cache, *out_baseimg, *out_filename;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001201 BlockDriver *drv, *proto_drv;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001202 BlockDriverState **bs = NULL, *out_bs = NULL;
Peter Lieven802c3d42013-12-05 15:54:53 +01001203 int64_t total_sectors, nb_sectors, sector_num, bs_offset;
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001204 int64_t *bs_sectors = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001205 uint8_t * buf = NULL;
Peter Lievenf2521c92013-11-27 11:07:06 +01001206 size_t bufsectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE;
bellardea2384d2004-08-01 21:59:26 +00001207 const uint8_t *buf1;
bellardfaea38e2006-08-05 21:31:00 +00001208 BlockDriverInfo bdi;
Chunyan Liu83d05212014-06-05 17:20:51 +08001209 QemuOpts *opts = NULL;
1210 QemuOptsList *create_opts = NULL;
1211 const char *out_baseimg_param;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001212 char *options = NULL;
edison51ef6722010-09-21 19:58:41 -07001213 const char *snapshot_name = NULL;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001214 int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001215 bool quiet = false;
Max Reitzcc84d902013-09-06 17:14:26 +02001216 Error *local_err = NULL;
Wenchao Xiaef806542013-12-04 17:10:57 +08001217 QemuOpts *sn_opts = NULL;
bellardea2384d2004-08-01 21:59:26 +00001218
1219 fmt = NULL;
1220 out_fmt = "raw";
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001221 cache = "unsafe";
Max Reitz40055952014-07-22 22:58:42 +02001222 src_cache = BDRV_DEFAULT_CACHE;
thsf58c7b32008-06-05 21:53:49 +00001223 out_baseimg = NULL;
Jes Sorenseneec77d92010-12-07 17:44:34 +01001224 compress = 0;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001225 skip_create = 0;
bellardea2384d2004-08-01 21:59:26 +00001226 for(;;) {
Max Reitz40055952014-07-22 22:58:42 +02001227 c = getopt(argc, argv, "hf:O:B:ce6o:s:l:S:pt:T:qn");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001228 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001229 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001230 }
bellardea2384d2004-08-01 21:59:26 +00001231 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001232 case '?':
bellardea2384d2004-08-01 21:59:26 +00001233 case 'h':
1234 help();
1235 break;
1236 case 'f':
1237 fmt = optarg;
1238 break;
1239 case 'O':
1240 out_fmt = optarg;
1241 break;
thsf58c7b32008-06-05 21:53:49 +00001242 case 'B':
1243 out_baseimg = optarg;
1244 break;
bellardea2384d2004-08-01 21:59:26 +00001245 case 'c':
Jes Sorenseneec77d92010-12-07 17:44:34 +01001246 compress = 1;
bellardea2384d2004-08-01 21:59:26 +00001247 break;
1248 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001249 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001250 "encryption\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001251 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001252 goto fail_getopt;
thsec36ba12007-09-16 21:59:02 +00001253 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001254 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001255 "compat6\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001256 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001257 goto fail_getopt;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001258 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01001259 if (!is_valid_option_list(optarg)) {
1260 error_report("Invalid option list: %s", optarg);
1261 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001262 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01001263 }
1264 if (!options) {
1265 options = g_strdup(optarg);
1266 } else {
1267 char *old_options = options;
1268 options = g_strdup_printf("%s,%s", options, optarg);
1269 g_free(old_options);
1270 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001271 break;
edison51ef6722010-09-21 19:58:41 -07001272 case 's':
1273 snapshot_name = optarg;
1274 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08001275 case 'l':
1276 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
1277 sn_opts = qemu_opts_parse(&internal_snapshot_opts, optarg, 0);
1278 if (!sn_opts) {
1279 error_report("Failed in parsing snapshot param '%s'",
1280 optarg);
Kevin Wolf2dc83282014-02-21 16:24:05 +01001281 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001282 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08001283 }
1284 } else {
1285 snapshot_name = optarg;
1286 }
1287 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001288 case 'S':
1289 {
1290 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +01001291 char *end;
1292 sval = strtosz_suffix(optarg, &end, STRTOSZ_DEFSUFFIX_B);
1293 if (sval < 0 || *end) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02001294 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001295 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001296 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001297 }
1298
1299 min_sparse = sval / BDRV_SECTOR_SIZE;
1300 break;
1301 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001302 case 'p':
1303 progress = 1;
1304 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001305 case 't':
1306 cache = optarg;
1307 break;
Max Reitz40055952014-07-22 22:58:42 +02001308 case 'T':
1309 src_cache = optarg;
1310 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001311 case 'q':
1312 quiet = true;
1313 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001314 case 'n':
1315 skip_create = 1;
1316 break;
bellardea2384d2004-08-01 21:59:26 +00001317 }
1318 }
ths3b46e622007-09-17 08:09:54 +00001319
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001320 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001321 if (quiet) {
1322 progress = 0;
1323 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001324 qemu_progress_init(progress, 1.0);
1325
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001326
balrog926c2d22007-10-31 01:11:44 +00001327 bs_n = argc - optind - 1;
Kevin Wolfa283cb62014-02-21 16:24:07 +01001328 out_filename = bs_n >= 1 ? argv[argc - 1] : NULL;
thsf58c7b32008-06-05 21:53:49 +00001329
Kevin Wolf2dc83282014-02-21 16:24:05 +01001330 if (options && has_help_option(options)) {
Jes Sorensen4ac8aac2010-12-06 15:25:38 +01001331 ret = print_block_option_help(out_filename, out_fmt);
1332 goto out;
1333 }
1334
bellardea2384d2004-08-01 21:59:26 +00001335 if (bs_n < 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001336 error_exit("Must specify image file name");
ths5fafdf22007-09-16 21:08:06 +00001337 }
bellardea2384d2004-08-01 21:59:26 +00001338
thsf58c7b32008-06-05 21:53:49 +00001339
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001340 if (bs_n > 1 && out_baseimg) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001341 error_report("-B makes no sense when concatenating multiple input "
1342 "images");
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001343 ret = -1;
1344 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001345 }
Dong Xu Wangf8111c22012-03-15 20:13:31 +08001346
Max Reitz40055952014-07-22 22:58:42 +02001347 src_flags = BDRV_O_FLAGS;
1348 ret = bdrv_parse_cache_flags(src_cache, &src_flags);
1349 if (ret < 0) {
1350 error_report("Invalid source cache option: %s", src_cache);
1351 goto out;
1352 }
1353
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001354 qemu_progress_print(0, 100);
1355
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001356 bs = g_new0(BlockDriverState *, bs_n);
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001357 bs_sectors = g_new(int64_t, bs_n);
balrog926c2d22007-10-31 01:11:44 +00001358
1359 total_sectors = 0;
1360 for (bs_i = 0; bs_i < bs_n; bs_i++) {
Kevin Wolf9ffe3332014-04-17 16:57:13 +02001361 char *id = bs_n > 1 ? g_strdup_printf("source %d", bs_i)
1362 : g_strdup("source");
Max Reitz40055952014-07-22 22:58:42 +02001363 bs[bs_i] = bdrv_new_open(id, argv[optind + bs_i], fmt, src_flags,
Kevin Wolf9ffe3332014-04-17 16:57:13 +02001364 true, quiet);
1365 g_free(id);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001366 if (!bs[bs_i]) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001367 error_report("Could not open '%s'", argv[optind + bs_i]);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001368 ret = -1;
1369 goto out;
1370 }
Markus Armbruster52bf1e72014-06-26 13:23:25 +02001371 bs_sectors[bs_i] = bdrv_nb_sectors(bs[bs_i]);
1372 if (bs_sectors[bs_i] < 0) {
1373 error_report("Could not get size of %s: %s",
1374 argv[optind + bs_i], strerror(-bs_sectors[bs_i]));
1375 ret = -1;
1376 goto out;
1377 }
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001378 total_sectors += bs_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00001379 }
bellardea2384d2004-08-01 21:59:26 +00001380
Wenchao Xiaef806542013-12-04 17:10:57 +08001381 if (sn_opts) {
1382 ret = bdrv_snapshot_load_tmp(bs[0],
1383 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1384 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1385 &local_err);
1386 } else if (snapshot_name != NULL) {
edison51ef6722010-09-21 19:58:41 -07001387 if (bs_n > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02001388 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07001389 ret = -1;
1390 goto out;
1391 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08001392
1393 bdrv_snapshot_load_tmp_by_id_or_name(bs[0], snapshot_name, &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08001394 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01001395 if (local_err) {
Wenchao Xiaef806542013-12-04 17:10:57 +08001396 error_report("Failed to load snapshot: %s",
1397 error_get_pretty(local_err));
1398 error_free(local_err);
1399 ret = -1;
1400 goto out;
edison51ef6722010-09-21 19:58:41 -07001401 }
1402
Kevin Wolfefa84d42009-05-18 16:42:12 +02001403 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +00001404 drv = bdrv_find_format(out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001405 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001406 error_report("Unknown file format '%s'", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001407 ret = -1;
1408 goto out;
1409 }
balrog926c2d22007-10-31 01:11:44 +00001410
Kevin Wolf98289622013-07-10 15:47:39 +02001411 proto_drv = bdrv_find_protocol(out_filename, true);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001412 if (!proto_drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001413 error_report("Unknown protocol '%s'", out_filename);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001414 ret = -1;
1415 goto out;
1416 }
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001417
Chunyan Liuc282e1f2014-06-05 17:21:11 +08001418 create_opts = qemu_opts_append(create_opts, drv->create_opts);
1419 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02001420
Chunyan Liu83d05212014-06-05 17:20:51 +08001421 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
1422 if (options && qemu_opts_do_parse(opts, options, NULL)) {
1423 error_report("Invalid options for file format '%s'", out_fmt);
1424 ret = -1;
1425 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001426 }
1427
Chunyan Liu83d05212014-06-05 17:20:51 +08001428 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_sectors * 512);
1429 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001430 if (ret < 0) {
1431 goto out;
1432 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001433
Kevin Wolfa18953f2010-10-14 15:46:04 +02001434 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08001435 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02001436 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08001437 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02001438 }
1439
Kevin Wolfefa84d42009-05-18 16:42:12 +02001440 /* Check if compression is supported */
Jes Sorenseneec77d92010-12-07 17:44:34 +01001441 if (compress) {
Chunyan Liu83d05212014-06-05 17:20:51 +08001442 bool encryption =
1443 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
1444 const char *preallocation =
1445 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02001446
1447 if (!drv->bdrv_write_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001448 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001449 ret = -1;
1450 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001451 }
1452
Chunyan Liu83d05212014-06-05 17:20:51 +08001453 if (encryption) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001454 error_report("Compression and encryption not supported at "
1455 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001456 ret = -1;
1457 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001458 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02001459
Chunyan Liu83d05212014-06-05 17:20:51 +08001460 if (preallocation
1461 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02001462 {
1463 error_report("Compression and preallocation not supported at "
1464 "the same time");
1465 ret = -1;
1466 goto out;
1467 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001468 }
1469
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001470 if (!skip_create) {
1471 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08001472 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001473 if (ret < 0) {
Max Reitzcc84d902013-09-06 17:14:26 +02001474 error_report("%s: error while converting %s: %s",
1475 out_filename, out_fmt, error_get_pretty(local_err));
1476 error_free(local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001477 goto out;
bellardea2384d2004-08-01 21:59:26 +00001478 }
1479 }
ths3b46e622007-09-17 08:09:54 +00001480
Peter Lieven5a37b602013-10-24 12:07:06 +02001481 flags = min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +01001482 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001483 if (ret < 0) {
1484 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02001485 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001486 }
1487
Kevin Wolf9ffe3332014-04-17 16:57:13 +02001488 out_bs = bdrv_new_open("target", out_filename, out_fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001489 if (!out_bs) {
1490 ret = -1;
1491 goto out;
1492 }
bellardea2384d2004-08-01 21:59:26 +00001493
balrog926c2d22007-10-31 01:11:44 +00001494 bs_i = 0;
1495 bs_offset = 0;
Peter Lievenf2521c92013-11-27 11:07:06 +01001496
1497 /* increase bufsectors from the default 4096 (2M) if opt_transfer_length
1498 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
1499 * as maximum. */
1500 bufsectors = MIN(32768,
1501 MAX(bufsectors, MAX(out_bs->bl.opt_transfer_length,
1502 out_bs->bl.discard_alignment))
1503 );
1504
1505 buf = qemu_blockalign(out_bs, bufsectors * BDRV_SECTOR_SIZE);
balrog926c2d22007-10-31 01:11:44 +00001506
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001507 if (skip_create) {
Markus Armbruster43716fa2014-06-26 13:23:21 +02001508 int64_t output_sectors = bdrv_nb_sectors(out_bs);
1509 if (output_sectors < 0) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001510 error_report("unable to get output image length: %s\n",
Markus Armbruster43716fa2014-06-26 13:23:21 +02001511 strerror(-output_sectors));
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001512 ret = -1;
1513 goto out;
Markus Armbruster43716fa2014-06-26 13:23:21 +02001514 } else if (output_sectors < total_sectors) {
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001515 error_report("output file is smaller than input file");
1516 ret = -1;
1517 goto out;
1518 }
1519 }
1520
Peter Lieven24f833c2013-11-27 11:07:07 +01001521 cluster_sectors = 0;
1522 ret = bdrv_get_info(out_bs, &bdi);
1523 if (ret < 0) {
1524 if (compress) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001525 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001526 goto out;
1527 }
Peter Lieven24f833c2013-11-27 11:07:07 +01001528 } else {
Fam Zheng85f49ca2014-05-06 21:08:43 +08001529 compress = compress || bdi.needs_compressed_writes;
Peter Lieven24f833c2013-11-27 11:07:07 +01001530 cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
1531 }
1532
1533 if (compress) {
1534 if (cluster_sectors <= 0 || cluster_sectors > bufsectors) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001535 error_report("invalid cluster size");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001536 ret = -1;
1537 goto out;
1538 }
bellardea2384d2004-08-01 21:59:26 +00001539 sector_num = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001540
1541 nb_sectors = total_sectors;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001542
bellardea2384d2004-08-01 21:59:26 +00001543 for(;;) {
balrog926c2d22007-10-31 01:11:44 +00001544 int64_t bs_num;
1545 int remainder;
1546 uint8_t *buf2;
1547
bellardea2384d2004-08-01 21:59:26 +00001548 nb_sectors = total_sectors - sector_num;
1549 if (nb_sectors <= 0)
1550 break;
1551 if (nb_sectors >= cluster_sectors)
1552 n = cluster_sectors;
1553 else
1554 n = nb_sectors;
balrog926c2d22007-10-31 01:11:44 +00001555
1556 bs_num = sector_num - bs_offset;
1557 assert (bs_num >= 0);
1558 remainder = n;
1559 buf2 = buf;
1560 while (remainder > 0) {
1561 int nlow;
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001562 while (bs_num == bs_sectors[bs_i]) {
1563 bs_offset += bs_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00001564 bs_i++;
1565 assert (bs_i < bs_n);
balrog926c2d22007-10-31 01:11:44 +00001566 bs_num = 0;
Blue Swirl0bfcd592010-05-22 08:02:12 +00001567 /* printf("changing part: sector_num=%" PRId64 ", "
1568 "bs_i=%d, bs_offset=%" PRId64 ", bs_sectors=%" PRId64
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001569 "\n", sector_num, bs_i, bs_offset, bs_sectors[bs_i]); */
balrog926c2d22007-10-31 01:11:44 +00001570 }
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001571 assert (bs_num < bs_sectors[bs_i]);
balrog926c2d22007-10-31 01:11:44 +00001572
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001573 nlow = remainder > bs_sectors[bs_i] - bs_num
1574 ? bs_sectors[bs_i] - bs_num : remainder;
balrog926c2d22007-10-31 01:11:44 +00001575
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001576 ret = bdrv_read(bs[bs_i], bs_num, buf2, nlow);
1577 if (ret < 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001578 error_report("error while reading sector %" PRId64 ": %s",
1579 bs_num, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001580 goto out;
1581 }
balrog926c2d22007-10-31 01:11:44 +00001582
1583 buf2 += nlow * 512;
1584 bs_num += nlow;
1585
1586 remainder -= nlow;
1587 }
1588 assert (remainder == 0);
1589
Stefan Hajnoczi54f106d2013-04-15 17:17:33 +02001590 if (!buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)) {
1591 ret = bdrv_write_compressed(out_bs, sector_num, buf, n);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001592 if (ret != 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001593 error_report("error while compressing sector %" PRId64
1594 ": %s", sector_num, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001595 goto out;
1596 }
bellardea2384d2004-08-01 21:59:26 +00001597 }
1598 sector_num += n;
Peter Lieven13c28af2013-11-27 11:07:01 +01001599 qemu_progress_print(100.0 * sector_num / total_sectors, 0);
bellardea2384d2004-08-01 21:59:26 +00001600 }
bellardfaea38e2006-08-05 21:31:00 +00001601 /* signal EOF to align */
1602 bdrv_write_compressed(out_bs, 0, NULL, 0);
bellardea2384d2004-08-01 21:59:26 +00001603 } else {
Peter Lieven802c3d42013-12-05 15:54:53 +01001604 int64_t sectors_to_read, sectors_read, sector_num_next_status;
1605 bool count_allocated_sectors;
Peter Lieven11b66992013-10-24 12:07:05 +02001606 int has_zero_init = min_sparse ? bdrv_has_zero_init(out_bs) : 0;
Kevin Wolff2feebb2010-04-14 17:30:35 +02001607
Peter Lieven5a37b602013-10-24 12:07:06 +02001608 if (!has_zero_init && bdrv_can_write_zeroes_with_unmap(out_bs)) {
1609 ret = bdrv_make_zero(out_bs, BDRV_REQ_MAY_UNMAP);
1610 if (ret < 0) {
1611 goto out;
1612 }
1613 has_zero_init = 1;
1614 }
1615
Peter Lieven802c3d42013-12-05 15:54:53 +01001616 sectors_to_read = total_sectors;
1617 count_allocated_sectors = progress && (out_baseimg || has_zero_init);
1618restart:
thsf58c7b32008-06-05 21:53:49 +00001619 sector_num = 0; // total number of sectors converted so far
Peter Lieven802c3d42013-12-05 15:54:53 +01001620 sectors_read = 0;
1621 sector_num_next_status = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001622
bellardea2384d2004-08-01 21:59:26 +00001623 for(;;) {
1624 nb_sectors = total_sectors - sector_num;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001625 if (nb_sectors <= 0) {
Peter Lieven802c3d42013-12-05 15:54:53 +01001626 if (count_allocated_sectors) {
1627 sectors_to_read = sectors_read;
1628 count_allocated_sectors = false;
1629 goto restart;
1630 }
Peter Lieven13c28af2013-11-27 11:07:01 +01001631 ret = 0;
bellardea2384d2004-08-01 21:59:26 +00001632 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001633 }
balrog926c2d22007-10-31 01:11:44 +00001634
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001635 while (sector_num - bs_offset >= bs_sectors[bs_i]) {
1636 bs_offset += bs_sectors[bs_i];
balrog926c2d22007-10-31 01:11:44 +00001637 bs_i ++;
1638 assert (bs_i < bs_n);
Blue Swirl0bfcd592010-05-22 08:02:12 +00001639 /* printf("changing part: sector_num=%" PRId64 ", bs_i=%d, "
1640 "bs_offset=%" PRId64 ", bs_sectors=%" PRId64 "\n",
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001641 sector_num, bs_i, bs_offset, bs_sectors[bs_i]); */
balrog926c2d22007-10-31 01:11:44 +00001642 }
1643
Peter Lieven13c28af2013-11-27 11:07:01 +01001644 if ((out_baseimg || has_zero_init) &&
1645 sector_num >= sector_num_next_status) {
1646 n = nb_sectors > INT_MAX ? INT_MAX : nb_sectors;
1647 ret = bdrv_get_block_status(bs[bs_i], sector_num - bs_offset,
1648 n, &n1);
Paolo Bonzinie4a86f82013-09-04 19:00:26 +02001649 if (ret < 0) {
Peter Lieven13c28af2013-11-27 11:07:01 +01001650 error_report("error while reading block status of sector %"
1651 PRId64 ": %s", sector_num - bs_offset,
1652 strerror(-ret));
Paolo Bonzinie4a86f82013-09-04 19:00:26 +02001653 goto out;
aliguori93c65b42009-04-05 17:40:43 +00001654 }
Peter Lieven13c28af2013-11-27 11:07:01 +01001655 /* If the output image is zero initialized, we are not working
1656 * on a shared base and the input is zero we can skip the next
1657 * n1 sectors */
1658 if (has_zero_init && !out_baseimg && (ret & BDRV_BLOCK_ZERO)) {
Paolo Bonzinie4a86f82013-09-04 19:00:26 +02001659 sector_num += n1;
1660 continue;
1661 }
Peter Lieven13c28af2013-11-27 11:07:01 +01001662 /* If the output image is being created as a copy on write
1663 * image, assume that sectors which are unallocated in the
1664 * input image are present in both the output's and input's
1665 * base images (no need to copy them). */
1666 if (out_baseimg) {
1667 if (!(ret & BDRV_BLOCK_DATA)) {
1668 sector_num += n1;
1669 continue;
1670 }
1671 /* The next 'n1' sectors are allocated in the input image.
1672 * Copy only those as they may be followed by unallocated
1673 * sectors. */
1674 nb_sectors = n1;
1675 }
1676 /* avoid redundant callouts to get_block_status */
1677 sector_num_next_status = sector_num + n1;
thsf58c7b32008-06-05 21:53:49 +00001678 }
1679
Peter Lievenf2521c92013-11-27 11:07:06 +01001680 n = MIN(nb_sectors, bufsectors);
Peter Lieven24f833c2013-11-27 11:07:07 +01001681
1682 /* round down request length to an aligned sector, but
1683 * do not bother doing this on short requests. They happen
1684 * when we found an all-zero area, and the next sector to
1685 * write will not be sector_num + n. */
1686 if (cluster_sectors > 0 && n >= cluster_sectors) {
1687 int64_t next_aligned_sector = (sector_num + n);
1688 next_aligned_sector -= next_aligned_sector % cluster_sectors;
1689 if (sector_num + n > next_aligned_sector) {
1690 n = next_aligned_sector - sector_num;
1691 }
1692 }
1693
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001694 n = MIN(n, bs_sectors[bs_i] - (sector_num - bs_offset));
Peter Lieven13c28af2013-11-27 11:07:01 +01001695
Peter Lieven802c3d42013-12-05 15:54:53 +01001696 sectors_read += n;
1697 if (count_allocated_sectors) {
1698 sector_num += n;
1699 continue;
1700 }
1701
1702 n1 = n;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001703 ret = bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n);
1704 if (ret < 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001705 error_report("error while reading sector %" PRId64 ": %s",
1706 sector_num - bs_offset, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001707 goto out;
1708 }
bellardea2384d2004-08-01 21:59:26 +00001709 /* NOTE: at the same time we convert, we do not write zero
1710 sectors to have a chance to compress the image. Ideally, we
1711 should add a specific call to have the info to go faster */
1712 buf1 = buf;
1713 while (n > 0) {
Paolo Bonzini11212d82013-09-04 19:00:27 +02001714 if (!has_zero_init ||
Kevin Wolfa22f1232011-08-26 15:27:13 +02001715 is_allocated_sectors_min(buf1, n, &n1, min_sparse)) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001716 ret = bdrv_write(out_bs, sector_num, buf1, n1);
1717 if (ret < 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001718 error_report("error while writing sector %" PRId64
1719 ": %s", sector_num, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001720 goto out;
1721 }
bellardea2384d2004-08-01 21:59:26 +00001722 }
1723 sector_num += n1;
1724 n -= n1;
1725 buf1 += n1 * 512;
1726 }
Peter Lieven802c3d42013-12-05 15:54:53 +01001727 qemu_progress_print(100.0 * sectors_read / sectors_to_read, 0);
bellardea2384d2004-08-01 21:59:26 +00001728 }
1729 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001730out:
Peter Lieven13c28af2013-11-27 11:07:01 +01001731 if (!ret) {
1732 qemu_progress_print(100, 0);
1733 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001734 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08001735 qemu_opts_del(opts);
1736 qemu_opts_free(create_opts);
Kevin Wolfbb1c0592011-08-08 14:09:12 +02001737 qemu_vfree(buf);
Wenchao Xiaef806542013-12-04 17:10:57 +08001738 if (sn_opts) {
1739 qemu_opts_del(sn_opts);
1740 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001741 if (out_bs) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001742 bdrv_unref(out_bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001743 }
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001744 if (bs) {
1745 for (bs_i = 0; bs_i < bs_n; bs_i++) {
1746 if (bs[bs_i]) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001747 bdrv_unref(bs[bs_i]);
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001748 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001749 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001750 g_free(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001751 }
Markus Armbrusterd739f1c2014-06-26 13:23:24 +02001752 g_free(bs_sectors);
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001753fail_getopt:
1754 g_free(options);
1755
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001756 if (ret) {
1757 return 1;
1758 }
bellardea2384d2004-08-01 21:59:26 +00001759 return 0;
1760}
1761
bellard57d1a2b2004-08-03 21:15:11 +00001762
bellardfaea38e2006-08-05 21:31:00 +00001763static void dump_snapshots(BlockDriverState *bs)
1764{
1765 QEMUSnapshotInfo *sn_tab, *sn;
1766 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00001767
1768 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1769 if (nb_sns <= 0)
1770 return;
1771 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08001772 bdrv_snapshot_dump(fprintf, stdout, NULL);
1773 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00001774 for(i = 0; i < nb_sns; i++) {
1775 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08001776 bdrv_snapshot_dump(fprintf, stdout, sn);
1777 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00001778 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001779 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00001780}
1781
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001782static void dump_json_image_info_list(ImageInfoList *list)
1783{
Markus Armbruster4399c432014-04-25 16:50:32 +02001784 Error *local_err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001785 QString *str;
1786 QmpOutputVisitor *ov = qmp_output_visitor_new();
1787 QObject *obj;
1788 visit_type_ImageInfoList(qmp_output_get_visitor(ov),
Markus Armbruster4399c432014-04-25 16:50:32 +02001789 &list, NULL, &local_err);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001790 obj = qmp_output_get_qobject(ov);
1791 str = qobject_to_json_pretty(obj);
1792 assert(str != NULL);
1793 printf("%s\n", qstring_get_str(str));
1794 qobject_decref(obj);
1795 qmp_output_visitor_cleanup(ov);
1796 QDECREF(str);
1797}
1798
Benoît Canetc054b3f2012-09-05 13:09:02 +02001799static void dump_json_image_info(ImageInfo *info)
1800{
Markus Armbruster4399c432014-04-25 16:50:32 +02001801 Error *local_err = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001802 QString *str;
1803 QmpOutputVisitor *ov = qmp_output_visitor_new();
1804 QObject *obj;
1805 visit_type_ImageInfo(qmp_output_get_visitor(ov),
Markus Armbruster4399c432014-04-25 16:50:32 +02001806 &info, NULL, &local_err);
Benoît Canetc054b3f2012-09-05 13:09:02 +02001807 obj = qmp_output_get_qobject(ov);
1808 str = qobject_to_json_pretty(obj);
1809 assert(str != NULL);
1810 printf("%s\n", qstring_get_str(str));
1811 qobject_decref(obj);
1812 qmp_output_visitor_cleanup(ov);
1813 QDECREF(str);
1814}
1815
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001816static void dump_human_image_info_list(ImageInfoList *list)
1817{
1818 ImageInfoList *elem;
1819 bool delim = false;
1820
1821 for (elem = list; elem; elem = elem->next) {
1822 if (delim) {
1823 printf("\n");
1824 }
1825 delim = true;
1826
Wenchao Xia5b917042013-05-25 11:09:45 +08001827 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001828 }
1829}
1830
1831static gboolean str_equal_func(gconstpointer a, gconstpointer b)
1832{
1833 return strcmp(a, b) == 0;
1834}
1835
1836/**
1837 * Open an image file chain and return an ImageInfoList
1838 *
1839 * @filename: topmost image filename
1840 * @fmt: topmost image format (may be NULL to autodetect)
1841 * @chain: true - enumerate entire backing file chain
1842 * false - only topmost image file
1843 *
1844 * Returns a list of ImageInfo objects or NULL if there was an error opening an
1845 * image file. If there was an error a message will have been printed to
1846 * stderr.
1847 */
1848static ImageInfoList *collect_image_info_list(const char *filename,
1849 const char *fmt,
1850 bool chain)
1851{
1852 ImageInfoList *head = NULL;
1853 ImageInfoList **last = &head;
1854 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08001855 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001856
1857 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
1858
1859 while (filename) {
1860 BlockDriverState *bs;
1861 ImageInfo *info;
1862 ImageInfoList *elem;
1863
1864 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
1865 error_report("Backing file '%s' creates an infinite loop.",
1866 filename);
1867 goto err;
1868 }
1869 g_hash_table_insert(filenames, (gpointer)filename, NULL);
1870
Kevin Wolf9ffe3332014-04-17 16:57:13 +02001871 bs = bdrv_new_open("image", filename, fmt,
1872 BDRV_O_FLAGS | BDRV_O_NO_BACKING, false, false);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001873 if (!bs) {
1874 goto err;
1875 }
1876
Wenchao Xia43526ec2013-06-06 12:27:58 +08001877 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001878 if (err) {
Wenchao Xia43526ec2013-06-06 12:27:58 +08001879 error_report("%s", error_get_pretty(err));
1880 error_free(err);
Prasad Joshibdf866f2014-03-26 01:55:53 +05301881 bdrv_unref(bs);
Wenchao Xia43526ec2013-06-06 12:27:58 +08001882 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08001883 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001884
1885 elem = g_new0(ImageInfoList, 1);
1886 elem->value = info;
1887 *last = elem;
1888 last = &elem->next;
1889
Fam Zheng4f6fd342013-08-23 09:14:47 +08001890 bdrv_unref(bs);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001891
1892 filename = fmt = NULL;
1893 if (chain) {
1894 if (info->has_full_backing_filename) {
1895 filename = info->full_backing_filename;
1896 } else if (info->has_backing_filename) {
1897 filename = info->backing_filename;
1898 }
1899 if (info->has_backing_filename_format) {
1900 fmt = info->backing_filename_format;
1901 }
1902 }
1903 }
1904 g_hash_table_destroy(filenames);
1905 return head;
1906
1907err:
1908 qapi_free_ImageInfoList(head);
1909 g_hash_table_destroy(filenames);
1910 return NULL;
1911}
1912
Benoît Canetc054b3f2012-09-05 13:09:02 +02001913static int img_info(int argc, char **argv)
1914{
1915 int c;
1916 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001917 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001918 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001919 ImageInfoList *list;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001920
bellardea2384d2004-08-01 21:59:26 +00001921 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001922 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00001923 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02001924 int option_index = 0;
1925 static const struct option long_options[] = {
1926 {"help", no_argument, 0, 'h'},
1927 {"format", required_argument, 0, 'f'},
1928 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001929 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Benoît Canetc054b3f2012-09-05 13:09:02 +02001930 {0, 0, 0, 0}
1931 };
1932 c = getopt_long(argc, argv, "f:h",
1933 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001934 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001935 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001936 }
bellardea2384d2004-08-01 21:59:26 +00001937 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001938 case '?':
bellardea2384d2004-08-01 21:59:26 +00001939 case 'h':
1940 help();
1941 break;
1942 case 'f':
1943 fmt = optarg;
1944 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001945 case OPTION_OUTPUT:
1946 output = optarg;
1947 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001948 case OPTION_BACKING_CHAIN:
1949 chain = true;
1950 break;
bellardea2384d2004-08-01 21:59:26 +00001951 }
1952 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02001953 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001954 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001955 }
bellardea2384d2004-08-01 21:59:26 +00001956 filename = argv[optind++];
1957
Benoît Canetc054b3f2012-09-05 13:09:02 +02001958 if (output && !strcmp(output, "json")) {
1959 output_format = OFORMAT_JSON;
1960 } else if (output && !strcmp(output, "human")) {
1961 output_format = OFORMAT_HUMAN;
1962 } else if (output) {
1963 error_report("--output must be used with human or json as argument.");
1964 return 1;
1965 }
1966
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001967 list = collect_image_info_list(filename, fmt, chain);
1968 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001969 return 1;
1970 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001971
Benoît Canetc054b3f2012-09-05 13:09:02 +02001972 switch (output_format) {
1973 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001974 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02001975 break;
1976 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001977 if (chain) {
1978 dump_json_image_info_list(list);
1979 } else {
1980 dump_json_image_info(list->value);
1981 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001982 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001983 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001984
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001985 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00001986 return 0;
1987}
1988
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02001989
1990typedef struct MapEntry {
1991 int flags;
1992 int depth;
1993 int64_t start;
1994 int64_t length;
1995 int64_t offset;
1996 BlockDriverState *bs;
1997} MapEntry;
1998
1999static void dump_map_entry(OutputFormat output_format, MapEntry *e,
2000 MapEntry *next)
2001{
2002 switch (output_format) {
2003 case OFORMAT_HUMAN:
2004 if ((e->flags & BDRV_BLOCK_DATA) &&
2005 !(e->flags & BDRV_BLOCK_OFFSET_VALID)) {
2006 error_report("File contains external, encrypted or compressed clusters.");
2007 exit(1);
2008 }
2009 if ((e->flags & (BDRV_BLOCK_DATA|BDRV_BLOCK_ZERO)) == BDRV_BLOCK_DATA) {
2010 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
2011 e->start, e->length, e->offset, e->bs->filename);
2012 }
2013 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
2014 * Modify the flags here to allow more coalescing.
2015 */
2016 if (next &&
2017 (next->flags & (BDRV_BLOCK_DATA|BDRV_BLOCK_ZERO)) != BDRV_BLOCK_DATA) {
2018 next->flags &= ~BDRV_BLOCK_DATA;
2019 next->flags |= BDRV_BLOCK_ZERO;
2020 }
2021 break;
2022 case OFORMAT_JSON:
2023 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64", \"depth\": %d,"
2024 " \"zero\": %s, \"data\": %s",
2025 (e->start == 0 ? "[" : ",\n"),
2026 e->start, e->length, e->depth,
2027 (e->flags & BDRV_BLOCK_ZERO) ? "true" : "false",
2028 (e->flags & BDRV_BLOCK_DATA) ? "true" : "false");
2029 if (e->flags & BDRV_BLOCK_OFFSET_VALID) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02002030 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002031 }
2032 putchar('}');
2033
2034 if (!next) {
2035 printf("]\n");
2036 }
2037 break;
2038 }
2039}
2040
2041static int get_block_status(BlockDriverState *bs, int64_t sector_num,
2042 int nb_sectors, MapEntry *e)
2043{
2044 int64_t ret;
2045 int depth;
2046
2047 /* As an optimization, we could cache the current range of unallocated
2048 * clusters in each file of the chain, and avoid querying the same
2049 * range repeatedly.
2050 */
2051
2052 depth = 0;
2053 for (;;) {
2054 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors);
2055 if (ret < 0) {
2056 return ret;
2057 }
2058 assert(nb_sectors);
2059 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2060 break;
2061 }
2062 bs = bs->backing_hd;
2063 if (bs == NULL) {
2064 ret = 0;
2065 break;
2066 }
2067
2068 depth++;
2069 }
2070
2071 e->start = sector_num * BDRV_SECTOR_SIZE;
2072 e->length = nb_sectors * BDRV_SECTOR_SIZE;
2073 e->flags = ret & ~BDRV_BLOCK_OFFSET_MASK;
2074 e->offset = ret & BDRV_BLOCK_OFFSET_MASK;
2075 e->depth = depth;
2076 e->bs = bs;
2077 return 0;
2078}
2079
2080static int img_map(int argc, char **argv)
2081{
2082 int c;
2083 OutputFormat output_format = OFORMAT_HUMAN;
2084 BlockDriverState *bs;
2085 const char *filename, *fmt, *output;
2086 int64_t length;
2087 MapEntry curr = { .length = 0 }, next;
2088 int ret = 0;
2089
2090 fmt = NULL;
2091 output = NULL;
2092 for (;;) {
2093 int option_index = 0;
2094 static const struct option long_options[] = {
2095 {"help", no_argument, 0, 'h'},
2096 {"format", required_argument, 0, 'f'},
2097 {"output", required_argument, 0, OPTION_OUTPUT},
2098 {0, 0, 0, 0}
2099 };
2100 c = getopt_long(argc, argv, "f:h",
2101 long_options, &option_index);
2102 if (c == -1) {
2103 break;
2104 }
2105 switch (c) {
2106 case '?':
2107 case 'h':
2108 help();
2109 break;
2110 case 'f':
2111 fmt = optarg;
2112 break;
2113 case OPTION_OUTPUT:
2114 output = optarg;
2115 break;
2116 }
2117 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002118 if (optind != argc - 1) {
2119 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002120 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002121 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002122
2123 if (output && !strcmp(output, "json")) {
2124 output_format = OFORMAT_JSON;
2125 } else if (output && !strcmp(output, "human")) {
2126 output_format = OFORMAT_HUMAN;
2127 } else if (output) {
2128 error_report("--output must be used with human or json as argument.");
2129 return 1;
2130 }
2131
Kevin Wolf9ffe3332014-04-17 16:57:13 +02002132 bs = bdrv_new_open("image", filename, fmt, BDRV_O_FLAGS, true, false);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002133 if (!bs) {
2134 return 1;
2135 }
2136
2137 if (output_format == OFORMAT_HUMAN) {
2138 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2139 }
2140
2141 length = bdrv_getlength(bs);
2142 while (curr.start + curr.length < length) {
2143 int64_t nsectors_left;
2144 int64_t sector_num;
2145 int n;
2146
2147 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2148
2149 /* Probe up to 1 GiB at a time. */
2150 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2151 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2152 ret = get_block_status(bs, sector_num, n, &next);
2153
2154 if (ret < 0) {
2155 error_report("Could not read file metadata: %s", strerror(-ret));
2156 goto out;
2157 }
2158
2159 if (curr.length != 0 && curr.flags == next.flags &&
2160 curr.depth == next.depth &&
2161 ((curr.flags & BDRV_BLOCK_OFFSET_VALID) == 0 ||
2162 curr.offset + curr.length == next.offset)) {
2163 curr.length += next.length;
2164 continue;
2165 }
2166
2167 if (curr.length > 0) {
2168 dump_map_entry(output_format, &curr, &next);
2169 }
2170 curr = next;
2171 }
2172
2173 dump_map_entry(output_format, &curr, NULL);
2174
2175out:
2176 bdrv_unref(bs);
2177 return ret < 0;
2178}
2179
aliguorif7b4a942009-01-07 17:40:15 +00002180#define SNAPSHOT_LIST 1
2181#define SNAPSHOT_CREATE 2
2182#define SNAPSHOT_APPLY 3
2183#define SNAPSHOT_DELETE 4
2184
Stuart Brady153859b2009-06-07 00:42:17 +01002185static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002186{
2187 BlockDriverState *bs;
2188 QEMUSnapshotInfo sn;
2189 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002190 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002191 int action = 0;
2192 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002193 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002194 Error *err = NULL;
aliguorif7b4a942009-01-07 17:40:15 +00002195
Kevin Wolf710da702011-01-10 12:33:02 +01002196 bdrv_oflags = BDRV_O_FLAGS | BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002197 /* Parse commandline parameters */
2198 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002199 c = getopt(argc, argv, "la:c:d:hq");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002200 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002201 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002202 }
aliguorif7b4a942009-01-07 17:40:15 +00002203 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002204 case '?':
aliguorif7b4a942009-01-07 17:40:15 +00002205 case 'h':
2206 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002207 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002208 case 'l':
2209 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002210 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002211 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002212 }
2213 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002214 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002215 break;
2216 case 'a':
2217 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002218 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002219 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002220 }
2221 action = SNAPSHOT_APPLY;
2222 snapshot_name = optarg;
2223 break;
2224 case 'c':
2225 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002226 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002227 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002228 }
2229 action = SNAPSHOT_CREATE;
2230 snapshot_name = optarg;
2231 break;
2232 case 'd':
2233 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002234 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002235 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002236 }
2237 action = SNAPSHOT_DELETE;
2238 snapshot_name = optarg;
2239 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002240 case 'q':
2241 quiet = true;
2242 break;
aliguorif7b4a942009-01-07 17:40:15 +00002243 }
2244 }
2245
Kevin Wolffc11eb22013-08-05 10:53:04 +02002246 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002247 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002248 }
aliguorif7b4a942009-01-07 17:40:15 +00002249 filename = argv[optind++];
2250
2251 /* Open the image */
Kevin Wolf9ffe3332014-04-17 16:57:13 +02002252 bs = bdrv_new_open("image", filename, NULL, bdrv_oflags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002253 if (!bs) {
2254 return 1;
2255 }
aliguorif7b4a942009-01-07 17:40:15 +00002256
2257 /* Perform the requested action */
2258 switch(action) {
2259 case SNAPSHOT_LIST:
2260 dump_snapshots(bs);
2261 break;
2262
2263 case SNAPSHOT_CREATE:
2264 memset(&sn, 0, sizeof(sn));
2265 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2266
2267 qemu_gettimeofday(&tv);
2268 sn.date_sec = tv.tv_sec;
2269 sn.date_nsec = tv.tv_usec * 1000;
2270
2271 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002272 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002273 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002274 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002275 }
aliguorif7b4a942009-01-07 17:40:15 +00002276 break;
2277
2278 case SNAPSHOT_APPLY:
2279 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002280 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002281 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002282 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002283 }
aliguorif7b4a942009-01-07 17:40:15 +00002284 break;
2285
2286 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002287 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002288 if (err) {
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002289 error_report("Could not delete snapshot '%s': (%s)",
2290 snapshot_name, error_get_pretty(err));
2291 error_free(err);
2292 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002293 }
aliguorif7b4a942009-01-07 17:40:15 +00002294 break;
2295 }
2296
2297 /* Cleanup */
Fam Zheng4f6fd342013-08-23 09:14:47 +08002298 bdrv_unref(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002299 if (ret) {
2300 return 1;
2301 }
Stuart Brady153859b2009-06-07 00:42:17 +01002302 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002303}
2304
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002305static int img_rebase(int argc, char **argv)
2306{
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002307 BlockDriverState *bs = NULL, *bs_old_backing = NULL, *bs_new_backing = NULL;
Stefan Hajnoczif163d072010-04-13 10:29:34 +01002308 BlockDriver *old_backing_drv, *new_backing_drv;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002309 char *filename;
Max Reitz40055952014-07-22 22:58:42 +02002310 const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
2311 int c, flags, src_flags, ret;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002312 int unsafe = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002313 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002314 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02002315 Error *local_err = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002316
2317 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002318 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002319 cache = BDRV_DEFAULT_CACHE;
Max Reitz40055952014-07-22 22:58:42 +02002320 src_cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002321 out_baseimg = NULL;
2322 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002323 for(;;) {
Max Reitz40055952014-07-22 22:58:42 +02002324 c = getopt(argc, argv, "hf:F:b:upt:T:q");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002325 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002326 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002327 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002328 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002329 case '?':
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002330 case 'h':
2331 help();
2332 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002333 case 'f':
2334 fmt = optarg;
2335 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002336 case 'F':
2337 out_basefmt = optarg;
2338 break;
2339 case 'b':
2340 out_baseimg = optarg;
2341 break;
2342 case 'u':
2343 unsafe = 1;
2344 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002345 case 'p':
2346 progress = 1;
2347 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002348 case 't':
2349 cache = optarg;
2350 break;
Max Reitz40055952014-07-22 22:58:42 +02002351 case 'T':
2352 src_cache = optarg;
2353 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002354 case 'q':
2355 quiet = true;
2356 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002357 }
2358 }
2359
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002360 if (quiet) {
2361 progress = 0;
2362 }
2363
Fam Zhengac1307a2014-04-22 13:36:11 +08002364 if (optind != argc - 1) {
2365 error_exit("Expecting one image file name");
2366 }
2367 if (!unsafe && !out_baseimg) {
2368 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002369 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002370 filename = argv[optind++];
2371
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002372 qemu_progress_init(progress, 2.0);
2373 qemu_progress_print(0, 100);
2374
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002375 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +01002376 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002377 if (ret < 0) {
2378 error_report("Invalid cache option: %s", cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002379 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002380 }
2381
Max Reitz40055952014-07-22 22:58:42 +02002382 src_flags = BDRV_O_FLAGS;
2383 ret = bdrv_parse_cache_flags(src_cache, &src_flags);
2384 if (ret < 0) {
2385 error_report("Invalid source cache option: %s", src_cache);
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002386 goto out;
Max Reitz40055952014-07-22 22:58:42 +02002387 }
2388
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002389 /*
2390 * Open the images.
2391 *
2392 * Ignore the old backing file for unsafe rebase in case we want to correct
2393 * the reference to a renamed or moved backing file.
2394 */
Kevin Wolf9ffe3332014-04-17 16:57:13 +02002395 bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002396 if (!bs) {
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002397 ret = -1;
2398 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002399 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002400
2401 /* Find the right drivers for the backing files */
2402 old_backing_drv = NULL;
2403 new_backing_drv = NULL;
2404
2405 if (!unsafe && bs->backing_format[0] != '\0') {
2406 old_backing_drv = bdrv_find_format(bs->backing_format);
2407 if (old_backing_drv == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002408 error_report("Invalid format name: '%s'", bs->backing_format);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002409 ret = -1;
2410 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002411 }
2412 }
2413
2414 if (out_basefmt != NULL) {
2415 new_backing_drv = bdrv_find_format(out_basefmt);
2416 if (new_backing_drv == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002417 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002418 ret = -1;
2419 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002420 }
2421 }
2422
2423 /* For safe rebasing we need to compare old and new backing file */
Stefan Hajnoczi40ed35a2014-08-26 19:17:56 +01002424 if (!unsafe) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002425 char backing_name[1024];
2426
Kevin Wolf98522f62014-04-17 13:16:01 +02002427 bs_old_backing = bdrv_new("old_backing", &error_abort);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002428 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitz40055952014-07-22 22:58:42 +02002429 ret = bdrv_open(&bs_old_backing, backing_name, NULL, NULL, src_flags,
Max Reitz34b5d2c2013-09-05 14:45:29 +02002430 old_backing_drv, &local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002431 if (ret) {
Max Reitz34b5d2c2013-09-05 14:45:29 +02002432 error_report("Could not open old backing file '%s': %s",
2433 backing_name, error_get_pretty(local_err));
2434 error_free(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002435 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002436 }
Alex Bligha6166732012-10-16 13:46:18 +01002437 if (out_baseimg[0]) {
Kevin Wolf98522f62014-04-17 13:16:01 +02002438 bs_new_backing = bdrv_new("new_backing", &error_abort);
Max Reitz40055952014-07-22 22:58:42 +02002439 ret = bdrv_open(&bs_new_backing, out_baseimg, NULL, NULL, src_flags,
2440 new_backing_drv, &local_err);
Alex Bligha6166732012-10-16 13:46:18 +01002441 if (ret) {
Max Reitz34b5d2c2013-09-05 14:45:29 +02002442 error_report("Could not open new backing file '%s': %s",
2443 out_baseimg, error_get_pretty(local_err));
2444 error_free(local_err);
Alex Bligha6166732012-10-16 13:46:18 +01002445 goto out;
2446 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002447 }
2448 }
2449
2450 /*
2451 * Check each unallocated cluster in the COW file. If it is unallocated,
2452 * accesses go to the backing file. We must therefore compare this cluster
2453 * in the old and new backing file, and if they differ we need to copy it
2454 * from the old backing file into the COW file.
2455 *
2456 * If qemu-img crashes during this step, no harm is done. The content of
2457 * the image is the same as the original one at any time.
2458 */
2459 if (!unsafe) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002460 int64_t num_sectors;
2461 int64_t old_backing_num_sectors;
2462 int64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002463 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02002464 int n;
TeLeMand6771bf2010-02-08 16:20:00 +08002465 uint8_t * buf_old;
2466 uint8_t * buf_new;
Kevin Wolf1f710492012-10-12 14:29:18 +02002467 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08002468
Kevin Wolfbb1c0592011-08-08 14:09:12 +02002469 buf_old = qemu_blockalign(bs, IO_BUF_SIZE);
2470 buf_new = qemu_blockalign(bs, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002471
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002472 num_sectors = bdrv_nb_sectors(bs);
2473 if (num_sectors < 0) {
2474 error_report("Could not get size of '%s': %s",
2475 filename, strerror(-num_sectors));
2476 ret = -1;
2477 goto out;
2478 }
2479 old_backing_num_sectors = bdrv_nb_sectors(bs_old_backing);
2480 if (old_backing_num_sectors < 0) {
2481 char backing_name[1024];
2482
2483 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
2484 error_report("Could not get size of '%s': %s",
2485 backing_name, strerror(-old_backing_num_sectors));
2486 ret = -1;
2487 goto out;
2488 }
Alex Bligha6166732012-10-16 13:46:18 +01002489 if (bs_new_backing) {
Markus Armbruster52bf1e72014-06-26 13:23:25 +02002490 new_backing_num_sectors = bdrv_nb_sectors(bs_new_backing);
2491 if (new_backing_num_sectors < 0) {
2492 error_report("Could not get size of '%s': %s",
2493 out_baseimg, strerror(-new_backing_num_sectors));
2494 ret = -1;
2495 goto out;
2496 }
Alex Bligha6166732012-10-16 13:46:18 +01002497 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002498
Kevin Wolf1f710492012-10-12 14:29:18 +02002499 if (num_sectors != 0) {
2500 local_progress = (float)100 /
2501 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
2502 }
2503
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002504 for (sector = 0; sector < num_sectors; sector += n) {
2505
2506 /* How many sectors can we handle with the next read? */
2507 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
2508 n = (IO_BUF_SIZE / 512);
2509 } else {
2510 n = num_sectors - sector;
2511 }
2512
2513 /* If the cluster is allocated, we don't need to take action */
Kevin Wolfcc60e322010-04-29 14:47:48 +02002514 ret = bdrv_is_allocated(bs, sector, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02002515 if (ret < 0) {
2516 error_report("error while reading image metadata: %s",
2517 strerror(-ret));
2518 goto out;
2519 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02002520 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002521 continue;
2522 }
2523
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002524 /*
2525 * Read old and new backing file and take into consideration that
2526 * backing files may be smaller than the COW image.
2527 */
2528 if (sector >= old_backing_num_sectors) {
2529 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
2530 } else {
2531 if (sector + n > old_backing_num_sectors) {
2532 n = old_backing_num_sectors - sector;
2533 }
2534
2535 ret = bdrv_read(bs_old_backing, sector, buf_old, n);
2536 if (ret < 0) {
2537 error_report("error while reading from old backing file");
2538 goto out;
2539 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002540 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002541
Alex Bligha6166732012-10-16 13:46:18 +01002542 if (sector >= new_backing_num_sectors || !bs_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002543 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
2544 } else {
2545 if (sector + n > new_backing_num_sectors) {
2546 n = new_backing_num_sectors - sector;
2547 }
2548
2549 ret = bdrv_read(bs_new_backing, sector, buf_new, n);
2550 if (ret < 0) {
2551 error_report("error while reading from new backing file");
2552 goto out;
2553 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002554 }
2555
2556 /* If they differ, we need to write to the COW file */
2557 uint64_t written = 0;
2558
2559 while (written < n) {
2560 int pnum;
2561
2562 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01002563 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002564 {
2565 ret = bdrv_write(bs, sector + written,
2566 buf_old + written * 512, pnum);
2567 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002568 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002569 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002570 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002571 }
2572 }
2573
2574 written += pnum;
2575 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002576 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002577 }
TeLeMand6771bf2010-02-08 16:20:00 +08002578
Kevin Wolfbb1c0592011-08-08 14:09:12 +02002579 qemu_vfree(buf_old);
2580 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002581 }
2582
2583 /*
2584 * Change the backing file. All clusters that are different from the old
2585 * backing file are overwritten in the COW file now, so the visible content
2586 * doesn't change when we switch the backing file.
2587 */
Alex Bligha6166732012-10-16 13:46:18 +01002588 if (out_baseimg && *out_baseimg) {
2589 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
2590 } else {
2591 ret = bdrv_change_backing_file(bs, NULL, NULL);
2592 }
2593
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002594 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002595 error_report("Could not change the backing file to '%s': No "
2596 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002597 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002598 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002599 out_baseimg, strerror(-ret));
2600 }
2601
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002602 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002603 /*
2604 * TODO At this point it is possible to check if any clusters that are
2605 * allocated in the COW file are the same in the backing file. If so, they
2606 * could be dropped from the COW file. Don't do this before switching the
2607 * backing file, in case of a crash this would lead to corruption.
2608 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002609out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002610 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002611 /* Cleanup */
2612 if (!unsafe) {
Kevin Wolfeb863ad2011-03-31 12:39:51 +02002613 if (bs_old_backing != NULL) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08002614 bdrv_unref(bs_old_backing);
Kevin Wolfeb863ad2011-03-31 12:39:51 +02002615 }
2616 if (bs_new_backing != NULL) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08002617 bdrv_unref(bs_new_backing);
Kevin Wolfeb863ad2011-03-31 12:39:51 +02002618 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002619 }
2620
Fam Zheng4f6fd342013-08-23 09:14:47 +08002621 bdrv_unref(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002622 if (ret) {
2623 return 1;
2624 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002625 return 0;
2626}
2627
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002628static int img_resize(int argc, char **argv)
2629{
2630 int c, ret, relative;
2631 const char *filename, *fmt, *size;
2632 int64_t n, total_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002633 bool quiet = false;
Jes Sorensen2a819982010-12-06 17:08:31 +01002634 BlockDriverState *bs = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002635 QemuOpts *param;
2636 static QemuOptsList resize_options = {
2637 .name = "resize_options",
2638 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
2639 .desc = {
2640 {
2641 .name = BLOCK_OPT_SIZE,
2642 .type = QEMU_OPT_SIZE,
2643 .help = "Virtual disk size"
2644 }, {
2645 /* end of list */
2646 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002647 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002648 };
2649
Kevin Wolfe80fec72011-04-29 10:58:12 +02002650 /* Remove size from argv manually so that negative numbers are not treated
2651 * as options by getopt. */
2652 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002653 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02002654 return 1;
2655 }
2656
2657 size = argv[--argc];
2658
2659 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002660 fmt = NULL;
2661 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002662 c = getopt(argc, argv, "f:hq");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002663 if (c == -1) {
2664 break;
2665 }
2666 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002667 case '?':
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002668 case 'h':
2669 help();
2670 break;
2671 case 'f':
2672 fmt = optarg;
2673 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002674 case 'q':
2675 quiet = true;
2676 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002677 }
2678 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002679 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002680 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002681 }
2682 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002683
2684 /* Choose grow, shrink, or absolute resize mode */
2685 switch (size[0]) {
2686 case '+':
2687 relative = 1;
2688 size++;
2689 break;
2690 case '-':
2691 relative = -1;
2692 size++;
2693 break;
2694 default:
2695 relative = 0;
2696 break;
2697 }
2698
2699 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08002700 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002701 if (qemu_opt_set(param, BLOCK_OPT_SIZE, size)) {
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002702 /* Error message already printed when size parsing fails */
Jes Sorensen2a819982010-12-06 17:08:31 +01002703 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002704 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01002705 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002706 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002707 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
2708 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002709
Kevin Wolf9ffe3332014-04-17 16:57:13 +02002710 bs = bdrv_new_open("image", filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR,
2711 true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002712 if (!bs) {
Jes Sorensen2a819982010-12-06 17:08:31 +01002713 ret = -1;
2714 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002715 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002716
2717 if (relative) {
2718 total_size = bdrv_getlength(bs) + n * relative;
2719 } else {
2720 total_size = n;
2721 }
2722 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002723 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002724 ret = -1;
2725 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002726 }
2727
2728 ret = bdrv_truncate(bs, total_size);
2729 switch (ret) {
2730 case 0:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002731 qprintf(quiet, "Image resized.\n");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002732 break;
2733 case -ENOTSUP:
Kevin Wolf259b2172012-03-06 12:44:45 +01002734 error_report("This image does not support resize");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002735 break;
2736 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +01002737 error_report("Image is read-only");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002738 break;
2739 default:
Jes Sorensen15654a62010-12-16 14:31:53 +01002740 error_report("Error resizing image (%d)", -ret);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002741 break;
2742 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002743out:
Jes Sorensen2a819982010-12-06 17:08:31 +01002744 if (bs) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08002745 bdrv_unref(bs);
Jes Sorensen2a819982010-12-06 17:08:31 +01002746 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002747 if (ret) {
2748 return 1;
2749 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002750 return 0;
2751}
2752
Max Reitz6f176b42013-09-03 10:09:50 +02002753static int img_amend(int argc, char **argv)
2754{
2755 int c, ret = 0;
2756 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08002757 QemuOptsList *create_opts = NULL;
2758 QemuOpts *opts = NULL;
Max Reitzbd39e6e2014-07-22 22:58:43 +02002759 const char *fmt = NULL, *filename, *cache;
2760 int flags;
Max Reitz6f176b42013-09-03 10:09:50 +02002761 bool quiet = false;
2762 BlockDriverState *bs = NULL;
2763
Max Reitzbd39e6e2014-07-22 22:58:43 +02002764 cache = BDRV_DEFAULT_CACHE;
Max Reitz6f176b42013-09-03 10:09:50 +02002765 for (;;) {
Max Reitzbd39e6e2014-07-22 22:58:43 +02002766 c = getopt(argc, argv, "ho:f:t:q");
Max Reitz6f176b42013-09-03 10:09:50 +02002767 if (c == -1) {
2768 break;
2769 }
2770
2771 switch (c) {
2772 case 'h':
2773 case '?':
2774 help();
2775 break;
2776 case 'o':
Kevin Wolf626f84f2014-02-21 16:24:06 +01002777 if (!is_valid_option_list(optarg)) {
2778 error_report("Invalid option list: %s", optarg);
2779 ret = -1;
2780 goto out;
2781 }
2782 if (!options) {
2783 options = g_strdup(optarg);
2784 } else {
2785 char *old_options = options;
2786 options = g_strdup_printf("%s,%s", options, optarg);
2787 g_free(old_options);
2788 }
Max Reitz6f176b42013-09-03 10:09:50 +02002789 break;
2790 case 'f':
2791 fmt = optarg;
2792 break;
Max Reitzbd39e6e2014-07-22 22:58:43 +02002793 case 't':
2794 cache = optarg;
2795 break;
Max Reitz6f176b42013-09-03 10:09:50 +02002796 case 'q':
2797 quiet = true;
2798 break;
2799 }
2800 }
2801
Max Reitz6f176b42013-09-03 10:09:50 +02002802 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002803 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02002804 }
2805
Kevin Wolfa283cb62014-02-21 16:24:07 +01002806 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
2807 if (fmt && has_help_option(options)) {
2808 /* If a format is explicitly specified (and possibly no filename is
2809 * given), print option help here */
2810 ret = print_block_option_help(filename, fmt);
2811 goto out;
2812 }
2813
2814 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002815 error_exit("Expecting one image file name");
Kevin Wolfa283cb62014-02-21 16:24:07 +01002816 }
Max Reitz6f176b42013-09-03 10:09:50 +02002817
Max Reitzbd39e6e2014-07-22 22:58:43 +02002818 flags = BDRV_O_FLAGS | BDRV_O_RDWR;
2819 ret = bdrv_parse_cache_flags(cache, &flags);
2820 if (ret < 0) {
2821 error_report("Invalid cache option: %s", cache);
2822 goto out;
2823 }
2824
2825 bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
Max Reitz6f176b42013-09-03 10:09:50 +02002826 if (!bs) {
2827 error_report("Could not open image '%s'", filename);
2828 ret = -1;
2829 goto out;
2830 }
2831
2832 fmt = bs->drv->format_name;
2833
Kevin Wolf626f84f2014-02-21 16:24:06 +01002834 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01002835 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02002836 ret = print_block_option_help(filename, fmt);
2837 goto out;
2838 }
2839
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002840 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08002841 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
2842 if (options && qemu_opts_do_parse(opts, options, NULL)) {
Max Reitz6f176b42013-09-03 10:09:50 +02002843 error_report("Invalid options for file format '%s'", fmt);
2844 ret = -1;
2845 goto out;
2846 }
2847
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002848 ret = bdrv_amend_options(bs, opts);
Max Reitz6f176b42013-09-03 10:09:50 +02002849 if (ret < 0) {
2850 error_report("Error while amending options: %s", strerror(-ret));
2851 goto out;
2852 }
2853
2854out:
2855 if (bs) {
2856 bdrv_unref(bs);
2857 }
Chunyan Liu83d05212014-06-05 17:20:51 +08002858 qemu_opts_del(opts);
2859 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01002860 g_free(options);
2861
Max Reitz6f176b42013-09-03 10:09:50 +02002862 if (ret) {
2863 return 1;
2864 }
2865 return 0;
2866}
2867
Anthony Liguoric227f092009-10-01 16:12:16 -05002868static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01002869#define DEF(option, callback, arg_string) \
2870 { option, callback },
2871#include "qemu-img-cmds.h"
2872#undef DEF
2873#undef GEN_DOCS
2874 { NULL, NULL, },
2875};
2876
bellardea2384d2004-08-01 21:59:26 +00002877int main(int argc, char **argv)
2878{
Anthony Liguoric227f092009-10-01 16:12:16 -05002879 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01002880 const char *cmdname;
Jeff Cody7db16892014-04-25 17:02:32 -04002881 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04002882 static const struct option long_options[] = {
2883 {"help", no_argument, 0, 'h'},
Jeff Cody5f6979c2014-04-28 14:37:18 -04002884 {"version", no_argument, 0, 'v'},
Jeff Cody7db16892014-04-25 17:02:32 -04002885 {0, 0, 0, 0}
2886 };
bellardea2384d2004-08-01 21:59:26 +00002887
MORITA Kazutaka526eda12013-07-23 17:30:11 +09002888#ifdef CONFIG_POSIX
2889 signal(SIGPIPE, SIG_IGN);
2890#endif
2891
Kevin Wolf53f76e52010-12-16 15:10:32 +01002892 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08002893 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01002894
Paolo Bonzini2592c592012-11-03 18:10:17 +01002895 qemu_init_main_loop();
bellardea2384d2004-08-01 21:59:26 +00002896 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08002897 if (argc < 2) {
2898 error_exit("Not enough arguments");
2899 }
Stuart Brady153859b2009-06-07 00:42:17 +01002900 cmdname = argv[1];
Stuart Brady153859b2009-06-07 00:42:17 +01002901
2902 /* find the command */
Jeff Cody5f6979c2014-04-28 14:37:18 -04002903 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
Stuart Brady153859b2009-06-07 00:42:17 +01002904 if (!strcmp(cmdname, cmd->name)) {
Jeff Cody7db16892014-04-25 17:02:32 -04002905 return cmd->handler(argc - 1, argv + 1);
Stuart Brady153859b2009-06-07 00:42:17 +01002906 }
bellardea2384d2004-08-01 21:59:26 +00002907 }
Stuart Brady153859b2009-06-07 00:42:17 +01002908
Jeff Cody5f6979c2014-04-28 14:37:18 -04002909 c = getopt_long(argc, argv, "h", long_options, NULL);
Jeff Cody7db16892014-04-25 17:02:32 -04002910
2911 if (c == 'h') {
2912 help();
2913 }
Jeff Cody5f6979c2014-04-28 14:37:18 -04002914 if (c == 'v') {
2915 printf(QEMU_IMG_VERSION);
2916 return 0;
2917 }
Jeff Cody7db16892014-04-25 17:02:32 -04002918
Stuart Brady153859b2009-06-07 00:42:17 +01002919 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08002920 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00002921}