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" |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 31 | #include "qapi/qapi-commands-block-core.h" |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 32 | #include "qapi/qapi-visit-block-core.h" |
Daniel P. Berrange | b3db211 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 33 | #include "qapi/qobject-output-visitor.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 34 | #include "qapi/qmp/qjson.h" |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 35 | #include "qapi/qmp/qdict.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" |
Daniel P. Berrangé | 98c5d2e | 2020-08-25 11:38:48 +0100 | [diff] [blame] | 43 | #include "qemu/sockets.h" |
Stefano Garzarella | 97ede57 | 2019-05-08 12:43:24 +0200 | [diff] [blame] | 44 | #include "qemu/units.h" |
Peter Maydell | 5df022c | 2022-02-26 18:07:23 +0000 | [diff] [blame^] | 45 | #include "qemu/memalign.h" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 46 | #include "qom/object_interfaces.h" |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 47 | #include "sysemu/block-backend.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 48 | #include "block/block_int.h" |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 49 | #include "block/blockjob.h" |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 50 | #include "block/qapi.h" |
Daniel P. Berrange | c229708 | 2016-04-06 12:12:06 +0100 | [diff] [blame] | 51 | #include "crypto/init.h" |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 52 | #include "trace/control.h" |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 53 | #include "qemu/throttle.h" |
| 54 | #include "block/throttle-groups.h" |
bellard | e844533 | 2006-06-14 15:32:10 +0000 | [diff] [blame] | 55 | |
Thomas Huth | 7e563bf | 2018-02-15 12:06:47 +0100 | [diff] [blame] | 56 | #define QEMU_IMG_VERSION "qemu-img version " QEMU_FULL_VERSION \ |
Thomas Huth | 0781dd6 | 2016-10-05 11:54:44 +0200 | [diff] [blame] | 57 | "\n" QEMU_COPYRIGHT "\n" |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 58 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 59 | typedef struct img_cmd_t { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 60 | const char *name; |
| 61 | int (*handler)(int argc, char **argv); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 62 | } img_cmd_t; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 63 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 64 | enum { |
| 65 | OPTION_OUTPUT = 256, |
| 66 | OPTION_BACKING_CHAIN = 257, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 67 | OPTION_OBJECT = 258, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 68 | OPTION_IMAGE_OPTS = 259, |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 69 | OPTION_PATTERN = 260, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 70 | OPTION_FLUSH_INTERVAL = 261, |
| 71 | OPTION_NO_DRAIN = 262, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 72 | OPTION_TARGET_IMAGE_OPTS = 263, |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 73 | OPTION_SIZE = 264, |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 74 | OPTION_PREALLOCATION = 265, |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 75 | OPTION_SHRINK = 266, |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 76 | OPTION_SALVAGE = 267, |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 77 | OPTION_TARGET_IS_ZERO = 268, |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 78 | OPTION_ADD = 269, |
| 79 | OPTION_REMOVE = 270, |
| 80 | OPTION_CLEAR = 271, |
| 81 | OPTION_ENABLE = 272, |
| 82 | OPTION_DISABLE = 273, |
| 83 | OPTION_MERGE = 274, |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 84 | OPTION_BITMAPS = 275, |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 85 | OPTION_FORCE = 276, |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 86 | OPTION_SKIP_BROKEN = 277, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | typedef enum OutputFormat { |
| 90 | OFORMAT_JSON, |
| 91 | OFORMAT_HUMAN, |
| 92 | } OutputFormat; |
| 93 | |
Kevin Wolf | e699614 | 2016-03-15 13:03:11 +0100 | [diff] [blame] | 94 | /* 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] | 95 | #define BDRV_DEFAULT_CACHE "writeback" |
aurel32 | 137519c | 2008-11-30 19:12:49 +0000 | [diff] [blame] | 96 | |
Stefan Hajnoczi | 00c6d40 | 2014-08-27 12:08:56 +0100 | [diff] [blame] | 97 | static void format_print(void *opaque, const char *name) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 98 | { |
Stefan Hajnoczi | 00c6d40 | 2014-08-27 12:08:56 +0100 | [diff] [blame] | 99 | printf(" %s", name); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 102 | static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...) |
| 103 | { |
| 104 | va_list ap; |
| 105 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 106 | va_start(ap, fmt); |
Markus Armbruster | e9e1d92 | 2019-04-17 21:06:27 +0200 | [diff] [blame] | 107 | error_vreport(fmt, ap); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 108 | va_end(ap); |
| 109 | |
Markus Armbruster | e9e1d92 | 2019-04-17 21:06:27 +0200 | [diff] [blame] | 110 | error_printf("Try 'qemu-img --help' for more information\n"); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 111 | exit(EXIT_FAILURE); |
| 112 | } |
| 113 | |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 114 | static void QEMU_NORETURN missing_argument(const char *option) |
| 115 | { |
| 116 | error_exit("missing argument for option '%s'", option); |
| 117 | } |
| 118 | |
| 119 | static void QEMU_NORETURN unrecognized_option(const char *option) |
| 120 | { |
| 121 | error_exit("unrecognized option '%s'", option); |
| 122 | } |
| 123 | |
Eric Blake | 0562adf | 2020-05-12 20:16:41 -0500 | [diff] [blame] | 124 | /* Please keep in synch with docs/tools/qemu-img.rst */ |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 125 | static void QEMU_NORETURN help(void) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 126 | { |
Paolo Bonzini | e00291c | 2010-02-04 16:49:56 +0100 | [diff] [blame] | 127 | const char *help_msg = |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 128 | QEMU_IMG_VERSION |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 129 | "usage: qemu-img [standard options] command [command options]\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 130 | "QEMU disk image utility\n" |
| 131 | "\n" |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 132 | " '-h', '--help' display this help and exit\n" |
| 133 | " '-V', '--version' output version information and exit\n" |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 134 | " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n" |
| 135 | " specify tracing options\n" |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 136 | "\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 137 | "Command syntax:\n" |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 138 | #define DEF(option, callback, arg_string) \ |
| 139 | " " arg_string "\n" |
| 140 | #include "qemu-img-cmds.h" |
| 141 | #undef DEF |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 142 | "\n" |
| 143 | "Command parameters:\n" |
| 144 | " 'filename' is a disk image filename\n" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 145 | " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n" |
| 146 | " manual page for a description of the object properties. The most common\n" |
| 147 | " object type is a 'secret', which is used to supply passwords and/or\n" |
| 148 | " encryption keys.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 149 | " '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] | 150 | " '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] | 151 | " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n" |
| 152 | " 'directsync' and 'unsafe' (default for convert)\n" |
Stefan Hajnoczi | bb87fdf | 2014-09-02 11:01:02 +0100 | [diff] [blame] | 153 | " 'src_cache' is the cache mode used to read input disk images, the valid\n" |
| 154 | " options are the same as for the 'cache' option\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 155 | " 'size' is the disk image size in bytes. Optional suffixes\n" |
Kevin Wolf | 5e00984 | 2013-06-05 14:19:27 +0200 | [diff] [blame] | 156 | " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n" |
| 157 | " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n" |
| 158 | " supported. 'b' is ignored.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 159 | " 'output_filename' is the destination disk image filename\n" |
| 160 | " 'output_fmt' is the destination format\n" |
| 161 | " 'options' is a comma separated list of format specific options in a\n" |
| 162 | " name=value format. Use -o ? for an overview of the options supported by the\n" |
| 163 | " used format\n" |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 164 | " 'snapshot_param' is param used for internal snapshot, format\n" |
| 165 | " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n" |
| 166 | " '[ID_OR_NAME]'\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 167 | " '-c' indicates that target image must be compressed (qcow format only)\n" |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 168 | " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n" |
| 169 | " new backing file match exactly. The image doesn't need a working\n" |
| 170 | " backing file before rebasing in this case (useful for renaming the\n" |
| 171 | " backing file). For image creation, allow creating without attempting\n" |
| 172 | " to open the backing file.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 173 | " '-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] | 174 | " '-p' show progress of command (only certain commands)\n" |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 175 | " '-q' use Quiet mode - do not print any output (except errors)\n" |
Peter Lieven | 11b6699 | 2013-10-24 12:07:05 +0200 | [diff] [blame] | 176 | " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n" |
| 177 | " contain only zeros for qemu-img to create a sparse image during\n" |
| 178 | " conversion. If the number of bytes is 0, the source will not be scanned for\n" |
| 179 | " unallocated or zero sectors, and the destination image will always be\n" |
| 180 | " fully allocated\n" |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 181 | " '--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] | 182 | " '-n' skips the target volume creation (useful if the volume is created\n" |
| 183 | " prior to running qemu-img)\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 184 | "\n" |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 185 | "Parameters to bitmap subcommand:\n" |
| 186 | " 'bitmap' is the name of the bitmap to manipulate, through one or more\n" |
| 187 | " actions from '--add', '--remove', '--clear', '--enable', '--disable',\n" |
| 188 | " or '--merge source'\n" |
| 189 | " '-g granularity' sets the granularity for '--add' actions\n" |
| 190 | " '-b source' and '-F src_fmt' tell '--merge' actions to find the source\n" |
| 191 | " bitmaps from an alternative file\n" |
| 192 | "\n" |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 193 | "Parameters to check subcommand:\n" |
| 194 | " '-r' tries to repair any inconsistencies that are found during the check.\n" |
| 195 | " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n" |
| 196 | " 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] | 197 | " hiding corruption that has already occurred.\n" |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 198 | "\n" |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 199 | "Parameters to convert subcommand:\n" |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 200 | " '--bitmaps' copies all top-level persistent bitmaps to destination\n" |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 201 | " '-m' specifies how many coroutines work in parallel during the convert\n" |
| 202 | " process (defaults to 8)\n" |
| 203 | " '-W' allow to write to the target out of order rather than sequential\n" |
| 204 | "\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 205 | "Parameters to snapshot subcommand:\n" |
| 206 | " 'snapshot' is the name of the snapshot to create, apply or delete\n" |
| 207 | " '-a' applies a snapshot (revert disk to saved state)\n" |
| 208 | " '-c' creates a snapshot\n" |
| 209 | " '-d' deletes a snapshot\n" |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 210 | " '-l' lists all snapshots in the given image\n" |
| 211 | "\n" |
| 212 | "Parameters to compare subcommand:\n" |
| 213 | " '-f' first image format\n" |
| 214 | " '-F' second image format\n" |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 215 | " '-s' run in Strict mode - fail on different image size or sector allocation\n" |
| 216 | "\n" |
| 217 | "Parameters to dd subcommand:\n" |
| 218 | " 'bs=BYTES' read and write up to BYTES bytes at a time " |
| 219 | "(default: 512)\n" |
| 220 | " 'count=N' copy only N input blocks\n" |
| 221 | " 'if=FILE' read from FILE\n" |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 222 | " 'of=FILE' write to FILE\n" |
| 223 | " '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] | 224 | |
| 225 | printf("%s\nSupported formats:", help_msg); |
Andrey Shinkevich | 9ac404c | 2019-03-07 16:33:58 +0300 | [diff] [blame] | 226 | bdrv_iterate_format(format_print, NULL, false); |
Eric Blake | f5048cb | 2017-08-03 11:33:53 -0500 | [diff] [blame] | 227 | printf("\n\n" QEMU_HELP_BOTTOM "\n"); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 228 | exit(EXIT_SUCCESS); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Markus Armbruster | 80c710c | 2020-04-15 09:49:26 +0200 | [diff] [blame] | 231 | /* |
| 232 | * Is @optarg safe for accumulate_options()? |
| 233 | * It is when multiple of them can be joined together separated by ','. |
| 234 | * To make that work, @optarg must not start with ',' (or else a |
| 235 | * separating ',' preceding it gets escaped), and it must not end with |
| 236 | * an odd number of ',' (or else a separating ',' following it gets |
Markus Armbruster | f62514b | 2020-04-15 09:49:27 +0200 | [diff] [blame] | 237 | * escaped), or be empty (or else a separating ',' preceding it can |
| 238 | * escape a separating ',' following it). |
| 239 | * |
Markus Armbruster | 80c710c | 2020-04-15 09:49:26 +0200 | [diff] [blame] | 240 | */ |
| 241 | static bool is_valid_option_list(const char *optarg) |
| 242 | { |
| 243 | size_t len = strlen(optarg); |
| 244 | size_t i; |
| 245 | |
Markus Armbruster | f62514b | 2020-04-15 09:49:27 +0200 | [diff] [blame] | 246 | if (!optarg[0] || optarg[0] == ',') { |
Markus Armbruster | 80c710c | 2020-04-15 09:49:26 +0200 | [diff] [blame] | 247 | return false; |
| 248 | } |
| 249 | |
| 250 | for (i = len; i > 0 && optarg[i - 1] == ','; i--) { |
| 251 | } |
| 252 | if ((len - i) % 2) { |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | return true; |
| 257 | } |
| 258 | |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 259 | static int accumulate_options(char **options, char *optarg) |
| 260 | { |
| 261 | char *new_options; |
| 262 | |
| 263 | if (!is_valid_option_list(optarg)) { |
| 264 | error_report("Invalid option list: %s", optarg); |
| 265 | return -1; |
| 266 | } |
| 267 | |
| 268 | if (!*options) { |
| 269 | *options = g_strdup(optarg); |
| 270 | } else { |
| 271 | new_options = g_strdup_printf("%s,%s", *options, optarg); |
| 272 | g_free(*options); |
| 273 | *options = new_options; |
| 274 | } |
| 275 | return 0; |
| 276 | } |
| 277 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 278 | static QemuOptsList qemu_source_opts = { |
| 279 | .name = "source", |
| 280 | .implied_opt_name = "file", |
| 281 | .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head), |
| 282 | .desc = { |
| 283 | { } |
| 284 | }, |
| 285 | }; |
| 286 | |
Stefan Weil | 7c30f65 | 2013-06-16 17:01:05 +0200 | [diff] [blame] | 287 | 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] | 288 | { |
| 289 | int ret = 0; |
| 290 | if (!quiet) { |
| 291 | va_list args; |
| 292 | va_start(args, fmt); |
| 293 | ret = vprintf(fmt, args); |
| 294 | va_end(args); |
| 295 | } |
| 296 | return ret; |
| 297 | } |
| 298 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 299 | |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 300 | static int print_block_option_help(const char *filename, const char *fmt) |
| 301 | { |
| 302 | BlockDriver *drv, *proto_drv; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 303 | QemuOptsList *create_opts = NULL; |
Max Reitz | b65a5e1 | 2015-02-05 13:58:12 -0500 | [diff] [blame] | 304 | Error *local_err = NULL; |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 305 | |
| 306 | /* Find driver and parse its options */ |
| 307 | drv = bdrv_find_format(fmt); |
| 308 | if (!drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 309 | error_report("Unknown file format '%s'", fmt); |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 310 | return 1; |
| 311 | } |
| 312 | |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 313 | if (!drv->create_opts) { |
| 314 | error_report("Format driver '%s' does not support image creation", fmt); |
| 315 | return 1; |
| 316 | } |
| 317 | |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 318 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 319 | if (filename) { |
Max Reitz | b65a5e1 | 2015-02-05 13:58:12 -0500 | [diff] [blame] | 320 | proto_drv = bdrv_find_protocol(filename, true, &local_err); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 321 | if (!proto_drv) { |
Markus Armbruster | 2867ce4 | 2015-03-12 16:08:02 +0100 | [diff] [blame] | 322 | error_report_err(local_err); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 323 | qemu_opts_free(create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 324 | return 1; |
| 325 | } |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 326 | if (!proto_drv->create_opts) { |
Max Reitz | f099887 | 2018-11-19 11:19:20 +0100 | [diff] [blame] | 327 | error_report("Protocol driver '%s' does not support image creation", |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 328 | proto_drv->format_name); |
Max Reitz | 3ecd5a4 | 2018-11-19 11:19:21 +0100 | [diff] [blame] | 329 | qemu_opts_free(create_opts); |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 330 | return 1; |
| 331 | } |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 332 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 333 | } |
| 334 | |
Max Reitz | f4619af | 2019-04-13 17:20:37 +0200 | [diff] [blame] | 335 | if (filename) { |
| 336 | printf("Supported options:\n"); |
| 337 | } else { |
| 338 | printf("Supported %s options:\n", fmt); |
| 339 | } |
Max Reitz | 6389871 | 2018-10-19 18:49:25 +0200 | [diff] [blame] | 340 | qemu_opts_print_help(create_opts, false); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 341 | qemu_opts_free(create_opts); |
Max Reitz | f4619af | 2019-04-13 17:20:37 +0200 | [diff] [blame] | 342 | |
| 343 | if (!filename) { |
| 344 | printf("\n" |
| 345 | "The protocol level may support further options.\n" |
| 346 | "Specify the target filename to include those options.\n"); |
| 347 | } |
| 348 | |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 349 | return 0; |
| 350 | } |
| 351 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 352 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 353 | static BlockBackend *img_open_opts(const char *optstr, |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 354 | QemuOpts *opts, int flags, bool writethrough, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 355 | bool quiet, bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 356 | { |
| 357 | QDict *options; |
| 358 | Error *local_err = NULL; |
| 359 | BlockBackend *blk; |
| 360 | options = qemu_opts_to_qdict(opts, NULL); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 361 | if (force_share) { |
| 362 | if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE) |
Max Reitz | 4615f87 | 2018-05-02 22:20:50 +0200 | [diff] [blame] | 363 | && strcmp(qdict_get_str(options, BDRV_OPT_FORCE_SHARE), "on")) { |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 364 | error_report("--force-share/-U conflicts with image options"); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 365 | qobject_unref(options); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 366 | return NULL; |
| 367 | } |
Max Reitz | 4615f87 | 2018-05-02 22:20:50 +0200 | [diff] [blame] | 368 | qdict_put_str(options, BDRV_OPT_FORCE_SHARE, "on"); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 369 | } |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 370 | blk = blk_new_open(NULL, NULL, options, flags, &local_err); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 371 | if (!blk) { |
Daniel P. Berrange | 143605a | 2016-04-06 10:16:18 +0100 | [diff] [blame] | 372 | error_reportf_err(local_err, "Could not open '%s': ", optstr); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 373 | return NULL; |
| 374 | } |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 375 | blk_set_enable_write_cache(blk, !writethrough); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 376 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 377 | return blk; |
| 378 | } |
| 379 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 380 | static BlockBackend *img_open_file(const char *filename, |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 381 | QDict *options, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 382 | const char *fmt, int flags, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 383 | bool writethrough, bool quiet, |
| 384 | bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 385 | { |
| 386 | BlockBackend *blk; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 387 | Error *local_err = NULL; |
Kevin Wolf | ad71713 | 2010-12-16 15:37:41 +0100 | [diff] [blame] | 388 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 389 | if (!options) { |
| 390 | options = qdict_new(); |
| 391 | } |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 392 | if (fmt) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 393 | qdict_put_str(options, "driver", fmt); |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 394 | } |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 395 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 396 | if (force_share) { |
Eric Blake | 579cf1d | 2017-05-15 14:54:39 -0500 | [diff] [blame] | 397 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 398 | } |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 399 | blk = blk_new_open(filename, NULL, options, flags, &local_err); |
Max Reitz | 5bd3132 | 2015-02-05 13:58:16 -0500 | [diff] [blame] | 400 | if (!blk) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 401 | error_reportf_err(local_err, "Could not open '%s': ", filename); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 402 | return NULL; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 403 | } |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 404 | blk_set_enable_write_cache(blk, !writethrough); |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 405 | |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 406 | return blk; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 409 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 410 | static int img_add_key_secrets(void *opaque, |
| 411 | const char *name, const char *value, |
| 412 | Error **errp) |
| 413 | { |
| 414 | QDict *options = opaque; |
| 415 | |
| 416 | if (g_str_has_suffix(name, "key-secret")) { |
Eric Blake | 187f47e | 2017-06-24 12:10:07 -0600 | [diff] [blame] | 417 | qdict_put_str(options, name, value); |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 423 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 424 | static BlockBackend *img_open(bool image_opts, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 425 | const char *filename, |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 426 | const char *fmt, int flags, bool writethrough, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 427 | bool quiet, bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 428 | { |
| 429 | BlockBackend *blk; |
| 430 | if (image_opts) { |
| 431 | QemuOpts *opts; |
| 432 | if (fmt) { |
| 433 | error_report("--image-opts and --format are mutually exclusive"); |
| 434 | return NULL; |
| 435 | } |
| 436 | opts = qemu_opts_parse_noisily(qemu_find_opts("source"), |
| 437 | filename, true); |
| 438 | if (!opts) { |
| 439 | return NULL; |
| 440 | } |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 441 | blk = img_open_opts(filename, opts, flags, writethrough, quiet, |
| 442 | force_share); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 443 | } else { |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 444 | blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 445 | force_share); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 446 | } |
| 447 | return blk; |
| 448 | } |
| 449 | |
| 450 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 451 | static int add_old_style_options(const char *fmt, QemuOpts *opts, |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 452 | const char *base_filename, |
| 453 | const char *base_fmt) |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 454 | { |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 455 | if (base_filename) { |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 456 | if (!qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, |
Markus Armbruster | 9e194e0 | 2020-07-07 18:06:11 +0200 | [diff] [blame] | 457 | NULL)) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 458 | error_report("Backing file not supported for file format '%s'", |
| 459 | fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 460 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | if (base_fmt) { |
Markus Armbruster | 9e194e0 | 2020-07-07 18:06:11 +0200 | [diff] [blame] | 464 | if (!qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, NULL)) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 465 | error_report("Backing file format not supported for file " |
| 466 | "format '%s'", fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 467 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 468 | } |
| 469 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 470 | return 0; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 471 | } |
| 472 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 473 | static int64_t cvtnum_full(const char *name, const char *value, int64_t min, |
| 474 | int64_t max) |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 475 | { |
Markus Armbruster | f17fd4f | 2017-02-21 21:14:06 +0100 | [diff] [blame] | 476 | int err; |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 477 | uint64_t res; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 478 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 479 | err = qemu_strtosz(value, NULL, &res); |
| 480 | if (err < 0 && err != -ERANGE) { |
| 481 | error_report("Invalid %s specified. You may use " |
| 482 | "k, M, G, T, P or E suffixes for", name); |
| 483 | error_report("kilobytes, megabytes, gigabytes, terabytes, " |
| 484 | "petabytes and exabytes."); |
Markus Armbruster | f17fd4f | 2017-02-21 21:14:06 +0100 | [diff] [blame] | 485 | return err; |
| 486 | } |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 487 | if (err == -ERANGE || res > max || res < min) { |
| 488 | error_report("Invalid %s specified. Must be between %" PRId64 |
| 489 | " and %" PRId64 ".", name, min, max); |
Markus Armbruster | f46bfdb | 2017-02-21 21:14:07 +0100 | [diff] [blame] | 490 | return -ERANGE; |
| 491 | } |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 492 | return res; |
| 493 | } |
| 494 | |
| 495 | static int64_t cvtnum(const char *name, const char *value) |
| 496 | { |
| 497 | return cvtnum_full(name, value, 0, INT64_MAX); |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 498 | } |
| 499 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 500 | static int img_create(int argc, char **argv) |
| 501 | { |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 502 | int c; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 503 | uint64_t img_size = -1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 504 | const char *fmt = "raw"; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 505 | const char *base_fmt = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 506 | const char *filename; |
| 507 | const char *base_filename = NULL; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 508 | char *options = NULL; |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 509 | Error *local_err = NULL; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 510 | bool quiet = false; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 511 | int flags = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 512 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 513 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 514 | static const struct option long_options[] = { |
| 515 | {"help", no_argument, 0, 'h'}, |
| 516 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 517 | {0, 0, 0, 0} |
| 518 | }; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 519 | c = getopt_long(argc, argv, ":F:b:f:ho:qu", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 520 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 521 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 522 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 523 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 524 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 525 | case ':': |
| 526 | missing_argument(argv[optind - 1]); |
| 527 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 528 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 529 | unrecognized_option(argv[optind - 1]); |
| 530 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 531 | case 'h': |
| 532 | help(); |
| 533 | break; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 534 | case 'F': |
| 535 | base_fmt = optarg; |
| 536 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 537 | case 'b': |
| 538 | base_filename = optarg; |
| 539 | break; |
| 540 | case 'f': |
| 541 | fmt = optarg; |
| 542 | break; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 543 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 544 | if (accumulate_options(&options, optarg) < 0) { |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 545 | goto fail; |
| 546 | } |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 547 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 548 | case 'q': |
| 549 | quiet = true; |
| 550 | break; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 551 | case 'u': |
| 552 | flags |= BDRV_O_NO_BACKING; |
| 553 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 554 | case OPTION_OBJECT: |
| 555 | user_creatable_process_cmdline(optarg); |
| 556 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 557 | } |
| 558 | } |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 559 | |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 560 | /* Get the filename */ |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 561 | filename = (optind < argc) ? argv[optind] : NULL; |
| 562 | if (options && has_help_option(options)) { |
| 563 | g_free(options); |
| 564 | return print_block_option_help(filename, fmt); |
| 565 | } |
| 566 | |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 567 | if (optind >= argc) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 568 | error_exit("Expecting image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 569 | } |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 570 | optind++; |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 571 | |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 572 | /* Get image size, if specified */ |
| 573 | if (optind < argc) { |
Jes Sorensen | 70b4f4b | 2011-01-05 11:41:02 +0100 | [diff] [blame] | 574 | int64_t sval; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 575 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 576 | sval = cvtnum("image size", argv[optind++]); |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 577 | if (sval < 0) { |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 578 | goto fail; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 579 | } |
| 580 | img_size = (uint64_t)sval; |
| 581 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 582 | if (optind != argc) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 583 | error_exit("Unexpected argument: %s", argv[optind]); |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 584 | } |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 585 | |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 586 | bdrv_img_create(filename, fmt, base_filename, base_fmt, |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 587 | options, img_size, flags, quiet, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 588 | if (local_err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 589 | error_reportf_err(local_err, "%s: ", filename); |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 590 | goto fail; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 591 | } |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 592 | |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 593 | g_free(options); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 594 | return 0; |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 595 | |
| 596 | fail: |
| 597 | g_free(options); |
| 598 | return 1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 601 | static void dump_json_image_check(ImageCheck *check, bool quiet) |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 602 | { |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 603 | GString *str; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 604 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 605 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 606 | |
| 607 | visit_type_ImageCheck(v, NULL, &check, &error_abort); |
| 608 | visit_complete(v, &obj); |
Markus Armbruster | 6589f45 | 2020-12-11 18:11:35 +0100 | [diff] [blame] | 609 | str = qobject_to_json_pretty(obj, true); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 610 | assert(str != NULL); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 611 | qprintf(quiet, "%s\n", str->str); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 612 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 613 | visit_free(v); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 614 | g_string_free(str, true); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 615 | } |
| 616 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 617 | static void dump_human_image_check(ImageCheck *check, bool quiet) |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 618 | { |
| 619 | if (!(check->corruptions || check->leaks || check->check_errors)) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 620 | qprintf(quiet, "No errors were found on the image.\n"); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 621 | } else { |
| 622 | if (check->corruptions) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 623 | qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n" |
| 624 | "Data may be corrupted, or further writes to the image " |
| 625 | "may corrupt it.\n", |
| 626 | check->corruptions); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | if (check->leaks) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 630 | qprintf(quiet, |
| 631 | "\n%" PRId64 " leaked clusters were found on the image.\n" |
| 632 | "This means waste of disk space, but no harm to data.\n", |
| 633 | check->leaks); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | if (check->check_errors) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 637 | qprintf(quiet, |
| 638 | "\n%" PRId64 |
| 639 | " internal errors have occurred during the check.\n", |
| 640 | check->check_errors); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | |
| 644 | if (check->total_clusters != 0 && check->allocated_clusters != 0) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 645 | qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, " |
| 646 | "%0.2f%% fragmented, %0.2f%% compressed clusters\n", |
| 647 | check->allocated_clusters, check->total_clusters, |
| 648 | check->allocated_clusters * 100.0 / check->total_clusters, |
| 649 | check->fragmented_clusters * 100.0 / check->allocated_clusters, |
| 650 | check->compressed_clusters * 100.0 / |
| 651 | check->allocated_clusters); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | if (check->image_end_offset) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 655 | qprintf(quiet, |
| 656 | "Image end offset: %" PRId64 "\n", check->image_end_offset); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | |
| 660 | static int collect_image_check(BlockDriverState *bs, |
| 661 | ImageCheck *check, |
| 662 | const char *filename, |
| 663 | const char *fmt, |
| 664 | int fix) |
| 665 | { |
| 666 | int ret; |
| 667 | BdrvCheckResult result; |
| 668 | |
| 669 | ret = bdrv_check(bs, &result, fix); |
| 670 | if (ret < 0) { |
| 671 | return ret; |
| 672 | } |
| 673 | |
| 674 | check->filename = g_strdup(filename); |
| 675 | check->format = g_strdup(bdrv_get_format_name(bs)); |
| 676 | check->check_errors = result.check_errors; |
| 677 | check->corruptions = result.corruptions; |
| 678 | check->has_corruptions = result.corruptions != 0; |
| 679 | check->leaks = result.leaks; |
| 680 | check->has_leaks = result.leaks != 0; |
| 681 | check->corruptions_fixed = result.corruptions_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 682 | check->has_corruptions_fixed = result.corruptions_fixed != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 683 | check->leaks_fixed = result.leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 684 | check->has_leaks_fixed = result.leaks_fixed != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 685 | check->image_end_offset = result.image_end_offset; |
| 686 | check->has_image_end_offset = result.image_end_offset != 0; |
| 687 | check->total_clusters = result.bfi.total_clusters; |
| 688 | check->has_total_clusters = result.bfi.total_clusters != 0; |
| 689 | check->allocated_clusters = result.bfi.allocated_clusters; |
| 690 | check->has_allocated_clusters = result.bfi.allocated_clusters != 0; |
| 691 | check->fragmented_clusters = result.bfi.fragmented_clusters; |
| 692 | check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0; |
Stefan Hajnoczi | e6439d7 | 2013-02-07 17:15:04 +0100 | [diff] [blame] | 693 | check->compressed_clusters = result.bfi.compressed_clusters; |
| 694 | check->has_compressed_clusters = result.bfi.compressed_clusters != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 695 | |
| 696 | return 0; |
| 697 | } |
| 698 | |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 699 | /* |
| 700 | * Checks an image for consistency. Exit codes: |
| 701 | * |
Max Reitz | d6635c4 | 2014-06-02 22:15:21 +0200 | [diff] [blame] | 702 | * 0 - Check completed, image is good |
| 703 | * 1 - Check not completed because of internal errors |
| 704 | * 2 - Check completed, image is corrupted |
| 705 | * 3 - Check completed, image has leaked clusters, but is good otherwise |
| 706 | * 63 - Checks are not supported by the image format |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 707 | */ |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 708 | static int img_check(int argc, char **argv) |
| 709 | { |
| 710 | int c, ret; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 711 | OutputFormat output_format = OFORMAT_HUMAN; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 712 | const char *filename, *fmt, *output, *cache; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 713 | BlockBackend *blk; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 714 | BlockDriverState *bs; |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 715 | int fix = 0; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 716 | int flags = BDRV_O_CHECK; |
| 717 | bool writethrough; |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 718 | ImageCheck *check; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 719 | bool quiet = false; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 720 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 721 | bool force_share = false; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 722 | |
| 723 | fmt = NULL; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 724 | output = NULL; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 725 | cache = BDRV_DEFAULT_CACHE; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 726 | |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 727 | for(;;) { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 728 | int option_index = 0; |
| 729 | static const struct option long_options[] = { |
| 730 | {"help", no_argument, 0, 'h'}, |
| 731 | {"format", required_argument, 0, 'f'}, |
Prasad Joshi | 4fd6a98 | 2014-03-25 00:08:54 +0530 | [diff] [blame] | 732 | {"repair", required_argument, 0, 'r'}, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 733 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 734 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 735 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 736 | {"force-share", no_argument, 0, 'U'}, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 737 | {0, 0, 0, 0} |
| 738 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 739 | c = getopt_long(argc, argv, ":hf:r:T:qU", |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 740 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 741 | if (c == -1) { |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 742 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 743 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 744 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 745 | case ':': |
| 746 | missing_argument(argv[optind - 1]); |
| 747 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 748 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 749 | unrecognized_option(argv[optind - 1]); |
| 750 | break; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 751 | case 'h': |
| 752 | help(); |
| 753 | break; |
| 754 | case 'f': |
| 755 | fmt = optarg; |
| 756 | break; |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 757 | case 'r': |
| 758 | flags |= BDRV_O_RDWR; |
| 759 | |
| 760 | if (!strcmp(optarg, "leaks")) { |
| 761 | fix = BDRV_FIX_LEAKS; |
| 762 | } else if (!strcmp(optarg, "all")) { |
| 763 | fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS; |
| 764 | } else { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 765 | error_exit("Unknown option value for -r " |
| 766 | "(expecting 'leaks' or 'all'): %s", optarg); |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 767 | } |
| 768 | break; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 769 | case OPTION_OUTPUT: |
| 770 | output = optarg; |
| 771 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 772 | case 'T': |
| 773 | cache = optarg; |
| 774 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 775 | case 'q': |
| 776 | quiet = true; |
| 777 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 778 | case 'U': |
| 779 | force_share = true; |
| 780 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 781 | case OPTION_OBJECT: |
| 782 | user_creatable_process_cmdline(optarg); |
| 783 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 784 | case OPTION_IMAGE_OPTS: |
| 785 | image_opts = true; |
| 786 | break; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 787 | } |
| 788 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 789 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 790 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 791 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 792 | filename = argv[optind++]; |
| 793 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 794 | if (output && !strcmp(output, "json")) { |
| 795 | output_format = OFORMAT_JSON; |
| 796 | } else if (output && !strcmp(output, "human")) { |
| 797 | output_format = OFORMAT_HUMAN; |
| 798 | } else if (output) { |
| 799 | error_report("--output must be used with human or json as argument."); |
| 800 | return 1; |
| 801 | } |
| 802 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 803 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 804 | if (ret < 0) { |
| 805 | error_report("Invalid source cache option: %s", cache); |
| 806 | return 1; |
| 807 | } |
| 808 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 809 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 810 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 811 | if (!blk) { |
| 812 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 813 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 814 | bs = blk_bs(blk); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 815 | |
| 816 | check = g_new0(ImageCheck, 1); |
| 817 | ret = collect_image_check(bs, check, filename, fmt, fix); |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 818 | |
| 819 | if (ret == -ENOTSUP) { |
Max Reitz | 55d492d | 2014-05-31 21:33:30 +0200 | [diff] [blame] | 820 | error_report("This image format does not support checks"); |
Peter Lieven | fefddf9 | 2013-10-24 08:53:34 +0200 | [diff] [blame] | 821 | ret = 63; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 822 | goto fail; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 823 | } |
| 824 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 825 | if (check->corruptions_fixed || check->leaks_fixed) { |
| 826 | int corruptions_fixed, leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 827 | bool has_leaks_fixed, has_corruptions_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 828 | |
| 829 | leaks_fixed = check->leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 830 | has_leaks_fixed = check->has_leaks_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 831 | corruptions_fixed = check->corruptions_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 832 | has_corruptions_fixed = check->has_corruptions_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 833 | |
| 834 | if (output_format == OFORMAT_HUMAN) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 835 | qprintf(quiet, |
| 836 | "The following inconsistencies were found and repaired:\n\n" |
| 837 | " %" PRId64 " leaked clusters\n" |
| 838 | " %" PRId64 " corruptions\n\n" |
| 839 | "Double checking the fixed image now...\n", |
| 840 | check->leaks_fixed, |
| 841 | check->corruptions_fixed); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 842 | } |
| 843 | |
Pan Nengyuan | fc124ea | 2020-02-27 09:29:50 +0800 | [diff] [blame] | 844 | qapi_free_ImageCheck(check); |
| 845 | check = g_new0(ImageCheck, 1); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 846 | ret = collect_image_check(bs, check, filename, fmt, 0); |
| 847 | |
| 848 | check->leaks_fixed = leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 849 | check->has_leaks_fixed = has_leaks_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 850 | check->corruptions_fixed = corruptions_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 851 | check->has_corruptions_fixed = has_corruptions_fixed; |
Kevin Wolf | ccf3471 | 2012-05-11 18:16:54 +0200 | [diff] [blame] | 852 | } |
| 853 | |
Max Reitz | 832390a | 2014-10-23 15:29:12 +0200 | [diff] [blame] | 854 | if (!ret) { |
| 855 | switch (output_format) { |
| 856 | case OFORMAT_HUMAN: |
| 857 | dump_human_image_check(check, quiet); |
| 858 | break; |
| 859 | case OFORMAT_JSON: |
| 860 | dump_json_image_check(check, quiet); |
| 861 | break; |
| 862 | } |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | if (ret || check->check_errors) { |
Max Reitz | 832390a | 2014-10-23 15:29:12 +0200 | [diff] [blame] | 866 | if (ret) { |
| 867 | error_report("Check failed: %s", strerror(-ret)); |
| 868 | } else { |
| 869 | error_report("Check failed"); |
| 870 | } |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 871 | ret = 1; |
| 872 | goto fail; |
| 873 | } |
| 874 | |
| 875 | if (check->corruptions) { |
| 876 | ret = 2; |
| 877 | } else if (check->leaks) { |
| 878 | ret = 3; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 879 | } else { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 880 | ret = 0; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 883 | fail: |
| 884 | qapi_free_ImageCheck(check); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 885 | blk_unref(blk); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 886 | return ret; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 889 | typedef struct CommonBlockJobCBInfo { |
| 890 | BlockDriverState *bs; |
| 891 | Error **errp; |
| 892 | } CommonBlockJobCBInfo; |
| 893 | |
| 894 | static void common_block_job_cb(void *opaque, int ret) |
| 895 | { |
| 896 | CommonBlockJobCBInfo *cbi = opaque; |
| 897 | |
| 898 | if (ret < 0) { |
| 899 | error_setg_errno(cbi->errp, -ret, "Block job failed"); |
| 900 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | static void run_block_job(BlockJob *job, Error **errp) |
| 904 | { |
Emanuele Giuseppe Esposito | a7b4f8f | 2021-06-14 10:11:29 +0200 | [diff] [blame] | 905 | uint64_t progress_current, progress_total; |
Vladimir Sementsov-Ogievskiy | df9a316 | 2021-05-06 17:13:53 +0300 | [diff] [blame] | 906 | AioContext *aio_context = block_job_get_aio_context(job); |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 907 | int ret = 0; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 908 | |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 909 | aio_context_acquire(aio_context); |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 910 | job_ref(&job->job); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 911 | do { |
Kevin Wolf | 30a5c88 | 2018-05-04 12:17:20 +0200 | [diff] [blame] | 912 | float progress = 0.0f; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 913 | aio_poll(aio_context, true); |
Emanuele Giuseppe Esposito | a7b4f8f | 2021-06-14 10:11:29 +0200 | [diff] [blame] | 914 | |
| 915 | progress_get_snapshot(&job->job.progress, &progress_current, |
| 916 | &progress_total); |
| 917 | if (progress_total) { |
| 918 | progress = (float)progress_current / progress_total * 100.f; |
Kevin Wolf | 30a5c88 | 2018-05-04 12:17:20 +0200 | [diff] [blame] | 919 | } |
| 920 | qemu_progress_print(progress, 0); |
Kevin Wolf | df956ae | 2018-04-25 15:09:58 +0200 | [diff] [blame] | 921 | } while (!job_is_ready(&job->job) && !job_is_completed(&job->job)); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 922 | |
Kevin Wolf | dbe5e6c | 2018-04-19 13:04:01 +0200 | [diff] [blame] | 923 | if (!job_is_completed(&job->job)) { |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 924 | ret = job_complete_sync(&job->job, errp); |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 925 | } else { |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 926 | ret = job->job.ret; |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 927 | } |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 928 | job_unref(&job->job); |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 929 | aio_context_release(aio_context); |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 930 | |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 931 | /* publish completion progress only when success */ |
| 932 | if (!ret) { |
| 933 | qemu_progress_print(100.f, 0); |
| 934 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 935 | } |
| 936 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 937 | static int img_commit(int argc, char **argv) |
| 938 | { |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 939 | int c, ret, flags; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 940 | const char *filename, *fmt, *cache, *base; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 941 | BlockBackend *blk; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 942 | BlockDriverState *bs, *base_bs; |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 943 | BlockJob *job; |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 944 | bool progress = false, quiet = false, drop = false; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 945 | bool writethrough; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 946 | Error *local_err = NULL; |
| 947 | CommonBlockJobCBInfo cbi; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 948 | bool image_opts = false; |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 949 | AioContext *aio_context; |
Zhengui | a0441b6 | 2020-10-20 14:47:43 +0000 | [diff] [blame] | 950 | int64_t rate_limit = 0; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 951 | |
| 952 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 953 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 954 | base = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 955 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 956 | static const struct option long_options[] = { |
| 957 | {"help", no_argument, 0, 'h'}, |
| 958 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 959 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 960 | {0, 0, 0, 0} |
| 961 | }; |
Zhengui | a0441b6 | 2020-10-20 14:47:43 +0000 | [diff] [blame] | 962 | c = getopt_long(argc, argv, ":f:ht:b:dpqr:", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 963 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 964 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 965 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 966 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 967 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 968 | case ':': |
| 969 | missing_argument(argv[optind - 1]); |
| 970 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 971 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 972 | unrecognized_option(argv[optind - 1]); |
| 973 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 974 | case 'h': |
| 975 | help(); |
| 976 | break; |
| 977 | case 'f': |
| 978 | fmt = optarg; |
| 979 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 980 | case 't': |
| 981 | cache = optarg; |
| 982 | break; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 983 | case 'b': |
| 984 | base = optarg; |
| 985 | /* -b implies -d */ |
| 986 | drop = true; |
| 987 | break; |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 988 | case 'd': |
| 989 | drop = true; |
| 990 | break; |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 991 | case 'p': |
| 992 | progress = true; |
| 993 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 994 | case 'q': |
| 995 | quiet = true; |
| 996 | break; |
Zhengui | a0441b6 | 2020-10-20 14:47:43 +0000 | [diff] [blame] | 997 | case 'r': |
| 998 | rate_limit = cvtnum("rate limit", optarg); |
| 999 | if (rate_limit < 0) { |
| 1000 | return 1; |
| 1001 | } |
| 1002 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 1003 | case OPTION_OBJECT: |
| 1004 | user_creatable_process_cmdline(optarg); |
| 1005 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1006 | case OPTION_IMAGE_OPTS: |
| 1007 | image_opts = true; |
| 1008 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1009 | } |
| 1010 | } |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 1011 | |
| 1012 | /* Progress is not shown in Quiet mode */ |
| 1013 | if (quiet) { |
| 1014 | progress = false; |
| 1015 | } |
| 1016 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 1017 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 1018 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1019 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1020 | filename = argv[optind++]; |
| 1021 | |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1022 | flags = BDRV_O_RDWR | BDRV_O_UNMAP; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1023 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1024 | if (ret < 0) { |
| 1025 | error_report("Invalid cache option: %s", cache); |
Stefan Hajnoczi | a3981eb | 2014-08-26 19:17:54 +0100 | [diff] [blame] | 1026 | return 1; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1027 | } |
| 1028 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1029 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 1030 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1031 | if (!blk) { |
| 1032 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1033 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1034 | bs = blk_bs(blk); |
| 1035 | |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 1036 | qemu_progress_init(progress, 1.f); |
| 1037 | qemu_progress_print(0.f, 100); |
| 1038 | |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1039 | if (base) { |
| 1040 | base_bs = bdrv_find_backing_image(bs, base); |
| 1041 | if (!base_bs) { |
Max Reitz | 6b33f3a | 2016-12-01 03:05:08 +0100 | [diff] [blame] | 1042 | error_setg(&local_err, |
| 1043 | "Did not find '%s' in the backing chain of '%s'", |
| 1044 | base, filename); |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1045 | goto done; |
| 1046 | } |
| 1047 | } else { |
| 1048 | /* This is different from QMP, which by default uses the deepest file in |
| 1049 | * the backing chain (i.e., the very base); however, the traditional |
| 1050 | * behavior of qemu-img commit is using the immediate backing file. */ |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 1051 | base_bs = bdrv_backing_chain_next(bs); |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1052 | if (!base_bs) { |
| 1053 | error_setg(&local_err, "Image does not have a backing file"); |
| 1054 | goto done; |
| 1055 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1058 | cbi = (CommonBlockJobCBInfo){ |
| 1059 | .errp = &local_err, |
| 1060 | .bs = bs, |
| 1061 | }; |
| 1062 | |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 1063 | aio_context = bdrv_get_aio_context(bs); |
| 1064 | aio_context_acquire(aio_context); |
Zhengui | a0441b6 | 2020-10-20 14:47:43 +0000 | [diff] [blame] | 1065 | commit_active_start("commit", bs, base_bs, JOB_DEFAULT, rate_limit, |
Kevin Wolf | 0db832f | 2017-02-20 18:10:05 +0100 | [diff] [blame] | 1066 | BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb, |
Fam Zheng | 78bbd91 | 2017-04-21 20:27:04 +0800 | [diff] [blame] | 1067 | &cbi, false, &local_err); |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 1068 | aio_context_release(aio_context); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1069 | if (local_err) { |
| 1070 | goto done; |
| 1071 | } |
| 1072 | |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1073 | /* When the block job completes, the BlockBackend reference will point to |
| 1074 | * the old backing file. In order to avoid that the top image is already |
| 1075 | * deleted, so we can still empty it afterwards, increment the reference |
| 1076 | * counter here preemptively. */ |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1077 | if (!drop) { |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1078 | bdrv_ref(bs); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1079 | } |
| 1080 | |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 1081 | job = block_job_get("commit"); |
Liam Merwick | 2e2db26 | 2018-11-05 21:38:37 +0000 | [diff] [blame] | 1082 | assert(job); |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 1083 | run_block_job(job, &local_err); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1084 | if (local_err) { |
| 1085 | goto unref_backing; |
| 1086 | } |
| 1087 | |
Max Reitz | 2d97fde | 2020-04-29 16:11:26 +0200 | [diff] [blame] | 1088 | if (!drop) { |
| 1089 | BlockBackend *old_backing_blk; |
| 1090 | |
| 1091 | old_backing_blk = blk_new_with_bs(bs, BLK_PERM_WRITE, BLK_PERM_ALL, |
| 1092 | &local_err); |
| 1093 | if (!old_backing_blk) { |
| 1094 | goto unref_backing; |
| 1095 | } |
| 1096 | ret = blk_make_empty(old_backing_blk, &local_err); |
| 1097 | blk_unref(old_backing_blk); |
| 1098 | if (ret == -ENOTSUP) { |
| 1099 | error_free(local_err); |
| 1100 | local_err = NULL; |
| 1101 | } else if (ret < 0) { |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1102 | goto unref_backing; |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | unref_backing: |
| 1107 | if (!drop) { |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1108 | bdrv_unref(bs); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1109 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1110 | |
| 1111 | done: |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 1112 | qemu_progress_end(); |
| 1113 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1114 | blk_unref(blk); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1115 | |
| 1116 | if (local_err) { |
Markus Armbruster | 6936f29 | 2015-02-10 15:14:02 +0100 | [diff] [blame] | 1117 | error_report_err(local_err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1118 | return 1; |
| 1119 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1120 | |
| 1121 | qprintf(quiet, "Image committed.\n"); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1122 | return 0; |
| 1123 | } |
| 1124 | |
Dmitry Konishchev | f6a00aa | 2011-05-18 15:03:59 +0400 | [diff] [blame] | 1125 | /* |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1126 | * Returns -1 if 'buf' contains only zeroes, otherwise the byte index |
| 1127 | * of the first sector boundary within buf where the sector contains a |
| 1128 | * non-zero byte. This function is robust to a buffer that is not |
| 1129 | * sector-aligned. |
| 1130 | */ |
| 1131 | static int64_t find_nonzero(const uint8_t *buf, int64_t n) |
| 1132 | { |
| 1133 | int64_t i; |
| 1134 | int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE); |
| 1135 | |
| 1136 | for (i = 0; i < end; i += BDRV_SECTOR_SIZE) { |
| 1137 | if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) { |
| 1138 | return i; |
| 1139 | } |
| 1140 | } |
| 1141 | if (i < n && !buffer_is_zero(buf + i, n - end)) { |
| 1142 | return i; |
| 1143 | } |
| 1144 | return -1; |
| 1145 | } |
| 1146 | |
| 1147 | /* |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1148 | * Returns true iff the first sector pointed to by 'buf' contains at least |
| 1149 | * a non-NUL byte. |
| 1150 | * |
| 1151 | * 'pnum' is set to the number of sectors (including and immediately following |
| 1152 | * 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] | 1153 | * The function will try to align the end offset to alignment boundaries so |
zhaolichang | e3a6e0d | 2020-09-17 15:50:20 +0800 | [diff] [blame] | 1154 | * that the request will at least end aligned and consecutive requests will |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1155 | * also start at an aligned offset. |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1156 | */ |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1157 | static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum, |
| 1158 | int64_t sector_num, int alignment) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1159 | { |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1160 | bool is_zero; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1161 | int i, tail; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1162 | |
| 1163 | if (n <= 0) { |
| 1164 | *pnum = 0; |
| 1165 | return 0; |
| 1166 | } |
Yi Li | c075c42 | 2020-08-19 09:36:07 +0800 | [diff] [blame] | 1167 | is_zero = buffer_is_zero(buf, BDRV_SECTOR_SIZE); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1168 | for(i = 1; i < n; i++) { |
Yi Li | c075c42 | 2020-08-19 09:36:07 +0800 | [diff] [blame] | 1169 | buf += BDRV_SECTOR_SIZE; |
| 1170 | if (is_zero != buffer_is_zero(buf, BDRV_SECTOR_SIZE)) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1171 | break; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1172 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1173 | } |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1174 | |
Vladimir Sementsov-Ogievskiy | 96054c7 | 2021-12-17 17:46:54 +0100 | [diff] [blame] | 1175 | if (i == n) { |
| 1176 | /* |
| 1177 | * The whole buf is the same. |
| 1178 | * No reason to split it into chunks, so return now. |
| 1179 | */ |
| 1180 | *pnum = i; |
| 1181 | return !is_zero; |
| 1182 | } |
| 1183 | |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1184 | tail = (sector_num + i) & (alignment - 1); |
| 1185 | if (tail) { |
| 1186 | if (is_zero && i <= tail) { |
Vladimir Sementsov-Ogievskiy | 96054c7 | 2021-12-17 17:46:54 +0100 | [diff] [blame] | 1187 | /* |
| 1188 | * For sure next sector after i is data, and it will rewrite this |
| 1189 | * tail anyway due to RMW. So, let's just write data now. |
| 1190 | */ |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1191 | is_zero = false; |
| 1192 | } |
| 1193 | if (!is_zero) { |
Vladimir Sementsov-Ogievskiy | 96054c7 | 2021-12-17 17:46:54 +0100 | [diff] [blame] | 1194 | /* If possible, align up end offset of allocated areas. */ |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1195 | i += alignment - tail; |
| 1196 | i = MIN(i, n); |
| 1197 | } else { |
Vladimir Sementsov-Ogievskiy | 96054c7 | 2021-12-17 17:46:54 +0100 | [diff] [blame] | 1198 | /* |
| 1199 | * For sure next sector after i is data, and it will rewrite this |
| 1200 | * tail anyway due to RMW. Better is avoid RMW and write zeroes up |
| 1201 | * to aligned bound. |
| 1202 | */ |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1203 | i -= tail; |
| 1204 | } |
| 1205 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1206 | *pnum = i; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1207 | return !is_zero; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1210 | /* |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1211 | * Like is_allocated_sectors, but if the buffer starts with a used sector, |
| 1212 | * up to 'min' consecutive sectors containing zeros are ignored. This avoids |
| 1213 | * breaking up write requests for only small sparse areas. |
| 1214 | */ |
| 1215 | 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] | 1216 | int min, int64_t sector_num, int alignment) |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1217 | { |
| 1218 | int ret; |
| 1219 | int num_checked, num_used; |
| 1220 | |
| 1221 | if (n < min) { |
| 1222 | min = n; |
| 1223 | } |
| 1224 | |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1225 | ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment); |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1226 | if (!ret) { |
| 1227 | return ret; |
| 1228 | } |
| 1229 | |
| 1230 | num_used = *pnum; |
| 1231 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 1232 | n -= *pnum; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1233 | sector_num += *pnum; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1234 | num_checked = num_used; |
| 1235 | |
| 1236 | while (n > 0) { |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1237 | ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment); |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1238 | |
| 1239 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 1240 | n -= *pnum; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1241 | sector_num += *pnum; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1242 | num_checked += *pnum; |
| 1243 | if (ret) { |
| 1244 | num_used = num_checked; |
| 1245 | } else if (*pnum >= min) { |
| 1246 | break; |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | *pnum = num_used; |
| 1251 | return 1; |
| 1252 | } |
| 1253 | |
| 1254 | /* |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1255 | * Compares two buffers sector by sector. Returns 0 if the first |
| 1256 | * sector of each buffer matches, non-zero otherwise. |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1257 | * |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1258 | * pnum is set to the sector-aligned size of the buffer prefix that |
| 1259 | * has the same matching status as the first sector. |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1260 | */ |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1261 | static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2, |
| 1262 | int64_t bytes, int64_t *pnum) |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1263 | { |
Radim Krčmář | 8c1ac47 | 2015-02-20 17:06:15 +0100 | [diff] [blame] | 1264 | bool res; |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1265 | int64_t i = MIN(bytes, BDRV_SECTOR_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1266 | |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1267 | assert(bytes > 0); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1268 | |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1269 | res = !!memcmp(buf1, buf2, i); |
| 1270 | while (i < bytes) { |
| 1271 | int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1272 | |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1273 | if (!!memcmp(buf1 + i, buf2 + i, len) != res) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1274 | break; |
| 1275 | } |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1276 | i += len; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | *pnum = i; |
| 1280 | return res; |
| 1281 | } |
| 1282 | |
Stefano Garzarella | 97ede57 | 2019-05-08 12:43:24 +0200 | [diff] [blame] | 1283 | #define IO_BUF_SIZE (2 * MiB) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1284 | |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1285 | /* |
| 1286 | * Check if passed sectors are empty (not allocated or contain only 0 bytes) |
| 1287 | * |
Eric Blake | 0608e40 | 2017-10-11 22:47:12 -0500 | [diff] [blame] | 1288 | * Intended for use by 'qemu-img compare': Returns 0 in case sectors are |
| 1289 | * filled with 0, 1 if sectors contain non-zero data (this is a comparison |
| 1290 | * failure), and 4 on error (the exit status for read errors), after emitting |
| 1291 | * an error message. |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1292 | * |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1293 | * @param blk: BlockBackend for the image |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1294 | * @param offset: Starting offset to check |
| 1295 | * @param bytes: Number of bytes to check |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1296 | * @param filename: Name of disk file we are checking (logging purpose) |
| 1297 | * @param buffer: Allocated buffer for storing read data |
| 1298 | * @param quiet: Flag for quiet mode |
| 1299 | */ |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1300 | static int check_empty_sectors(BlockBackend *blk, int64_t offset, |
| 1301 | int64_t bytes, const char *filename, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1302 | uint8_t *buffer, bool quiet) |
| 1303 | { |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1304 | int ret = 0; |
| 1305 | int64_t idx; |
| 1306 | |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1307 | ret = blk_pread(blk, offset, buffer, bytes); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1308 | if (ret < 0) { |
| 1309 | error_report("Error while reading offset %" PRId64 " of %s: %s", |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1310 | offset, filename, strerror(-ret)); |
Eric Blake | 0608e40 | 2017-10-11 22:47:12 -0500 | [diff] [blame] | 1311 | return 4; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1312 | } |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1313 | idx = find_nonzero(buffer, bytes); |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1314 | if (idx >= 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1315 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1316 | offset + idx); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1317 | return 1; |
| 1318 | } |
| 1319 | |
| 1320 | return 0; |
| 1321 | } |
| 1322 | |
| 1323 | /* |
| 1324 | * Compares two images. Exit codes: |
| 1325 | * |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 1326 | * 0 - Images are identical or the requested help was printed |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1327 | * 1 - Images differ |
| 1328 | * >1 - Error occurred |
| 1329 | */ |
| 1330 | static int img_compare(int argc, char **argv) |
| 1331 | { |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1332 | const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1333 | BlockBackend *blk1, *blk2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1334 | BlockDriverState *bs1, *bs2; |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1335 | int64_t total_size1, total_size2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1336 | uint8_t *buf1 = NULL, *buf2 = NULL; |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1337 | int64_t pnum1, pnum2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1338 | int allocated1, allocated2; |
| 1339 | int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */ |
| 1340 | bool progress = false, quiet = false, strict = false; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1341 | int flags; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1342 | bool writethrough; |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1343 | int64_t total_size; |
| 1344 | int64_t offset = 0; |
| 1345 | int64_t chunk; |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1346 | int c; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1347 | uint64_t progress_base; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1348 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1349 | bool force_share = false; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1350 | |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1351 | cache = BDRV_DEFAULT_CACHE; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1352 | for (;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1353 | static const struct option long_options[] = { |
| 1354 | {"help", no_argument, 0, 'h'}, |
| 1355 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1356 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1357 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1358 | {0, 0, 0, 0} |
| 1359 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1360 | c = getopt_long(argc, argv, ":hf:F:T:pqsU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1361 | long_options, NULL); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1362 | if (c == -1) { |
| 1363 | break; |
| 1364 | } |
| 1365 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 1366 | case ':': |
| 1367 | missing_argument(argv[optind - 1]); |
| 1368 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1369 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 1370 | unrecognized_option(argv[optind - 1]); |
| 1371 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1372 | case 'h': |
| 1373 | help(); |
| 1374 | break; |
| 1375 | case 'f': |
| 1376 | fmt1 = optarg; |
| 1377 | break; |
| 1378 | case 'F': |
| 1379 | fmt2 = optarg; |
| 1380 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1381 | case 'T': |
| 1382 | cache = optarg; |
| 1383 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1384 | case 'p': |
| 1385 | progress = true; |
| 1386 | break; |
| 1387 | case 'q': |
| 1388 | quiet = true; |
| 1389 | break; |
| 1390 | case 's': |
| 1391 | strict = true; |
| 1392 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1393 | case 'U': |
| 1394 | force_share = true; |
| 1395 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 1396 | case OPTION_OBJECT: |
| 1397 | { |
| 1398 | Error *local_err = NULL; |
| 1399 | |
| 1400 | if (!user_creatable_add_from_str(optarg, &local_err)) { |
| 1401 | if (local_err) { |
| 1402 | error_report_err(local_err); |
| 1403 | exit(2); |
| 1404 | } else { |
| 1405 | /* Help was printed */ |
| 1406 | exit(EXIT_SUCCESS); |
| 1407 | } |
| 1408 | } |
| 1409 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1410 | } |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1411 | case OPTION_IMAGE_OPTS: |
| 1412 | image_opts = true; |
| 1413 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | /* Progress is not shown in Quiet mode */ |
| 1418 | if (quiet) { |
| 1419 | progress = false; |
| 1420 | } |
| 1421 | |
| 1422 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 1423 | if (optind != argc - 2) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 1424 | error_exit("Expecting two image file names"); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1425 | } |
| 1426 | filename1 = argv[optind++]; |
| 1427 | filename2 = argv[optind++]; |
| 1428 | |
Stefan Hajnoczi | cbda016 | 2014-08-26 19:17:55 +0100 | [diff] [blame] | 1429 | /* Initialize before goto out */ |
| 1430 | qemu_progress_init(progress, 2.0); |
| 1431 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1432 | flags = 0; |
| 1433 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1434 | if (ret < 0) { |
| 1435 | error_report("Invalid source cache option: %s", cache); |
| 1436 | ret = 2; |
| 1437 | goto out3; |
| 1438 | } |
| 1439 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1440 | blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet, |
| 1441 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1442 | if (!blk1) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1443 | ret = 2; |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1444 | goto out3; |
| 1445 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1446 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1447 | blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet, |
| 1448 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1449 | if (!blk2) { |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1450 | ret = 2; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1451 | goto out2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1452 | } |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1453 | bs1 = blk_bs(blk1); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1454 | bs2 = blk_bs(blk2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1455 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1456 | buf1 = blk_blockalign(blk1, IO_BUF_SIZE); |
| 1457 | buf2 = blk_blockalign(blk2, IO_BUF_SIZE); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1458 | total_size1 = blk_getlength(blk1); |
| 1459 | if (total_size1 < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1460 | error_report("Can't get size of %s: %s", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1461 | filename1, strerror(-total_size1)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1462 | ret = 4; |
| 1463 | goto out; |
| 1464 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1465 | total_size2 = blk_getlength(blk2); |
| 1466 | if (total_size2 < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1467 | error_report("Can't get size of %s: %s", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1468 | filename2, strerror(-total_size2)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1469 | ret = 4; |
| 1470 | goto out; |
| 1471 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1472 | total_size = MIN(total_size1, total_size2); |
| 1473 | progress_base = MAX(total_size1, total_size2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1474 | |
| 1475 | qemu_progress_print(0, 100); |
| 1476 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1477 | if (strict && total_size1 != total_size2) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1478 | ret = 1; |
| 1479 | qprintf(quiet, "Strict mode: Image size mismatch!\n"); |
| 1480 | goto out; |
| 1481 | } |
| 1482 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1483 | while (offset < total_size) { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1484 | int status1, status2; |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 1485 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1486 | status1 = bdrv_block_status_above(bs1, NULL, offset, |
| 1487 | total_size1 - offset, &pnum1, NULL, |
| 1488 | NULL); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1489 | if (status1 < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1490 | ret = 3; |
| 1491 | error_report("Sector allocation test failed for %s", filename1); |
| 1492 | goto out; |
| 1493 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1494 | allocated1 = status1 & BDRV_BLOCK_ALLOCATED; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1495 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1496 | status2 = bdrv_block_status_above(bs2, NULL, offset, |
| 1497 | total_size2 - offset, &pnum2, NULL, |
| 1498 | NULL); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1499 | if (status2 < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1500 | ret = 3; |
| 1501 | error_report("Sector allocation test failed for %s", filename2); |
| 1502 | goto out; |
| 1503 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1504 | allocated2 = status2 & BDRV_BLOCK_ALLOCATED; |
Eric Blake | 7daddc6 | 2017-10-11 22:47:09 -0500 | [diff] [blame] | 1505 | |
| 1506 | assert(pnum1 && pnum2); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1507 | chunk = MIN(pnum1, pnum2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1508 | |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1509 | if (strict) { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1510 | if (status1 != status2) { |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1511 | ret = 1; |
| 1512 | qprintf(quiet, "Strict mode: Offset %" PRId64 |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1513 | " block status mismatch!\n", offset); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1514 | goto out; |
| 1515 | } |
| 1516 | } |
| 1517 | if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) { |
Eric Blake | 7daddc6 | 2017-10-11 22:47:09 -0500 | [diff] [blame] | 1518 | /* nothing to do */ |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1519 | } else if (allocated1 == allocated2) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1520 | if (allocated1) { |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1521 | int64_t pnum; |
| 1522 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1523 | chunk = MIN(chunk, IO_BUF_SIZE); |
| 1524 | ret = blk_pread(blk1, offset, buf1, chunk); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1525 | if (ret < 0) { |
| 1526 | error_report("Error while reading offset %" PRId64 |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1527 | " of %s: %s", |
| 1528 | offset, filename1, strerror(-ret)); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1529 | ret = 4; |
| 1530 | goto out; |
| 1531 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1532 | ret = blk_pread(blk2, offset, buf2, chunk); |
| 1533 | if (ret < 0) { |
| 1534 | error_report("Error while reading offset %" PRId64 |
| 1535 | " of %s: %s", |
| 1536 | offset, filename2, strerror(-ret)); |
| 1537 | ret = 4; |
| 1538 | goto out; |
| 1539 | } |
| 1540 | ret = compare_buffers(buf1, buf2, chunk, &pnum); |
| 1541 | if (ret || pnum != chunk) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1542 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1543 | offset + (ret ? 0 : pnum)); |
Fam Zheng | 36452f1 | 2013-11-13 20:26:49 +0800 | [diff] [blame] | 1544 | ret = 1; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1545 | goto out; |
| 1546 | } |
| 1547 | } |
| 1548 | } else { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1549 | chunk = MIN(chunk, IO_BUF_SIZE); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1550 | if (allocated1) { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1551 | ret = check_empty_sectors(blk1, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1552 | filename1, buf1, quiet); |
| 1553 | } else { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1554 | ret = check_empty_sectors(blk2, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1555 | filename2, buf1, quiet); |
| 1556 | } |
| 1557 | if (ret) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1558 | goto out; |
| 1559 | } |
| 1560 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1561 | offset += chunk; |
| 1562 | qemu_progress_print(((float) chunk / progress_base) * 100, 100); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1563 | } |
| 1564 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1565 | if (total_size1 != total_size2) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1566 | BlockBackend *blk_over; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1567 | const char *filename_over; |
| 1568 | |
| 1569 | qprintf(quiet, "Warning: Image size mismatch!\n"); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1570 | if (total_size1 > total_size2) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1571 | blk_over = blk1; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1572 | filename_over = filename1; |
| 1573 | } else { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1574 | blk_over = blk2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1575 | filename_over = filename2; |
| 1576 | } |
| 1577 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1578 | while (offset < progress_base) { |
| 1579 | ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset, |
| 1580 | progress_base - offset, &chunk, |
| 1581 | NULL, NULL); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1582 | if (ret < 0) { |
| 1583 | ret = 3; |
| 1584 | error_report("Sector allocation test failed for %s", |
| 1585 | filename_over); |
| 1586 | goto out; |
| 1587 | |
| 1588 | } |
Eric Blake | 391cb1a | 2017-10-11 22:47:10 -0500 | [diff] [blame] | 1589 | if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1590 | chunk = MIN(chunk, IO_BUF_SIZE); |
| 1591 | ret = check_empty_sectors(blk_over, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1592 | filename_over, buf1, quiet); |
| 1593 | if (ret) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1594 | goto out; |
| 1595 | } |
| 1596 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1597 | offset += chunk; |
| 1598 | qemu_progress_print(((float) chunk / progress_base) * 100, 100); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1599 | } |
| 1600 | } |
| 1601 | |
| 1602 | qprintf(quiet, "Images are identical.\n"); |
| 1603 | ret = 0; |
| 1604 | |
| 1605 | out: |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1606 | qemu_vfree(buf1); |
| 1607 | qemu_vfree(buf2); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1608 | blk_unref(blk2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1609 | out2: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1610 | blk_unref(blk1); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1611 | out3: |
| 1612 | qemu_progress_end(); |
| 1613 | return ret; |
| 1614 | } |
| 1615 | |
Eric Blake | 6c729dd | 2020-05-21 14:21:35 -0500 | [diff] [blame] | 1616 | /* Convenience wrapper around qmp_block_dirty_bitmap_merge */ |
| 1617 | static void do_dirty_bitmap_merge(const char *dst_node, const char *dst_name, |
| 1618 | const char *src_node, const char *src_name, |
| 1619 | Error **errp) |
| 1620 | { |
| 1621 | BlockDirtyBitmapMergeSource *merge_src; |
Eric Blake | 54aa3de | 2020-11-12 19:13:37 -0600 | [diff] [blame] | 1622 | BlockDirtyBitmapMergeSourceList *list = NULL; |
Eric Blake | 6c729dd | 2020-05-21 14:21:35 -0500 | [diff] [blame] | 1623 | |
| 1624 | merge_src = g_new0(BlockDirtyBitmapMergeSource, 1); |
| 1625 | merge_src->type = QTYPE_QDICT; |
| 1626 | merge_src->u.external.node = g_strdup(src_node); |
| 1627 | merge_src->u.external.name = g_strdup(src_name); |
Eric Blake | 54aa3de | 2020-11-12 19:13:37 -0600 | [diff] [blame] | 1628 | QAPI_LIST_PREPEND(list, merge_src); |
Eric Blake | 6c729dd | 2020-05-21 14:21:35 -0500 | [diff] [blame] | 1629 | qmp_block_dirty_bitmap_merge(dst_node, dst_name, list, errp); |
| 1630 | qapi_free_BlockDirtyBitmapMergeSourceList(list); |
| 1631 | } |
| 1632 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1633 | enum ImgConvertBlockStatus { |
| 1634 | BLK_DATA, |
| 1635 | BLK_ZERO, |
| 1636 | BLK_BACKING_FILE, |
| 1637 | }; |
| 1638 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1639 | #define MAX_COROUTINES 16 |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 1640 | #define CONVERT_THROTTLE_GROUP "img_convert" |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1641 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1642 | typedef struct ImgConvertState { |
| 1643 | BlockBackend **src; |
| 1644 | int64_t *src_sectors; |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 1645 | int *src_alignment; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1646 | int src_num; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1647 | int64_t total_sectors; |
| 1648 | int64_t allocated_sectors; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1649 | int64_t allocated_done; |
| 1650 | int64_t sector_num; |
| 1651 | int64_t wr_offs; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1652 | enum ImgConvertBlockStatus status; |
| 1653 | int64_t sector_next_status; |
| 1654 | BlockBackend *target; |
| 1655 | bool has_zero_init; |
| 1656 | bool compressed; |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 1657 | bool target_is_new; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1658 | bool target_has_backing; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1659 | int64_t target_backing_sectors; /* negative if unknown */ |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1660 | bool wr_in_order; |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1661 | bool copy_range; |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1662 | bool salvage; |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 1663 | bool quiet; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1664 | int min_sparse; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1665 | int alignment; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1666 | size_t cluster_sectors; |
| 1667 | size_t buf_sectors; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 1668 | long num_coroutines; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1669 | int running_coroutines; |
| 1670 | Coroutine *co[MAX_COROUTINES]; |
| 1671 | int64_t wait_sector_num[MAX_COROUTINES]; |
| 1672 | CoMutex lock; |
| 1673 | int ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1674 | } ImgConvertState; |
| 1675 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1676 | static void convert_select_part(ImgConvertState *s, int64_t sector_num, |
| 1677 | int *src_cur, int64_t *src_cur_offset) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1678 | { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1679 | *src_cur = 0; |
| 1680 | *src_cur_offset = 0; |
| 1681 | while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) { |
| 1682 | *src_cur_offset += s->src_sectors[*src_cur]; |
| 1683 | (*src_cur)++; |
| 1684 | assert(*src_cur < s->src_num); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1685 | } |
| 1686 | } |
| 1687 | |
| 1688 | static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num) |
| 1689 | { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1690 | int64_t src_cur_offset; |
| 1691 | int ret, n, src_cur; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1692 | bool post_backing_zero = false; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1693 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1694 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1695 | |
| 1696 | assert(s->total_sectors > sector_num); |
| 1697 | n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS); |
| 1698 | |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1699 | if (s->target_backing_sectors >= 0) { |
| 1700 | if (sector_num >= s->target_backing_sectors) { |
Vladimir Sementsov-Ogievskiy | 2253d86 | 2020-05-28 12:43:56 +0300 | [diff] [blame] | 1701 | post_backing_zero = true; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1702 | } else if (sector_num + n > s->target_backing_sectors) { |
| 1703 | /* Split requests around target_backing_sectors (because |
| 1704 | * starting from there, zeros are handled differently) */ |
| 1705 | n = s->target_backing_sectors - sector_num; |
| 1706 | } |
| 1707 | } |
| 1708 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1709 | if (s->sector_next_status <= sector_num) { |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1710 | uint64_t offset = (sector_num - src_cur_offset) * BDRV_SECTOR_SIZE; |
| 1711 | int64_t count; |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 1712 | int tail; |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 1713 | BlockDriverState *src_bs = blk_bs(s->src[src_cur]); |
| 1714 | BlockDriverState *base; |
| 1715 | |
| 1716 | if (s->target_has_backing) { |
| 1717 | base = bdrv_cow_bs(bdrv_skip_filters(src_bs)); |
| 1718 | } else { |
| 1719 | base = NULL; |
| 1720 | } |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1721 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1722 | do { |
| 1723 | count = n * BDRV_SECTOR_SIZE; |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 1724 | |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 1725 | ret = bdrv_block_status_above(src_bs, base, offset, count, &count, |
| 1726 | NULL, NULL); |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1727 | |
| 1728 | if (ret < 0) { |
| 1729 | if (s->salvage) { |
| 1730 | if (n == 1) { |
| 1731 | if (!s->quiet) { |
| 1732 | warn_report("error while reading block status at " |
| 1733 | "offset %" PRIu64 ": %s", offset, |
| 1734 | strerror(-ret)); |
| 1735 | } |
| 1736 | /* Just try to read the data, then */ |
| 1737 | ret = BDRV_BLOCK_DATA; |
| 1738 | count = BDRV_SECTOR_SIZE; |
| 1739 | } else { |
| 1740 | /* Retry on a shorter range */ |
| 1741 | n = DIV_ROUND_UP(n, 4); |
| 1742 | } |
| 1743 | } else { |
| 1744 | error_report("error while reading block status at offset " |
| 1745 | "%" PRIu64 ": %s", offset, strerror(-ret)); |
| 1746 | return ret; |
| 1747 | } |
| 1748 | } |
| 1749 | } while (ret < 0); |
| 1750 | |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1751 | n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1752 | |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 1753 | /* |
| 1754 | * Avoid that s->sector_next_status becomes unaligned to the source |
| 1755 | * request alignment and/or cluster size to avoid unnecessary read |
| 1756 | * cycles. |
| 1757 | */ |
| 1758 | tail = (sector_num - src_cur_offset + n) % s->src_alignment[src_cur]; |
| 1759 | if (n > tail) { |
| 1760 | n -= tail; |
| 1761 | } |
| 1762 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1763 | if (ret & BDRV_BLOCK_ZERO) { |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1764 | s->status = post_backing_zero ? BLK_BACKING_FILE : BLK_ZERO; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1765 | } else if (ret & BDRV_BLOCK_DATA) { |
| 1766 | s->status = BLK_DATA; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1767 | } else { |
Vladimir Sementsov-Ogievskiy | 9f1b92a | 2017-04-07 14:34:04 +0300 | [diff] [blame] | 1768 | s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | s->sector_next_status = sector_num + n; |
| 1772 | } |
| 1773 | |
| 1774 | n = MIN(n, s->sector_next_status - sector_num); |
| 1775 | if (s->status == BLK_DATA) { |
| 1776 | n = MIN(n, s->buf_sectors); |
| 1777 | } |
| 1778 | |
| 1779 | /* We need to write complete clusters for compressed images, so if an |
| 1780 | * unallocated area is shorter than that, we must consider the whole |
| 1781 | * cluster allocated. */ |
| 1782 | if (s->compressed) { |
| 1783 | if (n < s->cluster_sectors) { |
| 1784 | n = MIN(s->cluster_sectors, s->total_sectors - sector_num); |
| 1785 | s->status = BLK_DATA; |
| 1786 | } else { |
| 1787 | n = QEMU_ALIGN_DOWN(n, s->cluster_sectors); |
| 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | return n; |
| 1792 | } |
| 1793 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1794 | static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num, |
| 1795 | int nb_sectors, uint8_t *buf) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1796 | { |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1797 | uint64_t single_read_until = 0; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1798 | int n, ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1799 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1800 | assert(nb_sectors <= s->buf_sectors); |
| 1801 | while (nb_sectors > 0) { |
| 1802 | BlockBackend *blk; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1803 | int src_cur; |
| 1804 | int64_t bs_sectors, src_cur_offset; |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1805 | uint64_t offset; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1806 | |
| 1807 | /* In the case of compression with multiple source files, we can get a |
| 1808 | * nb_sectors that spreads into the next part. So we must be able to |
| 1809 | * read across multiple BDSes for one convert_read() call. */ |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1810 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
| 1811 | blk = s->src[src_cur]; |
| 1812 | bs_sectors = s->src_sectors[src_cur]; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1813 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1814 | offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1815 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1816 | n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset)); |
| 1817 | if (single_read_until > offset) { |
| 1818 | n = 1; |
| 1819 | } |
| 1820 | |
| 1821 | ret = blk_co_pread(blk, offset, n << BDRV_SECTOR_BITS, buf, 0); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1822 | if (ret < 0) { |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1823 | if (s->salvage) { |
| 1824 | if (n > 1) { |
| 1825 | single_read_until = offset + (n << BDRV_SECTOR_BITS); |
| 1826 | continue; |
| 1827 | } else { |
| 1828 | if (!s->quiet) { |
| 1829 | warn_report("error while reading offset %" PRIu64 |
| 1830 | ": %s", offset, strerror(-ret)); |
| 1831 | } |
| 1832 | memset(buf, 0, BDRV_SECTOR_SIZE); |
| 1833 | } |
| 1834 | } else { |
| 1835 | return ret; |
| 1836 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | sector_num += n; |
| 1840 | nb_sectors -= n; |
| 1841 | buf += n * BDRV_SECTOR_SIZE; |
| 1842 | } |
| 1843 | |
| 1844 | return 0; |
| 1845 | } |
| 1846 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1847 | |
| 1848 | static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num, |
| 1849 | int nb_sectors, uint8_t *buf, |
| 1850 | enum ImgConvertBlockStatus status) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1851 | { |
| 1852 | int ret; |
| 1853 | |
| 1854 | while (nb_sectors > 0) { |
| 1855 | int n = nb_sectors; |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1856 | BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0; |
| 1857 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1858 | switch (status) { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1859 | case BLK_BACKING_FILE: |
| 1860 | /* If we have a backing file, leave clusters unallocated that are |
| 1861 | * unallocated in the source image, so that the backing file is |
| 1862 | * visible at the respective offset. */ |
| 1863 | assert(s->target_has_backing); |
| 1864 | break; |
| 1865 | |
| 1866 | case BLK_DATA: |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1867 | /* If we're told to keep the target fully allocated (-S 0) or there |
| 1868 | * is real non-zero data, we must write it. Otherwise we can treat |
| 1869 | * it as zero sectors. |
| 1870 | * Compressed clusters need to be written as a whole, so in that |
| 1871 | * case we can only save the write if the buffer is completely |
| 1872 | * zeroed. */ |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1873 | if (!s->min_sparse || |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1874 | (!s->compressed && |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1875 | is_allocated_sectors_min(buf, n, &n, s->min_sparse, |
| 1876 | sector_num, s->alignment)) || |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1877 | (s->compressed && |
| 1878 | !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1879 | { |
Vladimir Sementsov-Ogievskiy | 265a7e5 | 2019-04-22 17:58:38 +0300 | [diff] [blame] | 1880 | ret = blk_co_pwrite(s->target, sector_num << BDRV_SECTOR_BITS, |
| 1881 | n << BDRV_SECTOR_BITS, buf, flags); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1882 | if (ret < 0) { |
| 1883 | return ret; |
| 1884 | } |
| 1885 | break; |
| 1886 | } |
| 1887 | /* fall-through */ |
| 1888 | |
| 1889 | case BLK_ZERO: |
| 1890 | if (s->has_zero_init) { |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1891 | assert(!s->target_has_backing); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1892 | break; |
| 1893 | } |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1894 | ret = blk_co_pwrite_zeroes(s->target, |
| 1895 | sector_num << BDRV_SECTOR_BITS, |
Nir Soffer | a3d6ae2 | 2019-03-24 02:20:12 +0200 | [diff] [blame] | 1896 | n << BDRV_SECTOR_BITS, |
| 1897 | BDRV_REQ_MAY_UNMAP); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1898 | if (ret < 0) { |
| 1899 | return ret; |
| 1900 | } |
| 1901 | break; |
| 1902 | } |
| 1903 | |
| 1904 | sector_num += n; |
| 1905 | nb_sectors -= n; |
| 1906 | buf += n * BDRV_SECTOR_SIZE; |
| 1907 | } |
| 1908 | |
| 1909 | return 0; |
| 1910 | } |
| 1911 | |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1912 | static int coroutine_fn convert_co_copy_range(ImgConvertState *s, int64_t sector_num, |
| 1913 | int nb_sectors) |
| 1914 | { |
| 1915 | int n, ret; |
| 1916 | |
| 1917 | while (nb_sectors > 0) { |
| 1918 | BlockBackend *blk; |
| 1919 | int src_cur; |
| 1920 | int64_t bs_sectors, src_cur_offset; |
| 1921 | int64_t offset; |
| 1922 | |
| 1923 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
| 1924 | offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS; |
| 1925 | blk = s->src[src_cur]; |
| 1926 | bs_sectors = s->src_sectors[src_cur]; |
| 1927 | |
| 1928 | n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset)); |
| 1929 | |
| 1930 | ret = blk_co_copy_range(blk, offset, s->target, |
| 1931 | sector_num << BDRV_SECTOR_BITS, |
Vladimir Sementsov-Ogievskiy | 67b51fb | 2018-07-09 19:37:17 +0300 | [diff] [blame] | 1932 | n << BDRV_SECTOR_BITS, 0, 0); |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1933 | if (ret < 0) { |
| 1934 | return ret; |
| 1935 | } |
| 1936 | |
| 1937 | sector_num += n; |
| 1938 | nb_sectors -= n; |
| 1939 | } |
| 1940 | return 0; |
| 1941 | } |
| 1942 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1943 | static void coroutine_fn convert_co_do_copy(void *opaque) |
| 1944 | { |
| 1945 | ImgConvertState *s = opaque; |
| 1946 | uint8_t *buf = NULL; |
| 1947 | int ret, i; |
| 1948 | int index = -1; |
| 1949 | |
| 1950 | for (i = 0; i < s->num_coroutines; i++) { |
| 1951 | if (s->co[i] == qemu_coroutine_self()) { |
| 1952 | index = i; |
| 1953 | break; |
| 1954 | } |
| 1955 | } |
| 1956 | assert(index >= 0); |
| 1957 | |
| 1958 | s->running_coroutines++; |
| 1959 | buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE); |
| 1960 | |
| 1961 | while (1) { |
| 1962 | int n; |
| 1963 | int64_t sector_num; |
| 1964 | enum ImgConvertBlockStatus status; |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1965 | bool copy_range; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1966 | |
| 1967 | qemu_co_mutex_lock(&s->lock); |
| 1968 | if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) { |
| 1969 | qemu_co_mutex_unlock(&s->lock); |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1970 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1971 | } |
| 1972 | n = convert_iteration_sectors(s, s->sector_num); |
| 1973 | if (n < 0) { |
| 1974 | qemu_co_mutex_unlock(&s->lock); |
| 1975 | s->ret = n; |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1976 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1977 | } |
| 1978 | /* save current sector and allocation status to local variables */ |
| 1979 | sector_num = s->sector_num; |
| 1980 | status = s->status; |
| 1981 | if (!s->min_sparse && s->status == BLK_ZERO) { |
| 1982 | n = MIN(n, s->buf_sectors); |
| 1983 | } |
| 1984 | /* increment global sector counter so that other coroutines can |
| 1985 | * already continue reading beyond this request */ |
| 1986 | s->sector_num += n; |
| 1987 | qemu_co_mutex_unlock(&s->lock); |
| 1988 | |
| 1989 | if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) { |
| 1990 | s->allocated_done += n; |
| 1991 | qemu_progress_print(100.0 * s->allocated_done / |
| 1992 | s->allocated_sectors, 0); |
| 1993 | } |
| 1994 | |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1995 | retry: |
| 1996 | copy_range = s->copy_range && s->status == BLK_DATA; |
| 1997 | if (status == BLK_DATA && !copy_range) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1998 | ret = convert_co_read(s, sector_num, n, buf); |
| 1999 | if (ret < 0) { |
Eric Blake | 39f77cb | 2020-04-02 08:57:17 -0500 | [diff] [blame] | 2000 | error_report("error while reading at byte %lld: %s", |
| 2001 | sector_num * BDRV_SECTOR_SIZE, strerror(-ret)); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2002 | s->ret = ret; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2003 | } |
| 2004 | } else if (!s->min_sparse && status == BLK_ZERO) { |
| 2005 | status = BLK_DATA; |
| 2006 | memset(buf, 0x00, n * BDRV_SECTOR_SIZE); |
| 2007 | } |
| 2008 | |
| 2009 | if (s->wr_in_order) { |
| 2010 | /* keep writes in order */ |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2011 | while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2012 | s->wait_sector_num[index] = sector_num; |
| 2013 | qemu_coroutine_yield(); |
| 2014 | } |
| 2015 | s->wait_sector_num[index] = -1; |
| 2016 | } |
| 2017 | |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2018 | if (s->ret == -EINPROGRESS) { |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 2019 | if (copy_range) { |
| 2020 | ret = convert_co_copy_range(s, sector_num, n); |
| 2021 | if (ret) { |
| 2022 | s->copy_range = false; |
| 2023 | goto retry; |
| 2024 | } |
| 2025 | } else { |
| 2026 | ret = convert_co_write(s, sector_num, n, buf, status); |
| 2027 | } |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2028 | if (ret < 0) { |
Eric Blake | 39f77cb | 2020-04-02 08:57:17 -0500 | [diff] [blame] | 2029 | error_report("error while writing at byte %lld: %s", |
| 2030 | sector_num * BDRV_SECTOR_SIZE, strerror(-ret)); |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2031 | s->ret = ret; |
| 2032 | } |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2033 | } |
| 2034 | |
| 2035 | if (s->wr_in_order) { |
| 2036 | /* reenter the coroutine that might have waited |
| 2037 | * for this write to complete */ |
| 2038 | s->wr_offs = sector_num + n; |
| 2039 | for (i = 0; i < s->num_coroutines; i++) { |
| 2040 | if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) { |
| 2041 | /* |
| 2042 | * A -> B -> A cannot occur because A has |
| 2043 | * s->wait_sector_num[i] == -1 during A -> B. Therefore |
| 2044 | * B will never enter A during this time window. |
| 2045 | */ |
| 2046 | qemu_coroutine_enter(s->co[i]); |
| 2047 | break; |
| 2048 | } |
| 2049 | } |
| 2050 | } |
| 2051 | } |
| 2052 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2053 | qemu_vfree(buf); |
| 2054 | s->co[index] = NULL; |
| 2055 | s->running_coroutines--; |
| 2056 | if (!s->running_coroutines && s->ret == -EINPROGRESS) { |
| 2057 | /* the convert job finished successfully */ |
| 2058 | s->ret = 0; |
| 2059 | } |
| 2060 | } |
| 2061 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2062 | static int convert_do_copy(ImgConvertState *s) |
| 2063 | { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2064 | int ret, i, n; |
| 2065 | int64_t sector_num = 0; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2066 | |
| 2067 | /* Check whether we have zero initialisation or can get it efficiently */ |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2068 | if (!s->has_zero_init && s->target_is_new && s->min_sparse && |
| 2069 | !s->target_has_backing) { |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 2070 | s->has_zero_init = bdrv_has_zero_init(blk_bs(s->target)); |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 2071 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2072 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2073 | /* Allocate buffer for copied data. For compressed images, only one cluster |
| 2074 | * can be copied at a time. */ |
| 2075 | if (s->compressed) { |
| 2076 | if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) { |
| 2077 | error_report("invalid cluster size"); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2078 | return -EINVAL; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2079 | } |
| 2080 | s->buf_sectors = s->cluster_sectors; |
| 2081 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2082 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2083 | while (sector_num < s->total_sectors) { |
| 2084 | n = convert_iteration_sectors(s, sector_num); |
| 2085 | if (n < 0) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2086 | return n; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2087 | } |
Max Reitz | aad15de | 2016-03-24 23:33:57 +0100 | [diff] [blame] | 2088 | if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO)) |
| 2089 | { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2090 | s->allocated_sectors += n; |
| 2091 | } |
| 2092 | sector_num += n; |
| 2093 | } |
| 2094 | |
| 2095 | /* Do the copy */ |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2096 | s->sector_next_status = 0; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2097 | s->ret = -EINPROGRESS; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2098 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2099 | qemu_co_mutex_init(&s->lock); |
| 2100 | for (i = 0; i < s->num_coroutines; i++) { |
| 2101 | s->co[i] = qemu_coroutine_create(convert_co_do_copy, s); |
| 2102 | s->wait_sector_num[i] = -1; |
| 2103 | qemu_coroutine_enter(s->co[i]); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2104 | } |
| 2105 | |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2106 | while (s->running_coroutines) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2107 | main_loop_wait(false); |
| 2108 | } |
| 2109 | |
| 2110 | if (s->compressed && !s->ret) { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2111 | /* signal EOF to align */ |
Pavel Butsykin | fe5c135 | 2016-07-22 11:17:40 +0300 | [diff] [blame] | 2112 | ret = blk_pwrite_compressed(s->target, 0, NULL, 0); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2113 | if (ret < 0) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2114 | return ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2115 | } |
| 2116 | } |
| 2117 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2118 | return s->ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2119 | } |
| 2120 | |
Eric Blake | 74a4320 | 2021-07-09 10:39:50 -0500 | [diff] [blame] | 2121 | /* Check that bitmaps can be copied, or output an error */ |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2122 | static int convert_check_bitmaps(BlockDriverState *src, bool skip_broken) |
Eric Blake | 74a4320 | 2021-07-09 10:39:50 -0500 | [diff] [blame] | 2123 | { |
| 2124 | BdrvDirtyBitmap *bm; |
| 2125 | |
| 2126 | if (!bdrv_supports_persistent_dirty_bitmap(src)) { |
| 2127 | error_report("Source lacks bitmap support"); |
| 2128 | return -1; |
| 2129 | } |
| 2130 | FOR_EACH_DIRTY_BITMAP(src, bm) { |
| 2131 | if (!bdrv_dirty_bitmap_get_persistence(bm)) { |
| 2132 | continue; |
| 2133 | } |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2134 | if (!skip_broken && bdrv_dirty_bitmap_inconsistent(bm)) { |
Eric Blake | 74a4320 | 2021-07-09 10:39:50 -0500 | [diff] [blame] | 2135 | error_report("Cannot copy inconsistent bitmap '%s'", |
| 2136 | bdrv_dirty_bitmap_name(bm)); |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2137 | error_printf("Try --skip-broken-bitmaps, or " |
| 2138 | "use 'qemu-img bitmap --remove' to delete it\n"); |
Eric Blake | 74a4320 | 2021-07-09 10:39:50 -0500 | [diff] [blame] | 2139 | return -1; |
| 2140 | } |
| 2141 | } |
| 2142 | return 0; |
| 2143 | } |
| 2144 | |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2145 | static int convert_copy_bitmaps(BlockDriverState *src, BlockDriverState *dst, |
| 2146 | bool skip_broken) |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2147 | { |
| 2148 | BdrvDirtyBitmap *bm; |
| 2149 | Error *err = NULL; |
| 2150 | |
| 2151 | FOR_EACH_DIRTY_BITMAP(src, bm) { |
| 2152 | const char *name; |
| 2153 | |
| 2154 | if (!bdrv_dirty_bitmap_get_persistence(bm)) { |
| 2155 | continue; |
| 2156 | } |
| 2157 | name = bdrv_dirty_bitmap_name(bm); |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2158 | if (skip_broken && bdrv_dirty_bitmap_inconsistent(bm)) { |
| 2159 | warn_report("Skipping inconsistent bitmap '%s'", name); |
| 2160 | continue; |
| 2161 | } |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2162 | qmp_block_dirty_bitmap_add(dst->node_name, name, |
| 2163 | true, bdrv_dirty_bitmap_granularity(bm), |
| 2164 | true, true, |
| 2165 | true, !bdrv_dirty_bitmap_enabled(bm), |
| 2166 | &err); |
| 2167 | if (err) { |
| 2168 | error_reportf_err(err, "Failed to create bitmap %s: ", name); |
| 2169 | return -1; |
| 2170 | } |
| 2171 | |
| 2172 | do_dirty_bitmap_merge(dst->node_name, name, src->node_name, name, |
| 2173 | &err); |
| 2174 | if (err) { |
| 2175 | error_reportf_err(err, "Failed to populate bitmap %s: ", name); |
Eric Blake | 74a4320 | 2021-07-09 10:39:50 -0500 | [diff] [blame] | 2176 | qmp_block_dirty_bitmap_remove(dst->node_name, name, NULL); |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2177 | return -1; |
| 2178 | } |
| 2179 | } |
| 2180 | |
| 2181 | return 0; |
| 2182 | } |
| 2183 | |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 2184 | #define MAX_BUF_SECTORS 32768 |
| 2185 | |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 2186 | static void set_rate_limit(BlockBackend *blk, int64_t rate_limit) |
| 2187 | { |
| 2188 | ThrottleConfig cfg; |
| 2189 | |
| 2190 | throttle_config_init(&cfg); |
| 2191 | cfg.buckets[THROTTLE_BPS_WRITE].avg = rate_limit; |
| 2192 | |
| 2193 | blk_io_limits_enable(blk, CONVERT_THROTTLE_GROUP); |
| 2194 | blk_set_io_limits(blk, &cfg); |
| 2195 | } |
| 2196 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2197 | static int img_convert(int argc, char **argv) |
| 2198 | { |
Kevin Wolf | 0b8fb55 | 2021-04-22 18:43:44 +0200 | [diff] [blame] | 2199 | int c, bs_i, flags, src_flags = BDRV_O_NO_SHARE; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2200 | const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe", |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2201 | *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL, |
Eric Blake | 1899bf4 | 2021-09-13 08:17:35 -0500 | [diff] [blame] | 2202 | *out_filename, *out_baseimg_param, *snapshot_name = NULL, |
| 2203 | *backing_fmt = NULL; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2204 | BlockDriver *drv = NULL, *proto_drv = NULL; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2205 | BlockDriverInfo bdi; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2206 | BlockDriverState *out_bs; |
| 2207 | QemuOpts *opts = NULL, *sn_opts = NULL; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2208 | QemuOptsList *create_opts = NULL; |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2209 | QDict *open_opts = NULL; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2210 | char *options = NULL; |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 2211 | Error *local_err = NULL; |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2212 | bool writethrough, src_writethrough, image_opts = false, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2213 | skip_create = false, progress = false, tgt_image_opts = false; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2214 | int64_t ret = -EINVAL; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2215 | bool force_share = false; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2216 | bool explict_min_sparse = false; |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2217 | bool bitmaps = false; |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2218 | bool skip_broken = false; |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 2219 | int64_t rate_limit = 0; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2220 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2221 | ImgConvertState s = (ImgConvertState) { |
| 2222 | /* Need at least 4k of zeros for sparse detection */ |
| 2223 | .min_sparse = 8, |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2224 | .copy_range = false, |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2225 | .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE, |
| 2226 | .wr_in_order = true, |
| 2227 | .num_coroutines = 8, |
| 2228 | }; |
| 2229 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2230 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2231 | static const struct option long_options[] = { |
| 2232 | {"help", no_argument, 0, 'h'}, |
| 2233 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2234 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2235 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2236 | {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS}, |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 2237 | {"salvage", no_argument, 0, OPTION_SALVAGE}, |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2238 | {"target-is-zero", no_argument, 0, OPTION_TARGET_IS_ZERO}, |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2239 | {"bitmaps", no_argument, 0, OPTION_BITMAPS}, |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2240 | {"skip-broken-bitmaps", no_argument, 0, OPTION_SKIP_BROKEN}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2241 | {0, 0, 0, 0} |
| 2242 | }; |
Eric Blake | 1899bf4 | 2021-09-13 08:17:35 -0500 | [diff] [blame] | 2243 | c = getopt_long(argc, argv, ":hf:O:B:CcF:o:l:S:pt:T:qnm:WUr:", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2244 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2245 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2246 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2247 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2248 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2249 | case ':': |
| 2250 | missing_argument(argv[optind - 1]); |
| 2251 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2252 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2253 | unrecognized_option(argv[optind - 1]); |
| 2254 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2255 | case 'h': |
| 2256 | help(); |
| 2257 | break; |
| 2258 | case 'f': |
| 2259 | fmt = optarg; |
| 2260 | break; |
| 2261 | case 'O': |
| 2262 | out_fmt = optarg; |
| 2263 | break; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 2264 | case 'B': |
| 2265 | out_baseimg = optarg; |
| 2266 | break; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2267 | case 'C': |
| 2268 | s.copy_range = true; |
| 2269 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2270 | case 'c': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2271 | s.compressed = true; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2272 | break; |
Eric Blake | 1899bf4 | 2021-09-13 08:17:35 -0500 | [diff] [blame] | 2273 | case 'F': |
| 2274 | backing_fmt = optarg; |
| 2275 | break; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2276 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 2277 | if (accumulate_options(&options, optarg) < 0) { |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2278 | goto fail_getopt; |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 2279 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2280 | break; |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2281 | case 'l': |
| 2282 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { |
Markus Armbruster | 70b9433 | 2015-02-13 12:50:26 +0100 | [diff] [blame] | 2283 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
| 2284 | optarg, false); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2285 | if (!sn_opts) { |
| 2286 | error_report("Failed in parsing snapshot param '%s'", |
| 2287 | optarg); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2288 | goto fail_getopt; |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2289 | } |
| 2290 | } else { |
| 2291 | snapshot_name = optarg; |
| 2292 | } |
| 2293 | break; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2294 | case 'S': |
| 2295 | { |
| 2296 | int64_t sval; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 2297 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 2298 | sval = cvtnum("buffer size for sparse output", optarg); |
| 2299 | if (sval < 0) { |
| 2300 | goto fail_getopt; |
| 2301 | } else if (!QEMU_IS_ALIGNED(sval, BDRV_SECTOR_SIZE) || |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 2302 | sval / BDRV_SECTOR_SIZE > MAX_BUF_SECTORS) { |
| 2303 | error_report("Invalid buffer size for sparse output specified. " |
| 2304 | "Valid sizes are multiples of %llu up to %llu. Select " |
| 2305 | "0 to disable sparse detection (fully allocates output).", |
| 2306 | BDRV_SECTOR_SIZE, MAX_BUF_SECTORS * BDRV_SECTOR_SIZE); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2307 | goto fail_getopt; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2308 | } |
| 2309 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2310 | s.min_sparse = sval / BDRV_SECTOR_SIZE; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2311 | explict_min_sparse = true; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2312 | break; |
| 2313 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2314 | case 'p': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2315 | progress = true; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2316 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2317 | case 't': |
| 2318 | cache = optarg; |
| 2319 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 2320 | case 'T': |
| 2321 | src_cache = optarg; |
| 2322 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2323 | case 'q': |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2324 | s.quiet = true; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2325 | break; |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2326 | case 'n': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2327 | skip_create = true; |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2328 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2329 | case 'm': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2330 | if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) || |
| 2331 | s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2332 | error_report("Invalid number of coroutines. Allowed number of" |
| 2333 | " coroutines is between 1 and %d", MAX_COROUTINES); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2334 | goto fail_getopt; |
| 2335 | } |
| 2336 | break; |
| 2337 | case 'W': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2338 | s.wr_in_order = false; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2339 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2340 | case 'U': |
| 2341 | force_share = true; |
| 2342 | break; |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 2343 | case 'r': |
| 2344 | rate_limit = cvtnum("rate limit", optarg); |
| 2345 | if (rate_limit < 0) { |
| 2346 | goto fail_getopt; |
| 2347 | } |
| 2348 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 2349 | case OPTION_OBJECT: |
| 2350 | user_creatable_process_cmdline(optarg); |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2351 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2352 | case OPTION_IMAGE_OPTS: |
| 2353 | image_opts = true; |
| 2354 | break; |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 2355 | case OPTION_SALVAGE: |
| 2356 | s.salvage = true; |
| 2357 | break; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2358 | case OPTION_TARGET_IMAGE_OPTS: |
| 2359 | tgt_image_opts = true; |
| 2360 | break; |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2361 | case OPTION_TARGET_IS_ZERO: |
| 2362 | /* |
| 2363 | * The user asserting that the target is blank has the |
| 2364 | * same effect as the target driver supporting zero |
| 2365 | * initialisation. |
| 2366 | */ |
| 2367 | s.has_zero_init = true; |
| 2368 | break; |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2369 | case OPTION_BITMAPS: |
| 2370 | bitmaps = true; |
| 2371 | break; |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2372 | case OPTION_SKIP_BROKEN: |
| 2373 | skip_broken = true; |
| 2374 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2375 | } |
| 2376 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2377 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2378 | if (!out_fmt && !tgt_image_opts) { |
| 2379 | out_fmt = "raw"; |
| 2380 | } |
| 2381 | |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2382 | if (skip_broken && !bitmaps) { |
| 2383 | error_report("Use of --skip-broken-bitmaps requires --bitmaps"); |
| 2384 | goto fail_getopt; |
| 2385 | } |
| 2386 | |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2387 | if (s.compressed && s.copy_range) { |
| 2388 | error_report("Cannot enable copy offloading when -c is used"); |
| 2389 | goto fail_getopt; |
| 2390 | } |
| 2391 | |
| 2392 | if (explict_min_sparse && s.copy_range) { |
| 2393 | error_report("Cannot enable copy offloading when -S is used"); |
| 2394 | goto fail_getopt; |
| 2395 | } |
| 2396 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 2397 | if (s.copy_range && s.salvage) { |
| 2398 | error_report("Cannot use copy offloading in salvaging mode"); |
| 2399 | goto fail_getopt; |
| 2400 | } |
| 2401 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2402 | if (tgt_image_opts && !skip_create) { |
| 2403 | error_report("--target-image-opts requires use of -n flag"); |
| 2404 | goto fail_getopt; |
| 2405 | } |
| 2406 | |
Kevin Wolf | ffd8e8f | 2019-08-09 11:09:21 +0200 | [diff] [blame] | 2407 | if (skip_create && options) { |
Eric Blake | 25956af | 2020-07-06 15:39:46 -0500 | [diff] [blame] | 2408 | error_report("-o has no effect when skipping image creation"); |
| 2409 | goto fail_getopt; |
Kevin Wolf | ffd8e8f | 2019-08-09 11:09:21 +0200 | [diff] [blame] | 2410 | } |
| 2411 | |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2412 | if (s.has_zero_init && !skip_create) { |
| 2413 | error_report("--target-is-zero requires use of -n flag"); |
| 2414 | goto fail_getopt; |
| 2415 | } |
| 2416 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2417 | s.src_num = argc - optind - 1; |
| 2418 | out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL; |
| 2419 | |
| 2420 | if (options && has_help_option(options)) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2421 | if (out_fmt) { |
| 2422 | ret = print_block_option_help(out_filename, out_fmt); |
| 2423 | goto fail_getopt; |
| 2424 | } else { |
| 2425 | error_report("Option help requires a format be specified"); |
| 2426 | goto fail_getopt; |
| 2427 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2428 | } |
| 2429 | |
| 2430 | if (s.src_num < 1) { |
| 2431 | error_report("Must specify image file name"); |
| 2432 | goto fail_getopt; |
| 2433 | } |
| 2434 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2435 | /* ret is still -EINVAL until here */ |
| 2436 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); |
| 2437 | if (ret < 0) { |
| 2438 | error_report("Invalid source cache option: %s", src_cache); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2439 | goto fail_getopt; |
| 2440 | } |
| 2441 | |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2442 | /* Initialize before goto out */ |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2443 | if (s.quiet) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2444 | progress = false; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2445 | } |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2446 | qemu_progress_init(progress, 1.0); |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2447 | qemu_progress_print(0, 100); |
| 2448 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2449 | s.src = g_new0(BlockBackend *, s.src_num); |
| 2450 | s.src_sectors = g_new(int64_t, s.src_num); |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 2451 | s.src_alignment = g_new(int, s.src_num); |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 2452 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2453 | for (bs_i = 0; bs_i < s.src_num; bs_i++) { |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 2454 | BlockDriverState *src_bs; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2455 | s.src[bs_i] = img_open(image_opts, argv[optind + bs_i], |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2456 | fmt, src_flags, src_writethrough, s.quiet, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2457 | force_share); |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2458 | if (!s.src[bs_i]) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2459 | ret = -1; |
| 2460 | goto out; |
| 2461 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2462 | s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]); |
| 2463 | if (s.src_sectors[bs_i] < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2464 | error_report("Could not get size of %s: %s", |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2465 | argv[optind + bs_i], strerror(-s.src_sectors[bs_i])); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2466 | ret = -1; |
| 2467 | goto out; |
| 2468 | } |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 2469 | src_bs = blk_bs(s.src[bs_i]); |
| 2470 | s.src_alignment[bs_i] = DIV_ROUND_UP(src_bs->bl.request_alignment, |
| 2471 | BDRV_SECTOR_SIZE); |
| 2472 | if (!bdrv_get_info(src_bs, &bdi)) { |
| 2473 | s.src_alignment[bs_i] = MAX(s.src_alignment[bs_i], |
| 2474 | bdi.cluster_size / BDRV_SECTOR_SIZE); |
| 2475 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2476 | s.total_sectors += s.src_sectors[bs_i]; |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 2477 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2478 | |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2479 | if (sn_opts) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2480 | bdrv_snapshot_load_tmp(blk_bs(s.src[0]), |
Peter Maydell | 10d6eda | 2017-02-10 16:28:24 +0000 | [diff] [blame] | 2481 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), |
| 2482 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), |
| 2483 | &local_err); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2484 | } else if (snapshot_name != NULL) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2485 | if (s.src_num > 1) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 2486 | error_report("No support for concatenating multiple snapshot"); |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 2487 | ret = -1; |
| 2488 | goto out; |
| 2489 | } |
Wenchao Xia | 7b4c478 | 2013-12-04 17:10:54 +0800 | [diff] [blame] | 2490 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2491 | bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name, |
| 2492 | &local_err); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2493 | } |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2494 | if (local_err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2495 | error_reportf_err(local_err, "Failed to load snapshot: "); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2496 | ret = -1; |
| 2497 | goto out; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 2498 | } |
| 2499 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2500 | if (!skip_create) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2501 | /* Find driver and parse its options */ |
| 2502 | drv = bdrv_find_format(out_fmt); |
| 2503 | if (!drv) { |
| 2504 | error_report("Unknown file format '%s'", out_fmt); |
| 2505 | ret = -1; |
| 2506 | goto out; |
| 2507 | } |
| 2508 | |
| 2509 | proto_drv = bdrv_find_protocol(out_filename, true, &local_err); |
| 2510 | if (!proto_drv) { |
| 2511 | error_report_err(local_err); |
| 2512 | ret = -1; |
| 2513 | goto out; |
| 2514 | } |
| 2515 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2516 | if (!drv->create_opts) { |
| 2517 | error_report("Format driver '%s' does not support image creation", |
| 2518 | drv->format_name); |
| 2519 | ret = -1; |
| 2520 | goto out; |
| 2521 | } |
Max Reitz | f75613c | 2014-12-02 18:32:46 +0100 | [diff] [blame] | 2522 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2523 | if (!proto_drv->create_opts) { |
| 2524 | error_report("Protocol driver '%s' does not support image creation", |
| 2525 | proto_drv->format_name); |
| 2526 | ret = -1; |
| 2527 | goto out; |
| 2528 | } |
Max Reitz | f75613c | 2014-12-02 18:32:46 +0100 | [diff] [blame] | 2529 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2530 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 2531 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
Kevin Wolf | db08adf | 2009-06-04 15:39:38 +0200 | [diff] [blame] | 2532 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2533 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 2534 | if (options) { |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 2535 | if (!qemu_opts_do_parse(opts, options, NULL, &local_err)) { |
Markus Armbruster | 97a2ca7 | 2015-03-14 10:23:15 +0100 | [diff] [blame] | 2536 | error_report_err(local_err); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 2537 | ret = -1; |
| 2538 | goto out; |
| 2539 | } |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2540 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2541 | |
Yi Li | c075c42 | 2020-08-19 09:36:07 +0800 | [diff] [blame] | 2542 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, |
| 2543 | s.total_sectors * BDRV_SECTOR_SIZE, &error_abort); |
Eric Blake | 1899bf4 | 2021-09-13 08:17:35 -0500 | [diff] [blame] | 2544 | ret = add_old_style_options(out_fmt, opts, out_baseimg, backing_fmt); |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2545 | if (ret < 0) { |
| 2546 | goto out; |
| 2547 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2548 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2549 | |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2550 | /* Get backing file name if -o backing_file was used */ |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2551 | out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2552 | if (out_baseimg_param) { |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2553 | out_baseimg = out_baseimg_param; |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2554 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2555 | s.target_has_backing = (bool) out_baseimg; |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2556 | |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2557 | if (s.has_zero_init && s.target_has_backing) { |
| 2558 | error_report("Cannot use --target-is-zero when the destination " |
| 2559 | "image has a backing file"); |
| 2560 | goto out; |
| 2561 | } |
| 2562 | |
Max Reitz | 48758a8 | 2017-04-26 15:46:48 +0200 | [diff] [blame] | 2563 | if (s.src_num > 1 && out_baseimg) { |
| 2564 | error_report("Having a backing file for the target makes no sense when " |
| 2565 | "concatenating multiple input images"); |
| 2566 | ret = -1; |
| 2567 | goto out; |
| 2568 | } |
| 2569 | |
Eric Blake | d9f059a | 2020-07-06 15:39:54 -0500 | [diff] [blame] | 2570 | if (out_baseimg_param) { |
| 2571 | if (!qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT)) { |
Eric Blake | 497a30d | 2021-05-03 14:36:00 -0700 | [diff] [blame] | 2572 | error_report("Use of backing file requires explicit " |
| 2573 | "backing format"); |
| 2574 | ret = -1; |
| 2575 | goto out; |
Eric Blake | d9f059a | 2020-07-06 15:39:54 -0500 | [diff] [blame] | 2576 | } |
| 2577 | } |
| 2578 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2579 | /* Check if compression is supported */ |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2580 | if (s.compressed) { |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2581 | bool encryption = |
| 2582 | qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false); |
Daniel P. Berrange | 0cb8d47 | 2017-06-23 17:24:06 +0100 | [diff] [blame] | 2583 | const char *encryptfmt = |
| 2584 | qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2585 | const char *preallocation = |
| 2586 | qemu_opt_get(opts, BLOCK_OPT_PREALLOC); |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2587 | |
Vladimir Sementsov-Ogievskiy | ac850bf | 2019-06-04 19:15:06 +0300 | [diff] [blame] | 2588 | if (drv && !block_driver_can_compress(drv)) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2589 | error_report("Compression not supported for this file format"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2590 | ret = -1; |
| 2591 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2592 | } |
| 2593 | |
Daniel P. Berrange | 0cb8d47 | 2017-06-23 17:24:06 +0100 | [diff] [blame] | 2594 | if (encryption || encryptfmt) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2595 | error_report("Compression and encryption not supported at " |
| 2596 | "the same time"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2597 | ret = -1; |
| 2598 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2599 | } |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 2600 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2601 | if (preallocation |
| 2602 | && strcmp(preallocation, "off")) |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 2603 | { |
| 2604 | error_report("Compression and preallocation not supported at " |
| 2605 | "the same time"); |
| 2606 | ret = -1; |
| 2607 | goto out; |
| 2608 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2609 | } |
| 2610 | |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2611 | /* Determine if bitmaps need copying */ |
| 2612 | if (bitmaps) { |
| 2613 | if (s.src_num > 1) { |
| 2614 | error_report("Copying bitmaps only possible with single source"); |
| 2615 | ret = -1; |
| 2616 | goto out; |
| 2617 | } |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2618 | ret = convert_check_bitmaps(blk_bs(s.src[0]), skip_broken); |
Eric Blake | 74a4320 | 2021-07-09 10:39:50 -0500 | [diff] [blame] | 2619 | if (ret < 0) { |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2620 | goto out; |
| 2621 | } |
| 2622 | } |
| 2623 | |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2624 | /* |
| 2625 | * The later open call will need any decryption secrets, and |
| 2626 | * bdrv_create() will purge "opts", so extract them now before |
| 2627 | * they are lost. |
| 2628 | */ |
| 2629 | if (!skip_create) { |
| 2630 | open_opts = qdict_new(); |
| 2631 | qemu_opt_foreach(opts, img_add_key_secrets, open_opts, &error_abort); |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2632 | |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2633 | /* Create the new image */ |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 2634 | ret = bdrv_create(drv, out_filename, opts, &local_err); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2635 | if (ret < 0) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2636 | error_reportf_err(local_err, "%s: error while converting %s: ", |
| 2637 | out_filename, out_fmt); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2638 | goto out; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2639 | } |
| 2640 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2641 | |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 2642 | s.target_is_new = !skip_create; |
| 2643 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2644 | 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] | 2645 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2646 | if (ret < 0) { |
| 2647 | error_report("Invalid cache option: %s", cache); |
Markus Armbruster | bb9cd2e | 2014-05-28 11:17:07 +0200 | [diff] [blame] | 2648 | goto out; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2649 | } |
| 2650 | |
Hanna Reitz | a1c6243 | 2021-08-19 12:12:00 +0200 | [diff] [blame] | 2651 | if (flags & BDRV_O_NOCACHE) { |
| 2652 | /* |
| 2653 | * If we open the target with O_DIRECT, it may be necessary to |
| 2654 | * extend its size to align to the physical sector size. |
| 2655 | */ |
| 2656 | flags |= BDRV_O_RESIZE; |
| 2657 | } |
| 2658 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2659 | if (skip_create) { |
| 2660 | s.target = img_open(tgt_image_opts, out_filename, out_fmt, |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2661 | flags, writethrough, s.quiet, false); |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2662 | } else { |
| 2663 | /* TODO ultimately we should allow --target-image-opts |
| 2664 | * to be used even when -n is not given. |
| 2665 | * That has to wait for bdrv_create to be improved |
| 2666 | * to allow filenames in option syntax |
| 2667 | */ |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2668 | s.target = img_open_file(out_filename, open_opts, out_fmt, |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2669 | flags, writethrough, s.quiet, false); |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2670 | open_opts = NULL; /* blk_new_open will have freed it */ |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2671 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2672 | if (!s.target) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2673 | ret = -1; |
| 2674 | goto out; |
| 2675 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2676 | out_bs = blk_bs(s.target); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2677 | |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2678 | if (bitmaps && !bdrv_supports_persistent_dirty_bitmap(out_bs)) { |
| 2679 | error_report("Format driver '%s' does not support bitmaps", |
| 2680 | out_bs->drv->format_name); |
| 2681 | ret = -1; |
| 2682 | goto out; |
| 2683 | } |
| 2684 | |
Vladimir Sementsov-Ogievskiy | ac850bf | 2019-06-04 19:15:06 +0300 | [diff] [blame] | 2685 | if (s.compressed && !block_driver_can_compress(out_bs->drv)) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2686 | error_report("Compression not supported for this file format"); |
| 2687 | ret = -1; |
| 2688 | goto out; |
| 2689 | } |
| 2690 | |
Eric Blake | 5def6b8 | 2016-06-23 16:37:19 -0600 | [diff] [blame] | 2691 | /* increase bufsectors from the default 4096 (2M) if opt_transfer |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 2692 | * or discard_alignment of the out_bs is greater. Limit to |
| 2693 | * MAX_BUF_SECTORS as maximum which is currently 32768 (16MB). */ |
| 2694 | s.buf_sectors = MIN(MAX_BUF_SECTORS, |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2695 | MAX(s.buf_sectors, |
| 2696 | MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS, |
| 2697 | out_bs->bl.pdiscard_alignment >> |
| 2698 | BDRV_SECTOR_BITS))); |
Peter Lieven | f2521c9 | 2013-11-27 11:07:06 +0100 | [diff] [blame] | 2699 | |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 2700 | /* try to align the write requests to the destination to avoid unnecessary |
| 2701 | * RMW cycles. */ |
| 2702 | s.alignment = MAX(pow2floor(s.min_sparse), |
| 2703 | DIV_ROUND_UP(out_bs->bl.request_alignment, |
| 2704 | BDRV_SECTOR_SIZE)); |
| 2705 | assert(is_power_of_2(s.alignment)); |
| 2706 | |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2707 | if (skip_create) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2708 | int64_t output_sectors = blk_nb_sectors(s.target); |
Markus Armbruster | 43716fa | 2014-06-26 13:23:21 +0200 | [diff] [blame] | 2709 | if (output_sectors < 0) { |
Gonglei | eec5eb4 | 2015-02-25 12:22:27 +0800 | [diff] [blame] | 2710 | error_report("unable to get output image length: %s", |
Markus Armbruster | 43716fa | 2014-06-26 13:23:21 +0200 | [diff] [blame] | 2711 | strerror(-output_sectors)); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2712 | ret = -1; |
| 2713 | goto out; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2714 | } else if (output_sectors < s.total_sectors) { |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2715 | error_report("output file is smaller than input file"); |
| 2716 | ret = -1; |
| 2717 | goto out; |
| 2718 | } |
| 2719 | } |
| 2720 | |
Max Reitz | c69291e | 2020-01-21 16:59:14 +0100 | [diff] [blame] | 2721 | if (s.target_has_backing && s.target_is_new) { |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 2722 | /* Errors are treated as "backing length unknown" (which means |
| 2723 | * s.target_backing_sectors has to be negative, which it will |
| 2724 | * be automatically). The backing file length is used only |
| 2725 | * for optimizations, so such a case is not fatal. */ |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 2726 | s.target_backing_sectors = |
| 2727 | bdrv_nb_sectors(bdrv_backing_chain_next(out_bs)); |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 2728 | } else { |
| 2729 | s.target_backing_sectors = -1; |
| 2730 | } |
| 2731 | |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2732 | ret = bdrv_get_info(out_bs, &bdi); |
| 2733 | if (ret < 0) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2734 | if (s.compressed) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2735 | error_report("could not get block driver info"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2736 | goto out; |
| 2737 | } |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2738 | } else { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2739 | s.compressed = s.compressed || bdi.needs_compressed_writes; |
| 2740 | s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE; |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2741 | } |
| 2742 | |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 2743 | if (rate_limit) { |
| 2744 | set_rate_limit(s.target, rate_limit); |
| 2745 | } |
| 2746 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2747 | ret = convert_do_copy(&s); |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2748 | |
| 2749 | /* Now copy the bitmaps */ |
| 2750 | if (bitmaps && ret == 0) { |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2751 | ret = convert_copy_bitmaps(blk_bs(s.src[0]), out_bs, skip_broken); |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2752 | } |
| 2753 | |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2754 | out: |
Peter Lieven | 13c28af | 2013-11-27 11:07:01 +0100 | [diff] [blame] | 2755 | if (!ret) { |
| 2756 | qemu_progress_print(100, 0); |
| 2757 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2758 | qemu_progress_end(); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2759 | qemu_opts_del(opts); |
| 2760 | qemu_opts_free(create_opts); |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2761 | qobject_unref(open_opts); |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2762 | blk_unref(s.target); |
| 2763 | if (s.src) { |
| 2764 | for (bs_i = 0; bs_i < s.src_num; bs_i++) { |
| 2765 | blk_unref(s.src[bs_i]); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2766 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2767 | g_free(s.src); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2768 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2769 | g_free(s.src_sectors); |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 2770 | g_free(s.src_alignment); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2771 | fail_getopt: |
Tuguoyi | 6aec830 | 2020-11-02 09:04:57 +0000 | [diff] [blame] | 2772 | qemu_opts_del(sn_opts); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2773 | g_free(options); |
| 2774 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2775 | return !!ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2776 | } |
| 2777 | |
bellard | 57d1a2b | 2004-08-03 21:15:11 +0000 | [diff] [blame] | 2778 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2779 | static void dump_snapshots(BlockDriverState *bs) |
| 2780 | { |
| 2781 | QEMUSnapshotInfo *sn_tab, *sn; |
| 2782 | int nb_sns, i; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2783 | |
| 2784 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 2785 | if (nb_sns <= 0) |
| 2786 | return; |
| 2787 | printf("Snapshot list:\n"); |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2788 | bdrv_snapshot_dump(NULL); |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2789 | printf("\n"); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2790 | for(i = 0; i < nb_sns; i++) { |
| 2791 | sn = &sn_tab[i]; |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2792 | bdrv_snapshot_dump(sn); |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2793 | printf("\n"); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2794 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2795 | g_free(sn_tab); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2798 | static void dump_json_image_info_list(ImageInfoList *list) |
| 2799 | { |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2800 | GString *str; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2801 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 2802 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2803 | |
| 2804 | visit_type_ImageInfoList(v, NULL, &list, &error_abort); |
| 2805 | visit_complete(v, &obj); |
Markus Armbruster | 6589f45 | 2020-12-11 18:11:35 +0100 | [diff] [blame] | 2806 | str = qobject_to_json_pretty(obj, true); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2807 | assert(str != NULL); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2808 | printf("%s\n", str->str); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2809 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2810 | visit_free(v); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2811 | g_string_free(str, true); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2812 | } |
| 2813 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2814 | static void dump_json_image_info(ImageInfo *info) |
| 2815 | { |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2816 | GString *str; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2817 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 2818 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2819 | |
| 2820 | visit_type_ImageInfo(v, NULL, &info, &error_abort); |
| 2821 | visit_complete(v, &obj); |
Markus Armbruster | 6589f45 | 2020-12-11 18:11:35 +0100 | [diff] [blame] | 2822 | str = qobject_to_json_pretty(obj, true); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2823 | assert(str != NULL); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2824 | printf("%s\n", str->str); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2825 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2826 | visit_free(v); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2827 | g_string_free(str, true); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2828 | } |
| 2829 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2830 | static void dump_human_image_info_list(ImageInfoList *list) |
| 2831 | { |
| 2832 | ImageInfoList *elem; |
| 2833 | bool delim = false; |
| 2834 | |
| 2835 | for (elem = list; elem; elem = elem->next) { |
| 2836 | if (delim) { |
| 2837 | printf("\n"); |
| 2838 | } |
| 2839 | delim = true; |
| 2840 | |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2841 | bdrv_image_info_dump(elem->value); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2842 | } |
| 2843 | } |
| 2844 | |
| 2845 | static gboolean str_equal_func(gconstpointer a, gconstpointer b) |
| 2846 | { |
| 2847 | return strcmp(a, b) == 0; |
| 2848 | } |
| 2849 | |
| 2850 | /** |
| 2851 | * Open an image file chain and return an ImageInfoList |
| 2852 | * |
| 2853 | * @filename: topmost image filename |
| 2854 | * @fmt: topmost image format (may be NULL to autodetect) |
| 2855 | * @chain: true - enumerate entire backing file chain |
| 2856 | * false - only topmost image file |
| 2857 | * |
| 2858 | * Returns a list of ImageInfo objects or NULL if there was an error opening an |
| 2859 | * image file. If there was an error a message will have been printed to |
| 2860 | * stderr. |
| 2861 | */ |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2862 | static ImageInfoList *collect_image_info_list(bool image_opts, |
| 2863 | const char *filename, |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2864 | const char *fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2865 | bool chain, bool force_share) |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2866 | { |
| 2867 | ImageInfoList *head = NULL; |
Eric Blake | c3033fd | 2021-01-13 16:10:12 -0600 | [diff] [blame] | 2868 | ImageInfoList **tail = &head; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2869 | GHashTable *filenames; |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2870 | Error *err = NULL; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2871 | |
| 2872 | filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL); |
| 2873 | |
| 2874 | while (filename) { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2875 | BlockBackend *blk; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2876 | BlockDriverState *bs; |
| 2877 | ImageInfo *info; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2878 | |
| 2879 | if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) { |
| 2880 | error_report("Backing file '%s' creates an infinite loop.", |
| 2881 | filename); |
| 2882 | goto err; |
| 2883 | } |
| 2884 | g_hash_table_insert(filenames, (gpointer)filename, NULL); |
| 2885 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 2886 | blk = img_open(image_opts, filename, fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2887 | BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false, |
| 2888 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2889 | if (!blk) { |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2890 | goto err; |
| 2891 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2892 | bs = blk_bs(blk); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2893 | |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2894 | bdrv_query_image_info(bs, &info, &err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2895 | if (err) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 2896 | error_report_err(err); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2897 | blk_unref(blk); |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2898 | goto err; |
Wenchao Xia | fb0ed45 | 2013-06-06 12:27:57 +0800 | [diff] [blame] | 2899 | } |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2900 | |
Eric Blake | c3033fd | 2021-01-13 16:10:12 -0600 | [diff] [blame] | 2901 | QAPI_LIST_APPEND(tail, info); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2902 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2903 | blk_unref(blk); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2904 | |
Stefan Hajnoczi | 0da7d13 | 2019-12-05 13:46:46 +0000 | [diff] [blame] | 2905 | /* Clear parameters that only apply to the topmost image */ |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2906 | filename = fmt = NULL; |
Stefan Hajnoczi | 0da7d13 | 2019-12-05 13:46:46 +0000 | [diff] [blame] | 2907 | image_opts = false; |
| 2908 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2909 | if (chain) { |
| 2910 | if (info->has_full_backing_filename) { |
| 2911 | filename = info->full_backing_filename; |
| 2912 | } else if (info->has_backing_filename) { |
John Snow | 92d617a | 2015-12-14 14:55:15 -0500 | [diff] [blame] | 2913 | error_report("Could not determine absolute backing filename," |
| 2914 | " but backing filename '%s' present", |
| 2915 | info->backing_filename); |
| 2916 | goto err; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2917 | } |
| 2918 | if (info->has_backing_filename_format) { |
| 2919 | fmt = info->backing_filename_format; |
| 2920 | } |
| 2921 | } |
| 2922 | } |
| 2923 | g_hash_table_destroy(filenames); |
| 2924 | return head; |
| 2925 | |
| 2926 | err: |
| 2927 | qapi_free_ImageInfoList(head); |
| 2928 | g_hash_table_destroy(filenames); |
| 2929 | return NULL; |
| 2930 | } |
| 2931 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2932 | static int img_info(int argc, char **argv) |
| 2933 | { |
| 2934 | int c; |
| 2935 | OutputFormat output_format = OFORMAT_HUMAN; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2936 | bool chain = false; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2937 | const char *filename, *fmt, *output; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2938 | ImageInfoList *list; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2939 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2940 | bool force_share = false; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2941 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2942 | fmt = NULL; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2943 | output = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2944 | for(;;) { |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2945 | int option_index = 0; |
| 2946 | static const struct option long_options[] = { |
| 2947 | {"help", no_argument, 0, 'h'}, |
| 2948 | {"format", required_argument, 0, 'f'}, |
| 2949 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2950 | {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2951 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2952 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2953 | {"force-share", no_argument, 0, 'U'}, |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2954 | {0, 0, 0, 0} |
| 2955 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2956 | c = getopt_long(argc, argv, ":f:hU", |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2957 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2958 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2959 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2960 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2961 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2962 | case ':': |
| 2963 | missing_argument(argv[optind - 1]); |
| 2964 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2965 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2966 | unrecognized_option(argv[optind - 1]); |
| 2967 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2968 | case 'h': |
| 2969 | help(); |
| 2970 | break; |
| 2971 | case 'f': |
| 2972 | fmt = optarg; |
| 2973 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2974 | case 'U': |
| 2975 | force_share = true; |
| 2976 | break; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2977 | case OPTION_OUTPUT: |
| 2978 | output = optarg; |
| 2979 | break; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2980 | case OPTION_BACKING_CHAIN: |
| 2981 | chain = true; |
| 2982 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 2983 | case OPTION_OBJECT: |
| 2984 | user_creatable_process_cmdline(optarg); |
| 2985 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2986 | case OPTION_IMAGE_OPTS: |
| 2987 | image_opts = true; |
| 2988 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2989 | } |
| 2990 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 2991 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2992 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2993 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2994 | filename = argv[optind++]; |
| 2995 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2996 | if (output && !strcmp(output, "json")) { |
| 2997 | output_format = OFORMAT_JSON; |
| 2998 | } else if (output && !strcmp(output, "human")) { |
| 2999 | output_format = OFORMAT_HUMAN; |
| 3000 | } else if (output) { |
| 3001 | error_report("--output must be used with human or json as argument."); |
| 3002 | return 1; |
| 3003 | } |
| 3004 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3005 | list = collect_image_info_list(image_opts, filename, fmt, chain, |
| 3006 | force_share); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 3007 | if (!list) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3008 | return 1; |
| 3009 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 3010 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 3011 | switch (output_format) { |
| 3012 | case OFORMAT_HUMAN: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 3013 | dump_human_image_info_list(list); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 3014 | break; |
| 3015 | case OFORMAT_JSON: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 3016 | if (chain) { |
| 3017 | dump_json_image_info_list(list); |
| 3018 | } else { |
| 3019 | dump_json_image_info(list->value); |
| 3020 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 3021 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3022 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 3023 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 3024 | qapi_free_ImageInfoList(list); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 3025 | return 0; |
| 3026 | } |
| 3027 | |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3028 | static int dump_map_entry(OutputFormat output_format, MapEntry *e, |
| 3029 | MapEntry *next) |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3030 | { |
| 3031 | switch (output_format) { |
| 3032 | case OFORMAT_HUMAN: |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3033 | if (e->data && !e->has_offset) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3034 | error_report("File contains external, encrypted or compressed clusters."); |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3035 | return -1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3036 | } |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3037 | if (e->data && !e->zero) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3038 | printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n", |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3039 | e->start, e->length, |
| 3040 | e->has_offset ? e->offset : 0, |
| 3041 | e->has_filename ? e->filename : ""); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3042 | } |
| 3043 | /* This format ignores the distinction between 0, ZERO and ZERO|DATA. |
| 3044 | * Modify the flags here to allow more coalescing. |
| 3045 | */ |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3046 | if (next && (!next->data || next->zero)) { |
| 3047 | next->data = false; |
| 3048 | next->zero = true; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3049 | } |
| 3050 | break; |
| 3051 | case OFORMAT_JSON: |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 3052 | printf("{ \"start\": %"PRId64", \"length\": %"PRId64"," |
Eric Blake | 8417e13 | 2021-07-01 14:06:55 -0500 | [diff] [blame] | 3053 | " \"depth\": %"PRId64", \"present\": %s, \"zero\": %s," |
| 3054 | " \"data\": %s", e->start, e->length, e->depth, |
| 3055 | e->present ? "true" : "false", |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3056 | e->zero ? "true" : "false", |
| 3057 | e->data ? "true" : "false"); |
| 3058 | if (e->has_offset) { |
Paolo Bonzini | c745bfb | 2013-09-11 18:47:52 +0200 | [diff] [blame] | 3059 | printf(", \"offset\": %"PRId64"", e->offset); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3060 | } |
| 3061 | putchar('}'); |
| 3062 | |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 3063 | if (next) { |
| 3064 | puts(","); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3065 | } |
| 3066 | break; |
| 3067 | } |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3068 | return 0; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3069 | } |
| 3070 | |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3071 | static int get_block_status(BlockDriverState *bs, int64_t offset, |
| 3072 | int64_t bytes, MapEntry *e) |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3073 | { |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3074 | int ret; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3075 | int depth; |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 3076 | BlockDriverState *file; |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3077 | bool has_offset; |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3078 | int64_t map; |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3079 | char *filename = NULL; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3080 | |
| 3081 | /* As an optimization, we could cache the current range of unallocated |
| 3082 | * clusters in each file of the chain, and avoid querying the same |
| 3083 | * range repeatedly. |
| 3084 | */ |
| 3085 | |
| 3086 | depth = 0; |
| 3087 | for (;;) { |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3088 | bs = bdrv_skip_filters(bs); |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3089 | ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3090 | if (ret < 0) { |
| 3091 | return ret; |
| 3092 | } |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3093 | assert(bytes); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3094 | if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) { |
| 3095 | break; |
| 3096 | } |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3097 | bs = bdrv_cow_bs(bs); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3098 | if (bs == NULL) { |
| 3099 | ret = 0; |
| 3100 | break; |
| 3101 | } |
| 3102 | |
| 3103 | depth++; |
| 3104 | } |
| 3105 | |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3106 | has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID); |
| 3107 | |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3108 | if (file && has_offset) { |
| 3109 | bdrv_refresh_filename(file); |
| 3110 | filename = file->filename; |
| 3111 | } |
| 3112 | |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3113 | *e = (MapEntry) { |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3114 | .start = offset, |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3115 | .length = bytes, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3116 | .data = !!(ret & BDRV_BLOCK_DATA), |
| 3117 | .zero = !!(ret & BDRV_BLOCK_ZERO), |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3118 | .offset = map, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3119 | .has_offset = has_offset, |
| 3120 | .depth = depth, |
Eric Blake | 8417e13 | 2021-07-01 14:06:55 -0500 | [diff] [blame] | 3121 | .present = !!(ret & BDRV_BLOCK_ALLOCATED), |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3122 | .has_filename = filename, |
| 3123 | .filename = filename, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3124 | }; |
| 3125 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3126 | return 0; |
| 3127 | } |
| 3128 | |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3129 | static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next) |
| 3130 | { |
| 3131 | if (curr->length == 0) { |
| 3132 | return false; |
| 3133 | } |
| 3134 | if (curr->zero != next->zero || |
| 3135 | curr->data != next->data || |
| 3136 | curr->depth != next->depth || |
Eric Blake | 8417e13 | 2021-07-01 14:06:55 -0500 | [diff] [blame] | 3137 | curr->present != next->present || |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3138 | curr->has_filename != next->has_filename || |
| 3139 | curr->has_offset != next->has_offset) { |
| 3140 | return false; |
| 3141 | } |
| 3142 | if (curr->has_filename && strcmp(curr->filename, next->filename)) { |
| 3143 | return false; |
| 3144 | } |
| 3145 | if (curr->has_offset && curr->offset + curr->length != next->offset) { |
| 3146 | return false; |
| 3147 | } |
| 3148 | return true; |
| 3149 | } |
| 3150 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3151 | static int img_map(int argc, char **argv) |
| 3152 | { |
| 3153 | int c; |
| 3154 | OutputFormat output_format = OFORMAT_HUMAN; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3155 | BlockBackend *blk; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3156 | BlockDriverState *bs; |
| 3157 | const char *filename, *fmt, *output; |
| 3158 | int64_t length; |
| 3159 | MapEntry curr = { .length = 0 }, next; |
| 3160 | int ret = 0; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3161 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3162 | bool force_share = false; |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3163 | int64_t start_offset = 0; |
| 3164 | int64_t max_length = -1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3165 | |
| 3166 | fmt = NULL; |
| 3167 | output = NULL; |
| 3168 | for (;;) { |
| 3169 | int option_index = 0; |
| 3170 | static const struct option long_options[] = { |
| 3171 | {"help", no_argument, 0, 'h'}, |
| 3172 | {"format", required_argument, 0, 'f'}, |
| 3173 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3174 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3175 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3176 | {"force-share", no_argument, 0, 'U'}, |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3177 | {"start-offset", required_argument, 0, 's'}, |
| 3178 | {"max-length", required_argument, 0, 'l'}, |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3179 | {0, 0, 0, 0} |
| 3180 | }; |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3181 | c = getopt_long(argc, argv, ":f:s:l:hU", |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3182 | long_options, &option_index); |
| 3183 | if (c == -1) { |
| 3184 | break; |
| 3185 | } |
| 3186 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3187 | case ':': |
| 3188 | missing_argument(argv[optind - 1]); |
| 3189 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3190 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3191 | unrecognized_option(argv[optind - 1]); |
| 3192 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3193 | case 'h': |
| 3194 | help(); |
| 3195 | break; |
| 3196 | case 'f': |
| 3197 | fmt = optarg; |
| 3198 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3199 | case 'U': |
| 3200 | force_share = true; |
| 3201 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3202 | case OPTION_OUTPUT: |
| 3203 | output = optarg; |
| 3204 | break; |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3205 | case 's': |
| 3206 | start_offset = cvtnum("start offset", optarg); |
| 3207 | if (start_offset < 0) { |
| 3208 | return 1; |
| 3209 | } |
| 3210 | break; |
| 3211 | case 'l': |
| 3212 | max_length = cvtnum("max length", optarg); |
| 3213 | if (max_length < 0) { |
| 3214 | return 1; |
| 3215 | } |
| 3216 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 3217 | case OPTION_OBJECT: |
| 3218 | user_creatable_process_cmdline(optarg); |
| 3219 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3220 | case OPTION_IMAGE_OPTS: |
| 3221 | image_opts = true; |
| 3222 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3223 | } |
| 3224 | } |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3225 | if (optind != argc - 1) { |
| 3226 | error_exit("Expecting one image file name"); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3227 | } |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3228 | filename = argv[optind]; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3229 | |
| 3230 | if (output && !strcmp(output, "json")) { |
| 3231 | output_format = OFORMAT_JSON; |
| 3232 | } else if (output && !strcmp(output, "human")) { |
| 3233 | output_format = OFORMAT_HUMAN; |
| 3234 | } else if (output) { |
| 3235 | error_report("--output must be used with human or json as argument."); |
| 3236 | return 1; |
| 3237 | } |
| 3238 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3239 | blk = img_open(image_opts, filename, fmt, 0, false, false, force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3240 | if (!blk) { |
| 3241 | return 1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3242 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3243 | bs = blk_bs(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3244 | |
| 3245 | if (output_format == OFORMAT_HUMAN) { |
| 3246 | printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File"); |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 3247 | } else if (output_format == OFORMAT_JSON) { |
| 3248 | putchar('['); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3249 | } |
| 3250 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3251 | length = blk_getlength(blk); |
Eyal Moscovici | 8f282e8 | 2020-05-13 16:36:27 +0300 | [diff] [blame] | 3252 | if (length < 0) { |
| 3253 | error_report("Failed to get size for '%s'", filename); |
| 3254 | return 1; |
| 3255 | } |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3256 | if (max_length != -1) { |
| 3257 | length = MIN(start_offset + max_length, length); |
| 3258 | } |
Eyal Moscovici | 8f282e8 | 2020-05-13 16:36:27 +0300 | [diff] [blame] | 3259 | |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3260 | curr.start = start_offset; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3261 | while (curr.start + curr.length < length) { |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3262 | int64_t offset = curr.start + curr.length; |
Kevin Wolf | d0ceea8 | 2020-07-07 16:46:29 +0200 | [diff] [blame] | 3263 | int64_t n = length - offset; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3264 | |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3265 | ret = get_block_status(bs, offset, n, &next); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3266 | if (ret < 0) { |
| 3267 | error_report("Could not read file metadata: %s", strerror(-ret)); |
| 3268 | goto out; |
| 3269 | } |
| 3270 | |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3271 | if (entry_mergeable(&curr, &next)) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3272 | curr.length += next.length; |
| 3273 | continue; |
| 3274 | } |
| 3275 | |
| 3276 | if (curr.length > 0) { |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3277 | ret = dump_map_entry(output_format, &curr, &next); |
| 3278 | if (ret < 0) { |
| 3279 | goto out; |
| 3280 | } |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3281 | } |
| 3282 | curr = next; |
| 3283 | } |
| 3284 | |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3285 | ret = dump_map_entry(output_format, &curr, NULL); |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 3286 | if (output_format == OFORMAT_JSON) { |
| 3287 | puts("]"); |
| 3288 | } |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3289 | |
| 3290 | out: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3291 | blk_unref(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3292 | return ret < 0; |
| 3293 | } |
| 3294 | |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3295 | #define SNAPSHOT_LIST 1 |
| 3296 | #define SNAPSHOT_CREATE 2 |
| 3297 | #define SNAPSHOT_APPLY 3 |
| 3298 | #define SNAPSHOT_DELETE 4 |
| 3299 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3300 | static int img_snapshot(int argc, char **argv) |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3301 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3302 | BlockBackend *blk; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3303 | BlockDriverState *bs; |
| 3304 | QEMUSnapshotInfo sn; |
| 3305 | char *filename, *snapshot_name = NULL; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3306 | int c, ret = 0, bdrv_oflags; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3307 | int action = 0; |
| 3308 | qemu_timeval tv; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3309 | bool quiet = false; |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 3310 | Error *err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3311 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3312 | bool force_share = false; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3313 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3314 | bdrv_oflags = BDRV_O_RDWR; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3315 | /* Parse commandline parameters */ |
| 3316 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3317 | static const struct option long_options[] = { |
| 3318 | {"help", no_argument, 0, 'h'}, |
| 3319 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3320 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3321 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3322 | {0, 0, 0, 0} |
| 3323 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3324 | c = getopt_long(argc, argv, ":la:c:d:hqU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3325 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3326 | if (c == -1) { |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3327 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3328 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3329 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3330 | case ':': |
| 3331 | missing_argument(argv[optind - 1]); |
| 3332 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3333 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3334 | unrecognized_option(argv[optind - 1]); |
| 3335 | break; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3336 | case 'h': |
| 3337 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3338 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3339 | case 'l': |
| 3340 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3341 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3342 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3343 | } |
| 3344 | action = SNAPSHOT_LIST; |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 3345 | bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */ |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3346 | break; |
| 3347 | case 'a': |
| 3348 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3349 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3350 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3351 | } |
| 3352 | action = SNAPSHOT_APPLY; |
| 3353 | snapshot_name = optarg; |
| 3354 | break; |
| 3355 | case 'c': |
| 3356 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3357 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3358 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3359 | } |
| 3360 | action = SNAPSHOT_CREATE; |
| 3361 | snapshot_name = optarg; |
| 3362 | break; |
| 3363 | case 'd': |
| 3364 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3365 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3366 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3367 | } |
| 3368 | action = SNAPSHOT_DELETE; |
| 3369 | snapshot_name = optarg; |
| 3370 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3371 | case 'q': |
| 3372 | quiet = true; |
| 3373 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3374 | case 'U': |
| 3375 | force_share = true; |
| 3376 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 3377 | case OPTION_OBJECT: |
| 3378 | user_creatable_process_cmdline(optarg); |
| 3379 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3380 | case OPTION_IMAGE_OPTS: |
| 3381 | image_opts = true; |
| 3382 | break; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3383 | } |
| 3384 | } |
| 3385 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 3386 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3387 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3388 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3389 | filename = argv[optind++]; |
| 3390 | |
| 3391 | /* Open the image */ |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3392 | blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet, |
| 3393 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3394 | if (!blk) { |
| 3395 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3396 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3397 | bs = blk_bs(blk); |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3398 | |
| 3399 | /* Perform the requested action */ |
| 3400 | switch(action) { |
| 3401 | case SNAPSHOT_LIST: |
| 3402 | dump_snapshots(bs); |
| 3403 | break; |
| 3404 | |
| 3405 | case SNAPSHOT_CREATE: |
| 3406 | memset(&sn, 0, sizeof(sn)); |
| 3407 | pstrcpy(sn.name, sizeof(sn.name), snapshot_name); |
| 3408 | |
| 3409 | qemu_gettimeofday(&tv); |
| 3410 | sn.date_sec = tv.tv_sec; |
| 3411 | sn.date_nsec = tv.tv_usec * 1000; |
| 3412 | |
| 3413 | ret = bdrv_snapshot_create(bs, &sn); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3414 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3415 | error_report("Could not create snapshot '%s': %d (%s)", |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3416 | snapshot_name, ret, strerror(-ret)); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3417 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3418 | break; |
| 3419 | |
| 3420 | case SNAPSHOT_APPLY: |
Kevin Wolf | 0b62bcb | 2017-11-20 15:28:41 +0100 | [diff] [blame] | 3421 | ret = bdrv_snapshot_goto(bs, snapshot_name, &err); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3422 | if (ret) { |
Kevin Wolf | 0b62bcb | 2017-11-20 15:28:41 +0100 | [diff] [blame] | 3423 | error_reportf_err(err, "Could not apply snapshot '%s': ", |
| 3424 | snapshot_name); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3425 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3426 | break; |
| 3427 | |
| 3428 | case SNAPSHOT_DELETE: |
Daniel Henrique Barboza | 8c04093 | 2018-11-07 11:09:59 -0200 | [diff] [blame] | 3429 | ret = bdrv_snapshot_find(bs, &sn, snapshot_name); |
| 3430 | if (ret < 0) { |
| 3431 | error_report("Could not delete snapshot '%s': snapshot not " |
| 3432 | "found", snapshot_name); |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 3433 | ret = 1; |
Daniel Henrique Barboza | 8c04093 | 2018-11-07 11:09:59 -0200 | [diff] [blame] | 3434 | } else { |
| 3435 | ret = bdrv_snapshot_delete(bs, sn.id_str, sn.name, &err); |
| 3436 | if (ret < 0) { |
| 3437 | error_reportf_err(err, "Could not delete snapshot '%s': ", |
| 3438 | snapshot_name); |
| 3439 | ret = 1; |
| 3440 | } |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3441 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3442 | break; |
| 3443 | } |
| 3444 | |
| 3445 | /* Cleanup */ |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3446 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3447 | if (ret) { |
| 3448 | return 1; |
| 3449 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3450 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3451 | } |
| 3452 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3453 | static int img_rebase(int argc, char **argv) |
| 3454 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3455 | BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL; |
Paolo Bonzini | 396374c | 2016-02-25 23:53:54 +0100 | [diff] [blame] | 3456 | uint8_t *buf_old = NULL; |
| 3457 | uint8_t *buf_new = NULL; |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3458 | BlockDriverState *bs = NULL, *prefix_chain_bs = NULL; |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3459 | BlockDriverState *unfiltered_bs; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3460 | char *filename; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3461 | const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg; |
| 3462 | int c, flags, src_flags, ret; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3463 | bool writethrough, src_writethrough; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3464 | int unsafe = 0; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3465 | bool force_share = false; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3466 | int progress = 0; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3467 | bool quiet = false; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 3468 | Error *local_err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3469 | bool image_opts = false; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3470 | |
| 3471 | /* Parse commandline parameters */ |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 3472 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3473 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3474 | src_cache = BDRV_DEFAULT_CACHE; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3475 | out_baseimg = NULL; |
| 3476 | out_basefmt = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3477 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3478 | static const struct option long_options[] = { |
| 3479 | {"help", no_argument, 0, 'h'}, |
| 3480 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3481 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3482 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3483 | {0, 0, 0, 0} |
| 3484 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3485 | c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3486 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3487 | if (c == -1) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3488 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3489 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3490 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3491 | case ':': |
| 3492 | missing_argument(argv[optind - 1]); |
| 3493 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3494 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3495 | unrecognized_option(argv[optind - 1]); |
| 3496 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3497 | case 'h': |
| 3498 | help(); |
| 3499 | return 0; |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 3500 | case 'f': |
| 3501 | fmt = optarg; |
| 3502 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3503 | case 'F': |
| 3504 | out_basefmt = optarg; |
| 3505 | break; |
| 3506 | case 'b': |
| 3507 | out_baseimg = optarg; |
| 3508 | break; |
| 3509 | case 'u': |
| 3510 | unsafe = 1; |
| 3511 | break; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3512 | case 'p': |
| 3513 | progress = 1; |
| 3514 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3515 | case 't': |
| 3516 | cache = optarg; |
| 3517 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3518 | case 'T': |
| 3519 | src_cache = optarg; |
| 3520 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3521 | case 'q': |
| 3522 | quiet = true; |
| 3523 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 3524 | case OPTION_OBJECT: |
| 3525 | user_creatable_process_cmdline(optarg); |
| 3526 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3527 | case OPTION_IMAGE_OPTS: |
| 3528 | image_opts = true; |
| 3529 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3530 | case 'U': |
| 3531 | force_share = true; |
| 3532 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3533 | } |
| 3534 | } |
| 3535 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3536 | if (quiet) { |
| 3537 | progress = 0; |
| 3538 | } |
| 3539 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3540 | if (optind != argc - 1) { |
| 3541 | error_exit("Expecting one image file name"); |
| 3542 | } |
| 3543 | if (!unsafe && !out_baseimg) { |
| 3544 | error_exit("Must specify backing file (-b) or use unsafe mode (-u)"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3545 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3546 | filename = argv[optind++]; |
| 3547 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3548 | qemu_progress_init(progress, 2.0); |
| 3549 | qemu_progress_print(0, 100); |
| 3550 | |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3551 | flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0); |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3552 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3553 | if (ret < 0) { |
| 3554 | error_report("Invalid cache option: %s", cache); |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3555 | goto out; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3556 | } |
| 3557 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3558 | src_flags = 0; |
| 3559 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3560 | if (ret < 0) { |
| 3561 | error_report("Invalid source cache option: %s", src_cache); |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3562 | goto out; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3563 | } |
| 3564 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3565 | /* The source files are opened read-only, don't care about WCE */ |
| 3566 | assert((src_flags & BDRV_O_RDWR) == 0); |
| 3567 | (void) src_writethrough; |
| 3568 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3569 | /* |
| 3570 | * Open the images. |
| 3571 | * |
| 3572 | * Ignore the old backing file for unsafe rebase in case we want to correct |
| 3573 | * the reference to a renamed or moved backing file. |
| 3574 | */ |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3575 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 3576 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3577 | if (!blk) { |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3578 | ret = -1; |
| 3579 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3580 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3581 | bs = blk_bs(blk); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3582 | |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3583 | unfiltered_bs = bdrv_skip_filters(bs); |
| 3584 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3585 | if (out_basefmt != NULL) { |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3586 | if (bdrv_find_format(out_basefmt) == NULL) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3587 | error_report("Invalid format name: '%s'", out_basefmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3588 | ret = -1; |
| 3589 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3590 | } |
| 3591 | } |
| 3592 | |
| 3593 | /* For safe rebasing we need to compare old and new backing file */ |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3594 | if (!unsafe) { |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3595 | QDict *options = NULL; |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3596 | BlockDriverState *base_bs = bdrv_cow_bs(unfiltered_bs); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3597 | |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3598 | if (base_bs) { |
Kevin Wolf | d861ab3 | 2019-04-25 14:25:10 +0200 | [diff] [blame] | 3599 | blk_old_backing = blk_new(qemu_get_aio_context(), |
| 3600 | BLK_PERM_CONSISTENT_READ, |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3601 | BLK_PERM_ALL); |
| 3602 | ret = blk_insert_bs(blk_old_backing, base_bs, |
| 3603 | &local_err); |
| 3604 | if (ret < 0) { |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3605 | error_reportf_err(local_err, |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3606 | "Could not reuse old backing file '%s': ", |
| 3607 | base_bs->filename); |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3608 | goto out; |
| 3609 | } |
| 3610 | } else { |
| 3611 | blk_old_backing = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3612 | } |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3613 | |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3614 | if (out_baseimg[0]) { |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3615 | const char *overlay_filename; |
| 3616 | char *out_real_path; |
| 3617 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3618 | options = qdict_new(); |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3619 | if (out_basefmt) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 3620 | qdict_put_str(options, "driver", out_basefmt); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3621 | } |
| 3622 | if (force_share) { |
| 3623 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3624 | } |
| 3625 | |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3626 | bdrv_refresh_filename(bs); |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3627 | overlay_filename = bs->exact_filename[0] ? bs->exact_filename |
| 3628 | : bs->filename; |
Max Reitz | 645ae7d | 2019-02-01 20:29:14 +0100 | [diff] [blame] | 3629 | out_real_path = |
| 3630 | bdrv_get_full_backing_filename_from_filename(overlay_filename, |
| 3631 | out_baseimg, |
| 3632 | &local_err); |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3633 | if (local_err) { |
Max Reitz | f22356d | 2019-05-28 21:53:38 +0200 | [diff] [blame] | 3634 | qobject_unref(options); |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3635 | error_reportf_err(local_err, |
| 3636 | "Could not resolve backing filename: "); |
| 3637 | ret = -1; |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3638 | goto out; |
| 3639 | } |
| 3640 | |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3641 | /* |
| 3642 | * Find out whether we rebase an image on top of a previous image |
| 3643 | * in its chain. |
| 3644 | */ |
| 3645 | prefix_chain_bs = bdrv_find_backing_image(bs, out_real_path); |
Sam Eiderman | 330c729 | 2019-05-23 19:33:37 +0300 | [diff] [blame] | 3646 | if (prefix_chain_bs) { |
Max Reitz | f22356d | 2019-05-28 21:53:38 +0200 | [diff] [blame] | 3647 | qobject_unref(options); |
Sam Eiderman | 330c729 | 2019-05-23 19:33:37 +0300 | [diff] [blame] | 3648 | g_free(out_real_path); |
Max Reitz | f22356d | 2019-05-28 21:53:38 +0200 | [diff] [blame] | 3649 | |
Kevin Wolf | d861ab3 | 2019-04-25 14:25:10 +0200 | [diff] [blame] | 3650 | blk_new_backing = blk_new(qemu_get_aio_context(), |
| 3651 | BLK_PERM_CONSISTENT_READ, |
Sam Eiderman | 330c729 | 2019-05-23 19:33:37 +0300 | [diff] [blame] | 3652 | BLK_PERM_ALL); |
| 3653 | ret = blk_insert_bs(blk_new_backing, prefix_chain_bs, |
| 3654 | &local_err); |
| 3655 | if (ret < 0) { |
| 3656 | error_reportf_err(local_err, |
| 3657 | "Could not reuse backing file '%s': ", |
| 3658 | out_baseimg); |
| 3659 | goto out; |
| 3660 | } |
| 3661 | } else { |
| 3662 | blk_new_backing = blk_new_open(out_real_path, NULL, |
| 3663 | options, src_flags, &local_err); |
| 3664 | g_free(out_real_path); |
| 3665 | if (!blk_new_backing) { |
| 3666 | error_reportf_err(local_err, |
| 3667 | "Could not open new backing file '%s': ", |
| 3668 | out_baseimg); |
| 3669 | ret = -1; |
| 3670 | goto out; |
| 3671 | } |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3672 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3673 | } |
| 3674 | } |
| 3675 | |
| 3676 | /* |
| 3677 | * Check each unallocated cluster in the COW file. If it is unallocated, |
| 3678 | * accesses go to the backing file. We must therefore compare this cluster |
| 3679 | * in the old and new backing file, and if they differ we need to copy it |
| 3680 | * from the old backing file into the COW file. |
| 3681 | * |
| 3682 | * If qemu-img crashes during this step, no harm is done. The content of |
| 3683 | * the image is the same as the original one at any time. |
| 3684 | */ |
| 3685 | if (!unsafe) { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3686 | int64_t size; |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3687 | int64_t old_backing_size = 0; |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3688 | int64_t new_backing_size = 0; |
| 3689 | uint64_t offset; |
| 3690 | int64_t n; |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 3691 | float local_progress = 0; |
TeLeMan | d6771bf | 2010-02-08 16:20:00 +0800 | [diff] [blame] | 3692 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3693 | buf_old = blk_blockalign(blk, IO_BUF_SIZE); |
| 3694 | buf_new = blk_blockalign(blk, IO_BUF_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3695 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3696 | size = blk_getlength(blk); |
| 3697 | if (size < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3698 | error_report("Could not get size of '%s': %s", |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3699 | filename, strerror(-size)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3700 | ret = -1; |
| 3701 | goto out; |
| 3702 | } |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3703 | if (blk_old_backing) { |
| 3704 | old_backing_size = blk_getlength(blk_old_backing); |
| 3705 | if (old_backing_size < 0) { |
| 3706 | char backing_name[PATH_MAX]; |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3707 | |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3708 | bdrv_get_backing_filename(bs, backing_name, |
| 3709 | sizeof(backing_name)); |
| 3710 | error_report("Could not get size of '%s': %s", |
| 3711 | backing_name, strerror(-old_backing_size)); |
| 3712 | ret = -1; |
| 3713 | goto out; |
| 3714 | } |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3715 | } |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3716 | if (blk_new_backing) { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3717 | new_backing_size = blk_getlength(blk_new_backing); |
| 3718 | if (new_backing_size < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3719 | error_report("Could not get size of '%s': %s", |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3720 | out_baseimg, strerror(-new_backing_size)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3721 | ret = -1; |
| 3722 | goto out; |
| 3723 | } |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3724 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3725 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3726 | if (size != 0) { |
| 3727 | local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE)); |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 3728 | } |
| 3729 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3730 | for (offset = 0; offset < size; offset += n) { |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3731 | bool buf_old_is_zero = false; |
| 3732 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3733 | /* How many bytes can we handle with the next read? */ |
| 3734 | n = MIN(IO_BUF_SIZE, size - offset); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3735 | |
| 3736 | /* If the cluster is allocated, we don't need to take action */ |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3737 | ret = bdrv_is_allocated(unfiltered_bs, offset, n, &n); |
Paolo Bonzini | d663640 | 2013-09-04 19:00:25 +0200 | [diff] [blame] | 3738 | if (ret < 0) { |
| 3739 | error_report("error while reading image metadata: %s", |
| 3740 | strerror(-ret)); |
| 3741 | goto out; |
| 3742 | } |
Kevin Wolf | cc60e32 | 2010-04-29 14:47:48 +0200 | [diff] [blame] | 3743 | if (ret) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3744 | continue; |
| 3745 | } |
| 3746 | |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3747 | if (prefix_chain_bs) { |
| 3748 | /* |
| 3749 | * If cluster wasn't changed since prefix_chain, we don't need |
| 3750 | * to take action |
| 3751 | */ |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3752 | ret = bdrv_is_allocated_above(bdrv_cow_bs(unfiltered_bs), |
| 3753 | prefix_chain_bs, false, |
| 3754 | offset, n, &n); |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3755 | if (ret < 0) { |
| 3756 | error_report("error while reading image metadata: %s", |
| 3757 | strerror(-ret)); |
| 3758 | goto out; |
| 3759 | } |
| 3760 | if (!ret) { |
| 3761 | continue; |
| 3762 | } |
| 3763 | } |
| 3764 | |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3765 | /* |
| 3766 | * Read old and new backing file and take into consideration that |
| 3767 | * backing files may be smaller than the COW image. |
| 3768 | */ |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3769 | if (offset >= old_backing_size) { |
| 3770 | memset(buf_old, 0, n); |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3771 | buf_old_is_zero = true; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3772 | } else { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3773 | if (offset + n > old_backing_size) { |
| 3774 | n = old_backing_size - offset; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3775 | } |
| 3776 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3777 | ret = blk_pread(blk_old_backing, offset, buf_old, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3778 | if (ret < 0) { |
| 3779 | error_report("error while reading from old backing file"); |
| 3780 | goto out; |
| 3781 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3782 | } |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3783 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3784 | if (offset >= new_backing_size || !blk_new_backing) { |
| 3785 | memset(buf_new, 0, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3786 | } else { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3787 | if (offset + n > new_backing_size) { |
| 3788 | n = new_backing_size - offset; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3789 | } |
| 3790 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3791 | ret = blk_pread(blk_new_backing, offset, buf_new, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3792 | if (ret < 0) { |
| 3793 | error_report("error while reading from new backing file"); |
| 3794 | goto out; |
| 3795 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3796 | } |
| 3797 | |
| 3798 | /* If they differ, we need to write to the COW file */ |
| 3799 | uint64_t written = 0; |
| 3800 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3801 | while (written < n) { |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 3802 | int64_t pnum; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3803 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3804 | if (compare_buffers(buf_old + written, buf_new + written, |
| 3805 | n - written, &pnum)) |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3806 | { |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3807 | if (buf_old_is_zero) { |
| 3808 | ret = blk_pwrite_zeroes(blk, offset + written, pnum, 0); |
| 3809 | } else { |
| 3810 | ret = blk_pwrite(blk, offset + written, |
| 3811 | buf_old + written, pnum, 0); |
| 3812 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3813 | if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3814 | error_report("Error while writing to COW image: %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3815 | strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3816 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3817 | } |
| 3818 | } |
| 3819 | |
| 3820 | written += pnum; |
| 3821 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3822 | qemu_progress_print(local_progress, 100); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3823 | } |
| 3824 | } |
| 3825 | |
| 3826 | /* |
| 3827 | * Change the backing file. All clusters that are different from the old |
| 3828 | * backing file are overwritten in the COW file now, so the visible content |
| 3829 | * doesn't change when we switch the backing file. |
| 3830 | */ |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3831 | if (out_baseimg && *out_baseimg) { |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3832 | ret = bdrv_change_backing_file(unfiltered_bs, out_baseimg, out_basefmt, |
| 3833 | true); |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3834 | } else { |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3835 | ret = bdrv_change_backing_file(unfiltered_bs, NULL, NULL, false); |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3836 | } |
| 3837 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3838 | if (ret == -ENOSPC) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3839 | error_report("Could not change the backing file to '%s': No " |
| 3840 | "space left in the file header", out_baseimg); |
Eric Blake | a7cd44b | 2021-07-08 10:52:28 -0500 | [diff] [blame] | 3841 | } else if (ret == -EINVAL && out_baseimg && !out_basefmt) { |
| 3842 | error_report("Could not change the backing file to '%s': backing " |
| 3843 | "format must be specified", out_baseimg); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3844 | } else if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3845 | error_report("Could not change the backing file to '%s': %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3846 | out_baseimg, strerror(-ret)); |
| 3847 | } |
| 3848 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3849 | qemu_progress_print(100, 0); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3850 | /* |
| 3851 | * TODO At this point it is possible to check if any clusters that are |
| 3852 | * allocated in the COW file are the same in the backing file. If so, they |
| 3853 | * could be dropped from the COW file. Don't do this before switching the |
| 3854 | * backing file, in case of a crash this would lead to corruption. |
| 3855 | */ |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3856 | out: |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3857 | qemu_progress_end(); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3858 | /* Cleanup */ |
| 3859 | if (!unsafe) { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3860 | blk_unref(blk_old_backing); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3861 | blk_unref(blk_new_backing); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3862 | } |
Paolo Bonzini | 396374c | 2016-02-25 23:53:54 +0100 | [diff] [blame] | 3863 | qemu_vfree(buf_old); |
| 3864 | qemu_vfree(buf_new); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3865 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3866 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3867 | if (ret) { |
| 3868 | return 1; |
| 3869 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3870 | return 0; |
| 3871 | } |
| 3872 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3873 | static int img_resize(int argc, char **argv) |
| 3874 | { |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 3875 | Error *err = NULL; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3876 | int c, ret, relative; |
| 3877 | const char *filename, *fmt, *size; |
Max Reitz | 09c5c6d | 2019-09-18 11:51:44 +0200 | [diff] [blame] | 3878 | int64_t n, total_size, current_size; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3879 | bool quiet = false; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3880 | BlockBackend *blk = NULL; |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3881 | PreallocMode prealloc = PREALLOC_MODE_OFF; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3882 | QemuOpts *param; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3883 | |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3884 | static QemuOptsList resize_options = { |
| 3885 | .name = "resize_options", |
| 3886 | .head = QTAILQ_HEAD_INITIALIZER(resize_options.head), |
| 3887 | .desc = { |
| 3888 | { |
| 3889 | .name = BLOCK_OPT_SIZE, |
| 3890 | .type = QEMU_OPT_SIZE, |
| 3891 | .help = "Virtual disk size" |
| 3892 | }, { |
| 3893 | /* end of list */ |
| 3894 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3895 | }, |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3896 | }; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3897 | bool image_opts = false; |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3898 | bool shrink = false; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3899 | |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 3900 | /* Remove size from argv manually so that negative numbers are not treated |
| 3901 | * as options by getopt. */ |
| 3902 | if (argc < 3) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3903 | error_exit("Not enough arguments"); |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 3904 | return 1; |
| 3905 | } |
| 3906 | |
| 3907 | size = argv[--argc]; |
| 3908 | |
| 3909 | /* Parse getopt arguments */ |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3910 | fmt = NULL; |
| 3911 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3912 | static const struct option long_options[] = { |
| 3913 | {"help", no_argument, 0, 'h'}, |
| 3914 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3915 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3916 | {"preallocation", required_argument, 0, OPTION_PREALLOCATION}, |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3917 | {"shrink", no_argument, 0, OPTION_SHRINK}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3918 | {0, 0, 0, 0} |
| 3919 | }; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3920 | c = getopt_long(argc, argv, ":f:hq", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3921 | long_options, NULL); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3922 | if (c == -1) { |
| 3923 | break; |
| 3924 | } |
| 3925 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3926 | case ':': |
| 3927 | missing_argument(argv[optind - 1]); |
| 3928 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3929 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3930 | unrecognized_option(argv[optind - 1]); |
| 3931 | break; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3932 | case 'h': |
| 3933 | help(); |
| 3934 | break; |
| 3935 | case 'f': |
| 3936 | fmt = optarg; |
| 3937 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3938 | case 'q': |
| 3939 | quiet = true; |
| 3940 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 3941 | case OPTION_OBJECT: |
| 3942 | user_creatable_process_cmdline(optarg); |
| 3943 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3944 | case OPTION_IMAGE_OPTS: |
| 3945 | image_opts = true; |
| 3946 | break; |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3947 | case OPTION_PREALLOCATION: |
Marc-André Lureau | f7abe0e | 2017-08-24 10:46:10 +0200 | [diff] [blame] | 3948 | prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg, |
Markus Armbruster | 06c60b6 | 2017-08-24 10:45:57 +0200 | [diff] [blame] | 3949 | PREALLOC_MODE__MAX, NULL); |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3950 | if (prealloc == PREALLOC_MODE__MAX) { |
| 3951 | error_report("Invalid preallocation mode '%s'", optarg); |
| 3952 | return 1; |
| 3953 | } |
| 3954 | break; |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3955 | case OPTION_SHRINK: |
| 3956 | shrink = true; |
| 3957 | break; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3958 | } |
| 3959 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 3960 | if (optind != argc - 1) { |
Max Reitz | be8fbd4 | 2018-02-05 17:27:45 +0100 | [diff] [blame] | 3961 | error_exit("Expecting image file name and size"); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3962 | } |
| 3963 | filename = argv[optind++]; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3964 | |
| 3965 | /* Choose grow, shrink, or absolute resize mode */ |
| 3966 | switch (size[0]) { |
| 3967 | case '+': |
| 3968 | relative = 1; |
| 3969 | size++; |
| 3970 | break; |
| 3971 | case '-': |
| 3972 | relative = -1; |
| 3973 | size++; |
| 3974 | break; |
| 3975 | default: |
| 3976 | relative = 0; |
| 3977 | break; |
| 3978 | } |
| 3979 | |
| 3980 | /* Parse size */ |
Peter Crosthwaite | 87ea75d | 2014-01-01 18:49:17 -0800 | [diff] [blame] | 3981 | param = qemu_opts_create(&resize_options, NULL, 0, &error_abort); |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 3982 | if (!qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err)) { |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 3983 | error_report_err(err); |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3984 | ret = -1; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3985 | qemu_opts_del(param); |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3986 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3987 | } |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3988 | n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0); |
| 3989 | qemu_opts_del(param); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3990 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 3991 | blk = img_open(image_opts, filename, fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3992 | BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet, |
| 3993 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3994 | if (!blk) { |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3995 | ret = -1; |
| 3996 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3997 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3998 | |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3999 | current_size = blk_getlength(blk); |
| 4000 | if (current_size < 0) { |
| 4001 | error_report("Failed to inquire current image length: %s", |
| 4002 | strerror(-current_size)); |
| 4003 | ret = -1; |
| 4004 | goto out; |
| 4005 | } |
| 4006 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4007 | if (relative) { |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 4008 | total_size = current_size + n * relative; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4009 | } else { |
| 4010 | total_size = n; |
| 4011 | } |
| 4012 | if (total_size <= 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 4013 | error_report("New image size must be positive"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 4014 | ret = -1; |
| 4015 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4016 | } |
| 4017 | |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 4018 | if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) { |
| 4019 | error_report("Preallocation can only be used for growing images"); |
| 4020 | ret = -1; |
| 4021 | goto out; |
| 4022 | } |
| 4023 | |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 4024 | if (total_size < current_size && !shrink) { |
Kevin Wolf | 1c404d7 | 2020-07-10 14:17:17 +0200 | [diff] [blame] | 4025 | error_report("Use the --shrink option to perform a shrink operation."); |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 4026 | warn_report("Shrinking an image will delete all data beyond the " |
| 4027 | "shrunken image's end. Before performing such an " |
| 4028 | "operation, make sure there is no important data there."); |
Kevin Wolf | 1c404d7 | 2020-07-10 14:17:17 +0200 | [diff] [blame] | 4029 | ret = -1; |
| 4030 | goto out; |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 4031 | } |
| 4032 | |
Max Reitz | e8d04f9 | 2019-09-18 11:51:43 +0200 | [diff] [blame] | 4033 | /* |
| 4034 | * The user expects the image to have the desired size after |
| 4035 | * resizing, so pass @exact=true. It is of no use to report |
| 4036 | * success when the image has not actually been resized. |
| 4037 | */ |
Kevin Wolf | 8c6242b | 2020-04-24 14:54:41 +0200 | [diff] [blame] | 4038 | ret = blk_truncate(blk, total_size, true, prealloc, 0, &err); |
Max Reitz | 09c5c6d | 2019-09-18 11:51:44 +0200 | [diff] [blame] | 4039 | if (!ret) { |
| 4040 | qprintf(quiet, "Image resized.\n"); |
| 4041 | } else { |
Max Reitz | ed3d2ec | 2017-03-28 22:51:27 +0200 | [diff] [blame] | 4042 | error_report_err(err); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4043 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 4044 | out: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 4045 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 4046 | if (ret) { |
| 4047 | return 1; |
| 4048 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4049 | return 0; |
| 4050 | } |
| 4051 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4052 | static void amend_status_cb(BlockDriverState *bs, |
Max Reitz | 8b13976 | 2015-07-27 17:51:32 +0200 | [diff] [blame] | 4053 | int64_t offset, int64_t total_work_size, |
| 4054 | void *opaque) |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4055 | { |
| 4056 | qemu_progress_print(100.f * offset / total_work_size, 0); |
| 4057 | } |
| 4058 | |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4059 | static int print_amend_option_help(const char *format) |
| 4060 | { |
| 4061 | BlockDriver *drv; |
| 4062 | |
| 4063 | /* Find driver and parse its options */ |
| 4064 | drv = bdrv_find_format(format); |
| 4065 | if (!drv) { |
| 4066 | error_report("Unknown file format '%s'", format); |
| 4067 | return 1; |
| 4068 | } |
| 4069 | |
| 4070 | if (!drv->bdrv_amend_options) { |
| 4071 | error_report("Format driver '%s' does not support option amendment", |
| 4072 | format); |
| 4073 | return 1; |
| 4074 | } |
| 4075 | |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4076 | /* Every driver supporting amendment must have amend_opts */ |
| 4077 | assert(drv->amend_opts); |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4078 | |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4079 | printf("Amend options for '%s':\n", format); |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4080 | qemu_opts_print_help(drv->amend_opts, false); |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4081 | return 0; |
| 4082 | } |
| 4083 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4084 | static int img_amend(int argc, char **argv) |
| 4085 | { |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 4086 | Error *err = NULL; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4087 | int c, ret = 0; |
| 4088 | char *options = NULL; |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4089 | QemuOptsList *amend_opts = NULL; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 4090 | QemuOpts *opts = NULL; |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 4091 | const char *fmt = NULL, *filename, *cache; |
| 4092 | int flags; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 4093 | bool writethrough; |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4094 | bool quiet = false, progress = false; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 4095 | BlockBackend *blk = NULL; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4096 | BlockDriverState *bs = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 4097 | bool image_opts = false; |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4098 | bool force = false; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4099 | |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 4100 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4101 | for (;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4102 | static const struct option long_options[] = { |
| 4103 | {"help", no_argument, 0, 'h'}, |
| 4104 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 4105 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4106 | {"force", no_argument, 0, OPTION_FORCE}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4107 | {0, 0, 0, 0} |
| 4108 | }; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4109 | c = getopt_long(argc, argv, ":ho:f:t:pq", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4110 | long_options, NULL); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4111 | if (c == -1) { |
| 4112 | break; |
| 4113 | } |
| 4114 | |
| 4115 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4116 | case ':': |
| 4117 | missing_argument(argv[optind - 1]); |
| 4118 | break; |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4119 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4120 | unrecognized_option(argv[optind - 1]); |
| 4121 | break; |
| 4122 | case 'h': |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4123 | help(); |
| 4124 | break; |
| 4125 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 4126 | if (accumulate_options(&options, optarg) < 0) { |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4127 | ret = -1; |
| 4128 | goto out_no_progress; |
| 4129 | } |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4130 | break; |
| 4131 | case 'f': |
| 4132 | fmt = optarg; |
| 4133 | break; |
| 4134 | case 't': |
| 4135 | cache = optarg; |
| 4136 | break; |
| 4137 | case 'p': |
| 4138 | progress = true; |
| 4139 | break; |
| 4140 | case 'q': |
| 4141 | quiet = true; |
| 4142 | break; |
| 4143 | case OPTION_OBJECT: |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 4144 | user_creatable_process_cmdline(optarg); |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4145 | break; |
| 4146 | case OPTION_IMAGE_OPTS: |
| 4147 | image_opts = true; |
| 4148 | break; |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4149 | case OPTION_FORCE: |
| 4150 | force = true; |
| 4151 | break; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4152 | } |
| 4153 | } |
| 4154 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4155 | if (!options) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 4156 | error_exit("Must specify options (-o)"); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4157 | } |
| 4158 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4159 | if (quiet) { |
| 4160 | progress = false; |
| 4161 | } |
| 4162 | qemu_progress_init(progress, 1.0); |
| 4163 | |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4164 | filename = (optind == argc - 1) ? argv[argc - 1] : NULL; |
| 4165 | if (fmt && has_help_option(options)) { |
| 4166 | /* If a format is explicitly specified (and possibly no filename is |
| 4167 | * given), print option help here */ |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4168 | ret = print_amend_option_help(fmt); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4169 | goto out; |
| 4170 | } |
| 4171 | |
| 4172 | if (optind != argc - 1) { |
Max Reitz | b2f27e4 | 2014-10-27 11:12:52 +0100 | [diff] [blame] | 4173 | error_report("Expecting one image file name"); |
| 4174 | ret = -1; |
| 4175 | goto out; |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4176 | } |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4177 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 4178 | flags = BDRV_O_RDWR; |
| 4179 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 4180 | if (ret < 0) { |
| 4181 | error_report("Invalid cache option: %s", cache); |
| 4182 | goto out; |
| 4183 | } |
| 4184 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4185 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 4186 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 4187 | if (!blk) { |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4188 | ret = -1; |
| 4189 | goto out; |
| 4190 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 4191 | bs = blk_bs(blk); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4192 | |
| 4193 | fmt = bs->drv->format_name; |
| 4194 | |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 4195 | if (has_help_option(options)) { |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4196 | /* If the format was auto-detected, print option help here */ |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4197 | ret = print_amend_option_help(fmt); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4198 | goto out; |
| 4199 | } |
| 4200 | |
Max Reitz | 1f99668 | 2018-05-09 23:00:17 +0200 | [diff] [blame] | 4201 | if (!bs->drv->bdrv_amend_options) { |
| 4202 | error_report("Format driver '%s' does not support option amendment", |
Max Reitz | b2439d2 | 2014-12-02 18:32:47 +0100 | [diff] [blame] | 4203 | fmt); |
| 4204 | ret = -1; |
| 4205 | goto out; |
| 4206 | } |
| 4207 | |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4208 | /* Every driver supporting amendment must have amend_opts */ |
| 4209 | assert(bs->drv->amend_opts); |
Max Reitz | 1f99668 | 2018-05-09 23:00:17 +0200 | [diff] [blame] | 4210 | |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4211 | amend_opts = qemu_opts_append(amend_opts, bs->drv->amend_opts); |
| 4212 | opts = qemu_opts_create(amend_opts, NULL, 0, &error_abort); |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 4213 | if (!qemu_opts_do_parse(opts, options, NULL, &err)) { |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4214 | /* Try to parse options using the create options */ |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4215 | amend_opts = qemu_opts_append(amend_opts, bs->drv->create_opts); |
| 4216 | qemu_opts_del(opts); |
| 4217 | opts = qemu_opts_create(amend_opts, NULL, 0, &error_abort); |
Markus Armbruster | 9e194e0 | 2020-07-07 18:06:11 +0200 | [diff] [blame] | 4218 | if (qemu_opts_do_parse(opts, options, NULL, NULL)) { |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4219 | error_append_hint(&err, |
| 4220 | "This option is only supported for image creation\n"); |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4221 | } |
| 4222 | |
Paolo Bonzini | ece9086 | 2017-01-04 15:56:24 +0100 | [diff] [blame] | 4223 | error_report_err(err); |
| 4224 | ret = -1; |
| 4225 | goto out; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4226 | } |
| 4227 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4228 | /* In case the driver does not call amend_status_cb() */ |
| 4229 | qemu_progress_print(0.f, 0); |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4230 | ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL, force, &err); |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4231 | qemu_progress_print(100.f, 0); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4232 | if (ret < 0) { |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 4233 | error_report_err(err); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4234 | goto out; |
| 4235 | } |
| 4236 | |
| 4237 | out: |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4238 | qemu_progress_end(); |
| 4239 | |
Max Reitz | e814dff | 2015-08-20 16:00:38 -0700 | [diff] [blame] | 4240 | out_no_progress: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 4241 | blk_unref(blk); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 4242 | qemu_opts_del(opts); |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4243 | qemu_opts_free(amend_opts); |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 4244 | g_free(options); |
| 4245 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4246 | if (ret) { |
| 4247 | return 1; |
| 4248 | } |
| 4249 | return 0; |
| 4250 | } |
| 4251 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4252 | typedef struct BenchData { |
| 4253 | BlockBackend *blk; |
| 4254 | uint64_t image_size; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4255 | bool write; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4256 | int bufsize; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4257 | int step; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4258 | int nrreq; |
| 4259 | int n; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4260 | int flush_interval; |
| 4261 | bool drain_on_flush; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4262 | uint8_t *buf; |
| 4263 | QEMUIOVector *qiov; |
| 4264 | |
| 4265 | int in_flight; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4266 | bool in_flush; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4267 | uint64_t offset; |
| 4268 | } BenchData; |
| 4269 | |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4270 | static void bench_undrained_flush_cb(void *opaque, int ret) |
| 4271 | { |
| 4272 | if (ret < 0) { |
Markus Armbruster | df3c286 | 2016-08-03 13:37:51 +0200 | [diff] [blame] | 4273 | error_report("Failed flush request: %s", strerror(-ret)); |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4274 | exit(EXIT_FAILURE); |
| 4275 | } |
| 4276 | } |
| 4277 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4278 | static void bench_cb(void *opaque, int ret) |
| 4279 | { |
| 4280 | BenchData *b = opaque; |
| 4281 | BlockAIOCB *acb; |
| 4282 | |
| 4283 | if (ret < 0) { |
Markus Armbruster | df3c286 | 2016-08-03 13:37:51 +0200 | [diff] [blame] | 4284 | error_report("Failed request: %s", strerror(-ret)); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4285 | exit(EXIT_FAILURE); |
| 4286 | } |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4287 | |
| 4288 | if (b->in_flush) { |
| 4289 | /* Just finished a flush with drained queue: Start next requests */ |
| 4290 | assert(b->in_flight == 0); |
| 4291 | b->in_flush = false; |
| 4292 | } else if (b->in_flight > 0) { |
| 4293 | int remaining = b->n - b->in_flight; |
| 4294 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4295 | b->n--; |
| 4296 | b->in_flight--; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4297 | |
| 4298 | /* Time for flush? Drain queue if requested, then flush */ |
| 4299 | if (b->flush_interval && remaining % b->flush_interval == 0) { |
| 4300 | if (!b->in_flight || !b->drain_on_flush) { |
| 4301 | BlockCompletionFunc *cb; |
| 4302 | |
| 4303 | if (b->drain_on_flush) { |
| 4304 | b->in_flush = true; |
| 4305 | cb = bench_cb; |
| 4306 | } else { |
| 4307 | cb = bench_undrained_flush_cb; |
| 4308 | } |
| 4309 | |
| 4310 | acb = blk_aio_flush(b->blk, cb, b); |
| 4311 | if (!acb) { |
| 4312 | error_report("Failed to issue flush request"); |
| 4313 | exit(EXIT_FAILURE); |
| 4314 | } |
| 4315 | } |
| 4316 | if (b->drain_on_flush) { |
| 4317 | return; |
| 4318 | } |
| 4319 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4320 | } |
| 4321 | |
| 4322 | while (b->n > b->in_flight && b->in_flight < b->nrreq) { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4323 | int64_t offset = b->offset; |
| 4324 | /* blk_aio_* might look for completed I/Os and kick bench_cb |
| 4325 | * again, so make sure this operation is counted by in_flight |
| 4326 | * and b->offset is ready for the next submission. |
| 4327 | */ |
| 4328 | b->in_flight++; |
| 4329 | b->offset += b->step; |
| 4330 | b->offset %= b->image_size; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4331 | if (b->write) { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4332 | 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] | 4333 | } else { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4334 | 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] | 4335 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4336 | if (!acb) { |
| 4337 | error_report("Failed to issue request"); |
| 4338 | exit(EXIT_FAILURE); |
| 4339 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4340 | } |
| 4341 | } |
| 4342 | |
| 4343 | static int img_bench(int argc, char **argv) |
| 4344 | { |
| 4345 | int c, ret = 0; |
| 4346 | const char *fmt = NULL, *filename; |
| 4347 | bool quiet = false; |
| 4348 | bool image_opts = false; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4349 | bool is_write = false; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4350 | int count = 75000; |
| 4351 | int depth = 64; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4352 | int64_t offset = 0; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4353 | size_t bufsize = 4096; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4354 | int pattern = 0; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4355 | size_t step = 0; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4356 | int flush_interval = 0; |
| 4357 | bool drain_on_flush = true; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4358 | int64_t image_size; |
| 4359 | BlockBackend *blk = NULL; |
| 4360 | BenchData data = {}; |
| 4361 | int flags = 0; |
Kevin Wolf | 604e861 | 2016-06-14 11:29:32 +0200 | [diff] [blame] | 4362 | bool writethrough = false; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4363 | struct timeval t1, t2; |
| 4364 | int i; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4365 | bool force_share = false; |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4366 | size_t buf_size; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4367 | |
| 4368 | for (;;) { |
| 4369 | static const struct option long_options[] = { |
| 4370 | {"help", no_argument, 0, 'h'}, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4371 | {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL}, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4372 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4373 | {"pattern", required_argument, 0, OPTION_PATTERN}, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4374 | {"no-drain", no_argument, 0, OPTION_NO_DRAIN}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4375 | {"force-share", no_argument, 0, 'U'}, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4376 | {0, 0, 0, 0} |
| 4377 | }; |
Aarushi Mehta | cdd2677 | 2020-01-20 14:18:55 +0000 | [diff] [blame] | 4378 | c = getopt_long(argc, argv, ":hc:d:f:ni:o:qs:S:t:wU", long_options, |
| 4379 | NULL); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4380 | if (c == -1) { |
| 4381 | break; |
| 4382 | } |
| 4383 | |
| 4384 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4385 | case ':': |
| 4386 | missing_argument(argv[optind - 1]); |
| 4387 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4388 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4389 | unrecognized_option(argv[optind - 1]); |
| 4390 | break; |
| 4391 | case 'h': |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4392 | help(); |
| 4393 | break; |
| 4394 | case 'c': |
| 4395 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4396 | unsigned long res; |
| 4397 | |
| 4398 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4399 | error_report("Invalid request count specified"); |
| 4400 | return 1; |
| 4401 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4402 | count = res; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4403 | break; |
| 4404 | } |
| 4405 | case 'd': |
| 4406 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4407 | unsigned long res; |
| 4408 | |
| 4409 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4410 | error_report("Invalid queue depth specified"); |
| 4411 | return 1; |
| 4412 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4413 | depth = res; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4414 | break; |
| 4415 | } |
| 4416 | case 'f': |
| 4417 | fmt = optarg; |
| 4418 | break; |
| 4419 | case 'n': |
| 4420 | flags |= BDRV_O_NATIVE_AIO; |
| 4421 | break; |
Aarushi Mehta | cdd2677 | 2020-01-20 14:18:55 +0000 | [diff] [blame] | 4422 | case 'i': |
| 4423 | ret = bdrv_parse_aio(optarg, &flags); |
| 4424 | if (ret < 0) { |
| 4425 | error_report("Invalid aio option: %s", optarg); |
| 4426 | ret = -1; |
| 4427 | goto out; |
| 4428 | } |
| 4429 | break; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4430 | case 'o': |
| 4431 | { |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4432 | offset = cvtnum("offset", optarg); |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4433 | if (offset < 0) { |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4434 | return 1; |
| 4435 | } |
| 4436 | break; |
| 4437 | } |
| 4438 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4439 | case 'q': |
| 4440 | quiet = true; |
| 4441 | break; |
| 4442 | case 's': |
| 4443 | { |
| 4444 | int64_t sval; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4445 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4446 | sval = cvtnum_full("buffer size", optarg, 0, INT_MAX); |
| 4447 | if (sval < 0) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4448 | return 1; |
| 4449 | } |
| 4450 | |
| 4451 | bufsize = sval; |
| 4452 | break; |
| 4453 | } |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4454 | case 'S': |
| 4455 | { |
| 4456 | int64_t sval; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4457 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4458 | sval = cvtnum_full("step_size", optarg, 0, INT_MAX); |
| 4459 | if (sval < 0) { |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4460 | return 1; |
| 4461 | } |
| 4462 | |
| 4463 | step = sval; |
| 4464 | break; |
| 4465 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4466 | case 't': |
| 4467 | ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough); |
| 4468 | if (ret < 0) { |
| 4469 | error_report("Invalid cache mode"); |
| 4470 | ret = -1; |
| 4471 | goto out; |
| 4472 | } |
| 4473 | break; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4474 | case 'w': |
| 4475 | flags |= BDRV_O_RDWR; |
| 4476 | is_write = true; |
| 4477 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4478 | case 'U': |
| 4479 | force_share = true; |
| 4480 | break; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4481 | case OPTION_PATTERN: |
| 4482 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4483 | unsigned long res; |
| 4484 | |
| 4485 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) { |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4486 | error_report("Invalid pattern byte specified"); |
| 4487 | return 1; |
| 4488 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4489 | pattern = res; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4490 | break; |
| 4491 | } |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4492 | case OPTION_FLUSH_INTERVAL: |
| 4493 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4494 | unsigned long res; |
| 4495 | |
| 4496 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4497 | error_report("Invalid flush interval specified"); |
| 4498 | return 1; |
| 4499 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4500 | flush_interval = res; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4501 | break; |
| 4502 | } |
| 4503 | case OPTION_NO_DRAIN: |
| 4504 | drain_on_flush = false; |
| 4505 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4506 | case OPTION_IMAGE_OPTS: |
| 4507 | image_opts = true; |
| 4508 | break; |
| 4509 | } |
| 4510 | } |
| 4511 | |
| 4512 | if (optind != argc - 1) { |
| 4513 | error_exit("Expecting one image file name"); |
| 4514 | } |
| 4515 | filename = argv[argc - 1]; |
| 4516 | |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4517 | if (!is_write && flush_interval) { |
| 4518 | error_report("--flush-interval is only available in write tests"); |
| 4519 | ret = -1; |
| 4520 | goto out; |
| 4521 | } |
| 4522 | if (flush_interval && flush_interval < depth) { |
| 4523 | error_report("Flush interval can't be smaller than depth"); |
| 4524 | ret = -1; |
| 4525 | goto out; |
| 4526 | } |
| 4527 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4528 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 4529 | force_share); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4530 | if (!blk) { |
| 4531 | ret = -1; |
| 4532 | goto out; |
| 4533 | } |
| 4534 | |
| 4535 | image_size = blk_getlength(blk); |
| 4536 | if (image_size < 0) { |
| 4537 | ret = image_size; |
| 4538 | goto out; |
| 4539 | } |
| 4540 | |
| 4541 | data = (BenchData) { |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4542 | .blk = blk, |
| 4543 | .image_size = image_size, |
| 4544 | .bufsize = bufsize, |
| 4545 | .step = step ?: bufsize, |
| 4546 | .nrreq = depth, |
| 4547 | .n = count, |
| 4548 | .offset = offset, |
| 4549 | .write = is_write, |
| 4550 | .flush_interval = flush_interval, |
| 4551 | .drain_on_flush = drain_on_flush, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4552 | }; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4553 | printf("Sending %d %s requests, %d bytes each, %d in parallel " |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4554 | "(starting at offset %" PRId64 ", step size %d)\n", |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4555 | data.n, data.write ? "write" : "read", data.bufsize, data.nrreq, |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4556 | data.offset, data.step); |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4557 | if (flush_interval) { |
| 4558 | printf("Sending flush every %d requests\n", flush_interval); |
| 4559 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4560 | |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4561 | buf_size = data.nrreq * data.bufsize; |
| 4562 | data.buf = blk_blockalign(blk, buf_size); |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4563 | memset(data.buf, pattern, data.nrreq * data.bufsize); |
| 4564 | |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4565 | blk_register_buf(blk, data.buf, buf_size); |
| 4566 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4567 | data.qiov = g_new(QEMUIOVector, data.nrreq); |
| 4568 | for (i = 0; i < data.nrreq; i++) { |
| 4569 | qemu_iovec_init(&data.qiov[i], 1); |
| 4570 | qemu_iovec_add(&data.qiov[i], |
| 4571 | data.buf + i * data.bufsize, data.bufsize); |
| 4572 | } |
| 4573 | |
| 4574 | gettimeofday(&t1, NULL); |
| 4575 | bench_cb(&data, 0); |
| 4576 | |
| 4577 | while (data.n > 0) { |
| 4578 | main_loop_wait(false); |
| 4579 | } |
| 4580 | gettimeofday(&t2, NULL); |
| 4581 | |
| 4582 | printf("Run completed in %3.3f seconds.\n", |
| 4583 | (t2.tv_sec - t1.tv_sec) |
| 4584 | + ((double)(t2.tv_usec - t1.tv_usec) / 1000000)); |
| 4585 | |
| 4586 | out: |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4587 | if (data.buf) { |
| 4588 | blk_unregister_buf(blk, data.buf); |
| 4589 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4590 | qemu_vfree(data.buf); |
| 4591 | blk_unref(blk); |
| 4592 | |
| 4593 | if (ret) { |
| 4594 | return 1; |
| 4595 | } |
| 4596 | return 0; |
| 4597 | } |
| 4598 | |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4599 | enum ImgBitmapAct { |
| 4600 | BITMAP_ADD, |
| 4601 | BITMAP_REMOVE, |
| 4602 | BITMAP_CLEAR, |
| 4603 | BITMAP_ENABLE, |
| 4604 | BITMAP_DISABLE, |
| 4605 | BITMAP_MERGE, |
| 4606 | }; |
| 4607 | typedef struct ImgBitmapAction { |
| 4608 | enum ImgBitmapAct act; |
| 4609 | const char *src; /* only used for merge */ |
| 4610 | QSIMPLEQ_ENTRY(ImgBitmapAction) next; |
| 4611 | } ImgBitmapAction; |
| 4612 | |
| 4613 | static int img_bitmap(int argc, char **argv) |
| 4614 | { |
| 4615 | Error *err = NULL; |
| 4616 | int c, ret = 1; |
| 4617 | QemuOpts *opts = NULL; |
| 4618 | const char *fmt = NULL, *src_fmt = NULL, *src_filename = NULL; |
| 4619 | const char *filename, *bitmap; |
| 4620 | BlockBackend *blk = NULL, *src = NULL; |
| 4621 | BlockDriverState *bs = NULL, *src_bs = NULL; |
| 4622 | bool image_opts = false; |
| 4623 | int64_t granularity = 0; |
| 4624 | bool add = false, merge = false; |
| 4625 | QSIMPLEQ_HEAD(, ImgBitmapAction) actions; |
| 4626 | ImgBitmapAction *act, *act_next; |
| 4627 | const char *op; |
| 4628 | |
| 4629 | QSIMPLEQ_INIT(&actions); |
| 4630 | |
| 4631 | for (;;) { |
| 4632 | static const struct option long_options[] = { |
| 4633 | {"help", no_argument, 0, 'h'}, |
| 4634 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 4635 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
| 4636 | {"add", no_argument, 0, OPTION_ADD}, |
| 4637 | {"remove", no_argument, 0, OPTION_REMOVE}, |
| 4638 | {"clear", no_argument, 0, OPTION_CLEAR}, |
| 4639 | {"enable", no_argument, 0, OPTION_ENABLE}, |
| 4640 | {"disable", no_argument, 0, OPTION_DISABLE}, |
| 4641 | {"merge", required_argument, 0, OPTION_MERGE}, |
| 4642 | {"granularity", required_argument, 0, 'g'}, |
| 4643 | {"source-file", required_argument, 0, 'b'}, |
| 4644 | {"source-format", required_argument, 0, 'F'}, |
| 4645 | {0, 0, 0, 0} |
| 4646 | }; |
| 4647 | c = getopt_long(argc, argv, ":b:f:F:g:h", long_options, NULL); |
| 4648 | if (c == -1) { |
| 4649 | break; |
| 4650 | } |
| 4651 | |
| 4652 | switch (c) { |
| 4653 | case ':': |
| 4654 | missing_argument(argv[optind - 1]); |
| 4655 | break; |
| 4656 | case '?': |
| 4657 | unrecognized_option(argv[optind - 1]); |
| 4658 | break; |
| 4659 | case 'h': |
| 4660 | help(); |
| 4661 | break; |
| 4662 | case 'b': |
| 4663 | src_filename = optarg; |
| 4664 | break; |
| 4665 | case 'f': |
| 4666 | fmt = optarg; |
| 4667 | break; |
| 4668 | case 'F': |
| 4669 | src_fmt = optarg; |
| 4670 | break; |
| 4671 | case 'g': |
| 4672 | granularity = cvtnum("granularity", optarg); |
| 4673 | if (granularity < 0) { |
| 4674 | return 1; |
| 4675 | } |
| 4676 | break; |
| 4677 | case OPTION_ADD: |
| 4678 | act = g_new0(ImgBitmapAction, 1); |
| 4679 | act->act = BITMAP_ADD; |
| 4680 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4681 | add = true; |
| 4682 | break; |
| 4683 | case OPTION_REMOVE: |
| 4684 | act = g_new0(ImgBitmapAction, 1); |
| 4685 | act->act = BITMAP_REMOVE; |
| 4686 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4687 | break; |
| 4688 | case OPTION_CLEAR: |
| 4689 | act = g_new0(ImgBitmapAction, 1); |
| 4690 | act->act = BITMAP_CLEAR; |
| 4691 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4692 | break; |
| 4693 | case OPTION_ENABLE: |
| 4694 | act = g_new0(ImgBitmapAction, 1); |
| 4695 | act->act = BITMAP_ENABLE; |
| 4696 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4697 | break; |
| 4698 | case OPTION_DISABLE: |
| 4699 | act = g_new0(ImgBitmapAction, 1); |
| 4700 | act->act = BITMAP_DISABLE; |
| 4701 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4702 | break; |
| 4703 | case OPTION_MERGE: |
| 4704 | act = g_new0(ImgBitmapAction, 1); |
| 4705 | act->act = BITMAP_MERGE; |
| 4706 | act->src = optarg; |
| 4707 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4708 | merge = true; |
| 4709 | break; |
| 4710 | case OPTION_OBJECT: |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 4711 | user_creatable_process_cmdline(optarg); |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4712 | break; |
| 4713 | case OPTION_IMAGE_OPTS: |
| 4714 | image_opts = true; |
| 4715 | break; |
| 4716 | } |
| 4717 | } |
| 4718 | |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4719 | if (QSIMPLEQ_EMPTY(&actions)) { |
| 4720 | error_report("Need at least one of --add, --remove, --clear, " |
| 4721 | "--enable, --disable, or --merge"); |
| 4722 | goto out; |
| 4723 | } |
| 4724 | |
| 4725 | if (granularity && !add) { |
| 4726 | error_report("granularity only supported with --add"); |
| 4727 | goto out; |
| 4728 | } |
| 4729 | if (src_fmt && !src_filename) { |
| 4730 | error_report("-F only supported with -b"); |
| 4731 | goto out; |
| 4732 | } |
| 4733 | if (src_filename && !merge) { |
| 4734 | error_report("Merge bitmap source file only supported with " |
| 4735 | "--merge"); |
| 4736 | goto out; |
| 4737 | } |
| 4738 | |
| 4739 | if (optind != argc - 2) { |
| 4740 | error_report("Expecting filename and bitmap name"); |
| 4741 | goto out; |
| 4742 | } |
| 4743 | |
| 4744 | filename = argv[optind]; |
| 4745 | bitmap = argv[optind + 1]; |
| 4746 | |
Eric Blake | 14f16bf | 2020-09-14 14:10:09 -0500 | [diff] [blame] | 4747 | /* |
| 4748 | * No need to open backing chains; we will be manipulating bitmaps |
| 4749 | * directly in this image without reference to image contents. |
| 4750 | */ |
| 4751 | blk = img_open(image_opts, filename, fmt, BDRV_O_RDWR | BDRV_O_NO_BACKING, |
| 4752 | false, false, false); |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4753 | if (!blk) { |
| 4754 | goto out; |
| 4755 | } |
| 4756 | bs = blk_bs(blk); |
| 4757 | if (src_filename) { |
Eric Blake | 14f16bf | 2020-09-14 14:10:09 -0500 | [diff] [blame] | 4758 | src = img_open(false, src_filename, src_fmt, BDRV_O_NO_BACKING, |
| 4759 | false, false, false); |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4760 | if (!src) { |
| 4761 | goto out; |
| 4762 | } |
| 4763 | src_bs = blk_bs(src); |
| 4764 | } else { |
| 4765 | src_bs = bs; |
| 4766 | } |
| 4767 | |
| 4768 | QSIMPLEQ_FOREACH_SAFE(act, &actions, next, act_next) { |
| 4769 | switch (act->act) { |
| 4770 | case BITMAP_ADD: |
| 4771 | qmp_block_dirty_bitmap_add(bs->node_name, bitmap, |
| 4772 | !!granularity, granularity, true, true, |
| 4773 | false, false, &err); |
| 4774 | op = "add"; |
| 4775 | break; |
| 4776 | case BITMAP_REMOVE: |
| 4777 | qmp_block_dirty_bitmap_remove(bs->node_name, bitmap, &err); |
| 4778 | op = "remove"; |
| 4779 | break; |
| 4780 | case BITMAP_CLEAR: |
| 4781 | qmp_block_dirty_bitmap_clear(bs->node_name, bitmap, &err); |
| 4782 | op = "clear"; |
| 4783 | break; |
| 4784 | case BITMAP_ENABLE: |
| 4785 | qmp_block_dirty_bitmap_enable(bs->node_name, bitmap, &err); |
| 4786 | op = "enable"; |
| 4787 | break; |
| 4788 | case BITMAP_DISABLE: |
| 4789 | qmp_block_dirty_bitmap_disable(bs->node_name, bitmap, &err); |
| 4790 | op = "disable"; |
| 4791 | break; |
Eric Blake | 6c729dd | 2020-05-21 14:21:35 -0500 | [diff] [blame] | 4792 | case BITMAP_MERGE: |
| 4793 | do_dirty_bitmap_merge(bs->node_name, bitmap, src_bs->node_name, |
| 4794 | act->src, &err); |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4795 | op = "merge"; |
| 4796 | break; |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4797 | default: |
| 4798 | g_assert_not_reached(); |
| 4799 | } |
| 4800 | |
| 4801 | if (err) { |
| 4802 | error_reportf_err(err, "Operation %s on bitmap %s failed: ", |
| 4803 | op, bitmap); |
| 4804 | goto out; |
| 4805 | } |
| 4806 | g_free(act); |
| 4807 | } |
| 4808 | |
| 4809 | ret = 0; |
| 4810 | |
| 4811 | out: |
| 4812 | blk_unref(src); |
| 4813 | blk_unref(blk); |
| 4814 | qemu_opts_del(opts); |
| 4815 | return ret; |
| 4816 | } |
| 4817 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4818 | #define C_BS 01 |
| 4819 | #define C_COUNT 02 |
| 4820 | #define C_IF 04 |
| 4821 | #define C_OF 010 |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4822 | #define C_SKIP 020 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4823 | |
| 4824 | struct DdInfo { |
| 4825 | unsigned int flags; |
| 4826 | int64_t count; |
| 4827 | }; |
| 4828 | |
| 4829 | struct DdIo { |
| 4830 | int bsz; /* Block size */ |
| 4831 | char *filename; |
| 4832 | uint8_t *buf; |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4833 | int64_t offset; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4834 | }; |
| 4835 | |
| 4836 | struct DdOpts { |
| 4837 | const char *name; |
| 4838 | int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *); |
| 4839 | unsigned int flag; |
| 4840 | }; |
| 4841 | |
| 4842 | static int img_dd_bs(const char *arg, |
| 4843 | struct DdIo *in, struct DdIo *out, |
| 4844 | struct DdInfo *dd) |
| 4845 | { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4846 | int64_t res; |
| 4847 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4848 | res = cvtnum_full("bs", arg, 1, INT_MAX); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4849 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4850 | if (res < 0) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4851 | return 1; |
| 4852 | } |
| 4853 | in->bsz = out->bsz = res; |
| 4854 | |
| 4855 | return 0; |
| 4856 | } |
| 4857 | |
| 4858 | static int img_dd_count(const char *arg, |
| 4859 | struct DdIo *in, struct DdIo *out, |
| 4860 | struct DdInfo *dd) |
| 4861 | { |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4862 | dd->count = cvtnum("count", arg); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4863 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4864 | if (dd->count < 0) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4865 | return 1; |
| 4866 | } |
| 4867 | |
| 4868 | return 0; |
| 4869 | } |
| 4870 | |
| 4871 | static int img_dd_if(const char *arg, |
| 4872 | struct DdIo *in, struct DdIo *out, |
| 4873 | struct DdInfo *dd) |
| 4874 | { |
| 4875 | in->filename = g_strdup(arg); |
| 4876 | |
| 4877 | return 0; |
| 4878 | } |
| 4879 | |
| 4880 | static int img_dd_of(const char *arg, |
| 4881 | struct DdIo *in, struct DdIo *out, |
| 4882 | struct DdInfo *dd) |
| 4883 | { |
| 4884 | out->filename = g_strdup(arg); |
| 4885 | |
| 4886 | return 0; |
| 4887 | } |
| 4888 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4889 | static int img_dd_skip(const char *arg, |
| 4890 | struct DdIo *in, struct DdIo *out, |
| 4891 | struct DdInfo *dd) |
| 4892 | { |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4893 | in->offset = cvtnum("skip", arg); |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4894 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4895 | if (in->offset < 0) { |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4896 | return 1; |
| 4897 | } |
| 4898 | |
| 4899 | return 0; |
| 4900 | } |
| 4901 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4902 | static int img_dd(int argc, char **argv) |
| 4903 | { |
| 4904 | int ret = 0; |
| 4905 | char *arg = NULL; |
| 4906 | char *tmp; |
| 4907 | BlockDriver *drv = NULL, *proto_drv = NULL; |
| 4908 | BlockBackend *blk1 = NULL, *blk2 = NULL; |
| 4909 | QemuOpts *opts = NULL; |
| 4910 | QemuOptsList *create_opts = NULL; |
| 4911 | Error *local_err = NULL; |
| 4912 | bool image_opts = false; |
| 4913 | int c, i; |
| 4914 | const char *out_fmt = "raw"; |
| 4915 | const char *fmt = NULL; |
| 4916 | int64_t size = 0; |
| 4917 | int64_t block_count = 0, out_pos, in_pos; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4918 | bool force_share = false; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4919 | struct DdInfo dd = { |
| 4920 | .flags = 0, |
| 4921 | .count = 0, |
| 4922 | }; |
| 4923 | struct DdIo in = { |
| 4924 | .bsz = 512, /* Block size is by default 512 bytes */ |
| 4925 | .filename = NULL, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4926 | .buf = NULL, |
| 4927 | .offset = 0 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4928 | }; |
| 4929 | struct DdIo out = { |
| 4930 | .bsz = 512, |
| 4931 | .filename = NULL, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4932 | .buf = NULL, |
| 4933 | .offset = 0 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4934 | }; |
| 4935 | |
| 4936 | const struct DdOpts options[] = { |
| 4937 | { "bs", img_dd_bs, C_BS }, |
| 4938 | { "count", img_dd_count, C_COUNT }, |
| 4939 | { "if", img_dd_if, C_IF }, |
| 4940 | { "of", img_dd_of, C_OF }, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4941 | { "skip", img_dd_skip, C_SKIP }, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4942 | { NULL, NULL, 0 } |
| 4943 | }; |
| 4944 | const struct option long_options[] = { |
| 4945 | { "help", no_argument, 0, 'h'}, |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 4946 | { "object", required_argument, 0, OPTION_OBJECT}, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4947 | { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4948 | { "force-share", no_argument, 0, 'U'}, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4949 | { 0, 0, 0, 0 } |
| 4950 | }; |
| 4951 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4952 | while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4953 | if (c == EOF) { |
| 4954 | break; |
| 4955 | } |
| 4956 | switch (c) { |
| 4957 | case 'O': |
| 4958 | out_fmt = optarg; |
| 4959 | break; |
| 4960 | case 'f': |
| 4961 | fmt = optarg; |
| 4962 | break; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4963 | case ':': |
| 4964 | missing_argument(argv[optind - 1]); |
| 4965 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4966 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4967 | unrecognized_option(argv[optind - 1]); |
| 4968 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4969 | case 'h': |
| 4970 | help(); |
| 4971 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4972 | case 'U': |
| 4973 | force_share = true; |
| 4974 | break; |
Stefan Hajnoczi | 2a24570 | 2017-06-19 16:00:02 +0100 | [diff] [blame] | 4975 | case OPTION_OBJECT: |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 4976 | user_creatable_process_cmdline(optarg); |
Stefan Hajnoczi | 2a24570 | 2017-06-19 16:00:02 +0100 | [diff] [blame] | 4977 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4978 | case OPTION_IMAGE_OPTS: |
| 4979 | image_opts = true; |
| 4980 | break; |
| 4981 | } |
| 4982 | } |
| 4983 | |
| 4984 | for (i = optind; i < argc; i++) { |
| 4985 | int j; |
| 4986 | arg = g_strdup(argv[i]); |
| 4987 | |
| 4988 | tmp = strchr(arg, '='); |
| 4989 | if (tmp == NULL) { |
| 4990 | error_report("unrecognized operand %s", arg); |
| 4991 | ret = -1; |
| 4992 | goto out; |
| 4993 | } |
| 4994 | |
| 4995 | *tmp++ = '\0'; |
| 4996 | |
| 4997 | for (j = 0; options[j].name != NULL; j++) { |
| 4998 | if (!strcmp(arg, options[j].name)) { |
| 4999 | break; |
| 5000 | } |
| 5001 | } |
| 5002 | if (options[j].name == NULL) { |
| 5003 | error_report("unrecognized operand %s", arg); |
| 5004 | ret = -1; |
| 5005 | goto out; |
| 5006 | } |
| 5007 | |
| 5008 | if (options[j].f(tmp, &in, &out, &dd) != 0) { |
| 5009 | ret = -1; |
| 5010 | goto out; |
| 5011 | } |
| 5012 | dd.flags |= options[j].flag; |
| 5013 | g_free(arg); |
| 5014 | arg = NULL; |
| 5015 | } |
| 5016 | |
| 5017 | if (!(dd.flags & C_IF && dd.flags & C_OF)) { |
| 5018 | error_report("Must specify both input and output files"); |
| 5019 | ret = -1; |
| 5020 | goto out; |
| 5021 | } |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 5022 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 5023 | blk1 = img_open(image_opts, in.filename, fmt, 0, false, false, |
| 5024 | force_share); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5025 | |
| 5026 | if (!blk1) { |
| 5027 | ret = -1; |
| 5028 | goto out; |
| 5029 | } |
| 5030 | |
| 5031 | drv = bdrv_find_format(out_fmt); |
| 5032 | if (!drv) { |
| 5033 | error_report("Unknown file format"); |
| 5034 | ret = -1; |
| 5035 | goto out; |
| 5036 | } |
| 5037 | proto_drv = bdrv_find_protocol(out.filename, true, &local_err); |
| 5038 | |
| 5039 | if (!proto_drv) { |
| 5040 | error_report_err(local_err); |
| 5041 | ret = -1; |
| 5042 | goto out; |
| 5043 | } |
| 5044 | if (!drv->create_opts) { |
| 5045 | error_report("Format driver '%s' does not support image creation", |
| 5046 | drv->format_name); |
| 5047 | ret = -1; |
| 5048 | goto out; |
| 5049 | } |
| 5050 | if (!proto_drv->create_opts) { |
| 5051 | error_report("Protocol driver '%s' does not support image creation", |
| 5052 | proto_drv->format_name); |
| 5053 | ret = -1; |
| 5054 | goto out; |
| 5055 | } |
| 5056 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 5057 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
| 5058 | |
| 5059 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
| 5060 | |
| 5061 | size = blk_getlength(blk1); |
| 5062 | if (size < 0) { |
| 5063 | error_report("Failed to get size for '%s'", in.filename); |
| 5064 | ret = -1; |
| 5065 | goto out; |
| 5066 | } |
| 5067 | |
| 5068 | if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz && |
| 5069 | dd.count * in.bsz < size) { |
| 5070 | size = dd.count * in.bsz; |
| 5071 | } |
| 5072 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 5073 | /* Overflow means the specified offset is beyond input image's size */ |
| 5074 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || |
| 5075 | size < in.bsz * in.offset)) { |
| 5076 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort); |
| 5077 | } else { |
| 5078 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, |
| 5079 | size - in.bsz * in.offset, &error_abort); |
| 5080 | } |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5081 | |
| 5082 | ret = bdrv_create(drv, out.filename, opts, &local_err); |
| 5083 | if (ret < 0) { |
| 5084 | error_reportf_err(local_err, |
| 5085 | "%s: error while creating output image: ", |
| 5086 | out.filename); |
| 5087 | ret = -1; |
| 5088 | goto out; |
| 5089 | } |
| 5090 | |
Daniel P. Berrange | ea204dd | 2017-05-15 17:47:10 +0100 | [diff] [blame] | 5091 | /* TODO, we can't honour --image-opts for the target, |
| 5092 | * since it needs to be given in a format compatible |
| 5093 | * with the bdrv_create() call above which does not |
| 5094 | * support image-opts style. |
| 5095 | */ |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 5096 | 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] | 5097 | false, false, false); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5098 | |
| 5099 | if (!blk2) { |
| 5100 | ret = -1; |
| 5101 | goto out; |
| 5102 | } |
| 5103 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 5104 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || |
| 5105 | size < in.offset * in.bsz)) { |
| 5106 | /* We give a warning if the skip option is bigger than the input |
| 5107 | * size and create an empty output disk image (i.e. like dd(1)). |
| 5108 | */ |
| 5109 | error_report("%s: cannot skip to specified offset", in.filename); |
| 5110 | in_pos = size; |
| 5111 | } else { |
| 5112 | in_pos = in.offset * in.bsz; |
| 5113 | } |
| 5114 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5115 | in.buf = g_new(uint8_t, in.bsz); |
| 5116 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 5117 | for (out_pos = 0; in_pos < size; block_count++) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5118 | int in_ret, out_ret; |
| 5119 | |
| 5120 | if (in_pos + in.bsz > size) { |
| 5121 | in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos); |
| 5122 | } else { |
| 5123 | in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz); |
| 5124 | } |
| 5125 | if (in_ret < 0) { |
| 5126 | error_report("error while reading from input image file: %s", |
| 5127 | strerror(-in_ret)); |
| 5128 | ret = -1; |
| 5129 | goto out; |
| 5130 | } |
| 5131 | in_pos += in_ret; |
| 5132 | |
| 5133 | out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0); |
| 5134 | |
| 5135 | if (out_ret < 0) { |
| 5136 | error_report("error while writing to output image file: %s", |
| 5137 | strerror(-out_ret)); |
| 5138 | ret = -1; |
| 5139 | goto out; |
| 5140 | } |
| 5141 | out_pos += out_ret; |
| 5142 | } |
| 5143 | |
| 5144 | out: |
| 5145 | g_free(arg); |
| 5146 | qemu_opts_del(opts); |
| 5147 | qemu_opts_free(create_opts); |
| 5148 | blk_unref(blk1); |
| 5149 | blk_unref(blk2); |
| 5150 | g_free(in.filename); |
| 5151 | g_free(out.filename); |
| 5152 | g_free(in.buf); |
| 5153 | g_free(out.buf); |
| 5154 | |
| 5155 | if (ret) { |
| 5156 | return 1; |
| 5157 | } |
| 5158 | return 0; |
| 5159 | } |
| 5160 | |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5161 | static void dump_json_block_measure_info(BlockMeasureInfo *info) |
| 5162 | { |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 5163 | GString *str; |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5164 | QObject *obj; |
| 5165 | Visitor *v = qobject_output_visitor_new(&obj); |
| 5166 | |
| 5167 | visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort); |
| 5168 | visit_complete(v, &obj); |
Markus Armbruster | 6589f45 | 2020-12-11 18:11:35 +0100 | [diff] [blame] | 5169 | str = qobject_to_json_pretty(obj, true); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5170 | assert(str != NULL); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 5171 | printf("%s\n", str->str); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 5172 | qobject_unref(obj); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5173 | visit_free(v); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 5174 | g_string_free(str, true); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5175 | } |
| 5176 | |
| 5177 | static int img_measure(int argc, char **argv) |
| 5178 | { |
| 5179 | static const struct option long_options[] = { |
| 5180 | {"help", no_argument, 0, 'h'}, |
| 5181 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
| 5182 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 5183 | {"output", required_argument, 0, OPTION_OUTPUT}, |
| 5184 | {"size", required_argument, 0, OPTION_SIZE}, |
| 5185 | {"force-share", no_argument, 0, 'U'}, |
| 5186 | {0, 0, 0, 0} |
| 5187 | }; |
| 5188 | OutputFormat output_format = OFORMAT_HUMAN; |
| 5189 | BlockBackend *in_blk = NULL; |
| 5190 | BlockDriver *drv; |
| 5191 | const char *filename = NULL; |
| 5192 | const char *fmt = NULL; |
| 5193 | const char *out_fmt = "raw"; |
| 5194 | char *options = NULL; |
| 5195 | char *snapshot_name = NULL; |
| 5196 | bool force_share = false; |
| 5197 | QemuOpts *opts = NULL; |
| 5198 | QemuOpts *object_opts = NULL; |
| 5199 | QemuOpts *sn_opts = NULL; |
| 5200 | QemuOptsList *create_opts = NULL; |
| 5201 | bool image_opts = false; |
| 5202 | uint64_t img_size = UINT64_MAX; |
| 5203 | BlockMeasureInfo *info = NULL; |
| 5204 | Error *local_err = NULL; |
| 5205 | int ret = 1; |
| 5206 | int c; |
| 5207 | |
| 5208 | while ((c = getopt_long(argc, argv, "hf:O:o:l:U", |
| 5209 | long_options, NULL)) != -1) { |
| 5210 | switch (c) { |
| 5211 | case '?': |
| 5212 | case 'h': |
| 5213 | help(); |
| 5214 | break; |
| 5215 | case 'f': |
| 5216 | fmt = optarg; |
| 5217 | break; |
| 5218 | case 'O': |
| 5219 | out_fmt = optarg; |
| 5220 | break; |
| 5221 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 5222 | if (accumulate_options(&options, optarg) < 0) { |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5223 | goto out; |
| 5224 | } |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5225 | break; |
| 5226 | case 'l': |
| 5227 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { |
| 5228 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
| 5229 | optarg, false); |
| 5230 | if (!sn_opts) { |
| 5231 | error_report("Failed in parsing snapshot param '%s'", |
| 5232 | optarg); |
| 5233 | goto out; |
| 5234 | } |
| 5235 | } else { |
| 5236 | snapshot_name = optarg; |
| 5237 | } |
| 5238 | break; |
| 5239 | case 'U': |
| 5240 | force_share = true; |
| 5241 | break; |
| 5242 | case OPTION_OBJECT: |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 5243 | user_creatable_process_cmdline(optarg); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5244 | break; |
| 5245 | case OPTION_IMAGE_OPTS: |
| 5246 | image_opts = true; |
| 5247 | break; |
| 5248 | case OPTION_OUTPUT: |
| 5249 | if (!strcmp(optarg, "json")) { |
| 5250 | output_format = OFORMAT_JSON; |
| 5251 | } else if (!strcmp(optarg, "human")) { |
| 5252 | output_format = OFORMAT_HUMAN; |
| 5253 | } else { |
| 5254 | error_report("--output must be used with human or json " |
| 5255 | "as argument."); |
| 5256 | goto out; |
| 5257 | } |
| 5258 | break; |
| 5259 | case OPTION_SIZE: |
| 5260 | { |
| 5261 | int64_t sval; |
| 5262 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 5263 | sval = cvtnum("image size", optarg); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5264 | if (sval < 0) { |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5265 | goto out; |
| 5266 | } |
| 5267 | img_size = (uint64_t)sval; |
| 5268 | } |
| 5269 | break; |
| 5270 | } |
| 5271 | } |
| 5272 | |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5273 | if (argc - optind > 1) { |
| 5274 | error_report("At most one filename argument is allowed."); |
| 5275 | goto out; |
| 5276 | } else if (argc - optind == 1) { |
| 5277 | filename = argv[optind]; |
| 5278 | } |
| 5279 | |
Stefan Hajnoczi | c3673dc | 2020-02-21 11:25:21 +0000 | [diff] [blame] | 5280 | if (!filename && (image_opts || fmt || snapshot_name || sn_opts)) { |
| 5281 | error_report("--image-opts, -f, and -l require a filename argument."); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5282 | goto out; |
| 5283 | } |
| 5284 | if (filename && img_size != UINT64_MAX) { |
| 5285 | error_report("--size N cannot be used together with a filename."); |
| 5286 | goto out; |
| 5287 | } |
| 5288 | if (!filename && img_size == UINT64_MAX) { |
| 5289 | error_report("Either --size N or one filename must be specified."); |
| 5290 | goto out; |
| 5291 | } |
| 5292 | |
| 5293 | if (filename) { |
| 5294 | in_blk = img_open(image_opts, filename, fmt, 0, |
| 5295 | false, false, force_share); |
| 5296 | if (!in_blk) { |
| 5297 | goto out; |
| 5298 | } |
| 5299 | |
| 5300 | if (sn_opts) { |
| 5301 | bdrv_snapshot_load_tmp(blk_bs(in_blk), |
| 5302 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), |
| 5303 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), |
| 5304 | &local_err); |
| 5305 | } else if (snapshot_name != NULL) { |
| 5306 | bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk), |
| 5307 | snapshot_name, &local_err); |
| 5308 | } |
| 5309 | if (local_err) { |
| 5310 | error_reportf_err(local_err, "Failed to load snapshot: "); |
| 5311 | goto out; |
| 5312 | } |
| 5313 | } |
| 5314 | |
| 5315 | drv = bdrv_find_format(out_fmt); |
| 5316 | if (!drv) { |
| 5317 | error_report("Unknown file format '%s'", out_fmt); |
| 5318 | goto out; |
| 5319 | } |
| 5320 | if (!drv->create_opts) { |
| 5321 | error_report("Format driver '%s' does not support image creation", |
| 5322 | drv->format_name); |
| 5323 | goto out; |
| 5324 | } |
| 5325 | |
| 5326 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 5327 | create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts); |
| 5328 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
| 5329 | if (options) { |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 5330 | if (!qemu_opts_do_parse(opts, options, NULL, &local_err)) { |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5331 | error_report_err(local_err); |
| 5332 | error_report("Invalid options for file format '%s'", out_fmt); |
| 5333 | goto out; |
| 5334 | } |
| 5335 | } |
| 5336 | if (img_size != UINT64_MAX) { |
| 5337 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort); |
| 5338 | } |
| 5339 | |
| 5340 | info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err); |
| 5341 | if (local_err) { |
| 5342 | error_report_err(local_err); |
| 5343 | goto out; |
| 5344 | } |
| 5345 | |
| 5346 | if (output_format == OFORMAT_HUMAN) { |
| 5347 | printf("required size: %" PRIu64 "\n", info->required); |
| 5348 | printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated); |
Eric Blake | 5d72c68 | 2020-05-21 14:21:34 -0500 | [diff] [blame] | 5349 | if (info->has_bitmaps) { |
| 5350 | printf("bitmaps size: %" PRIu64 "\n", info->bitmaps); |
| 5351 | } |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5352 | } else { |
| 5353 | dump_json_block_measure_info(info); |
| 5354 | } |
| 5355 | |
| 5356 | ret = 0; |
| 5357 | |
| 5358 | out: |
| 5359 | qapi_free_BlockMeasureInfo(info); |
| 5360 | qemu_opts_del(object_opts); |
| 5361 | qemu_opts_del(opts); |
| 5362 | qemu_opts_del(sn_opts); |
| 5363 | qemu_opts_free(create_opts); |
| 5364 | g_free(options); |
| 5365 | blk_unref(in_blk); |
| 5366 | return ret; |
| 5367 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 5368 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 5369 | static const img_cmd_t img_cmds[] = { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5370 | #define DEF(option, callback, arg_string) \ |
| 5371 | { option, callback }, |
| 5372 | #include "qemu-img-cmds.h" |
| 5373 | #undef DEF |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5374 | { NULL, NULL, }, |
| 5375 | }; |
| 5376 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5377 | int main(int argc, char **argv) |
| 5378 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 5379 | const img_cmd_t *cmd; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5380 | const char *cmdname; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5381 | int c; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5382 | static const struct option long_options[] = { |
| 5383 | {"help", no_argument, 0, 'h'}, |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5384 | {"version", no_argument, 0, 'V'}, |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5385 | {"trace", required_argument, NULL, 'T'}, |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5386 | {0, 0, 0, 0} |
| 5387 | }; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5388 | |
MORITA Kazutaka | 526eda1 | 2013-07-23 17:30:11 +0900 | [diff] [blame] | 5389 | #ifdef CONFIG_POSIX |
| 5390 | signal(SIGPIPE, SIG_IGN); |
| 5391 | #endif |
| 5392 | |
Daniel P. Berrangé | 98c5d2e | 2020-08-25 11:38:48 +0100 | [diff] [blame] | 5393 | socket_init(); |
Christophe Fergeau | f5852ef | 2019-01-31 17:46:14 +0100 | [diff] [blame] | 5394 | error_init(argv[0]); |
Daniel P. Berrange | fe4db84 | 2016-10-04 14:35:52 +0100 | [diff] [blame] | 5395 | module_call_init(MODULE_INIT_TRACE); |
Fam Zheng | 10f5bff | 2014-02-10 14:48:51 +0800 | [diff] [blame] | 5396 | qemu_init_exec_dir(argv[0]); |
Kevin Wolf | 53f76e5 | 2010-12-16 15:10:32 +0100 | [diff] [blame] | 5397 | |
Markus Armbruster | f9734d5 | 2021-07-20 14:53:53 +0200 | [diff] [blame] | 5398 | qemu_init_main_loop(&error_fatal); |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 5399 | |
Eduardo Habkost | e8f2d27 | 2016-05-12 11:10:04 -0300 | [diff] [blame] | 5400 | qcrypto_init(&error_fatal); |
Daniel P. Berrange | c229708 | 2016-04-06 12:12:06 +0100 | [diff] [blame] | 5401 | |
Daniel P. Berrange | 064097d | 2016-02-10 18:41:01 +0000 | [diff] [blame] | 5402 | module_call_init(MODULE_INIT_QOM); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5403 | bdrv_init(); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 5404 | if (argc < 2) { |
| 5405 | error_exit("Not enough arguments"); |
| 5406 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5407 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 5408 | qemu_add_opts(&qemu_source_opts); |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5409 | qemu_add_opts(&qemu_trace_opts); |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 5410 | |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 5411 | while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) { |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5412 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 5413 | case ':': |
| 5414 | missing_argument(argv[optind - 1]); |
| 5415 | return 0; |
Stefan Hajnoczi | 4581c16 | 2017-03-17 18:45:39 +0800 | [diff] [blame] | 5416 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 5417 | unrecognized_option(argv[optind - 1]); |
| 5418 | return 0; |
| 5419 | case 'h': |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5420 | help(); |
| 5421 | return 0; |
| 5422 | case 'V': |
| 5423 | printf(QEMU_IMG_VERSION); |
| 5424 | return 0; |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5425 | case 'T': |
Paolo Bonzini | 92eecff | 2020-11-02 06:58:41 -0500 | [diff] [blame] | 5426 | trace_opt_parse(optarg); |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5427 | break; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5428 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5429 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5430 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5431 | cmdname = argv[optind]; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5432 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5433 | /* reset getopt_long scanning */ |
| 5434 | argc -= optind; |
| 5435 | if (argc < 1) { |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 5436 | return 0; |
| 5437 | } |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5438 | argv += optind; |
Richard W.M. Jones | d339d76 | 2019-01-18 10:11:14 +0000 | [diff] [blame] | 5439 | qemu_reset_optind(); |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5440 | |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5441 | if (!trace_init_backends()) { |
| 5442 | exit(1); |
| 5443 | } |
Paolo Bonzini | 92eecff | 2020-11-02 06:58:41 -0500 | [diff] [blame] | 5444 | trace_init_file(); |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5445 | qemu_set_log(LOG_TRACE); |
| 5446 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5447 | /* find the command */ |
| 5448 | for (cmd = img_cmds; cmd->name != NULL; cmd++) { |
| 5449 | if (!strcmp(cmdname, cmd->name)) { |
| 5450 | return cmd->handler(argc, argv); |
| 5451 | } |
| 5452 | } |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5453 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5454 | /* not found */ |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 5455 | error_exit("Command not found: %s", cmdname); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5456 | } |