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