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