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