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