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