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