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 | |
Marc-André Lureau | 49f9522 | 2022-04-20 17:25:49 +0400 | [diff] [blame] | 28 | #include "qemu/help-texts.h" |
Marc-André Lureau | 16a18f2 | 2022-03-23 19:57:35 +0400 | [diff] [blame] | 29 | #include "qemu/qemu-progress.h" |
Fam Zheng | 67a1de0 | 2016-06-01 17:44:21 +0800 | [diff] [blame] | 30 | #include "qemu-version.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 31 | #include "qapi/error.h" |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 32 | #include "qapi/qapi-commands-block-core.h" |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 33 | #include "qapi/qapi-visit-block-core.h" |
Daniel P. Berrange | b3db211 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 34 | #include "qapi/qobject-output-visitor.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 35 | #include "qapi/qmp/qjson.h" |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 36 | #include "qapi/qmp/qdict.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 37 | #include "qemu/cutils.h" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 38 | #include "qemu/config-file.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 39 | #include "qemu/option.h" |
| 40 | #include "qemu/error-report.h" |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 41 | #include "qemu/log.h" |
Markus Armbruster | db72581 | 2019-08-12 07:23:50 +0200 | [diff] [blame] | 42 | #include "qemu/main-loop.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 43 | #include "qemu/module.h" |
Daniel P. Berrangé | 98c5d2e | 2020-08-25 11:38:48 +0100 | [diff] [blame] | 44 | #include "qemu/sockets.h" |
Stefano Garzarella | 97ede57 | 2019-05-08 12:43:24 +0200 | [diff] [blame] | 45 | #include "qemu/units.h" |
Peter Maydell | 5df022c | 2022-02-26 18:07:23 +0000 | [diff] [blame] | 46 | #include "qemu/memalign.h" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 47 | #include "qom/object_interfaces.h" |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 48 | #include "sysemu/block-backend.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 49 | #include "block/block_int.h" |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 50 | #include "block/blockjob.h" |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 51 | #include "block/qapi.h" |
Daniel P. Berrange | c229708 | 2016-04-06 12:12:06 +0100 | [diff] [blame] | 52 | #include "crypto/init.h" |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 53 | #include "trace/control.h" |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 54 | #include "qemu/throttle.h" |
| 55 | #include "block/throttle-groups.h" |
bellard | e844533 | 2006-06-14 15:32:10 +0000 | [diff] [blame] | 56 | |
Thomas Huth | 7e563bf | 2018-02-15 12:06:47 +0100 | [diff] [blame] | 57 | #define QEMU_IMG_VERSION "qemu-img version " QEMU_FULL_VERSION \ |
Thomas Huth | 0781dd6 | 2016-10-05 11:54:44 +0200 | [diff] [blame] | 58 | "\n" QEMU_COPYRIGHT "\n" |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 59 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 60 | typedef struct img_cmd_t { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 61 | const char *name; |
| 62 | int (*handler)(int argc, char **argv); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 63 | } img_cmd_t; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 64 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 65 | enum { |
| 66 | OPTION_OUTPUT = 256, |
| 67 | OPTION_BACKING_CHAIN = 257, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 68 | OPTION_OBJECT = 258, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 69 | OPTION_IMAGE_OPTS = 259, |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 70 | OPTION_PATTERN = 260, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 71 | OPTION_FLUSH_INTERVAL = 261, |
| 72 | OPTION_NO_DRAIN = 262, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 73 | OPTION_TARGET_IMAGE_OPTS = 263, |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 74 | OPTION_SIZE = 264, |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 75 | OPTION_PREALLOCATION = 265, |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 76 | OPTION_SHRINK = 266, |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 77 | OPTION_SALVAGE = 267, |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 78 | OPTION_TARGET_IS_ZERO = 268, |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 79 | OPTION_ADD = 269, |
| 80 | OPTION_REMOVE = 270, |
| 81 | OPTION_CLEAR = 271, |
| 82 | OPTION_ENABLE = 272, |
| 83 | OPTION_DISABLE = 273, |
| 84 | OPTION_MERGE = 274, |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 85 | OPTION_BITMAPS = 275, |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 86 | OPTION_FORCE = 276, |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 87 | OPTION_SKIP_BROKEN = 277, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | typedef enum OutputFormat { |
| 91 | OFORMAT_JSON, |
| 92 | OFORMAT_HUMAN, |
| 93 | } OutputFormat; |
| 94 | |
Kevin Wolf | e699614 | 2016-03-15 13:03:11 +0100 | [diff] [blame] | 95 | /* 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] | 96 | #define BDRV_DEFAULT_CACHE "writeback" |
aurel32 | 137519c | 2008-11-30 19:12:49 +0000 | [diff] [blame] | 97 | |
Stefan Hajnoczi | 00c6d40 | 2014-08-27 12:08:56 +0100 | [diff] [blame] | 98 | static void format_print(void *opaque, const char *name) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 99 | { |
Stefan Hajnoczi | 00c6d40 | 2014-08-27 12:08:56 +0100 | [diff] [blame] | 100 | printf(" %s", name); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Marc-André Lureau | 8905770 | 2022-04-20 17:26:02 +0400 | [diff] [blame] | 103 | static G_NORETURN G_GNUC_PRINTF(1, 2) |
| 104 | void error_exit(const char *fmt, ...) |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 105 | { |
| 106 | va_list ap; |
| 107 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 108 | va_start(ap, fmt); |
Markus Armbruster | e9e1d92 | 2019-04-17 21:06:27 +0200 | [diff] [blame] | 109 | error_vreport(fmt, ap); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 110 | va_end(ap); |
| 111 | |
Markus Armbruster | e9e1d92 | 2019-04-17 21:06:27 +0200 | [diff] [blame] | 112 | error_printf("Try 'qemu-img --help' for more information\n"); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 113 | exit(EXIT_FAILURE); |
| 114 | } |
| 115 | |
Marc-André Lureau | 8905770 | 2022-04-20 17:26:02 +0400 | [diff] [blame] | 116 | static G_NORETURN |
| 117 | void missing_argument(const char *option) |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 118 | { |
| 119 | error_exit("missing argument for option '%s'", option); |
| 120 | } |
| 121 | |
Marc-André Lureau | 8905770 | 2022-04-20 17:26:02 +0400 | [diff] [blame] | 122 | static G_NORETURN |
| 123 | void unrecognized_option(const char *option) |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 124 | { |
| 125 | error_exit("unrecognized option '%s'", option); |
| 126 | } |
| 127 | |
Eric Blake | 0562adf | 2020-05-12 20:16:41 -0500 | [diff] [blame] | 128 | /* Please keep in synch with docs/tools/qemu-img.rst */ |
Marc-André Lureau | 8905770 | 2022-04-20 17:26:02 +0400 | [diff] [blame] | 129 | static G_NORETURN |
| 130 | void help(void) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 131 | { |
Paolo Bonzini | e00291c | 2010-02-04 16:49:56 +0100 | [diff] [blame] | 132 | const char *help_msg = |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 133 | QEMU_IMG_VERSION |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 134 | "usage: qemu-img [standard options] command [command options]\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 135 | "QEMU disk image utility\n" |
| 136 | "\n" |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 137 | " '-h', '--help' display this help and exit\n" |
| 138 | " '-V', '--version' output version information and exit\n" |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 139 | " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n" |
| 140 | " specify tracing options\n" |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 141 | "\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 142 | "Command syntax:\n" |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 143 | #define DEF(option, callback, arg_string) \ |
| 144 | " " arg_string "\n" |
| 145 | #include "qemu-img-cmds.h" |
| 146 | #undef DEF |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 147 | "\n" |
| 148 | "Command parameters:\n" |
| 149 | " 'filename' is a disk image filename\n" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 150 | " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n" |
| 151 | " manual page for a description of the object properties. The most common\n" |
| 152 | " object type is a 'secret', which is used to supply passwords and/or\n" |
| 153 | " encryption keys.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 154 | " '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] | 155 | " '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] | 156 | " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n" |
| 157 | " 'directsync' and 'unsafe' (default for convert)\n" |
Stefan Hajnoczi | bb87fdf | 2014-09-02 11:01:02 +0100 | [diff] [blame] | 158 | " 'src_cache' is the cache mode used to read input disk images, the valid\n" |
| 159 | " options are the same as for the 'cache' option\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 160 | " 'size' is the disk image size in bytes. Optional suffixes\n" |
Kevin Wolf | 5e00984 | 2013-06-05 14:19:27 +0200 | [diff] [blame] | 161 | " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n" |
| 162 | " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n" |
| 163 | " supported. 'b' is ignored.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 164 | " 'output_filename' is the destination disk image filename\n" |
| 165 | " 'output_fmt' is the destination format\n" |
| 166 | " 'options' is a comma separated list of format specific options in a\n" |
| 167 | " name=value format. Use -o ? for an overview of the options supported by the\n" |
| 168 | " used format\n" |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 169 | " 'snapshot_param' is param used for internal snapshot, format\n" |
| 170 | " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n" |
| 171 | " '[ID_OR_NAME]'\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 172 | " '-c' indicates that target image must be compressed (qcow format only)\n" |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 173 | " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n" |
| 174 | " new backing file match exactly. The image doesn't need a working\n" |
| 175 | " backing file before rebasing in this case (useful for renaming the\n" |
| 176 | " backing file). For image creation, allow creating without attempting\n" |
| 177 | " to open the backing file.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 178 | " '-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] | 179 | " '-p' show progress of command (only certain commands)\n" |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 180 | " '-q' use Quiet mode - do not print any output (except errors)\n" |
Peter Lieven | 11b6699 | 2013-10-24 12:07:05 +0200 | [diff] [blame] | 181 | " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n" |
| 182 | " contain only zeros for qemu-img to create a sparse image during\n" |
| 183 | " conversion. If the number of bytes is 0, the source will not be scanned for\n" |
| 184 | " unallocated or zero sectors, and the destination image will always be\n" |
| 185 | " fully allocated\n" |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 186 | " '--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] | 187 | " '-n' skips the target volume creation (useful if the volume is created\n" |
| 188 | " prior to running qemu-img)\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 189 | "\n" |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 190 | "Parameters to bitmap subcommand:\n" |
| 191 | " 'bitmap' is the name of the bitmap to manipulate, through one or more\n" |
| 192 | " actions from '--add', '--remove', '--clear', '--enable', '--disable',\n" |
| 193 | " or '--merge source'\n" |
| 194 | " '-g granularity' sets the granularity for '--add' actions\n" |
| 195 | " '-b source' and '-F src_fmt' tell '--merge' actions to find the source\n" |
| 196 | " bitmaps from an alternative file\n" |
| 197 | "\n" |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 198 | "Parameters to check subcommand:\n" |
| 199 | " '-r' tries to repair any inconsistencies that are found during the check.\n" |
| 200 | " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n" |
| 201 | " 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] | 202 | " hiding corruption that has already occurred.\n" |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 203 | "\n" |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 204 | "Parameters to convert subcommand:\n" |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 205 | " '--bitmaps' copies all top-level persistent bitmaps to destination\n" |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 206 | " '-m' specifies how many coroutines work in parallel during the convert\n" |
| 207 | " process (defaults to 8)\n" |
| 208 | " '-W' allow to write to the target out of order rather than sequential\n" |
| 209 | "\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 210 | "Parameters to snapshot subcommand:\n" |
| 211 | " 'snapshot' is the name of the snapshot to create, apply or delete\n" |
| 212 | " '-a' applies a snapshot (revert disk to saved state)\n" |
| 213 | " '-c' creates a snapshot\n" |
| 214 | " '-d' deletes a snapshot\n" |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 215 | " '-l' lists all snapshots in the given image\n" |
| 216 | "\n" |
| 217 | "Parameters to compare subcommand:\n" |
| 218 | " '-f' first image format\n" |
| 219 | " '-F' second image format\n" |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 220 | " '-s' run in Strict mode - fail on different image size or sector allocation\n" |
| 221 | "\n" |
| 222 | "Parameters to dd subcommand:\n" |
| 223 | " 'bs=BYTES' read and write up to BYTES bytes at a time " |
| 224 | "(default: 512)\n" |
| 225 | " 'count=N' copy only N input blocks\n" |
| 226 | " 'if=FILE' read from FILE\n" |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 227 | " 'of=FILE' write to FILE\n" |
| 228 | " '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] | 229 | |
| 230 | printf("%s\nSupported formats:", help_msg); |
Andrey Shinkevich | 9ac404c | 2019-03-07 16:33:58 +0300 | [diff] [blame] | 231 | bdrv_iterate_format(format_print, NULL, false); |
Eric Blake | f5048cb | 2017-08-03 11:33:53 -0500 | [diff] [blame] | 232 | printf("\n\n" QEMU_HELP_BOTTOM "\n"); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 233 | exit(EXIT_SUCCESS); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Markus Armbruster | 80c710c | 2020-04-15 09:49:26 +0200 | [diff] [blame] | 236 | /* |
| 237 | * Is @optarg safe for accumulate_options()? |
| 238 | * It is when multiple of them can be joined together separated by ','. |
| 239 | * To make that work, @optarg must not start with ',' (or else a |
| 240 | * separating ',' preceding it gets escaped), and it must not end with |
| 241 | * an odd number of ',' (or else a separating ',' following it gets |
Markus Armbruster | f62514b | 2020-04-15 09:49:27 +0200 | [diff] [blame] | 242 | * escaped), or be empty (or else a separating ',' preceding it can |
| 243 | * escape a separating ',' following it). |
| 244 | * |
Markus Armbruster | 80c710c | 2020-04-15 09:49:26 +0200 | [diff] [blame] | 245 | */ |
| 246 | static bool is_valid_option_list(const char *optarg) |
| 247 | { |
| 248 | size_t len = strlen(optarg); |
| 249 | size_t i; |
| 250 | |
Markus Armbruster | f62514b | 2020-04-15 09:49:27 +0200 | [diff] [blame] | 251 | if (!optarg[0] || optarg[0] == ',') { |
Markus Armbruster | 80c710c | 2020-04-15 09:49:26 +0200 | [diff] [blame] | 252 | return false; |
| 253 | } |
| 254 | |
| 255 | for (i = len; i > 0 && optarg[i - 1] == ','; i--) { |
| 256 | } |
| 257 | if ((len - i) % 2) { |
| 258 | return false; |
| 259 | } |
| 260 | |
| 261 | return true; |
| 262 | } |
| 263 | |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 264 | static int accumulate_options(char **options, char *optarg) |
| 265 | { |
| 266 | char *new_options; |
| 267 | |
| 268 | if (!is_valid_option_list(optarg)) { |
| 269 | error_report("Invalid option list: %s", optarg); |
| 270 | return -1; |
| 271 | } |
| 272 | |
| 273 | if (!*options) { |
| 274 | *options = g_strdup(optarg); |
| 275 | } else { |
| 276 | new_options = g_strdup_printf("%s,%s", *options, optarg); |
| 277 | g_free(*options); |
| 278 | *options = new_options; |
| 279 | } |
| 280 | return 0; |
| 281 | } |
| 282 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 283 | static QemuOptsList qemu_source_opts = { |
| 284 | .name = "source", |
| 285 | .implied_opt_name = "file", |
| 286 | .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head), |
| 287 | .desc = { |
| 288 | { } |
| 289 | }, |
| 290 | }; |
| 291 | |
Marc-André Lureau | 9edc631 | 2022-02-20 20:39:25 +0400 | [diff] [blame] | 292 | static int G_GNUC_PRINTF(2, 3) qprintf(bool quiet, const char *fmt, ...) |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 293 | { |
| 294 | int ret = 0; |
| 295 | if (!quiet) { |
| 296 | va_list args; |
| 297 | va_start(args, fmt); |
| 298 | ret = vprintf(fmt, args); |
| 299 | va_end(args); |
| 300 | } |
| 301 | return ret; |
| 302 | } |
| 303 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 304 | |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 305 | static int print_block_option_help(const char *filename, const char *fmt) |
| 306 | { |
| 307 | BlockDriver *drv, *proto_drv; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 308 | QemuOptsList *create_opts = NULL; |
Max Reitz | b65a5e1 | 2015-02-05 13:58:12 -0500 | [diff] [blame] | 309 | Error *local_err = NULL; |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 310 | |
| 311 | /* Find driver and parse its options */ |
| 312 | drv = bdrv_find_format(fmt); |
| 313 | if (!drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 314 | error_report("Unknown file format '%s'", fmt); |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 315 | return 1; |
| 316 | } |
| 317 | |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 318 | if (!drv->create_opts) { |
| 319 | error_report("Format driver '%s' does not support image creation", fmt); |
| 320 | return 1; |
| 321 | } |
| 322 | |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 323 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 324 | if (filename) { |
Max Reitz | b65a5e1 | 2015-02-05 13:58:12 -0500 | [diff] [blame] | 325 | proto_drv = bdrv_find_protocol(filename, true, &local_err); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 326 | if (!proto_drv) { |
Markus Armbruster | 2867ce4 | 2015-03-12 16:08:02 +0100 | [diff] [blame] | 327 | error_report_err(local_err); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 328 | qemu_opts_free(create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 329 | return 1; |
| 330 | } |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 331 | if (!proto_drv->create_opts) { |
Max Reitz | f099887 | 2018-11-19 11:19:20 +0100 | [diff] [blame] | 332 | error_report("Protocol driver '%s' does not support image creation", |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 333 | proto_drv->format_name); |
Max Reitz | 3ecd5a4 | 2018-11-19 11:19:21 +0100 | [diff] [blame] | 334 | qemu_opts_free(create_opts); |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 335 | return 1; |
| 336 | } |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 337 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 338 | } |
| 339 | |
Max Reitz | f4619af | 2019-04-13 17:20:37 +0200 | [diff] [blame] | 340 | if (filename) { |
| 341 | printf("Supported options:\n"); |
| 342 | } else { |
| 343 | printf("Supported %s options:\n", fmt); |
| 344 | } |
Max Reitz | 6389871 | 2018-10-19 18:49:25 +0200 | [diff] [blame] | 345 | qemu_opts_print_help(create_opts, false); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 346 | qemu_opts_free(create_opts); |
Max Reitz | f4619af | 2019-04-13 17:20:37 +0200 | [diff] [blame] | 347 | |
| 348 | if (!filename) { |
| 349 | printf("\n" |
| 350 | "The protocol level may support further options.\n" |
| 351 | "Specify the target filename to include those options.\n"); |
| 352 | } |
| 353 | |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 354 | return 0; |
| 355 | } |
| 356 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 357 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 358 | static BlockBackend *img_open_opts(const char *optstr, |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 359 | QemuOpts *opts, int flags, bool writethrough, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 360 | bool quiet, bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 361 | { |
| 362 | QDict *options; |
| 363 | Error *local_err = NULL; |
| 364 | BlockBackend *blk; |
| 365 | options = qemu_opts_to_qdict(opts, NULL); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 366 | if (force_share) { |
| 367 | if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE) |
Max Reitz | 4615f87 | 2018-05-02 22:20:50 +0200 | [diff] [blame] | 368 | && strcmp(qdict_get_str(options, BDRV_OPT_FORCE_SHARE), "on")) { |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 369 | error_report("--force-share/-U conflicts with image options"); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 370 | qobject_unref(options); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 371 | return NULL; |
| 372 | } |
Max Reitz | 4615f87 | 2018-05-02 22:20:50 +0200 | [diff] [blame] | 373 | qdict_put_str(options, BDRV_OPT_FORCE_SHARE, "on"); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 374 | } |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 375 | blk = blk_new_open(NULL, NULL, options, flags, &local_err); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 376 | if (!blk) { |
Daniel P. Berrange | 143605a | 2016-04-06 10:16:18 +0100 | [diff] [blame] | 377 | error_reportf_err(local_err, "Could not open '%s': ", optstr); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 378 | return NULL; |
| 379 | } |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 380 | blk_set_enable_write_cache(blk, !writethrough); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 381 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 382 | return blk; |
| 383 | } |
| 384 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 385 | static BlockBackend *img_open_file(const char *filename, |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 386 | QDict *options, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 387 | const char *fmt, int flags, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 388 | bool writethrough, bool quiet, |
| 389 | bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 390 | { |
| 391 | BlockBackend *blk; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 392 | Error *local_err = NULL; |
Kevin Wolf | ad71713 | 2010-12-16 15:37:41 +0100 | [diff] [blame] | 393 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 394 | if (!options) { |
| 395 | options = qdict_new(); |
| 396 | } |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 397 | if (fmt) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 398 | qdict_put_str(options, "driver", fmt); |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 399 | } |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 400 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 401 | if (force_share) { |
Eric Blake | 579cf1d | 2017-05-15 14:54:39 -0500 | [diff] [blame] | 402 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 403 | } |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 404 | blk = blk_new_open(filename, NULL, options, flags, &local_err); |
Max Reitz | 5bd3132 | 2015-02-05 13:58:16 -0500 | [diff] [blame] | 405 | if (!blk) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 406 | error_reportf_err(local_err, "Could not open '%s': ", filename); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 407 | return NULL; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 408 | } |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 409 | blk_set_enable_write_cache(blk, !writethrough); |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 410 | |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 411 | return blk; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 414 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 415 | static int img_add_key_secrets(void *opaque, |
| 416 | const char *name, const char *value, |
| 417 | Error **errp) |
| 418 | { |
| 419 | QDict *options = opaque; |
| 420 | |
| 421 | if (g_str_has_suffix(name, "key-secret")) { |
Eric Blake | 187f47e | 2017-06-24 12:10:07 -0600 | [diff] [blame] | 422 | qdict_put_str(options, name, value); |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 428 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 429 | static BlockBackend *img_open(bool image_opts, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 430 | const char *filename, |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 431 | const char *fmt, int flags, bool writethrough, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 432 | bool quiet, bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 433 | { |
| 434 | BlockBackend *blk; |
| 435 | if (image_opts) { |
| 436 | QemuOpts *opts; |
| 437 | if (fmt) { |
| 438 | error_report("--image-opts and --format are mutually exclusive"); |
| 439 | return NULL; |
| 440 | } |
| 441 | opts = qemu_opts_parse_noisily(qemu_find_opts("source"), |
| 442 | filename, true); |
| 443 | if (!opts) { |
| 444 | return NULL; |
| 445 | } |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 446 | blk = img_open_opts(filename, opts, flags, writethrough, quiet, |
| 447 | force_share); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 448 | } else { |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 449 | blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 450 | force_share); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 451 | } |
| 452 | return blk; |
| 453 | } |
| 454 | |
| 455 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 456 | static int add_old_style_options(const char *fmt, QemuOpts *opts, |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 457 | const char *base_filename, |
| 458 | const char *base_fmt) |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 459 | { |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 460 | if (base_filename) { |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 461 | if (!qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, |
Markus Armbruster | 9e194e0 | 2020-07-07 18:06:11 +0200 | [diff] [blame] | 462 | NULL)) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 463 | error_report("Backing file not supported for file format '%s'", |
| 464 | fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 465 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | if (base_fmt) { |
Markus Armbruster | 9e194e0 | 2020-07-07 18:06:11 +0200 | [diff] [blame] | 469 | if (!qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, NULL)) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 470 | error_report("Backing file format not supported for file " |
| 471 | "format '%s'", fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 472 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 473 | } |
| 474 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 475 | return 0; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 476 | } |
| 477 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 478 | static int64_t cvtnum_full(const char *name, const char *value, int64_t min, |
| 479 | int64_t max) |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 480 | { |
Markus Armbruster | f17fd4f | 2017-02-21 21:14:06 +0100 | [diff] [blame] | 481 | int err; |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 482 | uint64_t res; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 483 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 484 | err = qemu_strtosz(value, NULL, &res); |
| 485 | if (err < 0 && err != -ERANGE) { |
| 486 | error_report("Invalid %s specified. You may use " |
| 487 | "k, M, G, T, P or E suffixes for", name); |
| 488 | error_report("kilobytes, megabytes, gigabytes, terabytes, " |
| 489 | "petabytes and exabytes."); |
Markus Armbruster | f17fd4f | 2017-02-21 21:14:06 +0100 | [diff] [blame] | 490 | return err; |
| 491 | } |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 492 | if (err == -ERANGE || res > max || res < min) { |
| 493 | error_report("Invalid %s specified. Must be between %" PRId64 |
| 494 | " and %" PRId64 ".", name, min, max); |
Markus Armbruster | f46bfdb | 2017-02-21 21:14:07 +0100 | [diff] [blame] | 495 | return -ERANGE; |
| 496 | } |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 497 | return res; |
| 498 | } |
| 499 | |
| 500 | static int64_t cvtnum(const char *name, const char *value) |
| 501 | { |
| 502 | return cvtnum_full(name, value, 0, INT64_MAX); |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 503 | } |
| 504 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 505 | static int img_create(int argc, char **argv) |
| 506 | { |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 507 | int c; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 508 | uint64_t img_size = -1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 509 | const char *fmt = "raw"; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 510 | const char *base_fmt = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 511 | const char *filename; |
| 512 | const char *base_filename = NULL; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 513 | char *options = NULL; |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 514 | Error *local_err = NULL; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 515 | bool quiet = false; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 516 | int flags = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 517 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 518 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 519 | static const struct option long_options[] = { |
| 520 | {"help", no_argument, 0, 'h'}, |
| 521 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 522 | {0, 0, 0, 0} |
| 523 | }; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 524 | c = getopt_long(argc, argv, ":F:b:f:ho:qu", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 525 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 526 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 527 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 528 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 529 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 530 | case ':': |
| 531 | missing_argument(argv[optind - 1]); |
| 532 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 533 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 534 | unrecognized_option(argv[optind - 1]); |
| 535 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 536 | case 'h': |
| 537 | help(); |
| 538 | break; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 539 | case 'F': |
| 540 | base_fmt = optarg; |
| 541 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 542 | case 'b': |
| 543 | base_filename = optarg; |
| 544 | break; |
| 545 | case 'f': |
| 546 | fmt = optarg; |
| 547 | break; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 548 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 549 | if (accumulate_options(&options, optarg) < 0) { |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 550 | goto fail; |
| 551 | } |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 552 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 553 | case 'q': |
| 554 | quiet = true; |
| 555 | break; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 556 | case 'u': |
| 557 | flags |= BDRV_O_NO_BACKING; |
| 558 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 559 | case OPTION_OBJECT: |
| 560 | user_creatable_process_cmdline(optarg); |
| 561 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 562 | } |
| 563 | } |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 564 | |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 565 | /* Get the filename */ |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 566 | filename = (optind < argc) ? argv[optind] : NULL; |
| 567 | if (options && has_help_option(options)) { |
| 568 | g_free(options); |
| 569 | return print_block_option_help(filename, fmt); |
| 570 | } |
| 571 | |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 572 | if (optind >= argc) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 573 | error_exit("Expecting image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 574 | } |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 575 | optind++; |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 576 | |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 577 | /* Get image size, if specified */ |
| 578 | if (optind < argc) { |
Jes Sorensen | 70b4f4b | 2011-01-05 11:41:02 +0100 | [diff] [blame] | 579 | int64_t sval; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 580 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 581 | sval = cvtnum("image size", argv[optind++]); |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 582 | if (sval < 0) { |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 583 | goto fail; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 584 | } |
| 585 | img_size = (uint64_t)sval; |
| 586 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 587 | if (optind != argc) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 588 | error_exit("Unexpected argument: %s", argv[optind]); |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 589 | } |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 590 | |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 591 | bdrv_img_create(filename, fmt, base_filename, base_fmt, |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 592 | options, img_size, flags, quiet, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 593 | if (local_err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 594 | error_reportf_err(local_err, "%s: ", filename); |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 595 | goto fail; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 596 | } |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 597 | |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 598 | g_free(options); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 599 | return 0; |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 600 | |
| 601 | fail: |
| 602 | g_free(options); |
| 603 | return 1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 606 | static void dump_json_image_check(ImageCheck *check, bool quiet) |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 607 | { |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 608 | GString *str; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 609 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 610 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 611 | |
| 612 | visit_type_ImageCheck(v, NULL, &check, &error_abort); |
| 613 | visit_complete(v, &obj); |
Markus Armbruster | 6589f45 | 2020-12-11 18:11:35 +0100 | [diff] [blame] | 614 | str = qobject_to_json_pretty(obj, true); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 615 | assert(str != NULL); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 616 | qprintf(quiet, "%s\n", str->str); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 617 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 618 | visit_free(v); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 619 | g_string_free(str, true); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 620 | } |
| 621 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 622 | static void dump_human_image_check(ImageCheck *check, bool quiet) |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 623 | { |
| 624 | if (!(check->corruptions || check->leaks || check->check_errors)) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 625 | qprintf(quiet, "No errors were found on the image.\n"); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 626 | } else { |
| 627 | if (check->corruptions) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 628 | qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n" |
| 629 | "Data may be corrupted, or further writes to the image " |
| 630 | "may corrupt it.\n", |
| 631 | check->corruptions); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | if (check->leaks) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 635 | qprintf(quiet, |
| 636 | "\n%" PRId64 " leaked clusters were found on the image.\n" |
| 637 | "This means waste of disk space, but no harm to data.\n", |
| 638 | check->leaks); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | if (check->check_errors) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 642 | qprintf(quiet, |
| 643 | "\n%" PRId64 |
| 644 | " internal errors have occurred during the check.\n", |
| 645 | check->check_errors); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | |
| 649 | if (check->total_clusters != 0 && check->allocated_clusters != 0) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 650 | qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, " |
| 651 | "%0.2f%% fragmented, %0.2f%% compressed clusters\n", |
| 652 | check->allocated_clusters, check->total_clusters, |
| 653 | check->allocated_clusters * 100.0 / check->total_clusters, |
| 654 | check->fragmented_clusters * 100.0 / check->allocated_clusters, |
| 655 | check->compressed_clusters * 100.0 / |
| 656 | check->allocated_clusters); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | if (check->image_end_offset) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 660 | qprintf(quiet, |
| 661 | "Image end offset: %" PRId64 "\n", check->image_end_offset); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
| 665 | static int collect_image_check(BlockDriverState *bs, |
| 666 | ImageCheck *check, |
| 667 | const char *filename, |
| 668 | const char *fmt, |
| 669 | int fix) |
| 670 | { |
| 671 | int ret; |
| 672 | BdrvCheckResult result; |
| 673 | |
| 674 | ret = bdrv_check(bs, &result, fix); |
| 675 | if (ret < 0) { |
| 676 | return ret; |
| 677 | } |
| 678 | |
| 679 | check->filename = g_strdup(filename); |
| 680 | check->format = g_strdup(bdrv_get_format_name(bs)); |
| 681 | check->check_errors = result.check_errors; |
| 682 | check->corruptions = result.corruptions; |
| 683 | check->has_corruptions = result.corruptions != 0; |
| 684 | check->leaks = result.leaks; |
| 685 | check->has_leaks = result.leaks != 0; |
| 686 | check->corruptions_fixed = result.corruptions_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 687 | check->has_corruptions_fixed = result.corruptions_fixed != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 688 | check->leaks_fixed = result.leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 689 | check->has_leaks_fixed = result.leaks_fixed != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 690 | check->image_end_offset = result.image_end_offset; |
| 691 | check->has_image_end_offset = result.image_end_offset != 0; |
| 692 | check->total_clusters = result.bfi.total_clusters; |
| 693 | check->has_total_clusters = result.bfi.total_clusters != 0; |
| 694 | check->allocated_clusters = result.bfi.allocated_clusters; |
| 695 | check->has_allocated_clusters = result.bfi.allocated_clusters != 0; |
| 696 | check->fragmented_clusters = result.bfi.fragmented_clusters; |
| 697 | check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0; |
Stefan Hajnoczi | e6439d7 | 2013-02-07 17:15:04 +0100 | [diff] [blame] | 698 | check->compressed_clusters = result.bfi.compressed_clusters; |
| 699 | check->has_compressed_clusters = result.bfi.compressed_clusters != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 700 | |
| 701 | return 0; |
| 702 | } |
| 703 | |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 704 | /* |
| 705 | * Checks an image for consistency. Exit codes: |
| 706 | * |
Max Reitz | d6635c4 | 2014-06-02 22:15:21 +0200 | [diff] [blame] | 707 | * 0 - Check completed, image is good |
| 708 | * 1 - Check not completed because of internal errors |
| 709 | * 2 - Check completed, image is corrupted |
| 710 | * 3 - Check completed, image has leaked clusters, but is good otherwise |
| 711 | * 63 - Checks are not supported by the image format |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 712 | */ |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 713 | static int img_check(int argc, char **argv) |
| 714 | { |
| 715 | int c, ret; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 716 | OutputFormat output_format = OFORMAT_HUMAN; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 717 | const char *filename, *fmt, *output, *cache; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 718 | BlockBackend *blk; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 719 | BlockDriverState *bs; |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 720 | int fix = 0; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 721 | int flags = BDRV_O_CHECK; |
| 722 | bool writethrough; |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 723 | ImageCheck *check; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 724 | bool quiet = false; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 725 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 726 | bool force_share = false; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 727 | |
| 728 | fmt = NULL; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 729 | output = NULL; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 730 | cache = BDRV_DEFAULT_CACHE; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 731 | |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 732 | for(;;) { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 733 | int option_index = 0; |
| 734 | static const struct option long_options[] = { |
| 735 | {"help", no_argument, 0, 'h'}, |
| 736 | {"format", required_argument, 0, 'f'}, |
Prasad Joshi | 4fd6a98 | 2014-03-25 00:08:54 +0530 | [diff] [blame] | 737 | {"repair", required_argument, 0, 'r'}, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 738 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 739 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 740 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 741 | {"force-share", no_argument, 0, 'U'}, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 742 | {0, 0, 0, 0} |
| 743 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 744 | c = getopt_long(argc, argv, ":hf:r:T:qU", |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 745 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 746 | if (c == -1) { |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 747 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 748 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 749 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 750 | case ':': |
| 751 | missing_argument(argv[optind - 1]); |
| 752 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 753 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 754 | unrecognized_option(argv[optind - 1]); |
| 755 | break; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 756 | case 'h': |
| 757 | help(); |
| 758 | break; |
| 759 | case 'f': |
| 760 | fmt = optarg; |
| 761 | break; |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 762 | case 'r': |
| 763 | flags |= BDRV_O_RDWR; |
| 764 | |
| 765 | if (!strcmp(optarg, "leaks")) { |
| 766 | fix = BDRV_FIX_LEAKS; |
| 767 | } else if (!strcmp(optarg, "all")) { |
| 768 | fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS; |
| 769 | } else { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 770 | error_exit("Unknown option value for -r " |
| 771 | "(expecting 'leaks' or 'all'): %s", optarg); |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 772 | } |
| 773 | break; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 774 | case OPTION_OUTPUT: |
| 775 | output = optarg; |
| 776 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 777 | case 'T': |
| 778 | cache = optarg; |
| 779 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 780 | case 'q': |
| 781 | quiet = true; |
| 782 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 783 | case 'U': |
| 784 | force_share = true; |
| 785 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 786 | case OPTION_OBJECT: |
| 787 | user_creatable_process_cmdline(optarg); |
| 788 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 789 | case OPTION_IMAGE_OPTS: |
| 790 | image_opts = true; |
| 791 | break; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 792 | } |
| 793 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 794 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 795 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 796 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 797 | filename = argv[optind++]; |
| 798 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 799 | if (output && !strcmp(output, "json")) { |
| 800 | output_format = OFORMAT_JSON; |
| 801 | } else if (output && !strcmp(output, "human")) { |
| 802 | output_format = OFORMAT_HUMAN; |
| 803 | } else if (output) { |
| 804 | error_report("--output must be used with human or json as argument."); |
| 805 | return 1; |
| 806 | } |
| 807 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 808 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 809 | if (ret < 0) { |
| 810 | error_report("Invalid source cache option: %s", cache); |
| 811 | return 1; |
| 812 | } |
| 813 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 814 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 815 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 816 | if (!blk) { |
| 817 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 818 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 819 | bs = blk_bs(blk); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 820 | |
| 821 | check = g_new0(ImageCheck, 1); |
| 822 | ret = collect_image_check(bs, check, filename, fmt, fix); |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 823 | |
| 824 | if (ret == -ENOTSUP) { |
Max Reitz | 55d492d | 2014-05-31 21:33:30 +0200 | [diff] [blame] | 825 | error_report("This image format does not support checks"); |
Peter Lieven | fefddf9 | 2013-10-24 08:53:34 +0200 | [diff] [blame] | 826 | ret = 63; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 827 | goto fail; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 828 | } |
| 829 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 830 | if (check->corruptions_fixed || check->leaks_fixed) { |
| 831 | int corruptions_fixed, leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 832 | bool has_leaks_fixed, has_corruptions_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 833 | |
| 834 | leaks_fixed = check->leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 835 | has_leaks_fixed = check->has_leaks_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 836 | corruptions_fixed = check->corruptions_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 837 | has_corruptions_fixed = check->has_corruptions_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 838 | |
| 839 | if (output_format == OFORMAT_HUMAN) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 840 | qprintf(quiet, |
| 841 | "The following inconsistencies were found and repaired:\n\n" |
| 842 | " %" PRId64 " leaked clusters\n" |
| 843 | " %" PRId64 " corruptions\n\n" |
| 844 | "Double checking the fixed image now...\n", |
| 845 | check->leaks_fixed, |
| 846 | check->corruptions_fixed); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 847 | } |
| 848 | |
Pan Nengyuan | fc124ea | 2020-02-27 09:29:50 +0800 | [diff] [blame] | 849 | qapi_free_ImageCheck(check); |
| 850 | check = g_new0(ImageCheck, 1); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 851 | ret = collect_image_check(bs, check, filename, fmt, 0); |
| 852 | |
| 853 | check->leaks_fixed = leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 854 | check->has_leaks_fixed = has_leaks_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 855 | check->corruptions_fixed = corruptions_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 856 | check->has_corruptions_fixed = has_corruptions_fixed; |
Kevin Wolf | ccf3471 | 2012-05-11 18:16:54 +0200 | [diff] [blame] | 857 | } |
| 858 | |
Max Reitz | 832390a | 2014-10-23 15:29:12 +0200 | [diff] [blame] | 859 | if (!ret) { |
| 860 | switch (output_format) { |
| 861 | case OFORMAT_HUMAN: |
| 862 | dump_human_image_check(check, quiet); |
| 863 | break; |
| 864 | case OFORMAT_JSON: |
| 865 | dump_json_image_check(check, quiet); |
| 866 | break; |
| 867 | } |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | if (ret || check->check_errors) { |
Max Reitz | 832390a | 2014-10-23 15:29:12 +0200 | [diff] [blame] | 871 | if (ret) { |
| 872 | error_report("Check failed: %s", strerror(-ret)); |
| 873 | } else { |
| 874 | error_report("Check failed"); |
| 875 | } |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 876 | ret = 1; |
| 877 | goto fail; |
| 878 | } |
| 879 | |
| 880 | if (check->corruptions) { |
| 881 | ret = 2; |
| 882 | } else if (check->leaks) { |
| 883 | ret = 3; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 884 | } else { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 885 | ret = 0; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 888 | fail: |
| 889 | qapi_free_ImageCheck(check); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 890 | blk_unref(blk); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 891 | return ret; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 894 | typedef struct CommonBlockJobCBInfo { |
| 895 | BlockDriverState *bs; |
| 896 | Error **errp; |
| 897 | } CommonBlockJobCBInfo; |
| 898 | |
| 899 | static void common_block_job_cb(void *opaque, int ret) |
| 900 | { |
| 901 | CommonBlockJobCBInfo *cbi = opaque; |
| 902 | |
| 903 | if (ret < 0) { |
| 904 | error_setg_errno(cbi->errp, -ret, "Block job failed"); |
| 905 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | static void run_block_job(BlockJob *job, Error **errp) |
| 909 | { |
Emanuele Giuseppe Esposito | a7b4f8f | 2021-06-14 10:11:29 +0200 | [diff] [blame] | 910 | uint64_t progress_current, progress_total; |
Vladimir Sementsov-Ogievskiy | df9a316 | 2021-05-06 17:13:53 +0300 | [diff] [blame] | 911 | AioContext *aio_context = block_job_get_aio_context(job); |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 912 | int ret = 0; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 913 | |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 914 | aio_context_acquire(aio_context); |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 915 | job_ref(&job->job); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 916 | do { |
Kevin Wolf | 30a5c88 | 2018-05-04 12:17:20 +0200 | [diff] [blame] | 917 | float progress = 0.0f; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 918 | aio_poll(aio_context, true); |
Emanuele Giuseppe Esposito | a7b4f8f | 2021-06-14 10:11:29 +0200 | [diff] [blame] | 919 | |
| 920 | progress_get_snapshot(&job->job.progress, &progress_current, |
| 921 | &progress_total); |
| 922 | if (progress_total) { |
| 923 | progress = (float)progress_current / progress_total * 100.f; |
Kevin Wolf | 30a5c88 | 2018-05-04 12:17:20 +0200 | [diff] [blame] | 924 | } |
| 925 | qemu_progress_print(progress, 0); |
Kevin Wolf | df956ae | 2018-04-25 15:09:58 +0200 | [diff] [blame] | 926 | } while (!job_is_ready(&job->job) && !job_is_completed(&job->job)); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 927 | |
Kevin Wolf | dbe5e6c | 2018-04-19 13:04:01 +0200 | [diff] [blame] | 928 | if (!job_is_completed(&job->job)) { |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 929 | ret = job_complete_sync(&job->job, errp); |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 930 | } else { |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 931 | ret = job->job.ret; |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 932 | } |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 933 | job_unref(&job->job); |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 934 | aio_context_release(aio_context); |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 935 | |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 936 | /* publish completion progress only when success */ |
| 937 | if (!ret) { |
| 938 | qemu_progress_print(100.f, 0); |
| 939 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 940 | } |
| 941 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 942 | static int img_commit(int argc, char **argv) |
| 943 | { |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 944 | int c, ret, flags; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 945 | const char *filename, *fmt, *cache, *base; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 946 | BlockBackend *blk; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 947 | BlockDriverState *bs, *base_bs; |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 948 | BlockJob *job; |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 949 | bool progress = false, quiet = false, drop = false; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 950 | bool writethrough; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 951 | Error *local_err = NULL; |
| 952 | CommonBlockJobCBInfo cbi; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 953 | bool image_opts = false; |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 954 | AioContext *aio_context; |
Zhengui | a0441b6 | 2020-10-20 14:47:43 +0000 | [diff] [blame] | 955 | int64_t rate_limit = 0; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 956 | |
| 957 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 958 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 959 | base = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 960 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 961 | static const struct option long_options[] = { |
| 962 | {"help", no_argument, 0, 'h'}, |
| 963 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 964 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 965 | {0, 0, 0, 0} |
| 966 | }; |
Zhengui | a0441b6 | 2020-10-20 14:47:43 +0000 | [diff] [blame] | 967 | c = getopt_long(argc, argv, ":f:ht:b:dpqr:", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 968 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 969 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 970 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 971 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 972 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 973 | case ':': |
| 974 | missing_argument(argv[optind - 1]); |
| 975 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 976 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 977 | unrecognized_option(argv[optind - 1]); |
| 978 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 979 | case 'h': |
| 980 | help(); |
| 981 | break; |
| 982 | case 'f': |
| 983 | fmt = optarg; |
| 984 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 985 | case 't': |
| 986 | cache = optarg; |
| 987 | break; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 988 | case 'b': |
| 989 | base = optarg; |
| 990 | /* -b implies -d */ |
| 991 | drop = true; |
| 992 | break; |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 993 | case 'd': |
| 994 | drop = true; |
| 995 | break; |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 996 | case 'p': |
| 997 | progress = true; |
| 998 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 999 | case 'q': |
| 1000 | quiet = true; |
| 1001 | break; |
Zhengui | a0441b6 | 2020-10-20 14:47:43 +0000 | [diff] [blame] | 1002 | case 'r': |
| 1003 | rate_limit = cvtnum("rate limit", optarg); |
| 1004 | if (rate_limit < 0) { |
| 1005 | return 1; |
| 1006 | } |
| 1007 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 1008 | case OPTION_OBJECT: |
| 1009 | user_creatable_process_cmdline(optarg); |
| 1010 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1011 | case OPTION_IMAGE_OPTS: |
| 1012 | image_opts = true; |
| 1013 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1014 | } |
| 1015 | } |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 1016 | |
| 1017 | /* Progress is not shown in Quiet mode */ |
| 1018 | if (quiet) { |
| 1019 | progress = false; |
| 1020 | } |
| 1021 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 1022 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 1023 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1024 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1025 | filename = argv[optind++]; |
| 1026 | |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1027 | flags = BDRV_O_RDWR | BDRV_O_UNMAP; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1028 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1029 | if (ret < 0) { |
| 1030 | error_report("Invalid cache option: %s", cache); |
Stefan Hajnoczi | a3981eb | 2014-08-26 19:17:54 +0100 | [diff] [blame] | 1031 | return 1; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1032 | } |
| 1033 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1034 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 1035 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1036 | if (!blk) { |
| 1037 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1038 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1039 | bs = blk_bs(blk); |
| 1040 | |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 1041 | qemu_progress_init(progress, 1.f); |
| 1042 | qemu_progress_print(0.f, 100); |
| 1043 | |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1044 | if (base) { |
| 1045 | base_bs = bdrv_find_backing_image(bs, base); |
| 1046 | if (!base_bs) { |
Max Reitz | 6b33f3a | 2016-12-01 03:05:08 +0100 | [diff] [blame] | 1047 | error_setg(&local_err, |
| 1048 | "Did not find '%s' in the backing chain of '%s'", |
| 1049 | base, filename); |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1050 | goto done; |
| 1051 | } |
| 1052 | } else { |
| 1053 | /* This is different from QMP, which by default uses the deepest file in |
| 1054 | * the backing chain (i.e., the very base); however, the traditional |
| 1055 | * behavior of qemu-img commit is using the immediate backing file. */ |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 1056 | base_bs = bdrv_backing_chain_next(bs); |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1057 | if (!base_bs) { |
| 1058 | error_setg(&local_err, "Image does not have a backing file"); |
| 1059 | goto done; |
| 1060 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1063 | cbi = (CommonBlockJobCBInfo){ |
| 1064 | .errp = &local_err, |
| 1065 | .bs = bs, |
| 1066 | }; |
| 1067 | |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 1068 | aio_context = bdrv_get_aio_context(bs); |
| 1069 | aio_context_acquire(aio_context); |
Zhengui | a0441b6 | 2020-10-20 14:47:43 +0000 | [diff] [blame] | 1070 | commit_active_start("commit", bs, base_bs, JOB_DEFAULT, rate_limit, |
Kevin Wolf | 0db832f | 2017-02-20 18:10:05 +0100 | [diff] [blame] | 1071 | BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb, |
Fam Zheng | 78bbd91 | 2017-04-21 20:27:04 +0800 | [diff] [blame] | 1072 | &cbi, false, &local_err); |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 1073 | aio_context_release(aio_context); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1074 | if (local_err) { |
| 1075 | goto done; |
| 1076 | } |
| 1077 | |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1078 | /* When the block job completes, the BlockBackend reference will point to |
| 1079 | * the old backing file. In order to avoid that the top image is already |
| 1080 | * deleted, so we can still empty it afterwards, increment the reference |
| 1081 | * counter here preemptively. */ |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1082 | if (!drop) { |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1083 | bdrv_ref(bs); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1084 | } |
| 1085 | |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 1086 | job = block_job_get("commit"); |
Liam Merwick | 2e2db26 | 2018-11-05 21:38:37 +0000 | [diff] [blame] | 1087 | assert(job); |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 1088 | run_block_job(job, &local_err); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1089 | if (local_err) { |
| 1090 | goto unref_backing; |
| 1091 | } |
| 1092 | |
Max Reitz | 2d97fde | 2020-04-29 16:11:26 +0200 | [diff] [blame] | 1093 | if (!drop) { |
| 1094 | BlockBackend *old_backing_blk; |
| 1095 | |
| 1096 | old_backing_blk = blk_new_with_bs(bs, BLK_PERM_WRITE, BLK_PERM_ALL, |
| 1097 | &local_err); |
| 1098 | if (!old_backing_blk) { |
| 1099 | goto unref_backing; |
| 1100 | } |
| 1101 | ret = blk_make_empty(old_backing_blk, &local_err); |
| 1102 | blk_unref(old_backing_blk); |
| 1103 | if (ret == -ENOTSUP) { |
| 1104 | error_free(local_err); |
| 1105 | local_err = NULL; |
| 1106 | } else if (ret < 0) { |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1107 | goto unref_backing; |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | unref_backing: |
| 1112 | if (!drop) { |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1113 | bdrv_unref(bs); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1114 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1115 | |
| 1116 | done: |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 1117 | qemu_progress_end(); |
| 1118 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1119 | blk_unref(blk); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1120 | |
| 1121 | if (local_err) { |
Markus Armbruster | 6936f29 | 2015-02-10 15:14:02 +0100 | [diff] [blame] | 1122 | error_report_err(local_err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1123 | return 1; |
| 1124 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1125 | |
| 1126 | qprintf(quiet, "Image committed.\n"); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1127 | return 0; |
| 1128 | } |
| 1129 | |
Dmitry Konishchev | f6a00aa | 2011-05-18 15:03:59 +0400 | [diff] [blame] | 1130 | /* |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1131 | * Returns -1 if 'buf' contains only zeroes, otherwise the byte index |
| 1132 | * of the first sector boundary within buf where the sector contains a |
| 1133 | * non-zero byte. This function is robust to a buffer that is not |
| 1134 | * sector-aligned. |
| 1135 | */ |
| 1136 | static int64_t find_nonzero(const uint8_t *buf, int64_t n) |
| 1137 | { |
| 1138 | int64_t i; |
| 1139 | int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE); |
| 1140 | |
| 1141 | for (i = 0; i < end; i += BDRV_SECTOR_SIZE) { |
| 1142 | if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) { |
| 1143 | return i; |
| 1144 | } |
| 1145 | } |
| 1146 | if (i < n && !buffer_is_zero(buf + i, n - end)) { |
| 1147 | return i; |
| 1148 | } |
| 1149 | return -1; |
| 1150 | } |
| 1151 | |
| 1152 | /* |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1153 | * Returns true iff the first sector pointed to by 'buf' contains at least |
| 1154 | * a non-NUL byte. |
| 1155 | * |
| 1156 | * 'pnum' is set to the number of sectors (including and immediately following |
| 1157 | * 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] | 1158 | * The function will try to align the end offset to alignment boundaries so |
zhaolichang | e3a6e0d | 2020-09-17 15:50:20 +0800 | [diff] [blame] | 1159 | * that the request will at least end aligned and consecutive requests will |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1160 | * also start at an aligned offset. |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1161 | */ |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1162 | static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum, |
| 1163 | int64_t sector_num, int alignment) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1164 | { |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1165 | bool is_zero; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1166 | int i, tail; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1167 | |
| 1168 | if (n <= 0) { |
| 1169 | *pnum = 0; |
| 1170 | return 0; |
| 1171 | } |
Yi Li | c075c42 | 2020-08-19 09:36:07 +0800 | [diff] [blame] | 1172 | is_zero = buffer_is_zero(buf, BDRV_SECTOR_SIZE); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1173 | for(i = 1; i < n; i++) { |
Yi Li | c075c42 | 2020-08-19 09:36:07 +0800 | [diff] [blame] | 1174 | buf += BDRV_SECTOR_SIZE; |
| 1175 | if (is_zero != buffer_is_zero(buf, BDRV_SECTOR_SIZE)) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1176 | break; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1177 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1178 | } |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1179 | |
Vladimir Sementsov-Ogievskiy | 96054c7 | 2021-12-17 17:46:54 +0100 | [diff] [blame] | 1180 | if (i == n) { |
| 1181 | /* |
| 1182 | * The whole buf is the same. |
| 1183 | * No reason to split it into chunks, so return now. |
| 1184 | */ |
| 1185 | *pnum = i; |
| 1186 | return !is_zero; |
| 1187 | } |
| 1188 | |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1189 | tail = (sector_num + i) & (alignment - 1); |
| 1190 | if (tail) { |
| 1191 | if (is_zero && i <= tail) { |
Vladimir Sementsov-Ogievskiy | 96054c7 | 2021-12-17 17:46:54 +0100 | [diff] [blame] | 1192 | /* |
| 1193 | * For sure next sector after i is data, and it will rewrite this |
| 1194 | * tail anyway due to RMW. So, let's just write data now. |
| 1195 | */ |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1196 | is_zero = false; |
| 1197 | } |
| 1198 | if (!is_zero) { |
Vladimir Sementsov-Ogievskiy | 96054c7 | 2021-12-17 17:46:54 +0100 | [diff] [blame] | 1199 | /* If possible, align up end offset of allocated areas. */ |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1200 | i += alignment - tail; |
| 1201 | i = MIN(i, n); |
| 1202 | } else { |
Vladimir Sementsov-Ogievskiy | 96054c7 | 2021-12-17 17:46:54 +0100 | [diff] [blame] | 1203 | /* |
| 1204 | * For sure next sector after i is data, and it will rewrite this |
| 1205 | * tail anyway due to RMW. Better is avoid RMW and write zeroes up |
| 1206 | * to aligned bound. |
| 1207 | */ |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1208 | i -= tail; |
| 1209 | } |
| 1210 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1211 | *pnum = i; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1212 | return !is_zero; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1215 | /* |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1216 | * Like is_allocated_sectors, but if the buffer starts with a used sector, |
| 1217 | * up to 'min' consecutive sectors containing zeros are ignored. This avoids |
| 1218 | * breaking up write requests for only small sparse areas. |
| 1219 | */ |
| 1220 | 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] | 1221 | int min, int64_t sector_num, int alignment) |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1222 | { |
| 1223 | int ret; |
| 1224 | int num_checked, num_used; |
| 1225 | |
| 1226 | if (n < min) { |
| 1227 | min = n; |
| 1228 | } |
| 1229 | |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1230 | ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment); |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1231 | if (!ret) { |
| 1232 | return ret; |
| 1233 | } |
| 1234 | |
| 1235 | num_used = *pnum; |
| 1236 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 1237 | n -= *pnum; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1238 | sector_num += *pnum; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1239 | num_checked = num_used; |
| 1240 | |
| 1241 | while (n > 0) { |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1242 | ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment); |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1243 | |
| 1244 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 1245 | n -= *pnum; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1246 | sector_num += *pnum; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1247 | num_checked += *pnum; |
| 1248 | if (ret) { |
| 1249 | num_used = num_checked; |
| 1250 | } else if (*pnum >= min) { |
| 1251 | break; |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | *pnum = num_used; |
| 1256 | return 1; |
| 1257 | } |
| 1258 | |
| 1259 | /* |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1260 | * Compares two buffers sector by sector. Returns 0 if the first |
| 1261 | * sector of each buffer matches, non-zero otherwise. |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1262 | * |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1263 | * pnum is set to the sector-aligned size of the buffer prefix that |
| 1264 | * has the same matching status as the first sector. |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1265 | */ |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1266 | static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2, |
| 1267 | int64_t bytes, int64_t *pnum) |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1268 | { |
Radim Krčmář | 8c1ac47 | 2015-02-20 17:06:15 +0100 | [diff] [blame] | 1269 | bool res; |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1270 | int64_t i = MIN(bytes, BDRV_SECTOR_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1271 | |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1272 | assert(bytes > 0); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1273 | |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1274 | res = !!memcmp(buf1, buf2, i); |
| 1275 | while (i < bytes) { |
| 1276 | int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1277 | |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1278 | if (!!memcmp(buf1 + i, buf2 + i, len) != res) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1279 | break; |
| 1280 | } |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1281 | i += len; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | *pnum = i; |
| 1285 | return res; |
| 1286 | } |
| 1287 | |
Stefano Garzarella | 97ede57 | 2019-05-08 12:43:24 +0200 | [diff] [blame] | 1288 | #define IO_BUF_SIZE (2 * MiB) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1289 | |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1290 | /* |
| 1291 | * Check if passed sectors are empty (not allocated or contain only 0 bytes) |
| 1292 | * |
Eric Blake | 0608e40 | 2017-10-11 22:47:12 -0500 | [diff] [blame] | 1293 | * Intended for use by 'qemu-img compare': Returns 0 in case sectors are |
| 1294 | * filled with 0, 1 if sectors contain non-zero data (this is a comparison |
| 1295 | * failure), and 4 on error (the exit status for read errors), after emitting |
| 1296 | * an error message. |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1297 | * |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1298 | * @param blk: BlockBackend for the image |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1299 | * @param offset: Starting offset to check |
| 1300 | * @param bytes: Number of bytes to check |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1301 | * @param filename: Name of disk file we are checking (logging purpose) |
| 1302 | * @param buffer: Allocated buffer for storing read data |
| 1303 | * @param quiet: Flag for quiet mode |
| 1304 | */ |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1305 | static int check_empty_sectors(BlockBackend *blk, int64_t offset, |
| 1306 | int64_t bytes, const char *filename, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1307 | uint8_t *buffer, bool quiet) |
| 1308 | { |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1309 | int ret = 0; |
| 1310 | int64_t idx; |
| 1311 | |
Alberto Faria | a9262f5 | 2022-07-05 17:15:11 +0100 | [diff] [blame] | 1312 | ret = blk_pread(blk, offset, bytes, buffer, 0); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1313 | if (ret < 0) { |
| 1314 | error_report("Error while reading offset %" PRId64 " of %s: %s", |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1315 | offset, filename, strerror(-ret)); |
Eric Blake | 0608e40 | 2017-10-11 22:47:12 -0500 | [diff] [blame] | 1316 | return 4; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1317 | } |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1318 | idx = find_nonzero(buffer, bytes); |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1319 | if (idx >= 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1320 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1321 | offset + idx); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1322 | return 1; |
| 1323 | } |
| 1324 | |
| 1325 | return 0; |
| 1326 | } |
| 1327 | |
| 1328 | /* |
| 1329 | * Compares two images. Exit codes: |
| 1330 | * |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 1331 | * 0 - Images are identical or the requested help was printed |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1332 | * 1 - Images differ |
| 1333 | * >1 - Error occurred |
| 1334 | */ |
| 1335 | static int img_compare(int argc, char **argv) |
| 1336 | { |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1337 | const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1338 | BlockBackend *blk1, *blk2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1339 | BlockDriverState *bs1, *bs2; |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1340 | int64_t total_size1, total_size2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1341 | uint8_t *buf1 = NULL, *buf2 = NULL; |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1342 | int64_t pnum1, pnum2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1343 | int allocated1, allocated2; |
| 1344 | int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */ |
| 1345 | bool progress = false, quiet = false, strict = false; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1346 | int flags; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1347 | bool writethrough; |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1348 | int64_t total_size; |
| 1349 | int64_t offset = 0; |
| 1350 | int64_t chunk; |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1351 | int c; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1352 | uint64_t progress_base; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1353 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1354 | bool force_share = false; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1355 | |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1356 | cache = BDRV_DEFAULT_CACHE; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1357 | for (;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1358 | static const struct option long_options[] = { |
| 1359 | {"help", no_argument, 0, 'h'}, |
| 1360 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1361 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1362 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1363 | {0, 0, 0, 0} |
| 1364 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1365 | c = getopt_long(argc, argv, ":hf:F:T:pqsU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1366 | long_options, NULL); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1367 | if (c == -1) { |
| 1368 | break; |
| 1369 | } |
| 1370 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 1371 | case ':': |
| 1372 | missing_argument(argv[optind - 1]); |
| 1373 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1374 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 1375 | unrecognized_option(argv[optind - 1]); |
| 1376 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1377 | case 'h': |
| 1378 | help(); |
| 1379 | break; |
| 1380 | case 'f': |
| 1381 | fmt1 = optarg; |
| 1382 | break; |
| 1383 | case 'F': |
| 1384 | fmt2 = optarg; |
| 1385 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1386 | case 'T': |
| 1387 | cache = optarg; |
| 1388 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1389 | case 'p': |
| 1390 | progress = true; |
| 1391 | break; |
| 1392 | case 'q': |
| 1393 | quiet = true; |
| 1394 | break; |
| 1395 | case 's': |
| 1396 | strict = true; |
| 1397 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1398 | case 'U': |
| 1399 | force_share = true; |
| 1400 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 1401 | case OPTION_OBJECT: |
| 1402 | { |
| 1403 | Error *local_err = NULL; |
| 1404 | |
| 1405 | if (!user_creatable_add_from_str(optarg, &local_err)) { |
| 1406 | if (local_err) { |
| 1407 | error_report_err(local_err); |
| 1408 | exit(2); |
| 1409 | } else { |
| 1410 | /* Help was printed */ |
| 1411 | exit(EXIT_SUCCESS); |
| 1412 | } |
| 1413 | } |
| 1414 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1415 | } |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1416 | case OPTION_IMAGE_OPTS: |
| 1417 | image_opts = true; |
| 1418 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | /* Progress is not shown in Quiet mode */ |
| 1423 | if (quiet) { |
| 1424 | progress = false; |
| 1425 | } |
| 1426 | |
| 1427 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 1428 | if (optind != argc - 2) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 1429 | error_exit("Expecting two image file names"); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1430 | } |
| 1431 | filename1 = argv[optind++]; |
| 1432 | filename2 = argv[optind++]; |
| 1433 | |
Stefan Hajnoczi | cbda016 | 2014-08-26 19:17:55 +0100 | [diff] [blame] | 1434 | /* Initialize before goto out */ |
| 1435 | qemu_progress_init(progress, 2.0); |
| 1436 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1437 | flags = 0; |
| 1438 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1439 | if (ret < 0) { |
| 1440 | error_report("Invalid source cache option: %s", cache); |
| 1441 | ret = 2; |
| 1442 | goto out3; |
| 1443 | } |
| 1444 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1445 | blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet, |
| 1446 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1447 | if (!blk1) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1448 | ret = 2; |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1449 | goto out3; |
| 1450 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1451 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1452 | blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet, |
| 1453 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1454 | if (!blk2) { |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1455 | ret = 2; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1456 | goto out2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1457 | } |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1458 | bs1 = blk_bs(blk1); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1459 | bs2 = blk_bs(blk2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1460 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1461 | buf1 = blk_blockalign(blk1, IO_BUF_SIZE); |
| 1462 | buf2 = blk_blockalign(blk2, IO_BUF_SIZE); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1463 | total_size1 = blk_getlength(blk1); |
| 1464 | if (total_size1 < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1465 | error_report("Can't get size of %s: %s", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1466 | filename1, strerror(-total_size1)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1467 | ret = 4; |
| 1468 | goto out; |
| 1469 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1470 | total_size2 = blk_getlength(blk2); |
| 1471 | if (total_size2 < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1472 | error_report("Can't get size of %s: %s", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1473 | filename2, strerror(-total_size2)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1474 | ret = 4; |
| 1475 | goto out; |
| 1476 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1477 | total_size = MIN(total_size1, total_size2); |
| 1478 | progress_base = MAX(total_size1, total_size2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1479 | |
| 1480 | qemu_progress_print(0, 100); |
| 1481 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1482 | if (strict && total_size1 != total_size2) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1483 | ret = 1; |
| 1484 | qprintf(quiet, "Strict mode: Image size mismatch!\n"); |
| 1485 | goto out; |
| 1486 | } |
| 1487 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1488 | while (offset < total_size) { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1489 | int status1, status2; |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 1490 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1491 | status1 = bdrv_block_status_above(bs1, NULL, offset, |
| 1492 | total_size1 - offset, &pnum1, NULL, |
| 1493 | NULL); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1494 | if (status1 < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1495 | ret = 3; |
| 1496 | error_report("Sector allocation test failed for %s", filename1); |
| 1497 | goto out; |
| 1498 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1499 | allocated1 = status1 & BDRV_BLOCK_ALLOCATED; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1500 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1501 | status2 = bdrv_block_status_above(bs2, NULL, offset, |
| 1502 | total_size2 - offset, &pnum2, NULL, |
| 1503 | NULL); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1504 | if (status2 < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1505 | ret = 3; |
| 1506 | error_report("Sector allocation test failed for %s", filename2); |
| 1507 | goto out; |
| 1508 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1509 | allocated2 = status2 & BDRV_BLOCK_ALLOCATED; |
Eric Blake | 7daddc6 | 2017-10-11 22:47:09 -0500 | [diff] [blame] | 1510 | |
| 1511 | assert(pnum1 && pnum2); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1512 | chunk = MIN(pnum1, pnum2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1513 | |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1514 | if (strict) { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1515 | if (status1 != status2) { |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1516 | ret = 1; |
| 1517 | qprintf(quiet, "Strict mode: Offset %" PRId64 |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1518 | " block status mismatch!\n", offset); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1519 | goto out; |
| 1520 | } |
| 1521 | } |
| 1522 | if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) { |
Eric Blake | 7daddc6 | 2017-10-11 22:47:09 -0500 | [diff] [blame] | 1523 | /* nothing to do */ |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1524 | } else if (allocated1 == allocated2) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1525 | if (allocated1) { |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1526 | int64_t pnum; |
| 1527 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1528 | chunk = MIN(chunk, IO_BUF_SIZE); |
Alberto Faria | a9262f5 | 2022-07-05 17:15:11 +0100 | [diff] [blame] | 1529 | ret = blk_pread(blk1, offset, chunk, buf1, 0); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1530 | if (ret < 0) { |
| 1531 | error_report("Error while reading offset %" PRId64 |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1532 | " of %s: %s", |
| 1533 | offset, filename1, strerror(-ret)); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1534 | ret = 4; |
| 1535 | goto out; |
| 1536 | } |
Alberto Faria | a9262f5 | 2022-07-05 17:15:11 +0100 | [diff] [blame] | 1537 | ret = blk_pread(blk2, offset, chunk, buf2, 0); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1538 | if (ret < 0) { |
| 1539 | error_report("Error while reading offset %" PRId64 |
| 1540 | " of %s: %s", |
| 1541 | offset, filename2, strerror(-ret)); |
| 1542 | ret = 4; |
| 1543 | goto out; |
| 1544 | } |
| 1545 | ret = compare_buffers(buf1, buf2, chunk, &pnum); |
| 1546 | if (ret || pnum != chunk) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1547 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1548 | offset + (ret ? 0 : pnum)); |
Fam Zheng | 36452f1 | 2013-11-13 20:26:49 +0800 | [diff] [blame] | 1549 | ret = 1; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1550 | goto out; |
| 1551 | } |
| 1552 | } |
| 1553 | } else { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1554 | chunk = MIN(chunk, IO_BUF_SIZE); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1555 | if (allocated1) { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1556 | ret = check_empty_sectors(blk1, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1557 | filename1, buf1, quiet); |
| 1558 | } else { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1559 | ret = check_empty_sectors(blk2, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1560 | filename2, buf1, quiet); |
| 1561 | } |
| 1562 | if (ret) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1563 | goto out; |
| 1564 | } |
| 1565 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1566 | offset += chunk; |
| 1567 | qemu_progress_print(((float) chunk / progress_base) * 100, 100); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1568 | } |
| 1569 | |
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 | BlockBackend *blk_over; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1572 | const char *filename_over; |
| 1573 | |
| 1574 | qprintf(quiet, "Warning: Image size mismatch!\n"); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1575 | if (total_size1 > total_size2) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1576 | blk_over = blk1; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1577 | filename_over = filename1; |
| 1578 | } else { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1579 | blk_over = blk2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1580 | filename_over = filename2; |
| 1581 | } |
| 1582 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1583 | while (offset < progress_base) { |
| 1584 | ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset, |
| 1585 | progress_base - offset, &chunk, |
| 1586 | NULL, NULL); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1587 | if (ret < 0) { |
| 1588 | ret = 3; |
| 1589 | error_report("Sector allocation test failed for %s", |
| 1590 | filename_over); |
| 1591 | goto out; |
| 1592 | |
| 1593 | } |
Eric Blake | 391cb1a | 2017-10-11 22:47:10 -0500 | [diff] [blame] | 1594 | if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1595 | chunk = MIN(chunk, IO_BUF_SIZE); |
| 1596 | ret = check_empty_sectors(blk_over, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1597 | filename_over, buf1, quiet); |
| 1598 | if (ret) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1599 | goto out; |
| 1600 | } |
| 1601 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1602 | offset += chunk; |
| 1603 | qemu_progress_print(((float) chunk / progress_base) * 100, 100); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | qprintf(quiet, "Images are identical.\n"); |
| 1608 | ret = 0; |
| 1609 | |
| 1610 | out: |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1611 | qemu_vfree(buf1); |
| 1612 | qemu_vfree(buf2); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1613 | blk_unref(blk2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1614 | out2: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1615 | blk_unref(blk1); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1616 | out3: |
| 1617 | qemu_progress_end(); |
| 1618 | return ret; |
| 1619 | } |
| 1620 | |
Eric Blake | 6c729dd | 2020-05-21 14:21:35 -0500 | [diff] [blame] | 1621 | /* Convenience wrapper around qmp_block_dirty_bitmap_merge */ |
| 1622 | static void do_dirty_bitmap_merge(const char *dst_node, const char *dst_name, |
| 1623 | const char *src_node, const char *src_name, |
| 1624 | Error **errp) |
| 1625 | { |
Vladimir Sementsov-Ogievskiy | 1466ef6 | 2022-03-15 00:32:24 +0300 | [diff] [blame] | 1626 | BlockDirtyBitmapOrStr *merge_src; |
| 1627 | BlockDirtyBitmapOrStrList *list = NULL; |
Eric Blake | 6c729dd | 2020-05-21 14:21:35 -0500 | [diff] [blame] | 1628 | |
Vladimir Sementsov-Ogievskiy | 1466ef6 | 2022-03-15 00:32:24 +0300 | [diff] [blame] | 1629 | merge_src = g_new0(BlockDirtyBitmapOrStr, 1); |
Eric Blake | 6c729dd | 2020-05-21 14:21:35 -0500 | [diff] [blame] | 1630 | merge_src->type = QTYPE_QDICT; |
| 1631 | merge_src->u.external.node = g_strdup(src_node); |
| 1632 | merge_src->u.external.name = g_strdup(src_name); |
Eric Blake | 54aa3de | 2020-11-12 19:13:37 -0600 | [diff] [blame] | 1633 | QAPI_LIST_PREPEND(list, merge_src); |
Eric Blake | 6c729dd | 2020-05-21 14:21:35 -0500 | [diff] [blame] | 1634 | qmp_block_dirty_bitmap_merge(dst_node, dst_name, list, errp); |
Vladimir Sementsov-Ogievskiy | 1466ef6 | 2022-03-15 00:32:24 +0300 | [diff] [blame] | 1635 | qapi_free_BlockDirtyBitmapOrStrList(list); |
Eric Blake | 6c729dd | 2020-05-21 14:21:35 -0500 | [diff] [blame] | 1636 | } |
| 1637 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1638 | enum ImgConvertBlockStatus { |
| 1639 | BLK_DATA, |
| 1640 | BLK_ZERO, |
| 1641 | BLK_BACKING_FILE, |
| 1642 | }; |
| 1643 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1644 | #define MAX_COROUTINES 16 |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 1645 | #define CONVERT_THROTTLE_GROUP "img_convert" |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1646 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1647 | typedef struct ImgConvertState { |
| 1648 | BlockBackend **src; |
| 1649 | int64_t *src_sectors; |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 1650 | int *src_alignment; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1651 | int src_num; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1652 | int64_t total_sectors; |
| 1653 | int64_t allocated_sectors; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1654 | int64_t allocated_done; |
| 1655 | int64_t sector_num; |
| 1656 | int64_t wr_offs; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1657 | enum ImgConvertBlockStatus status; |
| 1658 | int64_t sector_next_status; |
| 1659 | BlockBackend *target; |
| 1660 | bool has_zero_init; |
| 1661 | bool compressed; |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 1662 | bool target_is_new; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1663 | bool target_has_backing; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1664 | int64_t target_backing_sectors; /* negative if unknown */ |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1665 | bool wr_in_order; |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1666 | bool copy_range; |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1667 | bool salvage; |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 1668 | bool quiet; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1669 | int min_sparse; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1670 | int alignment; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1671 | size_t cluster_sectors; |
| 1672 | size_t buf_sectors; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 1673 | long num_coroutines; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1674 | int running_coroutines; |
| 1675 | Coroutine *co[MAX_COROUTINES]; |
| 1676 | int64_t wait_sector_num[MAX_COROUTINES]; |
| 1677 | CoMutex lock; |
| 1678 | int ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1679 | } ImgConvertState; |
| 1680 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1681 | static void convert_select_part(ImgConvertState *s, int64_t sector_num, |
| 1682 | int *src_cur, int64_t *src_cur_offset) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1683 | { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1684 | *src_cur = 0; |
| 1685 | *src_cur_offset = 0; |
| 1686 | while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) { |
| 1687 | *src_cur_offset += s->src_sectors[*src_cur]; |
| 1688 | (*src_cur)++; |
| 1689 | assert(*src_cur < s->src_num); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1690 | } |
| 1691 | } |
| 1692 | |
| 1693 | static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num) |
| 1694 | { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1695 | int64_t src_cur_offset; |
| 1696 | int ret, n, src_cur; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1697 | bool post_backing_zero = false; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1698 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1699 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1700 | |
| 1701 | assert(s->total_sectors > sector_num); |
| 1702 | n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS); |
| 1703 | |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1704 | if (s->target_backing_sectors >= 0) { |
| 1705 | if (sector_num >= s->target_backing_sectors) { |
Vladimir Sementsov-Ogievskiy | 2253d86 | 2020-05-28 12:43:56 +0300 | [diff] [blame] | 1706 | post_backing_zero = true; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1707 | } else if (sector_num + n > s->target_backing_sectors) { |
| 1708 | /* Split requests around target_backing_sectors (because |
| 1709 | * starting from there, zeros are handled differently) */ |
| 1710 | n = s->target_backing_sectors - sector_num; |
| 1711 | } |
| 1712 | } |
| 1713 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1714 | if (s->sector_next_status <= sector_num) { |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1715 | uint64_t offset = (sector_num - src_cur_offset) * BDRV_SECTOR_SIZE; |
| 1716 | int64_t count; |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 1717 | int tail; |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 1718 | BlockDriverState *src_bs = blk_bs(s->src[src_cur]); |
| 1719 | BlockDriverState *base; |
| 1720 | |
| 1721 | if (s->target_has_backing) { |
| 1722 | base = bdrv_cow_bs(bdrv_skip_filters(src_bs)); |
| 1723 | } else { |
| 1724 | base = NULL; |
| 1725 | } |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1726 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1727 | do { |
| 1728 | count = n * BDRV_SECTOR_SIZE; |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 1729 | |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 1730 | ret = bdrv_block_status_above(src_bs, base, offset, count, &count, |
| 1731 | NULL, NULL); |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1732 | |
| 1733 | if (ret < 0) { |
| 1734 | if (s->salvage) { |
| 1735 | if (n == 1) { |
| 1736 | if (!s->quiet) { |
| 1737 | warn_report("error while reading block status at " |
| 1738 | "offset %" PRIu64 ": %s", offset, |
| 1739 | strerror(-ret)); |
| 1740 | } |
| 1741 | /* Just try to read the data, then */ |
| 1742 | ret = BDRV_BLOCK_DATA; |
| 1743 | count = BDRV_SECTOR_SIZE; |
| 1744 | } else { |
| 1745 | /* Retry on a shorter range */ |
| 1746 | n = DIV_ROUND_UP(n, 4); |
| 1747 | } |
| 1748 | } else { |
| 1749 | error_report("error while reading block status at offset " |
| 1750 | "%" PRIu64 ": %s", offset, strerror(-ret)); |
| 1751 | return ret; |
| 1752 | } |
| 1753 | } |
| 1754 | } while (ret < 0); |
| 1755 | |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1756 | n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1757 | |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 1758 | /* |
| 1759 | * Avoid that s->sector_next_status becomes unaligned to the source |
| 1760 | * request alignment and/or cluster size to avoid unnecessary read |
| 1761 | * cycles. |
| 1762 | */ |
| 1763 | tail = (sector_num - src_cur_offset + n) % s->src_alignment[src_cur]; |
| 1764 | if (n > tail) { |
| 1765 | n -= tail; |
| 1766 | } |
| 1767 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1768 | if (ret & BDRV_BLOCK_ZERO) { |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1769 | s->status = post_backing_zero ? BLK_BACKING_FILE : BLK_ZERO; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1770 | } else if (ret & BDRV_BLOCK_DATA) { |
| 1771 | s->status = BLK_DATA; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1772 | } else { |
Vladimir Sementsov-Ogievskiy | 9f1b92a | 2017-04-07 14:34:04 +0300 | [diff] [blame] | 1773 | s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | s->sector_next_status = sector_num + n; |
| 1777 | } |
| 1778 | |
| 1779 | n = MIN(n, s->sector_next_status - sector_num); |
| 1780 | if (s->status == BLK_DATA) { |
| 1781 | n = MIN(n, s->buf_sectors); |
| 1782 | } |
| 1783 | |
| 1784 | /* We need to write complete clusters for compressed images, so if an |
| 1785 | * unallocated area is shorter than that, we must consider the whole |
| 1786 | * cluster allocated. */ |
| 1787 | if (s->compressed) { |
| 1788 | if (n < s->cluster_sectors) { |
| 1789 | n = MIN(s->cluster_sectors, s->total_sectors - sector_num); |
| 1790 | s->status = BLK_DATA; |
| 1791 | } else { |
| 1792 | n = QEMU_ALIGN_DOWN(n, s->cluster_sectors); |
| 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | return n; |
| 1797 | } |
| 1798 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1799 | static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num, |
| 1800 | int nb_sectors, uint8_t *buf) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1801 | { |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1802 | uint64_t single_read_until = 0; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1803 | int n, ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1804 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1805 | assert(nb_sectors <= s->buf_sectors); |
| 1806 | while (nb_sectors > 0) { |
| 1807 | BlockBackend *blk; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1808 | int src_cur; |
| 1809 | int64_t bs_sectors, src_cur_offset; |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1810 | uint64_t offset; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1811 | |
| 1812 | /* In the case of compression with multiple source files, we can get a |
| 1813 | * nb_sectors that spreads into the next part. So we must be able to |
| 1814 | * read across multiple BDSes for one convert_read() call. */ |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1815 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
| 1816 | blk = s->src[src_cur]; |
| 1817 | bs_sectors = s->src_sectors[src_cur]; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1818 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1819 | offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1820 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1821 | n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset)); |
| 1822 | if (single_read_until > offset) { |
| 1823 | n = 1; |
| 1824 | } |
| 1825 | |
| 1826 | ret = blk_co_pread(blk, offset, n << BDRV_SECTOR_BITS, buf, 0); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1827 | if (ret < 0) { |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1828 | if (s->salvage) { |
| 1829 | if (n > 1) { |
| 1830 | single_read_until = offset + (n << BDRV_SECTOR_BITS); |
| 1831 | continue; |
| 1832 | } else { |
| 1833 | if (!s->quiet) { |
| 1834 | warn_report("error while reading offset %" PRIu64 |
| 1835 | ": %s", offset, strerror(-ret)); |
| 1836 | } |
| 1837 | memset(buf, 0, BDRV_SECTOR_SIZE); |
| 1838 | } |
| 1839 | } else { |
| 1840 | return ret; |
| 1841 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | sector_num += n; |
| 1845 | nb_sectors -= n; |
| 1846 | buf += n * BDRV_SECTOR_SIZE; |
| 1847 | } |
| 1848 | |
| 1849 | return 0; |
| 1850 | } |
| 1851 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1852 | |
| 1853 | static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num, |
| 1854 | int nb_sectors, uint8_t *buf, |
| 1855 | enum ImgConvertBlockStatus status) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1856 | { |
| 1857 | int ret; |
| 1858 | |
| 1859 | while (nb_sectors > 0) { |
| 1860 | int n = nb_sectors; |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1861 | BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0; |
| 1862 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1863 | switch (status) { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1864 | case BLK_BACKING_FILE: |
| 1865 | /* If we have a backing file, leave clusters unallocated that are |
| 1866 | * unallocated in the source image, so that the backing file is |
| 1867 | * visible at the respective offset. */ |
| 1868 | assert(s->target_has_backing); |
| 1869 | break; |
| 1870 | |
| 1871 | case BLK_DATA: |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1872 | /* If we're told to keep the target fully allocated (-S 0) or there |
| 1873 | * is real non-zero data, we must write it. Otherwise we can treat |
| 1874 | * it as zero sectors. |
| 1875 | * Compressed clusters need to be written as a whole, so in that |
| 1876 | * case we can only save the write if the buffer is completely |
| 1877 | * zeroed. */ |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1878 | if (!s->min_sparse || |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1879 | (!s->compressed && |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1880 | is_allocated_sectors_min(buf, n, &n, s->min_sparse, |
| 1881 | sector_num, s->alignment)) || |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1882 | (s->compressed && |
| 1883 | !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1884 | { |
Vladimir Sementsov-Ogievskiy | 265a7e5 | 2019-04-22 17:58:38 +0300 | [diff] [blame] | 1885 | ret = blk_co_pwrite(s->target, sector_num << BDRV_SECTOR_BITS, |
| 1886 | n << BDRV_SECTOR_BITS, buf, flags); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1887 | if (ret < 0) { |
| 1888 | return ret; |
| 1889 | } |
| 1890 | break; |
| 1891 | } |
| 1892 | /* fall-through */ |
| 1893 | |
| 1894 | case BLK_ZERO: |
| 1895 | if (s->has_zero_init) { |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1896 | assert(!s->target_has_backing); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1897 | break; |
| 1898 | } |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1899 | ret = blk_co_pwrite_zeroes(s->target, |
| 1900 | sector_num << BDRV_SECTOR_BITS, |
Nir Soffer | a3d6ae2 | 2019-03-24 02:20:12 +0200 | [diff] [blame] | 1901 | n << BDRV_SECTOR_BITS, |
| 1902 | BDRV_REQ_MAY_UNMAP); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1903 | if (ret < 0) { |
| 1904 | return ret; |
| 1905 | } |
| 1906 | break; |
| 1907 | } |
| 1908 | |
| 1909 | sector_num += n; |
| 1910 | nb_sectors -= n; |
| 1911 | buf += n * BDRV_SECTOR_SIZE; |
| 1912 | } |
| 1913 | |
| 1914 | return 0; |
| 1915 | } |
| 1916 | |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1917 | static int coroutine_fn convert_co_copy_range(ImgConvertState *s, int64_t sector_num, |
| 1918 | int nb_sectors) |
| 1919 | { |
| 1920 | int n, ret; |
| 1921 | |
| 1922 | while (nb_sectors > 0) { |
| 1923 | BlockBackend *blk; |
| 1924 | int src_cur; |
| 1925 | int64_t bs_sectors, src_cur_offset; |
| 1926 | int64_t offset; |
| 1927 | |
| 1928 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
| 1929 | offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS; |
| 1930 | blk = s->src[src_cur]; |
| 1931 | bs_sectors = s->src_sectors[src_cur]; |
| 1932 | |
| 1933 | n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset)); |
| 1934 | |
| 1935 | ret = blk_co_copy_range(blk, offset, s->target, |
| 1936 | sector_num << BDRV_SECTOR_BITS, |
Vladimir Sementsov-Ogievskiy | 67b51fb | 2018-07-09 19:37:17 +0300 | [diff] [blame] | 1937 | n << BDRV_SECTOR_BITS, 0, 0); |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1938 | if (ret < 0) { |
| 1939 | return ret; |
| 1940 | } |
| 1941 | |
| 1942 | sector_num += n; |
| 1943 | nb_sectors -= n; |
| 1944 | } |
| 1945 | return 0; |
| 1946 | } |
| 1947 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1948 | static void coroutine_fn convert_co_do_copy(void *opaque) |
| 1949 | { |
| 1950 | ImgConvertState *s = opaque; |
| 1951 | uint8_t *buf = NULL; |
| 1952 | int ret, i; |
| 1953 | int index = -1; |
| 1954 | |
| 1955 | for (i = 0; i < s->num_coroutines; i++) { |
| 1956 | if (s->co[i] == qemu_coroutine_self()) { |
| 1957 | index = i; |
| 1958 | break; |
| 1959 | } |
| 1960 | } |
| 1961 | assert(index >= 0); |
| 1962 | |
| 1963 | s->running_coroutines++; |
| 1964 | buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE); |
| 1965 | |
| 1966 | while (1) { |
| 1967 | int n; |
| 1968 | int64_t sector_num; |
| 1969 | enum ImgConvertBlockStatus status; |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1970 | bool copy_range; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1971 | |
| 1972 | qemu_co_mutex_lock(&s->lock); |
| 1973 | if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) { |
| 1974 | qemu_co_mutex_unlock(&s->lock); |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1975 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1976 | } |
| 1977 | n = convert_iteration_sectors(s, s->sector_num); |
| 1978 | if (n < 0) { |
| 1979 | qemu_co_mutex_unlock(&s->lock); |
| 1980 | s->ret = n; |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1981 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1982 | } |
| 1983 | /* save current sector and allocation status to local variables */ |
| 1984 | sector_num = s->sector_num; |
| 1985 | status = s->status; |
| 1986 | if (!s->min_sparse && s->status == BLK_ZERO) { |
| 1987 | n = MIN(n, s->buf_sectors); |
| 1988 | } |
| 1989 | /* increment global sector counter so that other coroutines can |
| 1990 | * already continue reading beyond this request */ |
| 1991 | s->sector_num += n; |
| 1992 | qemu_co_mutex_unlock(&s->lock); |
| 1993 | |
| 1994 | if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) { |
| 1995 | s->allocated_done += n; |
| 1996 | qemu_progress_print(100.0 * s->allocated_done / |
| 1997 | s->allocated_sectors, 0); |
| 1998 | } |
| 1999 | |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 2000 | retry: |
| 2001 | copy_range = s->copy_range && s->status == BLK_DATA; |
| 2002 | if (status == BLK_DATA && !copy_range) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2003 | ret = convert_co_read(s, sector_num, n, buf); |
| 2004 | if (ret < 0) { |
Eric Blake | 39f77cb | 2020-04-02 08:57:17 -0500 | [diff] [blame] | 2005 | error_report("error while reading at byte %lld: %s", |
| 2006 | sector_num * BDRV_SECTOR_SIZE, strerror(-ret)); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2007 | s->ret = ret; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2008 | } |
| 2009 | } else if (!s->min_sparse && status == BLK_ZERO) { |
| 2010 | status = BLK_DATA; |
| 2011 | memset(buf, 0x00, n * BDRV_SECTOR_SIZE); |
| 2012 | } |
| 2013 | |
| 2014 | if (s->wr_in_order) { |
| 2015 | /* keep writes in order */ |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2016 | while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2017 | s->wait_sector_num[index] = sector_num; |
| 2018 | qemu_coroutine_yield(); |
| 2019 | } |
| 2020 | s->wait_sector_num[index] = -1; |
| 2021 | } |
| 2022 | |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2023 | if (s->ret == -EINPROGRESS) { |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 2024 | if (copy_range) { |
| 2025 | ret = convert_co_copy_range(s, sector_num, n); |
| 2026 | if (ret) { |
| 2027 | s->copy_range = false; |
| 2028 | goto retry; |
| 2029 | } |
| 2030 | } else { |
| 2031 | ret = convert_co_write(s, sector_num, n, buf, status); |
| 2032 | } |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2033 | if (ret < 0) { |
Eric Blake | 39f77cb | 2020-04-02 08:57:17 -0500 | [diff] [blame] | 2034 | error_report("error while writing at byte %lld: %s", |
| 2035 | sector_num * BDRV_SECTOR_SIZE, strerror(-ret)); |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2036 | s->ret = ret; |
| 2037 | } |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2038 | } |
| 2039 | |
| 2040 | if (s->wr_in_order) { |
| 2041 | /* reenter the coroutine that might have waited |
| 2042 | * for this write to complete */ |
| 2043 | s->wr_offs = sector_num + n; |
| 2044 | for (i = 0; i < s->num_coroutines; i++) { |
| 2045 | if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) { |
| 2046 | /* |
| 2047 | * A -> B -> A cannot occur because A has |
| 2048 | * s->wait_sector_num[i] == -1 during A -> B. Therefore |
| 2049 | * B will never enter A during this time window. |
| 2050 | */ |
| 2051 | qemu_coroutine_enter(s->co[i]); |
| 2052 | break; |
| 2053 | } |
| 2054 | } |
| 2055 | } |
| 2056 | } |
| 2057 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2058 | qemu_vfree(buf); |
| 2059 | s->co[index] = NULL; |
| 2060 | s->running_coroutines--; |
| 2061 | if (!s->running_coroutines && s->ret == -EINPROGRESS) { |
| 2062 | /* the convert job finished successfully */ |
| 2063 | s->ret = 0; |
| 2064 | } |
| 2065 | } |
| 2066 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2067 | static int convert_do_copy(ImgConvertState *s) |
| 2068 | { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2069 | int ret, i, n; |
| 2070 | int64_t sector_num = 0; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2071 | |
| 2072 | /* Check whether we have zero initialisation or can get it efficiently */ |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2073 | if (!s->has_zero_init && s->target_is_new && s->min_sparse && |
| 2074 | !s->target_has_backing) { |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 2075 | s->has_zero_init = bdrv_has_zero_init(blk_bs(s->target)); |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 2076 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2077 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2078 | /* Allocate buffer for copied data. For compressed images, only one cluster |
| 2079 | * can be copied at a time. */ |
| 2080 | if (s->compressed) { |
| 2081 | if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) { |
| 2082 | error_report("invalid cluster size"); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2083 | return -EINVAL; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2084 | } |
| 2085 | s->buf_sectors = s->cluster_sectors; |
| 2086 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2087 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2088 | while (sector_num < s->total_sectors) { |
| 2089 | n = convert_iteration_sectors(s, sector_num); |
| 2090 | if (n < 0) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2091 | return n; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2092 | } |
Max Reitz | aad15de | 2016-03-24 23:33:57 +0100 | [diff] [blame] | 2093 | if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO)) |
| 2094 | { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2095 | s->allocated_sectors += n; |
| 2096 | } |
| 2097 | sector_num += n; |
| 2098 | } |
| 2099 | |
| 2100 | /* Do the copy */ |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2101 | s->sector_next_status = 0; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2102 | s->ret = -EINPROGRESS; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2103 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2104 | qemu_co_mutex_init(&s->lock); |
| 2105 | for (i = 0; i < s->num_coroutines; i++) { |
| 2106 | s->co[i] = qemu_coroutine_create(convert_co_do_copy, s); |
| 2107 | s->wait_sector_num[i] = -1; |
| 2108 | qemu_coroutine_enter(s->co[i]); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2109 | } |
| 2110 | |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2111 | while (s->running_coroutines) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2112 | main_loop_wait(false); |
| 2113 | } |
| 2114 | |
| 2115 | if (s->compressed && !s->ret) { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2116 | /* signal EOF to align */ |
Alberto Faria | 0cadf2c | 2022-07-05 17:15:18 +0100 | [diff] [blame] | 2117 | ret = blk_pwrite_compressed(s->target, 0, 0, NULL); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2118 | if (ret < 0) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2119 | return ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2120 | } |
| 2121 | } |
| 2122 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2123 | return s->ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2124 | } |
| 2125 | |
Eric Blake | 74a4320 | 2021-07-09 10:39:50 -0500 | [diff] [blame] | 2126 | /* Check that bitmaps can be copied, or output an error */ |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2127 | static int convert_check_bitmaps(BlockDriverState *src, bool skip_broken) |
Eric Blake | 74a4320 | 2021-07-09 10:39:50 -0500 | [diff] [blame] | 2128 | { |
| 2129 | BdrvDirtyBitmap *bm; |
| 2130 | |
| 2131 | if (!bdrv_supports_persistent_dirty_bitmap(src)) { |
| 2132 | error_report("Source lacks bitmap support"); |
| 2133 | return -1; |
| 2134 | } |
| 2135 | FOR_EACH_DIRTY_BITMAP(src, bm) { |
| 2136 | if (!bdrv_dirty_bitmap_get_persistence(bm)) { |
| 2137 | continue; |
| 2138 | } |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2139 | if (!skip_broken && bdrv_dirty_bitmap_inconsistent(bm)) { |
Eric Blake | 74a4320 | 2021-07-09 10:39:50 -0500 | [diff] [blame] | 2140 | error_report("Cannot copy inconsistent bitmap '%s'", |
| 2141 | bdrv_dirty_bitmap_name(bm)); |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2142 | error_printf("Try --skip-broken-bitmaps, or " |
| 2143 | "use 'qemu-img bitmap --remove' to delete it\n"); |
Eric Blake | 74a4320 | 2021-07-09 10:39:50 -0500 | [diff] [blame] | 2144 | return -1; |
| 2145 | } |
| 2146 | } |
| 2147 | return 0; |
| 2148 | } |
| 2149 | |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2150 | static int convert_copy_bitmaps(BlockDriverState *src, BlockDriverState *dst, |
| 2151 | bool skip_broken) |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2152 | { |
| 2153 | BdrvDirtyBitmap *bm; |
| 2154 | Error *err = NULL; |
| 2155 | |
| 2156 | FOR_EACH_DIRTY_BITMAP(src, bm) { |
| 2157 | const char *name; |
| 2158 | |
| 2159 | if (!bdrv_dirty_bitmap_get_persistence(bm)) { |
| 2160 | continue; |
| 2161 | } |
| 2162 | name = bdrv_dirty_bitmap_name(bm); |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2163 | if (skip_broken && bdrv_dirty_bitmap_inconsistent(bm)) { |
| 2164 | warn_report("Skipping inconsistent bitmap '%s'", name); |
| 2165 | continue; |
| 2166 | } |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2167 | qmp_block_dirty_bitmap_add(dst->node_name, name, |
| 2168 | true, bdrv_dirty_bitmap_granularity(bm), |
| 2169 | true, true, |
| 2170 | true, !bdrv_dirty_bitmap_enabled(bm), |
| 2171 | &err); |
| 2172 | if (err) { |
| 2173 | error_reportf_err(err, "Failed to create bitmap %s: ", name); |
| 2174 | return -1; |
| 2175 | } |
| 2176 | |
| 2177 | do_dirty_bitmap_merge(dst->node_name, name, src->node_name, name, |
| 2178 | &err); |
| 2179 | if (err) { |
| 2180 | error_reportf_err(err, "Failed to populate bitmap %s: ", name); |
Eric Blake | 74a4320 | 2021-07-09 10:39:50 -0500 | [diff] [blame] | 2181 | qmp_block_dirty_bitmap_remove(dst->node_name, name, NULL); |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2182 | return -1; |
| 2183 | } |
| 2184 | } |
| 2185 | |
| 2186 | return 0; |
| 2187 | } |
| 2188 | |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 2189 | #define MAX_BUF_SECTORS 32768 |
| 2190 | |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 2191 | static void set_rate_limit(BlockBackend *blk, int64_t rate_limit) |
| 2192 | { |
| 2193 | ThrottleConfig cfg; |
| 2194 | |
| 2195 | throttle_config_init(&cfg); |
| 2196 | cfg.buckets[THROTTLE_BPS_WRITE].avg = rate_limit; |
| 2197 | |
| 2198 | blk_io_limits_enable(blk, CONVERT_THROTTLE_GROUP); |
| 2199 | blk_set_io_limits(blk, &cfg); |
| 2200 | } |
| 2201 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2202 | static int img_convert(int argc, char **argv) |
| 2203 | { |
Kevin Wolf | 0b8fb55 | 2021-04-22 18:43:44 +0200 | [diff] [blame] | 2204 | int c, bs_i, flags, src_flags = BDRV_O_NO_SHARE; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2205 | const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe", |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2206 | *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL, |
Eric Blake | 1899bf4 | 2021-09-13 08:17:35 -0500 | [diff] [blame] | 2207 | *out_filename, *out_baseimg_param, *snapshot_name = NULL, |
| 2208 | *backing_fmt = NULL; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2209 | BlockDriver *drv = NULL, *proto_drv = NULL; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2210 | BlockDriverInfo bdi; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2211 | BlockDriverState *out_bs; |
| 2212 | QemuOpts *opts = NULL, *sn_opts = NULL; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2213 | QemuOptsList *create_opts = NULL; |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2214 | QDict *open_opts = NULL; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2215 | char *options = NULL; |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 2216 | Error *local_err = NULL; |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2217 | bool writethrough, src_writethrough, image_opts = false, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2218 | skip_create = false, progress = false, tgt_image_opts = false; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2219 | int64_t ret = -EINVAL; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2220 | bool force_share = false; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2221 | bool explict_min_sparse = false; |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2222 | bool bitmaps = false; |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2223 | bool skip_broken = false; |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 2224 | int64_t rate_limit = 0; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2225 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2226 | ImgConvertState s = (ImgConvertState) { |
| 2227 | /* Need at least 4k of zeros for sparse detection */ |
| 2228 | .min_sparse = 8, |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2229 | .copy_range = false, |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2230 | .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE, |
| 2231 | .wr_in_order = true, |
| 2232 | .num_coroutines = 8, |
| 2233 | }; |
| 2234 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2235 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2236 | static const struct option long_options[] = { |
| 2237 | {"help", no_argument, 0, 'h'}, |
| 2238 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2239 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2240 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2241 | {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS}, |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 2242 | {"salvage", no_argument, 0, OPTION_SALVAGE}, |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2243 | {"target-is-zero", no_argument, 0, OPTION_TARGET_IS_ZERO}, |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2244 | {"bitmaps", no_argument, 0, OPTION_BITMAPS}, |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2245 | {"skip-broken-bitmaps", no_argument, 0, OPTION_SKIP_BROKEN}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2246 | {0, 0, 0, 0} |
| 2247 | }; |
Eric Blake | 1899bf4 | 2021-09-13 08:17:35 -0500 | [diff] [blame] | 2248 | 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] | 2249 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2250 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2251 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2252 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2253 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2254 | case ':': |
| 2255 | missing_argument(argv[optind - 1]); |
| 2256 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2257 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2258 | unrecognized_option(argv[optind - 1]); |
| 2259 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2260 | case 'h': |
| 2261 | help(); |
| 2262 | break; |
| 2263 | case 'f': |
| 2264 | fmt = optarg; |
| 2265 | break; |
| 2266 | case 'O': |
| 2267 | out_fmt = optarg; |
| 2268 | break; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 2269 | case 'B': |
| 2270 | out_baseimg = optarg; |
| 2271 | break; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2272 | case 'C': |
| 2273 | s.copy_range = true; |
| 2274 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2275 | case 'c': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2276 | s.compressed = true; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2277 | break; |
Eric Blake | 1899bf4 | 2021-09-13 08:17:35 -0500 | [diff] [blame] | 2278 | case 'F': |
| 2279 | backing_fmt = optarg; |
| 2280 | break; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2281 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 2282 | if (accumulate_options(&options, optarg) < 0) { |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2283 | goto fail_getopt; |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 2284 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2285 | break; |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2286 | case 'l': |
| 2287 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { |
Markus Armbruster | 70b9433 | 2015-02-13 12:50:26 +0100 | [diff] [blame] | 2288 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
| 2289 | optarg, false); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2290 | if (!sn_opts) { |
| 2291 | error_report("Failed in parsing snapshot param '%s'", |
| 2292 | optarg); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2293 | goto fail_getopt; |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2294 | } |
| 2295 | } else { |
| 2296 | snapshot_name = optarg; |
| 2297 | } |
| 2298 | break; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2299 | case 'S': |
| 2300 | { |
| 2301 | int64_t sval; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 2302 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 2303 | sval = cvtnum("buffer size for sparse output", optarg); |
| 2304 | if (sval < 0) { |
| 2305 | goto fail_getopt; |
| 2306 | } else if (!QEMU_IS_ALIGNED(sval, BDRV_SECTOR_SIZE) || |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 2307 | sval / BDRV_SECTOR_SIZE > MAX_BUF_SECTORS) { |
| 2308 | error_report("Invalid buffer size for sparse output specified. " |
| 2309 | "Valid sizes are multiples of %llu up to %llu. Select " |
| 2310 | "0 to disable sparse detection (fully allocates output).", |
| 2311 | BDRV_SECTOR_SIZE, MAX_BUF_SECTORS * BDRV_SECTOR_SIZE); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2312 | goto fail_getopt; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2313 | } |
| 2314 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2315 | s.min_sparse = sval / BDRV_SECTOR_SIZE; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2316 | explict_min_sparse = true; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2317 | break; |
| 2318 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2319 | case 'p': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2320 | progress = true; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2321 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2322 | case 't': |
| 2323 | cache = optarg; |
| 2324 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 2325 | case 'T': |
| 2326 | src_cache = optarg; |
| 2327 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2328 | case 'q': |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2329 | s.quiet = true; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2330 | break; |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2331 | case 'n': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2332 | skip_create = true; |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2333 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2334 | case 'm': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2335 | if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) || |
| 2336 | s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2337 | error_report("Invalid number of coroutines. Allowed number of" |
| 2338 | " coroutines is between 1 and %d", MAX_COROUTINES); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2339 | goto fail_getopt; |
| 2340 | } |
| 2341 | break; |
| 2342 | case 'W': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2343 | s.wr_in_order = false; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2344 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2345 | case 'U': |
| 2346 | force_share = true; |
| 2347 | break; |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 2348 | case 'r': |
| 2349 | rate_limit = cvtnum("rate limit", optarg); |
| 2350 | if (rate_limit < 0) { |
| 2351 | goto fail_getopt; |
| 2352 | } |
| 2353 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 2354 | case OPTION_OBJECT: |
| 2355 | user_creatable_process_cmdline(optarg); |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2356 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2357 | case OPTION_IMAGE_OPTS: |
| 2358 | image_opts = true; |
| 2359 | break; |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 2360 | case OPTION_SALVAGE: |
| 2361 | s.salvage = true; |
| 2362 | break; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2363 | case OPTION_TARGET_IMAGE_OPTS: |
| 2364 | tgt_image_opts = true; |
| 2365 | break; |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2366 | case OPTION_TARGET_IS_ZERO: |
| 2367 | /* |
| 2368 | * The user asserting that the target is blank has the |
| 2369 | * same effect as the target driver supporting zero |
| 2370 | * initialisation. |
| 2371 | */ |
| 2372 | s.has_zero_init = true; |
| 2373 | break; |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2374 | case OPTION_BITMAPS: |
| 2375 | bitmaps = true; |
| 2376 | break; |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2377 | case OPTION_SKIP_BROKEN: |
| 2378 | skip_broken = true; |
| 2379 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2380 | } |
| 2381 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2382 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2383 | if (!out_fmt && !tgt_image_opts) { |
| 2384 | out_fmt = "raw"; |
| 2385 | } |
| 2386 | |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2387 | if (skip_broken && !bitmaps) { |
| 2388 | error_report("Use of --skip-broken-bitmaps requires --bitmaps"); |
| 2389 | goto fail_getopt; |
| 2390 | } |
| 2391 | |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2392 | if (s.compressed && s.copy_range) { |
| 2393 | error_report("Cannot enable copy offloading when -c is used"); |
| 2394 | goto fail_getopt; |
| 2395 | } |
| 2396 | |
| 2397 | if (explict_min_sparse && s.copy_range) { |
| 2398 | error_report("Cannot enable copy offloading when -S is used"); |
| 2399 | goto fail_getopt; |
| 2400 | } |
| 2401 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 2402 | if (s.copy_range && s.salvage) { |
| 2403 | error_report("Cannot use copy offloading in salvaging mode"); |
| 2404 | goto fail_getopt; |
| 2405 | } |
| 2406 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2407 | if (tgt_image_opts && !skip_create) { |
| 2408 | error_report("--target-image-opts requires use of -n flag"); |
| 2409 | goto fail_getopt; |
| 2410 | } |
| 2411 | |
Kevin Wolf | ffd8e8f | 2019-08-09 11:09:21 +0200 | [diff] [blame] | 2412 | if (skip_create && options) { |
Eric Blake | 25956af | 2020-07-06 15:39:46 -0500 | [diff] [blame] | 2413 | error_report("-o has no effect when skipping image creation"); |
| 2414 | goto fail_getopt; |
Kevin Wolf | ffd8e8f | 2019-08-09 11:09:21 +0200 | [diff] [blame] | 2415 | } |
| 2416 | |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2417 | if (s.has_zero_init && !skip_create) { |
| 2418 | error_report("--target-is-zero requires use of -n flag"); |
| 2419 | goto fail_getopt; |
| 2420 | } |
| 2421 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2422 | s.src_num = argc - optind - 1; |
| 2423 | out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL; |
| 2424 | |
| 2425 | if (options && has_help_option(options)) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2426 | if (out_fmt) { |
| 2427 | ret = print_block_option_help(out_filename, out_fmt); |
| 2428 | goto fail_getopt; |
| 2429 | } else { |
| 2430 | error_report("Option help requires a format be specified"); |
| 2431 | goto fail_getopt; |
| 2432 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2433 | } |
| 2434 | |
| 2435 | if (s.src_num < 1) { |
| 2436 | error_report("Must specify image file name"); |
| 2437 | goto fail_getopt; |
| 2438 | } |
| 2439 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2440 | /* ret is still -EINVAL until here */ |
| 2441 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); |
| 2442 | if (ret < 0) { |
| 2443 | error_report("Invalid source cache option: %s", src_cache); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2444 | goto fail_getopt; |
| 2445 | } |
| 2446 | |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2447 | /* Initialize before goto out */ |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2448 | if (s.quiet) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2449 | progress = false; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2450 | } |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2451 | qemu_progress_init(progress, 1.0); |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2452 | qemu_progress_print(0, 100); |
| 2453 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2454 | s.src = g_new0(BlockBackend *, s.src_num); |
| 2455 | s.src_sectors = g_new(int64_t, s.src_num); |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 2456 | s.src_alignment = g_new(int, s.src_num); |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 2457 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2458 | for (bs_i = 0; bs_i < s.src_num; bs_i++) { |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 2459 | BlockDriverState *src_bs; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2460 | s.src[bs_i] = img_open(image_opts, argv[optind + bs_i], |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2461 | fmt, src_flags, src_writethrough, s.quiet, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2462 | force_share); |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2463 | if (!s.src[bs_i]) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2464 | ret = -1; |
| 2465 | goto out; |
| 2466 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2467 | s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]); |
| 2468 | if (s.src_sectors[bs_i] < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2469 | error_report("Could not get size of %s: %s", |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2470 | argv[optind + bs_i], strerror(-s.src_sectors[bs_i])); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2471 | ret = -1; |
| 2472 | goto out; |
| 2473 | } |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 2474 | src_bs = blk_bs(s.src[bs_i]); |
| 2475 | s.src_alignment[bs_i] = DIV_ROUND_UP(src_bs->bl.request_alignment, |
| 2476 | BDRV_SECTOR_SIZE); |
| 2477 | if (!bdrv_get_info(src_bs, &bdi)) { |
| 2478 | s.src_alignment[bs_i] = MAX(s.src_alignment[bs_i], |
| 2479 | bdi.cluster_size / BDRV_SECTOR_SIZE); |
| 2480 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2481 | s.total_sectors += s.src_sectors[bs_i]; |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 2482 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2483 | |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2484 | if (sn_opts) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2485 | bdrv_snapshot_load_tmp(blk_bs(s.src[0]), |
Peter Maydell | 10d6eda | 2017-02-10 16:28:24 +0000 | [diff] [blame] | 2486 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), |
| 2487 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), |
| 2488 | &local_err); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2489 | } else if (snapshot_name != NULL) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2490 | if (s.src_num > 1) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 2491 | error_report("No support for concatenating multiple snapshot"); |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 2492 | ret = -1; |
| 2493 | goto out; |
| 2494 | } |
Wenchao Xia | 7b4c478 | 2013-12-04 17:10:54 +0800 | [diff] [blame] | 2495 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2496 | bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name, |
| 2497 | &local_err); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2498 | } |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2499 | if (local_err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2500 | error_reportf_err(local_err, "Failed to load snapshot: "); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2501 | ret = -1; |
| 2502 | goto out; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 2503 | } |
| 2504 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2505 | if (!skip_create) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2506 | /* Find driver and parse its options */ |
| 2507 | drv = bdrv_find_format(out_fmt); |
| 2508 | if (!drv) { |
| 2509 | error_report("Unknown file format '%s'", out_fmt); |
| 2510 | ret = -1; |
| 2511 | goto out; |
| 2512 | } |
| 2513 | |
| 2514 | proto_drv = bdrv_find_protocol(out_filename, true, &local_err); |
| 2515 | if (!proto_drv) { |
| 2516 | error_report_err(local_err); |
| 2517 | ret = -1; |
| 2518 | goto out; |
| 2519 | } |
| 2520 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2521 | if (!drv->create_opts) { |
| 2522 | error_report("Format driver '%s' does not support image creation", |
| 2523 | drv->format_name); |
| 2524 | ret = -1; |
| 2525 | goto out; |
| 2526 | } |
Max Reitz | f75613c | 2014-12-02 18:32:46 +0100 | [diff] [blame] | 2527 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2528 | if (!proto_drv->create_opts) { |
| 2529 | error_report("Protocol driver '%s' does not support image creation", |
| 2530 | proto_drv->format_name); |
| 2531 | ret = -1; |
| 2532 | goto out; |
| 2533 | } |
Max Reitz | f75613c | 2014-12-02 18:32:46 +0100 | [diff] [blame] | 2534 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2535 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 2536 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
Kevin Wolf | db08adf | 2009-06-04 15:39:38 +0200 | [diff] [blame] | 2537 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2538 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 2539 | if (options) { |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 2540 | if (!qemu_opts_do_parse(opts, options, NULL, &local_err)) { |
Markus Armbruster | 97a2ca7 | 2015-03-14 10:23:15 +0100 | [diff] [blame] | 2541 | error_report_err(local_err); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 2542 | ret = -1; |
| 2543 | goto out; |
| 2544 | } |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2545 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2546 | |
Yi Li | c075c42 | 2020-08-19 09:36:07 +0800 | [diff] [blame] | 2547 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, |
| 2548 | s.total_sectors * BDRV_SECTOR_SIZE, &error_abort); |
Eric Blake | 1899bf4 | 2021-09-13 08:17:35 -0500 | [diff] [blame] | 2549 | ret = add_old_style_options(out_fmt, opts, out_baseimg, backing_fmt); |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2550 | if (ret < 0) { |
| 2551 | goto out; |
| 2552 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2553 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2554 | |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2555 | /* Get backing file name if -o backing_file was used */ |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2556 | out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2557 | if (out_baseimg_param) { |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2558 | out_baseimg = out_baseimg_param; |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2559 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2560 | s.target_has_backing = (bool) out_baseimg; |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2561 | |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2562 | if (s.has_zero_init && s.target_has_backing) { |
| 2563 | error_report("Cannot use --target-is-zero when the destination " |
| 2564 | "image has a backing file"); |
| 2565 | goto out; |
| 2566 | } |
| 2567 | |
Max Reitz | 48758a8 | 2017-04-26 15:46:48 +0200 | [diff] [blame] | 2568 | if (s.src_num > 1 && out_baseimg) { |
| 2569 | error_report("Having a backing file for the target makes no sense when " |
| 2570 | "concatenating multiple input images"); |
| 2571 | ret = -1; |
| 2572 | goto out; |
| 2573 | } |
| 2574 | |
Eric Blake | d9f059a | 2020-07-06 15:39:54 -0500 | [diff] [blame] | 2575 | if (out_baseimg_param) { |
| 2576 | if (!qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT)) { |
Eric Blake | 497a30d | 2021-05-03 14:36:00 -0700 | [diff] [blame] | 2577 | error_report("Use of backing file requires explicit " |
| 2578 | "backing format"); |
| 2579 | ret = -1; |
| 2580 | goto out; |
Eric Blake | d9f059a | 2020-07-06 15:39:54 -0500 | [diff] [blame] | 2581 | } |
| 2582 | } |
| 2583 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2584 | /* Check if compression is supported */ |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2585 | if (s.compressed) { |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2586 | bool encryption = |
| 2587 | qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false); |
Daniel P. Berrange | 0cb8d47 | 2017-06-23 17:24:06 +0100 | [diff] [blame] | 2588 | const char *encryptfmt = |
| 2589 | qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2590 | const char *preallocation = |
| 2591 | qemu_opt_get(opts, BLOCK_OPT_PREALLOC); |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2592 | |
Vladimir Sementsov-Ogievskiy | ac850bf | 2019-06-04 19:15:06 +0300 | [diff] [blame] | 2593 | if (drv && !block_driver_can_compress(drv)) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2594 | error_report("Compression not supported for this file format"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2595 | ret = -1; |
| 2596 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2597 | } |
| 2598 | |
Daniel P. Berrange | 0cb8d47 | 2017-06-23 17:24:06 +0100 | [diff] [blame] | 2599 | if (encryption || encryptfmt) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2600 | error_report("Compression and encryption not supported at " |
| 2601 | "the same time"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2602 | ret = -1; |
| 2603 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2604 | } |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 2605 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2606 | if (preallocation |
| 2607 | && strcmp(preallocation, "off")) |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 2608 | { |
| 2609 | error_report("Compression and preallocation not supported at " |
| 2610 | "the same time"); |
| 2611 | ret = -1; |
| 2612 | goto out; |
| 2613 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2614 | } |
| 2615 | |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2616 | /* Determine if bitmaps need copying */ |
| 2617 | if (bitmaps) { |
| 2618 | if (s.src_num > 1) { |
| 2619 | error_report("Copying bitmaps only possible with single source"); |
| 2620 | ret = -1; |
| 2621 | goto out; |
| 2622 | } |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2623 | ret = convert_check_bitmaps(blk_bs(s.src[0]), skip_broken); |
Eric Blake | 74a4320 | 2021-07-09 10:39:50 -0500 | [diff] [blame] | 2624 | if (ret < 0) { |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2625 | goto out; |
| 2626 | } |
| 2627 | } |
| 2628 | |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2629 | /* |
| 2630 | * The later open call will need any decryption secrets, and |
| 2631 | * bdrv_create() will purge "opts", so extract them now before |
| 2632 | * they are lost. |
| 2633 | */ |
| 2634 | if (!skip_create) { |
| 2635 | open_opts = qdict_new(); |
| 2636 | 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] | 2637 | |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2638 | /* Create the new image */ |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 2639 | ret = bdrv_create(drv, out_filename, opts, &local_err); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2640 | if (ret < 0) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2641 | error_reportf_err(local_err, "%s: error while converting %s: ", |
| 2642 | out_filename, out_fmt); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2643 | goto out; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2644 | } |
| 2645 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2646 | |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 2647 | s.target_is_new = !skip_create; |
| 2648 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2649 | 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] | 2650 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2651 | if (ret < 0) { |
| 2652 | error_report("Invalid cache option: %s", cache); |
Markus Armbruster | bb9cd2e | 2014-05-28 11:17:07 +0200 | [diff] [blame] | 2653 | goto out; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2654 | } |
| 2655 | |
Hanna Reitz | a1c6243 | 2021-08-19 12:12:00 +0200 | [diff] [blame] | 2656 | if (flags & BDRV_O_NOCACHE) { |
| 2657 | /* |
| 2658 | * If we open the target with O_DIRECT, it may be necessary to |
| 2659 | * extend its size to align to the physical sector size. |
| 2660 | */ |
| 2661 | flags |= BDRV_O_RESIZE; |
| 2662 | } |
| 2663 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2664 | if (skip_create) { |
| 2665 | s.target = img_open(tgt_image_opts, out_filename, out_fmt, |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2666 | flags, writethrough, s.quiet, false); |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2667 | } else { |
| 2668 | /* TODO ultimately we should allow --target-image-opts |
| 2669 | * to be used even when -n is not given. |
| 2670 | * That has to wait for bdrv_create to be improved |
| 2671 | * to allow filenames in option syntax |
| 2672 | */ |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2673 | s.target = img_open_file(out_filename, open_opts, out_fmt, |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2674 | flags, writethrough, s.quiet, false); |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2675 | open_opts = NULL; /* blk_new_open will have freed it */ |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2676 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2677 | if (!s.target) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2678 | ret = -1; |
| 2679 | goto out; |
| 2680 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2681 | out_bs = blk_bs(s.target); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2682 | |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2683 | if (bitmaps && !bdrv_supports_persistent_dirty_bitmap(out_bs)) { |
| 2684 | error_report("Format driver '%s' does not support bitmaps", |
| 2685 | out_bs->drv->format_name); |
| 2686 | ret = -1; |
| 2687 | goto out; |
| 2688 | } |
| 2689 | |
Vladimir Sementsov-Ogievskiy | ac850bf | 2019-06-04 19:15:06 +0300 | [diff] [blame] | 2690 | if (s.compressed && !block_driver_can_compress(out_bs->drv)) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2691 | error_report("Compression not supported for this file format"); |
| 2692 | ret = -1; |
| 2693 | goto out; |
| 2694 | } |
| 2695 | |
Eric Blake | 5def6b8 | 2016-06-23 16:37:19 -0600 | [diff] [blame] | 2696 | /* increase bufsectors from the default 4096 (2M) if opt_transfer |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 2697 | * or discard_alignment of the out_bs is greater. Limit to |
| 2698 | * MAX_BUF_SECTORS as maximum which is currently 32768 (16MB). */ |
| 2699 | s.buf_sectors = MIN(MAX_BUF_SECTORS, |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2700 | MAX(s.buf_sectors, |
| 2701 | MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS, |
| 2702 | out_bs->bl.pdiscard_alignment >> |
| 2703 | BDRV_SECTOR_BITS))); |
Peter Lieven | f2521c9 | 2013-11-27 11:07:06 +0100 | [diff] [blame] | 2704 | |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 2705 | /* try to align the write requests to the destination to avoid unnecessary |
| 2706 | * RMW cycles. */ |
| 2707 | s.alignment = MAX(pow2floor(s.min_sparse), |
| 2708 | DIV_ROUND_UP(out_bs->bl.request_alignment, |
| 2709 | BDRV_SECTOR_SIZE)); |
| 2710 | assert(is_power_of_2(s.alignment)); |
| 2711 | |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2712 | if (skip_create) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2713 | int64_t output_sectors = blk_nb_sectors(s.target); |
Markus Armbruster | 43716fa | 2014-06-26 13:23:21 +0200 | [diff] [blame] | 2714 | if (output_sectors < 0) { |
Gonglei | eec5eb4 | 2015-02-25 12:22:27 +0800 | [diff] [blame] | 2715 | error_report("unable to get output image length: %s", |
Markus Armbruster | 43716fa | 2014-06-26 13:23:21 +0200 | [diff] [blame] | 2716 | strerror(-output_sectors)); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2717 | ret = -1; |
| 2718 | goto out; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2719 | } else if (output_sectors < s.total_sectors) { |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2720 | error_report("output file is smaller than input file"); |
| 2721 | ret = -1; |
| 2722 | goto out; |
| 2723 | } |
| 2724 | } |
| 2725 | |
Max Reitz | c69291e | 2020-01-21 16:59:14 +0100 | [diff] [blame] | 2726 | if (s.target_has_backing && s.target_is_new) { |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 2727 | /* Errors are treated as "backing length unknown" (which means |
| 2728 | * s.target_backing_sectors has to be negative, which it will |
| 2729 | * be automatically). The backing file length is used only |
| 2730 | * for optimizations, so such a case is not fatal. */ |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 2731 | s.target_backing_sectors = |
| 2732 | bdrv_nb_sectors(bdrv_backing_chain_next(out_bs)); |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 2733 | } else { |
| 2734 | s.target_backing_sectors = -1; |
| 2735 | } |
| 2736 | |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2737 | ret = bdrv_get_info(out_bs, &bdi); |
| 2738 | if (ret < 0) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2739 | if (s.compressed) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2740 | error_report("could not get block driver info"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2741 | goto out; |
| 2742 | } |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2743 | } else { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2744 | s.compressed = s.compressed || bdi.needs_compressed_writes; |
| 2745 | s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE; |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2746 | } |
| 2747 | |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 2748 | if (rate_limit) { |
| 2749 | set_rate_limit(s.target, rate_limit); |
| 2750 | } |
| 2751 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2752 | ret = convert_do_copy(&s); |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2753 | |
| 2754 | /* Now copy the bitmaps */ |
| 2755 | if (bitmaps && ret == 0) { |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2756 | 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] | 2757 | } |
| 2758 | |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2759 | out: |
Peter Lieven | 13c28af | 2013-11-27 11:07:01 +0100 | [diff] [blame] | 2760 | if (!ret) { |
| 2761 | qemu_progress_print(100, 0); |
| 2762 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2763 | qemu_progress_end(); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2764 | qemu_opts_del(opts); |
| 2765 | qemu_opts_free(create_opts); |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2766 | qobject_unref(open_opts); |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2767 | blk_unref(s.target); |
| 2768 | if (s.src) { |
| 2769 | for (bs_i = 0; bs_i < s.src_num; bs_i++) { |
| 2770 | blk_unref(s.src[bs_i]); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2771 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2772 | g_free(s.src); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2773 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2774 | g_free(s.src_sectors); |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 2775 | g_free(s.src_alignment); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2776 | fail_getopt: |
Tuguoyi | 6aec830 | 2020-11-02 09:04:57 +0000 | [diff] [blame] | 2777 | qemu_opts_del(sn_opts); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2778 | g_free(options); |
| 2779 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2780 | return !!ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2781 | } |
| 2782 | |
bellard | 57d1a2b | 2004-08-03 21:15:11 +0000 | [diff] [blame] | 2783 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2784 | static void dump_snapshots(BlockDriverState *bs) |
| 2785 | { |
| 2786 | QEMUSnapshotInfo *sn_tab, *sn; |
| 2787 | int nb_sns, i; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2788 | |
| 2789 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 2790 | if (nb_sns <= 0) |
| 2791 | return; |
| 2792 | printf("Snapshot list:\n"); |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2793 | bdrv_snapshot_dump(NULL); |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2794 | printf("\n"); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2795 | for(i = 0; i < nb_sns; i++) { |
| 2796 | sn = &sn_tab[i]; |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2797 | bdrv_snapshot_dump(sn); |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2798 | printf("\n"); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2799 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2800 | g_free(sn_tab); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2801 | } |
| 2802 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2803 | static void dump_json_image_info_list(ImageInfoList *list) |
| 2804 | { |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2805 | GString *str; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2806 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 2807 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2808 | |
| 2809 | visit_type_ImageInfoList(v, NULL, &list, &error_abort); |
| 2810 | visit_complete(v, &obj); |
Markus Armbruster | 6589f45 | 2020-12-11 18:11:35 +0100 | [diff] [blame] | 2811 | str = qobject_to_json_pretty(obj, true); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2812 | assert(str != NULL); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2813 | printf("%s\n", str->str); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2814 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2815 | visit_free(v); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2816 | g_string_free(str, true); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2817 | } |
| 2818 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2819 | static void dump_json_image_info(ImageInfo *info) |
| 2820 | { |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2821 | GString *str; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2822 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 2823 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2824 | |
| 2825 | visit_type_ImageInfo(v, NULL, &info, &error_abort); |
| 2826 | visit_complete(v, &obj); |
Markus Armbruster | 6589f45 | 2020-12-11 18:11:35 +0100 | [diff] [blame] | 2827 | str = qobject_to_json_pretty(obj, true); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2828 | assert(str != NULL); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2829 | printf("%s\n", str->str); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2830 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2831 | visit_free(v); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2832 | g_string_free(str, true); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2833 | } |
| 2834 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2835 | static void dump_human_image_info_list(ImageInfoList *list) |
| 2836 | { |
| 2837 | ImageInfoList *elem; |
| 2838 | bool delim = false; |
| 2839 | |
| 2840 | for (elem = list; elem; elem = elem->next) { |
| 2841 | if (delim) { |
| 2842 | printf("\n"); |
| 2843 | } |
| 2844 | delim = true; |
| 2845 | |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2846 | bdrv_image_info_dump(elem->value); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2847 | } |
| 2848 | } |
| 2849 | |
| 2850 | static gboolean str_equal_func(gconstpointer a, gconstpointer b) |
| 2851 | { |
| 2852 | return strcmp(a, b) == 0; |
| 2853 | } |
| 2854 | |
| 2855 | /** |
| 2856 | * Open an image file chain and return an ImageInfoList |
| 2857 | * |
| 2858 | * @filename: topmost image filename |
| 2859 | * @fmt: topmost image format (may be NULL to autodetect) |
| 2860 | * @chain: true - enumerate entire backing file chain |
| 2861 | * false - only topmost image file |
| 2862 | * |
| 2863 | * Returns a list of ImageInfo objects or NULL if there was an error opening an |
| 2864 | * image file. If there was an error a message will have been printed to |
| 2865 | * stderr. |
| 2866 | */ |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2867 | static ImageInfoList *collect_image_info_list(bool image_opts, |
| 2868 | const char *filename, |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2869 | const char *fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2870 | bool chain, bool force_share) |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2871 | { |
| 2872 | ImageInfoList *head = NULL; |
Eric Blake | c3033fd | 2021-01-13 16:10:12 -0600 | [diff] [blame] | 2873 | ImageInfoList **tail = &head; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2874 | GHashTable *filenames; |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2875 | Error *err = NULL; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2876 | |
| 2877 | filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL); |
| 2878 | |
| 2879 | while (filename) { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2880 | BlockBackend *blk; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2881 | BlockDriverState *bs; |
| 2882 | ImageInfo *info; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2883 | |
| 2884 | if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) { |
| 2885 | error_report("Backing file '%s' creates an infinite loop.", |
| 2886 | filename); |
| 2887 | goto err; |
| 2888 | } |
| 2889 | g_hash_table_insert(filenames, (gpointer)filename, NULL); |
| 2890 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 2891 | blk = img_open(image_opts, filename, fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2892 | BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false, |
| 2893 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2894 | if (!blk) { |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2895 | goto err; |
| 2896 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2897 | bs = blk_bs(blk); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2898 | |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2899 | bdrv_query_image_info(bs, &info, &err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2900 | if (err) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 2901 | error_report_err(err); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2902 | blk_unref(blk); |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2903 | goto err; |
Wenchao Xia | fb0ed45 | 2013-06-06 12:27:57 +0800 | [diff] [blame] | 2904 | } |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2905 | |
Eric Blake | c3033fd | 2021-01-13 16:10:12 -0600 | [diff] [blame] | 2906 | QAPI_LIST_APPEND(tail, info); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2907 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2908 | blk_unref(blk); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2909 | |
Stefan Hajnoczi | 0da7d13 | 2019-12-05 13:46:46 +0000 | [diff] [blame] | 2910 | /* Clear parameters that only apply to the topmost image */ |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2911 | filename = fmt = NULL; |
Stefan Hajnoczi | 0da7d13 | 2019-12-05 13:46:46 +0000 | [diff] [blame] | 2912 | image_opts = false; |
| 2913 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2914 | if (chain) { |
| 2915 | if (info->has_full_backing_filename) { |
| 2916 | filename = info->full_backing_filename; |
| 2917 | } else if (info->has_backing_filename) { |
John Snow | 92d617a | 2015-12-14 14:55:15 -0500 | [diff] [blame] | 2918 | error_report("Could not determine absolute backing filename," |
| 2919 | " but backing filename '%s' present", |
| 2920 | info->backing_filename); |
| 2921 | goto err; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2922 | } |
| 2923 | if (info->has_backing_filename_format) { |
| 2924 | fmt = info->backing_filename_format; |
| 2925 | } |
| 2926 | } |
| 2927 | } |
| 2928 | g_hash_table_destroy(filenames); |
| 2929 | return head; |
| 2930 | |
| 2931 | err: |
| 2932 | qapi_free_ImageInfoList(head); |
| 2933 | g_hash_table_destroy(filenames); |
| 2934 | return NULL; |
| 2935 | } |
| 2936 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2937 | static int img_info(int argc, char **argv) |
| 2938 | { |
| 2939 | int c; |
| 2940 | OutputFormat output_format = OFORMAT_HUMAN; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2941 | bool chain = false; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2942 | const char *filename, *fmt, *output; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2943 | ImageInfoList *list; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2944 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2945 | bool force_share = false; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2946 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2947 | fmt = NULL; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2948 | output = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2949 | for(;;) { |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2950 | int option_index = 0; |
| 2951 | static const struct option long_options[] = { |
| 2952 | {"help", no_argument, 0, 'h'}, |
| 2953 | {"format", required_argument, 0, 'f'}, |
| 2954 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2955 | {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2956 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2957 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2958 | {"force-share", no_argument, 0, 'U'}, |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2959 | {0, 0, 0, 0} |
| 2960 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2961 | c = getopt_long(argc, argv, ":f:hU", |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2962 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2963 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2964 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2965 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2966 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2967 | case ':': |
| 2968 | missing_argument(argv[optind - 1]); |
| 2969 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2970 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2971 | unrecognized_option(argv[optind - 1]); |
| 2972 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2973 | case 'h': |
| 2974 | help(); |
| 2975 | break; |
| 2976 | case 'f': |
| 2977 | fmt = optarg; |
| 2978 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2979 | case 'U': |
| 2980 | force_share = true; |
| 2981 | break; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2982 | case OPTION_OUTPUT: |
| 2983 | output = optarg; |
| 2984 | break; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2985 | case OPTION_BACKING_CHAIN: |
| 2986 | chain = true; |
| 2987 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 2988 | case OPTION_OBJECT: |
| 2989 | user_creatable_process_cmdline(optarg); |
| 2990 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2991 | case OPTION_IMAGE_OPTS: |
| 2992 | image_opts = true; |
| 2993 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2994 | } |
| 2995 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 2996 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2997 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2998 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2999 | filename = argv[optind++]; |
| 3000 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 3001 | if (output && !strcmp(output, "json")) { |
| 3002 | output_format = OFORMAT_JSON; |
| 3003 | } else if (output && !strcmp(output, "human")) { |
| 3004 | output_format = OFORMAT_HUMAN; |
| 3005 | } else if (output) { |
| 3006 | error_report("--output must be used with human or json as argument."); |
| 3007 | return 1; |
| 3008 | } |
| 3009 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3010 | list = collect_image_info_list(image_opts, filename, fmt, chain, |
| 3011 | force_share); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 3012 | if (!list) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3013 | return 1; |
| 3014 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 3015 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 3016 | switch (output_format) { |
| 3017 | case OFORMAT_HUMAN: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 3018 | dump_human_image_info_list(list); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 3019 | break; |
| 3020 | case OFORMAT_JSON: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 3021 | if (chain) { |
| 3022 | dump_json_image_info_list(list); |
| 3023 | } else { |
| 3024 | dump_json_image_info(list->value); |
| 3025 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 3026 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3027 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 3028 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 3029 | qapi_free_ImageInfoList(list); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 3030 | return 0; |
| 3031 | } |
| 3032 | |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3033 | static int dump_map_entry(OutputFormat output_format, MapEntry *e, |
| 3034 | MapEntry *next) |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3035 | { |
| 3036 | switch (output_format) { |
| 3037 | case OFORMAT_HUMAN: |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3038 | if (e->data && !e->has_offset) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3039 | error_report("File contains external, encrypted or compressed clusters."); |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3040 | return -1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3041 | } |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3042 | if (e->data && !e->zero) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3043 | printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n", |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3044 | e->start, e->length, |
| 3045 | e->has_offset ? e->offset : 0, |
| 3046 | e->has_filename ? e->filename : ""); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3047 | } |
| 3048 | /* This format ignores the distinction between 0, ZERO and ZERO|DATA. |
| 3049 | * Modify the flags here to allow more coalescing. |
| 3050 | */ |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3051 | if (next && (!next->data || next->zero)) { |
| 3052 | next->data = false; |
| 3053 | next->zero = true; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3054 | } |
| 3055 | break; |
| 3056 | case OFORMAT_JSON: |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 3057 | printf("{ \"start\": %"PRId64", \"length\": %"PRId64"," |
Eric Blake | 8417e13 | 2021-07-01 14:06:55 -0500 | [diff] [blame] | 3058 | " \"depth\": %"PRId64", \"present\": %s, \"zero\": %s," |
| 3059 | " \"data\": %s", e->start, e->length, e->depth, |
| 3060 | e->present ? "true" : "false", |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3061 | e->zero ? "true" : "false", |
| 3062 | e->data ? "true" : "false"); |
| 3063 | if (e->has_offset) { |
Paolo Bonzini | c745bfb | 2013-09-11 18:47:52 +0200 | [diff] [blame] | 3064 | printf(", \"offset\": %"PRId64"", e->offset); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3065 | } |
| 3066 | putchar('}'); |
| 3067 | |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 3068 | if (next) { |
| 3069 | puts(","); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3070 | } |
| 3071 | break; |
| 3072 | } |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3073 | return 0; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3074 | } |
| 3075 | |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3076 | static int get_block_status(BlockDriverState *bs, int64_t offset, |
| 3077 | int64_t bytes, MapEntry *e) |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3078 | { |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3079 | int ret; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3080 | int depth; |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 3081 | BlockDriverState *file; |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3082 | bool has_offset; |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3083 | int64_t map; |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3084 | char *filename = NULL; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3085 | |
| 3086 | /* As an optimization, we could cache the current range of unallocated |
| 3087 | * clusters in each file of the chain, and avoid querying the same |
| 3088 | * range repeatedly. |
| 3089 | */ |
| 3090 | |
| 3091 | depth = 0; |
| 3092 | for (;;) { |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3093 | bs = bdrv_skip_filters(bs); |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3094 | ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3095 | if (ret < 0) { |
| 3096 | return ret; |
| 3097 | } |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3098 | assert(bytes); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3099 | if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) { |
| 3100 | break; |
| 3101 | } |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3102 | bs = bdrv_cow_bs(bs); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3103 | if (bs == NULL) { |
| 3104 | ret = 0; |
| 3105 | break; |
| 3106 | } |
| 3107 | |
| 3108 | depth++; |
| 3109 | } |
| 3110 | |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3111 | has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID); |
| 3112 | |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3113 | if (file && has_offset) { |
| 3114 | bdrv_refresh_filename(file); |
| 3115 | filename = file->filename; |
| 3116 | } |
| 3117 | |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3118 | *e = (MapEntry) { |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3119 | .start = offset, |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3120 | .length = bytes, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3121 | .data = !!(ret & BDRV_BLOCK_DATA), |
| 3122 | .zero = !!(ret & BDRV_BLOCK_ZERO), |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3123 | .offset = map, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3124 | .has_offset = has_offset, |
| 3125 | .depth = depth, |
Eric Blake | 8417e13 | 2021-07-01 14:06:55 -0500 | [diff] [blame] | 3126 | .present = !!(ret & BDRV_BLOCK_ALLOCATED), |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3127 | .has_filename = filename, |
| 3128 | .filename = filename, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3129 | }; |
| 3130 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3131 | return 0; |
| 3132 | } |
| 3133 | |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3134 | static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next) |
| 3135 | { |
| 3136 | if (curr->length == 0) { |
| 3137 | return false; |
| 3138 | } |
| 3139 | if (curr->zero != next->zero || |
| 3140 | curr->data != next->data || |
| 3141 | curr->depth != next->depth || |
Eric Blake | 8417e13 | 2021-07-01 14:06:55 -0500 | [diff] [blame] | 3142 | curr->present != next->present || |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3143 | curr->has_filename != next->has_filename || |
| 3144 | curr->has_offset != next->has_offset) { |
| 3145 | return false; |
| 3146 | } |
| 3147 | if (curr->has_filename && strcmp(curr->filename, next->filename)) { |
| 3148 | return false; |
| 3149 | } |
| 3150 | if (curr->has_offset && curr->offset + curr->length != next->offset) { |
| 3151 | return false; |
| 3152 | } |
| 3153 | return true; |
| 3154 | } |
| 3155 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3156 | static int img_map(int argc, char **argv) |
| 3157 | { |
| 3158 | int c; |
| 3159 | OutputFormat output_format = OFORMAT_HUMAN; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3160 | BlockBackend *blk; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3161 | BlockDriverState *bs; |
| 3162 | const char *filename, *fmt, *output; |
| 3163 | int64_t length; |
| 3164 | MapEntry curr = { .length = 0 }, next; |
| 3165 | int ret = 0; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3166 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3167 | bool force_share = false; |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3168 | int64_t start_offset = 0; |
| 3169 | int64_t max_length = -1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3170 | |
| 3171 | fmt = NULL; |
| 3172 | output = NULL; |
| 3173 | for (;;) { |
| 3174 | int option_index = 0; |
| 3175 | static const struct option long_options[] = { |
| 3176 | {"help", no_argument, 0, 'h'}, |
| 3177 | {"format", required_argument, 0, 'f'}, |
| 3178 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3179 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3180 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3181 | {"force-share", no_argument, 0, 'U'}, |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3182 | {"start-offset", required_argument, 0, 's'}, |
| 3183 | {"max-length", required_argument, 0, 'l'}, |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3184 | {0, 0, 0, 0} |
| 3185 | }; |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3186 | c = getopt_long(argc, argv, ":f:s:l:hU", |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3187 | long_options, &option_index); |
| 3188 | if (c == -1) { |
| 3189 | break; |
| 3190 | } |
| 3191 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3192 | case ':': |
| 3193 | missing_argument(argv[optind - 1]); |
| 3194 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3195 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3196 | unrecognized_option(argv[optind - 1]); |
| 3197 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3198 | case 'h': |
| 3199 | help(); |
| 3200 | break; |
| 3201 | case 'f': |
| 3202 | fmt = optarg; |
| 3203 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3204 | case 'U': |
| 3205 | force_share = true; |
| 3206 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3207 | case OPTION_OUTPUT: |
| 3208 | output = optarg; |
| 3209 | break; |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3210 | case 's': |
| 3211 | start_offset = cvtnum("start offset", optarg); |
| 3212 | if (start_offset < 0) { |
| 3213 | return 1; |
| 3214 | } |
| 3215 | break; |
| 3216 | case 'l': |
| 3217 | max_length = cvtnum("max length", optarg); |
| 3218 | if (max_length < 0) { |
| 3219 | return 1; |
| 3220 | } |
| 3221 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 3222 | case OPTION_OBJECT: |
| 3223 | user_creatable_process_cmdline(optarg); |
| 3224 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3225 | case OPTION_IMAGE_OPTS: |
| 3226 | image_opts = true; |
| 3227 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3228 | } |
| 3229 | } |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3230 | if (optind != argc - 1) { |
| 3231 | error_exit("Expecting one image file name"); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3232 | } |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3233 | filename = argv[optind]; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3234 | |
| 3235 | if (output && !strcmp(output, "json")) { |
| 3236 | output_format = OFORMAT_JSON; |
| 3237 | } else if (output && !strcmp(output, "human")) { |
| 3238 | output_format = OFORMAT_HUMAN; |
| 3239 | } else if (output) { |
| 3240 | error_report("--output must be used with human or json as argument."); |
| 3241 | return 1; |
| 3242 | } |
| 3243 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3244 | blk = img_open(image_opts, filename, fmt, 0, false, false, force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3245 | if (!blk) { |
| 3246 | return 1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3247 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3248 | bs = blk_bs(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3249 | |
| 3250 | if (output_format == OFORMAT_HUMAN) { |
| 3251 | printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File"); |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 3252 | } else if (output_format == OFORMAT_JSON) { |
| 3253 | putchar('['); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3254 | } |
| 3255 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3256 | length = blk_getlength(blk); |
Eyal Moscovici | 8f282e8 | 2020-05-13 16:36:27 +0300 | [diff] [blame] | 3257 | if (length < 0) { |
| 3258 | error_report("Failed to get size for '%s'", filename); |
| 3259 | return 1; |
| 3260 | } |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3261 | if (max_length != -1) { |
| 3262 | length = MIN(start_offset + max_length, length); |
| 3263 | } |
Eyal Moscovici | 8f282e8 | 2020-05-13 16:36:27 +0300 | [diff] [blame] | 3264 | |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3265 | curr.start = start_offset; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3266 | while (curr.start + curr.length < length) { |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3267 | int64_t offset = curr.start + curr.length; |
Kevin Wolf | d0ceea8 | 2020-07-07 16:46:29 +0200 | [diff] [blame] | 3268 | int64_t n = length - offset; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3269 | |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3270 | ret = get_block_status(bs, offset, n, &next); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3271 | if (ret < 0) { |
| 3272 | error_report("Could not read file metadata: %s", strerror(-ret)); |
| 3273 | goto out; |
| 3274 | } |
| 3275 | |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3276 | if (entry_mergeable(&curr, &next)) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3277 | curr.length += next.length; |
| 3278 | continue; |
| 3279 | } |
| 3280 | |
| 3281 | if (curr.length > 0) { |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3282 | ret = dump_map_entry(output_format, &curr, &next); |
| 3283 | if (ret < 0) { |
| 3284 | goto out; |
| 3285 | } |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3286 | } |
| 3287 | curr = next; |
| 3288 | } |
| 3289 | |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3290 | ret = dump_map_entry(output_format, &curr, NULL); |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 3291 | if (output_format == OFORMAT_JSON) { |
| 3292 | puts("]"); |
| 3293 | } |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3294 | |
| 3295 | out: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3296 | blk_unref(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3297 | return ret < 0; |
| 3298 | } |
| 3299 | |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3300 | #define SNAPSHOT_LIST 1 |
| 3301 | #define SNAPSHOT_CREATE 2 |
| 3302 | #define SNAPSHOT_APPLY 3 |
| 3303 | #define SNAPSHOT_DELETE 4 |
| 3304 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3305 | static int img_snapshot(int argc, char **argv) |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3306 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3307 | BlockBackend *blk; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3308 | BlockDriverState *bs; |
| 3309 | QEMUSnapshotInfo sn; |
| 3310 | char *filename, *snapshot_name = NULL; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3311 | int c, ret = 0, bdrv_oflags; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3312 | int action = 0; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3313 | bool quiet = false; |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 3314 | Error *err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3315 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3316 | bool force_share = false; |
Marc-André Lureau | f793dde | 2022-03-07 11:04:00 +0400 | [diff] [blame] | 3317 | int64_t rt; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3318 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3319 | bdrv_oflags = BDRV_O_RDWR; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3320 | /* Parse commandline parameters */ |
| 3321 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3322 | static const struct option long_options[] = { |
| 3323 | {"help", no_argument, 0, 'h'}, |
| 3324 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3325 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3326 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3327 | {0, 0, 0, 0} |
| 3328 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3329 | c = getopt_long(argc, argv, ":la:c:d:hqU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3330 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3331 | if (c == -1) { |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3332 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3333 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3334 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3335 | case ':': |
| 3336 | missing_argument(argv[optind - 1]); |
| 3337 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3338 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3339 | unrecognized_option(argv[optind - 1]); |
| 3340 | break; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3341 | case 'h': |
| 3342 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3343 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3344 | case 'l': |
| 3345 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3346 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3347 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3348 | } |
| 3349 | action = SNAPSHOT_LIST; |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 3350 | bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */ |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3351 | break; |
| 3352 | case 'a': |
| 3353 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3354 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3355 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3356 | } |
| 3357 | action = SNAPSHOT_APPLY; |
| 3358 | snapshot_name = optarg; |
| 3359 | break; |
| 3360 | case 'c': |
| 3361 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3362 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3363 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3364 | } |
| 3365 | action = SNAPSHOT_CREATE; |
| 3366 | snapshot_name = optarg; |
| 3367 | break; |
| 3368 | case 'd': |
| 3369 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3370 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3371 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3372 | } |
| 3373 | action = SNAPSHOT_DELETE; |
| 3374 | snapshot_name = optarg; |
| 3375 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3376 | case 'q': |
| 3377 | quiet = true; |
| 3378 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3379 | case 'U': |
| 3380 | force_share = true; |
| 3381 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 3382 | case OPTION_OBJECT: |
| 3383 | user_creatable_process_cmdline(optarg); |
| 3384 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3385 | case OPTION_IMAGE_OPTS: |
| 3386 | image_opts = true; |
| 3387 | break; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3388 | } |
| 3389 | } |
| 3390 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 3391 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3392 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3393 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3394 | filename = argv[optind++]; |
| 3395 | |
| 3396 | /* Open the image */ |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3397 | blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet, |
| 3398 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3399 | if (!blk) { |
| 3400 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3401 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3402 | bs = blk_bs(blk); |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3403 | |
| 3404 | /* Perform the requested action */ |
| 3405 | switch(action) { |
| 3406 | case SNAPSHOT_LIST: |
| 3407 | dump_snapshots(bs); |
| 3408 | break; |
| 3409 | |
| 3410 | case SNAPSHOT_CREATE: |
| 3411 | memset(&sn, 0, sizeof(sn)); |
| 3412 | pstrcpy(sn.name, sizeof(sn.name), snapshot_name); |
| 3413 | |
Marc-André Lureau | f793dde | 2022-03-07 11:04:00 +0400 | [diff] [blame] | 3414 | rt = g_get_real_time(); |
| 3415 | sn.date_sec = rt / G_USEC_PER_SEC; |
| 3416 | sn.date_nsec = (rt % G_USEC_PER_SEC) * 1000; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3417 | |
| 3418 | ret = bdrv_snapshot_create(bs, &sn); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3419 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3420 | error_report("Could not create snapshot '%s': %d (%s)", |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3421 | snapshot_name, ret, strerror(-ret)); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3422 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3423 | break; |
| 3424 | |
| 3425 | case SNAPSHOT_APPLY: |
Kevin Wolf | 0b62bcb | 2017-11-20 15:28:41 +0100 | [diff] [blame] | 3426 | ret = bdrv_snapshot_goto(bs, snapshot_name, &err); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3427 | if (ret) { |
Kevin Wolf | 0b62bcb | 2017-11-20 15:28:41 +0100 | [diff] [blame] | 3428 | error_reportf_err(err, "Could not apply snapshot '%s': ", |
| 3429 | snapshot_name); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3430 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3431 | break; |
| 3432 | |
| 3433 | case SNAPSHOT_DELETE: |
Daniel Henrique Barboza | 8c04093 | 2018-11-07 11:09:59 -0200 | [diff] [blame] | 3434 | ret = bdrv_snapshot_find(bs, &sn, snapshot_name); |
| 3435 | if (ret < 0) { |
| 3436 | error_report("Could not delete snapshot '%s': snapshot not " |
| 3437 | "found", snapshot_name); |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 3438 | ret = 1; |
Daniel Henrique Barboza | 8c04093 | 2018-11-07 11:09:59 -0200 | [diff] [blame] | 3439 | } else { |
| 3440 | ret = bdrv_snapshot_delete(bs, sn.id_str, sn.name, &err); |
| 3441 | if (ret < 0) { |
| 3442 | error_reportf_err(err, "Could not delete snapshot '%s': ", |
| 3443 | snapshot_name); |
| 3444 | ret = 1; |
| 3445 | } |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3446 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3447 | break; |
| 3448 | } |
| 3449 | |
| 3450 | /* Cleanup */ |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3451 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3452 | if (ret) { |
| 3453 | return 1; |
| 3454 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3455 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3456 | } |
| 3457 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3458 | static int img_rebase(int argc, char **argv) |
| 3459 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3460 | BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL; |
Paolo Bonzini | 396374c | 2016-02-25 23:53:54 +0100 | [diff] [blame] | 3461 | uint8_t *buf_old = NULL; |
| 3462 | uint8_t *buf_new = NULL; |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3463 | BlockDriverState *bs = NULL, *prefix_chain_bs = NULL; |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3464 | BlockDriverState *unfiltered_bs; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3465 | char *filename; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3466 | const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg; |
| 3467 | int c, flags, src_flags, ret; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3468 | bool writethrough, src_writethrough; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3469 | int unsafe = 0; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3470 | bool force_share = false; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3471 | int progress = 0; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3472 | bool quiet = false; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 3473 | Error *local_err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3474 | bool image_opts = false; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3475 | |
| 3476 | /* Parse commandline parameters */ |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 3477 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3478 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3479 | src_cache = BDRV_DEFAULT_CACHE; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3480 | out_baseimg = NULL; |
| 3481 | out_basefmt = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3482 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3483 | static const struct option long_options[] = { |
| 3484 | {"help", no_argument, 0, 'h'}, |
| 3485 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3486 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3487 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3488 | {0, 0, 0, 0} |
| 3489 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3490 | c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3491 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3492 | if (c == -1) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3493 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3494 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3495 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3496 | case ':': |
| 3497 | missing_argument(argv[optind - 1]); |
| 3498 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3499 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3500 | unrecognized_option(argv[optind - 1]); |
| 3501 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3502 | case 'h': |
| 3503 | help(); |
| 3504 | return 0; |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 3505 | case 'f': |
| 3506 | fmt = optarg; |
| 3507 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3508 | case 'F': |
| 3509 | out_basefmt = optarg; |
| 3510 | break; |
| 3511 | case 'b': |
| 3512 | out_baseimg = optarg; |
| 3513 | break; |
| 3514 | case 'u': |
| 3515 | unsafe = 1; |
| 3516 | break; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3517 | case 'p': |
| 3518 | progress = 1; |
| 3519 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3520 | case 't': |
| 3521 | cache = optarg; |
| 3522 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3523 | case 'T': |
| 3524 | src_cache = optarg; |
| 3525 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3526 | case 'q': |
| 3527 | quiet = true; |
| 3528 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 3529 | case OPTION_OBJECT: |
| 3530 | user_creatable_process_cmdline(optarg); |
| 3531 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3532 | case OPTION_IMAGE_OPTS: |
| 3533 | image_opts = true; |
| 3534 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3535 | case 'U': |
| 3536 | force_share = true; |
| 3537 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3538 | } |
| 3539 | } |
| 3540 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3541 | if (quiet) { |
| 3542 | progress = 0; |
| 3543 | } |
| 3544 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3545 | if (optind != argc - 1) { |
| 3546 | error_exit("Expecting one image file name"); |
| 3547 | } |
| 3548 | if (!unsafe && !out_baseimg) { |
| 3549 | error_exit("Must specify backing file (-b) or use unsafe mode (-u)"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3550 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3551 | filename = argv[optind++]; |
| 3552 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3553 | qemu_progress_init(progress, 2.0); |
| 3554 | qemu_progress_print(0, 100); |
| 3555 | |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3556 | flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0); |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3557 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3558 | if (ret < 0) { |
| 3559 | error_report("Invalid cache option: %s", cache); |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3560 | goto out; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3561 | } |
| 3562 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3563 | src_flags = 0; |
| 3564 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3565 | if (ret < 0) { |
| 3566 | error_report("Invalid source cache option: %s", src_cache); |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3567 | goto out; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3568 | } |
| 3569 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3570 | /* The source files are opened read-only, don't care about WCE */ |
| 3571 | assert((src_flags & BDRV_O_RDWR) == 0); |
| 3572 | (void) src_writethrough; |
| 3573 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3574 | /* |
| 3575 | * Open the images. |
| 3576 | * |
| 3577 | * Ignore the old backing file for unsafe rebase in case we want to correct |
| 3578 | * the reference to a renamed or moved backing file. |
| 3579 | */ |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3580 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 3581 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3582 | if (!blk) { |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3583 | ret = -1; |
| 3584 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3585 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3586 | bs = blk_bs(blk); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3587 | |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3588 | unfiltered_bs = bdrv_skip_filters(bs); |
| 3589 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3590 | if (out_basefmt != NULL) { |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3591 | if (bdrv_find_format(out_basefmt) == NULL) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3592 | error_report("Invalid format name: '%s'", out_basefmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3593 | ret = -1; |
| 3594 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3595 | } |
| 3596 | } |
| 3597 | |
| 3598 | /* For safe rebasing we need to compare old and new backing file */ |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3599 | if (!unsafe) { |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3600 | QDict *options = NULL; |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3601 | BlockDriverState *base_bs = bdrv_cow_bs(unfiltered_bs); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3602 | |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3603 | if (base_bs) { |
Kevin Wolf | d861ab3 | 2019-04-25 14:25:10 +0200 | [diff] [blame] | 3604 | blk_old_backing = blk_new(qemu_get_aio_context(), |
| 3605 | BLK_PERM_CONSISTENT_READ, |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3606 | BLK_PERM_ALL); |
| 3607 | ret = blk_insert_bs(blk_old_backing, base_bs, |
| 3608 | &local_err); |
| 3609 | if (ret < 0) { |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3610 | error_reportf_err(local_err, |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3611 | "Could not reuse old backing file '%s': ", |
| 3612 | base_bs->filename); |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3613 | goto out; |
| 3614 | } |
| 3615 | } else { |
| 3616 | blk_old_backing = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3617 | } |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3618 | |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3619 | if (out_baseimg[0]) { |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3620 | const char *overlay_filename; |
| 3621 | char *out_real_path; |
| 3622 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3623 | options = qdict_new(); |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3624 | if (out_basefmt) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 3625 | qdict_put_str(options, "driver", out_basefmt); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3626 | } |
| 3627 | if (force_share) { |
| 3628 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3629 | } |
| 3630 | |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3631 | bdrv_refresh_filename(bs); |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3632 | overlay_filename = bs->exact_filename[0] ? bs->exact_filename |
| 3633 | : bs->filename; |
Max Reitz | 645ae7d | 2019-02-01 20:29:14 +0100 | [diff] [blame] | 3634 | out_real_path = |
| 3635 | bdrv_get_full_backing_filename_from_filename(overlay_filename, |
| 3636 | out_baseimg, |
| 3637 | &local_err); |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3638 | if (local_err) { |
Max Reitz | f22356d | 2019-05-28 21:53:38 +0200 | [diff] [blame] | 3639 | qobject_unref(options); |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3640 | error_reportf_err(local_err, |
| 3641 | "Could not resolve backing filename: "); |
| 3642 | ret = -1; |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3643 | goto out; |
| 3644 | } |
| 3645 | |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3646 | /* |
| 3647 | * Find out whether we rebase an image on top of a previous image |
| 3648 | * in its chain. |
| 3649 | */ |
| 3650 | prefix_chain_bs = bdrv_find_backing_image(bs, out_real_path); |
Sam Eiderman | 330c729 | 2019-05-23 19:33:37 +0300 | [diff] [blame] | 3651 | if (prefix_chain_bs) { |
Max Reitz | f22356d | 2019-05-28 21:53:38 +0200 | [diff] [blame] | 3652 | qobject_unref(options); |
Sam Eiderman | 330c729 | 2019-05-23 19:33:37 +0300 | [diff] [blame] | 3653 | g_free(out_real_path); |
Max Reitz | f22356d | 2019-05-28 21:53:38 +0200 | [diff] [blame] | 3654 | |
Kevin Wolf | d861ab3 | 2019-04-25 14:25:10 +0200 | [diff] [blame] | 3655 | blk_new_backing = blk_new(qemu_get_aio_context(), |
| 3656 | BLK_PERM_CONSISTENT_READ, |
Sam Eiderman | 330c729 | 2019-05-23 19:33:37 +0300 | [diff] [blame] | 3657 | BLK_PERM_ALL); |
| 3658 | ret = blk_insert_bs(blk_new_backing, prefix_chain_bs, |
| 3659 | &local_err); |
| 3660 | if (ret < 0) { |
| 3661 | error_reportf_err(local_err, |
| 3662 | "Could not reuse backing file '%s': ", |
| 3663 | out_baseimg); |
| 3664 | goto out; |
| 3665 | } |
| 3666 | } else { |
| 3667 | blk_new_backing = blk_new_open(out_real_path, NULL, |
| 3668 | options, src_flags, &local_err); |
| 3669 | g_free(out_real_path); |
| 3670 | if (!blk_new_backing) { |
| 3671 | error_reportf_err(local_err, |
| 3672 | "Could not open new backing file '%s': ", |
| 3673 | out_baseimg); |
| 3674 | ret = -1; |
| 3675 | goto out; |
| 3676 | } |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3677 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3678 | } |
| 3679 | } |
| 3680 | |
| 3681 | /* |
| 3682 | * Check each unallocated cluster in the COW file. If it is unallocated, |
| 3683 | * accesses go to the backing file. We must therefore compare this cluster |
| 3684 | * in the old and new backing file, and if they differ we need to copy it |
| 3685 | * from the old backing file into the COW file. |
| 3686 | * |
| 3687 | * If qemu-img crashes during this step, no harm is done. The content of |
| 3688 | * the image is the same as the original one at any time. |
| 3689 | */ |
| 3690 | if (!unsafe) { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3691 | int64_t size; |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3692 | int64_t old_backing_size = 0; |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3693 | int64_t new_backing_size = 0; |
| 3694 | uint64_t offset; |
| 3695 | int64_t n; |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 3696 | float local_progress = 0; |
TeLeMan | d6771bf | 2010-02-08 16:20:00 +0800 | [diff] [blame] | 3697 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3698 | buf_old = blk_blockalign(blk, IO_BUF_SIZE); |
| 3699 | buf_new = blk_blockalign(blk, IO_BUF_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3700 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3701 | size = blk_getlength(blk); |
| 3702 | if (size < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3703 | error_report("Could not get size of '%s': %s", |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3704 | filename, strerror(-size)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3705 | ret = -1; |
| 3706 | goto out; |
| 3707 | } |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3708 | if (blk_old_backing) { |
| 3709 | old_backing_size = blk_getlength(blk_old_backing); |
| 3710 | if (old_backing_size < 0) { |
| 3711 | char backing_name[PATH_MAX]; |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3712 | |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3713 | bdrv_get_backing_filename(bs, backing_name, |
| 3714 | sizeof(backing_name)); |
| 3715 | error_report("Could not get size of '%s': %s", |
| 3716 | backing_name, strerror(-old_backing_size)); |
| 3717 | ret = -1; |
| 3718 | goto out; |
| 3719 | } |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3720 | } |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3721 | if (blk_new_backing) { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3722 | new_backing_size = blk_getlength(blk_new_backing); |
| 3723 | if (new_backing_size < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3724 | error_report("Could not get size of '%s': %s", |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3725 | out_baseimg, strerror(-new_backing_size)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3726 | ret = -1; |
| 3727 | goto out; |
| 3728 | } |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3729 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3730 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3731 | if (size != 0) { |
| 3732 | local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE)); |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 3733 | } |
| 3734 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3735 | for (offset = 0; offset < size; offset += n) { |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3736 | bool buf_old_is_zero = false; |
| 3737 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3738 | /* How many bytes can we handle with the next read? */ |
| 3739 | n = MIN(IO_BUF_SIZE, size - offset); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3740 | |
| 3741 | /* If the cluster is allocated, we don't need to take action */ |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3742 | ret = bdrv_is_allocated(unfiltered_bs, offset, n, &n); |
Paolo Bonzini | d663640 | 2013-09-04 19:00:25 +0200 | [diff] [blame] | 3743 | if (ret < 0) { |
| 3744 | error_report("error while reading image metadata: %s", |
| 3745 | strerror(-ret)); |
| 3746 | goto out; |
| 3747 | } |
Kevin Wolf | cc60e32 | 2010-04-29 14:47:48 +0200 | [diff] [blame] | 3748 | if (ret) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3749 | continue; |
| 3750 | } |
| 3751 | |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3752 | if (prefix_chain_bs) { |
| 3753 | /* |
| 3754 | * If cluster wasn't changed since prefix_chain, we don't need |
| 3755 | * to take action |
| 3756 | */ |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3757 | ret = bdrv_is_allocated_above(bdrv_cow_bs(unfiltered_bs), |
| 3758 | prefix_chain_bs, false, |
| 3759 | offset, n, &n); |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3760 | if (ret < 0) { |
| 3761 | error_report("error while reading image metadata: %s", |
| 3762 | strerror(-ret)); |
| 3763 | goto out; |
| 3764 | } |
| 3765 | if (!ret) { |
| 3766 | continue; |
| 3767 | } |
| 3768 | } |
| 3769 | |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3770 | /* |
| 3771 | * Read old and new backing file and take into consideration that |
| 3772 | * backing files may be smaller than the COW image. |
| 3773 | */ |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3774 | if (offset >= old_backing_size) { |
| 3775 | memset(buf_old, 0, n); |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3776 | buf_old_is_zero = true; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3777 | } else { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3778 | if (offset + n > old_backing_size) { |
| 3779 | n = old_backing_size - offset; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3780 | } |
| 3781 | |
Alberto Faria | a9262f5 | 2022-07-05 17:15:11 +0100 | [diff] [blame] | 3782 | ret = blk_pread(blk_old_backing, offset, n, buf_old, 0); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3783 | if (ret < 0) { |
| 3784 | error_report("error while reading from old backing file"); |
| 3785 | goto out; |
| 3786 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3787 | } |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3788 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3789 | if (offset >= new_backing_size || !blk_new_backing) { |
| 3790 | memset(buf_new, 0, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3791 | } else { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3792 | if (offset + n > new_backing_size) { |
| 3793 | n = new_backing_size - offset; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3794 | } |
| 3795 | |
Alberto Faria | a9262f5 | 2022-07-05 17:15:11 +0100 | [diff] [blame] | 3796 | ret = blk_pread(blk_new_backing, offset, n, buf_new, 0); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3797 | if (ret < 0) { |
| 3798 | error_report("error while reading from new backing file"); |
| 3799 | goto out; |
| 3800 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3801 | } |
| 3802 | |
| 3803 | /* If they differ, we need to write to the COW file */ |
| 3804 | uint64_t written = 0; |
| 3805 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3806 | while (written < n) { |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 3807 | int64_t pnum; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3808 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3809 | if (compare_buffers(buf_old + written, buf_new + written, |
| 3810 | n - written, &pnum)) |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3811 | { |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3812 | if (buf_old_is_zero) { |
| 3813 | ret = blk_pwrite_zeroes(blk, offset + written, pnum, 0); |
| 3814 | } else { |
Alberto Faria | a9262f5 | 2022-07-05 17:15:11 +0100 | [diff] [blame] | 3815 | ret = blk_pwrite(blk, offset + written, pnum, |
| 3816 | buf_old + written, 0); |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3817 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3818 | if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3819 | error_report("Error while writing to COW image: %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3820 | strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3821 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3822 | } |
| 3823 | } |
| 3824 | |
| 3825 | written += pnum; |
| 3826 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3827 | qemu_progress_print(local_progress, 100); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3828 | } |
| 3829 | } |
| 3830 | |
| 3831 | /* |
| 3832 | * Change the backing file. All clusters that are different from the old |
| 3833 | * backing file are overwritten in the COW file now, so the visible content |
| 3834 | * doesn't change when we switch the backing file. |
| 3835 | */ |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3836 | if (out_baseimg && *out_baseimg) { |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3837 | ret = bdrv_change_backing_file(unfiltered_bs, out_baseimg, out_basefmt, |
| 3838 | true); |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3839 | } else { |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3840 | ret = bdrv_change_backing_file(unfiltered_bs, NULL, NULL, false); |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3841 | } |
| 3842 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3843 | if (ret == -ENOSPC) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3844 | error_report("Could not change the backing file to '%s': No " |
| 3845 | "space left in the file header", out_baseimg); |
Eric Blake | a7cd44b | 2021-07-08 10:52:28 -0500 | [diff] [blame] | 3846 | } else if (ret == -EINVAL && out_baseimg && !out_basefmt) { |
| 3847 | error_report("Could not change the backing file to '%s': backing " |
| 3848 | "format must be specified", out_baseimg); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3849 | } else if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3850 | error_report("Could not change the backing file to '%s': %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3851 | out_baseimg, strerror(-ret)); |
| 3852 | } |
| 3853 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3854 | qemu_progress_print(100, 0); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3855 | /* |
| 3856 | * TODO At this point it is possible to check if any clusters that are |
| 3857 | * allocated in the COW file are the same in the backing file. If so, they |
| 3858 | * could be dropped from the COW file. Don't do this before switching the |
| 3859 | * backing file, in case of a crash this would lead to corruption. |
| 3860 | */ |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3861 | out: |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3862 | qemu_progress_end(); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3863 | /* Cleanup */ |
| 3864 | if (!unsafe) { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3865 | blk_unref(blk_old_backing); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3866 | blk_unref(blk_new_backing); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3867 | } |
Paolo Bonzini | 396374c | 2016-02-25 23:53:54 +0100 | [diff] [blame] | 3868 | qemu_vfree(buf_old); |
| 3869 | qemu_vfree(buf_new); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3870 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3871 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3872 | if (ret) { |
| 3873 | return 1; |
| 3874 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3875 | return 0; |
| 3876 | } |
| 3877 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3878 | static int img_resize(int argc, char **argv) |
| 3879 | { |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 3880 | Error *err = NULL; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3881 | int c, ret, relative; |
| 3882 | const char *filename, *fmt, *size; |
Max Reitz | 09c5c6d | 2019-09-18 11:51:44 +0200 | [diff] [blame] | 3883 | int64_t n, total_size, current_size; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3884 | bool quiet = false; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3885 | BlockBackend *blk = NULL; |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3886 | PreallocMode prealloc = PREALLOC_MODE_OFF; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3887 | QemuOpts *param; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3888 | |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3889 | static QemuOptsList resize_options = { |
| 3890 | .name = "resize_options", |
| 3891 | .head = QTAILQ_HEAD_INITIALIZER(resize_options.head), |
| 3892 | .desc = { |
| 3893 | { |
| 3894 | .name = BLOCK_OPT_SIZE, |
| 3895 | .type = QEMU_OPT_SIZE, |
| 3896 | .help = "Virtual disk size" |
| 3897 | }, { |
| 3898 | /* end of list */ |
| 3899 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3900 | }, |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3901 | }; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3902 | bool image_opts = false; |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3903 | bool shrink = false; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3904 | |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 3905 | /* Remove size from argv manually so that negative numbers are not treated |
| 3906 | * as options by getopt. */ |
| 3907 | if (argc < 3) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3908 | error_exit("Not enough arguments"); |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 3909 | return 1; |
| 3910 | } |
| 3911 | |
| 3912 | size = argv[--argc]; |
| 3913 | |
| 3914 | /* Parse getopt arguments */ |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3915 | fmt = NULL; |
| 3916 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3917 | static const struct option long_options[] = { |
| 3918 | {"help", no_argument, 0, 'h'}, |
| 3919 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3920 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3921 | {"preallocation", required_argument, 0, OPTION_PREALLOCATION}, |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3922 | {"shrink", no_argument, 0, OPTION_SHRINK}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3923 | {0, 0, 0, 0} |
| 3924 | }; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3925 | c = getopt_long(argc, argv, ":f:hq", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3926 | long_options, NULL); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3927 | if (c == -1) { |
| 3928 | break; |
| 3929 | } |
| 3930 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3931 | case ':': |
| 3932 | missing_argument(argv[optind - 1]); |
| 3933 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3934 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3935 | unrecognized_option(argv[optind - 1]); |
| 3936 | break; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3937 | case 'h': |
| 3938 | help(); |
| 3939 | break; |
| 3940 | case 'f': |
| 3941 | fmt = optarg; |
| 3942 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3943 | case 'q': |
| 3944 | quiet = true; |
| 3945 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 3946 | case OPTION_OBJECT: |
| 3947 | user_creatable_process_cmdline(optarg); |
| 3948 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3949 | case OPTION_IMAGE_OPTS: |
| 3950 | image_opts = true; |
| 3951 | break; |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3952 | case OPTION_PREALLOCATION: |
Marc-André Lureau | f7abe0e | 2017-08-24 10:46:10 +0200 | [diff] [blame] | 3953 | prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg, |
Markus Armbruster | 06c60b6 | 2017-08-24 10:45:57 +0200 | [diff] [blame] | 3954 | PREALLOC_MODE__MAX, NULL); |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3955 | if (prealloc == PREALLOC_MODE__MAX) { |
| 3956 | error_report("Invalid preallocation mode '%s'", optarg); |
| 3957 | return 1; |
| 3958 | } |
| 3959 | break; |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3960 | case OPTION_SHRINK: |
| 3961 | shrink = true; |
| 3962 | break; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3963 | } |
| 3964 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 3965 | if (optind != argc - 1) { |
Max Reitz | be8fbd4 | 2018-02-05 17:27:45 +0100 | [diff] [blame] | 3966 | error_exit("Expecting image file name and size"); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3967 | } |
| 3968 | filename = argv[optind++]; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3969 | |
| 3970 | /* Choose grow, shrink, or absolute resize mode */ |
| 3971 | switch (size[0]) { |
| 3972 | case '+': |
| 3973 | relative = 1; |
| 3974 | size++; |
| 3975 | break; |
| 3976 | case '-': |
| 3977 | relative = -1; |
| 3978 | size++; |
| 3979 | break; |
| 3980 | default: |
| 3981 | relative = 0; |
| 3982 | break; |
| 3983 | } |
| 3984 | |
| 3985 | /* Parse size */ |
Peter Crosthwaite | 87ea75d | 2014-01-01 18:49:17 -0800 | [diff] [blame] | 3986 | param = qemu_opts_create(&resize_options, NULL, 0, &error_abort); |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 3987 | if (!qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err)) { |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 3988 | error_report_err(err); |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3989 | ret = -1; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3990 | qemu_opts_del(param); |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3991 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3992 | } |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3993 | n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0); |
| 3994 | qemu_opts_del(param); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3995 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 3996 | blk = img_open(image_opts, filename, fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3997 | BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet, |
| 3998 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3999 | if (!blk) { |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 4000 | ret = -1; |
| 4001 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 4002 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4003 | |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 4004 | current_size = blk_getlength(blk); |
| 4005 | if (current_size < 0) { |
| 4006 | error_report("Failed to inquire current image length: %s", |
| 4007 | strerror(-current_size)); |
| 4008 | ret = -1; |
| 4009 | goto out; |
| 4010 | } |
| 4011 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4012 | if (relative) { |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 4013 | total_size = current_size + n * relative; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4014 | } else { |
| 4015 | total_size = n; |
| 4016 | } |
| 4017 | if (total_size <= 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 4018 | error_report("New image size must be positive"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 4019 | ret = -1; |
| 4020 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4021 | } |
| 4022 | |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 4023 | if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) { |
| 4024 | error_report("Preallocation can only be used for growing images"); |
| 4025 | ret = -1; |
| 4026 | goto out; |
| 4027 | } |
| 4028 | |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 4029 | if (total_size < current_size && !shrink) { |
Kevin Wolf | 1c404d7 | 2020-07-10 14:17:17 +0200 | [diff] [blame] | 4030 | error_report("Use the --shrink option to perform a shrink operation."); |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 4031 | warn_report("Shrinking an image will delete all data beyond the " |
| 4032 | "shrunken image's end. Before performing such an " |
| 4033 | "operation, make sure there is no important data there."); |
Kevin Wolf | 1c404d7 | 2020-07-10 14:17:17 +0200 | [diff] [blame] | 4034 | ret = -1; |
| 4035 | goto out; |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 4036 | } |
| 4037 | |
Max Reitz | e8d04f9 | 2019-09-18 11:51:43 +0200 | [diff] [blame] | 4038 | /* |
| 4039 | * The user expects the image to have the desired size after |
| 4040 | * resizing, so pass @exact=true. It is of no use to report |
| 4041 | * success when the image has not actually been resized. |
| 4042 | */ |
Kevin Wolf | 8c6242b | 2020-04-24 14:54:41 +0200 | [diff] [blame] | 4043 | ret = blk_truncate(blk, total_size, true, prealloc, 0, &err); |
Max Reitz | 09c5c6d | 2019-09-18 11:51:44 +0200 | [diff] [blame] | 4044 | if (!ret) { |
| 4045 | qprintf(quiet, "Image resized.\n"); |
| 4046 | } else { |
Max Reitz | ed3d2ec | 2017-03-28 22:51:27 +0200 | [diff] [blame] | 4047 | error_report_err(err); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4048 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 4049 | out: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 4050 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 4051 | if (ret) { |
| 4052 | return 1; |
| 4053 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4054 | return 0; |
| 4055 | } |
| 4056 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4057 | static void amend_status_cb(BlockDriverState *bs, |
Max Reitz | 8b13976 | 2015-07-27 17:51:32 +0200 | [diff] [blame] | 4058 | int64_t offset, int64_t total_work_size, |
| 4059 | void *opaque) |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4060 | { |
| 4061 | qemu_progress_print(100.f * offset / total_work_size, 0); |
| 4062 | } |
| 4063 | |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4064 | static int print_amend_option_help(const char *format) |
| 4065 | { |
| 4066 | BlockDriver *drv; |
| 4067 | |
| 4068 | /* Find driver and parse its options */ |
| 4069 | drv = bdrv_find_format(format); |
| 4070 | if (!drv) { |
| 4071 | error_report("Unknown file format '%s'", format); |
| 4072 | return 1; |
| 4073 | } |
| 4074 | |
| 4075 | if (!drv->bdrv_amend_options) { |
| 4076 | error_report("Format driver '%s' does not support option amendment", |
| 4077 | format); |
| 4078 | return 1; |
| 4079 | } |
| 4080 | |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4081 | /* Every driver supporting amendment must have amend_opts */ |
| 4082 | assert(drv->amend_opts); |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4083 | |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4084 | printf("Amend options for '%s':\n", format); |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4085 | qemu_opts_print_help(drv->amend_opts, false); |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4086 | return 0; |
| 4087 | } |
| 4088 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4089 | static int img_amend(int argc, char **argv) |
| 4090 | { |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 4091 | Error *err = NULL; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4092 | int c, ret = 0; |
| 4093 | char *options = NULL; |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4094 | QemuOptsList *amend_opts = NULL; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 4095 | QemuOpts *opts = NULL; |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 4096 | const char *fmt = NULL, *filename, *cache; |
| 4097 | int flags; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 4098 | bool writethrough; |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4099 | bool quiet = false, progress = false; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 4100 | BlockBackend *blk = NULL; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4101 | BlockDriverState *bs = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 4102 | bool image_opts = false; |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4103 | bool force = false; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4104 | |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 4105 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4106 | for (;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4107 | static const struct option long_options[] = { |
| 4108 | {"help", no_argument, 0, 'h'}, |
| 4109 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 4110 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4111 | {"force", no_argument, 0, OPTION_FORCE}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4112 | {0, 0, 0, 0} |
| 4113 | }; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4114 | c = getopt_long(argc, argv, ":ho:f:t:pq", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4115 | long_options, NULL); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4116 | if (c == -1) { |
| 4117 | break; |
| 4118 | } |
| 4119 | |
| 4120 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4121 | case ':': |
| 4122 | missing_argument(argv[optind - 1]); |
| 4123 | break; |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4124 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4125 | unrecognized_option(argv[optind - 1]); |
| 4126 | break; |
| 4127 | case 'h': |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4128 | help(); |
| 4129 | break; |
| 4130 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 4131 | if (accumulate_options(&options, optarg) < 0) { |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4132 | ret = -1; |
| 4133 | goto out_no_progress; |
| 4134 | } |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4135 | break; |
| 4136 | case 'f': |
| 4137 | fmt = optarg; |
| 4138 | break; |
| 4139 | case 't': |
| 4140 | cache = optarg; |
| 4141 | break; |
| 4142 | case 'p': |
| 4143 | progress = true; |
| 4144 | break; |
| 4145 | case 'q': |
| 4146 | quiet = true; |
| 4147 | break; |
| 4148 | case OPTION_OBJECT: |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 4149 | user_creatable_process_cmdline(optarg); |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4150 | break; |
| 4151 | case OPTION_IMAGE_OPTS: |
| 4152 | image_opts = true; |
| 4153 | break; |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4154 | case OPTION_FORCE: |
| 4155 | force = true; |
| 4156 | break; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4157 | } |
| 4158 | } |
| 4159 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4160 | if (!options) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 4161 | error_exit("Must specify options (-o)"); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4162 | } |
| 4163 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4164 | if (quiet) { |
| 4165 | progress = false; |
| 4166 | } |
| 4167 | qemu_progress_init(progress, 1.0); |
| 4168 | |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4169 | filename = (optind == argc - 1) ? argv[argc - 1] : NULL; |
| 4170 | if (fmt && has_help_option(options)) { |
| 4171 | /* If a format is explicitly specified (and possibly no filename is |
| 4172 | * given), print option help here */ |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4173 | ret = print_amend_option_help(fmt); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4174 | goto out; |
| 4175 | } |
| 4176 | |
| 4177 | if (optind != argc - 1) { |
Max Reitz | b2f27e4 | 2014-10-27 11:12:52 +0100 | [diff] [blame] | 4178 | error_report("Expecting one image file name"); |
| 4179 | ret = -1; |
| 4180 | goto out; |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4181 | } |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4182 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 4183 | flags = BDRV_O_RDWR; |
| 4184 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 4185 | if (ret < 0) { |
| 4186 | error_report("Invalid cache option: %s", cache); |
| 4187 | goto out; |
| 4188 | } |
| 4189 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4190 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 4191 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 4192 | if (!blk) { |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4193 | ret = -1; |
| 4194 | goto out; |
| 4195 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 4196 | bs = blk_bs(blk); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4197 | |
| 4198 | fmt = bs->drv->format_name; |
| 4199 | |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 4200 | if (has_help_option(options)) { |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4201 | /* If the format was auto-detected, print option help here */ |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4202 | ret = print_amend_option_help(fmt); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4203 | goto out; |
| 4204 | } |
| 4205 | |
Max Reitz | 1f99668 | 2018-05-09 23:00:17 +0200 | [diff] [blame] | 4206 | if (!bs->drv->bdrv_amend_options) { |
| 4207 | error_report("Format driver '%s' does not support option amendment", |
Max Reitz | b2439d2 | 2014-12-02 18:32:47 +0100 | [diff] [blame] | 4208 | fmt); |
| 4209 | ret = -1; |
| 4210 | goto out; |
| 4211 | } |
| 4212 | |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4213 | /* Every driver supporting amendment must have amend_opts */ |
| 4214 | assert(bs->drv->amend_opts); |
Max Reitz | 1f99668 | 2018-05-09 23:00:17 +0200 | [diff] [blame] | 4215 | |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4216 | amend_opts = qemu_opts_append(amend_opts, bs->drv->amend_opts); |
| 4217 | opts = qemu_opts_create(amend_opts, NULL, 0, &error_abort); |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 4218 | if (!qemu_opts_do_parse(opts, options, NULL, &err)) { |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4219 | /* Try to parse options using the create options */ |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4220 | amend_opts = qemu_opts_append(amend_opts, bs->drv->create_opts); |
| 4221 | qemu_opts_del(opts); |
| 4222 | opts = qemu_opts_create(amend_opts, NULL, 0, &error_abort); |
Markus Armbruster | 9e194e0 | 2020-07-07 18:06:11 +0200 | [diff] [blame] | 4223 | if (qemu_opts_do_parse(opts, options, NULL, NULL)) { |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4224 | error_append_hint(&err, |
| 4225 | "This option is only supported for image creation\n"); |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4226 | } |
| 4227 | |
Paolo Bonzini | ece9086 | 2017-01-04 15:56:24 +0100 | [diff] [blame] | 4228 | error_report_err(err); |
| 4229 | ret = -1; |
| 4230 | goto out; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4231 | } |
| 4232 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4233 | /* In case the driver does not call amend_status_cb() */ |
| 4234 | qemu_progress_print(0.f, 0); |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4235 | ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL, force, &err); |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4236 | qemu_progress_print(100.f, 0); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4237 | if (ret < 0) { |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 4238 | error_report_err(err); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4239 | goto out; |
| 4240 | } |
| 4241 | |
| 4242 | out: |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4243 | qemu_progress_end(); |
| 4244 | |
Max Reitz | e814dff | 2015-08-20 16:00:38 -0700 | [diff] [blame] | 4245 | out_no_progress: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 4246 | blk_unref(blk); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 4247 | qemu_opts_del(opts); |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4248 | qemu_opts_free(amend_opts); |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 4249 | g_free(options); |
| 4250 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4251 | if (ret) { |
| 4252 | return 1; |
| 4253 | } |
| 4254 | return 0; |
| 4255 | } |
| 4256 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4257 | typedef struct BenchData { |
| 4258 | BlockBackend *blk; |
| 4259 | uint64_t image_size; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4260 | bool write; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4261 | int bufsize; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4262 | int step; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4263 | int nrreq; |
| 4264 | int n; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4265 | int flush_interval; |
| 4266 | bool drain_on_flush; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4267 | uint8_t *buf; |
| 4268 | QEMUIOVector *qiov; |
| 4269 | |
| 4270 | int in_flight; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4271 | bool in_flush; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4272 | uint64_t offset; |
| 4273 | } BenchData; |
| 4274 | |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4275 | static void bench_undrained_flush_cb(void *opaque, int ret) |
| 4276 | { |
| 4277 | if (ret < 0) { |
Markus Armbruster | df3c286 | 2016-08-03 13:37:51 +0200 | [diff] [blame] | 4278 | error_report("Failed flush request: %s", strerror(-ret)); |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4279 | exit(EXIT_FAILURE); |
| 4280 | } |
| 4281 | } |
| 4282 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4283 | static void bench_cb(void *opaque, int ret) |
| 4284 | { |
| 4285 | BenchData *b = opaque; |
| 4286 | BlockAIOCB *acb; |
| 4287 | |
| 4288 | if (ret < 0) { |
Markus Armbruster | df3c286 | 2016-08-03 13:37:51 +0200 | [diff] [blame] | 4289 | error_report("Failed request: %s", strerror(-ret)); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4290 | exit(EXIT_FAILURE); |
| 4291 | } |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4292 | |
| 4293 | if (b->in_flush) { |
| 4294 | /* Just finished a flush with drained queue: Start next requests */ |
| 4295 | assert(b->in_flight == 0); |
| 4296 | b->in_flush = false; |
| 4297 | } else if (b->in_flight > 0) { |
| 4298 | int remaining = b->n - b->in_flight; |
| 4299 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4300 | b->n--; |
| 4301 | b->in_flight--; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4302 | |
| 4303 | /* Time for flush? Drain queue if requested, then flush */ |
| 4304 | if (b->flush_interval && remaining % b->flush_interval == 0) { |
| 4305 | if (!b->in_flight || !b->drain_on_flush) { |
| 4306 | BlockCompletionFunc *cb; |
| 4307 | |
| 4308 | if (b->drain_on_flush) { |
| 4309 | b->in_flush = true; |
| 4310 | cb = bench_cb; |
| 4311 | } else { |
| 4312 | cb = bench_undrained_flush_cb; |
| 4313 | } |
| 4314 | |
| 4315 | acb = blk_aio_flush(b->blk, cb, b); |
| 4316 | if (!acb) { |
| 4317 | error_report("Failed to issue flush request"); |
| 4318 | exit(EXIT_FAILURE); |
| 4319 | } |
| 4320 | } |
| 4321 | if (b->drain_on_flush) { |
| 4322 | return; |
| 4323 | } |
| 4324 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4325 | } |
| 4326 | |
| 4327 | while (b->n > b->in_flight && b->in_flight < b->nrreq) { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4328 | int64_t offset = b->offset; |
| 4329 | /* blk_aio_* might look for completed I/Os and kick bench_cb |
| 4330 | * again, so make sure this operation is counted by in_flight |
| 4331 | * and b->offset is ready for the next submission. |
| 4332 | */ |
| 4333 | b->in_flight++; |
| 4334 | b->offset += b->step; |
| 4335 | b->offset %= b->image_size; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4336 | if (b->write) { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4337 | 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] | 4338 | } else { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4339 | 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] | 4340 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4341 | if (!acb) { |
| 4342 | error_report("Failed to issue request"); |
| 4343 | exit(EXIT_FAILURE); |
| 4344 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4345 | } |
| 4346 | } |
| 4347 | |
| 4348 | static int img_bench(int argc, char **argv) |
| 4349 | { |
| 4350 | int c, ret = 0; |
| 4351 | const char *fmt = NULL, *filename; |
| 4352 | bool quiet = false; |
| 4353 | bool image_opts = false; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4354 | bool is_write = false; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4355 | int count = 75000; |
| 4356 | int depth = 64; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4357 | int64_t offset = 0; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4358 | size_t bufsize = 4096; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4359 | int pattern = 0; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4360 | size_t step = 0; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4361 | int flush_interval = 0; |
| 4362 | bool drain_on_flush = true; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4363 | int64_t image_size; |
| 4364 | BlockBackend *blk = NULL; |
| 4365 | BenchData data = {}; |
| 4366 | int flags = 0; |
Kevin Wolf | 604e861 | 2016-06-14 11:29:32 +0200 | [diff] [blame] | 4367 | bool writethrough = false; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4368 | struct timeval t1, t2; |
| 4369 | int i; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4370 | bool force_share = false; |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4371 | size_t buf_size; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4372 | |
| 4373 | for (;;) { |
| 4374 | static const struct option long_options[] = { |
| 4375 | {"help", no_argument, 0, 'h'}, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4376 | {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL}, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4377 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4378 | {"pattern", required_argument, 0, OPTION_PATTERN}, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4379 | {"no-drain", no_argument, 0, OPTION_NO_DRAIN}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4380 | {"force-share", no_argument, 0, 'U'}, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4381 | {0, 0, 0, 0} |
| 4382 | }; |
Aarushi Mehta | cdd2677 | 2020-01-20 14:18:55 +0000 | [diff] [blame] | 4383 | c = getopt_long(argc, argv, ":hc:d:f:ni:o:qs:S:t:wU", long_options, |
| 4384 | NULL); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4385 | if (c == -1) { |
| 4386 | break; |
| 4387 | } |
| 4388 | |
| 4389 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4390 | case ':': |
| 4391 | missing_argument(argv[optind - 1]); |
| 4392 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4393 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4394 | unrecognized_option(argv[optind - 1]); |
| 4395 | break; |
| 4396 | case 'h': |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4397 | help(); |
| 4398 | break; |
| 4399 | case 'c': |
| 4400 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4401 | unsigned long res; |
| 4402 | |
| 4403 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4404 | error_report("Invalid request count specified"); |
| 4405 | return 1; |
| 4406 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4407 | count = res; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4408 | break; |
| 4409 | } |
| 4410 | case 'd': |
| 4411 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4412 | unsigned long res; |
| 4413 | |
| 4414 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4415 | error_report("Invalid queue depth specified"); |
| 4416 | return 1; |
| 4417 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4418 | depth = res; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4419 | break; |
| 4420 | } |
| 4421 | case 'f': |
| 4422 | fmt = optarg; |
| 4423 | break; |
| 4424 | case 'n': |
| 4425 | flags |= BDRV_O_NATIVE_AIO; |
| 4426 | break; |
Aarushi Mehta | cdd2677 | 2020-01-20 14:18:55 +0000 | [diff] [blame] | 4427 | case 'i': |
| 4428 | ret = bdrv_parse_aio(optarg, &flags); |
| 4429 | if (ret < 0) { |
| 4430 | error_report("Invalid aio option: %s", optarg); |
| 4431 | ret = -1; |
| 4432 | goto out; |
| 4433 | } |
| 4434 | break; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4435 | case 'o': |
| 4436 | { |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4437 | offset = cvtnum("offset", optarg); |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4438 | if (offset < 0) { |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4439 | return 1; |
| 4440 | } |
| 4441 | break; |
| 4442 | } |
| 4443 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4444 | case 'q': |
| 4445 | quiet = true; |
| 4446 | break; |
| 4447 | case 's': |
| 4448 | { |
| 4449 | int64_t sval; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4450 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4451 | sval = cvtnum_full("buffer size", optarg, 0, INT_MAX); |
| 4452 | if (sval < 0) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4453 | return 1; |
| 4454 | } |
| 4455 | |
| 4456 | bufsize = sval; |
| 4457 | break; |
| 4458 | } |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4459 | case 'S': |
| 4460 | { |
| 4461 | int64_t sval; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4462 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4463 | sval = cvtnum_full("step_size", optarg, 0, INT_MAX); |
| 4464 | if (sval < 0) { |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4465 | return 1; |
| 4466 | } |
| 4467 | |
| 4468 | step = sval; |
| 4469 | break; |
| 4470 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4471 | case 't': |
| 4472 | ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough); |
| 4473 | if (ret < 0) { |
| 4474 | error_report("Invalid cache mode"); |
| 4475 | ret = -1; |
| 4476 | goto out; |
| 4477 | } |
| 4478 | break; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4479 | case 'w': |
| 4480 | flags |= BDRV_O_RDWR; |
| 4481 | is_write = true; |
| 4482 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4483 | case 'U': |
| 4484 | force_share = true; |
| 4485 | break; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4486 | case OPTION_PATTERN: |
| 4487 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4488 | unsigned long res; |
| 4489 | |
| 4490 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) { |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4491 | error_report("Invalid pattern byte specified"); |
| 4492 | return 1; |
| 4493 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4494 | pattern = res; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4495 | break; |
| 4496 | } |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4497 | case OPTION_FLUSH_INTERVAL: |
| 4498 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4499 | unsigned long res; |
| 4500 | |
| 4501 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4502 | error_report("Invalid flush interval specified"); |
| 4503 | return 1; |
| 4504 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4505 | flush_interval = res; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4506 | break; |
| 4507 | } |
| 4508 | case OPTION_NO_DRAIN: |
| 4509 | drain_on_flush = false; |
| 4510 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4511 | case OPTION_IMAGE_OPTS: |
| 4512 | image_opts = true; |
| 4513 | break; |
| 4514 | } |
| 4515 | } |
| 4516 | |
| 4517 | if (optind != argc - 1) { |
| 4518 | error_exit("Expecting one image file name"); |
| 4519 | } |
| 4520 | filename = argv[argc - 1]; |
| 4521 | |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4522 | if (!is_write && flush_interval) { |
| 4523 | error_report("--flush-interval is only available in write tests"); |
| 4524 | ret = -1; |
| 4525 | goto out; |
| 4526 | } |
| 4527 | if (flush_interval && flush_interval < depth) { |
| 4528 | error_report("Flush interval can't be smaller than depth"); |
| 4529 | ret = -1; |
| 4530 | goto out; |
| 4531 | } |
| 4532 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4533 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 4534 | force_share); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4535 | if (!blk) { |
| 4536 | ret = -1; |
| 4537 | goto out; |
| 4538 | } |
| 4539 | |
| 4540 | image_size = blk_getlength(blk); |
| 4541 | if (image_size < 0) { |
| 4542 | ret = image_size; |
| 4543 | goto out; |
| 4544 | } |
| 4545 | |
| 4546 | data = (BenchData) { |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4547 | .blk = blk, |
| 4548 | .image_size = image_size, |
| 4549 | .bufsize = bufsize, |
| 4550 | .step = step ?: bufsize, |
| 4551 | .nrreq = depth, |
| 4552 | .n = count, |
| 4553 | .offset = offset, |
| 4554 | .write = is_write, |
| 4555 | .flush_interval = flush_interval, |
| 4556 | .drain_on_flush = drain_on_flush, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4557 | }; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4558 | printf("Sending %d %s requests, %d bytes each, %d in parallel " |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4559 | "(starting at offset %" PRId64 ", step size %d)\n", |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4560 | data.n, data.write ? "write" : "read", data.bufsize, data.nrreq, |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4561 | data.offset, data.step); |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4562 | if (flush_interval) { |
| 4563 | printf("Sending flush every %d requests\n", flush_interval); |
| 4564 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4565 | |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4566 | buf_size = data.nrreq * data.bufsize; |
| 4567 | data.buf = blk_blockalign(blk, buf_size); |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4568 | memset(data.buf, pattern, data.nrreq * data.bufsize); |
| 4569 | |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4570 | blk_register_buf(blk, data.buf, buf_size); |
| 4571 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4572 | data.qiov = g_new(QEMUIOVector, data.nrreq); |
| 4573 | for (i = 0; i < data.nrreq; i++) { |
| 4574 | qemu_iovec_init(&data.qiov[i], 1); |
| 4575 | qemu_iovec_add(&data.qiov[i], |
| 4576 | data.buf + i * data.bufsize, data.bufsize); |
| 4577 | } |
| 4578 | |
| 4579 | gettimeofday(&t1, NULL); |
| 4580 | bench_cb(&data, 0); |
| 4581 | |
| 4582 | while (data.n > 0) { |
| 4583 | main_loop_wait(false); |
| 4584 | } |
| 4585 | gettimeofday(&t2, NULL); |
| 4586 | |
| 4587 | printf("Run completed in %3.3f seconds.\n", |
| 4588 | (t2.tv_sec - t1.tv_sec) |
| 4589 | + ((double)(t2.tv_usec - t1.tv_usec) / 1000000)); |
| 4590 | |
| 4591 | out: |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4592 | if (data.buf) { |
| 4593 | blk_unregister_buf(blk, data.buf); |
| 4594 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4595 | qemu_vfree(data.buf); |
| 4596 | blk_unref(blk); |
| 4597 | |
| 4598 | if (ret) { |
| 4599 | return 1; |
| 4600 | } |
| 4601 | return 0; |
| 4602 | } |
| 4603 | |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4604 | enum ImgBitmapAct { |
| 4605 | BITMAP_ADD, |
| 4606 | BITMAP_REMOVE, |
| 4607 | BITMAP_CLEAR, |
| 4608 | BITMAP_ENABLE, |
| 4609 | BITMAP_DISABLE, |
| 4610 | BITMAP_MERGE, |
| 4611 | }; |
| 4612 | typedef struct ImgBitmapAction { |
| 4613 | enum ImgBitmapAct act; |
| 4614 | const char *src; /* only used for merge */ |
| 4615 | QSIMPLEQ_ENTRY(ImgBitmapAction) next; |
| 4616 | } ImgBitmapAction; |
| 4617 | |
| 4618 | static int img_bitmap(int argc, char **argv) |
| 4619 | { |
| 4620 | Error *err = NULL; |
| 4621 | int c, ret = 1; |
| 4622 | QemuOpts *opts = NULL; |
| 4623 | const char *fmt = NULL, *src_fmt = NULL, *src_filename = NULL; |
| 4624 | const char *filename, *bitmap; |
| 4625 | BlockBackend *blk = NULL, *src = NULL; |
| 4626 | BlockDriverState *bs = NULL, *src_bs = NULL; |
| 4627 | bool image_opts = false; |
| 4628 | int64_t granularity = 0; |
| 4629 | bool add = false, merge = false; |
| 4630 | QSIMPLEQ_HEAD(, ImgBitmapAction) actions; |
| 4631 | ImgBitmapAction *act, *act_next; |
| 4632 | const char *op; |
| 4633 | |
| 4634 | QSIMPLEQ_INIT(&actions); |
| 4635 | |
| 4636 | for (;;) { |
| 4637 | static const struct option long_options[] = { |
| 4638 | {"help", no_argument, 0, 'h'}, |
| 4639 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 4640 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
| 4641 | {"add", no_argument, 0, OPTION_ADD}, |
| 4642 | {"remove", no_argument, 0, OPTION_REMOVE}, |
| 4643 | {"clear", no_argument, 0, OPTION_CLEAR}, |
| 4644 | {"enable", no_argument, 0, OPTION_ENABLE}, |
| 4645 | {"disable", no_argument, 0, OPTION_DISABLE}, |
| 4646 | {"merge", required_argument, 0, OPTION_MERGE}, |
| 4647 | {"granularity", required_argument, 0, 'g'}, |
| 4648 | {"source-file", required_argument, 0, 'b'}, |
| 4649 | {"source-format", required_argument, 0, 'F'}, |
| 4650 | {0, 0, 0, 0} |
| 4651 | }; |
| 4652 | c = getopt_long(argc, argv, ":b:f:F:g:h", long_options, NULL); |
| 4653 | if (c == -1) { |
| 4654 | break; |
| 4655 | } |
| 4656 | |
| 4657 | switch (c) { |
| 4658 | case ':': |
| 4659 | missing_argument(argv[optind - 1]); |
| 4660 | break; |
| 4661 | case '?': |
| 4662 | unrecognized_option(argv[optind - 1]); |
| 4663 | break; |
| 4664 | case 'h': |
| 4665 | help(); |
| 4666 | break; |
| 4667 | case 'b': |
| 4668 | src_filename = optarg; |
| 4669 | break; |
| 4670 | case 'f': |
| 4671 | fmt = optarg; |
| 4672 | break; |
| 4673 | case 'F': |
| 4674 | src_fmt = optarg; |
| 4675 | break; |
| 4676 | case 'g': |
| 4677 | granularity = cvtnum("granularity", optarg); |
| 4678 | if (granularity < 0) { |
| 4679 | return 1; |
| 4680 | } |
| 4681 | break; |
| 4682 | case OPTION_ADD: |
| 4683 | act = g_new0(ImgBitmapAction, 1); |
| 4684 | act->act = BITMAP_ADD; |
| 4685 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4686 | add = true; |
| 4687 | break; |
| 4688 | case OPTION_REMOVE: |
| 4689 | act = g_new0(ImgBitmapAction, 1); |
| 4690 | act->act = BITMAP_REMOVE; |
| 4691 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4692 | break; |
| 4693 | case OPTION_CLEAR: |
| 4694 | act = g_new0(ImgBitmapAction, 1); |
| 4695 | act->act = BITMAP_CLEAR; |
| 4696 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4697 | break; |
| 4698 | case OPTION_ENABLE: |
| 4699 | act = g_new0(ImgBitmapAction, 1); |
| 4700 | act->act = BITMAP_ENABLE; |
| 4701 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4702 | break; |
| 4703 | case OPTION_DISABLE: |
| 4704 | act = g_new0(ImgBitmapAction, 1); |
| 4705 | act->act = BITMAP_DISABLE; |
| 4706 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4707 | break; |
| 4708 | case OPTION_MERGE: |
| 4709 | act = g_new0(ImgBitmapAction, 1); |
| 4710 | act->act = BITMAP_MERGE; |
| 4711 | act->src = optarg; |
| 4712 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4713 | merge = true; |
| 4714 | break; |
| 4715 | case OPTION_OBJECT: |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 4716 | user_creatable_process_cmdline(optarg); |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4717 | break; |
| 4718 | case OPTION_IMAGE_OPTS: |
| 4719 | image_opts = true; |
| 4720 | break; |
| 4721 | } |
| 4722 | } |
| 4723 | |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4724 | if (QSIMPLEQ_EMPTY(&actions)) { |
| 4725 | error_report("Need at least one of --add, --remove, --clear, " |
| 4726 | "--enable, --disable, or --merge"); |
| 4727 | goto out; |
| 4728 | } |
| 4729 | |
| 4730 | if (granularity && !add) { |
| 4731 | error_report("granularity only supported with --add"); |
| 4732 | goto out; |
| 4733 | } |
| 4734 | if (src_fmt && !src_filename) { |
| 4735 | error_report("-F only supported with -b"); |
| 4736 | goto out; |
| 4737 | } |
| 4738 | if (src_filename && !merge) { |
| 4739 | error_report("Merge bitmap source file only supported with " |
| 4740 | "--merge"); |
| 4741 | goto out; |
| 4742 | } |
| 4743 | |
| 4744 | if (optind != argc - 2) { |
| 4745 | error_report("Expecting filename and bitmap name"); |
| 4746 | goto out; |
| 4747 | } |
| 4748 | |
| 4749 | filename = argv[optind]; |
| 4750 | bitmap = argv[optind + 1]; |
| 4751 | |
Eric Blake | 14f16bf | 2020-09-14 14:10:09 -0500 | [diff] [blame] | 4752 | /* |
| 4753 | * No need to open backing chains; we will be manipulating bitmaps |
| 4754 | * directly in this image without reference to image contents. |
| 4755 | */ |
| 4756 | blk = img_open(image_opts, filename, fmt, BDRV_O_RDWR | BDRV_O_NO_BACKING, |
| 4757 | false, false, false); |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4758 | if (!blk) { |
| 4759 | goto out; |
| 4760 | } |
| 4761 | bs = blk_bs(blk); |
| 4762 | if (src_filename) { |
Eric Blake | 14f16bf | 2020-09-14 14:10:09 -0500 | [diff] [blame] | 4763 | src = img_open(false, src_filename, src_fmt, BDRV_O_NO_BACKING, |
| 4764 | false, false, false); |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4765 | if (!src) { |
| 4766 | goto out; |
| 4767 | } |
| 4768 | src_bs = blk_bs(src); |
| 4769 | } else { |
| 4770 | src_bs = bs; |
| 4771 | } |
| 4772 | |
| 4773 | QSIMPLEQ_FOREACH_SAFE(act, &actions, next, act_next) { |
| 4774 | switch (act->act) { |
| 4775 | case BITMAP_ADD: |
| 4776 | qmp_block_dirty_bitmap_add(bs->node_name, bitmap, |
| 4777 | !!granularity, granularity, true, true, |
| 4778 | false, false, &err); |
| 4779 | op = "add"; |
| 4780 | break; |
| 4781 | case BITMAP_REMOVE: |
| 4782 | qmp_block_dirty_bitmap_remove(bs->node_name, bitmap, &err); |
| 4783 | op = "remove"; |
| 4784 | break; |
| 4785 | case BITMAP_CLEAR: |
| 4786 | qmp_block_dirty_bitmap_clear(bs->node_name, bitmap, &err); |
| 4787 | op = "clear"; |
| 4788 | break; |
| 4789 | case BITMAP_ENABLE: |
| 4790 | qmp_block_dirty_bitmap_enable(bs->node_name, bitmap, &err); |
| 4791 | op = "enable"; |
| 4792 | break; |
| 4793 | case BITMAP_DISABLE: |
| 4794 | qmp_block_dirty_bitmap_disable(bs->node_name, bitmap, &err); |
| 4795 | op = "disable"; |
| 4796 | break; |
Eric Blake | 6c729dd | 2020-05-21 14:21:35 -0500 | [diff] [blame] | 4797 | case BITMAP_MERGE: |
| 4798 | do_dirty_bitmap_merge(bs->node_name, bitmap, src_bs->node_name, |
| 4799 | act->src, &err); |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4800 | op = "merge"; |
| 4801 | break; |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4802 | default: |
| 4803 | g_assert_not_reached(); |
| 4804 | } |
| 4805 | |
| 4806 | if (err) { |
| 4807 | error_reportf_err(err, "Operation %s on bitmap %s failed: ", |
| 4808 | op, bitmap); |
| 4809 | goto out; |
| 4810 | } |
| 4811 | g_free(act); |
| 4812 | } |
| 4813 | |
| 4814 | ret = 0; |
| 4815 | |
| 4816 | out: |
| 4817 | blk_unref(src); |
| 4818 | blk_unref(blk); |
| 4819 | qemu_opts_del(opts); |
| 4820 | return ret; |
| 4821 | } |
| 4822 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4823 | #define C_BS 01 |
| 4824 | #define C_COUNT 02 |
| 4825 | #define C_IF 04 |
| 4826 | #define C_OF 010 |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4827 | #define C_SKIP 020 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4828 | |
| 4829 | struct DdInfo { |
| 4830 | unsigned int flags; |
| 4831 | int64_t count; |
| 4832 | }; |
| 4833 | |
| 4834 | struct DdIo { |
| 4835 | int bsz; /* Block size */ |
| 4836 | char *filename; |
| 4837 | uint8_t *buf; |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4838 | int64_t offset; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4839 | }; |
| 4840 | |
| 4841 | struct DdOpts { |
| 4842 | const char *name; |
| 4843 | int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *); |
| 4844 | unsigned int flag; |
| 4845 | }; |
| 4846 | |
| 4847 | static int img_dd_bs(const char *arg, |
| 4848 | struct DdIo *in, struct DdIo *out, |
| 4849 | struct DdInfo *dd) |
| 4850 | { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4851 | int64_t res; |
| 4852 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4853 | res = cvtnum_full("bs", arg, 1, INT_MAX); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4854 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4855 | if (res < 0) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4856 | return 1; |
| 4857 | } |
| 4858 | in->bsz = out->bsz = res; |
| 4859 | |
| 4860 | return 0; |
| 4861 | } |
| 4862 | |
| 4863 | static int img_dd_count(const char *arg, |
| 4864 | struct DdIo *in, struct DdIo *out, |
| 4865 | struct DdInfo *dd) |
| 4866 | { |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4867 | dd->count = cvtnum("count", arg); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4868 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4869 | if (dd->count < 0) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4870 | return 1; |
| 4871 | } |
| 4872 | |
| 4873 | return 0; |
| 4874 | } |
| 4875 | |
| 4876 | static int img_dd_if(const char *arg, |
| 4877 | struct DdIo *in, struct DdIo *out, |
| 4878 | struct DdInfo *dd) |
| 4879 | { |
| 4880 | in->filename = g_strdup(arg); |
| 4881 | |
| 4882 | return 0; |
| 4883 | } |
| 4884 | |
| 4885 | static int img_dd_of(const char *arg, |
| 4886 | struct DdIo *in, struct DdIo *out, |
| 4887 | struct DdInfo *dd) |
| 4888 | { |
| 4889 | out->filename = g_strdup(arg); |
| 4890 | |
| 4891 | return 0; |
| 4892 | } |
| 4893 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4894 | static int img_dd_skip(const char *arg, |
| 4895 | struct DdIo *in, struct DdIo *out, |
| 4896 | struct DdInfo *dd) |
| 4897 | { |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4898 | in->offset = cvtnum("skip", arg); |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4899 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4900 | if (in->offset < 0) { |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4901 | return 1; |
| 4902 | } |
| 4903 | |
| 4904 | return 0; |
| 4905 | } |
| 4906 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4907 | static int img_dd(int argc, char **argv) |
| 4908 | { |
| 4909 | int ret = 0; |
| 4910 | char *arg = NULL; |
| 4911 | char *tmp; |
| 4912 | BlockDriver *drv = NULL, *proto_drv = NULL; |
| 4913 | BlockBackend *blk1 = NULL, *blk2 = NULL; |
| 4914 | QemuOpts *opts = NULL; |
| 4915 | QemuOptsList *create_opts = NULL; |
| 4916 | Error *local_err = NULL; |
| 4917 | bool image_opts = false; |
| 4918 | int c, i; |
| 4919 | const char *out_fmt = "raw"; |
| 4920 | const char *fmt = NULL; |
| 4921 | int64_t size = 0; |
| 4922 | int64_t block_count = 0, out_pos, in_pos; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4923 | bool force_share = false; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4924 | struct DdInfo dd = { |
| 4925 | .flags = 0, |
| 4926 | .count = 0, |
| 4927 | }; |
| 4928 | struct DdIo in = { |
| 4929 | .bsz = 512, /* Block size is by default 512 bytes */ |
| 4930 | .filename = NULL, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4931 | .buf = NULL, |
| 4932 | .offset = 0 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4933 | }; |
| 4934 | struct DdIo out = { |
| 4935 | .bsz = 512, |
| 4936 | .filename = NULL, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4937 | .buf = NULL, |
| 4938 | .offset = 0 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4939 | }; |
| 4940 | |
| 4941 | const struct DdOpts options[] = { |
| 4942 | { "bs", img_dd_bs, C_BS }, |
| 4943 | { "count", img_dd_count, C_COUNT }, |
| 4944 | { "if", img_dd_if, C_IF }, |
| 4945 | { "of", img_dd_of, C_OF }, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4946 | { "skip", img_dd_skip, C_SKIP }, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4947 | { NULL, NULL, 0 } |
| 4948 | }; |
| 4949 | const struct option long_options[] = { |
| 4950 | { "help", no_argument, 0, 'h'}, |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 4951 | { "object", required_argument, 0, OPTION_OBJECT}, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4952 | { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4953 | { "force-share", no_argument, 0, 'U'}, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4954 | { 0, 0, 0, 0 } |
| 4955 | }; |
| 4956 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4957 | while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4958 | if (c == EOF) { |
| 4959 | break; |
| 4960 | } |
| 4961 | switch (c) { |
| 4962 | case 'O': |
| 4963 | out_fmt = optarg; |
| 4964 | break; |
| 4965 | case 'f': |
| 4966 | fmt = optarg; |
| 4967 | break; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4968 | case ':': |
| 4969 | missing_argument(argv[optind - 1]); |
| 4970 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4971 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4972 | unrecognized_option(argv[optind - 1]); |
| 4973 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4974 | case 'h': |
| 4975 | help(); |
| 4976 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4977 | case 'U': |
| 4978 | force_share = true; |
| 4979 | break; |
Stefan Hajnoczi | 2a24570 | 2017-06-19 16:00:02 +0100 | [diff] [blame] | 4980 | case OPTION_OBJECT: |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 4981 | user_creatable_process_cmdline(optarg); |
Stefan Hajnoczi | 2a24570 | 2017-06-19 16:00:02 +0100 | [diff] [blame] | 4982 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4983 | case OPTION_IMAGE_OPTS: |
| 4984 | image_opts = true; |
| 4985 | break; |
| 4986 | } |
| 4987 | } |
| 4988 | |
| 4989 | for (i = optind; i < argc; i++) { |
| 4990 | int j; |
| 4991 | arg = g_strdup(argv[i]); |
| 4992 | |
| 4993 | tmp = strchr(arg, '='); |
| 4994 | if (tmp == NULL) { |
| 4995 | error_report("unrecognized operand %s", arg); |
| 4996 | ret = -1; |
| 4997 | goto out; |
| 4998 | } |
| 4999 | |
| 5000 | *tmp++ = '\0'; |
| 5001 | |
| 5002 | for (j = 0; options[j].name != NULL; j++) { |
| 5003 | if (!strcmp(arg, options[j].name)) { |
| 5004 | break; |
| 5005 | } |
| 5006 | } |
| 5007 | if (options[j].name == NULL) { |
| 5008 | error_report("unrecognized operand %s", arg); |
| 5009 | ret = -1; |
| 5010 | goto out; |
| 5011 | } |
| 5012 | |
| 5013 | if (options[j].f(tmp, &in, &out, &dd) != 0) { |
| 5014 | ret = -1; |
| 5015 | goto out; |
| 5016 | } |
| 5017 | dd.flags |= options[j].flag; |
| 5018 | g_free(arg); |
| 5019 | arg = NULL; |
| 5020 | } |
| 5021 | |
| 5022 | if (!(dd.flags & C_IF && dd.flags & C_OF)) { |
| 5023 | error_report("Must specify both input and output files"); |
| 5024 | ret = -1; |
| 5025 | goto out; |
| 5026 | } |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 5027 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 5028 | blk1 = img_open(image_opts, in.filename, fmt, 0, false, false, |
| 5029 | force_share); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5030 | |
| 5031 | if (!blk1) { |
| 5032 | ret = -1; |
| 5033 | goto out; |
| 5034 | } |
| 5035 | |
| 5036 | drv = bdrv_find_format(out_fmt); |
| 5037 | if (!drv) { |
| 5038 | error_report("Unknown file format"); |
| 5039 | ret = -1; |
| 5040 | goto out; |
| 5041 | } |
| 5042 | proto_drv = bdrv_find_protocol(out.filename, true, &local_err); |
| 5043 | |
| 5044 | if (!proto_drv) { |
| 5045 | error_report_err(local_err); |
| 5046 | ret = -1; |
| 5047 | goto out; |
| 5048 | } |
| 5049 | if (!drv->create_opts) { |
| 5050 | error_report("Format driver '%s' does not support image creation", |
| 5051 | drv->format_name); |
| 5052 | ret = -1; |
| 5053 | goto out; |
| 5054 | } |
| 5055 | if (!proto_drv->create_opts) { |
| 5056 | error_report("Protocol driver '%s' does not support image creation", |
| 5057 | proto_drv->format_name); |
| 5058 | ret = -1; |
| 5059 | goto out; |
| 5060 | } |
| 5061 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 5062 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
| 5063 | |
| 5064 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
| 5065 | |
| 5066 | size = blk_getlength(blk1); |
| 5067 | if (size < 0) { |
| 5068 | error_report("Failed to get size for '%s'", in.filename); |
| 5069 | ret = -1; |
| 5070 | goto out; |
| 5071 | } |
| 5072 | |
| 5073 | if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz && |
| 5074 | dd.count * in.bsz < size) { |
| 5075 | size = dd.count * in.bsz; |
| 5076 | } |
| 5077 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 5078 | /* Overflow means the specified offset is beyond input image's size */ |
| 5079 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || |
| 5080 | size < in.bsz * in.offset)) { |
| 5081 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort); |
| 5082 | } else { |
| 5083 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, |
| 5084 | size - in.bsz * in.offset, &error_abort); |
| 5085 | } |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5086 | |
| 5087 | ret = bdrv_create(drv, out.filename, opts, &local_err); |
| 5088 | if (ret < 0) { |
| 5089 | error_reportf_err(local_err, |
| 5090 | "%s: error while creating output image: ", |
| 5091 | out.filename); |
| 5092 | ret = -1; |
| 5093 | goto out; |
| 5094 | } |
| 5095 | |
Daniel P. Berrange | ea204dd | 2017-05-15 17:47:10 +0100 | [diff] [blame] | 5096 | /* TODO, we can't honour --image-opts for the target, |
| 5097 | * since it needs to be given in a format compatible |
| 5098 | * with the bdrv_create() call above which does not |
| 5099 | * support image-opts style. |
| 5100 | */ |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 5101 | 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] | 5102 | false, false, false); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5103 | |
| 5104 | if (!blk2) { |
| 5105 | ret = -1; |
| 5106 | goto out; |
| 5107 | } |
| 5108 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 5109 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || |
| 5110 | size < in.offset * in.bsz)) { |
| 5111 | /* We give a warning if the skip option is bigger than the input |
| 5112 | * size and create an empty output disk image (i.e. like dd(1)). |
| 5113 | */ |
| 5114 | error_report("%s: cannot skip to specified offset", in.filename); |
| 5115 | in_pos = size; |
| 5116 | } else { |
| 5117 | in_pos = in.offset * in.bsz; |
| 5118 | } |
| 5119 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5120 | in.buf = g_new(uint8_t, in.bsz); |
| 5121 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 5122 | for (out_pos = 0; in_pos < size; block_count++) { |
Alberto Faria | bf5b16f | 2022-07-05 17:15:09 +0100 | [diff] [blame] | 5123 | int bytes = (in_pos + in.bsz > size) ? size - in_pos : in.bsz; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5124 | |
Alberto Faria | a9262f5 | 2022-07-05 17:15:11 +0100 | [diff] [blame] | 5125 | ret = blk_pread(blk1, in_pos, bytes, in.buf, 0); |
Alberto Faria | bf5b16f | 2022-07-05 17:15:09 +0100 | [diff] [blame] | 5126 | if (ret < 0) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5127 | error_report("error while reading from input image file: %s", |
Alberto Faria | bf5b16f | 2022-07-05 17:15:09 +0100 | [diff] [blame] | 5128 | strerror(-ret)); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5129 | goto out; |
| 5130 | } |
Alberto Faria | bf5b16f | 2022-07-05 17:15:09 +0100 | [diff] [blame] | 5131 | in_pos += bytes; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5132 | |
Alberto Faria | a9262f5 | 2022-07-05 17:15:11 +0100 | [diff] [blame] | 5133 | ret = blk_pwrite(blk2, out_pos, bytes, in.buf, 0); |
Alberto Faria | bf5b16f | 2022-07-05 17:15:09 +0100 | [diff] [blame] | 5134 | if (ret < 0) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5135 | error_report("error while writing to output image file: %s", |
Alberto Faria | bf5b16f | 2022-07-05 17:15:09 +0100 | [diff] [blame] | 5136 | strerror(-ret)); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5137 | goto out; |
| 5138 | } |
Alberto Faria | bf5b16f | 2022-07-05 17:15:09 +0100 | [diff] [blame] | 5139 | out_pos += bytes; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5140 | } |
| 5141 | |
| 5142 | out: |
| 5143 | g_free(arg); |
| 5144 | qemu_opts_del(opts); |
| 5145 | qemu_opts_free(create_opts); |
| 5146 | blk_unref(blk1); |
| 5147 | blk_unref(blk2); |
| 5148 | g_free(in.filename); |
| 5149 | g_free(out.filename); |
| 5150 | g_free(in.buf); |
| 5151 | g_free(out.buf); |
| 5152 | |
| 5153 | if (ret) { |
| 5154 | return 1; |
| 5155 | } |
| 5156 | return 0; |
| 5157 | } |
| 5158 | |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5159 | static void dump_json_block_measure_info(BlockMeasureInfo *info) |
| 5160 | { |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 5161 | GString *str; |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5162 | QObject *obj; |
| 5163 | Visitor *v = qobject_output_visitor_new(&obj); |
| 5164 | |
| 5165 | visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort); |
| 5166 | visit_complete(v, &obj); |
Markus Armbruster | 6589f45 | 2020-12-11 18:11:35 +0100 | [diff] [blame] | 5167 | str = qobject_to_json_pretty(obj, true); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5168 | assert(str != NULL); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 5169 | printf("%s\n", str->str); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 5170 | qobject_unref(obj); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5171 | visit_free(v); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 5172 | g_string_free(str, true); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5173 | } |
| 5174 | |
| 5175 | static int img_measure(int argc, char **argv) |
| 5176 | { |
| 5177 | static const struct option long_options[] = { |
| 5178 | {"help", no_argument, 0, 'h'}, |
| 5179 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
| 5180 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 5181 | {"output", required_argument, 0, OPTION_OUTPUT}, |
| 5182 | {"size", required_argument, 0, OPTION_SIZE}, |
| 5183 | {"force-share", no_argument, 0, 'U'}, |
| 5184 | {0, 0, 0, 0} |
| 5185 | }; |
| 5186 | OutputFormat output_format = OFORMAT_HUMAN; |
| 5187 | BlockBackend *in_blk = NULL; |
| 5188 | BlockDriver *drv; |
| 5189 | const char *filename = NULL; |
| 5190 | const char *fmt = NULL; |
| 5191 | const char *out_fmt = "raw"; |
| 5192 | char *options = NULL; |
| 5193 | char *snapshot_name = NULL; |
| 5194 | bool force_share = false; |
| 5195 | QemuOpts *opts = NULL; |
| 5196 | QemuOpts *object_opts = NULL; |
| 5197 | QemuOpts *sn_opts = NULL; |
| 5198 | QemuOptsList *create_opts = NULL; |
| 5199 | bool image_opts = false; |
| 5200 | uint64_t img_size = UINT64_MAX; |
| 5201 | BlockMeasureInfo *info = NULL; |
| 5202 | Error *local_err = NULL; |
| 5203 | int ret = 1; |
| 5204 | int c; |
| 5205 | |
| 5206 | while ((c = getopt_long(argc, argv, "hf:O:o:l:U", |
| 5207 | long_options, NULL)) != -1) { |
| 5208 | switch (c) { |
| 5209 | case '?': |
| 5210 | case 'h': |
| 5211 | help(); |
| 5212 | break; |
| 5213 | case 'f': |
| 5214 | fmt = optarg; |
| 5215 | break; |
| 5216 | case 'O': |
| 5217 | out_fmt = optarg; |
| 5218 | break; |
| 5219 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 5220 | if (accumulate_options(&options, optarg) < 0) { |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5221 | goto out; |
| 5222 | } |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5223 | break; |
| 5224 | case 'l': |
| 5225 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { |
| 5226 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
| 5227 | optarg, false); |
| 5228 | if (!sn_opts) { |
| 5229 | error_report("Failed in parsing snapshot param '%s'", |
| 5230 | optarg); |
| 5231 | goto out; |
| 5232 | } |
| 5233 | } else { |
| 5234 | snapshot_name = optarg; |
| 5235 | } |
| 5236 | break; |
| 5237 | case 'U': |
| 5238 | force_share = true; |
| 5239 | break; |
| 5240 | case OPTION_OBJECT: |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 5241 | user_creatable_process_cmdline(optarg); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5242 | break; |
| 5243 | case OPTION_IMAGE_OPTS: |
| 5244 | image_opts = true; |
| 5245 | break; |
| 5246 | case OPTION_OUTPUT: |
| 5247 | if (!strcmp(optarg, "json")) { |
| 5248 | output_format = OFORMAT_JSON; |
| 5249 | } else if (!strcmp(optarg, "human")) { |
| 5250 | output_format = OFORMAT_HUMAN; |
| 5251 | } else { |
| 5252 | error_report("--output must be used with human or json " |
| 5253 | "as argument."); |
| 5254 | goto out; |
| 5255 | } |
| 5256 | break; |
| 5257 | case OPTION_SIZE: |
| 5258 | { |
| 5259 | int64_t sval; |
| 5260 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 5261 | sval = cvtnum("image size", optarg); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5262 | if (sval < 0) { |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5263 | goto out; |
| 5264 | } |
| 5265 | img_size = (uint64_t)sval; |
| 5266 | } |
| 5267 | break; |
| 5268 | } |
| 5269 | } |
| 5270 | |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5271 | if (argc - optind > 1) { |
| 5272 | error_report("At most one filename argument is allowed."); |
| 5273 | goto out; |
| 5274 | } else if (argc - optind == 1) { |
| 5275 | filename = argv[optind]; |
| 5276 | } |
| 5277 | |
Stefan Hajnoczi | c3673dc | 2020-02-21 11:25:21 +0000 | [diff] [blame] | 5278 | if (!filename && (image_opts || fmt || snapshot_name || sn_opts)) { |
| 5279 | error_report("--image-opts, -f, and -l require a filename argument."); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5280 | goto out; |
| 5281 | } |
| 5282 | if (filename && img_size != UINT64_MAX) { |
| 5283 | error_report("--size N cannot be used together with a filename."); |
| 5284 | goto out; |
| 5285 | } |
| 5286 | if (!filename && img_size == UINT64_MAX) { |
| 5287 | error_report("Either --size N or one filename must be specified."); |
| 5288 | goto out; |
| 5289 | } |
| 5290 | |
| 5291 | if (filename) { |
| 5292 | in_blk = img_open(image_opts, filename, fmt, 0, |
| 5293 | false, false, force_share); |
| 5294 | if (!in_blk) { |
| 5295 | goto out; |
| 5296 | } |
| 5297 | |
| 5298 | if (sn_opts) { |
| 5299 | bdrv_snapshot_load_tmp(blk_bs(in_blk), |
| 5300 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), |
| 5301 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), |
| 5302 | &local_err); |
| 5303 | } else if (snapshot_name != NULL) { |
| 5304 | bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk), |
| 5305 | snapshot_name, &local_err); |
| 5306 | } |
| 5307 | if (local_err) { |
| 5308 | error_reportf_err(local_err, "Failed to load snapshot: "); |
| 5309 | goto out; |
| 5310 | } |
| 5311 | } |
| 5312 | |
| 5313 | drv = bdrv_find_format(out_fmt); |
| 5314 | if (!drv) { |
| 5315 | error_report("Unknown file format '%s'", out_fmt); |
| 5316 | goto out; |
| 5317 | } |
| 5318 | if (!drv->create_opts) { |
| 5319 | error_report("Format driver '%s' does not support image creation", |
| 5320 | drv->format_name); |
| 5321 | goto out; |
| 5322 | } |
| 5323 | |
| 5324 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 5325 | create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts); |
| 5326 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
| 5327 | if (options) { |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 5328 | if (!qemu_opts_do_parse(opts, options, NULL, &local_err)) { |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5329 | error_report_err(local_err); |
| 5330 | error_report("Invalid options for file format '%s'", out_fmt); |
| 5331 | goto out; |
| 5332 | } |
| 5333 | } |
| 5334 | if (img_size != UINT64_MAX) { |
| 5335 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort); |
| 5336 | } |
| 5337 | |
| 5338 | info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err); |
| 5339 | if (local_err) { |
| 5340 | error_report_err(local_err); |
| 5341 | goto out; |
| 5342 | } |
| 5343 | |
| 5344 | if (output_format == OFORMAT_HUMAN) { |
| 5345 | printf("required size: %" PRIu64 "\n", info->required); |
| 5346 | printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated); |
Eric Blake | 5d72c68 | 2020-05-21 14:21:34 -0500 | [diff] [blame] | 5347 | if (info->has_bitmaps) { |
| 5348 | printf("bitmaps size: %" PRIu64 "\n", info->bitmaps); |
| 5349 | } |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5350 | } else { |
| 5351 | dump_json_block_measure_info(info); |
| 5352 | } |
| 5353 | |
| 5354 | ret = 0; |
| 5355 | |
| 5356 | out: |
| 5357 | qapi_free_BlockMeasureInfo(info); |
| 5358 | qemu_opts_del(object_opts); |
| 5359 | qemu_opts_del(opts); |
| 5360 | qemu_opts_del(sn_opts); |
| 5361 | qemu_opts_free(create_opts); |
| 5362 | g_free(options); |
| 5363 | blk_unref(in_blk); |
| 5364 | return ret; |
| 5365 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 5366 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 5367 | static const img_cmd_t img_cmds[] = { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5368 | #define DEF(option, callback, arg_string) \ |
| 5369 | { option, callback }, |
| 5370 | #include "qemu-img-cmds.h" |
| 5371 | #undef DEF |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5372 | { NULL, NULL, }, |
| 5373 | }; |
| 5374 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5375 | int main(int argc, char **argv) |
| 5376 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 5377 | const img_cmd_t *cmd; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5378 | const char *cmdname; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5379 | int c; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5380 | static const struct option long_options[] = { |
| 5381 | {"help", no_argument, 0, 'h'}, |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5382 | {"version", no_argument, 0, 'V'}, |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5383 | {"trace", required_argument, NULL, 'T'}, |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5384 | {0, 0, 0, 0} |
| 5385 | }; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5386 | |
MORITA Kazutaka | 526eda1 | 2013-07-23 17:30:11 +0900 | [diff] [blame] | 5387 | #ifdef CONFIG_POSIX |
| 5388 | signal(SIGPIPE, SIG_IGN); |
| 5389 | #endif |
| 5390 | |
Daniel P. Berrangé | 98c5d2e | 2020-08-25 11:38:48 +0100 | [diff] [blame] | 5391 | socket_init(); |
Christophe Fergeau | f5852ef | 2019-01-31 17:46:14 +0100 | [diff] [blame] | 5392 | error_init(argv[0]); |
Daniel P. Berrange | fe4db84 | 2016-10-04 14:35:52 +0100 | [diff] [blame] | 5393 | module_call_init(MODULE_INIT_TRACE); |
Fam Zheng | 10f5bff | 2014-02-10 14:48:51 +0800 | [diff] [blame] | 5394 | qemu_init_exec_dir(argv[0]); |
Kevin Wolf | 53f76e5 | 2010-12-16 15:10:32 +0100 | [diff] [blame] | 5395 | |
Markus Armbruster | f9734d5 | 2021-07-20 14:53:53 +0200 | [diff] [blame] | 5396 | qemu_init_main_loop(&error_fatal); |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 5397 | |
Eduardo Habkost | e8f2d27 | 2016-05-12 11:10:04 -0300 | [diff] [blame] | 5398 | qcrypto_init(&error_fatal); |
Daniel P. Berrange | c229708 | 2016-04-06 12:12:06 +0100 | [diff] [blame] | 5399 | |
Daniel P. Berrange | 064097d | 2016-02-10 18:41:01 +0000 | [diff] [blame] | 5400 | module_call_init(MODULE_INIT_QOM); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5401 | bdrv_init(); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 5402 | if (argc < 2) { |
| 5403 | error_exit("Not enough arguments"); |
| 5404 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5405 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 5406 | qemu_add_opts(&qemu_source_opts); |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5407 | qemu_add_opts(&qemu_trace_opts); |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 5408 | |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 5409 | while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) { |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5410 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 5411 | case ':': |
| 5412 | missing_argument(argv[optind - 1]); |
| 5413 | return 0; |
Stefan Hajnoczi | 4581c16 | 2017-03-17 18:45:39 +0800 | [diff] [blame] | 5414 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 5415 | unrecognized_option(argv[optind - 1]); |
| 5416 | return 0; |
| 5417 | case 'h': |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5418 | help(); |
| 5419 | return 0; |
| 5420 | case 'V': |
| 5421 | printf(QEMU_IMG_VERSION); |
| 5422 | return 0; |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5423 | case 'T': |
Paolo Bonzini | 92eecff | 2020-11-02 06:58:41 -0500 | [diff] [blame] | 5424 | trace_opt_parse(optarg); |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5425 | break; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5426 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5427 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5428 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5429 | cmdname = argv[optind]; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5430 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5431 | /* reset getopt_long scanning */ |
| 5432 | argc -= optind; |
| 5433 | if (argc < 1) { |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 5434 | return 0; |
| 5435 | } |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5436 | argv += optind; |
Richard W.M. Jones | d339d76 | 2019-01-18 10:11:14 +0000 | [diff] [blame] | 5437 | qemu_reset_optind(); |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5438 | |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5439 | if (!trace_init_backends()) { |
| 5440 | exit(1); |
| 5441 | } |
Paolo Bonzini | 92eecff | 2020-11-02 06:58:41 -0500 | [diff] [blame] | 5442 | trace_init_file(); |
Richard Henderson | c5955f4 | 2022-04-17 11:29:44 -0700 | [diff] [blame] | 5443 | qemu_set_log(LOG_TRACE, &error_fatal); |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5444 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5445 | /* find the command */ |
| 5446 | for (cmd = img_cmds; cmd->name != NULL; cmd++) { |
| 5447 | if (!strcmp(cmdname, cmd->name)) { |
| 5448 | return cmd->handler(argc, argv); |
| 5449 | } |
| 5450 | } |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5451 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5452 | /* not found */ |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 5453 | error_exit("Command not found: %s", cmdname); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5454 | } |