bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1 | /* |
bellard | fb43f4d | 2006-08-07 21:34:46 +0000 | [diff] [blame] | 2 | * QEMU disk image utility |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | 68d0f70 | 2008-01-06 17:21:48 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5 | * |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 24 | |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
Eric Blake | c2a3d7d | 2017-07-21 08:50:47 -0500 | [diff] [blame] | 26 | #include <getopt.h> |
| 27 | |
Markus Armbruster | a8d2532 | 2019-05-23 16:35:08 +0200 | [diff] [blame] | 28 | #include "qemu-common.h" |
Fam Zheng | 67a1de0 | 2016-06-01 17:44:21 +0800 | [diff] [blame] | 29 | #include "qemu-version.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 30 | #include "qapi/error.h" |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 31 | #include "qapi/qapi-commands-block-core.h" |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 32 | #include "qapi/qapi-visit-block-core.h" |
Daniel P. Berrange | b3db211 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 33 | #include "qapi/qobject-output-visitor.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 34 | #include "qapi/qmp/qjson.h" |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 35 | #include "qapi/qmp/qdict.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 36 | #include "qemu/cutils.h" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 37 | #include "qemu/config-file.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 38 | #include "qemu/option.h" |
| 39 | #include "qemu/error-report.h" |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 40 | #include "qemu/log.h" |
Markus Armbruster | db72581 | 2019-08-12 07:23:50 +0200 | [diff] [blame] | 41 | #include "qemu/main-loop.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 42 | #include "qemu/module.h" |
Daniel P. Berrangé | 98c5d2e | 2020-08-25 11:38:48 +0100 | [diff] [blame] | 43 | #include "qemu/sockets.h" |
Stefano Garzarella | 97ede57 | 2019-05-08 12:43:24 +0200 | [diff] [blame] | 44 | #include "qemu/units.h" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 45 | #include "qom/object_interfaces.h" |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 46 | #include "sysemu/block-backend.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 47 | #include "block/block_int.h" |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 48 | #include "block/blockjob.h" |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 49 | #include "block/qapi.h" |
Daniel P. Berrange | c229708 | 2016-04-06 12:12:06 +0100 | [diff] [blame] | 50 | #include "crypto/init.h" |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 51 | #include "trace/control.h" |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 52 | #include "qemu/throttle.h" |
| 53 | #include "block/throttle-groups.h" |
bellard | e844533 | 2006-06-14 15:32:10 +0000 | [diff] [blame] | 54 | |
Thomas Huth | 7e563bf | 2018-02-15 12:06:47 +0100 | [diff] [blame] | 55 | #define QEMU_IMG_VERSION "qemu-img version " QEMU_FULL_VERSION \ |
Thomas Huth | 0781dd6 | 2016-10-05 11:54:44 +0200 | [diff] [blame] | 56 | "\n" QEMU_COPYRIGHT "\n" |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 57 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 58 | typedef struct img_cmd_t { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 59 | const char *name; |
| 60 | int (*handler)(int argc, char **argv); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 61 | } img_cmd_t; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 62 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 63 | enum { |
| 64 | OPTION_OUTPUT = 256, |
| 65 | OPTION_BACKING_CHAIN = 257, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 66 | OPTION_OBJECT = 258, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 67 | OPTION_IMAGE_OPTS = 259, |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 68 | OPTION_PATTERN = 260, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 69 | OPTION_FLUSH_INTERVAL = 261, |
| 70 | OPTION_NO_DRAIN = 262, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 71 | OPTION_TARGET_IMAGE_OPTS = 263, |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 72 | OPTION_SIZE = 264, |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 73 | OPTION_PREALLOCATION = 265, |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 74 | OPTION_SHRINK = 266, |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 75 | OPTION_SALVAGE = 267, |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 76 | OPTION_TARGET_IS_ZERO = 268, |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 77 | OPTION_ADD = 269, |
| 78 | OPTION_REMOVE = 270, |
| 79 | OPTION_CLEAR = 271, |
| 80 | OPTION_ENABLE = 272, |
| 81 | OPTION_DISABLE = 273, |
| 82 | OPTION_MERGE = 274, |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 83 | OPTION_BITMAPS = 275, |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 84 | OPTION_FORCE = 276, |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 85 | OPTION_SKIP_BROKEN = 277, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | typedef enum OutputFormat { |
| 89 | OFORMAT_JSON, |
| 90 | OFORMAT_HUMAN, |
| 91 | } OutputFormat; |
| 92 | |
Kevin Wolf | e699614 | 2016-03-15 13:03:11 +0100 | [diff] [blame] | 93 | /* 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] | 94 | #define BDRV_DEFAULT_CACHE "writeback" |
aurel32 | 137519c | 2008-11-30 19:12:49 +0000 | [diff] [blame] | 95 | |
Stefan Hajnoczi | 00c6d40 | 2014-08-27 12:08:56 +0100 | [diff] [blame] | 96 | static void format_print(void *opaque, const char *name) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 97 | { |
Stefan Hajnoczi | 00c6d40 | 2014-08-27 12:08:56 +0100 | [diff] [blame] | 98 | printf(" %s", name); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 101 | static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...) |
| 102 | { |
| 103 | va_list ap; |
| 104 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 105 | va_start(ap, fmt); |
Markus Armbruster | e9e1d92 | 2019-04-17 21:06:27 +0200 | [diff] [blame] | 106 | error_vreport(fmt, ap); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 107 | va_end(ap); |
| 108 | |
Markus Armbruster | e9e1d92 | 2019-04-17 21:06:27 +0200 | [diff] [blame] | 109 | error_printf("Try 'qemu-img --help' for more information\n"); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 110 | exit(EXIT_FAILURE); |
| 111 | } |
| 112 | |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 113 | static void QEMU_NORETURN missing_argument(const char *option) |
| 114 | { |
| 115 | error_exit("missing argument for option '%s'", option); |
| 116 | } |
| 117 | |
| 118 | static void QEMU_NORETURN unrecognized_option(const char *option) |
| 119 | { |
| 120 | error_exit("unrecognized option '%s'", option); |
| 121 | } |
| 122 | |
Eric Blake | 0562adf | 2020-05-12 20:16:41 -0500 | [diff] [blame] | 123 | /* Please keep in synch with docs/tools/qemu-img.rst */ |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 124 | static void QEMU_NORETURN help(void) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 125 | { |
Paolo Bonzini | e00291c | 2010-02-04 16:49:56 +0100 | [diff] [blame] | 126 | const char *help_msg = |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 127 | QEMU_IMG_VERSION |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 128 | "usage: qemu-img [standard options] command [command options]\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 129 | "QEMU disk image utility\n" |
| 130 | "\n" |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 131 | " '-h', '--help' display this help and exit\n" |
| 132 | " '-V', '--version' output version information and exit\n" |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 133 | " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n" |
| 134 | " specify tracing options\n" |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 135 | "\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 136 | "Command syntax:\n" |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 137 | #define DEF(option, callback, arg_string) \ |
| 138 | " " arg_string "\n" |
| 139 | #include "qemu-img-cmds.h" |
| 140 | #undef DEF |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 141 | "\n" |
| 142 | "Command parameters:\n" |
| 143 | " 'filename' is a disk image filename\n" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 144 | " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n" |
| 145 | " manual page for a description of the object properties. The most common\n" |
| 146 | " object type is a 'secret', which is used to supply passwords and/or\n" |
| 147 | " encryption keys.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 148 | " '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] | 149 | " '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] | 150 | " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n" |
| 151 | " 'directsync' and 'unsafe' (default for convert)\n" |
Stefan Hajnoczi | bb87fdf | 2014-09-02 11:01:02 +0100 | [diff] [blame] | 152 | " 'src_cache' is the cache mode used to read input disk images, the valid\n" |
| 153 | " options are the same as for the 'cache' option\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 154 | " 'size' is the disk image size in bytes. Optional suffixes\n" |
Kevin Wolf | 5e00984 | 2013-06-05 14:19:27 +0200 | [diff] [blame] | 155 | " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n" |
| 156 | " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n" |
| 157 | " supported. 'b' is ignored.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 158 | " 'output_filename' is the destination disk image filename\n" |
| 159 | " 'output_fmt' is the destination format\n" |
| 160 | " 'options' is a comma separated list of format specific options in a\n" |
| 161 | " name=value format. Use -o ? for an overview of the options supported by the\n" |
| 162 | " used format\n" |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 163 | " 'snapshot_param' is param used for internal snapshot, format\n" |
| 164 | " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n" |
| 165 | " '[ID_OR_NAME]'\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 166 | " '-c' indicates that target image must be compressed (qcow format only)\n" |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 167 | " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n" |
| 168 | " new backing file match exactly. The image doesn't need a working\n" |
| 169 | " backing file before rebasing in this case (useful for renaming the\n" |
| 170 | " backing file). For image creation, allow creating without attempting\n" |
| 171 | " to open the backing file.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 172 | " '-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] | 173 | " '-p' show progress of command (only certain commands)\n" |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 174 | " '-q' use Quiet mode - do not print any output (except errors)\n" |
Peter Lieven | 11b6699 | 2013-10-24 12:07:05 +0200 | [diff] [blame] | 175 | " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n" |
| 176 | " contain only zeros for qemu-img to create a sparse image during\n" |
| 177 | " conversion. If the number of bytes is 0, the source will not be scanned for\n" |
| 178 | " unallocated or zero sectors, and the destination image will always be\n" |
| 179 | " fully allocated\n" |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 180 | " '--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] | 181 | " '-n' skips the target volume creation (useful if the volume is created\n" |
| 182 | " prior to running qemu-img)\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 183 | "\n" |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 184 | "Parameters to bitmap subcommand:\n" |
| 185 | " 'bitmap' is the name of the bitmap to manipulate, through one or more\n" |
| 186 | " actions from '--add', '--remove', '--clear', '--enable', '--disable',\n" |
| 187 | " or '--merge source'\n" |
| 188 | " '-g granularity' sets the granularity for '--add' actions\n" |
| 189 | " '-b source' and '-F src_fmt' tell '--merge' actions to find the source\n" |
| 190 | " bitmaps from an alternative file\n" |
| 191 | "\n" |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 192 | "Parameters to check subcommand:\n" |
| 193 | " '-r' tries to repair any inconsistencies that are found during the check.\n" |
| 194 | " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n" |
| 195 | " 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] | 196 | " hiding corruption that has already occurred.\n" |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 197 | "\n" |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 198 | "Parameters to convert subcommand:\n" |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 199 | " '--bitmaps' copies all top-level persistent bitmaps to destination\n" |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 200 | " '-m' specifies how many coroutines work in parallel during the convert\n" |
| 201 | " process (defaults to 8)\n" |
| 202 | " '-W' allow to write to the target out of order rather than sequential\n" |
| 203 | "\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 204 | "Parameters to snapshot subcommand:\n" |
| 205 | " 'snapshot' is the name of the snapshot to create, apply or delete\n" |
| 206 | " '-a' applies a snapshot (revert disk to saved state)\n" |
| 207 | " '-c' creates a snapshot\n" |
| 208 | " '-d' deletes a snapshot\n" |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 209 | " '-l' lists all snapshots in the given image\n" |
| 210 | "\n" |
| 211 | "Parameters to compare subcommand:\n" |
| 212 | " '-f' first image format\n" |
| 213 | " '-F' second image format\n" |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 214 | " '-s' run in Strict mode - fail on different image size or sector allocation\n" |
| 215 | "\n" |
| 216 | "Parameters to dd subcommand:\n" |
| 217 | " 'bs=BYTES' read and write up to BYTES bytes at a time " |
| 218 | "(default: 512)\n" |
| 219 | " 'count=N' copy only N input blocks\n" |
| 220 | " 'if=FILE' read from FILE\n" |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 221 | " 'of=FILE' write to FILE\n" |
| 222 | " '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] | 223 | |
| 224 | printf("%s\nSupported formats:", help_msg); |
Andrey Shinkevich | 9ac404c | 2019-03-07 16:33:58 +0300 | [diff] [blame] | 225 | bdrv_iterate_format(format_print, NULL, false); |
Eric Blake | f5048cb | 2017-08-03 11:33:53 -0500 | [diff] [blame] | 226 | printf("\n\n" QEMU_HELP_BOTTOM "\n"); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 227 | exit(EXIT_SUCCESS); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Markus Armbruster | 80c710c | 2020-04-15 09:49:26 +0200 | [diff] [blame] | 230 | /* |
| 231 | * Is @optarg safe for accumulate_options()? |
| 232 | * It is when multiple of them can be joined together separated by ','. |
| 233 | * To make that work, @optarg must not start with ',' (or else a |
| 234 | * separating ',' preceding it gets escaped), and it must not end with |
| 235 | * an odd number of ',' (or else a separating ',' following it gets |
Markus Armbruster | f62514b | 2020-04-15 09:49:27 +0200 | [diff] [blame] | 236 | * escaped), or be empty (or else a separating ',' preceding it can |
| 237 | * escape a separating ',' following it). |
| 238 | * |
Markus Armbruster | 80c710c | 2020-04-15 09:49:26 +0200 | [diff] [blame] | 239 | */ |
| 240 | static bool is_valid_option_list(const char *optarg) |
| 241 | { |
| 242 | size_t len = strlen(optarg); |
| 243 | size_t i; |
| 244 | |
Markus Armbruster | f62514b | 2020-04-15 09:49:27 +0200 | [diff] [blame] | 245 | if (!optarg[0] || optarg[0] == ',') { |
Markus Armbruster | 80c710c | 2020-04-15 09:49:26 +0200 | [diff] [blame] | 246 | return false; |
| 247 | } |
| 248 | |
| 249 | for (i = len; i > 0 && optarg[i - 1] == ','; i--) { |
| 250 | } |
| 251 | if ((len - i) % 2) { |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | return true; |
| 256 | } |
| 257 | |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 258 | static int accumulate_options(char **options, char *optarg) |
| 259 | { |
| 260 | char *new_options; |
| 261 | |
| 262 | if (!is_valid_option_list(optarg)) { |
| 263 | error_report("Invalid option list: %s", optarg); |
| 264 | return -1; |
| 265 | } |
| 266 | |
| 267 | if (!*options) { |
| 268 | *options = g_strdup(optarg); |
| 269 | } else { |
| 270 | new_options = g_strdup_printf("%s,%s", *options, optarg); |
| 271 | g_free(*options); |
| 272 | *options = new_options; |
| 273 | } |
| 274 | return 0; |
| 275 | } |
| 276 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 277 | static QemuOptsList qemu_source_opts = { |
| 278 | .name = "source", |
| 279 | .implied_opt_name = "file", |
| 280 | .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head), |
| 281 | .desc = { |
| 282 | { } |
| 283 | }, |
| 284 | }; |
| 285 | |
Stefan Weil | 7c30f65 | 2013-06-16 17:01:05 +0200 | [diff] [blame] | 286 | static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...) |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 287 | { |
| 288 | int ret = 0; |
| 289 | if (!quiet) { |
| 290 | va_list args; |
| 291 | va_start(args, fmt); |
| 292 | ret = vprintf(fmt, args); |
| 293 | va_end(args); |
| 294 | } |
| 295 | return ret; |
| 296 | } |
| 297 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 298 | |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 299 | static int print_block_option_help(const char *filename, const char *fmt) |
| 300 | { |
| 301 | BlockDriver *drv, *proto_drv; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 302 | QemuOptsList *create_opts = NULL; |
Max Reitz | b65a5e1 | 2015-02-05 13:58:12 -0500 | [diff] [blame] | 303 | Error *local_err = NULL; |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 304 | |
| 305 | /* Find driver and parse its options */ |
| 306 | drv = bdrv_find_format(fmt); |
| 307 | if (!drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 308 | error_report("Unknown file format '%s'", fmt); |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 309 | return 1; |
| 310 | } |
| 311 | |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 312 | if (!drv->create_opts) { |
| 313 | error_report("Format driver '%s' does not support image creation", fmt); |
| 314 | return 1; |
| 315 | } |
| 316 | |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 317 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 318 | if (filename) { |
Max Reitz | b65a5e1 | 2015-02-05 13:58:12 -0500 | [diff] [blame] | 319 | proto_drv = bdrv_find_protocol(filename, true, &local_err); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 320 | if (!proto_drv) { |
Markus Armbruster | 2867ce4 | 2015-03-12 16:08:02 +0100 | [diff] [blame] | 321 | error_report_err(local_err); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 322 | qemu_opts_free(create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 323 | return 1; |
| 324 | } |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 325 | if (!proto_drv->create_opts) { |
Max Reitz | f099887 | 2018-11-19 11:19:20 +0100 | [diff] [blame] | 326 | error_report("Protocol driver '%s' does not support image creation", |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 327 | proto_drv->format_name); |
Max Reitz | 3ecd5a4 | 2018-11-19 11:19:21 +0100 | [diff] [blame] | 328 | qemu_opts_free(create_opts); |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 329 | return 1; |
| 330 | } |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 331 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 332 | } |
| 333 | |
Max Reitz | f4619af | 2019-04-13 17:20:37 +0200 | [diff] [blame] | 334 | if (filename) { |
| 335 | printf("Supported options:\n"); |
| 336 | } else { |
| 337 | printf("Supported %s options:\n", fmt); |
| 338 | } |
Max Reitz | 6389871 | 2018-10-19 18:49:25 +0200 | [diff] [blame] | 339 | qemu_opts_print_help(create_opts, false); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 340 | qemu_opts_free(create_opts); |
Max Reitz | f4619af | 2019-04-13 17:20:37 +0200 | [diff] [blame] | 341 | |
| 342 | if (!filename) { |
| 343 | printf("\n" |
| 344 | "The protocol level may support further options.\n" |
| 345 | "Specify the target filename to include those options.\n"); |
| 346 | } |
| 347 | |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 348 | return 0; |
| 349 | } |
| 350 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 351 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 352 | static BlockBackend *img_open_opts(const char *optstr, |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 353 | QemuOpts *opts, int flags, bool writethrough, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 354 | bool quiet, bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 355 | { |
| 356 | QDict *options; |
| 357 | Error *local_err = NULL; |
| 358 | BlockBackend *blk; |
| 359 | options = qemu_opts_to_qdict(opts, NULL); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 360 | if (force_share) { |
| 361 | if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE) |
Max Reitz | 4615f87 | 2018-05-02 22:20:50 +0200 | [diff] [blame] | 362 | && strcmp(qdict_get_str(options, BDRV_OPT_FORCE_SHARE), "on")) { |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 363 | error_report("--force-share/-U conflicts with image options"); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 364 | qobject_unref(options); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 365 | return NULL; |
| 366 | } |
Max Reitz | 4615f87 | 2018-05-02 22:20:50 +0200 | [diff] [blame] | 367 | qdict_put_str(options, BDRV_OPT_FORCE_SHARE, "on"); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 368 | } |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 369 | blk = blk_new_open(NULL, NULL, options, flags, &local_err); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 370 | if (!blk) { |
Daniel P. Berrange | 143605a | 2016-04-06 10:16:18 +0100 | [diff] [blame] | 371 | error_reportf_err(local_err, "Could not open '%s': ", optstr); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 372 | return NULL; |
| 373 | } |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 374 | blk_set_enable_write_cache(blk, !writethrough); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 375 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 376 | return blk; |
| 377 | } |
| 378 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 379 | static BlockBackend *img_open_file(const char *filename, |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 380 | QDict *options, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 381 | const char *fmt, int flags, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 382 | bool writethrough, bool quiet, |
| 383 | bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 384 | { |
| 385 | BlockBackend *blk; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 386 | Error *local_err = NULL; |
Kevin Wolf | ad71713 | 2010-12-16 15:37:41 +0100 | [diff] [blame] | 387 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 388 | if (!options) { |
| 389 | options = qdict_new(); |
| 390 | } |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 391 | if (fmt) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 392 | qdict_put_str(options, "driver", fmt); |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 393 | } |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 394 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 395 | if (force_share) { |
Eric Blake | 579cf1d | 2017-05-15 14:54:39 -0500 | [diff] [blame] | 396 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 397 | } |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 398 | blk = blk_new_open(filename, NULL, options, flags, &local_err); |
Max Reitz | 5bd3132 | 2015-02-05 13:58:16 -0500 | [diff] [blame] | 399 | if (!blk) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 400 | error_reportf_err(local_err, "Could not open '%s': ", filename); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 401 | return NULL; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 402 | } |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 403 | blk_set_enable_write_cache(blk, !writethrough); |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 404 | |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 405 | return blk; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 408 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 409 | static int img_add_key_secrets(void *opaque, |
| 410 | const char *name, const char *value, |
| 411 | Error **errp) |
| 412 | { |
| 413 | QDict *options = opaque; |
| 414 | |
| 415 | if (g_str_has_suffix(name, "key-secret")) { |
Eric Blake | 187f47e | 2017-06-24 12:10:07 -0600 | [diff] [blame] | 416 | qdict_put_str(options, name, value); |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 422 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 423 | static BlockBackend *img_open(bool image_opts, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 424 | const char *filename, |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 425 | const char *fmt, int flags, bool writethrough, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 426 | bool quiet, bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 427 | { |
| 428 | BlockBackend *blk; |
| 429 | if (image_opts) { |
| 430 | QemuOpts *opts; |
| 431 | if (fmt) { |
| 432 | error_report("--image-opts and --format are mutually exclusive"); |
| 433 | return NULL; |
| 434 | } |
| 435 | opts = qemu_opts_parse_noisily(qemu_find_opts("source"), |
| 436 | filename, true); |
| 437 | if (!opts) { |
| 438 | return NULL; |
| 439 | } |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 440 | blk = img_open_opts(filename, opts, flags, writethrough, quiet, |
| 441 | force_share); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 442 | } else { |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 443 | blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 444 | force_share); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 445 | } |
| 446 | return blk; |
| 447 | } |
| 448 | |
| 449 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 450 | static int add_old_style_options(const char *fmt, QemuOpts *opts, |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 451 | const char *base_filename, |
| 452 | const char *base_fmt) |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 453 | { |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 454 | if (base_filename) { |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 455 | if (!qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, |
Markus Armbruster | 9e194e0 | 2020-07-07 18:06:11 +0200 | [diff] [blame] | 456 | NULL)) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 457 | error_report("Backing file not supported for file format '%s'", |
| 458 | fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 459 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | if (base_fmt) { |
Markus Armbruster | 9e194e0 | 2020-07-07 18:06:11 +0200 | [diff] [blame] | 463 | if (!qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, NULL)) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 464 | error_report("Backing file format not supported for file " |
| 465 | "format '%s'", fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 466 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 467 | } |
| 468 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 469 | return 0; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 470 | } |
| 471 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 472 | static int64_t cvtnum_full(const char *name, const char *value, int64_t min, |
| 473 | int64_t max) |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 474 | { |
Markus Armbruster | f17fd4f | 2017-02-21 21:14:06 +0100 | [diff] [blame] | 475 | int err; |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 476 | uint64_t res; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 477 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 478 | err = qemu_strtosz(value, NULL, &res); |
| 479 | if (err < 0 && err != -ERANGE) { |
| 480 | error_report("Invalid %s specified. You may use " |
| 481 | "k, M, G, T, P or E suffixes for", name); |
| 482 | error_report("kilobytes, megabytes, gigabytes, terabytes, " |
| 483 | "petabytes and exabytes."); |
Markus Armbruster | f17fd4f | 2017-02-21 21:14:06 +0100 | [diff] [blame] | 484 | return err; |
| 485 | } |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 486 | if (err == -ERANGE || res > max || res < min) { |
| 487 | error_report("Invalid %s specified. Must be between %" PRId64 |
| 488 | " and %" PRId64 ".", name, min, max); |
Markus Armbruster | f46bfdb | 2017-02-21 21:14:07 +0100 | [diff] [blame] | 489 | return -ERANGE; |
| 490 | } |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 491 | return res; |
| 492 | } |
| 493 | |
| 494 | static int64_t cvtnum(const char *name, const char *value) |
| 495 | { |
| 496 | return cvtnum_full(name, value, 0, INT64_MAX); |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 497 | } |
| 498 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 499 | static int img_create(int argc, char **argv) |
| 500 | { |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 501 | int c; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 502 | uint64_t img_size = -1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 503 | const char *fmt = "raw"; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 504 | const char *base_fmt = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 505 | const char *filename; |
| 506 | const char *base_filename = NULL; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 507 | char *options = NULL; |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 508 | Error *local_err = NULL; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 509 | bool quiet = false; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 510 | int flags = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 511 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 512 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 513 | static const struct option long_options[] = { |
| 514 | {"help", no_argument, 0, 'h'}, |
| 515 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 516 | {0, 0, 0, 0} |
| 517 | }; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 518 | c = getopt_long(argc, argv, ":F:b:f:ho:qu", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 519 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 520 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 521 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 522 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 523 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 524 | case ':': |
| 525 | missing_argument(argv[optind - 1]); |
| 526 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 527 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 528 | unrecognized_option(argv[optind - 1]); |
| 529 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 530 | case 'h': |
| 531 | help(); |
| 532 | break; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 533 | case 'F': |
| 534 | base_fmt = optarg; |
| 535 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 536 | case 'b': |
| 537 | base_filename = optarg; |
| 538 | break; |
| 539 | case 'f': |
| 540 | fmt = optarg; |
| 541 | break; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 542 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 543 | if (accumulate_options(&options, optarg) < 0) { |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 544 | goto fail; |
| 545 | } |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 546 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 547 | case 'q': |
| 548 | quiet = true; |
| 549 | break; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 550 | case 'u': |
| 551 | flags |= BDRV_O_NO_BACKING; |
| 552 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 553 | case OPTION_OBJECT: |
| 554 | user_creatable_process_cmdline(optarg); |
| 555 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 556 | } |
| 557 | } |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 558 | |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 559 | /* Get the filename */ |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 560 | filename = (optind < argc) ? argv[optind] : NULL; |
| 561 | if (options && has_help_option(options)) { |
| 562 | g_free(options); |
| 563 | return print_block_option_help(filename, fmt); |
| 564 | } |
| 565 | |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 566 | if (optind >= argc) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 567 | error_exit("Expecting image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 568 | } |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 569 | optind++; |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 570 | |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 571 | /* Get image size, if specified */ |
| 572 | if (optind < argc) { |
Jes Sorensen | 70b4f4b | 2011-01-05 11:41:02 +0100 | [diff] [blame] | 573 | int64_t sval; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 574 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 575 | sval = cvtnum("image size", argv[optind++]); |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 576 | if (sval < 0) { |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 577 | goto fail; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 578 | } |
| 579 | img_size = (uint64_t)sval; |
| 580 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 581 | if (optind != argc) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 582 | error_exit("Unexpected argument: %s", argv[optind]); |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 583 | } |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 584 | |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 585 | bdrv_img_create(filename, fmt, base_filename, base_fmt, |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 586 | options, img_size, flags, quiet, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 587 | if (local_err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 588 | error_reportf_err(local_err, "%s: ", filename); |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 589 | goto fail; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 590 | } |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 591 | |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 592 | g_free(options); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 593 | return 0; |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 594 | |
| 595 | fail: |
| 596 | g_free(options); |
| 597 | return 1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 600 | static void dump_json_image_check(ImageCheck *check, bool quiet) |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 601 | { |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 602 | GString *str; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 603 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 604 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 605 | |
| 606 | visit_type_ImageCheck(v, NULL, &check, &error_abort); |
| 607 | visit_complete(v, &obj); |
Markus Armbruster | 6589f45 | 2020-12-11 18:11:35 +0100 | [diff] [blame] | 608 | str = qobject_to_json_pretty(obj, true); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 609 | assert(str != NULL); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 610 | qprintf(quiet, "%s\n", str->str); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 611 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 612 | visit_free(v); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 613 | g_string_free(str, true); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 614 | } |
| 615 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 616 | static void dump_human_image_check(ImageCheck *check, bool quiet) |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 617 | { |
| 618 | if (!(check->corruptions || check->leaks || check->check_errors)) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 619 | qprintf(quiet, "No errors were found on the image.\n"); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 620 | } else { |
| 621 | if (check->corruptions) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 622 | qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n" |
| 623 | "Data may be corrupted, or further writes to the image " |
| 624 | "may corrupt it.\n", |
| 625 | check->corruptions); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | if (check->leaks) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 629 | qprintf(quiet, |
| 630 | "\n%" PRId64 " leaked clusters were found on the image.\n" |
| 631 | "This means waste of disk space, but no harm to data.\n", |
| 632 | check->leaks); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | if (check->check_errors) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 636 | qprintf(quiet, |
| 637 | "\n%" PRId64 |
| 638 | " internal errors have occurred during the check.\n", |
| 639 | check->check_errors); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
| 643 | if (check->total_clusters != 0 && check->allocated_clusters != 0) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 644 | qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, " |
| 645 | "%0.2f%% fragmented, %0.2f%% compressed clusters\n", |
| 646 | check->allocated_clusters, check->total_clusters, |
| 647 | check->allocated_clusters * 100.0 / check->total_clusters, |
| 648 | check->fragmented_clusters * 100.0 / check->allocated_clusters, |
| 649 | check->compressed_clusters * 100.0 / |
| 650 | check->allocated_clusters); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | if (check->image_end_offset) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 654 | qprintf(quiet, |
| 655 | "Image end offset: %" PRId64 "\n", check->image_end_offset); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | |
| 659 | static int collect_image_check(BlockDriverState *bs, |
| 660 | ImageCheck *check, |
| 661 | const char *filename, |
| 662 | const char *fmt, |
| 663 | int fix) |
| 664 | { |
| 665 | int ret; |
| 666 | BdrvCheckResult result; |
| 667 | |
| 668 | ret = bdrv_check(bs, &result, fix); |
| 669 | if (ret < 0) { |
| 670 | return ret; |
| 671 | } |
| 672 | |
| 673 | check->filename = g_strdup(filename); |
| 674 | check->format = g_strdup(bdrv_get_format_name(bs)); |
| 675 | check->check_errors = result.check_errors; |
| 676 | check->corruptions = result.corruptions; |
| 677 | check->has_corruptions = result.corruptions != 0; |
| 678 | check->leaks = result.leaks; |
| 679 | check->has_leaks = result.leaks != 0; |
| 680 | check->corruptions_fixed = result.corruptions_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 681 | check->has_corruptions_fixed = result.corruptions_fixed != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 682 | check->leaks_fixed = result.leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 683 | check->has_leaks_fixed = result.leaks_fixed != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 684 | check->image_end_offset = result.image_end_offset; |
| 685 | check->has_image_end_offset = result.image_end_offset != 0; |
| 686 | check->total_clusters = result.bfi.total_clusters; |
| 687 | check->has_total_clusters = result.bfi.total_clusters != 0; |
| 688 | check->allocated_clusters = result.bfi.allocated_clusters; |
| 689 | check->has_allocated_clusters = result.bfi.allocated_clusters != 0; |
| 690 | check->fragmented_clusters = result.bfi.fragmented_clusters; |
| 691 | check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0; |
Stefan Hajnoczi | e6439d7 | 2013-02-07 17:15:04 +0100 | [diff] [blame] | 692 | check->compressed_clusters = result.bfi.compressed_clusters; |
| 693 | check->has_compressed_clusters = result.bfi.compressed_clusters != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 694 | |
| 695 | return 0; |
| 696 | } |
| 697 | |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 698 | /* |
| 699 | * Checks an image for consistency. Exit codes: |
| 700 | * |
Max Reitz | d6635c4 | 2014-06-02 22:15:21 +0200 | [diff] [blame] | 701 | * 0 - Check completed, image is good |
| 702 | * 1 - Check not completed because of internal errors |
| 703 | * 2 - Check completed, image is corrupted |
| 704 | * 3 - Check completed, image has leaked clusters, but is good otherwise |
| 705 | * 63 - Checks are not supported by the image format |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 706 | */ |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 707 | static int img_check(int argc, char **argv) |
| 708 | { |
| 709 | int c, ret; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 710 | OutputFormat output_format = OFORMAT_HUMAN; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 711 | const char *filename, *fmt, *output, *cache; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 712 | BlockBackend *blk; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 713 | BlockDriverState *bs; |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 714 | int fix = 0; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 715 | int flags = BDRV_O_CHECK; |
| 716 | bool writethrough; |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 717 | ImageCheck *check; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 718 | bool quiet = false; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 719 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 720 | bool force_share = false; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 721 | |
| 722 | fmt = NULL; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 723 | output = NULL; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 724 | cache = BDRV_DEFAULT_CACHE; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 725 | |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 726 | for(;;) { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 727 | int option_index = 0; |
| 728 | static const struct option long_options[] = { |
| 729 | {"help", no_argument, 0, 'h'}, |
| 730 | {"format", required_argument, 0, 'f'}, |
Prasad Joshi | 4fd6a98 | 2014-03-25 00:08:54 +0530 | [diff] [blame] | 731 | {"repair", required_argument, 0, 'r'}, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 732 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 733 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 734 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 735 | {"force-share", no_argument, 0, 'U'}, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 736 | {0, 0, 0, 0} |
| 737 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 738 | c = getopt_long(argc, argv, ":hf:r:T:qU", |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 739 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 740 | if (c == -1) { |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 741 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 742 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 743 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 744 | case ':': |
| 745 | missing_argument(argv[optind - 1]); |
| 746 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 747 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 748 | unrecognized_option(argv[optind - 1]); |
| 749 | break; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 750 | case 'h': |
| 751 | help(); |
| 752 | break; |
| 753 | case 'f': |
| 754 | fmt = optarg; |
| 755 | break; |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 756 | case 'r': |
| 757 | flags |= BDRV_O_RDWR; |
| 758 | |
| 759 | if (!strcmp(optarg, "leaks")) { |
| 760 | fix = BDRV_FIX_LEAKS; |
| 761 | } else if (!strcmp(optarg, "all")) { |
| 762 | fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS; |
| 763 | } else { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 764 | error_exit("Unknown option value for -r " |
| 765 | "(expecting 'leaks' or 'all'): %s", optarg); |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 766 | } |
| 767 | break; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 768 | case OPTION_OUTPUT: |
| 769 | output = optarg; |
| 770 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 771 | case 'T': |
| 772 | cache = optarg; |
| 773 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 774 | case 'q': |
| 775 | quiet = true; |
| 776 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 777 | case 'U': |
| 778 | force_share = true; |
| 779 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 780 | case OPTION_OBJECT: |
| 781 | user_creatable_process_cmdline(optarg); |
| 782 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 783 | case OPTION_IMAGE_OPTS: |
| 784 | image_opts = true; |
| 785 | break; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 786 | } |
| 787 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 788 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 789 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 790 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 791 | filename = argv[optind++]; |
| 792 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 793 | if (output && !strcmp(output, "json")) { |
| 794 | output_format = OFORMAT_JSON; |
| 795 | } else if (output && !strcmp(output, "human")) { |
| 796 | output_format = OFORMAT_HUMAN; |
| 797 | } else if (output) { |
| 798 | error_report("--output must be used with human or json as argument."); |
| 799 | return 1; |
| 800 | } |
| 801 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 802 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 803 | if (ret < 0) { |
| 804 | error_report("Invalid source cache option: %s", cache); |
| 805 | return 1; |
| 806 | } |
| 807 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 808 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 809 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 810 | if (!blk) { |
| 811 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 812 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 813 | bs = blk_bs(blk); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 814 | |
| 815 | check = g_new0(ImageCheck, 1); |
| 816 | ret = collect_image_check(bs, check, filename, fmt, fix); |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 817 | |
| 818 | if (ret == -ENOTSUP) { |
Max Reitz | 55d492d | 2014-05-31 21:33:30 +0200 | [diff] [blame] | 819 | error_report("This image format does not support checks"); |
Peter Lieven | fefddf9 | 2013-10-24 08:53:34 +0200 | [diff] [blame] | 820 | ret = 63; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 821 | goto fail; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 822 | } |
| 823 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 824 | if (check->corruptions_fixed || check->leaks_fixed) { |
| 825 | int corruptions_fixed, leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 826 | bool has_leaks_fixed, has_corruptions_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 827 | |
| 828 | leaks_fixed = check->leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 829 | has_leaks_fixed = check->has_leaks_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 830 | corruptions_fixed = check->corruptions_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 831 | has_corruptions_fixed = check->has_corruptions_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 832 | |
| 833 | if (output_format == OFORMAT_HUMAN) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 834 | qprintf(quiet, |
| 835 | "The following inconsistencies were found and repaired:\n\n" |
| 836 | " %" PRId64 " leaked clusters\n" |
| 837 | " %" PRId64 " corruptions\n\n" |
| 838 | "Double checking the fixed image now...\n", |
| 839 | check->leaks_fixed, |
| 840 | check->corruptions_fixed); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 841 | } |
| 842 | |
Pan Nengyuan | fc124ea | 2020-02-27 09:29:50 +0800 | [diff] [blame] | 843 | qapi_free_ImageCheck(check); |
| 844 | check = g_new0(ImageCheck, 1); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 845 | ret = collect_image_check(bs, check, filename, fmt, 0); |
| 846 | |
| 847 | check->leaks_fixed = leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 848 | check->has_leaks_fixed = has_leaks_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 849 | check->corruptions_fixed = corruptions_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 850 | check->has_corruptions_fixed = has_corruptions_fixed; |
Kevin Wolf | ccf3471 | 2012-05-11 18:16:54 +0200 | [diff] [blame] | 851 | } |
| 852 | |
Max Reitz | 832390a | 2014-10-23 15:29:12 +0200 | [diff] [blame] | 853 | if (!ret) { |
| 854 | switch (output_format) { |
| 855 | case OFORMAT_HUMAN: |
| 856 | dump_human_image_check(check, quiet); |
| 857 | break; |
| 858 | case OFORMAT_JSON: |
| 859 | dump_json_image_check(check, quiet); |
| 860 | break; |
| 861 | } |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | if (ret || check->check_errors) { |
Max Reitz | 832390a | 2014-10-23 15:29:12 +0200 | [diff] [blame] | 865 | if (ret) { |
| 866 | error_report("Check failed: %s", strerror(-ret)); |
| 867 | } else { |
| 868 | error_report("Check failed"); |
| 869 | } |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 870 | ret = 1; |
| 871 | goto fail; |
| 872 | } |
| 873 | |
| 874 | if (check->corruptions) { |
| 875 | ret = 2; |
| 876 | } else if (check->leaks) { |
| 877 | ret = 3; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 878 | } else { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 879 | ret = 0; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 882 | fail: |
| 883 | qapi_free_ImageCheck(check); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 884 | blk_unref(blk); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 885 | return ret; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 888 | typedef struct CommonBlockJobCBInfo { |
| 889 | BlockDriverState *bs; |
| 890 | Error **errp; |
| 891 | } CommonBlockJobCBInfo; |
| 892 | |
| 893 | static void common_block_job_cb(void *opaque, int ret) |
| 894 | { |
| 895 | CommonBlockJobCBInfo *cbi = opaque; |
| 896 | |
| 897 | if (ret < 0) { |
| 898 | error_setg_errno(cbi->errp, -ret, "Block job failed"); |
| 899 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | static void run_block_job(BlockJob *job, Error **errp) |
| 903 | { |
Emanuele Giuseppe Esposito | a7b4f8f | 2021-06-14 10:11:29 +0200 | [diff] [blame] | 904 | uint64_t progress_current, progress_total; |
Kevin Wolf | b75536c | 2016-04-18 17:30:17 +0200 | [diff] [blame] | 905 | AioContext *aio_context = blk_get_aio_context(job->blk); |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 906 | int ret = 0; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 907 | |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 908 | aio_context_acquire(aio_context); |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 909 | job_ref(&job->job); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 910 | do { |
Kevin Wolf | 30a5c88 | 2018-05-04 12:17:20 +0200 | [diff] [blame] | 911 | float progress = 0.0f; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 912 | aio_poll(aio_context, true); |
Emanuele Giuseppe Esposito | a7b4f8f | 2021-06-14 10:11:29 +0200 | [diff] [blame] | 913 | |
| 914 | progress_get_snapshot(&job->job.progress, &progress_current, |
| 915 | &progress_total); |
| 916 | if (progress_total) { |
| 917 | progress = (float)progress_current / progress_total * 100.f; |
Kevin Wolf | 30a5c88 | 2018-05-04 12:17:20 +0200 | [diff] [blame] | 918 | } |
| 919 | qemu_progress_print(progress, 0); |
Kevin Wolf | df956ae | 2018-04-25 15:09:58 +0200 | [diff] [blame] | 920 | } while (!job_is_ready(&job->job) && !job_is_completed(&job->job)); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 921 | |
Kevin Wolf | dbe5e6c | 2018-04-19 13:04:01 +0200 | [diff] [blame] | 922 | if (!job_is_completed(&job->job)) { |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 923 | ret = job_complete_sync(&job->job, errp); |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 924 | } else { |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 925 | ret = job->job.ret; |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 926 | } |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 927 | job_unref(&job->job); |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 928 | aio_context_release(aio_context); |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 929 | |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 930 | /* publish completion progress only when success */ |
| 931 | if (!ret) { |
| 932 | qemu_progress_print(100.f, 0); |
| 933 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 934 | } |
| 935 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 936 | static int img_commit(int argc, char **argv) |
| 937 | { |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 938 | int c, ret, flags; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 939 | const char *filename, *fmt, *cache, *base; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 940 | BlockBackend *blk; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 941 | BlockDriverState *bs, *base_bs; |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 942 | BlockJob *job; |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 943 | bool progress = false, quiet = false, drop = false; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 944 | bool writethrough; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 945 | Error *local_err = NULL; |
| 946 | CommonBlockJobCBInfo cbi; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 947 | bool image_opts = false; |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 948 | AioContext *aio_context; |
Zhengui | a0441b6 | 2020-10-20 14:47:43 +0000 | [diff] [blame] | 949 | int64_t rate_limit = 0; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 950 | |
| 951 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 952 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 953 | base = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 954 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 955 | static const struct option long_options[] = { |
| 956 | {"help", no_argument, 0, 'h'}, |
| 957 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 958 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 959 | {0, 0, 0, 0} |
| 960 | }; |
Zhengui | a0441b6 | 2020-10-20 14:47:43 +0000 | [diff] [blame] | 961 | c = getopt_long(argc, argv, ":f:ht:b:dpqr:", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 962 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 963 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 964 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 965 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 966 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 967 | case ':': |
| 968 | missing_argument(argv[optind - 1]); |
| 969 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 970 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 971 | unrecognized_option(argv[optind - 1]); |
| 972 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 973 | case 'h': |
| 974 | help(); |
| 975 | break; |
| 976 | case 'f': |
| 977 | fmt = optarg; |
| 978 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 979 | case 't': |
| 980 | cache = optarg; |
| 981 | break; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 982 | case 'b': |
| 983 | base = optarg; |
| 984 | /* -b implies -d */ |
| 985 | drop = true; |
| 986 | break; |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 987 | case 'd': |
| 988 | drop = true; |
| 989 | break; |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 990 | case 'p': |
| 991 | progress = true; |
| 992 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 993 | case 'q': |
| 994 | quiet = true; |
| 995 | break; |
Zhengui | a0441b6 | 2020-10-20 14:47:43 +0000 | [diff] [blame] | 996 | case 'r': |
| 997 | rate_limit = cvtnum("rate limit", optarg); |
| 998 | if (rate_limit < 0) { |
| 999 | return 1; |
| 1000 | } |
| 1001 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 1002 | case OPTION_OBJECT: |
| 1003 | user_creatable_process_cmdline(optarg); |
| 1004 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1005 | case OPTION_IMAGE_OPTS: |
| 1006 | image_opts = true; |
| 1007 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1008 | } |
| 1009 | } |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 1010 | |
| 1011 | /* Progress is not shown in Quiet mode */ |
| 1012 | if (quiet) { |
| 1013 | progress = false; |
| 1014 | } |
| 1015 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 1016 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 1017 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1018 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1019 | filename = argv[optind++]; |
| 1020 | |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1021 | flags = BDRV_O_RDWR | BDRV_O_UNMAP; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1022 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1023 | if (ret < 0) { |
| 1024 | error_report("Invalid cache option: %s", cache); |
Stefan Hajnoczi | a3981eb | 2014-08-26 19:17:54 +0100 | [diff] [blame] | 1025 | return 1; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1026 | } |
| 1027 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1028 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 1029 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1030 | if (!blk) { |
| 1031 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1032 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1033 | bs = blk_bs(blk); |
| 1034 | |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 1035 | qemu_progress_init(progress, 1.f); |
| 1036 | qemu_progress_print(0.f, 100); |
| 1037 | |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1038 | if (base) { |
| 1039 | base_bs = bdrv_find_backing_image(bs, base); |
| 1040 | if (!base_bs) { |
Max Reitz | 6b33f3a | 2016-12-01 03:05:08 +0100 | [diff] [blame] | 1041 | error_setg(&local_err, |
| 1042 | "Did not find '%s' in the backing chain of '%s'", |
| 1043 | base, filename); |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1044 | goto done; |
| 1045 | } |
| 1046 | } else { |
| 1047 | /* This is different from QMP, which by default uses the deepest file in |
| 1048 | * the backing chain (i.e., the very base); however, the traditional |
| 1049 | * behavior of qemu-img commit is using the immediate backing file. */ |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 1050 | base_bs = bdrv_backing_chain_next(bs); |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1051 | if (!base_bs) { |
| 1052 | error_setg(&local_err, "Image does not have a backing file"); |
| 1053 | goto done; |
| 1054 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1057 | cbi = (CommonBlockJobCBInfo){ |
| 1058 | .errp = &local_err, |
| 1059 | .bs = bs, |
| 1060 | }; |
| 1061 | |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 1062 | aio_context = bdrv_get_aio_context(bs); |
| 1063 | aio_context_acquire(aio_context); |
Zhengui | a0441b6 | 2020-10-20 14:47:43 +0000 | [diff] [blame] | 1064 | commit_active_start("commit", bs, base_bs, JOB_DEFAULT, rate_limit, |
Kevin Wolf | 0db832f | 2017-02-20 18:10:05 +0100 | [diff] [blame] | 1065 | BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb, |
Fam Zheng | 78bbd91 | 2017-04-21 20:27:04 +0800 | [diff] [blame] | 1066 | &cbi, false, &local_err); |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 1067 | aio_context_release(aio_context); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1068 | if (local_err) { |
| 1069 | goto done; |
| 1070 | } |
| 1071 | |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1072 | /* When the block job completes, the BlockBackend reference will point to |
| 1073 | * the old backing file. In order to avoid that the top image is already |
| 1074 | * deleted, so we can still empty it afterwards, increment the reference |
| 1075 | * counter here preemptively. */ |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1076 | if (!drop) { |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1077 | bdrv_ref(bs); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1078 | } |
| 1079 | |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 1080 | job = block_job_get("commit"); |
Liam Merwick | 2e2db26 | 2018-11-05 21:38:37 +0000 | [diff] [blame] | 1081 | assert(job); |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 1082 | run_block_job(job, &local_err); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1083 | if (local_err) { |
| 1084 | goto unref_backing; |
| 1085 | } |
| 1086 | |
Max Reitz | 2d97fde | 2020-04-29 16:11:26 +0200 | [diff] [blame] | 1087 | if (!drop) { |
| 1088 | BlockBackend *old_backing_blk; |
| 1089 | |
| 1090 | old_backing_blk = blk_new_with_bs(bs, BLK_PERM_WRITE, BLK_PERM_ALL, |
| 1091 | &local_err); |
| 1092 | if (!old_backing_blk) { |
| 1093 | goto unref_backing; |
| 1094 | } |
| 1095 | ret = blk_make_empty(old_backing_blk, &local_err); |
| 1096 | blk_unref(old_backing_blk); |
| 1097 | if (ret == -ENOTSUP) { |
| 1098 | error_free(local_err); |
| 1099 | local_err = NULL; |
| 1100 | } else if (ret < 0) { |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1101 | goto unref_backing; |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | unref_backing: |
| 1106 | if (!drop) { |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1107 | bdrv_unref(bs); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1108 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1109 | |
| 1110 | done: |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 1111 | qemu_progress_end(); |
| 1112 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1113 | blk_unref(blk); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1114 | |
| 1115 | if (local_err) { |
Markus Armbruster | 6936f29 | 2015-02-10 15:14:02 +0100 | [diff] [blame] | 1116 | error_report_err(local_err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1117 | return 1; |
| 1118 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1119 | |
| 1120 | qprintf(quiet, "Image committed.\n"); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1121 | return 0; |
| 1122 | } |
| 1123 | |
Dmitry Konishchev | f6a00aa | 2011-05-18 15:03:59 +0400 | [diff] [blame] | 1124 | /* |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1125 | * Returns -1 if 'buf' contains only zeroes, otherwise the byte index |
| 1126 | * of the first sector boundary within buf where the sector contains a |
| 1127 | * non-zero byte. This function is robust to a buffer that is not |
| 1128 | * sector-aligned. |
| 1129 | */ |
| 1130 | static int64_t find_nonzero(const uint8_t *buf, int64_t n) |
| 1131 | { |
| 1132 | int64_t i; |
| 1133 | int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE); |
| 1134 | |
| 1135 | for (i = 0; i < end; i += BDRV_SECTOR_SIZE) { |
| 1136 | if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) { |
| 1137 | return i; |
| 1138 | } |
| 1139 | } |
| 1140 | if (i < n && !buffer_is_zero(buf + i, n - end)) { |
| 1141 | return i; |
| 1142 | } |
| 1143 | return -1; |
| 1144 | } |
| 1145 | |
| 1146 | /* |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1147 | * Returns true iff the first sector pointed to by 'buf' contains at least |
| 1148 | * a non-NUL byte. |
| 1149 | * |
| 1150 | * 'pnum' is set to the number of sectors (including and immediately following |
| 1151 | * 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] | 1152 | * The function will try to align the end offset to alignment boundaries so |
zhaolichang | e3a6e0d | 2020-09-17 15:50:20 +0800 | [diff] [blame] | 1153 | * that the request will at least end aligned and consecutive requests will |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1154 | * also start at an aligned offset. |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1155 | */ |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1156 | static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum, |
| 1157 | int64_t sector_num, int alignment) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1158 | { |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1159 | bool is_zero; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1160 | int i, tail; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1161 | |
| 1162 | if (n <= 0) { |
| 1163 | *pnum = 0; |
| 1164 | return 0; |
| 1165 | } |
Yi Li | c075c42 | 2020-08-19 09:36:07 +0800 | [diff] [blame] | 1166 | is_zero = buffer_is_zero(buf, BDRV_SECTOR_SIZE); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1167 | for(i = 1; i < n; i++) { |
Yi Li | c075c42 | 2020-08-19 09:36:07 +0800 | [diff] [blame] | 1168 | buf += BDRV_SECTOR_SIZE; |
| 1169 | if (is_zero != buffer_is_zero(buf, BDRV_SECTOR_SIZE)) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1170 | break; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1171 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1172 | } |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1173 | |
| 1174 | tail = (sector_num + i) & (alignment - 1); |
| 1175 | if (tail) { |
| 1176 | if (is_zero && i <= tail) { |
| 1177 | /* treat unallocated areas which only consist |
| 1178 | * of a small tail as allocated. */ |
| 1179 | is_zero = false; |
| 1180 | } |
| 1181 | if (!is_zero) { |
| 1182 | /* align up end offset of allocated areas. */ |
| 1183 | i += alignment - tail; |
| 1184 | i = MIN(i, n); |
| 1185 | } else { |
| 1186 | /* align down end offset of zero areas. */ |
| 1187 | i -= tail; |
| 1188 | } |
| 1189 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1190 | *pnum = i; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1191 | return !is_zero; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1194 | /* |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1195 | * Like is_allocated_sectors, but if the buffer starts with a used sector, |
| 1196 | * up to 'min' consecutive sectors containing zeros are ignored. This avoids |
| 1197 | * breaking up write requests for only small sparse areas. |
| 1198 | */ |
| 1199 | 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] | 1200 | int min, int64_t sector_num, int alignment) |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1201 | { |
| 1202 | int ret; |
| 1203 | int num_checked, num_used; |
| 1204 | |
| 1205 | if (n < min) { |
| 1206 | min = n; |
| 1207 | } |
| 1208 | |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1209 | ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment); |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1210 | if (!ret) { |
| 1211 | return ret; |
| 1212 | } |
| 1213 | |
| 1214 | num_used = *pnum; |
| 1215 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 1216 | n -= *pnum; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1217 | sector_num += *pnum; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1218 | num_checked = num_used; |
| 1219 | |
| 1220 | while (n > 0) { |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1221 | ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment); |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1222 | |
| 1223 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 1224 | n -= *pnum; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1225 | sector_num += *pnum; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1226 | num_checked += *pnum; |
| 1227 | if (ret) { |
| 1228 | num_used = num_checked; |
| 1229 | } else if (*pnum >= min) { |
| 1230 | break; |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | *pnum = num_used; |
| 1235 | return 1; |
| 1236 | } |
| 1237 | |
| 1238 | /* |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1239 | * Compares two buffers sector by sector. Returns 0 if the first |
| 1240 | * sector of each buffer matches, non-zero otherwise. |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1241 | * |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1242 | * pnum is set to the sector-aligned size of the buffer prefix that |
| 1243 | * has the same matching status as the first sector. |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1244 | */ |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1245 | static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2, |
| 1246 | int64_t bytes, int64_t *pnum) |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1247 | { |
Radim Krčmář | 8c1ac47 | 2015-02-20 17:06:15 +0100 | [diff] [blame] | 1248 | bool res; |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1249 | int64_t i = MIN(bytes, BDRV_SECTOR_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1250 | |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1251 | assert(bytes > 0); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1252 | |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1253 | res = !!memcmp(buf1, buf2, i); |
| 1254 | while (i < bytes) { |
| 1255 | int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1256 | |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1257 | if (!!memcmp(buf1 + i, buf2 + i, len) != res) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1258 | break; |
| 1259 | } |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1260 | i += len; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | *pnum = i; |
| 1264 | return res; |
| 1265 | } |
| 1266 | |
Stefano Garzarella | 97ede57 | 2019-05-08 12:43:24 +0200 | [diff] [blame] | 1267 | #define IO_BUF_SIZE (2 * MiB) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1268 | |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1269 | /* |
| 1270 | * Check if passed sectors are empty (not allocated or contain only 0 bytes) |
| 1271 | * |
Eric Blake | 0608e40 | 2017-10-11 22:47:12 -0500 | [diff] [blame] | 1272 | * Intended for use by 'qemu-img compare': Returns 0 in case sectors are |
| 1273 | * filled with 0, 1 if sectors contain non-zero data (this is a comparison |
| 1274 | * failure), and 4 on error (the exit status for read errors), after emitting |
| 1275 | * an error message. |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1276 | * |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1277 | * @param blk: BlockBackend for the image |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1278 | * @param offset: Starting offset to check |
| 1279 | * @param bytes: Number of bytes to check |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1280 | * @param filename: Name of disk file we are checking (logging purpose) |
| 1281 | * @param buffer: Allocated buffer for storing read data |
| 1282 | * @param quiet: Flag for quiet mode |
| 1283 | */ |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1284 | static int check_empty_sectors(BlockBackend *blk, int64_t offset, |
| 1285 | int64_t bytes, const char *filename, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1286 | uint8_t *buffer, bool quiet) |
| 1287 | { |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1288 | int ret = 0; |
| 1289 | int64_t idx; |
| 1290 | |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1291 | ret = blk_pread(blk, offset, buffer, bytes); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1292 | if (ret < 0) { |
| 1293 | error_report("Error while reading offset %" PRId64 " of %s: %s", |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1294 | offset, filename, strerror(-ret)); |
Eric Blake | 0608e40 | 2017-10-11 22:47:12 -0500 | [diff] [blame] | 1295 | return 4; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1296 | } |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1297 | idx = find_nonzero(buffer, bytes); |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1298 | if (idx >= 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1299 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1300 | offset + idx); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1301 | return 1; |
| 1302 | } |
| 1303 | |
| 1304 | return 0; |
| 1305 | } |
| 1306 | |
| 1307 | /* |
| 1308 | * Compares two images. Exit codes: |
| 1309 | * |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 1310 | * 0 - Images are identical or the requested help was printed |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1311 | * 1 - Images differ |
| 1312 | * >1 - Error occurred |
| 1313 | */ |
| 1314 | static int img_compare(int argc, char **argv) |
| 1315 | { |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1316 | const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1317 | BlockBackend *blk1, *blk2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1318 | BlockDriverState *bs1, *bs2; |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1319 | int64_t total_size1, total_size2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1320 | uint8_t *buf1 = NULL, *buf2 = NULL; |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1321 | int64_t pnum1, pnum2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1322 | int allocated1, allocated2; |
| 1323 | int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */ |
| 1324 | bool progress = false, quiet = false, strict = false; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1325 | int flags; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1326 | bool writethrough; |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1327 | int64_t total_size; |
| 1328 | int64_t offset = 0; |
| 1329 | int64_t chunk; |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1330 | int c; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1331 | uint64_t progress_base; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1332 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1333 | bool force_share = false; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1334 | |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1335 | cache = BDRV_DEFAULT_CACHE; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1336 | for (;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1337 | static const struct option long_options[] = { |
| 1338 | {"help", no_argument, 0, 'h'}, |
| 1339 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1340 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1341 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1342 | {0, 0, 0, 0} |
| 1343 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1344 | c = getopt_long(argc, argv, ":hf:F:T:pqsU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1345 | long_options, NULL); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1346 | if (c == -1) { |
| 1347 | break; |
| 1348 | } |
| 1349 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 1350 | case ':': |
| 1351 | missing_argument(argv[optind - 1]); |
| 1352 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1353 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 1354 | unrecognized_option(argv[optind - 1]); |
| 1355 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1356 | case 'h': |
| 1357 | help(); |
| 1358 | break; |
| 1359 | case 'f': |
| 1360 | fmt1 = optarg; |
| 1361 | break; |
| 1362 | case 'F': |
| 1363 | fmt2 = optarg; |
| 1364 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1365 | case 'T': |
| 1366 | cache = optarg; |
| 1367 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1368 | case 'p': |
| 1369 | progress = true; |
| 1370 | break; |
| 1371 | case 'q': |
| 1372 | quiet = true; |
| 1373 | break; |
| 1374 | case 's': |
| 1375 | strict = true; |
| 1376 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1377 | case 'U': |
| 1378 | force_share = true; |
| 1379 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 1380 | case OPTION_OBJECT: |
| 1381 | { |
| 1382 | Error *local_err = NULL; |
| 1383 | |
| 1384 | if (!user_creatable_add_from_str(optarg, &local_err)) { |
| 1385 | if (local_err) { |
| 1386 | error_report_err(local_err); |
| 1387 | exit(2); |
| 1388 | } else { |
| 1389 | /* Help was printed */ |
| 1390 | exit(EXIT_SUCCESS); |
| 1391 | } |
| 1392 | } |
| 1393 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1394 | } |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1395 | case OPTION_IMAGE_OPTS: |
| 1396 | image_opts = true; |
| 1397 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | /* Progress is not shown in Quiet mode */ |
| 1402 | if (quiet) { |
| 1403 | progress = false; |
| 1404 | } |
| 1405 | |
| 1406 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 1407 | if (optind != argc - 2) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 1408 | error_exit("Expecting two image file names"); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1409 | } |
| 1410 | filename1 = argv[optind++]; |
| 1411 | filename2 = argv[optind++]; |
| 1412 | |
Stefan Hajnoczi | cbda016 | 2014-08-26 19:17:55 +0100 | [diff] [blame] | 1413 | /* Initialize before goto out */ |
| 1414 | qemu_progress_init(progress, 2.0); |
| 1415 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1416 | flags = 0; |
| 1417 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1418 | if (ret < 0) { |
| 1419 | error_report("Invalid source cache option: %s", cache); |
| 1420 | ret = 2; |
| 1421 | goto out3; |
| 1422 | } |
| 1423 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1424 | blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet, |
| 1425 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1426 | if (!blk1) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1427 | ret = 2; |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1428 | goto out3; |
| 1429 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1430 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1431 | blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet, |
| 1432 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1433 | if (!blk2) { |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1434 | ret = 2; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1435 | goto out2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1436 | } |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1437 | bs1 = blk_bs(blk1); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1438 | bs2 = blk_bs(blk2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1439 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1440 | buf1 = blk_blockalign(blk1, IO_BUF_SIZE); |
| 1441 | buf2 = blk_blockalign(blk2, IO_BUF_SIZE); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1442 | total_size1 = blk_getlength(blk1); |
| 1443 | if (total_size1 < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1444 | error_report("Can't get size of %s: %s", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1445 | filename1, strerror(-total_size1)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1446 | ret = 4; |
| 1447 | goto out; |
| 1448 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1449 | total_size2 = blk_getlength(blk2); |
| 1450 | if (total_size2 < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1451 | error_report("Can't get size of %s: %s", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1452 | filename2, strerror(-total_size2)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1453 | ret = 4; |
| 1454 | goto out; |
| 1455 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1456 | total_size = MIN(total_size1, total_size2); |
| 1457 | progress_base = MAX(total_size1, total_size2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1458 | |
| 1459 | qemu_progress_print(0, 100); |
| 1460 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1461 | if (strict && total_size1 != total_size2) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1462 | ret = 1; |
| 1463 | qprintf(quiet, "Strict mode: Image size mismatch!\n"); |
| 1464 | goto out; |
| 1465 | } |
| 1466 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1467 | while (offset < total_size) { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1468 | int status1, status2; |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 1469 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1470 | status1 = bdrv_block_status_above(bs1, NULL, offset, |
| 1471 | total_size1 - offset, &pnum1, NULL, |
| 1472 | NULL); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1473 | if (status1 < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1474 | ret = 3; |
| 1475 | error_report("Sector allocation test failed for %s", filename1); |
| 1476 | goto out; |
| 1477 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1478 | allocated1 = status1 & BDRV_BLOCK_ALLOCATED; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1479 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1480 | status2 = bdrv_block_status_above(bs2, NULL, offset, |
| 1481 | total_size2 - offset, &pnum2, NULL, |
| 1482 | NULL); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1483 | if (status2 < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1484 | ret = 3; |
| 1485 | error_report("Sector allocation test failed for %s", filename2); |
| 1486 | goto out; |
| 1487 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1488 | allocated2 = status2 & BDRV_BLOCK_ALLOCATED; |
Eric Blake | 7daddc6 | 2017-10-11 22:47:09 -0500 | [diff] [blame] | 1489 | |
| 1490 | assert(pnum1 && pnum2); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1491 | chunk = MIN(pnum1, pnum2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1492 | |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1493 | if (strict) { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1494 | if (status1 != status2) { |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1495 | ret = 1; |
| 1496 | qprintf(quiet, "Strict mode: Offset %" PRId64 |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1497 | " block status mismatch!\n", offset); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1498 | goto out; |
| 1499 | } |
| 1500 | } |
| 1501 | if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) { |
Eric Blake | 7daddc6 | 2017-10-11 22:47:09 -0500 | [diff] [blame] | 1502 | /* nothing to do */ |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1503 | } else if (allocated1 == allocated2) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1504 | if (allocated1) { |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1505 | int64_t pnum; |
| 1506 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1507 | chunk = MIN(chunk, IO_BUF_SIZE); |
| 1508 | ret = blk_pread(blk1, offset, buf1, chunk); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1509 | if (ret < 0) { |
| 1510 | error_report("Error while reading offset %" PRId64 |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1511 | " of %s: %s", |
| 1512 | offset, filename1, strerror(-ret)); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1513 | ret = 4; |
| 1514 | goto out; |
| 1515 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1516 | ret = blk_pread(blk2, offset, buf2, chunk); |
| 1517 | if (ret < 0) { |
| 1518 | error_report("Error while reading offset %" PRId64 |
| 1519 | " of %s: %s", |
| 1520 | offset, filename2, strerror(-ret)); |
| 1521 | ret = 4; |
| 1522 | goto out; |
| 1523 | } |
| 1524 | ret = compare_buffers(buf1, buf2, chunk, &pnum); |
| 1525 | if (ret || pnum != chunk) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1526 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1527 | offset + (ret ? 0 : pnum)); |
Fam Zheng | 36452f1 | 2013-11-13 20:26:49 +0800 | [diff] [blame] | 1528 | ret = 1; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1529 | goto out; |
| 1530 | } |
| 1531 | } |
| 1532 | } else { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1533 | chunk = MIN(chunk, IO_BUF_SIZE); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1534 | if (allocated1) { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1535 | ret = check_empty_sectors(blk1, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1536 | filename1, buf1, quiet); |
| 1537 | } else { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1538 | ret = check_empty_sectors(blk2, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1539 | filename2, buf1, quiet); |
| 1540 | } |
| 1541 | if (ret) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1542 | goto out; |
| 1543 | } |
| 1544 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1545 | offset += chunk; |
| 1546 | qemu_progress_print(((float) chunk / progress_base) * 100, 100); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1547 | } |
| 1548 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1549 | if (total_size1 != total_size2) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1550 | BlockBackend *blk_over; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1551 | const char *filename_over; |
| 1552 | |
| 1553 | qprintf(quiet, "Warning: Image size mismatch!\n"); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1554 | if (total_size1 > total_size2) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1555 | blk_over = blk1; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1556 | filename_over = filename1; |
| 1557 | } else { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1558 | blk_over = blk2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1559 | filename_over = filename2; |
| 1560 | } |
| 1561 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1562 | while (offset < progress_base) { |
| 1563 | ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset, |
| 1564 | progress_base - offset, &chunk, |
| 1565 | NULL, NULL); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1566 | if (ret < 0) { |
| 1567 | ret = 3; |
| 1568 | error_report("Sector allocation test failed for %s", |
| 1569 | filename_over); |
| 1570 | goto out; |
| 1571 | |
| 1572 | } |
Eric Blake | 391cb1a | 2017-10-11 22:47:10 -0500 | [diff] [blame] | 1573 | if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1574 | chunk = MIN(chunk, IO_BUF_SIZE); |
| 1575 | ret = check_empty_sectors(blk_over, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1576 | filename_over, buf1, quiet); |
| 1577 | if (ret) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1578 | goto out; |
| 1579 | } |
| 1580 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1581 | offset += chunk; |
| 1582 | qemu_progress_print(((float) chunk / progress_base) * 100, 100); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1583 | } |
| 1584 | } |
| 1585 | |
| 1586 | qprintf(quiet, "Images are identical.\n"); |
| 1587 | ret = 0; |
| 1588 | |
| 1589 | out: |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1590 | qemu_vfree(buf1); |
| 1591 | qemu_vfree(buf2); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1592 | blk_unref(blk2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1593 | out2: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1594 | blk_unref(blk1); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1595 | out3: |
| 1596 | qemu_progress_end(); |
| 1597 | return ret; |
| 1598 | } |
| 1599 | |
Eric Blake | 6c729dd | 2020-05-21 14:21:35 -0500 | [diff] [blame] | 1600 | /* Convenience wrapper around qmp_block_dirty_bitmap_merge */ |
| 1601 | static void do_dirty_bitmap_merge(const char *dst_node, const char *dst_name, |
| 1602 | const char *src_node, const char *src_name, |
| 1603 | Error **errp) |
| 1604 | { |
| 1605 | BlockDirtyBitmapMergeSource *merge_src; |
Eric Blake | 54aa3de | 2020-11-12 19:13:37 -0600 | [diff] [blame] | 1606 | BlockDirtyBitmapMergeSourceList *list = NULL; |
Eric Blake | 6c729dd | 2020-05-21 14:21:35 -0500 | [diff] [blame] | 1607 | |
| 1608 | merge_src = g_new0(BlockDirtyBitmapMergeSource, 1); |
| 1609 | merge_src->type = QTYPE_QDICT; |
| 1610 | merge_src->u.external.node = g_strdup(src_node); |
| 1611 | merge_src->u.external.name = g_strdup(src_name); |
Eric Blake | 54aa3de | 2020-11-12 19:13:37 -0600 | [diff] [blame] | 1612 | QAPI_LIST_PREPEND(list, merge_src); |
Eric Blake | 6c729dd | 2020-05-21 14:21:35 -0500 | [diff] [blame] | 1613 | qmp_block_dirty_bitmap_merge(dst_node, dst_name, list, errp); |
| 1614 | qapi_free_BlockDirtyBitmapMergeSourceList(list); |
| 1615 | } |
| 1616 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1617 | enum ImgConvertBlockStatus { |
| 1618 | BLK_DATA, |
| 1619 | BLK_ZERO, |
| 1620 | BLK_BACKING_FILE, |
| 1621 | }; |
| 1622 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1623 | #define MAX_COROUTINES 16 |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 1624 | #define CONVERT_THROTTLE_GROUP "img_convert" |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1625 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1626 | typedef struct ImgConvertState { |
| 1627 | BlockBackend **src; |
| 1628 | int64_t *src_sectors; |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 1629 | int *src_alignment; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1630 | int src_num; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1631 | int64_t total_sectors; |
| 1632 | int64_t allocated_sectors; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1633 | int64_t allocated_done; |
| 1634 | int64_t sector_num; |
| 1635 | int64_t wr_offs; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1636 | enum ImgConvertBlockStatus status; |
| 1637 | int64_t sector_next_status; |
| 1638 | BlockBackend *target; |
| 1639 | bool has_zero_init; |
| 1640 | bool compressed; |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 1641 | bool target_is_new; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1642 | bool target_has_backing; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1643 | int64_t target_backing_sectors; /* negative if unknown */ |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1644 | bool wr_in_order; |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1645 | bool copy_range; |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1646 | bool salvage; |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 1647 | bool quiet; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1648 | int min_sparse; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1649 | int alignment; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1650 | size_t cluster_sectors; |
| 1651 | size_t buf_sectors; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 1652 | long num_coroutines; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1653 | int running_coroutines; |
| 1654 | Coroutine *co[MAX_COROUTINES]; |
| 1655 | int64_t wait_sector_num[MAX_COROUTINES]; |
| 1656 | CoMutex lock; |
| 1657 | int ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1658 | } ImgConvertState; |
| 1659 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1660 | static void convert_select_part(ImgConvertState *s, int64_t sector_num, |
| 1661 | int *src_cur, int64_t *src_cur_offset) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1662 | { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1663 | *src_cur = 0; |
| 1664 | *src_cur_offset = 0; |
| 1665 | while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) { |
| 1666 | *src_cur_offset += s->src_sectors[*src_cur]; |
| 1667 | (*src_cur)++; |
| 1668 | assert(*src_cur < s->src_num); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num) |
| 1673 | { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1674 | int64_t src_cur_offset; |
| 1675 | int ret, n, src_cur; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1676 | bool post_backing_zero = false; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1677 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1678 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1679 | |
| 1680 | assert(s->total_sectors > sector_num); |
| 1681 | n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS); |
| 1682 | |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1683 | if (s->target_backing_sectors >= 0) { |
| 1684 | if (sector_num >= s->target_backing_sectors) { |
Vladimir Sementsov-Ogievskiy | 2253d86 | 2020-05-28 12:43:56 +0300 | [diff] [blame] | 1685 | post_backing_zero = true; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1686 | } else if (sector_num + n > s->target_backing_sectors) { |
| 1687 | /* Split requests around target_backing_sectors (because |
| 1688 | * starting from there, zeros are handled differently) */ |
| 1689 | n = s->target_backing_sectors - sector_num; |
| 1690 | } |
| 1691 | } |
| 1692 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1693 | if (s->sector_next_status <= sector_num) { |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1694 | uint64_t offset = (sector_num - src_cur_offset) * BDRV_SECTOR_SIZE; |
| 1695 | int64_t count; |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 1696 | int tail; |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 1697 | BlockDriverState *src_bs = blk_bs(s->src[src_cur]); |
| 1698 | BlockDriverState *base; |
| 1699 | |
| 1700 | if (s->target_has_backing) { |
| 1701 | base = bdrv_cow_bs(bdrv_skip_filters(src_bs)); |
| 1702 | } else { |
| 1703 | base = NULL; |
| 1704 | } |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1705 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1706 | do { |
| 1707 | count = n * BDRV_SECTOR_SIZE; |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 1708 | |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 1709 | ret = bdrv_block_status_above(src_bs, base, offset, count, &count, |
| 1710 | NULL, NULL); |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1711 | |
| 1712 | if (ret < 0) { |
| 1713 | if (s->salvage) { |
| 1714 | if (n == 1) { |
| 1715 | if (!s->quiet) { |
| 1716 | warn_report("error while reading block status at " |
| 1717 | "offset %" PRIu64 ": %s", offset, |
| 1718 | strerror(-ret)); |
| 1719 | } |
| 1720 | /* Just try to read the data, then */ |
| 1721 | ret = BDRV_BLOCK_DATA; |
| 1722 | count = BDRV_SECTOR_SIZE; |
| 1723 | } else { |
| 1724 | /* Retry on a shorter range */ |
| 1725 | n = DIV_ROUND_UP(n, 4); |
| 1726 | } |
| 1727 | } else { |
| 1728 | error_report("error while reading block status at offset " |
| 1729 | "%" PRIu64 ": %s", offset, strerror(-ret)); |
| 1730 | return ret; |
| 1731 | } |
| 1732 | } |
| 1733 | } while (ret < 0); |
| 1734 | |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1735 | n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1736 | |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 1737 | /* |
| 1738 | * Avoid that s->sector_next_status becomes unaligned to the source |
| 1739 | * request alignment and/or cluster size to avoid unnecessary read |
| 1740 | * cycles. |
| 1741 | */ |
| 1742 | tail = (sector_num - src_cur_offset + n) % s->src_alignment[src_cur]; |
| 1743 | if (n > tail) { |
| 1744 | n -= tail; |
| 1745 | } |
| 1746 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1747 | if (ret & BDRV_BLOCK_ZERO) { |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1748 | s->status = post_backing_zero ? BLK_BACKING_FILE : BLK_ZERO; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1749 | } else if (ret & BDRV_BLOCK_DATA) { |
| 1750 | s->status = BLK_DATA; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1751 | } else { |
Vladimir Sementsov-Ogievskiy | 9f1b92a | 2017-04-07 14:34:04 +0300 | [diff] [blame] | 1752 | s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | s->sector_next_status = sector_num + n; |
| 1756 | } |
| 1757 | |
| 1758 | n = MIN(n, s->sector_next_status - sector_num); |
| 1759 | if (s->status == BLK_DATA) { |
| 1760 | n = MIN(n, s->buf_sectors); |
| 1761 | } |
| 1762 | |
| 1763 | /* We need to write complete clusters for compressed images, so if an |
| 1764 | * unallocated area is shorter than that, we must consider the whole |
| 1765 | * cluster allocated. */ |
| 1766 | if (s->compressed) { |
| 1767 | if (n < s->cluster_sectors) { |
| 1768 | n = MIN(s->cluster_sectors, s->total_sectors - sector_num); |
| 1769 | s->status = BLK_DATA; |
| 1770 | } else { |
| 1771 | n = QEMU_ALIGN_DOWN(n, s->cluster_sectors); |
| 1772 | } |
| 1773 | } |
| 1774 | |
| 1775 | return n; |
| 1776 | } |
| 1777 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1778 | static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num, |
| 1779 | int nb_sectors, uint8_t *buf) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1780 | { |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1781 | uint64_t single_read_until = 0; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1782 | int n, ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1783 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1784 | assert(nb_sectors <= s->buf_sectors); |
| 1785 | while (nb_sectors > 0) { |
| 1786 | BlockBackend *blk; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1787 | int src_cur; |
| 1788 | int64_t bs_sectors, src_cur_offset; |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1789 | uint64_t offset; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1790 | |
| 1791 | /* In the case of compression with multiple source files, we can get a |
| 1792 | * nb_sectors that spreads into the next part. So we must be able to |
| 1793 | * read across multiple BDSes for one convert_read() call. */ |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1794 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
| 1795 | blk = s->src[src_cur]; |
| 1796 | bs_sectors = s->src_sectors[src_cur]; |
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 | offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1799 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1800 | n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset)); |
| 1801 | if (single_read_until > offset) { |
| 1802 | n = 1; |
| 1803 | } |
| 1804 | |
| 1805 | ret = blk_co_pread(blk, offset, n << BDRV_SECTOR_BITS, buf, 0); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1806 | if (ret < 0) { |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1807 | if (s->salvage) { |
| 1808 | if (n > 1) { |
| 1809 | single_read_until = offset + (n << BDRV_SECTOR_BITS); |
| 1810 | continue; |
| 1811 | } else { |
| 1812 | if (!s->quiet) { |
| 1813 | warn_report("error while reading offset %" PRIu64 |
| 1814 | ": %s", offset, strerror(-ret)); |
| 1815 | } |
| 1816 | memset(buf, 0, BDRV_SECTOR_SIZE); |
| 1817 | } |
| 1818 | } else { |
| 1819 | return ret; |
| 1820 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1821 | } |
| 1822 | |
| 1823 | sector_num += n; |
| 1824 | nb_sectors -= n; |
| 1825 | buf += n * BDRV_SECTOR_SIZE; |
| 1826 | } |
| 1827 | |
| 1828 | return 0; |
| 1829 | } |
| 1830 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1831 | |
| 1832 | static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num, |
| 1833 | int nb_sectors, uint8_t *buf, |
| 1834 | enum ImgConvertBlockStatus status) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1835 | { |
| 1836 | int ret; |
| 1837 | |
| 1838 | while (nb_sectors > 0) { |
| 1839 | int n = nb_sectors; |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1840 | BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0; |
| 1841 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1842 | switch (status) { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1843 | case BLK_BACKING_FILE: |
| 1844 | /* If we have a backing file, leave clusters unallocated that are |
| 1845 | * unallocated in the source image, so that the backing file is |
| 1846 | * visible at the respective offset. */ |
| 1847 | assert(s->target_has_backing); |
| 1848 | break; |
| 1849 | |
| 1850 | case BLK_DATA: |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1851 | /* If we're told to keep the target fully allocated (-S 0) or there |
| 1852 | * is real non-zero data, we must write it. Otherwise we can treat |
| 1853 | * it as zero sectors. |
| 1854 | * Compressed clusters need to be written as a whole, so in that |
| 1855 | * case we can only save the write if the buffer is completely |
| 1856 | * zeroed. */ |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1857 | if (!s->min_sparse || |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1858 | (!s->compressed && |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1859 | is_allocated_sectors_min(buf, n, &n, s->min_sparse, |
| 1860 | sector_num, s->alignment)) || |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1861 | (s->compressed && |
| 1862 | !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1863 | { |
Vladimir Sementsov-Ogievskiy | 265a7e5 | 2019-04-22 17:58:38 +0300 | [diff] [blame] | 1864 | ret = blk_co_pwrite(s->target, sector_num << BDRV_SECTOR_BITS, |
| 1865 | n << BDRV_SECTOR_BITS, buf, flags); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1866 | if (ret < 0) { |
| 1867 | return ret; |
| 1868 | } |
| 1869 | break; |
| 1870 | } |
| 1871 | /* fall-through */ |
| 1872 | |
| 1873 | case BLK_ZERO: |
| 1874 | if (s->has_zero_init) { |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1875 | assert(!s->target_has_backing); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1876 | break; |
| 1877 | } |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1878 | ret = blk_co_pwrite_zeroes(s->target, |
| 1879 | sector_num << BDRV_SECTOR_BITS, |
Nir Soffer | a3d6ae2 | 2019-03-24 02:20:12 +0200 | [diff] [blame] | 1880 | n << BDRV_SECTOR_BITS, |
| 1881 | BDRV_REQ_MAY_UNMAP); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1882 | if (ret < 0) { |
| 1883 | return ret; |
| 1884 | } |
| 1885 | break; |
| 1886 | } |
| 1887 | |
| 1888 | sector_num += n; |
| 1889 | nb_sectors -= n; |
| 1890 | buf += n * BDRV_SECTOR_SIZE; |
| 1891 | } |
| 1892 | |
| 1893 | return 0; |
| 1894 | } |
| 1895 | |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1896 | static int coroutine_fn convert_co_copy_range(ImgConvertState *s, int64_t sector_num, |
| 1897 | int nb_sectors) |
| 1898 | { |
| 1899 | int n, ret; |
| 1900 | |
| 1901 | while (nb_sectors > 0) { |
| 1902 | BlockBackend *blk; |
| 1903 | int src_cur; |
| 1904 | int64_t bs_sectors, src_cur_offset; |
| 1905 | int64_t offset; |
| 1906 | |
| 1907 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
| 1908 | offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS; |
| 1909 | blk = s->src[src_cur]; |
| 1910 | bs_sectors = s->src_sectors[src_cur]; |
| 1911 | |
| 1912 | n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset)); |
| 1913 | |
| 1914 | ret = blk_co_copy_range(blk, offset, s->target, |
| 1915 | sector_num << BDRV_SECTOR_BITS, |
Vladimir Sementsov-Ogievskiy | 67b51fb | 2018-07-09 19:37:17 +0300 | [diff] [blame] | 1916 | n << BDRV_SECTOR_BITS, 0, 0); |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1917 | if (ret < 0) { |
| 1918 | return ret; |
| 1919 | } |
| 1920 | |
| 1921 | sector_num += n; |
| 1922 | nb_sectors -= n; |
| 1923 | } |
| 1924 | return 0; |
| 1925 | } |
| 1926 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1927 | static void coroutine_fn convert_co_do_copy(void *opaque) |
| 1928 | { |
| 1929 | ImgConvertState *s = opaque; |
| 1930 | uint8_t *buf = NULL; |
| 1931 | int ret, i; |
| 1932 | int index = -1; |
| 1933 | |
| 1934 | for (i = 0; i < s->num_coroutines; i++) { |
| 1935 | if (s->co[i] == qemu_coroutine_self()) { |
| 1936 | index = i; |
| 1937 | break; |
| 1938 | } |
| 1939 | } |
| 1940 | assert(index >= 0); |
| 1941 | |
| 1942 | s->running_coroutines++; |
| 1943 | buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE); |
| 1944 | |
| 1945 | while (1) { |
| 1946 | int n; |
| 1947 | int64_t sector_num; |
| 1948 | enum ImgConvertBlockStatus status; |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1949 | bool copy_range; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1950 | |
| 1951 | qemu_co_mutex_lock(&s->lock); |
| 1952 | if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) { |
| 1953 | qemu_co_mutex_unlock(&s->lock); |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1954 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1955 | } |
| 1956 | n = convert_iteration_sectors(s, s->sector_num); |
| 1957 | if (n < 0) { |
| 1958 | qemu_co_mutex_unlock(&s->lock); |
| 1959 | s->ret = n; |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1960 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1961 | } |
| 1962 | /* save current sector and allocation status to local variables */ |
| 1963 | sector_num = s->sector_num; |
| 1964 | status = s->status; |
| 1965 | if (!s->min_sparse && s->status == BLK_ZERO) { |
| 1966 | n = MIN(n, s->buf_sectors); |
| 1967 | } |
| 1968 | /* increment global sector counter so that other coroutines can |
| 1969 | * already continue reading beyond this request */ |
| 1970 | s->sector_num += n; |
| 1971 | qemu_co_mutex_unlock(&s->lock); |
| 1972 | |
| 1973 | if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) { |
| 1974 | s->allocated_done += n; |
| 1975 | qemu_progress_print(100.0 * s->allocated_done / |
| 1976 | s->allocated_sectors, 0); |
| 1977 | } |
| 1978 | |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1979 | retry: |
| 1980 | copy_range = s->copy_range && s->status == BLK_DATA; |
| 1981 | if (status == BLK_DATA && !copy_range) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1982 | ret = convert_co_read(s, sector_num, n, buf); |
| 1983 | if (ret < 0) { |
Eric Blake | 39f77cb | 2020-04-02 08:57:17 -0500 | [diff] [blame] | 1984 | error_report("error while reading at byte %lld: %s", |
| 1985 | sector_num * BDRV_SECTOR_SIZE, strerror(-ret)); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1986 | s->ret = ret; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1987 | } |
| 1988 | } else if (!s->min_sparse && status == BLK_ZERO) { |
| 1989 | status = BLK_DATA; |
| 1990 | memset(buf, 0x00, n * BDRV_SECTOR_SIZE); |
| 1991 | } |
| 1992 | |
| 1993 | if (s->wr_in_order) { |
| 1994 | /* keep writes in order */ |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1995 | while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1996 | s->wait_sector_num[index] = sector_num; |
| 1997 | qemu_coroutine_yield(); |
| 1998 | } |
| 1999 | s->wait_sector_num[index] = -1; |
| 2000 | } |
| 2001 | |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2002 | if (s->ret == -EINPROGRESS) { |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 2003 | if (copy_range) { |
| 2004 | ret = convert_co_copy_range(s, sector_num, n); |
| 2005 | if (ret) { |
| 2006 | s->copy_range = false; |
| 2007 | goto retry; |
| 2008 | } |
| 2009 | } else { |
| 2010 | ret = convert_co_write(s, sector_num, n, buf, status); |
| 2011 | } |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2012 | if (ret < 0) { |
Eric Blake | 39f77cb | 2020-04-02 08:57:17 -0500 | [diff] [blame] | 2013 | error_report("error while writing at byte %lld: %s", |
| 2014 | sector_num * BDRV_SECTOR_SIZE, strerror(-ret)); |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2015 | s->ret = ret; |
| 2016 | } |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2017 | } |
| 2018 | |
| 2019 | if (s->wr_in_order) { |
| 2020 | /* reenter the coroutine that might have waited |
| 2021 | * for this write to complete */ |
| 2022 | s->wr_offs = sector_num + n; |
| 2023 | for (i = 0; i < s->num_coroutines; i++) { |
| 2024 | if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) { |
| 2025 | /* |
| 2026 | * A -> B -> A cannot occur because A has |
| 2027 | * s->wait_sector_num[i] == -1 during A -> B. Therefore |
| 2028 | * B will never enter A during this time window. |
| 2029 | */ |
| 2030 | qemu_coroutine_enter(s->co[i]); |
| 2031 | break; |
| 2032 | } |
| 2033 | } |
| 2034 | } |
| 2035 | } |
| 2036 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2037 | qemu_vfree(buf); |
| 2038 | s->co[index] = NULL; |
| 2039 | s->running_coroutines--; |
| 2040 | if (!s->running_coroutines && s->ret == -EINPROGRESS) { |
| 2041 | /* the convert job finished successfully */ |
| 2042 | s->ret = 0; |
| 2043 | } |
| 2044 | } |
| 2045 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2046 | static int convert_do_copy(ImgConvertState *s) |
| 2047 | { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2048 | int ret, i, n; |
| 2049 | int64_t sector_num = 0; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2050 | |
| 2051 | /* Check whether we have zero initialisation or can get it efficiently */ |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2052 | if (!s->has_zero_init && s->target_is_new && s->min_sparse && |
| 2053 | !s->target_has_backing) { |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 2054 | s->has_zero_init = bdrv_has_zero_init(blk_bs(s->target)); |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 2055 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2056 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2057 | /* Allocate buffer for copied data. For compressed images, only one cluster |
| 2058 | * can be copied at a time. */ |
| 2059 | if (s->compressed) { |
| 2060 | if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) { |
| 2061 | error_report("invalid cluster size"); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2062 | return -EINVAL; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2063 | } |
| 2064 | s->buf_sectors = s->cluster_sectors; |
| 2065 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2066 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2067 | while (sector_num < s->total_sectors) { |
| 2068 | n = convert_iteration_sectors(s, sector_num); |
| 2069 | if (n < 0) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2070 | return n; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2071 | } |
Max Reitz | aad15de | 2016-03-24 23:33:57 +0100 | [diff] [blame] | 2072 | if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO)) |
| 2073 | { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2074 | s->allocated_sectors += n; |
| 2075 | } |
| 2076 | sector_num += n; |
| 2077 | } |
| 2078 | |
| 2079 | /* Do the copy */ |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2080 | s->sector_next_status = 0; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2081 | s->ret = -EINPROGRESS; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2082 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2083 | qemu_co_mutex_init(&s->lock); |
| 2084 | for (i = 0; i < s->num_coroutines; i++) { |
| 2085 | s->co[i] = qemu_coroutine_create(convert_co_do_copy, s); |
| 2086 | s->wait_sector_num[i] = -1; |
| 2087 | qemu_coroutine_enter(s->co[i]); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2088 | } |
| 2089 | |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2090 | while (s->running_coroutines) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2091 | main_loop_wait(false); |
| 2092 | } |
| 2093 | |
| 2094 | if (s->compressed && !s->ret) { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2095 | /* signal EOF to align */ |
Pavel Butsykin | fe5c135 | 2016-07-22 11:17:40 +0300 | [diff] [blame] | 2096 | ret = blk_pwrite_compressed(s->target, 0, NULL, 0); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2097 | if (ret < 0) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2098 | return ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2099 | } |
| 2100 | } |
| 2101 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2102 | return s->ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2103 | } |
| 2104 | |
Eric Blake | 74a4320 | 2021-07-09 10:39:50 -0500 | [diff] [blame] | 2105 | /* Check that bitmaps can be copied, or output an error */ |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2106 | static int convert_check_bitmaps(BlockDriverState *src, bool skip_broken) |
Eric Blake | 74a4320 | 2021-07-09 10:39:50 -0500 | [diff] [blame] | 2107 | { |
| 2108 | BdrvDirtyBitmap *bm; |
| 2109 | |
| 2110 | if (!bdrv_supports_persistent_dirty_bitmap(src)) { |
| 2111 | error_report("Source lacks bitmap support"); |
| 2112 | return -1; |
| 2113 | } |
| 2114 | FOR_EACH_DIRTY_BITMAP(src, bm) { |
| 2115 | if (!bdrv_dirty_bitmap_get_persistence(bm)) { |
| 2116 | continue; |
| 2117 | } |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2118 | if (!skip_broken && bdrv_dirty_bitmap_inconsistent(bm)) { |
Eric Blake | 74a4320 | 2021-07-09 10:39:50 -0500 | [diff] [blame] | 2119 | error_report("Cannot copy inconsistent bitmap '%s'", |
| 2120 | bdrv_dirty_bitmap_name(bm)); |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2121 | error_printf("Try --skip-broken-bitmaps, or " |
| 2122 | "use 'qemu-img bitmap --remove' to delete it\n"); |
Eric Blake | 74a4320 | 2021-07-09 10:39:50 -0500 | [diff] [blame] | 2123 | return -1; |
| 2124 | } |
| 2125 | } |
| 2126 | return 0; |
| 2127 | } |
| 2128 | |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2129 | static int convert_copy_bitmaps(BlockDriverState *src, BlockDriverState *dst, |
| 2130 | bool skip_broken) |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2131 | { |
| 2132 | BdrvDirtyBitmap *bm; |
| 2133 | Error *err = NULL; |
| 2134 | |
| 2135 | FOR_EACH_DIRTY_BITMAP(src, bm) { |
| 2136 | const char *name; |
| 2137 | |
| 2138 | if (!bdrv_dirty_bitmap_get_persistence(bm)) { |
| 2139 | continue; |
| 2140 | } |
| 2141 | name = bdrv_dirty_bitmap_name(bm); |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2142 | if (skip_broken && bdrv_dirty_bitmap_inconsistent(bm)) { |
| 2143 | warn_report("Skipping inconsistent bitmap '%s'", name); |
| 2144 | continue; |
| 2145 | } |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2146 | qmp_block_dirty_bitmap_add(dst->node_name, name, |
| 2147 | true, bdrv_dirty_bitmap_granularity(bm), |
| 2148 | true, true, |
| 2149 | true, !bdrv_dirty_bitmap_enabled(bm), |
| 2150 | &err); |
| 2151 | if (err) { |
| 2152 | error_reportf_err(err, "Failed to create bitmap %s: ", name); |
| 2153 | return -1; |
| 2154 | } |
| 2155 | |
| 2156 | do_dirty_bitmap_merge(dst->node_name, name, src->node_name, name, |
| 2157 | &err); |
| 2158 | if (err) { |
| 2159 | error_reportf_err(err, "Failed to populate bitmap %s: ", name); |
Eric Blake | 74a4320 | 2021-07-09 10:39:50 -0500 | [diff] [blame] | 2160 | qmp_block_dirty_bitmap_remove(dst->node_name, name, NULL); |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2161 | return -1; |
| 2162 | } |
| 2163 | } |
| 2164 | |
| 2165 | return 0; |
| 2166 | } |
| 2167 | |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 2168 | #define MAX_BUF_SECTORS 32768 |
| 2169 | |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 2170 | static void set_rate_limit(BlockBackend *blk, int64_t rate_limit) |
| 2171 | { |
| 2172 | ThrottleConfig cfg; |
| 2173 | |
| 2174 | throttle_config_init(&cfg); |
| 2175 | cfg.buckets[THROTTLE_BPS_WRITE].avg = rate_limit; |
| 2176 | |
| 2177 | blk_io_limits_enable(blk, CONVERT_THROTTLE_GROUP); |
| 2178 | blk_set_io_limits(blk, &cfg); |
| 2179 | } |
| 2180 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2181 | static int img_convert(int argc, char **argv) |
| 2182 | { |
Kevin Wolf | 0b8fb55 | 2021-04-22 18:43:44 +0200 | [diff] [blame] | 2183 | int c, bs_i, flags, src_flags = BDRV_O_NO_SHARE; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2184 | const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe", |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2185 | *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL, |
Eric Blake | 1899bf4 | 2021-09-13 08:17:35 -0500 | [diff] [blame] | 2186 | *out_filename, *out_baseimg_param, *snapshot_name = NULL, |
| 2187 | *backing_fmt = NULL; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2188 | BlockDriver *drv = NULL, *proto_drv = NULL; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2189 | BlockDriverInfo bdi; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2190 | BlockDriverState *out_bs; |
| 2191 | QemuOpts *opts = NULL, *sn_opts = NULL; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2192 | QemuOptsList *create_opts = NULL; |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2193 | QDict *open_opts = NULL; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2194 | char *options = NULL; |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 2195 | Error *local_err = NULL; |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2196 | bool writethrough, src_writethrough, image_opts = false, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2197 | skip_create = false, progress = false, tgt_image_opts = false; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2198 | int64_t ret = -EINVAL; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2199 | bool force_share = false; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2200 | bool explict_min_sparse = false; |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2201 | bool bitmaps = false; |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2202 | bool skip_broken = false; |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 2203 | int64_t rate_limit = 0; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2204 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2205 | ImgConvertState s = (ImgConvertState) { |
| 2206 | /* Need at least 4k of zeros for sparse detection */ |
| 2207 | .min_sparse = 8, |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2208 | .copy_range = false, |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2209 | .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE, |
| 2210 | .wr_in_order = true, |
| 2211 | .num_coroutines = 8, |
| 2212 | }; |
| 2213 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2214 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2215 | static const struct option long_options[] = { |
| 2216 | {"help", no_argument, 0, 'h'}, |
| 2217 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2218 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2219 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2220 | {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS}, |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 2221 | {"salvage", no_argument, 0, OPTION_SALVAGE}, |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2222 | {"target-is-zero", no_argument, 0, OPTION_TARGET_IS_ZERO}, |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2223 | {"bitmaps", no_argument, 0, OPTION_BITMAPS}, |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2224 | {"skip-broken-bitmaps", no_argument, 0, OPTION_SKIP_BROKEN}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2225 | {0, 0, 0, 0} |
| 2226 | }; |
Eric Blake | 1899bf4 | 2021-09-13 08:17:35 -0500 | [diff] [blame] | 2227 | 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] | 2228 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2229 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2230 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2231 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2232 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2233 | case ':': |
| 2234 | missing_argument(argv[optind - 1]); |
| 2235 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2236 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2237 | unrecognized_option(argv[optind - 1]); |
| 2238 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2239 | case 'h': |
| 2240 | help(); |
| 2241 | break; |
| 2242 | case 'f': |
| 2243 | fmt = optarg; |
| 2244 | break; |
| 2245 | case 'O': |
| 2246 | out_fmt = optarg; |
| 2247 | break; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 2248 | case 'B': |
| 2249 | out_baseimg = optarg; |
| 2250 | break; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2251 | case 'C': |
| 2252 | s.copy_range = true; |
| 2253 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2254 | case 'c': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2255 | s.compressed = true; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2256 | break; |
Eric Blake | 1899bf4 | 2021-09-13 08:17:35 -0500 | [diff] [blame] | 2257 | case 'F': |
| 2258 | backing_fmt = optarg; |
| 2259 | break; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2260 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 2261 | if (accumulate_options(&options, optarg) < 0) { |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2262 | goto fail_getopt; |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 2263 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2264 | break; |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2265 | case 'l': |
| 2266 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { |
Markus Armbruster | 70b9433 | 2015-02-13 12:50:26 +0100 | [diff] [blame] | 2267 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
| 2268 | optarg, false); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2269 | if (!sn_opts) { |
| 2270 | error_report("Failed in parsing snapshot param '%s'", |
| 2271 | optarg); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2272 | goto fail_getopt; |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2273 | } |
| 2274 | } else { |
| 2275 | snapshot_name = optarg; |
| 2276 | } |
| 2277 | break; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2278 | case 'S': |
| 2279 | { |
| 2280 | int64_t sval; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 2281 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 2282 | sval = cvtnum("buffer size for sparse output", optarg); |
| 2283 | if (sval < 0) { |
| 2284 | goto fail_getopt; |
| 2285 | } else if (!QEMU_IS_ALIGNED(sval, BDRV_SECTOR_SIZE) || |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 2286 | sval / BDRV_SECTOR_SIZE > MAX_BUF_SECTORS) { |
| 2287 | error_report("Invalid buffer size for sparse output specified. " |
| 2288 | "Valid sizes are multiples of %llu up to %llu. Select " |
| 2289 | "0 to disable sparse detection (fully allocates output).", |
| 2290 | BDRV_SECTOR_SIZE, MAX_BUF_SECTORS * BDRV_SECTOR_SIZE); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2291 | goto fail_getopt; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2292 | } |
| 2293 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2294 | s.min_sparse = sval / BDRV_SECTOR_SIZE; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2295 | explict_min_sparse = true; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2296 | break; |
| 2297 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2298 | case 'p': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2299 | progress = true; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2300 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2301 | case 't': |
| 2302 | cache = optarg; |
| 2303 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 2304 | case 'T': |
| 2305 | src_cache = optarg; |
| 2306 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2307 | case 'q': |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2308 | s.quiet = true; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2309 | break; |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2310 | case 'n': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2311 | skip_create = true; |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2312 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2313 | case 'm': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2314 | if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) || |
| 2315 | s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2316 | error_report("Invalid number of coroutines. Allowed number of" |
| 2317 | " coroutines is between 1 and %d", MAX_COROUTINES); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2318 | goto fail_getopt; |
| 2319 | } |
| 2320 | break; |
| 2321 | case 'W': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2322 | s.wr_in_order = false; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2323 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2324 | case 'U': |
| 2325 | force_share = true; |
| 2326 | break; |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 2327 | case 'r': |
| 2328 | rate_limit = cvtnum("rate limit", optarg); |
| 2329 | if (rate_limit < 0) { |
| 2330 | goto fail_getopt; |
| 2331 | } |
| 2332 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 2333 | case OPTION_OBJECT: |
| 2334 | user_creatable_process_cmdline(optarg); |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2335 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2336 | case OPTION_IMAGE_OPTS: |
| 2337 | image_opts = true; |
| 2338 | break; |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 2339 | case OPTION_SALVAGE: |
| 2340 | s.salvage = true; |
| 2341 | break; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2342 | case OPTION_TARGET_IMAGE_OPTS: |
| 2343 | tgt_image_opts = true; |
| 2344 | break; |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2345 | case OPTION_TARGET_IS_ZERO: |
| 2346 | /* |
| 2347 | * The user asserting that the target is blank has the |
| 2348 | * same effect as the target driver supporting zero |
| 2349 | * initialisation. |
| 2350 | */ |
| 2351 | s.has_zero_init = true; |
| 2352 | break; |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2353 | case OPTION_BITMAPS: |
| 2354 | bitmaps = true; |
| 2355 | break; |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2356 | case OPTION_SKIP_BROKEN: |
| 2357 | skip_broken = true; |
| 2358 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2359 | } |
| 2360 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2361 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2362 | if (!out_fmt && !tgt_image_opts) { |
| 2363 | out_fmt = "raw"; |
| 2364 | } |
| 2365 | |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2366 | if (skip_broken && !bitmaps) { |
| 2367 | error_report("Use of --skip-broken-bitmaps requires --bitmaps"); |
| 2368 | goto fail_getopt; |
| 2369 | } |
| 2370 | |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2371 | if (s.compressed && s.copy_range) { |
| 2372 | error_report("Cannot enable copy offloading when -c is used"); |
| 2373 | goto fail_getopt; |
| 2374 | } |
| 2375 | |
| 2376 | if (explict_min_sparse && s.copy_range) { |
| 2377 | error_report("Cannot enable copy offloading when -S is used"); |
| 2378 | goto fail_getopt; |
| 2379 | } |
| 2380 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 2381 | if (s.copy_range && s.salvage) { |
| 2382 | error_report("Cannot use copy offloading in salvaging mode"); |
| 2383 | goto fail_getopt; |
| 2384 | } |
| 2385 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2386 | if (tgt_image_opts && !skip_create) { |
| 2387 | error_report("--target-image-opts requires use of -n flag"); |
| 2388 | goto fail_getopt; |
| 2389 | } |
| 2390 | |
Kevin Wolf | ffd8e8f | 2019-08-09 11:09:21 +0200 | [diff] [blame] | 2391 | if (skip_create && options) { |
Eric Blake | 25956af | 2020-07-06 15:39:46 -0500 | [diff] [blame] | 2392 | error_report("-o has no effect when skipping image creation"); |
| 2393 | goto fail_getopt; |
Kevin Wolf | ffd8e8f | 2019-08-09 11:09:21 +0200 | [diff] [blame] | 2394 | } |
| 2395 | |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2396 | if (s.has_zero_init && !skip_create) { |
| 2397 | error_report("--target-is-zero requires use of -n flag"); |
| 2398 | goto fail_getopt; |
| 2399 | } |
| 2400 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2401 | s.src_num = argc - optind - 1; |
| 2402 | out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL; |
| 2403 | |
| 2404 | if (options && has_help_option(options)) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2405 | if (out_fmt) { |
| 2406 | ret = print_block_option_help(out_filename, out_fmt); |
| 2407 | goto fail_getopt; |
| 2408 | } else { |
| 2409 | error_report("Option help requires a format be specified"); |
| 2410 | goto fail_getopt; |
| 2411 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2412 | } |
| 2413 | |
| 2414 | if (s.src_num < 1) { |
| 2415 | error_report("Must specify image file name"); |
| 2416 | goto fail_getopt; |
| 2417 | } |
| 2418 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2419 | /* ret is still -EINVAL until here */ |
| 2420 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); |
| 2421 | if (ret < 0) { |
| 2422 | error_report("Invalid source cache option: %s", src_cache); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2423 | goto fail_getopt; |
| 2424 | } |
| 2425 | |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2426 | /* Initialize before goto out */ |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2427 | if (s.quiet) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2428 | progress = false; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2429 | } |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2430 | qemu_progress_init(progress, 1.0); |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2431 | qemu_progress_print(0, 100); |
| 2432 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2433 | s.src = g_new0(BlockBackend *, s.src_num); |
| 2434 | s.src_sectors = g_new(int64_t, s.src_num); |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 2435 | s.src_alignment = g_new(int, s.src_num); |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 2436 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2437 | for (bs_i = 0; bs_i < s.src_num; bs_i++) { |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 2438 | BlockDriverState *src_bs; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2439 | s.src[bs_i] = img_open(image_opts, argv[optind + bs_i], |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2440 | fmt, src_flags, src_writethrough, s.quiet, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2441 | force_share); |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2442 | if (!s.src[bs_i]) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2443 | ret = -1; |
| 2444 | goto out; |
| 2445 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2446 | s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]); |
| 2447 | if (s.src_sectors[bs_i] < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2448 | error_report("Could not get size of %s: %s", |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2449 | argv[optind + bs_i], strerror(-s.src_sectors[bs_i])); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2450 | ret = -1; |
| 2451 | goto out; |
| 2452 | } |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 2453 | src_bs = blk_bs(s.src[bs_i]); |
| 2454 | s.src_alignment[bs_i] = DIV_ROUND_UP(src_bs->bl.request_alignment, |
| 2455 | BDRV_SECTOR_SIZE); |
| 2456 | if (!bdrv_get_info(src_bs, &bdi)) { |
| 2457 | s.src_alignment[bs_i] = MAX(s.src_alignment[bs_i], |
| 2458 | bdi.cluster_size / BDRV_SECTOR_SIZE); |
| 2459 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2460 | s.total_sectors += s.src_sectors[bs_i]; |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 2461 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2462 | |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2463 | if (sn_opts) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2464 | bdrv_snapshot_load_tmp(blk_bs(s.src[0]), |
Peter Maydell | 10d6eda | 2017-02-10 16:28:24 +0000 | [diff] [blame] | 2465 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), |
| 2466 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), |
| 2467 | &local_err); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2468 | } else if (snapshot_name != NULL) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2469 | if (s.src_num > 1) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 2470 | error_report("No support for concatenating multiple snapshot"); |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 2471 | ret = -1; |
| 2472 | goto out; |
| 2473 | } |
Wenchao Xia | 7b4c478 | 2013-12-04 17:10:54 +0800 | [diff] [blame] | 2474 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2475 | bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name, |
| 2476 | &local_err); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2477 | } |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2478 | if (local_err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2479 | error_reportf_err(local_err, "Failed to load snapshot: "); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2480 | ret = -1; |
| 2481 | goto out; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 2482 | } |
| 2483 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2484 | if (!skip_create) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2485 | /* Find driver and parse its options */ |
| 2486 | drv = bdrv_find_format(out_fmt); |
| 2487 | if (!drv) { |
| 2488 | error_report("Unknown file format '%s'", out_fmt); |
| 2489 | ret = -1; |
| 2490 | goto out; |
| 2491 | } |
| 2492 | |
| 2493 | proto_drv = bdrv_find_protocol(out_filename, true, &local_err); |
| 2494 | if (!proto_drv) { |
| 2495 | error_report_err(local_err); |
| 2496 | ret = -1; |
| 2497 | goto out; |
| 2498 | } |
| 2499 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2500 | if (!drv->create_opts) { |
| 2501 | error_report("Format driver '%s' does not support image creation", |
| 2502 | drv->format_name); |
| 2503 | ret = -1; |
| 2504 | goto out; |
| 2505 | } |
Max Reitz | f75613c | 2014-12-02 18:32:46 +0100 | [diff] [blame] | 2506 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2507 | if (!proto_drv->create_opts) { |
| 2508 | error_report("Protocol driver '%s' does not support image creation", |
| 2509 | proto_drv->format_name); |
| 2510 | ret = -1; |
| 2511 | goto out; |
| 2512 | } |
Max Reitz | f75613c | 2014-12-02 18:32:46 +0100 | [diff] [blame] | 2513 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2514 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 2515 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
Kevin Wolf | db08adf | 2009-06-04 15:39:38 +0200 | [diff] [blame] | 2516 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2517 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 2518 | if (options) { |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 2519 | if (!qemu_opts_do_parse(opts, options, NULL, &local_err)) { |
Markus Armbruster | 97a2ca7 | 2015-03-14 10:23:15 +0100 | [diff] [blame] | 2520 | error_report_err(local_err); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 2521 | ret = -1; |
| 2522 | goto out; |
| 2523 | } |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2524 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2525 | |
Yi Li | c075c42 | 2020-08-19 09:36:07 +0800 | [diff] [blame] | 2526 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, |
| 2527 | s.total_sectors * BDRV_SECTOR_SIZE, &error_abort); |
Eric Blake | 1899bf4 | 2021-09-13 08:17:35 -0500 | [diff] [blame] | 2528 | ret = add_old_style_options(out_fmt, opts, out_baseimg, backing_fmt); |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2529 | if (ret < 0) { |
| 2530 | goto out; |
| 2531 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2532 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2533 | |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2534 | /* Get backing file name if -o backing_file was used */ |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2535 | out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2536 | if (out_baseimg_param) { |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2537 | out_baseimg = out_baseimg_param; |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2538 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2539 | s.target_has_backing = (bool) out_baseimg; |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2540 | |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2541 | if (s.has_zero_init && s.target_has_backing) { |
| 2542 | error_report("Cannot use --target-is-zero when the destination " |
| 2543 | "image has a backing file"); |
| 2544 | goto out; |
| 2545 | } |
| 2546 | |
Max Reitz | 48758a8 | 2017-04-26 15:46:48 +0200 | [diff] [blame] | 2547 | if (s.src_num > 1 && out_baseimg) { |
| 2548 | error_report("Having a backing file for the target makes no sense when " |
| 2549 | "concatenating multiple input images"); |
| 2550 | ret = -1; |
| 2551 | goto out; |
| 2552 | } |
| 2553 | |
Eric Blake | d9f059a | 2020-07-06 15:39:54 -0500 | [diff] [blame] | 2554 | if (out_baseimg_param) { |
| 2555 | if (!qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT)) { |
Eric Blake | 497a30d | 2021-05-03 14:36:00 -0700 | [diff] [blame] | 2556 | error_report("Use of backing file requires explicit " |
| 2557 | "backing format"); |
| 2558 | ret = -1; |
| 2559 | goto out; |
Eric Blake | d9f059a | 2020-07-06 15:39:54 -0500 | [diff] [blame] | 2560 | } |
| 2561 | } |
| 2562 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2563 | /* Check if compression is supported */ |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2564 | if (s.compressed) { |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2565 | bool encryption = |
| 2566 | qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false); |
Daniel P. Berrange | 0cb8d47 | 2017-06-23 17:24:06 +0100 | [diff] [blame] | 2567 | const char *encryptfmt = |
| 2568 | qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2569 | const char *preallocation = |
| 2570 | qemu_opt_get(opts, BLOCK_OPT_PREALLOC); |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2571 | |
Vladimir Sementsov-Ogievskiy | ac850bf | 2019-06-04 19:15:06 +0300 | [diff] [blame] | 2572 | if (drv && !block_driver_can_compress(drv)) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2573 | error_report("Compression not supported for this file format"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2574 | ret = -1; |
| 2575 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2576 | } |
| 2577 | |
Daniel P. Berrange | 0cb8d47 | 2017-06-23 17:24:06 +0100 | [diff] [blame] | 2578 | if (encryption || encryptfmt) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2579 | error_report("Compression and encryption not supported at " |
| 2580 | "the same time"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2581 | ret = -1; |
| 2582 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2583 | } |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 2584 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2585 | if (preallocation |
| 2586 | && strcmp(preallocation, "off")) |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 2587 | { |
| 2588 | error_report("Compression and preallocation not supported at " |
| 2589 | "the same time"); |
| 2590 | ret = -1; |
| 2591 | goto out; |
| 2592 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2593 | } |
| 2594 | |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2595 | /* Determine if bitmaps need copying */ |
| 2596 | if (bitmaps) { |
| 2597 | if (s.src_num > 1) { |
| 2598 | error_report("Copying bitmaps only possible with single source"); |
| 2599 | ret = -1; |
| 2600 | goto out; |
| 2601 | } |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2602 | ret = convert_check_bitmaps(blk_bs(s.src[0]), skip_broken); |
Eric Blake | 74a4320 | 2021-07-09 10:39:50 -0500 | [diff] [blame] | 2603 | if (ret < 0) { |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2604 | goto out; |
| 2605 | } |
| 2606 | } |
| 2607 | |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2608 | /* |
| 2609 | * The later open call will need any decryption secrets, and |
| 2610 | * bdrv_create() will purge "opts", so extract them now before |
| 2611 | * they are lost. |
| 2612 | */ |
| 2613 | if (!skip_create) { |
| 2614 | open_opts = qdict_new(); |
| 2615 | 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] | 2616 | |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2617 | /* Create the new image */ |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 2618 | ret = bdrv_create(drv, out_filename, opts, &local_err); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2619 | if (ret < 0) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2620 | error_reportf_err(local_err, "%s: error while converting %s: ", |
| 2621 | out_filename, out_fmt); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2622 | goto out; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2623 | } |
| 2624 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2625 | |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 2626 | s.target_is_new = !skip_create; |
| 2627 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2628 | 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] | 2629 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2630 | if (ret < 0) { |
| 2631 | error_report("Invalid cache option: %s", cache); |
Markus Armbruster | bb9cd2e | 2014-05-28 11:17:07 +0200 | [diff] [blame] | 2632 | goto out; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2633 | } |
| 2634 | |
Hanna Reitz | a1c6243 | 2021-08-19 12:12:00 +0200 | [diff] [blame] | 2635 | if (flags & BDRV_O_NOCACHE) { |
| 2636 | /* |
| 2637 | * If we open the target with O_DIRECT, it may be necessary to |
| 2638 | * extend its size to align to the physical sector size. |
| 2639 | */ |
| 2640 | flags |= BDRV_O_RESIZE; |
| 2641 | } |
| 2642 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2643 | if (skip_create) { |
| 2644 | s.target = img_open(tgt_image_opts, out_filename, out_fmt, |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2645 | flags, writethrough, s.quiet, false); |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2646 | } else { |
| 2647 | /* TODO ultimately we should allow --target-image-opts |
| 2648 | * to be used even when -n is not given. |
| 2649 | * That has to wait for bdrv_create to be improved |
| 2650 | * to allow filenames in option syntax |
| 2651 | */ |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2652 | s.target = img_open_file(out_filename, open_opts, out_fmt, |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2653 | flags, writethrough, s.quiet, false); |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2654 | open_opts = NULL; /* blk_new_open will have freed it */ |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2655 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2656 | if (!s.target) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2657 | ret = -1; |
| 2658 | goto out; |
| 2659 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2660 | out_bs = blk_bs(s.target); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2661 | |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2662 | if (bitmaps && !bdrv_supports_persistent_dirty_bitmap(out_bs)) { |
| 2663 | error_report("Format driver '%s' does not support bitmaps", |
| 2664 | out_bs->drv->format_name); |
| 2665 | ret = -1; |
| 2666 | goto out; |
| 2667 | } |
| 2668 | |
Vladimir Sementsov-Ogievskiy | ac850bf | 2019-06-04 19:15:06 +0300 | [diff] [blame] | 2669 | if (s.compressed && !block_driver_can_compress(out_bs->drv)) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2670 | error_report("Compression not supported for this file format"); |
| 2671 | ret = -1; |
| 2672 | goto out; |
| 2673 | } |
| 2674 | |
Eric Blake | 5def6b8 | 2016-06-23 16:37:19 -0600 | [diff] [blame] | 2675 | /* increase bufsectors from the default 4096 (2M) if opt_transfer |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 2676 | * or discard_alignment of the out_bs is greater. Limit to |
| 2677 | * MAX_BUF_SECTORS as maximum which is currently 32768 (16MB). */ |
| 2678 | s.buf_sectors = MIN(MAX_BUF_SECTORS, |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2679 | MAX(s.buf_sectors, |
| 2680 | MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS, |
| 2681 | out_bs->bl.pdiscard_alignment >> |
| 2682 | BDRV_SECTOR_BITS))); |
Peter Lieven | f2521c9 | 2013-11-27 11:07:06 +0100 | [diff] [blame] | 2683 | |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 2684 | /* try to align the write requests to the destination to avoid unnecessary |
| 2685 | * RMW cycles. */ |
| 2686 | s.alignment = MAX(pow2floor(s.min_sparse), |
| 2687 | DIV_ROUND_UP(out_bs->bl.request_alignment, |
| 2688 | BDRV_SECTOR_SIZE)); |
| 2689 | assert(is_power_of_2(s.alignment)); |
| 2690 | |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2691 | if (skip_create) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2692 | int64_t output_sectors = blk_nb_sectors(s.target); |
Markus Armbruster | 43716fa | 2014-06-26 13:23:21 +0200 | [diff] [blame] | 2693 | if (output_sectors < 0) { |
Gonglei | eec5eb4 | 2015-02-25 12:22:27 +0800 | [diff] [blame] | 2694 | error_report("unable to get output image length: %s", |
Markus Armbruster | 43716fa | 2014-06-26 13:23:21 +0200 | [diff] [blame] | 2695 | strerror(-output_sectors)); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2696 | ret = -1; |
| 2697 | goto out; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2698 | } else if (output_sectors < s.total_sectors) { |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2699 | error_report("output file is smaller than input file"); |
| 2700 | ret = -1; |
| 2701 | goto out; |
| 2702 | } |
| 2703 | } |
| 2704 | |
Max Reitz | c69291e | 2020-01-21 16:59:14 +0100 | [diff] [blame] | 2705 | if (s.target_has_backing && s.target_is_new) { |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 2706 | /* Errors are treated as "backing length unknown" (which means |
| 2707 | * s.target_backing_sectors has to be negative, which it will |
| 2708 | * be automatically). The backing file length is used only |
| 2709 | * for optimizations, so such a case is not fatal. */ |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 2710 | s.target_backing_sectors = |
| 2711 | bdrv_nb_sectors(bdrv_backing_chain_next(out_bs)); |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 2712 | } else { |
| 2713 | s.target_backing_sectors = -1; |
| 2714 | } |
| 2715 | |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2716 | ret = bdrv_get_info(out_bs, &bdi); |
| 2717 | if (ret < 0) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2718 | if (s.compressed) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2719 | error_report("could not get block driver info"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2720 | goto out; |
| 2721 | } |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2722 | } else { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2723 | s.compressed = s.compressed || bdi.needs_compressed_writes; |
| 2724 | s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE; |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2725 | } |
| 2726 | |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 2727 | if (rate_limit) { |
| 2728 | set_rate_limit(s.target, rate_limit); |
| 2729 | } |
| 2730 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2731 | ret = convert_do_copy(&s); |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2732 | |
| 2733 | /* Now copy the bitmaps */ |
| 2734 | if (bitmaps && ret == 0) { |
Eric Blake | 955171e | 2021-07-21 10:53:48 -0500 | [diff] [blame] | 2735 | 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] | 2736 | } |
| 2737 | |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2738 | out: |
Peter Lieven | 13c28af | 2013-11-27 11:07:01 +0100 | [diff] [blame] | 2739 | if (!ret) { |
| 2740 | qemu_progress_print(100, 0); |
| 2741 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2742 | qemu_progress_end(); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2743 | qemu_opts_del(opts); |
| 2744 | qemu_opts_free(create_opts); |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2745 | qobject_unref(open_opts); |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2746 | blk_unref(s.target); |
| 2747 | if (s.src) { |
| 2748 | for (bs_i = 0; bs_i < s.src_num; bs_i++) { |
| 2749 | blk_unref(s.src[bs_i]); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2750 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2751 | g_free(s.src); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2752 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2753 | g_free(s.src_sectors); |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 2754 | g_free(s.src_alignment); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2755 | fail_getopt: |
Tuguoyi | 6aec830 | 2020-11-02 09:04:57 +0000 | [diff] [blame] | 2756 | qemu_opts_del(sn_opts); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2757 | g_free(options); |
| 2758 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2759 | return !!ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
bellard | 57d1a2b | 2004-08-03 21:15:11 +0000 | [diff] [blame] | 2762 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2763 | static void dump_snapshots(BlockDriverState *bs) |
| 2764 | { |
| 2765 | QEMUSnapshotInfo *sn_tab, *sn; |
| 2766 | int nb_sns, i; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2767 | |
| 2768 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 2769 | if (nb_sns <= 0) |
| 2770 | return; |
| 2771 | printf("Snapshot list:\n"); |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2772 | bdrv_snapshot_dump(NULL); |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2773 | printf("\n"); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2774 | for(i = 0; i < nb_sns; i++) { |
| 2775 | sn = &sn_tab[i]; |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2776 | bdrv_snapshot_dump(sn); |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2777 | printf("\n"); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2778 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2779 | g_free(sn_tab); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2780 | } |
| 2781 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2782 | static void dump_json_image_info_list(ImageInfoList *list) |
| 2783 | { |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2784 | GString *str; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2785 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 2786 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2787 | |
| 2788 | visit_type_ImageInfoList(v, NULL, &list, &error_abort); |
| 2789 | visit_complete(v, &obj); |
Markus Armbruster | 6589f45 | 2020-12-11 18:11:35 +0100 | [diff] [blame] | 2790 | str = qobject_to_json_pretty(obj, true); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2791 | assert(str != NULL); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2792 | printf("%s\n", str->str); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2793 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2794 | visit_free(v); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2795 | g_string_free(str, true); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2796 | } |
| 2797 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2798 | static void dump_json_image_info(ImageInfo *info) |
| 2799 | { |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2800 | GString *str; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2801 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 2802 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2803 | |
| 2804 | visit_type_ImageInfo(v, NULL, &info, &error_abort); |
| 2805 | visit_complete(v, &obj); |
Markus Armbruster | 6589f45 | 2020-12-11 18:11:35 +0100 | [diff] [blame] | 2806 | str = qobject_to_json_pretty(obj, true); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2807 | assert(str != NULL); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2808 | printf("%s\n", str->str); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2809 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2810 | visit_free(v); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2811 | g_string_free(str, true); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2812 | } |
| 2813 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2814 | static void dump_human_image_info_list(ImageInfoList *list) |
| 2815 | { |
| 2816 | ImageInfoList *elem; |
| 2817 | bool delim = false; |
| 2818 | |
| 2819 | for (elem = list; elem; elem = elem->next) { |
| 2820 | if (delim) { |
| 2821 | printf("\n"); |
| 2822 | } |
| 2823 | delim = true; |
| 2824 | |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2825 | bdrv_image_info_dump(elem->value); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2826 | } |
| 2827 | } |
| 2828 | |
| 2829 | static gboolean str_equal_func(gconstpointer a, gconstpointer b) |
| 2830 | { |
| 2831 | return strcmp(a, b) == 0; |
| 2832 | } |
| 2833 | |
| 2834 | /** |
| 2835 | * Open an image file chain and return an ImageInfoList |
| 2836 | * |
| 2837 | * @filename: topmost image filename |
| 2838 | * @fmt: topmost image format (may be NULL to autodetect) |
| 2839 | * @chain: true - enumerate entire backing file chain |
| 2840 | * false - only topmost image file |
| 2841 | * |
| 2842 | * Returns a list of ImageInfo objects or NULL if there was an error opening an |
| 2843 | * image file. If there was an error a message will have been printed to |
| 2844 | * stderr. |
| 2845 | */ |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2846 | static ImageInfoList *collect_image_info_list(bool image_opts, |
| 2847 | const char *filename, |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2848 | const char *fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2849 | bool chain, bool force_share) |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2850 | { |
| 2851 | ImageInfoList *head = NULL; |
Eric Blake | c3033fd | 2021-01-13 16:10:12 -0600 | [diff] [blame] | 2852 | ImageInfoList **tail = &head; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2853 | GHashTable *filenames; |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2854 | Error *err = NULL; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2855 | |
| 2856 | filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL); |
| 2857 | |
| 2858 | while (filename) { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2859 | BlockBackend *blk; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2860 | BlockDriverState *bs; |
| 2861 | ImageInfo *info; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2862 | |
| 2863 | if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) { |
| 2864 | error_report("Backing file '%s' creates an infinite loop.", |
| 2865 | filename); |
| 2866 | goto err; |
| 2867 | } |
| 2868 | g_hash_table_insert(filenames, (gpointer)filename, NULL); |
| 2869 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 2870 | blk = img_open(image_opts, filename, fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2871 | BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false, |
| 2872 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2873 | if (!blk) { |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2874 | goto err; |
| 2875 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2876 | bs = blk_bs(blk); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2877 | |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2878 | bdrv_query_image_info(bs, &info, &err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2879 | if (err) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 2880 | error_report_err(err); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2881 | blk_unref(blk); |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2882 | goto err; |
Wenchao Xia | fb0ed45 | 2013-06-06 12:27:57 +0800 | [diff] [blame] | 2883 | } |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2884 | |
Eric Blake | c3033fd | 2021-01-13 16:10:12 -0600 | [diff] [blame] | 2885 | QAPI_LIST_APPEND(tail, info); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2886 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2887 | blk_unref(blk); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2888 | |
Stefan Hajnoczi | 0da7d13 | 2019-12-05 13:46:46 +0000 | [diff] [blame] | 2889 | /* Clear parameters that only apply to the topmost image */ |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2890 | filename = fmt = NULL; |
Stefan Hajnoczi | 0da7d13 | 2019-12-05 13:46:46 +0000 | [diff] [blame] | 2891 | image_opts = false; |
| 2892 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2893 | if (chain) { |
| 2894 | if (info->has_full_backing_filename) { |
| 2895 | filename = info->full_backing_filename; |
| 2896 | } else if (info->has_backing_filename) { |
John Snow | 92d617a | 2015-12-14 14:55:15 -0500 | [diff] [blame] | 2897 | error_report("Could not determine absolute backing filename," |
| 2898 | " but backing filename '%s' present", |
| 2899 | info->backing_filename); |
| 2900 | goto err; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2901 | } |
| 2902 | if (info->has_backing_filename_format) { |
| 2903 | fmt = info->backing_filename_format; |
| 2904 | } |
| 2905 | } |
| 2906 | } |
| 2907 | g_hash_table_destroy(filenames); |
| 2908 | return head; |
| 2909 | |
| 2910 | err: |
| 2911 | qapi_free_ImageInfoList(head); |
| 2912 | g_hash_table_destroy(filenames); |
| 2913 | return NULL; |
| 2914 | } |
| 2915 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2916 | static int img_info(int argc, char **argv) |
| 2917 | { |
| 2918 | int c; |
| 2919 | OutputFormat output_format = OFORMAT_HUMAN; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2920 | bool chain = false; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2921 | const char *filename, *fmt, *output; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2922 | ImageInfoList *list; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2923 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2924 | bool force_share = false; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2925 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2926 | fmt = NULL; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2927 | output = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2928 | for(;;) { |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2929 | int option_index = 0; |
| 2930 | static const struct option long_options[] = { |
| 2931 | {"help", no_argument, 0, 'h'}, |
| 2932 | {"format", required_argument, 0, 'f'}, |
| 2933 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2934 | {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2935 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2936 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2937 | {"force-share", no_argument, 0, 'U'}, |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2938 | {0, 0, 0, 0} |
| 2939 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2940 | c = getopt_long(argc, argv, ":f:hU", |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2941 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2942 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2943 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2944 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2945 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2946 | case ':': |
| 2947 | missing_argument(argv[optind - 1]); |
| 2948 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2949 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2950 | unrecognized_option(argv[optind - 1]); |
| 2951 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2952 | case 'h': |
| 2953 | help(); |
| 2954 | break; |
| 2955 | case 'f': |
| 2956 | fmt = optarg; |
| 2957 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2958 | case 'U': |
| 2959 | force_share = true; |
| 2960 | break; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2961 | case OPTION_OUTPUT: |
| 2962 | output = optarg; |
| 2963 | break; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2964 | case OPTION_BACKING_CHAIN: |
| 2965 | chain = true; |
| 2966 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 2967 | case OPTION_OBJECT: |
| 2968 | user_creatable_process_cmdline(optarg); |
| 2969 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2970 | case OPTION_IMAGE_OPTS: |
| 2971 | image_opts = true; |
| 2972 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2973 | } |
| 2974 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 2975 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2976 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2977 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2978 | filename = argv[optind++]; |
| 2979 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2980 | if (output && !strcmp(output, "json")) { |
| 2981 | output_format = OFORMAT_JSON; |
| 2982 | } else if (output && !strcmp(output, "human")) { |
| 2983 | output_format = OFORMAT_HUMAN; |
| 2984 | } else if (output) { |
| 2985 | error_report("--output must be used with human or json as argument."); |
| 2986 | return 1; |
| 2987 | } |
| 2988 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2989 | list = collect_image_info_list(image_opts, filename, fmt, chain, |
| 2990 | force_share); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2991 | if (!list) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2992 | return 1; |
| 2993 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2994 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2995 | switch (output_format) { |
| 2996 | case OFORMAT_HUMAN: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2997 | dump_human_image_info_list(list); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2998 | break; |
| 2999 | case OFORMAT_JSON: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 3000 | if (chain) { |
| 3001 | dump_json_image_info_list(list); |
| 3002 | } else { |
| 3003 | dump_json_image_info(list->value); |
| 3004 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 3005 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3006 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 3007 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 3008 | qapi_free_ImageInfoList(list); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 3009 | return 0; |
| 3010 | } |
| 3011 | |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3012 | static int dump_map_entry(OutputFormat output_format, MapEntry *e, |
| 3013 | MapEntry *next) |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3014 | { |
| 3015 | switch (output_format) { |
| 3016 | case OFORMAT_HUMAN: |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3017 | if (e->data && !e->has_offset) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3018 | error_report("File contains external, encrypted or compressed clusters."); |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3019 | return -1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3020 | } |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3021 | if (e->data && !e->zero) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3022 | printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n", |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3023 | e->start, e->length, |
| 3024 | e->has_offset ? e->offset : 0, |
| 3025 | e->has_filename ? e->filename : ""); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3026 | } |
| 3027 | /* This format ignores the distinction between 0, ZERO and ZERO|DATA. |
| 3028 | * Modify the flags here to allow more coalescing. |
| 3029 | */ |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3030 | if (next && (!next->data || next->zero)) { |
| 3031 | next->data = false; |
| 3032 | next->zero = true; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3033 | } |
| 3034 | break; |
| 3035 | case OFORMAT_JSON: |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 3036 | printf("{ \"start\": %"PRId64", \"length\": %"PRId64"," |
Eric Blake | 8417e13 | 2021-07-01 14:06:55 -0500 | [diff] [blame] | 3037 | " \"depth\": %"PRId64", \"present\": %s, \"zero\": %s," |
| 3038 | " \"data\": %s", e->start, e->length, e->depth, |
| 3039 | e->present ? "true" : "false", |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3040 | e->zero ? "true" : "false", |
| 3041 | e->data ? "true" : "false"); |
| 3042 | if (e->has_offset) { |
Paolo Bonzini | c745bfb | 2013-09-11 18:47:52 +0200 | [diff] [blame] | 3043 | printf(", \"offset\": %"PRId64"", e->offset); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3044 | } |
| 3045 | putchar('}'); |
| 3046 | |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 3047 | if (next) { |
| 3048 | puts(","); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3049 | } |
| 3050 | break; |
| 3051 | } |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3052 | return 0; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3053 | } |
| 3054 | |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3055 | static int get_block_status(BlockDriverState *bs, int64_t offset, |
| 3056 | int64_t bytes, MapEntry *e) |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3057 | { |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3058 | int ret; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3059 | int depth; |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 3060 | BlockDriverState *file; |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3061 | bool has_offset; |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3062 | int64_t map; |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3063 | char *filename = NULL; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3064 | |
| 3065 | /* As an optimization, we could cache the current range of unallocated |
| 3066 | * clusters in each file of the chain, and avoid querying the same |
| 3067 | * range repeatedly. |
| 3068 | */ |
| 3069 | |
| 3070 | depth = 0; |
| 3071 | for (;;) { |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3072 | bs = bdrv_skip_filters(bs); |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3073 | ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3074 | if (ret < 0) { |
| 3075 | return ret; |
| 3076 | } |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3077 | assert(bytes); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3078 | if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) { |
| 3079 | break; |
| 3080 | } |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3081 | bs = bdrv_cow_bs(bs); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3082 | if (bs == NULL) { |
| 3083 | ret = 0; |
| 3084 | break; |
| 3085 | } |
| 3086 | |
| 3087 | depth++; |
| 3088 | } |
| 3089 | |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3090 | has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID); |
| 3091 | |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3092 | if (file && has_offset) { |
| 3093 | bdrv_refresh_filename(file); |
| 3094 | filename = file->filename; |
| 3095 | } |
| 3096 | |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3097 | *e = (MapEntry) { |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3098 | .start = offset, |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3099 | .length = bytes, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3100 | .data = !!(ret & BDRV_BLOCK_DATA), |
| 3101 | .zero = !!(ret & BDRV_BLOCK_ZERO), |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3102 | .offset = map, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3103 | .has_offset = has_offset, |
| 3104 | .depth = depth, |
Eric Blake | 8417e13 | 2021-07-01 14:06:55 -0500 | [diff] [blame] | 3105 | .present = !!(ret & BDRV_BLOCK_ALLOCATED), |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3106 | .has_filename = filename, |
| 3107 | .filename = filename, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3108 | }; |
| 3109 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3110 | return 0; |
| 3111 | } |
| 3112 | |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3113 | static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next) |
| 3114 | { |
| 3115 | if (curr->length == 0) { |
| 3116 | return false; |
| 3117 | } |
| 3118 | if (curr->zero != next->zero || |
| 3119 | curr->data != next->data || |
| 3120 | curr->depth != next->depth || |
Eric Blake | 8417e13 | 2021-07-01 14:06:55 -0500 | [diff] [blame] | 3121 | curr->present != next->present || |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3122 | curr->has_filename != next->has_filename || |
| 3123 | curr->has_offset != next->has_offset) { |
| 3124 | return false; |
| 3125 | } |
| 3126 | if (curr->has_filename && strcmp(curr->filename, next->filename)) { |
| 3127 | return false; |
| 3128 | } |
| 3129 | if (curr->has_offset && curr->offset + curr->length != next->offset) { |
| 3130 | return false; |
| 3131 | } |
| 3132 | return true; |
| 3133 | } |
| 3134 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3135 | static int img_map(int argc, char **argv) |
| 3136 | { |
| 3137 | int c; |
| 3138 | OutputFormat output_format = OFORMAT_HUMAN; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3139 | BlockBackend *blk; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3140 | BlockDriverState *bs; |
| 3141 | const char *filename, *fmt, *output; |
| 3142 | int64_t length; |
| 3143 | MapEntry curr = { .length = 0 }, next; |
| 3144 | int ret = 0; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3145 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3146 | bool force_share = false; |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3147 | int64_t start_offset = 0; |
| 3148 | int64_t max_length = -1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3149 | |
| 3150 | fmt = NULL; |
| 3151 | output = NULL; |
| 3152 | for (;;) { |
| 3153 | int option_index = 0; |
| 3154 | static const struct option long_options[] = { |
| 3155 | {"help", no_argument, 0, 'h'}, |
| 3156 | {"format", required_argument, 0, 'f'}, |
| 3157 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3158 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3159 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3160 | {"force-share", no_argument, 0, 'U'}, |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3161 | {"start-offset", required_argument, 0, 's'}, |
| 3162 | {"max-length", required_argument, 0, 'l'}, |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3163 | {0, 0, 0, 0} |
| 3164 | }; |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3165 | c = getopt_long(argc, argv, ":f:s:l:hU", |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3166 | long_options, &option_index); |
| 3167 | if (c == -1) { |
| 3168 | break; |
| 3169 | } |
| 3170 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3171 | case ':': |
| 3172 | missing_argument(argv[optind - 1]); |
| 3173 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3174 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3175 | unrecognized_option(argv[optind - 1]); |
| 3176 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3177 | case 'h': |
| 3178 | help(); |
| 3179 | break; |
| 3180 | case 'f': |
| 3181 | fmt = optarg; |
| 3182 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3183 | case 'U': |
| 3184 | force_share = true; |
| 3185 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3186 | case OPTION_OUTPUT: |
| 3187 | output = optarg; |
| 3188 | break; |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3189 | case 's': |
| 3190 | start_offset = cvtnum("start offset", optarg); |
| 3191 | if (start_offset < 0) { |
| 3192 | return 1; |
| 3193 | } |
| 3194 | break; |
| 3195 | case 'l': |
| 3196 | max_length = cvtnum("max length", optarg); |
| 3197 | if (max_length < 0) { |
| 3198 | return 1; |
| 3199 | } |
| 3200 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 3201 | case OPTION_OBJECT: |
| 3202 | user_creatable_process_cmdline(optarg); |
| 3203 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3204 | case OPTION_IMAGE_OPTS: |
| 3205 | image_opts = true; |
| 3206 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3207 | } |
| 3208 | } |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3209 | if (optind != argc - 1) { |
| 3210 | error_exit("Expecting one image file name"); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3211 | } |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3212 | filename = argv[optind]; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3213 | |
| 3214 | if (output && !strcmp(output, "json")) { |
| 3215 | output_format = OFORMAT_JSON; |
| 3216 | } else if (output && !strcmp(output, "human")) { |
| 3217 | output_format = OFORMAT_HUMAN; |
| 3218 | } else if (output) { |
| 3219 | error_report("--output must be used with human or json as argument."); |
| 3220 | return 1; |
| 3221 | } |
| 3222 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3223 | blk = img_open(image_opts, filename, fmt, 0, false, false, force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3224 | if (!blk) { |
| 3225 | return 1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3226 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3227 | bs = blk_bs(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3228 | |
| 3229 | if (output_format == OFORMAT_HUMAN) { |
| 3230 | printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File"); |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 3231 | } else if (output_format == OFORMAT_JSON) { |
| 3232 | putchar('['); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3233 | } |
| 3234 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3235 | length = blk_getlength(blk); |
Eyal Moscovici | 8f282e8 | 2020-05-13 16:36:27 +0300 | [diff] [blame] | 3236 | if (length < 0) { |
| 3237 | error_report("Failed to get size for '%s'", filename); |
| 3238 | return 1; |
| 3239 | } |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3240 | if (max_length != -1) { |
| 3241 | length = MIN(start_offset + max_length, length); |
| 3242 | } |
Eyal Moscovici | 8f282e8 | 2020-05-13 16:36:27 +0300 | [diff] [blame] | 3243 | |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3244 | curr.start = start_offset; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3245 | while (curr.start + curr.length < length) { |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3246 | int64_t offset = curr.start + curr.length; |
Kevin Wolf | d0ceea8 | 2020-07-07 16:46:29 +0200 | [diff] [blame] | 3247 | int64_t n = length - offset; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3248 | |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3249 | ret = get_block_status(bs, offset, n, &next); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3250 | if (ret < 0) { |
| 3251 | error_report("Could not read file metadata: %s", strerror(-ret)); |
| 3252 | goto out; |
| 3253 | } |
| 3254 | |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3255 | if (entry_mergeable(&curr, &next)) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3256 | curr.length += next.length; |
| 3257 | continue; |
| 3258 | } |
| 3259 | |
| 3260 | if (curr.length > 0) { |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3261 | ret = dump_map_entry(output_format, &curr, &next); |
| 3262 | if (ret < 0) { |
| 3263 | goto out; |
| 3264 | } |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3265 | } |
| 3266 | curr = next; |
| 3267 | } |
| 3268 | |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3269 | ret = dump_map_entry(output_format, &curr, NULL); |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 3270 | if (output_format == OFORMAT_JSON) { |
| 3271 | puts("]"); |
| 3272 | } |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3273 | |
| 3274 | out: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3275 | blk_unref(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3276 | return ret < 0; |
| 3277 | } |
| 3278 | |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3279 | #define SNAPSHOT_LIST 1 |
| 3280 | #define SNAPSHOT_CREATE 2 |
| 3281 | #define SNAPSHOT_APPLY 3 |
| 3282 | #define SNAPSHOT_DELETE 4 |
| 3283 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3284 | static int img_snapshot(int argc, char **argv) |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3285 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3286 | BlockBackend *blk; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3287 | BlockDriverState *bs; |
| 3288 | QEMUSnapshotInfo sn; |
| 3289 | char *filename, *snapshot_name = NULL; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3290 | int c, ret = 0, bdrv_oflags; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3291 | int action = 0; |
| 3292 | qemu_timeval tv; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3293 | bool quiet = false; |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 3294 | Error *err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3295 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3296 | bool force_share = false; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3297 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3298 | bdrv_oflags = BDRV_O_RDWR; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3299 | /* Parse commandline parameters */ |
| 3300 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3301 | static const struct option long_options[] = { |
| 3302 | {"help", no_argument, 0, 'h'}, |
| 3303 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3304 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3305 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3306 | {0, 0, 0, 0} |
| 3307 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3308 | c = getopt_long(argc, argv, ":la:c:d:hqU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3309 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3310 | if (c == -1) { |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3311 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3312 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3313 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3314 | case ':': |
| 3315 | missing_argument(argv[optind - 1]); |
| 3316 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3317 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3318 | unrecognized_option(argv[optind - 1]); |
| 3319 | break; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3320 | case 'h': |
| 3321 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3322 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3323 | case 'l': |
| 3324 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3325 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3326 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3327 | } |
| 3328 | action = SNAPSHOT_LIST; |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 3329 | bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */ |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3330 | break; |
| 3331 | case 'a': |
| 3332 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3333 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3334 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3335 | } |
| 3336 | action = SNAPSHOT_APPLY; |
| 3337 | snapshot_name = optarg; |
| 3338 | break; |
| 3339 | case 'c': |
| 3340 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3341 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3342 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3343 | } |
| 3344 | action = SNAPSHOT_CREATE; |
| 3345 | snapshot_name = optarg; |
| 3346 | break; |
| 3347 | case 'd': |
| 3348 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3349 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3350 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3351 | } |
| 3352 | action = SNAPSHOT_DELETE; |
| 3353 | snapshot_name = optarg; |
| 3354 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3355 | case 'q': |
| 3356 | quiet = true; |
| 3357 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3358 | case 'U': |
| 3359 | force_share = true; |
| 3360 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 3361 | case OPTION_OBJECT: |
| 3362 | user_creatable_process_cmdline(optarg); |
| 3363 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3364 | case OPTION_IMAGE_OPTS: |
| 3365 | image_opts = true; |
| 3366 | break; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3367 | } |
| 3368 | } |
| 3369 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 3370 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3371 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3372 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3373 | filename = argv[optind++]; |
| 3374 | |
| 3375 | /* Open the image */ |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3376 | blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet, |
| 3377 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3378 | if (!blk) { |
| 3379 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3380 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3381 | bs = blk_bs(blk); |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3382 | |
| 3383 | /* Perform the requested action */ |
| 3384 | switch(action) { |
| 3385 | case SNAPSHOT_LIST: |
| 3386 | dump_snapshots(bs); |
| 3387 | break; |
| 3388 | |
| 3389 | case SNAPSHOT_CREATE: |
| 3390 | memset(&sn, 0, sizeof(sn)); |
| 3391 | pstrcpy(sn.name, sizeof(sn.name), snapshot_name); |
| 3392 | |
| 3393 | qemu_gettimeofday(&tv); |
| 3394 | sn.date_sec = tv.tv_sec; |
| 3395 | sn.date_nsec = tv.tv_usec * 1000; |
| 3396 | |
| 3397 | ret = bdrv_snapshot_create(bs, &sn); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3398 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3399 | error_report("Could not create snapshot '%s': %d (%s)", |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3400 | snapshot_name, ret, strerror(-ret)); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3401 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3402 | break; |
| 3403 | |
| 3404 | case SNAPSHOT_APPLY: |
Kevin Wolf | 0b62bcb | 2017-11-20 15:28:41 +0100 | [diff] [blame] | 3405 | ret = bdrv_snapshot_goto(bs, snapshot_name, &err); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3406 | if (ret) { |
Kevin Wolf | 0b62bcb | 2017-11-20 15:28:41 +0100 | [diff] [blame] | 3407 | error_reportf_err(err, "Could not apply snapshot '%s': ", |
| 3408 | snapshot_name); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3409 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3410 | break; |
| 3411 | |
| 3412 | case SNAPSHOT_DELETE: |
Daniel Henrique Barboza | 8c04093 | 2018-11-07 11:09:59 -0200 | [diff] [blame] | 3413 | ret = bdrv_snapshot_find(bs, &sn, snapshot_name); |
| 3414 | if (ret < 0) { |
| 3415 | error_report("Could not delete snapshot '%s': snapshot not " |
| 3416 | "found", snapshot_name); |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 3417 | ret = 1; |
Daniel Henrique Barboza | 8c04093 | 2018-11-07 11:09:59 -0200 | [diff] [blame] | 3418 | } else { |
| 3419 | ret = bdrv_snapshot_delete(bs, sn.id_str, sn.name, &err); |
| 3420 | if (ret < 0) { |
| 3421 | error_reportf_err(err, "Could not delete snapshot '%s': ", |
| 3422 | snapshot_name); |
| 3423 | ret = 1; |
| 3424 | } |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3425 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3426 | break; |
| 3427 | } |
| 3428 | |
| 3429 | /* Cleanup */ |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3430 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3431 | if (ret) { |
| 3432 | return 1; |
| 3433 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3434 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3435 | } |
| 3436 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3437 | static int img_rebase(int argc, char **argv) |
| 3438 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3439 | BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL; |
Paolo Bonzini | 396374c | 2016-02-25 23:53:54 +0100 | [diff] [blame] | 3440 | uint8_t *buf_old = NULL; |
| 3441 | uint8_t *buf_new = NULL; |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3442 | BlockDriverState *bs = NULL, *prefix_chain_bs = NULL; |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3443 | BlockDriverState *unfiltered_bs; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3444 | char *filename; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3445 | const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg; |
| 3446 | int c, flags, src_flags, ret; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3447 | bool writethrough, src_writethrough; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3448 | int unsafe = 0; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3449 | bool force_share = false; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3450 | int progress = 0; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3451 | bool quiet = false; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 3452 | Error *local_err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3453 | bool image_opts = false; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3454 | |
| 3455 | /* Parse commandline parameters */ |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 3456 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3457 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3458 | src_cache = BDRV_DEFAULT_CACHE; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3459 | out_baseimg = NULL; |
| 3460 | out_basefmt = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3461 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3462 | static const struct option long_options[] = { |
| 3463 | {"help", no_argument, 0, 'h'}, |
| 3464 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3465 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3466 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3467 | {0, 0, 0, 0} |
| 3468 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3469 | c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3470 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3471 | if (c == -1) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3472 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3473 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3474 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3475 | case ':': |
| 3476 | missing_argument(argv[optind - 1]); |
| 3477 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3478 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3479 | unrecognized_option(argv[optind - 1]); |
| 3480 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3481 | case 'h': |
| 3482 | help(); |
| 3483 | return 0; |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 3484 | case 'f': |
| 3485 | fmt = optarg; |
| 3486 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3487 | case 'F': |
| 3488 | out_basefmt = optarg; |
| 3489 | break; |
| 3490 | case 'b': |
| 3491 | out_baseimg = optarg; |
| 3492 | break; |
| 3493 | case 'u': |
| 3494 | unsafe = 1; |
| 3495 | break; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3496 | case 'p': |
| 3497 | progress = 1; |
| 3498 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3499 | case 't': |
| 3500 | cache = optarg; |
| 3501 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3502 | case 'T': |
| 3503 | src_cache = optarg; |
| 3504 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3505 | case 'q': |
| 3506 | quiet = true; |
| 3507 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 3508 | case OPTION_OBJECT: |
| 3509 | user_creatable_process_cmdline(optarg); |
| 3510 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3511 | case OPTION_IMAGE_OPTS: |
| 3512 | image_opts = true; |
| 3513 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3514 | case 'U': |
| 3515 | force_share = true; |
| 3516 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3517 | } |
| 3518 | } |
| 3519 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3520 | if (quiet) { |
| 3521 | progress = 0; |
| 3522 | } |
| 3523 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3524 | if (optind != argc - 1) { |
| 3525 | error_exit("Expecting one image file name"); |
| 3526 | } |
| 3527 | if (!unsafe && !out_baseimg) { |
| 3528 | error_exit("Must specify backing file (-b) or use unsafe mode (-u)"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3529 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3530 | filename = argv[optind++]; |
| 3531 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3532 | qemu_progress_init(progress, 2.0); |
| 3533 | qemu_progress_print(0, 100); |
| 3534 | |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3535 | flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0); |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3536 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3537 | if (ret < 0) { |
| 3538 | error_report("Invalid cache option: %s", cache); |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3539 | goto out; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3540 | } |
| 3541 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3542 | src_flags = 0; |
| 3543 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3544 | if (ret < 0) { |
| 3545 | error_report("Invalid source cache option: %s", src_cache); |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3546 | goto out; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3547 | } |
| 3548 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3549 | /* The source files are opened read-only, don't care about WCE */ |
| 3550 | assert((src_flags & BDRV_O_RDWR) == 0); |
| 3551 | (void) src_writethrough; |
| 3552 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3553 | /* |
| 3554 | * Open the images. |
| 3555 | * |
| 3556 | * Ignore the old backing file for unsafe rebase in case we want to correct |
| 3557 | * the reference to a renamed or moved backing file. |
| 3558 | */ |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3559 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 3560 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3561 | if (!blk) { |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3562 | ret = -1; |
| 3563 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3564 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3565 | bs = blk_bs(blk); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3566 | |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3567 | unfiltered_bs = bdrv_skip_filters(bs); |
| 3568 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3569 | if (out_basefmt != NULL) { |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3570 | if (bdrv_find_format(out_basefmt) == NULL) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3571 | error_report("Invalid format name: '%s'", out_basefmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3572 | ret = -1; |
| 3573 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3574 | } |
| 3575 | } |
| 3576 | |
| 3577 | /* For safe rebasing we need to compare old and new backing file */ |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3578 | if (!unsafe) { |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3579 | QDict *options = NULL; |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3580 | BlockDriverState *base_bs = bdrv_cow_bs(unfiltered_bs); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3581 | |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3582 | if (base_bs) { |
Kevin Wolf | d861ab3 | 2019-04-25 14:25:10 +0200 | [diff] [blame] | 3583 | blk_old_backing = blk_new(qemu_get_aio_context(), |
| 3584 | BLK_PERM_CONSISTENT_READ, |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3585 | BLK_PERM_ALL); |
| 3586 | ret = blk_insert_bs(blk_old_backing, base_bs, |
| 3587 | &local_err); |
| 3588 | if (ret < 0) { |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3589 | error_reportf_err(local_err, |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3590 | "Could not reuse old backing file '%s': ", |
| 3591 | base_bs->filename); |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3592 | goto out; |
| 3593 | } |
| 3594 | } else { |
| 3595 | blk_old_backing = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3596 | } |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3597 | |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3598 | if (out_baseimg[0]) { |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3599 | const char *overlay_filename; |
| 3600 | char *out_real_path; |
| 3601 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3602 | options = qdict_new(); |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3603 | if (out_basefmt) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 3604 | qdict_put_str(options, "driver", out_basefmt); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3605 | } |
| 3606 | if (force_share) { |
| 3607 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3608 | } |
| 3609 | |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3610 | bdrv_refresh_filename(bs); |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3611 | overlay_filename = bs->exact_filename[0] ? bs->exact_filename |
| 3612 | : bs->filename; |
Max Reitz | 645ae7d | 2019-02-01 20:29:14 +0100 | [diff] [blame] | 3613 | out_real_path = |
| 3614 | bdrv_get_full_backing_filename_from_filename(overlay_filename, |
| 3615 | out_baseimg, |
| 3616 | &local_err); |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3617 | if (local_err) { |
Max Reitz | f22356d | 2019-05-28 21:53:38 +0200 | [diff] [blame] | 3618 | qobject_unref(options); |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3619 | error_reportf_err(local_err, |
| 3620 | "Could not resolve backing filename: "); |
| 3621 | ret = -1; |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3622 | goto out; |
| 3623 | } |
| 3624 | |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3625 | /* |
| 3626 | * Find out whether we rebase an image on top of a previous image |
| 3627 | * in its chain. |
| 3628 | */ |
| 3629 | prefix_chain_bs = bdrv_find_backing_image(bs, out_real_path); |
Sam Eiderman | 330c729 | 2019-05-23 19:33:37 +0300 | [diff] [blame] | 3630 | if (prefix_chain_bs) { |
Max Reitz | f22356d | 2019-05-28 21:53:38 +0200 | [diff] [blame] | 3631 | qobject_unref(options); |
Sam Eiderman | 330c729 | 2019-05-23 19:33:37 +0300 | [diff] [blame] | 3632 | g_free(out_real_path); |
Max Reitz | f22356d | 2019-05-28 21:53:38 +0200 | [diff] [blame] | 3633 | |
Kevin Wolf | d861ab3 | 2019-04-25 14:25:10 +0200 | [diff] [blame] | 3634 | blk_new_backing = blk_new(qemu_get_aio_context(), |
| 3635 | BLK_PERM_CONSISTENT_READ, |
Sam Eiderman | 330c729 | 2019-05-23 19:33:37 +0300 | [diff] [blame] | 3636 | BLK_PERM_ALL); |
| 3637 | ret = blk_insert_bs(blk_new_backing, prefix_chain_bs, |
| 3638 | &local_err); |
| 3639 | if (ret < 0) { |
| 3640 | error_reportf_err(local_err, |
| 3641 | "Could not reuse backing file '%s': ", |
| 3642 | out_baseimg); |
| 3643 | goto out; |
| 3644 | } |
| 3645 | } else { |
| 3646 | blk_new_backing = blk_new_open(out_real_path, NULL, |
| 3647 | options, src_flags, &local_err); |
| 3648 | g_free(out_real_path); |
| 3649 | if (!blk_new_backing) { |
| 3650 | error_reportf_err(local_err, |
| 3651 | "Could not open new backing file '%s': ", |
| 3652 | out_baseimg); |
| 3653 | ret = -1; |
| 3654 | goto out; |
| 3655 | } |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3656 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3657 | } |
| 3658 | } |
| 3659 | |
| 3660 | /* |
| 3661 | * Check each unallocated cluster in the COW file. If it is unallocated, |
| 3662 | * accesses go to the backing file. We must therefore compare this cluster |
| 3663 | * in the old and new backing file, and if they differ we need to copy it |
| 3664 | * from the old backing file into the COW file. |
| 3665 | * |
| 3666 | * If qemu-img crashes during this step, no harm is done. The content of |
| 3667 | * the image is the same as the original one at any time. |
| 3668 | */ |
| 3669 | if (!unsafe) { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3670 | int64_t size; |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3671 | int64_t old_backing_size = 0; |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3672 | int64_t new_backing_size = 0; |
| 3673 | uint64_t offset; |
| 3674 | int64_t n; |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 3675 | float local_progress = 0; |
TeLeMan | d6771bf | 2010-02-08 16:20:00 +0800 | [diff] [blame] | 3676 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3677 | buf_old = blk_blockalign(blk, IO_BUF_SIZE); |
| 3678 | buf_new = blk_blockalign(blk, IO_BUF_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3679 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3680 | size = blk_getlength(blk); |
| 3681 | if (size < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3682 | error_report("Could not get size of '%s': %s", |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3683 | filename, strerror(-size)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3684 | ret = -1; |
| 3685 | goto out; |
| 3686 | } |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3687 | if (blk_old_backing) { |
| 3688 | old_backing_size = blk_getlength(blk_old_backing); |
| 3689 | if (old_backing_size < 0) { |
| 3690 | char backing_name[PATH_MAX]; |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3691 | |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3692 | bdrv_get_backing_filename(bs, backing_name, |
| 3693 | sizeof(backing_name)); |
| 3694 | error_report("Could not get size of '%s': %s", |
| 3695 | backing_name, strerror(-old_backing_size)); |
| 3696 | ret = -1; |
| 3697 | goto out; |
| 3698 | } |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3699 | } |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3700 | if (blk_new_backing) { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3701 | new_backing_size = blk_getlength(blk_new_backing); |
| 3702 | if (new_backing_size < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3703 | error_report("Could not get size of '%s': %s", |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3704 | out_baseimg, strerror(-new_backing_size)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3705 | ret = -1; |
| 3706 | goto out; |
| 3707 | } |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3708 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3709 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3710 | if (size != 0) { |
| 3711 | local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE)); |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 3712 | } |
| 3713 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3714 | for (offset = 0; offset < size; offset += n) { |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3715 | bool buf_old_is_zero = false; |
| 3716 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3717 | /* How many bytes can we handle with the next read? */ |
| 3718 | n = MIN(IO_BUF_SIZE, size - offset); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3719 | |
| 3720 | /* If the cluster is allocated, we don't need to take action */ |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3721 | ret = bdrv_is_allocated(unfiltered_bs, offset, n, &n); |
Paolo Bonzini | d663640 | 2013-09-04 19:00:25 +0200 | [diff] [blame] | 3722 | if (ret < 0) { |
| 3723 | error_report("error while reading image metadata: %s", |
| 3724 | strerror(-ret)); |
| 3725 | goto out; |
| 3726 | } |
Kevin Wolf | cc60e32 | 2010-04-29 14:47:48 +0200 | [diff] [blame] | 3727 | if (ret) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3728 | continue; |
| 3729 | } |
| 3730 | |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3731 | if (prefix_chain_bs) { |
| 3732 | /* |
| 3733 | * If cluster wasn't changed since prefix_chain, we don't need |
| 3734 | * to take action |
| 3735 | */ |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3736 | ret = bdrv_is_allocated_above(bdrv_cow_bs(unfiltered_bs), |
| 3737 | prefix_chain_bs, false, |
| 3738 | offset, n, &n); |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3739 | if (ret < 0) { |
| 3740 | error_report("error while reading image metadata: %s", |
| 3741 | strerror(-ret)); |
| 3742 | goto out; |
| 3743 | } |
| 3744 | if (!ret) { |
| 3745 | continue; |
| 3746 | } |
| 3747 | } |
| 3748 | |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3749 | /* |
| 3750 | * Read old and new backing file and take into consideration that |
| 3751 | * backing files may be smaller than the COW image. |
| 3752 | */ |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3753 | if (offset >= old_backing_size) { |
| 3754 | memset(buf_old, 0, n); |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3755 | buf_old_is_zero = true; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3756 | } else { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3757 | if (offset + n > old_backing_size) { |
| 3758 | n = old_backing_size - offset; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3759 | } |
| 3760 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3761 | ret = blk_pread(blk_old_backing, offset, buf_old, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3762 | if (ret < 0) { |
| 3763 | error_report("error while reading from old backing file"); |
| 3764 | goto out; |
| 3765 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3766 | } |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3767 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3768 | if (offset >= new_backing_size || !blk_new_backing) { |
| 3769 | memset(buf_new, 0, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3770 | } else { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3771 | if (offset + n > new_backing_size) { |
| 3772 | n = new_backing_size - offset; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3773 | } |
| 3774 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3775 | ret = blk_pread(blk_new_backing, offset, buf_new, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3776 | if (ret < 0) { |
| 3777 | error_report("error while reading from new backing file"); |
| 3778 | goto out; |
| 3779 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3780 | } |
| 3781 | |
| 3782 | /* If they differ, we need to write to the COW file */ |
| 3783 | uint64_t written = 0; |
| 3784 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3785 | while (written < n) { |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 3786 | int64_t pnum; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3787 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3788 | if (compare_buffers(buf_old + written, buf_new + written, |
| 3789 | n - written, &pnum)) |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3790 | { |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3791 | if (buf_old_is_zero) { |
| 3792 | ret = blk_pwrite_zeroes(blk, offset + written, pnum, 0); |
| 3793 | } else { |
| 3794 | ret = blk_pwrite(blk, offset + written, |
| 3795 | buf_old + written, pnum, 0); |
| 3796 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3797 | if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3798 | error_report("Error while writing to COW image: %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3799 | strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3800 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3801 | } |
| 3802 | } |
| 3803 | |
| 3804 | written += pnum; |
| 3805 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3806 | qemu_progress_print(local_progress, 100); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3807 | } |
| 3808 | } |
| 3809 | |
| 3810 | /* |
| 3811 | * Change the backing file. All clusters that are different from the old |
| 3812 | * backing file are overwritten in the COW file now, so the visible content |
| 3813 | * doesn't change when we switch the backing file. |
| 3814 | */ |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3815 | if (out_baseimg && *out_baseimg) { |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3816 | ret = bdrv_change_backing_file(unfiltered_bs, out_baseimg, out_basefmt, |
| 3817 | true); |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3818 | } else { |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3819 | ret = bdrv_change_backing_file(unfiltered_bs, NULL, NULL, false); |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3820 | } |
| 3821 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3822 | if (ret == -ENOSPC) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3823 | error_report("Could not change the backing file to '%s': No " |
| 3824 | "space left in the file header", out_baseimg); |
Eric Blake | a7cd44b | 2021-07-08 10:52:28 -0500 | [diff] [blame] | 3825 | } else if (ret == -EINVAL && out_baseimg && !out_basefmt) { |
| 3826 | error_report("Could not change the backing file to '%s': backing " |
| 3827 | "format must be specified", out_baseimg); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3828 | } else if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3829 | error_report("Could not change the backing file to '%s': %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3830 | out_baseimg, strerror(-ret)); |
| 3831 | } |
| 3832 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3833 | qemu_progress_print(100, 0); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3834 | /* |
| 3835 | * TODO At this point it is possible to check if any clusters that are |
| 3836 | * allocated in the COW file are the same in the backing file. If so, they |
| 3837 | * could be dropped from the COW file. Don't do this before switching the |
| 3838 | * backing file, in case of a crash this would lead to corruption. |
| 3839 | */ |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3840 | out: |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3841 | qemu_progress_end(); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3842 | /* Cleanup */ |
| 3843 | if (!unsafe) { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3844 | blk_unref(blk_old_backing); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3845 | blk_unref(blk_new_backing); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3846 | } |
Paolo Bonzini | 396374c | 2016-02-25 23:53:54 +0100 | [diff] [blame] | 3847 | qemu_vfree(buf_old); |
| 3848 | qemu_vfree(buf_new); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3849 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3850 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3851 | if (ret) { |
| 3852 | return 1; |
| 3853 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3854 | return 0; |
| 3855 | } |
| 3856 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3857 | static int img_resize(int argc, char **argv) |
| 3858 | { |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 3859 | Error *err = NULL; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3860 | int c, ret, relative; |
| 3861 | const char *filename, *fmt, *size; |
Max Reitz | 09c5c6d | 2019-09-18 11:51:44 +0200 | [diff] [blame] | 3862 | int64_t n, total_size, current_size; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3863 | bool quiet = false; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3864 | BlockBackend *blk = NULL; |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3865 | PreallocMode prealloc = PREALLOC_MODE_OFF; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3866 | QemuOpts *param; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3867 | |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3868 | static QemuOptsList resize_options = { |
| 3869 | .name = "resize_options", |
| 3870 | .head = QTAILQ_HEAD_INITIALIZER(resize_options.head), |
| 3871 | .desc = { |
| 3872 | { |
| 3873 | .name = BLOCK_OPT_SIZE, |
| 3874 | .type = QEMU_OPT_SIZE, |
| 3875 | .help = "Virtual disk size" |
| 3876 | }, { |
| 3877 | /* end of list */ |
| 3878 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3879 | }, |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3880 | }; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3881 | bool image_opts = false; |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3882 | bool shrink = false; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3883 | |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 3884 | /* Remove size from argv manually so that negative numbers are not treated |
| 3885 | * as options by getopt. */ |
| 3886 | if (argc < 3) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3887 | error_exit("Not enough arguments"); |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 3888 | return 1; |
| 3889 | } |
| 3890 | |
| 3891 | size = argv[--argc]; |
| 3892 | |
| 3893 | /* Parse getopt arguments */ |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3894 | fmt = NULL; |
| 3895 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3896 | static const struct option long_options[] = { |
| 3897 | {"help", no_argument, 0, 'h'}, |
| 3898 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3899 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3900 | {"preallocation", required_argument, 0, OPTION_PREALLOCATION}, |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3901 | {"shrink", no_argument, 0, OPTION_SHRINK}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3902 | {0, 0, 0, 0} |
| 3903 | }; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3904 | c = getopt_long(argc, argv, ":f:hq", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3905 | long_options, NULL); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3906 | if (c == -1) { |
| 3907 | break; |
| 3908 | } |
| 3909 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3910 | case ':': |
| 3911 | missing_argument(argv[optind - 1]); |
| 3912 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3913 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3914 | unrecognized_option(argv[optind - 1]); |
| 3915 | break; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3916 | case 'h': |
| 3917 | help(); |
| 3918 | break; |
| 3919 | case 'f': |
| 3920 | fmt = optarg; |
| 3921 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3922 | case 'q': |
| 3923 | quiet = true; |
| 3924 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 3925 | case OPTION_OBJECT: |
| 3926 | user_creatable_process_cmdline(optarg); |
| 3927 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3928 | case OPTION_IMAGE_OPTS: |
| 3929 | image_opts = true; |
| 3930 | break; |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3931 | case OPTION_PREALLOCATION: |
Marc-André Lureau | f7abe0e | 2017-08-24 10:46:10 +0200 | [diff] [blame] | 3932 | prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg, |
Markus Armbruster | 06c60b6 | 2017-08-24 10:45:57 +0200 | [diff] [blame] | 3933 | PREALLOC_MODE__MAX, NULL); |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3934 | if (prealloc == PREALLOC_MODE__MAX) { |
| 3935 | error_report("Invalid preallocation mode '%s'", optarg); |
| 3936 | return 1; |
| 3937 | } |
| 3938 | break; |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3939 | case OPTION_SHRINK: |
| 3940 | shrink = true; |
| 3941 | break; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3942 | } |
| 3943 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 3944 | if (optind != argc - 1) { |
Max Reitz | be8fbd4 | 2018-02-05 17:27:45 +0100 | [diff] [blame] | 3945 | error_exit("Expecting image file name and size"); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3946 | } |
| 3947 | filename = argv[optind++]; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3948 | |
| 3949 | /* Choose grow, shrink, or absolute resize mode */ |
| 3950 | switch (size[0]) { |
| 3951 | case '+': |
| 3952 | relative = 1; |
| 3953 | size++; |
| 3954 | break; |
| 3955 | case '-': |
| 3956 | relative = -1; |
| 3957 | size++; |
| 3958 | break; |
| 3959 | default: |
| 3960 | relative = 0; |
| 3961 | break; |
| 3962 | } |
| 3963 | |
| 3964 | /* Parse size */ |
Peter Crosthwaite | 87ea75d | 2014-01-01 18:49:17 -0800 | [diff] [blame] | 3965 | param = qemu_opts_create(&resize_options, NULL, 0, &error_abort); |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 3966 | if (!qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err)) { |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 3967 | error_report_err(err); |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3968 | ret = -1; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3969 | qemu_opts_del(param); |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3970 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3971 | } |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3972 | n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0); |
| 3973 | qemu_opts_del(param); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3974 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 3975 | blk = img_open(image_opts, filename, fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3976 | BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet, |
| 3977 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3978 | if (!blk) { |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3979 | ret = -1; |
| 3980 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3981 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3982 | |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3983 | current_size = blk_getlength(blk); |
| 3984 | if (current_size < 0) { |
| 3985 | error_report("Failed to inquire current image length: %s", |
| 3986 | strerror(-current_size)); |
| 3987 | ret = -1; |
| 3988 | goto out; |
| 3989 | } |
| 3990 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3991 | if (relative) { |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3992 | total_size = current_size + n * relative; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3993 | } else { |
| 3994 | total_size = n; |
| 3995 | } |
| 3996 | if (total_size <= 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3997 | error_report("New image size must be positive"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3998 | ret = -1; |
| 3999 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4000 | } |
| 4001 | |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 4002 | if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) { |
| 4003 | error_report("Preallocation can only be used for growing images"); |
| 4004 | ret = -1; |
| 4005 | goto out; |
| 4006 | } |
| 4007 | |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 4008 | if (total_size < current_size && !shrink) { |
Kevin Wolf | 1c404d7 | 2020-07-10 14:17:17 +0200 | [diff] [blame] | 4009 | error_report("Use the --shrink option to perform a shrink operation."); |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 4010 | warn_report("Shrinking an image will delete all data beyond the " |
| 4011 | "shrunken image's end. Before performing such an " |
| 4012 | "operation, make sure there is no important data there."); |
Kevin Wolf | 1c404d7 | 2020-07-10 14:17:17 +0200 | [diff] [blame] | 4013 | ret = -1; |
| 4014 | goto out; |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 4015 | } |
| 4016 | |
Max Reitz | e8d04f9 | 2019-09-18 11:51:43 +0200 | [diff] [blame] | 4017 | /* |
| 4018 | * The user expects the image to have the desired size after |
| 4019 | * resizing, so pass @exact=true. It is of no use to report |
| 4020 | * success when the image has not actually been resized. |
| 4021 | */ |
Kevin Wolf | 8c6242b | 2020-04-24 14:54:41 +0200 | [diff] [blame] | 4022 | ret = blk_truncate(blk, total_size, true, prealloc, 0, &err); |
Max Reitz | 09c5c6d | 2019-09-18 11:51:44 +0200 | [diff] [blame] | 4023 | if (!ret) { |
| 4024 | qprintf(quiet, "Image resized.\n"); |
| 4025 | } else { |
Max Reitz | ed3d2ec | 2017-03-28 22:51:27 +0200 | [diff] [blame] | 4026 | error_report_err(err); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4027 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 4028 | out: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 4029 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 4030 | if (ret) { |
| 4031 | return 1; |
| 4032 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4033 | return 0; |
| 4034 | } |
| 4035 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4036 | static void amend_status_cb(BlockDriverState *bs, |
Max Reitz | 8b13976 | 2015-07-27 17:51:32 +0200 | [diff] [blame] | 4037 | int64_t offset, int64_t total_work_size, |
| 4038 | void *opaque) |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4039 | { |
| 4040 | qemu_progress_print(100.f * offset / total_work_size, 0); |
| 4041 | } |
| 4042 | |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4043 | static int print_amend_option_help(const char *format) |
| 4044 | { |
| 4045 | BlockDriver *drv; |
| 4046 | |
| 4047 | /* Find driver and parse its options */ |
| 4048 | drv = bdrv_find_format(format); |
| 4049 | if (!drv) { |
| 4050 | error_report("Unknown file format '%s'", format); |
| 4051 | return 1; |
| 4052 | } |
| 4053 | |
| 4054 | if (!drv->bdrv_amend_options) { |
| 4055 | error_report("Format driver '%s' does not support option amendment", |
| 4056 | format); |
| 4057 | return 1; |
| 4058 | } |
| 4059 | |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4060 | /* Every driver supporting amendment must have amend_opts */ |
| 4061 | assert(drv->amend_opts); |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4062 | |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4063 | printf("Amend options for '%s':\n", format); |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4064 | qemu_opts_print_help(drv->amend_opts, false); |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4065 | return 0; |
| 4066 | } |
| 4067 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4068 | static int img_amend(int argc, char **argv) |
| 4069 | { |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 4070 | Error *err = NULL; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4071 | int c, ret = 0; |
| 4072 | char *options = NULL; |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4073 | QemuOptsList *amend_opts = NULL; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 4074 | QemuOpts *opts = NULL; |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 4075 | const char *fmt = NULL, *filename, *cache; |
| 4076 | int flags; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 4077 | bool writethrough; |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4078 | bool quiet = false, progress = false; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 4079 | BlockBackend *blk = NULL; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4080 | BlockDriverState *bs = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 4081 | bool image_opts = false; |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4082 | bool force = false; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4083 | |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 4084 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4085 | for (;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4086 | static const struct option long_options[] = { |
| 4087 | {"help", no_argument, 0, 'h'}, |
| 4088 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 4089 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4090 | {"force", no_argument, 0, OPTION_FORCE}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4091 | {0, 0, 0, 0} |
| 4092 | }; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4093 | c = getopt_long(argc, argv, ":ho:f:t:pq", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4094 | long_options, NULL); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4095 | if (c == -1) { |
| 4096 | break; |
| 4097 | } |
| 4098 | |
| 4099 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4100 | case ':': |
| 4101 | missing_argument(argv[optind - 1]); |
| 4102 | break; |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4103 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4104 | unrecognized_option(argv[optind - 1]); |
| 4105 | break; |
| 4106 | case 'h': |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4107 | help(); |
| 4108 | break; |
| 4109 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 4110 | if (accumulate_options(&options, optarg) < 0) { |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4111 | ret = -1; |
| 4112 | goto out_no_progress; |
| 4113 | } |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4114 | break; |
| 4115 | case 'f': |
| 4116 | fmt = optarg; |
| 4117 | break; |
| 4118 | case 't': |
| 4119 | cache = optarg; |
| 4120 | break; |
| 4121 | case 'p': |
| 4122 | progress = true; |
| 4123 | break; |
| 4124 | case 'q': |
| 4125 | quiet = true; |
| 4126 | break; |
| 4127 | case OPTION_OBJECT: |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 4128 | user_creatable_process_cmdline(optarg); |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4129 | break; |
| 4130 | case OPTION_IMAGE_OPTS: |
| 4131 | image_opts = true; |
| 4132 | break; |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4133 | case OPTION_FORCE: |
| 4134 | force = true; |
| 4135 | break; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4136 | } |
| 4137 | } |
| 4138 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4139 | if (!options) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 4140 | error_exit("Must specify options (-o)"); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4141 | } |
| 4142 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4143 | if (quiet) { |
| 4144 | progress = false; |
| 4145 | } |
| 4146 | qemu_progress_init(progress, 1.0); |
| 4147 | |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4148 | filename = (optind == argc - 1) ? argv[argc - 1] : NULL; |
| 4149 | if (fmt && has_help_option(options)) { |
| 4150 | /* If a format is explicitly specified (and possibly no filename is |
| 4151 | * given), print option help here */ |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4152 | ret = print_amend_option_help(fmt); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4153 | goto out; |
| 4154 | } |
| 4155 | |
| 4156 | if (optind != argc - 1) { |
Max Reitz | b2f27e4 | 2014-10-27 11:12:52 +0100 | [diff] [blame] | 4157 | error_report("Expecting one image file name"); |
| 4158 | ret = -1; |
| 4159 | goto out; |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4160 | } |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4161 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 4162 | flags = BDRV_O_RDWR; |
| 4163 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 4164 | if (ret < 0) { |
| 4165 | error_report("Invalid cache option: %s", cache); |
| 4166 | goto out; |
| 4167 | } |
| 4168 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4169 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 4170 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 4171 | if (!blk) { |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4172 | ret = -1; |
| 4173 | goto out; |
| 4174 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 4175 | bs = blk_bs(blk); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4176 | |
| 4177 | fmt = bs->drv->format_name; |
| 4178 | |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 4179 | if (has_help_option(options)) { |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4180 | /* If the format was auto-detected, print option help here */ |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4181 | ret = print_amend_option_help(fmt); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4182 | goto out; |
| 4183 | } |
| 4184 | |
Max Reitz | 1f99668 | 2018-05-09 23:00:17 +0200 | [diff] [blame] | 4185 | if (!bs->drv->bdrv_amend_options) { |
| 4186 | error_report("Format driver '%s' does not support option amendment", |
Max Reitz | b2439d2 | 2014-12-02 18:32:47 +0100 | [diff] [blame] | 4187 | fmt); |
| 4188 | ret = -1; |
| 4189 | goto out; |
| 4190 | } |
| 4191 | |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4192 | /* Every driver supporting amendment must have amend_opts */ |
| 4193 | assert(bs->drv->amend_opts); |
Max Reitz | 1f99668 | 2018-05-09 23:00:17 +0200 | [diff] [blame] | 4194 | |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4195 | amend_opts = qemu_opts_append(amend_opts, bs->drv->amend_opts); |
| 4196 | opts = qemu_opts_create(amend_opts, NULL, 0, &error_abort); |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 4197 | if (!qemu_opts_do_parse(opts, options, NULL, &err)) { |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4198 | /* Try to parse options using the create options */ |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4199 | amend_opts = qemu_opts_append(amend_opts, bs->drv->create_opts); |
| 4200 | qemu_opts_del(opts); |
| 4201 | opts = qemu_opts_create(amend_opts, NULL, 0, &error_abort); |
Markus Armbruster | 9e194e0 | 2020-07-07 18:06:11 +0200 | [diff] [blame] | 4202 | if (qemu_opts_do_parse(opts, options, NULL, NULL)) { |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4203 | error_append_hint(&err, |
| 4204 | "This option is only supported for image creation\n"); |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4205 | } |
| 4206 | |
Paolo Bonzini | ece9086 | 2017-01-04 15:56:24 +0100 | [diff] [blame] | 4207 | error_report_err(err); |
| 4208 | ret = -1; |
| 4209 | goto out; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4210 | } |
| 4211 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4212 | /* In case the driver does not call amend_status_cb() */ |
| 4213 | qemu_progress_print(0.f, 0); |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4214 | ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL, force, &err); |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4215 | qemu_progress_print(100.f, 0); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4216 | if (ret < 0) { |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 4217 | error_report_err(err); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4218 | goto out; |
| 4219 | } |
| 4220 | |
| 4221 | out: |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4222 | qemu_progress_end(); |
| 4223 | |
Max Reitz | e814dff | 2015-08-20 16:00:38 -0700 | [diff] [blame] | 4224 | out_no_progress: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 4225 | blk_unref(blk); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 4226 | qemu_opts_del(opts); |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4227 | qemu_opts_free(amend_opts); |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 4228 | g_free(options); |
| 4229 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4230 | if (ret) { |
| 4231 | return 1; |
| 4232 | } |
| 4233 | return 0; |
| 4234 | } |
| 4235 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4236 | typedef struct BenchData { |
| 4237 | BlockBackend *blk; |
| 4238 | uint64_t image_size; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4239 | bool write; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4240 | int bufsize; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4241 | int step; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4242 | int nrreq; |
| 4243 | int n; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4244 | int flush_interval; |
| 4245 | bool drain_on_flush; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4246 | uint8_t *buf; |
| 4247 | QEMUIOVector *qiov; |
| 4248 | |
| 4249 | int in_flight; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4250 | bool in_flush; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4251 | uint64_t offset; |
| 4252 | } BenchData; |
| 4253 | |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4254 | static void bench_undrained_flush_cb(void *opaque, int ret) |
| 4255 | { |
| 4256 | if (ret < 0) { |
Markus Armbruster | df3c286 | 2016-08-03 13:37:51 +0200 | [diff] [blame] | 4257 | error_report("Failed flush request: %s", strerror(-ret)); |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4258 | exit(EXIT_FAILURE); |
| 4259 | } |
| 4260 | } |
| 4261 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4262 | static void bench_cb(void *opaque, int ret) |
| 4263 | { |
| 4264 | BenchData *b = opaque; |
| 4265 | BlockAIOCB *acb; |
| 4266 | |
| 4267 | if (ret < 0) { |
Markus Armbruster | df3c286 | 2016-08-03 13:37:51 +0200 | [diff] [blame] | 4268 | error_report("Failed request: %s", strerror(-ret)); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4269 | exit(EXIT_FAILURE); |
| 4270 | } |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4271 | |
| 4272 | if (b->in_flush) { |
| 4273 | /* Just finished a flush with drained queue: Start next requests */ |
| 4274 | assert(b->in_flight == 0); |
| 4275 | b->in_flush = false; |
| 4276 | } else if (b->in_flight > 0) { |
| 4277 | int remaining = b->n - b->in_flight; |
| 4278 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4279 | b->n--; |
| 4280 | b->in_flight--; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4281 | |
| 4282 | /* Time for flush? Drain queue if requested, then flush */ |
| 4283 | if (b->flush_interval && remaining % b->flush_interval == 0) { |
| 4284 | if (!b->in_flight || !b->drain_on_flush) { |
| 4285 | BlockCompletionFunc *cb; |
| 4286 | |
| 4287 | if (b->drain_on_flush) { |
| 4288 | b->in_flush = true; |
| 4289 | cb = bench_cb; |
| 4290 | } else { |
| 4291 | cb = bench_undrained_flush_cb; |
| 4292 | } |
| 4293 | |
| 4294 | acb = blk_aio_flush(b->blk, cb, b); |
| 4295 | if (!acb) { |
| 4296 | error_report("Failed to issue flush request"); |
| 4297 | exit(EXIT_FAILURE); |
| 4298 | } |
| 4299 | } |
| 4300 | if (b->drain_on_flush) { |
| 4301 | return; |
| 4302 | } |
| 4303 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4304 | } |
| 4305 | |
| 4306 | while (b->n > b->in_flight && b->in_flight < b->nrreq) { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4307 | int64_t offset = b->offset; |
| 4308 | /* blk_aio_* might look for completed I/Os and kick bench_cb |
| 4309 | * again, so make sure this operation is counted by in_flight |
| 4310 | * and b->offset is ready for the next submission. |
| 4311 | */ |
| 4312 | b->in_flight++; |
| 4313 | b->offset += b->step; |
| 4314 | b->offset %= b->image_size; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4315 | if (b->write) { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4316 | 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] | 4317 | } else { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4318 | 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] | 4319 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4320 | if (!acb) { |
| 4321 | error_report("Failed to issue request"); |
| 4322 | exit(EXIT_FAILURE); |
| 4323 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4324 | } |
| 4325 | } |
| 4326 | |
| 4327 | static int img_bench(int argc, char **argv) |
| 4328 | { |
| 4329 | int c, ret = 0; |
| 4330 | const char *fmt = NULL, *filename; |
| 4331 | bool quiet = false; |
| 4332 | bool image_opts = false; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4333 | bool is_write = false; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4334 | int count = 75000; |
| 4335 | int depth = 64; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4336 | int64_t offset = 0; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4337 | size_t bufsize = 4096; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4338 | int pattern = 0; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4339 | size_t step = 0; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4340 | int flush_interval = 0; |
| 4341 | bool drain_on_flush = true; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4342 | int64_t image_size; |
| 4343 | BlockBackend *blk = NULL; |
| 4344 | BenchData data = {}; |
| 4345 | int flags = 0; |
Kevin Wolf | 604e861 | 2016-06-14 11:29:32 +0200 | [diff] [blame] | 4346 | bool writethrough = false; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4347 | struct timeval t1, t2; |
| 4348 | int i; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4349 | bool force_share = false; |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4350 | size_t buf_size; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4351 | |
| 4352 | for (;;) { |
| 4353 | static const struct option long_options[] = { |
| 4354 | {"help", no_argument, 0, 'h'}, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4355 | {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL}, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4356 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4357 | {"pattern", required_argument, 0, OPTION_PATTERN}, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4358 | {"no-drain", no_argument, 0, OPTION_NO_DRAIN}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4359 | {"force-share", no_argument, 0, 'U'}, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4360 | {0, 0, 0, 0} |
| 4361 | }; |
Aarushi Mehta | cdd2677 | 2020-01-20 14:18:55 +0000 | [diff] [blame] | 4362 | c = getopt_long(argc, argv, ":hc:d:f:ni:o:qs:S:t:wU", long_options, |
| 4363 | NULL); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4364 | if (c == -1) { |
| 4365 | break; |
| 4366 | } |
| 4367 | |
| 4368 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4369 | case ':': |
| 4370 | missing_argument(argv[optind - 1]); |
| 4371 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4372 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4373 | unrecognized_option(argv[optind - 1]); |
| 4374 | break; |
| 4375 | case 'h': |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4376 | help(); |
| 4377 | break; |
| 4378 | case 'c': |
| 4379 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4380 | unsigned long res; |
| 4381 | |
| 4382 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4383 | error_report("Invalid request count specified"); |
| 4384 | return 1; |
| 4385 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4386 | count = res; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4387 | break; |
| 4388 | } |
| 4389 | case 'd': |
| 4390 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4391 | unsigned long res; |
| 4392 | |
| 4393 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4394 | error_report("Invalid queue depth specified"); |
| 4395 | return 1; |
| 4396 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4397 | depth = res; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4398 | break; |
| 4399 | } |
| 4400 | case 'f': |
| 4401 | fmt = optarg; |
| 4402 | break; |
| 4403 | case 'n': |
| 4404 | flags |= BDRV_O_NATIVE_AIO; |
| 4405 | break; |
Aarushi Mehta | cdd2677 | 2020-01-20 14:18:55 +0000 | [diff] [blame] | 4406 | case 'i': |
| 4407 | ret = bdrv_parse_aio(optarg, &flags); |
| 4408 | if (ret < 0) { |
| 4409 | error_report("Invalid aio option: %s", optarg); |
| 4410 | ret = -1; |
| 4411 | goto out; |
| 4412 | } |
| 4413 | break; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4414 | case 'o': |
| 4415 | { |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4416 | offset = cvtnum("offset", optarg); |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4417 | if (offset < 0) { |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4418 | return 1; |
| 4419 | } |
| 4420 | break; |
| 4421 | } |
| 4422 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4423 | case 'q': |
| 4424 | quiet = true; |
| 4425 | break; |
| 4426 | case 's': |
| 4427 | { |
| 4428 | int64_t sval; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4429 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4430 | sval = cvtnum_full("buffer size", optarg, 0, INT_MAX); |
| 4431 | if (sval < 0) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4432 | return 1; |
| 4433 | } |
| 4434 | |
| 4435 | bufsize = sval; |
| 4436 | break; |
| 4437 | } |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4438 | case 'S': |
| 4439 | { |
| 4440 | int64_t sval; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4441 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4442 | sval = cvtnum_full("step_size", optarg, 0, INT_MAX); |
| 4443 | if (sval < 0) { |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4444 | return 1; |
| 4445 | } |
| 4446 | |
| 4447 | step = sval; |
| 4448 | break; |
| 4449 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4450 | case 't': |
| 4451 | ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough); |
| 4452 | if (ret < 0) { |
| 4453 | error_report("Invalid cache mode"); |
| 4454 | ret = -1; |
| 4455 | goto out; |
| 4456 | } |
| 4457 | break; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4458 | case 'w': |
| 4459 | flags |= BDRV_O_RDWR; |
| 4460 | is_write = true; |
| 4461 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4462 | case 'U': |
| 4463 | force_share = true; |
| 4464 | break; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4465 | case OPTION_PATTERN: |
| 4466 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4467 | unsigned long res; |
| 4468 | |
| 4469 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) { |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4470 | error_report("Invalid pattern byte specified"); |
| 4471 | return 1; |
| 4472 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4473 | pattern = res; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4474 | break; |
| 4475 | } |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4476 | case OPTION_FLUSH_INTERVAL: |
| 4477 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4478 | unsigned long res; |
| 4479 | |
| 4480 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4481 | error_report("Invalid flush interval specified"); |
| 4482 | return 1; |
| 4483 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4484 | flush_interval = res; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4485 | break; |
| 4486 | } |
| 4487 | case OPTION_NO_DRAIN: |
| 4488 | drain_on_flush = false; |
| 4489 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4490 | case OPTION_IMAGE_OPTS: |
| 4491 | image_opts = true; |
| 4492 | break; |
| 4493 | } |
| 4494 | } |
| 4495 | |
| 4496 | if (optind != argc - 1) { |
| 4497 | error_exit("Expecting one image file name"); |
| 4498 | } |
| 4499 | filename = argv[argc - 1]; |
| 4500 | |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4501 | if (!is_write && flush_interval) { |
| 4502 | error_report("--flush-interval is only available in write tests"); |
| 4503 | ret = -1; |
| 4504 | goto out; |
| 4505 | } |
| 4506 | if (flush_interval && flush_interval < depth) { |
| 4507 | error_report("Flush interval can't be smaller than depth"); |
| 4508 | ret = -1; |
| 4509 | goto out; |
| 4510 | } |
| 4511 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4512 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 4513 | force_share); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4514 | if (!blk) { |
| 4515 | ret = -1; |
| 4516 | goto out; |
| 4517 | } |
| 4518 | |
| 4519 | image_size = blk_getlength(blk); |
| 4520 | if (image_size < 0) { |
| 4521 | ret = image_size; |
| 4522 | goto out; |
| 4523 | } |
| 4524 | |
| 4525 | data = (BenchData) { |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4526 | .blk = blk, |
| 4527 | .image_size = image_size, |
| 4528 | .bufsize = bufsize, |
| 4529 | .step = step ?: bufsize, |
| 4530 | .nrreq = depth, |
| 4531 | .n = count, |
| 4532 | .offset = offset, |
| 4533 | .write = is_write, |
| 4534 | .flush_interval = flush_interval, |
| 4535 | .drain_on_flush = drain_on_flush, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4536 | }; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4537 | printf("Sending %d %s requests, %d bytes each, %d in parallel " |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4538 | "(starting at offset %" PRId64 ", step size %d)\n", |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4539 | data.n, data.write ? "write" : "read", data.bufsize, data.nrreq, |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4540 | data.offset, data.step); |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4541 | if (flush_interval) { |
| 4542 | printf("Sending flush every %d requests\n", flush_interval); |
| 4543 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4544 | |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4545 | buf_size = data.nrreq * data.bufsize; |
| 4546 | data.buf = blk_blockalign(blk, buf_size); |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4547 | memset(data.buf, pattern, data.nrreq * data.bufsize); |
| 4548 | |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4549 | blk_register_buf(blk, data.buf, buf_size); |
| 4550 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4551 | data.qiov = g_new(QEMUIOVector, data.nrreq); |
| 4552 | for (i = 0; i < data.nrreq; i++) { |
| 4553 | qemu_iovec_init(&data.qiov[i], 1); |
| 4554 | qemu_iovec_add(&data.qiov[i], |
| 4555 | data.buf + i * data.bufsize, data.bufsize); |
| 4556 | } |
| 4557 | |
| 4558 | gettimeofday(&t1, NULL); |
| 4559 | bench_cb(&data, 0); |
| 4560 | |
| 4561 | while (data.n > 0) { |
| 4562 | main_loop_wait(false); |
| 4563 | } |
| 4564 | gettimeofday(&t2, NULL); |
| 4565 | |
| 4566 | printf("Run completed in %3.3f seconds.\n", |
| 4567 | (t2.tv_sec - t1.tv_sec) |
| 4568 | + ((double)(t2.tv_usec - t1.tv_usec) / 1000000)); |
| 4569 | |
| 4570 | out: |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4571 | if (data.buf) { |
| 4572 | blk_unregister_buf(blk, data.buf); |
| 4573 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4574 | qemu_vfree(data.buf); |
| 4575 | blk_unref(blk); |
| 4576 | |
| 4577 | if (ret) { |
| 4578 | return 1; |
| 4579 | } |
| 4580 | return 0; |
| 4581 | } |
| 4582 | |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4583 | enum ImgBitmapAct { |
| 4584 | BITMAP_ADD, |
| 4585 | BITMAP_REMOVE, |
| 4586 | BITMAP_CLEAR, |
| 4587 | BITMAP_ENABLE, |
| 4588 | BITMAP_DISABLE, |
| 4589 | BITMAP_MERGE, |
| 4590 | }; |
| 4591 | typedef struct ImgBitmapAction { |
| 4592 | enum ImgBitmapAct act; |
| 4593 | const char *src; /* only used for merge */ |
| 4594 | QSIMPLEQ_ENTRY(ImgBitmapAction) next; |
| 4595 | } ImgBitmapAction; |
| 4596 | |
| 4597 | static int img_bitmap(int argc, char **argv) |
| 4598 | { |
| 4599 | Error *err = NULL; |
| 4600 | int c, ret = 1; |
| 4601 | QemuOpts *opts = NULL; |
| 4602 | const char *fmt = NULL, *src_fmt = NULL, *src_filename = NULL; |
| 4603 | const char *filename, *bitmap; |
| 4604 | BlockBackend *blk = NULL, *src = NULL; |
| 4605 | BlockDriverState *bs = NULL, *src_bs = NULL; |
| 4606 | bool image_opts = false; |
| 4607 | int64_t granularity = 0; |
| 4608 | bool add = false, merge = false; |
| 4609 | QSIMPLEQ_HEAD(, ImgBitmapAction) actions; |
| 4610 | ImgBitmapAction *act, *act_next; |
| 4611 | const char *op; |
| 4612 | |
| 4613 | QSIMPLEQ_INIT(&actions); |
| 4614 | |
| 4615 | for (;;) { |
| 4616 | static const struct option long_options[] = { |
| 4617 | {"help", no_argument, 0, 'h'}, |
| 4618 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 4619 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
| 4620 | {"add", no_argument, 0, OPTION_ADD}, |
| 4621 | {"remove", no_argument, 0, OPTION_REMOVE}, |
| 4622 | {"clear", no_argument, 0, OPTION_CLEAR}, |
| 4623 | {"enable", no_argument, 0, OPTION_ENABLE}, |
| 4624 | {"disable", no_argument, 0, OPTION_DISABLE}, |
| 4625 | {"merge", required_argument, 0, OPTION_MERGE}, |
| 4626 | {"granularity", required_argument, 0, 'g'}, |
| 4627 | {"source-file", required_argument, 0, 'b'}, |
| 4628 | {"source-format", required_argument, 0, 'F'}, |
| 4629 | {0, 0, 0, 0} |
| 4630 | }; |
| 4631 | c = getopt_long(argc, argv, ":b:f:F:g:h", long_options, NULL); |
| 4632 | if (c == -1) { |
| 4633 | break; |
| 4634 | } |
| 4635 | |
| 4636 | switch (c) { |
| 4637 | case ':': |
| 4638 | missing_argument(argv[optind - 1]); |
| 4639 | break; |
| 4640 | case '?': |
| 4641 | unrecognized_option(argv[optind - 1]); |
| 4642 | break; |
| 4643 | case 'h': |
| 4644 | help(); |
| 4645 | break; |
| 4646 | case 'b': |
| 4647 | src_filename = optarg; |
| 4648 | break; |
| 4649 | case 'f': |
| 4650 | fmt = optarg; |
| 4651 | break; |
| 4652 | case 'F': |
| 4653 | src_fmt = optarg; |
| 4654 | break; |
| 4655 | case 'g': |
| 4656 | granularity = cvtnum("granularity", optarg); |
| 4657 | if (granularity < 0) { |
| 4658 | return 1; |
| 4659 | } |
| 4660 | break; |
| 4661 | case OPTION_ADD: |
| 4662 | act = g_new0(ImgBitmapAction, 1); |
| 4663 | act->act = BITMAP_ADD; |
| 4664 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4665 | add = true; |
| 4666 | break; |
| 4667 | case OPTION_REMOVE: |
| 4668 | act = g_new0(ImgBitmapAction, 1); |
| 4669 | act->act = BITMAP_REMOVE; |
| 4670 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4671 | break; |
| 4672 | case OPTION_CLEAR: |
| 4673 | act = g_new0(ImgBitmapAction, 1); |
| 4674 | act->act = BITMAP_CLEAR; |
| 4675 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4676 | break; |
| 4677 | case OPTION_ENABLE: |
| 4678 | act = g_new0(ImgBitmapAction, 1); |
| 4679 | act->act = BITMAP_ENABLE; |
| 4680 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4681 | break; |
| 4682 | case OPTION_DISABLE: |
| 4683 | act = g_new0(ImgBitmapAction, 1); |
| 4684 | act->act = BITMAP_DISABLE; |
| 4685 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4686 | break; |
| 4687 | case OPTION_MERGE: |
| 4688 | act = g_new0(ImgBitmapAction, 1); |
| 4689 | act->act = BITMAP_MERGE; |
| 4690 | act->src = optarg; |
| 4691 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4692 | merge = true; |
| 4693 | break; |
| 4694 | case OPTION_OBJECT: |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 4695 | user_creatable_process_cmdline(optarg); |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4696 | break; |
| 4697 | case OPTION_IMAGE_OPTS: |
| 4698 | image_opts = true; |
| 4699 | break; |
| 4700 | } |
| 4701 | } |
| 4702 | |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4703 | if (QSIMPLEQ_EMPTY(&actions)) { |
| 4704 | error_report("Need at least one of --add, --remove, --clear, " |
| 4705 | "--enable, --disable, or --merge"); |
| 4706 | goto out; |
| 4707 | } |
| 4708 | |
| 4709 | if (granularity && !add) { |
| 4710 | error_report("granularity only supported with --add"); |
| 4711 | goto out; |
| 4712 | } |
| 4713 | if (src_fmt && !src_filename) { |
| 4714 | error_report("-F only supported with -b"); |
| 4715 | goto out; |
| 4716 | } |
| 4717 | if (src_filename && !merge) { |
| 4718 | error_report("Merge bitmap source file only supported with " |
| 4719 | "--merge"); |
| 4720 | goto out; |
| 4721 | } |
| 4722 | |
| 4723 | if (optind != argc - 2) { |
| 4724 | error_report("Expecting filename and bitmap name"); |
| 4725 | goto out; |
| 4726 | } |
| 4727 | |
| 4728 | filename = argv[optind]; |
| 4729 | bitmap = argv[optind + 1]; |
| 4730 | |
Eric Blake | 14f16bf | 2020-09-14 14:10:09 -0500 | [diff] [blame] | 4731 | /* |
| 4732 | * No need to open backing chains; we will be manipulating bitmaps |
| 4733 | * directly in this image without reference to image contents. |
| 4734 | */ |
| 4735 | blk = img_open(image_opts, filename, fmt, BDRV_O_RDWR | BDRV_O_NO_BACKING, |
| 4736 | false, false, false); |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4737 | if (!blk) { |
| 4738 | goto out; |
| 4739 | } |
| 4740 | bs = blk_bs(blk); |
| 4741 | if (src_filename) { |
Eric Blake | 14f16bf | 2020-09-14 14:10:09 -0500 | [diff] [blame] | 4742 | src = img_open(false, src_filename, src_fmt, BDRV_O_NO_BACKING, |
| 4743 | false, false, false); |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4744 | if (!src) { |
| 4745 | goto out; |
| 4746 | } |
| 4747 | src_bs = blk_bs(src); |
| 4748 | } else { |
| 4749 | src_bs = bs; |
| 4750 | } |
| 4751 | |
| 4752 | QSIMPLEQ_FOREACH_SAFE(act, &actions, next, act_next) { |
| 4753 | switch (act->act) { |
| 4754 | case BITMAP_ADD: |
| 4755 | qmp_block_dirty_bitmap_add(bs->node_name, bitmap, |
| 4756 | !!granularity, granularity, true, true, |
| 4757 | false, false, &err); |
| 4758 | op = "add"; |
| 4759 | break; |
| 4760 | case BITMAP_REMOVE: |
| 4761 | qmp_block_dirty_bitmap_remove(bs->node_name, bitmap, &err); |
| 4762 | op = "remove"; |
| 4763 | break; |
| 4764 | case BITMAP_CLEAR: |
| 4765 | qmp_block_dirty_bitmap_clear(bs->node_name, bitmap, &err); |
| 4766 | op = "clear"; |
| 4767 | break; |
| 4768 | case BITMAP_ENABLE: |
| 4769 | qmp_block_dirty_bitmap_enable(bs->node_name, bitmap, &err); |
| 4770 | op = "enable"; |
| 4771 | break; |
| 4772 | case BITMAP_DISABLE: |
| 4773 | qmp_block_dirty_bitmap_disable(bs->node_name, bitmap, &err); |
| 4774 | op = "disable"; |
| 4775 | break; |
Eric Blake | 6c729dd | 2020-05-21 14:21:35 -0500 | [diff] [blame] | 4776 | case BITMAP_MERGE: |
| 4777 | do_dirty_bitmap_merge(bs->node_name, bitmap, src_bs->node_name, |
| 4778 | act->src, &err); |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4779 | op = "merge"; |
| 4780 | break; |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4781 | default: |
| 4782 | g_assert_not_reached(); |
| 4783 | } |
| 4784 | |
| 4785 | if (err) { |
| 4786 | error_reportf_err(err, "Operation %s on bitmap %s failed: ", |
| 4787 | op, bitmap); |
| 4788 | goto out; |
| 4789 | } |
| 4790 | g_free(act); |
| 4791 | } |
| 4792 | |
| 4793 | ret = 0; |
| 4794 | |
| 4795 | out: |
| 4796 | blk_unref(src); |
| 4797 | blk_unref(blk); |
| 4798 | qemu_opts_del(opts); |
| 4799 | return ret; |
| 4800 | } |
| 4801 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4802 | #define C_BS 01 |
| 4803 | #define C_COUNT 02 |
| 4804 | #define C_IF 04 |
| 4805 | #define C_OF 010 |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4806 | #define C_SKIP 020 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4807 | |
| 4808 | struct DdInfo { |
| 4809 | unsigned int flags; |
| 4810 | int64_t count; |
| 4811 | }; |
| 4812 | |
| 4813 | struct DdIo { |
| 4814 | int bsz; /* Block size */ |
| 4815 | char *filename; |
| 4816 | uint8_t *buf; |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4817 | int64_t offset; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4818 | }; |
| 4819 | |
| 4820 | struct DdOpts { |
| 4821 | const char *name; |
| 4822 | int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *); |
| 4823 | unsigned int flag; |
| 4824 | }; |
| 4825 | |
| 4826 | static int img_dd_bs(const char *arg, |
| 4827 | struct DdIo *in, struct DdIo *out, |
| 4828 | struct DdInfo *dd) |
| 4829 | { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4830 | int64_t res; |
| 4831 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4832 | res = cvtnum_full("bs", arg, 1, INT_MAX); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4833 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4834 | if (res < 0) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4835 | return 1; |
| 4836 | } |
| 4837 | in->bsz = out->bsz = res; |
| 4838 | |
| 4839 | return 0; |
| 4840 | } |
| 4841 | |
| 4842 | static int img_dd_count(const char *arg, |
| 4843 | struct DdIo *in, struct DdIo *out, |
| 4844 | struct DdInfo *dd) |
| 4845 | { |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4846 | dd->count = cvtnum("count", arg); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4847 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4848 | if (dd->count < 0) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4849 | return 1; |
| 4850 | } |
| 4851 | |
| 4852 | return 0; |
| 4853 | } |
| 4854 | |
| 4855 | static int img_dd_if(const char *arg, |
| 4856 | struct DdIo *in, struct DdIo *out, |
| 4857 | struct DdInfo *dd) |
| 4858 | { |
| 4859 | in->filename = g_strdup(arg); |
| 4860 | |
| 4861 | return 0; |
| 4862 | } |
| 4863 | |
| 4864 | static int img_dd_of(const char *arg, |
| 4865 | struct DdIo *in, struct DdIo *out, |
| 4866 | struct DdInfo *dd) |
| 4867 | { |
| 4868 | out->filename = g_strdup(arg); |
| 4869 | |
| 4870 | return 0; |
| 4871 | } |
| 4872 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4873 | static int img_dd_skip(const char *arg, |
| 4874 | struct DdIo *in, struct DdIo *out, |
| 4875 | struct DdInfo *dd) |
| 4876 | { |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4877 | in->offset = cvtnum("skip", arg); |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4878 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4879 | if (in->offset < 0) { |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4880 | return 1; |
| 4881 | } |
| 4882 | |
| 4883 | return 0; |
| 4884 | } |
| 4885 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4886 | static int img_dd(int argc, char **argv) |
| 4887 | { |
| 4888 | int ret = 0; |
| 4889 | char *arg = NULL; |
| 4890 | char *tmp; |
| 4891 | BlockDriver *drv = NULL, *proto_drv = NULL; |
| 4892 | BlockBackend *blk1 = NULL, *blk2 = NULL; |
| 4893 | QemuOpts *opts = NULL; |
| 4894 | QemuOptsList *create_opts = NULL; |
| 4895 | Error *local_err = NULL; |
| 4896 | bool image_opts = false; |
| 4897 | int c, i; |
| 4898 | const char *out_fmt = "raw"; |
| 4899 | const char *fmt = NULL; |
| 4900 | int64_t size = 0; |
| 4901 | int64_t block_count = 0, out_pos, in_pos; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4902 | bool force_share = false; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4903 | struct DdInfo dd = { |
| 4904 | .flags = 0, |
| 4905 | .count = 0, |
| 4906 | }; |
| 4907 | struct DdIo in = { |
| 4908 | .bsz = 512, /* Block size is by default 512 bytes */ |
| 4909 | .filename = NULL, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4910 | .buf = NULL, |
| 4911 | .offset = 0 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4912 | }; |
| 4913 | struct DdIo out = { |
| 4914 | .bsz = 512, |
| 4915 | .filename = NULL, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4916 | .buf = NULL, |
| 4917 | .offset = 0 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4918 | }; |
| 4919 | |
| 4920 | const struct DdOpts options[] = { |
| 4921 | { "bs", img_dd_bs, C_BS }, |
| 4922 | { "count", img_dd_count, C_COUNT }, |
| 4923 | { "if", img_dd_if, C_IF }, |
| 4924 | { "of", img_dd_of, C_OF }, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4925 | { "skip", img_dd_skip, C_SKIP }, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4926 | { NULL, NULL, 0 } |
| 4927 | }; |
| 4928 | const struct option long_options[] = { |
| 4929 | { "help", no_argument, 0, 'h'}, |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 4930 | { "object", required_argument, 0, OPTION_OBJECT}, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4931 | { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4932 | { "force-share", no_argument, 0, 'U'}, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4933 | { 0, 0, 0, 0 } |
| 4934 | }; |
| 4935 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4936 | while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4937 | if (c == EOF) { |
| 4938 | break; |
| 4939 | } |
| 4940 | switch (c) { |
| 4941 | case 'O': |
| 4942 | out_fmt = optarg; |
| 4943 | break; |
| 4944 | case 'f': |
| 4945 | fmt = optarg; |
| 4946 | break; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4947 | case ':': |
| 4948 | missing_argument(argv[optind - 1]); |
| 4949 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4950 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4951 | unrecognized_option(argv[optind - 1]); |
| 4952 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4953 | case 'h': |
| 4954 | help(); |
| 4955 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4956 | case 'U': |
| 4957 | force_share = true; |
| 4958 | break; |
Stefan Hajnoczi | 2a24570 | 2017-06-19 16:00:02 +0100 | [diff] [blame] | 4959 | case OPTION_OBJECT: |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 4960 | user_creatable_process_cmdline(optarg); |
Stefan Hajnoczi | 2a24570 | 2017-06-19 16:00:02 +0100 | [diff] [blame] | 4961 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4962 | case OPTION_IMAGE_OPTS: |
| 4963 | image_opts = true; |
| 4964 | break; |
| 4965 | } |
| 4966 | } |
| 4967 | |
| 4968 | for (i = optind; i < argc; i++) { |
| 4969 | int j; |
| 4970 | arg = g_strdup(argv[i]); |
| 4971 | |
| 4972 | tmp = strchr(arg, '='); |
| 4973 | if (tmp == NULL) { |
| 4974 | error_report("unrecognized operand %s", arg); |
| 4975 | ret = -1; |
| 4976 | goto out; |
| 4977 | } |
| 4978 | |
| 4979 | *tmp++ = '\0'; |
| 4980 | |
| 4981 | for (j = 0; options[j].name != NULL; j++) { |
| 4982 | if (!strcmp(arg, options[j].name)) { |
| 4983 | break; |
| 4984 | } |
| 4985 | } |
| 4986 | if (options[j].name == NULL) { |
| 4987 | error_report("unrecognized operand %s", arg); |
| 4988 | ret = -1; |
| 4989 | goto out; |
| 4990 | } |
| 4991 | |
| 4992 | if (options[j].f(tmp, &in, &out, &dd) != 0) { |
| 4993 | ret = -1; |
| 4994 | goto out; |
| 4995 | } |
| 4996 | dd.flags |= options[j].flag; |
| 4997 | g_free(arg); |
| 4998 | arg = NULL; |
| 4999 | } |
| 5000 | |
| 5001 | if (!(dd.flags & C_IF && dd.flags & C_OF)) { |
| 5002 | error_report("Must specify both input and output files"); |
| 5003 | ret = -1; |
| 5004 | goto out; |
| 5005 | } |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 5006 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 5007 | blk1 = img_open(image_opts, in.filename, fmt, 0, false, false, |
| 5008 | force_share); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5009 | |
| 5010 | if (!blk1) { |
| 5011 | ret = -1; |
| 5012 | goto out; |
| 5013 | } |
| 5014 | |
| 5015 | drv = bdrv_find_format(out_fmt); |
| 5016 | if (!drv) { |
| 5017 | error_report("Unknown file format"); |
| 5018 | ret = -1; |
| 5019 | goto out; |
| 5020 | } |
| 5021 | proto_drv = bdrv_find_protocol(out.filename, true, &local_err); |
| 5022 | |
| 5023 | if (!proto_drv) { |
| 5024 | error_report_err(local_err); |
| 5025 | ret = -1; |
| 5026 | goto out; |
| 5027 | } |
| 5028 | if (!drv->create_opts) { |
| 5029 | error_report("Format driver '%s' does not support image creation", |
| 5030 | drv->format_name); |
| 5031 | ret = -1; |
| 5032 | goto out; |
| 5033 | } |
| 5034 | if (!proto_drv->create_opts) { |
| 5035 | error_report("Protocol driver '%s' does not support image creation", |
| 5036 | proto_drv->format_name); |
| 5037 | ret = -1; |
| 5038 | goto out; |
| 5039 | } |
| 5040 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 5041 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
| 5042 | |
| 5043 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
| 5044 | |
| 5045 | size = blk_getlength(blk1); |
| 5046 | if (size < 0) { |
| 5047 | error_report("Failed to get size for '%s'", in.filename); |
| 5048 | ret = -1; |
| 5049 | goto out; |
| 5050 | } |
| 5051 | |
| 5052 | if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz && |
| 5053 | dd.count * in.bsz < size) { |
| 5054 | size = dd.count * in.bsz; |
| 5055 | } |
| 5056 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 5057 | /* Overflow means the specified offset is beyond input image's size */ |
| 5058 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || |
| 5059 | size < in.bsz * in.offset)) { |
| 5060 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort); |
| 5061 | } else { |
| 5062 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, |
| 5063 | size - in.bsz * in.offset, &error_abort); |
| 5064 | } |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5065 | |
| 5066 | ret = bdrv_create(drv, out.filename, opts, &local_err); |
| 5067 | if (ret < 0) { |
| 5068 | error_reportf_err(local_err, |
| 5069 | "%s: error while creating output image: ", |
| 5070 | out.filename); |
| 5071 | ret = -1; |
| 5072 | goto out; |
| 5073 | } |
| 5074 | |
Daniel P. Berrange | ea204dd | 2017-05-15 17:47:10 +0100 | [diff] [blame] | 5075 | /* TODO, we can't honour --image-opts for the target, |
| 5076 | * since it needs to be given in a format compatible |
| 5077 | * with the bdrv_create() call above which does not |
| 5078 | * support image-opts style. |
| 5079 | */ |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 5080 | 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] | 5081 | false, false, false); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5082 | |
| 5083 | if (!blk2) { |
| 5084 | ret = -1; |
| 5085 | goto out; |
| 5086 | } |
| 5087 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 5088 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || |
| 5089 | size < in.offset * in.bsz)) { |
| 5090 | /* We give a warning if the skip option is bigger than the input |
| 5091 | * size and create an empty output disk image (i.e. like dd(1)). |
| 5092 | */ |
| 5093 | error_report("%s: cannot skip to specified offset", in.filename); |
| 5094 | in_pos = size; |
| 5095 | } else { |
| 5096 | in_pos = in.offset * in.bsz; |
| 5097 | } |
| 5098 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5099 | in.buf = g_new(uint8_t, in.bsz); |
| 5100 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 5101 | for (out_pos = 0; in_pos < size; block_count++) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5102 | int in_ret, out_ret; |
| 5103 | |
| 5104 | if (in_pos + in.bsz > size) { |
| 5105 | in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos); |
| 5106 | } else { |
| 5107 | in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz); |
| 5108 | } |
| 5109 | if (in_ret < 0) { |
| 5110 | error_report("error while reading from input image file: %s", |
| 5111 | strerror(-in_ret)); |
| 5112 | ret = -1; |
| 5113 | goto out; |
| 5114 | } |
| 5115 | in_pos += in_ret; |
| 5116 | |
| 5117 | out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0); |
| 5118 | |
| 5119 | if (out_ret < 0) { |
| 5120 | error_report("error while writing to output image file: %s", |
| 5121 | strerror(-out_ret)); |
| 5122 | ret = -1; |
| 5123 | goto out; |
| 5124 | } |
| 5125 | out_pos += out_ret; |
| 5126 | } |
| 5127 | |
| 5128 | out: |
| 5129 | g_free(arg); |
| 5130 | qemu_opts_del(opts); |
| 5131 | qemu_opts_free(create_opts); |
| 5132 | blk_unref(blk1); |
| 5133 | blk_unref(blk2); |
| 5134 | g_free(in.filename); |
| 5135 | g_free(out.filename); |
| 5136 | g_free(in.buf); |
| 5137 | g_free(out.buf); |
| 5138 | |
| 5139 | if (ret) { |
| 5140 | return 1; |
| 5141 | } |
| 5142 | return 0; |
| 5143 | } |
| 5144 | |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5145 | static void dump_json_block_measure_info(BlockMeasureInfo *info) |
| 5146 | { |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 5147 | GString *str; |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5148 | QObject *obj; |
| 5149 | Visitor *v = qobject_output_visitor_new(&obj); |
| 5150 | |
| 5151 | visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort); |
| 5152 | visit_complete(v, &obj); |
Markus Armbruster | 6589f45 | 2020-12-11 18:11:35 +0100 | [diff] [blame] | 5153 | str = qobject_to_json_pretty(obj, true); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5154 | assert(str != NULL); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 5155 | printf("%s\n", str->str); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 5156 | qobject_unref(obj); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5157 | visit_free(v); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 5158 | g_string_free(str, true); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5159 | } |
| 5160 | |
| 5161 | static int img_measure(int argc, char **argv) |
| 5162 | { |
| 5163 | static const struct option long_options[] = { |
| 5164 | {"help", no_argument, 0, 'h'}, |
| 5165 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
| 5166 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 5167 | {"output", required_argument, 0, OPTION_OUTPUT}, |
| 5168 | {"size", required_argument, 0, OPTION_SIZE}, |
| 5169 | {"force-share", no_argument, 0, 'U'}, |
| 5170 | {0, 0, 0, 0} |
| 5171 | }; |
| 5172 | OutputFormat output_format = OFORMAT_HUMAN; |
| 5173 | BlockBackend *in_blk = NULL; |
| 5174 | BlockDriver *drv; |
| 5175 | const char *filename = NULL; |
| 5176 | const char *fmt = NULL; |
| 5177 | const char *out_fmt = "raw"; |
| 5178 | char *options = NULL; |
| 5179 | char *snapshot_name = NULL; |
| 5180 | bool force_share = false; |
| 5181 | QemuOpts *opts = NULL; |
| 5182 | QemuOpts *object_opts = NULL; |
| 5183 | QemuOpts *sn_opts = NULL; |
| 5184 | QemuOptsList *create_opts = NULL; |
| 5185 | bool image_opts = false; |
| 5186 | uint64_t img_size = UINT64_MAX; |
| 5187 | BlockMeasureInfo *info = NULL; |
| 5188 | Error *local_err = NULL; |
| 5189 | int ret = 1; |
| 5190 | int c; |
| 5191 | |
| 5192 | while ((c = getopt_long(argc, argv, "hf:O:o:l:U", |
| 5193 | long_options, NULL)) != -1) { |
| 5194 | switch (c) { |
| 5195 | case '?': |
| 5196 | case 'h': |
| 5197 | help(); |
| 5198 | break; |
| 5199 | case 'f': |
| 5200 | fmt = optarg; |
| 5201 | break; |
| 5202 | case 'O': |
| 5203 | out_fmt = optarg; |
| 5204 | break; |
| 5205 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 5206 | if (accumulate_options(&options, optarg) < 0) { |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5207 | goto out; |
| 5208 | } |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5209 | break; |
| 5210 | case 'l': |
| 5211 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { |
| 5212 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
| 5213 | optarg, false); |
| 5214 | if (!sn_opts) { |
| 5215 | error_report("Failed in parsing snapshot param '%s'", |
| 5216 | optarg); |
| 5217 | goto out; |
| 5218 | } |
| 5219 | } else { |
| 5220 | snapshot_name = optarg; |
| 5221 | } |
| 5222 | break; |
| 5223 | case 'U': |
| 5224 | force_share = true; |
| 5225 | break; |
| 5226 | case OPTION_OBJECT: |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 5227 | user_creatable_process_cmdline(optarg); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5228 | break; |
| 5229 | case OPTION_IMAGE_OPTS: |
| 5230 | image_opts = true; |
| 5231 | break; |
| 5232 | case OPTION_OUTPUT: |
| 5233 | if (!strcmp(optarg, "json")) { |
| 5234 | output_format = OFORMAT_JSON; |
| 5235 | } else if (!strcmp(optarg, "human")) { |
| 5236 | output_format = OFORMAT_HUMAN; |
| 5237 | } else { |
| 5238 | error_report("--output must be used with human or json " |
| 5239 | "as argument."); |
| 5240 | goto out; |
| 5241 | } |
| 5242 | break; |
| 5243 | case OPTION_SIZE: |
| 5244 | { |
| 5245 | int64_t sval; |
| 5246 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 5247 | sval = cvtnum("image size", optarg); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5248 | if (sval < 0) { |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5249 | goto out; |
| 5250 | } |
| 5251 | img_size = (uint64_t)sval; |
| 5252 | } |
| 5253 | break; |
| 5254 | } |
| 5255 | } |
| 5256 | |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5257 | if (argc - optind > 1) { |
| 5258 | error_report("At most one filename argument is allowed."); |
| 5259 | goto out; |
| 5260 | } else if (argc - optind == 1) { |
| 5261 | filename = argv[optind]; |
| 5262 | } |
| 5263 | |
Stefan Hajnoczi | c3673dc | 2020-02-21 11:25:21 +0000 | [diff] [blame] | 5264 | if (!filename && (image_opts || fmt || snapshot_name || sn_opts)) { |
| 5265 | error_report("--image-opts, -f, and -l require a filename argument."); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5266 | goto out; |
| 5267 | } |
| 5268 | if (filename && img_size != UINT64_MAX) { |
| 5269 | error_report("--size N cannot be used together with a filename."); |
| 5270 | goto out; |
| 5271 | } |
| 5272 | if (!filename && img_size == UINT64_MAX) { |
| 5273 | error_report("Either --size N or one filename must be specified."); |
| 5274 | goto out; |
| 5275 | } |
| 5276 | |
| 5277 | if (filename) { |
| 5278 | in_blk = img_open(image_opts, filename, fmt, 0, |
| 5279 | false, false, force_share); |
| 5280 | if (!in_blk) { |
| 5281 | goto out; |
| 5282 | } |
| 5283 | |
| 5284 | if (sn_opts) { |
| 5285 | bdrv_snapshot_load_tmp(blk_bs(in_blk), |
| 5286 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), |
| 5287 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), |
| 5288 | &local_err); |
| 5289 | } else if (snapshot_name != NULL) { |
| 5290 | bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk), |
| 5291 | snapshot_name, &local_err); |
| 5292 | } |
| 5293 | if (local_err) { |
| 5294 | error_reportf_err(local_err, "Failed to load snapshot: "); |
| 5295 | goto out; |
| 5296 | } |
| 5297 | } |
| 5298 | |
| 5299 | drv = bdrv_find_format(out_fmt); |
| 5300 | if (!drv) { |
| 5301 | error_report("Unknown file format '%s'", out_fmt); |
| 5302 | goto out; |
| 5303 | } |
| 5304 | if (!drv->create_opts) { |
| 5305 | error_report("Format driver '%s' does not support image creation", |
| 5306 | drv->format_name); |
| 5307 | goto out; |
| 5308 | } |
| 5309 | |
| 5310 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 5311 | create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts); |
| 5312 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
| 5313 | if (options) { |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 5314 | if (!qemu_opts_do_parse(opts, options, NULL, &local_err)) { |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5315 | error_report_err(local_err); |
| 5316 | error_report("Invalid options for file format '%s'", out_fmt); |
| 5317 | goto out; |
| 5318 | } |
| 5319 | } |
| 5320 | if (img_size != UINT64_MAX) { |
| 5321 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort); |
| 5322 | } |
| 5323 | |
| 5324 | info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err); |
| 5325 | if (local_err) { |
| 5326 | error_report_err(local_err); |
| 5327 | goto out; |
| 5328 | } |
| 5329 | |
| 5330 | if (output_format == OFORMAT_HUMAN) { |
| 5331 | printf("required size: %" PRIu64 "\n", info->required); |
| 5332 | printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated); |
Eric Blake | 5d72c68 | 2020-05-21 14:21:34 -0500 | [diff] [blame] | 5333 | if (info->has_bitmaps) { |
| 5334 | printf("bitmaps size: %" PRIu64 "\n", info->bitmaps); |
| 5335 | } |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5336 | } else { |
| 5337 | dump_json_block_measure_info(info); |
| 5338 | } |
| 5339 | |
| 5340 | ret = 0; |
| 5341 | |
| 5342 | out: |
| 5343 | qapi_free_BlockMeasureInfo(info); |
| 5344 | qemu_opts_del(object_opts); |
| 5345 | qemu_opts_del(opts); |
| 5346 | qemu_opts_del(sn_opts); |
| 5347 | qemu_opts_free(create_opts); |
| 5348 | g_free(options); |
| 5349 | blk_unref(in_blk); |
| 5350 | return ret; |
| 5351 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 5352 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 5353 | static const img_cmd_t img_cmds[] = { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5354 | #define DEF(option, callback, arg_string) \ |
| 5355 | { option, callback }, |
| 5356 | #include "qemu-img-cmds.h" |
| 5357 | #undef DEF |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5358 | { NULL, NULL, }, |
| 5359 | }; |
| 5360 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5361 | int main(int argc, char **argv) |
| 5362 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 5363 | const img_cmd_t *cmd; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5364 | const char *cmdname; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5365 | int c; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5366 | static const struct option long_options[] = { |
| 5367 | {"help", no_argument, 0, 'h'}, |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5368 | {"version", no_argument, 0, 'V'}, |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5369 | {"trace", required_argument, NULL, 'T'}, |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5370 | {0, 0, 0, 0} |
| 5371 | }; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5372 | |
MORITA Kazutaka | 526eda1 | 2013-07-23 17:30:11 +0900 | [diff] [blame] | 5373 | #ifdef CONFIG_POSIX |
| 5374 | signal(SIGPIPE, SIG_IGN); |
| 5375 | #endif |
| 5376 | |
Daniel P. Berrangé | 98c5d2e | 2020-08-25 11:38:48 +0100 | [diff] [blame] | 5377 | socket_init(); |
Christophe Fergeau | f5852ef | 2019-01-31 17:46:14 +0100 | [diff] [blame] | 5378 | error_init(argv[0]); |
Daniel P. Berrange | fe4db84 | 2016-10-04 14:35:52 +0100 | [diff] [blame] | 5379 | module_call_init(MODULE_INIT_TRACE); |
Fam Zheng | 10f5bff | 2014-02-10 14:48:51 +0800 | [diff] [blame] | 5380 | qemu_init_exec_dir(argv[0]); |
Kevin Wolf | 53f76e5 | 2010-12-16 15:10:32 +0100 | [diff] [blame] | 5381 | |
Markus Armbruster | f9734d5 | 2021-07-20 14:53:53 +0200 | [diff] [blame] | 5382 | qemu_init_main_loop(&error_fatal); |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 5383 | |
Eduardo Habkost | e8f2d27 | 2016-05-12 11:10:04 -0300 | [diff] [blame] | 5384 | qcrypto_init(&error_fatal); |
Daniel P. Berrange | c229708 | 2016-04-06 12:12:06 +0100 | [diff] [blame] | 5385 | |
Daniel P. Berrange | 064097d | 2016-02-10 18:41:01 +0000 | [diff] [blame] | 5386 | module_call_init(MODULE_INIT_QOM); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5387 | bdrv_init(); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 5388 | if (argc < 2) { |
| 5389 | error_exit("Not enough arguments"); |
| 5390 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5391 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 5392 | qemu_add_opts(&qemu_source_opts); |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5393 | qemu_add_opts(&qemu_trace_opts); |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 5394 | |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 5395 | while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) { |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5396 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 5397 | case ':': |
| 5398 | missing_argument(argv[optind - 1]); |
| 5399 | return 0; |
Stefan Hajnoczi | 4581c16 | 2017-03-17 18:45:39 +0800 | [diff] [blame] | 5400 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 5401 | unrecognized_option(argv[optind - 1]); |
| 5402 | return 0; |
| 5403 | case 'h': |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5404 | help(); |
| 5405 | return 0; |
| 5406 | case 'V': |
| 5407 | printf(QEMU_IMG_VERSION); |
| 5408 | return 0; |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5409 | case 'T': |
Paolo Bonzini | 92eecff | 2020-11-02 06:58:41 -0500 | [diff] [blame] | 5410 | trace_opt_parse(optarg); |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5411 | break; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5412 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5413 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5414 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5415 | cmdname = argv[optind]; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5416 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5417 | /* reset getopt_long scanning */ |
| 5418 | argc -= optind; |
| 5419 | if (argc < 1) { |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 5420 | return 0; |
| 5421 | } |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5422 | argv += optind; |
Richard W.M. Jones | d339d76 | 2019-01-18 10:11:14 +0000 | [diff] [blame] | 5423 | qemu_reset_optind(); |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5424 | |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5425 | if (!trace_init_backends()) { |
| 5426 | exit(1); |
| 5427 | } |
Paolo Bonzini | 92eecff | 2020-11-02 06:58:41 -0500 | [diff] [blame] | 5428 | trace_init_file(); |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5429 | qemu_set_log(LOG_TRACE); |
| 5430 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5431 | /* find the command */ |
| 5432 | for (cmd = img_cmds; cmd->name != NULL; cmd++) { |
| 5433 | if (!strcmp(cmdname, cmd->name)) { |
| 5434 | return cmd->handler(argc, argv); |
| 5435 | } |
| 5436 | } |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5437 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5438 | /* not found */ |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 5439 | error_exit("Command not found: %s", cmdname); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5440 | } |