bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1 | /* |
bellard | fb43f4d | 2006-08-07 21:34:46 +0000 | [diff] [blame] | 2 | * QEMU disk image utility |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | 68d0f70 | 2008-01-06 17:21:48 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5 | * |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 24 | |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
Eric Blake | c2a3d7d | 2017-07-21 08:50:47 -0500 | [diff] [blame] | 26 | #include <getopt.h> |
| 27 | |
Markus Armbruster | a8d2532 | 2019-05-23 16:35:08 +0200 | [diff] [blame] | 28 | #include "qemu-common.h" |
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 | 9edc631 | 2022-02-20 20:39:25 +0400 | [diff] [blame] | 103 | static void QEMU_NORETURN G_GNUC_PRINTF(1, 2) error_exit(const char *fmt, ...) |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 104 | { |
| 105 | va_list ap; |
| 106 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 107 | va_start(ap, fmt); |
Markus Armbruster | e9e1d92 | 2019-04-17 21:06:27 +0200 | [diff] [blame] | 108 | error_vreport(fmt, ap); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 109 | va_end(ap); |
| 110 | |
Markus Armbruster | e9e1d92 | 2019-04-17 21:06:27 +0200 | [diff] [blame] | 111 | error_printf("Try 'qemu-img --help' for more information\n"); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 112 | exit(EXIT_FAILURE); |
| 113 | } |
| 114 | |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 115 | static void QEMU_NORETURN missing_argument(const char *option) |
| 116 | { |
| 117 | error_exit("missing argument for option '%s'", option); |
| 118 | } |
| 119 | |
| 120 | static void QEMU_NORETURN unrecognized_option(const char *option) |
| 121 | { |
| 122 | error_exit("unrecognized option '%s'", option); |
| 123 | } |
| 124 | |
Eric Blake | 0562adf | 2020-05-12 20:16:41 -0500 | [diff] [blame] | 125 | /* Please keep in synch with docs/tools/qemu-img.rst */ |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 126 | static void QEMU_NORETURN help(void) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 127 | { |
Paolo Bonzini | e00291c | 2010-02-04 16:49:56 +0100 | [diff] [blame] | 128 | const char *help_msg = |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 129 | QEMU_IMG_VERSION |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 130 | "usage: qemu-img [standard options] command [command options]\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 131 | "QEMU disk image utility\n" |
| 132 | "\n" |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 133 | " '-h', '--help' display this help and exit\n" |
| 134 | " '-V', '--version' output version information and exit\n" |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 135 | " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n" |
| 136 | " specify tracing options\n" |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 137 | "\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 138 | "Command syntax:\n" |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 139 | #define DEF(option, callback, arg_string) \ |
| 140 | " " arg_string "\n" |
| 141 | #include "qemu-img-cmds.h" |
| 142 | #undef DEF |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 143 | "\n" |
| 144 | "Command parameters:\n" |
| 145 | " 'filename' is a disk image filename\n" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 146 | " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n" |
| 147 | " manual page for a description of the object properties. The most common\n" |
| 148 | " object type is a 'secret', which is used to supply passwords and/or\n" |
| 149 | " encryption keys.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 150 | " '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] | 151 | " '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] | 152 | " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n" |
| 153 | " 'directsync' and 'unsafe' (default for convert)\n" |
Stefan Hajnoczi | bb87fdf | 2014-09-02 11:01:02 +0100 | [diff] [blame] | 154 | " 'src_cache' is the cache mode used to read input disk images, the valid\n" |
| 155 | " options are the same as for the 'cache' option\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 156 | " 'size' is the disk image size in bytes. Optional suffixes\n" |
Kevin Wolf | 5e00984 | 2013-06-05 14:19:27 +0200 | [diff] [blame] | 157 | " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n" |
| 158 | " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n" |
| 159 | " supported. 'b' is ignored.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 160 | " 'output_filename' is the destination disk image filename\n" |
| 161 | " 'output_fmt' is the destination format\n" |
| 162 | " 'options' is a comma separated list of format specific options in a\n" |
| 163 | " name=value format. Use -o ? for an overview of the options supported by the\n" |
| 164 | " used format\n" |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 165 | " 'snapshot_param' is param used for internal snapshot, format\n" |
| 166 | " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n" |
| 167 | " '[ID_OR_NAME]'\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 168 | " '-c' indicates that target image must be compressed (qcow format only)\n" |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 169 | " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n" |
| 170 | " new backing file match exactly. The image doesn't need a working\n" |
| 171 | " backing file before rebasing in this case (useful for renaming the\n" |
| 172 | " backing file). For image creation, allow creating without attempting\n" |
| 173 | " to open the backing file.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 174 | " '-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] | 175 | " '-p' show progress of command (only certain commands)\n" |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 176 | " '-q' use Quiet mode - do not print any output (except errors)\n" |
Peter Lieven | 11b6699 | 2013-10-24 12:07:05 +0200 | [diff] [blame] | 177 | " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n" |
| 178 | " contain only zeros for qemu-img to create a sparse image during\n" |
| 179 | " conversion. If the number of bytes is 0, the source will not be scanned for\n" |
| 180 | " unallocated or zero sectors, and the destination image will always be\n" |
| 181 | " fully allocated\n" |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 182 | " '--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] | 183 | " '-n' skips the target volume creation (useful if the volume is created\n" |
| 184 | " prior to running qemu-img)\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 185 | "\n" |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 186 | "Parameters to bitmap subcommand:\n" |
| 187 | " 'bitmap' is the name of the bitmap to manipulate, through one or more\n" |
| 188 | " actions from '--add', '--remove', '--clear', '--enable', '--disable',\n" |
| 189 | " or '--merge source'\n" |
| 190 | " '-g granularity' sets the granularity for '--add' actions\n" |
| 191 | " '-b source' and '-F src_fmt' tell '--merge' actions to find the source\n" |
| 192 | " bitmaps from an alternative file\n" |
| 193 | "\n" |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 194 | "Parameters to check subcommand:\n" |
| 195 | " '-r' tries to repair any inconsistencies that are found during the check.\n" |
| 196 | " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n" |
| 197 | " 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] | 198 | " hiding corruption that has already occurred.\n" |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 199 | "\n" |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 200 | "Parameters to convert subcommand:\n" |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 201 | " '--bitmaps' copies all top-level persistent bitmaps to destination\n" |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 202 | " '-m' specifies how many coroutines work in parallel during the convert\n" |
| 203 | " process (defaults to 8)\n" |
| 204 | " '-W' allow to write to the target out of order rather than sequential\n" |
| 205 | "\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 206 | "Parameters to snapshot subcommand:\n" |
| 207 | " 'snapshot' is the name of the snapshot to create, apply or delete\n" |
| 208 | " '-a' applies a snapshot (revert disk to saved state)\n" |
| 209 | " '-c' creates a snapshot\n" |
| 210 | " '-d' deletes a snapshot\n" |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 211 | " '-l' lists all snapshots in the given image\n" |
| 212 | "\n" |
| 213 | "Parameters to compare subcommand:\n" |
| 214 | " '-f' first image format\n" |
| 215 | " '-F' second image format\n" |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 216 | " '-s' run in Strict mode - fail on different image size or sector allocation\n" |
| 217 | "\n" |
| 218 | "Parameters to dd subcommand:\n" |
| 219 | " 'bs=BYTES' read and write up to BYTES bytes at a time " |
| 220 | "(default: 512)\n" |
| 221 | " 'count=N' copy only N input blocks\n" |
| 222 | " 'if=FILE' read from FILE\n" |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 223 | " 'of=FILE' write to FILE\n" |
| 224 | " '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] | 225 | |
| 226 | printf("%s\nSupported formats:", help_msg); |
Andrey Shinkevich | 9ac404c | 2019-03-07 16:33:58 +0300 | [diff] [blame] | 227 | bdrv_iterate_format(format_print, NULL, false); |
Eric Blake | f5048cb | 2017-08-03 11:33:53 -0500 | [diff] [blame] | 228 | printf("\n\n" QEMU_HELP_BOTTOM "\n"); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 229 | exit(EXIT_SUCCESS); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Markus Armbruster | 80c710c | 2020-04-15 09:49:26 +0200 | [diff] [blame] | 232 | /* |
| 233 | * Is @optarg safe for accumulate_options()? |
| 234 | * It is when multiple of them can be joined together separated by ','. |
| 235 | * To make that work, @optarg must not start with ',' (or else a |
| 236 | * separating ',' preceding it gets escaped), and it must not end with |
| 237 | * an odd number of ',' (or else a separating ',' following it gets |
Markus Armbruster | f62514b | 2020-04-15 09:49:27 +0200 | [diff] [blame] | 238 | * escaped), or be empty (or else a separating ',' preceding it can |
| 239 | * escape a separating ',' following it). |
| 240 | * |
Markus Armbruster | 80c710c | 2020-04-15 09:49:26 +0200 | [diff] [blame] | 241 | */ |
| 242 | static bool is_valid_option_list(const char *optarg) |
| 243 | { |
| 244 | size_t len = strlen(optarg); |
| 245 | size_t i; |
| 246 | |
Markus Armbruster | f62514b | 2020-04-15 09:49:27 +0200 | [diff] [blame] | 247 | if (!optarg[0] || optarg[0] == ',') { |
Markus Armbruster | 80c710c | 2020-04-15 09:49:26 +0200 | [diff] [blame] | 248 | return false; |
| 249 | } |
| 250 | |
| 251 | for (i = len; i > 0 && optarg[i - 1] == ','; i--) { |
| 252 | } |
| 253 | if ((len - i) % 2) { |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | return true; |
| 258 | } |
| 259 | |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 260 | static int accumulate_options(char **options, char *optarg) |
| 261 | { |
| 262 | char *new_options; |
| 263 | |
| 264 | if (!is_valid_option_list(optarg)) { |
| 265 | error_report("Invalid option list: %s", optarg); |
| 266 | return -1; |
| 267 | } |
| 268 | |
| 269 | if (!*options) { |
| 270 | *options = g_strdup(optarg); |
| 271 | } else { |
| 272 | new_options = g_strdup_printf("%s,%s", *options, optarg); |
| 273 | g_free(*options); |
| 274 | *options = new_options; |
| 275 | } |
| 276 | return 0; |
| 277 | } |
| 278 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 279 | static QemuOptsList qemu_source_opts = { |
| 280 | .name = "source", |
| 281 | .implied_opt_name = "file", |
| 282 | .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head), |
| 283 | .desc = { |
| 284 | { } |
| 285 | }, |
| 286 | }; |
| 287 | |
Marc-André Lureau | 9edc631 | 2022-02-20 20:39:25 +0400 | [diff] [blame] | 288 | 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] | 289 | { |
| 290 | int ret = 0; |
| 291 | if (!quiet) { |
| 292 | va_list args; |
| 293 | va_start(args, fmt); |
| 294 | ret = vprintf(fmt, args); |
| 295 | va_end(args); |
| 296 | } |
| 297 | return ret; |
| 298 | } |
| 299 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 300 | |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 301 | static int print_block_option_help(const char *filename, const char *fmt) |
| 302 | { |
| 303 | BlockDriver *drv, *proto_drv; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 304 | QemuOptsList *create_opts = NULL; |
Max Reitz | b65a5e1 | 2015-02-05 13:58:12 -0500 | [diff] [blame] | 305 | Error *local_err = NULL; |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 306 | |
| 307 | /* Find driver and parse its options */ |
| 308 | drv = bdrv_find_format(fmt); |
| 309 | if (!drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 310 | error_report("Unknown file format '%s'", fmt); |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 311 | return 1; |
| 312 | } |
| 313 | |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 314 | if (!drv->create_opts) { |
| 315 | error_report("Format driver '%s' does not support image creation", fmt); |
| 316 | return 1; |
| 317 | } |
| 318 | |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 319 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 320 | if (filename) { |
Max Reitz | b65a5e1 | 2015-02-05 13:58:12 -0500 | [diff] [blame] | 321 | proto_drv = bdrv_find_protocol(filename, true, &local_err); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 322 | if (!proto_drv) { |
Markus Armbruster | 2867ce4 | 2015-03-12 16:08:02 +0100 | [diff] [blame] | 323 | error_report_err(local_err); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 324 | qemu_opts_free(create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 325 | return 1; |
| 326 | } |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 327 | if (!proto_drv->create_opts) { |
Max Reitz | f099887 | 2018-11-19 11:19:20 +0100 | [diff] [blame] | 328 | error_report("Protocol driver '%s' does not support image creation", |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 329 | proto_drv->format_name); |
Max Reitz | 3ecd5a4 | 2018-11-19 11:19:21 +0100 | [diff] [blame] | 330 | qemu_opts_free(create_opts); |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 331 | return 1; |
| 332 | } |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 333 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 334 | } |
| 335 | |
Max Reitz | f4619af | 2019-04-13 17:20:37 +0200 | [diff] [blame] | 336 | if (filename) { |
| 337 | printf("Supported options:\n"); |
| 338 | } else { |
| 339 | printf("Supported %s options:\n", fmt); |
| 340 | } |
Max Reitz | 6389871 | 2018-10-19 18:49:25 +0200 | [diff] [blame] | 341 | qemu_opts_print_help(create_opts, false); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 342 | qemu_opts_free(create_opts); |
Max Reitz | f4619af | 2019-04-13 17:20:37 +0200 | [diff] [blame] | 343 | |
| 344 | if (!filename) { |
| 345 | printf("\n" |
| 346 | "The protocol level may support further options.\n" |
| 347 | "Specify the target filename to include those options.\n"); |
| 348 | } |
| 349 | |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 350 | return 0; |
| 351 | } |
| 352 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 353 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 354 | static BlockBackend *img_open_opts(const char *optstr, |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 355 | QemuOpts *opts, int flags, bool writethrough, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 356 | bool quiet, bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 357 | { |
| 358 | QDict *options; |
| 359 | Error *local_err = NULL; |
| 360 | BlockBackend *blk; |
| 361 | options = qemu_opts_to_qdict(opts, NULL); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 362 | if (force_share) { |
| 363 | if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE) |
Max Reitz | 4615f87 | 2018-05-02 22:20:50 +0200 | [diff] [blame] | 364 | && strcmp(qdict_get_str(options, BDRV_OPT_FORCE_SHARE), "on")) { |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 365 | error_report("--force-share/-U conflicts with image options"); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 366 | qobject_unref(options); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 367 | return NULL; |
| 368 | } |
Max Reitz | 4615f87 | 2018-05-02 22:20:50 +0200 | [diff] [blame] | 369 | qdict_put_str(options, BDRV_OPT_FORCE_SHARE, "on"); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 370 | } |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 371 | blk = blk_new_open(NULL, NULL, options, flags, &local_err); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 372 | if (!blk) { |
Daniel P. Berrange | 143605a | 2016-04-06 10:16:18 +0100 | [diff] [blame] | 373 | error_reportf_err(local_err, "Could not open '%s': ", optstr); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 374 | return NULL; |
| 375 | } |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 376 | blk_set_enable_write_cache(blk, !writethrough); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 377 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 378 | return blk; |
| 379 | } |
| 380 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 381 | static BlockBackend *img_open_file(const char *filename, |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 382 | QDict *options, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 383 | const char *fmt, int flags, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 384 | bool writethrough, bool quiet, |
| 385 | bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 386 | { |
| 387 | BlockBackend *blk; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 388 | Error *local_err = NULL; |
Kevin Wolf | ad71713 | 2010-12-16 15:37:41 +0100 | [diff] [blame] | 389 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 390 | if (!options) { |
| 391 | options = qdict_new(); |
| 392 | } |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 393 | if (fmt) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 394 | qdict_put_str(options, "driver", fmt); |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 395 | } |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 396 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 397 | if (force_share) { |
Eric Blake | 579cf1d | 2017-05-15 14:54:39 -0500 | [diff] [blame] | 398 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 399 | } |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 400 | blk = blk_new_open(filename, NULL, options, flags, &local_err); |
Max Reitz | 5bd3132 | 2015-02-05 13:58:16 -0500 | [diff] [blame] | 401 | if (!blk) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 402 | error_reportf_err(local_err, "Could not open '%s': ", filename); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 403 | return NULL; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 404 | } |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 405 | blk_set_enable_write_cache(blk, !writethrough); |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 406 | |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 407 | return blk; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 410 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 411 | static int img_add_key_secrets(void *opaque, |
| 412 | const char *name, const char *value, |
| 413 | Error **errp) |
| 414 | { |
| 415 | QDict *options = opaque; |
| 416 | |
| 417 | if (g_str_has_suffix(name, "key-secret")) { |
Eric Blake | 187f47e | 2017-06-24 12:10:07 -0600 | [diff] [blame] | 418 | qdict_put_str(options, name, value); |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 424 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 425 | static BlockBackend *img_open(bool image_opts, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 426 | const char *filename, |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 427 | const char *fmt, int flags, bool writethrough, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 428 | bool quiet, bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 429 | { |
| 430 | BlockBackend *blk; |
| 431 | if (image_opts) { |
| 432 | QemuOpts *opts; |
| 433 | if (fmt) { |
| 434 | error_report("--image-opts and --format are mutually exclusive"); |
| 435 | return NULL; |
| 436 | } |
| 437 | opts = qemu_opts_parse_noisily(qemu_find_opts("source"), |
| 438 | filename, true); |
| 439 | if (!opts) { |
| 440 | return NULL; |
| 441 | } |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 442 | blk = img_open_opts(filename, opts, flags, writethrough, quiet, |
| 443 | force_share); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 444 | } else { |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 445 | blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 446 | force_share); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 447 | } |
| 448 | return blk; |
| 449 | } |
| 450 | |
| 451 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 452 | static int add_old_style_options(const char *fmt, QemuOpts *opts, |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 453 | const char *base_filename, |
| 454 | const char *base_fmt) |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 455 | { |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 456 | if (base_filename) { |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 457 | if (!qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, |
Markus Armbruster | 9e194e0 | 2020-07-07 18:06:11 +0200 | [diff] [blame] | 458 | NULL)) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 459 | error_report("Backing file not supported for file format '%s'", |
| 460 | fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 461 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | if (base_fmt) { |
Markus Armbruster | 9e194e0 | 2020-07-07 18:06:11 +0200 | [diff] [blame] | 465 | if (!qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, NULL)) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 466 | error_report("Backing file format not supported for file " |
| 467 | "format '%s'", fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 468 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 469 | } |
| 470 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 471 | return 0; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 472 | } |
| 473 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 474 | static int64_t cvtnum_full(const char *name, const char *value, int64_t min, |
| 475 | int64_t max) |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 476 | { |
Markus Armbruster | f17fd4f | 2017-02-21 21:14:06 +0100 | [diff] [blame] | 477 | int err; |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 478 | uint64_t res; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 479 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 480 | err = qemu_strtosz(value, NULL, &res); |
| 481 | if (err < 0 && err != -ERANGE) { |
| 482 | error_report("Invalid %s specified. You may use " |
| 483 | "k, M, G, T, P or E suffixes for", name); |
| 484 | error_report("kilobytes, megabytes, gigabytes, terabytes, " |
| 485 | "petabytes and exabytes."); |
Markus Armbruster | f17fd4f | 2017-02-21 21:14:06 +0100 | [diff] [blame] | 486 | return err; |
| 487 | } |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 488 | if (err == -ERANGE || res > max || res < min) { |
| 489 | error_report("Invalid %s specified. Must be between %" PRId64 |
| 490 | " and %" PRId64 ".", name, min, max); |
Markus Armbruster | f46bfdb | 2017-02-21 21:14:07 +0100 | [diff] [blame] | 491 | return -ERANGE; |
| 492 | } |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 493 | return res; |
| 494 | } |
| 495 | |
| 496 | static int64_t cvtnum(const char *name, const char *value) |
| 497 | { |
| 498 | return cvtnum_full(name, value, 0, INT64_MAX); |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 499 | } |
| 500 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 501 | static int img_create(int argc, char **argv) |
| 502 | { |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 503 | int c; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 504 | uint64_t img_size = -1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 505 | const char *fmt = "raw"; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 506 | const char *base_fmt = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 507 | const char *filename; |
| 508 | const char *base_filename = NULL; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 509 | char *options = NULL; |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 510 | Error *local_err = NULL; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 511 | bool quiet = false; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 512 | int flags = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 513 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 514 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 515 | static const struct option long_options[] = { |
| 516 | {"help", no_argument, 0, 'h'}, |
| 517 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 518 | {0, 0, 0, 0} |
| 519 | }; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 520 | c = getopt_long(argc, argv, ":F:b:f:ho:qu", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 521 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 522 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 523 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 524 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 525 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 526 | case ':': |
| 527 | missing_argument(argv[optind - 1]); |
| 528 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 529 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 530 | unrecognized_option(argv[optind - 1]); |
| 531 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 532 | case 'h': |
| 533 | help(); |
| 534 | break; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 535 | case 'F': |
| 536 | base_fmt = optarg; |
| 537 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 538 | case 'b': |
| 539 | base_filename = optarg; |
| 540 | break; |
| 541 | case 'f': |
| 542 | fmt = optarg; |
| 543 | break; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 544 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 545 | if (accumulate_options(&options, optarg) < 0) { |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 546 | goto fail; |
| 547 | } |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 548 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 549 | case 'q': |
| 550 | quiet = true; |
| 551 | break; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 552 | case 'u': |
| 553 | flags |= BDRV_O_NO_BACKING; |
| 554 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 555 | case OPTION_OBJECT: |
| 556 | user_creatable_process_cmdline(optarg); |
| 557 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 558 | } |
| 559 | } |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 560 | |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 561 | /* Get the filename */ |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 562 | filename = (optind < argc) ? argv[optind] : NULL; |
| 563 | if (options && has_help_option(options)) { |
| 564 | g_free(options); |
| 565 | return print_block_option_help(filename, fmt); |
| 566 | } |
| 567 | |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 568 | if (optind >= argc) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 569 | error_exit("Expecting image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 570 | } |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 571 | optind++; |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 572 | |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 573 | /* Get image size, if specified */ |
| 574 | if (optind < argc) { |
Jes Sorensen | 70b4f4b | 2011-01-05 11:41:02 +0100 | [diff] [blame] | 575 | int64_t sval; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 576 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 577 | sval = cvtnum("image size", argv[optind++]); |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 578 | if (sval < 0) { |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 579 | goto fail; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 580 | } |
| 581 | img_size = (uint64_t)sval; |
| 582 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 583 | if (optind != argc) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 584 | error_exit("Unexpected argument: %s", argv[optind]); |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 585 | } |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 586 | |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 587 | bdrv_img_create(filename, fmt, base_filename, base_fmt, |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 588 | options, img_size, flags, quiet, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 589 | if (local_err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 590 | error_reportf_err(local_err, "%s: ", filename); |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 591 | goto fail; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 592 | } |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 593 | |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 594 | g_free(options); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 595 | return 0; |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 596 | |
| 597 | fail: |
| 598 | g_free(options); |
| 599 | return 1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 602 | static void dump_json_image_check(ImageCheck *check, bool quiet) |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 603 | { |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 604 | GString *str; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 605 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 606 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 607 | |
| 608 | visit_type_ImageCheck(v, NULL, &check, &error_abort); |
| 609 | visit_complete(v, &obj); |
Markus Armbruster | 6589f45 | 2020-12-11 18:11:35 +0100 | [diff] [blame] | 610 | str = qobject_to_json_pretty(obj, true); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 611 | assert(str != NULL); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 612 | qprintf(quiet, "%s\n", str->str); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 613 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 614 | visit_free(v); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 615 | g_string_free(str, true); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 616 | } |
| 617 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 618 | static void dump_human_image_check(ImageCheck *check, bool quiet) |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 619 | { |
| 620 | if (!(check->corruptions || check->leaks || check->check_errors)) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 621 | qprintf(quiet, "No errors were found on the image.\n"); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 622 | } else { |
| 623 | if (check->corruptions) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 624 | qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n" |
| 625 | "Data may be corrupted, or further writes to the image " |
| 626 | "may corrupt it.\n", |
| 627 | check->corruptions); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | if (check->leaks) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 631 | qprintf(quiet, |
| 632 | "\n%" PRId64 " leaked clusters were found on the image.\n" |
| 633 | "This means waste of disk space, but no harm to data.\n", |
| 634 | check->leaks); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | if (check->check_errors) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 638 | qprintf(quiet, |
| 639 | "\n%" PRId64 |
| 640 | " internal errors have occurred during the check.\n", |
| 641 | check->check_errors); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
| 645 | if (check->total_clusters != 0 && check->allocated_clusters != 0) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 646 | qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, " |
| 647 | "%0.2f%% fragmented, %0.2f%% compressed clusters\n", |
| 648 | check->allocated_clusters, check->total_clusters, |
| 649 | check->allocated_clusters * 100.0 / check->total_clusters, |
| 650 | check->fragmented_clusters * 100.0 / check->allocated_clusters, |
| 651 | check->compressed_clusters * 100.0 / |
| 652 | check->allocated_clusters); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | if (check->image_end_offset) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 656 | qprintf(quiet, |
| 657 | "Image end offset: %" PRId64 "\n", check->image_end_offset); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
| 661 | static int collect_image_check(BlockDriverState *bs, |
| 662 | ImageCheck *check, |
| 663 | const char *filename, |
| 664 | const char *fmt, |
| 665 | int fix) |
| 666 | { |
| 667 | int ret; |
| 668 | BdrvCheckResult result; |
| 669 | |
| 670 | ret = bdrv_check(bs, &result, fix); |
| 671 | if (ret < 0) { |
| 672 | return ret; |
| 673 | } |
| 674 | |
| 675 | check->filename = g_strdup(filename); |
| 676 | check->format = g_strdup(bdrv_get_format_name(bs)); |
| 677 | check->check_errors = result.check_errors; |
| 678 | check->corruptions = result.corruptions; |
| 679 | check->has_corruptions = result.corruptions != 0; |
| 680 | check->leaks = result.leaks; |
| 681 | check->has_leaks = result.leaks != 0; |
| 682 | check->corruptions_fixed = result.corruptions_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 683 | check->has_corruptions_fixed = result.corruptions_fixed != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 684 | check->leaks_fixed = result.leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 685 | check->has_leaks_fixed = result.leaks_fixed != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 686 | check->image_end_offset = result.image_end_offset; |
| 687 | check->has_image_end_offset = result.image_end_offset != 0; |
| 688 | check->total_clusters = result.bfi.total_clusters; |
| 689 | check->has_total_clusters = result.bfi.total_clusters != 0; |
| 690 | check->allocated_clusters = result.bfi.allocated_clusters; |
| 691 | check->has_allocated_clusters = result.bfi.allocated_clusters != 0; |
| 692 | check->fragmented_clusters = result.bfi.fragmented_clusters; |
| 693 | check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0; |
Stefan Hajnoczi | e6439d7 | 2013-02-07 17:15:04 +0100 | [diff] [blame] | 694 | check->compressed_clusters = result.bfi.compressed_clusters; |
| 695 | check->has_compressed_clusters = result.bfi.compressed_clusters != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 696 | |
| 697 | return 0; |
| 698 | } |
| 699 | |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 700 | /* |
| 701 | * Checks an image for consistency. Exit codes: |
| 702 | * |
Max Reitz | d6635c4 | 2014-06-02 22:15:21 +0200 | [diff] [blame] | 703 | * 0 - Check completed, image is good |
| 704 | * 1 - Check not completed because of internal errors |
| 705 | * 2 - Check completed, image is corrupted |
| 706 | * 3 - Check completed, image has leaked clusters, but is good otherwise |
| 707 | * 63 - Checks are not supported by the image format |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 708 | */ |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 709 | static int img_check(int argc, char **argv) |
| 710 | { |
| 711 | int c, ret; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 712 | OutputFormat output_format = OFORMAT_HUMAN; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 713 | const char *filename, *fmt, *output, *cache; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 714 | BlockBackend *blk; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 715 | BlockDriverState *bs; |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 716 | int fix = 0; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 717 | int flags = BDRV_O_CHECK; |
| 718 | bool writethrough; |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 719 | ImageCheck *check; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 720 | bool quiet = false; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 721 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 722 | bool force_share = false; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 723 | |
| 724 | fmt = NULL; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 725 | output = NULL; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 726 | cache = BDRV_DEFAULT_CACHE; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 727 | |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 728 | for(;;) { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 729 | int option_index = 0; |
| 730 | static const struct option long_options[] = { |
| 731 | {"help", no_argument, 0, 'h'}, |
| 732 | {"format", required_argument, 0, 'f'}, |
Prasad Joshi | 4fd6a98 | 2014-03-25 00:08:54 +0530 | [diff] [blame] | 733 | {"repair", required_argument, 0, 'r'}, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 734 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 735 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 736 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 737 | {"force-share", no_argument, 0, 'U'}, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 738 | {0, 0, 0, 0} |
| 739 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 740 | c = getopt_long(argc, argv, ":hf:r:T:qU", |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 741 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 742 | if (c == -1) { |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 743 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 744 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 745 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 746 | case ':': |
| 747 | missing_argument(argv[optind - 1]); |
| 748 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 749 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 750 | unrecognized_option(argv[optind - 1]); |
| 751 | break; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 752 | case 'h': |
| 753 | help(); |
| 754 | break; |
| 755 | case 'f': |
| 756 | fmt = optarg; |
| 757 | break; |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 758 | case 'r': |
| 759 | flags |= BDRV_O_RDWR; |
| 760 | |
| 761 | if (!strcmp(optarg, "leaks")) { |
| 762 | fix = BDRV_FIX_LEAKS; |
| 763 | } else if (!strcmp(optarg, "all")) { |
| 764 | fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS; |
| 765 | } else { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 766 | error_exit("Unknown option value for -r " |
| 767 | "(expecting 'leaks' or 'all'): %s", optarg); |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 768 | } |
| 769 | break; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 770 | case OPTION_OUTPUT: |
| 771 | output = optarg; |
| 772 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 773 | case 'T': |
| 774 | cache = optarg; |
| 775 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 776 | case 'q': |
| 777 | quiet = true; |
| 778 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 779 | case 'U': |
| 780 | force_share = true; |
| 781 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 782 | case OPTION_OBJECT: |
| 783 | user_creatable_process_cmdline(optarg); |
| 784 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 785 | case OPTION_IMAGE_OPTS: |
| 786 | image_opts = true; |
| 787 | break; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 788 | } |
| 789 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 790 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 791 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 792 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 793 | filename = argv[optind++]; |
| 794 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 795 | if (output && !strcmp(output, "json")) { |
| 796 | output_format = OFORMAT_JSON; |
| 797 | } else if (output && !strcmp(output, "human")) { |
| 798 | output_format = OFORMAT_HUMAN; |
| 799 | } else if (output) { |
| 800 | error_report("--output must be used with human or json as argument."); |
| 801 | return 1; |
| 802 | } |
| 803 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 804 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 805 | if (ret < 0) { |
| 806 | error_report("Invalid source cache option: %s", cache); |
| 807 | return 1; |
| 808 | } |
| 809 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 810 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 811 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 812 | if (!blk) { |
| 813 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 814 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 815 | bs = blk_bs(blk); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 816 | |
| 817 | check = g_new0(ImageCheck, 1); |
| 818 | ret = collect_image_check(bs, check, filename, fmt, fix); |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 819 | |
| 820 | if (ret == -ENOTSUP) { |
Max Reitz | 55d492d | 2014-05-31 21:33:30 +0200 | [diff] [blame] | 821 | error_report("This image format does not support checks"); |
Peter Lieven | fefddf9 | 2013-10-24 08:53:34 +0200 | [diff] [blame] | 822 | ret = 63; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 823 | goto fail; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 824 | } |
| 825 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 826 | if (check->corruptions_fixed || check->leaks_fixed) { |
| 827 | int corruptions_fixed, leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 828 | bool has_leaks_fixed, has_corruptions_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 829 | |
| 830 | leaks_fixed = check->leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 831 | has_leaks_fixed = check->has_leaks_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 832 | corruptions_fixed = check->corruptions_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 833 | has_corruptions_fixed = check->has_corruptions_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 834 | |
| 835 | if (output_format == OFORMAT_HUMAN) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 836 | qprintf(quiet, |
| 837 | "The following inconsistencies were found and repaired:\n\n" |
| 838 | " %" PRId64 " leaked clusters\n" |
| 839 | " %" PRId64 " corruptions\n\n" |
| 840 | "Double checking the fixed image now...\n", |
| 841 | check->leaks_fixed, |
| 842 | check->corruptions_fixed); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 843 | } |
| 844 | |
Pan Nengyuan | fc124ea | 2020-02-27 09:29:50 +0800 | [diff] [blame] | 845 | qapi_free_ImageCheck(check); |
| 846 | check = g_new0(ImageCheck, 1); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 847 | ret = collect_image_check(bs, check, filename, fmt, 0); |
| 848 | |
| 849 | check->leaks_fixed = leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 850 | check->has_leaks_fixed = has_leaks_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 851 | check->corruptions_fixed = corruptions_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 852 | check->has_corruptions_fixed = has_corruptions_fixed; |
Kevin Wolf | ccf3471 | 2012-05-11 18:16:54 +0200 | [diff] [blame] | 853 | } |
| 854 | |
Max Reitz | 832390a | 2014-10-23 15:29:12 +0200 | [diff] [blame] | 855 | if (!ret) { |
| 856 | switch (output_format) { |
| 857 | case OFORMAT_HUMAN: |
| 858 | dump_human_image_check(check, quiet); |
| 859 | break; |
| 860 | case OFORMAT_JSON: |
| 861 | dump_json_image_check(check, quiet); |
| 862 | break; |
| 863 | } |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | if (ret || check->check_errors) { |
Max Reitz | 832390a | 2014-10-23 15:29:12 +0200 | [diff] [blame] | 867 | if (ret) { |
| 868 | error_report("Check failed: %s", strerror(-ret)); |
| 869 | } else { |
| 870 | error_report("Check failed"); |
| 871 | } |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 872 | ret = 1; |
| 873 | goto fail; |
| 874 | } |
| 875 | |
| 876 | if (check->corruptions) { |
| 877 | ret = 2; |
| 878 | } else if (check->leaks) { |
| 879 | ret = 3; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 880 | } else { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 881 | ret = 0; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 884 | fail: |
| 885 | qapi_free_ImageCheck(check); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 886 | blk_unref(blk); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 887 | return ret; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 890 | typedef struct CommonBlockJobCBInfo { |
| 891 | BlockDriverState *bs; |
| 892 | Error **errp; |
| 893 | } CommonBlockJobCBInfo; |
| 894 | |
| 895 | static void common_block_job_cb(void *opaque, int ret) |
| 896 | { |
| 897 | CommonBlockJobCBInfo *cbi = opaque; |
| 898 | |
| 899 | if (ret < 0) { |
| 900 | error_setg_errno(cbi->errp, -ret, "Block job failed"); |
| 901 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | static void run_block_job(BlockJob *job, Error **errp) |
| 905 | { |
Emanuele Giuseppe Esposito | a7b4f8f | 2021-06-14 10:11:29 +0200 | [diff] [blame] | 906 | uint64_t progress_current, progress_total; |
Vladimir Sementsov-Ogievskiy | df9a316 | 2021-05-06 17:13:53 +0300 | [diff] [blame] | 907 | AioContext *aio_context = block_job_get_aio_context(job); |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 908 | int ret = 0; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 909 | |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 910 | aio_context_acquire(aio_context); |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 911 | job_ref(&job->job); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 912 | do { |
Kevin Wolf | 30a5c88 | 2018-05-04 12:17:20 +0200 | [diff] [blame] | 913 | float progress = 0.0f; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 914 | aio_poll(aio_context, true); |
Emanuele Giuseppe Esposito | a7b4f8f | 2021-06-14 10:11:29 +0200 | [diff] [blame] | 915 | |
| 916 | progress_get_snapshot(&job->job.progress, &progress_current, |
| 917 | &progress_total); |
| 918 | if (progress_total) { |
| 919 | progress = (float)progress_current / progress_total * 100.f; |
Kevin Wolf | 30a5c88 | 2018-05-04 12:17:20 +0200 | [diff] [blame] | 920 | } |
| 921 | qemu_progress_print(progress, 0); |
Kevin Wolf | df956ae | 2018-04-25 15:09:58 +0200 | [diff] [blame] | 922 | } while (!job_is_ready(&job->job) && !job_is_completed(&job->job)); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 923 | |
Kevin Wolf | dbe5e6c | 2018-04-19 13:04:01 +0200 | [diff] [blame] | 924 | if (!job_is_completed(&job->job)) { |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 925 | ret = job_complete_sync(&job->job, errp); |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 926 | } else { |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 927 | ret = job->job.ret; |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 928 | } |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 929 | job_unref(&job->job); |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 930 | aio_context_release(aio_context); |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 931 | |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 932 | /* publish completion progress only when success */ |
| 933 | if (!ret) { |
| 934 | qemu_progress_print(100.f, 0); |
| 935 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 936 | } |
| 937 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 938 | static int img_commit(int argc, char **argv) |
| 939 | { |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 940 | int c, ret, flags; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 941 | const char *filename, *fmt, *cache, *base; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 942 | BlockBackend *blk; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 943 | BlockDriverState *bs, *base_bs; |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 944 | BlockJob *job; |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 945 | bool progress = false, quiet = false, drop = false; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 946 | bool writethrough; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 947 | Error *local_err = NULL; |
| 948 | CommonBlockJobCBInfo cbi; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 949 | bool image_opts = false; |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 950 | AioContext *aio_context; |
Zhengui | a0441b6 | 2020-10-20 14:47:43 +0000 | [diff] [blame] | 951 | int64_t rate_limit = 0; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 952 | |
| 953 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 954 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 955 | base = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 956 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 957 | static const struct option long_options[] = { |
| 958 | {"help", no_argument, 0, 'h'}, |
| 959 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 960 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 961 | {0, 0, 0, 0} |
| 962 | }; |
Zhengui | a0441b6 | 2020-10-20 14:47:43 +0000 | [diff] [blame] | 963 | c = getopt_long(argc, argv, ":f:ht:b:dpqr:", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 964 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 965 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 966 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 967 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 968 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 969 | case ':': |
| 970 | missing_argument(argv[optind - 1]); |
| 971 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 972 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 973 | unrecognized_option(argv[optind - 1]); |
| 974 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 975 | case 'h': |
| 976 | help(); |
| 977 | break; |
| 978 | case 'f': |
| 979 | fmt = optarg; |
| 980 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 981 | case 't': |
| 982 | cache = optarg; |
| 983 | break; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 984 | case 'b': |
| 985 | base = optarg; |
| 986 | /* -b implies -d */ |
| 987 | drop = true; |
| 988 | break; |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 989 | case 'd': |
| 990 | drop = true; |
| 991 | break; |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 992 | case 'p': |
| 993 | progress = true; |
| 994 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 995 | case 'q': |
| 996 | quiet = true; |
| 997 | break; |
Zhengui | a0441b6 | 2020-10-20 14:47:43 +0000 | [diff] [blame] | 998 | case 'r': |
| 999 | rate_limit = cvtnum("rate limit", optarg); |
| 1000 | if (rate_limit < 0) { |
| 1001 | return 1; |
| 1002 | } |
| 1003 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 1004 | case OPTION_OBJECT: |
| 1005 | user_creatable_process_cmdline(optarg); |
| 1006 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1007 | case OPTION_IMAGE_OPTS: |
| 1008 | image_opts = true; |
| 1009 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1010 | } |
| 1011 | } |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 1012 | |
| 1013 | /* Progress is not shown in Quiet mode */ |
| 1014 | if (quiet) { |
| 1015 | progress = false; |
| 1016 | } |
| 1017 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 1018 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 1019 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1020 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1021 | filename = argv[optind++]; |
| 1022 | |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1023 | flags = BDRV_O_RDWR | BDRV_O_UNMAP; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1024 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1025 | if (ret < 0) { |
| 1026 | error_report("Invalid cache option: %s", cache); |
Stefan Hajnoczi | a3981eb | 2014-08-26 19:17:54 +0100 | [diff] [blame] | 1027 | return 1; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1028 | } |
| 1029 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1030 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 1031 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1032 | if (!blk) { |
| 1033 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1034 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1035 | bs = blk_bs(blk); |
| 1036 | |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 1037 | qemu_progress_init(progress, 1.f); |
| 1038 | qemu_progress_print(0.f, 100); |
| 1039 | |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1040 | if (base) { |
| 1041 | base_bs = bdrv_find_backing_image(bs, base); |
| 1042 | if (!base_bs) { |
Max Reitz | 6b33f3a | 2016-12-01 03:05:08 +0100 | [diff] [blame] | 1043 | error_setg(&local_err, |
| 1044 | "Did not find '%s' in the backing chain of '%s'", |
| 1045 | base, filename); |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1046 | goto done; |
| 1047 | } |
| 1048 | } else { |
| 1049 | /* This is different from QMP, which by default uses the deepest file in |
| 1050 | * the backing chain (i.e., the very base); however, the traditional |
| 1051 | * behavior of qemu-img commit is using the immediate backing file. */ |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 1052 | base_bs = bdrv_backing_chain_next(bs); |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1053 | if (!base_bs) { |
| 1054 | error_setg(&local_err, "Image does not have a backing file"); |
| 1055 | goto done; |
| 1056 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1059 | cbi = (CommonBlockJobCBInfo){ |
| 1060 | .errp = &local_err, |
| 1061 | .bs = bs, |
| 1062 | }; |
| 1063 | |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 1064 | aio_context = bdrv_get_aio_context(bs); |
| 1065 | aio_context_acquire(aio_context); |
Zhengui | a0441b6 | 2020-10-20 14:47:43 +0000 | [diff] [blame] | 1066 | commit_active_start("commit", bs, base_bs, JOB_DEFAULT, rate_limit, |
Kevin Wolf | 0db832f | 2017-02-20 18:10:05 +0100 | [diff] [blame] | 1067 | BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb, |
Fam Zheng | 78bbd91 | 2017-04-21 20:27:04 +0800 | [diff] [blame] | 1068 | &cbi, false, &local_err); |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 1069 | aio_context_release(aio_context); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1070 | if (local_err) { |
| 1071 | goto done; |
| 1072 | } |
| 1073 | |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1074 | /* When the block job completes, the BlockBackend reference will point to |
| 1075 | * the old backing file. In order to avoid that the top image is already |
| 1076 | * deleted, so we can still empty it afterwards, increment the reference |
| 1077 | * counter here preemptively. */ |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1078 | if (!drop) { |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1079 | bdrv_ref(bs); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1080 | } |
| 1081 | |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 1082 | job = block_job_get("commit"); |
Liam Merwick | 2e2db26 | 2018-11-05 21:38:37 +0000 | [diff] [blame] | 1083 | assert(job); |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 1084 | run_block_job(job, &local_err); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1085 | if (local_err) { |
| 1086 | goto unref_backing; |
| 1087 | } |
| 1088 | |
Max Reitz | 2d97fde | 2020-04-29 16:11:26 +0200 | [diff] [blame] | 1089 | if (!drop) { |
| 1090 | BlockBackend *old_backing_blk; |
| 1091 | |
| 1092 | old_backing_blk = blk_new_with_bs(bs, BLK_PERM_WRITE, BLK_PERM_ALL, |
| 1093 | &local_err); |
| 1094 | if (!old_backing_blk) { |
| 1095 | goto unref_backing; |
| 1096 | } |
| 1097 | ret = blk_make_empty(old_backing_blk, &local_err); |
| 1098 | blk_unref(old_backing_blk); |
| 1099 | if (ret == -ENOTSUP) { |
| 1100 | error_free(local_err); |
| 1101 | local_err = NULL; |
| 1102 | } else if (ret < 0) { |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1103 | goto unref_backing; |
| 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | unref_backing: |
| 1108 | if (!drop) { |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1109 | bdrv_unref(bs); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1110 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1111 | |
| 1112 | done: |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 1113 | qemu_progress_end(); |
| 1114 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1115 | blk_unref(blk); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1116 | |
| 1117 | if (local_err) { |
Markus Armbruster | 6936f29 | 2015-02-10 15:14:02 +0100 | [diff] [blame] | 1118 | error_report_err(local_err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1119 | return 1; |
| 1120 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1121 | |
| 1122 | qprintf(quiet, "Image committed.\n"); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1123 | return 0; |
| 1124 | } |
| 1125 | |
Dmitry Konishchev | f6a00aa | 2011-05-18 15:03:59 +0400 | [diff] [blame] | 1126 | /* |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1127 | * Returns -1 if 'buf' contains only zeroes, otherwise the byte index |
| 1128 | * of the first sector boundary within buf where the sector contains a |
| 1129 | * non-zero byte. This function is robust to a buffer that is not |
| 1130 | * sector-aligned. |
| 1131 | */ |
| 1132 | static int64_t find_nonzero(const uint8_t *buf, int64_t n) |
| 1133 | { |
| 1134 | int64_t i; |
| 1135 | int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE); |
| 1136 | |
| 1137 | for (i = 0; i < end; i += BDRV_SECTOR_SIZE) { |
| 1138 | if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) { |
| 1139 | return i; |
| 1140 | } |
| 1141 | } |
| 1142 | if (i < n && !buffer_is_zero(buf + i, n - end)) { |
| 1143 | return i; |
| 1144 | } |
| 1145 | return -1; |
| 1146 | } |
| 1147 | |
| 1148 | /* |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1149 | * Returns true iff the first sector pointed to by 'buf' contains at least |
| 1150 | * a non-NUL byte. |
| 1151 | * |
| 1152 | * 'pnum' is set to the number of sectors (including and immediately following |
| 1153 | * 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] | 1154 | * The function will try to align the end offset to alignment boundaries so |
zhaolichang | e3a6e0d | 2020-09-17 15:50:20 +0800 | [diff] [blame] | 1155 | * that the request will at least end aligned and consecutive requests will |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1156 | * also start at an aligned offset. |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1157 | */ |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1158 | static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum, |
| 1159 | int64_t sector_num, int alignment) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1160 | { |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1161 | bool is_zero; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1162 | int i, tail; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1163 | |
| 1164 | if (n <= 0) { |
| 1165 | *pnum = 0; |
| 1166 | return 0; |
| 1167 | } |
Yi Li | c075c42 | 2020-08-19 09:36:07 +0800 | [diff] [blame] | 1168 | is_zero = buffer_is_zero(buf, BDRV_SECTOR_SIZE); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1169 | for(i = 1; i < n; i++) { |
Yi Li | c075c42 | 2020-08-19 09:36:07 +0800 | [diff] [blame] | 1170 | buf += BDRV_SECTOR_SIZE; |
| 1171 | if (is_zero != buffer_is_zero(buf, BDRV_SECTOR_SIZE)) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1172 | break; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1173 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1174 | } |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1175 | |
Vladimir Sementsov-Ogievskiy | 96054c7 | 2021-12-17 17:46:54 +0100 | [diff] [blame] | 1176 | if (i == n) { |
| 1177 | /* |
| 1178 | * The whole buf is the same. |
| 1179 | * No reason to split it into chunks, so return now. |
| 1180 | */ |
| 1181 | *pnum = i; |
| 1182 | return !is_zero; |
| 1183 | } |
| 1184 | |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1185 | tail = (sector_num + i) & (alignment - 1); |
| 1186 | if (tail) { |
| 1187 | if (is_zero && i <= tail) { |
Vladimir Sementsov-Ogievskiy | 96054c7 | 2021-12-17 17:46:54 +0100 | [diff] [blame] | 1188 | /* |
| 1189 | * For sure next sector after i is data, and it will rewrite this |
| 1190 | * tail anyway due to RMW. So, let's just write data now. |
| 1191 | */ |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1192 | is_zero = false; |
| 1193 | } |
| 1194 | if (!is_zero) { |
Vladimir Sementsov-Ogievskiy | 96054c7 | 2021-12-17 17:46:54 +0100 | [diff] [blame] | 1195 | /* If possible, align up end offset of allocated areas. */ |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1196 | i += alignment - tail; |
| 1197 | i = MIN(i, n); |
| 1198 | } else { |
Vladimir Sementsov-Ogievskiy | 96054c7 | 2021-12-17 17:46:54 +0100 | [diff] [blame] | 1199 | /* |
| 1200 | * For sure next sector after i is data, and it will rewrite this |
| 1201 | * tail anyway due to RMW. Better is avoid RMW and write zeroes up |
| 1202 | * to aligned bound. |
| 1203 | */ |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1204 | i -= tail; |
| 1205 | } |
| 1206 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1207 | *pnum = i; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1208 | return !is_zero; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1211 | /* |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1212 | * Like is_allocated_sectors, but if the buffer starts with a used sector, |
| 1213 | * up to 'min' consecutive sectors containing zeros are ignored. This avoids |
| 1214 | * breaking up write requests for only small sparse areas. |
| 1215 | */ |
| 1216 | 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] | 1217 | int min, int64_t sector_num, int alignment) |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1218 | { |
| 1219 | int ret; |
| 1220 | int num_checked, num_used; |
| 1221 | |
| 1222 | if (n < min) { |
| 1223 | min = n; |
| 1224 | } |
| 1225 | |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1226 | ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment); |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1227 | if (!ret) { |
| 1228 | return ret; |
| 1229 | } |
| 1230 | |
| 1231 | num_used = *pnum; |
| 1232 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 1233 | n -= *pnum; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1234 | sector_num += *pnum; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1235 | num_checked = num_used; |
| 1236 | |
| 1237 | while (n > 0) { |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1238 | ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment); |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1239 | |
| 1240 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 1241 | n -= *pnum; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1242 | sector_num += *pnum; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1243 | num_checked += *pnum; |
| 1244 | if (ret) { |
| 1245 | num_used = num_checked; |
| 1246 | } else if (*pnum >= min) { |
| 1247 | break; |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | *pnum = num_used; |
| 1252 | return 1; |
| 1253 | } |
| 1254 | |
| 1255 | /* |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1256 | * Compares two buffers sector by sector. Returns 0 if the first |
| 1257 | * sector of each buffer matches, non-zero otherwise. |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1258 | * |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1259 | * pnum is set to the sector-aligned size of the buffer prefix that |
| 1260 | * has the same matching status as the first sector. |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1261 | */ |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1262 | static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2, |
| 1263 | int64_t bytes, int64_t *pnum) |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1264 | { |
Radim Krčmář | 8c1ac47 | 2015-02-20 17:06:15 +0100 | [diff] [blame] | 1265 | bool res; |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1266 | int64_t i = MIN(bytes, BDRV_SECTOR_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1267 | |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1268 | assert(bytes > 0); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1269 | |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1270 | res = !!memcmp(buf1, buf2, i); |
| 1271 | while (i < bytes) { |
| 1272 | int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE); |
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 | if (!!memcmp(buf1 + i, buf2 + i, len) != res) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1275 | break; |
| 1276 | } |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1277 | i += len; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | *pnum = i; |
| 1281 | return res; |
| 1282 | } |
| 1283 | |
Stefano Garzarella | 97ede57 | 2019-05-08 12:43:24 +0200 | [diff] [blame] | 1284 | #define IO_BUF_SIZE (2 * MiB) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1285 | |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1286 | /* |
| 1287 | * Check if passed sectors are empty (not allocated or contain only 0 bytes) |
| 1288 | * |
Eric Blake | 0608e40 | 2017-10-11 22:47:12 -0500 | [diff] [blame] | 1289 | * Intended for use by 'qemu-img compare': Returns 0 in case sectors are |
| 1290 | * filled with 0, 1 if sectors contain non-zero data (this is a comparison |
| 1291 | * failure), and 4 on error (the exit status for read errors), after emitting |
| 1292 | * an error message. |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1293 | * |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1294 | * @param blk: BlockBackend for the image |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1295 | * @param offset: Starting offset to check |
| 1296 | * @param bytes: Number of bytes to check |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1297 | * @param filename: Name of disk file we are checking (logging purpose) |
| 1298 | * @param buffer: Allocated buffer for storing read data |
| 1299 | * @param quiet: Flag for quiet mode |
| 1300 | */ |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1301 | static int check_empty_sectors(BlockBackend *blk, int64_t offset, |
| 1302 | int64_t bytes, const char *filename, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1303 | uint8_t *buffer, bool quiet) |
| 1304 | { |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1305 | int ret = 0; |
| 1306 | int64_t idx; |
| 1307 | |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1308 | ret = blk_pread(blk, offset, buffer, bytes); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1309 | if (ret < 0) { |
| 1310 | error_report("Error while reading offset %" PRId64 " of %s: %s", |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1311 | offset, filename, strerror(-ret)); |
Eric Blake | 0608e40 | 2017-10-11 22:47:12 -0500 | [diff] [blame] | 1312 | return 4; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1313 | } |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1314 | idx = find_nonzero(buffer, bytes); |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1315 | if (idx >= 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1316 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1317 | offset + idx); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1318 | return 1; |
| 1319 | } |
| 1320 | |
| 1321 | return 0; |
| 1322 | } |
| 1323 | |
| 1324 | /* |
| 1325 | * Compares two images. Exit codes: |
| 1326 | * |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 1327 | * 0 - Images are identical or the requested help was printed |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1328 | * 1 - Images differ |
| 1329 | * >1 - Error occurred |
| 1330 | */ |
| 1331 | static int img_compare(int argc, char **argv) |
| 1332 | { |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1333 | const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1334 | BlockBackend *blk1, *blk2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1335 | BlockDriverState *bs1, *bs2; |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1336 | int64_t total_size1, total_size2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1337 | uint8_t *buf1 = NULL, *buf2 = NULL; |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1338 | int64_t pnum1, pnum2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1339 | int allocated1, allocated2; |
| 1340 | int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */ |
| 1341 | bool progress = false, quiet = false, strict = false; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1342 | int flags; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1343 | bool writethrough; |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1344 | int64_t total_size; |
| 1345 | int64_t offset = 0; |
| 1346 | int64_t chunk; |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1347 | int c; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1348 | uint64_t progress_base; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1349 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1350 | bool force_share = false; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1351 | |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1352 | cache = BDRV_DEFAULT_CACHE; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1353 | for (;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1354 | static const struct option long_options[] = { |
| 1355 | {"help", no_argument, 0, 'h'}, |
| 1356 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1357 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1358 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1359 | {0, 0, 0, 0} |
| 1360 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1361 | c = getopt_long(argc, argv, ":hf:F:T:pqsU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1362 | long_options, NULL); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1363 | if (c == -1) { |
| 1364 | break; |
| 1365 | } |
| 1366 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 1367 | case ':': |
| 1368 | missing_argument(argv[optind - 1]); |
| 1369 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1370 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 1371 | unrecognized_option(argv[optind - 1]); |
| 1372 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1373 | case 'h': |
| 1374 | help(); |
| 1375 | break; |
| 1376 | case 'f': |
| 1377 | fmt1 = optarg; |
| 1378 | break; |
| 1379 | case 'F': |
| 1380 | fmt2 = optarg; |
| 1381 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1382 | case 'T': |
| 1383 | cache = optarg; |
| 1384 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1385 | case 'p': |
| 1386 | progress = true; |
| 1387 | break; |
| 1388 | case 'q': |
| 1389 | quiet = true; |
| 1390 | break; |
| 1391 | case 's': |
| 1392 | strict = true; |
| 1393 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1394 | case 'U': |
| 1395 | force_share = true; |
| 1396 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 1397 | case OPTION_OBJECT: |
| 1398 | { |
| 1399 | Error *local_err = NULL; |
| 1400 | |
| 1401 | if (!user_creatable_add_from_str(optarg, &local_err)) { |
| 1402 | if (local_err) { |
| 1403 | error_report_err(local_err); |
| 1404 | exit(2); |
| 1405 | } else { |
| 1406 | /* Help was printed */ |
| 1407 | exit(EXIT_SUCCESS); |
| 1408 | } |
| 1409 | } |
| 1410 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1411 | } |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1412 | case OPTION_IMAGE_OPTS: |
| 1413 | image_opts = true; |
| 1414 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | /* Progress is not shown in Quiet mode */ |
| 1419 | if (quiet) { |
| 1420 | progress = false; |
| 1421 | } |
| 1422 | |
| 1423 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 1424 | if (optind != argc - 2) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 1425 | error_exit("Expecting two image file names"); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1426 | } |
| 1427 | filename1 = argv[optind++]; |
| 1428 | filename2 = argv[optind++]; |
| 1429 | |
Stefan Hajnoczi | cbda016 | 2014-08-26 19:17:55 +0100 | [diff] [blame] | 1430 | /* Initialize before goto out */ |
| 1431 | qemu_progress_init(progress, 2.0); |
| 1432 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1433 | flags = 0; |
| 1434 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1435 | if (ret < 0) { |
| 1436 | error_report("Invalid source cache option: %s", cache); |
| 1437 | ret = 2; |
| 1438 | goto out3; |
| 1439 | } |
| 1440 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1441 | blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet, |
| 1442 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1443 | if (!blk1) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1444 | ret = 2; |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1445 | goto out3; |
| 1446 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1447 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1448 | blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet, |
| 1449 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1450 | if (!blk2) { |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1451 | ret = 2; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1452 | goto out2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1453 | } |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1454 | bs1 = blk_bs(blk1); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1455 | bs2 = blk_bs(blk2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1456 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1457 | buf1 = blk_blockalign(blk1, IO_BUF_SIZE); |
| 1458 | buf2 = blk_blockalign(blk2, IO_BUF_SIZE); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1459 | total_size1 = blk_getlength(blk1); |
| 1460 | if (total_size1 < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1461 | error_report("Can't get size of %s: %s", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1462 | filename1, strerror(-total_size1)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1463 | ret = 4; |
| 1464 | goto out; |
| 1465 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1466 | total_size2 = blk_getlength(blk2); |
| 1467 | if (total_size2 < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1468 | error_report("Can't get size of %s: %s", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1469 | filename2, strerror(-total_size2)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1470 | ret = 4; |
| 1471 | goto out; |
| 1472 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1473 | total_size = MIN(total_size1, total_size2); |
| 1474 | progress_base = MAX(total_size1, total_size2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1475 | |
| 1476 | qemu_progress_print(0, 100); |
| 1477 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1478 | if (strict && total_size1 != total_size2) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1479 | ret = 1; |
| 1480 | qprintf(quiet, "Strict mode: Image size mismatch!\n"); |
| 1481 | goto out; |
| 1482 | } |
| 1483 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1484 | while (offset < total_size) { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1485 | int status1, status2; |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 1486 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1487 | status1 = bdrv_block_status_above(bs1, NULL, offset, |
| 1488 | total_size1 - offset, &pnum1, NULL, |
| 1489 | NULL); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1490 | if (status1 < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1491 | ret = 3; |
| 1492 | error_report("Sector allocation test failed for %s", filename1); |
| 1493 | goto out; |
| 1494 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1495 | allocated1 = status1 & BDRV_BLOCK_ALLOCATED; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1496 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1497 | status2 = bdrv_block_status_above(bs2, NULL, offset, |
| 1498 | total_size2 - offset, &pnum2, NULL, |
| 1499 | NULL); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1500 | if (status2 < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1501 | ret = 3; |
| 1502 | error_report("Sector allocation test failed for %s", filename2); |
| 1503 | goto out; |
| 1504 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1505 | allocated2 = status2 & BDRV_BLOCK_ALLOCATED; |
Eric Blake | 7daddc6 | 2017-10-11 22:47:09 -0500 | [diff] [blame] | 1506 | |
| 1507 | assert(pnum1 && pnum2); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1508 | chunk = MIN(pnum1, pnum2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1509 | |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1510 | if (strict) { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1511 | if (status1 != status2) { |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1512 | ret = 1; |
| 1513 | qprintf(quiet, "Strict mode: Offset %" PRId64 |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1514 | " block status mismatch!\n", offset); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1515 | goto out; |
| 1516 | } |
| 1517 | } |
| 1518 | if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) { |
Eric Blake | 7daddc6 | 2017-10-11 22:47:09 -0500 | [diff] [blame] | 1519 | /* nothing to do */ |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1520 | } else if (allocated1 == allocated2) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1521 | if (allocated1) { |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1522 | int64_t pnum; |
| 1523 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1524 | chunk = MIN(chunk, IO_BUF_SIZE); |
| 1525 | ret = blk_pread(blk1, offset, buf1, chunk); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1526 | if (ret < 0) { |
| 1527 | error_report("Error while reading offset %" PRId64 |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1528 | " of %s: %s", |
| 1529 | offset, filename1, strerror(-ret)); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1530 | ret = 4; |
| 1531 | goto out; |
| 1532 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1533 | ret = blk_pread(blk2, offset, buf2, chunk); |
| 1534 | if (ret < 0) { |
| 1535 | error_report("Error while reading offset %" PRId64 |
| 1536 | " of %s: %s", |
| 1537 | offset, filename2, strerror(-ret)); |
| 1538 | ret = 4; |
| 1539 | goto out; |
| 1540 | } |
| 1541 | ret = compare_buffers(buf1, buf2, chunk, &pnum); |
| 1542 | if (ret || pnum != chunk) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1543 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1544 | offset + (ret ? 0 : pnum)); |
Fam Zheng | 36452f1 | 2013-11-13 20:26:49 +0800 | [diff] [blame] | 1545 | ret = 1; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1546 | goto out; |
| 1547 | } |
| 1548 | } |
| 1549 | } else { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1550 | chunk = MIN(chunk, IO_BUF_SIZE); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1551 | if (allocated1) { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1552 | ret = check_empty_sectors(blk1, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1553 | filename1, buf1, quiet); |
| 1554 | } else { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1555 | ret = check_empty_sectors(blk2, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1556 | filename2, buf1, quiet); |
| 1557 | } |
| 1558 | if (ret) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1559 | goto out; |
| 1560 | } |
| 1561 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1562 | offset += chunk; |
| 1563 | qemu_progress_print(((float) chunk / progress_base) * 100, 100); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1564 | } |
| 1565 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1566 | if (total_size1 != total_size2) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1567 | BlockBackend *blk_over; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1568 | const char *filename_over; |
| 1569 | |
| 1570 | qprintf(quiet, "Warning: Image size mismatch!\n"); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1571 | if (total_size1 > total_size2) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1572 | blk_over = blk1; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1573 | filename_over = filename1; |
| 1574 | } else { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1575 | blk_over = blk2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1576 | filename_over = filename2; |
| 1577 | } |
| 1578 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1579 | while (offset < progress_base) { |
| 1580 | ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset, |
| 1581 | progress_base - offset, &chunk, |
| 1582 | NULL, NULL); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1583 | if (ret < 0) { |
| 1584 | ret = 3; |
| 1585 | error_report("Sector allocation test failed for %s", |
| 1586 | filename_over); |
| 1587 | goto out; |
| 1588 | |
| 1589 | } |
Eric Blake | 391cb1a | 2017-10-11 22:47:10 -0500 | [diff] [blame] | 1590 | if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1591 | chunk = MIN(chunk, IO_BUF_SIZE); |
| 1592 | ret = check_empty_sectors(blk_over, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1593 | filename_over, buf1, quiet); |
| 1594 | if (ret) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1595 | goto out; |
| 1596 | } |
| 1597 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1598 | offset += chunk; |
| 1599 | qemu_progress_print(((float) chunk / progress_base) * 100, 100); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | qprintf(quiet, "Images are identical.\n"); |
| 1604 | ret = 0; |
| 1605 | |
| 1606 | out: |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1607 | qemu_vfree(buf1); |
| 1608 | qemu_vfree(buf2); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1609 | blk_unref(blk2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1610 | out2: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1611 | blk_unref(blk1); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1612 | out3: |
| 1613 | qemu_progress_end(); |
| 1614 | return ret; |
| 1615 | } |
| 1616 | |
Eric Blake | 6c729dd | 2020-05-21 14:21:35 -0500 | [diff] [blame] | 1617 | /* Convenience wrapper around qmp_block_dirty_bitmap_merge */ |
| 1618 | static void do_dirty_bitmap_merge(const char *dst_node, const char *dst_name, |
| 1619 | const char *src_node, const char *src_name, |
| 1620 | Error **errp) |
| 1621 | { |
| 1622 | BlockDirtyBitmapMergeSource *merge_src; |
Eric Blake | 54aa3de | 2020-11-12 19:13:37 -0600 | [diff] [blame] | 1623 | BlockDirtyBitmapMergeSourceList *list = NULL; |
Eric Blake | 6c729dd | 2020-05-21 14:21:35 -0500 | [diff] [blame] | 1624 | |
| 1625 | merge_src = g_new0(BlockDirtyBitmapMergeSource, 1); |
| 1626 | merge_src->type = QTYPE_QDICT; |
| 1627 | merge_src->u.external.node = g_strdup(src_node); |
| 1628 | merge_src->u.external.name = g_strdup(src_name); |
Eric Blake | 54aa3de | 2020-11-12 19:13:37 -0600 | [diff] [blame] | 1629 | QAPI_LIST_PREPEND(list, merge_src); |
Eric Blake | 6c729dd | 2020-05-21 14:21:35 -0500 | [diff] [blame] | 1630 | qmp_block_dirty_bitmap_merge(dst_node, dst_name, list, errp); |
| 1631 | qapi_free_BlockDirtyBitmapMergeSourceList(list); |
| 1632 | } |
| 1633 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1634 | enum ImgConvertBlockStatus { |
| 1635 | BLK_DATA, |
| 1636 | BLK_ZERO, |
| 1637 | BLK_BACKING_FILE, |
| 1638 | }; |
| 1639 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1640 | #define MAX_COROUTINES 16 |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 1641 | #define CONVERT_THROTTLE_GROUP "img_convert" |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1642 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1643 | typedef struct ImgConvertState { |
| 1644 | BlockBackend **src; |
| 1645 | int64_t *src_sectors; |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 1646 | int *src_alignment; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1647 | int src_num; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1648 | int64_t total_sectors; |
| 1649 | int64_t allocated_sectors; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1650 | int64_t allocated_done; |
| 1651 | int64_t sector_num; |
| 1652 | int64_t wr_offs; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1653 | enum ImgConvertBlockStatus status; |
| 1654 | int64_t sector_next_status; |
| 1655 | BlockBackend *target; |
| 1656 | bool has_zero_init; |
| 1657 | bool compressed; |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 1658 | bool target_is_new; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1659 | bool target_has_backing; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1660 | int64_t target_backing_sectors; /* negative if unknown */ |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1661 | bool wr_in_order; |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1662 | bool copy_range; |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1663 | bool salvage; |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 1664 | bool quiet; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1665 | int min_sparse; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1666 | int alignment; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1667 | size_t cluster_sectors; |
| 1668 | size_t buf_sectors; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 1669 | long num_coroutines; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1670 | int running_coroutines; |
| 1671 | Coroutine *co[MAX_COROUTINES]; |
| 1672 | int64_t wait_sector_num[MAX_COROUTINES]; |
| 1673 | CoMutex lock; |
| 1674 | int ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1675 | } ImgConvertState; |
| 1676 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1677 | static void convert_select_part(ImgConvertState *s, int64_t sector_num, |
| 1678 | int *src_cur, int64_t *src_cur_offset) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1679 | { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1680 | *src_cur = 0; |
| 1681 | *src_cur_offset = 0; |
| 1682 | while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) { |
| 1683 | *src_cur_offset += s->src_sectors[*src_cur]; |
| 1684 | (*src_cur)++; |
| 1685 | assert(*src_cur < s->src_num); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num) |
| 1690 | { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1691 | int64_t src_cur_offset; |
| 1692 | int ret, n, src_cur; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1693 | bool post_backing_zero = false; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1694 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1695 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1696 | |
| 1697 | assert(s->total_sectors > sector_num); |
| 1698 | n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS); |
| 1699 | |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1700 | if (s->target_backing_sectors >= 0) { |
| 1701 | if (sector_num >= s->target_backing_sectors) { |
Vladimir Sementsov-Ogievskiy | 2253d86 | 2020-05-28 12:43:56 +0300 | [diff] [blame] | 1702 | post_backing_zero = true; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1703 | } else if (sector_num + n > s->target_backing_sectors) { |
| 1704 | /* Split requests around target_backing_sectors (because |
| 1705 | * starting from there, zeros are handled differently) */ |
| 1706 | n = s->target_backing_sectors - sector_num; |
| 1707 | } |
| 1708 | } |
| 1709 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1710 | if (s->sector_next_status <= sector_num) { |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1711 | uint64_t offset = (sector_num - src_cur_offset) * BDRV_SECTOR_SIZE; |
| 1712 | int64_t count; |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 1713 | int tail; |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 1714 | BlockDriverState *src_bs = blk_bs(s->src[src_cur]); |
| 1715 | BlockDriverState *base; |
| 1716 | |
| 1717 | if (s->target_has_backing) { |
| 1718 | base = bdrv_cow_bs(bdrv_skip_filters(src_bs)); |
| 1719 | } else { |
| 1720 | base = NULL; |
| 1721 | } |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1722 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1723 | do { |
| 1724 | count = n * BDRV_SECTOR_SIZE; |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 1725 | |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 1726 | ret = bdrv_block_status_above(src_bs, base, offset, count, &count, |
| 1727 | NULL, NULL); |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1728 | |
| 1729 | if (ret < 0) { |
| 1730 | if (s->salvage) { |
| 1731 | if (n == 1) { |
| 1732 | if (!s->quiet) { |
| 1733 | warn_report("error while reading block status at " |
| 1734 | "offset %" PRIu64 ": %s", offset, |
| 1735 | strerror(-ret)); |
| 1736 | } |
| 1737 | /* Just try to read the data, then */ |
| 1738 | ret = BDRV_BLOCK_DATA; |
| 1739 | count = BDRV_SECTOR_SIZE; |
| 1740 | } else { |
| 1741 | /* Retry on a shorter range */ |
| 1742 | n = DIV_ROUND_UP(n, 4); |
| 1743 | } |
| 1744 | } else { |
| 1745 | error_report("error while reading block status at offset " |
| 1746 | "%" PRIu64 ": %s", offset, strerror(-ret)); |
| 1747 | return ret; |
| 1748 | } |
| 1749 | } |
| 1750 | } while (ret < 0); |
| 1751 | |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1752 | n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1753 | |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 1754 | /* |
| 1755 | * Avoid that s->sector_next_status becomes unaligned to the source |
| 1756 | * request alignment and/or cluster size to avoid unnecessary read |
| 1757 | * cycles. |
| 1758 | */ |
| 1759 | tail = (sector_num - src_cur_offset + n) % s->src_alignment[src_cur]; |
| 1760 | if (n > tail) { |
| 1761 | n -= tail; |
| 1762 | } |
| 1763 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1764 | if (ret & BDRV_BLOCK_ZERO) { |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1765 | s->status = post_backing_zero ? BLK_BACKING_FILE : BLK_ZERO; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1766 | } else if (ret & BDRV_BLOCK_DATA) { |
| 1767 | s->status = BLK_DATA; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1768 | } else { |
Vladimir Sementsov-Ogievskiy | 9f1b92a | 2017-04-07 14:34:04 +0300 | [diff] [blame] | 1769 | s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1770 | } |
| 1771 | |
| 1772 | s->sector_next_status = sector_num + n; |
| 1773 | } |
| 1774 | |
| 1775 | n = MIN(n, s->sector_next_status - sector_num); |
| 1776 | if (s->status == BLK_DATA) { |
| 1777 | n = MIN(n, s->buf_sectors); |
| 1778 | } |
| 1779 | |
| 1780 | /* We need to write complete clusters for compressed images, so if an |
| 1781 | * unallocated area is shorter than that, we must consider the whole |
| 1782 | * cluster allocated. */ |
| 1783 | if (s->compressed) { |
| 1784 | if (n < s->cluster_sectors) { |
| 1785 | n = MIN(s->cluster_sectors, s->total_sectors - sector_num); |
| 1786 | s->status = BLK_DATA; |
| 1787 | } else { |
| 1788 | n = QEMU_ALIGN_DOWN(n, s->cluster_sectors); |
| 1789 | } |
| 1790 | } |
| 1791 | |
| 1792 | return n; |
| 1793 | } |
| 1794 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1795 | static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num, |
| 1796 | int nb_sectors, uint8_t *buf) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1797 | { |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1798 | uint64_t single_read_until = 0; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1799 | int n, ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1800 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1801 | assert(nb_sectors <= s->buf_sectors); |
| 1802 | while (nb_sectors > 0) { |
| 1803 | BlockBackend *blk; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1804 | int src_cur; |
| 1805 | int64_t bs_sectors, src_cur_offset; |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1806 | uint64_t offset; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1807 | |
| 1808 | /* In the case of compression with multiple source files, we can get a |
| 1809 | * nb_sectors that spreads into the next part. So we must be able to |
| 1810 | * read across multiple BDSes for one convert_read() call. */ |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1811 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
| 1812 | blk = s->src[src_cur]; |
| 1813 | bs_sectors = s->src_sectors[src_cur]; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1814 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1815 | offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1816 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1817 | n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset)); |
| 1818 | if (single_read_until > offset) { |
| 1819 | n = 1; |
| 1820 | } |
| 1821 | |
| 1822 | ret = blk_co_pread(blk, offset, n << BDRV_SECTOR_BITS, buf, 0); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1823 | if (ret < 0) { |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1824 | if (s->salvage) { |
| 1825 | if (n > 1) { |
| 1826 | single_read_until = offset + (n << BDRV_SECTOR_BITS); |
| 1827 | continue; |
| 1828 | } else { |
| 1829 | if (!s->quiet) { |
| 1830 | warn_report("error while reading offset %" PRIu64 |
| 1831 | ": %s", offset, strerror(-ret)); |
| 1832 | } |
| 1833 | memset(buf, 0, BDRV_SECTOR_SIZE); |
| 1834 | } |
| 1835 | } else { |
| 1836 | return ret; |
| 1837 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | sector_num += n; |
| 1841 | nb_sectors -= n; |
| 1842 | buf += n * BDRV_SECTOR_SIZE; |
| 1843 | } |
| 1844 | |
| 1845 | return 0; |
| 1846 | } |
| 1847 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1848 | |
| 1849 | static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num, |
| 1850 | int nb_sectors, uint8_t *buf, |
| 1851 | enum ImgConvertBlockStatus status) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1852 | { |
| 1853 | int ret; |
| 1854 | |
| 1855 | while (nb_sectors > 0) { |
| 1856 | int n = nb_sectors; |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1857 | BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0; |
| 1858 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1859 | switch (status) { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1860 | case BLK_BACKING_FILE: |
| 1861 | /* If we have a backing file, leave clusters unallocated that are |
| 1862 | * unallocated in the source image, so that the backing file is |
| 1863 | * visible at the respective offset. */ |
| 1864 | assert(s->target_has_backing); |
| 1865 | break; |
| 1866 | |
| 1867 | case BLK_DATA: |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1868 | /* If we're told to keep the target fully allocated (-S 0) or there |
| 1869 | * is real non-zero data, we must write it. Otherwise we can treat |
| 1870 | * it as zero sectors. |
| 1871 | * Compressed clusters need to be written as a whole, so in that |
| 1872 | * case we can only save the write if the buffer is completely |
| 1873 | * zeroed. */ |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1874 | if (!s->min_sparse || |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1875 | (!s->compressed && |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1876 | is_allocated_sectors_min(buf, n, &n, s->min_sparse, |
| 1877 | sector_num, s->alignment)) || |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1878 | (s->compressed && |
| 1879 | !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1880 | { |
Vladimir Sementsov-Ogievskiy | 265a7e5 | 2019-04-22 17:58:38 +0300 | [diff] [blame] | 1881 | ret = blk_co_pwrite(s->target, sector_num << BDRV_SECTOR_BITS, |
| 1882 | n << BDRV_SECTOR_BITS, buf, flags); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1883 | if (ret < 0) { |
| 1884 | return ret; |
| 1885 | } |
| 1886 | break; |
| 1887 | } |
| 1888 | /* fall-through */ |
| 1889 | |
| 1890 | case BLK_ZERO: |
| 1891 | if (s->has_zero_init) { |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1892 | assert(!s->target_has_backing); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1893 | break; |
| 1894 | } |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1895 | ret = blk_co_pwrite_zeroes(s->target, |
| 1896 | sector_num << BDRV_SECTOR_BITS, |
Nir Soffer | a3d6ae2 | 2019-03-24 02:20:12 +0200 | [diff] [blame] | 1897 | n << BDRV_SECTOR_BITS, |
| 1898 | BDRV_REQ_MAY_UNMAP); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1899 | if (ret < 0) { |
| 1900 | return ret; |
| 1901 | } |
| 1902 | break; |
| 1903 | } |
| 1904 | |
| 1905 | sector_num += n; |
| 1906 | nb_sectors -= n; |
| 1907 | buf += n * BDRV_SECTOR_SIZE; |
| 1908 | } |
| 1909 | |
| 1910 | return 0; |
| 1911 | } |
| 1912 | |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1913 | static int coroutine_fn convert_co_copy_range(ImgConvertState *s, int64_t sector_num, |
| 1914 | int nb_sectors) |
| 1915 | { |
| 1916 | int n, ret; |
| 1917 | |
| 1918 | while (nb_sectors > 0) { |
| 1919 | BlockBackend *blk; |
| 1920 | int src_cur; |
| 1921 | int64_t bs_sectors, src_cur_offset; |
| 1922 | int64_t offset; |
| 1923 | |
| 1924 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
| 1925 | offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS; |
| 1926 | blk = s->src[src_cur]; |
| 1927 | bs_sectors = s->src_sectors[src_cur]; |
| 1928 | |
| 1929 | n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset)); |
| 1930 | |
| 1931 | ret = blk_co_copy_range(blk, offset, s->target, |
| 1932 | sector_num << BDRV_SECTOR_BITS, |
Vladimir Sementsov-Ogievskiy | 67b51fb | 2018-07-09 19:37:17 +0300 | [diff] [blame] | 1933 | n << BDRV_SECTOR_BITS, 0, 0); |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1934 | if (ret < 0) { |
| 1935 | return ret; |
| 1936 | } |
| 1937 | |
| 1938 | sector_num += n; |
| 1939 | nb_sectors -= n; |
| 1940 | } |
| 1941 | return 0; |
| 1942 | } |
| 1943 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1944 | static void coroutine_fn convert_co_do_copy(void *opaque) |
| 1945 | { |
| 1946 | ImgConvertState *s = opaque; |
| 1947 | uint8_t *buf = NULL; |
| 1948 | int ret, i; |
| 1949 | int index = -1; |
| 1950 | |
| 1951 | for (i = 0; i < s->num_coroutines; i++) { |
| 1952 | if (s->co[i] == qemu_coroutine_self()) { |
| 1953 | index = i; |
| 1954 | break; |
| 1955 | } |
| 1956 | } |
| 1957 | assert(index >= 0); |
| 1958 | |
| 1959 | s->running_coroutines++; |
| 1960 | buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE); |
| 1961 | |
| 1962 | while (1) { |
| 1963 | int n; |
| 1964 | int64_t sector_num; |
| 1965 | enum ImgConvertBlockStatus status; |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1966 | bool copy_range; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1967 | |
| 1968 | qemu_co_mutex_lock(&s->lock); |
| 1969 | if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) { |
| 1970 | qemu_co_mutex_unlock(&s->lock); |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1971 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1972 | } |
| 1973 | n = convert_iteration_sectors(s, s->sector_num); |
| 1974 | if (n < 0) { |
| 1975 | qemu_co_mutex_unlock(&s->lock); |
| 1976 | s->ret = n; |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1977 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1978 | } |
| 1979 | /* save current sector and allocation status to local variables */ |
| 1980 | sector_num = s->sector_num; |
| 1981 | status = s->status; |
| 1982 | if (!s->min_sparse && s->status == BLK_ZERO) { |
| 1983 | n = MIN(n, s->buf_sectors); |
| 1984 | } |
| 1985 | /* increment global sector counter so that other coroutines can |
| 1986 | * already continue reading beyond this request */ |
| 1987 | s->sector_num += n; |
| 1988 | qemu_co_mutex_unlock(&s->lock); |
| 1989 | |
| 1990 | if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) { |
| 1991 | s->allocated_done += n; |
| 1992 | qemu_progress_print(100.0 * s->allocated_done / |
| 1993 | s->allocated_sectors, 0); |
| 1994 | } |
| 1995 | |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1996 | retry: |
| 1997 | copy_range = s->copy_range && s->status == BLK_DATA; |
| 1998 | if (status == BLK_DATA && !copy_range) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1999 | ret = convert_co_read(s, sector_num, n, buf); |
| 2000 | if (ret < 0) { |
Eric Blake | 39f77cb | 2020-04-02 08:57:17 -0500 | [diff] [blame] | 2001 | error_report("error while reading at byte %lld: %s", |
| 2002 | sector_num * BDRV_SECTOR_SIZE, strerror(-ret)); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2003 | s->ret = ret; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2004 | } |
| 2005 | } else if (!s->min_sparse && status == BLK_ZERO) { |
| 2006 | status = BLK_DATA; |
| 2007 | memset(buf, 0x00, n * BDRV_SECTOR_SIZE); |
| 2008 | } |
| 2009 | |
| 2010 | if (s->wr_in_order) { |
| 2011 | /* keep writes in order */ |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2012 | while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2013 | s->wait_sector_num[index] = sector_num; |
| 2014 | qemu_coroutine_yield(); |
| 2015 | } |
| 2016 | s->wait_sector_num[index] = -1; |
| 2017 | } |
| 2018 | |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2019 | if (s->ret == -EINPROGRESS) { |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 2020 | if (copy_range) { |
| 2021 | ret = convert_co_copy_range(s, sector_num, n); |
| 2022 | if (ret) { |
| 2023 | s->copy_range = false; |
| 2024 | goto retry; |
| 2025 | } |
| 2026 | } else { |
| 2027 | ret = convert_co_write(s, sector_num, n, buf, status); |
| 2028 | } |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2029 | if (ret < 0) { |
Eric Blake | 39f77cb | 2020-04-02 08:57:17 -0500 | [diff] [blame] | 2030 | error_report("error while writing at byte %lld: %s", |
| 2031 | sector_num * BDRV_SECTOR_SIZE, strerror(-ret)); |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2032 | s->ret = ret; |
| 2033 | } |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2034 | } |
| 2035 | |
| 2036 | if (s->wr_in_order) { |
| 2037 | /* reenter the coroutine that might have waited |
| 2038 | * for this write to complete */ |
| 2039 | s->wr_offs = sector_num + n; |
| 2040 | for (i = 0; i < s->num_coroutines; i++) { |
| 2041 | if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) { |
| 2042 | /* |
| 2043 | * A -> B -> A cannot occur because A has |
| 2044 | * s->wait_sector_num[i] == -1 during A -> B. Therefore |
| 2045 | * B will never enter A during this time window. |
| 2046 | */ |
| 2047 | qemu_coroutine_enter(s->co[i]); |
| 2048 | break; |
| 2049 | } |
| 2050 | } |
| 2051 | } |
| 2052 | } |
| 2053 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2054 | qemu_vfree(buf); |
| 2055 | s->co[index] = NULL; |
| 2056 | s->running_coroutines--; |
| 2057 | if (!s->running_coroutines && s->ret == -EINPROGRESS) { |
| 2058 | /* the convert job finished successfully */ |
| 2059 | s->ret = 0; |
| 2060 | } |
| 2061 | } |
| 2062 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2063 | static int convert_do_copy(ImgConvertState *s) |
| 2064 | { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2065 | int ret, i, n; |
| 2066 | int64_t sector_num = 0; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2067 | |
| 2068 | /* Check whether we have zero initialisation or can get it efficiently */ |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2069 | if (!s->has_zero_init && s->target_is_new && s->min_sparse && |
| 2070 | !s->target_has_backing) { |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 2071 | s->has_zero_init = bdrv_has_zero_init(blk_bs(s->target)); |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 2072 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2073 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2074 | /* Allocate buffer for copied data. For compressed images, only one cluster |
| 2075 | * can be copied at a time. */ |
| 2076 | if (s->compressed) { |
| 2077 | if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) { |
| 2078 | error_report("invalid cluster size"); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2079 | return -EINVAL; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2080 | } |
| 2081 | s->buf_sectors = s->cluster_sectors; |
| 2082 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2083 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2084 | while (sector_num < s->total_sectors) { |
| 2085 | n = convert_iteration_sectors(s, sector_num); |
| 2086 | if (n < 0) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2087 | return n; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2088 | } |
Max Reitz | aad15de | 2016-03-24 23:33:57 +0100 | [diff] [blame] | 2089 | if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO)) |
| 2090 | { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2091 | s->allocated_sectors += n; |
| 2092 | } |
| 2093 | sector_num += n; |
| 2094 | } |
| 2095 | |
| 2096 | /* Do the copy */ |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2097 | s->sector_next_status = 0; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2098 | s->ret = -EINPROGRESS; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2099 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2100 | qemu_co_mutex_init(&s->lock); |
| 2101 | for (i = 0; i < s->num_coroutines; i++) { |
| 2102 | s->co[i] = qemu_coroutine_create(convert_co_do_copy, s); |
| 2103 | s->wait_sector_num[i] = -1; |
| 2104 | qemu_coroutine_enter(s->co[i]); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2105 | } |
| 2106 | |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2107 | while (s->running_coroutines) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2108 | main_loop_wait(false); |
| 2109 | } |
| 2110 | |
| 2111 | if (s->compressed && !s->ret) { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2112 | /* signal EOF to align */ |
Pavel Butsykin | fe5c135 | 2016-07-22 11:17:40 +0300 | [diff] [blame] | 2113 | ret = blk_pwrite_compressed(s->target, 0, NULL, 0); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2114 | if (ret < 0) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2115 | return ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2116 | } |
| 2117 | } |
| 2118 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2119 | return s->ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2120 | } |
| 2121 | |
Eric Blake | 74a4320 | 2021-07-09 10:39:50 -0500 | [diff] [blame] | 2122 | /* Check that bitmaps can be copied, or output an error */ |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2123 | static int convert_check_bitmaps(BlockDriverState *src, bool skip_broken) |
Eric Blake | 74a4320 | 2021-07-09 10:39:50 -0500 | [diff] [blame] | 2124 | { |
| 2125 | BdrvDirtyBitmap *bm; |
| 2126 | |
| 2127 | if (!bdrv_supports_persistent_dirty_bitmap(src)) { |
| 2128 | error_report("Source lacks bitmap support"); |
| 2129 | return -1; |
| 2130 | } |
| 2131 | FOR_EACH_DIRTY_BITMAP(src, bm) { |
| 2132 | if (!bdrv_dirty_bitmap_get_persistence(bm)) { |
| 2133 | continue; |
| 2134 | } |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2135 | if (!skip_broken && bdrv_dirty_bitmap_inconsistent(bm)) { |
Eric Blake | 74a4320 | 2021-07-09 10:39:50 -0500 | [diff] [blame] | 2136 | error_report("Cannot copy inconsistent bitmap '%s'", |
| 2137 | bdrv_dirty_bitmap_name(bm)); |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2138 | error_printf("Try --skip-broken-bitmaps, or " |
| 2139 | "use 'qemu-img bitmap --remove' to delete it\n"); |
Eric Blake | 74a4320 | 2021-07-09 10:39:50 -0500 | [diff] [blame] | 2140 | return -1; |
| 2141 | } |
| 2142 | } |
| 2143 | return 0; |
| 2144 | } |
| 2145 | |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2146 | static int convert_copy_bitmaps(BlockDriverState *src, BlockDriverState *dst, |
| 2147 | bool skip_broken) |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2148 | { |
| 2149 | BdrvDirtyBitmap *bm; |
| 2150 | Error *err = NULL; |
| 2151 | |
| 2152 | FOR_EACH_DIRTY_BITMAP(src, bm) { |
| 2153 | const char *name; |
| 2154 | |
| 2155 | if (!bdrv_dirty_bitmap_get_persistence(bm)) { |
| 2156 | continue; |
| 2157 | } |
| 2158 | name = bdrv_dirty_bitmap_name(bm); |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2159 | if (skip_broken && bdrv_dirty_bitmap_inconsistent(bm)) { |
| 2160 | warn_report("Skipping inconsistent bitmap '%s'", name); |
| 2161 | continue; |
| 2162 | } |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2163 | qmp_block_dirty_bitmap_add(dst->node_name, name, |
| 2164 | true, bdrv_dirty_bitmap_granularity(bm), |
| 2165 | true, true, |
| 2166 | true, !bdrv_dirty_bitmap_enabled(bm), |
| 2167 | &err); |
| 2168 | if (err) { |
| 2169 | error_reportf_err(err, "Failed to create bitmap %s: ", name); |
| 2170 | return -1; |
| 2171 | } |
| 2172 | |
| 2173 | do_dirty_bitmap_merge(dst->node_name, name, src->node_name, name, |
| 2174 | &err); |
| 2175 | if (err) { |
| 2176 | error_reportf_err(err, "Failed to populate bitmap %s: ", name); |
Eric Blake | 74a4320 | 2021-07-09 10:39:50 -0500 | [diff] [blame] | 2177 | qmp_block_dirty_bitmap_remove(dst->node_name, name, NULL); |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2178 | return -1; |
| 2179 | } |
| 2180 | } |
| 2181 | |
| 2182 | return 0; |
| 2183 | } |
| 2184 | |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 2185 | #define MAX_BUF_SECTORS 32768 |
| 2186 | |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 2187 | static void set_rate_limit(BlockBackend *blk, int64_t rate_limit) |
| 2188 | { |
| 2189 | ThrottleConfig cfg; |
| 2190 | |
| 2191 | throttle_config_init(&cfg); |
| 2192 | cfg.buckets[THROTTLE_BPS_WRITE].avg = rate_limit; |
| 2193 | |
| 2194 | blk_io_limits_enable(blk, CONVERT_THROTTLE_GROUP); |
| 2195 | blk_set_io_limits(blk, &cfg); |
| 2196 | } |
| 2197 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2198 | static int img_convert(int argc, char **argv) |
| 2199 | { |
Kevin Wolf | 0b8fb55 | 2021-04-22 18:43:44 +0200 | [diff] [blame] | 2200 | int c, bs_i, flags, src_flags = BDRV_O_NO_SHARE; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2201 | const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe", |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2202 | *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL, |
Eric Blake | 1899bf4 | 2021-09-13 08:17:35 -0500 | [diff] [blame] | 2203 | *out_filename, *out_baseimg_param, *snapshot_name = NULL, |
| 2204 | *backing_fmt = NULL; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2205 | BlockDriver *drv = NULL, *proto_drv = NULL; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2206 | BlockDriverInfo bdi; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2207 | BlockDriverState *out_bs; |
| 2208 | QemuOpts *opts = NULL, *sn_opts = NULL; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2209 | QemuOptsList *create_opts = NULL; |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2210 | QDict *open_opts = NULL; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2211 | char *options = NULL; |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 2212 | Error *local_err = NULL; |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2213 | bool writethrough, src_writethrough, image_opts = false, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2214 | skip_create = false, progress = false, tgt_image_opts = false; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2215 | int64_t ret = -EINVAL; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2216 | bool force_share = false; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2217 | bool explict_min_sparse = false; |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2218 | bool bitmaps = false; |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2219 | bool skip_broken = false; |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 2220 | int64_t rate_limit = 0; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2221 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2222 | ImgConvertState s = (ImgConvertState) { |
| 2223 | /* Need at least 4k of zeros for sparse detection */ |
| 2224 | .min_sparse = 8, |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2225 | .copy_range = false, |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2226 | .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE, |
| 2227 | .wr_in_order = true, |
| 2228 | .num_coroutines = 8, |
| 2229 | }; |
| 2230 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2231 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2232 | static const struct option long_options[] = { |
| 2233 | {"help", no_argument, 0, 'h'}, |
| 2234 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2235 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2236 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2237 | {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS}, |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 2238 | {"salvage", no_argument, 0, OPTION_SALVAGE}, |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2239 | {"target-is-zero", no_argument, 0, OPTION_TARGET_IS_ZERO}, |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2240 | {"bitmaps", no_argument, 0, OPTION_BITMAPS}, |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2241 | {"skip-broken-bitmaps", no_argument, 0, OPTION_SKIP_BROKEN}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2242 | {0, 0, 0, 0} |
| 2243 | }; |
Eric Blake | 1899bf4 | 2021-09-13 08:17:35 -0500 | [diff] [blame] | 2244 | 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] | 2245 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2246 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2247 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2248 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2249 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2250 | case ':': |
| 2251 | missing_argument(argv[optind - 1]); |
| 2252 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2253 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2254 | unrecognized_option(argv[optind - 1]); |
| 2255 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2256 | case 'h': |
| 2257 | help(); |
| 2258 | break; |
| 2259 | case 'f': |
| 2260 | fmt = optarg; |
| 2261 | break; |
| 2262 | case 'O': |
| 2263 | out_fmt = optarg; |
| 2264 | break; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 2265 | case 'B': |
| 2266 | out_baseimg = optarg; |
| 2267 | break; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2268 | case 'C': |
| 2269 | s.copy_range = true; |
| 2270 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2271 | case 'c': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2272 | s.compressed = true; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2273 | break; |
Eric Blake | 1899bf4 | 2021-09-13 08:17:35 -0500 | [diff] [blame] | 2274 | case 'F': |
| 2275 | backing_fmt = optarg; |
| 2276 | break; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2277 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 2278 | if (accumulate_options(&options, optarg) < 0) { |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2279 | goto fail_getopt; |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 2280 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2281 | break; |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2282 | case 'l': |
| 2283 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { |
Markus Armbruster | 70b9433 | 2015-02-13 12:50:26 +0100 | [diff] [blame] | 2284 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
| 2285 | optarg, false); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2286 | if (!sn_opts) { |
| 2287 | error_report("Failed in parsing snapshot param '%s'", |
| 2288 | optarg); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2289 | goto fail_getopt; |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2290 | } |
| 2291 | } else { |
| 2292 | snapshot_name = optarg; |
| 2293 | } |
| 2294 | break; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2295 | case 'S': |
| 2296 | { |
| 2297 | int64_t sval; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 2298 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 2299 | sval = cvtnum("buffer size for sparse output", optarg); |
| 2300 | if (sval < 0) { |
| 2301 | goto fail_getopt; |
| 2302 | } else if (!QEMU_IS_ALIGNED(sval, BDRV_SECTOR_SIZE) || |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 2303 | sval / BDRV_SECTOR_SIZE > MAX_BUF_SECTORS) { |
| 2304 | error_report("Invalid buffer size for sparse output specified. " |
| 2305 | "Valid sizes are multiples of %llu up to %llu. Select " |
| 2306 | "0 to disable sparse detection (fully allocates output).", |
| 2307 | BDRV_SECTOR_SIZE, MAX_BUF_SECTORS * BDRV_SECTOR_SIZE); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2308 | goto fail_getopt; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2309 | } |
| 2310 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2311 | s.min_sparse = sval / BDRV_SECTOR_SIZE; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2312 | explict_min_sparse = true; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2313 | break; |
| 2314 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2315 | case 'p': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2316 | progress = true; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2317 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2318 | case 't': |
| 2319 | cache = optarg; |
| 2320 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 2321 | case 'T': |
| 2322 | src_cache = optarg; |
| 2323 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2324 | case 'q': |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2325 | s.quiet = true; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2326 | break; |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2327 | case 'n': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2328 | skip_create = true; |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2329 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2330 | case 'm': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2331 | if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) || |
| 2332 | s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2333 | error_report("Invalid number of coroutines. Allowed number of" |
| 2334 | " coroutines is between 1 and %d", MAX_COROUTINES); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2335 | goto fail_getopt; |
| 2336 | } |
| 2337 | break; |
| 2338 | case 'W': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2339 | s.wr_in_order = false; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2340 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2341 | case 'U': |
| 2342 | force_share = true; |
| 2343 | break; |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 2344 | case 'r': |
| 2345 | rate_limit = cvtnum("rate limit", optarg); |
| 2346 | if (rate_limit < 0) { |
| 2347 | goto fail_getopt; |
| 2348 | } |
| 2349 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 2350 | case OPTION_OBJECT: |
| 2351 | user_creatable_process_cmdline(optarg); |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2352 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2353 | case OPTION_IMAGE_OPTS: |
| 2354 | image_opts = true; |
| 2355 | break; |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 2356 | case OPTION_SALVAGE: |
| 2357 | s.salvage = true; |
| 2358 | break; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2359 | case OPTION_TARGET_IMAGE_OPTS: |
| 2360 | tgt_image_opts = true; |
| 2361 | break; |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2362 | case OPTION_TARGET_IS_ZERO: |
| 2363 | /* |
| 2364 | * The user asserting that the target is blank has the |
| 2365 | * same effect as the target driver supporting zero |
| 2366 | * initialisation. |
| 2367 | */ |
| 2368 | s.has_zero_init = true; |
| 2369 | break; |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2370 | case OPTION_BITMAPS: |
| 2371 | bitmaps = true; |
| 2372 | break; |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2373 | case OPTION_SKIP_BROKEN: |
| 2374 | skip_broken = true; |
| 2375 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2376 | } |
| 2377 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2378 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2379 | if (!out_fmt && !tgt_image_opts) { |
| 2380 | out_fmt = "raw"; |
| 2381 | } |
| 2382 | |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2383 | if (skip_broken && !bitmaps) { |
| 2384 | error_report("Use of --skip-broken-bitmaps requires --bitmaps"); |
| 2385 | goto fail_getopt; |
| 2386 | } |
| 2387 | |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2388 | if (s.compressed && s.copy_range) { |
| 2389 | error_report("Cannot enable copy offloading when -c is used"); |
| 2390 | goto fail_getopt; |
| 2391 | } |
| 2392 | |
| 2393 | if (explict_min_sparse && s.copy_range) { |
| 2394 | error_report("Cannot enable copy offloading when -S is used"); |
| 2395 | goto fail_getopt; |
| 2396 | } |
| 2397 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 2398 | if (s.copy_range && s.salvage) { |
| 2399 | error_report("Cannot use copy offloading in salvaging mode"); |
| 2400 | goto fail_getopt; |
| 2401 | } |
| 2402 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2403 | if (tgt_image_opts && !skip_create) { |
| 2404 | error_report("--target-image-opts requires use of -n flag"); |
| 2405 | goto fail_getopt; |
| 2406 | } |
| 2407 | |
Kevin Wolf | ffd8e8f | 2019-08-09 11:09:21 +0200 | [diff] [blame] | 2408 | if (skip_create && options) { |
Eric Blake | 25956af | 2020-07-06 15:39:46 -0500 | [diff] [blame] | 2409 | error_report("-o has no effect when skipping image creation"); |
| 2410 | goto fail_getopt; |
Kevin Wolf | ffd8e8f | 2019-08-09 11:09:21 +0200 | [diff] [blame] | 2411 | } |
| 2412 | |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2413 | if (s.has_zero_init && !skip_create) { |
| 2414 | error_report("--target-is-zero requires use of -n flag"); |
| 2415 | goto fail_getopt; |
| 2416 | } |
| 2417 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2418 | s.src_num = argc - optind - 1; |
| 2419 | out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL; |
| 2420 | |
| 2421 | if (options && has_help_option(options)) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2422 | if (out_fmt) { |
| 2423 | ret = print_block_option_help(out_filename, out_fmt); |
| 2424 | goto fail_getopt; |
| 2425 | } else { |
| 2426 | error_report("Option help requires a format be specified"); |
| 2427 | goto fail_getopt; |
| 2428 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | if (s.src_num < 1) { |
| 2432 | error_report("Must specify image file name"); |
| 2433 | goto fail_getopt; |
| 2434 | } |
| 2435 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2436 | /* ret is still -EINVAL until here */ |
| 2437 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); |
| 2438 | if (ret < 0) { |
| 2439 | error_report("Invalid source cache option: %s", src_cache); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2440 | goto fail_getopt; |
| 2441 | } |
| 2442 | |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2443 | /* Initialize before goto out */ |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2444 | if (s.quiet) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2445 | progress = false; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2446 | } |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2447 | qemu_progress_init(progress, 1.0); |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2448 | qemu_progress_print(0, 100); |
| 2449 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2450 | s.src = g_new0(BlockBackend *, s.src_num); |
| 2451 | s.src_sectors = g_new(int64_t, s.src_num); |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 2452 | s.src_alignment = g_new(int, s.src_num); |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 2453 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2454 | for (bs_i = 0; bs_i < s.src_num; bs_i++) { |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 2455 | BlockDriverState *src_bs; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2456 | s.src[bs_i] = img_open(image_opts, argv[optind + bs_i], |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2457 | fmt, src_flags, src_writethrough, s.quiet, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2458 | force_share); |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2459 | if (!s.src[bs_i]) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2460 | ret = -1; |
| 2461 | goto out; |
| 2462 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2463 | s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]); |
| 2464 | if (s.src_sectors[bs_i] < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2465 | error_report("Could not get size of %s: %s", |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2466 | argv[optind + bs_i], strerror(-s.src_sectors[bs_i])); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2467 | ret = -1; |
| 2468 | goto out; |
| 2469 | } |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 2470 | src_bs = blk_bs(s.src[bs_i]); |
| 2471 | s.src_alignment[bs_i] = DIV_ROUND_UP(src_bs->bl.request_alignment, |
| 2472 | BDRV_SECTOR_SIZE); |
| 2473 | if (!bdrv_get_info(src_bs, &bdi)) { |
| 2474 | s.src_alignment[bs_i] = MAX(s.src_alignment[bs_i], |
| 2475 | bdi.cluster_size / BDRV_SECTOR_SIZE); |
| 2476 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2477 | s.total_sectors += s.src_sectors[bs_i]; |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 2478 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2479 | |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2480 | if (sn_opts) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2481 | bdrv_snapshot_load_tmp(blk_bs(s.src[0]), |
Peter Maydell | 10d6eda | 2017-02-10 16:28:24 +0000 | [diff] [blame] | 2482 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), |
| 2483 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), |
| 2484 | &local_err); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2485 | } else if (snapshot_name != NULL) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2486 | if (s.src_num > 1) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 2487 | error_report("No support for concatenating multiple snapshot"); |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 2488 | ret = -1; |
| 2489 | goto out; |
| 2490 | } |
Wenchao Xia | 7b4c478 | 2013-12-04 17:10:54 +0800 | [diff] [blame] | 2491 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2492 | bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name, |
| 2493 | &local_err); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2494 | } |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2495 | if (local_err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2496 | error_reportf_err(local_err, "Failed to load snapshot: "); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2497 | ret = -1; |
| 2498 | goto out; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 2499 | } |
| 2500 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2501 | if (!skip_create) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2502 | /* Find driver and parse its options */ |
| 2503 | drv = bdrv_find_format(out_fmt); |
| 2504 | if (!drv) { |
| 2505 | error_report("Unknown file format '%s'", out_fmt); |
| 2506 | ret = -1; |
| 2507 | goto out; |
| 2508 | } |
| 2509 | |
| 2510 | proto_drv = bdrv_find_protocol(out_filename, true, &local_err); |
| 2511 | if (!proto_drv) { |
| 2512 | error_report_err(local_err); |
| 2513 | ret = -1; |
| 2514 | goto out; |
| 2515 | } |
| 2516 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2517 | if (!drv->create_opts) { |
| 2518 | error_report("Format driver '%s' does not support image creation", |
| 2519 | drv->format_name); |
| 2520 | ret = -1; |
| 2521 | goto out; |
| 2522 | } |
Max Reitz | f75613c | 2014-12-02 18:32:46 +0100 | [diff] [blame] | 2523 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2524 | if (!proto_drv->create_opts) { |
| 2525 | error_report("Protocol driver '%s' does not support image creation", |
| 2526 | proto_drv->format_name); |
| 2527 | ret = -1; |
| 2528 | goto out; |
| 2529 | } |
Max Reitz | f75613c | 2014-12-02 18:32:46 +0100 | [diff] [blame] | 2530 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2531 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 2532 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
Kevin Wolf | db08adf | 2009-06-04 15:39:38 +0200 | [diff] [blame] | 2533 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2534 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 2535 | if (options) { |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 2536 | if (!qemu_opts_do_parse(opts, options, NULL, &local_err)) { |
Markus Armbruster | 97a2ca7 | 2015-03-14 10:23:15 +0100 | [diff] [blame] | 2537 | error_report_err(local_err); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 2538 | ret = -1; |
| 2539 | goto out; |
| 2540 | } |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2541 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2542 | |
Yi Li | c075c42 | 2020-08-19 09:36:07 +0800 | [diff] [blame] | 2543 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, |
| 2544 | s.total_sectors * BDRV_SECTOR_SIZE, &error_abort); |
Eric Blake | 1899bf4 | 2021-09-13 08:17:35 -0500 | [diff] [blame] | 2545 | ret = add_old_style_options(out_fmt, opts, out_baseimg, backing_fmt); |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2546 | if (ret < 0) { |
| 2547 | goto out; |
| 2548 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2549 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2550 | |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2551 | /* Get backing file name if -o backing_file was used */ |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2552 | out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2553 | if (out_baseimg_param) { |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2554 | out_baseimg = out_baseimg_param; |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2555 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2556 | s.target_has_backing = (bool) out_baseimg; |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2557 | |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2558 | if (s.has_zero_init && s.target_has_backing) { |
| 2559 | error_report("Cannot use --target-is-zero when the destination " |
| 2560 | "image has a backing file"); |
| 2561 | goto out; |
| 2562 | } |
| 2563 | |
Max Reitz | 48758a8 | 2017-04-26 15:46:48 +0200 | [diff] [blame] | 2564 | if (s.src_num > 1 && out_baseimg) { |
| 2565 | error_report("Having a backing file for the target makes no sense when " |
| 2566 | "concatenating multiple input images"); |
| 2567 | ret = -1; |
| 2568 | goto out; |
| 2569 | } |
| 2570 | |
Eric Blake | d9f059a | 2020-07-06 15:39:54 -0500 | [diff] [blame] | 2571 | if (out_baseimg_param) { |
| 2572 | if (!qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT)) { |
Eric Blake | 497a30d | 2021-05-03 14:36:00 -0700 | [diff] [blame] | 2573 | error_report("Use of backing file requires explicit " |
| 2574 | "backing format"); |
| 2575 | ret = -1; |
| 2576 | goto out; |
Eric Blake | d9f059a | 2020-07-06 15:39:54 -0500 | [diff] [blame] | 2577 | } |
| 2578 | } |
| 2579 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2580 | /* Check if compression is supported */ |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2581 | if (s.compressed) { |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2582 | bool encryption = |
| 2583 | qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false); |
Daniel P. Berrange | 0cb8d47 | 2017-06-23 17:24:06 +0100 | [diff] [blame] | 2584 | const char *encryptfmt = |
| 2585 | qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2586 | const char *preallocation = |
| 2587 | qemu_opt_get(opts, BLOCK_OPT_PREALLOC); |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2588 | |
Vladimir Sementsov-Ogievskiy | ac850bf | 2019-06-04 19:15:06 +0300 | [diff] [blame] | 2589 | if (drv && !block_driver_can_compress(drv)) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2590 | error_report("Compression not supported for this file format"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2591 | ret = -1; |
| 2592 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2593 | } |
| 2594 | |
Daniel P. Berrange | 0cb8d47 | 2017-06-23 17:24:06 +0100 | [diff] [blame] | 2595 | if (encryption || encryptfmt) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2596 | error_report("Compression and encryption not supported at " |
| 2597 | "the same time"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2598 | ret = -1; |
| 2599 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2600 | } |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 2601 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2602 | if (preallocation |
| 2603 | && strcmp(preallocation, "off")) |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 2604 | { |
| 2605 | error_report("Compression and preallocation not supported at " |
| 2606 | "the same time"); |
| 2607 | ret = -1; |
| 2608 | goto out; |
| 2609 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2610 | } |
| 2611 | |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2612 | /* Determine if bitmaps need copying */ |
| 2613 | if (bitmaps) { |
| 2614 | if (s.src_num > 1) { |
| 2615 | error_report("Copying bitmaps only possible with single source"); |
| 2616 | ret = -1; |
| 2617 | goto out; |
| 2618 | } |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2619 | ret = convert_check_bitmaps(blk_bs(s.src[0]), skip_broken); |
Eric Blake | 74a4320 | 2021-07-09 10:39:50 -0500 | [diff] [blame] | 2620 | if (ret < 0) { |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2621 | goto out; |
| 2622 | } |
| 2623 | } |
| 2624 | |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2625 | /* |
| 2626 | * The later open call will need any decryption secrets, and |
| 2627 | * bdrv_create() will purge "opts", so extract them now before |
| 2628 | * they are lost. |
| 2629 | */ |
| 2630 | if (!skip_create) { |
| 2631 | open_opts = qdict_new(); |
| 2632 | 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] | 2633 | |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2634 | /* Create the new image */ |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 2635 | ret = bdrv_create(drv, out_filename, opts, &local_err); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2636 | if (ret < 0) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2637 | error_reportf_err(local_err, "%s: error while converting %s: ", |
| 2638 | out_filename, out_fmt); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2639 | goto out; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2640 | } |
| 2641 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2642 | |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 2643 | s.target_is_new = !skip_create; |
| 2644 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2645 | 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] | 2646 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2647 | if (ret < 0) { |
| 2648 | error_report("Invalid cache option: %s", cache); |
Markus Armbruster | bb9cd2e | 2014-05-28 11:17:07 +0200 | [diff] [blame] | 2649 | goto out; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2650 | } |
| 2651 | |
Hanna Reitz | a1c6243 | 2021-08-19 12:12:00 +0200 | [diff] [blame] | 2652 | if (flags & BDRV_O_NOCACHE) { |
| 2653 | /* |
| 2654 | * If we open the target with O_DIRECT, it may be necessary to |
| 2655 | * extend its size to align to the physical sector size. |
| 2656 | */ |
| 2657 | flags |= BDRV_O_RESIZE; |
| 2658 | } |
| 2659 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2660 | if (skip_create) { |
| 2661 | s.target = img_open(tgt_image_opts, out_filename, out_fmt, |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2662 | flags, writethrough, s.quiet, false); |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2663 | } else { |
| 2664 | /* TODO ultimately we should allow --target-image-opts |
| 2665 | * to be used even when -n is not given. |
| 2666 | * That has to wait for bdrv_create to be improved |
| 2667 | * to allow filenames in option syntax |
| 2668 | */ |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2669 | s.target = img_open_file(out_filename, open_opts, out_fmt, |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2670 | flags, writethrough, s.quiet, false); |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2671 | open_opts = NULL; /* blk_new_open will have freed it */ |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2672 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2673 | if (!s.target) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2674 | ret = -1; |
| 2675 | goto out; |
| 2676 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2677 | out_bs = blk_bs(s.target); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2678 | |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2679 | if (bitmaps && !bdrv_supports_persistent_dirty_bitmap(out_bs)) { |
| 2680 | error_report("Format driver '%s' does not support bitmaps", |
| 2681 | out_bs->drv->format_name); |
| 2682 | ret = -1; |
| 2683 | goto out; |
| 2684 | } |
| 2685 | |
Vladimir Sementsov-Ogievskiy | ac850bf | 2019-06-04 19:15:06 +0300 | [diff] [blame] | 2686 | if (s.compressed && !block_driver_can_compress(out_bs->drv)) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2687 | error_report("Compression not supported for this file format"); |
| 2688 | ret = -1; |
| 2689 | goto out; |
| 2690 | } |
| 2691 | |
Eric Blake | 5def6b8 | 2016-06-23 16:37:19 -0600 | [diff] [blame] | 2692 | /* increase bufsectors from the default 4096 (2M) if opt_transfer |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 2693 | * or discard_alignment of the out_bs is greater. Limit to |
| 2694 | * MAX_BUF_SECTORS as maximum which is currently 32768 (16MB). */ |
| 2695 | s.buf_sectors = MIN(MAX_BUF_SECTORS, |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2696 | MAX(s.buf_sectors, |
| 2697 | MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS, |
| 2698 | out_bs->bl.pdiscard_alignment >> |
| 2699 | BDRV_SECTOR_BITS))); |
Peter Lieven | f2521c9 | 2013-11-27 11:07:06 +0100 | [diff] [blame] | 2700 | |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 2701 | /* try to align the write requests to the destination to avoid unnecessary |
| 2702 | * RMW cycles. */ |
| 2703 | s.alignment = MAX(pow2floor(s.min_sparse), |
| 2704 | DIV_ROUND_UP(out_bs->bl.request_alignment, |
| 2705 | BDRV_SECTOR_SIZE)); |
| 2706 | assert(is_power_of_2(s.alignment)); |
| 2707 | |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2708 | if (skip_create) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2709 | int64_t output_sectors = blk_nb_sectors(s.target); |
Markus Armbruster | 43716fa | 2014-06-26 13:23:21 +0200 | [diff] [blame] | 2710 | if (output_sectors < 0) { |
Gonglei | eec5eb4 | 2015-02-25 12:22:27 +0800 | [diff] [blame] | 2711 | error_report("unable to get output image length: %s", |
Markus Armbruster | 43716fa | 2014-06-26 13:23:21 +0200 | [diff] [blame] | 2712 | strerror(-output_sectors)); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2713 | ret = -1; |
| 2714 | goto out; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2715 | } else if (output_sectors < s.total_sectors) { |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2716 | error_report("output file is smaller than input file"); |
| 2717 | ret = -1; |
| 2718 | goto out; |
| 2719 | } |
| 2720 | } |
| 2721 | |
Max Reitz | c69291e | 2020-01-21 16:59:14 +0100 | [diff] [blame] | 2722 | if (s.target_has_backing && s.target_is_new) { |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 2723 | /* Errors are treated as "backing length unknown" (which means |
| 2724 | * s.target_backing_sectors has to be negative, which it will |
| 2725 | * be automatically). The backing file length is used only |
| 2726 | * for optimizations, so such a case is not fatal. */ |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 2727 | s.target_backing_sectors = |
| 2728 | bdrv_nb_sectors(bdrv_backing_chain_next(out_bs)); |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 2729 | } else { |
| 2730 | s.target_backing_sectors = -1; |
| 2731 | } |
| 2732 | |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2733 | ret = bdrv_get_info(out_bs, &bdi); |
| 2734 | if (ret < 0) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2735 | if (s.compressed) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2736 | error_report("could not get block driver info"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2737 | goto out; |
| 2738 | } |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2739 | } else { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2740 | s.compressed = s.compressed || bdi.needs_compressed_writes; |
| 2741 | s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE; |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2742 | } |
| 2743 | |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 2744 | if (rate_limit) { |
| 2745 | set_rate_limit(s.target, rate_limit); |
| 2746 | } |
| 2747 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2748 | ret = convert_do_copy(&s); |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2749 | |
| 2750 | /* Now copy the bitmaps */ |
| 2751 | if (bitmaps && ret == 0) { |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2752 | 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] | 2753 | } |
| 2754 | |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2755 | out: |
Peter Lieven | 13c28af | 2013-11-27 11:07:01 +0100 | [diff] [blame] | 2756 | if (!ret) { |
| 2757 | qemu_progress_print(100, 0); |
| 2758 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2759 | qemu_progress_end(); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2760 | qemu_opts_del(opts); |
| 2761 | qemu_opts_free(create_opts); |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2762 | qobject_unref(open_opts); |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2763 | blk_unref(s.target); |
| 2764 | if (s.src) { |
| 2765 | for (bs_i = 0; bs_i < s.src_num; bs_i++) { |
| 2766 | blk_unref(s.src[bs_i]); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2767 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2768 | g_free(s.src); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2769 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2770 | g_free(s.src_sectors); |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 2771 | g_free(s.src_alignment); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2772 | fail_getopt: |
Tuguoyi | 6aec830 | 2020-11-02 09:04:57 +0000 | [diff] [blame] | 2773 | qemu_opts_del(sn_opts); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2774 | g_free(options); |
| 2775 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2776 | return !!ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2777 | } |
| 2778 | |
bellard | 57d1a2b | 2004-08-03 21:15:11 +0000 | [diff] [blame] | 2779 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2780 | static void dump_snapshots(BlockDriverState *bs) |
| 2781 | { |
| 2782 | QEMUSnapshotInfo *sn_tab, *sn; |
| 2783 | int nb_sns, i; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2784 | |
| 2785 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 2786 | if (nb_sns <= 0) |
| 2787 | return; |
| 2788 | printf("Snapshot list:\n"); |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2789 | bdrv_snapshot_dump(NULL); |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2790 | printf("\n"); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2791 | for(i = 0; i < nb_sns; i++) { |
| 2792 | sn = &sn_tab[i]; |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2793 | bdrv_snapshot_dump(sn); |
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 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2796 | g_free(sn_tab); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2797 | } |
| 2798 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2799 | static void dump_json_image_info_list(ImageInfoList *list) |
| 2800 | { |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2801 | GString *str; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2802 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 2803 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2804 | |
| 2805 | visit_type_ImageInfoList(v, NULL, &list, &error_abort); |
| 2806 | visit_complete(v, &obj); |
Markus Armbruster | 6589f45 | 2020-12-11 18:11:35 +0100 | [diff] [blame] | 2807 | str = qobject_to_json_pretty(obj, true); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2808 | assert(str != NULL); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2809 | printf("%s\n", str->str); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2810 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2811 | visit_free(v); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2812 | g_string_free(str, true); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2813 | } |
| 2814 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2815 | static void dump_json_image_info(ImageInfo *info) |
| 2816 | { |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2817 | GString *str; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2818 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 2819 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2820 | |
| 2821 | visit_type_ImageInfo(v, NULL, &info, &error_abort); |
| 2822 | visit_complete(v, &obj); |
Markus Armbruster | 6589f45 | 2020-12-11 18:11:35 +0100 | [diff] [blame] | 2823 | str = qobject_to_json_pretty(obj, true); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2824 | assert(str != NULL); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2825 | printf("%s\n", str->str); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2826 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2827 | visit_free(v); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2828 | g_string_free(str, true); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2829 | } |
| 2830 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2831 | static void dump_human_image_info_list(ImageInfoList *list) |
| 2832 | { |
| 2833 | ImageInfoList *elem; |
| 2834 | bool delim = false; |
| 2835 | |
| 2836 | for (elem = list; elem; elem = elem->next) { |
| 2837 | if (delim) { |
| 2838 | printf("\n"); |
| 2839 | } |
| 2840 | delim = true; |
| 2841 | |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2842 | bdrv_image_info_dump(elem->value); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2843 | } |
| 2844 | } |
| 2845 | |
| 2846 | static gboolean str_equal_func(gconstpointer a, gconstpointer b) |
| 2847 | { |
| 2848 | return strcmp(a, b) == 0; |
| 2849 | } |
| 2850 | |
| 2851 | /** |
| 2852 | * Open an image file chain and return an ImageInfoList |
| 2853 | * |
| 2854 | * @filename: topmost image filename |
| 2855 | * @fmt: topmost image format (may be NULL to autodetect) |
| 2856 | * @chain: true - enumerate entire backing file chain |
| 2857 | * false - only topmost image file |
| 2858 | * |
| 2859 | * Returns a list of ImageInfo objects or NULL if there was an error opening an |
| 2860 | * image file. If there was an error a message will have been printed to |
| 2861 | * stderr. |
| 2862 | */ |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2863 | static ImageInfoList *collect_image_info_list(bool image_opts, |
| 2864 | const char *filename, |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2865 | const char *fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2866 | bool chain, bool force_share) |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2867 | { |
| 2868 | ImageInfoList *head = NULL; |
Eric Blake | c3033fd | 2021-01-13 16:10:12 -0600 | [diff] [blame] | 2869 | ImageInfoList **tail = &head; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2870 | GHashTable *filenames; |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2871 | Error *err = NULL; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2872 | |
| 2873 | filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL); |
| 2874 | |
| 2875 | while (filename) { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2876 | BlockBackend *blk; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2877 | BlockDriverState *bs; |
| 2878 | ImageInfo *info; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2879 | |
| 2880 | if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) { |
| 2881 | error_report("Backing file '%s' creates an infinite loop.", |
| 2882 | filename); |
| 2883 | goto err; |
| 2884 | } |
| 2885 | g_hash_table_insert(filenames, (gpointer)filename, NULL); |
| 2886 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 2887 | blk = img_open(image_opts, filename, fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2888 | BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false, |
| 2889 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2890 | if (!blk) { |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2891 | goto err; |
| 2892 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2893 | bs = blk_bs(blk); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2894 | |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2895 | bdrv_query_image_info(bs, &info, &err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2896 | if (err) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 2897 | error_report_err(err); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2898 | blk_unref(blk); |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2899 | goto err; |
Wenchao Xia | fb0ed45 | 2013-06-06 12:27:57 +0800 | [diff] [blame] | 2900 | } |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2901 | |
Eric Blake | c3033fd | 2021-01-13 16:10:12 -0600 | [diff] [blame] | 2902 | QAPI_LIST_APPEND(tail, info); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2903 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2904 | blk_unref(blk); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2905 | |
Stefan Hajnoczi | 0da7d13 | 2019-12-05 13:46:46 +0000 | [diff] [blame] | 2906 | /* Clear parameters that only apply to the topmost image */ |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2907 | filename = fmt = NULL; |
Stefan Hajnoczi | 0da7d13 | 2019-12-05 13:46:46 +0000 | [diff] [blame] | 2908 | image_opts = false; |
| 2909 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2910 | if (chain) { |
| 2911 | if (info->has_full_backing_filename) { |
| 2912 | filename = info->full_backing_filename; |
| 2913 | } else if (info->has_backing_filename) { |
John Snow | 92d617a | 2015-12-14 14:55:15 -0500 | [diff] [blame] | 2914 | error_report("Could not determine absolute backing filename," |
| 2915 | " but backing filename '%s' present", |
| 2916 | info->backing_filename); |
| 2917 | goto err; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2918 | } |
| 2919 | if (info->has_backing_filename_format) { |
| 2920 | fmt = info->backing_filename_format; |
| 2921 | } |
| 2922 | } |
| 2923 | } |
| 2924 | g_hash_table_destroy(filenames); |
| 2925 | return head; |
| 2926 | |
| 2927 | err: |
| 2928 | qapi_free_ImageInfoList(head); |
| 2929 | g_hash_table_destroy(filenames); |
| 2930 | return NULL; |
| 2931 | } |
| 2932 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2933 | static int img_info(int argc, char **argv) |
| 2934 | { |
| 2935 | int c; |
| 2936 | OutputFormat output_format = OFORMAT_HUMAN; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2937 | bool chain = false; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2938 | const char *filename, *fmt, *output; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2939 | ImageInfoList *list; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2940 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2941 | bool force_share = false; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2942 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2943 | fmt = NULL; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2944 | output = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2945 | for(;;) { |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2946 | int option_index = 0; |
| 2947 | static const struct option long_options[] = { |
| 2948 | {"help", no_argument, 0, 'h'}, |
| 2949 | {"format", required_argument, 0, 'f'}, |
| 2950 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2951 | {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2952 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2953 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2954 | {"force-share", no_argument, 0, 'U'}, |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2955 | {0, 0, 0, 0} |
| 2956 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2957 | c = getopt_long(argc, argv, ":f:hU", |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2958 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2959 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2960 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2961 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2962 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2963 | case ':': |
| 2964 | missing_argument(argv[optind - 1]); |
| 2965 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2966 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2967 | unrecognized_option(argv[optind - 1]); |
| 2968 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2969 | case 'h': |
| 2970 | help(); |
| 2971 | break; |
| 2972 | case 'f': |
| 2973 | fmt = optarg; |
| 2974 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2975 | case 'U': |
| 2976 | force_share = true; |
| 2977 | break; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2978 | case OPTION_OUTPUT: |
| 2979 | output = optarg; |
| 2980 | break; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2981 | case OPTION_BACKING_CHAIN: |
| 2982 | chain = true; |
| 2983 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 2984 | case OPTION_OBJECT: |
| 2985 | user_creatable_process_cmdline(optarg); |
| 2986 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2987 | case OPTION_IMAGE_OPTS: |
| 2988 | image_opts = true; |
| 2989 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2990 | } |
| 2991 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 2992 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2993 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2994 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2995 | filename = argv[optind++]; |
| 2996 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2997 | if (output && !strcmp(output, "json")) { |
| 2998 | output_format = OFORMAT_JSON; |
| 2999 | } else if (output && !strcmp(output, "human")) { |
| 3000 | output_format = OFORMAT_HUMAN; |
| 3001 | } else if (output) { |
| 3002 | error_report("--output must be used with human or json as argument."); |
| 3003 | return 1; |
| 3004 | } |
| 3005 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3006 | list = collect_image_info_list(image_opts, filename, fmt, chain, |
| 3007 | force_share); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 3008 | if (!list) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3009 | return 1; |
| 3010 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 3011 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 3012 | switch (output_format) { |
| 3013 | case OFORMAT_HUMAN: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 3014 | dump_human_image_info_list(list); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 3015 | break; |
| 3016 | case OFORMAT_JSON: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 3017 | if (chain) { |
| 3018 | dump_json_image_info_list(list); |
| 3019 | } else { |
| 3020 | dump_json_image_info(list->value); |
| 3021 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 3022 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3023 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 3024 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 3025 | qapi_free_ImageInfoList(list); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 3026 | return 0; |
| 3027 | } |
| 3028 | |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3029 | static int dump_map_entry(OutputFormat output_format, MapEntry *e, |
| 3030 | MapEntry *next) |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3031 | { |
| 3032 | switch (output_format) { |
| 3033 | case OFORMAT_HUMAN: |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3034 | if (e->data && !e->has_offset) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3035 | error_report("File contains external, encrypted or compressed clusters."); |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3036 | return -1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3037 | } |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3038 | if (e->data && !e->zero) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3039 | printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n", |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3040 | e->start, e->length, |
| 3041 | e->has_offset ? e->offset : 0, |
| 3042 | e->has_filename ? e->filename : ""); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3043 | } |
| 3044 | /* This format ignores the distinction between 0, ZERO and ZERO|DATA. |
| 3045 | * Modify the flags here to allow more coalescing. |
| 3046 | */ |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3047 | if (next && (!next->data || next->zero)) { |
| 3048 | next->data = false; |
| 3049 | next->zero = true; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3050 | } |
| 3051 | break; |
| 3052 | case OFORMAT_JSON: |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 3053 | printf("{ \"start\": %"PRId64", \"length\": %"PRId64"," |
Eric Blake | 8417e13 | 2021-07-01 14:06:55 -0500 | [diff] [blame] | 3054 | " \"depth\": %"PRId64", \"present\": %s, \"zero\": %s," |
| 3055 | " \"data\": %s", e->start, e->length, e->depth, |
| 3056 | e->present ? "true" : "false", |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3057 | e->zero ? "true" : "false", |
| 3058 | e->data ? "true" : "false"); |
| 3059 | if (e->has_offset) { |
Paolo Bonzini | c745bfb | 2013-09-11 18:47:52 +0200 | [diff] [blame] | 3060 | printf(", \"offset\": %"PRId64"", e->offset); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3061 | } |
| 3062 | putchar('}'); |
| 3063 | |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 3064 | if (next) { |
| 3065 | puts(","); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3066 | } |
| 3067 | break; |
| 3068 | } |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3069 | return 0; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3070 | } |
| 3071 | |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3072 | static int get_block_status(BlockDriverState *bs, int64_t offset, |
| 3073 | int64_t bytes, MapEntry *e) |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3074 | { |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3075 | int ret; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3076 | int depth; |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 3077 | BlockDriverState *file; |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3078 | bool has_offset; |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3079 | int64_t map; |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3080 | char *filename = NULL; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3081 | |
| 3082 | /* As an optimization, we could cache the current range of unallocated |
| 3083 | * clusters in each file of the chain, and avoid querying the same |
| 3084 | * range repeatedly. |
| 3085 | */ |
| 3086 | |
| 3087 | depth = 0; |
| 3088 | for (;;) { |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3089 | bs = bdrv_skip_filters(bs); |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3090 | ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3091 | if (ret < 0) { |
| 3092 | return ret; |
| 3093 | } |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3094 | assert(bytes); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3095 | if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) { |
| 3096 | break; |
| 3097 | } |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3098 | bs = bdrv_cow_bs(bs); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3099 | if (bs == NULL) { |
| 3100 | ret = 0; |
| 3101 | break; |
| 3102 | } |
| 3103 | |
| 3104 | depth++; |
| 3105 | } |
| 3106 | |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3107 | has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID); |
| 3108 | |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3109 | if (file && has_offset) { |
| 3110 | bdrv_refresh_filename(file); |
| 3111 | filename = file->filename; |
| 3112 | } |
| 3113 | |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3114 | *e = (MapEntry) { |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3115 | .start = offset, |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3116 | .length = bytes, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3117 | .data = !!(ret & BDRV_BLOCK_DATA), |
| 3118 | .zero = !!(ret & BDRV_BLOCK_ZERO), |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3119 | .offset = map, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3120 | .has_offset = has_offset, |
| 3121 | .depth = depth, |
Eric Blake | 8417e13 | 2021-07-01 14:06:55 -0500 | [diff] [blame] | 3122 | .present = !!(ret & BDRV_BLOCK_ALLOCATED), |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3123 | .has_filename = filename, |
| 3124 | .filename = filename, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3125 | }; |
| 3126 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3127 | return 0; |
| 3128 | } |
| 3129 | |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3130 | static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next) |
| 3131 | { |
| 3132 | if (curr->length == 0) { |
| 3133 | return false; |
| 3134 | } |
| 3135 | if (curr->zero != next->zero || |
| 3136 | curr->data != next->data || |
| 3137 | curr->depth != next->depth || |
Eric Blake | 8417e13 | 2021-07-01 14:06:55 -0500 | [diff] [blame] | 3138 | curr->present != next->present || |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3139 | curr->has_filename != next->has_filename || |
| 3140 | curr->has_offset != next->has_offset) { |
| 3141 | return false; |
| 3142 | } |
| 3143 | if (curr->has_filename && strcmp(curr->filename, next->filename)) { |
| 3144 | return false; |
| 3145 | } |
| 3146 | if (curr->has_offset && curr->offset + curr->length != next->offset) { |
| 3147 | return false; |
| 3148 | } |
| 3149 | return true; |
| 3150 | } |
| 3151 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3152 | static int img_map(int argc, char **argv) |
| 3153 | { |
| 3154 | int c; |
| 3155 | OutputFormat output_format = OFORMAT_HUMAN; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3156 | BlockBackend *blk; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3157 | BlockDriverState *bs; |
| 3158 | const char *filename, *fmt, *output; |
| 3159 | int64_t length; |
| 3160 | MapEntry curr = { .length = 0 }, next; |
| 3161 | int ret = 0; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3162 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3163 | bool force_share = false; |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3164 | int64_t start_offset = 0; |
| 3165 | int64_t max_length = -1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3166 | |
| 3167 | fmt = NULL; |
| 3168 | output = NULL; |
| 3169 | for (;;) { |
| 3170 | int option_index = 0; |
| 3171 | static const struct option long_options[] = { |
| 3172 | {"help", no_argument, 0, 'h'}, |
| 3173 | {"format", required_argument, 0, 'f'}, |
| 3174 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3175 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3176 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3177 | {"force-share", no_argument, 0, 'U'}, |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3178 | {"start-offset", required_argument, 0, 's'}, |
| 3179 | {"max-length", required_argument, 0, 'l'}, |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3180 | {0, 0, 0, 0} |
| 3181 | }; |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3182 | c = getopt_long(argc, argv, ":f:s:l:hU", |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3183 | long_options, &option_index); |
| 3184 | if (c == -1) { |
| 3185 | break; |
| 3186 | } |
| 3187 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3188 | case ':': |
| 3189 | missing_argument(argv[optind - 1]); |
| 3190 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3191 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3192 | unrecognized_option(argv[optind - 1]); |
| 3193 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3194 | case 'h': |
| 3195 | help(); |
| 3196 | break; |
| 3197 | case 'f': |
| 3198 | fmt = optarg; |
| 3199 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3200 | case 'U': |
| 3201 | force_share = true; |
| 3202 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3203 | case OPTION_OUTPUT: |
| 3204 | output = optarg; |
| 3205 | break; |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3206 | case 's': |
| 3207 | start_offset = cvtnum("start offset", optarg); |
| 3208 | if (start_offset < 0) { |
| 3209 | return 1; |
| 3210 | } |
| 3211 | break; |
| 3212 | case 'l': |
| 3213 | max_length = cvtnum("max length", optarg); |
| 3214 | if (max_length < 0) { |
| 3215 | return 1; |
| 3216 | } |
| 3217 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 3218 | case OPTION_OBJECT: |
| 3219 | user_creatable_process_cmdline(optarg); |
| 3220 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3221 | case OPTION_IMAGE_OPTS: |
| 3222 | image_opts = true; |
| 3223 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3224 | } |
| 3225 | } |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3226 | if (optind != argc - 1) { |
| 3227 | error_exit("Expecting one image file name"); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3228 | } |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3229 | filename = argv[optind]; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3230 | |
| 3231 | if (output && !strcmp(output, "json")) { |
| 3232 | output_format = OFORMAT_JSON; |
| 3233 | } else if (output && !strcmp(output, "human")) { |
| 3234 | output_format = OFORMAT_HUMAN; |
| 3235 | } else if (output) { |
| 3236 | error_report("--output must be used with human or json as argument."); |
| 3237 | return 1; |
| 3238 | } |
| 3239 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3240 | blk = img_open(image_opts, filename, fmt, 0, false, false, force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3241 | if (!blk) { |
| 3242 | return 1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3243 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3244 | bs = blk_bs(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3245 | |
| 3246 | if (output_format == OFORMAT_HUMAN) { |
| 3247 | printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File"); |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 3248 | } else if (output_format == OFORMAT_JSON) { |
| 3249 | putchar('['); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3250 | } |
| 3251 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3252 | length = blk_getlength(blk); |
Eyal Moscovici | 8f282e8 | 2020-05-13 16:36:27 +0300 | [diff] [blame] | 3253 | if (length < 0) { |
| 3254 | error_report("Failed to get size for '%s'", filename); |
| 3255 | return 1; |
| 3256 | } |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3257 | if (max_length != -1) { |
| 3258 | length = MIN(start_offset + max_length, length); |
| 3259 | } |
Eyal Moscovici | 8f282e8 | 2020-05-13 16:36:27 +0300 | [diff] [blame] | 3260 | |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3261 | curr.start = start_offset; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3262 | while (curr.start + curr.length < length) { |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3263 | int64_t offset = curr.start + curr.length; |
Kevin Wolf | d0ceea8 | 2020-07-07 16:46:29 +0200 | [diff] [blame] | 3264 | int64_t n = length - offset; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3265 | |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3266 | ret = get_block_status(bs, offset, n, &next); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3267 | if (ret < 0) { |
| 3268 | error_report("Could not read file metadata: %s", strerror(-ret)); |
| 3269 | goto out; |
| 3270 | } |
| 3271 | |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3272 | if (entry_mergeable(&curr, &next)) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3273 | curr.length += next.length; |
| 3274 | continue; |
| 3275 | } |
| 3276 | |
| 3277 | if (curr.length > 0) { |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3278 | ret = dump_map_entry(output_format, &curr, &next); |
| 3279 | if (ret < 0) { |
| 3280 | goto out; |
| 3281 | } |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3282 | } |
| 3283 | curr = next; |
| 3284 | } |
| 3285 | |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3286 | ret = dump_map_entry(output_format, &curr, NULL); |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 3287 | if (output_format == OFORMAT_JSON) { |
| 3288 | puts("]"); |
| 3289 | } |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3290 | |
| 3291 | out: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3292 | blk_unref(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3293 | return ret < 0; |
| 3294 | } |
| 3295 | |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3296 | #define SNAPSHOT_LIST 1 |
| 3297 | #define SNAPSHOT_CREATE 2 |
| 3298 | #define SNAPSHOT_APPLY 3 |
| 3299 | #define SNAPSHOT_DELETE 4 |
| 3300 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3301 | static int img_snapshot(int argc, char **argv) |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3302 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3303 | BlockBackend *blk; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3304 | BlockDriverState *bs; |
| 3305 | QEMUSnapshotInfo sn; |
| 3306 | char *filename, *snapshot_name = NULL; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3307 | int c, ret = 0, bdrv_oflags; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3308 | int action = 0; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3309 | bool quiet = false; |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 3310 | Error *err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3311 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3312 | bool force_share = false; |
Marc-André Lureau | f793dde | 2022-03-07 11:04:00 +0400 | [diff] [blame] | 3313 | int64_t rt; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3314 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3315 | bdrv_oflags = BDRV_O_RDWR; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3316 | /* Parse commandline parameters */ |
| 3317 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3318 | static const struct option long_options[] = { |
| 3319 | {"help", no_argument, 0, 'h'}, |
| 3320 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3321 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3322 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3323 | {0, 0, 0, 0} |
| 3324 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3325 | c = getopt_long(argc, argv, ":la:c:d:hqU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3326 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3327 | if (c == -1) { |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3328 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3329 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3330 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3331 | case ':': |
| 3332 | missing_argument(argv[optind - 1]); |
| 3333 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3334 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3335 | unrecognized_option(argv[optind - 1]); |
| 3336 | break; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3337 | case 'h': |
| 3338 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3339 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3340 | case 'l': |
| 3341 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3342 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
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 | } |
| 3345 | action = SNAPSHOT_LIST; |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 3346 | bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */ |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3347 | break; |
| 3348 | case 'a': |
| 3349 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3350 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3351 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3352 | } |
| 3353 | action = SNAPSHOT_APPLY; |
| 3354 | snapshot_name = optarg; |
| 3355 | break; |
| 3356 | case 'c': |
| 3357 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3358 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3359 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3360 | } |
| 3361 | action = SNAPSHOT_CREATE; |
| 3362 | snapshot_name = optarg; |
| 3363 | break; |
| 3364 | case 'd': |
| 3365 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3366 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3367 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3368 | } |
| 3369 | action = SNAPSHOT_DELETE; |
| 3370 | snapshot_name = optarg; |
| 3371 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3372 | case 'q': |
| 3373 | quiet = true; |
| 3374 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3375 | case 'U': |
| 3376 | force_share = true; |
| 3377 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 3378 | case OPTION_OBJECT: |
| 3379 | user_creatable_process_cmdline(optarg); |
| 3380 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3381 | case OPTION_IMAGE_OPTS: |
| 3382 | image_opts = true; |
| 3383 | break; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3384 | } |
| 3385 | } |
| 3386 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 3387 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3388 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3389 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3390 | filename = argv[optind++]; |
| 3391 | |
| 3392 | /* Open the image */ |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3393 | blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet, |
| 3394 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3395 | if (!blk) { |
| 3396 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3397 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3398 | bs = blk_bs(blk); |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3399 | |
| 3400 | /* Perform the requested action */ |
| 3401 | switch(action) { |
| 3402 | case SNAPSHOT_LIST: |
| 3403 | dump_snapshots(bs); |
| 3404 | break; |
| 3405 | |
| 3406 | case SNAPSHOT_CREATE: |
| 3407 | memset(&sn, 0, sizeof(sn)); |
| 3408 | pstrcpy(sn.name, sizeof(sn.name), snapshot_name); |
| 3409 | |
Marc-André Lureau | f793dde | 2022-03-07 11:04:00 +0400 | [diff] [blame] | 3410 | rt = g_get_real_time(); |
| 3411 | sn.date_sec = rt / G_USEC_PER_SEC; |
| 3412 | sn.date_nsec = (rt % G_USEC_PER_SEC) * 1000; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3413 | |
| 3414 | ret = bdrv_snapshot_create(bs, &sn); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3415 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3416 | error_report("Could not create snapshot '%s': %d (%s)", |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3417 | snapshot_name, ret, strerror(-ret)); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3418 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3419 | break; |
| 3420 | |
| 3421 | case SNAPSHOT_APPLY: |
Kevin Wolf | 0b62bcb | 2017-11-20 15:28:41 +0100 | [diff] [blame] | 3422 | ret = bdrv_snapshot_goto(bs, snapshot_name, &err); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3423 | if (ret) { |
Kevin Wolf | 0b62bcb | 2017-11-20 15:28:41 +0100 | [diff] [blame] | 3424 | error_reportf_err(err, "Could not apply snapshot '%s': ", |
| 3425 | snapshot_name); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3426 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3427 | break; |
| 3428 | |
| 3429 | case SNAPSHOT_DELETE: |
Daniel Henrique Barboza | 8c04093 | 2018-11-07 11:09:59 -0200 | [diff] [blame] | 3430 | ret = bdrv_snapshot_find(bs, &sn, snapshot_name); |
| 3431 | if (ret < 0) { |
| 3432 | error_report("Could not delete snapshot '%s': snapshot not " |
| 3433 | "found", snapshot_name); |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 3434 | ret = 1; |
Daniel Henrique Barboza | 8c04093 | 2018-11-07 11:09:59 -0200 | [diff] [blame] | 3435 | } else { |
| 3436 | ret = bdrv_snapshot_delete(bs, sn.id_str, sn.name, &err); |
| 3437 | if (ret < 0) { |
| 3438 | error_reportf_err(err, "Could not delete snapshot '%s': ", |
| 3439 | snapshot_name); |
| 3440 | ret = 1; |
| 3441 | } |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3442 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3443 | break; |
| 3444 | } |
| 3445 | |
| 3446 | /* Cleanup */ |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3447 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3448 | if (ret) { |
| 3449 | return 1; |
| 3450 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3451 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3452 | } |
| 3453 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3454 | static int img_rebase(int argc, char **argv) |
| 3455 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3456 | BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL; |
Paolo Bonzini | 396374c | 2016-02-25 23:53:54 +0100 | [diff] [blame] | 3457 | uint8_t *buf_old = NULL; |
| 3458 | uint8_t *buf_new = NULL; |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3459 | BlockDriverState *bs = NULL, *prefix_chain_bs = NULL; |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3460 | BlockDriverState *unfiltered_bs; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3461 | char *filename; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3462 | const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg; |
| 3463 | int c, flags, src_flags, ret; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3464 | bool writethrough, src_writethrough; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3465 | int unsafe = 0; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3466 | bool force_share = false; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3467 | int progress = 0; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3468 | bool quiet = false; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 3469 | Error *local_err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3470 | bool image_opts = false; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3471 | |
| 3472 | /* Parse commandline parameters */ |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 3473 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3474 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3475 | src_cache = BDRV_DEFAULT_CACHE; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3476 | out_baseimg = NULL; |
| 3477 | out_basefmt = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3478 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3479 | static const struct option long_options[] = { |
| 3480 | {"help", no_argument, 0, 'h'}, |
| 3481 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3482 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3483 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3484 | {0, 0, 0, 0} |
| 3485 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3486 | c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3487 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3488 | if (c == -1) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3489 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3490 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3491 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3492 | case ':': |
| 3493 | missing_argument(argv[optind - 1]); |
| 3494 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3495 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3496 | unrecognized_option(argv[optind - 1]); |
| 3497 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3498 | case 'h': |
| 3499 | help(); |
| 3500 | return 0; |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 3501 | case 'f': |
| 3502 | fmt = optarg; |
| 3503 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3504 | case 'F': |
| 3505 | out_basefmt = optarg; |
| 3506 | break; |
| 3507 | case 'b': |
| 3508 | out_baseimg = optarg; |
| 3509 | break; |
| 3510 | case 'u': |
| 3511 | unsafe = 1; |
| 3512 | break; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3513 | case 'p': |
| 3514 | progress = 1; |
| 3515 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3516 | case 't': |
| 3517 | cache = optarg; |
| 3518 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3519 | case 'T': |
| 3520 | src_cache = optarg; |
| 3521 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3522 | case 'q': |
| 3523 | quiet = true; |
| 3524 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 3525 | case OPTION_OBJECT: |
| 3526 | user_creatable_process_cmdline(optarg); |
| 3527 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3528 | case OPTION_IMAGE_OPTS: |
| 3529 | image_opts = true; |
| 3530 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3531 | case 'U': |
| 3532 | force_share = true; |
| 3533 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3534 | } |
| 3535 | } |
| 3536 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3537 | if (quiet) { |
| 3538 | progress = 0; |
| 3539 | } |
| 3540 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3541 | if (optind != argc - 1) { |
| 3542 | error_exit("Expecting one image file name"); |
| 3543 | } |
| 3544 | if (!unsafe && !out_baseimg) { |
| 3545 | error_exit("Must specify backing file (-b) or use unsafe mode (-u)"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3546 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3547 | filename = argv[optind++]; |
| 3548 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3549 | qemu_progress_init(progress, 2.0); |
| 3550 | qemu_progress_print(0, 100); |
| 3551 | |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3552 | flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0); |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3553 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3554 | if (ret < 0) { |
| 3555 | error_report("Invalid cache option: %s", cache); |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3556 | goto out; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3557 | } |
| 3558 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3559 | src_flags = 0; |
| 3560 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3561 | if (ret < 0) { |
| 3562 | error_report("Invalid source cache option: %s", src_cache); |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3563 | goto out; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3564 | } |
| 3565 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3566 | /* The source files are opened read-only, don't care about WCE */ |
| 3567 | assert((src_flags & BDRV_O_RDWR) == 0); |
| 3568 | (void) src_writethrough; |
| 3569 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3570 | /* |
| 3571 | * Open the images. |
| 3572 | * |
| 3573 | * Ignore the old backing file for unsafe rebase in case we want to correct |
| 3574 | * the reference to a renamed or moved backing file. |
| 3575 | */ |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3576 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 3577 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3578 | if (!blk) { |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3579 | ret = -1; |
| 3580 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3581 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3582 | bs = blk_bs(blk); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3583 | |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3584 | unfiltered_bs = bdrv_skip_filters(bs); |
| 3585 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3586 | if (out_basefmt != NULL) { |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3587 | if (bdrv_find_format(out_basefmt) == NULL) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3588 | error_report("Invalid format name: '%s'", out_basefmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3589 | ret = -1; |
| 3590 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3591 | } |
| 3592 | } |
| 3593 | |
| 3594 | /* For safe rebasing we need to compare old and new backing file */ |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3595 | if (!unsafe) { |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3596 | QDict *options = NULL; |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3597 | BlockDriverState *base_bs = bdrv_cow_bs(unfiltered_bs); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3598 | |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3599 | if (base_bs) { |
Kevin Wolf | d861ab3 | 2019-04-25 14:25:10 +0200 | [diff] [blame] | 3600 | blk_old_backing = blk_new(qemu_get_aio_context(), |
| 3601 | BLK_PERM_CONSISTENT_READ, |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3602 | BLK_PERM_ALL); |
| 3603 | ret = blk_insert_bs(blk_old_backing, base_bs, |
| 3604 | &local_err); |
| 3605 | if (ret < 0) { |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3606 | error_reportf_err(local_err, |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3607 | "Could not reuse old backing file '%s': ", |
| 3608 | base_bs->filename); |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3609 | goto out; |
| 3610 | } |
| 3611 | } else { |
| 3612 | blk_old_backing = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3613 | } |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3614 | |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3615 | if (out_baseimg[0]) { |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3616 | const char *overlay_filename; |
| 3617 | char *out_real_path; |
| 3618 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3619 | options = qdict_new(); |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3620 | if (out_basefmt) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 3621 | qdict_put_str(options, "driver", out_basefmt); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3622 | } |
| 3623 | if (force_share) { |
| 3624 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3625 | } |
| 3626 | |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3627 | bdrv_refresh_filename(bs); |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3628 | overlay_filename = bs->exact_filename[0] ? bs->exact_filename |
| 3629 | : bs->filename; |
Max Reitz | 645ae7d | 2019-02-01 20:29:14 +0100 | [diff] [blame] | 3630 | out_real_path = |
| 3631 | bdrv_get_full_backing_filename_from_filename(overlay_filename, |
| 3632 | out_baseimg, |
| 3633 | &local_err); |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3634 | if (local_err) { |
Max Reitz | f22356d | 2019-05-28 21:53:38 +0200 | [diff] [blame] | 3635 | qobject_unref(options); |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3636 | error_reportf_err(local_err, |
| 3637 | "Could not resolve backing filename: "); |
| 3638 | ret = -1; |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3639 | goto out; |
| 3640 | } |
| 3641 | |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3642 | /* |
| 3643 | * Find out whether we rebase an image on top of a previous image |
| 3644 | * in its chain. |
| 3645 | */ |
| 3646 | prefix_chain_bs = bdrv_find_backing_image(bs, out_real_path); |
Sam Eiderman | 330c729 | 2019-05-23 19:33:37 +0300 | [diff] [blame] | 3647 | if (prefix_chain_bs) { |
Max Reitz | f22356d | 2019-05-28 21:53:38 +0200 | [diff] [blame] | 3648 | qobject_unref(options); |
Sam Eiderman | 330c729 | 2019-05-23 19:33:37 +0300 | [diff] [blame] | 3649 | g_free(out_real_path); |
Max Reitz | f22356d | 2019-05-28 21:53:38 +0200 | [diff] [blame] | 3650 | |
Kevin Wolf | d861ab3 | 2019-04-25 14:25:10 +0200 | [diff] [blame] | 3651 | blk_new_backing = blk_new(qemu_get_aio_context(), |
| 3652 | BLK_PERM_CONSISTENT_READ, |
Sam Eiderman | 330c729 | 2019-05-23 19:33:37 +0300 | [diff] [blame] | 3653 | BLK_PERM_ALL); |
| 3654 | ret = blk_insert_bs(blk_new_backing, prefix_chain_bs, |
| 3655 | &local_err); |
| 3656 | if (ret < 0) { |
| 3657 | error_reportf_err(local_err, |
| 3658 | "Could not reuse backing file '%s': ", |
| 3659 | out_baseimg); |
| 3660 | goto out; |
| 3661 | } |
| 3662 | } else { |
| 3663 | blk_new_backing = blk_new_open(out_real_path, NULL, |
| 3664 | options, src_flags, &local_err); |
| 3665 | g_free(out_real_path); |
| 3666 | if (!blk_new_backing) { |
| 3667 | error_reportf_err(local_err, |
| 3668 | "Could not open new backing file '%s': ", |
| 3669 | out_baseimg); |
| 3670 | ret = -1; |
| 3671 | goto out; |
| 3672 | } |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3673 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3674 | } |
| 3675 | } |
| 3676 | |
| 3677 | /* |
| 3678 | * Check each unallocated cluster in the COW file. If it is unallocated, |
| 3679 | * accesses go to the backing file. We must therefore compare this cluster |
| 3680 | * in the old and new backing file, and if they differ we need to copy it |
| 3681 | * from the old backing file into the COW file. |
| 3682 | * |
| 3683 | * If qemu-img crashes during this step, no harm is done. The content of |
| 3684 | * the image is the same as the original one at any time. |
| 3685 | */ |
| 3686 | if (!unsafe) { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3687 | int64_t size; |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3688 | int64_t old_backing_size = 0; |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3689 | int64_t new_backing_size = 0; |
| 3690 | uint64_t offset; |
| 3691 | int64_t n; |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 3692 | float local_progress = 0; |
TeLeMan | d6771bf | 2010-02-08 16:20:00 +0800 | [diff] [blame] | 3693 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3694 | buf_old = blk_blockalign(blk, IO_BUF_SIZE); |
| 3695 | buf_new = blk_blockalign(blk, IO_BUF_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3696 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3697 | size = blk_getlength(blk); |
| 3698 | if (size < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3699 | error_report("Could not get size of '%s': %s", |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3700 | filename, strerror(-size)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3701 | ret = -1; |
| 3702 | goto out; |
| 3703 | } |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3704 | if (blk_old_backing) { |
| 3705 | old_backing_size = blk_getlength(blk_old_backing); |
| 3706 | if (old_backing_size < 0) { |
| 3707 | char backing_name[PATH_MAX]; |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3708 | |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3709 | bdrv_get_backing_filename(bs, backing_name, |
| 3710 | sizeof(backing_name)); |
| 3711 | error_report("Could not get size of '%s': %s", |
| 3712 | backing_name, strerror(-old_backing_size)); |
| 3713 | ret = -1; |
| 3714 | goto out; |
| 3715 | } |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3716 | } |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3717 | if (blk_new_backing) { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3718 | new_backing_size = blk_getlength(blk_new_backing); |
| 3719 | if (new_backing_size < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3720 | error_report("Could not get size of '%s': %s", |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3721 | out_baseimg, strerror(-new_backing_size)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3722 | ret = -1; |
| 3723 | goto out; |
| 3724 | } |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3725 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3726 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3727 | if (size != 0) { |
| 3728 | local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE)); |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 3729 | } |
| 3730 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3731 | for (offset = 0; offset < size; offset += n) { |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3732 | bool buf_old_is_zero = false; |
| 3733 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3734 | /* How many bytes can we handle with the next read? */ |
| 3735 | n = MIN(IO_BUF_SIZE, size - offset); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3736 | |
| 3737 | /* If the cluster is allocated, we don't need to take action */ |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3738 | ret = bdrv_is_allocated(unfiltered_bs, offset, n, &n); |
Paolo Bonzini | d663640 | 2013-09-04 19:00:25 +0200 | [diff] [blame] | 3739 | if (ret < 0) { |
| 3740 | error_report("error while reading image metadata: %s", |
| 3741 | strerror(-ret)); |
| 3742 | goto out; |
| 3743 | } |
Kevin Wolf | cc60e32 | 2010-04-29 14:47:48 +0200 | [diff] [blame] | 3744 | if (ret) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3745 | continue; |
| 3746 | } |
| 3747 | |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3748 | if (prefix_chain_bs) { |
| 3749 | /* |
| 3750 | * If cluster wasn't changed since prefix_chain, we don't need |
| 3751 | * to take action |
| 3752 | */ |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3753 | ret = bdrv_is_allocated_above(bdrv_cow_bs(unfiltered_bs), |
| 3754 | prefix_chain_bs, false, |
| 3755 | offset, n, &n); |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3756 | if (ret < 0) { |
| 3757 | error_report("error while reading image metadata: %s", |
| 3758 | strerror(-ret)); |
| 3759 | goto out; |
| 3760 | } |
| 3761 | if (!ret) { |
| 3762 | continue; |
| 3763 | } |
| 3764 | } |
| 3765 | |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3766 | /* |
| 3767 | * Read old and new backing file and take into consideration that |
| 3768 | * backing files may be smaller than the COW image. |
| 3769 | */ |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3770 | if (offset >= old_backing_size) { |
| 3771 | memset(buf_old, 0, n); |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3772 | buf_old_is_zero = true; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3773 | } else { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3774 | if (offset + n > old_backing_size) { |
| 3775 | n = old_backing_size - offset; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3776 | } |
| 3777 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3778 | ret = blk_pread(blk_old_backing, offset, buf_old, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3779 | if (ret < 0) { |
| 3780 | error_report("error while reading from old backing file"); |
| 3781 | goto out; |
| 3782 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3783 | } |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3784 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3785 | if (offset >= new_backing_size || !blk_new_backing) { |
| 3786 | memset(buf_new, 0, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3787 | } else { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3788 | if (offset + n > new_backing_size) { |
| 3789 | n = new_backing_size - offset; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3790 | } |
| 3791 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3792 | ret = blk_pread(blk_new_backing, offset, buf_new, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3793 | if (ret < 0) { |
| 3794 | error_report("error while reading from new backing file"); |
| 3795 | goto out; |
| 3796 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3797 | } |
| 3798 | |
| 3799 | /* If they differ, we need to write to the COW file */ |
| 3800 | uint64_t written = 0; |
| 3801 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3802 | while (written < n) { |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 3803 | int64_t pnum; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3804 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3805 | if (compare_buffers(buf_old + written, buf_new + written, |
| 3806 | n - written, &pnum)) |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3807 | { |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3808 | if (buf_old_is_zero) { |
| 3809 | ret = blk_pwrite_zeroes(blk, offset + written, pnum, 0); |
| 3810 | } else { |
| 3811 | ret = blk_pwrite(blk, offset + written, |
| 3812 | buf_old + written, pnum, 0); |
| 3813 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3814 | if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3815 | error_report("Error while writing to COW image: %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3816 | strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3817 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3818 | } |
| 3819 | } |
| 3820 | |
| 3821 | written += pnum; |
| 3822 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3823 | qemu_progress_print(local_progress, 100); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3824 | } |
| 3825 | } |
| 3826 | |
| 3827 | /* |
| 3828 | * Change the backing file. All clusters that are different from the old |
| 3829 | * backing file are overwritten in the COW file now, so the visible content |
| 3830 | * doesn't change when we switch the backing file. |
| 3831 | */ |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3832 | if (out_baseimg && *out_baseimg) { |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3833 | ret = bdrv_change_backing_file(unfiltered_bs, out_baseimg, out_basefmt, |
| 3834 | true); |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3835 | } else { |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3836 | ret = bdrv_change_backing_file(unfiltered_bs, NULL, NULL, false); |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3837 | } |
| 3838 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3839 | if (ret == -ENOSPC) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3840 | error_report("Could not change the backing file to '%s': No " |
| 3841 | "space left in the file header", out_baseimg); |
Eric Blake | a7cd44b | 2021-07-08 10:52:28 -0500 | [diff] [blame] | 3842 | } else if (ret == -EINVAL && out_baseimg && !out_basefmt) { |
| 3843 | error_report("Could not change the backing file to '%s': backing " |
| 3844 | "format must be specified", out_baseimg); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3845 | } else if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3846 | error_report("Could not change the backing file to '%s': %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3847 | out_baseimg, strerror(-ret)); |
| 3848 | } |
| 3849 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3850 | qemu_progress_print(100, 0); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3851 | /* |
| 3852 | * TODO At this point it is possible to check if any clusters that are |
| 3853 | * allocated in the COW file are the same in the backing file. If so, they |
| 3854 | * could be dropped from the COW file. Don't do this before switching the |
| 3855 | * backing file, in case of a crash this would lead to corruption. |
| 3856 | */ |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3857 | out: |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3858 | qemu_progress_end(); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3859 | /* Cleanup */ |
| 3860 | if (!unsafe) { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3861 | blk_unref(blk_old_backing); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3862 | blk_unref(blk_new_backing); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3863 | } |
Paolo Bonzini | 396374c | 2016-02-25 23:53:54 +0100 | [diff] [blame] | 3864 | qemu_vfree(buf_old); |
| 3865 | qemu_vfree(buf_new); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3866 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3867 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3868 | if (ret) { |
| 3869 | return 1; |
| 3870 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3871 | return 0; |
| 3872 | } |
| 3873 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3874 | static int img_resize(int argc, char **argv) |
| 3875 | { |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 3876 | Error *err = NULL; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3877 | int c, ret, relative; |
| 3878 | const char *filename, *fmt, *size; |
Max Reitz | 09c5c6d | 2019-09-18 11:51:44 +0200 | [diff] [blame] | 3879 | int64_t n, total_size, current_size; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3880 | bool quiet = false; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3881 | BlockBackend *blk = NULL; |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3882 | PreallocMode prealloc = PREALLOC_MODE_OFF; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3883 | QemuOpts *param; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3884 | |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3885 | static QemuOptsList resize_options = { |
| 3886 | .name = "resize_options", |
| 3887 | .head = QTAILQ_HEAD_INITIALIZER(resize_options.head), |
| 3888 | .desc = { |
| 3889 | { |
| 3890 | .name = BLOCK_OPT_SIZE, |
| 3891 | .type = QEMU_OPT_SIZE, |
| 3892 | .help = "Virtual disk size" |
| 3893 | }, { |
| 3894 | /* end of list */ |
| 3895 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3896 | }, |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3897 | }; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3898 | bool image_opts = false; |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3899 | bool shrink = false; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3900 | |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 3901 | /* Remove size from argv manually so that negative numbers are not treated |
| 3902 | * as options by getopt. */ |
| 3903 | if (argc < 3) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3904 | error_exit("Not enough arguments"); |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 3905 | return 1; |
| 3906 | } |
| 3907 | |
| 3908 | size = argv[--argc]; |
| 3909 | |
| 3910 | /* Parse getopt arguments */ |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3911 | fmt = NULL; |
| 3912 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3913 | static const struct option long_options[] = { |
| 3914 | {"help", no_argument, 0, 'h'}, |
| 3915 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3916 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3917 | {"preallocation", required_argument, 0, OPTION_PREALLOCATION}, |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3918 | {"shrink", no_argument, 0, OPTION_SHRINK}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3919 | {0, 0, 0, 0} |
| 3920 | }; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3921 | c = getopt_long(argc, argv, ":f:hq", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3922 | long_options, NULL); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3923 | if (c == -1) { |
| 3924 | break; |
| 3925 | } |
| 3926 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3927 | case ':': |
| 3928 | missing_argument(argv[optind - 1]); |
| 3929 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3930 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3931 | unrecognized_option(argv[optind - 1]); |
| 3932 | break; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3933 | case 'h': |
| 3934 | help(); |
| 3935 | break; |
| 3936 | case 'f': |
| 3937 | fmt = optarg; |
| 3938 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3939 | case 'q': |
| 3940 | quiet = true; |
| 3941 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 3942 | case OPTION_OBJECT: |
| 3943 | user_creatable_process_cmdline(optarg); |
| 3944 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3945 | case OPTION_IMAGE_OPTS: |
| 3946 | image_opts = true; |
| 3947 | break; |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3948 | case OPTION_PREALLOCATION: |
Marc-André Lureau | f7abe0e | 2017-08-24 10:46:10 +0200 | [diff] [blame] | 3949 | prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg, |
Markus Armbruster | 06c60b6 | 2017-08-24 10:45:57 +0200 | [diff] [blame] | 3950 | PREALLOC_MODE__MAX, NULL); |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3951 | if (prealloc == PREALLOC_MODE__MAX) { |
| 3952 | error_report("Invalid preallocation mode '%s'", optarg); |
| 3953 | return 1; |
| 3954 | } |
| 3955 | break; |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3956 | case OPTION_SHRINK: |
| 3957 | shrink = true; |
| 3958 | break; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3959 | } |
| 3960 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 3961 | if (optind != argc - 1) { |
Max Reitz | be8fbd4 | 2018-02-05 17:27:45 +0100 | [diff] [blame] | 3962 | error_exit("Expecting image file name and size"); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3963 | } |
| 3964 | filename = argv[optind++]; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3965 | |
| 3966 | /* Choose grow, shrink, or absolute resize mode */ |
| 3967 | switch (size[0]) { |
| 3968 | case '+': |
| 3969 | relative = 1; |
| 3970 | size++; |
| 3971 | break; |
| 3972 | case '-': |
| 3973 | relative = -1; |
| 3974 | size++; |
| 3975 | break; |
| 3976 | default: |
| 3977 | relative = 0; |
| 3978 | break; |
| 3979 | } |
| 3980 | |
| 3981 | /* Parse size */ |
Peter Crosthwaite | 87ea75d | 2014-01-01 18:49:17 -0800 | [diff] [blame] | 3982 | param = qemu_opts_create(&resize_options, NULL, 0, &error_abort); |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 3983 | if (!qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err)) { |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 3984 | error_report_err(err); |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3985 | ret = -1; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3986 | qemu_opts_del(param); |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3987 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3988 | } |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3989 | n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0); |
| 3990 | qemu_opts_del(param); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3991 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 3992 | blk = img_open(image_opts, filename, fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3993 | BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet, |
| 3994 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3995 | if (!blk) { |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3996 | ret = -1; |
| 3997 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3998 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3999 | |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 4000 | current_size = blk_getlength(blk); |
| 4001 | if (current_size < 0) { |
| 4002 | error_report("Failed to inquire current image length: %s", |
| 4003 | strerror(-current_size)); |
| 4004 | ret = -1; |
| 4005 | goto out; |
| 4006 | } |
| 4007 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4008 | if (relative) { |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 4009 | total_size = current_size + n * relative; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4010 | } else { |
| 4011 | total_size = n; |
| 4012 | } |
| 4013 | if (total_size <= 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 4014 | error_report("New image size must be positive"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 4015 | ret = -1; |
| 4016 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4017 | } |
| 4018 | |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 4019 | if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) { |
| 4020 | error_report("Preallocation can only be used for growing images"); |
| 4021 | ret = -1; |
| 4022 | goto out; |
| 4023 | } |
| 4024 | |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 4025 | if (total_size < current_size && !shrink) { |
Kevin Wolf | 1c404d7 | 2020-07-10 14:17:17 +0200 | [diff] [blame] | 4026 | error_report("Use the --shrink option to perform a shrink operation."); |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 4027 | warn_report("Shrinking an image will delete all data beyond the " |
| 4028 | "shrunken image's end. Before performing such an " |
| 4029 | "operation, make sure there is no important data there."); |
Kevin Wolf | 1c404d7 | 2020-07-10 14:17:17 +0200 | [diff] [blame] | 4030 | ret = -1; |
| 4031 | goto out; |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 4032 | } |
| 4033 | |
Max Reitz | e8d04f9 | 2019-09-18 11:51:43 +0200 | [diff] [blame] | 4034 | /* |
| 4035 | * The user expects the image to have the desired size after |
| 4036 | * resizing, so pass @exact=true. It is of no use to report |
| 4037 | * success when the image has not actually been resized. |
| 4038 | */ |
Kevin Wolf | 8c6242b | 2020-04-24 14:54:41 +0200 | [diff] [blame] | 4039 | ret = blk_truncate(blk, total_size, true, prealloc, 0, &err); |
Max Reitz | 09c5c6d | 2019-09-18 11:51:44 +0200 | [diff] [blame] | 4040 | if (!ret) { |
| 4041 | qprintf(quiet, "Image resized.\n"); |
| 4042 | } else { |
Max Reitz | ed3d2ec | 2017-03-28 22:51:27 +0200 | [diff] [blame] | 4043 | error_report_err(err); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4044 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 4045 | out: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 4046 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 4047 | if (ret) { |
| 4048 | return 1; |
| 4049 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4050 | return 0; |
| 4051 | } |
| 4052 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4053 | static void amend_status_cb(BlockDriverState *bs, |
Max Reitz | 8b13976 | 2015-07-27 17:51:32 +0200 | [diff] [blame] | 4054 | int64_t offset, int64_t total_work_size, |
| 4055 | void *opaque) |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4056 | { |
| 4057 | qemu_progress_print(100.f * offset / total_work_size, 0); |
| 4058 | } |
| 4059 | |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4060 | static int print_amend_option_help(const char *format) |
| 4061 | { |
| 4062 | BlockDriver *drv; |
| 4063 | |
| 4064 | /* Find driver and parse its options */ |
| 4065 | drv = bdrv_find_format(format); |
| 4066 | if (!drv) { |
| 4067 | error_report("Unknown file format '%s'", format); |
| 4068 | return 1; |
| 4069 | } |
| 4070 | |
| 4071 | if (!drv->bdrv_amend_options) { |
| 4072 | error_report("Format driver '%s' does not support option amendment", |
| 4073 | format); |
| 4074 | return 1; |
| 4075 | } |
| 4076 | |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4077 | /* Every driver supporting amendment must have amend_opts */ |
| 4078 | assert(drv->amend_opts); |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4079 | |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4080 | printf("Amend options for '%s':\n", format); |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4081 | qemu_opts_print_help(drv->amend_opts, false); |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4082 | return 0; |
| 4083 | } |
| 4084 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4085 | static int img_amend(int argc, char **argv) |
| 4086 | { |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 4087 | Error *err = NULL; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4088 | int c, ret = 0; |
| 4089 | char *options = NULL; |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4090 | QemuOptsList *amend_opts = NULL; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 4091 | QemuOpts *opts = NULL; |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 4092 | const char *fmt = NULL, *filename, *cache; |
| 4093 | int flags; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 4094 | bool writethrough; |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4095 | bool quiet = false, progress = false; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 4096 | BlockBackend *blk = NULL; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4097 | BlockDriverState *bs = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 4098 | bool image_opts = false; |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4099 | bool force = false; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4100 | |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 4101 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4102 | for (;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4103 | static const struct option long_options[] = { |
| 4104 | {"help", no_argument, 0, 'h'}, |
| 4105 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 4106 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4107 | {"force", no_argument, 0, OPTION_FORCE}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4108 | {0, 0, 0, 0} |
| 4109 | }; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4110 | c = getopt_long(argc, argv, ":ho:f:t:pq", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4111 | long_options, NULL); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4112 | if (c == -1) { |
| 4113 | break; |
| 4114 | } |
| 4115 | |
| 4116 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4117 | case ':': |
| 4118 | missing_argument(argv[optind - 1]); |
| 4119 | break; |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4120 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4121 | unrecognized_option(argv[optind - 1]); |
| 4122 | break; |
| 4123 | case 'h': |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4124 | help(); |
| 4125 | break; |
| 4126 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 4127 | if (accumulate_options(&options, optarg) < 0) { |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4128 | ret = -1; |
| 4129 | goto out_no_progress; |
| 4130 | } |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4131 | break; |
| 4132 | case 'f': |
| 4133 | fmt = optarg; |
| 4134 | break; |
| 4135 | case 't': |
| 4136 | cache = optarg; |
| 4137 | break; |
| 4138 | case 'p': |
| 4139 | progress = true; |
| 4140 | break; |
| 4141 | case 'q': |
| 4142 | quiet = true; |
| 4143 | break; |
| 4144 | case OPTION_OBJECT: |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 4145 | user_creatable_process_cmdline(optarg); |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4146 | break; |
| 4147 | case OPTION_IMAGE_OPTS: |
| 4148 | image_opts = true; |
| 4149 | break; |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4150 | case OPTION_FORCE: |
| 4151 | force = true; |
| 4152 | break; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4153 | } |
| 4154 | } |
| 4155 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4156 | if (!options) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 4157 | error_exit("Must specify options (-o)"); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4158 | } |
| 4159 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4160 | if (quiet) { |
| 4161 | progress = false; |
| 4162 | } |
| 4163 | qemu_progress_init(progress, 1.0); |
| 4164 | |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4165 | filename = (optind == argc - 1) ? argv[argc - 1] : NULL; |
| 4166 | if (fmt && has_help_option(options)) { |
| 4167 | /* If a format is explicitly specified (and possibly no filename is |
| 4168 | * given), print option help here */ |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4169 | ret = print_amend_option_help(fmt); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4170 | goto out; |
| 4171 | } |
| 4172 | |
| 4173 | if (optind != argc - 1) { |
Max Reitz | b2f27e4 | 2014-10-27 11:12:52 +0100 | [diff] [blame] | 4174 | error_report("Expecting one image file name"); |
| 4175 | ret = -1; |
| 4176 | goto out; |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4177 | } |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4178 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 4179 | flags = BDRV_O_RDWR; |
| 4180 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 4181 | if (ret < 0) { |
| 4182 | error_report("Invalid cache option: %s", cache); |
| 4183 | goto out; |
| 4184 | } |
| 4185 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4186 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 4187 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 4188 | if (!blk) { |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4189 | ret = -1; |
| 4190 | goto out; |
| 4191 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 4192 | bs = blk_bs(blk); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4193 | |
| 4194 | fmt = bs->drv->format_name; |
| 4195 | |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 4196 | if (has_help_option(options)) { |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4197 | /* If the format was auto-detected, print option help here */ |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4198 | ret = print_amend_option_help(fmt); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4199 | goto out; |
| 4200 | } |
| 4201 | |
Max Reitz | 1f99668 | 2018-05-09 23:00:17 +0200 | [diff] [blame] | 4202 | if (!bs->drv->bdrv_amend_options) { |
| 4203 | error_report("Format driver '%s' does not support option amendment", |
Max Reitz | b2439d2 | 2014-12-02 18:32:47 +0100 | [diff] [blame] | 4204 | fmt); |
| 4205 | ret = -1; |
| 4206 | goto out; |
| 4207 | } |
| 4208 | |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4209 | /* Every driver supporting amendment must have amend_opts */ |
| 4210 | assert(bs->drv->amend_opts); |
Max Reitz | 1f99668 | 2018-05-09 23:00:17 +0200 | [diff] [blame] | 4211 | |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4212 | amend_opts = qemu_opts_append(amend_opts, bs->drv->amend_opts); |
| 4213 | opts = qemu_opts_create(amend_opts, NULL, 0, &error_abort); |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 4214 | if (!qemu_opts_do_parse(opts, options, NULL, &err)) { |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4215 | /* Try to parse options using the create options */ |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4216 | amend_opts = qemu_opts_append(amend_opts, bs->drv->create_opts); |
| 4217 | qemu_opts_del(opts); |
| 4218 | opts = qemu_opts_create(amend_opts, NULL, 0, &error_abort); |
Markus Armbruster | 9e194e0 | 2020-07-07 18:06:11 +0200 | [diff] [blame] | 4219 | if (qemu_opts_do_parse(opts, options, NULL, NULL)) { |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4220 | error_append_hint(&err, |
| 4221 | "This option is only supported for image creation\n"); |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4222 | } |
| 4223 | |
Paolo Bonzini | ece9086 | 2017-01-04 15:56:24 +0100 | [diff] [blame] | 4224 | error_report_err(err); |
| 4225 | ret = -1; |
| 4226 | goto out; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4227 | } |
| 4228 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4229 | /* In case the driver does not call amend_status_cb() */ |
| 4230 | qemu_progress_print(0.f, 0); |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4231 | ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL, force, &err); |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4232 | qemu_progress_print(100.f, 0); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4233 | if (ret < 0) { |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 4234 | error_report_err(err); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4235 | goto out; |
| 4236 | } |
| 4237 | |
| 4238 | out: |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4239 | qemu_progress_end(); |
| 4240 | |
Max Reitz | e814dff | 2015-08-20 16:00:38 -0700 | [diff] [blame] | 4241 | out_no_progress: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 4242 | blk_unref(blk); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 4243 | qemu_opts_del(opts); |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4244 | qemu_opts_free(amend_opts); |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 4245 | g_free(options); |
| 4246 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4247 | if (ret) { |
| 4248 | return 1; |
| 4249 | } |
| 4250 | return 0; |
| 4251 | } |
| 4252 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4253 | typedef struct BenchData { |
| 4254 | BlockBackend *blk; |
| 4255 | uint64_t image_size; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4256 | bool write; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4257 | int bufsize; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4258 | int step; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4259 | int nrreq; |
| 4260 | int n; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4261 | int flush_interval; |
| 4262 | bool drain_on_flush; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4263 | uint8_t *buf; |
| 4264 | QEMUIOVector *qiov; |
| 4265 | |
| 4266 | int in_flight; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4267 | bool in_flush; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4268 | uint64_t offset; |
| 4269 | } BenchData; |
| 4270 | |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4271 | static void bench_undrained_flush_cb(void *opaque, int ret) |
| 4272 | { |
| 4273 | if (ret < 0) { |
Markus Armbruster | df3c286 | 2016-08-03 13:37:51 +0200 | [diff] [blame] | 4274 | error_report("Failed flush request: %s", strerror(-ret)); |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4275 | exit(EXIT_FAILURE); |
| 4276 | } |
| 4277 | } |
| 4278 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4279 | static void bench_cb(void *opaque, int ret) |
| 4280 | { |
| 4281 | BenchData *b = opaque; |
| 4282 | BlockAIOCB *acb; |
| 4283 | |
| 4284 | if (ret < 0) { |
Markus Armbruster | df3c286 | 2016-08-03 13:37:51 +0200 | [diff] [blame] | 4285 | error_report("Failed request: %s", strerror(-ret)); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4286 | exit(EXIT_FAILURE); |
| 4287 | } |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4288 | |
| 4289 | if (b->in_flush) { |
| 4290 | /* Just finished a flush with drained queue: Start next requests */ |
| 4291 | assert(b->in_flight == 0); |
| 4292 | b->in_flush = false; |
| 4293 | } else if (b->in_flight > 0) { |
| 4294 | int remaining = b->n - b->in_flight; |
| 4295 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4296 | b->n--; |
| 4297 | b->in_flight--; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4298 | |
| 4299 | /* Time for flush? Drain queue if requested, then flush */ |
| 4300 | if (b->flush_interval && remaining % b->flush_interval == 0) { |
| 4301 | if (!b->in_flight || !b->drain_on_flush) { |
| 4302 | BlockCompletionFunc *cb; |
| 4303 | |
| 4304 | if (b->drain_on_flush) { |
| 4305 | b->in_flush = true; |
| 4306 | cb = bench_cb; |
| 4307 | } else { |
| 4308 | cb = bench_undrained_flush_cb; |
| 4309 | } |
| 4310 | |
| 4311 | acb = blk_aio_flush(b->blk, cb, b); |
| 4312 | if (!acb) { |
| 4313 | error_report("Failed to issue flush request"); |
| 4314 | exit(EXIT_FAILURE); |
| 4315 | } |
| 4316 | } |
| 4317 | if (b->drain_on_flush) { |
| 4318 | return; |
| 4319 | } |
| 4320 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4321 | } |
| 4322 | |
| 4323 | while (b->n > b->in_flight && b->in_flight < b->nrreq) { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4324 | int64_t offset = b->offset; |
| 4325 | /* blk_aio_* might look for completed I/Os and kick bench_cb |
| 4326 | * again, so make sure this operation is counted by in_flight |
| 4327 | * and b->offset is ready for the next submission. |
| 4328 | */ |
| 4329 | b->in_flight++; |
| 4330 | b->offset += b->step; |
| 4331 | b->offset %= b->image_size; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4332 | if (b->write) { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4333 | 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] | 4334 | } else { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4335 | 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] | 4336 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4337 | if (!acb) { |
| 4338 | error_report("Failed to issue request"); |
| 4339 | exit(EXIT_FAILURE); |
| 4340 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4341 | } |
| 4342 | } |
| 4343 | |
| 4344 | static int img_bench(int argc, char **argv) |
| 4345 | { |
| 4346 | int c, ret = 0; |
| 4347 | const char *fmt = NULL, *filename; |
| 4348 | bool quiet = false; |
| 4349 | bool image_opts = false; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4350 | bool is_write = false; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4351 | int count = 75000; |
| 4352 | int depth = 64; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4353 | int64_t offset = 0; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4354 | size_t bufsize = 4096; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4355 | int pattern = 0; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4356 | size_t step = 0; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4357 | int flush_interval = 0; |
| 4358 | bool drain_on_flush = true; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4359 | int64_t image_size; |
| 4360 | BlockBackend *blk = NULL; |
| 4361 | BenchData data = {}; |
| 4362 | int flags = 0; |
Kevin Wolf | 604e861 | 2016-06-14 11:29:32 +0200 | [diff] [blame] | 4363 | bool writethrough = false; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4364 | struct timeval t1, t2; |
| 4365 | int i; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4366 | bool force_share = false; |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4367 | size_t buf_size; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4368 | |
| 4369 | for (;;) { |
| 4370 | static const struct option long_options[] = { |
| 4371 | {"help", no_argument, 0, 'h'}, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4372 | {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL}, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4373 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4374 | {"pattern", required_argument, 0, OPTION_PATTERN}, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4375 | {"no-drain", no_argument, 0, OPTION_NO_DRAIN}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4376 | {"force-share", no_argument, 0, 'U'}, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4377 | {0, 0, 0, 0} |
| 4378 | }; |
Aarushi Mehta | cdd2677 | 2020-01-20 14:18:55 +0000 | [diff] [blame] | 4379 | c = getopt_long(argc, argv, ":hc:d:f:ni:o:qs:S:t:wU", long_options, |
| 4380 | NULL); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4381 | if (c == -1) { |
| 4382 | break; |
| 4383 | } |
| 4384 | |
| 4385 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4386 | case ':': |
| 4387 | missing_argument(argv[optind - 1]); |
| 4388 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4389 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4390 | unrecognized_option(argv[optind - 1]); |
| 4391 | break; |
| 4392 | case 'h': |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4393 | help(); |
| 4394 | break; |
| 4395 | case 'c': |
| 4396 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4397 | unsigned long res; |
| 4398 | |
| 4399 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4400 | error_report("Invalid request count specified"); |
| 4401 | return 1; |
| 4402 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4403 | count = res; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4404 | break; |
| 4405 | } |
| 4406 | case 'd': |
| 4407 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4408 | unsigned long res; |
| 4409 | |
| 4410 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4411 | error_report("Invalid queue depth specified"); |
| 4412 | return 1; |
| 4413 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4414 | depth = res; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4415 | break; |
| 4416 | } |
| 4417 | case 'f': |
| 4418 | fmt = optarg; |
| 4419 | break; |
| 4420 | case 'n': |
| 4421 | flags |= BDRV_O_NATIVE_AIO; |
| 4422 | break; |
Aarushi Mehta | cdd2677 | 2020-01-20 14:18:55 +0000 | [diff] [blame] | 4423 | case 'i': |
| 4424 | ret = bdrv_parse_aio(optarg, &flags); |
| 4425 | if (ret < 0) { |
| 4426 | error_report("Invalid aio option: %s", optarg); |
| 4427 | ret = -1; |
| 4428 | goto out; |
| 4429 | } |
| 4430 | break; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4431 | case 'o': |
| 4432 | { |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4433 | offset = cvtnum("offset", optarg); |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4434 | if (offset < 0) { |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4435 | return 1; |
| 4436 | } |
| 4437 | break; |
| 4438 | } |
| 4439 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4440 | case 'q': |
| 4441 | quiet = true; |
| 4442 | break; |
| 4443 | case 's': |
| 4444 | { |
| 4445 | int64_t sval; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4446 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4447 | sval = cvtnum_full("buffer size", optarg, 0, INT_MAX); |
| 4448 | if (sval < 0) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4449 | return 1; |
| 4450 | } |
| 4451 | |
| 4452 | bufsize = sval; |
| 4453 | break; |
| 4454 | } |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4455 | case 'S': |
| 4456 | { |
| 4457 | int64_t sval; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4458 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4459 | sval = cvtnum_full("step_size", optarg, 0, INT_MAX); |
| 4460 | if (sval < 0) { |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4461 | return 1; |
| 4462 | } |
| 4463 | |
| 4464 | step = sval; |
| 4465 | break; |
| 4466 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4467 | case 't': |
| 4468 | ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough); |
| 4469 | if (ret < 0) { |
| 4470 | error_report("Invalid cache mode"); |
| 4471 | ret = -1; |
| 4472 | goto out; |
| 4473 | } |
| 4474 | break; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4475 | case 'w': |
| 4476 | flags |= BDRV_O_RDWR; |
| 4477 | is_write = true; |
| 4478 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4479 | case 'U': |
| 4480 | force_share = true; |
| 4481 | break; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4482 | case OPTION_PATTERN: |
| 4483 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4484 | unsigned long res; |
| 4485 | |
| 4486 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) { |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4487 | error_report("Invalid pattern byte specified"); |
| 4488 | return 1; |
| 4489 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4490 | pattern = res; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4491 | break; |
| 4492 | } |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4493 | case OPTION_FLUSH_INTERVAL: |
| 4494 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4495 | unsigned long res; |
| 4496 | |
| 4497 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4498 | error_report("Invalid flush interval specified"); |
| 4499 | return 1; |
| 4500 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4501 | flush_interval = res; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4502 | break; |
| 4503 | } |
| 4504 | case OPTION_NO_DRAIN: |
| 4505 | drain_on_flush = false; |
| 4506 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4507 | case OPTION_IMAGE_OPTS: |
| 4508 | image_opts = true; |
| 4509 | break; |
| 4510 | } |
| 4511 | } |
| 4512 | |
| 4513 | if (optind != argc - 1) { |
| 4514 | error_exit("Expecting one image file name"); |
| 4515 | } |
| 4516 | filename = argv[argc - 1]; |
| 4517 | |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4518 | if (!is_write && flush_interval) { |
| 4519 | error_report("--flush-interval is only available in write tests"); |
| 4520 | ret = -1; |
| 4521 | goto out; |
| 4522 | } |
| 4523 | if (flush_interval && flush_interval < depth) { |
| 4524 | error_report("Flush interval can't be smaller than depth"); |
| 4525 | ret = -1; |
| 4526 | goto out; |
| 4527 | } |
| 4528 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4529 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 4530 | force_share); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4531 | if (!blk) { |
| 4532 | ret = -1; |
| 4533 | goto out; |
| 4534 | } |
| 4535 | |
| 4536 | image_size = blk_getlength(blk); |
| 4537 | if (image_size < 0) { |
| 4538 | ret = image_size; |
| 4539 | goto out; |
| 4540 | } |
| 4541 | |
| 4542 | data = (BenchData) { |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4543 | .blk = blk, |
| 4544 | .image_size = image_size, |
| 4545 | .bufsize = bufsize, |
| 4546 | .step = step ?: bufsize, |
| 4547 | .nrreq = depth, |
| 4548 | .n = count, |
| 4549 | .offset = offset, |
| 4550 | .write = is_write, |
| 4551 | .flush_interval = flush_interval, |
| 4552 | .drain_on_flush = drain_on_flush, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4553 | }; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4554 | printf("Sending %d %s requests, %d bytes each, %d in parallel " |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4555 | "(starting at offset %" PRId64 ", step size %d)\n", |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4556 | data.n, data.write ? "write" : "read", data.bufsize, data.nrreq, |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4557 | data.offset, data.step); |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4558 | if (flush_interval) { |
| 4559 | printf("Sending flush every %d requests\n", flush_interval); |
| 4560 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4561 | |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4562 | buf_size = data.nrreq * data.bufsize; |
| 4563 | data.buf = blk_blockalign(blk, buf_size); |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4564 | memset(data.buf, pattern, data.nrreq * data.bufsize); |
| 4565 | |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4566 | blk_register_buf(blk, data.buf, buf_size); |
| 4567 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4568 | data.qiov = g_new(QEMUIOVector, data.nrreq); |
| 4569 | for (i = 0; i < data.nrreq; i++) { |
| 4570 | qemu_iovec_init(&data.qiov[i], 1); |
| 4571 | qemu_iovec_add(&data.qiov[i], |
| 4572 | data.buf + i * data.bufsize, data.bufsize); |
| 4573 | } |
| 4574 | |
| 4575 | gettimeofday(&t1, NULL); |
| 4576 | bench_cb(&data, 0); |
| 4577 | |
| 4578 | while (data.n > 0) { |
| 4579 | main_loop_wait(false); |
| 4580 | } |
| 4581 | gettimeofday(&t2, NULL); |
| 4582 | |
| 4583 | printf("Run completed in %3.3f seconds.\n", |
| 4584 | (t2.tv_sec - t1.tv_sec) |
| 4585 | + ((double)(t2.tv_usec - t1.tv_usec) / 1000000)); |
| 4586 | |
| 4587 | out: |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4588 | if (data.buf) { |
| 4589 | blk_unregister_buf(blk, data.buf); |
| 4590 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4591 | qemu_vfree(data.buf); |
| 4592 | blk_unref(blk); |
| 4593 | |
| 4594 | if (ret) { |
| 4595 | return 1; |
| 4596 | } |
| 4597 | return 0; |
| 4598 | } |
| 4599 | |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4600 | enum ImgBitmapAct { |
| 4601 | BITMAP_ADD, |
| 4602 | BITMAP_REMOVE, |
| 4603 | BITMAP_CLEAR, |
| 4604 | BITMAP_ENABLE, |
| 4605 | BITMAP_DISABLE, |
| 4606 | BITMAP_MERGE, |
| 4607 | }; |
| 4608 | typedef struct ImgBitmapAction { |
| 4609 | enum ImgBitmapAct act; |
| 4610 | const char *src; /* only used for merge */ |
| 4611 | QSIMPLEQ_ENTRY(ImgBitmapAction) next; |
| 4612 | } ImgBitmapAction; |
| 4613 | |
| 4614 | static int img_bitmap(int argc, char **argv) |
| 4615 | { |
| 4616 | Error *err = NULL; |
| 4617 | int c, ret = 1; |
| 4618 | QemuOpts *opts = NULL; |
| 4619 | const char *fmt = NULL, *src_fmt = NULL, *src_filename = NULL; |
| 4620 | const char *filename, *bitmap; |
| 4621 | BlockBackend *blk = NULL, *src = NULL; |
| 4622 | BlockDriverState *bs = NULL, *src_bs = NULL; |
| 4623 | bool image_opts = false; |
| 4624 | int64_t granularity = 0; |
| 4625 | bool add = false, merge = false; |
| 4626 | QSIMPLEQ_HEAD(, ImgBitmapAction) actions; |
| 4627 | ImgBitmapAction *act, *act_next; |
| 4628 | const char *op; |
| 4629 | |
| 4630 | QSIMPLEQ_INIT(&actions); |
| 4631 | |
| 4632 | for (;;) { |
| 4633 | static const struct option long_options[] = { |
| 4634 | {"help", no_argument, 0, 'h'}, |
| 4635 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 4636 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
| 4637 | {"add", no_argument, 0, OPTION_ADD}, |
| 4638 | {"remove", no_argument, 0, OPTION_REMOVE}, |
| 4639 | {"clear", no_argument, 0, OPTION_CLEAR}, |
| 4640 | {"enable", no_argument, 0, OPTION_ENABLE}, |
| 4641 | {"disable", no_argument, 0, OPTION_DISABLE}, |
| 4642 | {"merge", required_argument, 0, OPTION_MERGE}, |
| 4643 | {"granularity", required_argument, 0, 'g'}, |
| 4644 | {"source-file", required_argument, 0, 'b'}, |
| 4645 | {"source-format", required_argument, 0, 'F'}, |
| 4646 | {0, 0, 0, 0} |
| 4647 | }; |
| 4648 | c = getopt_long(argc, argv, ":b:f:F:g:h", long_options, NULL); |
| 4649 | if (c == -1) { |
| 4650 | break; |
| 4651 | } |
| 4652 | |
| 4653 | switch (c) { |
| 4654 | case ':': |
| 4655 | missing_argument(argv[optind - 1]); |
| 4656 | break; |
| 4657 | case '?': |
| 4658 | unrecognized_option(argv[optind - 1]); |
| 4659 | break; |
| 4660 | case 'h': |
| 4661 | help(); |
| 4662 | break; |
| 4663 | case 'b': |
| 4664 | src_filename = optarg; |
| 4665 | break; |
| 4666 | case 'f': |
| 4667 | fmt = optarg; |
| 4668 | break; |
| 4669 | case 'F': |
| 4670 | src_fmt = optarg; |
| 4671 | break; |
| 4672 | case 'g': |
| 4673 | granularity = cvtnum("granularity", optarg); |
| 4674 | if (granularity < 0) { |
| 4675 | return 1; |
| 4676 | } |
| 4677 | break; |
| 4678 | case OPTION_ADD: |
| 4679 | act = g_new0(ImgBitmapAction, 1); |
| 4680 | act->act = BITMAP_ADD; |
| 4681 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4682 | add = true; |
| 4683 | break; |
| 4684 | case OPTION_REMOVE: |
| 4685 | act = g_new0(ImgBitmapAction, 1); |
| 4686 | act->act = BITMAP_REMOVE; |
| 4687 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4688 | break; |
| 4689 | case OPTION_CLEAR: |
| 4690 | act = g_new0(ImgBitmapAction, 1); |
| 4691 | act->act = BITMAP_CLEAR; |
| 4692 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4693 | break; |
| 4694 | case OPTION_ENABLE: |
| 4695 | act = g_new0(ImgBitmapAction, 1); |
| 4696 | act->act = BITMAP_ENABLE; |
| 4697 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4698 | break; |
| 4699 | case OPTION_DISABLE: |
| 4700 | act = g_new0(ImgBitmapAction, 1); |
| 4701 | act->act = BITMAP_DISABLE; |
| 4702 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4703 | break; |
| 4704 | case OPTION_MERGE: |
| 4705 | act = g_new0(ImgBitmapAction, 1); |
| 4706 | act->act = BITMAP_MERGE; |
| 4707 | act->src = optarg; |
| 4708 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4709 | merge = true; |
| 4710 | break; |
| 4711 | case OPTION_OBJECT: |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 4712 | user_creatable_process_cmdline(optarg); |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4713 | break; |
| 4714 | case OPTION_IMAGE_OPTS: |
| 4715 | image_opts = true; |
| 4716 | break; |
| 4717 | } |
| 4718 | } |
| 4719 | |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4720 | if (QSIMPLEQ_EMPTY(&actions)) { |
| 4721 | error_report("Need at least one of --add, --remove, --clear, " |
| 4722 | "--enable, --disable, or --merge"); |
| 4723 | goto out; |
| 4724 | } |
| 4725 | |
| 4726 | if (granularity && !add) { |
| 4727 | error_report("granularity only supported with --add"); |
| 4728 | goto out; |
| 4729 | } |
| 4730 | if (src_fmt && !src_filename) { |
| 4731 | error_report("-F only supported with -b"); |
| 4732 | goto out; |
| 4733 | } |
| 4734 | if (src_filename && !merge) { |
| 4735 | error_report("Merge bitmap source file only supported with " |
| 4736 | "--merge"); |
| 4737 | goto out; |
| 4738 | } |
| 4739 | |
| 4740 | if (optind != argc - 2) { |
| 4741 | error_report("Expecting filename and bitmap name"); |
| 4742 | goto out; |
| 4743 | } |
| 4744 | |
| 4745 | filename = argv[optind]; |
| 4746 | bitmap = argv[optind + 1]; |
| 4747 | |
Eric Blake | 14f16bf | 2020-09-14 14:10:09 -0500 | [diff] [blame] | 4748 | /* |
| 4749 | * No need to open backing chains; we will be manipulating bitmaps |
| 4750 | * directly in this image without reference to image contents. |
| 4751 | */ |
| 4752 | blk = img_open(image_opts, filename, fmt, BDRV_O_RDWR | BDRV_O_NO_BACKING, |
| 4753 | false, false, false); |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4754 | if (!blk) { |
| 4755 | goto out; |
| 4756 | } |
| 4757 | bs = blk_bs(blk); |
| 4758 | if (src_filename) { |
Eric Blake | 14f16bf | 2020-09-14 14:10:09 -0500 | [diff] [blame] | 4759 | src = img_open(false, src_filename, src_fmt, BDRV_O_NO_BACKING, |
| 4760 | false, false, false); |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4761 | if (!src) { |
| 4762 | goto out; |
| 4763 | } |
| 4764 | src_bs = blk_bs(src); |
| 4765 | } else { |
| 4766 | src_bs = bs; |
| 4767 | } |
| 4768 | |
| 4769 | QSIMPLEQ_FOREACH_SAFE(act, &actions, next, act_next) { |
| 4770 | switch (act->act) { |
| 4771 | case BITMAP_ADD: |
| 4772 | qmp_block_dirty_bitmap_add(bs->node_name, bitmap, |
| 4773 | !!granularity, granularity, true, true, |
| 4774 | false, false, &err); |
| 4775 | op = "add"; |
| 4776 | break; |
| 4777 | case BITMAP_REMOVE: |
| 4778 | qmp_block_dirty_bitmap_remove(bs->node_name, bitmap, &err); |
| 4779 | op = "remove"; |
| 4780 | break; |
| 4781 | case BITMAP_CLEAR: |
| 4782 | qmp_block_dirty_bitmap_clear(bs->node_name, bitmap, &err); |
| 4783 | op = "clear"; |
| 4784 | break; |
| 4785 | case BITMAP_ENABLE: |
| 4786 | qmp_block_dirty_bitmap_enable(bs->node_name, bitmap, &err); |
| 4787 | op = "enable"; |
| 4788 | break; |
| 4789 | case BITMAP_DISABLE: |
| 4790 | qmp_block_dirty_bitmap_disable(bs->node_name, bitmap, &err); |
| 4791 | op = "disable"; |
| 4792 | break; |
Eric Blake | 6c729dd | 2020-05-21 14:21:35 -0500 | [diff] [blame] | 4793 | case BITMAP_MERGE: |
| 4794 | do_dirty_bitmap_merge(bs->node_name, bitmap, src_bs->node_name, |
| 4795 | act->src, &err); |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4796 | op = "merge"; |
| 4797 | break; |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4798 | default: |
| 4799 | g_assert_not_reached(); |
| 4800 | } |
| 4801 | |
| 4802 | if (err) { |
| 4803 | error_reportf_err(err, "Operation %s on bitmap %s failed: ", |
| 4804 | op, bitmap); |
| 4805 | goto out; |
| 4806 | } |
| 4807 | g_free(act); |
| 4808 | } |
| 4809 | |
| 4810 | ret = 0; |
| 4811 | |
| 4812 | out: |
| 4813 | blk_unref(src); |
| 4814 | blk_unref(blk); |
| 4815 | qemu_opts_del(opts); |
| 4816 | return ret; |
| 4817 | } |
| 4818 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4819 | #define C_BS 01 |
| 4820 | #define C_COUNT 02 |
| 4821 | #define C_IF 04 |
| 4822 | #define C_OF 010 |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4823 | #define C_SKIP 020 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4824 | |
| 4825 | struct DdInfo { |
| 4826 | unsigned int flags; |
| 4827 | int64_t count; |
| 4828 | }; |
| 4829 | |
| 4830 | struct DdIo { |
| 4831 | int bsz; /* Block size */ |
| 4832 | char *filename; |
| 4833 | uint8_t *buf; |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4834 | int64_t offset; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4835 | }; |
| 4836 | |
| 4837 | struct DdOpts { |
| 4838 | const char *name; |
| 4839 | int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *); |
| 4840 | unsigned int flag; |
| 4841 | }; |
| 4842 | |
| 4843 | static int img_dd_bs(const char *arg, |
| 4844 | struct DdIo *in, struct DdIo *out, |
| 4845 | struct DdInfo *dd) |
| 4846 | { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4847 | int64_t res; |
| 4848 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4849 | res = cvtnum_full("bs", arg, 1, INT_MAX); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4850 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4851 | if (res < 0) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4852 | return 1; |
| 4853 | } |
| 4854 | in->bsz = out->bsz = res; |
| 4855 | |
| 4856 | return 0; |
| 4857 | } |
| 4858 | |
| 4859 | static int img_dd_count(const char *arg, |
| 4860 | struct DdIo *in, struct DdIo *out, |
| 4861 | struct DdInfo *dd) |
| 4862 | { |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4863 | dd->count = cvtnum("count", arg); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4864 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4865 | if (dd->count < 0) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4866 | return 1; |
| 4867 | } |
| 4868 | |
| 4869 | return 0; |
| 4870 | } |
| 4871 | |
| 4872 | static int img_dd_if(const char *arg, |
| 4873 | struct DdIo *in, struct DdIo *out, |
| 4874 | struct DdInfo *dd) |
| 4875 | { |
| 4876 | in->filename = g_strdup(arg); |
| 4877 | |
| 4878 | return 0; |
| 4879 | } |
| 4880 | |
| 4881 | static int img_dd_of(const char *arg, |
| 4882 | struct DdIo *in, struct DdIo *out, |
| 4883 | struct DdInfo *dd) |
| 4884 | { |
| 4885 | out->filename = g_strdup(arg); |
| 4886 | |
| 4887 | return 0; |
| 4888 | } |
| 4889 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4890 | static int img_dd_skip(const char *arg, |
| 4891 | struct DdIo *in, struct DdIo *out, |
| 4892 | struct DdInfo *dd) |
| 4893 | { |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4894 | in->offset = cvtnum("skip", arg); |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4895 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4896 | if (in->offset < 0) { |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4897 | return 1; |
| 4898 | } |
| 4899 | |
| 4900 | return 0; |
| 4901 | } |
| 4902 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4903 | static int img_dd(int argc, char **argv) |
| 4904 | { |
| 4905 | int ret = 0; |
| 4906 | char *arg = NULL; |
| 4907 | char *tmp; |
| 4908 | BlockDriver *drv = NULL, *proto_drv = NULL; |
| 4909 | BlockBackend *blk1 = NULL, *blk2 = NULL; |
| 4910 | QemuOpts *opts = NULL; |
| 4911 | QemuOptsList *create_opts = NULL; |
| 4912 | Error *local_err = NULL; |
| 4913 | bool image_opts = false; |
| 4914 | int c, i; |
| 4915 | const char *out_fmt = "raw"; |
| 4916 | const char *fmt = NULL; |
| 4917 | int64_t size = 0; |
| 4918 | int64_t block_count = 0, out_pos, in_pos; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4919 | bool force_share = false; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4920 | struct DdInfo dd = { |
| 4921 | .flags = 0, |
| 4922 | .count = 0, |
| 4923 | }; |
| 4924 | struct DdIo in = { |
| 4925 | .bsz = 512, /* Block size is by default 512 bytes */ |
| 4926 | .filename = NULL, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4927 | .buf = NULL, |
| 4928 | .offset = 0 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4929 | }; |
| 4930 | struct DdIo out = { |
| 4931 | .bsz = 512, |
| 4932 | .filename = NULL, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4933 | .buf = NULL, |
| 4934 | .offset = 0 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4935 | }; |
| 4936 | |
| 4937 | const struct DdOpts options[] = { |
| 4938 | { "bs", img_dd_bs, C_BS }, |
| 4939 | { "count", img_dd_count, C_COUNT }, |
| 4940 | { "if", img_dd_if, C_IF }, |
| 4941 | { "of", img_dd_of, C_OF }, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4942 | { "skip", img_dd_skip, C_SKIP }, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4943 | { NULL, NULL, 0 } |
| 4944 | }; |
| 4945 | const struct option long_options[] = { |
| 4946 | { "help", no_argument, 0, 'h'}, |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 4947 | { "object", required_argument, 0, OPTION_OBJECT}, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4948 | { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4949 | { "force-share", no_argument, 0, 'U'}, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4950 | { 0, 0, 0, 0 } |
| 4951 | }; |
| 4952 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4953 | while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4954 | if (c == EOF) { |
| 4955 | break; |
| 4956 | } |
| 4957 | switch (c) { |
| 4958 | case 'O': |
| 4959 | out_fmt = optarg; |
| 4960 | break; |
| 4961 | case 'f': |
| 4962 | fmt = optarg; |
| 4963 | break; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4964 | case ':': |
| 4965 | missing_argument(argv[optind - 1]); |
| 4966 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4967 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4968 | unrecognized_option(argv[optind - 1]); |
| 4969 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4970 | case 'h': |
| 4971 | help(); |
| 4972 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4973 | case 'U': |
| 4974 | force_share = true; |
| 4975 | break; |
Stefan Hajnoczi | 2a24570 | 2017-06-19 16:00:02 +0100 | [diff] [blame] | 4976 | case OPTION_OBJECT: |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 4977 | user_creatable_process_cmdline(optarg); |
Stefan Hajnoczi | 2a24570 | 2017-06-19 16:00:02 +0100 | [diff] [blame] | 4978 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4979 | case OPTION_IMAGE_OPTS: |
| 4980 | image_opts = true; |
| 4981 | break; |
| 4982 | } |
| 4983 | } |
| 4984 | |
| 4985 | for (i = optind; i < argc; i++) { |
| 4986 | int j; |
| 4987 | arg = g_strdup(argv[i]); |
| 4988 | |
| 4989 | tmp = strchr(arg, '='); |
| 4990 | if (tmp == NULL) { |
| 4991 | error_report("unrecognized operand %s", arg); |
| 4992 | ret = -1; |
| 4993 | goto out; |
| 4994 | } |
| 4995 | |
| 4996 | *tmp++ = '\0'; |
| 4997 | |
| 4998 | for (j = 0; options[j].name != NULL; j++) { |
| 4999 | if (!strcmp(arg, options[j].name)) { |
| 5000 | break; |
| 5001 | } |
| 5002 | } |
| 5003 | if (options[j].name == NULL) { |
| 5004 | error_report("unrecognized operand %s", arg); |
| 5005 | ret = -1; |
| 5006 | goto out; |
| 5007 | } |
| 5008 | |
| 5009 | if (options[j].f(tmp, &in, &out, &dd) != 0) { |
| 5010 | ret = -1; |
| 5011 | goto out; |
| 5012 | } |
| 5013 | dd.flags |= options[j].flag; |
| 5014 | g_free(arg); |
| 5015 | arg = NULL; |
| 5016 | } |
| 5017 | |
| 5018 | if (!(dd.flags & C_IF && dd.flags & C_OF)) { |
| 5019 | error_report("Must specify both input and output files"); |
| 5020 | ret = -1; |
| 5021 | goto out; |
| 5022 | } |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 5023 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 5024 | blk1 = img_open(image_opts, in.filename, fmt, 0, false, false, |
| 5025 | force_share); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5026 | |
| 5027 | if (!blk1) { |
| 5028 | ret = -1; |
| 5029 | goto out; |
| 5030 | } |
| 5031 | |
| 5032 | drv = bdrv_find_format(out_fmt); |
| 5033 | if (!drv) { |
| 5034 | error_report("Unknown file format"); |
| 5035 | ret = -1; |
| 5036 | goto out; |
| 5037 | } |
| 5038 | proto_drv = bdrv_find_protocol(out.filename, true, &local_err); |
| 5039 | |
| 5040 | if (!proto_drv) { |
| 5041 | error_report_err(local_err); |
| 5042 | ret = -1; |
| 5043 | goto out; |
| 5044 | } |
| 5045 | if (!drv->create_opts) { |
| 5046 | error_report("Format driver '%s' does not support image creation", |
| 5047 | drv->format_name); |
| 5048 | ret = -1; |
| 5049 | goto out; |
| 5050 | } |
| 5051 | if (!proto_drv->create_opts) { |
| 5052 | error_report("Protocol driver '%s' does not support image creation", |
| 5053 | proto_drv->format_name); |
| 5054 | ret = -1; |
| 5055 | goto out; |
| 5056 | } |
| 5057 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 5058 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
| 5059 | |
| 5060 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
| 5061 | |
| 5062 | size = blk_getlength(blk1); |
| 5063 | if (size < 0) { |
| 5064 | error_report("Failed to get size for '%s'", in.filename); |
| 5065 | ret = -1; |
| 5066 | goto out; |
| 5067 | } |
| 5068 | |
| 5069 | if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz && |
| 5070 | dd.count * in.bsz < size) { |
| 5071 | size = dd.count * in.bsz; |
| 5072 | } |
| 5073 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 5074 | /* Overflow means the specified offset is beyond input image's size */ |
| 5075 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || |
| 5076 | size < in.bsz * in.offset)) { |
| 5077 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort); |
| 5078 | } else { |
| 5079 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, |
| 5080 | size - in.bsz * in.offset, &error_abort); |
| 5081 | } |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5082 | |
| 5083 | ret = bdrv_create(drv, out.filename, opts, &local_err); |
| 5084 | if (ret < 0) { |
| 5085 | error_reportf_err(local_err, |
| 5086 | "%s: error while creating output image: ", |
| 5087 | out.filename); |
| 5088 | ret = -1; |
| 5089 | goto out; |
| 5090 | } |
| 5091 | |
Daniel P. Berrange | ea204dd | 2017-05-15 17:47:10 +0100 | [diff] [blame] | 5092 | /* TODO, we can't honour --image-opts for the target, |
| 5093 | * since it needs to be given in a format compatible |
| 5094 | * with the bdrv_create() call above which does not |
| 5095 | * support image-opts style. |
| 5096 | */ |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 5097 | 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] | 5098 | false, false, false); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5099 | |
| 5100 | if (!blk2) { |
| 5101 | ret = -1; |
| 5102 | goto out; |
| 5103 | } |
| 5104 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 5105 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || |
| 5106 | size < in.offset * in.bsz)) { |
| 5107 | /* We give a warning if the skip option is bigger than the input |
| 5108 | * size and create an empty output disk image (i.e. like dd(1)). |
| 5109 | */ |
| 5110 | error_report("%s: cannot skip to specified offset", in.filename); |
| 5111 | in_pos = size; |
| 5112 | } else { |
| 5113 | in_pos = in.offset * in.bsz; |
| 5114 | } |
| 5115 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5116 | in.buf = g_new(uint8_t, in.bsz); |
| 5117 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 5118 | for (out_pos = 0; in_pos < size; block_count++) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5119 | int in_ret, out_ret; |
| 5120 | |
| 5121 | if (in_pos + in.bsz > size) { |
| 5122 | in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos); |
| 5123 | } else { |
| 5124 | in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz); |
| 5125 | } |
| 5126 | if (in_ret < 0) { |
| 5127 | error_report("error while reading from input image file: %s", |
| 5128 | strerror(-in_ret)); |
| 5129 | ret = -1; |
| 5130 | goto out; |
| 5131 | } |
| 5132 | in_pos += in_ret; |
| 5133 | |
| 5134 | out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0); |
| 5135 | |
| 5136 | if (out_ret < 0) { |
| 5137 | error_report("error while writing to output image file: %s", |
| 5138 | strerror(-out_ret)); |
| 5139 | ret = -1; |
| 5140 | goto out; |
| 5141 | } |
| 5142 | out_pos += out_ret; |
| 5143 | } |
| 5144 | |
| 5145 | out: |
| 5146 | g_free(arg); |
| 5147 | qemu_opts_del(opts); |
| 5148 | qemu_opts_free(create_opts); |
| 5149 | blk_unref(blk1); |
| 5150 | blk_unref(blk2); |
| 5151 | g_free(in.filename); |
| 5152 | g_free(out.filename); |
| 5153 | g_free(in.buf); |
| 5154 | g_free(out.buf); |
| 5155 | |
| 5156 | if (ret) { |
| 5157 | return 1; |
| 5158 | } |
| 5159 | return 0; |
| 5160 | } |
| 5161 | |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5162 | static void dump_json_block_measure_info(BlockMeasureInfo *info) |
| 5163 | { |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 5164 | GString *str; |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5165 | QObject *obj; |
| 5166 | Visitor *v = qobject_output_visitor_new(&obj); |
| 5167 | |
| 5168 | visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort); |
| 5169 | visit_complete(v, &obj); |
Markus Armbruster | 6589f45 | 2020-12-11 18:11:35 +0100 | [diff] [blame] | 5170 | str = qobject_to_json_pretty(obj, true); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5171 | assert(str != NULL); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 5172 | printf("%s\n", str->str); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 5173 | qobject_unref(obj); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5174 | visit_free(v); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 5175 | g_string_free(str, true); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5176 | } |
| 5177 | |
| 5178 | static int img_measure(int argc, char **argv) |
| 5179 | { |
| 5180 | static const struct option long_options[] = { |
| 5181 | {"help", no_argument, 0, 'h'}, |
| 5182 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
| 5183 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 5184 | {"output", required_argument, 0, OPTION_OUTPUT}, |
| 5185 | {"size", required_argument, 0, OPTION_SIZE}, |
| 5186 | {"force-share", no_argument, 0, 'U'}, |
| 5187 | {0, 0, 0, 0} |
| 5188 | }; |
| 5189 | OutputFormat output_format = OFORMAT_HUMAN; |
| 5190 | BlockBackend *in_blk = NULL; |
| 5191 | BlockDriver *drv; |
| 5192 | const char *filename = NULL; |
| 5193 | const char *fmt = NULL; |
| 5194 | const char *out_fmt = "raw"; |
| 5195 | char *options = NULL; |
| 5196 | char *snapshot_name = NULL; |
| 5197 | bool force_share = false; |
| 5198 | QemuOpts *opts = NULL; |
| 5199 | QemuOpts *object_opts = NULL; |
| 5200 | QemuOpts *sn_opts = NULL; |
| 5201 | QemuOptsList *create_opts = NULL; |
| 5202 | bool image_opts = false; |
| 5203 | uint64_t img_size = UINT64_MAX; |
| 5204 | BlockMeasureInfo *info = NULL; |
| 5205 | Error *local_err = NULL; |
| 5206 | int ret = 1; |
| 5207 | int c; |
| 5208 | |
| 5209 | while ((c = getopt_long(argc, argv, "hf:O:o:l:U", |
| 5210 | long_options, NULL)) != -1) { |
| 5211 | switch (c) { |
| 5212 | case '?': |
| 5213 | case 'h': |
| 5214 | help(); |
| 5215 | break; |
| 5216 | case 'f': |
| 5217 | fmt = optarg; |
| 5218 | break; |
| 5219 | case 'O': |
| 5220 | out_fmt = optarg; |
| 5221 | break; |
| 5222 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 5223 | if (accumulate_options(&options, optarg) < 0) { |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5224 | goto out; |
| 5225 | } |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5226 | break; |
| 5227 | case 'l': |
| 5228 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { |
| 5229 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
| 5230 | optarg, false); |
| 5231 | if (!sn_opts) { |
| 5232 | error_report("Failed in parsing snapshot param '%s'", |
| 5233 | optarg); |
| 5234 | goto out; |
| 5235 | } |
| 5236 | } else { |
| 5237 | snapshot_name = optarg; |
| 5238 | } |
| 5239 | break; |
| 5240 | case 'U': |
| 5241 | force_share = true; |
| 5242 | break; |
| 5243 | case OPTION_OBJECT: |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 5244 | user_creatable_process_cmdline(optarg); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5245 | break; |
| 5246 | case OPTION_IMAGE_OPTS: |
| 5247 | image_opts = true; |
| 5248 | break; |
| 5249 | case OPTION_OUTPUT: |
| 5250 | if (!strcmp(optarg, "json")) { |
| 5251 | output_format = OFORMAT_JSON; |
| 5252 | } else if (!strcmp(optarg, "human")) { |
| 5253 | output_format = OFORMAT_HUMAN; |
| 5254 | } else { |
| 5255 | error_report("--output must be used with human or json " |
| 5256 | "as argument."); |
| 5257 | goto out; |
| 5258 | } |
| 5259 | break; |
| 5260 | case OPTION_SIZE: |
| 5261 | { |
| 5262 | int64_t sval; |
| 5263 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 5264 | sval = cvtnum("image size", optarg); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5265 | if (sval < 0) { |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5266 | goto out; |
| 5267 | } |
| 5268 | img_size = (uint64_t)sval; |
| 5269 | } |
| 5270 | break; |
| 5271 | } |
| 5272 | } |
| 5273 | |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5274 | if (argc - optind > 1) { |
| 5275 | error_report("At most one filename argument is allowed."); |
| 5276 | goto out; |
| 5277 | } else if (argc - optind == 1) { |
| 5278 | filename = argv[optind]; |
| 5279 | } |
| 5280 | |
Stefan Hajnoczi | c3673dc | 2020-02-21 11:25:21 +0000 | [diff] [blame] | 5281 | if (!filename && (image_opts || fmt || snapshot_name || sn_opts)) { |
| 5282 | error_report("--image-opts, -f, and -l require a filename argument."); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5283 | goto out; |
| 5284 | } |
| 5285 | if (filename && img_size != UINT64_MAX) { |
| 5286 | error_report("--size N cannot be used together with a filename."); |
| 5287 | goto out; |
| 5288 | } |
| 5289 | if (!filename && img_size == UINT64_MAX) { |
| 5290 | error_report("Either --size N or one filename must be specified."); |
| 5291 | goto out; |
| 5292 | } |
| 5293 | |
| 5294 | if (filename) { |
| 5295 | in_blk = img_open(image_opts, filename, fmt, 0, |
| 5296 | false, false, force_share); |
| 5297 | if (!in_blk) { |
| 5298 | goto out; |
| 5299 | } |
| 5300 | |
| 5301 | if (sn_opts) { |
| 5302 | bdrv_snapshot_load_tmp(blk_bs(in_blk), |
| 5303 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), |
| 5304 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), |
| 5305 | &local_err); |
| 5306 | } else if (snapshot_name != NULL) { |
| 5307 | bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk), |
| 5308 | snapshot_name, &local_err); |
| 5309 | } |
| 5310 | if (local_err) { |
| 5311 | error_reportf_err(local_err, "Failed to load snapshot: "); |
| 5312 | goto out; |
| 5313 | } |
| 5314 | } |
| 5315 | |
| 5316 | drv = bdrv_find_format(out_fmt); |
| 5317 | if (!drv) { |
| 5318 | error_report("Unknown file format '%s'", out_fmt); |
| 5319 | goto out; |
| 5320 | } |
| 5321 | if (!drv->create_opts) { |
| 5322 | error_report("Format driver '%s' does not support image creation", |
| 5323 | drv->format_name); |
| 5324 | goto out; |
| 5325 | } |
| 5326 | |
| 5327 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 5328 | create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts); |
| 5329 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
| 5330 | if (options) { |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 5331 | if (!qemu_opts_do_parse(opts, options, NULL, &local_err)) { |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5332 | error_report_err(local_err); |
| 5333 | error_report("Invalid options for file format '%s'", out_fmt); |
| 5334 | goto out; |
| 5335 | } |
| 5336 | } |
| 5337 | if (img_size != UINT64_MAX) { |
| 5338 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort); |
| 5339 | } |
| 5340 | |
| 5341 | info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err); |
| 5342 | if (local_err) { |
| 5343 | error_report_err(local_err); |
| 5344 | goto out; |
| 5345 | } |
| 5346 | |
| 5347 | if (output_format == OFORMAT_HUMAN) { |
| 5348 | printf("required size: %" PRIu64 "\n", info->required); |
| 5349 | printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated); |
Eric Blake | 5d72c68 | 2020-05-21 14:21:34 -0500 | [diff] [blame] | 5350 | if (info->has_bitmaps) { |
| 5351 | printf("bitmaps size: %" PRIu64 "\n", info->bitmaps); |
| 5352 | } |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5353 | } else { |
| 5354 | dump_json_block_measure_info(info); |
| 5355 | } |
| 5356 | |
| 5357 | ret = 0; |
| 5358 | |
| 5359 | out: |
| 5360 | qapi_free_BlockMeasureInfo(info); |
| 5361 | qemu_opts_del(object_opts); |
| 5362 | qemu_opts_del(opts); |
| 5363 | qemu_opts_del(sn_opts); |
| 5364 | qemu_opts_free(create_opts); |
| 5365 | g_free(options); |
| 5366 | blk_unref(in_blk); |
| 5367 | return ret; |
| 5368 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 5369 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 5370 | static const img_cmd_t img_cmds[] = { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5371 | #define DEF(option, callback, arg_string) \ |
| 5372 | { option, callback }, |
| 5373 | #include "qemu-img-cmds.h" |
| 5374 | #undef DEF |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5375 | { NULL, NULL, }, |
| 5376 | }; |
| 5377 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5378 | int main(int argc, char **argv) |
| 5379 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 5380 | const img_cmd_t *cmd; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5381 | const char *cmdname; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5382 | int c; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5383 | static const struct option long_options[] = { |
| 5384 | {"help", no_argument, 0, 'h'}, |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5385 | {"version", no_argument, 0, 'V'}, |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5386 | {"trace", required_argument, NULL, 'T'}, |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5387 | {0, 0, 0, 0} |
| 5388 | }; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5389 | |
MORITA Kazutaka | 526eda1 | 2013-07-23 17:30:11 +0900 | [diff] [blame] | 5390 | #ifdef CONFIG_POSIX |
| 5391 | signal(SIGPIPE, SIG_IGN); |
| 5392 | #endif |
| 5393 | |
Daniel P. Berrangé | 98c5d2e | 2020-08-25 11:38:48 +0100 | [diff] [blame] | 5394 | socket_init(); |
Christophe Fergeau | f5852ef | 2019-01-31 17:46:14 +0100 | [diff] [blame] | 5395 | error_init(argv[0]); |
Daniel P. Berrange | fe4db84 | 2016-10-04 14:35:52 +0100 | [diff] [blame] | 5396 | module_call_init(MODULE_INIT_TRACE); |
Fam Zheng | 10f5bff | 2014-02-10 14:48:51 +0800 | [diff] [blame] | 5397 | qemu_init_exec_dir(argv[0]); |
Kevin Wolf | 53f76e5 | 2010-12-16 15:10:32 +0100 | [diff] [blame] | 5398 | |
Markus Armbruster | f9734d5 | 2021-07-20 14:53:53 +0200 | [diff] [blame] | 5399 | qemu_init_main_loop(&error_fatal); |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 5400 | |
Eduardo Habkost | e8f2d27 | 2016-05-12 11:10:04 -0300 | [diff] [blame] | 5401 | qcrypto_init(&error_fatal); |
Daniel P. Berrange | c229708 | 2016-04-06 12:12:06 +0100 | [diff] [blame] | 5402 | |
Daniel P. Berrange | 064097d | 2016-02-10 18:41:01 +0000 | [diff] [blame] | 5403 | module_call_init(MODULE_INIT_QOM); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5404 | bdrv_init(); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 5405 | if (argc < 2) { |
| 5406 | error_exit("Not enough arguments"); |
| 5407 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5408 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 5409 | qemu_add_opts(&qemu_source_opts); |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5410 | qemu_add_opts(&qemu_trace_opts); |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 5411 | |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 5412 | while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) { |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5413 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 5414 | case ':': |
| 5415 | missing_argument(argv[optind - 1]); |
| 5416 | return 0; |
Stefan Hajnoczi | 4581c16 | 2017-03-17 18:45:39 +0800 | [diff] [blame] | 5417 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 5418 | unrecognized_option(argv[optind - 1]); |
| 5419 | return 0; |
| 5420 | case 'h': |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5421 | help(); |
| 5422 | return 0; |
| 5423 | case 'V': |
| 5424 | printf(QEMU_IMG_VERSION); |
| 5425 | return 0; |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5426 | case 'T': |
Paolo Bonzini | 92eecff | 2020-11-02 06:58:41 -0500 | [diff] [blame] | 5427 | trace_opt_parse(optarg); |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5428 | break; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5429 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5430 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5431 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5432 | cmdname = argv[optind]; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5433 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5434 | /* reset getopt_long scanning */ |
| 5435 | argc -= optind; |
| 5436 | if (argc < 1) { |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 5437 | return 0; |
| 5438 | } |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5439 | argv += optind; |
Richard W.M. Jones | d339d76 | 2019-01-18 10:11:14 +0000 | [diff] [blame] | 5440 | qemu_reset_optind(); |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5441 | |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5442 | if (!trace_init_backends()) { |
| 5443 | exit(1); |
| 5444 | } |
Paolo Bonzini | 92eecff | 2020-11-02 06:58:41 -0500 | [diff] [blame] | 5445 | trace_init_file(); |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5446 | qemu_set_log(LOG_TRACE); |
| 5447 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5448 | /* find the command */ |
| 5449 | for (cmd = img_cmds; cmd->name != NULL; cmd++) { |
| 5450 | if (!strcmp(cmdname, cmd->name)) { |
| 5451 | return cmd->handler(argc, argv); |
| 5452 | } |
| 5453 | } |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5454 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5455 | /* not found */ |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 5456 | error_exit("Command not found: %s", cmdname); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5457 | } |