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