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