]>
Commit | Line | Data |
---|---|---|
ea2384d3 | 1 | /* |
fb43f4dd | 2 | * QEMU disk image utility |
5fafdf24 | 3 | * |
68d0f70e | 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
5fafdf24 | 5 | * |
ea2384d3 FB |
6 | * 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 | */ | |
80c71a24 | 24 | #include "qemu/osdep.h" |
da34e65c | 25 | #include "qapi/error.h" |
c054b3fd BC |
26 | #include "qapi-visit.h" |
27 | #include "qapi/qmp-output-visitor.h" | |
cc7a8ea7 | 28 | #include "qapi/qmp/qerror.h" |
7b1b5d19 | 29 | #include "qapi/qmp/qjson.h" |
f348b6d1 | 30 | #include "qemu/cutils.h" |
3babeb15 | 31 | #include "qemu/config-file.h" |
1de7afc9 PB |
32 | #include "qemu/option.h" |
33 | #include "qemu/error-report.h" | |
3babeb15 | 34 | #include "qom/object_interfaces.h" |
9c17d615 | 35 | #include "sysemu/sysemu.h" |
26f54e9a | 36 | #include "sysemu/block-backend.h" |
737e150e | 37 | #include "block/block_int.h" |
d4a3238a | 38 | #include "block/blockjob.h" |
f364ec65 | 39 | #include "block/qapi.h" |
c054b3fd | 40 | #include <getopt.h> |
e8445331 | 41 | |
61979a6a | 42 | #define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \ |
5f6979cb JC |
43 | ", Copyright (c) 2004-2008 Fabrice Bellard\n" |
44 | ||
c227f099 | 45 | typedef struct img_cmd_t { |
153859be SB |
46 | const char *name; |
47 | int (*handler)(int argc, char **argv); | |
c227f099 | 48 | } img_cmd_t; |
153859be | 49 | |
8599ea4c FS |
50 | enum { |
51 | OPTION_OUTPUT = 256, | |
52 | OPTION_BACKING_CHAIN = 257, | |
3babeb15 | 53 | OPTION_OBJECT = 258, |
eb769f74 | 54 | OPTION_IMAGE_OPTS = 259, |
8599ea4c FS |
55 | }; |
56 | ||
57 | typedef enum OutputFormat { | |
58 | OFORMAT_JSON, | |
59 | OFORMAT_HUMAN, | |
60 | } OutputFormat; | |
61 | ||
e6996143 | 62 | /* Default to cache=writeback as data integrity is not important for qemu-img */ |
661a0f71 | 63 | #define BDRV_DEFAULT_CACHE "writeback" |
137519ce | 64 | |
00c6d403 | 65 | static void format_print(void *opaque, const char *name) |
ea2384d3 | 66 | { |
00c6d403 | 67 | printf(" %s", name); |
ea2384d3 FB |
68 | } |
69 | ||
ac1307ab FZ |
70 | static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...) |
71 | { | |
72 | va_list ap; | |
73 | ||
74 | error_printf("qemu-img: "); | |
75 | ||
76 | va_start(ap, fmt); | |
77 | error_vprintf(fmt, ap); | |
78 | va_end(ap); | |
79 | ||
80 | error_printf("\nTry 'qemu-img --help' for more information\n"); | |
81 | exit(EXIT_FAILURE); | |
82 | } | |
83 | ||
d2c639d6 | 84 | /* Please keep in synch with qemu-img.texi */ |
ac1307ab | 85 | static void QEMU_NORETURN help(void) |
ea2384d3 | 86 | { |
e00291c0 | 87 | const char *help_msg = |
5f6979cb | 88 | QEMU_IMG_VERSION |
3f020d70 | 89 | "usage: qemu-img command [command options]\n" |
90 | "QEMU disk image utility\n" | |
91 | "\n" | |
92 | "Command syntax:\n" | |
153859be SB |
93 | #define DEF(option, callback, arg_string) \ |
94 | " " arg_string "\n" | |
95 | #include "qemu-img-cmds.h" | |
96 | #undef DEF | |
97 | #undef GEN_DOCS | |
3f020d70 | 98 | "\n" |
99 | "Command parameters:\n" | |
100 | " 'filename' is a disk image filename\n" | |
3babeb15 DB |
101 | " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n" |
102 | " manual page for a description of the object properties. The most common\n" | |
103 | " object type is a 'secret', which is used to supply passwords and/or\n" | |
104 | " encryption keys.\n" | |
3f020d70 | 105 | " 'fmt' is the disk image format. It is guessed automatically in most cases\n" |
661a0f71 | 106 | " 'cache' is the cache mode used to write the output disk image, the valid\n" |
80ccf93b LY |
107 | " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n" |
108 | " 'directsync' and 'unsafe' (default for convert)\n" | |
bb87fdf8 SH |
109 | " 'src_cache' is the cache mode used to read input disk images, the valid\n" |
110 | " options are the same as for the 'cache' option\n" | |
3f020d70 | 111 | " 'size' is the disk image size in bytes. Optional suffixes\n" |
5e00984a KW |
112 | " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n" |
113 | " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n" | |
114 | " supported. 'b' is ignored.\n" | |
3f020d70 | 115 | " 'output_filename' is the destination disk image filename\n" |
116 | " 'output_fmt' is the destination format\n" | |
117 | " 'options' is a comma separated list of format specific options in a\n" | |
118 | " name=value format. Use -o ? for an overview of the options supported by the\n" | |
119 | " used format\n" | |
ef80654d WX |
120 | " 'snapshot_param' is param used for internal snapshot, format\n" |
121 | " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n" | |
122 | " '[ID_OR_NAME]'\n" | |
123 | " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n" | |
124 | " instead\n" | |
3f020d70 | 125 | " '-c' indicates that target image must be compressed (qcow format only)\n" |
126 | " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n" | |
127 | " match exactly. The image doesn't need a working backing file before\n" | |
128 | " rebasing in this case (useful for renaming the backing file)\n" | |
129 | " '-h' with or without a command shows this help and lists the supported formats\n" | |
6b837bc4 | 130 | " '-p' show progress of command (only certain commands)\n" |
f382d43a | 131 | " '-q' use Quiet mode - do not print any output (except errors)\n" |
11b6699a PL |
132 | " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n" |
133 | " contain only zeros for qemu-img to create a sparse image during\n" | |
134 | " conversion. If the number of bytes is 0, the source will not be scanned for\n" | |
135 | " unallocated or zero sectors, and the destination image will always be\n" | |
136 | " fully allocated\n" | |
c054b3fd | 137 | " '--output' takes the format in which the output must be done (human or json)\n" |
b2e10493 AD |
138 | " '-n' skips the target volume creation (useful if the volume is created\n" |
139 | " prior to running qemu-img)\n" | |
3f020d70 | 140 | "\n" |
4534ff54 KW |
141 | "Parameters to check subcommand:\n" |
142 | " '-r' tries to repair any inconsistencies that are found during the check.\n" | |
143 | " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n" | |
144 | " kinds of errors, with a higher risk of choosing the wrong fix or\n" | |
0546b8c2 | 145 | " hiding corruption that has already occurred.\n" |
4534ff54 | 146 | "\n" |
3f020d70 | 147 | "Parameters to snapshot subcommand:\n" |
148 | " 'snapshot' is the name of the snapshot to create, apply or delete\n" | |
149 | " '-a' applies a snapshot (revert disk to saved state)\n" | |
150 | " '-c' creates a snapshot\n" | |
151 | " '-d' deletes a snapshot\n" | |
d14ed18c MR |
152 | " '-l' lists all snapshots in the given image\n" |
153 | "\n" | |
154 | "Parameters to compare subcommand:\n" | |
155 | " '-f' first image format\n" | |
156 | " '-F' second image format\n" | |
157 | " '-s' run in Strict mode - fail on different image size or sector allocation\n"; | |
e00291c0 PB |
158 | |
159 | printf("%s\nSupported formats:", help_msg); | |
00c6d403 | 160 | bdrv_iterate_format(format_print, NULL); |
ea2384d3 | 161 | printf("\n"); |
ac1307ab | 162 | exit(EXIT_SUCCESS); |
ea2384d3 FB |
163 | } |
164 | ||
3babeb15 DB |
165 | static QemuOptsList qemu_object_opts = { |
166 | .name = "object", | |
167 | .implied_opt_name = "qom-type", | |
168 | .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head), | |
169 | .desc = { | |
170 | { } | |
171 | }, | |
172 | }; | |
173 | ||
eb769f74 DB |
174 | static QemuOptsList qemu_source_opts = { |
175 | .name = "source", | |
176 | .implied_opt_name = "file", | |
177 | .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head), | |
178 | .desc = { | |
179 | { } | |
180 | }, | |
181 | }; | |
182 | ||
7c30f657 | 183 | static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...) |
f382d43a MR |
184 | { |
185 | int ret = 0; | |
186 | if (!quiet) { | |
187 | va_list args; | |
188 | va_start(args, fmt); | |
189 | ret = vprintf(fmt, args); | |
190 | va_end(args); | |
191 | } | |
192 | return ret; | |
193 | } | |
194 | ||
ea2384d3 | 195 | |
4ac8aacd JS |
196 | static int print_block_option_help(const char *filename, const char *fmt) |
197 | { | |
198 | BlockDriver *drv, *proto_drv; | |
83d0521a | 199 | QemuOptsList *create_opts = NULL; |
b65a5e12 | 200 | Error *local_err = NULL; |
4ac8aacd JS |
201 | |
202 | /* Find driver and parse its options */ | |
203 | drv = bdrv_find_format(fmt); | |
204 | if (!drv) { | |
15654a6d | 205 | error_report("Unknown file format '%s'", fmt); |
4ac8aacd JS |
206 | return 1; |
207 | } | |
208 | ||
c282e1fd | 209 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
a283cb6e | 210 | if (filename) { |
b65a5e12 | 211 | proto_drv = bdrv_find_protocol(filename, true, &local_err); |
a283cb6e | 212 | if (!proto_drv) { |
2867ce4a | 213 | error_report_err(local_err); |
83d0521a | 214 | qemu_opts_free(create_opts); |
a283cb6e KW |
215 | return 1; |
216 | } | |
c282e1fd | 217 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
a283cb6e KW |
218 | } |
219 | ||
83d0521a CL |
220 | qemu_opts_print_help(create_opts); |
221 | qemu_opts_free(create_opts); | |
4ac8aacd JS |
222 | return 0; |
223 | } | |
224 | ||
eb769f74 DB |
225 | |
226 | static int img_open_password(BlockBackend *blk, const char *filename, | |
abb06c5a | 227 | int flags, bool quiet) |
75c23805 FB |
228 | { |
229 | BlockDriverState *bs; | |
75c23805 | 230 | char password[256]; |
eb769f74 DB |
231 | |
232 | bs = blk_bs(blk); | |
4ef130fc DB |
233 | if (bdrv_is_encrypted(bs) && bdrv_key_required(bs) && |
234 | !(flags & BDRV_O_NO_IO)) { | |
eb769f74 DB |
235 | qprintf(quiet, "Disk image '%s' is encrypted.\n", filename); |
236 | if (qemu_read_password(password, sizeof(password)) < 0) { | |
237 | error_report("No password given"); | |
238 | return -1; | |
239 | } | |
240 | if (bdrv_set_key(bs, password) < 0) { | |
241 | error_report("invalid password"); | |
242 | return -1; | |
243 | } | |
244 | } | |
245 | return 0; | |
246 | } | |
247 | ||
248 | ||
efaa7c4e | 249 | static BlockBackend *img_open_opts(const char *optstr, |
ce099547 | 250 | QemuOpts *opts, int flags, bool writethrough, |
abb06c5a | 251 | bool quiet) |
eb769f74 DB |
252 | { |
253 | QDict *options; | |
254 | Error *local_err = NULL; | |
255 | BlockBackend *blk; | |
256 | options = qemu_opts_to_qdict(opts, NULL); | |
efaa7c4e | 257 | blk = blk_new_open(NULL, NULL, options, flags, &local_err); |
eb769f74 DB |
258 | if (!blk) { |
259 | error_reportf_err(local_err, "Could not open '%s'", optstr); | |
260 | return NULL; | |
261 | } | |
ce099547 | 262 | blk_set_enable_write_cache(blk, !writethrough); |
eb769f74 | 263 | |
abb06c5a | 264 | if (img_open_password(blk, optstr, flags, quiet) < 0) { |
eb769f74 DB |
265 | blk_unref(blk); |
266 | return NULL; | |
267 | } | |
268 | return blk; | |
269 | } | |
270 | ||
efaa7c4e | 271 | static BlockBackend *img_open_file(const char *filename, |
eb769f74 | 272 | const char *fmt, int flags, |
ce099547 | 273 | bool writethrough, bool quiet) |
eb769f74 DB |
274 | { |
275 | BlockBackend *blk; | |
34b5d2c6 | 276 | Error *local_err = NULL; |
5bd31326 | 277 | QDict *options = NULL; |
ad717139 | 278 | |
75c23805 | 279 | if (fmt) { |
5bd31326 HR |
280 | options = qdict_new(); |
281 | qdict_put(options, "driver", qstring_from_str(fmt)); | |
75c23805 | 282 | } |
b9eaf9ec | 283 | |
efaa7c4e | 284 | blk = blk_new_open(filename, NULL, options, flags, &local_err); |
5bd31326 | 285 | if (!blk) { |
c29b77f9 | 286 | error_reportf_err(local_err, "Could not open '%s': ", filename); |
eb769f74 | 287 | return NULL; |
75c23805 | 288 | } |
ce099547 | 289 | blk_set_enable_write_cache(blk, !writethrough); |
b9eaf9ec | 290 | |
abb06c5a | 291 | if (img_open_password(blk, filename, flags, quiet) < 0) { |
eb769f74 DB |
292 | blk_unref(blk); |
293 | return NULL; | |
294 | } | |
295 | return blk; | |
296 | } | |
297 | ||
298 | ||
efaa7c4e | 299 | static BlockBackend *img_open(bool image_opts, |
eb769f74 | 300 | const char *filename, |
ce099547 | 301 | const char *fmt, int flags, bool writethrough, |
abb06c5a | 302 | bool quiet) |
eb769f74 DB |
303 | { |
304 | BlockBackend *blk; | |
305 | if (image_opts) { | |
306 | QemuOpts *opts; | |
307 | if (fmt) { | |
308 | error_report("--image-opts and --format are mutually exclusive"); | |
309 | return NULL; | |
310 | } | |
311 | opts = qemu_opts_parse_noisily(qemu_find_opts("source"), | |
312 | filename, true); | |
313 | if (!opts) { | |
314 | return NULL; | |
315 | } | |
ce099547 | 316 | blk = img_open_opts(filename, opts, flags, writethrough, quiet); |
eb769f74 | 317 | } else { |
ce099547 | 318 | blk = img_open_file(filename, fmt, flags, writethrough, quiet); |
75c23805 | 319 | } |
7e7d56d9 | 320 | return blk; |
75c23805 FB |
321 | } |
322 | ||
eb769f74 | 323 | |
83d0521a | 324 | static int add_old_style_options(const char *fmt, QemuOpts *opts, |
eec77d9e JS |
325 | const char *base_filename, |
326 | const char *base_fmt) | |
efa84d43 | 327 | { |
6750e795 MA |
328 | Error *err = NULL; |
329 | ||
efa84d43 | 330 | if (base_filename) { |
f43e47db | 331 | qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err); |
6750e795 | 332 | if (err) { |
15654a6d JS |
333 | error_report("Backing file not supported for file format '%s'", |
334 | fmt); | |
6750e795 | 335 | error_free(err); |
c2abccec | 336 | return -1; |
efa84d43 KW |
337 | } |
338 | } | |
339 | if (base_fmt) { | |
f43e47db | 340 | qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err); |
6750e795 | 341 | if (err) { |
15654a6d JS |
342 | error_report("Backing file format not supported for file " |
343 | "format '%s'", fmt); | |
6750e795 | 344 | error_free(err); |
c2abccec | 345 | return -1; |
efa84d43 KW |
346 | } |
347 | } | |
c2abccec | 348 | return 0; |
efa84d43 KW |
349 | } |
350 | ||
ea2384d3 FB |
351 | static int img_create(int argc, char **argv) |
352 | { | |
a9300911 | 353 | int c; |
1da7cfbd | 354 | uint64_t img_size = -1; |
ea2384d3 | 355 | const char *fmt = "raw"; |
9230eaf6 | 356 | const char *base_fmt = NULL; |
ea2384d3 FB |
357 | const char *filename; |
358 | const char *base_filename = NULL; | |
9ea2ea71 | 359 | char *options = NULL; |
9b37525a | 360 | Error *local_err = NULL; |
f382d43a | 361 | bool quiet = false; |
3b46e624 | 362 | |
ea2384d3 | 363 | for(;;) { |
3babeb15 DB |
364 | static const struct option long_options[] = { |
365 | {"help", no_argument, 0, 'h'}, | |
366 | {"object", required_argument, 0, OPTION_OBJECT}, | |
367 | {0, 0, 0, 0} | |
368 | }; | |
369 | c = getopt_long(argc, argv, "F:b:f:he6o:q", | |
370 | long_options, NULL); | |
b8fb60da | 371 | if (c == -1) { |
ea2384d3 | 372 | break; |
b8fb60da | 373 | } |
ea2384d3 | 374 | switch(c) { |
ef87394c | 375 | case '?': |
ea2384d3 FB |
376 | case 'h': |
377 | help(); | |
378 | break; | |
9230eaf6 AL |
379 | case 'F': |
380 | base_fmt = optarg; | |
381 | break; | |
ea2384d3 FB |
382 | case 'b': |
383 | base_filename = optarg; | |
384 | break; | |
385 | case 'f': | |
386 | fmt = optarg; | |
387 | break; | |
388 | case 'e': | |
9d42e15d | 389 | error_report("option -e is deprecated, please use \'-o " |
eec77d9e | 390 | "encryption\' instead!"); |
77386bf6 | 391 | goto fail; |
d8871c5a | 392 | case '6': |
9d42e15d | 393 | error_report("option -6 is deprecated, please use \'-o " |
eec77d9e | 394 | "compat6\' instead!"); |
77386bf6 | 395 | goto fail; |
9ea2ea71 | 396 | case 'o': |
77386bf6 KW |
397 | if (!is_valid_option_list(optarg)) { |
398 | error_report("Invalid option list: %s", optarg); | |
399 | goto fail; | |
400 | } | |
401 | if (!options) { | |
402 | options = g_strdup(optarg); | |
403 | } else { | |
404 | char *old_options = options; | |
405 | options = g_strdup_printf("%s,%s", options, optarg); | |
406 | g_free(old_options); | |
407 | } | |
9ea2ea71 | 408 | break; |
f382d43a MR |
409 | case 'q': |
410 | quiet = true; | |
411 | break; | |
3babeb15 DB |
412 | case OPTION_OBJECT: { |
413 | QemuOpts *opts; | |
414 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
415 | optarg, true); | |
416 | if (!opts) { | |
417 | goto fail; | |
418 | } | |
419 | } break; | |
ea2384d3 FB |
420 | } |
421 | } | |
9230eaf6 | 422 | |
b50cbabc | 423 | /* Get the filename */ |
a283cb6e KW |
424 | filename = (optind < argc) ? argv[optind] : NULL; |
425 | if (options && has_help_option(options)) { | |
426 | g_free(options); | |
427 | return print_block_option_help(filename, fmt); | |
428 | } | |
429 | ||
b8fb60da | 430 | if (optind >= argc) { |
ac1307ab | 431 | error_exit("Expecting image file name"); |
b8fb60da | 432 | } |
a283cb6e | 433 | optind++; |
b50cbabc | 434 | |
3babeb15 DB |
435 | if (qemu_opts_foreach(&qemu_object_opts, |
436 | user_creatable_add_opts_foreach, | |
437 | NULL, &local_err)) { | |
438 | error_report_err(local_err); | |
439 | goto fail; | |
440 | } | |
441 | ||
1da7cfbd JS |
442 | /* Get image size, if specified */ |
443 | if (optind < argc) { | |
70b4f4bb | 444 | int64_t sval; |
e36b3695 | 445 | char *end; |
4677bb40 MAL |
446 | sval = qemu_strtosz_suffix(argv[optind++], &end, |
447 | QEMU_STRTOSZ_DEFSUFFIX_B); | |
e36b3695 | 448 | if (sval < 0 || *end) { |
79443397 LG |
449 | if (sval == -ERANGE) { |
450 | error_report("Image size must be less than 8 EiB!"); | |
451 | } else { | |
452 | error_report("Invalid image size specified! You may use k, M, " | |
5e00984a KW |
453 | "G, T, P or E suffixes for "); |
454 | error_report("kilobytes, megabytes, gigabytes, terabytes, " | |
455 | "petabytes and exabytes."); | |
79443397 | 456 | } |
77386bf6 | 457 | goto fail; |
1da7cfbd JS |
458 | } |
459 | img_size = (uint64_t)sval; | |
460 | } | |
fc11eb26 | 461 | if (optind != argc) { |
ac1307ab | 462 | error_exit("Unexpected argument: %s", argv[optind]); |
fc11eb26 | 463 | } |
1da7cfbd | 464 | |
9b37525a | 465 | bdrv_img_create(filename, fmt, base_filename, base_fmt, |
e6996143 | 466 | options, img_size, BDRV_O_CACHE_WB, &local_err, quiet); |
84d18f06 | 467 | if (local_err) { |
c29b77f9 | 468 | error_reportf_err(local_err, "%s: ", filename); |
77386bf6 | 469 | goto fail; |
c2abccec | 470 | } |
a9300911 | 471 | |
77386bf6 | 472 | g_free(options); |
ea2384d3 | 473 | return 0; |
77386bf6 KW |
474 | |
475 | fail: | |
476 | g_free(options); | |
477 | return 1; | |
ea2384d3 FB |
478 | } |
479 | ||
f382d43a | 480 | static void dump_json_image_check(ImageCheck *check, bool quiet) |
8599ea4c | 481 | { |
4399c438 | 482 | Error *local_err = NULL; |
8599ea4c FS |
483 | QString *str; |
484 | QmpOutputVisitor *ov = qmp_output_visitor_new(); | |
485 | QObject *obj; | |
51e72bc1 EB |
486 | visit_type_ImageCheck(qmp_output_get_visitor(ov), NULL, &check, |
487 | &local_err); | |
8599ea4c FS |
488 | obj = qmp_output_get_qobject(ov); |
489 | str = qobject_to_json_pretty(obj); | |
490 | assert(str != NULL); | |
f382d43a | 491 | qprintf(quiet, "%s\n", qstring_get_str(str)); |
8599ea4c FS |
492 | qobject_decref(obj); |
493 | qmp_output_visitor_cleanup(ov); | |
494 | QDECREF(str); | |
495 | } | |
496 | ||
f382d43a | 497 | static void dump_human_image_check(ImageCheck *check, bool quiet) |
8599ea4c FS |
498 | { |
499 | if (!(check->corruptions || check->leaks || check->check_errors)) { | |
f382d43a | 500 | qprintf(quiet, "No errors were found on the image.\n"); |
8599ea4c FS |
501 | } else { |
502 | if (check->corruptions) { | |
f382d43a MR |
503 | qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n" |
504 | "Data may be corrupted, or further writes to the image " | |
505 | "may corrupt it.\n", | |
506 | check->corruptions); | |
8599ea4c FS |
507 | } |
508 | ||
509 | if (check->leaks) { | |
f382d43a MR |
510 | qprintf(quiet, |
511 | "\n%" PRId64 " leaked clusters were found on the image.\n" | |
512 | "This means waste of disk space, but no harm to data.\n", | |
513 | check->leaks); | |
8599ea4c FS |
514 | } |
515 | ||
516 | if (check->check_errors) { | |
f382d43a MR |
517 | qprintf(quiet, |
518 | "\n%" PRId64 | |
519 | " internal errors have occurred during the check.\n", | |
520 | check->check_errors); | |
8599ea4c FS |
521 | } |
522 | } | |
523 | ||
524 | if (check->total_clusters != 0 && check->allocated_clusters != 0) { | |
f382d43a MR |
525 | qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, " |
526 | "%0.2f%% fragmented, %0.2f%% compressed clusters\n", | |
527 | check->allocated_clusters, check->total_clusters, | |
528 | check->allocated_clusters * 100.0 / check->total_clusters, | |
529 | check->fragmented_clusters * 100.0 / check->allocated_clusters, | |
530 | check->compressed_clusters * 100.0 / | |
531 | check->allocated_clusters); | |
8599ea4c FS |
532 | } |
533 | ||
534 | if (check->image_end_offset) { | |
f382d43a MR |
535 | qprintf(quiet, |
536 | "Image end offset: %" PRId64 "\n", check->image_end_offset); | |
8599ea4c FS |
537 | } |
538 | } | |
539 | ||
540 | static int collect_image_check(BlockDriverState *bs, | |
541 | ImageCheck *check, | |
542 | const char *filename, | |
543 | const char *fmt, | |
544 | int fix) | |
545 | { | |
546 | int ret; | |
547 | BdrvCheckResult result; | |
548 | ||
549 | ret = bdrv_check(bs, &result, fix); | |
550 | if (ret < 0) { | |
551 | return ret; | |
552 | } | |
553 | ||
554 | check->filename = g_strdup(filename); | |
555 | check->format = g_strdup(bdrv_get_format_name(bs)); | |
556 | check->check_errors = result.check_errors; | |
557 | check->corruptions = result.corruptions; | |
558 | check->has_corruptions = result.corruptions != 0; | |
559 | check->leaks = result.leaks; | |
560 | check->has_leaks = result.leaks != 0; | |
561 | check->corruptions_fixed = result.corruptions_fixed; | |
562 | check->has_corruptions_fixed = result.corruptions != 0; | |
563 | check->leaks_fixed = result.leaks_fixed; | |
564 | check->has_leaks_fixed = result.leaks != 0; | |
565 | check->image_end_offset = result.image_end_offset; | |
566 | check->has_image_end_offset = result.image_end_offset != 0; | |
567 | check->total_clusters = result.bfi.total_clusters; | |
568 | check->has_total_clusters = result.bfi.total_clusters != 0; | |
569 | check->allocated_clusters = result.bfi.allocated_clusters; | |
570 | check->has_allocated_clusters = result.bfi.allocated_clusters != 0; | |
571 | check->fragmented_clusters = result.bfi.fragmented_clusters; | |
572 | check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0; | |
e6439d78 SH |
573 | check->compressed_clusters = result.bfi.compressed_clusters; |
574 | check->has_compressed_clusters = result.bfi.compressed_clusters != 0; | |
8599ea4c FS |
575 | |
576 | return 0; | |
577 | } | |
578 | ||
e076f338 KW |
579 | /* |
580 | * Checks an image for consistency. Exit codes: | |
581 | * | |
d6635c4d HR |
582 | * 0 - Check completed, image is good |
583 | * 1 - Check not completed because of internal errors | |
584 | * 2 - Check completed, image is corrupted | |
585 | * 3 - Check completed, image has leaked clusters, but is good otherwise | |
586 | * 63 - Checks are not supported by the image format | |
e076f338 | 587 | */ |
1585969c AL |
588 | static int img_check(int argc, char **argv) |
589 | { | |
590 | int c, ret; | |
8599ea4c | 591 | OutputFormat output_format = OFORMAT_HUMAN; |
40055951 | 592 | const char *filename, *fmt, *output, *cache; |
26f54e9a | 593 | BlockBackend *blk; |
1585969c | 594 | BlockDriverState *bs; |
4534ff54 | 595 | int fix = 0; |
ce099547 KW |
596 | int flags = BDRV_O_CHECK; |
597 | bool writethrough; | |
7e7d56d9 | 598 | ImageCheck *check; |
f382d43a | 599 | bool quiet = false; |
3babeb15 | 600 | Error *local_err = NULL; |
eb769f74 | 601 | bool image_opts = false; |
1585969c AL |
602 | |
603 | fmt = NULL; | |
8599ea4c | 604 | output = NULL; |
40055951 | 605 | cache = BDRV_DEFAULT_CACHE; |
ce099547 | 606 | |
1585969c | 607 | for(;;) { |
8599ea4c FS |
608 | int option_index = 0; |
609 | static const struct option long_options[] = { | |
610 | {"help", no_argument, 0, 'h'}, | |
611 | {"format", required_argument, 0, 'f'}, | |
4fd6a984 | 612 | {"repair", required_argument, 0, 'r'}, |
8599ea4c | 613 | {"output", required_argument, 0, OPTION_OUTPUT}, |
3babeb15 | 614 | {"object", required_argument, 0, OPTION_OBJECT}, |
eb769f74 | 615 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
8599ea4c FS |
616 | {0, 0, 0, 0} |
617 | }; | |
40055951 | 618 | c = getopt_long(argc, argv, "hf:r:T:q", |
8599ea4c | 619 | long_options, &option_index); |
b8fb60da | 620 | if (c == -1) { |
1585969c | 621 | break; |
b8fb60da | 622 | } |
1585969c | 623 | switch(c) { |
ef87394c | 624 | case '?': |
1585969c AL |
625 | case 'h': |
626 | help(); | |
627 | break; | |
628 | case 'f': | |
629 | fmt = optarg; | |
630 | break; | |
4534ff54 KW |
631 | case 'r': |
632 | flags |= BDRV_O_RDWR; | |
633 | ||
634 | if (!strcmp(optarg, "leaks")) { | |
635 | fix = BDRV_FIX_LEAKS; | |
636 | } else if (!strcmp(optarg, "all")) { | |
637 | fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS; | |
638 | } else { | |
ac1307ab FZ |
639 | error_exit("Unknown option value for -r " |
640 | "(expecting 'leaks' or 'all'): %s", optarg); | |
4534ff54 KW |
641 | } |
642 | break; | |
8599ea4c FS |
643 | case OPTION_OUTPUT: |
644 | output = optarg; | |
645 | break; | |
40055951 HR |
646 | case 'T': |
647 | cache = optarg; | |
648 | break; | |
f382d43a MR |
649 | case 'q': |
650 | quiet = true; | |
651 | break; | |
3babeb15 DB |
652 | case OPTION_OBJECT: { |
653 | QemuOpts *opts; | |
654 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
655 | optarg, true); | |
656 | if (!opts) { | |
657 | return 1; | |
658 | } | |
659 | } break; | |
eb769f74 DB |
660 | case OPTION_IMAGE_OPTS: |
661 | image_opts = true; | |
662 | break; | |
1585969c AL |
663 | } |
664 | } | |
fc11eb26 | 665 | if (optind != argc - 1) { |
ac1307ab | 666 | error_exit("Expecting one image file name"); |
b8fb60da | 667 | } |
1585969c AL |
668 | filename = argv[optind++]; |
669 | ||
8599ea4c FS |
670 | if (output && !strcmp(output, "json")) { |
671 | output_format = OFORMAT_JSON; | |
672 | } else if (output && !strcmp(output, "human")) { | |
673 | output_format = OFORMAT_HUMAN; | |
674 | } else if (output) { | |
675 | error_report("--output must be used with human or json as argument."); | |
676 | return 1; | |
677 | } | |
678 | ||
3babeb15 DB |
679 | if (qemu_opts_foreach(&qemu_object_opts, |
680 | user_creatable_add_opts_foreach, | |
681 | NULL, &local_err)) { | |
682 | error_report_err(local_err); | |
683 | return 1; | |
684 | } | |
685 | ||
ce099547 | 686 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
40055951 HR |
687 | if (ret < 0) { |
688 | error_report("Invalid source cache option: %s", cache); | |
689 | return 1; | |
690 | } | |
691 | ||
ce099547 | 692 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet); |
7e7d56d9 MA |
693 | if (!blk) { |
694 | return 1; | |
c2abccec | 695 | } |
7e7d56d9 | 696 | bs = blk_bs(blk); |
8599ea4c FS |
697 | |
698 | check = g_new0(ImageCheck, 1); | |
699 | ret = collect_image_check(bs, check, filename, fmt, fix); | |
e076f338 KW |
700 | |
701 | if (ret == -ENOTSUP) { | |
55d492d7 | 702 | error_report("This image format does not support checks"); |
fefddf95 | 703 | ret = 63; |
8599ea4c | 704 | goto fail; |
e076f338 KW |
705 | } |
706 | ||
8599ea4c FS |
707 | if (check->corruptions_fixed || check->leaks_fixed) { |
708 | int corruptions_fixed, leaks_fixed; | |
ccf34716 | 709 | |
8599ea4c FS |
710 | leaks_fixed = check->leaks_fixed; |
711 | corruptions_fixed = check->corruptions_fixed; | |
e076f338 | 712 | |
8599ea4c | 713 | if (output_format == OFORMAT_HUMAN) { |
f382d43a MR |
714 | qprintf(quiet, |
715 | "The following inconsistencies were found and repaired:\n\n" | |
716 | " %" PRId64 " leaked clusters\n" | |
717 | " %" PRId64 " corruptions\n\n" | |
718 | "Double checking the fixed image now...\n", | |
719 | check->leaks_fixed, | |
720 | check->corruptions_fixed); | |
e076f338 KW |
721 | } |
722 | ||
8599ea4c | 723 | ret = collect_image_check(bs, check, filename, fmt, 0); |
1585969c | 724 | |
8599ea4c FS |
725 | check->leaks_fixed = leaks_fixed; |
726 | check->corruptions_fixed = corruptions_fixed; | |
f8111c24 DXW |
727 | } |
728 | ||
832390a5 HR |
729 | if (!ret) { |
730 | switch (output_format) { | |
731 | case OFORMAT_HUMAN: | |
732 | dump_human_image_check(check, quiet); | |
733 | break; | |
734 | case OFORMAT_JSON: | |
735 | dump_json_image_check(check, quiet); | |
736 | break; | |
737 | } | |
c6bb9ad1 FS |
738 | } |
739 | ||
8599ea4c | 740 | if (ret || check->check_errors) { |
832390a5 HR |
741 | if (ret) { |
742 | error_report("Check failed: %s", strerror(-ret)); | |
743 | } else { | |
744 | error_report("Check failed"); | |
745 | } | |
8599ea4c FS |
746 | ret = 1; |
747 | goto fail; | |
c2abccec | 748 | } |
e076f338 | 749 | |
8599ea4c FS |
750 | if (check->corruptions) { |
751 | ret = 2; | |
752 | } else if (check->leaks) { | |
753 | ret = 3; | |
e076f338 | 754 | } else { |
8599ea4c | 755 | ret = 0; |
e076f338 | 756 | } |
8599ea4c FS |
757 | |
758 | fail: | |
759 | qapi_free_ImageCheck(check); | |
26f54e9a | 760 | blk_unref(blk); |
8599ea4c | 761 | return ret; |
1585969c AL |
762 | } |
763 | ||
d4a3238a HR |
764 | typedef struct CommonBlockJobCBInfo { |
765 | BlockDriverState *bs; | |
766 | Error **errp; | |
767 | } CommonBlockJobCBInfo; | |
768 | ||
769 | static void common_block_job_cb(void *opaque, int ret) | |
770 | { | |
771 | CommonBlockJobCBInfo *cbi = opaque; | |
772 | ||
773 | if (ret < 0) { | |
774 | error_setg_errno(cbi->errp, -ret, "Block job failed"); | |
775 | } | |
d4a3238a HR |
776 | } |
777 | ||
778 | static void run_block_job(BlockJob *job, Error **errp) | |
779 | { | |
780 | AioContext *aio_context = bdrv_get_aio_context(job->bs); | |
781 | ||
782 | do { | |
783 | aio_poll(aio_context, true); | |
62547b8a JS |
784 | qemu_progress_print(job->len ? |
785 | ((float)job->offset / job->len * 100.f) : 0.0f, 0); | |
d4a3238a HR |
786 | } while (!job->ready); |
787 | ||
788 | block_job_complete_sync(job, errp); | |
687fa1d8 HR |
789 | |
790 | /* A block job may finish instantaneously without publishing any progress, | |
791 | * so just signal completion here */ | |
792 | qemu_progress_print(100.f, 0); | |
d4a3238a HR |
793 | } |
794 | ||
ea2384d3 FB |
795 | static int img_commit(int argc, char **argv) |
796 | { | |
661a0f71 | 797 | int c, ret, flags; |
1b22bffd | 798 | const char *filename, *fmt, *cache, *base; |
26f54e9a | 799 | BlockBackend *blk; |
d4a3238a | 800 | BlockDriverState *bs, *base_bs; |
687fa1d8 | 801 | bool progress = false, quiet = false, drop = false; |
ce099547 | 802 | bool writethrough; |
d4a3238a HR |
803 | Error *local_err = NULL; |
804 | CommonBlockJobCBInfo cbi; | |
eb769f74 | 805 | bool image_opts = false; |
ea2384d3 FB |
806 | |
807 | fmt = NULL; | |
661a0f71 | 808 | cache = BDRV_DEFAULT_CACHE; |
1b22bffd | 809 | base = NULL; |
ea2384d3 | 810 | for(;;) { |
3babeb15 DB |
811 | static const struct option long_options[] = { |
812 | {"help", no_argument, 0, 'h'}, | |
813 | {"object", required_argument, 0, OPTION_OBJECT}, | |
eb769f74 | 814 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
3babeb15 DB |
815 | {0, 0, 0, 0} |
816 | }; | |
817 | c = getopt_long(argc, argv, "f:ht:b:dpq", | |
818 | long_options, NULL); | |
b8fb60da | 819 | if (c == -1) { |
ea2384d3 | 820 | break; |
b8fb60da | 821 | } |
ea2384d3 | 822 | switch(c) { |
ef87394c | 823 | case '?': |
ea2384d3 FB |
824 | case 'h': |
825 | help(); | |
826 | break; | |
827 | case 'f': | |
828 | fmt = optarg; | |
829 | break; | |
661a0f71 FS |
830 | case 't': |
831 | cache = optarg; | |
832 | break; | |
1b22bffd HR |
833 | case 'b': |
834 | base = optarg; | |
835 | /* -b implies -d */ | |
836 | drop = true; | |
837 | break; | |
9a86fe48 HR |
838 | case 'd': |
839 | drop = true; | |
840 | break; | |
687fa1d8 HR |
841 | case 'p': |
842 | progress = true; | |
843 | break; | |
f382d43a MR |
844 | case 'q': |
845 | quiet = true; | |
846 | break; | |
3babeb15 DB |
847 | case OPTION_OBJECT: { |
848 | QemuOpts *opts; | |
849 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
850 | optarg, true); | |
851 | if (!opts) { | |
852 | return 1; | |
853 | } | |
854 | } break; | |
eb769f74 DB |
855 | case OPTION_IMAGE_OPTS: |
856 | image_opts = true; | |
857 | break; | |
ea2384d3 FB |
858 | } |
859 | } | |
687fa1d8 HR |
860 | |
861 | /* Progress is not shown in Quiet mode */ | |
862 | if (quiet) { | |
863 | progress = false; | |
864 | } | |
865 | ||
fc11eb26 | 866 | if (optind != argc - 1) { |
ac1307ab | 867 | error_exit("Expecting one image file name"); |
b8fb60da | 868 | } |
ea2384d3 FB |
869 | filename = argv[optind++]; |
870 | ||
3babeb15 DB |
871 | if (qemu_opts_foreach(&qemu_object_opts, |
872 | user_creatable_add_opts_foreach, | |
873 | NULL, &local_err)) { | |
874 | error_report_err(local_err); | |
875 | return 1; | |
876 | } | |
877 | ||
9a86fe48 | 878 | flags = BDRV_O_RDWR | BDRV_O_UNMAP; |
ce099547 | 879 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
661a0f71 FS |
880 | if (ret < 0) { |
881 | error_report("Invalid cache option: %s", cache); | |
a3981eb9 | 882 | return 1; |
661a0f71 FS |
883 | } |
884 | ||
ce099547 | 885 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet); |
7e7d56d9 MA |
886 | if (!blk) { |
887 | return 1; | |
c2abccec | 888 | } |
7e7d56d9 MA |
889 | bs = blk_bs(blk); |
890 | ||
687fa1d8 HR |
891 | qemu_progress_init(progress, 1.f); |
892 | qemu_progress_print(0.f, 100); | |
893 | ||
1b22bffd HR |
894 | if (base) { |
895 | base_bs = bdrv_find_backing_image(bs, base); | |
896 | if (!base_bs) { | |
c6bd8c70 | 897 | error_setg(&local_err, QERR_BASE_NOT_FOUND, base); |
1b22bffd HR |
898 | goto done; |
899 | } | |
900 | } else { | |
901 | /* This is different from QMP, which by default uses the deepest file in | |
902 | * the backing chain (i.e., the very base); however, the traditional | |
903 | * behavior of qemu-img commit is using the immediate backing file. */ | |
760e0063 | 904 | base_bs = backing_bs(bs); |
1b22bffd HR |
905 | if (!base_bs) { |
906 | error_setg(&local_err, "Image does not have a backing file"); | |
907 | goto done; | |
908 | } | |
d4a3238a HR |
909 | } |
910 | ||
911 | cbi = (CommonBlockJobCBInfo){ | |
912 | .errp = &local_err, | |
913 | .bs = bs, | |
914 | }; | |
915 | ||
916 | commit_active_start(bs, base_bs, 0, BLOCKDEV_ON_ERROR_REPORT, | |
917 | common_block_job_cb, &cbi, &local_err); | |
918 | if (local_err) { | |
919 | goto done; | |
ea2384d3 FB |
920 | } |
921 | ||
3f09bfbc KW |
922 | /* When the block job completes, the BlockBackend reference will point to |
923 | * the old backing file. In order to avoid that the top image is already | |
924 | * deleted, so we can still empty it afterwards, increment the reference | |
925 | * counter here preemptively. */ | |
9a86fe48 | 926 | if (!drop) { |
3f09bfbc | 927 | bdrv_ref(bs); |
9a86fe48 HR |
928 | } |
929 | ||
d4a3238a | 930 | run_block_job(bs->job, &local_err); |
9a86fe48 HR |
931 | if (local_err) { |
932 | goto unref_backing; | |
933 | } | |
934 | ||
3f09bfbc KW |
935 | if (!drop && bs->drv->bdrv_make_empty) { |
936 | ret = bs->drv->bdrv_make_empty(bs); | |
9a86fe48 HR |
937 | if (ret) { |
938 | error_setg_errno(&local_err, -ret, "Could not empty %s", | |
939 | filename); | |
940 | goto unref_backing; | |
941 | } | |
942 | } | |
943 | ||
944 | unref_backing: | |
945 | if (!drop) { | |
3f09bfbc | 946 | bdrv_unref(bs); |
9a86fe48 | 947 | } |
d4a3238a HR |
948 | |
949 | done: | |
687fa1d8 HR |
950 | qemu_progress_end(); |
951 | ||
26f54e9a | 952 | blk_unref(blk); |
d4a3238a HR |
953 | |
954 | if (local_err) { | |
6936f299 | 955 | error_report_err(local_err); |
c2abccec MK |
956 | return 1; |
957 | } | |
d4a3238a HR |
958 | |
959 | qprintf(quiet, "Image committed.\n"); | |
ea2384d3 FB |
960 | return 0; |
961 | } | |
962 | ||
f58c7b35 TS |
963 | /* |
964 | * Returns true iff the first sector pointed to by 'buf' contains at least | |
965 | * a non-NUL byte. | |
966 | * | |
967 | * 'pnum' is set to the number of sectors (including and immediately following | |
968 | * the first one) that are known to be in the same allocated/unallocated state. | |
969 | */ | |
ea2384d3 FB |
970 | static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum) |
971 | { | |
1a6d39fd SH |
972 | bool is_zero; |
973 | int i; | |
ea2384d3 FB |
974 | |
975 | if (n <= 0) { | |
976 | *pnum = 0; | |
977 | return 0; | |
978 | } | |
1a6d39fd | 979 | is_zero = buffer_is_zero(buf, 512); |
ea2384d3 FB |
980 | for(i = 1; i < n; i++) { |
981 | buf += 512; | |
1a6d39fd | 982 | if (is_zero != buffer_is_zero(buf, 512)) { |
ea2384d3 | 983 | break; |
1a6d39fd | 984 | } |
ea2384d3 FB |
985 | } |
986 | *pnum = i; | |
1a6d39fd | 987 | return !is_zero; |
ea2384d3 FB |
988 | } |
989 | ||
a22f123c KW |
990 | /* |
991 | * Like is_allocated_sectors, but if the buffer starts with a used sector, | |
992 | * up to 'min' consecutive sectors containing zeros are ignored. This avoids | |
993 | * breaking up write requests for only small sparse areas. | |
994 | */ | |
995 | static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum, | |
996 | int min) | |
997 | { | |
998 | int ret; | |
999 | int num_checked, num_used; | |
1000 | ||
1001 | if (n < min) { | |
1002 | min = n; | |
1003 | } | |
1004 | ||
1005 | ret = is_allocated_sectors(buf, n, pnum); | |
1006 | if (!ret) { | |
1007 | return ret; | |
1008 | } | |
1009 | ||
1010 | num_used = *pnum; | |
1011 | buf += BDRV_SECTOR_SIZE * *pnum; | |
1012 | n -= *pnum; | |
1013 | num_checked = num_used; | |
1014 | ||
1015 | while (n > 0) { | |
1016 | ret = is_allocated_sectors(buf, n, pnum); | |
1017 | ||
1018 | buf += BDRV_SECTOR_SIZE * *pnum; | |
1019 | n -= *pnum; | |
1020 | num_checked += *pnum; | |
1021 | if (ret) { | |
1022 | num_used = num_checked; | |
1023 | } else if (*pnum >= min) { | |
1024 | break; | |
1025 | } | |
1026 | } | |
1027 | ||
1028 | *pnum = num_used; | |
1029 | return 1; | |
1030 | } | |
1031 | ||
3e85c6fd KW |
1032 | /* |
1033 | * Compares two buffers sector by sector. Returns 0 if the first sector of both | |
1034 | * buffers matches, non-zero otherwise. | |
1035 | * | |
1036 | * pnum is set to the number of sectors (including and immediately following | |
1037 | * the first one) that are known to have the same comparison result | |
1038 | */ | |
1039 | static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n, | |
1040 | int *pnum) | |
1041 | { | |
8c1ac475 RK |
1042 | bool res; |
1043 | int i; | |
3e85c6fd KW |
1044 | |
1045 | if (n <= 0) { | |
1046 | *pnum = 0; | |
1047 | return 0; | |
1048 | } | |
1049 | ||
1050 | res = !!memcmp(buf1, buf2, 512); | |
1051 | for(i = 1; i < n; i++) { | |
1052 | buf1 += 512; | |
1053 | buf2 += 512; | |
1054 | ||
1055 | if (!!memcmp(buf1, buf2, 512) != res) { | |
1056 | break; | |
1057 | } | |
1058 | } | |
1059 | ||
1060 | *pnum = i; | |
1061 | return res; | |
1062 | } | |
1063 | ||
80ee15a6 | 1064 | #define IO_BUF_SIZE (2 * 1024 * 1024) |
ea2384d3 | 1065 | |
d14ed18c MR |
1066 | static int64_t sectors_to_bytes(int64_t sectors) |
1067 | { | |
1068 | return sectors << BDRV_SECTOR_BITS; | |
1069 | } | |
1070 | ||
1071 | static int64_t sectors_to_process(int64_t total, int64_t from) | |
1072 | { | |
1073 | return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS); | |
1074 | } | |
1075 | ||
1076 | /* | |
1077 | * Check if passed sectors are empty (not allocated or contain only 0 bytes) | |
1078 | * | |
1079 | * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero | |
1080 | * data and negative value on error. | |
1081 | * | |
f1d3cd79 | 1082 | * @param blk: BlockBackend for the image |
d14ed18c MR |
1083 | * @param sect_num: Number of first sector to check |
1084 | * @param sect_count: Number of sectors to check | |
1085 | * @param filename: Name of disk file we are checking (logging purpose) | |
1086 | * @param buffer: Allocated buffer for storing read data | |
1087 | * @param quiet: Flag for quiet mode | |
1088 | */ | |
f1d3cd79 | 1089 | static int check_empty_sectors(BlockBackend *blk, int64_t sect_num, |
d14ed18c MR |
1090 | int sect_count, const char *filename, |
1091 | uint8_t *buffer, bool quiet) | |
1092 | { | |
1093 | int pnum, ret = 0; | |
f1d3cd79 | 1094 | ret = blk_read(blk, sect_num, buffer, sect_count); |
d14ed18c MR |
1095 | if (ret < 0) { |
1096 | error_report("Error while reading offset %" PRId64 " of %s: %s", | |
1097 | sectors_to_bytes(sect_num), filename, strerror(-ret)); | |
1098 | return ret; | |
1099 | } | |
1100 | ret = is_allocated_sectors(buffer, sect_count, &pnum); | |
1101 | if (ret || pnum != sect_count) { | |
1102 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", | |
1103 | sectors_to_bytes(ret ? sect_num : sect_num + pnum)); | |
1104 | return 1; | |
1105 | } | |
1106 | ||
1107 | return 0; | |
1108 | } | |
1109 | ||
1110 | /* | |
1111 | * Compares two images. Exit codes: | |
1112 | * | |
1113 | * 0 - Images are identical | |
1114 | * 1 - Images differ | |
1115 | * >1 - Error occurred | |
1116 | */ | |
1117 | static int img_compare(int argc, char **argv) | |
1118 | { | |
40055951 | 1119 | const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2; |
26f54e9a | 1120 | BlockBackend *blk1, *blk2; |
d14ed18c MR |
1121 | BlockDriverState *bs1, *bs2; |
1122 | int64_t total_sectors1, total_sectors2; | |
1123 | uint8_t *buf1 = NULL, *buf2 = NULL; | |
1124 | int pnum1, pnum2; | |
1125 | int allocated1, allocated2; | |
1126 | int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */ | |
1127 | bool progress = false, quiet = false, strict = false; | |
40055951 | 1128 | int flags; |
ce099547 | 1129 | bool writethrough; |
d14ed18c MR |
1130 | int64_t total_sectors; |
1131 | int64_t sector_num = 0; | |
1132 | int64_t nb_sectors; | |
1133 | int c, pnum; | |
d14ed18c | 1134 | uint64_t progress_base; |
3babeb15 | 1135 | Error *local_err = NULL; |
eb769f74 | 1136 | bool image_opts = false; |
d14ed18c | 1137 | |
40055951 | 1138 | cache = BDRV_DEFAULT_CACHE; |
d14ed18c | 1139 | for (;;) { |
3babeb15 DB |
1140 | static const struct option long_options[] = { |
1141 | {"help", no_argument, 0, 'h'}, | |
1142 | {"object", required_argument, 0, OPTION_OBJECT}, | |
eb769f74 | 1143 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
3babeb15 DB |
1144 | {0, 0, 0, 0} |
1145 | }; | |
1146 | c = getopt_long(argc, argv, "hf:F:T:pqs", | |
1147 | long_options, NULL); | |
d14ed18c MR |
1148 | if (c == -1) { |
1149 | break; | |
1150 | } | |
1151 | switch (c) { | |
1152 | case '?': | |
1153 | case 'h': | |
1154 | help(); | |
1155 | break; | |
1156 | case 'f': | |
1157 | fmt1 = optarg; | |
1158 | break; | |
1159 | case 'F': | |
1160 | fmt2 = optarg; | |
1161 | break; | |
40055951 HR |
1162 | case 'T': |
1163 | cache = optarg; | |
1164 | break; | |
d14ed18c MR |
1165 | case 'p': |
1166 | progress = true; | |
1167 | break; | |
1168 | case 'q': | |
1169 | quiet = true; | |
1170 | break; | |
1171 | case 's': | |
1172 | strict = true; | |
1173 | break; | |
3babeb15 DB |
1174 | case OPTION_OBJECT: { |
1175 | QemuOpts *opts; | |
1176 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
1177 | optarg, true); | |
1178 | if (!opts) { | |
1179 | ret = 2; | |
1180 | goto out4; | |
1181 | } | |
1182 | } break; | |
eb769f74 DB |
1183 | case OPTION_IMAGE_OPTS: |
1184 | image_opts = true; | |
1185 | break; | |
d14ed18c MR |
1186 | } |
1187 | } | |
1188 | ||
1189 | /* Progress is not shown in Quiet mode */ | |
1190 | if (quiet) { | |
1191 | progress = false; | |
1192 | } | |
1193 | ||
1194 | ||
fc11eb26 | 1195 | if (optind != argc - 2) { |
ac1307ab | 1196 | error_exit("Expecting two image file names"); |
d14ed18c MR |
1197 | } |
1198 | filename1 = argv[optind++]; | |
1199 | filename2 = argv[optind++]; | |
1200 | ||
3babeb15 DB |
1201 | if (qemu_opts_foreach(&qemu_object_opts, |
1202 | user_creatable_add_opts_foreach, | |
1203 | NULL, &local_err)) { | |
1204 | error_report_err(local_err); | |
1205 | ret = 2; | |
1206 | goto out4; | |
1207 | } | |
1208 | ||
cbda016d SH |
1209 | /* Initialize before goto out */ |
1210 | qemu_progress_init(progress, 2.0); | |
1211 | ||
ce099547 KW |
1212 | flags = 0; |
1213 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); | |
40055951 HR |
1214 | if (ret < 0) { |
1215 | error_report("Invalid source cache option: %s", cache); | |
1216 | ret = 2; | |
1217 | goto out3; | |
1218 | } | |
1219 | ||
ce099547 | 1220 | blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet); |
7e7d56d9 | 1221 | if (!blk1) { |
d14ed18c | 1222 | ret = 2; |
7e7d56d9 | 1223 | goto out3; |
d14ed18c MR |
1224 | } |
1225 | ||
ce099547 | 1226 | blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet); |
7e7d56d9 | 1227 | if (!blk2) { |
d14ed18c | 1228 | ret = 2; |
7e7d56d9 | 1229 | goto out2; |
d14ed18c | 1230 | } |
eb769f74 | 1231 | bs1 = blk_bs(blk1); |
7e7d56d9 | 1232 | bs2 = blk_bs(blk2); |
d14ed18c | 1233 | |
f1d3cd79 HR |
1234 | buf1 = blk_blockalign(blk1, IO_BUF_SIZE); |
1235 | buf2 = blk_blockalign(blk2, IO_BUF_SIZE); | |
1236 | total_sectors1 = blk_nb_sectors(blk1); | |
52bf1e72 MA |
1237 | if (total_sectors1 < 0) { |
1238 | error_report("Can't get size of %s: %s", | |
1239 | filename1, strerror(-total_sectors1)); | |
1240 | ret = 4; | |
1241 | goto out; | |
1242 | } | |
f1d3cd79 | 1243 | total_sectors2 = blk_nb_sectors(blk2); |
52bf1e72 MA |
1244 | if (total_sectors2 < 0) { |
1245 | error_report("Can't get size of %s: %s", | |
1246 | filename2, strerror(-total_sectors2)); | |
1247 | ret = 4; | |
1248 | goto out; | |
1249 | } | |
d14ed18c MR |
1250 | total_sectors = MIN(total_sectors1, total_sectors2); |
1251 | progress_base = MAX(total_sectors1, total_sectors2); | |
1252 | ||
1253 | qemu_progress_print(0, 100); | |
1254 | ||
1255 | if (strict && total_sectors1 != total_sectors2) { | |
1256 | ret = 1; | |
1257 | qprintf(quiet, "Strict mode: Image size mismatch!\n"); | |
1258 | goto out; | |
1259 | } | |
1260 | ||
1261 | for (;;) { | |
25ad8e6e | 1262 | int64_t status1, status2; |
67a0fd2a FZ |
1263 | BlockDriverState *file; |
1264 | ||
d14ed18c MR |
1265 | nb_sectors = sectors_to_process(total_sectors, sector_num); |
1266 | if (nb_sectors <= 0) { | |
1267 | break; | |
1268 | } | |
25ad8e6e FZ |
1269 | status1 = bdrv_get_block_status_above(bs1, NULL, sector_num, |
1270 | total_sectors1 - sector_num, | |
67a0fd2a | 1271 | &pnum1, &file); |
25ad8e6e | 1272 | if (status1 < 0) { |
d14ed18c MR |
1273 | ret = 3; |
1274 | error_report("Sector allocation test failed for %s", filename1); | |
1275 | goto out; | |
1276 | } | |
25ad8e6e | 1277 | allocated1 = status1 & BDRV_BLOCK_ALLOCATED; |
d14ed18c | 1278 | |
25ad8e6e FZ |
1279 | status2 = bdrv_get_block_status_above(bs2, NULL, sector_num, |
1280 | total_sectors2 - sector_num, | |
67a0fd2a | 1281 | &pnum2, &file); |
25ad8e6e | 1282 | if (status2 < 0) { |
d14ed18c MR |
1283 | ret = 3; |
1284 | error_report("Sector allocation test failed for %s", filename2); | |
1285 | goto out; | |
1286 | } | |
25ad8e6e FZ |
1287 | allocated2 = status2 & BDRV_BLOCK_ALLOCATED; |
1288 | if (pnum1) { | |
1289 | nb_sectors = MIN(nb_sectors, pnum1); | |
1290 | } | |
1291 | if (pnum2) { | |
1292 | nb_sectors = MIN(nb_sectors, pnum2); | |
1293 | } | |
d14ed18c | 1294 | |
25ad8e6e FZ |
1295 | if (strict) { |
1296 | if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) != | |
1297 | (status2 & ~BDRV_BLOCK_OFFSET_MASK)) { | |
1298 | ret = 1; | |
1299 | qprintf(quiet, "Strict mode: Offset %" PRId64 | |
1300 | " block status mismatch!\n", | |
1301 | sectors_to_bytes(sector_num)); | |
1302 | goto out; | |
1303 | } | |
1304 | } | |
1305 | if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) { | |
1306 | nb_sectors = MIN(pnum1, pnum2); | |
1307 | } else if (allocated1 == allocated2) { | |
d14ed18c | 1308 | if (allocated1) { |
f1d3cd79 | 1309 | ret = blk_read(blk1, sector_num, buf1, nb_sectors); |
d14ed18c MR |
1310 | if (ret < 0) { |
1311 | error_report("Error while reading offset %" PRId64 " of %s:" | |
1312 | " %s", sectors_to_bytes(sector_num), filename1, | |
1313 | strerror(-ret)); | |
1314 | ret = 4; | |
1315 | goto out; | |
1316 | } | |
f1d3cd79 | 1317 | ret = blk_read(blk2, sector_num, buf2, nb_sectors); |
d14ed18c MR |
1318 | if (ret < 0) { |
1319 | error_report("Error while reading offset %" PRId64 | |
1320 | " of %s: %s", sectors_to_bytes(sector_num), | |
1321 | filename2, strerror(-ret)); | |
1322 | ret = 4; | |
1323 | goto out; | |
1324 | } | |
1325 | ret = compare_sectors(buf1, buf2, nb_sectors, &pnum); | |
1326 | if (ret || pnum != nb_sectors) { | |
d14ed18c MR |
1327 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
1328 | sectors_to_bytes( | |
1329 | ret ? sector_num : sector_num + pnum)); | |
36452f12 | 1330 | ret = 1; |
d14ed18c MR |
1331 | goto out; |
1332 | } | |
1333 | } | |
1334 | } else { | |
d14ed18c MR |
1335 | |
1336 | if (allocated1) { | |
f1d3cd79 | 1337 | ret = check_empty_sectors(blk1, sector_num, nb_sectors, |
d14ed18c MR |
1338 | filename1, buf1, quiet); |
1339 | } else { | |
f1d3cd79 | 1340 | ret = check_empty_sectors(blk2, sector_num, nb_sectors, |
d14ed18c MR |
1341 | filename2, buf1, quiet); |
1342 | } | |
1343 | if (ret) { | |
1344 | if (ret < 0) { | |
d14ed18c MR |
1345 | error_report("Error while reading offset %" PRId64 ": %s", |
1346 | sectors_to_bytes(sector_num), strerror(-ret)); | |
36452f12 | 1347 | ret = 4; |
d14ed18c MR |
1348 | } |
1349 | goto out; | |
1350 | } | |
1351 | } | |
1352 | sector_num += nb_sectors; | |
1353 | qemu_progress_print(((float) nb_sectors / progress_base)*100, 100); | |
1354 | } | |
1355 | ||
1356 | if (total_sectors1 != total_sectors2) { | |
f1d3cd79 | 1357 | BlockBackend *blk_over; |
d14ed18c MR |
1358 | int64_t total_sectors_over; |
1359 | const char *filename_over; | |
1360 | ||
1361 | qprintf(quiet, "Warning: Image size mismatch!\n"); | |
1362 | if (total_sectors1 > total_sectors2) { | |
1363 | total_sectors_over = total_sectors1; | |
f1d3cd79 | 1364 | blk_over = blk1; |
d14ed18c MR |
1365 | filename_over = filename1; |
1366 | } else { | |
1367 | total_sectors_over = total_sectors2; | |
f1d3cd79 | 1368 | blk_over = blk2; |
d14ed18c MR |
1369 | filename_over = filename2; |
1370 | } | |
1371 | ||
1372 | for (;;) { | |
1373 | nb_sectors = sectors_to_process(total_sectors_over, sector_num); | |
1374 | if (nb_sectors <= 0) { | |
1375 | break; | |
1376 | } | |
f1d3cd79 | 1377 | ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL, sector_num, |
d14ed18c MR |
1378 | nb_sectors, &pnum); |
1379 | if (ret < 0) { | |
1380 | ret = 3; | |
1381 | error_report("Sector allocation test failed for %s", | |
1382 | filename_over); | |
1383 | goto out; | |
1384 | ||
1385 | } | |
1386 | nb_sectors = pnum; | |
1387 | if (ret) { | |
f1d3cd79 | 1388 | ret = check_empty_sectors(blk_over, sector_num, nb_sectors, |
d14ed18c MR |
1389 | filename_over, buf1, quiet); |
1390 | if (ret) { | |
1391 | if (ret < 0) { | |
d14ed18c MR |
1392 | error_report("Error while reading offset %" PRId64 |
1393 | " of %s: %s", sectors_to_bytes(sector_num), | |
1394 | filename_over, strerror(-ret)); | |
36452f12 | 1395 | ret = 4; |
d14ed18c MR |
1396 | } |
1397 | goto out; | |
1398 | } | |
1399 | } | |
1400 | sector_num += nb_sectors; | |
1401 | qemu_progress_print(((float) nb_sectors / progress_base)*100, 100); | |
1402 | } | |
1403 | } | |
1404 | ||
1405 | qprintf(quiet, "Images are identical.\n"); | |
1406 | ret = 0; | |
1407 | ||
1408 | out: | |
d14ed18c MR |
1409 | qemu_vfree(buf1); |
1410 | qemu_vfree(buf2); | |
26f54e9a | 1411 | blk_unref(blk2); |
d14ed18c | 1412 | out2: |
26f54e9a | 1413 | blk_unref(blk1); |
d14ed18c MR |
1414 | out3: |
1415 | qemu_progress_end(); | |
3babeb15 | 1416 | out4: |
d14ed18c MR |
1417 | return ret; |
1418 | } | |
1419 | ||
690c7301 KW |
1420 | enum ImgConvertBlockStatus { |
1421 | BLK_DATA, | |
1422 | BLK_ZERO, | |
1423 | BLK_BACKING_FILE, | |
1424 | }; | |
1425 | ||
1426 | typedef struct ImgConvertState { | |
1427 | BlockBackend **src; | |
1428 | int64_t *src_sectors; | |
1429 | int src_cur, src_num; | |
1430 | int64_t src_cur_offset; | |
1431 | int64_t total_sectors; | |
1432 | int64_t allocated_sectors; | |
1433 | enum ImgConvertBlockStatus status; | |
1434 | int64_t sector_next_status; | |
1435 | BlockBackend *target; | |
1436 | bool has_zero_init; | |
1437 | bool compressed; | |
1438 | bool target_has_backing; | |
1439 | int min_sparse; | |
1440 | size_t cluster_sectors; | |
1441 | size_t buf_sectors; | |
1442 | } ImgConvertState; | |
1443 | ||
1444 | static void convert_select_part(ImgConvertState *s, int64_t sector_num) | |
1445 | { | |
1446 | assert(sector_num >= s->src_cur_offset); | |
1447 | while (sector_num - s->src_cur_offset >= s->src_sectors[s->src_cur]) { | |
1448 | s->src_cur_offset += s->src_sectors[s->src_cur]; | |
1449 | s->src_cur++; | |
1450 | assert(s->src_cur < s->src_num); | |
1451 | } | |
1452 | } | |
1453 | ||
1454 | static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num) | |
1455 | { | |
1456 | int64_t ret; | |
1457 | int n; | |
1458 | ||
1459 | convert_select_part(s, sector_num); | |
1460 | ||
1461 | assert(s->total_sectors > sector_num); | |
1462 | n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS); | |
1463 | ||
1464 | if (s->sector_next_status <= sector_num) { | |
67a0fd2a | 1465 | BlockDriverState *file; |
690c7301 KW |
1466 | ret = bdrv_get_block_status(blk_bs(s->src[s->src_cur]), |
1467 | sector_num - s->src_cur_offset, | |
67a0fd2a | 1468 | n, &n, &file); |
690c7301 KW |
1469 | if (ret < 0) { |
1470 | return ret; | |
1471 | } | |
1472 | ||
1473 | if (ret & BDRV_BLOCK_ZERO) { | |
1474 | s->status = BLK_ZERO; | |
1475 | } else if (ret & BDRV_BLOCK_DATA) { | |
1476 | s->status = BLK_DATA; | |
1477 | } else if (!s->target_has_backing) { | |
1478 | /* Without a target backing file we must copy over the contents of | |
1479 | * the backing file as well. */ | |
1480 | /* TODO Check block status of the backing file chain to avoid | |
1481 | * needlessly reading zeroes and limiting the iteration to the | |
1482 | * buffer size */ | |
1483 | s->status = BLK_DATA; | |
1484 | } else { | |
1485 | s->status = BLK_BACKING_FILE; | |
1486 | } | |
1487 | ||
1488 | s->sector_next_status = sector_num + n; | |
1489 | } | |
1490 | ||
1491 | n = MIN(n, s->sector_next_status - sector_num); | |
1492 | if (s->status == BLK_DATA) { | |
1493 | n = MIN(n, s->buf_sectors); | |
1494 | } | |
1495 | ||
1496 | /* We need to write complete clusters for compressed images, so if an | |
1497 | * unallocated area is shorter than that, we must consider the whole | |
1498 | * cluster allocated. */ | |
1499 | if (s->compressed) { | |
1500 | if (n < s->cluster_sectors) { | |
1501 | n = MIN(s->cluster_sectors, s->total_sectors - sector_num); | |
1502 | s->status = BLK_DATA; | |
1503 | } else { | |
1504 | n = QEMU_ALIGN_DOWN(n, s->cluster_sectors); | |
1505 | } | |
1506 | } | |
1507 | ||
1508 | return n; | |
1509 | } | |
1510 | ||
1511 | static int convert_read(ImgConvertState *s, int64_t sector_num, int nb_sectors, | |
1512 | uint8_t *buf) | |
1513 | { | |
1514 | int n; | |
1515 | int ret; | |
1516 | ||
1517 | if (s->status == BLK_ZERO || s->status == BLK_BACKING_FILE) { | |
1518 | return 0; | |
1519 | } | |
1520 | ||
1521 | assert(nb_sectors <= s->buf_sectors); | |
1522 | while (nb_sectors > 0) { | |
1523 | BlockBackend *blk; | |
1524 | int64_t bs_sectors; | |
1525 | ||
1526 | /* In the case of compression with multiple source files, we can get a | |
1527 | * nb_sectors that spreads into the next part. So we must be able to | |
1528 | * read across multiple BDSes for one convert_read() call. */ | |
1529 | convert_select_part(s, sector_num); | |
1530 | blk = s->src[s->src_cur]; | |
1531 | bs_sectors = s->src_sectors[s->src_cur]; | |
1532 | ||
1533 | n = MIN(nb_sectors, bs_sectors - (sector_num - s->src_cur_offset)); | |
1534 | ret = blk_read(blk, sector_num - s->src_cur_offset, buf, n); | |
1535 | if (ret < 0) { | |
1536 | return ret; | |
1537 | } | |
1538 | ||
1539 | sector_num += n; | |
1540 | nb_sectors -= n; | |
1541 | buf += n * BDRV_SECTOR_SIZE; | |
1542 | } | |
1543 | ||
1544 | return 0; | |
1545 | } | |
1546 | ||
1547 | static int convert_write(ImgConvertState *s, int64_t sector_num, int nb_sectors, | |
1548 | const uint8_t *buf) | |
1549 | { | |
1550 | int ret; | |
1551 | ||
1552 | while (nb_sectors > 0) { | |
1553 | int n = nb_sectors; | |
1554 | ||
1555 | switch (s->status) { | |
1556 | case BLK_BACKING_FILE: | |
1557 | /* If we have a backing file, leave clusters unallocated that are | |
1558 | * unallocated in the source image, so that the backing file is | |
1559 | * visible at the respective offset. */ | |
1560 | assert(s->target_has_backing); | |
1561 | break; | |
1562 | ||
1563 | case BLK_DATA: | |
1564 | /* We must always write compressed clusters as a whole, so don't | |
1565 | * try to find zeroed parts in the buffer. We can only save the | |
1566 | * write if the buffer is completely zeroed and we're allowed to | |
1567 | * keep the target sparse. */ | |
1568 | if (s->compressed) { | |
1569 | if (s->has_zero_init && s->min_sparse && | |
1570 | buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)) | |
1571 | { | |
1572 | assert(!s->target_has_backing); | |
1573 | break; | |
1574 | } | |
1575 | ||
1576 | ret = blk_write_compressed(s->target, sector_num, buf, n); | |
1577 | if (ret < 0) { | |
1578 | return ret; | |
1579 | } | |
1580 | break; | |
1581 | } | |
1582 | ||
1583 | /* If there is real non-zero data or we're told to keep the target | |
1584 | * fully allocated (-S 0), we must write it. Otherwise we can treat | |
1585 | * it as zero sectors. */ | |
1586 | if (!s->min_sparse || | |
1587 | is_allocated_sectors_min(buf, n, &n, s->min_sparse)) | |
1588 | { | |
1589 | ret = blk_write(s->target, sector_num, buf, n); | |
1590 | if (ret < 0) { | |
1591 | return ret; | |
1592 | } | |
1593 | break; | |
1594 | } | |
1595 | /* fall-through */ | |
1596 | ||
1597 | case BLK_ZERO: | |
1598 | if (s->has_zero_init) { | |
1599 | break; | |
1600 | } | |
1601 | ret = blk_write_zeroes(s->target, sector_num, n, 0); | |
1602 | if (ret < 0) { | |
1603 | return ret; | |
1604 | } | |
1605 | break; | |
1606 | } | |
1607 | ||
1608 | sector_num += n; | |
1609 | nb_sectors -= n; | |
1610 | buf += n * BDRV_SECTOR_SIZE; | |
1611 | } | |
1612 | ||
1613 | return 0; | |
1614 | } | |
1615 | ||
1616 | static int convert_do_copy(ImgConvertState *s) | |
1617 | { | |
1618 | uint8_t *buf = NULL; | |
1619 | int64_t sector_num, allocated_done; | |
1620 | int ret; | |
1621 | int n; | |
1622 | ||
1623 | /* Check whether we have zero initialisation or can get it efficiently */ | |
1624 | s->has_zero_init = s->min_sparse && !s->target_has_backing | |
1625 | ? bdrv_has_zero_init(blk_bs(s->target)) | |
1626 | : false; | |
1627 | ||
1628 | if (!s->has_zero_init && !s->target_has_backing && | |
1629 | bdrv_can_write_zeroes_with_unmap(blk_bs(s->target))) | |
1630 | { | |
1631 | ret = bdrv_make_zero(blk_bs(s->target), BDRV_REQ_MAY_UNMAP); | |
1632 | if (ret == 0) { | |
1633 | s->has_zero_init = true; | |
1634 | } | |
1635 | } | |
1636 | ||
1637 | /* Allocate buffer for copied data. For compressed images, only one cluster | |
1638 | * can be copied at a time. */ | |
1639 | if (s->compressed) { | |
1640 | if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) { | |
1641 | error_report("invalid cluster size"); | |
1642 | ret = -EINVAL; | |
1643 | goto fail; | |
1644 | } | |
1645 | s->buf_sectors = s->cluster_sectors; | |
1646 | } | |
1647 | buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE); | |
1648 | ||
1649 | /* Calculate allocated sectors for progress */ | |
1650 | s->allocated_sectors = 0; | |
1651 | sector_num = 0; | |
1652 | while (sector_num < s->total_sectors) { | |
1653 | n = convert_iteration_sectors(s, sector_num); | |
1654 | if (n < 0) { | |
1655 | ret = n; | |
1656 | goto fail; | |
1657 | } | |
1658 | if (s->status == BLK_DATA) { | |
1659 | s->allocated_sectors += n; | |
1660 | } | |
1661 | sector_num += n; | |
1662 | } | |
1663 | ||
1664 | /* Do the copy */ | |
1665 | s->src_cur = 0; | |
1666 | s->src_cur_offset = 0; | |
1667 | s->sector_next_status = 0; | |
1668 | ||
1669 | sector_num = 0; | |
1670 | allocated_done = 0; | |
1671 | ||
1672 | while (sector_num < s->total_sectors) { | |
1673 | n = convert_iteration_sectors(s, sector_num); | |
1674 | if (n < 0) { | |
1675 | ret = n; | |
1676 | goto fail; | |
1677 | } | |
1678 | if (s->status == BLK_DATA) { | |
1679 | allocated_done += n; | |
1680 | qemu_progress_print(100.0 * allocated_done / s->allocated_sectors, | |
1681 | 0); | |
1682 | } | |
1683 | ||
1684 | ret = convert_read(s, sector_num, n, buf); | |
1685 | if (ret < 0) { | |
1686 | error_report("error while reading sector %" PRId64 | |
1687 | ": %s", sector_num, strerror(-ret)); | |
1688 | goto fail; | |
1689 | } | |
1690 | ||
1691 | ret = convert_write(s, sector_num, n, buf); | |
1692 | if (ret < 0) { | |
1693 | error_report("error while writing sector %" PRId64 | |
1694 | ": %s", sector_num, strerror(-ret)); | |
1695 | goto fail; | |
1696 | } | |
1697 | ||
1698 | sector_num += n; | |
1699 | } | |
1700 | ||
1701 | if (s->compressed) { | |
1702 | /* signal EOF to align */ | |
1703 | ret = blk_write_compressed(s->target, 0, NULL, 0); | |
1704 | if (ret < 0) { | |
1705 | goto fail; | |
1706 | } | |
1707 | } | |
1708 | ||
1709 | ret = 0; | |
1710 | fail: | |
1711 | qemu_vfree(buf); | |
1712 | return ret; | |
1713 | } | |
1714 | ||
ea2384d3 FB |
1715 | static int img_convert(int argc, char **argv) |
1716 | { | |
690c7301 | 1717 | int c, bs_n, bs_i, compress, cluster_sectors, skip_create; |
13c28af8 | 1718 | int64_t ret = 0; |
40055951 | 1719 | int progress = 0, flags, src_flags; |
ce099547 | 1720 | bool writethrough, src_writethrough; |
40055951 | 1721 | const char *fmt, *out_fmt, *cache, *src_cache, *out_baseimg, *out_filename; |
b50cbabc | 1722 | BlockDriver *drv, *proto_drv; |
26f54e9a | 1723 | BlockBackend **blk = NULL, *out_blk = NULL; |
c2abccec | 1724 | BlockDriverState **bs = NULL, *out_bs = NULL; |
690c7301 | 1725 | int64_t total_sectors; |
52bf1e72 | 1726 | int64_t *bs_sectors = NULL; |
f2521c90 | 1727 | size_t bufsectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE; |
faea38e7 | 1728 | BlockDriverInfo bdi; |
83d0521a CL |
1729 | QemuOpts *opts = NULL; |
1730 | QemuOptsList *create_opts = NULL; | |
1731 | const char *out_baseimg_param; | |
efa84d43 | 1732 | char *options = NULL; |
51ef6727 | 1733 | const char *snapshot_name = NULL; |
a22f123c | 1734 | int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */ |
f382d43a | 1735 | bool quiet = false; |
cc84d90f | 1736 | Error *local_err = NULL; |
ef80654d | 1737 | QemuOpts *sn_opts = NULL; |
690c7301 | 1738 | ImgConvertState state; |
eb769f74 | 1739 | bool image_opts = false; |
ea2384d3 FB |
1740 | |
1741 | fmt = NULL; | |
1742 | out_fmt = "raw"; | |
661a0f71 | 1743 | cache = "unsafe"; |
40055951 | 1744 | src_cache = BDRV_DEFAULT_CACHE; |
f58c7b35 | 1745 | out_baseimg = NULL; |
eec77d9e | 1746 | compress = 0; |
b2e10493 | 1747 | skip_create = 0; |
ea2384d3 | 1748 | for(;;) { |
3babeb15 DB |
1749 | static const struct option long_options[] = { |
1750 | {"help", no_argument, 0, 'h'}, | |
1751 | {"object", required_argument, 0, OPTION_OBJECT}, | |
eb769f74 | 1752 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
3babeb15 DB |
1753 | {0, 0, 0, 0} |
1754 | }; | |
1755 | c = getopt_long(argc, argv, "hf:O:B:ce6o:s:l:S:pt:T:qn", | |
1756 | long_options, NULL); | |
b8fb60da | 1757 | if (c == -1) { |
ea2384d3 | 1758 | break; |
b8fb60da | 1759 | } |
ea2384d3 | 1760 | switch(c) { |
ef87394c | 1761 | case '?': |
ea2384d3 FB |
1762 | case 'h': |
1763 | help(); | |
1764 | break; | |
1765 | case 'f': | |
1766 | fmt = optarg; | |
1767 | break; | |
1768 | case 'O': | |
1769 | out_fmt = optarg; | |
1770 | break; | |
f58c7b35 TS |
1771 | case 'B': |
1772 | out_baseimg = optarg; | |
1773 | break; | |
ea2384d3 | 1774 | case 'c': |
eec77d9e | 1775 | compress = 1; |
ea2384d3 FB |
1776 | break; |
1777 | case 'e': | |
9d42e15d | 1778 | error_report("option -e is deprecated, please use \'-o " |
eec77d9e | 1779 | "encryption\' instead!"); |
2dc8328b | 1780 | ret = -1; |
64bb01aa | 1781 | goto fail_getopt; |
ec36ba14 | 1782 | case '6': |
9d42e15d | 1783 | error_report("option -6 is deprecated, please use \'-o " |
eec77d9e | 1784 | "compat6\' instead!"); |
2dc8328b | 1785 | ret = -1; |
64bb01aa | 1786 | goto fail_getopt; |
efa84d43 | 1787 | case 'o': |
2dc8328b KW |
1788 | if (!is_valid_option_list(optarg)) { |
1789 | error_report("Invalid option list: %s", optarg); | |
1790 | ret = -1; | |
64bb01aa | 1791 | goto fail_getopt; |
2dc8328b KW |
1792 | } |
1793 | if (!options) { | |
1794 | options = g_strdup(optarg); | |
1795 | } else { | |
1796 | char *old_options = options; | |
1797 | options = g_strdup_printf("%s,%s", options, optarg); | |
1798 | g_free(old_options); | |
1799 | } | |
efa84d43 | 1800 | break; |
51ef6727 | 1801 | case 's': |
1802 | snapshot_name = optarg; | |
1803 | break; | |
ef80654d WX |
1804 | case 'l': |
1805 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { | |
70b94331 MA |
1806 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
1807 | optarg, false); | |
ef80654d WX |
1808 | if (!sn_opts) { |
1809 | error_report("Failed in parsing snapshot param '%s'", | |
1810 | optarg); | |
2dc8328b | 1811 | ret = -1; |
64bb01aa | 1812 | goto fail_getopt; |
ef80654d WX |
1813 | } |
1814 | } else { | |
1815 | snapshot_name = optarg; | |
1816 | } | |
1817 | break; | |
a22f123c KW |
1818 | case 'S': |
1819 | { | |
1820 | int64_t sval; | |
e36b3695 | 1821 | char *end; |
4677bb40 | 1822 | sval = qemu_strtosz_suffix(optarg, &end, QEMU_STRTOSZ_DEFSUFFIX_B); |
e36b3695 | 1823 | if (sval < 0 || *end) { |
a22f123c | 1824 | error_report("Invalid minimum zero buffer size for sparse output specified"); |
2dc8328b | 1825 | ret = -1; |
64bb01aa | 1826 | goto fail_getopt; |
a22f123c KW |
1827 | } |
1828 | ||
1829 | min_sparse = sval / BDRV_SECTOR_SIZE; | |
1830 | break; | |
1831 | } | |
6b837bc4 JS |
1832 | case 'p': |
1833 | progress = 1; | |
1834 | break; | |
661a0f71 FS |
1835 | case 't': |
1836 | cache = optarg; | |
1837 | break; | |
40055951 HR |
1838 | case 'T': |
1839 | src_cache = optarg; | |
1840 | break; | |
f382d43a MR |
1841 | case 'q': |
1842 | quiet = true; | |
1843 | break; | |
b2e10493 AD |
1844 | case 'n': |
1845 | skip_create = 1; | |
1846 | break; | |
3babeb15 DB |
1847 | case OPTION_OBJECT: |
1848 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
1849 | optarg, true); | |
1850 | if (!opts) { | |
1851 | goto fail_getopt; | |
1852 | } | |
1853 | break; | |
eb769f74 DB |
1854 | case OPTION_IMAGE_OPTS: |
1855 | image_opts = true; | |
1856 | break; | |
ea2384d3 FB |
1857 | } |
1858 | } | |
3b46e624 | 1859 | |
3babeb15 DB |
1860 | if (qemu_opts_foreach(&qemu_object_opts, |
1861 | user_creatable_add_opts_foreach, | |
1862 | NULL, &local_err)) { | |
1863 | error_report_err(local_err); | |
1864 | goto fail_getopt; | |
1865 | } | |
1866 | ||
64bb01aa | 1867 | /* Initialize before goto out */ |
f382d43a MR |
1868 | if (quiet) { |
1869 | progress = 0; | |
1870 | } | |
64bb01aa KW |
1871 | qemu_progress_init(progress, 1.0); |
1872 | ||
926c2d23 | 1873 | bs_n = argc - optind - 1; |
a283cb6e | 1874 | out_filename = bs_n >= 1 ? argv[argc - 1] : NULL; |
f58c7b35 | 1875 | |
2dc8328b | 1876 | if (options && has_help_option(options)) { |
4ac8aacd JS |
1877 | ret = print_block_option_help(out_filename, out_fmt); |
1878 | goto out; | |
1879 | } | |
1880 | ||
a283cb6e | 1881 | if (bs_n < 1) { |
ac1307ab | 1882 | error_exit("Must specify image file name"); |
a283cb6e KW |
1883 | } |
1884 | ||
1885 | ||
c2abccec | 1886 | if (bs_n > 1 && out_baseimg) { |
15654a6d JS |
1887 | error_report("-B makes no sense when concatenating multiple input " |
1888 | "images"); | |
31ca34b8 JS |
1889 | ret = -1; |
1890 | goto out; | |
c2abccec | 1891 | } |
f8111c24 | 1892 | |
ce099547 KW |
1893 | src_flags = 0; |
1894 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); | |
40055951 HR |
1895 | if (ret < 0) { |
1896 | error_report("Invalid source cache option: %s", src_cache); | |
1897 | goto out; | |
1898 | } | |
1899 | ||
6b837bc4 JS |
1900 | qemu_progress_print(0, 100); |
1901 | ||
26f54e9a | 1902 | blk = g_new0(BlockBackend *, bs_n); |
d739f1c4 | 1903 | bs = g_new0(BlockDriverState *, bs_n); |
52bf1e72 | 1904 | bs_sectors = g_new(int64_t, bs_n); |
926c2d23 AZ |
1905 | |
1906 | total_sectors = 0; | |
1907 | for (bs_i = 0; bs_i < bs_n; bs_i++) { | |
efaa7c4e | 1908 | blk[bs_i] = img_open(image_opts, argv[optind + bs_i], |
ce099547 | 1909 | fmt, src_flags, src_writethrough, quiet); |
7e7d56d9 | 1910 | if (!blk[bs_i]) { |
c2abccec MK |
1911 | ret = -1; |
1912 | goto out; | |
1913 | } | |
7e7d56d9 | 1914 | bs[bs_i] = blk_bs(blk[bs_i]); |
f1d3cd79 | 1915 | bs_sectors[bs_i] = blk_nb_sectors(blk[bs_i]); |
52bf1e72 MA |
1916 | if (bs_sectors[bs_i] < 0) { |
1917 | error_report("Could not get size of %s: %s", | |
1918 | argv[optind + bs_i], strerror(-bs_sectors[bs_i])); | |
1919 | ret = -1; | |
1920 | goto out; | |
1921 | } | |
d739f1c4 | 1922 | total_sectors += bs_sectors[bs_i]; |
926c2d23 | 1923 | } |
ea2384d3 | 1924 | |
ef80654d WX |
1925 | if (sn_opts) { |
1926 | ret = bdrv_snapshot_load_tmp(bs[0], | |
1927 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), | |
1928 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), | |
1929 | &local_err); | |
1930 | } else if (snapshot_name != NULL) { | |
51ef6727 | 1931 | if (bs_n > 1) { |
6daf194d | 1932 | error_report("No support for concatenating multiple snapshot"); |
51ef6727 | 1933 | ret = -1; |
1934 | goto out; | |
1935 | } | |
7b4c4781 WX |
1936 | |
1937 | bdrv_snapshot_load_tmp_by_id_or_name(bs[0], snapshot_name, &local_err); | |
ef80654d | 1938 | } |
84d18f06 | 1939 | if (local_err) { |
c29b77f9 | 1940 | error_reportf_err(local_err, "Failed to load snapshot: "); |
ef80654d WX |
1941 | ret = -1; |
1942 | goto out; | |
51ef6727 | 1943 | } |
1944 | ||
efa84d43 | 1945 | /* Find driver and parse its options */ |
ea2384d3 | 1946 | drv = bdrv_find_format(out_fmt); |
c2abccec | 1947 | if (!drv) { |
15654a6d | 1948 | error_report("Unknown file format '%s'", out_fmt); |
c2abccec MK |
1949 | ret = -1; |
1950 | goto out; | |
1951 | } | |
efa84d43 | 1952 | |
b65a5e12 | 1953 | proto_drv = bdrv_find_protocol(out_filename, true, &local_err); |
c2abccec | 1954 | if (!proto_drv) { |
2867ce4a | 1955 | error_report_err(local_err); |
c2abccec MK |
1956 | ret = -1; |
1957 | goto out; | |
1958 | } | |
b50cbabc | 1959 | |
2e024cde HR |
1960 | if (!skip_create) { |
1961 | if (!drv->create_opts) { | |
1962 | error_report("Format driver '%s' does not support image creation", | |
1963 | drv->format_name); | |
1964 | ret = -1; | |
1965 | goto out; | |
1966 | } | |
f75613cf | 1967 | |
2e024cde HR |
1968 | if (!proto_drv->create_opts) { |
1969 | error_report("Protocol driver '%s' does not support image creation", | |
1970 | proto_drv->format_name); | |
1971 | ret = -1; | |
1972 | goto out; | |
1973 | } | |
f75613cf | 1974 | |
2e024cde HR |
1975 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
1976 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); | |
db08adf5 | 1977 | |
2e024cde | 1978 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
dc523cd3 MA |
1979 | if (options) { |
1980 | qemu_opts_do_parse(opts, options, NULL, &local_err); | |
1981 | if (local_err) { | |
97a2ca7a | 1982 | error_report_err(local_err); |
dc523cd3 MA |
1983 | ret = -1; |
1984 | goto out; | |
1985 | } | |
2e024cde | 1986 | } |
efa84d43 | 1987 | |
39101f25 MA |
1988 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_sectors * 512, |
1989 | &error_abort); | |
2e024cde HR |
1990 | ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL); |
1991 | if (ret < 0) { | |
1992 | goto out; | |
1993 | } | |
c2abccec | 1994 | } |
efa84d43 | 1995 | |
a18953fb | 1996 | /* Get backing file name if -o backing_file was used */ |
83d0521a | 1997 | out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); |
a18953fb | 1998 | if (out_baseimg_param) { |
83d0521a | 1999 | out_baseimg = out_baseimg_param; |
a18953fb KW |
2000 | } |
2001 | ||
efa84d43 | 2002 | /* Check if compression is supported */ |
eec77d9e | 2003 | if (compress) { |
83d0521a CL |
2004 | bool encryption = |
2005 | qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false); | |
2006 | const char *preallocation = | |
2007 | qemu_opt_get(opts, BLOCK_OPT_PREALLOC); | |
efa84d43 KW |
2008 | |
2009 | if (!drv->bdrv_write_compressed) { | |
15654a6d | 2010 | error_report("Compression not supported for this file format"); |
c2abccec MK |
2011 | ret = -1; |
2012 | goto out; | |
efa84d43 KW |
2013 | } |
2014 | ||
83d0521a | 2015 | if (encryption) { |
15654a6d JS |
2016 | error_report("Compression and encryption not supported at " |
2017 | "the same time"); | |
c2abccec MK |
2018 | ret = -1; |
2019 | goto out; | |
efa84d43 | 2020 | } |
41521fa4 | 2021 | |
83d0521a CL |
2022 | if (preallocation |
2023 | && strcmp(preallocation, "off")) | |
41521fa4 KW |
2024 | { |
2025 | error_report("Compression and preallocation not supported at " | |
2026 | "the same time"); | |
2027 | ret = -1; | |
2028 | goto out; | |
2029 | } | |
efa84d43 KW |
2030 | } |
2031 | ||
b2e10493 AD |
2032 | if (!skip_create) { |
2033 | /* Create the new image */ | |
c282e1fd | 2034 | ret = bdrv_create(drv, out_filename, opts, &local_err); |
b2e10493 | 2035 | if (ret < 0) { |
c29b77f9 MA |
2036 | error_reportf_err(local_err, "%s: error while converting %s: ", |
2037 | out_filename, out_fmt); | |
b2e10493 | 2038 | goto out; |
ea2384d3 FB |
2039 | } |
2040 | } | |
3b46e624 | 2041 | |
5a37b60a | 2042 | flags = min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR; |
ce099547 | 2043 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
661a0f71 FS |
2044 | if (ret < 0) { |
2045 | error_report("Invalid cache option: %s", cache); | |
bb9cd2ee | 2046 | goto out; |
661a0f71 FS |
2047 | } |
2048 | ||
eb769f74 DB |
2049 | /* XXX we should allow --image-opts to trigger use of |
2050 | * img_open() here, but then we have trouble with | |
2051 | * the bdrv_create() call which takes different params. | |
2052 | * Not critical right now, so fix can wait... | |
2053 | */ | |
ce099547 | 2054 | out_blk = img_open_file(out_filename, out_fmt, flags, writethrough, quiet); |
7e7d56d9 | 2055 | if (!out_blk) { |
c2abccec MK |
2056 | ret = -1; |
2057 | goto out; | |
2058 | } | |
7e7d56d9 | 2059 | out_bs = blk_bs(out_blk); |
ea2384d3 | 2060 | |
f2521c90 PL |
2061 | /* increase bufsectors from the default 4096 (2M) if opt_transfer_length |
2062 | * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB) | |
2063 | * as maximum. */ | |
2064 | bufsectors = MIN(32768, | |
2065 | MAX(bufsectors, MAX(out_bs->bl.opt_transfer_length, | |
2066 | out_bs->bl.discard_alignment)) | |
2067 | ); | |
2068 | ||
b2e10493 | 2069 | if (skip_create) { |
f1d3cd79 | 2070 | int64_t output_sectors = blk_nb_sectors(out_blk); |
43716fa8 | 2071 | if (output_sectors < 0) { |
eec5eb42 | 2072 | error_report("unable to get output image length: %s", |
43716fa8 | 2073 | strerror(-output_sectors)); |
b2e10493 AD |
2074 | ret = -1; |
2075 | goto out; | |
43716fa8 | 2076 | } else if (output_sectors < total_sectors) { |
b2e10493 AD |
2077 | error_report("output file is smaller than input file"); |
2078 | ret = -1; | |
2079 | goto out; | |
2080 | } | |
2081 | } | |
2082 | ||
24f833cd PL |
2083 | cluster_sectors = 0; |
2084 | ret = bdrv_get_info(out_bs, &bdi); | |
2085 | if (ret < 0) { | |
2086 | if (compress) { | |
15654a6d | 2087 | error_report("could not get block driver info"); |
c2abccec MK |
2088 | goto out; |
2089 | } | |
24f833cd | 2090 | } else { |
85f49cad | 2091 | compress = compress || bdi.needs_compressed_writes; |
24f833cd PL |
2092 | cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE; |
2093 | } | |
2094 | ||
690c7301 KW |
2095 | state = (ImgConvertState) { |
2096 | .src = blk, | |
2097 | .src_sectors = bs_sectors, | |
2098 | .src_num = bs_n, | |
2099 | .total_sectors = total_sectors, | |
2100 | .target = out_blk, | |
2101 | .compressed = compress, | |
2102 | .target_has_backing = (bool) out_baseimg, | |
2103 | .min_sparse = min_sparse, | |
2104 | .cluster_sectors = cluster_sectors, | |
2105 | .buf_sectors = bufsectors, | |
2106 | }; | |
2107 | ret = convert_do_copy(&state); | |
802c3d4c | 2108 | |
c2abccec | 2109 | out: |
13c28af8 PL |
2110 | if (!ret) { |
2111 | qemu_progress_print(100, 0); | |
2112 | } | |
6b837bc4 | 2113 | qemu_progress_end(); |
83d0521a CL |
2114 | qemu_opts_del(opts); |
2115 | qemu_opts_free(create_opts); | |
fbf28a43 | 2116 | qemu_opts_del(sn_opts); |
26f54e9a | 2117 | blk_unref(out_blk); |
9ba10c95 | 2118 | g_free(bs); |
26f54e9a MA |
2119 | if (blk) { |
2120 | for (bs_i = 0; bs_i < bs_n; bs_i++) { | |
2121 | blk_unref(blk[bs_i]); | |
2122 | } | |
2123 | g_free(blk); | |
2124 | } | |
d739f1c4 | 2125 | g_free(bs_sectors); |
64bb01aa KW |
2126 | fail_getopt: |
2127 | g_free(options); | |
2128 | ||
c2abccec MK |
2129 | if (ret) { |
2130 | return 1; | |
2131 | } | |
ea2384d3 FB |
2132 | return 0; |
2133 | } | |
2134 | ||
57d1a2b6 | 2135 | |
faea38e7 FB |
2136 | static void dump_snapshots(BlockDriverState *bs) |
2137 | { | |
2138 | QEMUSnapshotInfo *sn_tab, *sn; | |
2139 | int nb_sns, i; | |
faea38e7 FB |
2140 | |
2141 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); | |
2142 | if (nb_sns <= 0) | |
2143 | return; | |
2144 | printf("Snapshot list:\n"); | |
5b917044 WX |
2145 | bdrv_snapshot_dump(fprintf, stdout, NULL); |
2146 | printf("\n"); | |
faea38e7 FB |
2147 | for(i = 0; i < nb_sns; i++) { |
2148 | sn = &sn_tab[i]; | |
5b917044 WX |
2149 | bdrv_snapshot_dump(fprintf, stdout, sn); |
2150 | printf("\n"); | |
faea38e7 | 2151 | } |
7267c094 | 2152 | g_free(sn_tab); |
faea38e7 FB |
2153 | } |
2154 | ||
9699bf0d SH |
2155 | static void dump_json_image_info_list(ImageInfoList *list) |
2156 | { | |
4399c438 | 2157 | Error *local_err = NULL; |
9699bf0d SH |
2158 | QString *str; |
2159 | QmpOutputVisitor *ov = qmp_output_visitor_new(); | |
2160 | QObject *obj; | |
51e72bc1 EB |
2161 | visit_type_ImageInfoList(qmp_output_get_visitor(ov), NULL, &list, |
2162 | &local_err); | |
9699bf0d SH |
2163 | obj = qmp_output_get_qobject(ov); |
2164 | str = qobject_to_json_pretty(obj); | |
2165 | assert(str != NULL); | |
2166 | printf("%s\n", qstring_get_str(str)); | |
2167 | qobject_decref(obj); | |
2168 | qmp_output_visitor_cleanup(ov); | |
2169 | QDECREF(str); | |
2170 | } | |
2171 | ||
c054b3fd BC |
2172 | static void dump_json_image_info(ImageInfo *info) |
2173 | { | |
4399c438 | 2174 | Error *local_err = NULL; |
c054b3fd BC |
2175 | QString *str; |
2176 | QmpOutputVisitor *ov = qmp_output_visitor_new(); | |
2177 | QObject *obj; | |
51e72bc1 | 2178 | visit_type_ImageInfo(qmp_output_get_visitor(ov), NULL, &info, &local_err); |
c054b3fd BC |
2179 | obj = qmp_output_get_qobject(ov); |
2180 | str = qobject_to_json_pretty(obj); | |
2181 | assert(str != NULL); | |
2182 | printf("%s\n", qstring_get_str(str)); | |
2183 | qobject_decref(obj); | |
2184 | qmp_output_visitor_cleanup(ov); | |
2185 | QDECREF(str); | |
2186 | } | |
2187 | ||
9699bf0d SH |
2188 | static void dump_human_image_info_list(ImageInfoList *list) |
2189 | { | |
2190 | ImageInfoList *elem; | |
2191 | bool delim = false; | |
2192 | ||
2193 | for (elem = list; elem; elem = elem->next) { | |
2194 | if (delim) { | |
2195 | printf("\n"); | |
2196 | } | |
2197 | delim = true; | |
2198 | ||
5b917044 | 2199 | bdrv_image_info_dump(fprintf, stdout, elem->value); |
9699bf0d SH |
2200 | } |
2201 | } | |
2202 | ||
2203 | static gboolean str_equal_func(gconstpointer a, gconstpointer b) | |
2204 | { | |
2205 | return strcmp(a, b) == 0; | |
2206 | } | |
2207 | ||
2208 | /** | |
2209 | * Open an image file chain and return an ImageInfoList | |
2210 | * | |
2211 | * @filename: topmost image filename | |
2212 | * @fmt: topmost image format (may be NULL to autodetect) | |
2213 | * @chain: true - enumerate entire backing file chain | |
2214 | * false - only topmost image file | |
2215 | * | |
2216 | * Returns a list of ImageInfo objects or NULL if there was an error opening an | |
2217 | * image file. If there was an error a message will have been printed to | |
2218 | * stderr. | |
2219 | */ | |
eb769f74 DB |
2220 | static ImageInfoList *collect_image_info_list(bool image_opts, |
2221 | const char *filename, | |
9699bf0d SH |
2222 | const char *fmt, |
2223 | bool chain) | |
2224 | { | |
2225 | ImageInfoList *head = NULL; | |
2226 | ImageInfoList **last = &head; | |
2227 | GHashTable *filenames; | |
43526ec8 | 2228 | Error *err = NULL; |
9699bf0d SH |
2229 | |
2230 | filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL); | |
2231 | ||
2232 | while (filename) { | |
26f54e9a | 2233 | BlockBackend *blk; |
9699bf0d SH |
2234 | BlockDriverState *bs; |
2235 | ImageInfo *info; | |
2236 | ImageInfoList *elem; | |
2237 | ||
2238 | if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) { | |
2239 | error_report("Backing file '%s' creates an infinite loop.", | |
2240 | filename); | |
2241 | goto err; | |
2242 | } | |
2243 | g_hash_table_insert(filenames, (gpointer)filename, NULL); | |
2244 | ||
efaa7c4e | 2245 | blk = img_open(image_opts, filename, fmt, |
ce099547 | 2246 | BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false); |
7e7d56d9 | 2247 | if (!blk) { |
9699bf0d SH |
2248 | goto err; |
2249 | } | |
7e7d56d9 | 2250 | bs = blk_bs(blk); |
9699bf0d | 2251 | |
43526ec8 | 2252 | bdrv_query_image_info(bs, &info, &err); |
84d18f06 | 2253 | if (err) { |
565f65d2 | 2254 | error_report_err(err); |
26f54e9a | 2255 | blk_unref(blk); |
43526ec8 | 2256 | goto err; |
fb0ed453 | 2257 | } |
9699bf0d SH |
2258 | |
2259 | elem = g_new0(ImageInfoList, 1); | |
2260 | elem->value = info; | |
2261 | *last = elem; | |
2262 | last = &elem->next; | |
2263 | ||
26f54e9a | 2264 | blk_unref(blk); |
9699bf0d SH |
2265 | |
2266 | filename = fmt = NULL; | |
2267 | if (chain) { | |
2268 | if (info->has_full_backing_filename) { | |
2269 | filename = info->full_backing_filename; | |
2270 | } else if (info->has_backing_filename) { | |
92d617ab JS |
2271 | error_report("Could not determine absolute backing filename," |
2272 | " but backing filename '%s' present", | |
2273 | info->backing_filename); | |
2274 | goto err; | |
9699bf0d SH |
2275 | } |
2276 | if (info->has_backing_filename_format) { | |
2277 | fmt = info->backing_filename_format; | |
2278 | } | |
2279 | } | |
2280 | } | |
2281 | g_hash_table_destroy(filenames); | |
2282 | return head; | |
2283 | ||
2284 | err: | |
2285 | qapi_free_ImageInfoList(head); | |
2286 | g_hash_table_destroy(filenames); | |
2287 | return NULL; | |
2288 | } | |
2289 | ||
c054b3fd BC |
2290 | static int img_info(int argc, char **argv) |
2291 | { | |
2292 | int c; | |
2293 | OutputFormat output_format = OFORMAT_HUMAN; | |
9699bf0d | 2294 | bool chain = false; |
c054b3fd | 2295 | const char *filename, *fmt, *output; |
9699bf0d | 2296 | ImageInfoList *list; |
3babeb15 | 2297 | Error *local_err = NULL; |
eb769f74 | 2298 | bool image_opts = false; |
c054b3fd | 2299 | |
ea2384d3 | 2300 | fmt = NULL; |
c054b3fd | 2301 | output = NULL; |
ea2384d3 | 2302 | for(;;) { |
c054b3fd BC |
2303 | int option_index = 0; |
2304 | static const struct option long_options[] = { | |
2305 | {"help", no_argument, 0, 'h'}, | |
2306 | {"format", required_argument, 0, 'f'}, | |
2307 | {"output", required_argument, 0, OPTION_OUTPUT}, | |
9699bf0d | 2308 | {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN}, |
3babeb15 | 2309 | {"object", required_argument, 0, OPTION_OBJECT}, |
eb769f74 | 2310 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
c054b3fd BC |
2311 | {0, 0, 0, 0} |
2312 | }; | |
2313 | c = getopt_long(argc, argv, "f:h", | |
2314 | long_options, &option_index); | |
b8fb60da | 2315 | if (c == -1) { |
ea2384d3 | 2316 | break; |
b8fb60da | 2317 | } |
ea2384d3 | 2318 | switch(c) { |
ef87394c | 2319 | case '?': |
ea2384d3 FB |
2320 | case 'h': |
2321 | help(); | |
2322 | break; | |
2323 | case 'f': | |
2324 | fmt = optarg; | |
2325 | break; | |
c054b3fd BC |
2326 | case OPTION_OUTPUT: |
2327 | output = optarg; | |
2328 | break; | |
9699bf0d SH |
2329 | case OPTION_BACKING_CHAIN: |
2330 | chain = true; | |
2331 | break; | |
3babeb15 DB |
2332 | case OPTION_OBJECT: { |
2333 | QemuOpts *opts; | |
2334 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
2335 | optarg, true); | |
2336 | if (!opts) { | |
2337 | return 1; | |
2338 | } | |
2339 | } break; | |
eb769f74 DB |
2340 | case OPTION_IMAGE_OPTS: |
2341 | image_opts = true; | |
2342 | break; | |
ea2384d3 FB |
2343 | } |
2344 | } | |
fc11eb26 | 2345 | if (optind != argc - 1) { |
ac1307ab | 2346 | error_exit("Expecting one image file name"); |
b8fb60da | 2347 | } |
ea2384d3 FB |
2348 | filename = argv[optind++]; |
2349 | ||
c054b3fd BC |
2350 | if (output && !strcmp(output, "json")) { |
2351 | output_format = OFORMAT_JSON; | |
2352 | } else if (output && !strcmp(output, "human")) { | |
2353 | output_format = OFORMAT_HUMAN; | |
2354 | } else if (output) { | |
2355 | error_report("--output must be used with human or json as argument."); | |
c2abccec MK |
2356 | return 1; |
2357 | } | |
c054b3fd | 2358 | |
3babeb15 DB |
2359 | if (qemu_opts_foreach(&qemu_object_opts, |
2360 | user_creatable_add_opts_foreach, | |
2361 | NULL, &local_err)) { | |
2362 | error_report_err(local_err); | |
2363 | return 1; | |
2364 | } | |
2365 | ||
eb769f74 | 2366 | list = collect_image_info_list(image_opts, filename, fmt, chain); |
9699bf0d | 2367 | if (!list) { |
c2abccec | 2368 | return 1; |
faea38e7 | 2369 | } |
c054b3fd | 2370 | |
c054b3fd BC |
2371 | switch (output_format) { |
2372 | case OFORMAT_HUMAN: | |
9699bf0d | 2373 | dump_human_image_info_list(list); |
c054b3fd BC |
2374 | break; |
2375 | case OFORMAT_JSON: | |
9699bf0d SH |
2376 | if (chain) { |
2377 | dump_json_image_info_list(list); | |
2378 | } else { | |
2379 | dump_json_image_info(list->value); | |
2380 | } | |
c054b3fd | 2381 | break; |
faea38e7 | 2382 | } |
c054b3fd | 2383 | |
9699bf0d | 2384 | qapi_free_ImageInfoList(list); |
ea2384d3 FB |
2385 | return 0; |
2386 | } | |
2387 | ||
4c93a13b PB |
2388 | static void dump_map_entry(OutputFormat output_format, MapEntry *e, |
2389 | MapEntry *next) | |
2390 | { | |
2391 | switch (output_format) { | |
2392 | case OFORMAT_HUMAN: | |
16b0d555 | 2393 | if (e->data && !e->has_offset) { |
4c93a13b PB |
2394 | error_report("File contains external, encrypted or compressed clusters."); |
2395 | exit(1); | |
2396 | } | |
16b0d555 | 2397 | if (e->data && !e->zero) { |
4c93a13b | 2398 | printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n", |
16b0d555 FZ |
2399 | e->start, e->length, |
2400 | e->has_offset ? e->offset : 0, | |
2401 | e->has_filename ? e->filename : ""); | |
4c93a13b PB |
2402 | } |
2403 | /* This format ignores the distinction between 0, ZERO and ZERO|DATA. | |
2404 | * Modify the flags here to allow more coalescing. | |
2405 | */ | |
16b0d555 FZ |
2406 | if (next && (!next->data || next->zero)) { |
2407 | next->data = false; | |
2408 | next->zero = true; | |
4c93a13b PB |
2409 | } |
2410 | break; | |
2411 | case OFORMAT_JSON: | |
16b0d555 FZ |
2412 | printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64"," |
2413 | " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s", | |
4c93a13b PB |
2414 | (e->start == 0 ? "[" : ",\n"), |
2415 | e->start, e->length, e->depth, | |
16b0d555 FZ |
2416 | e->zero ? "true" : "false", |
2417 | e->data ? "true" : "false"); | |
2418 | if (e->has_offset) { | |
c745bfb4 | 2419 | printf(", \"offset\": %"PRId64"", e->offset); |
4c93a13b PB |
2420 | } |
2421 | putchar('}'); | |
2422 | ||
2423 | if (!next) { | |
2424 | printf("]\n"); | |
2425 | } | |
2426 | break; | |
2427 | } | |
2428 | } | |
2429 | ||
2430 | static int get_block_status(BlockDriverState *bs, int64_t sector_num, | |
2431 | int nb_sectors, MapEntry *e) | |
2432 | { | |
2433 | int64_t ret; | |
2434 | int depth; | |
67a0fd2a | 2435 | BlockDriverState *file; |
2875645b | 2436 | bool has_offset; |
4c93a13b PB |
2437 | |
2438 | /* As an optimization, we could cache the current range of unallocated | |
2439 | * clusters in each file of the chain, and avoid querying the same | |
2440 | * range repeatedly. | |
2441 | */ | |
2442 | ||
2443 | depth = 0; | |
2444 | for (;;) { | |
67a0fd2a FZ |
2445 | ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors, |
2446 | &file); | |
4c93a13b PB |
2447 | if (ret < 0) { |
2448 | return ret; | |
2449 | } | |
2450 | assert(nb_sectors); | |
2451 | if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) { | |
2452 | break; | |
2453 | } | |
760e0063 | 2454 | bs = backing_bs(bs); |
4c93a13b PB |
2455 | if (bs == NULL) { |
2456 | ret = 0; | |
2457 | break; | |
2458 | } | |
2459 | ||
2460 | depth++; | |
2461 | } | |
2462 | ||
2875645b JS |
2463 | has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID); |
2464 | ||
2465 | *e = (MapEntry) { | |
2466 | .start = sector_num * BDRV_SECTOR_SIZE, | |
2467 | .length = nb_sectors * BDRV_SECTOR_SIZE, | |
2468 | .data = !!(ret & BDRV_BLOCK_DATA), | |
2469 | .zero = !!(ret & BDRV_BLOCK_ZERO), | |
2470 | .offset = ret & BDRV_BLOCK_OFFSET_MASK, | |
2471 | .has_offset = has_offset, | |
2472 | .depth = depth, | |
2473 | .has_filename = file && has_offset, | |
2474 | .filename = file && has_offset ? file->filename : NULL, | |
2475 | }; | |
2476 | ||
4c93a13b PB |
2477 | return 0; |
2478 | } | |
2479 | ||
16b0d555 FZ |
2480 | static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next) |
2481 | { | |
2482 | if (curr->length == 0) { | |
2483 | return false; | |
2484 | } | |
2485 | if (curr->zero != next->zero || | |
2486 | curr->data != next->data || | |
2487 | curr->depth != next->depth || | |
2488 | curr->has_filename != next->has_filename || | |
2489 | curr->has_offset != next->has_offset) { | |
2490 | return false; | |
2491 | } | |
2492 | if (curr->has_filename && strcmp(curr->filename, next->filename)) { | |
2493 | return false; | |
2494 | } | |
2495 | if (curr->has_offset && curr->offset + curr->length != next->offset) { | |
2496 | return false; | |
2497 | } | |
2498 | return true; | |
2499 | } | |
2500 | ||
4c93a13b PB |
2501 | static int img_map(int argc, char **argv) |
2502 | { | |
2503 | int c; | |
2504 | OutputFormat output_format = OFORMAT_HUMAN; | |
26f54e9a | 2505 | BlockBackend *blk; |
4c93a13b PB |
2506 | BlockDriverState *bs; |
2507 | const char *filename, *fmt, *output; | |
2508 | int64_t length; | |
2509 | MapEntry curr = { .length = 0 }, next; | |
2510 | int ret = 0; | |
3babeb15 | 2511 | Error *local_err = NULL; |
eb769f74 | 2512 | bool image_opts = false; |
4c93a13b PB |
2513 | |
2514 | fmt = NULL; | |
2515 | output = NULL; | |
2516 | for (;;) { | |
2517 | int option_index = 0; | |
2518 | static const struct option long_options[] = { | |
2519 | {"help", no_argument, 0, 'h'}, | |
2520 | {"format", required_argument, 0, 'f'}, | |
2521 | {"output", required_argument, 0, OPTION_OUTPUT}, | |
3babeb15 | 2522 | {"object", required_argument, 0, OPTION_OBJECT}, |
eb769f74 | 2523 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
4c93a13b PB |
2524 | {0, 0, 0, 0} |
2525 | }; | |
2526 | c = getopt_long(argc, argv, "f:h", | |
2527 | long_options, &option_index); | |
2528 | if (c == -1) { | |
2529 | break; | |
2530 | } | |
2531 | switch (c) { | |
2532 | case '?': | |
2533 | case 'h': | |
2534 | help(); | |
2535 | break; | |
2536 | case 'f': | |
2537 | fmt = optarg; | |
2538 | break; | |
2539 | case OPTION_OUTPUT: | |
2540 | output = optarg; | |
2541 | break; | |
3babeb15 DB |
2542 | case OPTION_OBJECT: { |
2543 | QemuOpts *opts; | |
2544 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
2545 | optarg, true); | |
2546 | if (!opts) { | |
2547 | return 1; | |
2548 | } | |
2549 | } break; | |
eb769f74 DB |
2550 | case OPTION_IMAGE_OPTS: |
2551 | image_opts = true; | |
2552 | break; | |
4c93a13b PB |
2553 | } |
2554 | } | |
ac1307ab FZ |
2555 | if (optind != argc - 1) { |
2556 | error_exit("Expecting one image file name"); | |
4c93a13b | 2557 | } |
ac1307ab | 2558 | filename = argv[optind]; |
4c93a13b PB |
2559 | |
2560 | if (output && !strcmp(output, "json")) { | |
2561 | output_format = OFORMAT_JSON; | |
2562 | } else if (output && !strcmp(output, "human")) { | |
2563 | output_format = OFORMAT_HUMAN; | |
2564 | } else if (output) { | |
2565 | error_report("--output must be used with human or json as argument."); | |
2566 | return 1; | |
2567 | } | |
2568 | ||
3babeb15 DB |
2569 | if (qemu_opts_foreach(&qemu_object_opts, |
2570 | user_creatable_add_opts_foreach, | |
2571 | NULL, &local_err)) { | |
2572 | error_report_err(local_err); | |
2573 | return 1; | |
2574 | } | |
2575 | ||
ce099547 | 2576 | blk = img_open(image_opts, filename, fmt, 0, false, false); |
7e7d56d9 MA |
2577 | if (!blk) { |
2578 | return 1; | |
4c93a13b | 2579 | } |
7e7d56d9 | 2580 | bs = blk_bs(blk); |
4c93a13b PB |
2581 | |
2582 | if (output_format == OFORMAT_HUMAN) { | |
2583 | printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File"); | |
2584 | } | |
2585 | ||
f1d3cd79 | 2586 | length = blk_getlength(blk); |
4c93a13b PB |
2587 | while (curr.start + curr.length < length) { |
2588 | int64_t nsectors_left; | |
2589 | int64_t sector_num; | |
2590 | int n; | |
2591 | ||
2592 | sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS; | |
2593 | ||
2594 | /* Probe up to 1 GiB at a time. */ | |
2595 | nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num; | |
2596 | n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left); | |
2597 | ret = get_block_status(bs, sector_num, n, &next); | |
2598 | ||
2599 | if (ret < 0) { | |
2600 | error_report("Could not read file metadata: %s", strerror(-ret)); | |
2601 | goto out; | |
2602 | } | |
2603 | ||
16b0d555 | 2604 | if (entry_mergeable(&curr, &next)) { |
4c93a13b PB |
2605 | curr.length += next.length; |
2606 | continue; | |
2607 | } | |
2608 | ||
2609 | if (curr.length > 0) { | |
2610 | dump_map_entry(output_format, &curr, &next); | |
2611 | } | |
2612 | curr = next; | |
2613 | } | |
2614 | ||
2615 | dump_map_entry(output_format, &curr, NULL); | |
2616 | ||
2617 | out: | |
26f54e9a | 2618 | blk_unref(blk); |
4c93a13b PB |
2619 | return ret < 0; |
2620 | } | |
2621 | ||
f7b4a940 AL |
2622 | #define SNAPSHOT_LIST 1 |
2623 | #define SNAPSHOT_CREATE 2 | |
2624 | #define SNAPSHOT_APPLY 3 | |
2625 | #define SNAPSHOT_DELETE 4 | |
2626 | ||
153859be | 2627 | static int img_snapshot(int argc, char **argv) |
f7b4a940 | 2628 | { |
26f54e9a | 2629 | BlockBackend *blk; |
f7b4a940 AL |
2630 | BlockDriverState *bs; |
2631 | QEMUSnapshotInfo sn; | |
2632 | char *filename, *snapshot_name = NULL; | |
c2abccec | 2633 | int c, ret = 0, bdrv_oflags; |
f7b4a940 AL |
2634 | int action = 0; |
2635 | qemu_timeval tv; | |
f382d43a | 2636 | bool quiet = false; |
a89d89d3 | 2637 | Error *err = NULL; |
eb769f74 | 2638 | bool image_opts = false; |
f7b4a940 | 2639 | |
ce099547 | 2640 | bdrv_oflags = BDRV_O_RDWR; |
f7b4a940 AL |
2641 | /* Parse commandline parameters */ |
2642 | for(;;) { | |
3babeb15 DB |
2643 | static const struct option long_options[] = { |
2644 | {"help", no_argument, 0, 'h'}, | |
2645 | {"object", required_argument, 0, OPTION_OBJECT}, | |
eb769f74 | 2646 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
3babeb15 DB |
2647 | {0, 0, 0, 0} |
2648 | }; | |
2649 | c = getopt_long(argc, argv, "la:c:d:hq", | |
2650 | long_options, NULL); | |
b8fb60da | 2651 | if (c == -1) { |
f7b4a940 | 2652 | break; |
b8fb60da | 2653 | } |
f7b4a940 | 2654 | switch(c) { |
ef87394c | 2655 | case '?': |
f7b4a940 AL |
2656 | case 'h': |
2657 | help(); | |
153859be | 2658 | return 0; |
f7b4a940 AL |
2659 | case 'l': |
2660 | if (action) { | |
ac1307ab | 2661 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
153859be | 2662 | return 0; |
f7b4a940 AL |
2663 | } |
2664 | action = SNAPSHOT_LIST; | |
f5edb014 | 2665 | bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */ |
f7b4a940 AL |
2666 | break; |
2667 | case 'a': | |
2668 | if (action) { | |
ac1307ab | 2669 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
153859be | 2670 | return 0; |
f7b4a940 AL |
2671 | } |
2672 | action = SNAPSHOT_APPLY; | |
2673 | snapshot_name = optarg; | |
2674 | break; | |
2675 | case 'c': | |
2676 | if (action) { | |
ac1307ab | 2677 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
153859be | 2678 | return 0; |
f7b4a940 AL |
2679 | } |
2680 | action = SNAPSHOT_CREATE; | |
2681 | snapshot_name = optarg; | |
2682 | break; | |
2683 | case 'd': | |
2684 | if (action) { | |
ac1307ab | 2685 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
153859be | 2686 | return 0; |
f7b4a940 AL |
2687 | } |
2688 | action = SNAPSHOT_DELETE; | |
2689 | snapshot_name = optarg; | |
2690 | break; | |
f382d43a MR |
2691 | case 'q': |
2692 | quiet = true; | |
2693 | break; | |
3babeb15 DB |
2694 | case OPTION_OBJECT: { |
2695 | QemuOpts *opts; | |
2696 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
2697 | optarg, true); | |
2698 | if (!opts) { | |
2699 | return 1; | |
2700 | } | |
2701 | } break; | |
eb769f74 DB |
2702 | case OPTION_IMAGE_OPTS: |
2703 | image_opts = true; | |
2704 | break; | |
f7b4a940 AL |
2705 | } |
2706 | } | |
2707 | ||
fc11eb26 | 2708 | if (optind != argc - 1) { |
ac1307ab | 2709 | error_exit("Expecting one image file name"); |
b8fb60da | 2710 | } |
f7b4a940 AL |
2711 | filename = argv[optind++]; |
2712 | ||
3babeb15 DB |
2713 | if (qemu_opts_foreach(&qemu_object_opts, |
2714 | user_creatable_add_opts_foreach, | |
2715 | NULL, &err)) { | |
2716 | error_report_err(err); | |
2717 | return 1; | |
2718 | } | |
2719 | ||
f7b4a940 | 2720 | /* Open the image */ |
ce099547 | 2721 | blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet); |
7e7d56d9 MA |
2722 | if (!blk) { |
2723 | return 1; | |
c2abccec | 2724 | } |
7e7d56d9 | 2725 | bs = blk_bs(blk); |
f7b4a940 AL |
2726 | |
2727 | /* Perform the requested action */ | |
2728 | switch(action) { | |
2729 | case SNAPSHOT_LIST: | |
2730 | dump_snapshots(bs); | |
2731 | break; | |
2732 | ||
2733 | case SNAPSHOT_CREATE: | |
2734 | memset(&sn, 0, sizeof(sn)); | |
2735 | pstrcpy(sn.name, sizeof(sn.name), snapshot_name); | |
2736 | ||
2737 | qemu_gettimeofday(&tv); | |
2738 | sn.date_sec = tv.tv_sec; | |
2739 | sn.date_nsec = tv.tv_usec * 1000; | |
2740 | ||
2741 | ret = bdrv_snapshot_create(bs, &sn); | |
b8fb60da | 2742 | if (ret) { |
15654a6d | 2743 | error_report("Could not create snapshot '%s': %d (%s)", |
f7b4a940 | 2744 | snapshot_name, ret, strerror(-ret)); |
b8fb60da | 2745 | } |
f7b4a940 AL |
2746 | break; |
2747 | ||
2748 | case SNAPSHOT_APPLY: | |
2749 | ret = bdrv_snapshot_goto(bs, snapshot_name); | |
b8fb60da | 2750 | if (ret) { |
15654a6d | 2751 | error_report("Could not apply snapshot '%s': %d (%s)", |
f7b4a940 | 2752 | snapshot_name, ret, strerror(-ret)); |
b8fb60da | 2753 | } |
f7b4a940 AL |
2754 | break; |
2755 | ||
2756 | case SNAPSHOT_DELETE: | |
a89d89d3 | 2757 | bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err); |
84d18f06 | 2758 | if (err) { |
c29b77f9 MA |
2759 | error_reportf_err(err, "Could not delete snapshot '%s': ", |
2760 | snapshot_name); | |
a89d89d3 | 2761 | ret = 1; |
b8fb60da | 2762 | } |
f7b4a940 AL |
2763 | break; |
2764 | } | |
2765 | ||
2766 | /* Cleanup */ | |
26f54e9a | 2767 | blk_unref(blk); |
c2abccec MK |
2768 | if (ret) { |
2769 | return 1; | |
2770 | } | |
153859be | 2771 | return 0; |
f7b4a940 AL |
2772 | } |
2773 | ||
3e85c6fd KW |
2774 | static int img_rebase(int argc, char **argv) |
2775 | { | |
26f54e9a | 2776 | BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL; |
396374ca PB |
2777 | uint8_t *buf_old = NULL; |
2778 | uint8_t *buf_new = NULL; | |
f1d3cd79 | 2779 | BlockDriverState *bs = NULL; |
3e85c6fd | 2780 | char *filename; |
40055951 HR |
2781 | const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg; |
2782 | int c, flags, src_flags, ret; | |
ce099547 | 2783 | bool writethrough, src_writethrough; |
3e85c6fd | 2784 | int unsafe = 0; |
6b837bc4 | 2785 | int progress = 0; |
f382d43a | 2786 | bool quiet = false; |
34b5d2c6 | 2787 | Error *local_err = NULL; |
eb769f74 | 2788 | bool image_opts = false; |
3e85c6fd KW |
2789 | |
2790 | /* Parse commandline parameters */ | |
e53dbee0 | 2791 | fmt = NULL; |
661a0f71 | 2792 | cache = BDRV_DEFAULT_CACHE; |
40055951 | 2793 | src_cache = BDRV_DEFAULT_CACHE; |
3e85c6fd KW |
2794 | out_baseimg = NULL; |
2795 | out_basefmt = NULL; | |
3e85c6fd | 2796 | for(;;) { |
3babeb15 DB |
2797 | static const struct option long_options[] = { |
2798 | {"help", no_argument, 0, 'h'}, | |
2799 | {"object", required_argument, 0, OPTION_OBJECT}, | |
eb769f74 | 2800 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
3babeb15 DB |
2801 | {0, 0, 0, 0} |
2802 | }; | |
2803 | c = getopt_long(argc, argv, "hf:F:b:upt:T:q", | |
2804 | long_options, NULL); | |
b8fb60da | 2805 | if (c == -1) { |
3e85c6fd | 2806 | break; |
b8fb60da | 2807 | } |
3e85c6fd | 2808 | switch(c) { |
ef87394c | 2809 | case '?': |
3e85c6fd KW |
2810 | case 'h': |
2811 | help(); | |
2812 | return 0; | |
e53dbee0 KW |
2813 | case 'f': |
2814 | fmt = optarg; | |
2815 | break; | |
3e85c6fd KW |
2816 | case 'F': |
2817 | out_basefmt = optarg; | |
2818 | break; | |
2819 | case 'b': | |
2820 | out_baseimg = optarg; | |
2821 | break; | |
2822 | case 'u': | |
2823 | unsafe = 1; | |
2824 | break; | |
6b837bc4 JS |
2825 | case 'p': |
2826 | progress = 1; | |
2827 | break; | |
661a0f71 FS |
2828 | case 't': |
2829 | cache = optarg; | |
2830 | break; | |
40055951 HR |
2831 | case 'T': |
2832 | src_cache = optarg; | |
2833 | break; | |
f382d43a MR |
2834 | case 'q': |
2835 | quiet = true; | |
2836 | break; | |
3babeb15 DB |
2837 | case OPTION_OBJECT: { |
2838 | QemuOpts *opts; | |
2839 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
2840 | optarg, true); | |
2841 | if (!opts) { | |
2842 | return 1; | |
2843 | } | |
2844 | } break; | |
eb769f74 DB |
2845 | case OPTION_IMAGE_OPTS: |
2846 | image_opts = true; | |
2847 | break; | |
3e85c6fd KW |
2848 | } |
2849 | } | |
2850 | ||
f382d43a MR |
2851 | if (quiet) { |
2852 | progress = 0; | |
2853 | } | |
2854 | ||
ac1307ab FZ |
2855 | if (optind != argc - 1) { |
2856 | error_exit("Expecting one image file name"); | |
2857 | } | |
2858 | if (!unsafe && !out_baseimg) { | |
2859 | error_exit("Must specify backing file (-b) or use unsafe mode (-u)"); | |
b8fb60da | 2860 | } |
3e85c6fd KW |
2861 | filename = argv[optind++]; |
2862 | ||
3babeb15 DB |
2863 | if (qemu_opts_foreach(&qemu_object_opts, |
2864 | user_creatable_add_opts_foreach, | |
2865 | NULL, &local_err)) { | |
2866 | error_report_err(local_err); | |
2867 | return 1; | |
2868 | } | |
2869 | ||
6b837bc4 JS |
2870 | qemu_progress_init(progress, 2.0); |
2871 | qemu_progress_print(0, 100); | |
2872 | ||
661a0f71 | 2873 | flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0); |
ce099547 | 2874 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
661a0f71 FS |
2875 | if (ret < 0) { |
2876 | error_report("Invalid cache option: %s", cache); | |
40ed35a3 | 2877 | goto out; |
661a0f71 FS |
2878 | } |
2879 | ||
ce099547 KW |
2880 | src_flags = 0; |
2881 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); | |
40055951 HR |
2882 | if (ret < 0) { |
2883 | error_report("Invalid source cache option: %s", src_cache); | |
40ed35a3 | 2884 | goto out; |
40055951 HR |
2885 | } |
2886 | ||
ce099547 KW |
2887 | /* The source files are opened read-only, don't care about WCE */ |
2888 | assert((src_flags & BDRV_O_RDWR) == 0); | |
2889 | (void) src_writethrough; | |
2890 | ||
3e85c6fd KW |
2891 | /* |
2892 | * Open the images. | |
2893 | * | |
2894 | * Ignore the old backing file for unsafe rebase in case we want to correct | |
2895 | * the reference to a renamed or moved backing file. | |
2896 | */ | |
ce099547 | 2897 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet); |
7e7d56d9 | 2898 | if (!blk) { |
40ed35a3 SH |
2899 | ret = -1; |
2900 | goto out; | |
c2abccec | 2901 | } |
7e7d56d9 | 2902 | bs = blk_bs(blk); |
3e85c6fd | 2903 | |
3e85c6fd | 2904 | if (out_basefmt != NULL) { |
644483d9 | 2905 | if (bdrv_find_format(out_basefmt) == NULL) { |
15654a6d | 2906 | error_report("Invalid format name: '%s'", out_basefmt); |
c2abccec MK |
2907 | ret = -1; |
2908 | goto out; | |
3e85c6fd KW |
2909 | } |
2910 | } | |
2911 | ||
2912 | /* For safe rebasing we need to compare old and new backing file */ | |
40ed35a3 | 2913 | if (!unsafe) { |
9a29e18f | 2914 | char backing_name[PATH_MAX]; |
644483d9 HR |
2915 | QDict *options = NULL; |
2916 | ||
2917 | if (bs->backing_format[0] != '\0') { | |
2918 | options = qdict_new(); | |
2919 | qdict_put(options, "driver", qstring_from_str(bs->backing_format)); | |
2920 | } | |
3e85c6fd | 2921 | |
3e85c6fd | 2922 | bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name)); |
efaa7c4e | 2923 | blk_old_backing = blk_new_open(backing_name, NULL, |
644483d9 HR |
2924 | options, src_flags, &local_err); |
2925 | if (!blk_old_backing) { | |
c29b77f9 MA |
2926 | error_reportf_err(local_err, |
2927 | "Could not open old backing file '%s': ", | |
2928 | backing_name); | |
c2abccec | 2929 | goto out; |
3e85c6fd | 2930 | } |
644483d9 | 2931 | |
a616673d | 2932 | if (out_baseimg[0]) { |
644483d9 HR |
2933 | if (out_basefmt) { |
2934 | options = qdict_new(); | |
2935 | qdict_put(options, "driver", qstring_from_str(out_basefmt)); | |
2936 | } else { | |
2937 | options = NULL; | |
2938 | } | |
2939 | ||
efaa7c4e | 2940 | blk_new_backing = blk_new_open(out_baseimg, NULL, |
644483d9 HR |
2941 | options, src_flags, &local_err); |
2942 | if (!blk_new_backing) { | |
c29b77f9 MA |
2943 | error_reportf_err(local_err, |
2944 | "Could not open new backing file '%s': ", | |
2945 | out_baseimg); | |
a616673d AB |
2946 | goto out; |
2947 | } | |
3e85c6fd KW |
2948 | } |
2949 | } | |
2950 | ||
2951 | /* | |
2952 | * Check each unallocated cluster in the COW file. If it is unallocated, | |
2953 | * accesses go to the backing file. We must therefore compare this cluster | |
2954 | * in the old and new backing file, and if they differ we need to copy it | |
2955 | * from the old backing file into the COW file. | |
2956 | * | |
2957 | * If qemu-img crashes during this step, no harm is done. The content of | |
2958 | * the image is the same as the original one at any time. | |
2959 | */ | |
2960 | if (!unsafe) { | |
52bf1e72 MA |
2961 | int64_t num_sectors; |
2962 | int64_t old_backing_num_sectors; | |
2963 | int64_t new_backing_num_sectors = 0; | |
3e85c6fd | 2964 | uint64_t sector; |
cc60e327 | 2965 | int n; |
1f710495 | 2966 | float local_progress = 0; |
d6771bfa | 2967 | |
f1d3cd79 HR |
2968 | buf_old = blk_blockalign(blk, IO_BUF_SIZE); |
2969 | buf_new = blk_blockalign(blk, IO_BUF_SIZE); | |
3e85c6fd | 2970 | |
f1d3cd79 | 2971 | num_sectors = blk_nb_sectors(blk); |
52bf1e72 MA |
2972 | if (num_sectors < 0) { |
2973 | error_report("Could not get size of '%s': %s", | |
2974 | filename, strerror(-num_sectors)); | |
2975 | ret = -1; | |
2976 | goto out; | |
2977 | } | |
f1d3cd79 | 2978 | old_backing_num_sectors = blk_nb_sectors(blk_old_backing); |
52bf1e72 | 2979 | if (old_backing_num_sectors < 0) { |
9a29e18f | 2980 | char backing_name[PATH_MAX]; |
52bf1e72 MA |
2981 | |
2982 | bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name)); | |
2983 | error_report("Could not get size of '%s': %s", | |
2984 | backing_name, strerror(-old_backing_num_sectors)); | |
2985 | ret = -1; | |
2986 | goto out; | |
2987 | } | |
f1d3cd79 HR |
2988 | if (blk_new_backing) { |
2989 | new_backing_num_sectors = blk_nb_sectors(blk_new_backing); | |
52bf1e72 MA |
2990 | if (new_backing_num_sectors < 0) { |
2991 | error_report("Could not get size of '%s': %s", | |
2992 | out_baseimg, strerror(-new_backing_num_sectors)); | |
2993 | ret = -1; | |
2994 | goto out; | |
2995 | } | |
a616673d | 2996 | } |
3e85c6fd | 2997 | |
1f710495 KW |
2998 | if (num_sectors != 0) { |
2999 | local_progress = (float)100 / | |
3000 | (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512)); | |
3001 | } | |
3002 | ||
3e85c6fd KW |
3003 | for (sector = 0; sector < num_sectors; sector += n) { |
3004 | ||
3005 | /* How many sectors can we handle with the next read? */ | |
3006 | if (sector + (IO_BUF_SIZE / 512) <= num_sectors) { | |
3007 | n = (IO_BUF_SIZE / 512); | |
3008 | } else { | |
3009 | n = num_sectors - sector; | |
3010 | } | |
3011 | ||
3012 | /* If the cluster is allocated, we don't need to take action */ | |
cc60e327 | 3013 | ret = bdrv_is_allocated(bs, sector, n, &n); |
d663640c PB |
3014 | if (ret < 0) { |
3015 | error_report("error while reading image metadata: %s", | |
3016 | strerror(-ret)); | |
3017 | goto out; | |
3018 | } | |
cc60e327 | 3019 | if (ret) { |
3e85c6fd KW |
3020 | continue; |
3021 | } | |
3022 | ||
87a1b3e3 KW |
3023 | /* |
3024 | * Read old and new backing file and take into consideration that | |
3025 | * backing files may be smaller than the COW image. | |
3026 | */ | |
3027 | if (sector >= old_backing_num_sectors) { | |
3028 | memset(buf_old, 0, n * BDRV_SECTOR_SIZE); | |
3029 | } else { | |
3030 | if (sector + n > old_backing_num_sectors) { | |
3031 | n = old_backing_num_sectors - sector; | |
3032 | } | |
3033 | ||
f1d3cd79 | 3034 | ret = blk_read(blk_old_backing, sector, buf_old, n); |
87a1b3e3 KW |
3035 | if (ret < 0) { |
3036 | error_report("error while reading from old backing file"); | |
3037 | goto out; | |
3038 | } | |
3e85c6fd | 3039 | } |
87a1b3e3 | 3040 | |
f1d3cd79 | 3041 | if (sector >= new_backing_num_sectors || !blk_new_backing) { |
87a1b3e3 KW |
3042 | memset(buf_new, 0, n * BDRV_SECTOR_SIZE); |
3043 | } else { | |
3044 | if (sector + n > new_backing_num_sectors) { | |
3045 | n = new_backing_num_sectors - sector; | |
3046 | } | |
3047 | ||
f1d3cd79 | 3048 | ret = blk_read(blk_new_backing, sector, buf_new, n); |
87a1b3e3 KW |
3049 | if (ret < 0) { |
3050 | error_report("error while reading from new backing file"); | |
3051 | goto out; | |
3052 | } | |
3e85c6fd KW |
3053 | } |
3054 | ||
3055 | /* If they differ, we need to write to the COW file */ | |
3056 | uint64_t written = 0; | |
3057 | ||
3058 | while (written < n) { | |
3059 | int pnum; | |
3060 | ||
3061 | if (compare_sectors(buf_old + written * 512, | |
60b1bd4f | 3062 | buf_new + written * 512, n - written, &pnum)) |
3e85c6fd | 3063 | { |
f1d3cd79 HR |
3064 | ret = blk_write(blk, sector + written, |
3065 | buf_old + written * 512, pnum); | |
3e85c6fd | 3066 | if (ret < 0) { |
15654a6d | 3067 | error_report("Error while writing to COW image: %s", |
3e85c6fd | 3068 | strerror(-ret)); |
c2abccec | 3069 | goto out; |
3e85c6fd KW |
3070 | } |
3071 | } | |
3072 | ||
3073 | written += pnum; | |
3074 | } | |
6b837bc4 | 3075 | qemu_progress_print(local_progress, 100); |
3e85c6fd KW |
3076 | } |
3077 | } | |
3078 | ||
3079 | /* | |
3080 | * Change the backing file. All clusters that are different from the old | |
3081 | * backing file are overwritten in the COW file now, so the visible content | |
3082 | * doesn't change when we switch the backing file. | |
3083 | */ | |
a616673d AB |
3084 | if (out_baseimg && *out_baseimg) { |
3085 | ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt); | |
3086 | } else { | |
3087 | ret = bdrv_change_backing_file(bs, NULL, NULL); | |
3088 | } | |
3089 | ||
3e85c6fd | 3090 | if (ret == -ENOSPC) { |
15654a6d JS |
3091 | error_report("Could not change the backing file to '%s': No " |
3092 | "space left in the file header", out_baseimg); | |
3e85c6fd | 3093 | } else if (ret < 0) { |
15654a6d | 3094 | error_report("Could not change the backing file to '%s': %s", |
3e85c6fd KW |
3095 | out_baseimg, strerror(-ret)); |
3096 | } | |
3097 | ||
6b837bc4 | 3098 | qemu_progress_print(100, 0); |
3e85c6fd KW |
3099 | /* |
3100 | * TODO At this point it is possible to check if any clusters that are | |
3101 | * allocated in the COW file are the same in the backing file. If so, they | |
3102 | * could be dropped from the COW file. Don't do this before switching the | |
3103 | * backing file, in case of a crash this would lead to corruption. | |
3104 | */ | |
c2abccec | 3105 | out: |
6b837bc4 | 3106 | qemu_progress_end(); |
3e85c6fd KW |
3107 | /* Cleanup */ |
3108 | if (!unsafe) { | |
26f54e9a | 3109 | blk_unref(blk_old_backing); |
26f54e9a | 3110 | blk_unref(blk_new_backing); |
3e85c6fd | 3111 | } |
396374ca PB |
3112 | qemu_vfree(buf_old); |
3113 | qemu_vfree(buf_new); | |
3e85c6fd | 3114 | |
26f54e9a | 3115 | blk_unref(blk); |
c2abccec MK |
3116 | if (ret) { |
3117 | return 1; | |
3118 | } | |
3e85c6fd KW |
3119 | return 0; |
3120 | } | |
3121 | ||
ae6b0ed6 SH |
3122 | static int img_resize(int argc, char **argv) |
3123 | { | |
6750e795 | 3124 | Error *err = NULL; |
ae6b0ed6 SH |
3125 | int c, ret, relative; |
3126 | const char *filename, *fmt, *size; | |
3127 | int64_t n, total_size; | |
f382d43a | 3128 | bool quiet = false; |
26f54e9a | 3129 | BlockBackend *blk = NULL; |
20caf0f7 | 3130 | QemuOpts *param; |
3babeb15 DB |
3131 | Error *local_err = NULL; |
3132 | ||
20caf0f7 DXW |
3133 | static QemuOptsList resize_options = { |
3134 | .name = "resize_options", | |
3135 | .head = QTAILQ_HEAD_INITIALIZER(resize_options.head), | |
3136 | .desc = { | |
3137 | { | |
3138 | .name = BLOCK_OPT_SIZE, | |
3139 | .type = QEMU_OPT_SIZE, | |
3140 | .help = "Virtual disk size" | |
3141 | }, { | |
3142 | /* end of list */ | |
3143 | } | |
ae6b0ed6 | 3144 | }, |
ae6b0ed6 | 3145 | }; |
eb769f74 | 3146 | bool image_opts = false; |
ae6b0ed6 | 3147 | |
e80fec7f KW |
3148 | /* Remove size from argv manually so that negative numbers are not treated |
3149 | * as options by getopt. */ | |
3150 | if (argc < 3) { | |
ac1307ab | 3151 | error_exit("Not enough arguments"); |
e80fec7f KW |
3152 | return 1; |
3153 | } | |
3154 | ||
3155 | size = argv[--argc]; | |
3156 | ||
3157 | /* Parse getopt arguments */ | |
ae6b0ed6 SH |
3158 | fmt = NULL; |
3159 | for(;;) { | |
3babeb15 DB |
3160 | static const struct option long_options[] = { |
3161 | {"help", no_argument, 0, 'h'}, | |
3162 | {"object", required_argument, 0, OPTION_OBJECT}, | |
eb769f74 | 3163 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
3babeb15 DB |
3164 | {0, 0, 0, 0} |
3165 | }; | |
3166 | c = getopt_long(argc, argv, "f:hq", | |
3167 | long_options, NULL); | |
ae6b0ed6 SH |
3168 | if (c == -1) { |
3169 | break; | |
3170 | } | |
3171 | switch(c) { | |
ef87394c | 3172 | case '?': |
ae6b0ed6 SH |
3173 | case 'h': |
3174 | help(); | |
3175 | break; | |
3176 | case 'f': | |
3177 | fmt = optarg; | |
3178 | break; | |
f382d43a MR |
3179 | case 'q': |
3180 | quiet = true; | |
3181 | break; | |
3babeb15 DB |
3182 | case OPTION_OBJECT: { |
3183 | QemuOpts *opts; | |
3184 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
3185 | optarg, true); | |
3186 | if (!opts) { | |
3187 | return 1; | |
3188 | } | |
3189 | } break; | |
eb769f74 DB |
3190 | case OPTION_IMAGE_OPTS: |
3191 | image_opts = true; | |
3192 | break; | |
ae6b0ed6 SH |
3193 | } |
3194 | } | |
fc11eb26 | 3195 | if (optind != argc - 1) { |
ac1307ab | 3196 | error_exit("Expecting one image file name"); |
ae6b0ed6 SH |
3197 | } |
3198 | filename = argv[optind++]; | |
ae6b0ed6 | 3199 | |
3babeb15 DB |
3200 | if (qemu_opts_foreach(&qemu_object_opts, |
3201 | user_creatable_add_opts_foreach, | |
3202 | NULL, &local_err)) { | |
3203 | error_report_err(local_err); | |
3204 | return 1; | |
3205 | } | |
3206 | ||
ae6b0ed6 SH |
3207 | /* Choose grow, shrink, or absolute resize mode */ |
3208 | switch (size[0]) { | |
3209 | case '+': | |
3210 | relative = 1; | |
3211 | size++; | |
3212 | break; | |
3213 | case '-': | |
3214 | relative = -1; | |
3215 | size++; | |
3216 | break; | |
3217 | default: | |
3218 | relative = 0; | |
3219 | break; | |
3220 | } | |
3221 | ||
3222 | /* Parse size */ | |
87ea75d5 | 3223 | param = qemu_opts_create(&resize_options, NULL, 0, &error_abort); |
f43e47db | 3224 | qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err); |
6750e795 MA |
3225 | if (err) { |
3226 | error_report_err(err); | |
2a81998a | 3227 | ret = -1; |
20caf0f7 | 3228 | qemu_opts_del(param); |
2a81998a | 3229 | goto out; |
ae6b0ed6 | 3230 | } |
20caf0f7 DXW |
3231 | n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0); |
3232 | qemu_opts_del(param); | |
ae6b0ed6 | 3233 | |
efaa7c4e | 3234 | blk = img_open(image_opts, filename, fmt, |
ce099547 | 3235 | BDRV_O_RDWR, false, quiet); |
7e7d56d9 | 3236 | if (!blk) { |
2a81998a JS |
3237 | ret = -1; |
3238 | goto out; | |
c2abccec | 3239 | } |
ae6b0ed6 SH |
3240 | |
3241 | if (relative) { | |
f1d3cd79 | 3242 | total_size = blk_getlength(blk) + n * relative; |
ae6b0ed6 SH |
3243 | } else { |
3244 | total_size = n; | |
3245 | } | |
3246 | if (total_size <= 0) { | |
15654a6d | 3247 | error_report("New image size must be positive"); |
c2abccec MK |
3248 | ret = -1; |
3249 | goto out; | |
ae6b0ed6 SH |
3250 | } |
3251 | ||
f1d3cd79 | 3252 | ret = blk_truncate(blk, total_size); |
ae6b0ed6 SH |
3253 | switch (ret) { |
3254 | case 0: | |
f382d43a | 3255 | qprintf(quiet, "Image resized.\n"); |
ae6b0ed6 SH |
3256 | break; |
3257 | case -ENOTSUP: | |
259b2173 | 3258 | error_report("This image does not support resize"); |
ae6b0ed6 SH |
3259 | break; |
3260 | case -EACCES: | |
15654a6d | 3261 | error_report("Image is read-only"); |
ae6b0ed6 SH |
3262 | break; |
3263 | default: | |
15654a6d | 3264 | error_report("Error resizing image (%d)", -ret); |
ae6b0ed6 SH |
3265 | break; |
3266 | } | |
c2abccec | 3267 | out: |
26f54e9a | 3268 | blk_unref(blk); |
c2abccec MK |
3269 | if (ret) { |
3270 | return 1; | |
3271 | } | |
ae6b0ed6 SH |
3272 | return 0; |
3273 | } | |
3274 | ||
76a3a34d | 3275 | static void amend_status_cb(BlockDriverState *bs, |
8b13976d HR |
3276 | int64_t offset, int64_t total_work_size, |
3277 | void *opaque) | |
76a3a34d HR |
3278 | { |
3279 | qemu_progress_print(100.f * offset / total_work_size, 0); | |
3280 | } | |
3281 | ||
6f176b48 HR |
3282 | static int img_amend(int argc, char **argv) |
3283 | { | |
dc523cd3 | 3284 | Error *err = NULL; |
6f176b48 HR |
3285 | int c, ret = 0; |
3286 | char *options = NULL; | |
83d0521a CL |
3287 | QemuOptsList *create_opts = NULL; |
3288 | QemuOpts *opts = NULL; | |
bd39e6ed HR |
3289 | const char *fmt = NULL, *filename, *cache; |
3290 | int flags; | |
ce099547 | 3291 | bool writethrough; |
76a3a34d | 3292 | bool quiet = false, progress = false; |
26f54e9a | 3293 | BlockBackend *blk = NULL; |
6f176b48 | 3294 | BlockDriverState *bs = NULL; |
3babeb15 | 3295 | Error *local_err = NULL; |
eb769f74 | 3296 | bool image_opts = false; |
6f176b48 | 3297 | |
bd39e6ed | 3298 | cache = BDRV_DEFAULT_CACHE; |
6f176b48 | 3299 | for (;;) { |
3babeb15 DB |
3300 | static const struct option long_options[] = { |
3301 | {"help", no_argument, 0, 'h'}, | |
3302 | {"object", required_argument, 0, OPTION_OBJECT}, | |
eb769f74 | 3303 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
3babeb15 DB |
3304 | {0, 0, 0, 0} |
3305 | }; | |
3306 | c = getopt_long(argc, argv, "ho:f:t:pq", | |
3307 | long_options, NULL); | |
6f176b48 HR |
3308 | if (c == -1) { |
3309 | break; | |
3310 | } | |
3311 | ||
3312 | switch (c) { | |
3313 | case 'h': | |
3314 | case '?': | |
3315 | help(); | |
3316 | break; | |
3317 | case 'o': | |
626f84f3 KW |
3318 | if (!is_valid_option_list(optarg)) { |
3319 | error_report("Invalid option list: %s", optarg); | |
3320 | ret = -1; | |
e814dffc | 3321 | goto out_no_progress; |
626f84f3 KW |
3322 | } |
3323 | if (!options) { | |
3324 | options = g_strdup(optarg); | |
3325 | } else { | |
3326 | char *old_options = options; | |
3327 | options = g_strdup_printf("%s,%s", options, optarg); | |
3328 | g_free(old_options); | |
3329 | } | |
6f176b48 HR |
3330 | break; |
3331 | case 'f': | |
3332 | fmt = optarg; | |
3333 | break; | |
bd39e6ed HR |
3334 | case 't': |
3335 | cache = optarg; | |
3336 | break; | |
76a3a34d HR |
3337 | case 'p': |
3338 | progress = true; | |
3339 | break; | |
6f176b48 HR |
3340 | case 'q': |
3341 | quiet = true; | |
3342 | break; | |
3babeb15 DB |
3343 | case OPTION_OBJECT: |
3344 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
3345 | optarg, true); | |
3346 | if (!opts) { | |
3347 | ret = -1; | |
3348 | goto out_no_progress; | |
3349 | } | |
3350 | break; | |
eb769f74 DB |
3351 | case OPTION_IMAGE_OPTS: |
3352 | image_opts = true; | |
3353 | break; | |
6f176b48 HR |
3354 | } |
3355 | } | |
3356 | ||
a283cb6e | 3357 | if (!options) { |
ac1307ab | 3358 | error_exit("Must specify options (-o)"); |
6f176b48 HR |
3359 | } |
3360 | ||
3babeb15 DB |
3361 | if (qemu_opts_foreach(&qemu_object_opts, |
3362 | user_creatable_add_opts_foreach, | |
3363 | NULL, &local_err)) { | |
3364 | error_report_err(local_err); | |
3365 | ret = -1; | |
3366 | goto out_no_progress; | |
3367 | } | |
3368 | ||
76a3a34d HR |
3369 | if (quiet) { |
3370 | progress = false; | |
3371 | } | |
3372 | qemu_progress_init(progress, 1.0); | |
3373 | ||
a283cb6e KW |
3374 | filename = (optind == argc - 1) ? argv[argc - 1] : NULL; |
3375 | if (fmt && has_help_option(options)) { | |
3376 | /* If a format is explicitly specified (and possibly no filename is | |
3377 | * given), print option help here */ | |
3378 | ret = print_block_option_help(filename, fmt); | |
3379 | goto out; | |
6f176b48 HR |
3380 | } |
3381 | ||
a283cb6e | 3382 | if (optind != argc - 1) { |
b2f27e44 HR |
3383 | error_report("Expecting one image file name"); |
3384 | ret = -1; | |
3385 | goto out; | |
a283cb6e | 3386 | } |
6f176b48 | 3387 | |
ce099547 KW |
3388 | flags = BDRV_O_RDWR; |
3389 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); | |
bd39e6ed HR |
3390 | if (ret < 0) { |
3391 | error_report("Invalid cache option: %s", cache); | |
3392 | goto out; | |
3393 | } | |
3394 | ||
ce099547 | 3395 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet); |
7e7d56d9 | 3396 | if (!blk) { |
6f176b48 HR |
3397 | ret = -1; |
3398 | goto out; | |
3399 | } | |
7e7d56d9 | 3400 | bs = blk_bs(blk); |
6f176b48 HR |
3401 | |
3402 | fmt = bs->drv->format_name; | |
3403 | ||
626f84f3 | 3404 | if (has_help_option(options)) { |
a283cb6e | 3405 | /* If the format was auto-detected, print option help here */ |
6f176b48 HR |
3406 | ret = print_block_option_help(filename, fmt); |
3407 | goto out; | |
3408 | } | |
3409 | ||
b2439d26 HR |
3410 | if (!bs->drv->create_opts) { |
3411 | error_report("Format driver '%s' does not support any options to amend", | |
3412 | fmt); | |
3413 | ret = -1; | |
3414 | goto out; | |
3415 | } | |
3416 | ||
c282e1fd | 3417 | create_opts = qemu_opts_append(create_opts, bs->drv->create_opts); |
83d0521a | 3418 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
dc523cd3 MA |
3419 | if (options) { |
3420 | qemu_opts_do_parse(opts, options, NULL, &err); | |
3421 | if (err) { | |
97a2ca7a | 3422 | error_report_err(err); |
dc523cd3 MA |
3423 | ret = -1; |
3424 | goto out; | |
3425 | } | |
6f176b48 HR |
3426 | } |
3427 | ||
76a3a34d HR |
3428 | /* In case the driver does not call amend_status_cb() */ |
3429 | qemu_progress_print(0.f, 0); | |
8b13976d | 3430 | ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL); |
76a3a34d | 3431 | qemu_progress_print(100.f, 0); |
6f176b48 HR |
3432 | if (ret < 0) { |
3433 | error_report("Error while amending options: %s", strerror(-ret)); | |
3434 | goto out; | |
3435 | } | |
3436 | ||
3437 | out: | |
76a3a34d HR |
3438 | qemu_progress_end(); |
3439 | ||
e814dffc | 3440 | out_no_progress: |
26f54e9a | 3441 | blk_unref(blk); |
83d0521a CL |
3442 | qemu_opts_del(opts); |
3443 | qemu_opts_free(create_opts); | |
626f84f3 KW |
3444 | g_free(options); |
3445 | ||
6f176b48 HR |
3446 | if (ret) { |
3447 | return 1; | |
3448 | } | |
3449 | return 0; | |
3450 | } | |
3451 | ||
c227f099 | 3452 | static const img_cmd_t img_cmds[] = { |
153859be SB |
3453 | #define DEF(option, callback, arg_string) \ |
3454 | { option, callback }, | |
3455 | #include "qemu-img-cmds.h" | |
3456 | #undef DEF | |
3457 | #undef GEN_DOCS | |
3458 | { NULL, NULL, }, | |
3459 | }; | |
3460 | ||
ea2384d3 FB |
3461 | int main(int argc, char **argv) |
3462 | { | |
c227f099 | 3463 | const img_cmd_t *cmd; |
153859be | 3464 | const char *cmdname; |
2f78e491 | 3465 | Error *local_error = NULL; |
7db1689c | 3466 | int c; |
7db1689c JC |
3467 | static const struct option long_options[] = { |
3468 | {"help", no_argument, 0, 'h'}, | |
5f6979cb | 3469 | {"version", no_argument, 0, 'v'}, |
7db1689c JC |
3470 | {0, 0, 0, 0} |
3471 | }; | |
ea2384d3 | 3472 | |
526eda14 MK |
3473 | #ifdef CONFIG_POSIX |
3474 | signal(SIGPIPE, SIG_IGN); | |
3475 | #endif | |
3476 | ||
53f76e58 | 3477 | error_set_progname(argv[0]); |
10f5bff6 | 3478 | qemu_init_exec_dir(argv[0]); |
53f76e58 | 3479 | |
2f78e491 | 3480 | if (qemu_init_main_loop(&local_error)) { |
565f65d2 | 3481 | error_report_err(local_error); |
2f78e491 CN |
3482 | exit(EXIT_FAILURE); |
3483 | } | |
3484 | ||
064097d9 | 3485 | module_call_init(MODULE_INIT_QOM); |
ea2384d3 | 3486 | bdrv_init(); |
ac1307ab FZ |
3487 | if (argc < 2) { |
3488 | error_exit("Not enough arguments"); | |
3489 | } | |
153859be | 3490 | cmdname = argv[1]; |
153859be | 3491 | |
3babeb15 | 3492 | qemu_add_opts(&qemu_object_opts); |
eb769f74 | 3493 | qemu_add_opts(&qemu_source_opts); |
3babeb15 | 3494 | |
153859be | 3495 | /* find the command */ |
5f6979cb | 3496 | for (cmd = img_cmds; cmd->name != NULL; cmd++) { |
153859be | 3497 | if (!strcmp(cmdname, cmd->name)) { |
7db1689c | 3498 | return cmd->handler(argc - 1, argv + 1); |
153859be | 3499 | } |
ea2384d3 | 3500 | } |
153859be | 3501 | |
5f6979cb | 3502 | c = getopt_long(argc, argv, "h", long_options, NULL); |
7db1689c JC |
3503 | |
3504 | if (c == 'h') { | |
3505 | help(); | |
3506 | } | |
5f6979cb JC |
3507 | if (c == 'v') { |
3508 | printf(QEMU_IMG_VERSION); | |
3509 | return 0; | |
3510 | } | |
7db1689c | 3511 | |
153859be | 3512 | /* not found */ |
ac1307ab | 3513 | error_exit("Command not found: %s", cmdname); |
ea2384d3 | 3514 | } |