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