]>
Commit | Line | Data |
---|---|---|
ea2384d3 | 1 | /* |
fb43f4dd | 2 | * QEMU disk image utility |
5fafdf24 | 3 | * |
68d0f70e | 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
5fafdf24 | 5 | * |
ea2384d3 FB |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
452fcdbc | 24 | |
80c71a24 | 25 | #include "qemu/osdep.h" |
c2a3d7da EB |
26 | #include <getopt.h> |
27 | ||
a8d25326 | 28 | #include "qemu-common.h" |
67a1de0d | 29 | #include "qemu-version.h" |
da34e65c | 30 | #include "qapi/error.h" |
9af23989 | 31 | #include "qapi/qapi-visit-block-core.h" |
b3db211f | 32 | #include "qapi/qobject-output-visitor.h" |
7b1b5d19 | 33 | #include "qapi/qmp/qjson.h" |
452fcdbc | 34 | #include "qapi/qmp/qdict.h" |
fc81fa1e | 35 | #include "qapi/qmp/qstring.h" |
f348b6d1 | 36 | #include "qemu/cutils.h" |
3babeb15 | 37 | #include "qemu/config-file.h" |
1de7afc9 PB |
38 | #include "qemu/option.h" |
39 | #include "qemu/error-report.h" | |
06a1e0c1 | 40 | #include "qemu/log.h" |
db725815 | 41 | #include "qemu/main-loop.h" |
0b8fa32f | 42 | #include "qemu/module.h" |
97ede57a | 43 | #include "qemu/units.h" |
3babeb15 | 44 | #include "qom/object_interfaces.h" |
26f54e9a | 45 | #include "sysemu/block-backend.h" |
737e150e | 46 | #include "block/block_int.h" |
d4a3238a | 47 | #include "block/blockjob.h" |
f364ec65 | 48 | #include "block/qapi.h" |
c2297088 | 49 | #include "crypto/init.h" |
06a1e0c1 | 50 | #include "trace/control.h" |
e8445331 | 51 | |
7e563bfb | 52 | #define QEMU_IMG_VERSION "qemu-img version " QEMU_FULL_VERSION \ |
0781dd6e | 53 | "\n" QEMU_COPYRIGHT "\n" |
5f6979cb | 54 | |
c227f099 | 55 | typedef struct img_cmd_t { |
153859be SB |
56 | const char *name; |
57 | int (*handler)(int argc, char **argv); | |
c227f099 | 58 | } img_cmd_t; |
153859be | 59 | |
8599ea4c FS |
60 | enum { |
61 | OPTION_OUTPUT = 256, | |
62 | OPTION_BACKING_CHAIN = 257, | |
3babeb15 | 63 | OPTION_OBJECT = 258, |
eb769f74 | 64 | OPTION_IMAGE_OPTS = 259, |
b6495fa8 | 65 | OPTION_PATTERN = 260, |
55d539c8 KW |
66 | OPTION_FLUSH_INTERVAL = 261, |
67 | OPTION_NO_DRAIN = 262, | |
305b4c60 | 68 | OPTION_TARGET_IMAGE_OPTS = 263, |
fd03c2b8 | 69 | OPTION_SIZE = 264, |
dc5f690b | 70 | OPTION_PREALLOCATION = 265, |
4ffca890 | 71 | OPTION_SHRINK = 266, |
8eaac025 | 72 | OPTION_SALVAGE = 267, |
168468fe | 73 | OPTION_TARGET_IS_ZERO = 268, |
8599ea4c FS |
74 | }; |
75 | ||
76 | typedef enum OutputFormat { | |
77 | OFORMAT_JSON, | |
78 | OFORMAT_HUMAN, | |
79 | } OutputFormat; | |
80 | ||
e6996143 | 81 | /* Default to cache=writeback as data integrity is not important for qemu-img */ |
661a0f71 | 82 | #define BDRV_DEFAULT_CACHE "writeback" |
137519ce | 83 | |
00c6d403 | 84 | static void format_print(void *opaque, const char *name) |
ea2384d3 | 85 | { |
00c6d403 | 86 | printf(" %s", name); |
ea2384d3 FB |
87 | } |
88 | ||
ac1307ab FZ |
89 | static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...) |
90 | { | |
91 | va_list ap; | |
92 | ||
ac1307ab | 93 | va_start(ap, fmt); |
e9e1d92d | 94 | error_vreport(fmt, ap); |
ac1307ab FZ |
95 | va_end(ap); |
96 | ||
e9e1d92d | 97 | error_printf("Try 'qemu-img --help' for more information\n"); |
ac1307ab FZ |
98 | exit(EXIT_FAILURE); |
99 | } | |
100 | ||
c9192973 SH |
101 | static void QEMU_NORETURN missing_argument(const char *option) |
102 | { | |
103 | error_exit("missing argument for option '%s'", option); | |
104 | } | |
105 | ||
106 | static void QEMU_NORETURN unrecognized_option(const char *option) | |
107 | { | |
108 | error_exit("unrecognized option '%s'", option); | |
109 | } | |
110 | ||
d2c639d6 | 111 | /* Please keep in synch with qemu-img.texi */ |
ac1307ab | 112 | static void QEMU_NORETURN help(void) |
ea2384d3 | 113 | { |
e00291c0 | 114 | const char *help_msg = |
5f6979cb | 115 | QEMU_IMG_VERSION |
10985131 | 116 | "usage: qemu-img [standard options] command [command options]\n" |
3f020d70 | 117 | "QEMU disk image utility\n" |
118 | "\n" | |
10985131 DL |
119 | " '-h', '--help' display this help and exit\n" |
120 | " '-V', '--version' output version information and exit\n" | |
06a1e0c1 DL |
121 | " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n" |
122 | " specify tracing options\n" | |
10985131 | 123 | "\n" |
3f020d70 | 124 | "Command syntax:\n" |
153859be SB |
125 | #define DEF(option, callback, arg_string) \ |
126 | " " arg_string "\n" | |
127 | #include "qemu-img-cmds.h" | |
128 | #undef DEF | |
3f020d70 | 129 | "\n" |
130 | "Command parameters:\n" | |
131 | " 'filename' is a disk image filename\n" | |
3babeb15 DB |
132 | " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n" |
133 | " manual page for a description of the object properties. The most common\n" | |
134 | " object type is a 'secret', which is used to supply passwords and/or\n" | |
135 | " encryption keys.\n" | |
3f020d70 | 136 | " 'fmt' is the disk image format. It is guessed automatically in most cases\n" |
661a0f71 | 137 | " 'cache' is the cache mode used to write the output disk image, the valid\n" |
80ccf93b LY |
138 | " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n" |
139 | " 'directsync' and 'unsafe' (default for convert)\n" | |
bb87fdf8 SH |
140 | " 'src_cache' is the cache mode used to read input disk images, the valid\n" |
141 | " options are the same as for the 'cache' option\n" | |
3f020d70 | 142 | " 'size' is the disk image size in bytes. Optional suffixes\n" |
5e00984a KW |
143 | " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n" |
144 | " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n" | |
145 | " supported. 'b' is ignored.\n" | |
3f020d70 | 146 | " 'output_filename' is the destination disk image filename\n" |
147 | " 'output_fmt' is the destination format\n" | |
148 | " 'options' is a comma separated list of format specific options in a\n" | |
149 | " name=value format. Use -o ? for an overview of the options supported by the\n" | |
150 | " used format\n" | |
ef80654d WX |
151 | " 'snapshot_param' is param used for internal snapshot, format\n" |
152 | " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n" | |
153 | " '[ID_OR_NAME]'\n" | |
3f020d70 | 154 | " '-c' indicates that target image must be compressed (qcow format only)\n" |
6e6e55f5 JS |
155 | " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n" |
156 | " new backing file match exactly. The image doesn't need a working\n" | |
157 | " backing file before rebasing in this case (useful for renaming the\n" | |
158 | " backing file). For image creation, allow creating without attempting\n" | |
159 | " to open the backing file.\n" | |
3f020d70 | 160 | " '-h' with or without a command shows this help and lists the supported formats\n" |
6b837bc4 | 161 | " '-p' show progress of command (only certain commands)\n" |
f382d43a | 162 | " '-q' use Quiet mode - do not print any output (except errors)\n" |
11b6699a PL |
163 | " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n" |
164 | " contain only zeros for qemu-img to create a sparse image during\n" | |
165 | " conversion. If the number of bytes is 0, the source will not be scanned for\n" | |
166 | " unallocated or zero sectors, and the destination image will always be\n" | |
167 | " fully allocated\n" | |
c054b3fd | 168 | " '--output' takes the format in which the output must be done (human or json)\n" |
b2e10493 AD |
169 | " '-n' skips the target volume creation (useful if the volume is created\n" |
170 | " prior to running qemu-img)\n" | |
3f020d70 | 171 | "\n" |
4534ff54 KW |
172 | "Parameters to check subcommand:\n" |
173 | " '-r' tries to repair any inconsistencies that are found during the check.\n" | |
174 | " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n" | |
175 | " kinds of errors, with a higher risk of choosing the wrong fix or\n" | |
0546b8c2 | 176 | " hiding corruption that has already occurred.\n" |
4534ff54 | 177 | "\n" |
2d9187bc PL |
178 | "Parameters to convert subcommand:\n" |
179 | " '-m' specifies how many coroutines work in parallel during the convert\n" | |
180 | " process (defaults to 8)\n" | |
181 | " '-W' allow to write to the target out of order rather than sequential\n" | |
182 | "\n" | |
3f020d70 | 183 | "Parameters to snapshot subcommand:\n" |
184 | " 'snapshot' is the name of the snapshot to create, apply or delete\n" | |
185 | " '-a' applies a snapshot (revert disk to saved state)\n" | |
186 | " '-c' creates a snapshot\n" | |
187 | " '-d' deletes a snapshot\n" | |
d14ed18c MR |
188 | " '-l' lists all snapshots in the given image\n" |
189 | "\n" | |
190 | "Parameters to compare subcommand:\n" | |
191 | " '-f' first image format\n" | |
192 | " '-F' second image format\n" | |
86ce1f6e RS |
193 | " '-s' run in Strict mode - fail on different image size or sector allocation\n" |
194 | "\n" | |
195 | "Parameters to dd subcommand:\n" | |
196 | " 'bs=BYTES' read and write up to BYTES bytes at a time " | |
197 | "(default: 512)\n" | |
198 | " 'count=N' copy only N input blocks\n" | |
199 | " 'if=FILE' read from FILE\n" | |
f7c15533 RS |
200 | " 'of=FILE' write to FILE\n" |
201 | " 'skip=N' skip N bs-sized blocks at the start of input\n"; | |
e00291c0 PB |
202 | |
203 | printf("%s\nSupported formats:", help_msg); | |
9ac404c5 | 204 | bdrv_iterate_format(format_print, NULL, false); |
f5048cb7 | 205 | printf("\n\n" QEMU_HELP_BOTTOM "\n"); |
ac1307ab | 206 | exit(EXIT_SUCCESS); |
ea2384d3 FB |
207 | } |
208 | ||
3babeb15 DB |
209 | static QemuOptsList qemu_object_opts = { |
210 | .name = "object", | |
211 | .implied_opt_name = "qom-type", | |
212 | .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head), | |
213 | .desc = { | |
214 | { } | |
215 | }, | |
216 | }; | |
217 | ||
c6e5cdfd KW |
218 | static bool qemu_img_object_print_help(const char *type, QemuOpts *opts) |
219 | { | |
220 | if (user_creatable_print_help(type, opts)) { | |
221 | exit(0); | |
222 | } | |
223 | return true; | |
80c710cb MA |
224 | } |
225 | ||
226 | /* | |
227 | * Is @optarg safe for accumulate_options()? | |
228 | * It is when multiple of them can be joined together separated by ','. | |
229 | * To make that work, @optarg must not start with ',' (or else a | |
230 | * separating ',' preceding it gets escaped), and it must not end with | |
231 | * an odd number of ',' (or else a separating ',' following it gets | |
f62514b3 MA |
232 | * escaped), or be empty (or else a separating ',' preceding it can |
233 | * escape a separating ',' following it). | |
234 | * | |
80c710cb MA |
235 | */ |
236 | static bool is_valid_option_list(const char *optarg) | |
237 | { | |
238 | size_t len = strlen(optarg); | |
239 | size_t i; | |
240 | ||
f62514b3 | 241 | if (!optarg[0] || optarg[0] == ',') { |
80c710cb MA |
242 | return false; |
243 | } | |
244 | ||
245 | for (i = len; i > 0 && optarg[i - 1] == ','; i--) { | |
246 | } | |
247 | if ((len - i) % 2) { | |
248 | return false; | |
249 | } | |
250 | ||
251 | return true; | |
c6e5cdfd KW |
252 | } |
253 | ||
6d2b5cba MA |
254 | static int accumulate_options(char **options, char *optarg) |
255 | { | |
256 | char *new_options; | |
257 | ||
258 | if (!is_valid_option_list(optarg)) { | |
259 | error_report("Invalid option list: %s", optarg); | |
260 | return -1; | |
261 | } | |
262 | ||
263 | if (!*options) { | |
264 | *options = g_strdup(optarg); | |
265 | } else { | |
266 | new_options = g_strdup_printf("%s,%s", *options, optarg); | |
267 | g_free(*options); | |
268 | *options = new_options; | |
269 | } | |
270 | return 0; | |
271 | } | |
272 | ||
eb769f74 DB |
273 | static QemuOptsList qemu_source_opts = { |
274 | .name = "source", | |
275 | .implied_opt_name = "file", | |
276 | .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head), | |
277 | .desc = { | |
278 | { } | |
279 | }, | |
280 | }; | |
281 | ||
7c30f657 | 282 | static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...) |
f382d43a MR |
283 | { |
284 | int ret = 0; | |
285 | if (!quiet) { | |
286 | va_list args; | |
287 | va_start(args, fmt); | |
288 | ret = vprintf(fmt, args); | |
289 | va_end(args); | |
290 | } | |
291 | return ret; | |
292 | } | |
293 | ||
ea2384d3 | 294 | |
4ac8aacd JS |
295 | static int print_block_option_help(const char *filename, const char *fmt) |
296 | { | |
297 | BlockDriver *drv, *proto_drv; | |
83d0521a | 298 | QemuOptsList *create_opts = NULL; |
b65a5e12 | 299 | Error *local_err = NULL; |
4ac8aacd JS |
300 | |
301 | /* Find driver and parse its options */ | |
302 | drv = bdrv_find_format(fmt); | |
303 | if (!drv) { | |
15654a6d | 304 | error_report("Unknown file format '%s'", fmt); |
4ac8aacd JS |
305 | return 1; |
306 | } | |
307 | ||
d402b6a2 HR |
308 | if (!drv->create_opts) { |
309 | error_report("Format driver '%s' does not support image creation", fmt); | |
310 | return 1; | |
311 | } | |
312 | ||
c282e1fd | 313 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
a283cb6e | 314 | if (filename) { |
b65a5e12 | 315 | proto_drv = bdrv_find_protocol(filename, true, &local_err); |
a283cb6e | 316 | if (!proto_drv) { |
2867ce4a | 317 | error_report_err(local_err); |
83d0521a | 318 | qemu_opts_free(create_opts); |
a283cb6e KW |
319 | return 1; |
320 | } | |
d402b6a2 | 321 | if (!proto_drv->create_opts) { |
f0998879 | 322 | error_report("Protocol driver '%s' does not support image creation", |
d402b6a2 | 323 | proto_drv->format_name); |
3ecd5a4f | 324 | qemu_opts_free(create_opts); |
d402b6a2 HR |
325 | return 1; |
326 | } | |
c282e1fd | 327 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
a283cb6e KW |
328 | } |
329 | ||
f4619af0 HR |
330 | if (filename) { |
331 | printf("Supported options:\n"); | |
332 | } else { | |
333 | printf("Supported %s options:\n", fmt); | |
334 | } | |
63898712 | 335 | qemu_opts_print_help(create_opts, false); |
83d0521a | 336 | qemu_opts_free(create_opts); |
f4619af0 HR |
337 | |
338 | if (!filename) { | |
339 | printf("\n" | |
340 | "The protocol level may support further options.\n" | |
341 | "Specify the target filename to include those options.\n"); | |
342 | } | |
343 | ||
4ac8aacd JS |
344 | return 0; |
345 | } | |
346 | ||
eb769f74 | 347 | |
efaa7c4e | 348 | static BlockBackend *img_open_opts(const char *optstr, |
ce099547 | 349 | QemuOpts *opts, int flags, bool writethrough, |
335e9937 | 350 | bool quiet, bool force_share) |
eb769f74 DB |
351 | { |
352 | QDict *options; | |
353 | Error *local_err = NULL; | |
354 | BlockBackend *blk; | |
355 | options = qemu_opts_to_qdict(opts, NULL); | |
335e9937 FZ |
356 | if (force_share) { |
357 | if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE) | |
4615f878 | 358 | && strcmp(qdict_get_str(options, BDRV_OPT_FORCE_SHARE), "on")) { |
335e9937 | 359 | error_report("--force-share/-U conflicts with image options"); |
cb3e7f08 | 360 | qobject_unref(options); |
335e9937 FZ |
361 | return NULL; |
362 | } | |
4615f878 | 363 | qdict_put_str(options, BDRV_OPT_FORCE_SHARE, "on"); |
335e9937 | 364 | } |
efaa7c4e | 365 | blk = blk_new_open(NULL, NULL, options, flags, &local_err); |
eb769f74 | 366 | if (!blk) { |
143605a2 | 367 | error_reportf_err(local_err, "Could not open '%s': ", optstr); |
eb769f74 DB |
368 | return NULL; |
369 | } | |
ce099547 | 370 | blk_set_enable_write_cache(blk, !writethrough); |
eb769f74 | 371 | |
eb769f74 DB |
372 | return blk; |
373 | } | |
374 | ||
efaa7c4e | 375 | static BlockBackend *img_open_file(const char *filename, |
29cf9336 | 376 | QDict *options, |
eb769f74 | 377 | const char *fmt, int flags, |
335e9937 FZ |
378 | bool writethrough, bool quiet, |
379 | bool force_share) | |
eb769f74 DB |
380 | { |
381 | BlockBackend *blk; | |
34b5d2c6 | 382 | Error *local_err = NULL; |
ad717139 | 383 | |
29cf9336 DB |
384 | if (!options) { |
385 | options = qdict_new(); | |
386 | } | |
75c23805 | 387 | if (fmt) { |
46f5ac20 | 388 | qdict_put_str(options, "driver", fmt); |
75c23805 | 389 | } |
b9eaf9ec | 390 | |
335e9937 | 391 | if (force_share) { |
579cf1d1 | 392 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); |
335e9937 | 393 | } |
efaa7c4e | 394 | blk = blk_new_open(filename, NULL, options, flags, &local_err); |
5bd31326 | 395 | if (!blk) { |
c29b77f9 | 396 | error_reportf_err(local_err, "Could not open '%s': ", filename); |
eb769f74 | 397 | return NULL; |
75c23805 | 398 | } |
ce099547 | 399 | blk_set_enable_write_cache(blk, !writethrough); |
b9eaf9ec | 400 | |
eb769f74 DB |
401 | return blk; |
402 | } | |
403 | ||
404 | ||
29cf9336 DB |
405 | static int img_add_key_secrets(void *opaque, |
406 | const char *name, const char *value, | |
407 | Error **errp) | |
408 | { | |
409 | QDict *options = opaque; | |
410 | ||
411 | if (g_str_has_suffix(name, "key-secret")) { | |
187f47e9 | 412 | qdict_put_str(options, name, value); |
29cf9336 DB |
413 | } |
414 | ||
415 | return 0; | |
416 | } | |
417 | ||
29cf9336 | 418 | |
efaa7c4e | 419 | static BlockBackend *img_open(bool image_opts, |
eb769f74 | 420 | const char *filename, |
ce099547 | 421 | const char *fmt, int flags, bool writethrough, |
335e9937 | 422 | bool quiet, bool force_share) |
eb769f74 DB |
423 | { |
424 | BlockBackend *blk; | |
425 | if (image_opts) { | |
426 | QemuOpts *opts; | |
427 | if (fmt) { | |
428 | error_report("--image-opts and --format are mutually exclusive"); | |
429 | return NULL; | |
430 | } | |
431 | opts = qemu_opts_parse_noisily(qemu_find_opts("source"), | |
432 | filename, true); | |
433 | if (!opts) { | |
434 | return NULL; | |
435 | } | |
335e9937 FZ |
436 | blk = img_open_opts(filename, opts, flags, writethrough, quiet, |
437 | force_share); | |
eb769f74 | 438 | } else { |
29cf9336 | 439 | blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet, |
335e9937 | 440 | force_share); |
75c23805 | 441 | } |
7e7d56d9 | 442 | return blk; |
75c23805 FB |
443 | } |
444 | ||
eb769f74 | 445 | |
83d0521a | 446 | static int add_old_style_options(const char *fmt, QemuOpts *opts, |
eec77d9e JS |
447 | const char *base_filename, |
448 | const char *base_fmt) | |
efa84d43 | 449 | { |
6750e795 MA |
450 | Error *err = NULL; |
451 | ||
efa84d43 | 452 | if (base_filename) { |
f43e47db | 453 | qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err); |
6750e795 | 454 | if (err) { |
15654a6d JS |
455 | error_report("Backing file not supported for file format '%s'", |
456 | fmt); | |
6750e795 | 457 | error_free(err); |
c2abccec | 458 | return -1; |
efa84d43 KW |
459 | } |
460 | } | |
461 | if (base_fmt) { | |
f43e47db | 462 | qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err); |
6750e795 | 463 | if (err) { |
15654a6d JS |
464 | error_report("Backing file format not supported for file " |
465 | "format '%s'", fmt); | |
6750e795 | 466 | error_free(err); |
c2abccec | 467 | return -1; |
efa84d43 KW |
468 | } |
469 | } | |
c2abccec | 470 | return 0; |
efa84d43 KW |
471 | } |
472 | ||
43d589b0 EM |
473 | static int64_t cvtnum_full(const char *name, const char *value, int64_t min, |
474 | int64_t max) | |
606caa0a | 475 | { |
f17fd4fd | 476 | int err; |
43d589b0 EM |
477 | uint64_t res; |
478 | ||
479 | err = qemu_strtosz(value, NULL, &res); | |
480 | if (err < 0 && err != -ERANGE) { | |
481 | error_report("Invalid %s specified. You may use " | |
482 | "k, M, G, T, P or E suffixes for", name); | |
483 | error_report("kilobytes, megabytes, gigabytes, terabytes, " | |
484 | "petabytes and exabytes."); | |
f17fd4fd MA |
485 | return err; |
486 | } | |
43d589b0 EM |
487 | if (err == -ERANGE || res > max || res < min) { |
488 | error_report("Invalid %s specified. Must be between %" PRId64 | |
489 | " and %" PRId64 ".", name, min, max); | |
f46bfdbf MA |
490 | return -ERANGE; |
491 | } | |
43d589b0 EM |
492 | return res; |
493 | } | |
494 | ||
495 | static int64_t cvtnum(const char *name, const char *value) | |
496 | { | |
497 | return cvtnum_full(name, value, 0, INT64_MAX); | |
606caa0a MA |
498 | } |
499 | ||
ea2384d3 FB |
500 | static int img_create(int argc, char **argv) |
501 | { | |
a9300911 | 502 | int c; |
1da7cfbd | 503 | uint64_t img_size = -1; |
ea2384d3 | 504 | const char *fmt = "raw"; |
9230eaf6 | 505 | const char *base_fmt = NULL; |
ea2384d3 FB |
506 | const char *filename; |
507 | const char *base_filename = NULL; | |
9ea2ea71 | 508 | char *options = NULL; |
9b37525a | 509 | Error *local_err = NULL; |
f382d43a | 510 | bool quiet = false; |
6e6e55f5 | 511 | int flags = 0; |
3b46e624 | 512 | |
ea2384d3 | 513 | for(;;) { |
3babeb15 DB |
514 | static const struct option long_options[] = { |
515 | {"help", no_argument, 0, 'h'}, | |
516 | {"object", required_argument, 0, OPTION_OBJECT}, | |
517 | {0, 0, 0, 0} | |
518 | }; | |
6e6e55f5 | 519 | c = getopt_long(argc, argv, ":F:b:f:ho:qu", |
3babeb15 | 520 | long_options, NULL); |
b8fb60da | 521 | if (c == -1) { |
ea2384d3 | 522 | break; |
b8fb60da | 523 | } |
ea2384d3 | 524 | switch(c) { |
c9192973 SH |
525 | case ':': |
526 | missing_argument(argv[optind - 1]); | |
527 | break; | |
ef87394c | 528 | case '?': |
c9192973 SH |
529 | unrecognized_option(argv[optind - 1]); |
530 | break; | |
ea2384d3 FB |
531 | case 'h': |
532 | help(); | |
533 | break; | |
9230eaf6 AL |
534 | case 'F': |
535 | base_fmt = optarg; | |
536 | break; | |
ea2384d3 FB |
537 | case 'b': |
538 | base_filename = optarg; | |
539 | break; | |
540 | case 'f': | |
541 | fmt = optarg; | |
542 | break; | |
9ea2ea71 | 543 | case 'o': |
6d2b5cba | 544 | if (accumulate_options(&options, optarg) < 0) { |
77386bf6 KW |
545 | goto fail; |
546 | } | |
9ea2ea71 | 547 | break; |
f382d43a MR |
548 | case 'q': |
549 | quiet = true; | |
550 | break; | |
6e6e55f5 JS |
551 | case 'u': |
552 | flags |= BDRV_O_NO_BACKING; | |
553 | break; | |
3babeb15 DB |
554 | case OPTION_OBJECT: { |
555 | QemuOpts *opts; | |
556 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
557 | optarg, true); | |
558 | if (!opts) { | |
559 | goto fail; | |
560 | } | |
561 | } break; | |
ea2384d3 FB |
562 | } |
563 | } | |
9230eaf6 | 564 | |
b50cbabc | 565 | /* Get the filename */ |
a283cb6e KW |
566 | filename = (optind < argc) ? argv[optind] : NULL; |
567 | if (options && has_help_option(options)) { | |
568 | g_free(options); | |
569 | return print_block_option_help(filename, fmt); | |
570 | } | |
571 | ||
b8fb60da | 572 | if (optind >= argc) { |
ac1307ab | 573 | error_exit("Expecting image file name"); |
b8fb60da | 574 | } |
a283cb6e | 575 | optind++; |
b50cbabc | 576 | |
3babeb15 DB |
577 | if (qemu_opts_foreach(&qemu_object_opts, |
578 | user_creatable_add_opts_foreach, | |
c6e5cdfd | 579 | qemu_img_object_print_help, &error_fatal)) { |
3babeb15 DB |
580 | goto fail; |
581 | } | |
582 | ||
1da7cfbd JS |
583 | /* Get image size, if specified */ |
584 | if (optind < argc) { | |
70b4f4bb | 585 | int64_t sval; |
606caa0a | 586 | |
43d589b0 | 587 | sval = cvtnum("image size", argv[optind++]); |
606caa0a | 588 | if (sval < 0) { |
77386bf6 | 589 | goto fail; |
1da7cfbd JS |
590 | } |
591 | img_size = (uint64_t)sval; | |
592 | } | |
fc11eb26 | 593 | if (optind != argc) { |
ac1307ab | 594 | error_exit("Unexpected argument: %s", argv[optind]); |
fc11eb26 | 595 | } |
1da7cfbd | 596 | |
9b37525a | 597 | bdrv_img_create(filename, fmt, base_filename, base_fmt, |
6e6e55f5 | 598 | options, img_size, flags, quiet, &local_err); |
84d18f06 | 599 | if (local_err) { |
c29b77f9 | 600 | error_reportf_err(local_err, "%s: ", filename); |
77386bf6 | 601 | goto fail; |
c2abccec | 602 | } |
a9300911 | 603 | |
77386bf6 | 604 | g_free(options); |
ea2384d3 | 605 | return 0; |
77386bf6 KW |
606 | |
607 | fail: | |
608 | g_free(options); | |
609 | return 1; | |
ea2384d3 FB |
610 | } |
611 | ||
f382d43a | 612 | static void dump_json_image_check(ImageCheck *check, bool quiet) |
8599ea4c | 613 | { |
8599ea4c | 614 | QString *str; |
8599ea4c | 615 | QObject *obj; |
7d5e199a | 616 | Visitor *v = qobject_output_visitor_new(&obj); |
3b098d56 EB |
617 | |
618 | visit_type_ImageCheck(v, NULL, &check, &error_abort); | |
619 | visit_complete(v, &obj); | |
8599ea4c FS |
620 | str = qobject_to_json_pretty(obj); |
621 | assert(str != NULL); | |
f382d43a | 622 | qprintf(quiet, "%s\n", qstring_get_str(str)); |
cb3e7f08 | 623 | qobject_unref(obj); |
3b098d56 | 624 | visit_free(v); |
cb3e7f08 | 625 | qobject_unref(str); |
8599ea4c FS |
626 | } |
627 | ||
f382d43a | 628 | static void dump_human_image_check(ImageCheck *check, bool quiet) |
8599ea4c FS |
629 | { |
630 | if (!(check->corruptions || check->leaks || check->check_errors)) { | |
f382d43a | 631 | qprintf(quiet, "No errors were found on the image.\n"); |
8599ea4c FS |
632 | } else { |
633 | if (check->corruptions) { | |
f382d43a MR |
634 | qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n" |
635 | "Data may be corrupted, or further writes to the image " | |
636 | "may corrupt it.\n", | |
637 | check->corruptions); | |
8599ea4c FS |
638 | } |
639 | ||
640 | if (check->leaks) { | |
f382d43a MR |
641 | qprintf(quiet, |
642 | "\n%" PRId64 " leaked clusters were found on the image.\n" | |
643 | "This means waste of disk space, but no harm to data.\n", | |
644 | check->leaks); | |
8599ea4c FS |
645 | } |
646 | ||
647 | if (check->check_errors) { | |
f382d43a MR |
648 | qprintf(quiet, |
649 | "\n%" PRId64 | |
650 | " internal errors have occurred during the check.\n", | |
651 | check->check_errors); | |
8599ea4c FS |
652 | } |
653 | } | |
654 | ||
655 | if (check->total_clusters != 0 && check->allocated_clusters != 0) { | |
f382d43a MR |
656 | qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, " |
657 | "%0.2f%% fragmented, %0.2f%% compressed clusters\n", | |
658 | check->allocated_clusters, check->total_clusters, | |
659 | check->allocated_clusters * 100.0 / check->total_clusters, | |
660 | check->fragmented_clusters * 100.0 / check->allocated_clusters, | |
661 | check->compressed_clusters * 100.0 / | |
662 | check->allocated_clusters); | |
8599ea4c FS |
663 | } |
664 | ||
665 | if (check->image_end_offset) { | |
f382d43a MR |
666 | qprintf(quiet, |
667 | "Image end offset: %" PRId64 "\n", check->image_end_offset); | |
8599ea4c FS |
668 | } |
669 | } | |
670 | ||
671 | static int collect_image_check(BlockDriverState *bs, | |
672 | ImageCheck *check, | |
673 | const char *filename, | |
674 | const char *fmt, | |
675 | int fix) | |
676 | { | |
677 | int ret; | |
678 | BdrvCheckResult result; | |
679 | ||
680 | ret = bdrv_check(bs, &result, fix); | |
681 | if (ret < 0) { | |
682 | return ret; | |
683 | } | |
684 | ||
685 | check->filename = g_strdup(filename); | |
686 | check->format = g_strdup(bdrv_get_format_name(bs)); | |
687 | check->check_errors = result.check_errors; | |
688 | check->corruptions = result.corruptions; | |
689 | check->has_corruptions = result.corruptions != 0; | |
690 | check->leaks = result.leaks; | |
691 | check->has_leaks = result.leaks != 0; | |
692 | check->corruptions_fixed = result.corruptions_fixed; | |
1656324e | 693 | check->has_corruptions_fixed = result.corruptions_fixed != 0; |
8599ea4c | 694 | check->leaks_fixed = result.leaks_fixed; |
1656324e | 695 | check->has_leaks_fixed = result.leaks_fixed != 0; |
8599ea4c FS |
696 | check->image_end_offset = result.image_end_offset; |
697 | check->has_image_end_offset = result.image_end_offset != 0; | |
698 | check->total_clusters = result.bfi.total_clusters; | |
699 | check->has_total_clusters = result.bfi.total_clusters != 0; | |
700 | check->allocated_clusters = result.bfi.allocated_clusters; | |
701 | check->has_allocated_clusters = result.bfi.allocated_clusters != 0; | |
702 | check->fragmented_clusters = result.bfi.fragmented_clusters; | |
703 | check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0; | |
e6439d78 SH |
704 | check->compressed_clusters = result.bfi.compressed_clusters; |
705 | check->has_compressed_clusters = result.bfi.compressed_clusters != 0; | |
8599ea4c FS |
706 | |
707 | return 0; | |
708 | } | |
709 | ||
e076f338 KW |
710 | /* |
711 | * Checks an image for consistency. Exit codes: | |
712 | * | |
d6635c4d HR |
713 | * 0 - Check completed, image is good |
714 | * 1 - Check not completed because of internal errors | |
715 | * 2 - Check completed, image is corrupted | |
716 | * 3 - Check completed, image has leaked clusters, but is good otherwise | |
717 | * 63 - Checks are not supported by the image format | |
e076f338 | 718 | */ |
1585969c AL |
719 | static int img_check(int argc, char **argv) |
720 | { | |
721 | int c, ret; | |
8599ea4c | 722 | OutputFormat output_format = OFORMAT_HUMAN; |
40055951 | 723 | const char *filename, *fmt, *output, *cache; |
26f54e9a | 724 | BlockBackend *blk; |
1585969c | 725 | BlockDriverState *bs; |
4534ff54 | 726 | int fix = 0; |
ce099547 KW |
727 | int flags = BDRV_O_CHECK; |
728 | bool writethrough; | |
7e7d56d9 | 729 | ImageCheck *check; |
f382d43a | 730 | bool quiet = false; |
eb769f74 | 731 | bool image_opts = false; |
335e9937 | 732 | bool force_share = false; |
1585969c AL |
733 | |
734 | fmt = NULL; | |
8599ea4c | 735 | output = NULL; |
40055951 | 736 | cache = BDRV_DEFAULT_CACHE; |
ce099547 | 737 | |
1585969c | 738 | for(;;) { |
8599ea4c FS |
739 | int option_index = 0; |
740 | static const struct option long_options[] = { | |
741 | {"help", no_argument, 0, 'h'}, | |
742 | {"format", required_argument, 0, 'f'}, | |
4fd6a984 | 743 | {"repair", required_argument, 0, 'r'}, |
8599ea4c | 744 | {"output", required_argument, 0, OPTION_OUTPUT}, |
3babeb15 | 745 | {"object", required_argument, 0, OPTION_OBJECT}, |
eb769f74 | 746 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
335e9937 | 747 | {"force-share", no_argument, 0, 'U'}, |
8599ea4c FS |
748 | {0, 0, 0, 0} |
749 | }; | |
335e9937 | 750 | c = getopt_long(argc, argv, ":hf:r:T:qU", |
8599ea4c | 751 | long_options, &option_index); |
b8fb60da | 752 | if (c == -1) { |
1585969c | 753 | break; |
b8fb60da | 754 | } |
1585969c | 755 | switch(c) { |
c9192973 SH |
756 | case ':': |
757 | missing_argument(argv[optind - 1]); | |
758 | break; | |
ef87394c | 759 | case '?': |
c9192973 SH |
760 | unrecognized_option(argv[optind - 1]); |
761 | break; | |
1585969c AL |
762 | case 'h': |
763 | help(); | |
764 | break; | |
765 | case 'f': | |
766 | fmt = optarg; | |
767 | break; | |
4534ff54 KW |
768 | case 'r': |
769 | flags |= BDRV_O_RDWR; | |
770 | ||
771 | if (!strcmp(optarg, "leaks")) { | |
772 | fix = BDRV_FIX_LEAKS; | |
773 | } else if (!strcmp(optarg, "all")) { | |
774 | fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS; | |
775 | } else { | |
ac1307ab FZ |
776 | error_exit("Unknown option value for -r " |
777 | "(expecting 'leaks' or 'all'): %s", optarg); | |
4534ff54 KW |
778 | } |
779 | break; | |
8599ea4c FS |
780 | case OPTION_OUTPUT: |
781 | output = optarg; | |
782 | break; | |
40055951 HR |
783 | case 'T': |
784 | cache = optarg; | |
785 | break; | |
f382d43a MR |
786 | case 'q': |
787 | quiet = true; | |
788 | break; | |
335e9937 FZ |
789 | case 'U': |
790 | force_share = true; | |
791 | break; | |
3babeb15 DB |
792 | case OPTION_OBJECT: { |
793 | QemuOpts *opts; | |
794 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
795 | optarg, true); | |
796 | if (!opts) { | |
797 | return 1; | |
798 | } | |
799 | } break; | |
eb769f74 DB |
800 | case OPTION_IMAGE_OPTS: |
801 | image_opts = true; | |
802 | break; | |
1585969c AL |
803 | } |
804 | } | |
fc11eb26 | 805 | if (optind != argc - 1) { |
ac1307ab | 806 | error_exit("Expecting one image file name"); |
b8fb60da | 807 | } |
1585969c AL |
808 | filename = argv[optind++]; |
809 | ||
8599ea4c FS |
810 | if (output && !strcmp(output, "json")) { |
811 | output_format = OFORMAT_JSON; | |
812 | } else if (output && !strcmp(output, "human")) { | |
813 | output_format = OFORMAT_HUMAN; | |
814 | } else if (output) { | |
815 | error_report("--output must be used with human or json as argument."); | |
816 | return 1; | |
817 | } | |
818 | ||
3babeb15 DB |
819 | if (qemu_opts_foreach(&qemu_object_opts, |
820 | user_creatable_add_opts_foreach, | |
c6e5cdfd | 821 | qemu_img_object_print_help, &error_fatal)) { |
3babeb15 DB |
822 | return 1; |
823 | } | |
824 | ||
ce099547 | 825 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
40055951 HR |
826 | if (ret < 0) { |
827 | error_report("Invalid source cache option: %s", cache); | |
828 | return 1; | |
829 | } | |
830 | ||
335e9937 FZ |
831 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
832 | force_share); | |
7e7d56d9 MA |
833 | if (!blk) { |
834 | return 1; | |
c2abccec | 835 | } |
7e7d56d9 | 836 | bs = blk_bs(blk); |
8599ea4c FS |
837 | |
838 | check = g_new0(ImageCheck, 1); | |
839 | ret = collect_image_check(bs, check, filename, fmt, fix); | |
e076f338 KW |
840 | |
841 | if (ret == -ENOTSUP) { | |
55d492d7 | 842 | error_report("This image format does not support checks"); |
fefddf95 | 843 | ret = 63; |
8599ea4c | 844 | goto fail; |
e076f338 KW |
845 | } |
846 | ||
8599ea4c FS |
847 | if (check->corruptions_fixed || check->leaks_fixed) { |
848 | int corruptions_fixed, leaks_fixed; | |
1656324e | 849 | bool has_leaks_fixed, has_corruptions_fixed; |
ccf34716 | 850 | |
8599ea4c | 851 | leaks_fixed = check->leaks_fixed; |
1656324e | 852 | has_leaks_fixed = check->has_leaks_fixed; |
8599ea4c | 853 | corruptions_fixed = check->corruptions_fixed; |
1656324e | 854 | has_corruptions_fixed = check->has_corruptions_fixed; |
e076f338 | 855 | |
8599ea4c | 856 | if (output_format == OFORMAT_HUMAN) { |
f382d43a MR |
857 | qprintf(quiet, |
858 | "The following inconsistencies were found and repaired:\n\n" | |
859 | " %" PRId64 " leaked clusters\n" | |
860 | " %" PRId64 " corruptions\n\n" | |
861 | "Double checking the fixed image now...\n", | |
862 | check->leaks_fixed, | |
863 | check->corruptions_fixed); | |
e076f338 KW |
864 | } |
865 | ||
fc124ea1 PN |
866 | qapi_free_ImageCheck(check); |
867 | check = g_new0(ImageCheck, 1); | |
8599ea4c | 868 | ret = collect_image_check(bs, check, filename, fmt, 0); |
1585969c | 869 | |
8599ea4c | 870 | check->leaks_fixed = leaks_fixed; |
1656324e | 871 | check->has_leaks_fixed = has_leaks_fixed; |
8599ea4c | 872 | check->corruptions_fixed = corruptions_fixed; |
1656324e | 873 | check->has_corruptions_fixed = has_corruptions_fixed; |
f8111c24 DXW |
874 | } |
875 | ||
832390a5 HR |
876 | if (!ret) { |
877 | switch (output_format) { | |
878 | case OFORMAT_HUMAN: | |
879 | dump_human_image_check(check, quiet); | |
880 | break; | |
881 | case OFORMAT_JSON: | |
882 | dump_json_image_check(check, quiet); | |
883 | break; | |
884 | } | |
c6bb9ad1 FS |
885 | } |
886 | ||
8599ea4c | 887 | if (ret || check->check_errors) { |
832390a5 HR |
888 | if (ret) { |
889 | error_report("Check failed: %s", strerror(-ret)); | |
890 | } else { | |
891 | error_report("Check failed"); | |
892 | } | |
8599ea4c FS |
893 | ret = 1; |
894 | goto fail; | |
c2abccec | 895 | } |
e076f338 | 896 | |
8599ea4c FS |
897 | if (check->corruptions) { |
898 | ret = 2; | |
899 | } else if (check->leaks) { | |
900 | ret = 3; | |
e076f338 | 901 | } else { |
8599ea4c | 902 | ret = 0; |
e076f338 | 903 | } |
8599ea4c FS |
904 | |
905 | fail: | |
906 | qapi_free_ImageCheck(check); | |
26f54e9a | 907 | blk_unref(blk); |
8599ea4c | 908 | return ret; |
1585969c AL |
909 | } |
910 | ||
d4a3238a HR |
911 | typedef struct CommonBlockJobCBInfo { |
912 | BlockDriverState *bs; | |
913 | Error **errp; | |
914 | } CommonBlockJobCBInfo; | |
915 | ||
916 | static void common_block_job_cb(void *opaque, int ret) | |
917 | { | |
918 | CommonBlockJobCBInfo *cbi = opaque; | |
919 | ||
920 | if (ret < 0) { | |
921 | error_setg_errno(cbi->errp, -ret, "Block job failed"); | |
922 | } | |
d4a3238a HR |
923 | } |
924 | ||
925 | static void run_block_job(BlockJob *job, Error **errp) | |
926 | { | |
b75536c9 | 927 | AioContext *aio_context = blk_get_aio_context(job->blk); |
4172a003 | 928 | int ret = 0; |
d4a3238a | 929 | |
9e944cb4 | 930 | aio_context_acquire(aio_context); |
80fa2c75 | 931 | job_ref(&job->job); |
d4a3238a | 932 | do { |
30a5c887 | 933 | float progress = 0.0f; |
d4a3238a | 934 | aio_poll(aio_context, true); |
01fe1ca9 VSO |
935 | if (job->job.progress.total) { |
936 | progress = (float)job->job.progress.current / | |
937 | job->job.progress.total * 100.f; | |
30a5c887 KW |
938 | } |
939 | qemu_progress_print(progress, 0); | |
df956ae2 | 940 | } while (!job_is_ready(&job->job) && !job_is_completed(&job->job)); |
d4a3238a | 941 | |
dbe5e6c1 | 942 | if (!job_is_completed(&job->job)) { |
3d70ff53 | 943 | ret = job_complete_sync(&job->job, errp); |
4172a003 | 944 | } else { |
4ad35181 | 945 | ret = job->job.ret; |
4172a003 | 946 | } |
80fa2c75 | 947 | job_unref(&job->job); |
9e944cb4 | 948 | aio_context_release(aio_context); |
687fa1d8 | 949 | |
4172a003 SJ |
950 | /* publish completion progress only when success */ |
951 | if (!ret) { | |
952 | qemu_progress_print(100.f, 0); | |
953 | } | |
d4a3238a HR |
954 | } |
955 | ||
ea2384d3 FB |
956 | static int img_commit(int argc, char **argv) |
957 | { | |
661a0f71 | 958 | int c, ret, flags; |
1b22bffd | 959 | const char *filename, *fmt, *cache, *base; |
26f54e9a | 960 | BlockBackend *blk; |
d4a3238a | 961 | BlockDriverState *bs, *base_bs; |
4ef85a9c | 962 | BlockJob *job; |
687fa1d8 | 963 | bool progress = false, quiet = false, drop = false; |
ce099547 | 964 | bool writethrough; |
d4a3238a HR |
965 | Error *local_err = NULL; |
966 | CommonBlockJobCBInfo cbi; | |
eb769f74 | 967 | bool image_opts = false; |
9e944cb4 | 968 | AioContext *aio_context; |
ea2384d3 FB |
969 | |
970 | fmt = NULL; | |
661a0f71 | 971 | cache = BDRV_DEFAULT_CACHE; |
1b22bffd | 972 | base = NULL; |
ea2384d3 | 973 | for(;;) { |
3babeb15 DB |
974 | static const struct option long_options[] = { |
975 | {"help", no_argument, 0, 'h'}, | |
976 | {"object", required_argument, 0, OPTION_OBJECT}, | |
eb769f74 | 977 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
3babeb15 DB |
978 | {0, 0, 0, 0} |
979 | }; | |
c9192973 | 980 | c = getopt_long(argc, argv, ":f:ht:b:dpq", |
3babeb15 | 981 | long_options, NULL); |
b8fb60da | 982 | if (c == -1) { |
ea2384d3 | 983 | break; |
b8fb60da | 984 | } |
ea2384d3 | 985 | switch(c) { |
c9192973 SH |
986 | case ':': |
987 | missing_argument(argv[optind - 1]); | |
988 | break; | |
ef87394c | 989 | case '?': |
c9192973 SH |
990 | unrecognized_option(argv[optind - 1]); |
991 | break; | |
ea2384d3 FB |
992 | case 'h': |
993 | help(); | |
994 | break; | |
995 | case 'f': | |
996 | fmt = optarg; | |
997 | break; | |
661a0f71 FS |
998 | case 't': |
999 | cache = optarg; | |
1000 | break; | |
1b22bffd HR |
1001 | case 'b': |
1002 | base = optarg; | |
1003 | /* -b implies -d */ | |
1004 | drop = true; | |
1005 | break; | |
9a86fe48 HR |
1006 | case 'd': |
1007 | drop = true; | |
1008 | break; | |
687fa1d8 HR |
1009 | case 'p': |
1010 | progress = true; | |
1011 | break; | |
f382d43a MR |
1012 | case 'q': |
1013 | quiet = true; | |
1014 | break; | |
3babeb15 DB |
1015 | case OPTION_OBJECT: { |
1016 | QemuOpts *opts; | |
1017 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
1018 | optarg, true); | |
1019 | if (!opts) { | |
1020 | return 1; | |
1021 | } | |
1022 | } break; | |
eb769f74 DB |
1023 | case OPTION_IMAGE_OPTS: |
1024 | image_opts = true; | |
1025 | break; | |
ea2384d3 FB |
1026 | } |
1027 | } | |
687fa1d8 HR |
1028 | |
1029 | /* Progress is not shown in Quiet mode */ | |
1030 | if (quiet) { | |
1031 | progress = false; | |
1032 | } | |
1033 | ||
fc11eb26 | 1034 | if (optind != argc - 1) { |
ac1307ab | 1035 | error_exit("Expecting one image file name"); |
b8fb60da | 1036 | } |
ea2384d3 FB |
1037 | filename = argv[optind++]; |
1038 | ||
3babeb15 DB |
1039 | if (qemu_opts_foreach(&qemu_object_opts, |
1040 | user_creatable_add_opts_foreach, | |
c6e5cdfd | 1041 | qemu_img_object_print_help, &error_fatal)) { |
3babeb15 DB |
1042 | return 1; |
1043 | } | |
1044 | ||
9a86fe48 | 1045 | flags = BDRV_O_RDWR | BDRV_O_UNMAP; |
ce099547 | 1046 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
661a0f71 FS |
1047 | if (ret < 0) { |
1048 | error_report("Invalid cache option: %s", cache); | |
a3981eb9 | 1049 | return 1; |
661a0f71 FS |
1050 | } |
1051 | ||
335e9937 FZ |
1052 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
1053 | false); | |
7e7d56d9 MA |
1054 | if (!blk) { |
1055 | return 1; | |
c2abccec | 1056 | } |
7e7d56d9 MA |
1057 | bs = blk_bs(blk); |
1058 | ||
687fa1d8 HR |
1059 | qemu_progress_init(progress, 1.f); |
1060 | qemu_progress_print(0.f, 100); | |
1061 | ||
1b22bffd HR |
1062 | if (base) { |
1063 | base_bs = bdrv_find_backing_image(bs, base); | |
1064 | if (!base_bs) { | |
6b33f3ae HR |
1065 | error_setg(&local_err, |
1066 | "Did not find '%s' in the backing chain of '%s'", | |
1067 | base, filename); | |
1b22bffd HR |
1068 | goto done; |
1069 | } | |
1070 | } else { | |
1071 | /* This is different from QMP, which by default uses the deepest file in | |
1072 | * the backing chain (i.e., the very base); however, the traditional | |
1073 | * behavior of qemu-img commit is using the immediate backing file. */ | |
760e0063 | 1074 | base_bs = backing_bs(bs); |
1b22bffd HR |
1075 | if (!base_bs) { |
1076 | error_setg(&local_err, "Image does not have a backing file"); | |
1077 | goto done; | |
1078 | } | |
d4a3238a HR |
1079 | } |
1080 | ||
1081 | cbi = (CommonBlockJobCBInfo){ | |
1082 | .errp = &local_err, | |
1083 | .bs = bs, | |
1084 | }; | |
1085 | ||
9e944cb4 PB |
1086 | aio_context = bdrv_get_aio_context(bs); |
1087 | aio_context_acquire(aio_context); | |
bb02b65c | 1088 | commit_active_start("commit", bs, base_bs, JOB_DEFAULT, 0, |
0db832f4 | 1089 | BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb, |
78bbd910 | 1090 | &cbi, false, &local_err); |
9e944cb4 | 1091 | aio_context_release(aio_context); |
d4a3238a HR |
1092 | if (local_err) { |
1093 | goto done; | |
ea2384d3 FB |
1094 | } |
1095 | ||
3f09bfbc KW |
1096 | /* When the block job completes, the BlockBackend reference will point to |
1097 | * the old backing file. In order to avoid that the top image is already | |
1098 | * deleted, so we can still empty it afterwards, increment the reference | |
1099 | * counter here preemptively. */ | |
9a86fe48 | 1100 | if (!drop) { |
3f09bfbc | 1101 | bdrv_ref(bs); |
9a86fe48 HR |
1102 | } |
1103 | ||
4ef85a9c | 1104 | job = block_job_get("commit"); |
2e2db260 | 1105 | assert(job); |
4ef85a9c | 1106 | run_block_job(job, &local_err); |
9a86fe48 HR |
1107 | if (local_err) { |
1108 | goto unref_backing; | |
1109 | } | |
1110 | ||
3f09bfbc KW |
1111 | if (!drop && bs->drv->bdrv_make_empty) { |
1112 | ret = bs->drv->bdrv_make_empty(bs); | |
9a86fe48 HR |
1113 | if (ret) { |
1114 | error_setg_errno(&local_err, -ret, "Could not empty %s", | |
1115 | filename); | |
1116 | goto unref_backing; | |
1117 | } | |
1118 | } | |
1119 | ||
1120 | unref_backing: | |
1121 | if (!drop) { | |
3f09bfbc | 1122 | bdrv_unref(bs); |
9a86fe48 | 1123 | } |
d4a3238a HR |
1124 | |
1125 | done: | |
687fa1d8 HR |
1126 | qemu_progress_end(); |
1127 | ||
26f54e9a | 1128 | blk_unref(blk); |
d4a3238a HR |
1129 | |
1130 | if (local_err) { | |
6936f299 | 1131 | error_report_err(local_err); |
c2abccec MK |
1132 | return 1; |
1133 | } | |
d4a3238a HR |
1134 | |
1135 | qprintf(quiet, "Image committed.\n"); | |
ea2384d3 FB |
1136 | return 0; |
1137 | } | |
1138 | ||
debb38a4 EB |
1139 | /* |
1140 | * Returns -1 if 'buf' contains only zeroes, otherwise the byte index | |
1141 | * of the first sector boundary within buf where the sector contains a | |
1142 | * non-zero byte. This function is robust to a buffer that is not | |
1143 | * sector-aligned. | |
1144 | */ | |
1145 | static int64_t find_nonzero(const uint8_t *buf, int64_t n) | |
1146 | { | |
1147 | int64_t i; | |
1148 | int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE); | |
1149 | ||
1150 | for (i = 0; i < end; i += BDRV_SECTOR_SIZE) { | |
1151 | if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) { | |
1152 | return i; | |
1153 | } | |
1154 | } | |
1155 | if (i < n && !buffer_is_zero(buf + i, n - end)) { | |
1156 | return i; | |
1157 | } | |
1158 | return -1; | |
1159 | } | |
1160 | ||
f58c7b35 TS |
1161 | /* |
1162 | * Returns true iff the first sector pointed to by 'buf' contains at least | |
1163 | * a non-NUL byte. | |
1164 | * | |
1165 | * 'pnum' is set to the number of sectors (including and immediately following | |
1166 | * the first one) that are known to be in the same allocated/unallocated state. | |
8dcd3c9b PL |
1167 | * The function will try to align the end offset to alignment boundaries so |
1168 | * that the request will at least end aligned and consequtive requests will | |
1169 | * also start at an aligned offset. | |
f58c7b35 | 1170 | */ |
8dcd3c9b PL |
1171 | static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum, |
1172 | int64_t sector_num, int alignment) | |
ea2384d3 | 1173 | { |
1a6d39fd | 1174 | bool is_zero; |
8dcd3c9b | 1175 | int i, tail; |
ea2384d3 FB |
1176 | |
1177 | if (n <= 0) { | |
1178 | *pnum = 0; | |
1179 | return 0; | |
1180 | } | |
1a6d39fd | 1181 | is_zero = buffer_is_zero(buf, 512); |
ea2384d3 FB |
1182 | for(i = 1; i < n; i++) { |
1183 | buf += 512; | |
1a6d39fd | 1184 | if (is_zero != buffer_is_zero(buf, 512)) { |
ea2384d3 | 1185 | break; |
1a6d39fd | 1186 | } |
ea2384d3 | 1187 | } |
8dcd3c9b PL |
1188 | |
1189 | tail = (sector_num + i) & (alignment - 1); | |
1190 | if (tail) { | |
1191 | if (is_zero && i <= tail) { | |
1192 | /* treat unallocated areas which only consist | |
1193 | * of a small tail as allocated. */ | |
1194 | is_zero = false; | |
1195 | } | |
1196 | if (!is_zero) { | |
1197 | /* align up end offset of allocated areas. */ | |
1198 | i += alignment - tail; | |
1199 | i = MIN(i, n); | |
1200 | } else { | |
1201 | /* align down end offset of zero areas. */ | |
1202 | i -= tail; | |
1203 | } | |
1204 | } | |
ea2384d3 | 1205 | *pnum = i; |
1a6d39fd | 1206 | return !is_zero; |
ea2384d3 FB |
1207 | } |
1208 | ||
a22f123c KW |
1209 | /* |
1210 | * Like is_allocated_sectors, but if the buffer starts with a used sector, | |
1211 | * up to 'min' consecutive sectors containing zeros are ignored. This avoids | |
1212 | * breaking up write requests for only small sparse areas. | |
1213 | */ | |
1214 | static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum, | |
8dcd3c9b | 1215 | int min, int64_t sector_num, int alignment) |
a22f123c KW |
1216 | { |
1217 | int ret; | |
1218 | int num_checked, num_used; | |
1219 | ||
1220 | if (n < min) { | |
1221 | min = n; | |
1222 | } | |
1223 | ||
8dcd3c9b | 1224 | ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment); |
a22f123c KW |
1225 | if (!ret) { |
1226 | return ret; | |
1227 | } | |
1228 | ||
1229 | num_used = *pnum; | |
1230 | buf += BDRV_SECTOR_SIZE * *pnum; | |
1231 | n -= *pnum; | |
8dcd3c9b | 1232 | sector_num += *pnum; |
a22f123c KW |
1233 | num_checked = num_used; |
1234 | ||
1235 | while (n > 0) { | |
8dcd3c9b | 1236 | ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment); |
a22f123c KW |
1237 | |
1238 | buf += BDRV_SECTOR_SIZE * *pnum; | |
1239 | n -= *pnum; | |
8dcd3c9b | 1240 | sector_num += *pnum; |
a22f123c KW |
1241 | num_checked += *pnum; |
1242 | if (ret) { | |
1243 | num_used = num_checked; | |
1244 | } else if (*pnum >= min) { | |
1245 | break; | |
1246 | } | |
1247 | } | |
1248 | ||
1249 | *pnum = num_used; | |
1250 | return 1; | |
1251 | } | |
1252 | ||
3e85c6fd | 1253 | /* |
dc61cd3b EB |
1254 | * Compares two buffers sector by sector. Returns 0 if the first |
1255 | * sector of each buffer matches, non-zero otherwise. | |
3e85c6fd | 1256 | * |
dc61cd3b EB |
1257 | * pnum is set to the sector-aligned size of the buffer prefix that |
1258 | * has the same matching status as the first sector. | |
3e85c6fd | 1259 | */ |
dc61cd3b EB |
1260 | static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2, |
1261 | int64_t bytes, int64_t *pnum) | |
3e85c6fd | 1262 | { |
8c1ac475 | 1263 | bool res; |
dc61cd3b | 1264 | int64_t i = MIN(bytes, BDRV_SECTOR_SIZE); |
3e85c6fd | 1265 | |
dc61cd3b | 1266 | assert(bytes > 0); |
3e85c6fd | 1267 | |
dc61cd3b EB |
1268 | res = !!memcmp(buf1, buf2, i); |
1269 | while (i < bytes) { | |
1270 | int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE); | |
3e85c6fd | 1271 | |
dc61cd3b | 1272 | if (!!memcmp(buf1 + i, buf2 + i, len) != res) { |
3e85c6fd KW |
1273 | break; |
1274 | } | |
dc61cd3b | 1275 | i += len; |
3e85c6fd KW |
1276 | } |
1277 | ||
1278 | *pnum = i; | |
1279 | return res; | |
1280 | } | |
1281 | ||
97ede57a | 1282 | #define IO_BUF_SIZE (2 * MiB) |
ea2384d3 | 1283 | |
d14ed18c MR |
1284 | /* |
1285 | * Check if passed sectors are empty (not allocated or contain only 0 bytes) | |
1286 | * | |
0608e40e EB |
1287 | * Intended for use by 'qemu-img compare': Returns 0 in case sectors are |
1288 | * filled with 0, 1 if sectors contain non-zero data (this is a comparison | |
1289 | * failure), and 4 on error (the exit status for read errors), after emitting | |
1290 | * an error message. | |
d14ed18c | 1291 | * |
f1d3cd79 | 1292 | * @param blk: BlockBackend for the image |
c41508ed EB |
1293 | * @param offset: Starting offset to check |
1294 | * @param bytes: Number of bytes to check | |
d14ed18c MR |
1295 | * @param filename: Name of disk file we are checking (logging purpose) |
1296 | * @param buffer: Allocated buffer for storing read data | |
1297 | * @param quiet: Flag for quiet mode | |
1298 | */ | |
c41508ed EB |
1299 | static int check_empty_sectors(BlockBackend *blk, int64_t offset, |
1300 | int64_t bytes, const char *filename, | |
d14ed18c MR |
1301 | uint8_t *buffer, bool quiet) |
1302 | { | |
debb38a4 EB |
1303 | int ret = 0; |
1304 | int64_t idx; | |
1305 | ||
c41508ed | 1306 | ret = blk_pread(blk, offset, buffer, bytes); |
d14ed18c MR |
1307 | if (ret < 0) { |
1308 | error_report("Error while reading offset %" PRId64 " of %s: %s", | |
c41508ed | 1309 | offset, filename, strerror(-ret)); |
0608e40e | 1310 | return 4; |
d14ed18c | 1311 | } |
c41508ed | 1312 | idx = find_nonzero(buffer, bytes); |
debb38a4 | 1313 | if (idx >= 0) { |
d14ed18c | 1314 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
c41508ed | 1315 | offset + idx); |
d14ed18c MR |
1316 | return 1; |
1317 | } | |
1318 | ||
1319 | return 0; | |
1320 | } | |
1321 | ||
1322 | /* | |
1323 | * Compares two images. Exit codes: | |
1324 | * | |
1325 | * 0 - Images are identical | |
1326 | * 1 - Images differ | |
1327 | * >1 - Error occurred | |
1328 | */ | |
1329 | static int img_compare(int argc, char **argv) | |
1330 | { | |
40055951 | 1331 | const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2; |
26f54e9a | 1332 | BlockBackend *blk1, *blk2; |
d14ed18c | 1333 | BlockDriverState *bs1, *bs2; |
033d9fc2 | 1334 | int64_t total_size1, total_size2; |
d14ed18c | 1335 | uint8_t *buf1 = NULL, *buf2 = NULL; |
31826642 | 1336 | int64_t pnum1, pnum2; |
d14ed18c MR |
1337 | int allocated1, allocated2; |
1338 | int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */ | |
1339 | bool progress = false, quiet = false, strict = false; | |
40055951 | 1340 | int flags; |
ce099547 | 1341 | bool writethrough; |
033d9fc2 EB |
1342 | int64_t total_size; |
1343 | int64_t offset = 0; | |
1344 | int64_t chunk; | |
dc61cd3b | 1345 | int c; |
d14ed18c | 1346 | uint64_t progress_base; |
eb769f74 | 1347 | bool image_opts = false; |
335e9937 | 1348 | bool force_share = false; |
d14ed18c | 1349 | |
40055951 | 1350 | cache = BDRV_DEFAULT_CACHE; |
d14ed18c | 1351 | for (;;) { |
3babeb15 DB |
1352 | static const struct option long_options[] = { |
1353 | {"help", no_argument, 0, 'h'}, | |
1354 | {"object", required_argument, 0, OPTION_OBJECT}, | |
eb769f74 | 1355 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
335e9937 | 1356 | {"force-share", no_argument, 0, 'U'}, |
3babeb15 DB |
1357 | {0, 0, 0, 0} |
1358 | }; | |
335e9937 | 1359 | c = getopt_long(argc, argv, ":hf:F:T:pqsU", |
3babeb15 | 1360 | long_options, NULL); |
d14ed18c MR |
1361 | if (c == -1) { |
1362 | break; | |
1363 | } | |
1364 | switch (c) { | |
c9192973 SH |
1365 | case ':': |
1366 | missing_argument(argv[optind - 1]); | |
1367 | break; | |
d14ed18c | 1368 | case '?': |
c9192973 SH |
1369 | unrecognized_option(argv[optind - 1]); |
1370 | break; | |
d14ed18c MR |
1371 | case 'h': |
1372 | help(); | |
1373 | break; | |
1374 | case 'f': | |
1375 | fmt1 = optarg; | |
1376 | break; | |
1377 | case 'F': | |
1378 | fmt2 = optarg; | |
1379 | break; | |
40055951 HR |
1380 | case 'T': |
1381 | cache = optarg; | |
1382 | break; | |
d14ed18c MR |
1383 | case 'p': |
1384 | progress = true; | |
1385 | break; | |
1386 | case 'q': | |
1387 | quiet = true; | |
1388 | break; | |
1389 | case 's': | |
1390 | strict = true; | |
1391 | break; | |
335e9937 FZ |
1392 | case 'U': |
1393 | force_share = true; | |
1394 | break; | |
3babeb15 DB |
1395 | case OPTION_OBJECT: { |
1396 | QemuOpts *opts; | |
1397 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
1398 | optarg, true); | |
1399 | if (!opts) { | |
1400 | ret = 2; | |
1401 | goto out4; | |
1402 | } | |
1403 | } break; | |
eb769f74 DB |
1404 | case OPTION_IMAGE_OPTS: |
1405 | image_opts = true; | |
1406 | break; | |
d14ed18c MR |
1407 | } |
1408 | } | |
1409 | ||
1410 | /* Progress is not shown in Quiet mode */ | |
1411 | if (quiet) { | |
1412 | progress = false; | |
1413 | } | |
1414 | ||
1415 | ||
fc11eb26 | 1416 | if (optind != argc - 2) { |
ac1307ab | 1417 | error_exit("Expecting two image file names"); |
d14ed18c MR |
1418 | } |
1419 | filename1 = argv[optind++]; | |
1420 | filename2 = argv[optind++]; | |
1421 | ||
3babeb15 DB |
1422 | if (qemu_opts_foreach(&qemu_object_opts, |
1423 | user_creatable_add_opts_foreach, | |
c6e5cdfd | 1424 | qemu_img_object_print_help, &error_fatal)) { |
3babeb15 DB |
1425 | ret = 2; |
1426 | goto out4; | |
1427 | } | |
1428 | ||
cbda016d SH |
1429 | /* Initialize before goto out */ |
1430 | qemu_progress_init(progress, 2.0); | |
1431 | ||
ce099547 KW |
1432 | flags = 0; |
1433 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); | |
40055951 HR |
1434 | if (ret < 0) { |
1435 | error_report("Invalid source cache option: %s", cache); | |
1436 | ret = 2; | |
1437 | goto out3; | |
1438 | } | |
1439 | ||
335e9937 FZ |
1440 | blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet, |
1441 | force_share); | |
7e7d56d9 | 1442 | if (!blk1) { |
d14ed18c | 1443 | ret = 2; |
7e7d56d9 | 1444 | goto out3; |
d14ed18c MR |
1445 | } |
1446 | ||
335e9937 FZ |
1447 | blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet, |
1448 | force_share); | |
7e7d56d9 | 1449 | if (!blk2) { |
d14ed18c | 1450 | ret = 2; |
7e7d56d9 | 1451 | goto out2; |
d14ed18c | 1452 | } |
eb769f74 | 1453 | bs1 = blk_bs(blk1); |
7e7d56d9 | 1454 | bs2 = blk_bs(blk2); |
d14ed18c | 1455 | |
f1d3cd79 HR |
1456 | buf1 = blk_blockalign(blk1, IO_BUF_SIZE); |
1457 | buf2 = blk_blockalign(blk2, IO_BUF_SIZE); | |
033d9fc2 EB |
1458 | total_size1 = blk_getlength(blk1); |
1459 | if (total_size1 < 0) { | |
52bf1e72 | 1460 | error_report("Can't get size of %s: %s", |
033d9fc2 | 1461 | filename1, strerror(-total_size1)); |
52bf1e72 MA |
1462 | ret = 4; |
1463 | goto out; | |
1464 | } | |
033d9fc2 EB |
1465 | total_size2 = blk_getlength(blk2); |
1466 | if (total_size2 < 0) { | |
52bf1e72 | 1467 | error_report("Can't get size of %s: %s", |
033d9fc2 | 1468 | filename2, strerror(-total_size2)); |
52bf1e72 MA |
1469 | ret = 4; |
1470 | goto out; | |
1471 | } | |
033d9fc2 EB |
1472 | total_size = MIN(total_size1, total_size2); |
1473 | progress_base = MAX(total_size1, total_size2); | |
d14ed18c MR |
1474 | |
1475 | qemu_progress_print(0, 100); | |
1476 | ||
033d9fc2 | 1477 | if (strict && total_size1 != total_size2) { |
d14ed18c MR |
1478 | ret = 1; |
1479 | qprintf(quiet, "Strict mode: Image size mismatch!\n"); | |
1480 | goto out; | |
1481 | } | |
1482 | ||
033d9fc2 | 1483 | while (offset < total_size) { |
31826642 | 1484 | int status1, status2; |
67a0fd2a | 1485 | |
033d9fc2 EB |
1486 | status1 = bdrv_block_status_above(bs1, NULL, offset, |
1487 | total_size1 - offset, &pnum1, NULL, | |
1488 | NULL); | |
25ad8e6e | 1489 | if (status1 < 0) { |
d14ed18c MR |
1490 | ret = 3; |
1491 | error_report("Sector allocation test failed for %s", filename1); | |
1492 | goto out; | |
1493 | } | |
25ad8e6e | 1494 | allocated1 = status1 & BDRV_BLOCK_ALLOCATED; |
d14ed18c | 1495 | |
033d9fc2 EB |
1496 | status2 = bdrv_block_status_above(bs2, NULL, offset, |
1497 | total_size2 - offset, &pnum2, NULL, | |
1498 | NULL); | |
25ad8e6e | 1499 | if (status2 < 0) { |
d14ed18c MR |
1500 | ret = 3; |
1501 | error_report("Sector allocation test failed for %s", filename2); | |
1502 | goto out; | |
1503 | } | |
25ad8e6e | 1504 | allocated2 = status2 & BDRV_BLOCK_ALLOCATED; |
7daddc61 EB |
1505 | |
1506 | assert(pnum1 && pnum2); | |
033d9fc2 | 1507 | chunk = MIN(pnum1, pnum2); |
d14ed18c | 1508 | |
25ad8e6e | 1509 | if (strict) { |
31826642 | 1510 | if (status1 != status2) { |
25ad8e6e FZ |
1511 | ret = 1; |
1512 | qprintf(quiet, "Strict mode: Offset %" PRId64 | |
033d9fc2 | 1513 | " block status mismatch!\n", offset); |
25ad8e6e FZ |
1514 | goto out; |
1515 | } | |
1516 | } | |
1517 | if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) { | |
7daddc61 | 1518 | /* nothing to do */ |
25ad8e6e | 1519 | } else if (allocated1 == allocated2) { |
d14ed18c | 1520 | if (allocated1) { |
dc61cd3b EB |
1521 | int64_t pnum; |
1522 | ||
033d9fc2 EB |
1523 | chunk = MIN(chunk, IO_BUF_SIZE); |
1524 | ret = blk_pread(blk1, offset, buf1, chunk); | |
d14ed18c | 1525 | if (ret < 0) { |
033d9fc2 EB |
1526 | error_report("Error while reading offset %" PRId64 |
1527 | " of %s: %s", | |
1528 | offset, filename1, strerror(-ret)); | |
d14ed18c MR |
1529 | ret = 4; |
1530 | goto out; | |
1531 | } | |
033d9fc2 | 1532 | ret = blk_pread(blk2, offset, buf2, chunk); |
d14ed18c MR |
1533 | if (ret < 0) { |
1534 | error_report("Error while reading offset %" PRId64 | |
033d9fc2 EB |
1535 | " of %s: %s", |
1536 | offset, filename2, strerror(-ret)); | |
d14ed18c MR |
1537 | ret = 4; |
1538 | goto out; | |
1539 | } | |
033d9fc2 EB |
1540 | ret = compare_buffers(buf1, buf2, chunk, &pnum); |
1541 | if (ret || pnum != chunk) { | |
d14ed18c | 1542 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
033d9fc2 | 1543 | offset + (ret ? 0 : pnum)); |
36452f12 | 1544 | ret = 1; |
d14ed18c MR |
1545 | goto out; |
1546 | } | |
1547 | } | |
1548 | } else { | |
033d9fc2 | 1549 | chunk = MIN(chunk, IO_BUF_SIZE); |
d14ed18c | 1550 | if (allocated1) { |
033d9fc2 | 1551 | ret = check_empty_sectors(blk1, offset, chunk, |
d14ed18c MR |
1552 | filename1, buf1, quiet); |
1553 | } else { | |
033d9fc2 | 1554 | ret = check_empty_sectors(blk2, offset, chunk, |
d14ed18c MR |
1555 | filename2, buf1, quiet); |
1556 | } | |
1557 | if (ret) { | |
d14ed18c MR |
1558 | goto out; |
1559 | } | |
1560 | } | |
033d9fc2 EB |
1561 | offset += chunk; |
1562 | qemu_progress_print(((float) chunk / progress_base) * 100, 100); | |
d14ed18c MR |
1563 | } |
1564 | ||
033d9fc2 | 1565 | if (total_size1 != total_size2) { |
f1d3cd79 | 1566 | BlockBackend *blk_over; |
d14ed18c MR |
1567 | const char *filename_over; |
1568 | ||
1569 | qprintf(quiet, "Warning: Image size mismatch!\n"); | |
033d9fc2 | 1570 | if (total_size1 > total_size2) { |
f1d3cd79 | 1571 | blk_over = blk1; |
d14ed18c MR |
1572 | filename_over = filename1; |
1573 | } else { | |
f1d3cd79 | 1574 | blk_over = blk2; |
d14ed18c MR |
1575 | filename_over = filename2; |
1576 | } | |
1577 | ||
033d9fc2 EB |
1578 | while (offset < progress_base) { |
1579 | ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset, | |
1580 | progress_base - offset, &chunk, | |
1581 | NULL, NULL); | |
d14ed18c MR |
1582 | if (ret < 0) { |
1583 | ret = 3; | |
1584 | error_report("Sector allocation test failed for %s", | |
1585 | filename_over); | |
1586 | goto out; | |
1587 | ||
1588 | } | |
391cb1aa | 1589 | if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) { |
033d9fc2 EB |
1590 | chunk = MIN(chunk, IO_BUF_SIZE); |
1591 | ret = check_empty_sectors(blk_over, offset, chunk, | |
d14ed18c MR |
1592 | filename_over, buf1, quiet); |
1593 | if (ret) { | |
d14ed18c MR |
1594 | goto out; |
1595 | } | |
1596 | } | |
033d9fc2 EB |
1597 | offset += chunk; |
1598 | qemu_progress_print(((float) chunk / progress_base) * 100, 100); | |
d14ed18c MR |
1599 | } |
1600 | } | |
1601 | ||
1602 | qprintf(quiet, "Images are identical.\n"); | |
1603 | ret = 0; | |
1604 | ||
1605 | out: | |
d14ed18c MR |
1606 | qemu_vfree(buf1); |
1607 | qemu_vfree(buf2); | |
26f54e9a | 1608 | blk_unref(blk2); |
d14ed18c | 1609 | out2: |
26f54e9a | 1610 | blk_unref(blk1); |
d14ed18c MR |
1611 | out3: |
1612 | qemu_progress_end(); | |
3babeb15 | 1613 | out4: |
d14ed18c MR |
1614 | return ret; |
1615 | } | |
1616 | ||
690c7301 KW |
1617 | enum ImgConvertBlockStatus { |
1618 | BLK_DATA, | |
1619 | BLK_ZERO, | |
1620 | BLK_BACKING_FILE, | |
1621 | }; | |
1622 | ||
2d9187bc PL |
1623 | #define MAX_COROUTINES 16 |
1624 | ||
690c7301 KW |
1625 | typedef struct ImgConvertState { |
1626 | BlockBackend **src; | |
1627 | int64_t *src_sectors; | |
2d9187bc | 1628 | int src_num; |
690c7301 KW |
1629 | int64_t total_sectors; |
1630 | int64_t allocated_sectors; | |
2d9187bc PL |
1631 | int64_t allocated_done; |
1632 | int64_t sector_num; | |
1633 | int64_t wr_offs; | |
690c7301 KW |
1634 | enum ImgConvertBlockStatus status; |
1635 | int64_t sector_next_status; | |
1636 | BlockBackend *target; | |
1637 | bool has_zero_init; | |
1638 | bool compressed; | |
351c8eff | 1639 | bool unallocated_blocks_are_zero; |
4d7c487e | 1640 | bool target_is_new; |
690c7301 | 1641 | bool target_has_backing; |
351c8eff | 1642 | int64_t target_backing_sectors; /* negative if unknown */ |
2d9187bc | 1643 | bool wr_in_order; |
ee5306d0 | 1644 | bool copy_range; |
8eaac025 | 1645 | bool salvage; |
3d96cb91 | 1646 | bool quiet; |
690c7301 | 1647 | int min_sparse; |
8dcd3c9b | 1648 | int alignment; |
690c7301 KW |
1649 | size_t cluster_sectors; |
1650 | size_t buf_sectors; | |
9fd77f99 | 1651 | long num_coroutines; |
2d9187bc PL |
1652 | int running_coroutines; |
1653 | Coroutine *co[MAX_COROUTINES]; | |
1654 | int64_t wait_sector_num[MAX_COROUTINES]; | |
1655 | CoMutex lock; | |
1656 | int ret; | |
690c7301 KW |
1657 | } ImgConvertState; |
1658 | ||
2d9187bc PL |
1659 | static void convert_select_part(ImgConvertState *s, int64_t sector_num, |
1660 | int *src_cur, int64_t *src_cur_offset) | |
690c7301 | 1661 | { |
2d9187bc PL |
1662 | *src_cur = 0; |
1663 | *src_cur_offset = 0; | |
1664 | while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) { | |
1665 | *src_cur_offset += s->src_sectors[*src_cur]; | |
1666 | (*src_cur)++; | |
1667 | assert(*src_cur < s->src_num); | |
690c7301 KW |
1668 | } |
1669 | } | |
1670 | ||
1671 | static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num) | |
1672 | { | |
31826642 EB |
1673 | int64_t src_cur_offset; |
1674 | int ret, n, src_cur; | |
351c8eff | 1675 | bool post_backing_zero = false; |
690c7301 | 1676 | |
2d9187bc | 1677 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
690c7301 KW |
1678 | |
1679 | assert(s->total_sectors > sector_num); | |
1680 | n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS); | |
1681 | ||
351c8eff HR |
1682 | if (s->target_backing_sectors >= 0) { |
1683 | if (sector_num >= s->target_backing_sectors) { | |
1684 | post_backing_zero = s->unallocated_blocks_are_zero; | |
1685 | } else if (sector_num + n > s->target_backing_sectors) { | |
1686 | /* Split requests around target_backing_sectors (because | |
1687 | * starting from there, zeros are handled differently) */ | |
1688 | n = s->target_backing_sectors - sector_num; | |
1689 | } | |
1690 | } | |
1691 | ||
690c7301 | 1692 | if (s->sector_next_status <= sector_num) { |
8eaac025 HR |
1693 | uint64_t offset = (sector_num - src_cur_offset) * BDRV_SECTOR_SIZE; |
1694 | int64_t count; | |
31826642 | 1695 | |
8eaac025 HR |
1696 | do { |
1697 | count = n * BDRV_SECTOR_SIZE; | |
1698 | ||
1699 | if (s->target_has_backing) { | |
1700 | ret = bdrv_block_status(blk_bs(s->src[src_cur]), offset, | |
1701 | count, &count, NULL, NULL); | |
1702 | } else { | |
1703 | ret = bdrv_block_status_above(blk_bs(s->src[src_cur]), NULL, | |
1704 | offset, count, &count, NULL, | |
1705 | NULL); | |
1706 | } | |
1707 | ||
1708 | if (ret < 0) { | |
1709 | if (s->salvage) { | |
1710 | if (n == 1) { | |
1711 | if (!s->quiet) { | |
1712 | warn_report("error while reading block status at " | |
1713 | "offset %" PRIu64 ": %s", offset, | |
1714 | strerror(-ret)); | |
1715 | } | |
1716 | /* Just try to read the data, then */ | |
1717 | ret = BDRV_BLOCK_DATA; | |
1718 | count = BDRV_SECTOR_SIZE; | |
1719 | } else { | |
1720 | /* Retry on a shorter range */ | |
1721 | n = DIV_ROUND_UP(n, 4); | |
1722 | } | |
1723 | } else { | |
1724 | error_report("error while reading block status at offset " | |
1725 | "%" PRIu64 ": %s", offset, strerror(-ret)); | |
1726 | return ret; | |
1727 | } | |
1728 | } | |
1729 | } while (ret < 0); | |
237d78f8 | 1730 | |
31826642 | 1731 | n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE); |
690c7301 KW |
1732 | |
1733 | if (ret & BDRV_BLOCK_ZERO) { | |
351c8eff | 1734 | s->status = post_backing_zero ? BLK_BACKING_FILE : BLK_ZERO; |
690c7301 KW |
1735 | } else if (ret & BDRV_BLOCK_DATA) { |
1736 | s->status = BLK_DATA; | |
690c7301 | 1737 | } else { |
9f1b92ad | 1738 | s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA; |
690c7301 KW |
1739 | } |
1740 | ||
1741 | s->sector_next_status = sector_num + n; | |
1742 | } | |
1743 | ||
1744 | n = MIN(n, s->sector_next_status - sector_num); | |
1745 | if (s->status == BLK_DATA) { | |
1746 | n = MIN(n, s->buf_sectors); | |
1747 | } | |
1748 | ||
1749 | /* We need to write complete clusters for compressed images, so if an | |
1750 | * unallocated area is shorter than that, we must consider the whole | |
1751 | * cluster allocated. */ | |
1752 | if (s->compressed) { | |
1753 | if (n < s->cluster_sectors) { | |
1754 | n = MIN(s->cluster_sectors, s->total_sectors - sector_num); | |
1755 | s->status = BLK_DATA; | |
1756 | } else { | |
1757 | n = QEMU_ALIGN_DOWN(n, s->cluster_sectors); | |
1758 | } | |
1759 | } | |
1760 | ||
1761 | return n; | |
1762 | } | |
1763 | ||
2d9187bc PL |
1764 | static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num, |
1765 | int nb_sectors, uint8_t *buf) | |
690c7301 | 1766 | { |
8eaac025 | 1767 | uint64_t single_read_until = 0; |
2d9187bc | 1768 | int n, ret; |
690c7301 | 1769 | |
690c7301 KW |
1770 | assert(nb_sectors <= s->buf_sectors); |
1771 | while (nb_sectors > 0) { | |
1772 | BlockBackend *blk; | |
2d9187bc PL |
1773 | int src_cur; |
1774 | int64_t bs_sectors, src_cur_offset; | |
8eaac025 | 1775 | uint64_t offset; |
690c7301 KW |
1776 | |
1777 | /* In the case of compression with multiple source files, we can get a | |
1778 | * nb_sectors that spreads into the next part. So we must be able to | |
1779 | * read across multiple BDSes for one convert_read() call. */ | |
2d9187bc PL |
1780 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
1781 | blk = s->src[src_cur]; | |
1782 | bs_sectors = s->src_sectors[src_cur]; | |
1783 | ||
8eaac025 HR |
1784 | offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS; |
1785 | ||
2d9187bc | 1786 | n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset)); |
8eaac025 HR |
1787 | if (single_read_until > offset) { |
1788 | n = 1; | |
1789 | } | |
2d9187bc | 1790 | |
8eaac025 | 1791 | ret = blk_co_pread(blk, offset, n << BDRV_SECTOR_BITS, buf, 0); |
690c7301 | 1792 | if (ret < 0) { |
8eaac025 HR |
1793 | if (s->salvage) { |
1794 | if (n > 1) { | |
1795 | single_read_until = offset + (n << BDRV_SECTOR_BITS); | |
1796 | continue; | |
1797 | } else { | |
1798 | if (!s->quiet) { | |
1799 | warn_report("error while reading offset %" PRIu64 | |
1800 | ": %s", offset, strerror(-ret)); | |
1801 | } | |
1802 | memset(buf, 0, BDRV_SECTOR_SIZE); | |
1803 | } | |
1804 | } else { | |
1805 | return ret; | |
1806 | } | |
690c7301 KW |
1807 | } |
1808 | ||
1809 | sector_num += n; | |
1810 | nb_sectors -= n; | |
1811 | buf += n * BDRV_SECTOR_SIZE; | |
1812 | } | |
1813 | ||
1814 | return 0; | |
1815 | } | |
1816 | ||
2d9187bc PL |
1817 | |
1818 | static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num, | |
1819 | int nb_sectors, uint8_t *buf, | |
1820 | enum ImgConvertBlockStatus status) | |
690c7301 KW |
1821 | { |
1822 | int ret; | |
1823 | ||
1824 | while (nb_sectors > 0) { | |
1825 | int n = nb_sectors; | |
db933fbe LC |
1826 | BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0; |
1827 | ||
2d9187bc | 1828 | switch (status) { |
690c7301 KW |
1829 | case BLK_BACKING_FILE: |
1830 | /* If we have a backing file, leave clusters unallocated that are | |
1831 | * unallocated in the source image, so that the backing file is | |
1832 | * visible at the respective offset. */ | |
1833 | assert(s->target_has_backing); | |
1834 | break; | |
1835 | ||
1836 | case BLK_DATA: | |
db933fbe LC |
1837 | /* If we're told to keep the target fully allocated (-S 0) or there |
1838 | * is real non-zero data, we must write it. Otherwise we can treat | |
1839 | * it as zero sectors. | |
1840 | * Compressed clusters need to be written as a whole, so in that | |
1841 | * case we can only save the write if the buffer is completely | |
1842 | * zeroed. */ | |
690c7301 | 1843 | if (!s->min_sparse || |
db933fbe | 1844 | (!s->compressed && |
8dcd3c9b PL |
1845 | is_allocated_sectors_min(buf, n, &n, s->min_sparse, |
1846 | sector_num, s->alignment)) || | |
db933fbe LC |
1847 | (s->compressed && |
1848 | !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))) | |
690c7301 | 1849 | { |
265a7e54 VSO |
1850 | ret = blk_co_pwrite(s->target, sector_num << BDRV_SECTOR_BITS, |
1851 | n << BDRV_SECTOR_BITS, buf, flags); | |
690c7301 KW |
1852 | if (ret < 0) { |
1853 | return ret; | |
1854 | } | |
1855 | break; | |
1856 | } | |
1857 | /* fall-through */ | |
1858 | ||
1859 | case BLK_ZERO: | |
1860 | if (s->has_zero_init) { | |
db933fbe | 1861 | assert(!s->target_has_backing); |
690c7301 KW |
1862 | break; |
1863 | } | |
2d9187bc PL |
1864 | ret = blk_co_pwrite_zeroes(s->target, |
1865 | sector_num << BDRV_SECTOR_BITS, | |
a3d6ae22 NS |
1866 | n << BDRV_SECTOR_BITS, |
1867 | BDRV_REQ_MAY_UNMAP); | |
690c7301 KW |
1868 | if (ret < 0) { |
1869 | return ret; | |
1870 | } | |
1871 | break; | |
1872 | } | |
1873 | ||
1874 | sector_num += n; | |
1875 | nb_sectors -= n; | |
1876 | buf += n * BDRV_SECTOR_SIZE; | |
1877 | } | |
1878 | ||
1879 | return 0; | |
1880 | } | |
1881 | ||
ee5306d0 FZ |
1882 | static int coroutine_fn convert_co_copy_range(ImgConvertState *s, int64_t sector_num, |
1883 | int nb_sectors) | |
1884 | { | |
1885 | int n, ret; | |
1886 | ||
1887 | while (nb_sectors > 0) { | |
1888 | BlockBackend *blk; | |
1889 | int src_cur; | |
1890 | int64_t bs_sectors, src_cur_offset; | |
1891 | int64_t offset; | |
1892 | ||
1893 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); | |
1894 | offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS; | |
1895 | blk = s->src[src_cur]; | |
1896 | bs_sectors = s->src_sectors[src_cur]; | |
1897 | ||
1898 | n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset)); | |
1899 | ||
1900 | ret = blk_co_copy_range(blk, offset, s->target, | |
1901 | sector_num << BDRV_SECTOR_BITS, | |
67b51fb9 | 1902 | n << BDRV_SECTOR_BITS, 0, 0); |
ee5306d0 FZ |
1903 | if (ret < 0) { |
1904 | return ret; | |
1905 | } | |
1906 | ||
1907 | sector_num += n; | |
1908 | nb_sectors -= n; | |
1909 | } | |
1910 | return 0; | |
1911 | } | |
1912 | ||
2d9187bc | 1913 | static void coroutine_fn convert_co_do_copy(void *opaque) |
690c7301 | 1914 | { |
2d9187bc | 1915 | ImgConvertState *s = opaque; |
690c7301 | 1916 | uint8_t *buf = NULL; |
2d9187bc PL |
1917 | int ret, i; |
1918 | int index = -1; | |
1919 | ||
1920 | for (i = 0; i < s->num_coroutines; i++) { | |
1921 | if (s->co[i] == qemu_coroutine_self()) { | |
1922 | index = i; | |
1923 | break; | |
1924 | } | |
1925 | } | |
1926 | assert(index >= 0); | |
1927 | ||
1928 | s->running_coroutines++; | |
1929 | buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE); | |
1930 | ||
1931 | while (1) { | |
1932 | int n; | |
1933 | int64_t sector_num; | |
1934 | enum ImgConvertBlockStatus status; | |
ee5306d0 | 1935 | bool copy_range; |
2d9187bc PL |
1936 | |
1937 | qemu_co_mutex_lock(&s->lock); | |
1938 | if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) { | |
1939 | qemu_co_mutex_unlock(&s->lock); | |
b91127ed | 1940 | break; |
2d9187bc PL |
1941 | } |
1942 | n = convert_iteration_sectors(s, s->sector_num); | |
1943 | if (n < 0) { | |
1944 | qemu_co_mutex_unlock(&s->lock); | |
1945 | s->ret = n; | |
b91127ed | 1946 | break; |
2d9187bc PL |
1947 | } |
1948 | /* save current sector and allocation status to local variables */ | |
1949 | sector_num = s->sector_num; | |
1950 | status = s->status; | |
1951 | if (!s->min_sparse && s->status == BLK_ZERO) { | |
1952 | n = MIN(n, s->buf_sectors); | |
1953 | } | |
1954 | /* increment global sector counter so that other coroutines can | |
1955 | * already continue reading beyond this request */ | |
1956 | s->sector_num += n; | |
1957 | qemu_co_mutex_unlock(&s->lock); | |
1958 | ||
1959 | if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) { | |
1960 | s->allocated_done += n; | |
1961 | qemu_progress_print(100.0 * s->allocated_done / | |
1962 | s->allocated_sectors, 0); | |
1963 | } | |
1964 | ||
ee5306d0 FZ |
1965 | retry: |
1966 | copy_range = s->copy_range && s->status == BLK_DATA; | |
1967 | if (status == BLK_DATA && !copy_range) { | |
2d9187bc PL |
1968 | ret = convert_co_read(s, sector_num, n, buf); |
1969 | if (ret < 0) { | |
39f77cb6 EB |
1970 | error_report("error while reading at byte %lld: %s", |
1971 | sector_num * BDRV_SECTOR_SIZE, strerror(-ret)); | |
2d9187bc | 1972 | s->ret = ret; |
2d9187bc PL |
1973 | } |
1974 | } else if (!s->min_sparse && status == BLK_ZERO) { | |
1975 | status = BLK_DATA; | |
1976 | memset(buf, 0x00, n * BDRV_SECTOR_SIZE); | |
1977 | } | |
1978 | ||
1979 | if (s->wr_in_order) { | |
1980 | /* keep writes in order */ | |
b91127ed | 1981 | while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) { |
2d9187bc PL |
1982 | s->wait_sector_num[index] = sector_num; |
1983 | qemu_coroutine_yield(); | |
1984 | } | |
1985 | s->wait_sector_num[index] = -1; | |
1986 | } | |
1987 | ||
b91127ed | 1988 | if (s->ret == -EINPROGRESS) { |
ee5306d0 FZ |
1989 | if (copy_range) { |
1990 | ret = convert_co_copy_range(s, sector_num, n); | |
1991 | if (ret) { | |
1992 | s->copy_range = false; | |
1993 | goto retry; | |
1994 | } | |
1995 | } else { | |
1996 | ret = convert_co_write(s, sector_num, n, buf, status); | |
1997 | } | |
b91127ed | 1998 | if (ret < 0) { |
39f77cb6 EB |
1999 | error_report("error while writing at byte %lld: %s", |
2000 | sector_num * BDRV_SECTOR_SIZE, strerror(-ret)); | |
b91127ed AN |
2001 | s->ret = ret; |
2002 | } | |
2d9187bc PL |
2003 | } |
2004 | ||
2005 | if (s->wr_in_order) { | |
2006 | /* reenter the coroutine that might have waited | |
2007 | * for this write to complete */ | |
2008 | s->wr_offs = sector_num + n; | |
2009 | for (i = 0; i < s->num_coroutines; i++) { | |
2010 | if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) { | |
2011 | /* | |
2012 | * A -> B -> A cannot occur because A has | |
2013 | * s->wait_sector_num[i] == -1 during A -> B. Therefore | |
2014 | * B will never enter A during this time window. | |
2015 | */ | |
2016 | qemu_coroutine_enter(s->co[i]); | |
2017 | break; | |
2018 | } | |
2019 | } | |
2020 | } | |
2021 | } | |
2022 | ||
2d9187bc PL |
2023 | qemu_vfree(buf); |
2024 | s->co[index] = NULL; | |
2025 | s->running_coroutines--; | |
2026 | if (!s->running_coroutines && s->ret == -EINPROGRESS) { | |
2027 | /* the convert job finished successfully */ | |
2028 | s->ret = 0; | |
2029 | } | |
2030 | } | |
2031 | ||
2032 | static int convert_do_copy(ImgConvertState *s) | |
2033 | { | |
2034 | int ret, i, n; | |
2035 | int64_t sector_num = 0; | |
690c7301 KW |
2036 | |
2037 | /* Check whether we have zero initialisation or can get it efficiently */ | |
168468fe DE |
2038 | if (!s->has_zero_init && s->target_is_new && s->min_sparse && |
2039 | !s->target_has_backing) { | |
4d7c487e | 2040 | s->has_zero_init = bdrv_has_zero_init(blk_bs(s->target)); |
4d7c487e | 2041 | } |
690c7301 KW |
2042 | |
2043 | if (!s->has_zero_init && !s->target_has_backing && | |
2044 | bdrv_can_write_zeroes_with_unmap(blk_bs(s->target))) | |
2045 | { | |
c9fdcf20 | 2046 | ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK); |
690c7301 KW |
2047 | if (ret == 0) { |
2048 | s->has_zero_init = true; | |
2049 | } | |
2050 | } | |
2051 | ||
2052 | /* Allocate buffer for copied data. For compressed images, only one cluster | |
2053 | * can be copied at a time. */ | |
2054 | if (s->compressed) { | |
2055 | if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) { | |
2056 | error_report("invalid cluster size"); | |
2d9187bc | 2057 | return -EINVAL; |
690c7301 KW |
2058 | } |
2059 | s->buf_sectors = s->cluster_sectors; | |
2060 | } | |
690c7301 | 2061 | |
690c7301 KW |
2062 | while (sector_num < s->total_sectors) { |
2063 | n = convert_iteration_sectors(s, sector_num); | |
2064 | if (n < 0) { | |
2d9187bc | 2065 | return n; |
690c7301 | 2066 | } |
aad15de4 HR |
2067 | if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO)) |
2068 | { | |
690c7301 KW |
2069 | s->allocated_sectors += n; |
2070 | } | |
2071 | sector_num += n; | |
2072 | } | |
2073 | ||
2074 | /* Do the copy */ | |
690c7301 | 2075 | s->sector_next_status = 0; |
2d9187bc | 2076 | s->ret = -EINPROGRESS; |
690c7301 | 2077 | |
2d9187bc PL |
2078 | qemu_co_mutex_init(&s->lock); |
2079 | for (i = 0; i < s->num_coroutines; i++) { | |
2080 | s->co[i] = qemu_coroutine_create(convert_co_do_copy, s); | |
2081 | s->wait_sector_num[i] = -1; | |
2082 | qemu_coroutine_enter(s->co[i]); | |
2083 | } | |
690c7301 | 2084 | |
b91127ed | 2085 | while (s->running_coroutines) { |
2d9187bc | 2086 | main_loop_wait(false); |
690c7301 KW |
2087 | } |
2088 | ||
2d9187bc | 2089 | if (s->compressed && !s->ret) { |
690c7301 | 2090 | /* signal EOF to align */ |
fe5c1355 | 2091 | ret = blk_pwrite_compressed(s->target, 0, NULL, 0); |
690c7301 | 2092 | if (ret < 0) { |
2d9187bc | 2093 | return ret; |
690c7301 KW |
2094 | } |
2095 | } | |
2096 | ||
2d9187bc | 2097 | return s->ret; |
690c7301 KW |
2098 | } |
2099 | ||
6360ab27 PL |
2100 | #define MAX_BUF_SECTORS 32768 |
2101 | ||
ea2384d3 FB |
2102 | static int img_convert(int argc, char **argv) |
2103 | { | |
9fd77f99 | 2104 | int c, bs_i, flags, src_flags = 0; |
305b4c60 | 2105 | const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe", |
9fd77f99 PL |
2106 | *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL, |
2107 | *out_filename, *out_baseimg_param, *snapshot_name = NULL; | |
305b4c60 | 2108 | BlockDriver *drv = NULL, *proto_drv = NULL; |
faea38e7 | 2109 | BlockDriverInfo bdi; |
9fd77f99 PL |
2110 | BlockDriverState *out_bs; |
2111 | QemuOpts *opts = NULL, *sn_opts = NULL; | |
83d0521a | 2112 | QemuOptsList *create_opts = NULL; |
8d65a3cc | 2113 | QDict *open_opts = NULL; |
efa84d43 | 2114 | char *options = NULL; |
cc84d90f | 2115 | Error *local_err = NULL; |
3d96cb91 | 2116 | bool writethrough, src_writethrough, image_opts = false, |
305b4c60 | 2117 | skip_create = false, progress = false, tgt_image_opts = false; |
9fd77f99 | 2118 | int64_t ret = -EINVAL; |
335e9937 | 2119 | bool force_share = false; |
e11ce12f | 2120 | bool explict_min_sparse = false; |
9fd77f99 PL |
2121 | |
2122 | ImgConvertState s = (ImgConvertState) { | |
2123 | /* Need at least 4k of zeros for sparse detection */ | |
2124 | .min_sparse = 8, | |
e11ce12f | 2125 | .copy_range = false, |
9fd77f99 PL |
2126 | .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE, |
2127 | .wr_in_order = true, | |
2128 | .num_coroutines = 8, | |
2129 | }; | |
ea2384d3 | 2130 | |
ea2384d3 | 2131 | for(;;) { |
3babeb15 DB |
2132 | static const struct option long_options[] = { |
2133 | {"help", no_argument, 0, 'h'}, | |
2134 | {"object", required_argument, 0, OPTION_OBJECT}, | |
eb769f74 | 2135 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
335e9937 | 2136 | {"force-share", no_argument, 0, 'U'}, |
305b4c60 | 2137 | {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS}, |
8eaac025 | 2138 | {"salvage", no_argument, 0, OPTION_SALVAGE}, |
168468fe | 2139 | {"target-is-zero", no_argument, 0, OPTION_TARGET_IS_ZERO}, |
3babeb15 DB |
2140 | {0, 0, 0, 0} |
2141 | }; | |
e11ce12f | 2142 | c = getopt_long(argc, argv, ":hf:O:B:Cco:l:S:pt:T:qnm:WU", |
3babeb15 | 2143 | long_options, NULL); |
b8fb60da | 2144 | if (c == -1) { |
ea2384d3 | 2145 | break; |
b8fb60da | 2146 | } |
ea2384d3 | 2147 | switch(c) { |
c9192973 SH |
2148 | case ':': |
2149 | missing_argument(argv[optind - 1]); | |
2150 | break; | |
ef87394c | 2151 | case '?': |
c9192973 SH |
2152 | unrecognized_option(argv[optind - 1]); |
2153 | break; | |
ea2384d3 FB |
2154 | case 'h': |
2155 | help(); | |
2156 | break; | |
2157 | case 'f': | |
2158 | fmt = optarg; | |
2159 | break; | |
2160 | case 'O': | |
2161 | out_fmt = optarg; | |
2162 | break; | |
f58c7b35 TS |
2163 | case 'B': |
2164 | out_baseimg = optarg; | |
2165 | break; | |
e11ce12f FZ |
2166 | case 'C': |
2167 | s.copy_range = true; | |
2168 | break; | |
ea2384d3 | 2169 | case 'c': |
9fd77f99 | 2170 | s.compressed = true; |
ea2384d3 | 2171 | break; |
efa84d43 | 2172 | case 'o': |
6d2b5cba | 2173 | if (accumulate_options(&options, optarg) < 0) { |
64bb01aa | 2174 | goto fail_getopt; |
2dc8328b | 2175 | } |
efa84d43 | 2176 | break; |
ef80654d WX |
2177 | case 'l': |
2178 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { | |
70b94331 MA |
2179 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
2180 | optarg, false); | |
ef80654d WX |
2181 | if (!sn_opts) { |
2182 | error_report("Failed in parsing snapshot param '%s'", | |
2183 | optarg); | |
64bb01aa | 2184 | goto fail_getopt; |
ef80654d WX |
2185 | } |
2186 | } else { | |
2187 | snapshot_name = optarg; | |
2188 | } | |
2189 | break; | |
a22f123c KW |
2190 | case 'S': |
2191 | { | |
2192 | int64_t sval; | |
606caa0a | 2193 | |
43d589b0 EM |
2194 | sval = cvtnum("buffer size for sparse output", optarg); |
2195 | if (sval < 0) { | |
2196 | goto fail_getopt; | |
2197 | } else if (!QEMU_IS_ALIGNED(sval, BDRV_SECTOR_SIZE) || | |
6360ab27 PL |
2198 | sval / BDRV_SECTOR_SIZE > MAX_BUF_SECTORS) { |
2199 | error_report("Invalid buffer size for sparse output specified. " | |
2200 | "Valid sizes are multiples of %llu up to %llu. Select " | |
2201 | "0 to disable sparse detection (fully allocates output).", | |
2202 | BDRV_SECTOR_SIZE, MAX_BUF_SECTORS * BDRV_SECTOR_SIZE); | |
64bb01aa | 2203 | goto fail_getopt; |
a22f123c KW |
2204 | } |
2205 | ||
9fd77f99 | 2206 | s.min_sparse = sval / BDRV_SECTOR_SIZE; |
e11ce12f | 2207 | explict_min_sparse = true; |
a22f123c KW |
2208 | break; |
2209 | } | |
6b837bc4 | 2210 | case 'p': |
9fd77f99 | 2211 | progress = true; |
6b837bc4 | 2212 | break; |
661a0f71 FS |
2213 | case 't': |
2214 | cache = optarg; | |
2215 | break; | |
40055951 HR |
2216 | case 'T': |
2217 | src_cache = optarg; | |
2218 | break; | |
f382d43a | 2219 | case 'q': |
3d96cb91 | 2220 | s.quiet = true; |
f382d43a | 2221 | break; |
b2e10493 | 2222 | case 'n': |
9fd77f99 | 2223 | skip_create = true; |
b2e10493 | 2224 | break; |
2d9187bc | 2225 | case 'm': |
9fd77f99 PL |
2226 | if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) || |
2227 | s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) { | |
2d9187bc PL |
2228 | error_report("Invalid number of coroutines. Allowed number of" |
2229 | " coroutines is between 1 and %d", MAX_COROUTINES); | |
2d9187bc PL |
2230 | goto fail_getopt; |
2231 | } | |
2232 | break; | |
2233 | case 'W': | |
9fd77f99 | 2234 | s.wr_in_order = false; |
2d9187bc | 2235 | break; |
335e9937 FZ |
2236 | case 'U': |
2237 | force_share = true; | |
2238 | break; | |
3258b911 HR |
2239 | case OPTION_OBJECT: { |
2240 | QemuOpts *object_opts; | |
2241 | object_opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
2242 | optarg, true); | |
2243 | if (!object_opts) { | |
3babeb15 DB |
2244 | goto fail_getopt; |
2245 | } | |
2246 | break; | |
3258b911 | 2247 | } |
eb769f74 DB |
2248 | case OPTION_IMAGE_OPTS: |
2249 | image_opts = true; | |
2250 | break; | |
8eaac025 HR |
2251 | case OPTION_SALVAGE: |
2252 | s.salvage = true; | |
2253 | break; | |
305b4c60 DB |
2254 | case OPTION_TARGET_IMAGE_OPTS: |
2255 | tgt_image_opts = true; | |
2256 | break; | |
168468fe DE |
2257 | case OPTION_TARGET_IS_ZERO: |
2258 | /* | |
2259 | * The user asserting that the target is blank has the | |
2260 | * same effect as the target driver supporting zero | |
2261 | * initialisation. | |
2262 | */ | |
2263 | s.has_zero_init = true; | |
2264 | break; | |
ea2384d3 FB |
2265 | } |
2266 | } | |
3b46e624 | 2267 | |
305b4c60 DB |
2268 | if (!out_fmt && !tgt_image_opts) { |
2269 | out_fmt = "raw"; | |
2270 | } | |
2271 | ||
3babeb15 DB |
2272 | if (qemu_opts_foreach(&qemu_object_opts, |
2273 | user_creatable_add_opts_foreach, | |
c6e5cdfd | 2274 | qemu_img_object_print_help, &error_fatal)) { |
3babeb15 DB |
2275 | goto fail_getopt; |
2276 | } | |
2277 | ||
e11ce12f FZ |
2278 | if (s.compressed && s.copy_range) { |
2279 | error_report("Cannot enable copy offloading when -c is used"); | |
2280 | goto fail_getopt; | |
2281 | } | |
2282 | ||
2283 | if (explict_min_sparse && s.copy_range) { | |
2284 | error_report("Cannot enable copy offloading when -S is used"); | |
2285 | goto fail_getopt; | |
2286 | } | |
2287 | ||
8eaac025 HR |
2288 | if (s.copy_range && s.salvage) { |
2289 | error_report("Cannot use copy offloading in salvaging mode"); | |
2290 | goto fail_getopt; | |
2291 | } | |
2292 | ||
305b4c60 DB |
2293 | if (tgt_image_opts && !skip_create) { |
2294 | error_report("--target-image-opts requires use of -n flag"); | |
2295 | goto fail_getopt; | |
2296 | } | |
2297 | ||
ffd8e8ff KW |
2298 | if (skip_create && options) { |
2299 | warn_report("-o has no effect when skipping image creation"); | |
2300 | warn_report("This will become an error in future QEMU versions."); | |
2301 | } | |
2302 | ||
168468fe DE |
2303 | if (s.has_zero_init && !skip_create) { |
2304 | error_report("--target-is-zero requires use of -n flag"); | |
2305 | goto fail_getopt; | |
2306 | } | |
2307 | ||
9fd77f99 PL |
2308 | s.src_num = argc - optind - 1; |
2309 | out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL; | |
f58c7b35 | 2310 | |
2dc8328b | 2311 | if (options && has_help_option(options)) { |
305b4c60 DB |
2312 | if (out_fmt) { |
2313 | ret = print_block_option_help(out_filename, out_fmt); | |
2314 | goto fail_getopt; | |
2315 | } else { | |
2316 | error_report("Option help requires a format be specified"); | |
2317 | goto fail_getopt; | |
2318 | } | |
4ac8aacd JS |
2319 | } |
2320 | ||
9fd77f99 PL |
2321 | if (s.src_num < 1) { |
2322 | error_report("Must specify image file name"); | |
2323 | goto fail_getopt; | |
a283cb6e KW |
2324 | } |
2325 | ||
2326 | ||
9fd77f99 | 2327 | /* ret is still -EINVAL until here */ |
ce099547 | 2328 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); |
40055951 HR |
2329 | if (ret < 0) { |
2330 | error_report("Invalid source cache option: %s", src_cache); | |
9fd77f99 | 2331 | goto fail_getopt; |
40055951 HR |
2332 | } |
2333 | ||
9fd77f99 | 2334 | /* Initialize before goto out */ |
3d96cb91 | 2335 | if (s.quiet) { |
9fd77f99 PL |
2336 | progress = false; |
2337 | } | |
2338 | qemu_progress_init(progress, 1.0); | |
6b837bc4 JS |
2339 | qemu_progress_print(0, 100); |
2340 | ||
9fd77f99 PL |
2341 | s.src = g_new0(BlockBackend *, s.src_num); |
2342 | s.src_sectors = g_new(int64_t, s.src_num); | |
926c2d23 | 2343 | |
9fd77f99 PL |
2344 | for (bs_i = 0; bs_i < s.src_num; bs_i++) { |
2345 | s.src[bs_i] = img_open(image_opts, argv[optind + bs_i], | |
3d96cb91 | 2346 | fmt, src_flags, src_writethrough, s.quiet, |
335e9937 | 2347 | force_share); |
9fd77f99 | 2348 | if (!s.src[bs_i]) { |
c2abccec MK |
2349 | ret = -1; |
2350 | goto out; | |
2351 | } | |
9fd77f99 PL |
2352 | s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]); |
2353 | if (s.src_sectors[bs_i] < 0) { | |
52bf1e72 | 2354 | error_report("Could not get size of %s: %s", |
9fd77f99 | 2355 | argv[optind + bs_i], strerror(-s.src_sectors[bs_i])); |
52bf1e72 MA |
2356 | ret = -1; |
2357 | goto out; | |
2358 | } | |
9fd77f99 | 2359 | s.total_sectors += s.src_sectors[bs_i]; |
926c2d23 | 2360 | } |
ea2384d3 | 2361 | |
ef80654d | 2362 | if (sn_opts) { |
9fd77f99 | 2363 | bdrv_snapshot_load_tmp(blk_bs(s.src[0]), |
10d6eda1 PM |
2364 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), |
2365 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), | |
2366 | &local_err); | |
ef80654d | 2367 | } else if (snapshot_name != NULL) { |
9fd77f99 | 2368 | if (s.src_num > 1) { |
6daf194d | 2369 | error_report("No support for concatenating multiple snapshot"); |
51ef6727 | 2370 | ret = -1; |
2371 | goto out; | |
2372 | } | |
7b4c4781 | 2373 | |
9fd77f99 PL |
2374 | bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name, |
2375 | &local_err); | |
ef80654d | 2376 | } |
84d18f06 | 2377 | if (local_err) { |
c29b77f9 | 2378 | error_reportf_err(local_err, "Failed to load snapshot: "); |
ef80654d WX |
2379 | ret = -1; |
2380 | goto out; | |
51ef6727 | 2381 | } |
2382 | ||
305b4c60 DB |
2383 | if (!skip_create) { |
2384 | /* Find driver and parse its options */ | |
2385 | drv = bdrv_find_format(out_fmt); | |
2386 | if (!drv) { | |
2387 | error_report("Unknown file format '%s'", out_fmt); | |
2388 | ret = -1; | |
2389 | goto out; | |
2390 | } | |
efa84d43 | 2391 | |
305b4c60 DB |
2392 | proto_drv = bdrv_find_protocol(out_filename, true, &local_err); |
2393 | if (!proto_drv) { | |
2394 | error_report_err(local_err); | |
2395 | ret = -1; | |
2396 | goto out; | |
2397 | } | |
b50cbabc | 2398 | |
2e024cde HR |
2399 | if (!drv->create_opts) { |
2400 | error_report("Format driver '%s' does not support image creation", | |
2401 | drv->format_name); | |
2402 | ret = -1; | |
2403 | goto out; | |
2404 | } | |
f75613cf | 2405 | |
2e024cde HR |
2406 | if (!proto_drv->create_opts) { |
2407 | error_report("Protocol driver '%s' does not support image creation", | |
2408 | proto_drv->format_name); | |
2409 | ret = -1; | |
2410 | goto out; | |
2411 | } | |
f75613cf | 2412 | |
2e024cde HR |
2413 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
2414 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); | |
db08adf5 | 2415 | |
2e024cde | 2416 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
dc523cd3 MA |
2417 | if (options) { |
2418 | qemu_opts_do_parse(opts, options, NULL, &local_err); | |
2419 | if (local_err) { | |
97a2ca7a | 2420 | error_report_err(local_err); |
dc523cd3 MA |
2421 | ret = -1; |
2422 | goto out; | |
2423 | } | |
2e024cde | 2424 | } |
efa84d43 | 2425 | |
9fd77f99 | 2426 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512, |
39101f25 | 2427 | &error_abort); |
2e024cde HR |
2428 | ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL); |
2429 | if (ret < 0) { | |
2430 | goto out; | |
2431 | } | |
c2abccec | 2432 | } |
efa84d43 | 2433 | |
a18953fb | 2434 | /* Get backing file name if -o backing_file was used */ |
83d0521a | 2435 | out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); |
a18953fb | 2436 | if (out_baseimg_param) { |
83d0521a | 2437 | out_baseimg = out_baseimg_param; |
a18953fb | 2438 | } |
9fd77f99 | 2439 | s.target_has_backing = (bool) out_baseimg; |
a18953fb | 2440 | |
168468fe DE |
2441 | if (s.has_zero_init && s.target_has_backing) { |
2442 | error_report("Cannot use --target-is-zero when the destination " | |
2443 | "image has a backing file"); | |
2444 | goto out; | |
2445 | } | |
2446 | ||
48758a84 HR |
2447 | if (s.src_num > 1 && out_baseimg) { |
2448 | error_report("Having a backing file for the target makes no sense when " | |
2449 | "concatenating multiple input images"); | |
2450 | ret = -1; | |
2451 | goto out; | |
2452 | } | |
2453 | ||
efa84d43 | 2454 | /* Check if compression is supported */ |
9fd77f99 | 2455 | if (s.compressed) { |
83d0521a CL |
2456 | bool encryption = |
2457 | qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false); | |
0cb8d47b DB |
2458 | const char *encryptfmt = |
2459 | qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT); | |
83d0521a CL |
2460 | const char *preallocation = |
2461 | qemu_opt_get(opts, BLOCK_OPT_PREALLOC); | |
efa84d43 | 2462 | |
ac850bf0 | 2463 | if (drv && !block_driver_can_compress(drv)) { |
15654a6d | 2464 | error_report("Compression not supported for this file format"); |
c2abccec MK |
2465 | ret = -1; |
2466 | goto out; | |
efa84d43 KW |
2467 | } |
2468 | ||
0cb8d47b | 2469 | if (encryption || encryptfmt) { |
15654a6d JS |
2470 | error_report("Compression and encryption not supported at " |
2471 | "the same time"); | |
c2abccec MK |
2472 | ret = -1; |
2473 | goto out; | |
efa84d43 | 2474 | } |
41521fa4 | 2475 | |
83d0521a CL |
2476 | if (preallocation |
2477 | && strcmp(preallocation, "off")) | |
41521fa4 KW |
2478 | { |
2479 | error_report("Compression and preallocation not supported at " | |
2480 | "the same time"); | |
2481 | ret = -1; | |
2482 | goto out; | |
2483 | } | |
efa84d43 KW |
2484 | } |
2485 | ||
8d65a3cc DB |
2486 | /* |
2487 | * The later open call will need any decryption secrets, and | |
2488 | * bdrv_create() will purge "opts", so extract them now before | |
2489 | * they are lost. | |
2490 | */ | |
2491 | if (!skip_create) { | |
2492 | open_opts = qdict_new(); | |
2493 | qemu_opt_foreach(opts, img_add_key_secrets, open_opts, &error_abort); | |
2494 | } | |
2495 | ||
b2e10493 AD |
2496 | if (!skip_create) { |
2497 | /* Create the new image */ | |
c282e1fd | 2498 | ret = bdrv_create(drv, out_filename, opts, &local_err); |
b2e10493 | 2499 | if (ret < 0) { |
c29b77f9 MA |
2500 | error_reportf_err(local_err, "%s: error while converting %s: ", |
2501 | out_filename, out_fmt); | |
b2e10493 | 2502 | goto out; |
ea2384d3 FB |
2503 | } |
2504 | } | |
3b46e624 | 2505 | |
4d7c487e HR |
2506 | s.target_is_new = !skip_create; |
2507 | ||
9fd77f99 | 2508 | flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR; |
ce099547 | 2509 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
661a0f71 FS |
2510 | if (ret < 0) { |
2511 | error_report("Invalid cache option: %s", cache); | |
bb9cd2ee | 2512 | goto out; |
661a0f71 FS |
2513 | } |
2514 | ||
305b4c60 DB |
2515 | if (skip_create) { |
2516 | s.target = img_open(tgt_image_opts, out_filename, out_fmt, | |
3d96cb91 | 2517 | flags, writethrough, s.quiet, false); |
305b4c60 DB |
2518 | } else { |
2519 | /* TODO ultimately we should allow --target-image-opts | |
2520 | * to be used even when -n is not given. | |
2521 | * That has to wait for bdrv_create to be improved | |
2522 | * to allow filenames in option syntax | |
2523 | */ | |
8d65a3cc | 2524 | s.target = img_open_file(out_filename, open_opts, out_fmt, |
3d96cb91 | 2525 | flags, writethrough, s.quiet, false); |
8d65a3cc | 2526 | open_opts = NULL; /* blk_new_open will have freed it */ |
305b4c60 | 2527 | } |
9fd77f99 | 2528 | if (!s.target) { |
c2abccec MK |
2529 | ret = -1; |
2530 | goto out; | |
2531 | } | |
9fd77f99 | 2532 | out_bs = blk_bs(s.target); |
ea2384d3 | 2533 | |
ac850bf0 | 2534 | if (s.compressed && !block_driver_can_compress(out_bs->drv)) { |
305b4c60 DB |
2535 | error_report("Compression not supported for this file format"); |
2536 | ret = -1; | |
2537 | goto out; | |
2538 | } | |
2539 | ||
5def6b80 | 2540 | /* increase bufsectors from the default 4096 (2M) if opt_transfer |
6360ab27 PL |
2541 | * or discard_alignment of the out_bs is greater. Limit to |
2542 | * MAX_BUF_SECTORS as maximum which is currently 32768 (16MB). */ | |
2543 | s.buf_sectors = MIN(MAX_BUF_SECTORS, | |
9fd77f99 PL |
2544 | MAX(s.buf_sectors, |
2545 | MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS, | |
2546 | out_bs->bl.pdiscard_alignment >> | |
2547 | BDRV_SECTOR_BITS))); | |
f2521c90 | 2548 | |
8dcd3c9b PL |
2549 | /* try to align the write requests to the destination to avoid unnecessary |
2550 | * RMW cycles. */ | |
2551 | s.alignment = MAX(pow2floor(s.min_sparse), | |
2552 | DIV_ROUND_UP(out_bs->bl.request_alignment, | |
2553 | BDRV_SECTOR_SIZE)); | |
2554 | assert(is_power_of_2(s.alignment)); | |
2555 | ||
b2e10493 | 2556 | if (skip_create) { |
9fd77f99 | 2557 | int64_t output_sectors = blk_nb_sectors(s.target); |
43716fa8 | 2558 | if (output_sectors < 0) { |
eec5eb42 | 2559 | error_report("unable to get output image length: %s", |
43716fa8 | 2560 | strerror(-output_sectors)); |
b2e10493 AD |
2561 | ret = -1; |
2562 | goto out; | |
9fd77f99 | 2563 | } else if (output_sectors < s.total_sectors) { |
b2e10493 AD |
2564 | error_report("output file is smaller than input file"); |
2565 | ret = -1; | |
2566 | goto out; | |
2567 | } | |
2568 | } | |
2569 | ||
c69291e7 | 2570 | if (s.target_has_backing && s.target_is_new) { |
351c8eff HR |
2571 | /* Errors are treated as "backing length unknown" (which means |
2572 | * s.target_backing_sectors has to be negative, which it will | |
2573 | * be automatically). The backing file length is used only | |
2574 | * for optimizations, so such a case is not fatal. */ | |
2575 | s.target_backing_sectors = bdrv_nb_sectors(out_bs->backing->bs); | |
2576 | } else { | |
2577 | s.target_backing_sectors = -1; | |
2578 | } | |
2579 | ||
24f833cd PL |
2580 | ret = bdrv_get_info(out_bs, &bdi); |
2581 | if (ret < 0) { | |
9fd77f99 | 2582 | if (s.compressed) { |
15654a6d | 2583 | error_report("could not get block driver info"); |
c2abccec MK |
2584 | goto out; |
2585 | } | |
24f833cd | 2586 | } else { |
9fd77f99 PL |
2587 | s.compressed = s.compressed || bdi.needs_compressed_writes; |
2588 | s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE; | |
351c8eff | 2589 | s.unallocated_blocks_are_zero = bdi.unallocated_blocks_are_zero; |
9fd77f99 | 2590 | } |
802c3d4c | 2591 | |
9fd77f99 | 2592 | ret = convert_do_copy(&s); |
c2abccec | 2593 | out: |
13c28af8 PL |
2594 | if (!ret) { |
2595 | qemu_progress_print(100, 0); | |
2596 | } | |
6b837bc4 | 2597 | qemu_progress_end(); |
83d0521a CL |
2598 | qemu_opts_del(opts); |
2599 | qemu_opts_free(create_opts); | |
fbf28a43 | 2600 | qemu_opts_del(sn_opts); |
8d65a3cc | 2601 | qobject_unref(open_opts); |
9fd77f99 PL |
2602 | blk_unref(s.target); |
2603 | if (s.src) { | |
2604 | for (bs_i = 0; bs_i < s.src_num; bs_i++) { | |
2605 | blk_unref(s.src[bs_i]); | |
26f54e9a | 2606 | } |
9fd77f99 | 2607 | g_free(s.src); |
26f54e9a | 2608 | } |
9fd77f99 | 2609 | g_free(s.src_sectors); |
64bb01aa KW |
2610 | fail_getopt: |
2611 | g_free(options); | |
2612 | ||
9fd77f99 | 2613 | return !!ret; |
ea2384d3 FB |
2614 | } |
2615 | ||
57d1a2b6 | 2616 | |
faea38e7 FB |
2617 | static void dump_snapshots(BlockDriverState *bs) |
2618 | { | |
2619 | QEMUSnapshotInfo *sn_tab, *sn; | |
2620 | int nb_sns, i; | |
faea38e7 FB |
2621 | |
2622 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); | |
2623 | if (nb_sns <= 0) | |
2624 | return; | |
2625 | printf("Snapshot list:\n"); | |
e1ce7d74 | 2626 | bdrv_snapshot_dump(NULL); |
5b917044 | 2627 | printf("\n"); |
faea38e7 FB |
2628 | for(i = 0; i < nb_sns; i++) { |
2629 | sn = &sn_tab[i]; | |
e1ce7d74 | 2630 | bdrv_snapshot_dump(sn); |
5b917044 | 2631 | printf("\n"); |
faea38e7 | 2632 | } |
7267c094 | 2633 | g_free(sn_tab); |
faea38e7 FB |
2634 | } |
2635 | ||
9699bf0d SH |
2636 | static void dump_json_image_info_list(ImageInfoList *list) |
2637 | { | |
9699bf0d | 2638 | QString *str; |
9699bf0d | 2639 | QObject *obj; |
7d5e199a | 2640 | Visitor *v = qobject_output_visitor_new(&obj); |
3b098d56 EB |
2641 | |
2642 | visit_type_ImageInfoList(v, NULL, &list, &error_abort); | |
2643 | visit_complete(v, &obj); | |
9699bf0d SH |
2644 | str = qobject_to_json_pretty(obj); |
2645 | assert(str != NULL); | |
2646 | printf("%s\n", qstring_get_str(str)); | |
cb3e7f08 | 2647 | qobject_unref(obj); |
3b098d56 | 2648 | visit_free(v); |
cb3e7f08 | 2649 | qobject_unref(str); |
9699bf0d SH |
2650 | } |
2651 | ||
c054b3fd BC |
2652 | static void dump_json_image_info(ImageInfo *info) |
2653 | { | |
c054b3fd | 2654 | QString *str; |
c054b3fd | 2655 | QObject *obj; |
7d5e199a | 2656 | Visitor *v = qobject_output_visitor_new(&obj); |
3b098d56 EB |
2657 | |
2658 | visit_type_ImageInfo(v, NULL, &info, &error_abort); | |
2659 | visit_complete(v, &obj); | |
c054b3fd BC |
2660 | str = qobject_to_json_pretty(obj); |
2661 | assert(str != NULL); | |
2662 | printf("%s\n", qstring_get_str(str)); | |
cb3e7f08 | 2663 | qobject_unref(obj); |
3b098d56 | 2664 | visit_free(v); |
cb3e7f08 | 2665 | qobject_unref(str); |
c054b3fd BC |
2666 | } |
2667 | ||
9699bf0d SH |
2668 | static void dump_human_image_info_list(ImageInfoList *list) |
2669 | { | |
2670 | ImageInfoList *elem; | |
2671 | bool delim = false; | |
2672 | ||
2673 | for (elem = list; elem; elem = elem->next) { | |
2674 | if (delim) { | |
2675 | printf("\n"); | |
2676 | } | |
2677 | delim = true; | |
2678 | ||
e1ce7d74 | 2679 | bdrv_image_info_dump(elem->value); |
9699bf0d SH |
2680 | } |
2681 | } | |
2682 | ||
2683 | static gboolean str_equal_func(gconstpointer a, gconstpointer b) | |
2684 | { | |
2685 | return strcmp(a, b) == 0; | |
2686 | } | |
2687 | ||
2688 | /** | |
2689 | * Open an image file chain and return an ImageInfoList | |
2690 | * | |
2691 | * @filename: topmost image filename | |
2692 | * @fmt: topmost image format (may be NULL to autodetect) | |
2693 | * @chain: true - enumerate entire backing file chain | |
2694 | * false - only topmost image file | |
2695 | * | |
2696 | * Returns a list of ImageInfo objects or NULL if there was an error opening an | |
2697 | * image file. If there was an error a message will have been printed to | |
2698 | * stderr. | |
2699 | */ | |
eb769f74 DB |
2700 | static ImageInfoList *collect_image_info_list(bool image_opts, |
2701 | const char *filename, | |
9699bf0d | 2702 | const char *fmt, |
335e9937 | 2703 | bool chain, bool force_share) |
9699bf0d SH |
2704 | { |
2705 | ImageInfoList *head = NULL; | |
2706 | ImageInfoList **last = &head; | |
2707 | GHashTable *filenames; | |
43526ec8 | 2708 | Error *err = NULL; |
9699bf0d SH |
2709 | |
2710 | filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL); | |
2711 | ||
2712 | while (filename) { | |
26f54e9a | 2713 | BlockBackend *blk; |
9699bf0d SH |
2714 | BlockDriverState *bs; |
2715 | ImageInfo *info; | |
2716 | ImageInfoList *elem; | |
2717 | ||
2718 | if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) { | |
2719 | error_report("Backing file '%s' creates an infinite loop.", | |
2720 | filename); | |
2721 | goto err; | |
2722 | } | |
2723 | g_hash_table_insert(filenames, (gpointer)filename, NULL); | |
2724 | ||
efaa7c4e | 2725 | blk = img_open(image_opts, filename, fmt, |
335e9937 FZ |
2726 | BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false, |
2727 | force_share); | |
7e7d56d9 | 2728 | if (!blk) { |
9699bf0d SH |
2729 | goto err; |
2730 | } | |
7e7d56d9 | 2731 | bs = blk_bs(blk); |
9699bf0d | 2732 | |
43526ec8 | 2733 | bdrv_query_image_info(bs, &info, &err); |
84d18f06 | 2734 | if (err) { |
565f65d2 | 2735 | error_report_err(err); |
26f54e9a | 2736 | blk_unref(blk); |
43526ec8 | 2737 | goto err; |
fb0ed453 | 2738 | } |
9699bf0d SH |
2739 | |
2740 | elem = g_new0(ImageInfoList, 1); | |
2741 | elem->value = info; | |
2742 | *last = elem; | |
2743 | last = &elem->next; | |
2744 | ||
26f54e9a | 2745 | blk_unref(blk); |
9699bf0d | 2746 | |
0da7d13a | 2747 | /* Clear parameters that only apply to the topmost image */ |
9699bf0d | 2748 | filename = fmt = NULL; |
0da7d13a SH |
2749 | image_opts = false; |
2750 | ||
9699bf0d SH |
2751 | if (chain) { |
2752 | if (info->has_full_backing_filename) { | |
2753 | filename = info->full_backing_filename; | |
2754 | } else if (info->has_backing_filename) { | |
92d617ab JS |
2755 | error_report("Could not determine absolute backing filename," |
2756 | " but backing filename '%s' present", | |
2757 | info->backing_filename); | |
2758 | goto err; | |
9699bf0d SH |
2759 | } |
2760 | if (info->has_backing_filename_format) { | |
2761 | fmt = info->backing_filename_format; | |
2762 | } | |
2763 | } | |
2764 | } | |
2765 | g_hash_table_destroy(filenames); | |
2766 | return head; | |
2767 | ||
2768 | err: | |
2769 | qapi_free_ImageInfoList(head); | |
2770 | g_hash_table_destroy(filenames); | |
2771 | return NULL; | |
2772 | } | |
2773 | ||
c054b3fd BC |
2774 | static int img_info(int argc, char **argv) |
2775 | { | |
2776 | int c; | |
2777 | OutputFormat output_format = OFORMAT_HUMAN; | |
9699bf0d | 2778 | bool chain = false; |
c054b3fd | 2779 | const char *filename, *fmt, *output; |
9699bf0d | 2780 | ImageInfoList *list; |
eb769f74 | 2781 | bool image_opts = false; |
335e9937 | 2782 | bool force_share = false; |
c054b3fd | 2783 | |
ea2384d3 | 2784 | fmt = NULL; |
c054b3fd | 2785 | output = NULL; |
ea2384d3 | 2786 | for(;;) { |
c054b3fd BC |
2787 | int option_index = 0; |
2788 | static const struct option long_options[] = { | |
2789 | {"help", no_argument, 0, 'h'}, | |
2790 | {"format", required_argument, 0, 'f'}, | |
2791 | {"output", required_argument, 0, OPTION_OUTPUT}, | |
9699bf0d | 2792 | {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN}, |
3babeb15 | 2793 | {"object", required_argument, 0, OPTION_OBJECT}, |
eb769f74 | 2794 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
335e9937 | 2795 | {"force-share", no_argument, 0, 'U'}, |
c054b3fd BC |
2796 | {0, 0, 0, 0} |
2797 | }; | |
335e9937 | 2798 | c = getopt_long(argc, argv, ":f:hU", |
c054b3fd | 2799 | long_options, &option_index); |
b8fb60da | 2800 | if (c == -1) { |
ea2384d3 | 2801 | break; |
b8fb60da | 2802 | } |
ea2384d3 | 2803 | switch(c) { |
c9192973 SH |
2804 | case ':': |
2805 | missing_argument(argv[optind - 1]); | |
2806 | break; | |
ef87394c | 2807 | case '?': |
c9192973 SH |
2808 | unrecognized_option(argv[optind - 1]); |
2809 | break; | |
ea2384d3 FB |
2810 | case 'h': |
2811 | help(); | |
2812 | break; | |
2813 | case 'f': | |
2814 | fmt = optarg; | |
2815 | break; | |
335e9937 FZ |
2816 | case 'U': |
2817 | force_share = true; | |
2818 | break; | |
c054b3fd BC |
2819 | case OPTION_OUTPUT: |
2820 | output = optarg; | |
2821 | break; | |
9699bf0d SH |
2822 | case OPTION_BACKING_CHAIN: |
2823 | chain = true; | |
2824 | break; | |
3babeb15 DB |
2825 | case OPTION_OBJECT: { |
2826 | QemuOpts *opts; | |
2827 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
2828 | optarg, true); | |
2829 | if (!opts) { | |
2830 | return 1; | |
2831 | } | |
2832 | } break; | |
eb769f74 DB |
2833 | case OPTION_IMAGE_OPTS: |
2834 | image_opts = true; | |
2835 | break; | |
ea2384d3 FB |
2836 | } |
2837 | } | |
fc11eb26 | 2838 | if (optind != argc - 1) { |
ac1307ab | 2839 | error_exit("Expecting one image file name"); |
b8fb60da | 2840 | } |
ea2384d3 FB |
2841 | filename = argv[optind++]; |
2842 | ||
c054b3fd BC |
2843 | if (output && !strcmp(output, "json")) { |
2844 | output_format = OFORMAT_JSON; | |
2845 | } else if (output && !strcmp(output, "human")) { | |
2846 | output_format = OFORMAT_HUMAN; | |
2847 | } else if (output) { | |
2848 | error_report("--output must be used with human or json as argument."); | |
c2abccec MK |
2849 | return 1; |
2850 | } | |
c054b3fd | 2851 | |
3babeb15 DB |
2852 | if (qemu_opts_foreach(&qemu_object_opts, |
2853 | user_creatable_add_opts_foreach, | |
c6e5cdfd | 2854 | qemu_img_object_print_help, &error_fatal)) { |
3babeb15 DB |
2855 | return 1; |
2856 | } | |
2857 | ||
335e9937 FZ |
2858 | list = collect_image_info_list(image_opts, filename, fmt, chain, |
2859 | force_share); | |
9699bf0d | 2860 | if (!list) { |
c2abccec | 2861 | return 1; |
faea38e7 | 2862 | } |
c054b3fd | 2863 | |
c054b3fd BC |
2864 | switch (output_format) { |
2865 | case OFORMAT_HUMAN: | |
9699bf0d | 2866 | dump_human_image_info_list(list); |
c054b3fd BC |
2867 | break; |
2868 | case OFORMAT_JSON: | |
9699bf0d SH |
2869 | if (chain) { |
2870 | dump_json_image_info_list(list); | |
2871 | } else { | |
2872 | dump_json_image_info(list->value); | |
2873 | } | |
c054b3fd | 2874 | break; |
faea38e7 | 2875 | } |
c054b3fd | 2876 | |
9699bf0d | 2877 | qapi_free_ImageInfoList(list); |
ea2384d3 FB |
2878 | return 0; |
2879 | } | |
2880 | ||
30065d14 EB |
2881 | static int dump_map_entry(OutputFormat output_format, MapEntry *e, |
2882 | MapEntry *next) | |
4c93a13b PB |
2883 | { |
2884 | switch (output_format) { | |
2885 | case OFORMAT_HUMAN: | |
16b0d555 | 2886 | if (e->data && !e->has_offset) { |
4c93a13b | 2887 | error_report("File contains external, encrypted or compressed clusters."); |
30065d14 | 2888 | return -1; |
4c93a13b | 2889 | } |
16b0d555 | 2890 | if (e->data && !e->zero) { |
4c93a13b | 2891 | printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n", |
16b0d555 FZ |
2892 | e->start, e->length, |
2893 | e->has_offset ? e->offset : 0, | |
2894 | e->has_filename ? e->filename : ""); | |
4c93a13b PB |
2895 | } |
2896 | /* This format ignores the distinction between 0, ZERO and ZERO|DATA. | |
2897 | * Modify the flags here to allow more coalescing. | |
2898 | */ | |
16b0d555 FZ |
2899 | if (next && (!next->data || next->zero)) { |
2900 | next->data = false; | |
2901 | next->zero = true; | |
4c93a13b PB |
2902 | } |
2903 | break; | |
2904 | case OFORMAT_JSON: | |
e46c0b18 | 2905 | printf("{ \"start\": %"PRId64", \"length\": %"PRId64"," |
16b0d555 | 2906 | " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s", |
4c93a13b | 2907 | e->start, e->length, e->depth, |
16b0d555 FZ |
2908 | e->zero ? "true" : "false", |
2909 | e->data ? "true" : "false"); | |
2910 | if (e->has_offset) { | |
c745bfb4 | 2911 | printf(", \"offset\": %"PRId64"", e->offset); |
4c93a13b PB |
2912 | } |
2913 | putchar('}'); | |
2914 | ||
e46c0b18 EM |
2915 | if (next) { |
2916 | puts(","); | |
4c93a13b PB |
2917 | } |
2918 | break; | |
2919 | } | |
30065d14 | 2920 | return 0; |
4c93a13b PB |
2921 | } |
2922 | ||
5e344dd8 EB |
2923 | static int get_block_status(BlockDriverState *bs, int64_t offset, |
2924 | int64_t bytes, MapEntry *e) | |
4c93a13b | 2925 | { |
237d78f8 | 2926 | int ret; |
4c93a13b | 2927 | int depth; |
67a0fd2a | 2928 | BlockDriverState *file; |
2875645b | 2929 | bool has_offset; |
237d78f8 | 2930 | int64_t map; |
f30c66ba | 2931 | char *filename = NULL; |
4c93a13b PB |
2932 | |
2933 | /* As an optimization, we could cache the current range of unallocated | |
2934 | * clusters in each file of the chain, and avoid querying the same | |
2935 | * range repeatedly. | |
2936 | */ | |
2937 | ||
2938 | depth = 0; | |
2939 | for (;;) { | |
237d78f8 | 2940 | ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file); |
4c93a13b PB |
2941 | if (ret < 0) { |
2942 | return ret; | |
2943 | } | |
237d78f8 | 2944 | assert(bytes); |
4c93a13b PB |
2945 | if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) { |
2946 | break; | |
2947 | } | |
760e0063 | 2948 | bs = backing_bs(bs); |
4c93a13b PB |
2949 | if (bs == NULL) { |
2950 | ret = 0; | |
2951 | break; | |
2952 | } | |
2953 | ||
2954 | depth++; | |
2955 | } | |
2956 | ||
2875645b JS |
2957 | has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID); |
2958 | ||
f30c66ba HR |
2959 | if (file && has_offset) { |
2960 | bdrv_refresh_filename(file); | |
2961 | filename = file->filename; | |
2962 | } | |
2963 | ||
2875645b | 2964 | *e = (MapEntry) { |
5e344dd8 | 2965 | .start = offset, |
237d78f8 | 2966 | .length = bytes, |
2875645b JS |
2967 | .data = !!(ret & BDRV_BLOCK_DATA), |
2968 | .zero = !!(ret & BDRV_BLOCK_ZERO), | |
237d78f8 | 2969 | .offset = map, |
2875645b JS |
2970 | .has_offset = has_offset, |
2971 | .depth = depth, | |
f30c66ba HR |
2972 | .has_filename = filename, |
2973 | .filename = filename, | |
2875645b JS |
2974 | }; |
2975 | ||
4c93a13b PB |
2976 | return 0; |
2977 | } | |
2978 | ||
16b0d555 FZ |
2979 | static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next) |
2980 | { | |
2981 | if (curr->length == 0) { | |
2982 | return false; | |
2983 | } | |
2984 | if (curr->zero != next->zero || | |
2985 | curr->data != next->data || | |
2986 | curr->depth != next->depth || | |
2987 | curr->has_filename != next->has_filename || | |
2988 | curr->has_offset != next->has_offset) { | |
2989 | return false; | |
2990 | } | |
2991 | if (curr->has_filename && strcmp(curr->filename, next->filename)) { | |
2992 | return false; | |
2993 | } | |
2994 | if (curr->has_offset && curr->offset + curr->length != next->offset) { | |
2995 | return false; | |
2996 | } | |
2997 | return true; | |
2998 | } | |
2999 | ||
4c93a13b PB |
3000 | static int img_map(int argc, char **argv) |
3001 | { | |
3002 | int c; | |
3003 | OutputFormat output_format = OFORMAT_HUMAN; | |
26f54e9a | 3004 | BlockBackend *blk; |
4c93a13b PB |
3005 | BlockDriverState *bs; |
3006 | const char *filename, *fmt, *output; | |
3007 | int64_t length; | |
3008 | MapEntry curr = { .length = 0 }, next; | |
3009 | int ret = 0; | |
eb769f74 | 3010 | bool image_opts = false; |
335e9937 | 3011 | bool force_share = false; |
4c93a13b PB |
3012 | |
3013 | fmt = NULL; | |
3014 | output = NULL; | |
3015 | for (;;) { | |
3016 | int option_index = 0; | |
3017 | static const struct option long_options[] = { | |
3018 | {"help", no_argument, 0, 'h'}, | |
3019 | {"format", required_argument, 0, 'f'}, | |
3020 | {"output", required_argument, 0, OPTION_OUTPUT}, | |
3babeb15 | 3021 | {"object", required_argument, 0, OPTION_OBJECT}, |
eb769f74 | 3022 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
335e9937 | 3023 | {"force-share", no_argument, 0, 'U'}, |
4c93a13b PB |
3024 | {0, 0, 0, 0} |
3025 | }; | |
335e9937 | 3026 | c = getopt_long(argc, argv, ":f:hU", |
4c93a13b PB |
3027 | long_options, &option_index); |
3028 | if (c == -1) { | |
3029 | break; | |
3030 | } | |
3031 | switch (c) { | |
c9192973 SH |
3032 | case ':': |
3033 | missing_argument(argv[optind - 1]); | |
3034 | break; | |
4c93a13b | 3035 | case '?': |
c9192973 SH |
3036 | unrecognized_option(argv[optind - 1]); |
3037 | break; | |
4c93a13b PB |
3038 | case 'h': |
3039 | help(); | |
3040 | break; | |
3041 | case 'f': | |
3042 | fmt = optarg; | |
3043 | break; | |
335e9937 FZ |
3044 | case 'U': |
3045 | force_share = true; | |
3046 | break; | |
4c93a13b PB |
3047 | case OPTION_OUTPUT: |
3048 | output = optarg; | |
3049 | break; | |
3babeb15 DB |
3050 | case OPTION_OBJECT: { |
3051 | QemuOpts *opts; | |
3052 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
3053 | optarg, true); | |
3054 | if (!opts) { | |
3055 | return 1; | |
3056 | } | |
3057 | } break; | |
eb769f74 DB |
3058 | case OPTION_IMAGE_OPTS: |
3059 | image_opts = true; | |
3060 | break; | |
4c93a13b PB |
3061 | } |
3062 | } | |
ac1307ab FZ |
3063 | if (optind != argc - 1) { |
3064 | error_exit("Expecting one image file name"); | |
4c93a13b | 3065 | } |
ac1307ab | 3066 | filename = argv[optind]; |
4c93a13b PB |
3067 | |
3068 | if (output && !strcmp(output, "json")) { | |
3069 | output_format = OFORMAT_JSON; | |
3070 | } else if (output && !strcmp(output, "human")) { | |
3071 | output_format = OFORMAT_HUMAN; | |
3072 | } else if (output) { | |
3073 | error_report("--output must be used with human or json as argument."); | |
3074 | return 1; | |
3075 | } | |
3076 | ||
3babeb15 DB |
3077 | if (qemu_opts_foreach(&qemu_object_opts, |
3078 | user_creatable_add_opts_foreach, | |
c6e5cdfd | 3079 | qemu_img_object_print_help, &error_fatal)) { |
3babeb15 DB |
3080 | return 1; |
3081 | } | |
3082 | ||
335e9937 | 3083 | blk = img_open(image_opts, filename, fmt, 0, false, false, force_share); |
7e7d56d9 MA |
3084 | if (!blk) { |
3085 | return 1; | |
4c93a13b | 3086 | } |
7e7d56d9 | 3087 | bs = blk_bs(blk); |
4c93a13b PB |
3088 | |
3089 | if (output_format == OFORMAT_HUMAN) { | |
3090 | printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File"); | |
e46c0b18 EM |
3091 | } else if (output_format == OFORMAT_JSON) { |
3092 | putchar('['); | |
4c93a13b PB |
3093 | } |
3094 | ||
f1d3cd79 | 3095 | length = blk_getlength(blk); |
8f282e83 EM |
3096 | if (length < 0) { |
3097 | error_report("Failed to get size for '%s'", filename); | |
3098 | return 1; | |
3099 | } | |
3100 | ||
4c93a13b | 3101 | while (curr.start + curr.length < length) { |
5e344dd8 EB |
3102 | int64_t offset = curr.start + curr.length; |
3103 | int64_t n; | |
4c93a13b PB |
3104 | |
3105 | /* Probe up to 1 GiB at a time. */ | |
97ede57a | 3106 | n = MIN(1 * GiB, length - offset); |
5e344dd8 | 3107 | ret = get_block_status(bs, offset, n, &next); |
4c93a13b PB |
3108 | |
3109 | if (ret < 0) { | |
3110 | error_report("Could not read file metadata: %s", strerror(-ret)); | |
3111 | goto out; | |
3112 | } | |
3113 | ||
16b0d555 | 3114 | if (entry_mergeable(&curr, &next)) { |
4c93a13b PB |
3115 | curr.length += next.length; |
3116 | continue; | |
3117 | } | |
3118 | ||
3119 | if (curr.length > 0) { | |
30065d14 EB |
3120 | ret = dump_map_entry(output_format, &curr, &next); |
3121 | if (ret < 0) { | |
3122 | goto out; | |
3123 | } | |
4c93a13b PB |
3124 | } |
3125 | curr = next; | |
3126 | } | |
3127 | ||
30065d14 | 3128 | ret = dump_map_entry(output_format, &curr, NULL); |
e46c0b18 EM |
3129 | if (output_format == OFORMAT_JSON) { |
3130 | puts("]"); | |
3131 | } | |
4c93a13b PB |
3132 | |
3133 | out: | |
26f54e9a | 3134 | blk_unref(blk); |
4c93a13b PB |
3135 | return ret < 0; |
3136 | } | |
3137 | ||
f7b4a940 AL |
3138 | #define SNAPSHOT_LIST 1 |
3139 | #define SNAPSHOT_CREATE 2 | |
3140 | #define SNAPSHOT_APPLY 3 | |
3141 | #define SNAPSHOT_DELETE 4 | |
3142 | ||
153859be | 3143 | static int img_snapshot(int argc, char **argv) |
f7b4a940 | 3144 | { |
26f54e9a | 3145 | BlockBackend *blk; |
f7b4a940 AL |
3146 | BlockDriverState *bs; |
3147 | QEMUSnapshotInfo sn; | |
3148 | char *filename, *snapshot_name = NULL; | |
c2abccec | 3149 | int c, ret = 0, bdrv_oflags; |
f7b4a940 AL |
3150 | int action = 0; |
3151 | qemu_timeval tv; | |
f382d43a | 3152 | bool quiet = false; |
a89d89d3 | 3153 | Error *err = NULL; |
eb769f74 | 3154 | bool image_opts = false; |
335e9937 | 3155 | bool force_share = false; |
f7b4a940 | 3156 | |
ce099547 | 3157 | bdrv_oflags = BDRV_O_RDWR; |
f7b4a940 AL |
3158 | /* Parse commandline parameters */ |
3159 | for(;;) { | |
3babeb15 DB |
3160 | static const struct option long_options[] = { |
3161 | {"help", no_argument, 0, 'h'}, | |
3162 | {"object", required_argument, 0, OPTION_OBJECT}, | |
eb769f74 | 3163 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
335e9937 | 3164 | {"force-share", no_argument, 0, 'U'}, |
3babeb15 DB |
3165 | {0, 0, 0, 0} |
3166 | }; | |
335e9937 | 3167 | c = getopt_long(argc, argv, ":la:c:d:hqU", |
3babeb15 | 3168 | long_options, NULL); |
b8fb60da | 3169 | if (c == -1) { |
f7b4a940 | 3170 | break; |
b8fb60da | 3171 | } |
f7b4a940 | 3172 | switch(c) { |
c9192973 SH |
3173 | case ':': |
3174 | missing_argument(argv[optind - 1]); | |
3175 | break; | |
ef87394c | 3176 | case '?': |
c9192973 SH |
3177 | unrecognized_option(argv[optind - 1]); |
3178 | break; | |
f7b4a940 AL |
3179 | case 'h': |
3180 | help(); | |
153859be | 3181 | return 0; |
f7b4a940 AL |
3182 | case 'l': |
3183 | if (action) { | |
ac1307ab | 3184 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
153859be | 3185 | return 0; |
f7b4a940 AL |
3186 | } |
3187 | action = SNAPSHOT_LIST; | |
f5edb014 | 3188 | bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */ |
f7b4a940 AL |
3189 | break; |
3190 | case 'a': | |
3191 | if (action) { | |
ac1307ab | 3192 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
153859be | 3193 | return 0; |
f7b4a940 AL |
3194 | } |
3195 | action = SNAPSHOT_APPLY; | |
3196 | snapshot_name = optarg; | |
3197 | break; | |
3198 | case 'c': | |
3199 | if (action) { | |
ac1307ab | 3200 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
153859be | 3201 | return 0; |
f7b4a940 AL |
3202 | } |
3203 | action = SNAPSHOT_CREATE; | |
3204 | snapshot_name = optarg; | |
3205 | break; | |
3206 | case 'd': | |
3207 | if (action) { | |
ac1307ab | 3208 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
153859be | 3209 | return 0; |
f7b4a940 AL |
3210 | } |
3211 | action = SNAPSHOT_DELETE; | |
3212 | snapshot_name = optarg; | |
3213 | break; | |
f382d43a MR |
3214 | case 'q': |
3215 | quiet = true; | |
3216 | break; | |
335e9937 FZ |
3217 | case 'U': |
3218 | force_share = true; | |
3219 | break; | |
3babeb15 DB |
3220 | case OPTION_OBJECT: { |
3221 | QemuOpts *opts; | |
3222 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
3223 | optarg, true); | |
3224 | if (!opts) { | |
3225 | return 1; | |
3226 | } | |
3227 | } break; | |
eb769f74 DB |
3228 | case OPTION_IMAGE_OPTS: |
3229 | image_opts = true; | |
3230 | break; | |
f7b4a940 AL |
3231 | } |
3232 | } | |
3233 | ||
fc11eb26 | 3234 | if (optind != argc - 1) { |
ac1307ab | 3235 | error_exit("Expecting one image file name"); |
b8fb60da | 3236 | } |
f7b4a940 AL |
3237 | filename = argv[optind++]; |
3238 | ||
3babeb15 DB |
3239 | if (qemu_opts_foreach(&qemu_object_opts, |
3240 | user_creatable_add_opts_foreach, | |
c6e5cdfd | 3241 | qemu_img_object_print_help, &error_fatal)) { |
3babeb15 DB |
3242 | return 1; |
3243 | } | |
3244 | ||
f7b4a940 | 3245 | /* Open the image */ |
335e9937 FZ |
3246 | blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet, |
3247 | force_share); | |
7e7d56d9 MA |
3248 | if (!blk) { |
3249 | return 1; | |
c2abccec | 3250 | } |
7e7d56d9 | 3251 | bs = blk_bs(blk); |
f7b4a940 AL |
3252 | |
3253 | /* Perform the requested action */ | |
3254 | switch(action) { | |
3255 | case SNAPSHOT_LIST: | |
3256 | dump_snapshots(bs); | |
3257 | break; | |
3258 | ||
3259 | case SNAPSHOT_CREATE: | |
3260 | memset(&sn, 0, sizeof(sn)); | |
3261 | pstrcpy(sn.name, sizeof(sn.name), snapshot_name); | |
3262 | ||
3263 | qemu_gettimeofday(&tv); | |
3264 | sn.date_sec = tv.tv_sec; | |
3265 | sn.date_nsec = tv.tv_usec * 1000; | |
3266 | ||
3267 | ret = bdrv_snapshot_create(bs, &sn); | |
b8fb60da | 3268 | if (ret) { |
15654a6d | 3269 | error_report("Could not create snapshot '%s': %d (%s)", |
f7b4a940 | 3270 | snapshot_name, ret, strerror(-ret)); |
b8fb60da | 3271 | } |
f7b4a940 AL |
3272 | break; |
3273 | ||
3274 | case SNAPSHOT_APPLY: | |
0b62bcbc | 3275 | ret = bdrv_snapshot_goto(bs, snapshot_name, &err); |
b8fb60da | 3276 | if (ret) { |
0b62bcbc KW |
3277 | error_reportf_err(err, "Could not apply snapshot '%s': ", |
3278 | snapshot_name); | |
b8fb60da | 3279 | } |
f7b4a940 AL |
3280 | break; |
3281 | ||
3282 | case SNAPSHOT_DELETE: | |
8c04093c DHB |
3283 | ret = bdrv_snapshot_find(bs, &sn, snapshot_name); |
3284 | if (ret < 0) { | |
3285 | error_report("Could not delete snapshot '%s': snapshot not " | |
3286 | "found", snapshot_name); | |
a89d89d3 | 3287 | ret = 1; |
8c04093c DHB |
3288 | } else { |
3289 | ret = bdrv_snapshot_delete(bs, sn.id_str, sn.name, &err); | |
3290 | if (ret < 0) { | |
3291 | error_reportf_err(err, "Could not delete snapshot '%s': ", | |
3292 | snapshot_name); | |
3293 | ret = 1; | |
3294 | } | |
b8fb60da | 3295 | } |
f7b4a940 AL |
3296 | break; |
3297 | } | |
3298 | ||
3299 | /* Cleanup */ | |
26f54e9a | 3300 | blk_unref(blk); |
c2abccec MK |
3301 | if (ret) { |
3302 | return 1; | |
3303 | } | |
153859be | 3304 | return 0; |
f7b4a940 AL |
3305 | } |
3306 | ||
3e85c6fd KW |
3307 | static int img_rebase(int argc, char **argv) |
3308 | { | |
26f54e9a | 3309 | BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL; |
396374ca PB |
3310 | uint8_t *buf_old = NULL; |
3311 | uint8_t *buf_new = NULL; | |
863cc78f | 3312 | BlockDriverState *bs = NULL, *prefix_chain_bs = NULL; |
3e85c6fd | 3313 | char *filename; |
40055951 HR |
3314 | const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg; |
3315 | int c, flags, src_flags, ret; | |
ce099547 | 3316 | bool writethrough, src_writethrough; |
3e85c6fd | 3317 | int unsafe = 0; |
335e9937 | 3318 | bool force_share = false; |
6b837bc4 | 3319 | int progress = 0; |
f382d43a | 3320 | bool quiet = false; |
34b5d2c6 | 3321 | Error *local_err = NULL; |
eb769f74 | 3322 | bool image_opts = false; |
3e85c6fd KW |
3323 | |
3324 | /* Parse commandline parameters */ | |
e53dbee0 | 3325 | fmt = NULL; |
661a0f71 | 3326 | cache = BDRV_DEFAULT_CACHE; |
40055951 | 3327 | src_cache = BDRV_DEFAULT_CACHE; |
3e85c6fd KW |
3328 | out_baseimg = NULL; |
3329 | out_basefmt = NULL; | |
3e85c6fd | 3330 | for(;;) { |
3babeb15 DB |
3331 | static const struct option long_options[] = { |
3332 | {"help", no_argument, 0, 'h'}, | |
3333 | {"object", required_argument, 0, OPTION_OBJECT}, | |
eb769f74 | 3334 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
335e9937 | 3335 | {"force-share", no_argument, 0, 'U'}, |
3babeb15 DB |
3336 | {0, 0, 0, 0} |
3337 | }; | |
335e9937 | 3338 | c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU", |
3babeb15 | 3339 | long_options, NULL); |
b8fb60da | 3340 | if (c == -1) { |
3e85c6fd | 3341 | break; |
b8fb60da | 3342 | } |
3e85c6fd | 3343 | switch(c) { |
c9192973 SH |
3344 | case ':': |
3345 | missing_argument(argv[optind - 1]); | |
3346 | break; | |
ef87394c | 3347 | case '?': |
c9192973 SH |
3348 | unrecognized_option(argv[optind - 1]); |
3349 | break; | |
3e85c6fd KW |
3350 | case 'h': |
3351 | help(); | |
3352 | return 0; | |
e53dbee0 KW |
3353 | case 'f': |
3354 | fmt = optarg; | |
3355 | break; | |
3e85c6fd KW |
3356 | case 'F': |
3357 | out_basefmt = optarg; | |
3358 | break; | |
3359 | case 'b': | |
3360 | out_baseimg = optarg; | |
3361 | break; | |
3362 | case 'u': | |
3363 | unsafe = 1; | |
3364 | break; | |
6b837bc4 JS |
3365 | case 'p': |
3366 | progress = 1; | |
3367 | break; | |
661a0f71 FS |
3368 | case 't': |
3369 | cache = optarg; | |
3370 | break; | |
40055951 HR |
3371 | case 'T': |
3372 | src_cache = optarg; | |
3373 | break; | |
f382d43a MR |
3374 | case 'q': |
3375 | quiet = true; | |
3376 | break; | |
3babeb15 DB |
3377 | case OPTION_OBJECT: { |
3378 | QemuOpts *opts; | |
3379 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
3380 | optarg, true); | |
3381 | if (!opts) { | |
3382 | return 1; | |
3383 | } | |
3384 | } break; | |
eb769f74 DB |
3385 | case OPTION_IMAGE_OPTS: |
3386 | image_opts = true; | |
3387 | break; | |
335e9937 FZ |
3388 | case 'U': |
3389 | force_share = true; | |
3390 | break; | |
3e85c6fd KW |
3391 | } |
3392 | } | |
3393 | ||
f382d43a MR |
3394 | if (quiet) { |
3395 | progress = 0; | |
3396 | } | |
3397 | ||
ac1307ab FZ |
3398 | if (optind != argc - 1) { |
3399 | error_exit("Expecting one image file name"); | |
3400 | } | |
3401 | if (!unsafe && !out_baseimg) { | |
3402 | error_exit("Must specify backing file (-b) or use unsafe mode (-u)"); | |
b8fb60da | 3403 | } |
3e85c6fd KW |
3404 | filename = argv[optind++]; |
3405 | ||
3babeb15 DB |
3406 | if (qemu_opts_foreach(&qemu_object_opts, |
3407 | user_creatable_add_opts_foreach, | |
c6e5cdfd | 3408 | qemu_img_object_print_help, &error_fatal)) { |
3babeb15 DB |
3409 | return 1; |
3410 | } | |
3411 | ||
6b837bc4 JS |
3412 | qemu_progress_init(progress, 2.0); |
3413 | qemu_progress_print(0, 100); | |
3414 | ||
661a0f71 | 3415 | flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0); |
ce099547 | 3416 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
661a0f71 FS |
3417 | if (ret < 0) { |
3418 | error_report("Invalid cache option: %s", cache); | |
40ed35a3 | 3419 | goto out; |
661a0f71 FS |
3420 | } |
3421 | ||
ce099547 KW |
3422 | src_flags = 0; |
3423 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); | |
40055951 HR |
3424 | if (ret < 0) { |
3425 | error_report("Invalid source cache option: %s", src_cache); | |
40ed35a3 | 3426 | goto out; |
40055951 HR |
3427 | } |
3428 | ||
ce099547 KW |
3429 | /* The source files are opened read-only, don't care about WCE */ |
3430 | assert((src_flags & BDRV_O_RDWR) == 0); | |
3431 | (void) src_writethrough; | |
3432 | ||
3e85c6fd KW |
3433 | /* |
3434 | * Open the images. | |
3435 | * | |
3436 | * Ignore the old backing file for unsafe rebase in case we want to correct | |
3437 | * the reference to a renamed or moved backing file. | |
3438 | */ | |
335e9937 FZ |
3439 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
3440 | false); | |
7e7d56d9 | 3441 | if (!blk) { |
40ed35a3 SH |
3442 | ret = -1; |
3443 | goto out; | |
c2abccec | 3444 | } |
7e7d56d9 | 3445 | bs = blk_bs(blk); |
3e85c6fd | 3446 | |
3e85c6fd | 3447 | if (out_basefmt != NULL) { |
644483d9 | 3448 | if (bdrv_find_format(out_basefmt) == NULL) { |
15654a6d | 3449 | error_report("Invalid format name: '%s'", out_basefmt); |
c2abccec MK |
3450 | ret = -1; |
3451 | goto out; | |
3e85c6fd KW |
3452 | } |
3453 | } | |
3454 | ||
3455 | /* For safe rebasing we need to compare old and new backing file */ | |
40ed35a3 | 3456 | if (!unsafe) { |
644483d9 | 3457 | QDict *options = NULL; |
4ebe0617 | 3458 | BlockDriverState *base_bs = backing_bs(bs); |
644483d9 | 3459 | |
4ebe0617 | 3460 | if (base_bs) { |
d861ab3a KW |
3461 | blk_old_backing = blk_new(qemu_get_aio_context(), |
3462 | BLK_PERM_CONSISTENT_READ, | |
4ebe0617 SE |
3463 | BLK_PERM_ALL); |
3464 | ret = blk_insert_bs(blk_old_backing, base_bs, | |
3465 | &local_err); | |
3466 | if (ret < 0) { | |
35ddd930 | 3467 | error_reportf_err(local_err, |
4ebe0617 SE |
3468 | "Could not reuse old backing file '%s': ", |
3469 | base_bs->filename); | |
35ddd930 HR |
3470 | goto out; |
3471 | } | |
3472 | } else { | |
3473 | blk_old_backing = NULL; | |
3e85c6fd | 3474 | } |
644483d9 | 3475 | |
a616673d | 3476 | if (out_baseimg[0]) { |
d16699b6 HR |
3477 | const char *overlay_filename; |
3478 | char *out_real_path; | |
3479 | ||
335e9937 | 3480 | options = qdict_new(); |
644483d9 | 3481 | if (out_basefmt) { |
46f5ac20 | 3482 | qdict_put_str(options, "driver", out_basefmt); |
335e9937 FZ |
3483 | } |
3484 | if (force_share) { | |
3485 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); | |
644483d9 HR |
3486 | } |
3487 | ||
f30c66ba | 3488 | bdrv_refresh_filename(bs); |
d16699b6 HR |
3489 | overlay_filename = bs->exact_filename[0] ? bs->exact_filename |
3490 | : bs->filename; | |
645ae7d8 HR |
3491 | out_real_path = |
3492 | bdrv_get_full_backing_filename_from_filename(overlay_filename, | |
3493 | out_baseimg, | |
3494 | &local_err); | |
d16699b6 | 3495 | if (local_err) { |
f22356d9 | 3496 | qobject_unref(options); |
d16699b6 HR |
3497 | error_reportf_err(local_err, |
3498 | "Could not resolve backing filename: "); | |
3499 | ret = -1; | |
d16699b6 HR |
3500 | goto out; |
3501 | } | |
3502 | ||
863cc78f SE |
3503 | /* |
3504 | * Find out whether we rebase an image on top of a previous image | |
3505 | * in its chain. | |
3506 | */ | |
3507 | prefix_chain_bs = bdrv_find_backing_image(bs, out_real_path); | |
330c7295 | 3508 | if (prefix_chain_bs) { |
f22356d9 | 3509 | qobject_unref(options); |
330c7295 | 3510 | g_free(out_real_path); |
f22356d9 | 3511 | |
d861ab3a KW |
3512 | blk_new_backing = blk_new(qemu_get_aio_context(), |
3513 | BLK_PERM_CONSISTENT_READ, | |
330c7295 SE |
3514 | BLK_PERM_ALL); |
3515 | ret = blk_insert_bs(blk_new_backing, prefix_chain_bs, | |
3516 | &local_err); | |
3517 | if (ret < 0) { | |
3518 | error_reportf_err(local_err, | |
3519 | "Could not reuse backing file '%s': ", | |
3520 | out_baseimg); | |
3521 | goto out; | |
3522 | } | |
3523 | } else { | |
3524 | blk_new_backing = blk_new_open(out_real_path, NULL, | |
3525 | options, src_flags, &local_err); | |
3526 | g_free(out_real_path); | |
3527 | if (!blk_new_backing) { | |
3528 | error_reportf_err(local_err, | |
3529 | "Could not open new backing file '%s': ", | |
3530 | out_baseimg); | |
3531 | ret = -1; | |
3532 | goto out; | |
3533 | } | |
a616673d | 3534 | } |
3e85c6fd KW |
3535 | } |
3536 | } | |
3537 | ||
3538 | /* | |
3539 | * Check each unallocated cluster in the COW file. If it is unallocated, | |
3540 | * accesses go to the backing file. We must therefore compare this cluster | |
3541 | * in the old and new backing file, and if they differ we need to copy it | |
3542 | * from the old backing file into the COW file. | |
3543 | * | |
3544 | * If qemu-img crashes during this step, no harm is done. The content of | |
3545 | * the image is the same as the original one at any time. | |
3546 | */ | |
3547 | if (!unsafe) { | |
41536287 | 3548 | int64_t size; |
35ddd930 | 3549 | int64_t old_backing_size = 0; |
41536287 EB |
3550 | int64_t new_backing_size = 0; |
3551 | uint64_t offset; | |
3552 | int64_t n; | |
1f710495 | 3553 | float local_progress = 0; |
d6771bfa | 3554 | |
f1d3cd79 HR |
3555 | buf_old = blk_blockalign(blk, IO_BUF_SIZE); |
3556 | buf_new = blk_blockalign(blk, IO_BUF_SIZE); | |
3e85c6fd | 3557 | |
41536287 EB |
3558 | size = blk_getlength(blk); |
3559 | if (size < 0) { | |
52bf1e72 | 3560 | error_report("Could not get size of '%s': %s", |
41536287 | 3561 | filename, strerror(-size)); |
52bf1e72 MA |
3562 | ret = -1; |
3563 | goto out; | |
3564 | } | |
35ddd930 HR |
3565 | if (blk_old_backing) { |
3566 | old_backing_size = blk_getlength(blk_old_backing); | |
3567 | if (old_backing_size < 0) { | |
3568 | char backing_name[PATH_MAX]; | |
52bf1e72 | 3569 | |
35ddd930 HR |
3570 | bdrv_get_backing_filename(bs, backing_name, |
3571 | sizeof(backing_name)); | |
3572 | error_report("Could not get size of '%s': %s", | |
3573 | backing_name, strerror(-old_backing_size)); | |
3574 | ret = -1; | |
3575 | goto out; | |
3576 | } | |
52bf1e72 | 3577 | } |
f1d3cd79 | 3578 | if (blk_new_backing) { |
41536287 EB |
3579 | new_backing_size = blk_getlength(blk_new_backing); |
3580 | if (new_backing_size < 0) { | |
52bf1e72 | 3581 | error_report("Could not get size of '%s': %s", |
41536287 | 3582 | out_baseimg, strerror(-new_backing_size)); |
52bf1e72 MA |
3583 | ret = -1; |
3584 | goto out; | |
3585 | } | |
a616673d | 3586 | } |
3e85c6fd | 3587 | |
41536287 EB |
3588 | if (size != 0) { |
3589 | local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE)); | |
1f710495 KW |
3590 | } |
3591 | ||
41536287 | 3592 | for (offset = 0; offset < size; offset += n) { |
1c6e8779 HR |
3593 | bool buf_old_is_zero = false; |
3594 | ||
41536287 EB |
3595 | /* How many bytes can we handle with the next read? */ |
3596 | n = MIN(IO_BUF_SIZE, size - offset); | |
3e85c6fd KW |
3597 | |
3598 | /* If the cluster is allocated, we don't need to take action */ | |
41536287 | 3599 | ret = bdrv_is_allocated(bs, offset, n, &n); |
d663640c PB |
3600 | if (ret < 0) { |
3601 | error_report("error while reading image metadata: %s", | |
3602 | strerror(-ret)); | |
3603 | goto out; | |
3604 | } | |
cc60e327 | 3605 | if (ret) { |
3e85c6fd KW |
3606 | continue; |
3607 | } | |
3608 | ||
863cc78f SE |
3609 | if (prefix_chain_bs) { |
3610 | /* | |
3611 | * If cluster wasn't changed since prefix_chain, we don't need | |
3612 | * to take action | |
3613 | */ | |
3614 | ret = bdrv_is_allocated_above(backing_bs(bs), prefix_chain_bs, | |
170d3bd3 | 3615 | false, offset, n, &n); |
863cc78f SE |
3616 | if (ret < 0) { |
3617 | error_report("error while reading image metadata: %s", | |
3618 | strerror(-ret)); | |
3619 | goto out; | |
3620 | } | |
3621 | if (!ret) { | |
3622 | continue; | |
3623 | } | |
3624 | } | |
3625 | ||
87a1b3e3 KW |
3626 | /* |
3627 | * Read old and new backing file and take into consideration that | |
3628 | * backing files may be smaller than the COW image. | |
3629 | */ | |
41536287 EB |
3630 | if (offset >= old_backing_size) { |
3631 | memset(buf_old, 0, n); | |
1c6e8779 | 3632 | buf_old_is_zero = true; |
87a1b3e3 | 3633 | } else { |
41536287 EB |
3634 | if (offset + n > old_backing_size) { |
3635 | n = old_backing_size - offset; | |
87a1b3e3 KW |
3636 | } |
3637 | ||
41536287 | 3638 | ret = blk_pread(blk_old_backing, offset, buf_old, n); |
87a1b3e3 KW |
3639 | if (ret < 0) { |
3640 | error_report("error while reading from old backing file"); | |
3641 | goto out; | |
3642 | } | |
3e85c6fd | 3643 | } |
87a1b3e3 | 3644 | |
41536287 EB |
3645 | if (offset >= new_backing_size || !blk_new_backing) { |
3646 | memset(buf_new, 0, n); | |
87a1b3e3 | 3647 | } else { |
41536287 EB |
3648 | if (offset + n > new_backing_size) { |
3649 | n = new_backing_size - offset; | |
87a1b3e3 KW |
3650 | } |
3651 | ||
41536287 | 3652 | ret = blk_pread(blk_new_backing, offset, buf_new, n); |
87a1b3e3 KW |
3653 | if (ret < 0) { |
3654 | error_report("error while reading from new backing file"); | |
3655 | goto out; | |
3656 | } | |
3e85c6fd KW |
3657 | } |
3658 | ||
3659 | /* If they differ, we need to write to the COW file */ | |
3660 | uint64_t written = 0; | |
3661 | ||
41536287 | 3662 | while (written < n) { |
dc61cd3b | 3663 | int64_t pnum; |
3e85c6fd | 3664 | |
41536287 EB |
3665 | if (compare_buffers(buf_old + written, buf_new + written, |
3666 | n - written, &pnum)) | |
3e85c6fd | 3667 | { |
1c6e8779 HR |
3668 | if (buf_old_is_zero) { |
3669 | ret = blk_pwrite_zeroes(blk, offset + written, pnum, 0); | |
3670 | } else { | |
3671 | ret = blk_pwrite(blk, offset + written, | |
3672 | buf_old + written, pnum, 0); | |
3673 | } | |
3e85c6fd | 3674 | if (ret < 0) { |
15654a6d | 3675 | error_report("Error while writing to COW image: %s", |
3e85c6fd | 3676 | strerror(-ret)); |
c2abccec | 3677 | goto out; |
3e85c6fd KW |
3678 | } |
3679 | } | |
3680 | ||
3681 | written += pnum; | |
3682 | } | |
6b837bc4 | 3683 | qemu_progress_print(local_progress, 100); |
3e85c6fd KW |
3684 | } |
3685 | } | |
3686 | ||
3687 | /* | |
3688 | * Change the backing file. All clusters that are different from the old | |
3689 | * backing file are overwritten in the COW file now, so the visible content | |
3690 | * doesn't change when we switch the backing file. | |
3691 | */ | |
a616673d AB |
3692 | if (out_baseimg && *out_baseimg) { |
3693 | ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt); | |
3694 | } else { | |
3695 | ret = bdrv_change_backing_file(bs, NULL, NULL); | |
3696 | } | |
3697 | ||
3e85c6fd | 3698 | if (ret == -ENOSPC) { |
15654a6d JS |
3699 | error_report("Could not change the backing file to '%s': No " |
3700 | "space left in the file header", out_baseimg); | |
3e85c6fd | 3701 | } else if (ret < 0) { |
15654a6d | 3702 | error_report("Could not change the backing file to '%s': %s", |
3e85c6fd KW |
3703 | out_baseimg, strerror(-ret)); |
3704 | } | |
3705 | ||
6b837bc4 | 3706 | qemu_progress_print(100, 0); |
3e85c6fd KW |
3707 | /* |
3708 | * TODO At this point it is possible to check if any clusters that are | |
3709 | * allocated in the COW file are the same in the backing file. If so, they | |
3710 | * could be dropped from the COW file. Don't do this before switching the | |
3711 | * backing file, in case of a crash this would lead to corruption. | |
3712 | */ | |
c2abccec | 3713 | out: |
6b837bc4 | 3714 | qemu_progress_end(); |
3e85c6fd KW |
3715 | /* Cleanup */ |
3716 | if (!unsafe) { | |
26f54e9a | 3717 | blk_unref(blk_old_backing); |
26f54e9a | 3718 | blk_unref(blk_new_backing); |
3e85c6fd | 3719 | } |
396374ca PB |
3720 | qemu_vfree(buf_old); |
3721 | qemu_vfree(buf_new); | |
3e85c6fd | 3722 | |
26f54e9a | 3723 | blk_unref(blk); |
c2abccec MK |
3724 | if (ret) { |
3725 | return 1; | |
3726 | } | |
3e85c6fd KW |
3727 | return 0; |
3728 | } | |
3729 | ||
ae6b0ed6 SH |
3730 | static int img_resize(int argc, char **argv) |
3731 | { | |
6750e795 | 3732 | Error *err = NULL; |
ae6b0ed6 SH |
3733 | int c, ret, relative; |
3734 | const char *filename, *fmt, *size; | |
09c5c6de | 3735 | int64_t n, total_size, current_size; |
f382d43a | 3736 | bool quiet = false; |
26f54e9a | 3737 | BlockBackend *blk = NULL; |
dc5f690b | 3738 | PreallocMode prealloc = PREALLOC_MODE_OFF; |
20caf0f7 | 3739 | QemuOpts *param; |
3babeb15 | 3740 | |
20caf0f7 DXW |
3741 | static QemuOptsList resize_options = { |
3742 | .name = "resize_options", | |
3743 | .head = QTAILQ_HEAD_INITIALIZER(resize_options.head), | |
3744 | .desc = { | |
3745 | { | |
3746 | .name = BLOCK_OPT_SIZE, | |
3747 | .type = QEMU_OPT_SIZE, | |
3748 | .help = "Virtual disk size" | |
3749 | }, { | |
3750 | /* end of list */ | |
3751 | } | |
ae6b0ed6 | 3752 | }, |
ae6b0ed6 | 3753 | }; |
eb769f74 | 3754 | bool image_opts = false; |
4ffca890 | 3755 | bool shrink = false; |
ae6b0ed6 | 3756 | |
e80fec7f KW |
3757 | /* Remove size from argv manually so that negative numbers are not treated |
3758 | * as options by getopt. */ | |
3759 | if (argc < 3) { | |
ac1307ab | 3760 | error_exit("Not enough arguments"); |
e80fec7f KW |
3761 | return 1; |
3762 | } | |
3763 | ||
3764 | size = argv[--argc]; | |
3765 | ||
3766 | /* Parse getopt arguments */ | |
ae6b0ed6 SH |
3767 | fmt = NULL; |
3768 | for(;;) { | |
3babeb15 DB |
3769 | static const struct option long_options[] = { |
3770 | {"help", no_argument, 0, 'h'}, | |
3771 | {"object", required_argument, 0, OPTION_OBJECT}, | |
eb769f74 | 3772 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
dc5f690b | 3773 | {"preallocation", required_argument, 0, OPTION_PREALLOCATION}, |
4ffca890 | 3774 | {"shrink", no_argument, 0, OPTION_SHRINK}, |
3babeb15 DB |
3775 | {0, 0, 0, 0} |
3776 | }; | |
c9192973 | 3777 | c = getopt_long(argc, argv, ":f:hq", |
3babeb15 | 3778 | long_options, NULL); |
ae6b0ed6 SH |
3779 | if (c == -1) { |
3780 | break; | |
3781 | } | |
3782 | switch(c) { | |
c9192973 SH |
3783 | case ':': |
3784 | missing_argument(argv[optind - 1]); | |
3785 | break; | |
ef87394c | 3786 | case '?': |
c9192973 SH |
3787 | unrecognized_option(argv[optind - 1]); |
3788 | break; | |
ae6b0ed6 SH |
3789 | case 'h': |
3790 | help(); | |
3791 | break; | |
3792 | case 'f': | |
3793 | fmt = optarg; | |
3794 | break; | |
f382d43a MR |
3795 | case 'q': |
3796 | quiet = true; | |
3797 | break; | |
3babeb15 DB |
3798 | case OPTION_OBJECT: { |
3799 | QemuOpts *opts; | |
3800 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
3801 | optarg, true); | |
3802 | if (!opts) { | |
3803 | return 1; | |
3804 | } | |
3805 | } break; | |
eb769f74 DB |
3806 | case OPTION_IMAGE_OPTS: |
3807 | image_opts = true; | |
3808 | break; | |
dc5f690b | 3809 | case OPTION_PREALLOCATION: |
f7abe0ec | 3810 | prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg, |
06c60b6c | 3811 | PREALLOC_MODE__MAX, NULL); |
dc5f690b HR |
3812 | if (prealloc == PREALLOC_MODE__MAX) { |
3813 | error_report("Invalid preallocation mode '%s'", optarg); | |
3814 | return 1; | |
3815 | } | |
3816 | break; | |
4ffca890 PB |
3817 | case OPTION_SHRINK: |
3818 | shrink = true; | |
3819 | break; | |
ae6b0ed6 SH |
3820 | } |
3821 | } | |
fc11eb26 | 3822 | if (optind != argc - 1) { |
be8fbd47 | 3823 | error_exit("Expecting image file name and size"); |
ae6b0ed6 SH |
3824 | } |
3825 | filename = argv[optind++]; | |
ae6b0ed6 | 3826 | |
3babeb15 DB |
3827 | if (qemu_opts_foreach(&qemu_object_opts, |
3828 | user_creatable_add_opts_foreach, | |
c6e5cdfd | 3829 | qemu_img_object_print_help, &error_fatal)) { |
3babeb15 DB |
3830 | return 1; |
3831 | } | |
3832 | ||
ae6b0ed6 SH |
3833 | /* Choose grow, shrink, or absolute resize mode */ |
3834 | switch (size[0]) { | |
3835 | case '+': | |
3836 | relative = 1; | |
3837 | size++; | |
3838 | break; | |
3839 | case '-': | |
3840 | relative = -1; | |
3841 | size++; | |
3842 | break; | |
3843 | default: | |
3844 | relative = 0; | |
3845 | break; | |
3846 | } | |
3847 | ||
3848 | /* Parse size */ | |
87ea75d5 | 3849 | param = qemu_opts_create(&resize_options, NULL, 0, &error_abort); |
f43e47db | 3850 | qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err); |
6750e795 MA |
3851 | if (err) { |
3852 | error_report_err(err); | |
2a81998a | 3853 | ret = -1; |
20caf0f7 | 3854 | qemu_opts_del(param); |
2a81998a | 3855 | goto out; |
ae6b0ed6 | 3856 | } |
20caf0f7 DXW |
3857 | n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0); |
3858 | qemu_opts_del(param); | |
ae6b0ed6 | 3859 | |
efaa7c4e | 3860 | blk = img_open(image_opts, filename, fmt, |
335e9937 FZ |
3861 | BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet, |
3862 | false); | |
7e7d56d9 | 3863 | if (!blk) { |
2a81998a JS |
3864 | ret = -1; |
3865 | goto out; | |
c2abccec | 3866 | } |
ae6b0ed6 | 3867 | |
dc5f690b HR |
3868 | current_size = blk_getlength(blk); |
3869 | if (current_size < 0) { | |
3870 | error_report("Failed to inquire current image length: %s", | |
3871 | strerror(-current_size)); | |
3872 | ret = -1; | |
3873 | goto out; | |
3874 | } | |
3875 | ||
ae6b0ed6 | 3876 | if (relative) { |
dc5f690b | 3877 | total_size = current_size + n * relative; |
ae6b0ed6 SH |
3878 | } else { |
3879 | total_size = n; | |
3880 | } | |
3881 | if (total_size <= 0) { | |
15654a6d | 3882 | error_report("New image size must be positive"); |
c2abccec MK |
3883 | ret = -1; |
3884 | goto out; | |
ae6b0ed6 SH |
3885 | } |
3886 | ||
dc5f690b HR |
3887 | if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) { |
3888 | error_report("Preallocation can only be used for growing images"); | |
3889 | ret = -1; | |
3890 | goto out; | |
3891 | } | |
3892 | ||
4ffca890 PB |
3893 | if (total_size < current_size && !shrink) { |
3894 | warn_report("Shrinking an image will delete all data beyond the " | |
3895 | "shrunken image's end. Before performing such an " | |
3896 | "operation, make sure there is no important data there."); | |
3897 | ||
3898 | if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") != 0) { | |
3899 | error_report( | |
3900 | "Use the --shrink option to perform a shrink operation."); | |
3901 | ret = -1; | |
3902 | goto out; | |
3903 | } else { | |
3904 | warn_report("Using the --shrink option will suppress this message. " | |
3905 | "Note that future versions of qemu-img may refuse to " | |
3906 | "shrink images without this option."); | |
3907 | } | |
3908 | } | |
3909 | ||
e8d04f92 HR |
3910 | /* |
3911 | * The user expects the image to have the desired size after | |
3912 | * resizing, so pass @exact=true. It is of no use to report | |
3913 | * success when the image has not actually been resized. | |
3914 | */ | |
8c6242b6 | 3915 | ret = blk_truncate(blk, total_size, true, prealloc, 0, &err); |
09c5c6de HR |
3916 | if (!ret) { |
3917 | qprintf(quiet, "Image resized.\n"); | |
3918 | } else { | |
ed3d2ec9 | 3919 | error_report_err(err); |
ae6b0ed6 | 3920 | } |
c2abccec | 3921 | out: |
26f54e9a | 3922 | blk_unref(blk); |
c2abccec MK |
3923 | if (ret) { |
3924 | return 1; | |
3925 | } | |
ae6b0ed6 SH |
3926 | return 0; |
3927 | } | |
3928 | ||
76a3a34d | 3929 | static void amend_status_cb(BlockDriverState *bs, |
8b13976d HR |
3930 | int64_t offset, int64_t total_work_size, |
3931 | void *opaque) | |
76a3a34d HR |
3932 | { |
3933 | qemu_progress_print(100.f * offset / total_work_size, 0); | |
3934 | } | |
3935 | ||
51641351 HR |
3936 | static int print_amend_option_help(const char *format) |
3937 | { | |
3938 | BlockDriver *drv; | |
3939 | ||
3940 | /* Find driver and parse its options */ | |
3941 | drv = bdrv_find_format(format); | |
3942 | if (!drv) { | |
3943 | error_report("Unknown file format '%s'", format); | |
3944 | return 1; | |
3945 | } | |
3946 | ||
3947 | if (!drv->bdrv_amend_options) { | |
3948 | error_report("Format driver '%s' does not support option amendment", | |
3949 | format); | |
3950 | return 1; | |
3951 | } | |
3952 | ||
3953 | /* Every driver supporting amendment must have create_opts */ | |
3954 | assert(drv->create_opts); | |
3955 | ||
3956 | printf("Creation options for '%s':\n", format); | |
63898712 | 3957 | qemu_opts_print_help(drv->create_opts, false); |
51641351 HR |
3958 | printf("\nNote that not all of these options may be amendable.\n"); |
3959 | return 0; | |
3960 | } | |
3961 | ||
6f176b48 HR |
3962 | static int img_amend(int argc, char **argv) |
3963 | { | |
dc523cd3 | 3964 | Error *err = NULL; |
6f176b48 HR |
3965 | int c, ret = 0; |
3966 | char *options = NULL; | |
83d0521a CL |
3967 | QemuOptsList *create_opts = NULL; |
3968 | QemuOpts *opts = NULL; | |
bd39e6ed HR |
3969 | const char *fmt = NULL, *filename, *cache; |
3970 | int flags; | |
ce099547 | 3971 | bool writethrough; |
76a3a34d | 3972 | bool quiet = false, progress = false; |
26f54e9a | 3973 | BlockBackend *blk = NULL; |
6f176b48 | 3974 | BlockDriverState *bs = NULL; |
eb769f74 | 3975 | bool image_opts = false; |
6f176b48 | 3976 | |
bd39e6ed | 3977 | cache = BDRV_DEFAULT_CACHE; |
6f176b48 | 3978 | for (;;) { |
3babeb15 DB |
3979 | static const struct option long_options[] = { |
3980 | {"help", no_argument, 0, 'h'}, | |
3981 | {"object", required_argument, 0, OPTION_OBJECT}, | |
eb769f74 | 3982 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
3babeb15 DB |
3983 | {0, 0, 0, 0} |
3984 | }; | |
c9192973 | 3985 | c = getopt_long(argc, argv, ":ho:f:t:pq", |
3babeb15 | 3986 | long_options, NULL); |
6f176b48 HR |
3987 | if (c == -1) { |
3988 | break; | |
3989 | } | |
3990 | ||
3991 | switch (c) { | |
c9192973 SH |
3992 | case ':': |
3993 | missing_argument(argv[optind - 1]); | |
3994 | break; | |
f7077624 | 3995 | case '?': |
c9192973 SH |
3996 | unrecognized_option(argv[optind - 1]); |
3997 | break; | |
3998 | case 'h': | |
f7077624 SH |
3999 | help(); |
4000 | break; | |
4001 | case 'o': | |
6d2b5cba | 4002 | if (accumulate_options(&options, optarg) < 0) { |
f7077624 SH |
4003 | ret = -1; |
4004 | goto out_no_progress; | |
4005 | } | |
f7077624 SH |
4006 | break; |
4007 | case 'f': | |
4008 | fmt = optarg; | |
4009 | break; | |
4010 | case 't': | |
4011 | cache = optarg; | |
4012 | break; | |
4013 | case 'p': | |
4014 | progress = true; | |
4015 | break; | |
4016 | case 'q': | |
4017 | quiet = true; | |
4018 | break; | |
4019 | case OPTION_OBJECT: | |
4020 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
4021 | optarg, true); | |
4022 | if (!opts) { | |
4023 | ret = -1; | |
4024 | goto out_no_progress; | |
4025 | } | |
4026 | break; | |
4027 | case OPTION_IMAGE_OPTS: | |
4028 | image_opts = true; | |
4029 | break; | |
6f176b48 HR |
4030 | } |
4031 | } | |
4032 | ||
a283cb6e | 4033 | if (!options) { |
ac1307ab | 4034 | error_exit("Must specify options (-o)"); |
6f176b48 HR |
4035 | } |
4036 | ||
3babeb15 DB |
4037 | if (qemu_opts_foreach(&qemu_object_opts, |
4038 | user_creatable_add_opts_foreach, | |
c6e5cdfd | 4039 | qemu_img_object_print_help, &error_fatal)) { |
3babeb15 DB |
4040 | ret = -1; |
4041 | goto out_no_progress; | |
4042 | } | |
4043 | ||
76a3a34d HR |
4044 | if (quiet) { |
4045 | progress = false; | |
4046 | } | |
4047 | qemu_progress_init(progress, 1.0); | |
4048 | ||
a283cb6e KW |
4049 | filename = (optind == argc - 1) ? argv[argc - 1] : NULL; |
4050 | if (fmt && has_help_option(options)) { | |
4051 | /* If a format is explicitly specified (and possibly no filename is | |
4052 | * given), print option help here */ | |
51641351 | 4053 | ret = print_amend_option_help(fmt); |
a283cb6e | 4054 | goto out; |
6f176b48 HR |
4055 | } |
4056 | ||
a283cb6e | 4057 | if (optind != argc - 1) { |
b2f27e44 HR |
4058 | error_report("Expecting one image file name"); |
4059 | ret = -1; | |
4060 | goto out; | |
a283cb6e | 4061 | } |
6f176b48 | 4062 | |
ce099547 KW |
4063 | flags = BDRV_O_RDWR; |
4064 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); | |
bd39e6ed HR |
4065 | if (ret < 0) { |
4066 | error_report("Invalid cache option: %s", cache); | |
4067 | goto out; | |
4068 | } | |
4069 | ||
335e9937 FZ |
4070 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
4071 | false); | |
7e7d56d9 | 4072 | if (!blk) { |
6f176b48 HR |
4073 | ret = -1; |
4074 | goto out; | |
4075 | } | |
7e7d56d9 | 4076 | bs = blk_bs(blk); |
6f176b48 HR |
4077 | |
4078 | fmt = bs->drv->format_name; | |
4079 | ||
626f84f3 | 4080 | if (has_help_option(options)) { |
a283cb6e | 4081 | /* If the format was auto-detected, print option help here */ |
51641351 | 4082 | ret = print_amend_option_help(fmt); |
6f176b48 HR |
4083 | goto out; |
4084 | } | |
4085 | ||
1f996683 HR |
4086 | if (!bs->drv->bdrv_amend_options) { |
4087 | error_report("Format driver '%s' does not support option amendment", | |
b2439d26 HR |
4088 | fmt); |
4089 | ret = -1; | |
4090 | goto out; | |
4091 | } | |
4092 | ||
1f996683 HR |
4093 | /* Every driver supporting amendment must have create_opts */ |
4094 | assert(bs->drv->create_opts); | |
4095 | ||
c282e1fd | 4096 | create_opts = qemu_opts_append(create_opts, bs->drv->create_opts); |
83d0521a | 4097 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
ece9086e PB |
4098 | qemu_opts_do_parse(opts, options, NULL, &err); |
4099 | if (err) { | |
4100 | error_report_err(err); | |
4101 | ret = -1; | |
4102 | goto out; | |
6f176b48 HR |
4103 | } |
4104 | ||
76a3a34d HR |
4105 | /* In case the driver does not call amend_status_cb() */ |
4106 | qemu_progress_print(0.f, 0); | |
d1402b50 | 4107 | ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL, &err); |
76a3a34d | 4108 | qemu_progress_print(100.f, 0); |
6f176b48 | 4109 | if (ret < 0) { |
d1402b50 | 4110 | error_report_err(err); |
6f176b48 HR |
4111 | goto out; |
4112 | } | |
4113 | ||
4114 | out: | |
76a3a34d HR |
4115 | qemu_progress_end(); |
4116 | ||
e814dffc | 4117 | out_no_progress: |
26f54e9a | 4118 | blk_unref(blk); |
83d0521a CL |
4119 | qemu_opts_del(opts); |
4120 | qemu_opts_free(create_opts); | |
626f84f3 KW |
4121 | g_free(options); |
4122 | ||
6f176b48 HR |
4123 | if (ret) { |
4124 | return 1; | |
4125 | } | |
4126 | return 0; | |
4127 | } | |
4128 | ||
b6133b8c KW |
4129 | typedef struct BenchData { |
4130 | BlockBackend *blk; | |
4131 | uint64_t image_size; | |
b6495fa8 | 4132 | bool write; |
b6133b8c | 4133 | int bufsize; |
83de9be0 | 4134 | int step; |
b6133b8c KW |
4135 | int nrreq; |
4136 | int n; | |
55d539c8 KW |
4137 | int flush_interval; |
4138 | bool drain_on_flush; | |
b6133b8c KW |
4139 | uint8_t *buf; |
4140 | QEMUIOVector *qiov; | |
4141 | ||
4142 | int in_flight; | |
55d539c8 | 4143 | bool in_flush; |
b6133b8c KW |
4144 | uint64_t offset; |
4145 | } BenchData; | |
4146 | ||
55d539c8 KW |
4147 | static void bench_undrained_flush_cb(void *opaque, int ret) |
4148 | { | |
4149 | if (ret < 0) { | |
df3c286c | 4150 | error_report("Failed flush request: %s", strerror(-ret)); |
55d539c8 KW |
4151 | exit(EXIT_FAILURE); |
4152 | } | |
4153 | } | |
4154 | ||
b6133b8c KW |
4155 | static void bench_cb(void *opaque, int ret) |
4156 | { | |
4157 | BenchData *b = opaque; | |
4158 | BlockAIOCB *acb; | |
4159 | ||
4160 | if (ret < 0) { | |
df3c286c | 4161 | error_report("Failed request: %s", strerror(-ret)); |
b6133b8c KW |
4162 | exit(EXIT_FAILURE); |
4163 | } | |
55d539c8 KW |
4164 | |
4165 | if (b->in_flush) { | |
4166 | /* Just finished a flush with drained queue: Start next requests */ | |
4167 | assert(b->in_flight == 0); | |
4168 | b->in_flush = false; | |
4169 | } else if (b->in_flight > 0) { | |
4170 | int remaining = b->n - b->in_flight; | |
4171 | ||
b6133b8c KW |
4172 | b->n--; |
4173 | b->in_flight--; | |
55d539c8 KW |
4174 | |
4175 | /* Time for flush? Drain queue if requested, then flush */ | |
4176 | if (b->flush_interval && remaining % b->flush_interval == 0) { | |
4177 | if (!b->in_flight || !b->drain_on_flush) { | |
4178 | BlockCompletionFunc *cb; | |
4179 | ||
4180 | if (b->drain_on_flush) { | |
4181 | b->in_flush = true; | |
4182 | cb = bench_cb; | |
4183 | } else { | |
4184 | cb = bench_undrained_flush_cb; | |
4185 | } | |
4186 | ||
4187 | acb = blk_aio_flush(b->blk, cb, b); | |
4188 | if (!acb) { | |
4189 | error_report("Failed to issue flush request"); | |
4190 | exit(EXIT_FAILURE); | |
4191 | } | |
4192 | } | |
4193 | if (b->drain_on_flush) { | |
4194 | return; | |
4195 | } | |
4196 | } | |
b6133b8c KW |
4197 | } |
4198 | ||
4199 | while (b->n > b->in_flight && b->in_flight < b->nrreq) { | |
4baaa8c3 PB |
4200 | int64_t offset = b->offset; |
4201 | /* blk_aio_* might look for completed I/Os and kick bench_cb | |
4202 | * again, so make sure this operation is counted by in_flight | |
4203 | * and b->offset is ready for the next submission. | |
4204 | */ | |
4205 | b->in_flight++; | |
4206 | b->offset += b->step; | |
4207 | b->offset %= b->image_size; | |
b6495fa8 | 4208 | if (b->write) { |
4baaa8c3 | 4209 | acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b); |
b6495fa8 | 4210 | } else { |
4baaa8c3 | 4211 | acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b); |
b6495fa8 | 4212 | } |
b6133b8c KW |
4213 | if (!acb) { |
4214 | error_report("Failed to issue request"); | |
4215 | exit(EXIT_FAILURE); | |
4216 | } | |
b6133b8c KW |
4217 | } |
4218 | } | |
4219 | ||
4220 | static int img_bench(int argc, char **argv) | |
4221 | { | |
4222 | int c, ret = 0; | |
4223 | const char *fmt = NULL, *filename; | |
4224 | bool quiet = false; | |
4225 | bool image_opts = false; | |
b6495fa8 | 4226 | bool is_write = false; |
b6133b8c KW |
4227 | int count = 75000; |
4228 | int depth = 64; | |
d3199a31 | 4229 | int64_t offset = 0; |
b6133b8c | 4230 | size_t bufsize = 4096; |
b6495fa8 | 4231 | int pattern = 0; |
83de9be0 | 4232 | size_t step = 0; |
55d539c8 KW |
4233 | int flush_interval = 0; |
4234 | bool drain_on_flush = true; | |
b6133b8c KW |
4235 | int64_t image_size; |
4236 | BlockBackend *blk = NULL; | |
4237 | BenchData data = {}; | |
4238 | int flags = 0; | |
604e8613 | 4239 | bool writethrough = false; |
b6133b8c KW |
4240 | struct timeval t1, t2; |
4241 | int i; | |
335e9937 | 4242 | bool force_share = false; |
79d46583 | 4243 | size_t buf_size; |
b6133b8c KW |
4244 | |
4245 | for (;;) { | |
4246 | static const struct option long_options[] = { | |
4247 | {"help", no_argument, 0, 'h'}, | |
55d539c8 | 4248 | {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL}, |
b6133b8c | 4249 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
b6495fa8 | 4250 | {"pattern", required_argument, 0, OPTION_PATTERN}, |
55d539c8 | 4251 | {"no-drain", no_argument, 0, OPTION_NO_DRAIN}, |
335e9937 | 4252 | {"force-share", no_argument, 0, 'U'}, |
b6133b8c KW |
4253 | {0, 0, 0, 0} |
4254 | }; | |
cdd26774 AM |
4255 | c = getopt_long(argc, argv, ":hc:d:f:ni:o:qs:S:t:wU", long_options, |
4256 | NULL); | |
b6133b8c KW |
4257 | if (c == -1) { |
4258 | break; | |
4259 | } | |
4260 | ||
4261 | switch (c) { | |
c9192973 SH |
4262 | case ':': |
4263 | missing_argument(argv[optind - 1]); | |
4264 | break; | |
b6133b8c | 4265 | case '?': |
c9192973 SH |
4266 | unrecognized_option(argv[optind - 1]); |
4267 | break; | |
4268 | case 'h': | |
b6133b8c KW |
4269 | help(); |
4270 | break; | |
4271 | case 'c': | |
4272 | { | |
8b3c6792 PM |
4273 | unsigned long res; |
4274 | ||
4275 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { | |
b6133b8c KW |
4276 | error_report("Invalid request count specified"); |
4277 | return 1; | |
4278 | } | |
8b3c6792 | 4279 | count = res; |
b6133b8c KW |
4280 | break; |
4281 | } | |
4282 | case 'd': | |
4283 | { | |
8b3c6792 PM |
4284 | unsigned long res; |
4285 | ||
4286 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { | |
b6133b8c KW |
4287 | error_report("Invalid queue depth specified"); |
4288 | return 1; | |
4289 | } | |
8b3c6792 | 4290 | depth = res; |
b6133b8c KW |
4291 | break; |
4292 | } | |
4293 | case 'f': | |
4294 | fmt = optarg; | |
4295 | break; | |
4296 | case 'n': | |
4297 | flags |= BDRV_O_NATIVE_AIO; | |
4298 | break; | |
cdd26774 AM |
4299 | case 'i': |
4300 | ret = bdrv_parse_aio(optarg, &flags); | |
4301 | if (ret < 0) { | |
4302 | error_report("Invalid aio option: %s", optarg); | |
4303 | ret = -1; | |
4304 | goto out; | |
4305 | } | |
4306 | break; | |
d3199a31 KW |
4307 | case 'o': |
4308 | { | |
43d589b0 | 4309 | offset = cvtnum("offset", optarg); |
606caa0a | 4310 | if (offset < 0) { |
d3199a31 KW |
4311 | return 1; |
4312 | } | |
4313 | break; | |
4314 | } | |
4315 | break; | |
b6133b8c KW |
4316 | case 'q': |
4317 | quiet = true; | |
4318 | break; | |
4319 | case 's': | |
4320 | { | |
4321 | int64_t sval; | |
b6133b8c | 4322 | |
43d589b0 EM |
4323 | sval = cvtnum_full("buffer size", optarg, 0, INT_MAX); |
4324 | if (sval < 0) { | |
b6133b8c KW |
4325 | return 1; |
4326 | } | |
4327 | ||
4328 | bufsize = sval; | |
4329 | break; | |
4330 | } | |
83de9be0 KW |
4331 | case 'S': |
4332 | { | |
4333 | int64_t sval; | |
83de9be0 | 4334 | |
43d589b0 EM |
4335 | sval = cvtnum_full("step_size", optarg, 0, INT_MAX); |
4336 | if (sval < 0) { | |
83de9be0 KW |
4337 | return 1; |
4338 | } | |
4339 | ||
4340 | step = sval; | |
4341 | break; | |
4342 | } | |
b6133b8c KW |
4343 | case 't': |
4344 | ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough); | |
4345 | if (ret < 0) { | |
4346 | error_report("Invalid cache mode"); | |
4347 | ret = -1; | |
4348 | goto out; | |
4349 | } | |
4350 | break; | |
b6495fa8 KW |
4351 | case 'w': |
4352 | flags |= BDRV_O_RDWR; | |
4353 | is_write = true; | |
4354 | break; | |
335e9937 FZ |
4355 | case 'U': |
4356 | force_share = true; | |
4357 | break; | |
b6495fa8 KW |
4358 | case OPTION_PATTERN: |
4359 | { | |
8b3c6792 PM |
4360 | unsigned long res; |
4361 | ||
4362 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) { | |
b6495fa8 KW |
4363 | error_report("Invalid pattern byte specified"); |
4364 | return 1; | |
4365 | } | |
8b3c6792 | 4366 | pattern = res; |
b6495fa8 KW |
4367 | break; |
4368 | } | |
55d539c8 KW |
4369 | case OPTION_FLUSH_INTERVAL: |
4370 | { | |
8b3c6792 PM |
4371 | unsigned long res; |
4372 | ||
4373 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { | |
55d539c8 KW |
4374 | error_report("Invalid flush interval specified"); |
4375 | return 1; | |
4376 | } | |
8b3c6792 | 4377 | flush_interval = res; |
55d539c8 KW |
4378 | break; |
4379 | } | |
4380 | case OPTION_NO_DRAIN: | |
4381 | drain_on_flush = false; | |
4382 | break; | |
b6133b8c KW |
4383 | case OPTION_IMAGE_OPTS: |
4384 | image_opts = true; | |
4385 | break; | |
4386 | } | |
4387 | } | |
4388 | ||
4389 | if (optind != argc - 1) { | |
4390 | error_exit("Expecting one image file name"); | |
4391 | } | |
4392 | filename = argv[argc - 1]; | |
4393 | ||
55d539c8 KW |
4394 | if (!is_write && flush_interval) { |
4395 | error_report("--flush-interval is only available in write tests"); | |
4396 | ret = -1; | |
4397 | goto out; | |
4398 | } | |
4399 | if (flush_interval && flush_interval < depth) { | |
4400 | error_report("Flush interval can't be smaller than depth"); | |
4401 | ret = -1; | |
4402 | goto out; | |
4403 | } | |
4404 | ||
335e9937 FZ |
4405 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
4406 | force_share); | |
b6133b8c KW |
4407 | if (!blk) { |
4408 | ret = -1; | |
4409 | goto out; | |
4410 | } | |
4411 | ||
4412 | image_size = blk_getlength(blk); | |
4413 | if (image_size < 0) { | |
4414 | ret = image_size; | |
4415 | goto out; | |
4416 | } | |
4417 | ||
4418 | data = (BenchData) { | |
55d539c8 KW |
4419 | .blk = blk, |
4420 | .image_size = image_size, | |
4421 | .bufsize = bufsize, | |
4422 | .step = step ?: bufsize, | |
4423 | .nrreq = depth, | |
4424 | .n = count, | |
4425 | .offset = offset, | |
4426 | .write = is_write, | |
4427 | .flush_interval = flush_interval, | |
4428 | .drain_on_flush = drain_on_flush, | |
b6133b8c | 4429 | }; |
d3199a31 | 4430 | printf("Sending %d %s requests, %d bytes each, %d in parallel " |
83de9be0 | 4431 | "(starting at offset %" PRId64 ", step size %d)\n", |
d3199a31 | 4432 | data.n, data.write ? "write" : "read", data.bufsize, data.nrreq, |
83de9be0 | 4433 | data.offset, data.step); |
55d539c8 KW |
4434 | if (flush_interval) { |
4435 | printf("Sending flush every %d requests\n", flush_interval); | |
4436 | } | |
b6133b8c | 4437 | |
79d46583 FZ |
4438 | buf_size = data.nrreq * data.bufsize; |
4439 | data.buf = blk_blockalign(blk, buf_size); | |
b6495fa8 KW |
4440 | memset(data.buf, pattern, data.nrreq * data.bufsize); |
4441 | ||
79d46583 FZ |
4442 | blk_register_buf(blk, data.buf, buf_size); |
4443 | ||
b6133b8c KW |
4444 | data.qiov = g_new(QEMUIOVector, data.nrreq); |
4445 | for (i = 0; i < data.nrreq; i++) { | |
4446 | qemu_iovec_init(&data.qiov[i], 1); | |
4447 | qemu_iovec_add(&data.qiov[i], | |
4448 | data.buf + i * data.bufsize, data.bufsize); | |
4449 | } | |
4450 | ||
4451 | gettimeofday(&t1, NULL); | |
4452 | bench_cb(&data, 0); | |
4453 | ||
4454 | while (data.n > 0) { | |
4455 | main_loop_wait(false); | |
4456 | } | |
4457 | gettimeofday(&t2, NULL); | |
4458 | ||
4459 | printf("Run completed in %3.3f seconds.\n", | |
4460 | (t2.tv_sec - t1.tv_sec) | |
4461 | + ((double)(t2.tv_usec - t1.tv_usec) / 1000000)); | |
4462 | ||
4463 | out: | |
79d46583 FZ |
4464 | if (data.buf) { |
4465 | blk_unregister_buf(blk, data.buf); | |
4466 | } | |
b6133b8c KW |
4467 | qemu_vfree(data.buf); |
4468 | blk_unref(blk); | |
4469 | ||
4470 | if (ret) { | |
86ce1f6e RS |
4471 | return 1; |
4472 | } | |
4473 | return 0; | |
4474 | } | |
4475 | ||
4476 | #define C_BS 01 | |
4477 | #define C_COUNT 02 | |
4478 | #define C_IF 04 | |
4479 | #define C_OF 010 | |
f7c15533 | 4480 | #define C_SKIP 020 |
86ce1f6e RS |
4481 | |
4482 | struct DdInfo { | |
4483 | unsigned int flags; | |
4484 | int64_t count; | |
4485 | }; | |
4486 | ||
4487 | struct DdIo { | |
4488 | int bsz; /* Block size */ | |
4489 | char *filename; | |
4490 | uint8_t *buf; | |
f7c15533 | 4491 | int64_t offset; |
86ce1f6e RS |
4492 | }; |
4493 | ||
4494 | struct DdOpts { | |
4495 | const char *name; | |
4496 | int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *); | |
4497 | unsigned int flag; | |
4498 | }; | |
4499 | ||
4500 | static int img_dd_bs(const char *arg, | |
4501 | struct DdIo *in, struct DdIo *out, | |
4502 | struct DdInfo *dd) | |
4503 | { | |
86ce1f6e RS |
4504 | int64_t res; |
4505 | ||
43d589b0 | 4506 | res = cvtnum_full("bs", arg, 1, INT_MAX); |
86ce1f6e | 4507 | |
43d589b0 | 4508 | if (res < 0) { |
86ce1f6e RS |
4509 | return 1; |
4510 | } | |
4511 | in->bsz = out->bsz = res; | |
4512 | ||
4513 | return 0; | |
4514 | } | |
4515 | ||
4516 | static int img_dd_count(const char *arg, | |
4517 | struct DdIo *in, struct DdIo *out, | |
4518 | struct DdInfo *dd) | |
4519 | { | |
43d589b0 | 4520 | dd->count = cvtnum("count", arg); |
86ce1f6e | 4521 | |
606caa0a | 4522 | if (dd->count < 0) { |
86ce1f6e RS |
4523 | return 1; |
4524 | } | |
4525 | ||
4526 | return 0; | |
4527 | } | |
4528 | ||
4529 | static int img_dd_if(const char *arg, | |
4530 | struct DdIo *in, struct DdIo *out, | |
4531 | struct DdInfo *dd) | |
4532 | { | |
4533 | in->filename = g_strdup(arg); | |
4534 | ||
4535 | return 0; | |
4536 | } | |
4537 | ||
4538 | static int img_dd_of(const char *arg, | |
4539 | struct DdIo *in, struct DdIo *out, | |
4540 | struct DdInfo *dd) | |
4541 | { | |
4542 | out->filename = g_strdup(arg); | |
4543 | ||
4544 | return 0; | |
4545 | } | |
4546 | ||
f7c15533 RS |
4547 | static int img_dd_skip(const char *arg, |
4548 | struct DdIo *in, struct DdIo *out, | |
4549 | struct DdInfo *dd) | |
4550 | { | |
43d589b0 | 4551 | in->offset = cvtnum("skip", arg); |
f7c15533 | 4552 | |
606caa0a | 4553 | if (in->offset < 0) { |
f7c15533 RS |
4554 | return 1; |
4555 | } | |
4556 | ||
4557 | return 0; | |
4558 | } | |
4559 | ||
86ce1f6e RS |
4560 | static int img_dd(int argc, char **argv) |
4561 | { | |
4562 | int ret = 0; | |
4563 | char *arg = NULL; | |
4564 | char *tmp; | |
4565 | BlockDriver *drv = NULL, *proto_drv = NULL; | |
4566 | BlockBackend *blk1 = NULL, *blk2 = NULL; | |
4567 | QemuOpts *opts = NULL; | |
4568 | QemuOptsList *create_opts = NULL; | |
4569 | Error *local_err = NULL; | |
4570 | bool image_opts = false; | |
4571 | int c, i; | |
4572 | const char *out_fmt = "raw"; | |
4573 | const char *fmt = NULL; | |
4574 | int64_t size = 0; | |
4575 | int64_t block_count = 0, out_pos, in_pos; | |
335e9937 | 4576 | bool force_share = false; |
86ce1f6e RS |
4577 | struct DdInfo dd = { |
4578 | .flags = 0, | |
4579 | .count = 0, | |
4580 | }; | |
4581 | struct DdIo in = { | |
4582 | .bsz = 512, /* Block size is by default 512 bytes */ | |
4583 | .filename = NULL, | |
f7c15533 RS |
4584 | .buf = NULL, |
4585 | .offset = 0 | |
86ce1f6e RS |
4586 | }; |
4587 | struct DdIo out = { | |
4588 | .bsz = 512, | |
4589 | .filename = NULL, | |
f7c15533 RS |
4590 | .buf = NULL, |
4591 | .offset = 0 | |
86ce1f6e RS |
4592 | }; |
4593 | ||
4594 | const struct DdOpts options[] = { | |
4595 | { "bs", img_dd_bs, C_BS }, | |
4596 | { "count", img_dd_count, C_COUNT }, | |
4597 | { "if", img_dd_if, C_IF }, | |
4598 | { "of", img_dd_of, C_OF }, | |
f7c15533 | 4599 | { "skip", img_dd_skip, C_SKIP }, |
86ce1f6e RS |
4600 | { NULL, NULL, 0 } |
4601 | }; | |
4602 | const struct option long_options[] = { | |
4603 | { "help", no_argument, 0, 'h'}, | |
83d4bf94 | 4604 | { "object", required_argument, 0, OPTION_OBJECT}, |
86ce1f6e | 4605 | { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
335e9937 | 4606 | { "force-share", no_argument, 0, 'U'}, |
86ce1f6e RS |
4607 | { 0, 0, 0, 0 } |
4608 | }; | |
4609 | ||
335e9937 | 4610 | while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) { |
86ce1f6e RS |
4611 | if (c == EOF) { |
4612 | break; | |
4613 | } | |
4614 | switch (c) { | |
4615 | case 'O': | |
4616 | out_fmt = optarg; | |
4617 | break; | |
4618 | case 'f': | |
4619 | fmt = optarg; | |
4620 | break; | |
c9192973 SH |
4621 | case ':': |
4622 | missing_argument(argv[optind - 1]); | |
4623 | break; | |
86ce1f6e | 4624 | case '?': |
c9192973 SH |
4625 | unrecognized_option(argv[optind - 1]); |
4626 | break; | |
86ce1f6e RS |
4627 | case 'h': |
4628 | help(); | |
4629 | break; | |
335e9937 FZ |
4630 | case 'U': |
4631 | force_share = true; | |
4632 | break; | |
2a245709 SH |
4633 | case OPTION_OBJECT: |
4634 | if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) { | |
83d4bf94 DB |
4635 | ret = -1; |
4636 | goto out; | |
4637 | } | |
2a245709 | 4638 | break; |
86ce1f6e RS |
4639 | case OPTION_IMAGE_OPTS: |
4640 | image_opts = true; | |
4641 | break; | |
4642 | } | |
4643 | } | |
4644 | ||
4645 | for (i = optind; i < argc; i++) { | |
4646 | int j; | |
4647 | arg = g_strdup(argv[i]); | |
4648 | ||
4649 | tmp = strchr(arg, '='); | |
4650 | if (tmp == NULL) { | |
4651 | error_report("unrecognized operand %s", arg); | |
4652 | ret = -1; | |
4653 | goto out; | |
4654 | } | |
4655 | ||
4656 | *tmp++ = '\0'; | |
4657 | ||
4658 | for (j = 0; options[j].name != NULL; j++) { | |
4659 | if (!strcmp(arg, options[j].name)) { | |
4660 | break; | |
4661 | } | |
4662 | } | |
4663 | if (options[j].name == NULL) { | |
4664 | error_report("unrecognized operand %s", arg); | |
4665 | ret = -1; | |
4666 | goto out; | |
4667 | } | |
4668 | ||
4669 | if (options[j].f(tmp, &in, &out, &dd) != 0) { | |
4670 | ret = -1; | |
4671 | goto out; | |
4672 | } | |
4673 | dd.flags |= options[j].flag; | |
4674 | g_free(arg); | |
4675 | arg = NULL; | |
4676 | } | |
4677 | ||
4678 | if (!(dd.flags & C_IF && dd.flags & C_OF)) { | |
4679 | error_report("Must specify both input and output files"); | |
4680 | ret = -1; | |
4681 | goto out; | |
4682 | } | |
83d4bf94 DB |
4683 | |
4684 | if (qemu_opts_foreach(&qemu_object_opts, | |
4685 | user_creatable_add_opts_foreach, | |
c6e5cdfd | 4686 | qemu_img_object_print_help, &error_fatal)) { |
83d4bf94 DB |
4687 | ret = -1; |
4688 | goto out; | |
4689 | } | |
4690 | ||
335e9937 FZ |
4691 | blk1 = img_open(image_opts, in.filename, fmt, 0, false, false, |
4692 | force_share); | |
86ce1f6e RS |
4693 | |
4694 | if (!blk1) { | |
4695 | ret = -1; | |
4696 | goto out; | |
4697 | } | |
4698 | ||
4699 | drv = bdrv_find_format(out_fmt); | |
4700 | if (!drv) { | |
4701 | error_report("Unknown file format"); | |
4702 | ret = -1; | |
4703 | goto out; | |
4704 | } | |
4705 | proto_drv = bdrv_find_protocol(out.filename, true, &local_err); | |
4706 | ||
4707 | if (!proto_drv) { | |
4708 | error_report_err(local_err); | |
4709 | ret = -1; | |
4710 | goto out; | |
4711 | } | |
4712 | if (!drv->create_opts) { | |
4713 | error_report("Format driver '%s' does not support image creation", | |
4714 | drv->format_name); | |
4715 | ret = -1; | |
4716 | goto out; | |
4717 | } | |
4718 | if (!proto_drv->create_opts) { | |
4719 | error_report("Protocol driver '%s' does not support image creation", | |
4720 | proto_drv->format_name); | |
4721 | ret = -1; | |
4722 | goto out; | |
4723 | } | |
4724 | create_opts = qemu_opts_append(create_opts, drv->create_opts); | |
4725 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); | |
4726 | ||
4727 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); | |
4728 | ||
4729 | size = blk_getlength(blk1); | |
4730 | if (size < 0) { | |
4731 | error_report("Failed to get size for '%s'", in.filename); | |
4732 | ret = -1; | |
4733 | goto out; | |
4734 | } | |
4735 | ||
4736 | if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz && | |
4737 | dd.count * in.bsz < size) { | |
4738 | size = dd.count * in.bsz; | |
4739 | } | |
4740 | ||
f7c15533 RS |
4741 | /* Overflow means the specified offset is beyond input image's size */ |
4742 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || | |
4743 | size < in.bsz * in.offset)) { | |
4744 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort); | |
4745 | } else { | |
4746 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, | |
4747 | size - in.bsz * in.offset, &error_abort); | |
4748 | } | |
86ce1f6e RS |
4749 | |
4750 | ret = bdrv_create(drv, out.filename, opts, &local_err); | |
4751 | if (ret < 0) { | |
4752 | error_reportf_err(local_err, | |
4753 | "%s: error while creating output image: ", | |
4754 | out.filename); | |
4755 | ret = -1; | |
4756 | goto out; | |
4757 | } | |
4758 | ||
ea204dda DB |
4759 | /* TODO, we can't honour --image-opts for the target, |
4760 | * since it needs to be given in a format compatible | |
4761 | * with the bdrv_create() call above which does not | |
4762 | * support image-opts style. | |
4763 | */ | |
29cf9336 | 4764 | blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR, |
ea204dda | 4765 | false, false, false); |
86ce1f6e RS |
4766 | |
4767 | if (!blk2) { | |
4768 | ret = -1; | |
4769 | goto out; | |
4770 | } | |
4771 | ||
f7c15533 RS |
4772 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || |
4773 | size < in.offset * in.bsz)) { | |
4774 | /* We give a warning if the skip option is bigger than the input | |
4775 | * size and create an empty output disk image (i.e. like dd(1)). | |
4776 | */ | |
4777 | error_report("%s: cannot skip to specified offset", in.filename); | |
4778 | in_pos = size; | |
4779 | } else { | |
4780 | in_pos = in.offset * in.bsz; | |
4781 | } | |
4782 | ||
86ce1f6e RS |
4783 | in.buf = g_new(uint8_t, in.bsz); |
4784 | ||
f7c15533 | 4785 | for (out_pos = 0; in_pos < size; block_count++) { |
86ce1f6e RS |
4786 | int in_ret, out_ret; |
4787 | ||
4788 | if (in_pos + in.bsz > size) { | |
4789 | in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos); | |
4790 | } else { | |
4791 | in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz); | |
4792 | } | |
4793 | if (in_ret < 0) { | |
4794 | error_report("error while reading from input image file: %s", | |
4795 | strerror(-in_ret)); | |
4796 | ret = -1; | |
4797 | goto out; | |
4798 | } | |
4799 | in_pos += in_ret; | |
4800 | ||
4801 | out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0); | |
4802 | ||
4803 | if (out_ret < 0) { | |
4804 | error_report("error while writing to output image file: %s", | |
4805 | strerror(-out_ret)); | |
4806 | ret = -1; | |
4807 | goto out; | |
4808 | } | |
4809 | out_pos += out_ret; | |
4810 | } | |
4811 | ||
4812 | out: | |
4813 | g_free(arg); | |
4814 | qemu_opts_del(opts); | |
4815 | qemu_opts_free(create_opts); | |
4816 | blk_unref(blk1); | |
4817 | blk_unref(blk2); | |
4818 | g_free(in.filename); | |
4819 | g_free(out.filename); | |
4820 | g_free(in.buf); | |
4821 | g_free(out.buf); | |
4822 | ||
4823 | if (ret) { | |
b6133b8c KW |
4824 | return 1; |
4825 | } | |
4826 | return 0; | |
4827 | } | |
4828 | ||
fd03c2b8 SH |
4829 | static void dump_json_block_measure_info(BlockMeasureInfo *info) |
4830 | { | |
4831 | QString *str; | |
4832 | QObject *obj; | |
4833 | Visitor *v = qobject_output_visitor_new(&obj); | |
4834 | ||
4835 | visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort); | |
4836 | visit_complete(v, &obj); | |
4837 | str = qobject_to_json_pretty(obj); | |
4838 | assert(str != NULL); | |
4839 | printf("%s\n", qstring_get_str(str)); | |
cb3e7f08 | 4840 | qobject_unref(obj); |
fd03c2b8 | 4841 | visit_free(v); |
cb3e7f08 | 4842 | qobject_unref(str); |
fd03c2b8 SH |
4843 | } |
4844 | ||
4845 | static int img_measure(int argc, char **argv) | |
4846 | { | |
4847 | static const struct option long_options[] = { | |
4848 | {"help", no_argument, 0, 'h'}, | |
4849 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, | |
4850 | {"object", required_argument, 0, OPTION_OBJECT}, | |
4851 | {"output", required_argument, 0, OPTION_OUTPUT}, | |
4852 | {"size", required_argument, 0, OPTION_SIZE}, | |
4853 | {"force-share", no_argument, 0, 'U'}, | |
4854 | {0, 0, 0, 0} | |
4855 | }; | |
4856 | OutputFormat output_format = OFORMAT_HUMAN; | |
4857 | BlockBackend *in_blk = NULL; | |
4858 | BlockDriver *drv; | |
4859 | const char *filename = NULL; | |
4860 | const char *fmt = NULL; | |
4861 | const char *out_fmt = "raw"; | |
4862 | char *options = NULL; | |
4863 | char *snapshot_name = NULL; | |
4864 | bool force_share = false; | |
4865 | QemuOpts *opts = NULL; | |
4866 | QemuOpts *object_opts = NULL; | |
4867 | QemuOpts *sn_opts = NULL; | |
4868 | QemuOptsList *create_opts = NULL; | |
4869 | bool image_opts = false; | |
4870 | uint64_t img_size = UINT64_MAX; | |
4871 | BlockMeasureInfo *info = NULL; | |
4872 | Error *local_err = NULL; | |
4873 | int ret = 1; | |
4874 | int c; | |
4875 | ||
4876 | while ((c = getopt_long(argc, argv, "hf:O:o:l:U", | |
4877 | long_options, NULL)) != -1) { | |
4878 | switch (c) { | |
4879 | case '?': | |
4880 | case 'h': | |
4881 | help(); | |
4882 | break; | |
4883 | case 'f': | |
4884 | fmt = optarg; | |
4885 | break; | |
4886 | case 'O': | |
4887 | out_fmt = optarg; | |
4888 | break; | |
4889 | case 'o': | |
6d2b5cba | 4890 | if (accumulate_options(&options, optarg) < 0) { |
fd03c2b8 SH |
4891 | goto out; |
4892 | } | |
fd03c2b8 SH |
4893 | break; |
4894 | case 'l': | |
4895 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { | |
4896 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, | |
4897 | optarg, false); | |
4898 | if (!sn_opts) { | |
4899 | error_report("Failed in parsing snapshot param '%s'", | |
4900 | optarg); | |
4901 | goto out; | |
4902 | } | |
4903 | } else { | |
4904 | snapshot_name = optarg; | |
4905 | } | |
4906 | break; | |
4907 | case 'U': | |
4908 | force_share = true; | |
4909 | break; | |
4910 | case OPTION_OBJECT: | |
4911 | object_opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
4912 | optarg, true); | |
4913 | if (!object_opts) { | |
4914 | goto out; | |
4915 | } | |
4916 | break; | |
4917 | case OPTION_IMAGE_OPTS: | |
4918 | image_opts = true; | |
4919 | break; | |
4920 | case OPTION_OUTPUT: | |
4921 | if (!strcmp(optarg, "json")) { | |
4922 | output_format = OFORMAT_JSON; | |
4923 | } else if (!strcmp(optarg, "human")) { | |
4924 | output_format = OFORMAT_HUMAN; | |
4925 | } else { | |
4926 | error_report("--output must be used with human or json " | |
4927 | "as argument."); | |
4928 | goto out; | |
4929 | } | |
4930 | break; | |
4931 | case OPTION_SIZE: | |
4932 | { | |
4933 | int64_t sval; | |
4934 | ||
43d589b0 | 4935 | sval = cvtnum("image size", optarg); |
fd03c2b8 | 4936 | if (sval < 0) { |
fd03c2b8 SH |
4937 | goto out; |
4938 | } | |
4939 | img_size = (uint64_t)sval; | |
4940 | } | |
4941 | break; | |
4942 | } | |
4943 | } | |
4944 | ||
4945 | if (qemu_opts_foreach(&qemu_object_opts, | |
4946 | user_creatable_add_opts_foreach, | |
c6e5cdfd | 4947 | qemu_img_object_print_help, &error_fatal)) { |
fd03c2b8 SH |
4948 | goto out; |
4949 | } | |
4950 | ||
4951 | if (argc - optind > 1) { | |
4952 | error_report("At most one filename argument is allowed."); | |
4953 | goto out; | |
4954 | } else if (argc - optind == 1) { | |
4955 | filename = argv[optind]; | |
4956 | } | |
4957 | ||
c3673dcf SH |
4958 | if (!filename && (image_opts || fmt || snapshot_name || sn_opts)) { |
4959 | error_report("--image-opts, -f, and -l require a filename argument."); | |
fd03c2b8 SH |
4960 | goto out; |
4961 | } | |
4962 | if (filename && img_size != UINT64_MAX) { | |
4963 | error_report("--size N cannot be used together with a filename."); | |
4964 | goto out; | |
4965 | } | |
4966 | if (!filename && img_size == UINT64_MAX) { | |
4967 | error_report("Either --size N or one filename must be specified."); | |
4968 | goto out; | |
4969 | } | |
4970 | ||
4971 | if (filename) { | |
4972 | in_blk = img_open(image_opts, filename, fmt, 0, | |
4973 | false, false, force_share); | |
4974 | if (!in_blk) { | |
4975 | goto out; | |
4976 | } | |
4977 | ||
4978 | if (sn_opts) { | |
4979 | bdrv_snapshot_load_tmp(blk_bs(in_blk), | |
4980 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), | |
4981 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), | |
4982 | &local_err); | |
4983 | } else if (snapshot_name != NULL) { | |
4984 | bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk), | |
4985 | snapshot_name, &local_err); | |
4986 | } | |
4987 | if (local_err) { | |
4988 | error_reportf_err(local_err, "Failed to load snapshot: "); | |
4989 | goto out; | |
4990 | } | |
4991 | } | |
4992 | ||
4993 | drv = bdrv_find_format(out_fmt); | |
4994 | if (!drv) { | |
4995 | error_report("Unknown file format '%s'", out_fmt); | |
4996 | goto out; | |
4997 | } | |
4998 | if (!drv->create_opts) { | |
4999 | error_report("Format driver '%s' does not support image creation", | |
5000 | drv->format_name); | |
5001 | goto out; | |
5002 | } | |
5003 | ||
5004 | create_opts = qemu_opts_append(create_opts, drv->create_opts); | |
5005 | create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts); | |
5006 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); | |
5007 | if (options) { | |
5008 | qemu_opts_do_parse(opts, options, NULL, &local_err); | |
5009 | if (local_err) { | |
5010 | error_report_err(local_err); | |
5011 | error_report("Invalid options for file format '%s'", out_fmt); | |
5012 | goto out; | |
5013 | } | |
5014 | } | |
5015 | if (img_size != UINT64_MAX) { | |
5016 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort); | |
5017 | } | |
5018 | ||
5019 | info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err); | |
5020 | if (local_err) { | |
5021 | error_report_err(local_err); | |
5022 | goto out; | |
5023 | } | |
5024 | ||
5025 | if (output_format == OFORMAT_HUMAN) { | |
5026 | printf("required size: %" PRIu64 "\n", info->required); | |
5027 | printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated); | |
5028 | } else { | |
5029 | dump_json_block_measure_info(info); | |
5030 | } | |
5031 | ||
5032 | ret = 0; | |
5033 | ||
5034 | out: | |
5035 | qapi_free_BlockMeasureInfo(info); | |
5036 | qemu_opts_del(object_opts); | |
5037 | qemu_opts_del(opts); | |
5038 | qemu_opts_del(sn_opts); | |
5039 | qemu_opts_free(create_opts); | |
5040 | g_free(options); | |
5041 | blk_unref(in_blk); | |
5042 | return ret; | |
5043 | } | |
b6133b8c | 5044 | |
c227f099 | 5045 | static const img_cmd_t img_cmds[] = { |
153859be SB |
5046 | #define DEF(option, callback, arg_string) \ |
5047 | { option, callback }, | |
5048 | #include "qemu-img-cmds.h" | |
5049 | #undef DEF | |
153859be SB |
5050 | { NULL, NULL, }, |
5051 | }; | |
5052 | ||
ea2384d3 FB |
5053 | int main(int argc, char **argv) |
5054 | { | |
c227f099 | 5055 | const img_cmd_t *cmd; |
153859be | 5056 | const char *cmdname; |
2f78e491 | 5057 | Error *local_error = NULL; |
06a1e0c1 | 5058 | char *trace_file = NULL; |
7db1689c | 5059 | int c; |
7db1689c JC |
5060 | static const struct option long_options[] = { |
5061 | {"help", no_argument, 0, 'h'}, | |
10985131 | 5062 | {"version", no_argument, 0, 'V'}, |
06a1e0c1 | 5063 | {"trace", required_argument, NULL, 'T'}, |
7db1689c JC |
5064 | {0, 0, 0, 0} |
5065 | }; | |
ea2384d3 | 5066 | |
526eda14 MK |
5067 | #ifdef CONFIG_POSIX |
5068 | signal(SIGPIPE, SIG_IGN); | |
5069 | #endif | |
5070 | ||
f5852efa | 5071 | error_init(argv[0]); |
fe4db84d | 5072 | module_call_init(MODULE_INIT_TRACE); |
10f5bff6 | 5073 | qemu_init_exec_dir(argv[0]); |
53f76e58 | 5074 | |
2f78e491 | 5075 | if (qemu_init_main_loop(&local_error)) { |
565f65d2 | 5076 | error_report_err(local_error); |
2f78e491 CN |
5077 | exit(EXIT_FAILURE); |
5078 | } | |
5079 | ||
e8f2d272 | 5080 | qcrypto_init(&error_fatal); |
c2297088 | 5081 | |
064097d9 | 5082 | module_call_init(MODULE_INIT_QOM); |
ea2384d3 | 5083 | bdrv_init(); |
ac1307ab FZ |
5084 | if (argc < 2) { |
5085 | error_exit("Not enough arguments"); | |
5086 | } | |
153859be | 5087 | |
3babeb15 | 5088 | qemu_add_opts(&qemu_object_opts); |
eb769f74 | 5089 | qemu_add_opts(&qemu_source_opts); |
06a1e0c1 | 5090 | qemu_add_opts(&qemu_trace_opts); |
3babeb15 | 5091 | |
c9192973 | 5092 | while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) { |
10985131 | 5093 | switch (c) { |
c9192973 SH |
5094 | case ':': |
5095 | missing_argument(argv[optind - 1]); | |
5096 | return 0; | |
4581c16f | 5097 | case '?': |
c9192973 SH |
5098 | unrecognized_option(argv[optind - 1]); |
5099 | return 0; | |
5100 | case 'h': | |
10985131 DL |
5101 | help(); |
5102 | return 0; | |
5103 | case 'V': | |
5104 | printf(QEMU_IMG_VERSION); | |
5105 | return 0; | |
06a1e0c1 DL |
5106 | case 'T': |
5107 | g_free(trace_file); | |
5108 | trace_file = trace_opt_parse(optarg); | |
5109 | break; | |
153859be | 5110 | } |
ea2384d3 | 5111 | } |
153859be | 5112 | |
10985131 | 5113 | cmdname = argv[optind]; |
7db1689c | 5114 | |
10985131 DL |
5115 | /* reset getopt_long scanning */ |
5116 | argc -= optind; | |
5117 | if (argc < 1) { | |
5f6979cb JC |
5118 | return 0; |
5119 | } | |
10985131 | 5120 | argv += optind; |
d339d766 | 5121 | qemu_reset_optind(); |
10985131 | 5122 | |
06a1e0c1 DL |
5123 | if (!trace_init_backends()) { |
5124 | exit(1); | |
5125 | } | |
5126 | trace_init_file(trace_file); | |
5127 | qemu_set_log(LOG_TRACE); | |
5128 | ||
10985131 DL |
5129 | /* find the command */ |
5130 | for (cmd = img_cmds; cmd->name != NULL; cmd++) { | |
5131 | if (!strcmp(cmdname, cmd->name)) { | |
5132 | return cmd->handler(argc, argv); | |
5133 | } | |
5134 | } | |
7db1689c | 5135 | |
153859be | 5136 | /* not found */ |
ac1307ab | 5137 | error_exit("Command not found: %s", cmdname); |
ea2384d3 | 5138 | } |