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