]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * QEMU disk image utility | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
5 | * | |
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 | */ | |
24 | #include "qemu/osdep.h" | |
25 | #include "qemu-version.h" | |
26 | #include "qapi/error.h" | |
27 | #include "qapi-visit.h" | |
28 | #include "qapi/qobject-output-visitor.h" | |
29 | #include "qapi/qmp/qerror.h" | |
30 | #include "qapi/qmp/qjson.h" | |
31 | #include "qemu/cutils.h" | |
32 | #include "qemu/config-file.h" | |
33 | #include "qemu/option.h" | |
34 | #include "qemu/error-report.h" | |
35 | #include "qemu/log.h" | |
36 | #include "qom/object_interfaces.h" | |
37 | #include "sysemu/sysemu.h" | |
38 | #include "sysemu/block-backend.h" | |
39 | #include "block/block_int.h" | |
40 | #include "block/blockjob.h" | |
41 | #include "block/qapi.h" | |
42 | #include "crypto/init.h" | |
43 | #include "trace/control.h" | |
44 | #include <getopt.h> | |
45 | ||
46 | #define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \ | |
47 | "\n" QEMU_COPYRIGHT "\n" | |
48 | ||
49 | typedef struct img_cmd_t { | |
50 | const char *name; | |
51 | int (*handler)(int argc, char **argv); | |
52 | } img_cmd_t; | |
53 | ||
54 | enum { | |
55 | OPTION_OUTPUT = 256, | |
56 | OPTION_BACKING_CHAIN = 257, | |
57 | OPTION_OBJECT = 258, | |
58 | OPTION_IMAGE_OPTS = 259, | |
59 | OPTION_PATTERN = 260, | |
60 | OPTION_FLUSH_INTERVAL = 261, | |
61 | OPTION_NO_DRAIN = 262, | |
62 | }; | |
63 | ||
64 | typedef enum OutputFormat { | |
65 | OFORMAT_JSON, | |
66 | OFORMAT_HUMAN, | |
67 | } OutputFormat; | |
68 | ||
69 | /* Default to cache=writeback as data integrity is not important for qemu-img */ | |
70 | #define BDRV_DEFAULT_CACHE "writeback" | |
71 | ||
72 | static void format_print(void *opaque, const char *name) | |
73 | { | |
74 | printf(" %s", name); | |
75 | } | |
76 | ||
77 | static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...) | |
78 | { | |
79 | va_list ap; | |
80 | ||
81 | error_printf("qemu-img: "); | |
82 | ||
83 | va_start(ap, fmt); | |
84 | error_vprintf(fmt, ap); | |
85 | va_end(ap); | |
86 | ||
87 | error_printf("\nTry 'qemu-img --help' for more information\n"); | |
88 | exit(EXIT_FAILURE); | |
89 | } | |
90 | ||
91 | static void QEMU_NORETURN missing_argument(const char *option) | |
92 | { | |
93 | error_exit("missing argument for option '%s'", option); | |
94 | } | |
95 | ||
96 | static void QEMU_NORETURN unrecognized_option(const char *option) | |
97 | { | |
98 | error_exit("unrecognized option '%s'", option); | |
99 | } | |
100 | ||
101 | /* Please keep in synch with qemu-img.texi */ | |
102 | static void QEMU_NORETURN help(void) | |
103 | { | |
104 | const char *help_msg = | |
105 | QEMU_IMG_VERSION | |
106 | "usage: qemu-img [standard options] command [command options]\n" | |
107 | "QEMU disk image utility\n" | |
108 | "\n" | |
109 | " '-h', '--help' display this help and exit\n" | |
110 | " '-V', '--version' output version information and exit\n" | |
111 | " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n" | |
112 | " specify tracing options\n" | |
113 | "\n" | |
114 | "Command syntax:\n" | |
115 | #define DEF(option, callback, arg_string) \ | |
116 | " " arg_string "\n" | |
117 | #include "qemu-img-cmds.h" | |
118 | #undef DEF | |
119 | #undef GEN_DOCS | |
120 | "\n" | |
121 | "Command parameters:\n" | |
122 | " 'filename' is a disk image filename\n" | |
123 | " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n" | |
124 | " manual page for a description of the object properties. The most common\n" | |
125 | " object type is a 'secret', which is used to supply passwords and/or\n" | |
126 | " encryption keys.\n" | |
127 | " 'fmt' is the disk image format. It is guessed automatically in most cases\n" | |
128 | " 'cache' is the cache mode used to write the output disk image, the valid\n" | |
129 | " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n" | |
130 | " 'directsync' and 'unsafe' (default for convert)\n" | |
131 | " 'src_cache' is the cache mode used to read input disk images, the valid\n" | |
132 | " options are the same as for the 'cache' option\n" | |
133 | " 'size' is the disk image size in bytes. Optional suffixes\n" | |
134 | " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n" | |
135 | " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n" | |
136 | " supported. 'b' is ignored.\n" | |
137 | " 'output_filename' is the destination disk image filename\n" | |
138 | " 'output_fmt' is the destination format\n" | |
139 | " 'options' is a comma separated list of format specific options in a\n" | |
140 | " name=value format. Use -o ? for an overview of the options supported by the\n" | |
141 | " used format\n" | |
142 | " 'snapshot_param' is param used for internal snapshot, format\n" | |
143 | " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n" | |
144 | " '[ID_OR_NAME]'\n" | |
145 | " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n" | |
146 | " instead\n" | |
147 | " '-c' indicates that target image must be compressed (qcow format only)\n" | |
148 | " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n" | |
149 | " match exactly. The image doesn't need a working backing file before\n" | |
150 | " rebasing in this case (useful for renaming the backing file)\n" | |
151 | " '-h' with or without a command shows this help and lists the supported formats\n" | |
152 | " '-p' show progress of command (only certain commands)\n" | |
153 | " '-q' use Quiet mode - do not print any output (except errors)\n" | |
154 | " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n" | |
155 | " contain only zeros for qemu-img to create a sparse image during\n" | |
156 | " conversion. If the number of bytes is 0, the source will not be scanned for\n" | |
157 | " unallocated or zero sectors, and the destination image will always be\n" | |
158 | " fully allocated\n" | |
159 | " '--output' takes the format in which the output must be done (human or json)\n" | |
160 | " '-n' skips the target volume creation (useful if the volume is created\n" | |
161 | " prior to running qemu-img)\n" | |
162 | "\n" | |
163 | "Parameters to check subcommand:\n" | |
164 | " '-r' tries to repair any inconsistencies that are found during the check.\n" | |
165 | " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n" | |
166 | " kinds of errors, with a higher risk of choosing the wrong fix or\n" | |
167 | " hiding corruption that has already occurred.\n" | |
168 | "\n" | |
169 | "Parameters to convert subcommand:\n" | |
170 | " '-m' specifies how many coroutines work in parallel during the convert\n" | |
171 | " process (defaults to 8)\n" | |
172 | " '-W' allow to write to the target out of order rather than sequential\n" | |
173 | "\n" | |
174 | "Parameters to snapshot subcommand:\n" | |
175 | " 'snapshot' is the name of the snapshot to create, apply or delete\n" | |
176 | " '-a' applies a snapshot (revert disk to saved state)\n" | |
177 | " '-c' creates a snapshot\n" | |
178 | " '-d' deletes a snapshot\n" | |
179 | " '-l' lists all snapshots in the given image\n" | |
180 | "\n" | |
181 | "Parameters to compare subcommand:\n" | |
182 | " '-f' first image format\n" | |
183 | " '-F' second image format\n" | |
184 | " '-s' run in Strict mode - fail on different image size or sector allocation\n" | |
185 | "\n" | |
186 | "Parameters to dd subcommand:\n" | |
187 | " 'bs=BYTES' read and write up to BYTES bytes at a time " | |
188 | "(default: 512)\n" | |
189 | " 'count=N' copy only N input blocks\n" | |
190 | " 'if=FILE' read from FILE\n" | |
191 | " 'of=FILE' write to FILE\n" | |
192 | " 'skip=N' skip N bs-sized blocks at the start of input\n"; | |
193 | ||
194 | printf("%s\nSupported formats:", help_msg); | |
195 | bdrv_iterate_format(format_print, NULL); | |
196 | printf("\n"); | |
197 | exit(EXIT_SUCCESS); | |
198 | } | |
199 | ||
200 | static QemuOptsList qemu_object_opts = { | |
201 | .name = "object", | |
202 | .implied_opt_name = "qom-type", | |
203 | .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head), | |
204 | .desc = { | |
205 | { } | |
206 | }, | |
207 | }; | |
208 | ||
209 | static QemuOptsList qemu_source_opts = { | |
210 | .name = "source", | |
211 | .implied_opt_name = "file", | |
212 | .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head), | |
213 | .desc = { | |
214 | { } | |
215 | }, | |
216 | }; | |
217 | ||
218 | static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...) | |
219 | { | |
220 | int ret = 0; | |
221 | if (!quiet) { | |
222 | va_list args; | |
223 | va_start(args, fmt); | |
224 | ret = vprintf(fmt, args); | |
225 | va_end(args); | |
226 | } | |
227 | return ret; | |
228 | } | |
229 | ||
230 | ||
231 | static int print_block_option_help(const char *filename, const char *fmt) | |
232 | { | |
233 | BlockDriver *drv, *proto_drv; | |
234 | QemuOptsList *create_opts = NULL; | |
235 | Error *local_err = NULL; | |
236 | ||
237 | /* Find driver and parse its options */ | |
238 | drv = bdrv_find_format(fmt); | |
239 | if (!drv) { | |
240 | error_report("Unknown file format '%s'", fmt); | |
241 | return 1; | |
242 | } | |
243 | ||
244 | create_opts = qemu_opts_append(create_opts, drv->create_opts); | |
245 | if (filename) { | |
246 | proto_drv = bdrv_find_protocol(filename, true, &local_err); | |
247 | if (!proto_drv) { | |
248 | error_report_err(local_err); | |
249 | qemu_opts_free(create_opts); | |
250 | return 1; | |
251 | } | |
252 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); | |
253 | } | |
254 | ||
255 | qemu_opts_print_help(create_opts); | |
256 | qemu_opts_free(create_opts); | |
257 | return 0; | |
258 | } | |
259 | ||
260 | ||
261 | static int img_open_password(BlockBackend *blk, const char *filename, | |
262 | int flags, bool quiet) | |
263 | { | |
264 | BlockDriverState *bs; | |
265 | char password[256]; | |
266 | ||
267 | bs = blk_bs(blk); | |
268 | if (bdrv_is_encrypted(bs) && bdrv_key_required(bs) && | |
269 | !(flags & BDRV_O_NO_IO)) { | |
270 | qprintf(quiet, "Disk image '%s' is encrypted.\n", filename); | |
271 | if (qemu_read_password(password, sizeof(password)) < 0) { | |
272 | error_report("No password given"); | |
273 | return -1; | |
274 | } | |
275 | if (bdrv_set_key(bs, password) < 0) { | |
276 | error_report("invalid password"); | |
277 | return -1; | |
278 | } | |
279 | } | |
280 | return 0; | |
281 | } | |
282 | ||
283 | ||
284 | static BlockBackend *img_open_opts(const char *optstr, | |
285 | QemuOpts *opts, int flags, bool writethrough, | |
286 | bool quiet) | |
287 | { | |
288 | QDict *options; | |
289 | Error *local_err = NULL; | |
290 | BlockBackend *blk; | |
291 | options = qemu_opts_to_qdict(opts, NULL); | |
292 | blk = blk_new_open(NULL, NULL, options, flags, &local_err); | |
293 | if (!blk) { | |
294 | error_reportf_err(local_err, "Could not open '%s': ", optstr); | |
295 | return NULL; | |
296 | } | |
297 | blk_set_enable_write_cache(blk, !writethrough); | |
298 | ||
299 | if (img_open_password(blk, optstr, flags, quiet) < 0) { | |
300 | blk_unref(blk); | |
301 | return NULL; | |
302 | } | |
303 | return blk; | |
304 | } | |
305 | ||
306 | static BlockBackend *img_open_file(const char *filename, | |
307 | const char *fmt, int flags, | |
308 | bool writethrough, bool quiet) | |
309 | { | |
310 | BlockBackend *blk; | |
311 | Error *local_err = NULL; | |
312 | QDict *options = NULL; | |
313 | ||
314 | if (fmt) { | |
315 | options = qdict_new(); | |
316 | qdict_put_str(options, "driver", fmt); | |
317 | } | |
318 | ||
319 | blk = blk_new_open(filename, NULL, options, flags, &local_err); | |
320 | if (!blk) { | |
321 | error_reportf_err(local_err, "Could not open '%s': ", filename); | |
322 | return NULL; | |
323 | } | |
324 | blk_set_enable_write_cache(blk, !writethrough); | |
325 | ||
326 | if (img_open_password(blk, filename, flags, quiet) < 0) { | |
327 | blk_unref(blk); | |
328 | return NULL; | |
329 | } | |
330 | return blk; | |
331 | } | |
332 | ||
333 | ||
334 | static BlockBackend *img_open(bool image_opts, | |
335 | const char *filename, | |
336 | const char *fmt, int flags, bool writethrough, | |
337 | bool quiet) | |
338 | { | |
339 | BlockBackend *blk; | |
340 | if (image_opts) { | |
341 | QemuOpts *opts; | |
342 | if (fmt) { | |
343 | error_report("--image-opts and --format are mutually exclusive"); | |
344 | return NULL; | |
345 | } | |
346 | opts = qemu_opts_parse_noisily(qemu_find_opts("source"), | |
347 | filename, true); | |
348 | if (!opts) { | |
349 | return NULL; | |
350 | } | |
351 | blk = img_open_opts(filename, opts, flags, writethrough, quiet); | |
352 | } else { | |
353 | blk = img_open_file(filename, fmt, flags, writethrough, quiet); | |
354 | } | |
355 | return blk; | |
356 | } | |
357 | ||
358 | ||
359 | static int add_old_style_options(const char *fmt, QemuOpts *opts, | |
360 | const char *base_filename, | |
361 | const char *base_fmt) | |
362 | { | |
363 | Error *err = NULL; | |
364 | ||
365 | if (base_filename) { | |
366 | qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err); | |
367 | if (err) { | |
368 | error_report("Backing file not supported for file format '%s'", | |
369 | fmt); | |
370 | error_free(err); | |
371 | return -1; | |
372 | } | |
373 | } | |
374 | if (base_fmt) { | |
375 | qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err); | |
376 | if (err) { | |
377 | error_report("Backing file format not supported for file " | |
378 | "format '%s'", fmt); | |
379 | error_free(err); | |
380 | return -1; | |
381 | } | |
382 | } | |
383 | return 0; | |
384 | } | |
385 | ||
386 | static int64_t cvtnum(const char *s) | |
387 | { | |
388 | int err; | |
389 | uint64_t value; | |
390 | ||
391 | err = qemu_strtosz(s, NULL, &value); | |
392 | if (err < 0) { | |
393 | return err; | |
394 | } | |
395 | if (value > INT64_MAX) { | |
396 | return -ERANGE; | |
397 | } | |
398 | return value; | |
399 | } | |
400 | ||
401 | static int img_create(int argc, char **argv) | |
402 | { | |
403 | int c; | |
404 | uint64_t img_size = -1; | |
405 | const char *fmt = "raw"; | |
406 | const char *base_fmt = NULL; | |
407 | const char *filename; | |
408 | const char *base_filename = NULL; | |
409 | char *options = NULL; | |
410 | Error *local_err = NULL; | |
411 | bool quiet = false; | |
412 | ||
413 | for(;;) { | |
414 | static const struct option long_options[] = { | |
415 | {"help", no_argument, 0, 'h'}, | |
416 | {"object", required_argument, 0, OPTION_OBJECT}, | |
417 | {0, 0, 0, 0} | |
418 | }; | |
419 | c = getopt_long(argc, argv, ":F:b:f:he6o:q", | |
420 | long_options, NULL); | |
421 | if (c == -1) { | |
422 | break; | |
423 | } | |
424 | switch(c) { | |
425 | case ':': | |
426 | missing_argument(argv[optind - 1]); | |
427 | break; | |
428 | case '?': | |
429 | unrecognized_option(argv[optind - 1]); | |
430 | break; | |
431 | case 'h': | |
432 | help(); | |
433 | break; | |
434 | case 'F': | |
435 | base_fmt = optarg; | |
436 | break; | |
437 | case 'b': | |
438 | base_filename = optarg; | |
439 | break; | |
440 | case 'f': | |
441 | fmt = optarg; | |
442 | break; | |
443 | case 'e': | |
444 | error_report("option -e is deprecated, please use \'-o " | |
445 | "encryption\' instead!"); | |
446 | goto fail; | |
447 | case '6': | |
448 | error_report("option -6 is deprecated, please use \'-o " | |
449 | "compat6\' instead!"); | |
450 | goto fail; | |
451 | case 'o': | |
452 | if (!is_valid_option_list(optarg)) { | |
453 | error_report("Invalid option list: %s", optarg); | |
454 | goto fail; | |
455 | } | |
456 | if (!options) { | |
457 | options = g_strdup(optarg); | |
458 | } else { | |
459 | char *old_options = options; | |
460 | options = g_strdup_printf("%s,%s", options, optarg); | |
461 | g_free(old_options); | |
462 | } | |
463 | break; | |
464 | case 'q': | |
465 | quiet = true; | |
466 | break; | |
467 | case OPTION_OBJECT: { | |
468 | QemuOpts *opts; | |
469 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
470 | optarg, true); | |
471 | if (!opts) { | |
472 | goto fail; | |
473 | } | |
474 | } break; | |
475 | } | |
476 | } | |
477 | ||
478 | /* Get the filename */ | |
479 | filename = (optind < argc) ? argv[optind] : NULL; | |
480 | if (options && has_help_option(options)) { | |
481 | g_free(options); | |
482 | return print_block_option_help(filename, fmt); | |
483 | } | |
484 | ||
485 | if (optind >= argc) { | |
486 | error_exit("Expecting image file name"); | |
487 | } | |
488 | optind++; | |
489 | ||
490 | if (qemu_opts_foreach(&qemu_object_opts, | |
491 | user_creatable_add_opts_foreach, | |
492 | NULL, NULL)) { | |
493 | goto fail; | |
494 | } | |
495 | ||
496 | /* Get image size, if specified */ | |
497 | if (optind < argc) { | |
498 | int64_t sval; | |
499 | ||
500 | sval = cvtnum(argv[optind++]); | |
501 | if (sval < 0) { | |
502 | if (sval == -ERANGE) { | |
503 | error_report("Image size must be less than 8 EiB!"); | |
504 | } else { | |
505 | error_report("Invalid image size specified! You may use k, M, " | |
506 | "G, T, P or E suffixes for "); | |
507 | error_report("kilobytes, megabytes, gigabytes, terabytes, " | |
508 | "petabytes and exabytes."); | |
509 | } | |
510 | goto fail; | |
511 | } | |
512 | img_size = (uint64_t)sval; | |
513 | } | |
514 | if (optind != argc) { | |
515 | error_exit("Unexpected argument: %s", argv[optind]); | |
516 | } | |
517 | ||
518 | bdrv_img_create(filename, fmt, base_filename, base_fmt, | |
519 | options, img_size, 0, &local_err, quiet); | |
520 | if (local_err) { | |
521 | error_reportf_err(local_err, "%s: ", filename); | |
522 | goto fail; | |
523 | } | |
524 | ||
525 | g_free(options); | |
526 | return 0; | |
527 | ||
528 | fail: | |
529 | g_free(options); | |
530 | return 1; | |
531 | } | |
532 | ||
533 | static void dump_json_image_check(ImageCheck *check, bool quiet) | |
534 | { | |
535 | QString *str; | |
536 | QObject *obj; | |
537 | Visitor *v = qobject_output_visitor_new(&obj); | |
538 | ||
539 | visit_type_ImageCheck(v, NULL, &check, &error_abort); | |
540 | visit_complete(v, &obj); | |
541 | str = qobject_to_json_pretty(obj); | |
542 | assert(str != NULL); | |
543 | qprintf(quiet, "%s\n", qstring_get_str(str)); | |
544 | qobject_decref(obj); | |
545 | visit_free(v); | |
546 | QDECREF(str); | |
547 | } | |
548 | ||
549 | static void dump_human_image_check(ImageCheck *check, bool quiet) | |
550 | { | |
551 | if (!(check->corruptions || check->leaks || check->check_errors)) { | |
552 | qprintf(quiet, "No errors were found on the image.\n"); | |
553 | } else { | |
554 | if (check->corruptions) { | |
555 | qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n" | |
556 | "Data may be corrupted, or further writes to the image " | |
557 | "may corrupt it.\n", | |
558 | check->corruptions); | |
559 | } | |
560 | ||
561 | if (check->leaks) { | |
562 | qprintf(quiet, | |
563 | "\n%" PRId64 " leaked clusters were found on the image.\n" | |
564 | "This means waste of disk space, but no harm to data.\n", | |
565 | check->leaks); | |
566 | } | |
567 | ||
568 | if (check->check_errors) { | |
569 | qprintf(quiet, | |
570 | "\n%" PRId64 | |
571 | " internal errors have occurred during the check.\n", | |
572 | check->check_errors); | |
573 | } | |
574 | } | |
575 | ||
576 | if (check->total_clusters != 0 && check->allocated_clusters != 0) { | |
577 | qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, " | |
578 | "%0.2f%% fragmented, %0.2f%% compressed clusters\n", | |
579 | check->allocated_clusters, check->total_clusters, | |
580 | check->allocated_clusters * 100.0 / check->total_clusters, | |
581 | check->fragmented_clusters * 100.0 / check->allocated_clusters, | |
582 | check->compressed_clusters * 100.0 / | |
583 | check->allocated_clusters); | |
584 | } | |
585 | ||
586 | if (check->image_end_offset) { | |
587 | qprintf(quiet, | |
588 | "Image end offset: %" PRId64 "\n", check->image_end_offset); | |
589 | } | |
590 | } | |
591 | ||
592 | static int collect_image_check(BlockDriverState *bs, | |
593 | ImageCheck *check, | |
594 | const char *filename, | |
595 | const char *fmt, | |
596 | int fix) | |
597 | { | |
598 | int ret; | |
599 | BdrvCheckResult result; | |
600 | ||
601 | ret = bdrv_check(bs, &result, fix); | |
602 | if (ret < 0) { | |
603 | return ret; | |
604 | } | |
605 | ||
606 | check->filename = g_strdup(filename); | |
607 | check->format = g_strdup(bdrv_get_format_name(bs)); | |
608 | check->check_errors = result.check_errors; | |
609 | check->corruptions = result.corruptions; | |
610 | check->has_corruptions = result.corruptions != 0; | |
611 | check->leaks = result.leaks; | |
612 | check->has_leaks = result.leaks != 0; | |
613 | check->corruptions_fixed = result.corruptions_fixed; | |
614 | check->has_corruptions_fixed = result.corruptions != 0; | |
615 | check->leaks_fixed = result.leaks_fixed; | |
616 | check->has_leaks_fixed = result.leaks != 0; | |
617 | check->image_end_offset = result.image_end_offset; | |
618 | check->has_image_end_offset = result.image_end_offset != 0; | |
619 | check->total_clusters = result.bfi.total_clusters; | |
620 | check->has_total_clusters = result.bfi.total_clusters != 0; | |
621 | check->allocated_clusters = result.bfi.allocated_clusters; | |
622 | check->has_allocated_clusters = result.bfi.allocated_clusters != 0; | |
623 | check->fragmented_clusters = result.bfi.fragmented_clusters; | |
624 | check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0; | |
625 | check->compressed_clusters = result.bfi.compressed_clusters; | |
626 | check->has_compressed_clusters = result.bfi.compressed_clusters != 0; | |
627 | ||
628 | return 0; | |
629 | } | |
630 | ||
631 | /* | |
632 | * Checks an image for consistency. Exit codes: | |
633 | * | |
634 | * 0 - Check completed, image is good | |
635 | * 1 - Check not completed because of internal errors | |
636 | * 2 - Check completed, image is corrupted | |
637 | * 3 - Check completed, image has leaked clusters, but is good otherwise | |
638 | * 63 - Checks are not supported by the image format | |
639 | */ | |
640 | static int img_check(int argc, char **argv) | |
641 | { | |
642 | int c, ret; | |
643 | OutputFormat output_format = OFORMAT_HUMAN; | |
644 | const char *filename, *fmt, *output, *cache; | |
645 | BlockBackend *blk; | |
646 | BlockDriverState *bs; | |
647 | int fix = 0; | |
648 | int flags = BDRV_O_CHECK; | |
649 | bool writethrough; | |
650 | ImageCheck *check; | |
651 | bool quiet = false; | |
652 | bool image_opts = false; | |
653 | ||
654 | fmt = NULL; | |
655 | output = NULL; | |
656 | cache = BDRV_DEFAULT_CACHE; | |
657 | ||
658 | for(;;) { | |
659 | int option_index = 0; | |
660 | static const struct option long_options[] = { | |
661 | {"help", no_argument, 0, 'h'}, | |
662 | {"format", required_argument, 0, 'f'}, | |
663 | {"repair", required_argument, 0, 'r'}, | |
664 | {"output", required_argument, 0, OPTION_OUTPUT}, | |
665 | {"object", required_argument, 0, OPTION_OBJECT}, | |
666 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, | |
667 | {0, 0, 0, 0} | |
668 | }; | |
669 | c = getopt_long(argc, argv, ":hf:r:T:q", | |
670 | long_options, &option_index); | |
671 | if (c == -1) { | |
672 | break; | |
673 | } | |
674 | switch(c) { | |
675 | case ':': | |
676 | missing_argument(argv[optind - 1]); | |
677 | break; | |
678 | case '?': | |
679 | unrecognized_option(argv[optind - 1]); | |
680 | break; | |
681 | case 'h': | |
682 | help(); | |
683 | break; | |
684 | case 'f': | |
685 | fmt = optarg; | |
686 | break; | |
687 | case 'r': | |
688 | flags |= BDRV_O_RDWR; | |
689 | ||
690 | if (!strcmp(optarg, "leaks")) { | |
691 | fix = BDRV_FIX_LEAKS; | |
692 | } else if (!strcmp(optarg, "all")) { | |
693 | fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS; | |
694 | } else { | |
695 | error_exit("Unknown option value for -r " | |
696 | "(expecting 'leaks' or 'all'): %s", optarg); | |
697 | } | |
698 | break; | |
699 | case OPTION_OUTPUT: | |
700 | output = optarg; | |
701 | break; | |
702 | case 'T': | |
703 | cache = optarg; | |
704 | break; | |
705 | case 'q': | |
706 | quiet = true; | |
707 | break; | |
708 | case OPTION_OBJECT: { | |
709 | QemuOpts *opts; | |
710 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
711 | optarg, true); | |
712 | if (!opts) { | |
713 | return 1; | |
714 | } | |
715 | } break; | |
716 | case OPTION_IMAGE_OPTS: | |
717 | image_opts = true; | |
718 | break; | |
719 | } | |
720 | } | |
721 | if (optind != argc - 1) { | |
722 | error_exit("Expecting one image file name"); | |
723 | } | |
724 | filename = argv[optind++]; | |
725 | ||
726 | if (output && !strcmp(output, "json")) { | |
727 | output_format = OFORMAT_JSON; | |
728 | } else if (output && !strcmp(output, "human")) { | |
729 | output_format = OFORMAT_HUMAN; | |
730 | } else if (output) { | |
731 | error_report("--output must be used with human or json as argument."); | |
732 | return 1; | |
733 | } | |
734 | ||
735 | if (qemu_opts_foreach(&qemu_object_opts, | |
736 | user_creatable_add_opts_foreach, | |
737 | NULL, NULL)) { | |
738 | return 1; | |
739 | } | |
740 | ||
741 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); | |
742 | if (ret < 0) { | |
743 | error_report("Invalid source cache option: %s", cache); | |
744 | return 1; | |
745 | } | |
746 | ||
747 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet); | |
748 | if (!blk) { | |
749 | return 1; | |
750 | } | |
751 | bs = blk_bs(blk); | |
752 | ||
753 | check = g_new0(ImageCheck, 1); | |
754 | ret = collect_image_check(bs, check, filename, fmt, fix); | |
755 | ||
756 | if (ret == -ENOTSUP) { | |
757 | error_report("This image format does not support checks"); | |
758 | ret = 63; | |
759 | goto fail; | |
760 | } | |
761 | ||
762 | if (check->corruptions_fixed || check->leaks_fixed) { | |
763 | int corruptions_fixed, leaks_fixed; | |
764 | ||
765 | leaks_fixed = check->leaks_fixed; | |
766 | corruptions_fixed = check->corruptions_fixed; | |
767 | ||
768 | if (output_format == OFORMAT_HUMAN) { | |
769 | qprintf(quiet, | |
770 | "The following inconsistencies were found and repaired:\n\n" | |
771 | " %" PRId64 " leaked clusters\n" | |
772 | " %" PRId64 " corruptions\n\n" | |
773 | "Double checking the fixed image now...\n", | |
774 | check->leaks_fixed, | |
775 | check->corruptions_fixed); | |
776 | } | |
777 | ||
778 | ret = collect_image_check(bs, check, filename, fmt, 0); | |
779 | ||
780 | check->leaks_fixed = leaks_fixed; | |
781 | check->corruptions_fixed = corruptions_fixed; | |
782 | } | |
783 | ||
784 | if (!ret) { | |
785 | switch (output_format) { | |
786 | case OFORMAT_HUMAN: | |
787 | dump_human_image_check(check, quiet); | |
788 | break; | |
789 | case OFORMAT_JSON: | |
790 | dump_json_image_check(check, quiet); | |
791 | break; | |
792 | } | |
793 | } | |
794 | ||
795 | if (ret || check->check_errors) { | |
796 | if (ret) { | |
797 | error_report("Check failed: %s", strerror(-ret)); | |
798 | } else { | |
799 | error_report("Check failed"); | |
800 | } | |
801 | ret = 1; | |
802 | goto fail; | |
803 | } | |
804 | ||
805 | if (check->corruptions) { | |
806 | ret = 2; | |
807 | } else if (check->leaks) { | |
808 | ret = 3; | |
809 | } else { | |
810 | ret = 0; | |
811 | } | |
812 | ||
813 | fail: | |
814 | qapi_free_ImageCheck(check); | |
815 | blk_unref(blk); | |
816 | return ret; | |
817 | } | |
818 | ||
819 | typedef struct CommonBlockJobCBInfo { | |
820 | BlockDriverState *bs; | |
821 | Error **errp; | |
822 | } CommonBlockJobCBInfo; | |
823 | ||
824 | static void common_block_job_cb(void *opaque, int ret) | |
825 | { | |
826 | CommonBlockJobCBInfo *cbi = opaque; | |
827 | ||
828 | if (ret < 0) { | |
829 | error_setg_errno(cbi->errp, -ret, "Block job failed"); | |
830 | } | |
831 | } | |
832 | ||
833 | static void run_block_job(BlockJob *job, Error **errp) | |
834 | { | |
835 | AioContext *aio_context = blk_get_aio_context(job->blk); | |
836 | ||
837 | /* FIXME In error cases, the job simply goes away and we access a dangling | |
838 | * pointer below. */ | |
839 | aio_context_acquire(aio_context); | |
840 | do { | |
841 | aio_poll(aio_context, true); | |
842 | qemu_progress_print(job->len ? | |
843 | ((float)job->offset / job->len * 100.f) : 0.0f, 0); | |
844 | } while (!job->ready); | |
845 | ||
846 | block_job_complete_sync(job, errp); | |
847 | aio_context_release(aio_context); | |
848 | ||
849 | /* A block job may finish instantaneously without publishing any progress, | |
850 | * so just signal completion here */ | |
851 | qemu_progress_print(100.f, 0); | |
852 | } | |
853 | ||
854 | static int img_commit(int argc, char **argv) | |
855 | { | |
856 | int c, ret, flags; | |
857 | const char *filename, *fmt, *cache, *base; | |
858 | BlockBackend *blk; | |
859 | BlockDriverState *bs, *base_bs; | |
860 | BlockJob *job; | |
861 | bool progress = false, quiet = false, drop = false; | |
862 | bool writethrough; | |
863 | Error *local_err = NULL; | |
864 | CommonBlockJobCBInfo cbi; | |
865 | bool image_opts = false; | |
866 | AioContext *aio_context; | |
867 | ||
868 | fmt = NULL; | |
869 | cache = BDRV_DEFAULT_CACHE; | |
870 | base = NULL; | |
871 | for(;;) { | |
872 | static const struct option long_options[] = { | |
873 | {"help", no_argument, 0, 'h'}, | |
874 | {"object", required_argument, 0, OPTION_OBJECT}, | |
875 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, | |
876 | {0, 0, 0, 0} | |
877 | }; | |
878 | c = getopt_long(argc, argv, ":f:ht:b:dpq", | |
879 | long_options, NULL); | |
880 | if (c == -1) { | |
881 | break; | |
882 | } | |
883 | switch(c) { | |
884 | case ':': | |
885 | missing_argument(argv[optind - 1]); | |
886 | break; | |
887 | case '?': | |
888 | unrecognized_option(argv[optind - 1]); | |
889 | break; | |
890 | case 'h': | |
891 | help(); | |
892 | break; | |
893 | case 'f': | |
894 | fmt = optarg; | |
895 | break; | |
896 | case 't': | |
897 | cache = optarg; | |
898 | break; | |
899 | case 'b': | |
900 | base = optarg; | |
901 | /* -b implies -d */ | |
902 | drop = true; | |
903 | break; | |
904 | case 'd': | |
905 | drop = true; | |
906 | break; | |
907 | case 'p': | |
908 | progress = true; | |
909 | break; | |
910 | case 'q': | |
911 | quiet = true; | |
912 | break; | |
913 | case OPTION_OBJECT: { | |
914 | QemuOpts *opts; | |
915 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
916 | optarg, true); | |
917 | if (!opts) { | |
918 | return 1; | |
919 | } | |
920 | } break; | |
921 | case OPTION_IMAGE_OPTS: | |
922 | image_opts = true; | |
923 | break; | |
924 | } | |
925 | } | |
926 | ||
927 | /* Progress is not shown in Quiet mode */ | |
928 | if (quiet) { | |
929 | progress = false; | |
930 | } | |
931 | ||
932 | if (optind != argc - 1) { | |
933 | error_exit("Expecting one image file name"); | |
934 | } | |
935 | filename = argv[optind++]; | |
936 | ||
937 | if (qemu_opts_foreach(&qemu_object_opts, | |
938 | user_creatable_add_opts_foreach, | |
939 | NULL, NULL)) { | |
940 | return 1; | |
941 | } | |
942 | ||
943 | flags = BDRV_O_RDWR | BDRV_O_UNMAP; | |
944 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); | |
945 | if (ret < 0) { | |
946 | error_report("Invalid cache option: %s", cache); | |
947 | return 1; | |
948 | } | |
949 | ||
950 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet); | |
951 | if (!blk) { | |
952 | return 1; | |
953 | } | |
954 | bs = blk_bs(blk); | |
955 | ||
956 | qemu_progress_init(progress, 1.f); | |
957 | qemu_progress_print(0.f, 100); | |
958 | ||
959 | if (base) { | |
960 | base_bs = bdrv_find_backing_image(bs, base); | |
961 | if (!base_bs) { | |
962 | error_setg(&local_err, | |
963 | "Did not find '%s' in the backing chain of '%s'", | |
964 | base, filename); | |
965 | goto done; | |
966 | } | |
967 | } else { | |
968 | /* This is different from QMP, which by default uses the deepest file in | |
969 | * the backing chain (i.e., the very base); however, the traditional | |
970 | * behavior of qemu-img commit is using the immediate backing file. */ | |
971 | base_bs = backing_bs(bs); | |
972 | if (!base_bs) { | |
973 | error_setg(&local_err, "Image does not have a backing file"); | |
974 | goto done; | |
975 | } | |
976 | } | |
977 | ||
978 | cbi = (CommonBlockJobCBInfo){ | |
979 | .errp = &local_err, | |
980 | .bs = bs, | |
981 | }; | |
982 | ||
983 | aio_context = bdrv_get_aio_context(bs); | |
984 | aio_context_acquire(aio_context); | |
985 | commit_active_start("commit", bs, base_bs, BLOCK_JOB_DEFAULT, 0, | |
986 | BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb, | |
987 | &cbi, &local_err, false); | |
988 | aio_context_release(aio_context); | |
989 | if (local_err) { | |
990 | goto done; | |
991 | } | |
992 | ||
993 | /* When the block job completes, the BlockBackend reference will point to | |
994 | * the old backing file. In order to avoid that the top image is already | |
995 | * deleted, so we can still empty it afterwards, increment the reference | |
996 | * counter here preemptively. */ | |
997 | if (!drop) { | |
998 | bdrv_ref(bs); | |
999 | } | |
1000 | ||
1001 | job = block_job_get("commit"); | |
1002 | run_block_job(job, &local_err); | |
1003 | if (local_err) { | |
1004 | goto unref_backing; | |
1005 | } | |
1006 | ||
1007 | if (!drop && bs->drv->bdrv_make_empty) { | |
1008 | ret = bs->drv->bdrv_make_empty(bs); | |
1009 | if (ret) { | |
1010 | error_setg_errno(&local_err, -ret, "Could not empty %s", | |
1011 | filename); | |
1012 | goto unref_backing; | |
1013 | } | |
1014 | } | |
1015 | ||
1016 | unref_backing: | |
1017 | if (!drop) { | |
1018 | bdrv_unref(bs); | |
1019 | } | |
1020 | ||
1021 | done: | |
1022 | qemu_progress_end(); | |
1023 | ||
1024 | blk_unref(blk); | |
1025 | ||
1026 | if (local_err) { | |
1027 | error_report_err(local_err); | |
1028 | return 1; | |
1029 | } | |
1030 | ||
1031 | qprintf(quiet, "Image committed.\n"); | |
1032 | return 0; | |
1033 | } | |
1034 | ||
1035 | /* | |
1036 | * Returns true iff the first sector pointed to by 'buf' contains at least | |
1037 | * a non-NUL byte. | |
1038 | * | |
1039 | * 'pnum' is set to the number of sectors (including and immediately following | |
1040 | * the first one) that are known to be in the same allocated/unallocated state. | |
1041 | */ | |
1042 | static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum) | |
1043 | { | |
1044 | bool is_zero; | |
1045 | int i; | |
1046 | ||
1047 | if (n <= 0) { | |
1048 | *pnum = 0; | |
1049 | return 0; | |
1050 | } | |
1051 | is_zero = buffer_is_zero(buf, 512); | |
1052 | for(i = 1; i < n; i++) { | |
1053 | buf += 512; | |
1054 | if (is_zero != buffer_is_zero(buf, 512)) { | |
1055 | break; | |
1056 | } | |
1057 | } | |
1058 | *pnum = i; | |
1059 | return !is_zero; | |
1060 | } | |
1061 | ||
1062 | /* | |
1063 | * Like is_allocated_sectors, but if the buffer starts with a used sector, | |
1064 | * up to 'min' consecutive sectors containing zeros are ignored. This avoids | |
1065 | * breaking up write requests for only small sparse areas. | |
1066 | */ | |
1067 | static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum, | |
1068 | int min) | |
1069 | { | |
1070 | int ret; | |
1071 | int num_checked, num_used; | |
1072 | ||
1073 | if (n < min) { | |
1074 | min = n; | |
1075 | } | |
1076 | ||
1077 | ret = is_allocated_sectors(buf, n, pnum); | |
1078 | if (!ret) { | |
1079 | return ret; | |
1080 | } | |
1081 | ||
1082 | num_used = *pnum; | |
1083 | buf += BDRV_SECTOR_SIZE * *pnum; | |
1084 | n -= *pnum; | |
1085 | num_checked = num_used; | |
1086 | ||
1087 | while (n > 0) { | |
1088 | ret = is_allocated_sectors(buf, n, pnum); | |
1089 | ||
1090 | buf += BDRV_SECTOR_SIZE * *pnum; | |
1091 | n -= *pnum; | |
1092 | num_checked += *pnum; | |
1093 | if (ret) { | |
1094 | num_used = num_checked; | |
1095 | } else if (*pnum >= min) { | |
1096 | break; | |
1097 | } | |
1098 | } | |
1099 | ||
1100 | *pnum = num_used; | |
1101 | return 1; | |
1102 | } | |
1103 | ||
1104 | /* | |
1105 | * Compares two buffers sector by sector. Returns 0 if the first sector of both | |
1106 | * buffers matches, non-zero otherwise. | |
1107 | * | |
1108 | * pnum is set to the number of sectors (including and immediately following | |
1109 | * the first one) that are known to have the same comparison result | |
1110 | */ | |
1111 | static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n, | |
1112 | int *pnum) | |
1113 | { | |
1114 | bool res; | |
1115 | int i; | |
1116 | ||
1117 | if (n <= 0) { | |
1118 | *pnum = 0; | |
1119 | return 0; | |
1120 | } | |
1121 | ||
1122 | res = !!memcmp(buf1, buf2, 512); | |
1123 | for(i = 1; i < n; i++) { | |
1124 | buf1 += 512; | |
1125 | buf2 += 512; | |
1126 | ||
1127 | if (!!memcmp(buf1, buf2, 512) != res) { | |
1128 | break; | |
1129 | } | |
1130 | } | |
1131 | ||
1132 | *pnum = i; | |
1133 | return res; | |
1134 | } | |
1135 | ||
1136 | #define IO_BUF_SIZE (2 * 1024 * 1024) | |
1137 | ||
1138 | static int64_t sectors_to_bytes(int64_t sectors) | |
1139 | { | |
1140 | return sectors << BDRV_SECTOR_BITS; | |
1141 | } | |
1142 | ||
1143 | static int64_t sectors_to_process(int64_t total, int64_t from) | |
1144 | { | |
1145 | return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS); | |
1146 | } | |
1147 | ||
1148 | /* | |
1149 | * Check if passed sectors are empty (not allocated or contain only 0 bytes) | |
1150 | * | |
1151 | * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero | |
1152 | * data and negative value on error. | |
1153 | * | |
1154 | * @param blk: BlockBackend for the image | |
1155 | * @param sect_num: Number of first sector to check | |
1156 | * @param sect_count: Number of sectors to check | |
1157 | * @param filename: Name of disk file we are checking (logging purpose) | |
1158 | * @param buffer: Allocated buffer for storing read data | |
1159 | * @param quiet: Flag for quiet mode | |
1160 | */ | |
1161 | static int check_empty_sectors(BlockBackend *blk, int64_t sect_num, | |
1162 | int sect_count, const char *filename, | |
1163 | uint8_t *buffer, bool quiet) | |
1164 | { | |
1165 | int pnum, ret = 0; | |
1166 | ret = blk_pread(blk, sect_num << BDRV_SECTOR_BITS, buffer, | |
1167 | sect_count << BDRV_SECTOR_BITS); | |
1168 | if (ret < 0) { | |
1169 | error_report("Error while reading offset %" PRId64 " of %s: %s", | |
1170 | sectors_to_bytes(sect_num), filename, strerror(-ret)); | |
1171 | return ret; | |
1172 | } | |
1173 | ret = is_allocated_sectors(buffer, sect_count, &pnum); | |
1174 | if (ret || pnum != sect_count) { | |
1175 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", | |
1176 | sectors_to_bytes(ret ? sect_num : sect_num + pnum)); | |
1177 | return 1; | |
1178 | } | |
1179 | ||
1180 | return 0; | |
1181 | } | |
1182 | ||
1183 | /* | |
1184 | * Compares two images. Exit codes: | |
1185 | * | |
1186 | * 0 - Images are identical | |
1187 | * 1 - Images differ | |
1188 | * >1 - Error occurred | |
1189 | */ | |
1190 | static int img_compare(int argc, char **argv) | |
1191 | { | |
1192 | const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2; | |
1193 | BlockBackend *blk1, *blk2; | |
1194 | BlockDriverState *bs1, *bs2; | |
1195 | int64_t total_sectors1, total_sectors2; | |
1196 | uint8_t *buf1 = NULL, *buf2 = NULL; | |
1197 | int pnum1, pnum2; | |
1198 | int allocated1, allocated2; | |
1199 | int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */ | |
1200 | bool progress = false, quiet = false, strict = false; | |
1201 | int flags; | |
1202 | bool writethrough; | |
1203 | int64_t total_sectors; | |
1204 | int64_t sector_num = 0; | |
1205 | int64_t nb_sectors; | |
1206 | int c, pnum; | |
1207 | uint64_t progress_base; | |
1208 | bool image_opts = false; | |
1209 | ||
1210 | cache = BDRV_DEFAULT_CACHE; | |
1211 | for (;;) { | |
1212 | static const struct option long_options[] = { | |
1213 | {"help", no_argument, 0, 'h'}, | |
1214 | {"object", required_argument, 0, OPTION_OBJECT}, | |
1215 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, | |
1216 | {0, 0, 0, 0} | |
1217 | }; | |
1218 | c = getopt_long(argc, argv, ":hf:F:T:pqs", | |
1219 | long_options, NULL); | |
1220 | if (c == -1) { | |
1221 | break; | |
1222 | } | |
1223 | switch (c) { | |
1224 | case ':': | |
1225 | missing_argument(argv[optind - 1]); | |
1226 | break; | |
1227 | case '?': | |
1228 | unrecognized_option(argv[optind - 1]); | |
1229 | break; | |
1230 | case 'h': | |
1231 | help(); | |
1232 | break; | |
1233 | case 'f': | |
1234 | fmt1 = optarg; | |
1235 | break; | |
1236 | case 'F': | |
1237 | fmt2 = optarg; | |
1238 | break; | |
1239 | case 'T': | |
1240 | cache = optarg; | |
1241 | break; | |
1242 | case 'p': | |
1243 | progress = true; | |
1244 | break; | |
1245 | case 'q': | |
1246 | quiet = true; | |
1247 | break; | |
1248 | case 's': | |
1249 | strict = true; | |
1250 | break; | |
1251 | case OPTION_OBJECT: { | |
1252 | QemuOpts *opts; | |
1253 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
1254 | optarg, true); | |
1255 | if (!opts) { | |
1256 | ret = 2; | |
1257 | goto out4; | |
1258 | } | |
1259 | } break; | |
1260 | case OPTION_IMAGE_OPTS: | |
1261 | image_opts = true; | |
1262 | break; | |
1263 | } | |
1264 | } | |
1265 | ||
1266 | /* Progress is not shown in Quiet mode */ | |
1267 | if (quiet) { | |
1268 | progress = false; | |
1269 | } | |
1270 | ||
1271 | ||
1272 | if (optind != argc - 2) { | |
1273 | error_exit("Expecting two image file names"); | |
1274 | } | |
1275 | filename1 = argv[optind++]; | |
1276 | filename2 = argv[optind++]; | |
1277 | ||
1278 | if (qemu_opts_foreach(&qemu_object_opts, | |
1279 | user_creatable_add_opts_foreach, | |
1280 | NULL, NULL)) { | |
1281 | ret = 2; | |
1282 | goto out4; | |
1283 | } | |
1284 | ||
1285 | /* Initialize before goto out */ | |
1286 | qemu_progress_init(progress, 2.0); | |
1287 | ||
1288 | flags = 0; | |
1289 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); | |
1290 | if (ret < 0) { | |
1291 | error_report("Invalid source cache option: %s", cache); | |
1292 | ret = 2; | |
1293 | goto out3; | |
1294 | } | |
1295 | ||
1296 | blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet); | |
1297 | if (!blk1) { | |
1298 | ret = 2; | |
1299 | goto out3; | |
1300 | } | |
1301 | ||
1302 | blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet); | |
1303 | if (!blk2) { | |
1304 | ret = 2; | |
1305 | goto out2; | |
1306 | } | |
1307 | bs1 = blk_bs(blk1); | |
1308 | bs2 = blk_bs(blk2); | |
1309 | ||
1310 | buf1 = blk_blockalign(blk1, IO_BUF_SIZE); | |
1311 | buf2 = blk_blockalign(blk2, IO_BUF_SIZE); | |
1312 | total_sectors1 = blk_nb_sectors(blk1); | |
1313 | if (total_sectors1 < 0) { | |
1314 | error_report("Can't get size of %s: %s", | |
1315 | filename1, strerror(-total_sectors1)); | |
1316 | ret = 4; | |
1317 | goto out; | |
1318 | } | |
1319 | total_sectors2 = blk_nb_sectors(blk2); | |
1320 | if (total_sectors2 < 0) { | |
1321 | error_report("Can't get size of %s: %s", | |
1322 | filename2, strerror(-total_sectors2)); | |
1323 | ret = 4; | |
1324 | goto out; | |
1325 | } | |
1326 | total_sectors = MIN(total_sectors1, total_sectors2); | |
1327 | progress_base = MAX(total_sectors1, total_sectors2); | |
1328 | ||
1329 | qemu_progress_print(0, 100); | |
1330 | ||
1331 | if (strict && total_sectors1 != total_sectors2) { | |
1332 | ret = 1; | |
1333 | qprintf(quiet, "Strict mode: Image size mismatch!\n"); | |
1334 | goto out; | |
1335 | } | |
1336 | ||
1337 | for (;;) { | |
1338 | int64_t status1, status2; | |
1339 | BlockDriverState *file; | |
1340 | ||
1341 | nb_sectors = sectors_to_process(total_sectors, sector_num); | |
1342 | if (nb_sectors <= 0) { | |
1343 | break; | |
1344 | } | |
1345 | status1 = bdrv_get_block_status_above(bs1, NULL, sector_num, | |
1346 | total_sectors1 - sector_num, | |
1347 | &pnum1, &file); | |
1348 | if (status1 < 0) { | |
1349 | ret = 3; | |
1350 | error_report("Sector allocation test failed for %s", filename1); | |
1351 | goto out; | |
1352 | } | |
1353 | allocated1 = status1 & BDRV_BLOCK_ALLOCATED; | |
1354 | ||
1355 | status2 = bdrv_get_block_status_above(bs2, NULL, sector_num, | |
1356 | total_sectors2 - sector_num, | |
1357 | &pnum2, &file); | |
1358 | if (status2 < 0) { | |
1359 | ret = 3; | |
1360 | error_report("Sector allocation test failed for %s", filename2); | |
1361 | goto out; | |
1362 | } | |
1363 | allocated2 = status2 & BDRV_BLOCK_ALLOCATED; | |
1364 | if (pnum1) { | |
1365 | nb_sectors = MIN(nb_sectors, pnum1); | |
1366 | } | |
1367 | if (pnum2) { | |
1368 | nb_sectors = MIN(nb_sectors, pnum2); | |
1369 | } | |
1370 | ||
1371 | if (strict) { | |
1372 | if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) != | |
1373 | (status2 & ~BDRV_BLOCK_OFFSET_MASK)) { | |
1374 | ret = 1; | |
1375 | qprintf(quiet, "Strict mode: Offset %" PRId64 | |
1376 | " block status mismatch!\n", | |
1377 | sectors_to_bytes(sector_num)); | |
1378 | goto out; | |
1379 | } | |
1380 | } | |
1381 | if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) { | |
1382 | nb_sectors = MIN(pnum1, pnum2); | |
1383 | } else if (allocated1 == allocated2) { | |
1384 | if (allocated1) { | |
1385 | ret = blk_pread(blk1, sector_num << BDRV_SECTOR_BITS, buf1, | |
1386 | nb_sectors << BDRV_SECTOR_BITS); | |
1387 | if (ret < 0) { | |
1388 | error_report("Error while reading offset %" PRId64 " of %s:" | |
1389 | " %s", sectors_to_bytes(sector_num), filename1, | |
1390 | strerror(-ret)); | |
1391 | ret = 4; | |
1392 | goto out; | |
1393 | } | |
1394 | ret = blk_pread(blk2, sector_num << BDRV_SECTOR_BITS, buf2, | |
1395 | nb_sectors << BDRV_SECTOR_BITS); | |
1396 | if (ret < 0) { | |
1397 | error_report("Error while reading offset %" PRId64 | |
1398 | " of %s: %s", sectors_to_bytes(sector_num), | |
1399 | filename2, strerror(-ret)); | |
1400 | ret = 4; | |
1401 | goto out; | |
1402 | } | |
1403 | ret = compare_sectors(buf1, buf2, nb_sectors, &pnum); | |
1404 | if (ret || pnum != nb_sectors) { | |
1405 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", | |
1406 | sectors_to_bytes( | |
1407 | ret ? sector_num : sector_num + pnum)); | |
1408 | ret = 1; | |
1409 | goto out; | |
1410 | } | |
1411 | } | |
1412 | } else { | |
1413 | ||
1414 | if (allocated1) { | |
1415 | ret = check_empty_sectors(blk1, sector_num, nb_sectors, | |
1416 | filename1, buf1, quiet); | |
1417 | } else { | |
1418 | ret = check_empty_sectors(blk2, sector_num, nb_sectors, | |
1419 | filename2, buf1, quiet); | |
1420 | } | |
1421 | if (ret) { | |
1422 | if (ret < 0) { | |
1423 | error_report("Error while reading offset %" PRId64 ": %s", | |
1424 | sectors_to_bytes(sector_num), strerror(-ret)); | |
1425 | ret = 4; | |
1426 | } | |
1427 | goto out; | |
1428 | } | |
1429 | } | |
1430 | sector_num += nb_sectors; | |
1431 | qemu_progress_print(((float) nb_sectors / progress_base)*100, 100); | |
1432 | } | |
1433 | ||
1434 | if (total_sectors1 != total_sectors2) { | |
1435 | BlockBackend *blk_over; | |
1436 | int64_t total_sectors_over; | |
1437 | const char *filename_over; | |
1438 | ||
1439 | qprintf(quiet, "Warning: Image size mismatch!\n"); | |
1440 | if (total_sectors1 > total_sectors2) { | |
1441 | total_sectors_over = total_sectors1; | |
1442 | blk_over = blk1; | |
1443 | filename_over = filename1; | |
1444 | } else { | |
1445 | total_sectors_over = total_sectors2; | |
1446 | blk_over = blk2; | |
1447 | filename_over = filename2; | |
1448 | } | |
1449 | ||
1450 | for (;;) { | |
1451 | nb_sectors = sectors_to_process(total_sectors_over, sector_num); | |
1452 | if (nb_sectors <= 0) { | |
1453 | break; | |
1454 | } | |
1455 | ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL, sector_num, | |
1456 | nb_sectors, &pnum); | |
1457 | if (ret < 0) { | |
1458 | ret = 3; | |
1459 | error_report("Sector allocation test failed for %s", | |
1460 | filename_over); | |
1461 | goto out; | |
1462 | ||
1463 | } | |
1464 | nb_sectors = pnum; | |
1465 | if (ret) { | |
1466 | ret = check_empty_sectors(blk_over, sector_num, nb_sectors, | |
1467 | filename_over, buf1, quiet); | |
1468 | if (ret) { | |
1469 | if (ret < 0) { | |
1470 | error_report("Error while reading offset %" PRId64 | |
1471 | " of %s: %s", sectors_to_bytes(sector_num), | |
1472 | filename_over, strerror(-ret)); | |
1473 | ret = 4; | |
1474 | } | |
1475 | goto out; | |
1476 | } | |
1477 | } | |
1478 | sector_num += nb_sectors; | |
1479 | qemu_progress_print(((float) nb_sectors / progress_base)*100, 100); | |
1480 | } | |
1481 | } | |
1482 | ||
1483 | qprintf(quiet, "Images are identical.\n"); | |
1484 | ret = 0; | |
1485 | ||
1486 | out: | |
1487 | qemu_vfree(buf1); | |
1488 | qemu_vfree(buf2); | |
1489 | blk_unref(blk2); | |
1490 | out2: | |
1491 | blk_unref(blk1); | |
1492 | out3: | |
1493 | qemu_progress_end(); | |
1494 | out4: | |
1495 | return ret; | |
1496 | } | |
1497 | ||
1498 | enum ImgConvertBlockStatus { | |
1499 | BLK_DATA, | |
1500 | BLK_ZERO, | |
1501 | BLK_BACKING_FILE, | |
1502 | }; | |
1503 | ||
1504 | #define MAX_COROUTINES 16 | |
1505 | ||
1506 | typedef struct ImgConvertState { | |
1507 | BlockBackend **src; | |
1508 | int64_t *src_sectors; | |
1509 | int src_num; | |
1510 | int64_t total_sectors; | |
1511 | int64_t allocated_sectors; | |
1512 | int64_t allocated_done; | |
1513 | int64_t sector_num; | |
1514 | int64_t wr_offs; | |
1515 | enum ImgConvertBlockStatus status; | |
1516 | int64_t sector_next_status; | |
1517 | BlockBackend *target; | |
1518 | bool has_zero_init; | |
1519 | bool compressed; | |
1520 | bool target_has_backing; | |
1521 | bool wr_in_order; | |
1522 | int min_sparse; | |
1523 | size_t cluster_sectors; | |
1524 | size_t buf_sectors; | |
1525 | int num_coroutines; | |
1526 | int running_coroutines; | |
1527 | Coroutine *co[MAX_COROUTINES]; | |
1528 | int64_t wait_sector_num[MAX_COROUTINES]; | |
1529 | CoMutex lock; | |
1530 | int ret; | |
1531 | } ImgConvertState; | |
1532 | ||
1533 | static void convert_select_part(ImgConvertState *s, int64_t sector_num, | |
1534 | int *src_cur, int64_t *src_cur_offset) | |
1535 | { | |
1536 | *src_cur = 0; | |
1537 | *src_cur_offset = 0; | |
1538 | while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) { | |
1539 | *src_cur_offset += s->src_sectors[*src_cur]; | |
1540 | (*src_cur)++; | |
1541 | assert(*src_cur < s->src_num); | |
1542 | } | |
1543 | } | |
1544 | ||
1545 | static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num) | |
1546 | { | |
1547 | int64_t ret, src_cur_offset; | |
1548 | int n, src_cur; | |
1549 | ||
1550 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); | |
1551 | ||
1552 | assert(s->total_sectors > sector_num); | |
1553 | n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS); | |
1554 | ||
1555 | if (s->sector_next_status <= sector_num) { | |
1556 | BlockDriverState *file; | |
1557 | ret = bdrv_get_block_status(blk_bs(s->src[src_cur]), | |
1558 | sector_num - src_cur_offset, | |
1559 | n, &n, &file); | |
1560 | if (ret < 0) { | |
1561 | return ret; | |
1562 | } | |
1563 | ||
1564 | if (ret & BDRV_BLOCK_ZERO) { | |
1565 | s->status = BLK_ZERO; | |
1566 | } else if (ret & BDRV_BLOCK_DATA) { | |
1567 | s->status = BLK_DATA; | |
1568 | } else if (!s->target_has_backing) { | |
1569 | /* Without a target backing file we must copy over the contents of | |
1570 | * the backing file as well. */ | |
1571 | /* Check block status of the backing file chain to avoid | |
1572 | * needlessly reading zeroes and limiting the iteration to the | |
1573 | * buffer size */ | |
1574 | ret = bdrv_get_block_status_above(blk_bs(s->src[src_cur]), NULL, | |
1575 | sector_num - src_cur_offset, | |
1576 | n, &n, &file); | |
1577 | if (ret < 0) { | |
1578 | return ret; | |
1579 | } | |
1580 | ||
1581 | if (ret & BDRV_BLOCK_ZERO) { | |
1582 | s->status = BLK_ZERO; | |
1583 | } else { | |
1584 | s->status = BLK_DATA; | |
1585 | } | |
1586 | } else { | |
1587 | s->status = BLK_BACKING_FILE; | |
1588 | } | |
1589 | ||
1590 | s->sector_next_status = sector_num + n; | |
1591 | } | |
1592 | ||
1593 | n = MIN(n, s->sector_next_status - sector_num); | |
1594 | if (s->status == BLK_DATA) { | |
1595 | n = MIN(n, s->buf_sectors); | |
1596 | } | |
1597 | ||
1598 | /* We need to write complete clusters for compressed images, so if an | |
1599 | * unallocated area is shorter than that, we must consider the whole | |
1600 | * cluster allocated. */ | |
1601 | if (s->compressed) { | |
1602 | if (n < s->cluster_sectors) { | |
1603 | n = MIN(s->cluster_sectors, s->total_sectors - sector_num); | |
1604 | s->status = BLK_DATA; | |
1605 | } else { | |
1606 | n = QEMU_ALIGN_DOWN(n, s->cluster_sectors); | |
1607 | } | |
1608 | } | |
1609 | ||
1610 | return n; | |
1611 | } | |
1612 | ||
1613 | static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num, | |
1614 | int nb_sectors, uint8_t *buf) | |
1615 | { | |
1616 | int n, ret; | |
1617 | QEMUIOVector qiov; | |
1618 | struct iovec iov; | |
1619 | ||
1620 | assert(nb_sectors <= s->buf_sectors); | |
1621 | while (nb_sectors > 0) { | |
1622 | BlockBackend *blk; | |
1623 | int src_cur; | |
1624 | int64_t bs_sectors, src_cur_offset; | |
1625 | ||
1626 | /* In the case of compression with multiple source files, we can get a | |
1627 | * nb_sectors that spreads into the next part. So we must be able to | |
1628 | * read across multiple BDSes for one convert_read() call. */ | |
1629 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); | |
1630 | blk = s->src[src_cur]; | |
1631 | bs_sectors = s->src_sectors[src_cur]; | |
1632 | ||
1633 | n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset)); | |
1634 | iov.iov_base = buf; | |
1635 | iov.iov_len = n << BDRV_SECTOR_BITS; | |
1636 | qemu_iovec_init_external(&qiov, &iov, 1); | |
1637 | ||
1638 | ret = blk_co_preadv( | |
1639 | blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS, | |
1640 | n << BDRV_SECTOR_BITS, &qiov, 0); | |
1641 | if (ret < 0) { | |
1642 | return ret; | |
1643 | } | |
1644 | ||
1645 | sector_num += n; | |
1646 | nb_sectors -= n; | |
1647 | buf += n * BDRV_SECTOR_SIZE; | |
1648 | } | |
1649 | ||
1650 | return 0; | |
1651 | } | |
1652 | ||
1653 | ||
1654 | static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num, | |
1655 | int nb_sectors, uint8_t *buf, | |
1656 | enum ImgConvertBlockStatus status) | |
1657 | { | |
1658 | int ret; | |
1659 | QEMUIOVector qiov; | |
1660 | struct iovec iov; | |
1661 | ||
1662 | while (nb_sectors > 0) { | |
1663 | int n = nb_sectors; | |
1664 | switch (status) { | |
1665 | case BLK_BACKING_FILE: | |
1666 | /* If we have a backing file, leave clusters unallocated that are | |
1667 | * unallocated in the source image, so that the backing file is | |
1668 | * visible at the respective offset. */ | |
1669 | assert(s->target_has_backing); | |
1670 | break; | |
1671 | ||
1672 | case BLK_DATA: | |
1673 | /* We must always write compressed clusters as a whole, so don't | |
1674 | * try to find zeroed parts in the buffer. We can only save the | |
1675 | * write if the buffer is completely zeroed and we're allowed to | |
1676 | * keep the target sparse. */ | |
1677 | if (s->compressed) { | |
1678 | if (s->has_zero_init && s->min_sparse && | |
1679 | buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)) | |
1680 | { | |
1681 | assert(!s->target_has_backing); | |
1682 | break; | |
1683 | } | |
1684 | ||
1685 | iov.iov_base = buf; | |
1686 | iov.iov_len = n << BDRV_SECTOR_BITS; | |
1687 | qemu_iovec_init_external(&qiov, &iov, 1); | |
1688 | ||
1689 | ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS, | |
1690 | n << BDRV_SECTOR_BITS, &qiov, | |
1691 | BDRV_REQ_WRITE_COMPRESSED); | |
1692 | if (ret < 0) { | |
1693 | return ret; | |
1694 | } | |
1695 | break; | |
1696 | } | |
1697 | ||
1698 | /* If there is real non-zero data or we're told to keep the target | |
1699 | * fully allocated (-S 0), we must write it. Otherwise we can treat | |
1700 | * it as zero sectors. */ | |
1701 | if (!s->min_sparse || | |
1702 | is_allocated_sectors_min(buf, n, &n, s->min_sparse)) | |
1703 | { | |
1704 | iov.iov_base = buf; | |
1705 | iov.iov_len = n << BDRV_SECTOR_BITS; | |
1706 | qemu_iovec_init_external(&qiov, &iov, 1); | |
1707 | ||
1708 | ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS, | |
1709 | n << BDRV_SECTOR_BITS, &qiov, 0); | |
1710 | if (ret < 0) { | |
1711 | return ret; | |
1712 | } | |
1713 | break; | |
1714 | } | |
1715 | /* fall-through */ | |
1716 | ||
1717 | case BLK_ZERO: | |
1718 | if (s->has_zero_init) { | |
1719 | break; | |
1720 | } | |
1721 | ret = blk_co_pwrite_zeroes(s->target, | |
1722 | sector_num << BDRV_SECTOR_BITS, | |
1723 | n << BDRV_SECTOR_BITS, 0); | |
1724 | if (ret < 0) { | |
1725 | return ret; | |
1726 | } | |
1727 | break; | |
1728 | } | |
1729 | ||
1730 | sector_num += n; | |
1731 | nb_sectors -= n; | |
1732 | buf += n * BDRV_SECTOR_SIZE; | |
1733 | } | |
1734 | ||
1735 | return 0; | |
1736 | } | |
1737 | ||
1738 | static void coroutine_fn convert_co_do_copy(void *opaque) | |
1739 | { | |
1740 | ImgConvertState *s = opaque; | |
1741 | uint8_t *buf = NULL; | |
1742 | int ret, i; | |
1743 | int index = -1; | |
1744 | ||
1745 | for (i = 0; i < s->num_coroutines; i++) { | |
1746 | if (s->co[i] == qemu_coroutine_self()) { | |
1747 | index = i; | |
1748 | break; | |
1749 | } | |
1750 | } | |
1751 | assert(index >= 0); | |
1752 | ||
1753 | s->running_coroutines++; | |
1754 | buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE); | |
1755 | ||
1756 | while (1) { | |
1757 | int n; | |
1758 | int64_t sector_num; | |
1759 | enum ImgConvertBlockStatus status; | |
1760 | ||
1761 | qemu_co_mutex_lock(&s->lock); | |
1762 | if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) { | |
1763 | qemu_co_mutex_unlock(&s->lock); | |
1764 | break; | |
1765 | } | |
1766 | n = convert_iteration_sectors(s, s->sector_num); | |
1767 | if (n < 0) { | |
1768 | qemu_co_mutex_unlock(&s->lock); | |
1769 | s->ret = n; | |
1770 | break; | |
1771 | } | |
1772 | /* save current sector and allocation status to local variables */ | |
1773 | sector_num = s->sector_num; | |
1774 | status = s->status; | |
1775 | if (!s->min_sparse && s->status == BLK_ZERO) { | |
1776 | n = MIN(n, s->buf_sectors); | |
1777 | } | |
1778 | /* increment global sector counter so that other coroutines can | |
1779 | * already continue reading beyond this request */ | |
1780 | s->sector_num += n; | |
1781 | qemu_co_mutex_unlock(&s->lock); | |
1782 | ||
1783 | if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) { | |
1784 | s->allocated_done += n; | |
1785 | qemu_progress_print(100.0 * s->allocated_done / | |
1786 | s->allocated_sectors, 0); | |
1787 | } | |
1788 | ||
1789 | if (status == BLK_DATA) { | |
1790 | ret = convert_co_read(s, sector_num, n, buf); | |
1791 | if (ret < 0) { | |
1792 | error_report("error while reading sector %" PRId64 | |
1793 | ": %s", sector_num, strerror(-ret)); | |
1794 | s->ret = ret; | |
1795 | } | |
1796 | } else if (!s->min_sparse && status == BLK_ZERO) { | |
1797 | status = BLK_DATA; | |
1798 | memset(buf, 0x00, n * BDRV_SECTOR_SIZE); | |
1799 | } | |
1800 | ||
1801 | if (s->wr_in_order) { | |
1802 | /* keep writes in order */ | |
1803 | while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) { | |
1804 | s->wait_sector_num[index] = sector_num; | |
1805 | qemu_coroutine_yield(); | |
1806 | } | |
1807 | s->wait_sector_num[index] = -1; | |
1808 | } | |
1809 | ||
1810 | if (s->ret == -EINPROGRESS) { | |
1811 | ret = convert_co_write(s, sector_num, n, buf, status); | |
1812 | if (ret < 0) { | |
1813 | error_report("error while writing sector %" PRId64 | |
1814 | ": %s", sector_num, strerror(-ret)); | |
1815 | s->ret = ret; | |
1816 | } | |
1817 | } | |
1818 | ||
1819 | if (s->wr_in_order) { | |
1820 | /* reenter the coroutine that might have waited | |
1821 | * for this write to complete */ | |
1822 | s->wr_offs = sector_num + n; | |
1823 | for (i = 0; i < s->num_coroutines; i++) { | |
1824 | if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) { | |
1825 | /* | |
1826 | * A -> B -> A cannot occur because A has | |
1827 | * s->wait_sector_num[i] == -1 during A -> B. Therefore | |
1828 | * B will never enter A during this time window. | |
1829 | */ | |
1830 | qemu_coroutine_enter(s->co[i]); | |
1831 | break; | |
1832 | } | |
1833 | } | |
1834 | } | |
1835 | } | |
1836 | ||
1837 | qemu_vfree(buf); | |
1838 | s->co[index] = NULL; | |
1839 | s->running_coroutines--; | |
1840 | if (!s->running_coroutines && s->ret == -EINPROGRESS) { | |
1841 | /* the convert job finished successfully */ | |
1842 | s->ret = 0; | |
1843 | } | |
1844 | } | |
1845 | ||
1846 | static int convert_do_copy(ImgConvertState *s) | |
1847 | { | |
1848 | int ret, i, n; | |
1849 | int64_t sector_num = 0; | |
1850 | ||
1851 | /* Check whether we have zero initialisation or can get it efficiently */ | |
1852 | s->has_zero_init = s->min_sparse && !s->target_has_backing | |
1853 | ? bdrv_has_zero_init(blk_bs(s->target)) | |
1854 | : false; | |
1855 | ||
1856 | if (!s->has_zero_init && !s->target_has_backing && | |
1857 | bdrv_can_write_zeroes_with_unmap(blk_bs(s->target))) | |
1858 | { | |
1859 | ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP); | |
1860 | if (ret == 0) { | |
1861 | s->has_zero_init = true; | |
1862 | } | |
1863 | } | |
1864 | ||
1865 | /* Allocate buffer for copied data. For compressed images, only one cluster | |
1866 | * can be copied at a time. */ | |
1867 | if (s->compressed) { | |
1868 | if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) { | |
1869 | error_report("invalid cluster size"); | |
1870 | return -EINVAL; | |
1871 | } | |
1872 | s->buf_sectors = s->cluster_sectors; | |
1873 | } | |
1874 | ||
1875 | while (sector_num < s->total_sectors) { | |
1876 | n = convert_iteration_sectors(s, sector_num); | |
1877 | if (n < 0) { | |
1878 | return n; | |
1879 | } | |
1880 | if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO)) | |
1881 | { | |
1882 | s->allocated_sectors += n; | |
1883 | } | |
1884 | sector_num += n; | |
1885 | } | |
1886 | ||
1887 | /* Do the copy */ | |
1888 | s->sector_next_status = 0; | |
1889 | s->ret = -EINPROGRESS; | |
1890 | ||
1891 | qemu_co_mutex_init(&s->lock); | |
1892 | for (i = 0; i < s->num_coroutines; i++) { | |
1893 | s->co[i] = qemu_coroutine_create(convert_co_do_copy, s); | |
1894 | s->wait_sector_num[i] = -1; | |
1895 | qemu_coroutine_enter(s->co[i]); | |
1896 | } | |
1897 | ||
1898 | while (s->running_coroutines) { | |
1899 | main_loop_wait(false); | |
1900 | } | |
1901 | ||
1902 | if (s->compressed && !s->ret) { | |
1903 | /* signal EOF to align */ | |
1904 | ret = blk_pwrite_compressed(s->target, 0, NULL, 0); | |
1905 | if (ret < 0) { | |
1906 | return ret; | |
1907 | } | |
1908 | } | |
1909 | ||
1910 | return s->ret; | |
1911 | } | |
1912 | ||
1913 | static int img_convert(int argc, char **argv) | |
1914 | { | |
1915 | int c, bs_n, bs_i, compress, cluster_sectors, skip_create; | |
1916 | int64_t ret = 0; | |
1917 | int progress = 0, flags, src_flags; | |
1918 | bool writethrough, src_writethrough; | |
1919 | const char *fmt, *out_fmt, *cache, *src_cache, *out_baseimg, *out_filename; | |
1920 | BlockDriver *drv, *proto_drv; | |
1921 | BlockBackend **blk = NULL, *out_blk = NULL; | |
1922 | BlockDriverState **bs = NULL, *out_bs = NULL; | |
1923 | int64_t total_sectors; | |
1924 | int64_t *bs_sectors = NULL; | |
1925 | size_t bufsectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE; | |
1926 | BlockDriverInfo bdi; | |
1927 | QemuOpts *opts = NULL; | |
1928 | QemuOptsList *create_opts = NULL; | |
1929 | const char *out_baseimg_param; | |
1930 | char *options = NULL; | |
1931 | const char *snapshot_name = NULL; | |
1932 | int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */ | |
1933 | bool quiet = false; | |
1934 | Error *local_err = NULL; | |
1935 | QemuOpts *sn_opts = NULL; | |
1936 | ImgConvertState state; | |
1937 | bool image_opts = false; | |
1938 | bool wr_in_order = true; | |
1939 | long num_coroutines = 8; | |
1940 | ||
1941 | fmt = NULL; | |
1942 | out_fmt = "raw"; | |
1943 | cache = "unsafe"; | |
1944 | src_cache = BDRV_DEFAULT_CACHE; | |
1945 | out_baseimg = NULL; | |
1946 | compress = 0; | |
1947 | skip_create = 0; | |
1948 | for(;;) { | |
1949 | static const struct option long_options[] = { | |
1950 | {"help", no_argument, 0, 'h'}, | |
1951 | {"object", required_argument, 0, OPTION_OBJECT}, | |
1952 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, | |
1953 | {0, 0, 0, 0} | |
1954 | }; | |
1955 | c = getopt_long(argc, argv, ":hf:O:B:ce6o:s:l:S:pt:T:qnm:W", | |
1956 | long_options, NULL); | |
1957 | if (c == -1) { | |
1958 | break; | |
1959 | } | |
1960 | switch(c) { | |
1961 | case ':': | |
1962 | missing_argument(argv[optind - 1]); | |
1963 | break; | |
1964 | case '?': | |
1965 | unrecognized_option(argv[optind - 1]); | |
1966 | break; | |
1967 | case 'h': | |
1968 | help(); | |
1969 | break; | |
1970 | case 'f': | |
1971 | fmt = optarg; | |
1972 | break; | |
1973 | case 'O': | |
1974 | out_fmt = optarg; | |
1975 | break; | |
1976 | case 'B': | |
1977 | out_baseimg = optarg; | |
1978 | break; | |
1979 | case 'c': | |
1980 | compress = 1; | |
1981 | break; | |
1982 | case 'e': | |
1983 | error_report("option -e is deprecated, please use \'-o " | |
1984 | "encryption\' instead!"); | |
1985 | ret = -1; | |
1986 | goto fail_getopt; | |
1987 | case '6': | |
1988 | error_report("option -6 is deprecated, please use \'-o " | |
1989 | "compat6\' instead!"); | |
1990 | ret = -1; | |
1991 | goto fail_getopt; | |
1992 | case 'o': | |
1993 | if (!is_valid_option_list(optarg)) { | |
1994 | error_report("Invalid option list: %s", optarg); | |
1995 | ret = -1; | |
1996 | goto fail_getopt; | |
1997 | } | |
1998 | if (!options) { | |
1999 | options = g_strdup(optarg); | |
2000 | } else { | |
2001 | char *old_options = options; | |
2002 | options = g_strdup_printf("%s,%s", options, optarg); | |
2003 | g_free(old_options); | |
2004 | } | |
2005 | break; | |
2006 | case 's': | |
2007 | snapshot_name = optarg; | |
2008 | break; | |
2009 | case 'l': | |
2010 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { | |
2011 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, | |
2012 | optarg, false); | |
2013 | if (!sn_opts) { | |
2014 | error_report("Failed in parsing snapshot param '%s'", | |
2015 | optarg); | |
2016 | ret = -1; | |
2017 | goto fail_getopt; | |
2018 | } | |
2019 | } else { | |
2020 | snapshot_name = optarg; | |
2021 | } | |
2022 | break; | |
2023 | case 'S': | |
2024 | { | |
2025 | int64_t sval; | |
2026 | ||
2027 | sval = cvtnum(optarg); | |
2028 | if (sval < 0) { | |
2029 | error_report("Invalid minimum zero buffer size for sparse output specified"); | |
2030 | ret = -1; | |
2031 | goto fail_getopt; | |
2032 | } | |
2033 | ||
2034 | min_sparse = sval / BDRV_SECTOR_SIZE; | |
2035 | break; | |
2036 | } | |
2037 | case 'p': | |
2038 | progress = 1; | |
2039 | break; | |
2040 | case 't': | |
2041 | cache = optarg; | |
2042 | break; | |
2043 | case 'T': | |
2044 | src_cache = optarg; | |
2045 | break; | |
2046 | case 'q': | |
2047 | quiet = true; | |
2048 | break; | |
2049 | case 'n': | |
2050 | skip_create = 1; | |
2051 | break; | |
2052 | case 'm': | |
2053 | if (qemu_strtol(optarg, NULL, 0, &num_coroutines) || | |
2054 | num_coroutines < 1 || num_coroutines > MAX_COROUTINES) { | |
2055 | error_report("Invalid number of coroutines. Allowed number of" | |
2056 | " coroutines is between 1 and %d", MAX_COROUTINES); | |
2057 | ret = -1; | |
2058 | goto fail_getopt; | |
2059 | } | |
2060 | break; | |
2061 | case 'W': | |
2062 | wr_in_order = false; | |
2063 | break; | |
2064 | case OPTION_OBJECT: { | |
2065 | QemuOpts *object_opts; | |
2066 | object_opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
2067 | optarg, true); | |
2068 | if (!object_opts) { | |
2069 | ret = -1; | |
2070 | goto fail_getopt; | |
2071 | } | |
2072 | break; | |
2073 | } | |
2074 | case OPTION_IMAGE_OPTS: | |
2075 | image_opts = true; | |
2076 | break; | |
2077 | } | |
2078 | } | |
2079 | ||
2080 | if (qemu_opts_foreach(&qemu_object_opts, | |
2081 | user_creatable_add_opts_foreach, | |
2082 | NULL, NULL)) { | |
2083 | ret = -1; | |
2084 | goto fail_getopt; | |
2085 | } | |
2086 | ||
2087 | if (!wr_in_order && compress) { | |
2088 | error_report("Out of order write and compress are mutually exclusive"); | |
2089 | ret = -1; | |
2090 | goto fail_getopt; | |
2091 | } | |
2092 | ||
2093 | /* Initialize before goto out */ | |
2094 | if (quiet) { | |
2095 | progress = 0; | |
2096 | } | |
2097 | qemu_progress_init(progress, 1.0); | |
2098 | ||
2099 | bs_n = argc - optind - 1; | |
2100 | out_filename = bs_n >= 1 ? argv[argc - 1] : NULL; | |
2101 | ||
2102 | if (options && has_help_option(options)) { | |
2103 | ret = print_block_option_help(out_filename, out_fmt); | |
2104 | goto out; | |
2105 | } | |
2106 | ||
2107 | if (bs_n < 1) { | |
2108 | error_exit("Must specify image file name"); | |
2109 | } | |
2110 | ||
2111 | ||
2112 | src_flags = 0; | |
2113 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); | |
2114 | if (ret < 0) { | |
2115 | error_report("Invalid source cache option: %s", src_cache); | |
2116 | goto out; | |
2117 | } | |
2118 | ||
2119 | qemu_progress_print(0, 100); | |
2120 | ||
2121 | blk = g_new0(BlockBackend *, bs_n); | |
2122 | bs = g_new0(BlockDriverState *, bs_n); | |
2123 | bs_sectors = g_new(int64_t, bs_n); | |
2124 | ||
2125 | total_sectors = 0; | |
2126 | for (bs_i = 0; bs_i < bs_n; bs_i++) { | |
2127 | blk[bs_i] = img_open(image_opts, argv[optind + bs_i], | |
2128 | fmt, src_flags, src_writethrough, quiet); | |
2129 | if (!blk[bs_i]) { | |
2130 | ret = -1; | |
2131 | goto out; | |
2132 | } | |
2133 | bs[bs_i] = blk_bs(blk[bs_i]); | |
2134 | bs_sectors[bs_i] = blk_nb_sectors(blk[bs_i]); | |
2135 | if (bs_sectors[bs_i] < 0) { | |
2136 | error_report("Could not get size of %s: %s", | |
2137 | argv[optind + bs_i], strerror(-bs_sectors[bs_i])); | |
2138 | ret = -1; | |
2139 | goto out; | |
2140 | } | |
2141 | total_sectors += bs_sectors[bs_i]; | |
2142 | } | |
2143 | ||
2144 | if (sn_opts) { | |
2145 | bdrv_snapshot_load_tmp(bs[0], | |
2146 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), | |
2147 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), | |
2148 | &local_err); | |
2149 | } else if (snapshot_name != NULL) { | |
2150 | if (bs_n > 1) { | |
2151 | error_report("No support for concatenating multiple snapshot"); | |
2152 | ret = -1; | |
2153 | goto out; | |
2154 | } | |
2155 | ||
2156 | bdrv_snapshot_load_tmp_by_id_or_name(bs[0], snapshot_name, &local_err); | |
2157 | } | |
2158 | if (local_err) { | |
2159 | error_reportf_err(local_err, "Failed to load snapshot: "); | |
2160 | ret = -1; | |
2161 | goto out; | |
2162 | } | |
2163 | ||
2164 | /* Find driver and parse its options */ | |
2165 | drv = bdrv_find_format(out_fmt); | |
2166 | if (!drv) { | |
2167 | error_report("Unknown file format '%s'", out_fmt); | |
2168 | ret = -1; | |
2169 | goto out; | |
2170 | } | |
2171 | ||
2172 | proto_drv = bdrv_find_protocol(out_filename, true, &local_err); | |
2173 | if (!proto_drv) { | |
2174 | error_report_err(local_err); | |
2175 | ret = -1; | |
2176 | goto out; | |
2177 | } | |
2178 | ||
2179 | if (!skip_create) { | |
2180 | if (!drv->create_opts) { | |
2181 | error_report("Format driver '%s' does not support image creation", | |
2182 | drv->format_name); | |
2183 | ret = -1; | |
2184 | goto out; | |
2185 | } | |
2186 | ||
2187 | if (!proto_drv->create_opts) { | |
2188 | error_report("Protocol driver '%s' does not support image creation", | |
2189 | proto_drv->format_name); | |
2190 | ret = -1; | |
2191 | goto out; | |
2192 | } | |
2193 | ||
2194 | create_opts = qemu_opts_append(create_opts, drv->create_opts); | |
2195 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); | |
2196 | ||
2197 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); | |
2198 | if (options) { | |
2199 | qemu_opts_do_parse(opts, options, NULL, &local_err); | |
2200 | if (local_err) { | |
2201 | error_report_err(local_err); | |
2202 | ret = -1; | |
2203 | goto out; | |
2204 | } | |
2205 | } | |
2206 | ||
2207 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_sectors * 512, | |
2208 | &error_abort); | |
2209 | ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL); | |
2210 | if (ret < 0) { | |
2211 | goto out; | |
2212 | } | |
2213 | } | |
2214 | ||
2215 | /* Get backing file name if -o backing_file was used */ | |
2216 | out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); | |
2217 | if (out_baseimg_param) { | |
2218 | out_baseimg = out_baseimg_param; | |
2219 | } | |
2220 | ||
2221 | if (bs_n > 1 && out_baseimg) { | |
2222 | error_report("Having a backing file for the target makes no sense when " | |
2223 | "concatenating multiple input images"); | |
2224 | ret = -1; | |
2225 | goto out; | |
2226 | } | |
2227 | ||
2228 | /* Check if compression is supported */ | |
2229 | if (compress) { | |
2230 | bool encryption = | |
2231 | qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false); | |
2232 | const char *preallocation = | |
2233 | qemu_opt_get(opts, BLOCK_OPT_PREALLOC); | |
2234 | ||
2235 | if (!drv->bdrv_co_pwritev_compressed) { | |
2236 | error_report("Compression not supported for this file format"); | |
2237 | ret = -1; | |
2238 | goto out; | |
2239 | } | |
2240 | ||
2241 | if (encryption) { | |
2242 | error_report("Compression and encryption not supported at " | |
2243 | "the same time"); | |
2244 | ret = -1; | |
2245 | goto out; | |
2246 | } | |
2247 | ||
2248 | if (preallocation | |
2249 | && strcmp(preallocation, "off")) | |
2250 | { | |
2251 | error_report("Compression and preallocation not supported at " | |
2252 | "the same time"); | |
2253 | ret = -1; | |
2254 | goto out; | |
2255 | } | |
2256 | } | |
2257 | ||
2258 | if (!skip_create) { | |
2259 | /* Create the new image */ | |
2260 | ret = bdrv_create(drv, out_filename, opts, &local_err); | |
2261 | if (ret < 0) { | |
2262 | error_reportf_err(local_err, "%s: error while converting %s: ", | |
2263 | out_filename, out_fmt); | |
2264 | goto out; | |
2265 | } | |
2266 | } | |
2267 | ||
2268 | flags = min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR; | |
2269 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); | |
2270 | if (ret < 0) { | |
2271 | error_report("Invalid cache option: %s", cache); | |
2272 | goto out; | |
2273 | } | |
2274 | ||
2275 | /* XXX we should allow --image-opts to trigger use of | |
2276 | * img_open() here, but then we have trouble with | |
2277 | * the bdrv_create() call which takes different params. | |
2278 | * Not critical right now, so fix can wait... | |
2279 | */ | |
2280 | out_blk = img_open_file(out_filename, out_fmt, flags, writethrough, quiet); | |
2281 | if (!out_blk) { | |
2282 | ret = -1; | |
2283 | goto out; | |
2284 | } | |
2285 | out_bs = blk_bs(out_blk); | |
2286 | ||
2287 | /* increase bufsectors from the default 4096 (2M) if opt_transfer | |
2288 | * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB) | |
2289 | * as maximum. */ | |
2290 | bufsectors = MIN(32768, | |
2291 | MAX(bufsectors, | |
2292 | MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS, | |
2293 | out_bs->bl.pdiscard_alignment >> | |
2294 | BDRV_SECTOR_BITS))); | |
2295 | ||
2296 | if (skip_create) { | |
2297 | int64_t output_sectors = blk_nb_sectors(out_blk); | |
2298 | if (output_sectors < 0) { | |
2299 | error_report("unable to get output image length: %s", | |
2300 | strerror(-output_sectors)); | |
2301 | ret = -1; | |
2302 | goto out; | |
2303 | } else if (output_sectors < total_sectors) { | |
2304 | error_report("output file is smaller than input file"); | |
2305 | ret = -1; | |
2306 | goto out; | |
2307 | } | |
2308 | } | |
2309 | ||
2310 | cluster_sectors = 0; | |
2311 | ret = bdrv_get_info(out_bs, &bdi); | |
2312 | if (ret < 0) { | |
2313 | if (compress) { | |
2314 | error_report("could not get block driver info"); | |
2315 | goto out; | |
2316 | } | |
2317 | } else { | |
2318 | compress = compress || bdi.needs_compressed_writes; | |
2319 | cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE; | |
2320 | } | |
2321 | ||
2322 | state = (ImgConvertState) { | |
2323 | .src = blk, | |
2324 | .src_sectors = bs_sectors, | |
2325 | .src_num = bs_n, | |
2326 | .total_sectors = total_sectors, | |
2327 | .target = out_blk, | |
2328 | .compressed = compress, | |
2329 | .target_has_backing = (bool) out_baseimg, | |
2330 | .min_sparse = min_sparse, | |
2331 | .cluster_sectors = cluster_sectors, | |
2332 | .buf_sectors = bufsectors, | |
2333 | .wr_in_order = wr_in_order, | |
2334 | .num_coroutines = num_coroutines, | |
2335 | }; | |
2336 | ret = convert_do_copy(&state); | |
2337 | ||
2338 | out: | |
2339 | if (!ret) { | |
2340 | qemu_progress_print(100, 0); | |
2341 | } | |
2342 | qemu_progress_end(); | |
2343 | qemu_opts_del(opts); | |
2344 | qemu_opts_free(create_opts); | |
2345 | qemu_opts_del(sn_opts); | |
2346 | blk_unref(out_blk); | |
2347 | g_free(bs); | |
2348 | if (blk) { | |
2349 | for (bs_i = 0; bs_i < bs_n; bs_i++) { | |
2350 | blk_unref(blk[bs_i]); | |
2351 | } | |
2352 | g_free(blk); | |
2353 | } | |
2354 | g_free(bs_sectors); | |
2355 | fail_getopt: | |
2356 | g_free(options); | |
2357 | ||
2358 | if (ret) { | |
2359 | return 1; | |
2360 | } | |
2361 | return 0; | |
2362 | } | |
2363 | ||
2364 | ||
2365 | static void dump_snapshots(BlockDriverState *bs) | |
2366 | { | |
2367 | QEMUSnapshotInfo *sn_tab, *sn; | |
2368 | int nb_sns, i; | |
2369 | ||
2370 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); | |
2371 | if (nb_sns <= 0) | |
2372 | return; | |
2373 | printf("Snapshot list:\n"); | |
2374 | bdrv_snapshot_dump(fprintf, stdout, NULL); | |
2375 | printf("\n"); | |
2376 | for(i = 0; i < nb_sns; i++) { | |
2377 | sn = &sn_tab[i]; | |
2378 | bdrv_snapshot_dump(fprintf, stdout, sn); | |
2379 | printf("\n"); | |
2380 | } | |
2381 | g_free(sn_tab); | |
2382 | } | |
2383 | ||
2384 | static void dump_json_image_info_list(ImageInfoList *list) | |
2385 | { | |
2386 | QString *str; | |
2387 | QObject *obj; | |
2388 | Visitor *v = qobject_output_visitor_new(&obj); | |
2389 | ||
2390 | visit_type_ImageInfoList(v, NULL, &list, &error_abort); | |
2391 | visit_complete(v, &obj); | |
2392 | str = qobject_to_json_pretty(obj); | |
2393 | assert(str != NULL); | |
2394 | printf("%s\n", qstring_get_str(str)); | |
2395 | qobject_decref(obj); | |
2396 | visit_free(v); | |
2397 | QDECREF(str); | |
2398 | } | |
2399 | ||
2400 | static void dump_json_image_info(ImageInfo *info) | |
2401 | { | |
2402 | QString *str; | |
2403 | QObject *obj; | |
2404 | Visitor *v = qobject_output_visitor_new(&obj); | |
2405 | ||
2406 | visit_type_ImageInfo(v, NULL, &info, &error_abort); | |
2407 | visit_complete(v, &obj); | |
2408 | str = qobject_to_json_pretty(obj); | |
2409 | assert(str != NULL); | |
2410 | printf("%s\n", qstring_get_str(str)); | |
2411 | qobject_decref(obj); | |
2412 | visit_free(v); | |
2413 | QDECREF(str); | |
2414 | } | |
2415 | ||
2416 | static void dump_human_image_info_list(ImageInfoList *list) | |
2417 | { | |
2418 | ImageInfoList *elem; | |
2419 | bool delim = false; | |
2420 | ||
2421 | for (elem = list; elem; elem = elem->next) { | |
2422 | if (delim) { | |
2423 | printf("\n"); | |
2424 | } | |
2425 | delim = true; | |
2426 | ||
2427 | bdrv_image_info_dump(fprintf, stdout, elem->value); | |
2428 | } | |
2429 | } | |
2430 | ||
2431 | static gboolean str_equal_func(gconstpointer a, gconstpointer b) | |
2432 | { | |
2433 | return strcmp(a, b) == 0; | |
2434 | } | |
2435 | ||
2436 | /** | |
2437 | * Open an image file chain and return an ImageInfoList | |
2438 | * | |
2439 | * @filename: topmost image filename | |
2440 | * @fmt: topmost image format (may be NULL to autodetect) | |
2441 | * @chain: true - enumerate entire backing file chain | |
2442 | * false - only topmost image file | |
2443 | * | |
2444 | * Returns a list of ImageInfo objects or NULL if there was an error opening an | |
2445 | * image file. If there was an error a message will have been printed to | |
2446 | * stderr. | |
2447 | */ | |
2448 | static ImageInfoList *collect_image_info_list(bool image_opts, | |
2449 | const char *filename, | |
2450 | const char *fmt, | |
2451 | bool chain) | |
2452 | { | |
2453 | ImageInfoList *head = NULL; | |
2454 | ImageInfoList **last = &head; | |
2455 | GHashTable *filenames; | |
2456 | Error *err = NULL; | |
2457 | ||
2458 | filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL); | |
2459 | ||
2460 | while (filename) { | |
2461 | BlockBackend *blk; | |
2462 | BlockDriverState *bs; | |
2463 | ImageInfo *info; | |
2464 | ImageInfoList *elem; | |
2465 | ||
2466 | if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) { | |
2467 | error_report("Backing file '%s' creates an infinite loop.", | |
2468 | filename); | |
2469 | goto err; | |
2470 | } | |
2471 | g_hash_table_insert(filenames, (gpointer)filename, NULL); | |
2472 | ||
2473 | blk = img_open(image_opts, filename, fmt, | |
2474 | BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false); | |
2475 | if (!blk) { | |
2476 | goto err; | |
2477 | } | |
2478 | bs = blk_bs(blk); | |
2479 | ||
2480 | bdrv_query_image_info(bs, &info, &err); | |
2481 | if (err) { | |
2482 | error_report_err(err); | |
2483 | blk_unref(blk); | |
2484 | goto err; | |
2485 | } | |
2486 | ||
2487 | elem = g_new0(ImageInfoList, 1); | |
2488 | elem->value = info; | |
2489 | *last = elem; | |
2490 | last = &elem->next; | |
2491 | ||
2492 | blk_unref(blk); | |
2493 | ||
2494 | filename = fmt = NULL; | |
2495 | if (chain) { | |
2496 | if (info->has_full_backing_filename) { | |
2497 | filename = info->full_backing_filename; | |
2498 | } else if (info->has_backing_filename) { | |
2499 | error_report("Could not determine absolute backing filename," | |
2500 | " but backing filename '%s' present", | |
2501 | info->backing_filename); | |
2502 | goto err; | |
2503 | } | |
2504 | if (info->has_backing_filename_format) { | |
2505 | fmt = info->backing_filename_format; | |
2506 | } | |
2507 | } | |
2508 | } | |
2509 | g_hash_table_destroy(filenames); | |
2510 | return head; | |
2511 | ||
2512 | err: | |
2513 | qapi_free_ImageInfoList(head); | |
2514 | g_hash_table_destroy(filenames); | |
2515 | return NULL; | |
2516 | } | |
2517 | ||
2518 | static int img_info(int argc, char **argv) | |
2519 | { | |
2520 | int c; | |
2521 | OutputFormat output_format = OFORMAT_HUMAN; | |
2522 | bool chain = false; | |
2523 | const char *filename, *fmt, *output; | |
2524 | ImageInfoList *list; | |
2525 | bool image_opts = false; | |
2526 | ||
2527 | fmt = NULL; | |
2528 | output = NULL; | |
2529 | for(;;) { | |
2530 | int option_index = 0; | |
2531 | static const struct option long_options[] = { | |
2532 | {"help", no_argument, 0, 'h'}, | |
2533 | {"format", required_argument, 0, 'f'}, | |
2534 | {"output", required_argument, 0, OPTION_OUTPUT}, | |
2535 | {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN}, | |
2536 | {"object", required_argument, 0, OPTION_OBJECT}, | |
2537 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, | |
2538 | {0, 0, 0, 0} | |
2539 | }; | |
2540 | c = getopt_long(argc, argv, ":f:h", | |
2541 | long_options, &option_index); | |
2542 | if (c == -1) { | |
2543 | break; | |
2544 | } | |
2545 | switch(c) { | |
2546 | case ':': | |
2547 | missing_argument(argv[optind - 1]); | |
2548 | break; | |
2549 | case '?': | |
2550 | unrecognized_option(argv[optind - 1]); | |
2551 | break; | |
2552 | case 'h': | |
2553 | help(); | |
2554 | break; | |
2555 | case 'f': | |
2556 | fmt = optarg; | |
2557 | break; | |
2558 | case OPTION_OUTPUT: | |
2559 | output = optarg; | |
2560 | break; | |
2561 | case OPTION_BACKING_CHAIN: | |
2562 | chain = true; | |
2563 | break; | |
2564 | case OPTION_OBJECT: { | |
2565 | QemuOpts *opts; | |
2566 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
2567 | optarg, true); | |
2568 | if (!opts) { | |
2569 | return 1; | |
2570 | } | |
2571 | } break; | |
2572 | case OPTION_IMAGE_OPTS: | |
2573 | image_opts = true; | |
2574 | break; | |
2575 | } | |
2576 | } | |
2577 | if (optind != argc - 1) { | |
2578 | error_exit("Expecting one image file name"); | |
2579 | } | |
2580 | filename = argv[optind++]; | |
2581 | ||
2582 | if (output && !strcmp(output, "json")) { | |
2583 | output_format = OFORMAT_JSON; | |
2584 | } else if (output && !strcmp(output, "human")) { | |
2585 | output_format = OFORMAT_HUMAN; | |
2586 | } else if (output) { | |
2587 | error_report("--output must be used with human or json as argument."); | |
2588 | return 1; | |
2589 | } | |
2590 | ||
2591 | if (qemu_opts_foreach(&qemu_object_opts, | |
2592 | user_creatable_add_opts_foreach, | |
2593 | NULL, NULL)) { | |
2594 | return 1; | |
2595 | } | |
2596 | ||
2597 | list = collect_image_info_list(image_opts, filename, fmt, chain); | |
2598 | if (!list) { | |
2599 | return 1; | |
2600 | } | |
2601 | ||
2602 | switch (output_format) { | |
2603 | case OFORMAT_HUMAN: | |
2604 | dump_human_image_info_list(list); | |
2605 | break; | |
2606 | case OFORMAT_JSON: | |
2607 | if (chain) { | |
2608 | dump_json_image_info_list(list); | |
2609 | } else { | |
2610 | dump_json_image_info(list->value); | |
2611 | } | |
2612 | break; | |
2613 | } | |
2614 | ||
2615 | qapi_free_ImageInfoList(list); | |
2616 | return 0; | |
2617 | } | |
2618 | ||
2619 | static void dump_map_entry(OutputFormat output_format, MapEntry *e, | |
2620 | MapEntry *next) | |
2621 | { | |
2622 | switch (output_format) { | |
2623 | case OFORMAT_HUMAN: | |
2624 | if (e->data && !e->has_offset) { | |
2625 | error_report("File contains external, encrypted or compressed clusters."); | |
2626 | exit(1); | |
2627 | } | |
2628 | if (e->data && !e->zero) { | |
2629 | printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n", | |
2630 | e->start, e->length, | |
2631 | e->has_offset ? e->offset : 0, | |
2632 | e->has_filename ? e->filename : ""); | |
2633 | } | |
2634 | /* This format ignores the distinction between 0, ZERO and ZERO|DATA. | |
2635 | * Modify the flags here to allow more coalescing. | |
2636 | */ | |
2637 | if (next && (!next->data || next->zero)) { | |
2638 | next->data = false; | |
2639 | next->zero = true; | |
2640 | } | |
2641 | break; | |
2642 | case OFORMAT_JSON: | |
2643 | printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64"," | |
2644 | " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s", | |
2645 | (e->start == 0 ? "[" : ",\n"), | |
2646 | e->start, e->length, e->depth, | |
2647 | e->zero ? "true" : "false", | |
2648 | e->data ? "true" : "false"); | |
2649 | if (e->has_offset) { | |
2650 | printf(", \"offset\": %"PRId64"", e->offset); | |
2651 | } | |
2652 | putchar('}'); | |
2653 | ||
2654 | if (!next) { | |
2655 | printf("]\n"); | |
2656 | } | |
2657 | break; | |
2658 | } | |
2659 | } | |
2660 | ||
2661 | static int get_block_status(BlockDriverState *bs, int64_t sector_num, | |
2662 | int nb_sectors, MapEntry *e) | |
2663 | { | |
2664 | int64_t ret; | |
2665 | int depth; | |
2666 | BlockDriverState *file; | |
2667 | bool has_offset; | |
2668 | ||
2669 | /* As an optimization, we could cache the current range of unallocated | |
2670 | * clusters in each file of the chain, and avoid querying the same | |
2671 | * range repeatedly. | |
2672 | */ | |
2673 | ||
2674 | depth = 0; | |
2675 | for (;;) { | |
2676 | ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors, | |
2677 | &file); | |
2678 | if (ret < 0) { | |
2679 | return ret; | |
2680 | } | |
2681 | assert(nb_sectors); | |
2682 | if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) { | |
2683 | break; | |
2684 | } | |
2685 | bs = backing_bs(bs); | |
2686 | if (bs == NULL) { | |
2687 | ret = 0; | |
2688 | break; | |
2689 | } | |
2690 | ||
2691 | depth++; | |
2692 | } | |
2693 | ||
2694 | has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID); | |
2695 | ||
2696 | *e = (MapEntry) { | |
2697 | .start = sector_num * BDRV_SECTOR_SIZE, | |
2698 | .length = nb_sectors * BDRV_SECTOR_SIZE, | |
2699 | .data = !!(ret & BDRV_BLOCK_DATA), | |
2700 | .zero = !!(ret & BDRV_BLOCK_ZERO), | |
2701 | .offset = ret & BDRV_BLOCK_OFFSET_MASK, | |
2702 | .has_offset = has_offset, | |
2703 | .depth = depth, | |
2704 | .has_filename = file && has_offset, | |
2705 | .filename = file && has_offset ? file->filename : NULL, | |
2706 | }; | |
2707 | ||
2708 | return 0; | |
2709 | } | |
2710 | ||
2711 | static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next) | |
2712 | { | |
2713 | if (curr->length == 0) { | |
2714 | return false; | |
2715 | } | |
2716 | if (curr->zero != next->zero || | |
2717 | curr->data != next->data || | |
2718 | curr->depth != next->depth || | |
2719 | curr->has_filename != next->has_filename || | |
2720 | curr->has_offset != next->has_offset) { | |
2721 | return false; | |
2722 | } | |
2723 | if (curr->has_filename && strcmp(curr->filename, next->filename)) { | |
2724 | return false; | |
2725 | } | |
2726 | if (curr->has_offset && curr->offset + curr->length != next->offset) { | |
2727 | return false; | |
2728 | } | |
2729 | return true; | |
2730 | } | |
2731 | ||
2732 | static int img_map(int argc, char **argv) | |
2733 | { | |
2734 | int c; | |
2735 | OutputFormat output_format = OFORMAT_HUMAN; | |
2736 | BlockBackend *blk; | |
2737 | BlockDriverState *bs; | |
2738 | const char *filename, *fmt, *output; | |
2739 | int64_t length; | |
2740 | MapEntry curr = { .length = 0 }, next; | |
2741 | int ret = 0; | |
2742 | bool image_opts = false; | |
2743 | ||
2744 | fmt = NULL; | |
2745 | output = NULL; | |
2746 | for (;;) { | |
2747 | int option_index = 0; | |
2748 | static const struct option long_options[] = { | |
2749 | {"help", no_argument, 0, 'h'}, | |
2750 | {"format", required_argument, 0, 'f'}, | |
2751 | {"output", required_argument, 0, OPTION_OUTPUT}, | |
2752 | {"object", required_argument, 0, OPTION_OBJECT}, | |
2753 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, | |
2754 | {0, 0, 0, 0} | |
2755 | }; | |
2756 | c = getopt_long(argc, argv, ":f:h", | |
2757 | long_options, &option_index); | |
2758 | if (c == -1) { | |
2759 | break; | |
2760 | } | |
2761 | switch (c) { | |
2762 | case ':': | |
2763 | missing_argument(argv[optind - 1]); | |
2764 | break; | |
2765 | case '?': | |
2766 | unrecognized_option(argv[optind - 1]); | |
2767 | break; | |
2768 | case 'h': | |
2769 | help(); | |
2770 | break; | |
2771 | case 'f': | |
2772 | fmt = optarg; | |
2773 | break; | |
2774 | case OPTION_OUTPUT: | |
2775 | output = optarg; | |
2776 | break; | |
2777 | case OPTION_OBJECT: { | |
2778 | QemuOpts *opts; | |
2779 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
2780 | optarg, true); | |
2781 | if (!opts) { | |
2782 | return 1; | |
2783 | } | |
2784 | } break; | |
2785 | case OPTION_IMAGE_OPTS: | |
2786 | image_opts = true; | |
2787 | break; | |
2788 | } | |
2789 | } | |
2790 | if (optind != argc - 1) { | |
2791 | error_exit("Expecting one image file name"); | |
2792 | } | |
2793 | filename = argv[optind]; | |
2794 | ||
2795 | if (output && !strcmp(output, "json")) { | |
2796 | output_format = OFORMAT_JSON; | |
2797 | } else if (output && !strcmp(output, "human")) { | |
2798 | output_format = OFORMAT_HUMAN; | |
2799 | } else if (output) { | |
2800 | error_report("--output must be used with human or json as argument."); | |
2801 | return 1; | |
2802 | } | |
2803 | ||
2804 | if (qemu_opts_foreach(&qemu_object_opts, | |
2805 | user_creatable_add_opts_foreach, | |
2806 | NULL, NULL)) { | |
2807 | return 1; | |
2808 | } | |
2809 | ||
2810 | blk = img_open(image_opts, filename, fmt, 0, false, false); | |
2811 | if (!blk) { | |
2812 | return 1; | |
2813 | } | |
2814 | bs = blk_bs(blk); | |
2815 | ||
2816 | if (output_format == OFORMAT_HUMAN) { | |
2817 | printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File"); | |
2818 | } | |
2819 | ||
2820 | length = blk_getlength(blk); | |
2821 | while (curr.start + curr.length < length) { | |
2822 | int64_t nsectors_left; | |
2823 | int64_t sector_num; | |
2824 | int n; | |
2825 | ||
2826 | sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS; | |
2827 | ||
2828 | /* Probe up to 1 GiB at a time. */ | |
2829 | nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num; | |
2830 | n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left); | |
2831 | ret = get_block_status(bs, sector_num, n, &next); | |
2832 | ||
2833 | if (ret < 0) { | |
2834 | error_report("Could not read file metadata: %s", strerror(-ret)); | |
2835 | goto out; | |
2836 | } | |
2837 | ||
2838 | if (entry_mergeable(&curr, &next)) { | |
2839 | curr.length += next.length; | |
2840 | continue; | |
2841 | } | |
2842 | ||
2843 | if (curr.length > 0) { | |
2844 | dump_map_entry(output_format, &curr, &next); | |
2845 | } | |
2846 | curr = next; | |
2847 | } | |
2848 | ||
2849 | dump_map_entry(output_format, &curr, NULL); | |
2850 | ||
2851 | out: | |
2852 | blk_unref(blk); | |
2853 | return ret < 0; | |
2854 | } | |
2855 | ||
2856 | #define SNAPSHOT_LIST 1 | |
2857 | #define SNAPSHOT_CREATE 2 | |
2858 | #define SNAPSHOT_APPLY 3 | |
2859 | #define SNAPSHOT_DELETE 4 | |
2860 | ||
2861 | static int img_snapshot(int argc, char **argv) | |
2862 | { | |
2863 | BlockBackend *blk; | |
2864 | BlockDriverState *bs; | |
2865 | QEMUSnapshotInfo sn; | |
2866 | char *filename, *snapshot_name = NULL; | |
2867 | int c, ret = 0, bdrv_oflags; | |
2868 | int action = 0; | |
2869 | qemu_timeval tv; | |
2870 | bool quiet = false; | |
2871 | Error *err = NULL; | |
2872 | bool image_opts = false; | |
2873 | ||
2874 | bdrv_oflags = BDRV_O_RDWR; | |
2875 | /* Parse commandline parameters */ | |
2876 | for(;;) { | |
2877 | static const struct option long_options[] = { | |
2878 | {"help", no_argument, 0, 'h'}, | |
2879 | {"object", required_argument, 0, OPTION_OBJECT}, | |
2880 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, | |
2881 | {0, 0, 0, 0} | |
2882 | }; | |
2883 | c = getopt_long(argc, argv, ":la:c:d:hq", | |
2884 | long_options, NULL); | |
2885 | if (c == -1) { | |
2886 | break; | |
2887 | } | |
2888 | switch(c) { | |
2889 | case ':': | |
2890 | missing_argument(argv[optind - 1]); | |
2891 | break; | |
2892 | case '?': | |
2893 | unrecognized_option(argv[optind - 1]); | |
2894 | break; | |
2895 | case 'h': | |
2896 | help(); | |
2897 | return 0; | |
2898 | case 'l': | |
2899 | if (action) { | |
2900 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); | |
2901 | return 0; | |
2902 | } | |
2903 | action = SNAPSHOT_LIST; | |
2904 | bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */ | |
2905 | break; | |
2906 | case 'a': | |
2907 | if (action) { | |
2908 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); | |
2909 | return 0; | |
2910 | } | |
2911 | action = SNAPSHOT_APPLY; | |
2912 | snapshot_name = optarg; | |
2913 | break; | |
2914 | case 'c': | |
2915 | if (action) { | |
2916 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); | |
2917 | return 0; | |
2918 | } | |
2919 | action = SNAPSHOT_CREATE; | |
2920 | snapshot_name = optarg; | |
2921 | break; | |
2922 | case 'd': | |
2923 | if (action) { | |
2924 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); | |
2925 | return 0; | |
2926 | } | |
2927 | action = SNAPSHOT_DELETE; | |
2928 | snapshot_name = optarg; | |
2929 | break; | |
2930 | case 'q': | |
2931 | quiet = true; | |
2932 | break; | |
2933 | case OPTION_OBJECT: { | |
2934 | QemuOpts *opts; | |
2935 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
2936 | optarg, true); | |
2937 | if (!opts) { | |
2938 | return 1; | |
2939 | } | |
2940 | } break; | |
2941 | case OPTION_IMAGE_OPTS: | |
2942 | image_opts = true; | |
2943 | break; | |
2944 | } | |
2945 | } | |
2946 | ||
2947 | if (optind != argc - 1) { | |
2948 | error_exit("Expecting one image file name"); | |
2949 | } | |
2950 | filename = argv[optind++]; | |
2951 | ||
2952 | if (qemu_opts_foreach(&qemu_object_opts, | |
2953 | user_creatable_add_opts_foreach, | |
2954 | NULL, NULL)) { | |
2955 | return 1; | |
2956 | } | |
2957 | ||
2958 | /* Open the image */ | |
2959 | blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet); | |
2960 | if (!blk) { | |
2961 | return 1; | |
2962 | } | |
2963 | bs = blk_bs(blk); | |
2964 | ||
2965 | /* Perform the requested action */ | |
2966 | switch(action) { | |
2967 | case SNAPSHOT_LIST: | |
2968 | dump_snapshots(bs); | |
2969 | break; | |
2970 | ||
2971 | case SNAPSHOT_CREATE: | |
2972 | memset(&sn, 0, sizeof(sn)); | |
2973 | pstrcpy(sn.name, sizeof(sn.name), snapshot_name); | |
2974 | ||
2975 | qemu_gettimeofday(&tv); | |
2976 | sn.date_sec = tv.tv_sec; | |
2977 | sn.date_nsec = tv.tv_usec * 1000; | |
2978 | ||
2979 | ret = bdrv_snapshot_create(bs, &sn); | |
2980 | if (ret) { | |
2981 | error_report("Could not create snapshot '%s': %d (%s)", | |
2982 | snapshot_name, ret, strerror(-ret)); | |
2983 | } | |
2984 | break; | |
2985 | ||
2986 | case SNAPSHOT_APPLY: | |
2987 | ret = bdrv_snapshot_goto(bs, snapshot_name); | |
2988 | if (ret) { | |
2989 | error_report("Could not apply snapshot '%s': %d (%s)", | |
2990 | snapshot_name, ret, strerror(-ret)); | |
2991 | } | |
2992 | break; | |
2993 | ||
2994 | case SNAPSHOT_DELETE: | |
2995 | bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err); | |
2996 | if (err) { | |
2997 | error_reportf_err(err, "Could not delete snapshot '%s': ", | |
2998 | snapshot_name); | |
2999 | ret = 1; | |
3000 | } | |
3001 | break; | |
3002 | } | |
3003 | ||
3004 | /* Cleanup */ | |
3005 | blk_unref(blk); | |
3006 | if (ret) { | |
3007 | return 1; | |
3008 | } | |
3009 | return 0; | |
3010 | } | |
3011 | ||
3012 | static int img_rebase(int argc, char **argv) | |
3013 | { | |
3014 | BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL; | |
3015 | uint8_t *buf_old = NULL; | |
3016 | uint8_t *buf_new = NULL; | |
3017 | BlockDriverState *bs = NULL; | |
3018 | char *filename; | |
3019 | const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg; | |
3020 | int c, flags, src_flags, ret; | |
3021 | bool writethrough, src_writethrough; | |
3022 | int unsafe = 0; | |
3023 | int progress = 0; | |
3024 | bool quiet = false; | |
3025 | Error *local_err = NULL; | |
3026 | bool image_opts = false; | |
3027 | ||
3028 | /* Parse commandline parameters */ | |
3029 | fmt = NULL; | |
3030 | cache = BDRV_DEFAULT_CACHE; | |
3031 | src_cache = BDRV_DEFAULT_CACHE; | |
3032 | out_baseimg = NULL; | |
3033 | out_basefmt = NULL; | |
3034 | for(;;) { | |
3035 | static const struct option long_options[] = { | |
3036 | {"help", no_argument, 0, 'h'}, | |
3037 | {"object", required_argument, 0, OPTION_OBJECT}, | |
3038 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, | |
3039 | {0, 0, 0, 0} | |
3040 | }; | |
3041 | c = getopt_long(argc, argv, ":hf:F:b:upt:T:q", | |
3042 | long_options, NULL); | |
3043 | if (c == -1) { | |
3044 | break; | |
3045 | } | |
3046 | switch(c) { | |
3047 | case ':': | |
3048 | missing_argument(argv[optind - 1]); | |
3049 | break; | |
3050 | case '?': | |
3051 | unrecognized_option(argv[optind - 1]); | |
3052 | break; | |
3053 | case 'h': | |
3054 | help(); | |
3055 | return 0; | |
3056 | case 'f': | |
3057 | fmt = optarg; | |
3058 | break; | |
3059 | case 'F': | |
3060 | out_basefmt = optarg; | |
3061 | break; | |
3062 | case 'b': | |
3063 | out_baseimg = optarg; | |
3064 | break; | |
3065 | case 'u': | |
3066 | unsafe = 1; | |
3067 | break; | |
3068 | case 'p': | |
3069 | progress = 1; | |
3070 | break; | |
3071 | case 't': | |
3072 | cache = optarg; | |
3073 | break; | |
3074 | case 'T': | |
3075 | src_cache = optarg; | |
3076 | break; | |
3077 | case 'q': | |
3078 | quiet = true; | |
3079 | break; | |
3080 | case OPTION_OBJECT: { | |
3081 | QemuOpts *opts; | |
3082 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
3083 | optarg, true); | |
3084 | if (!opts) { | |
3085 | return 1; | |
3086 | } | |
3087 | } break; | |
3088 | case OPTION_IMAGE_OPTS: | |
3089 | image_opts = true; | |
3090 | break; | |
3091 | } | |
3092 | } | |
3093 | ||
3094 | if (quiet) { | |
3095 | progress = 0; | |
3096 | } | |
3097 | ||
3098 | if (optind != argc - 1) { | |
3099 | error_exit("Expecting one image file name"); | |
3100 | } | |
3101 | if (!unsafe && !out_baseimg) { | |
3102 | error_exit("Must specify backing file (-b) or use unsafe mode (-u)"); | |
3103 | } | |
3104 | filename = argv[optind++]; | |
3105 | ||
3106 | if (qemu_opts_foreach(&qemu_object_opts, | |
3107 | user_creatable_add_opts_foreach, | |
3108 | NULL, NULL)) { | |
3109 | return 1; | |
3110 | } | |
3111 | ||
3112 | qemu_progress_init(progress, 2.0); | |
3113 | qemu_progress_print(0, 100); | |
3114 | ||
3115 | flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0); | |
3116 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); | |
3117 | if (ret < 0) { | |
3118 | error_report("Invalid cache option: %s", cache); | |
3119 | goto out; | |
3120 | } | |
3121 | ||
3122 | src_flags = 0; | |
3123 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); | |
3124 | if (ret < 0) { | |
3125 | error_report("Invalid source cache option: %s", src_cache); | |
3126 | goto out; | |
3127 | } | |
3128 | ||
3129 | /* The source files are opened read-only, don't care about WCE */ | |
3130 | assert((src_flags & BDRV_O_RDWR) == 0); | |
3131 | (void) src_writethrough; | |
3132 | ||
3133 | /* | |
3134 | * Open the images. | |
3135 | * | |
3136 | * Ignore the old backing file for unsafe rebase in case we want to correct | |
3137 | * the reference to a renamed or moved backing file. | |
3138 | */ | |
3139 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet); | |
3140 | if (!blk) { | |
3141 | ret = -1; | |
3142 | goto out; | |
3143 | } | |
3144 | bs = blk_bs(blk); | |
3145 | ||
3146 | if (out_basefmt != NULL) { | |
3147 | if (bdrv_find_format(out_basefmt) == NULL) { | |
3148 | error_report("Invalid format name: '%s'", out_basefmt); | |
3149 | ret = -1; | |
3150 | goto out; | |
3151 | } | |
3152 | } | |
3153 | ||
3154 | /* For safe rebasing we need to compare old and new backing file */ | |
3155 | if (!unsafe) { | |
3156 | char backing_name[PATH_MAX]; | |
3157 | QDict *options = NULL; | |
3158 | ||
3159 | if (bs->backing_format[0] != '\0') { | |
3160 | options = qdict_new(); | |
3161 | qdict_put_str(options, "driver", bs->backing_format); | |
3162 | } | |
3163 | ||
3164 | bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name)); | |
3165 | blk_old_backing = blk_new_open(backing_name, NULL, | |
3166 | options, src_flags, &local_err); | |
3167 | if (!blk_old_backing) { | |
3168 | error_reportf_err(local_err, | |
3169 | "Could not open old backing file '%s': ", | |
3170 | backing_name); | |
3171 | ret = -1; | |
3172 | goto out; | |
3173 | } | |
3174 | ||
3175 | if (out_baseimg[0]) { | |
3176 | if (out_basefmt) { | |
3177 | options = qdict_new(); | |
3178 | qdict_put_str(options, "driver", out_basefmt); | |
3179 | } else { | |
3180 | options = NULL; | |
3181 | } | |
3182 | ||
3183 | blk_new_backing = blk_new_open(out_baseimg, NULL, | |
3184 | options, src_flags, &local_err); | |
3185 | if (!blk_new_backing) { | |
3186 | error_reportf_err(local_err, | |
3187 | "Could not open new backing file '%s': ", | |
3188 | out_baseimg); | |
3189 | ret = -1; | |
3190 | goto out; | |
3191 | } | |
3192 | } | |
3193 | } | |
3194 | ||
3195 | /* | |
3196 | * Check each unallocated cluster in the COW file. If it is unallocated, | |
3197 | * accesses go to the backing file. We must therefore compare this cluster | |
3198 | * in the old and new backing file, and if they differ we need to copy it | |
3199 | * from the old backing file into the COW file. | |
3200 | * | |
3201 | * If qemu-img crashes during this step, no harm is done. The content of | |
3202 | * the image is the same as the original one at any time. | |
3203 | */ | |
3204 | if (!unsafe) { | |
3205 | int64_t num_sectors; | |
3206 | int64_t old_backing_num_sectors; | |
3207 | int64_t new_backing_num_sectors = 0; | |
3208 | uint64_t sector; | |
3209 | int n; | |
3210 | float local_progress = 0; | |
3211 | ||
3212 | buf_old = blk_blockalign(blk, IO_BUF_SIZE); | |
3213 | buf_new = blk_blockalign(blk, IO_BUF_SIZE); | |
3214 | ||
3215 | num_sectors = blk_nb_sectors(blk); | |
3216 | if (num_sectors < 0) { | |
3217 | error_report("Could not get size of '%s': %s", | |
3218 | filename, strerror(-num_sectors)); | |
3219 | ret = -1; | |
3220 | goto out; | |
3221 | } | |
3222 | old_backing_num_sectors = blk_nb_sectors(blk_old_backing); | |
3223 | if (old_backing_num_sectors < 0) { | |
3224 | char backing_name[PATH_MAX]; | |
3225 | ||
3226 | bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name)); | |
3227 | error_report("Could not get size of '%s': %s", | |
3228 | backing_name, strerror(-old_backing_num_sectors)); | |
3229 | ret = -1; | |
3230 | goto out; | |
3231 | } | |
3232 | if (blk_new_backing) { | |
3233 | new_backing_num_sectors = blk_nb_sectors(blk_new_backing); | |
3234 | if (new_backing_num_sectors < 0) { | |
3235 | error_report("Could not get size of '%s': %s", | |
3236 | out_baseimg, strerror(-new_backing_num_sectors)); | |
3237 | ret = -1; | |
3238 | goto out; | |
3239 | } | |
3240 | } | |
3241 | ||
3242 | if (num_sectors != 0) { | |
3243 | local_progress = (float)100 / | |
3244 | (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512)); | |
3245 | } | |
3246 | ||
3247 | for (sector = 0; sector < num_sectors; sector += n) { | |
3248 | ||
3249 | /* How many sectors can we handle with the next read? */ | |
3250 | if (sector + (IO_BUF_SIZE / 512) <= num_sectors) { | |
3251 | n = (IO_BUF_SIZE / 512); | |
3252 | } else { | |
3253 | n = num_sectors - sector; | |
3254 | } | |
3255 | ||
3256 | /* If the cluster is allocated, we don't need to take action */ | |
3257 | ret = bdrv_is_allocated(bs, sector, n, &n); | |
3258 | if (ret < 0) { | |
3259 | error_report("error while reading image metadata: %s", | |
3260 | strerror(-ret)); | |
3261 | goto out; | |
3262 | } | |
3263 | if (ret) { | |
3264 | continue; | |
3265 | } | |
3266 | ||
3267 | /* | |
3268 | * Read old and new backing file and take into consideration that | |
3269 | * backing files may be smaller than the COW image. | |
3270 | */ | |
3271 | if (sector >= old_backing_num_sectors) { | |
3272 | memset(buf_old, 0, n * BDRV_SECTOR_SIZE); | |
3273 | } else { | |
3274 | if (sector + n > old_backing_num_sectors) { | |
3275 | n = old_backing_num_sectors - sector; | |
3276 | } | |
3277 | ||
3278 | ret = blk_pread(blk_old_backing, sector << BDRV_SECTOR_BITS, | |
3279 | buf_old, n << BDRV_SECTOR_BITS); | |
3280 | if (ret < 0) { | |
3281 | error_report("error while reading from old backing file"); | |
3282 | goto out; | |
3283 | } | |
3284 | } | |
3285 | ||
3286 | if (sector >= new_backing_num_sectors || !blk_new_backing) { | |
3287 | memset(buf_new, 0, n * BDRV_SECTOR_SIZE); | |
3288 | } else { | |
3289 | if (sector + n > new_backing_num_sectors) { | |
3290 | n = new_backing_num_sectors - sector; | |
3291 | } | |
3292 | ||
3293 | ret = blk_pread(blk_new_backing, sector << BDRV_SECTOR_BITS, | |
3294 | buf_new, n << BDRV_SECTOR_BITS); | |
3295 | if (ret < 0) { | |
3296 | error_report("error while reading from new backing file"); | |
3297 | goto out; | |
3298 | } | |
3299 | } | |
3300 | ||
3301 | /* If they differ, we need to write to the COW file */ | |
3302 | uint64_t written = 0; | |
3303 | ||
3304 | while (written < n) { | |
3305 | int pnum; | |
3306 | ||
3307 | if (compare_sectors(buf_old + written * 512, | |
3308 | buf_new + written * 512, n - written, &pnum)) | |
3309 | { | |
3310 | ret = blk_pwrite(blk, | |
3311 | (sector + written) << BDRV_SECTOR_BITS, | |
3312 | buf_old + written * 512, | |
3313 | pnum << BDRV_SECTOR_BITS, 0); | |
3314 | if (ret < 0) { | |
3315 | error_report("Error while writing to COW image: %s", | |
3316 | strerror(-ret)); | |
3317 | goto out; | |
3318 | } | |
3319 | } | |
3320 | ||
3321 | written += pnum; | |
3322 | } | |
3323 | qemu_progress_print(local_progress, 100); | |
3324 | } | |
3325 | } | |
3326 | ||
3327 | /* | |
3328 | * Change the backing file. All clusters that are different from the old | |
3329 | * backing file are overwritten in the COW file now, so the visible content | |
3330 | * doesn't change when we switch the backing file. | |
3331 | */ | |
3332 | if (out_baseimg && *out_baseimg) { | |
3333 | ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt); | |
3334 | } else { | |
3335 | ret = bdrv_change_backing_file(bs, NULL, NULL); | |
3336 | } | |
3337 | ||
3338 | if (ret == -ENOSPC) { | |
3339 | error_report("Could not change the backing file to '%s': No " | |
3340 | "space left in the file header", out_baseimg); | |
3341 | } else if (ret < 0) { | |
3342 | error_report("Could not change the backing file to '%s': %s", | |
3343 | out_baseimg, strerror(-ret)); | |
3344 | } | |
3345 | ||
3346 | qemu_progress_print(100, 0); | |
3347 | /* | |
3348 | * TODO At this point it is possible to check if any clusters that are | |
3349 | * allocated in the COW file are the same in the backing file. If so, they | |
3350 | * could be dropped from the COW file. Don't do this before switching the | |
3351 | * backing file, in case of a crash this would lead to corruption. | |
3352 | */ | |
3353 | out: | |
3354 | qemu_progress_end(); | |
3355 | /* Cleanup */ | |
3356 | if (!unsafe) { | |
3357 | blk_unref(blk_old_backing); | |
3358 | blk_unref(blk_new_backing); | |
3359 | } | |
3360 | qemu_vfree(buf_old); | |
3361 | qemu_vfree(buf_new); | |
3362 | ||
3363 | blk_unref(blk); | |
3364 | if (ret) { | |
3365 | return 1; | |
3366 | } | |
3367 | return 0; | |
3368 | } | |
3369 | ||
3370 | static int img_resize(int argc, char **argv) | |
3371 | { | |
3372 | Error *err = NULL; | |
3373 | int c, ret, relative; | |
3374 | const char *filename, *fmt, *size; | |
3375 | int64_t n, total_size; | |
3376 | bool quiet = false; | |
3377 | BlockBackend *blk = NULL; | |
3378 | QemuOpts *param; | |
3379 | ||
3380 | static QemuOptsList resize_options = { | |
3381 | .name = "resize_options", | |
3382 | .head = QTAILQ_HEAD_INITIALIZER(resize_options.head), | |
3383 | .desc = { | |
3384 | { | |
3385 | .name = BLOCK_OPT_SIZE, | |
3386 | .type = QEMU_OPT_SIZE, | |
3387 | .help = "Virtual disk size" | |
3388 | }, { | |
3389 | /* end of list */ | |
3390 | } | |
3391 | }, | |
3392 | }; | |
3393 | bool image_opts = false; | |
3394 | ||
3395 | /* Remove size from argv manually so that negative numbers are not treated | |
3396 | * as options by getopt. */ | |
3397 | if (argc < 3) { | |
3398 | error_exit("Not enough arguments"); | |
3399 | return 1; | |
3400 | } | |
3401 | ||
3402 | size = argv[--argc]; | |
3403 | ||
3404 | /* Parse getopt arguments */ | |
3405 | fmt = NULL; | |
3406 | for(;;) { | |
3407 | static const struct option long_options[] = { | |
3408 | {"help", no_argument, 0, 'h'}, | |
3409 | {"object", required_argument, 0, OPTION_OBJECT}, | |
3410 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, | |
3411 | {0, 0, 0, 0} | |
3412 | }; | |
3413 | c = getopt_long(argc, argv, ":f:hq", | |
3414 | long_options, NULL); | |
3415 | if (c == -1) { | |
3416 | break; | |
3417 | } | |
3418 | switch(c) { | |
3419 | case ':': | |
3420 | missing_argument(argv[optind - 1]); | |
3421 | break; | |
3422 | case '?': | |
3423 | unrecognized_option(argv[optind - 1]); | |
3424 | break; | |
3425 | case 'h': | |
3426 | help(); | |
3427 | break; | |
3428 | case 'f': | |
3429 | fmt = optarg; | |
3430 | break; | |
3431 | case 'q': | |
3432 | quiet = true; | |
3433 | break; | |
3434 | case OPTION_OBJECT: { | |
3435 | QemuOpts *opts; | |
3436 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
3437 | optarg, true); | |
3438 | if (!opts) { | |
3439 | return 1; | |
3440 | } | |
3441 | } break; | |
3442 | case OPTION_IMAGE_OPTS: | |
3443 | image_opts = true; | |
3444 | break; | |
3445 | } | |
3446 | } | |
3447 | if (optind != argc - 1) { | |
3448 | error_exit("Expecting one image file name"); | |
3449 | } | |
3450 | filename = argv[optind++]; | |
3451 | ||
3452 | if (qemu_opts_foreach(&qemu_object_opts, | |
3453 | user_creatable_add_opts_foreach, | |
3454 | NULL, NULL)) { | |
3455 | return 1; | |
3456 | } | |
3457 | ||
3458 | /* Choose grow, shrink, or absolute resize mode */ | |
3459 | switch (size[0]) { | |
3460 | case '+': | |
3461 | relative = 1; | |
3462 | size++; | |
3463 | break; | |
3464 | case '-': | |
3465 | relative = -1; | |
3466 | size++; | |
3467 | break; | |
3468 | default: | |
3469 | relative = 0; | |
3470 | break; | |
3471 | } | |
3472 | ||
3473 | /* Parse size */ | |
3474 | param = qemu_opts_create(&resize_options, NULL, 0, &error_abort); | |
3475 | qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err); | |
3476 | if (err) { | |
3477 | error_report_err(err); | |
3478 | ret = -1; | |
3479 | qemu_opts_del(param); | |
3480 | goto out; | |
3481 | } | |
3482 | n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0); | |
3483 | qemu_opts_del(param); | |
3484 | ||
3485 | blk = img_open(image_opts, filename, fmt, | |
3486 | BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet); | |
3487 | if (!blk) { | |
3488 | ret = -1; | |
3489 | goto out; | |
3490 | } | |
3491 | ||
3492 | if (relative) { | |
3493 | total_size = blk_getlength(blk) + n * relative; | |
3494 | } else { | |
3495 | total_size = n; | |
3496 | } | |
3497 | if (total_size <= 0) { | |
3498 | error_report("New image size must be positive"); | |
3499 | ret = -1; | |
3500 | goto out; | |
3501 | } | |
3502 | ||
3503 | ret = blk_truncate(blk, total_size, &err); | |
3504 | if (!ret) { | |
3505 | qprintf(quiet, "Image resized.\n"); | |
3506 | } else { | |
3507 | error_report_err(err); | |
3508 | } | |
3509 | out: | |
3510 | blk_unref(blk); | |
3511 | if (ret) { | |
3512 | return 1; | |
3513 | } | |
3514 | return 0; | |
3515 | } | |
3516 | ||
3517 | static void amend_status_cb(BlockDriverState *bs, | |
3518 | int64_t offset, int64_t total_work_size, | |
3519 | void *opaque) | |
3520 | { | |
3521 | qemu_progress_print(100.f * offset / total_work_size, 0); | |
3522 | } | |
3523 | ||
3524 | static int img_amend(int argc, char **argv) | |
3525 | { | |
3526 | Error *err = NULL; | |
3527 | int c, ret = 0; | |
3528 | char *options = NULL; | |
3529 | QemuOptsList *create_opts = NULL; | |
3530 | QemuOpts *opts = NULL; | |
3531 | const char *fmt = NULL, *filename, *cache; | |
3532 | int flags; | |
3533 | bool writethrough; | |
3534 | bool quiet = false, progress = false; | |
3535 | BlockBackend *blk = NULL; | |
3536 | BlockDriverState *bs = NULL; | |
3537 | bool image_opts = false; | |
3538 | ||
3539 | cache = BDRV_DEFAULT_CACHE; | |
3540 | for (;;) { | |
3541 | static const struct option long_options[] = { | |
3542 | {"help", no_argument, 0, 'h'}, | |
3543 | {"object", required_argument, 0, OPTION_OBJECT}, | |
3544 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, | |
3545 | {0, 0, 0, 0} | |
3546 | }; | |
3547 | c = getopt_long(argc, argv, ":ho:f:t:pq", | |
3548 | long_options, NULL); | |
3549 | if (c == -1) { | |
3550 | break; | |
3551 | } | |
3552 | ||
3553 | switch (c) { | |
3554 | case ':': | |
3555 | missing_argument(argv[optind - 1]); | |
3556 | break; | |
3557 | case '?': | |
3558 | unrecognized_option(argv[optind - 1]); | |
3559 | break; | |
3560 | case 'h': | |
3561 | help(); | |
3562 | break; | |
3563 | case 'o': | |
3564 | if (!is_valid_option_list(optarg)) { | |
3565 | error_report("Invalid option list: %s", optarg); | |
3566 | ret = -1; | |
3567 | goto out_no_progress; | |
3568 | } | |
3569 | if (!options) { | |
3570 | options = g_strdup(optarg); | |
3571 | } else { | |
3572 | char *old_options = options; | |
3573 | options = g_strdup_printf("%s,%s", options, optarg); | |
3574 | g_free(old_options); | |
3575 | } | |
3576 | break; | |
3577 | case 'f': | |
3578 | fmt = optarg; | |
3579 | break; | |
3580 | case 't': | |
3581 | cache = optarg; | |
3582 | break; | |
3583 | case 'p': | |
3584 | progress = true; | |
3585 | break; | |
3586 | case 'q': | |
3587 | quiet = true; | |
3588 | break; | |
3589 | case OPTION_OBJECT: | |
3590 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
3591 | optarg, true); | |
3592 | if (!opts) { | |
3593 | ret = -1; | |
3594 | goto out_no_progress; | |
3595 | } | |
3596 | break; | |
3597 | case OPTION_IMAGE_OPTS: | |
3598 | image_opts = true; | |
3599 | break; | |
3600 | } | |
3601 | } | |
3602 | ||
3603 | if (!options) { | |
3604 | error_exit("Must specify options (-o)"); | |
3605 | } | |
3606 | ||
3607 | if (qemu_opts_foreach(&qemu_object_opts, | |
3608 | user_creatable_add_opts_foreach, | |
3609 | NULL, NULL)) { | |
3610 | ret = -1; | |
3611 | goto out_no_progress; | |
3612 | } | |
3613 | ||
3614 | if (quiet) { | |
3615 | progress = false; | |
3616 | } | |
3617 | qemu_progress_init(progress, 1.0); | |
3618 | ||
3619 | filename = (optind == argc - 1) ? argv[argc - 1] : NULL; | |
3620 | if (fmt && has_help_option(options)) { | |
3621 | /* If a format is explicitly specified (and possibly no filename is | |
3622 | * given), print option help here */ | |
3623 | ret = print_block_option_help(filename, fmt); | |
3624 | goto out; | |
3625 | } | |
3626 | ||
3627 | if (optind != argc - 1) { | |
3628 | error_report("Expecting one image file name"); | |
3629 | ret = -1; | |
3630 | goto out; | |
3631 | } | |
3632 | ||
3633 | flags = BDRV_O_RDWR; | |
3634 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); | |
3635 | if (ret < 0) { | |
3636 | error_report("Invalid cache option: %s", cache); | |
3637 | goto out; | |
3638 | } | |
3639 | ||
3640 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet); | |
3641 | if (!blk) { | |
3642 | ret = -1; | |
3643 | goto out; | |
3644 | } | |
3645 | bs = blk_bs(blk); | |
3646 | ||
3647 | fmt = bs->drv->format_name; | |
3648 | ||
3649 | if (has_help_option(options)) { | |
3650 | /* If the format was auto-detected, print option help here */ | |
3651 | ret = print_block_option_help(filename, fmt); | |
3652 | goto out; | |
3653 | } | |
3654 | ||
3655 | if (!bs->drv->create_opts) { | |
3656 | error_report("Format driver '%s' does not support any options to amend", | |
3657 | fmt); | |
3658 | ret = -1; | |
3659 | goto out; | |
3660 | } | |
3661 | ||
3662 | create_opts = qemu_opts_append(create_opts, bs->drv->create_opts); | |
3663 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); | |
3664 | qemu_opts_do_parse(opts, options, NULL, &err); | |
3665 | if (err) { | |
3666 | error_report_err(err); | |
3667 | ret = -1; | |
3668 | goto out; | |
3669 | } | |
3670 | ||
3671 | /* In case the driver does not call amend_status_cb() */ | |
3672 | qemu_progress_print(0.f, 0); | |
3673 | ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL); | |
3674 | qemu_progress_print(100.f, 0); | |
3675 | if (ret < 0) { | |
3676 | error_report("Error while amending options: %s", strerror(-ret)); | |
3677 | goto out; | |
3678 | } | |
3679 | ||
3680 | out: | |
3681 | qemu_progress_end(); | |
3682 | ||
3683 | out_no_progress: | |
3684 | blk_unref(blk); | |
3685 | qemu_opts_del(opts); | |
3686 | qemu_opts_free(create_opts); | |
3687 | g_free(options); | |
3688 | ||
3689 | if (ret) { | |
3690 | return 1; | |
3691 | } | |
3692 | return 0; | |
3693 | } | |
3694 | ||
3695 | typedef struct BenchData { | |
3696 | BlockBackend *blk; | |
3697 | uint64_t image_size; | |
3698 | bool write; | |
3699 | int bufsize; | |
3700 | int step; | |
3701 | int nrreq; | |
3702 | int n; | |
3703 | int flush_interval; | |
3704 | bool drain_on_flush; | |
3705 | uint8_t *buf; | |
3706 | QEMUIOVector *qiov; | |
3707 | ||
3708 | int in_flight; | |
3709 | bool in_flush; | |
3710 | uint64_t offset; | |
3711 | } BenchData; | |
3712 | ||
3713 | static void bench_undrained_flush_cb(void *opaque, int ret) | |
3714 | { | |
3715 | if (ret < 0) { | |
3716 | error_report("Failed flush request: %s", strerror(-ret)); | |
3717 | exit(EXIT_FAILURE); | |
3718 | } | |
3719 | } | |
3720 | ||
3721 | static void bench_cb(void *opaque, int ret) | |
3722 | { | |
3723 | BenchData *b = opaque; | |
3724 | BlockAIOCB *acb; | |
3725 | ||
3726 | if (ret < 0) { | |
3727 | error_report("Failed request: %s", strerror(-ret)); | |
3728 | exit(EXIT_FAILURE); | |
3729 | } | |
3730 | ||
3731 | if (b->in_flush) { | |
3732 | /* Just finished a flush with drained queue: Start next requests */ | |
3733 | assert(b->in_flight == 0); | |
3734 | b->in_flush = false; | |
3735 | } else if (b->in_flight > 0) { | |
3736 | int remaining = b->n - b->in_flight; | |
3737 | ||
3738 | b->n--; | |
3739 | b->in_flight--; | |
3740 | ||
3741 | /* Time for flush? Drain queue if requested, then flush */ | |
3742 | if (b->flush_interval && remaining % b->flush_interval == 0) { | |
3743 | if (!b->in_flight || !b->drain_on_flush) { | |
3744 | BlockCompletionFunc *cb; | |
3745 | ||
3746 | if (b->drain_on_flush) { | |
3747 | b->in_flush = true; | |
3748 | cb = bench_cb; | |
3749 | } else { | |
3750 | cb = bench_undrained_flush_cb; | |
3751 | } | |
3752 | ||
3753 | acb = blk_aio_flush(b->blk, cb, b); | |
3754 | if (!acb) { | |
3755 | error_report("Failed to issue flush request"); | |
3756 | exit(EXIT_FAILURE); | |
3757 | } | |
3758 | } | |
3759 | if (b->drain_on_flush) { | |
3760 | return; | |
3761 | } | |
3762 | } | |
3763 | } | |
3764 | ||
3765 | while (b->n > b->in_flight && b->in_flight < b->nrreq) { | |
3766 | int64_t offset = b->offset; | |
3767 | /* blk_aio_* might look for completed I/Os and kick bench_cb | |
3768 | * again, so make sure this operation is counted by in_flight | |
3769 | * and b->offset is ready for the next submission. | |
3770 | */ | |
3771 | b->in_flight++; | |
3772 | b->offset += b->step; | |
3773 | b->offset %= b->image_size; | |
3774 | if (b->write) { | |
3775 | acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b); | |
3776 | } else { | |
3777 | acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b); | |
3778 | } | |
3779 | if (!acb) { | |
3780 | error_report("Failed to issue request"); | |
3781 | exit(EXIT_FAILURE); | |
3782 | } | |
3783 | } | |
3784 | } | |
3785 | ||
3786 | static int img_bench(int argc, char **argv) | |
3787 | { | |
3788 | int c, ret = 0; | |
3789 | const char *fmt = NULL, *filename; | |
3790 | bool quiet = false; | |
3791 | bool image_opts = false; | |
3792 | bool is_write = false; | |
3793 | int count = 75000; | |
3794 | int depth = 64; | |
3795 | int64_t offset = 0; | |
3796 | size_t bufsize = 4096; | |
3797 | int pattern = 0; | |
3798 | size_t step = 0; | |
3799 | int flush_interval = 0; | |
3800 | bool drain_on_flush = true; | |
3801 | int64_t image_size; | |
3802 | BlockBackend *blk = NULL; | |
3803 | BenchData data = {}; | |
3804 | int flags = 0; | |
3805 | bool writethrough = false; | |
3806 | struct timeval t1, t2; | |
3807 | int i; | |
3808 | ||
3809 | for (;;) { | |
3810 | static const struct option long_options[] = { | |
3811 | {"help", no_argument, 0, 'h'}, | |
3812 | {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL}, | |
3813 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, | |
3814 | {"pattern", required_argument, 0, OPTION_PATTERN}, | |
3815 | {"no-drain", no_argument, 0, OPTION_NO_DRAIN}, | |
3816 | {0, 0, 0, 0} | |
3817 | }; | |
3818 | c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:w", long_options, NULL); | |
3819 | if (c == -1) { | |
3820 | break; | |
3821 | } | |
3822 | ||
3823 | switch (c) { | |
3824 | case ':': | |
3825 | missing_argument(argv[optind - 1]); | |
3826 | break; | |
3827 | case '?': | |
3828 | unrecognized_option(argv[optind - 1]); | |
3829 | break; | |
3830 | case 'h': | |
3831 | help(); | |
3832 | break; | |
3833 | case 'c': | |
3834 | { | |
3835 | unsigned long res; | |
3836 | ||
3837 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { | |
3838 | error_report("Invalid request count specified"); | |
3839 | return 1; | |
3840 | } | |
3841 | count = res; | |
3842 | break; | |
3843 | } | |
3844 | case 'd': | |
3845 | { | |
3846 | unsigned long res; | |
3847 | ||
3848 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { | |
3849 | error_report("Invalid queue depth specified"); | |
3850 | return 1; | |
3851 | } | |
3852 | depth = res; | |
3853 | break; | |
3854 | } | |
3855 | case 'f': | |
3856 | fmt = optarg; | |
3857 | break; | |
3858 | case 'n': | |
3859 | flags |= BDRV_O_NATIVE_AIO; | |
3860 | break; | |
3861 | case 'o': | |
3862 | { | |
3863 | offset = cvtnum(optarg); | |
3864 | if (offset < 0) { | |
3865 | error_report("Invalid offset specified"); | |
3866 | return 1; | |
3867 | } | |
3868 | break; | |
3869 | } | |
3870 | break; | |
3871 | case 'q': | |
3872 | quiet = true; | |
3873 | break; | |
3874 | case 's': | |
3875 | { | |
3876 | int64_t sval; | |
3877 | ||
3878 | sval = cvtnum(optarg); | |
3879 | if (sval < 0 || sval > INT_MAX) { | |
3880 | error_report("Invalid buffer size specified"); | |
3881 | return 1; | |
3882 | } | |
3883 | ||
3884 | bufsize = sval; | |
3885 | break; | |
3886 | } | |
3887 | case 'S': | |
3888 | { | |
3889 | int64_t sval; | |
3890 | ||
3891 | sval = cvtnum(optarg); | |
3892 | if (sval < 0 || sval > INT_MAX) { | |
3893 | error_report("Invalid step size specified"); | |
3894 | return 1; | |
3895 | } | |
3896 | ||
3897 | step = sval; | |
3898 | break; | |
3899 | } | |
3900 | case 't': | |
3901 | ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough); | |
3902 | if (ret < 0) { | |
3903 | error_report("Invalid cache mode"); | |
3904 | ret = -1; | |
3905 | goto out; | |
3906 | } | |
3907 | break; | |
3908 | case 'w': | |
3909 | flags |= BDRV_O_RDWR; | |
3910 | is_write = true; | |
3911 | break; | |
3912 | case OPTION_PATTERN: | |
3913 | { | |
3914 | unsigned long res; | |
3915 | ||
3916 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) { | |
3917 | error_report("Invalid pattern byte specified"); | |
3918 | return 1; | |
3919 | } | |
3920 | pattern = res; | |
3921 | break; | |
3922 | } | |
3923 | case OPTION_FLUSH_INTERVAL: | |
3924 | { | |
3925 | unsigned long res; | |
3926 | ||
3927 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { | |
3928 | error_report("Invalid flush interval specified"); | |
3929 | return 1; | |
3930 | } | |
3931 | flush_interval = res; | |
3932 | break; | |
3933 | } | |
3934 | case OPTION_NO_DRAIN: | |
3935 | drain_on_flush = false; | |
3936 | break; | |
3937 | case OPTION_IMAGE_OPTS: | |
3938 | image_opts = true; | |
3939 | break; | |
3940 | } | |
3941 | } | |
3942 | ||
3943 | if (optind != argc - 1) { | |
3944 | error_exit("Expecting one image file name"); | |
3945 | } | |
3946 | filename = argv[argc - 1]; | |
3947 | ||
3948 | if (!is_write && flush_interval) { | |
3949 | error_report("--flush-interval is only available in write tests"); | |
3950 | ret = -1; | |
3951 | goto out; | |
3952 | } | |
3953 | if (flush_interval && flush_interval < depth) { | |
3954 | error_report("Flush interval can't be smaller than depth"); | |
3955 | ret = -1; | |
3956 | goto out; | |
3957 | } | |
3958 | ||
3959 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet); | |
3960 | if (!blk) { | |
3961 | ret = -1; | |
3962 | goto out; | |
3963 | } | |
3964 | ||
3965 | image_size = blk_getlength(blk); | |
3966 | if (image_size < 0) { | |
3967 | ret = image_size; | |
3968 | goto out; | |
3969 | } | |
3970 | ||
3971 | data = (BenchData) { | |
3972 | .blk = blk, | |
3973 | .image_size = image_size, | |
3974 | .bufsize = bufsize, | |
3975 | .step = step ?: bufsize, | |
3976 | .nrreq = depth, | |
3977 | .n = count, | |
3978 | .offset = offset, | |
3979 | .write = is_write, | |
3980 | .flush_interval = flush_interval, | |
3981 | .drain_on_flush = drain_on_flush, | |
3982 | }; | |
3983 | printf("Sending %d %s requests, %d bytes each, %d in parallel " | |
3984 | "(starting at offset %" PRId64 ", step size %d)\n", | |
3985 | data.n, data.write ? "write" : "read", data.bufsize, data.nrreq, | |
3986 | data.offset, data.step); | |
3987 | if (flush_interval) { | |
3988 | printf("Sending flush every %d requests\n", flush_interval); | |
3989 | } | |
3990 | ||
3991 | data.buf = blk_blockalign(blk, data.nrreq * data.bufsize); | |
3992 | memset(data.buf, pattern, data.nrreq * data.bufsize); | |
3993 | ||
3994 | data.qiov = g_new(QEMUIOVector, data.nrreq); | |
3995 | for (i = 0; i < data.nrreq; i++) { | |
3996 | qemu_iovec_init(&data.qiov[i], 1); | |
3997 | qemu_iovec_add(&data.qiov[i], | |
3998 | data.buf + i * data.bufsize, data.bufsize); | |
3999 | } | |
4000 | ||
4001 | gettimeofday(&t1, NULL); | |
4002 | bench_cb(&data, 0); | |
4003 | ||
4004 | while (data.n > 0) { | |
4005 | main_loop_wait(false); | |
4006 | } | |
4007 | gettimeofday(&t2, NULL); | |
4008 | ||
4009 | printf("Run completed in %3.3f seconds.\n", | |
4010 | (t2.tv_sec - t1.tv_sec) | |
4011 | + ((double)(t2.tv_usec - t1.tv_usec) / 1000000)); | |
4012 | ||
4013 | out: | |
4014 | qemu_vfree(data.buf); | |
4015 | blk_unref(blk); | |
4016 | ||
4017 | if (ret) { | |
4018 | return 1; | |
4019 | } | |
4020 | return 0; | |
4021 | } | |
4022 | ||
4023 | #define C_BS 01 | |
4024 | #define C_COUNT 02 | |
4025 | #define C_IF 04 | |
4026 | #define C_OF 010 | |
4027 | #define C_SKIP 020 | |
4028 | ||
4029 | struct DdInfo { | |
4030 | unsigned int flags; | |
4031 | int64_t count; | |
4032 | }; | |
4033 | ||
4034 | struct DdIo { | |
4035 | int bsz; /* Block size */ | |
4036 | char *filename; | |
4037 | uint8_t *buf; | |
4038 | int64_t offset; | |
4039 | }; | |
4040 | ||
4041 | struct DdOpts { | |
4042 | const char *name; | |
4043 | int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *); | |
4044 | unsigned int flag; | |
4045 | }; | |
4046 | ||
4047 | static int img_dd_bs(const char *arg, | |
4048 | struct DdIo *in, struct DdIo *out, | |
4049 | struct DdInfo *dd) | |
4050 | { | |
4051 | int64_t res; | |
4052 | ||
4053 | res = cvtnum(arg); | |
4054 | ||
4055 | if (res <= 0 || res > INT_MAX) { | |
4056 | error_report("invalid number: '%s'", arg); | |
4057 | return 1; | |
4058 | } | |
4059 | in->bsz = out->bsz = res; | |
4060 | ||
4061 | return 0; | |
4062 | } | |
4063 | ||
4064 | static int img_dd_count(const char *arg, | |
4065 | struct DdIo *in, struct DdIo *out, | |
4066 | struct DdInfo *dd) | |
4067 | { | |
4068 | dd->count = cvtnum(arg); | |
4069 | ||
4070 | if (dd->count < 0) { | |
4071 | error_report("invalid number: '%s'", arg); | |
4072 | return 1; | |
4073 | } | |
4074 | ||
4075 | return 0; | |
4076 | } | |
4077 | ||
4078 | static int img_dd_if(const char *arg, | |
4079 | struct DdIo *in, struct DdIo *out, | |
4080 | struct DdInfo *dd) | |
4081 | { | |
4082 | in->filename = g_strdup(arg); | |
4083 | ||
4084 | return 0; | |
4085 | } | |
4086 | ||
4087 | static int img_dd_of(const char *arg, | |
4088 | struct DdIo *in, struct DdIo *out, | |
4089 | struct DdInfo *dd) | |
4090 | { | |
4091 | out->filename = g_strdup(arg); | |
4092 | ||
4093 | return 0; | |
4094 | } | |
4095 | ||
4096 | static int img_dd_skip(const char *arg, | |
4097 | struct DdIo *in, struct DdIo *out, | |
4098 | struct DdInfo *dd) | |
4099 | { | |
4100 | in->offset = cvtnum(arg); | |
4101 | ||
4102 | if (in->offset < 0) { | |
4103 | error_report("invalid number: '%s'", arg); | |
4104 | return 1; | |
4105 | } | |
4106 | ||
4107 | return 0; | |
4108 | } | |
4109 | ||
4110 | static int img_dd(int argc, char **argv) | |
4111 | { | |
4112 | int ret = 0; | |
4113 | char *arg = NULL; | |
4114 | char *tmp; | |
4115 | BlockDriver *drv = NULL, *proto_drv = NULL; | |
4116 | BlockBackend *blk1 = NULL, *blk2 = NULL; | |
4117 | QemuOpts *opts = NULL; | |
4118 | QemuOptsList *create_opts = NULL; | |
4119 | Error *local_err = NULL; | |
4120 | bool image_opts = false; | |
4121 | int c, i; | |
4122 | const char *out_fmt = "raw"; | |
4123 | const char *fmt = NULL; | |
4124 | int64_t size = 0; | |
4125 | int64_t block_count = 0, out_pos, in_pos; | |
4126 | struct DdInfo dd = { | |
4127 | .flags = 0, | |
4128 | .count = 0, | |
4129 | }; | |
4130 | struct DdIo in = { | |
4131 | .bsz = 512, /* Block size is by default 512 bytes */ | |
4132 | .filename = NULL, | |
4133 | .buf = NULL, | |
4134 | .offset = 0 | |
4135 | }; | |
4136 | struct DdIo out = { | |
4137 | .bsz = 512, | |
4138 | .filename = NULL, | |
4139 | .buf = NULL, | |
4140 | .offset = 0 | |
4141 | }; | |
4142 | ||
4143 | const struct DdOpts options[] = { | |
4144 | { "bs", img_dd_bs, C_BS }, | |
4145 | { "count", img_dd_count, C_COUNT }, | |
4146 | { "if", img_dd_if, C_IF }, | |
4147 | { "of", img_dd_of, C_OF }, | |
4148 | { "skip", img_dd_skip, C_SKIP }, | |
4149 | { NULL, NULL, 0 } | |
4150 | }; | |
4151 | const struct option long_options[] = { | |
4152 | { "help", no_argument, 0, 'h'}, | |
4153 | { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, | |
4154 | { 0, 0, 0, 0 } | |
4155 | }; | |
4156 | ||
4157 | while ((c = getopt_long(argc, argv, ":hf:O:", long_options, NULL))) { | |
4158 | if (c == EOF) { | |
4159 | break; | |
4160 | } | |
4161 | switch (c) { | |
4162 | case 'O': | |
4163 | out_fmt = optarg; | |
4164 | break; | |
4165 | case 'f': | |
4166 | fmt = optarg; | |
4167 | break; | |
4168 | case ':': | |
4169 | missing_argument(argv[optind - 1]); | |
4170 | break; | |
4171 | case '?': | |
4172 | unrecognized_option(argv[optind - 1]); | |
4173 | break; | |
4174 | case 'h': | |
4175 | help(); | |
4176 | break; | |
4177 | case OPTION_IMAGE_OPTS: | |
4178 | image_opts = true; | |
4179 | break; | |
4180 | } | |
4181 | } | |
4182 | ||
4183 | for (i = optind; i < argc; i++) { | |
4184 | int j; | |
4185 | arg = g_strdup(argv[i]); | |
4186 | ||
4187 | tmp = strchr(arg, '='); | |
4188 | if (tmp == NULL) { | |
4189 | error_report("unrecognized operand %s", arg); | |
4190 | ret = -1; | |
4191 | goto out; | |
4192 | } | |
4193 | ||
4194 | *tmp++ = '\0'; | |
4195 | ||
4196 | for (j = 0; options[j].name != NULL; j++) { | |
4197 | if (!strcmp(arg, options[j].name)) { | |
4198 | break; | |
4199 | } | |
4200 | } | |
4201 | if (options[j].name == NULL) { | |
4202 | error_report("unrecognized operand %s", arg); | |
4203 | ret = -1; | |
4204 | goto out; | |
4205 | } | |
4206 | ||
4207 | if (options[j].f(tmp, &in, &out, &dd) != 0) { | |
4208 | ret = -1; | |
4209 | goto out; | |
4210 | } | |
4211 | dd.flags |= options[j].flag; | |
4212 | g_free(arg); | |
4213 | arg = NULL; | |
4214 | } | |
4215 | ||
4216 | if (!(dd.flags & C_IF && dd.flags & C_OF)) { | |
4217 | error_report("Must specify both input and output files"); | |
4218 | ret = -1; | |
4219 | goto out; | |
4220 | } | |
4221 | blk1 = img_open(image_opts, in.filename, fmt, 0, false, false); | |
4222 | ||
4223 | if (!blk1) { | |
4224 | ret = -1; | |
4225 | goto out; | |
4226 | } | |
4227 | ||
4228 | drv = bdrv_find_format(out_fmt); | |
4229 | if (!drv) { | |
4230 | error_report("Unknown file format"); | |
4231 | ret = -1; | |
4232 | goto out; | |
4233 | } | |
4234 | proto_drv = bdrv_find_protocol(out.filename, true, &local_err); | |
4235 | ||
4236 | if (!proto_drv) { | |
4237 | error_report_err(local_err); | |
4238 | ret = -1; | |
4239 | goto out; | |
4240 | } | |
4241 | if (!drv->create_opts) { | |
4242 | error_report("Format driver '%s' does not support image creation", | |
4243 | drv->format_name); | |
4244 | ret = -1; | |
4245 | goto out; | |
4246 | } | |
4247 | if (!proto_drv->create_opts) { | |
4248 | error_report("Protocol driver '%s' does not support image creation", | |
4249 | proto_drv->format_name); | |
4250 | ret = -1; | |
4251 | goto out; | |
4252 | } | |
4253 | create_opts = qemu_opts_append(create_opts, drv->create_opts); | |
4254 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); | |
4255 | ||
4256 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); | |
4257 | ||
4258 | size = blk_getlength(blk1); | |
4259 | if (size < 0) { | |
4260 | error_report("Failed to get size for '%s'", in.filename); | |
4261 | ret = -1; | |
4262 | goto out; | |
4263 | } | |
4264 | ||
4265 | if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz && | |
4266 | dd.count * in.bsz < size) { | |
4267 | size = dd.count * in.bsz; | |
4268 | } | |
4269 | ||
4270 | /* Overflow means the specified offset is beyond input image's size */ | |
4271 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || | |
4272 | size < in.bsz * in.offset)) { | |
4273 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort); | |
4274 | } else { | |
4275 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, | |
4276 | size - in.bsz * in.offset, &error_abort); | |
4277 | } | |
4278 | ||
4279 | ret = bdrv_create(drv, out.filename, opts, &local_err); | |
4280 | if (ret < 0) { | |
4281 | error_reportf_err(local_err, | |
4282 | "%s: error while creating output image: ", | |
4283 | out.filename); | |
4284 | ret = -1; | |
4285 | goto out; | |
4286 | } | |
4287 | ||
4288 | blk2 = img_open(image_opts, out.filename, out_fmt, BDRV_O_RDWR, | |
4289 | false, false); | |
4290 | ||
4291 | if (!blk2) { | |
4292 | ret = -1; | |
4293 | goto out; | |
4294 | } | |
4295 | ||
4296 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || | |
4297 | size < in.offset * in.bsz)) { | |
4298 | /* We give a warning if the skip option is bigger than the input | |
4299 | * size and create an empty output disk image (i.e. like dd(1)). | |
4300 | */ | |
4301 | error_report("%s: cannot skip to specified offset", in.filename); | |
4302 | in_pos = size; | |
4303 | } else { | |
4304 | in_pos = in.offset * in.bsz; | |
4305 | } | |
4306 | ||
4307 | in.buf = g_new(uint8_t, in.bsz); | |
4308 | ||
4309 | for (out_pos = 0; in_pos < size; block_count++) { | |
4310 | int in_ret, out_ret; | |
4311 | ||
4312 | if (in_pos + in.bsz > size) { | |
4313 | in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos); | |
4314 | } else { | |
4315 | in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz); | |
4316 | } | |
4317 | if (in_ret < 0) { | |
4318 | error_report("error while reading from input image file: %s", | |
4319 | strerror(-in_ret)); | |
4320 | ret = -1; | |
4321 | goto out; | |
4322 | } | |
4323 | in_pos += in_ret; | |
4324 | ||
4325 | out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0); | |
4326 | ||
4327 | if (out_ret < 0) { | |
4328 | error_report("error while writing to output image file: %s", | |
4329 | strerror(-out_ret)); | |
4330 | ret = -1; | |
4331 | goto out; | |
4332 | } | |
4333 | out_pos += out_ret; | |
4334 | } | |
4335 | ||
4336 | out: | |
4337 | g_free(arg); | |
4338 | qemu_opts_del(opts); | |
4339 | qemu_opts_free(create_opts); | |
4340 | blk_unref(blk1); | |
4341 | blk_unref(blk2); | |
4342 | g_free(in.filename); | |
4343 | g_free(out.filename); | |
4344 | g_free(in.buf); | |
4345 | g_free(out.buf); | |
4346 | ||
4347 | if (ret) { | |
4348 | return 1; | |
4349 | } | |
4350 | return 0; | |
4351 | } | |
4352 | ||
4353 | ||
4354 | static const img_cmd_t img_cmds[] = { | |
4355 | #define DEF(option, callback, arg_string) \ | |
4356 | { option, callback }, | |
4357 | #include "qemu-img-cmds.h" | |
4358 | #undef DEF | |
4359 | #undef GEN_DOCS | |
4360 | { NULL, NULL, }, | |
4361 | }; | |
4362 | ||
4363 | int main(int argc, char **argv) | |
4364 | { | |
4365 | const img_cmd_t *cmd; | |
4366 | const char *cmdname; | |
4367 | Error *local_error = NULL; | |
4368 | char *trace_file = NULL; | |
4369 | int c; | |
4370 | static const struct option long_options[] = { | |
4371 | {"help", no_argument, 0, 'h'}, | |
4372 | {"version", no_argument, 0, 'V'}, | |
4373 | {"trace", required_argument, NULL, 'T'}, | |
4374 | {0, 0, 0, 0} | |
4375 | }; | |
4376 | ||
4377 | #ifdef CONFIG_POSIX | |
4378 | signal(SIGPIPE, SIG_IGN); | |
4379 | #endif | |
4380 | ||
4381 | module_call_init(MODULE_INIT_TRACE); | |
4382 | error_set_progname(argv[0]); | |
4383 | qemu_init_exec_dir(argv[0]); | |
4384 | ||
4385 | if (qemu_init_main_loop(&local_error)) { | |
4386 | error_report_err(local_error); | |
4387 | exit(EXIT_FAILURE); | |
4388 | } | |
4389 | ||
4390 | qcrypto_init(&error_fatal); | |
4391 | ||
4392 | module_call_init(MODULE_INIT_QOM); | |
4393 | bdrv_init(); | |
4394 | if (argc < 2) { | |
4395 | error_exit("Not enough arguments"); | |
4396 | } | |
4397 | ||
4398 | qemu_add_opts(&qemu_object_opts); | |
4399 | qemu_add_opts(&qemu_source_opts); | |
4400 | qemu_add_opts(&qemu_trace_opts); | |
4401 | ||
4402 | while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) { | |
4403 | switch (c) { | |
4404 | case ':': | |
4405 | missing_argument(argv[optind - 1]); | |
4406 | return 0; | |
4407 | case '?': | |
4408 | unrecognized_option(argv[optind - 1]); | |
4409 | return 0; | |
4410 | case 'h': | |
4411 | help(); | |
4412 | return 0; | |
4413 | case 'V': | |
4414 | printf(QEMU_IMG_VERSION); | |
4415 | return 0; | |
4416 | case 'T': | |
4417 | g_free(trace_file); | |
4418 | trace_file = trace_opt_parse(optarg); | |
4419 | break; | |
4420 | } | |
4421 | } | |
4422 | ||
4423 | cmdname = argv[optind]; | |
4424 | ||
4425 | /* reset getopt_long scanning */ | |
4426 | argc -= optind; | |
4427 | if (argc < 1) { | |
4428 | return 0; | |
4429 | } | |
4430 | argv += optind; | |
4431 | optind = 0; | |
4432 | ||
4433 | if (!trace_init_backends()) { | |
4434 | exit(1); | |
4435 | } | |
4436 | trace_init_file(trace_file); | |
4437 | qemu_set_log(LOG_TRACE); | |
4438 | ||
4439 | /* find the command */ | |
4440 | for (cmd = img_cmds; cmd->name != NULL; cmd++) { | |
4441 | if (!strcmp(cmdname, cmd->name)) { | |
4442 | return cmd->handler(argc, argv); | |
4443 | } | |
4444 | } | |
4445 | ||
4446 | /* not found */ | |
4447 | error_exit("Command not found: %s", cmdname); | |
4448 | } |