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