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