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, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | typedef enum OutputFormat { |
| 88 | OFORMAT_JSON, |
| 89 | OFORMAT_HUMAN, |
| 90 | } OutputFormat; |
| 91 | |
Kevin Wolf | e699614 | 2016-03-15 13:03:11 +0100 | [diff] [blame] | 92 | /* 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] | 93 | #define BDRV_DEFAULT_CACHE "writeback" |
aurel32 | 137519c | 2008-11-30 19:12:49 +0000 | [diff] [blame] | 94 | |
Stefan Hajnoczi | 00c6d40 | 2014-08-27 12:08:56 +0100 | [diff] [blame] | 95 | static void format_print(void *opaque, const char *name) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 96 | { |
Stefan Hajnoczi | 00c6d40 | 2014-08-27 12:08:56 +0100 | [diff] [blame] | 97 | printf(" %s", name); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 100 | static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...) |
| 101 | { |
| 102 | va_list ap; |
| 103 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 104 | va_start(ap, fmt); |
Markus Armbruster | e9e1d92 | 2019-04-17 21:06:27 +0200 | [diff] [blame] | 105 | error_vreport(fmt, ap); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 106 | va_end(ap); |
| 107 | |
Markus Armbruster | e9e1d92 | 2019-04-17 21:06:27 +0200 | [diff] [blame] | 108 | error_printf("Try 'qemu-img --help' for more information\n"); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 109 | exit(EXIT_FAILURE); |
| 110 | } |
| 111 | |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 112 | static void QEMU_NORETURN missing_argument(const char *option) |
| 113 | { |
| 114 | error_exit("missing argument for option '%s'", option); |
| 115 | } |
| 116 | |
| 117 | static void QEMU_NORETURN unrecognized_option(const char *option) |
| 118 | { |
| 119 | error_exit("unrecognized option '%s'", option); |
| 120 | } |
| 121 | |
Eric Blake | 0562adf | 2020-05-12 20:16:41 -0500 | [diff] [blame] | 122 | /* Please keep in synch with docs/tools/qemu-img.rst */ |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 123 | static void QEMU_NORETURN help(void) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 124 | { |
Paolo Bonzini | e00291c | 2010-02-04 16:49:56 +0100 | [diff] [blame] | 125 | const char *help_msg = |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 126 | QEMU_IMG_VERSION |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 127 | "usage: qemu-img [standard options] command [command options]\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 128 | "QEMU disk image utility\n" |
| 129 | "\n" |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 130 | " '-h', '--help' display this help and exit\n" |
| 131 | " '-V', '--version' output version information and exit\n" |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 132 | " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n" |
| 133 | " specify tracing options\n" |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 134 | "\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 135 | "Command syntax:\n" |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 136 | #define DEF(option, callback, arg_string) \ |
| 137 | " " arg_string "\n" |
| 138 | #include "qemu-img-cmds.h" |
| 139 | #undef DEF |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 140 | "\n" |
| 141 | "Command parameters:\n" |
| 142 | " 'filename' is a disk image filename\n" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 143 | " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n" |
| 144 | " manual page for a description of the object properties. The most common\n" |
| 145 | " object type is a 'secret', which is used to supply passwords and/or\n" |
| 146 | " encryption keys.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 147 | " '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] | 148 | " '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] | 149 | " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n" |
| 150 | " 'directsync' and 'unsafe' (default for convert)\n" |
Stefan Hajnoczi | bb87fdf | 2014-09-02 11:01:02 +0100 | [diff] [blame] | 151 | " 'src_cache' is the cache mode used to read input disk images, the valid\n" |
| 152 | " options are the same as for the 'cache' option\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 153 | " 'size' is the disk image size in bytes. Optional suffixes\n" |
Kevin Wolf | 5e00984 | 2013-06-05 14:19:27 +0200 | [diff] [blame] | 154 | " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n" |
| 155 | " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n" |
| 156 | " supported. 'b' is ignored.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 157 | " 'output_filename' is the destination disk image filename\n" |
| 158 | " 'output_fmt' is the destination format\n" |
| 159 | " 'options' is a comma separated list of format specific options in a\n" |
| 160 | " name=value format. Use -o ? for an overview of the options supported by the\n" |
| 161 | " used format\n" |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 162 | " 'snapshot_param' is param used for internal snapshot, format\n" |
| 163 | " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n" |
| 164 | " '[ID_OR_NAME]'\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 165 | " '-c' indicates that target image must be compressed (qcow format only)\n" |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 166 | " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n" |
| 167 | " new backing file match exactly. The image doesn't need a working\n" |
| 168 | " backing file before rebasing in this case (useful for renaming the\n" |
| 169 | " backing file). For image creation, allow creating without attempting\n" |
| 170 | " to open the backing file.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 171 | " '-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] | 172 | " '-p' show progress of command (only certain commands)\n" |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 173 | " '-q' use Quiet mode - do not print any output (except errors)\n" |
Peter Lieven | 11b6699 | 2013-10-24 12:07:05 +0200 | [diff] [blame] | 174 | " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n" |
| 175 | " contain only zeros for qemu-img to create a sparse image during\n" |
| 176 | " conversion. If the number of bytes is 0, the source will not be scanned for\n" |
| 177 | " unallocated or zero sectors, and the destination image will always be\n" |
| 178 | " fully allocated\n" |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 179 | " '--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] | 180 | " '-n' skips the target volume creation (useful if the volume is created\n" |
| 181 | " prior to running qemu-img)\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 182 | "\n" |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 183 | "Parameters to bitmap subcommand:\n" |
| 184 | " 'bitmap' is the name of the bitmap to manipulate, through one or more\n" |
| 185 | " actions from '--add', '--remove', '--clear', '--enable', '--disable',\n" |
| 186 | " or '--merge source'\n" |
| 187 | " '-g granularity' sets the granularity for '--add' actions\n" |
| 188 | " '-b source' and '-F src_fmt' tell '--merge' actions to find the source\n" |
| 189 | " bitmaps from an alternative file\n" |
| 190 | "\n" |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 191 | "Parameters to check subcommand:\n" |
| 192 | " '-r' tries to repair any inconsistencies that are found during the check.\n" |
| 193 | " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n" |
| 194 | " 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] | 195 | " hiding corruption that has already occurred.\n" |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 196 | "\n" |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 197 | "Parameters to convert subcommand:\n" |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 198 | " '--bitmaps' copies all top-level persistent bitmaps to destination\n" |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 199 | " '-m' specifies how many coroutines work in parallel during the convert\n" |
| 200 | " process (defaults to 8)\n" |
| 201 | " '-W' allow to write to the target out of order rather than sequential\n" |
| 202 | "\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 203 | "Parameters to snapshot subcommand:\n" |
| 204 | " 'snapshot' is the name of the snapshot to create, apply or delete\n" |
| 205 | " '-a' applies a snapshot (revert disk to saved state)\n" |
| 206 | " '-c' creates a snapshot\n" |
| 207 | " '-d' deletes a snapshot\n" |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 208 | " '-l' lists all snapshots in the given image\n" |
| 209 | "\n" |
| 210 | "Parameters to compare subcommand:\n" |
| 211 | " '-f' first image format\n" |
| 212 | " '-F' second image format\n" |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 213 | " '-s' run in Strict mode - fail on different image size or sector allocation\n" |
| 214 | "\n" |
| 215 | "Parameters to dd subcommand:\n" |
| 216 | " 'bs=BYTES' read and write up to BYTES bytes at a time " |
| 217 | "(default: 512)\n" |
| 218 | " 'count=N' copy only N input blocks\n" |
| 219 | " 'if=FILE' read from FILE\n" |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 220 | " 'of=FILE' write to FILE\n" |
| 221 | " '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] | 222 | |
| 223 | printf("%s\nSupported formats:", help_msg); |
Andrey Shinkevich | 9ac404c | 2019-03-07 16:33:58 +0300 | [diff] [blame] | 224 | bdrv_iterate_format(format_print, NULL, false); |
Eric Blake | f5048cb | 2017-08-03 11:33:53 -0500 | [diff] [blame] | 225 | printf("\n\n" QEMU_HELP_BOTTOM "\n"); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 226 | exit(EXIT_SUCCESS); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Markus Armbruster | 80c710c | 2020-04-15 09:49:26 +0200 | [diff] [blame] | 229 | /* |
| 230 | * Is @optarg safe for accumulate_options()? |
| 231 | * It is when multiple of them can be joined together separated by ','. |
| 232 | * To make that work, @optarg must not start with ',' (or else a |
| 233 | * separating ',' preceding it gets escaped), and it must not end with |
| 234 | * an odd number of ',' (or else a separating ',' following it gets |
Markus Armbruster | f62514b | 2020-04-15 09:49:27 +0200 | [diff] [blame] | 235 | * escaped), or be empty (or else a separating ',' preceding it can |
| 236 | * escape a separating ',' following it). |
| 237 | * |
Markus Armbruster | 80c710c | 2020-04-15 09:49:26 +0200 | [diff] [blame] | 238 | */ |
| 239 | static bool is_valid_option_list(const char *optarg) |
| 240 | { |
| 241 | size_t len = strlen(optarg); |
| 242 | size_t i; |
| 243 | |
Markus Armbruster | f62514b | 2020-04-15 09:49:27 +0200 | [diff] [blame] | 244 | if (!optarg[0] || optarg[0] == ',') { |
Markus Armbruster | 80c710c | 2020-04-15 09:49:26 +0200 | [diff] [blame] | 245 | return false; |
| 246 | } |
| 247 | |
| 248 | for (i = len; i > 0 && optarg[i - 1] == ','; i--) { |
| 249 | } |
| 250 | if ((len - i) % 2) { |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | return true; |
| 255 | } |
| 256 | |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 257 | static int accumulate_options(char **options, char *optarg) |
| 258 | { |
| 259 | char *new_options; |
| 260 | |
| 261 | if (!is_valid_option_list(optarg)) { |
| 262 | error_report("Invalid option list: %s", optarg); |
| 263 | return -1; |
| 264 | } |
| 265 | |
| 266 | if (!*options) { |
| 267 | *options = g_strdup(optarg); |
| 268 | } else { |
| 269 | new_options = g_strdup_printf("%s,%s", *options, optarg); |
| 270 | g_free(*options); |
| 271 | *options = new_options; |
| 272 | } |
| 273 | return 0; |
| 274 | } |
| 275 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 276 | static QemuOptsList qemu_source_opts = { |
| 277 | .name = "source", |
| 278 | .implied_opt_name = "file", |
| 279 | .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head), |
| 280 | .desc = { |
| 281 | { } |
| 282 | }, |
| 283 | }; |
| 284 | |
Stefan Weil | 7c30f65 | 2013-06-16 17:01:05 +0200 | [diff] [blame] | 285 | 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] | 286 | { |
| 287 | int ret = 0; |
| 288 | if (!quiet) { |
| 289 | va_list args; |
| 290 | va_start(args, fmt); |
| 291 | ret = vprintf(fmt, args); |
| 292 | va_end(args); |
| 293 | } |
| 294 | return ret; |
| 295 | } |
| 296 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 297 | |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 298 | static int print_block_option_help(const char *filename, const char *fmt) |
| 299 | { |
| 300 | BlockDriver *drv, *proto_drv; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 301 | QemuOptsList *create_opts = NULL; |
Max Reitz | b65a5e1 | 2015-02-05 13:58:12 -0500 | [diff] [blame] | 302 | Error *local_err = NULL; |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 303 | |
| 304 | /* Find driver and parse its options */ |
| 305 | drv = bdrv_find_format(fmt); |
| 306 | if (!drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 307 | error_report("Unknown file format '%s'", fmt); |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 308 | return 1; |
| 309 | } |
| 310 | |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 311 | if (!drv->create_opts) { |
| 312 | error_report("Format driver '%s' does not support image creation", fmt); |
| 313 | return 1; |
| 314 | } |
| 315 | |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 316 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 317 | if (filename) { |
Max Reitz | b65a5e1 | 2015-02-05 13:58:12 -0500 | [diff] [blame] | 318 | proto_drv = bdrv_find_protocol(filename, true, &local_err); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 319 | if (!proto_drv) { |
Markus Armbruster | 2867ce4 | 2015-03-12 16:08:02 +0100 | [diff] [blame] | 320 | error_report_err(local_err); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 321 | qemu_opts_free(create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 322 | return 1; |
| 323 | } |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 324 | if (!proto_drv->create_opts) { |
Max Reitz | f099887 | 2018-11-19 11:19:20 +0100 | [diff] [blame] | 325 | error_report("Protocol driver '%s' does not support image creation", |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 326 | proto_drv->format_name); |
Max Reitz | 3ecd5a4 | 2018-11-19 11:19:21 +0100 | [diff] [blame] | 327 | qemu_opts_free(create_opts); |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 328 | return 1; |
| 329 | } |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 330 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 331 | } |
| 332 | |
Max Reitz | f4619af | 2019-04-13 17:20:37 +0200 | [diff] [blame] | 333 | if (filename) { |
| 334 | printf("Supported options:\n"); |
| 335 | } else { |
| 336 | printf("Supported %s options:\n", fmt); |
| 337 | } |
Max Reitz | 6389871 | 2018-10-19 18:49:25 +0200 | [diff] [blame] | 338 | qemu_opts_print_help(create_opts, false); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 339 | qemu_opts_free(create_opts); |
Max Reitz | f4619af | 2019-04-13 17:20:37 +0200 | [diff] [blame] | 340 | |
| 341 | if (!filename) { |
| 342 | printf("\n" |
| 343 | "The protocol level may support further options.\n" |
| 344 | "Specify the target filename to include those options.\n"); |
| 345 | } |
| 346 | |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 347 | return 0; |
| 348 | } |
| 349 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 350 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 351 | static BlockBackend *img_open_opts(const char *optstr, |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 352 | QemuOpts *opts, int flags, bool writethrough, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 353 | bool quiet, bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 354 | { |
| 355 | QDict *options; |
| 356 | Error *local_err = NULL; |
| 357 | BlockBackend *blk; |
| 358 | options = qemu_opts_to_qdict(opts, NULL); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 359 | if (force_share) { |
| 360 | if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE) |
Max Reitz | 4615f87 | 2018-05-02 22:20:50 +0200 | [diff] [blame] | 361 | && strcmp(qdict_get_str(options, BDRV_OPT_FORCE_SHARE), "on")) { |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 362 | error_report("--force-share/-U conflicts with image options"); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 363 | qobject_unref(options); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 364 | return NULL; |
| 365 | } |
Max Reitz | 4615f87 | 2018-05-02 22:20:50 +0200 | [diff] [blame] | 366 | qdict_put_str(options, BDRV_OPT_FORCE_SHARE, "on"); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 367 | } |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 368 | blk = blk_new_open(NULL, NULL, options, flags, &local_err); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 369 | if (!blk) { |
Daniel P. Berrange | 143605a | 2016-04-06 10:16:18 +0100 | [diff] [blame] | 370 | error_reportf_err(local_err, "Could not open '%s': ", optstr); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 371 | return NULL; |
| 372 | } |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 373 | blk_set_enable_write_cache(blk, !writethrough); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 374 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 375 | return blk; |
| 376 | } |
| 377 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 378 | static BlockBackend *img_open_file(const char *filename, |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 379 | QDict *options, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 380 | const char *fmt, int flags, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 381 | bool writethrough, bool quiet, |
| 382 | bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 383 | { |
| 384 | BlockBackend *blk; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 385 | Error *local_err = NULL; |
Kevin Wolf | ad71713 | 2010-12-16 15:37:41 +0100 | [diff] [blame] | 386 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 387 | if (!options) { |
| 388 | options = qdict_new(); |
| 389 | } |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 390 | if (fmt) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 391 | qdict_put_str(options, "driver", fmt); |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 392 | } |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 393 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 394 | if (force_share) { |
Eric Blake | 579cf1d | 2017-05-15 14:54:39 -0500 | [diff] [blame] | 395 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 396 | } |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 397 | blk = blk_new_open(filename, NULL, options, flags, &local_err); |
Max Reitz | 5bd3132 | 2015-02-05 13:58:16 -0500 | [diff] [blame] | 398 | if (!blk) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 399 | error_reportf_err(local_err, "Could not open '%s': ", filename); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 400 | return NULL; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 401 | } |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 402 | blk_set_enable_write_cache(blk, !writethrough); |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 403 | |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 404 | return blk; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 407 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 408 | static int img_add_key_secrets(void *opaque, |
| 409 | const char *name, const char *value, |
| 410 | Error **errp) |
| 411 | { |
| 412 | QDict *options = opaque; |
| 413 | |
| 414 | if (g_str_has_suffix(name, "key-secret")) { |
Eric Blake | 187f47e | 2017-06-24 12:10:07 -0600 | [diff] [blame] | 415 | qdict_put_str(options, name, value); |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | return 0; |
| 419 | } |
| 420 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 421 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 422 | static BlockBackend *img_open(bool image_opts, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 423 | const char *filename, |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 424 | const char *fmt, int flags, bool writethrough, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 425 | bool quiet, bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 426 | { |
| 427 | BlockBackend *blk; |
| 428 | if (image_opts) { |
| 429 | QemuOpts *opts; |
| 430 | if (fmt) { |
| 431 | error_report("--image-opts and --format are mutually exclusive"); |
| 432 | return NULL; |
| 433 | } |
| 434 | opts = qemu_opts_parse_noisily(qemu_find_opts("source"), |
| 435 | filename, true); |
| 436 | if (!opts) { |
| 437 | return NULL; |
| 438 | } |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 439 | blk = img_open_opts(filename, opts, flags, writethrough, quiet, |
| 440 | force_share); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 441 | } else { |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 442 | blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 443 | force_share); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 444 | } |
| 445 | return blk; |
| 446 | } |
| 447 | |
| 448 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 449 | static int add_old_style_options(const char *fmt, QemuOpts *opts, |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 450 | const char *base_filename, |
| 451 | const char *base_fmt) |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 452 | { |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 453 | if (base_filename) { |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 454 | if (!qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, |
Markus Armbruster | 9e194e0 | 2020-07-07 18:06:11 +0200 | [diff] [blame] | 455 | NULL)) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 456 | error_report("Backing file not supported for file format '%s'", |
| 457 | fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 458 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | if (base_fmt) { |
Markus Armbruster | 9e194e0 | 2020-07-07 18:06:11 +0200 | [diff] [blame] | 462 | if (!qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, NULL)) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 463 | error_report("Backing file format not supported for file " |
| 464 | "format '%s'", fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 465 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 466 | } |
| 467 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 468 | return 0; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 469 | } |
| 470 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 471 | static int64_t cvtnum_full(const char *name, const char *value, int64_t min, |
| 472 | int64_t max) |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 473 | { |
Markus Armbruster | f17fd4f | 2017-02-21 21:14:06 +0100 | [diff] [blame] | 474 | int err; |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 475 | uint64_t res; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 476 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 477 | err = qemu_strtosz(value, NULL, &res); |
| 478 | if (err < 0 && err != -ERANGE) { |
| 479 | error_report("Invalid %s specified. You may use " |
| 480 | "k, M, G, T, P or E suffixes for", name); |
| 481 | error_report("kilobytes, megabytes, gigabytes, terabytes, " |
| 482 | "petabytes and exabytes."); |
Markus Armbruster | f17fd4f | 2017-02-21 21:14:06 +0100 | [diff] [blame] | 483 | return err; |
| 484 | } |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 485 | if (err == -ERANGE || res > max || res < min) { |
| 486 | error_report("Invalid %s specified. Must be between %" PRId64 |
| 487 | " and %" PRId64 ".", name, min, max); |
Markus Armbruster | f46bfdb | 2017-02-21 21:14:07 +0100 | [diff] [blame] | 488 | return -ERANGE; |
| 489 | } |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 490 | return res; |
| 491 | } |
| 492 | |
| 493 | static int64_t cvtnum(const char *name, const char *value) |
| 494 | { |
| 495 | return cvtnum_full(name, value, 0, INT64_MAX); |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 496 | } |
| 497 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 498 | static int img_create(int argc, char **argv) |
| 499 | { |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 500 | int c; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 501 | uint64_t img_size = -1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 502 | const char *fmt = "raw"; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 503 | const char *base_fmt = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 504 | const char *filename; |
| 505 | const char *base_filename = NULL; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 506 | char *options = NULL; |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 507 | Error *local_err = NULL; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 508 | bool quiet = false; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 509 | int flags = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 510 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 511 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 512 | static const struct option long_options[] = { |
| 513 | {"help", no_argument, 0, 'h'}, |
| 514 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 515 | {0, 0, 0, 0} |
| 516 | }; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 517 | c = getopt_long(argc, argv, ":F:b:f:ho:qu", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 518 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 519 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 520 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 521 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 522 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 523 | case ':': |
| 524 | missing_argument(argv[optind - 1]); |
| 525 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 526 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 527 | unrecognized_option(argv[optind - 1]); |
| 528 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 529 | case 'h': |
| 530 | help(); |
| 531 | break; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 532 | case 'F': |
| 533 | base_fmt = optarg; |
| 534 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 535 | case 'b': |
| 536 | base_filename = optarg; |
| 537 | break; |
| 538 | case 'f': |
| 539 | fmt = optarg; |
| 540 | break; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 541 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 542 | if (accumulate_options(&options, optarg) < 0) { |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 543 | goto fail; |
| 544 | } |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 545 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 546 | case 'q': |
| 547 | quiet = true; |
| 548 | break; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 549 | case 'u': |
| 550 | flags |= BDRV_O_NO_BACKING; |
| 551 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 552 | case OPTION_OBJECT: |
| 553 | user_creatable_process_cmdline(optarg); |
| 554 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 555 | } |
| 556 | } |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 557 | |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 558 | /* Get the filename */ |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 559 | filename = (optind < argc) ? argv[optind] : NULL; |
| 560 | if (options && has_help_option(options)) { |
| 561 | g_free(options); |
| 562 | return print_block_option_help(filename, fmt); |
| 563 | } |
| 564 | |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 565 | if (optind >= argc) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 566 | error_exit("Expecting image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 567 | } |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 568 | optind++; |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 569 | |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 570 | /* Get image size, if specified */ |
| 571 | if (optind < argc) { |
Jes Sorensen | 70b4f4b | 2011-01-05 11:41:02 +0100 | [diff] [blame] | 572 | int64_t sval; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 573 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 574 | sval = cvtnum("image size", argv[optind++]); |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 575 | if (sval < 0) { |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 576 | goto fail; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 577 | } |
| 578 | img_size = (uint64_t)sval; |
| 579 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 580 | if (optind != argc) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 581 | error_exit("Unexpected argument: %s", argv[optind]); |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 582 | } |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 583 | |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 584 | bdrv_img_create(filename, fmt, base_filename, base_fmt, |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 585 | options, img_size, flags, quiet, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 586 | if (local_err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 587 | error_reportf_err(local_err, "%s: ", filename); |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 588 | goto fail; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 589 | } |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 590 | |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 591 | g_free(options); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 592 | return 0; |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 593 | |
| 594 | fail: |
| 595 | g_free(options); |
| 596 | return 1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 599 | static void dump_json_image_check(ImageCheck *check, bool quiet) |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 600 | { |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 601 | GString *str; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 602 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 603 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 604 | |
| 605 | visit_type_ImageCheck(v, NULL, &check, &error_abort); |
| 606 | visit_complete(v, &obj); |
Markus Armbruster | 6589f45 | 2020-12-11 18:11:35 +0100 | [diff] [blame] | 607 | str = qobject_to_json_pretty(obj, true); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 608 | assert(str != NULL); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 609 | qprintf(quiet, "%s\n", str->str); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 610 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 611 | visit_free(v); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 612 | g_string_free(str, true); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 613 | } |
| 614 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 615 | static void dump_human_image_check(ImageCheck *check, bool quiet) |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 616 | { |
| 617 | if (!(check->corruptions || check->leaks || check->check_errors)) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 618 | qprintf(quiet, "No errors were found on the image.\n"); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 619 | } else { |
| 620 | if (check->corruptions) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 621 | qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n" |
| 622 | "Data may be corrupted, or further writes to the image " |
| 623 | "may corrupt it.\n", |
| 624 | check->corruptions); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | if (check->leaks) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 628 | qprintf(quiet, |
| 629 | "\n%" PRId64 " leaked clusters were found on the image.\n" |
| 630 | "This means waste of disk space, but no harm to data.\n", |
| 631 | check->leaks); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | if (check->check_errors) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 635 | qprintf(quiet, |
| 636 | "\n%" PRId64 |
| 637 | " internal errors have occurred during the check.\n", |
| 638 | check->check_errors); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 639 | } |
| 640 | } |
| 641 | |
| 642 | if (check->total_clusters != 0 && check->allocated_clusters != 0) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 643 | qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, " |
| 644 | "%0.2f%% fragmented, %0.2f%% compressed clusters\n", |
| 645 | check->allocated_clusters, check->total_clusters, |
| 646 | check->allocated_clusters * 100.0 / check->total_clusters, |
| 647 | check->fragmented_clusters * 100.0 / check->allocated_clusters, |
| 648 | check->compressed_clusters * 100.0 / |
| 649 | check->allocated_clusters); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | if (check->image_end_offset) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 653 | qprintf(quiet, |
| 654 | "Image end offset: %" PRId64 "\n", check->image_end_offset); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | |
| 658 | static int collect_image_check(BlockDriverState *bs, |
| 659 | ImageCheck *check, |
| 660 | const char *filename, |
| 661 | const char *fmt, |
| 662 | int fix) |
| 663 | { |
| 664 | int ret; |
| 665 | BdrvCheckResult result; |
| 666 | |
| 667 | ret = bdrv_check(bs, &result, fix); |
| 668 | if (ret < 0) { |
| 669 | return ret; |
| 670 | } |
| 671 | |
| 672 | check->filename = g_strdup(filename); |
| 673 | check->format = g_strdup(bdrv_get_format_name(bs)); |
| 674 | check->check_errors = result.check_errors; |
| 675 | check->corruptions = result.corruptions; |
| 676 | check->has_corruptions = result.corruptions != 0; |
| 677 | check->leaks = result.leaks; |
| 678 | check->has_leaks = result.leaks != 0; |
| 679 | check->corruptions_fixed = result.corruptions_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 680 | check->has_corruptions_fixed = result.corruptions_fixed != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 681 | check->leaks_fixed = result.leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 682 | check->has_leaks_fixed = result.leaks_fixed != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 683 | check->image_end_offset = result.image_end_offset; |
| 684 | check->has_image_end_offset = result.image_end_offset != 0; |
| 685 | check->total_clusters = result.bfi.total_clusters; |
| 686 | check->has_total_clusters = result.bfi.total_clusters != 0; |
| 687 | check->allocated_clusters = result.bfi.allocated_clusters; |
| 688 | check->has_allocated_clusters = result.bfi.allocated_clusters != 0; |
| 689 | check->fragmented_clusters = result.bfi.fragmented_clusters; |
| 690 | check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0; |
Stefan Hajnoczi | e6439d7 | 2013-02-07 17:15:04 +0100 | [diff] [blame] | 691 | check->compressed_clusters = result.bfi.compressed_clusters; |
| 692 | check->has_compressed_clusters = result.bfi.compressed_clusters != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 693 | |
| 694 | return 0; |
| 695 | } |
| 696 | |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 697 | /* |
| 698 | * Checks an image for consistency. Exit codes: |
| 699 | * |
Max Reitz | d6635c4 | 2014-06-02 22:15:21 +0200 | [diff] [blame] | 700 | * 0 - Check completed, image is good |
| 701 | * 1 - Check not completed because of internal errors |
| 702 | * 2 - Check completed, image is corrupted |
| 703 | * 3 - Check completed, image has leaked clusters, but is good otherwise |
| 704 | * 63 - Checks are not supported by the image format |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 705 | */ |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 706 | static int img_check(int argc, char **argv) |
| 707 | { |
| 708 | int c, ret; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 709 | OutputFormat output_format = OFORMAT_HUMAN; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 710 | const char *filename, *fmt, *output, *cache; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 711 | BlockBackend *blk; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 712 | BlockDriverState *bs; |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 713 | int fix = 0; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 714 | int flags = BDRV_O_CHECK; |
| 715 | bool writethrough; |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 716 | ImageCheck *check; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 717 | bool quiet = false; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 718 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 719 | bool force_share = false; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 720 | |
| 721 | fmt = NULL; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 722 | output = NULL; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 723 | cache = BDRV_DEFAULT_CACHE; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 724 | |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 725 | for(;;) { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 726 | int option_index = 0; |
| 727 | static const struct option long_options[] = { |
| 728 | {"help", no_argument, 0, 'h'}, |
| 729 | {"format", required_argument, 0, 'f'}, |
Prasad Joshi | 4fd6a98 | 2014-03-25 00:08:54 +0530 | [diff] [blame] | 730 | {"repair", required_argument, 0, 'r'}, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 731 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 732 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 733 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 734 | {"force-share", no_argument, 0, 'U'}, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 735 | {0, 0, 0, 0} |
| 736 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 737 | c = getopt_long(argc, argv, ":hf:r:T:qU", |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 738 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 739 | if (c == -1) { |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 740 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 741 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 742 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 743 | case ':': |
| 744 | missing_argument(argv[optind - 1]); |
| 745 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 746 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 747 | unrecognized_option(argv[optind - 1]); |
| 748 | break; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 749 | case 'h': |
| 750 | help(); |
| 751 | break; |
| 752 | case 'f': |
| 753 | fmt = optarg; |
| 754 | break; |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 755 | case 'r': |
| 756 | flags |= BDRV_O_RDWR; |
| 757 | |
| 758 | if (!strcmp(optarg, "leaks")) { |
| 759 | fix = BDRV_FIX_LEAKS; |
| 760 | } else if (!strcmp(optarg, "all")) { |
| 761 | fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS; |
| 762 | } else { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 763 | error_exit("Unknown option value for -r " |
| 764 | "(expecting 'leaks' or 'all'): %s", optarg); |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 765 | } |
| 766 | break; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 767 | case OPTION_OUTPUT: |
| 768 | output = optarg; |
| 769 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 770 | case 'T': |
| 771 | cache = optarg; |
| 772 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 773 | case 'q': |
| 774 | quiet = true; |
| 775 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 776 | case 'U': |
| 777 | force_share = true; |
| 778 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 779 | case OPTION_OBJECT: |
| 780 | user_creatable_process_cmdline(optarg); |
| 781 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 782 | case OPTION_IMAGE_OPTS: |
| 783 | image_opts = true; |
| 784 | break; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 785 | } |
| 786 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 787 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 788 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 789 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 790 | filename = argv[optind++]; |
| 791 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 792 | if (output && !strcmp(output, "json")) { |
| 793 | output_format = OFORMAT_JSON; |
| 794 | } else if (output && !strcmp(output, "human")) { |
| 795 | output_format = OFORMAT_HUMAN; |
| 796 | } else if (output) { |
| 797 | error_report("--output must be used with human or json as argument."); |
| 798 | return 1; |
| 799 | } |
| 800 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 801 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 802 | if (ret < 0) { |
| 803 | error_report("Invalid source cache option: %s", cache); |
| 804 | return 1; |
| 805 | } |
| 806 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 807 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 808 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 809 | if (!blk) { |
| 810 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 811 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 812 | bs = blk_bs(blk); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 813 | |
| 814 | check = g_new0(ImageCheck, 1); |
| 815 | ret = collect_image_check(bs, check, filename, fmt, fix); |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 816 | |
| 817 | if (ret == -ENOTSUP) { |
Max Reitz | 55d492d | 2014-05-31 21:33:30 +0200 | [diff] [blame] | 818 | error_report("This image format does not support checks"); |
Peter Lieven | fefddf9 | 2013-10-24 08:53:34 +0200 | [diff] [blame] | 819 | ret = 63; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 820 | goto fail; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 821 | } |
| 822 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 823 | if (check->corruptions_fixed || check->leaks_fixed) { |
| 824 | int corruptions_fixed, leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 825 | bool has_leaks_fixed, has_corruptions_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 826 | |
| 827 | leaks_fixed = check->leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 828 | has_leaks_fixed = check->has_leaks_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 829 | corruptions_fixed = check->corruptions_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 830 | has_corruptions_fixed = check->has_corruptions_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 831 | |
| 832 | if (output_format == OFORMAT_HUMAN) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 833 | qprintf(quiet, |
| 834 | "The following inconsistencies were found and repaired:\n\n" |
| 835 | " %" PRId64 " leaked clusters\n" |
| 836 | " %" PRId64 " corruptions\n\n" |
| 837 | "Double checking the fixed image now...\n", |
| 838 | check->leaks_fixed, |
| 839 | check->corruptions_fixed); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 840 | } |
| 841 | |
Pan Nengyuan | fc124ea | 2020-02-27 09:29:50 +0800 | [diff] [blame] | 842 | qapi_free_ImageCheck(check); |
| 843 | check = g_new0(ImageCheck, 1); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 844 | ret = collect_image_check(bs, check, filename, fmt, 0); |
| 845 | |
| 846 | check->leaks_fixed = leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 847 | check->has_leaks_fixed = has_leaks_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 848 | check->corruptions_fixed = corruptions_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 849 | check->has_corruptions_fixed = has_corruptions_fixed; |
Kevin Wolf | ccf3471 | 2012-05-11 18:16:54 +0200 | [diff] [blame] | 850 | } |
| 851 | |
Max Reitz | 832390a | 2014-10-23 15:29:12 +0200 | [diff] [blame] | 852 | if (!ret) { |
| 853 | switch (output_format) { |
| 854 | case OFORMAT_HUMAN: |
| 855 | dump_human_image_check(check, quiet); |
| 856 | break; |
| 857 | case OFORMAT_JSON: |
| 858 | dump_json_image_check(check, quiet); |
| 859 | break; |
| 860 | } |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | if (ret || check->check_errors) { |
Max Reitz | 832390a | 2014-10-23 15:29:12 +0200 | [diff] [blame] | 864 | if (ret) { |
| 865 | error_report("Check failed: %s", strerror(-ret)); |
| 866 | } else { |
| 867 | error_report("Check failed"); |
| 868 | } |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 869 | ret = 1; |
| 870 | goto fail; |
| 871 | } |
| 872 | |
| 873 | if (check->corruptions) { |
| 874 | ret = 2; |
| 875 | } else if (check->leaks) { |
| 876 | ret = 3; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 877 | } else { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 878 | ret = 0; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 881 | fail: |
| 882 | qapi_free_ImageCheck(check); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 883 | blk_unref(blk); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 884 | return ret; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 887 | typedef struct CommonBlockJobCBInfo { |
| 888 | BlockDriverState *bs; |
| 889 | Error **errp; |
| 890 | } CommonBlockJobCBInfo; |
| 891 | |
| 892 | static void common_block_job_cb(void *opaque, int ret) |
| 893 | { |
| 894 | CommonBlockJobCBInfo *cbi = opaque; |
| 895 | |
| 896 | if (ret < 0) { |
| 897 | error_setg_errno(cbi->errp, -ret, "Block job failed"); |
| 898 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | static void run_block_job(BlockJob *job, Error **errp) |
| 902 | { |
Emanuele Giuseppe Esposito | a7b4f8f | 2021-06-14 10:11:29 +0200 | [diff] [blame] | 903 | uint64_t progress_current, progress_total; |
Kevin Wolf | b75536c | 2016-04-18 17:30:17 +0200 | [diff] [blame] | 904 | AioContext *aio_context = blk_get_aio_context(job->blk); |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 905 | int ret = 0; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 906 | |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 907 | aio_context_acquire(aio_context); |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 908 | job_ref(&job->job); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 909 | do { |
Kevin Wolf | 30a5c88 | 2018-05-04 12:17:20 +0200 | [diff] [blame] | 910 | float progress = 0.0f; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 911 | aio_poll(aio_context, true); |
Emanuele Giuseppe Esposito | a7b4f8f | 2021-06-14 10:11:29 +0200 | [diff] [blame] | 912 | |
| 913 | progress_get_snapshot(&job->job.progress, &progress_current, |
| 914 | &progress_total); |
| 915 | if (progress_total) { |
| 916 | progress = (float)progress_current / progress_total * 100.f; |
Kevin Wolf | 30a5c88 | 2018-05-04 12:17:20 +0200 | [diff] [blame] | 917 | } |
| 918 | qemu_progress_print(progress, 0); |
Kevin Wolf | df956ae | 2018-04-25 15:09:58 +0200 | [diff] [blame] | 919 | } while (!job_is_ready(&job->job) && !job_is_completed(&job->job)); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 920 | |
Kevin Wolf | dbe5e6c | 2018-04-19 13:04:01 +0200 | [diff] [blame] | 921 | if (!job_is_completed(&job->job)) { |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 922 | ret = job_complete_sync(&job->job, errp); |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 923 | } else { |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 924 | ret = job->job.ret; |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 925 | } |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 926 | job_unref(&job->job); |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 927 | aio_context_release(aio_context); |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 928 | |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 929 | /* publish completion progress only when success */ |
| 930 | if (!ret) { |
| 931 | qemu_progress_print(100.f, 0); |
| 932 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 933 | } |
| 934 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 935 | static int img_commit(int argc, char **argv) |
| 936 | { |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 937 | int c, ret, flags; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 938 | const char *filename, *fmt, *cache, *base; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 939 | BlockBackend *blk; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 940 | BlockDriverState *bs, *base_bs; |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 941 | BlockJob *job; |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 942 | bool progress = false, quiet = false, drop = false; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 943 | bool writethrough; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 944 | Error *local_err = NULL; |
| 945 | CommonBlockJobCBInfo cbi; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 946 | bool image_opts = false; |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 947 | AioContext *aio_context; |
Zhengui | a0441b6 | 2020-10-20 14:47:43 +0000 | [diff] [blame] | 948 | int64_t rate_limit = 0; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 949 | |
| 950 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 951 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 952 | base = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 953 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 954 | static const struct option long_options[] = { |
| 955 | {"help", no_argument, 0, 'h'}, |
| 956 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 957 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 958 | {0, 0, 0, 0} |
| 959 | }; |
Zhengui | a0441b6 | 2020-10-20 14:47:43 +0000 | [diff] [blame] | 960 | c = getopt_long(argc, argv, ":f:ht:b:dpqr:", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 961 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 962 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 963 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 964 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 965 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 966 | case ':': |
| 967 | missing_argument(argv[optind - 1]); |
| 968 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 969 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 970 | unrecognized_option(argv[optind - 1]); |
| 971 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 972 | case 'h': |
| 973 | help(); |
| 974 | break; |
| 975 | case 'f': |
| 976 | fmt = optarg; |
| 977 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 978 | case 't': |
| 979 | cache = optarg; |
| 980 | break; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 981 | case 'b': |
| 982 | base = optarg; |
| 983 | /* -b implies -d */ |
| 984 | drop = true; |
| 985 | break; |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 986 | case 'd': |
| 987 | drop = true; |
| 988 | break; |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 989 | case 'p': |
| 990 | progress = true; |
| 991 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 992 | case 'q': |
| 993 | quiet = true; |
| 994 | break; |
Zhengui | a0441b6 | 2020-10-20 14:47:43 +0000 | [diff] [blame] | 995 | case 'r': |
| 996 | rate_limit = cvtnum("rate limit", optarg); |
| 997 | if (rate_limit < 0) { |
| 998 | return 1; |
| 999 | } |
| 1000 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 1001 | case OPTION_OBJECT: |
| 1002 | user_creatable_process_cmdline(optarg); |
| 1003 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1004 | case OPTION_IMAGE_OPTS: |
| 1005 | image_opts = true; |
| 1006 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1007 | } |
| 1008 | } |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 1009 | |
| 1010 | /* Progress is not shown in Quiet mode */ |
| 1011 | if (quiet) { |
| 1012 | progress = false; |
| 1013 | } |
| 1014 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 1015 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 1016 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1017 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1018 | filename = argv[optind++]; |
| 1019 | |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1020 | flags = BDRV_O_RDWR | BDRV_O_UNMAP; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1021 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1022 | if (ret < 0) { |
| 1023 | error_report("Invalid cache option: %s", cache); |
Stefan Hajnoczi | a3981eb | 2014-08-26 19:17:54 +0100 | [diff] [blame] | 1024 | return 1; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1025 | } |
| 1026 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1027 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 1028 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1029 | if (!blk) { |
| 1030 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1031 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1032 | bs = blk_bs(blk); |
| 1033 | |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 1034 | qemu_progress_init(progress, 1.f); |
| 1035 | qemu_progress_print(0.f, 100); |
| 1036 | |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1037 | if (base) { |
| 1038 | base_bs = bdrv_find_backing_image(bs, base); |
| 1039 | if (!base_bs) { |
Max Reitz | 6b33f3a | 2016-12-01 03:05:08 +0100 | [diff] [blame] | 1040 | error_setg(&local_err, |
| 1041 | "Did not find '%s' in the backing chain of '%s'", |
| 1042 | base, filename); |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1043 | goto done; |
| 1044 | } |
| 1045 | } else { |
| 1046 | /* This is different from QMP, which by default uses the deepest file in |
| 1047 | * the backing chain (i.e., the very base); however, the traditional |
| 1048 | * behavior of qemu-img commit is using the immediate backing file. */ |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 1049 | base_bs = bdrv_backing_chain_next(bs); |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1050 | if (!base_bs) { |
| 1051 | error_setg(&local_err, "Image does not have a backing file"); |
| 1052 | goto done; |
| 1053 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1056 | cbi = (CommonBlockJobCBInfo){ |
| 1057 | .errp = &local_err, |
| 1058 | .bs = bs, |
| 1059 | }; |
| 1060 | |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 1061 | aio_context = bdrv_get_aio_context(bs); |
| 1062 | aio_context_acquire(aio_context); |
Zhengui | a0441b6 | 2020-10-20 14:47:43 +0000 | [diff] [blame] | 1063 | commit_active_start("commit", bs, base_bs, JOB_DEFAULT, rate_limit, |
Kevin Wolf | 0db832f | 2017-02-20 18:10:05 +0100 | [diff] [blame] | 1064 | BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb, |
Fam Zheng | 78bbd91 | 2017-04-21 20:27:04 +0800 | [diff] [blame] | 1065 | &cbi, false, &local_err); |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 1066 | aio_context_release(aio_context); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1067 | if (local_err) { |
| 1068 | goto done; |
| 1069 | } |
| 1070 | |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1071 | /* When the block job completes, the BlockBackend reference will point to |
| 1072 | * the old backing file. In order to avoid that the top image is already |
| 1073 | * deleted, so we can still empty it afterwards, increment the reference |
| 1074 | * counter here preemptively. */ |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1075 | if (!drop) { |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1076 | bdrv_ref(bs); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1077 | } |
| 1078 | |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 1079 | job = block_job_get("commit"); |
Liam Merwick | 2e2db26 | 2018-11-05 21:38:37 +0000 | [diff] [blame] | 1080 | assert(job); |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 1081 | run_block_job(job, &local_err); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1082 | if (local_err) { |
| 1083 | goto unref_backing; |
| 1084 | } |
| 1085 | |
Max Reitz | 2d97fde | 2020-04-29 16:11:26 +0200 | [diff] [blame] | 1086 | if (!drop) { |
| 1087 | BlockBackend *old_backing_blk; |
| 1088 | |
| 1089 | old_backing_blk = blk_new_with_bs(bs, BLK_PERM_WRITE, BLK_PERM_ALL, |
| 1090 | &local_err); |
| 1091 | if (!old_backing_blk) { |
| 1092 | goto unref_backing; |
| 1093 | } |
| 1094 | ret = blk_make_empty(old_backing_blk, &local_err); |
| 1095 | blk_unref(old_backing_blk); |
| 1096 | if (ret == -ENOTSUP) { |
| 1097 | error_free(local_err); |
| 1098 | local_err = NULL; |
| 1099 | } else if (ret < 0) { |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1100 | goto unref_backing; |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | unref_backing: |
| 1105 | if (!drop) { |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1106 | bdrv_unref(bs); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1107 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1108 | |
| 1109 | done: |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 1110 | qemu_progress_end(); |
| 1111 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1112 | blk_unref(blk); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1113 | |
| 1114 | if (local_err) { |
Markus Armbruster | 6936f29 | 2015-02-10 15:14:02 +0100 | [diff] [blame] | 1115 | error_report_err(local_err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1116 | return 1; |
| 1117 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1118 | |
| 1119 | qprintf(quiet, "Image committed.\n"); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1120 | return 0; |
| 1121 | } |
| 1122 | |
Dmitry Konishchev | f6a00aa | 2011-05-18 15:03:59 +0400 | [diff] [blame] | 1123 | /* |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1124 | * Returns -1 if 'buf' contains only zeroes, otherwise the byte index |
| 1125 | * of the first sector boundary within buf where the sector contains a |
| 1126 | * non-zero byte. This function is robust to a buffer that is not |
| 1127 | * sector-aligned. |
| 1128 | */ |
| 1129 | static int64_t find_nonzero(const uint8_t *buf, int64_t n) |
| 1130 | { |
| 1131 | int64_t i; |
| 1132 | int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE); |
| 1133 | |
| 1134 | for (i = 0; i < end; i += BDRV_SECTOR_SIZE) { |
| 1135 | if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) { |
| 1136 | return i; |
| 1137 | } |
| 1138 | } |
| 1139 | if (i < n && !buffer_is_zero(buf + i, n - end)) { |
| 1140 | return i; |
| 1141 | } |
| 1142 | return -1; |
| 1143 | } |
| 1144 | |
| 1145 | /* |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1146 | * Returns true iff the first sector pointed to by 'buf' contains at least |
| 1147 | * a non-NUL byte. |
| 1148 | * |
| 1149 | * 'pnum' is set to the number of sectors (including and immediately following |
| 1150 | * 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] | 1151 | * The function will try to align the end offset to alignment boundaries so |
zhaolichang | e3a6e0d | 2020-09-17 15:50:20 +0800 | [diff] [blame] | 1152 | * that the request will at least end aligned and consecutive requests will |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1153 | * also start at an aligned offset. |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1154 | */ |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1155 | static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum, |
| 1156 | int64_t sector_num, int alignment) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1157 | { |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1158 | bool is_zero; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1159 | int i, tail; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1160 | |
| 1161 | if (n <= 0) { |
| 1162 | *pnum = 0; |
| 1163 | return 0; |
| 1164 | } |
Yi Li | c075c42 | 2020-08-19 09:36:07 +0800 | [diff] [blame] | 1165 | is_zero = buffer_is_zero(buf, BDRV_SECTOR_SIZE); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1166 | for(i = 1; i < n; i++) { |
Yi Li | c075c42 | 2020-08-19 09:36:07 +0800 | [diff] [blame] | 1167 | buf += BDRV_SECTOR_SIZE; |
| 1168 | if (is_zero != buffer_is_zero(buf, BDRV_SECTOR_SIZE)) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1169 | break; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1170 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1171 | } |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1172 | |
| 1173 | tail = (sector_num + i) & (alignment - 1); |
| 1174 | if (tail) { |
| 1175 | if (is_zero && i <= tail) { |
| 1176 | /* treat unallocated areas which only consist |
| 1177 | * of a small tail as allocated. */ |
| 1178 | is_zero = false; |
| 1179 | } |
| 1180 | if (!is_zero) { |
| 1181 | /* align up end offset of allocated areas. */ |
| 1182 | i += alignment - tail; |
| 1183 | i = MIN(i, n); |
| 1184 | } else { |
| 1185 | /* align down end offset of zero areas. */ |
| 1186 | i -= tail; |
| 1187 | } |
| 1188 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1189 | *pnum = i; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1190 | return !is_zero; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1193 | /* |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1194 | * Like is_allocated_sectors, but if the buffer starts with a used sector, |
| 1195 | * up to 'min' consecutive sectors containing zeros are ignored. This avoids |
| 1196 | * breaking up write requests for only small sparse areas. |
| 1197 | */ |
| 1198 | 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] | 1199 | int min, int64_t sector_num, int alignment) |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1200 | { |
| 1201 | int ret; |
| 1202 | int num_checked, num_used; |
| 1203 | |
| 1204 | if (n < min) { |
| 1205 | min = n; |
| 1206 | } |
| 1207 | |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1208 | ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment); |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1209 | if (!ret) { |
| 1210 | return ret; |
| 1211 | } |
| 1212 | |
| 1213 | num_used = *pnum; |
| 1214 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 1215 | n -= *pnum; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1216 | sector_num += *pnum; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1217 | num_checked = num_used; |
| 1218 | |
| 1219 | while (n > 0) { |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1220 | ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment); |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1221 | |
| 1222 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 1223 | n -= *pnum; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1224 | sector_num += *pnum; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1225 | num_checked += *pnum; |
| 1226 | if (ret) { |
| 1227 | num_used = num_checked; |
| 1228 | } else if (*pnum >= min) { |
| 1229 | break; |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | *pnum = num_used; |
| 1234 | return 1; |
| 1235 | } |
| 1236 | |
| 1237 | /* |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1238 | * Compares two buffers sector by sector. Returns 0 if the first |
| 1239 | * sector of each buffer matches, non-zero otherwise. |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1240 | * |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1241 | * pnum is set to the sector-aligned size of the buffer prefix that |
| 1242 | * has the same matching status as the first sector. |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1243 | */ |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1244 | static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2, |
| 1245 | int64_t bytes, int64_t *pnum) |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1246 | { |
Radim Krčmář | 8c1ac47 | 2015-02-20 17:06:15 +0100 | [diff] [blame] | 1247 | bool res; |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1248 | int64_t i = MIN(bytes, BDRV_SECTOR_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1249 | |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1250 | assert(bytes > 0); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1251 | |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1252 | res = !!memcmp(buf1, buf2, i); |
| 1253 | while (i < bytes) { |
| 1254 | int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1255 | |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1256 | if (!!memcmp(buf1 + i, buf2 + i, len) != res) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1257 | break; |
| 1258 | } |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1259 | i += len; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | *pnum = i; |
| 1263 | return res; |
| 1264 | } |
| 1265 | |
Stefano Garzarella | 97ede57 | 2019-05-08 12:43:24 +0200 | [diff] [blame] | 1266 | #define IO_BUF_SIZE (2 * MiB) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1267 | |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1268 | /* |
| 1269 | * Check if passed sectors are empty (not allocated or contain only 0 bytes) |
| 1270 | * |
Eric Blake | 0608e40 | 2017-10-11 22:47:12 -0500 | [diff] [blame] | 1271 | * Intended for use by 'qemu-img compare': Returns 0 in case sectors are |
| 1272 | * filled with 0, 1 if sectors contain non-zero data (this is a comparison |
| 1273 | * failure), and 4 on error (the exit status for read errors), after emitting |
| 1274 | * an error message. |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1275 | * |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1276 | * @param blk: BlockBackend for the image |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1277 | * @param offset: Starting offset to check |
| 1278 | * @param bytes: Number of bytes to check |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1279 | * @param filename: Name of disk file we are checking (logging purpose) |
| 1280 | * @param buffer: Allocated buffer for storing read data |
| 1281 | * @param quiet: Flag for quiet mode |
| 1282 | */ |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1283 | static int check_empty_sectors(BlockBackend *blk, int64_t offset, |
| 1284 | int64_t bytes, const char *filename, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1285 | uint8_t *buffer, bool quiet) |
| 1286 | { |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1287 | int ret = 0; |
| 1288 | int64_t idx; |
| 1289 | |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1290 | ret = blk_pread(blk, offset, buffer, bytes); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1291 | if (ret < 0) { |
| 1292 | error_report("Error while reading offset %" PRId64 " of %s: %s", |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1293 | offset, filename, strerror(-ret)); |
Eric Blake | 0608e40 | 2017-10-11 22:47:12 -0500 | [diff] [blame] | 1294 | return 4; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1295 | } |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1296 | idx = find_nonzero(buffer, bytes); |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1297 | if (idx >= 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1298 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1299 | offset + idx); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1300 | return 1; |
| 1301 | } |
| 1302 | |
| 1303 | return 0; |
| 1304 | } |
| 1305 | |
| 1306 | /* |
| 1307 | * Compares two images. Exit codes: |
| 1308 | * |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 1309 | * 0 - Images are identical or the requested help was printed |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1310 | * 1 - Images differ |
| 1311 | * >1 - Error occurred |
| 1312 | */ |
| 1313 | static int img_compare(int argc, char **argv) |
| 1314 | { |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1315 | const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1316 | BlockBackend *blk1, *blk2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1317 | BlockDriverState *bs1, *bs2; |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1318 | int64_t total_size1, total_size2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1319 | uint8_t *buf1 = NULL, *buf2 = NULL; |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1320 | int64_t pnum1, pnum2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1321 | int allocated1, allocated2; |
| 1322 | int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */ |
| 1323 | bool progress = false, quiet = false, strict = false; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1324 | int flags; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1325 | bool writethrough; |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1326 | int64_t total_size; |
| 1327 | int64_t offset = 0; |
| 1328 | int64_t chunk; |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1329 | int c; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1330 | uint64_t progress_base; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1331 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1332 | bool force_share = false; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1333 | |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1334 | cache = BDRV_DEFAULT_CACHE; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1335 | for (;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1336 | static const struct option long_options[] = { |
| 1337 | {"help", no_argument, 0, 'h'}, |
| 1338 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1339 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1340 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1341 | {0, 0, 0, 0} |
| 1342 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1343 | c = getopt_long(argc, argv, ":hf:F:T:pqsU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1344 | long_options, NULL); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1345 | if (c == -1) { |
| 1346 | break; |
| 1347 | } |
| 1348 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 1349 | case ':': |
| 1350 | missing_argument(argv[optind - 1]); |
| 1351 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1352 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 1353 | unrecognized_option(argv[optind - 1]); |
| 1354 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1355 | case 'h': |
| 1356 | help(); |
| 1357 | break; |
| 1358 | case 'f': |
| 1359 | fmt1 = optarg; |
| 1360 | break; |
| 1361 | case 'F': |
| 1362 | fmt2 = optarg; |
| 1363 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1364 | case 'T': |
| 1365 | cache = optarg; |
| 1366 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1367 | case 'p': |
| 1368 | progress = true; |
| 1369 | break; |
| 1370 | case 'q': |
| 1371 | quiet = true; |
| 1372 | break; |
| 1373 | case 's': |
| 1374 | strict = true; |
| 1375 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1376 | case 'U': |
| 1377 | force_share = true; |
| 1378 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 1379 | case OPTION_OBJECT: |
| 1380 | { |
| 1381 | Error *local_err = NULL; |
| 1382 | |
| 1383 | if (!user_creatable_add_from_str(optarg, &local_err)) { |
| 1384 | if (local_err) { |
| 1385 | error_report_err(local_err); |
| 1386 | exit(2); |
| 1387 | } else { |
| 1388 | /* Help was printed */ |
| 1389 | exit(EXIT_SUCCESS); |
| 1390 | } |
| 1391 | } |
| 1392 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1393 | } |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1394 | case OPTION_IMAGE_OPTS: |
| 1395 | image_opts = true; |
| 1396 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | /* Progress is not shown in Quiet mode */ |
| 1401 | if (quiet) { |
| 1402 | progress = false; |
| 1403 | } |
| 1404 | |
| 1405 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 1406 | if (optind != argc - 2) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 1407 | error_exit("Expecting two image file names"); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1408 | } |
| 1409 | filename1 = argv[optind++]; |
| 1410 | filename2 = argv[optind++]; |
| 1411 | |
Stefan Hajnoczi | cbda016 | 2014-08-26 19:17:55 +0100 | [diff] [blame] | 1412 | /* Initialize before goto out */ |
| 1413 | qemu_progress_init(progress, 2.0); |
| 1414 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1415 | flags = 0; |
| 1416 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1417 | if (ret < 0) { |
| 1418 | error_report("Invalid source cache option: %s", cache); |
| 1419 | ret = 2; |
| 1420 | goto out3; |
| 1421 | } |
| 1422 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1423 | blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet, |
| 1424 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1425 | if (!blk1) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1426 | ret = 2; |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1427 | goto out3; |
| 1428 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1429 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1430 | blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet, |
| 1431 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1432 | if (!blk2) { |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1433 | ret = 2; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1434 | goto out2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1435 | } |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1436 | bs1 = blk_bs(blk1); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1437 | bs2 = blk_bs(blk2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1438 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1439 | buf1 = blk_blockalign(blk1, IO_BUF_SIZE); |
| 1440 | buf2 = blk_blockalign(blk2, IO_BUF_SIZE); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1441 | total_size1 = blk_getlength(blk1); |
| 1442 | if (total_size1 < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1443 | error_report("Can't get size of %s: %s", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1444 | filename1, strerror(-total_size1)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1445 | ret = 4; |
| 1446 | goto out; |
| 1447 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1448 | total_size2 = blk_getlength(blk2); |
| 1449 | if (total_size2 < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1450 | error_report("Can't get size of %s: %s", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1451 | filename2, strerror(-total_size2)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1452 | ret = 4; |
| 1453 | goto out; |
| 1454 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1455 | total_size = MIN(total_size1, total_size2); |
| 1456 | progress_base = MAX(total_size1, total_size2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1457 | |
| 1458 | qemu_progress_print(0, 100); |
| 1459 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1460 | if (strict && total_size1 != total_size2) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1461 | ret = 1; |
| 1462 | qprintf(quiet, "Strict mode: Image size mismatch!\n"); |
| 1463 | goto out; |
| 1464 | } |
| 1465 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1466 | while (offset < total_size) { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1467 | int status1, status2; |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 1468 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1469 | status1 = bdrv_block_status_above(bs1, NULL, offset, |
| 1470 | total_size1 - offset, &pnum1, NULL, |
| 1471 | NULL); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1472 | if (status1 < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1473 | ret = 3; |
| 1474 | error_report("Sector allocation test failed for %s", filename1); |
| 1475 | goto out; |
| 1476 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1477 | allocated1 = status1 & BDRV_BLOCK_ALLOCATED; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1478 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1479 | status2 = bdrv_block_status_above(bs2, NULL, offset, |
| 1480 | total_size2 - offset, &pnum2, NULL, |
| 1481 | NULL); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1482 | if (status2 < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1483 | ret = 3; |
| 1484 | error_report("Sector allocation test failed for %s", filename2); |
| 1485 | goto out; |
| 1486 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1487 | allocated2 = status2 & BDRV_BLOCK_ALLOCATED; |
Eric Blake | 7daddc6 | 2017-10-11 22:47:09 -0500 | [diff] [blame] | 1488 | |
| 1489 | assert(pnum1 && pnum2); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1490 | chunk = MIN(pnum1, pnum2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1491 | |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1492 | if (strict) { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1493 | if (status1 != status2) { |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1494 | ret = 1; |
| 1495 | qprintf(quiet, "Strict mode: Offset %" PRId64 |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1496 | " block status mismatch!\n", offset); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1497 | goto out; |
| 1498 | } |
| 1499 | } |
| 1500 | if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) { |
Eric Blake | 7daddc6 | 2017-10-11 22:47:09 -0500 | [diff] [blame] | 1501 | /* nothing to do */ |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1502 | } else if (allocated1 == allocated2) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1503 | if (allocated1) { |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1504 | int64_t pnum; |
| 1505 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1506 | chunk = MIN(chunk, IO_BUF_SIZE); |
| 1507 | ret = blk_pread(blk1, offset, buf1, chunk); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1508 | if (ret < 0) { |
| 1509 | error_report("Error while reading offset %" PRId64 |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1510 | " of %s: %s", |
| 1511 | offset, filename1, strerror(-ret)); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1512 | ret = 4; |
| 1513 | goto out; |
| 1514 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1515 | ret = blk_pread(blk2, offset, buf2, chunk); |
| 1516 | if (ret < 0) { |
| 1517 | error_report("Error while reading offset %" PRId64 |
| 1518 | " of %s: %s", |
| 1519 | offset, filename2, strerror(-ret)); |
| 1520 | ret = 4; |
| 1521 | goto out; |
| 1522 | } |
| 1523 | ret = compare_buffers(buf1, buf2, chunk, &pnum); |
| 1524 | if (ret || pnum != chunk) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1525 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1526 | offset + (ret ? 0 : pnum)); |
Fam Zheng | 36452f1 | 2013-11-13 20:26:49 +0800 | [diff] [blame] | 1527 | ret = 1; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1528 | goto out; |
| 1529 | } |
| 1530 | } |
| 1531 | } else { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1532 | chunk = MIN(chunk, IO_BUF_SIZE); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1533 | if (allocated1) { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1534 | ret = check_empty_sectors(blk1, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1535 | filename1, buf1, quiet); |
| 1536 | } else { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1537 | ret = check_empty_sectors(blk2, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1538 | filename2, buf1, quiet); |
| 1539 | } |
| 1540 | if (ret) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1541 | goto out; |
| 1542 | } |
| 1543 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1544 | offset += chunk; |
| 1545 | qemu_progress_print(((float) chunk / progress_base) * 100, 100); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1546 | } |
| 1547 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1548 | if (total_size1 != total_size2) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1549 | BlockBackend *blk_over; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1550 | const char *filename_over; |
| 1551 | |
| 1552 | qprintf(quiet, "Warning: Image size mismatch!\n"); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1553 | if (total_size1 > total_size2) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1554 | blk_over = blk1; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1555 | filename_over = filename1; |
| 1556 | } else { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1557 | blk_over = blk2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1558 | filename_over = filename2; |
| 1559 | } |
| 1560 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1561 | while (offset < progress_base) { |
| 1562 | ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset, |
| 1563 | progress_base - offset, &chunk, |
| 1564 | NULL, NULL); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1565 | if (ret < 0) { |
| 1566 | ret = 3; |
| 1567 | error_report("Sector allocation test failed for %s", |
| 1568 | filename_over); |
| 1569 | goto out; |
| 1570 | |
| 1571 | } |
Eric Blake | 391cb1a | 2017-10-11 22:47:10 -0500 | [diff] [blame] | 1572 | if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1573 | chunk = MIN(chunk, IO_BUF_SIZE); |
| 1574 | ret = check_empty_sectors(blk_over, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1575 | filename_over, buf1, quiet); |
| 1576 | if (ret) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1577 | goto out; |
| 1578 | } |
| 1579 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1580 | offset += chunk; |
| 1581 | qemu_progress_print(((float) chunk / progress_base) * 100, 100); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | qprintf(quiet, "Images are identical.\n"); |
| 1586 | ret = 0; |
| 1587 | |
| 1588 | out: |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1589 | qemu_vfree(buf1); |
| 1590 | qemu_vfree(buf2); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1591 | blk_unref(blk2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1592 | out2: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1593 | blk_unref(blk1); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1594 | out3: |
| 1595 | qemu_progress_end(); |
| 1596 | return ret; |
| 1597 | } |
| 1598 | |
Eric Blake | 6c729dd | 2020-05-21 14:21:35 -0500 | [diff] [blame] | 1599 | /* Convenience wrapper around qmp_block_dirty_bitmap_merge */ |
| 1600 | static void do_dirty_bitmap_merge(const char *dst_node, const char *dst_name, |
| 1601 | const char *src_node, const char *src_name, |
| 1602 | Error **errp) |
| 1603 | { |
| 1604 | BlockDirtyBitmapMergeSource *merge_src; |
Eric Blake | 54aa3de | 2020-11-12 19:13:37 -0600 | [diff] [blame] | 1605 | BlockDirtyBitmapMergeSourceList *list = NULL; |
Eric Blake | 6c729dd | 2020-05-21 14:21:35 -0500 | [diff] [blame] | 1606 | |
| 1607 | merge_src = g_new0(BlockDirtyBitmapMergeSource, 1); |
| 1608 | merge_src->type = QTYPE_QDICT; |
| 1609 | merge_src->u.external.node = g_strdup(src_node); |
| 1610 | merge_src->u.external.name = g_strdup(src_name); |
Eric Blake | 54aa3de | 2020-11-12 19:13:37 -0600 | [diff] [blame] | 1611 | QAPI_LIST_PREPEND(list, merge_src); |
Eric Blake | 6c729dd | 2020-05-21 14:21:35 -0500 | [diff] [blame] | 1612 | qmp_block_dirty_bitmap_merge(dst_node, dst_name, list, errp); |
| 1613 | qapi_free_BlockDirtyBitmapMergeSourceList(list); |
| 1614 | } |
| 1615 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1616 | enum ImgConvertBlockStatus { |
| 1617 | BLK_DATA, |
| 1618 | BLK_ZERO, |
| 1619 | BLK_BACKING_FILE, |
| 1620 | }; |
| 1621 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1622 | #define MAX_COROUTINES 16 |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 1623 | #define CONVERT_THROTTLE_GROUP "img_convert" |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1624 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1625 | typedef struct ImgConvertState { |
| 1626 | BlockBackend **src; |
| 1627 | int64_t *src_sectors; |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 1628 | int *src_alignment; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1629 | int src_num; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1630 | int64_t total_sectors; |
| 1631 | int64_t allocated_sectors; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1632 | int64_t allocated_done; |
| 1633 | int64_t sector_num; |
| 1634 | int64_t wr_offs; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1635 | enum ImgConvertBlockStatus status; |
| 1636 | int64_t sector_next_status; |
| 1637 | BlockBackend *target; |
| 1638 | bool has_zero_init; |
| 1639 | bool compressed; |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 1640 | bool target_is_new; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1641 | bool target_has_backing; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1642 | int64_t target_backing_sectors; /* negative if unknown */ |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1643 | bool wr_in_order; |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1644 | bool copy_range; |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1645 | bool salvage; |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 1646 | bool quiet; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1647 | int min_sparse; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1648 | int alignment; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1649 | size_t cluster_sectors; |
| 1650 | size_t buf_sectors; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 1651 | long num_coroutines; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1652 | int running_coroutines; |
| 1653 | Coroutine *co[MAX_COROUTINES]; |
| 1654 | int64_t wait_sector_num[MAX_COROUTINES]; |
| 1655 | CoMutex lock; |
| 1656 | int ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1657 | } ImgConvertState; |
| 1658 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1659 | static void convert_select_part(ImgConvertState *s, int64_t sector_num, |
| 1660 | int *src_cur, int64_t *src_cur_offset) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1661 | { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1662 | *src_cur = 0; |
| 1663 | *src_cur_offset = 0; |
| 1664 | while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) { |
| 1665 | *src_cur_offset += s->src_sectors[*src_cur]; |
| 1666 | (*src_cur)++; |
| 1667 | assert(*src_cur < s->src_num); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1668 | } |
| 1669 | } |
| 1670 | |
| 1671 | static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num) |
| 1672 | { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1673 | int64_t src_cur_offset; |
| 1674 | int ret, n, src_cur; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1675 | bool post_backing_zero = false; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1676 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1677 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1678 | |
| 1679 | assert(s->total_sectors > sector_num); |
| 1680 | n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS); |
| 1681 | |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1682 | if (s->target_backing_sectors >= 0) { |
| 1683 | if (sector_num >= s->target_backing_sectors) { |
Vladimir Sementsov-Ogievskiy | 2253d86 | 2020-05-28 12:43:56 +0300 | [diff] [blame] | 1684 | post_backing_zero = true; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1685 | } else if (sector_num + n > s->target_backing_sectors) { |
| 1686 | /* Split requests around target_backing_sectors (because |
| 1687 | * starting from there, zeros are handled differently) */ |
| 1688 | n = s->target_backing_sectors - sector_num; |
| 1689 | } |
| 1690 | } |
| 1691 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1692 | if (s->sector_next_status <= sector_num) { |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1693 | uint64_t offset = (sector_num - src_cur_offset) * BDRV_SECTOR_SIZE; |
| 1694 | int64_t count; |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 1695 | int tail; |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 1696 | BlockDriverState *src_bs = blk_bs(s->src[src_cur]); |
| 1697 | BlockDriverState *base; |
| 1698 | |
| 1699 | if (s->target_has_backing) { |
| 1700 | base = bdrv_cow_bs(bdrv_skip_filters(src_bs)); |
| 1701 | } else { |
| 1702 | base = NULL; |
| 1703 | } |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1704 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1705 | do { |
| 1706 | count = n * BDRV_SECTOR_SIZE; |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 1707 | |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 1708 | ret = bdrv_block_status_above(src_bs, base, offset, count, &count, |
| 1709 | NULL, NULL); |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1710 | |
| 1711 | if (ret < 0) { |
| 1712 | if (s->salvage) { |
| 1713 | if (n == 1) { |
| 1714 | if (!s->quiet) { |
| 1715 | warn_report("error while reading block status at " |
| 1716 | "offset %" PRIu64 ": %s", offset, |
| 1717 | strerror(-ret)); |
| 1718 | } |
| 1719 | /* Just try to read the data, then */ |
| 1720 | ret = BDRV_BLOCK_DATA; |
| 1721 | count = BDRV_SECTOR_SIZE; |
| 1722 | } else { |
| 1723 | /* Retry on a shorter range */ |
| 1724 | n = DIV_ROUND_UP(n, 4); |
| 1725 | } |
| 1726 | } else { |
| 1727 | error_report("error while reading block status at offset " |
| 1728 | "%" PRIu64 ": %s", offset, strerror(-ret)); |
| 1729 | return ret; |
| 1730 | } |
| 1731 | } |
| 1732 | } while (ret < 0); |
| 1733 | |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1734 | n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1735 | |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 1736 | /* |
| 1737 | * Avoid that s->sector_next_status becomes unaligned to the source |
| 1738 | * request alignment and/or cluster size to avoid unnecessary read |
| 1739 | * cycles. |
| 1740 | */ |
| 1741 | tail = (sector_num - src_cur_offset + n) % s->src_alignment[src_cur]; |
| 1742 | if (n > tail) { |
| 1743 | n -= tail; |
| 1744 | } |
| 1745 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1746 | if (ret & BDRV_BLOCK_ZERO) { |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1747 | s->status = post_backing_zero ? BLK_BACKING_FILE : BLK_ZERO; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1748 | } else if (ret & BDRV_BLOCK_DATA) { |
| 1749 | s->status = BLK_DATA; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1750 | } else { |
Vladimir Sementsov-Ogievskiy | 9f1b92a | 2017-04-07 14:34:04 +0300 | [diff] [blame] | 1751 | s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1752 | } |
| 1753 | |
| 1754 | s->sector_next_status = sector_num + n; |
| 1755 | } |
| 1756 | |
| 1757 | n = MIN(n, s->sector_next_status - sector_num); |
| 1758 | if (s->status == BLK_DATA) { |
| 1759 | n = MIN(n, s->buf_sectors); |
| 1760 | } |
| 1761 | |
| 1762 | /* We need to write complete clusters for compressed images, so if an |
| 1763 | * unallocated area is shorter than that, we must consider the whole |
| 1764 | * cluster allocated. */ |
| 1765 | if (s->compressed) { |
| 1766 | if (n < s->cluster_sectors) { |
| 1767 | n = MIN(s->cluster_sectors, s->total_sectors - sector_num); |
| 1768 | s->status = BLK_DATA; |
| 1769 | } else { |
| 1770 | n = QEMU_ALIGN_DOWN(n, s->cluster_sectors); |
| 1771 | } |
| 1772 | } |
| 1773 | |
| 1774 | return n; |
| 1775 | } |
| 1776 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1777 | static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num, |
| 1778 | int nb_sectors, uint8_t *buf) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1779 | { |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1780 | uint64_t single_read_until = 0; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1781 | int n, ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1782 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1783 | assert(nb_sectors <= s->buf_sectors); |
| 1784 | while (nb_sectors > 0) { |
| 1785 | BlockBackend *blk; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1786 | int src_cur; |
| 1787 | int64_t bs_sectors, src_cur_offset; |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1788 | uint64_t offset; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1789 | |
| 1790 | /* In the case of compression with multiple source files, we can get a |
| 1791 | * nb_sectors that spreads into the next part. So we must be able to |
| 1792 | * read across multiple BDSes for one convert_read() call. */ |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1793 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
| 1794 | blk = s->src[src_cur]; |
| 1795 | bs_sectors = s->src_sectors[src_cur]; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1796 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1797 | offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1798 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1799 | n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset)); |
| 1800 | if (single_read_until > offset) { |
| 1801 | n = 1; |
| 1802 | } |
| 1803 | |
| 1804 | ret = blk_co_pread(blk, offset, n << BDRV_SECTOR_BITS, buf, 0); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1805 | if (ret < 0) { |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1806 | if (s->salvage) { |
| 1807 | if (n > 1) { |
| 1808 | single_read_until = offset + (n << BDRV_SECTOR_BITS); |
| 1809 | continue; |
| 1810 | } else { |
| 1811 | if (!s->quiet) { |
| 1812 | warn_report("error while reading offset %" PRIu64 |
| 1813 | ": %s", offset, strerror(-ret)); |
| 1814 | } |
| 1815 | memset(buf, 0, BDRV_SECTOR_SIZE); |
| 1816 | } |
| 1817 | } else { |
| 1818 | return ret; |
| 1819 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | sector_num += n; |
| 1823 | nb_sectors -= n; |
| 1824 | buf += n * BDRV_SECTOR_SIZE; |
| 1825 | } |
| 1826 | |
| 1827 | return 0; |
| 1828 | } |
| 1829 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1830 | |
| 1831 | static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num, |
| 1832 | int nb_sectors, uint8_t *buf, |
| 1833 | enum ImgConvertBlockStatus status) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1834 | { |
| 1835 | int ret; |
| 1836 | |
| 1837 | while (nb_sectors > 0) { |
| 1838 | int n = nb_sectors; |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1839 | BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0; |
| 1840 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1841 | switch (status) { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1842 | case BLK_BACKING_FILE: |
| 1843 | /* If we have a backing file, leave clusters unallocated that are |
| 1844 | * unallocated in the source image, so that the backing file is |
| 1845 | * visible at the respective offset. */ |
| 1846 | assert(s->target_has_backing); |
| 1847 | break; |
| 1848 | |
| 1849 | case BLK_DATA: |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1850 | /* If we're told to keep the target fully allocated (-S 0) or there |
| 1851 | * is real non-zero data, we must write it. Otherwise we can treat |
| 1852 | * it as zero sectors. |
| 1853 | * Compressed clusters need to be written as a whole, so in that |
| 1854 | * case we can only save the write if the buffer is completely |
| 1855 | * zeroed. */ |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1856 | if (!s->min_sparse || |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1857 | (!s->compressed && |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1858 | is_allocated_sectors_min(buf, n, &n, s->min_sparse, |
| 1859 | sector_num, s->alignment)) || |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1860 | (s->compressed && |
| 1861 | !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1862 | { |
Vladimir Sementsov-Ogievskiy | 265a7e5 | 2019-04-22 17:58:38 +0300 | [diff] [blame] | 1863 | ret = blk_co_pwrite(s->target, sector_num << BDRV_SECTOR_BITS, |
| 1864 | n << BDRV_SECTOR_BITS, buf, flags); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1865 | if (ret < 0) { |
| 1866 | return ret; |
| 1867 | } |
| 1868 | break; |
| 1869 | } |
| 1870 | /* fall-through */ |
| 1871 | |
| 1872 | case BLK_ZERO: |
| 1873 | if (s->has_zero_init) { |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1874 | assert(!s->target_has_backing); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1875 | break; |
| 1876 | } |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1877 | ret = blk_co_pwrite_zeroes(s->target, |
| 1878 | sector_num << BDRV_SECTOR_BITS, |
Nir Soffer | a3d6ae2 | 2019-03-24 02:20:12 +0200 | [diff] [blame] | 1879 | n << BDRV_SECTOR_BITS, |
| 1880 | BDRV_REQ_MAY_UNMAP); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1881 | if (ret < 0) { |
| 1882 | return ret; |
| 1883 | } |
| 1884 | break; |
| 1885 | } |
| 1886 | |
| 1887 | sector_num += n; |
| 1888 | nb_sectors -= n; |
| 1889 | buf += n * BDRV_SECTOR_SIZE; |
| 1890 | } |
| 1891 | |
| 1892 | return 0; |
| 1893 | } |
| 1894 | |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1895 | static int coroutine_fn convert_co_copy_range(ImgConvertState *s, int64_t sector_num, |
| 1896 | int nb_sectors) |
| 1897 | { |
| 1898 | int n, ret; |
| 1899 | |
| 1900 | while (nb_sectors > 0) { |
| 1901 | BlockBackend *blk; |
| 1902 | int src_cur; |
| 1903 | int64_t bs_sectors, src_cur_offset; |
| 1904 | int64_t offset; |
| 1905 | |
| 1906 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
| 1907 | offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS; |
| 1908 | blk = s->src[src_cur]; |
| 1909 | bs_sectors = s->src_sectors[src_cur]; |
| 1910 | |
| 1911 | n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset)); |
| 1912 | |
| 1913 | ret = blk_co_copy_range(blk, offset, s->target, |
| 1914 | sector_num << BDRV_SECTOR_BITS, |
Vladimir Sementsov-Ogievskiy | 67b51fb | 2018-07-09 19:37:17 +0300 | [diff] [blame] | 1915 | n << BDRV_SECTOR_BITS, 0, 0); |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1916 | if (ret < 0) { |
| 1917 | return ret; |
| 1918 | } |
| 1919 | |
| 1920 | sector_num += n; |
| 1921 | nb_sectors -= n; |
| 1922 | } |
| 1923 | return 0; |
| 1924 | } |
| 1925 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1926 | static void coroutine_fn convert_co_do_copy(void *opaque) |
| 1927 | { |
| 1928 | ImgConvertState *s = opaque; |
| 1929 | uint8_t *buf = NULL; |
| 1930 | int ret, i; |
| 1931 | int index = -1; |
| 1932 | |
| 1933 | for (i = 0; i < s->num_coroutines; i++) { |
| 1934 | if (s->co[i] == qemu_coroutine_self()) { |
| 1935 | index = i; |
| 1936 | break; |
| 1937 | } |
| 1938 | } |
| 1939 | assert(index >= 0); |
| 1940 | |
| 1941 | s->running_coroutines++; |
| 1942 | buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE); |
| 1943 | |
| 1944 | while (1) { |
| 1945 | int n; |
| 1946 | int64_t sector_num; |
| 1947 | enum ImgConvertBlockStatus status; |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1948 | bool copy_range; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1949 | |
| 1950 | qemu_co_mutex_lock(&s->lock); |
| 1951 | if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) { |
| 1952 | qemu_co_mutex_unlock(&s->lock); |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1953 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1954 | } |
| 1955 | n = convert_iteration_sectors(s, s->sector_num); |
| 1956 | if (n < 0) { |
| 1957 | qemu_co_mutex_unlock(&s->lock); |
| 1958 | s->ret = n; |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1959 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1960 | } |
| 1961 | /* save current sector and allocation status to local variables */ |
| 1962 | sector_num = s->sector_num; |
| 1963 | status = s->status; |
| 1964 | if (!s->min_sparse && s->status == BLK_ZERO) { |
| 1965 | n = MIN(n, s->buf_sectors); |
| 1966 | } |
| 1967 | /* increment global sector counter so that other coroutines can |
| 1968 | * already continue reading beyond this request */ |
| 1969 | s->sector_num += n; |
| 1970 | qemu_co_mutex_unlock(&s->lock); |
| 1971 | |
| 1972 | if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) { |
| 1973 | s->allocated_done += n; |
| 1974 | qemu_progress_print(100.0 * s->allocated_done / |
| 1975 | s->allocated_sectors, 0); |
| 1976 | } |
| 1977 | |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1978 | retry: |
| 1979 | copy_range = s->copy_range && s->status == BLK_DATA; |
| 1980 | if (status == BLK_DATA && !copy_range) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1981 | ret = convert_co_read(s, sector_num, n, buf); |
| 1982 | if (ret < 0) { |
Eric Blake | 39f77cb | 2020-04-02 08:57:17 -0500 | [diff] [blame] | 1983 | error_report("error while reading at byte %lld: %s", |
| 1984 | sector_num * BDRV_SECTOR_SIZE, strerror(-ret)); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1985 | s->ret = ret; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1986 | } |
| 1987 | } else if (!s->min_sparse && status == BLK_ZERO) { |
| 1988 | status = BLK_DATA; |
| 1989 | memset(buf, 0x00, n * BDRV_SECTOR_SIZE); |
| 1990 | } |
| 1991 | |
| 1992 | if (s->wr_in_order) { |
| 1993 | /* keep writes in order */ |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1994 | while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1995 | s->wait_sector_num[index] = sector_num; |
| 1996 | qemu_coroutine_yield(); |
| 1997 | } |
| 1998 | s->wait_sector_num[index] = -1; |
| 1999 | } |
| 2000 | |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2001 | if (s->ret == -EINPROGRESS) { |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 2002 | if (copy_range) { |
| 2003 | ret = convert_co_copy_range(s, sector_num, n); |
| 2004 | if (ret) { |
| 2005 | s->copy_range = false; |
| 2006 | goto retry; |
| 2007 | } |
| 2008 | } else { |
| 2009 | ret = convert_co_write(s, sector_num, n, buf, status); |
| 2010 | } |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2011 | if (ret < 0) { |
Eric Blake | 39f77cb | 2020-04-02 08:57:17 -0500 | [diff] [blame] | 2012 | error_report("error while writing at byte %lld: %s", |
| 2013 | sector_num * BDRV_SECTOR_SIZE, strerror(-ret)); |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2014 | s->ret = ret; |
| 2015 | } |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2016 | } |
| 2017 | |
| 2018 | if (s->wr_in_order) { |
| 2019 | /* reenter the coroutine that might have waited |
| 2020 | * for this write to complete */ |
| 2021 | s->wr_offs = sector_num + n; |
| 2022 | for (i = 0; i < s->num_coroutines; i++) { |
| 2023 | if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) { |
| 2024 | /* |
| 2025 | * A -> B -> A cannot occur because A has |
| 2026 | * s->wait_sector_num[i] == -1 during A -> B. Therefore |
| 2027 | * B will never enter A during this time window. |
| 2028 | */ |
| 2029 | qemu_coroutine_enter(s->co[i]); |
| 2030 | break; |
| 2031 | } |
| 2032 | } |
| 2033 | } |
| 2034 | } |
| 2035 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2036 | qemu_vfree(buf); |
| 2037 | s->co[index] = NULL; |
| 2038 | s->running_coroutines--; |
| 2039 | if (!s->running_coroutines && s->ret == -EINPROGRESS) { |
| 2040 | /* the convert job finished successfully */ |
| 2041 | s->ret = 0; |
| 2042 | } |
| 2043 | } |
| 2044 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2045 | static int convert_do_copy(ImgConvertState *s) |
| 2046 | { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2047 | int ret, i, n; |
| 2048 | int64_t sector_num = 0; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2049 | |
| 2050 | /* Check whether we have zero initialisation or can get it efficiently */ |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2051 | if (!s->has_zero_init && s->target_is_new && s->min_sparse && |
| 2052 | !s->target_has_backing) { |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 2053 | s->has_zero_init = bdrv_has_zero_init(blk_bs(s->target)); |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 2054 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2055 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2056 | /* Allocate buffer for copied data. For compressed images, only one cluster |
| 2057 | * can be copied at a time. */ |
| 2058 | if (s->compressed) { |
| 2059 | if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) { |
| 2060 | error_report("invalid cluster size"); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2061 | return -EINVAL; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2062 | } |
| 2063 | s->buf_sectors = s->cluster_sectors; |
| 2064 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2065 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2066 | while (sector_num < s->total_sectors) { |
| 2067 | n = convert_iteration_sectors(s, sector_num); |
| 2068 | if (n < 0) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2069 | return n; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2070 | } |
Max Reitz | aad15de | 2016-03-24 23:33:57 +0100 | [diff] [blame] | 2071 | if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO)) |
| 2072 | { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2073 | s->allocated_sectors += n; |
| 2074 | } |
| 2075 | sector_num += n; |
| 2076 | } |
| 2077 | |
| 2078 | /* Do the copy */ |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2079 | s->sector_next_status = 0; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2080 | s->ret = -EINPROGRESS; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2081 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2082 | qemu_co_mutex_init(&s->lock); |
| 2083 | for (i = 0; i < s->num_coroutines; i++) { |
| 2084 | s->co[i] = qemu_coroutine_create(convert_co_do_copy, s); |
| 2085 | s->wait_sector_num[i] = -1; |
| 2086 | qemu_coroutine_enter(s->co[i]); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2087 | } |
| 2088 | |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2089 | while (s->running_coroutines) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2090 | main_loop_wait(false); |
| 2091 | } |
| 2092 | |
| 2093 | if (s->compressed && !s->ret) { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2094 | /* signal EOF to align */ |
Pavel Butsykin | fe5c135 | 2016-07-22 11:17:40 +0300 | [diff] [blame] | 2095 | ret = blk_pwrite_compressed(s->target, 0, NULL, 0); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2096 | if (ret < 0) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2097 | return ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2098 | } |
| 2099 | } |
| 2100 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2101 | return s->ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2102 | } |
| 2103 | |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2104 | static int convert_copy_bitmaps(BlockDriverState *src, BlockDriverState *dst) |
| 2105 | { |
| 2106 | BdrvDirtyBitmap *bm; |
| 2107 | Error *err = NULL; |
| 2108 | |
| 2109 | FOR_EACH_DIRTY_BITMAP(src, bm) { |
| 2110 | const char *name; |
| 2111 | |
| 2112 | if (!bdrv_dirty_bitmap_get_persistence(bm)) { |
| 2113 | continue; |
| 2114 | } |
| 2115 | name = bdrv_dirty_bitmap_name(bm); |
| 2116 | qmp_block_dirty_bitmap_add(dst->node_name, name, |
| 2117 | true, bdrv_dirty_bitmap_granularity(bm), |
| 2118 | true, true, |
| 2119 | true, !bdrv_dirty_bitmap_enabled(bm), |
| 2120 | &err); |
| 2121 | if (err) { |
| 2122 | error_reportf_err(err, "Failed to create bitmap %s: ", name); |
| 2123 | return -1; |
| 2124 | } |
| 2125 | |
| 2126 | do_dirty_bitmap_merge(dst->node_name, name, src->node_name, name, |
| 2127 | &err); |
| 2128 | if (err) { |
| 2129 | error_reportf_err(err, "Failed to populate bitmap %s: ", name); |
| 2130 | return -1; |
| 2131 | } |
| 2132 | } |
| 2133 | |
| 2134 | return 0; |
| 2135 | } |
| 2136 | |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 2137 | #define MAX_BUF_SECTORS 32768 |
| 2138 | |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 2139 | static void set_rate_limit(BlockBackend *blk, int64_t rate_limit) |
| 2140 | { |
| 2141 | ThrottleConfig cfg; |
| 2142 | |
| 2143 | throttle_config_init(&cfg); |
| 2144 | cfg.buckets[THROTTLE_BPS_WRITE].avg = rate_limit; |
| 2145 | |
| 2146 | blk_io_limits_enable(blk, CONVERT_THROTTLE_GROUP); |
| 2147 | blk_set_io_limits(blk, &cfg); |
| 2148 | } |
| 2149 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2150 | static int img_convert(int argc, char **argv) |
| 2151 | { |
Kevin Wolf | 0b8fb55 | 2021-04-22 18:43:44 +0200 | [diff] [blame] | 2152 | int c, bs_i, flags, src_flags = BDRV_O_NO_SHARE; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2153 | const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe", |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2154 | *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL, |
| 2155 | *out_filename, *out_baseimg_param, *snapshot_name = NULL; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2156 | BlockDriver *drv = NULL, *proto_drv = NULL; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2157 | BlockDriverInfo bdi; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2158 | BlockDriverState *out_bs; |
| 2159 | QemuOpts *opts = NULL, *sn_opts = NULL; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2160 | QemuOptsList *create_opts = NULL; |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2161 | QDict *open_opts = NULL; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2162 | char *options = NULL; |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 2163 | Error *local_err = NULL; |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2164 | bool writethrough, src_writethrough, image_opts = false, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2165 | skip_create = false, progress = false, tgt_image_opts = false; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2166 | int64_t ret = -EINVAL; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2167 | bool force_share = false; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2168 | bool explict_min_sparse = false; |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2169 | bool bitmaps = false; |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 2170 | int64_t rate_limit = 0; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2171 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2172 | ImgConvertState s = (ImgConvertState) { |
| 2173 | /* Need at least 4k of zeros for sparse detection */ |
| 2174 | .min_sparse = 8, |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2175 | .copy_range = false, |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2176 | .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE, |
| 2177 | .wr_in_order = true, |
| 2178 | .num_coroutines = 8, |
| 2179 | }; |
| 2180 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2181 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2182 | static const struct option long_options[] = { |
| 2183 | {"help", no_argument, 0, 'h'}, |
| 2184 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2185 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2186 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2187 | {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS}, |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 2188 | {"salvage", no_argument, 0, OPTION_SALVAGE}, |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2189 | {"target-is-zero", no_argument, 0, OPTION_TARGET_IS_ZERO}, |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2190 | {"bitmaps", no_argument, 0, OPTION_BITMAPS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2191 | {0, 0, 0, 0} |
| 2192 | }; |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 2193 | c = getopt_long(argc, argv, ":hf:O:B:Cco:l:S:pt:T:qnm:WUr:", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2194 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2195 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2196 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2197 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2198 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2199 | case ':': |
| 2200 | missing_argument(argv[optind - 1]); |
| 2201 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2202 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2203 | unrecognized_option(argv[optind - 1]); |
| 2204 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2205 | case 'h': |
| 2206 | help(); |
| 2207 | break; |
| 2208 | case 'f': |
| 2209 | fmt = optarg; |
| 2210 | break; |
| 2211 | case 'O': |
| 2212 | out_fmt = optarg; |
| 2213 | break; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 2214 | case 'B': |
| 2215 | out_baseimg = optarg; |
| 2216 | break; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2217 | case 'C': |
| 2218 | s.copy_range = true; |
| 2219 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2220 | case 'c': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2221 | s.compressed = true; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2222 | break; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2223 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 2224 | if (accumulate_options(&options, optarg) < 0) { |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2225 | goto fail_getopt; |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 2226 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2227 | break; |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2228 | case 'l': |
| 2229 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { |
Markus Armbruster | 70b9433 | 2015-02-13 12:50:26 +0100 | [diff] [blame] | 2230 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
| 2231 | optarg, false); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2232 | if (!sn_opts) { |
| 2233 | error_report("Failed in parsing snapshot param '%s'", |
| 2234 | optarg); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2235 | goto fail_getopt; |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2236 | } |
| 2237 | } else { |
| 2238 | snapshot_name = optarg; |
| 2239 | } |
| 2240 | break; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2241 | case 'S': |
| 2242 | { |
| 2243 | int64_t sval; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 2244 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 2245 | sval = cvtnum("buffer size for sparse output", optarg); |
| 2246 | if (sval < 0) { |
| 2247 | goto fail_getopt; |
| 2248 | } else if (!QEMU_IS_ALIGNED(sval, BDRV_SECTOR_SIZE) || |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 2249 | sval / BDRV_SECTOR_SIZE > MAX_BUF_SECTORS) { |
| 2250 | error_report("Invalid buffer size for sparse output specified. " |
| 2251 | "Valid sizes are multiples of %llu up to %llu. Select " |
| 2252 | "0 to disable sparse detection (fully allocates output).", |
| 2253 | BDRV_SECTOR_SIZE, MAX_BUF_SECTORS * BDRV_SECTOR_SIZE); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2254 | goto fail_getopt; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2255 | } |
| 2256 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2257 | s.min_sparse = sval / BDRV_SECTOR_SIZE; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2258 | explict_min_sparse = true; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2259 | break; |
| 2260 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2261 | case 'p': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2262 | progress = true; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2263 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2264 | case 't': |
| 2265 | cache = optarg; |
| 2266 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 2267 | case 'T': |
| 2268 | src_cache = optarg; |
| 2269 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2270 | case 'q': |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2271 | s.quiet = true; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2272 | break; |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2273 | case 'n': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2274 | skip_create = true; |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2275 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2276 | case 'm': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2277 | if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) || |
| 2278 | s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2279 | error_report("Invalid number of coroutines. Allowed number of" |
| 2280 | " coroutines is between 1 and %d", MAX_COROUTINES); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2281 | goto fail_getopt; |
| 2282 | } |
| 2283 | break; |
| 2284 | case 'W': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2285 | s.wr_in_order = false; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2286 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2287 | case 'U': |
| 2288 | force_share = true; |
| 2289 | break; |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 2290 | case 'r': |
| 2291 | rate_limit = cvtnum("rate limit", optarg); |
| 2292 | if (rate_limit < 0) { |
| 2293 | goto fail_getopt; |
| 2294 | } |
| 2295 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 2296 | case OPTION_OBJECT: |
| 2297 | user_creatable_process_cmdline(optarg); |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2298 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2299 | case OPTION_IMAGE_OPTS: |
| 2300 | image_opts = true; |
| 2301 | break; |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 2302 | case OPTION_SALVAGE: |
| 2303 | s.salvage = true; |
| 2304 | break; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2305 | case OPTION_TARGET_IMAGE_OPTS: |
| 2306 | tgt_image_opts = true; |
| 2307 | break; |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2308 | case OPTION_TARGET_IS_ZERO: |
| 2309 | /* |
| 2310 | * The user asserting that the target is blank has the |
| 2311 | * same effect as the target driver supporting zero |
| 2312 | * initialisation. |
| 2313 | */ |
| 2314 | s.has_zero_init = true; |
| 2315 | break; |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2316 | case OPTION_BITMAPS: |
| 2317 | bitmaps = true; |
| 2318 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2319 | } |
| 2320 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2321 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2322 | if (!out_fmt && !tgt_image_opts) { |
| 2323 | out_fmt = "raw"; |
| 2324 | } |
| 2325 | |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2326 | if (s.compressed && s.copy_range) { |
| 2327 | error_report("Cannot enable copy offloading when -c is used"); |
| 2328 | goto fail_getopt; |
| 2329 | } |
| 2330 | |
| 2331 | if (explict_min_sparse && s.copy_range) { |
| 2332 | error_report("Cannot enable copy offloading when -S is used"); |
| 2333 | goto fail_getopt; |
| 2334 | } |
| 2335 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 2336 | if (s.copy_range && s.salvage) { |
| 2337 | error_report("Cannot use copy offloading in salvaging mode"); |
| 2338 | goto fail_getopt; |
| 2339 | } |
| 2340 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2341 | if (tgt_image_opts && !skip_create) { |
| 2342 | error_report("--target-image-opts requires use of -n flag"); |
| 2343 | goto fail_getopt; |
| 2344 | } |
| 2345 | |
Kevin Wolf | ffd8e8f | 2019-08-09 11:09:21 +0200 | [diff] [blame] | 2346 | if (skip_create && options) { |
Eric Blake | 25956af | 2020-07-06 15:39:46 -0500 | [diff] [blame] | 2347 | error_report("-o has no effect when skipping image creation"); |
| 2348 | goto fail_getopt; |
Kevin Wolf | ffd8e8f | 2019-08-09 11:09:21 +0200 | [diff] [blame] | 2349 | } |
| 2350 | |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2351 | if (s.has_zero_init && !skip_create) { |
| 2352 | error_report("--target-is-zero requires use of -n flag"); |
| 2353 | goto fail_getopt; |
| 2354 | } |
| 2355 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2356 | s.src_num = argc - optind - 1; |
| 2357 | out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL; |
| 2358 | |
| 2359 | if (options && has_help_option(options)) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2360 | if (out_fmt) { |
| 2361 | ret = print_block_option_help(out_filename, out_fmt); |
| 2362 | goto fail_getopt; |
| 2363 | } else { |
| 2364 | error_report("Option help requires a format be specified"); |
| 2365 | goto fail_getopt; |
| 2366 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2367 | } |
| 2368 | |
| 2369 | if (s.src_num < 1) { |
| 2370 | error_report("Must specify image file name"); |
| 2371 | goto fail_getopt; |
| 2372 | } |
| 2373 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2374 | /* ret is still -EINVAL until here */ |
| 2375 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); |
| 2376 | if (ret < 0) { |
| 2377 | error_report("Invalid source cache option: %s", src_cache); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2378 | goto fail_getopt; |
| 2379 | } |
| 2380 | |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2381 | /* Initialize before goto out */ |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2382 | if (s.quiet) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2383 | progress = false; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2384 | } |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2385 | qemu_progress_init(progress, 1.0); |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2386 | qemu_progress_print(0, 100); |
| 2387 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2388 | s.src = g_new0(BlockBackend *, s.src_num); |
| 2389 | s.src_sectors = g_new(int64_t, s.src_num); |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 2390 | s.src_alignment = g_new(int, s.src_num); |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 2391 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2392 | for (bs_i = 0; bs_i < s.src_num; bs_i++) { |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 2393 | BlockDriverState *src_bs; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2394 | s.src[bs_i] = img_open(image_opts, argv[optind + bs_i], |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2395 | fmt, src_flags, src_writethrough, s.quiet, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2396 | force_share); |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2397 | if (!s.src[bs_i]) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2398 | ret = -1; |
| 2399 | goto out; |
| 2400 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2401 | s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]); |
| 2402 | if (s.src_sectors[bs_i] < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2403 | error_report("Could not get size of %s: %s", |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2404 | argv[optind + bs_i], strerror(-s.src_sectors[bs_i])); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2405 | ret = -1; |
| 2406 | goto out; |
| 2407 | } |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 2408 | src_bs = blk_bs(s.src[bs_i]); |
| 2409 | s.src_alignment[bs_i] = DIV_ROUND_UP(src_bs->bl.request_alignment, |
| 2410 | BDRV_SECTOR_SIZE); |
| 2411 | if (!bdrv_get_info(src_bs, &bdi)) { |
| 2412 | s.src_alignment[bs_i] = MAX(s.src_alignment[bs_i], |
| 2413 | bdi.cluster_size / BDRV_SECTOR_SIZE); |
| 2414 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2415 | s.total_sectors += s.src_sectors[bs_i]; |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 2416 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2417 | |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2418 | if (sn_opts) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2419 | bdrv_snapshot_load_tmp(blk_bs(s.src[0]), |
Peter Maydell | 10d6eda | 2017-02-10 16:28:24 +0000 | [diff] [blame] | 2420 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), |
| 2421 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), |
| 2422 | &local_err); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2423 | } else if (snapshot_name != NULL) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2424 | if (s.src_num > 1) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 2425 | error_report("No support for concatenating multiple snapshot"); |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 2426 | ret = -1; |
| 2427 | goto out; |
| 2428 | } |
Wenchao Xia | 7b4c478 | 2013-12-04 17:10:54 +0800 | [diff] [blame] | 2429 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2430 | bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name, |
| 2431 | &local_err); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2432 | } |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2433 | if (local_err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2434 | error_reportf_err(local_err, "Failed to load snapshot: "); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2435 | ret = -1; |
| 2436 | goto out; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 2437 | } |
| 2438 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2439 | if (!skip_create) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2440 | /* Find driver and parse its options */ |
| 2441 | drv = bdrv_find_format(out_fmt); |
| 2442 | if (!drv) { |
| 2443 | error_report("Unknown file format '%s'", out_fmt); |
| 2444 | ret = -1; |
| 2445 | goto out; |
| 2446 | } |
| 2447 | |
| 2448 | proto_drv = bdrv_find_protocol(out_filename, true, &local_err); |
| 2449 | if (!proto_drv) { |
| 2450 | error_report_err(local_err); |
| 2451 | ret = -1; |
| 2452 | goto out; |
| 2453 | } |
| 2454 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2455 | if (!drv->create_opts) { |
| 2456 | error_report("Format driver '%s' does not support image creation", |
| 2457 | drv->format_name); |
| 2458 | ret = -1; |
| 2459 | goto out; |
| 2460 | } |
Max Reitz | f75613c | 2014-12-02 18:32:46 +0100 | [diff] [blame] | 2461 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2462 | if (!proto_drv->create_opts) { |
| 2463 | error_report("Protocol driver '%s' does not support image creation", |
| 2464 | proto_drv->format_name); |
| 2465 | ret = -1; |
| 2466 | goto out; |
| 2467 | } |
Max Reitz | f75613c | 2014-12-02 18:32:46 +0100 | [diff] [blame] | 2468 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2469 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 2470 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
Kevin Wolf | db08adf | 2009-06-04 15:39:38 +0200 | [diff] [blame] | 2471 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2472 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 2473 | if (options) { |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 2474 | if (!qemu_opts_do_parse(opts, options, NULL, &local_err)) { |
Markus Armbruster | 97a2ca7 | 2015-03-14 10:23:15 +0100 | [diff] [blame] | 2475 | error_report_err(local_err); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 2476 | ret = -1; |
| 2477 | goto out; |
| 2478 | } |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2479 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2480 | |
Yi Li | c075c42 | 2020-08-19 09:36:07 +0800 | [diff] [blame] | 2481 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, |
| 2482 | s.total_sectors * BDRV_SECTOR_SIZE, &error_abort); |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2483 | ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL); |
| 2484 | if (ret < 0) { |
| 2485 | goto out; |
| 2486 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2487 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2488 | |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2489 | /* Get backing file name if -o backing_file was used */ |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2490 | out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2491 | if (out_baseimg_param) { |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2492 | out_baseimg = out_baseimg_param; |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2493 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2494 | s.target_has_backing = (bool) out_baseimg; |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2495 | |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2496 | if (s.has_zero_init && s.target_has_backing) { |
| 2497 | error_report("Cannot use --target-is-zero when the destination " |
| 2498 | "image has a backing file"); |
| 2499 | goto out; |
| 2500 | } |
| 2501 | |
Max Reitz | 48758a8 | 2017-04-26 15:46:48 +0200 | [diff] [blame] | 2502 | if (s.src_num > 1 && out_baseimg) { |
| 2503 | error_report("Having a backing file for the target makes no sense when " |
| 2504 | "concatenating multiple input images"); |
| 2505 | ret = -1; |
| 2506 | goto out; |
| 2507 | } |
| 2508 | |
Eric Blake | d9f059a | 2020-07-06 15:39:54 -0500 | [diff] [blame] | 2509 | if (out_baseimg_param) { |
| 2510 | if (!qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT)) { |
Eric Blake | 497a30d | 2021-05-03 14:36:00 -0700 | [diff] [blame] | 2511 | error_report("Use of backing file requires explicit " |
| 2512 | "backing format"); |
| 2513 | ret = -1; |
| 2514 | goto out; |
Eric Blake | d9f059a | 2020-07-06 15:39:54 -0500 | [diff] [blame] | 2515 | } |
| 2516 | } |
| 2517 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2518 | /* Check if compression is supported */ |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2519 | if (s.compressed) { |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2520 | bool encryption = |
| 2521 | qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false); |
Daniel P. Berrange | 0cb8d47 | 2017-06-23 17:24:06 +0100 | [diff] [blame] | 2522 | const char *encryptfmt = |
| 2523 | qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2524 | const char *preallocation = |
| 2525 | qemu_opt_get(opts, BLOCK_OPT_PREALLOC); |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2526 | |
Vladimir Sementsov-Ogievskiy | ac850bf | 2019-06-04 19:15:06 +0300 | [diff] [blame] | 2527 | if (drv && !block_driver_can_compress(drv)) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2528 | error_report("Compression not supported for this file format"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2529 | ret = -1; |
| 2530 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2531 | } |
| 2532 | |
Daniel P. Berrange | 0cb8d47 | 2017-06-23 17:24:06 +0100 | [diff] [blame] | 2533 | if (encryption || encryptfmt) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2534 | error_report("Compression and encryption not supported at " |
| 2535 | "the same time"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2536 | ret = -1; |
| 2537 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2538 | } |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 2539 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2540 | if (preallocation |
| 2541 | && strcmp(preallocation, "off")) |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 2542 | { |
| 2543 | error_report("Compression and preallocation not supported at " |
| 2544 | "the same time"); |
| 2545 | ret = -1; |
| 2546 | goto out; |
| 2547 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2548 | } |
| 2549 | |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2550 | /* Determine if bitmaps need copying */ |
| 2551 | if (bitmaps) { |
| 2552 | if (s.src_num > 1) { |
| 2553 | error_report("Copying bitmaps only possible with single source"); |
| 2554 | ret = -1; |
| 2555 | goto out; |
| 2556 | } |
| 2557 | if (!bdrv_supports_persistent_dirty_bitmap(blk_bs(s.src[0]))) { |
| 2558 | error_report("Source lacks bitmap support"); |
| 2559 | ret = -1; |
| 2560 | goto out; |
| 2561 | } |
| 2562 | } |
| 2563 | |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2564 | /* |
| 2565 | * The later open call will need any decryption secrets, and |
| 2566 | * bdrv_create() will purge "opts", so extract them now before |
| 2567 | * they are lost. |
| 2568 | */ |
| 2569 | if (!skip_create) { |
| 2570 | open_opts = qdict_new(); |
| 2571 | 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] | 2572 | |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2573 | /* Create the new image */ |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 2574 | ret = bdrv_create(drv, out_filename, opts, &local_err); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2575 | if (ret < 0) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2576 | error_reportf_err(local_err, "%s: error while converting %s: ", |
| 2577 | out_filename, out_fmt); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2578 | goto out; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2579 | } |
| 2580 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2581 | |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 2582 | s.target_is_new = !skip_create; |
| 2583 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2584 | 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] | 2585 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2586 | if (ret < 0) { |
| 2587 | error_report("Invalid cache option: %s", cache); |
Markus Armbruster | bb9cd2e | 2014-05-28 11:17:07 +0200 | [diff] [blame] | 2588 | goto out; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2589 | } |
| 2590 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2591 | if (skip_create) { |
| 2592 | s.target = img_open(tgt_image_opts, out_filename, out_fmt, |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2593 | flags, writethrough, s.quiet, false); |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2594 | } else { |
| 2595 | /* TODO ultimately we should allow --target-image-opts |
| 2596 | * to be used even when -n is not given. |
| 2597 | * That has to wait for bdrv_create to be improved |
| 2598 | * to allow filenames in option syntax |
| 2599 | */ |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2600 | s.target = img_open_file(out_filename, open_opts, out_fmt, |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2601 | flags, writethrough, s.quiet, false); |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2602 | open_opts = NULL; /* blk_new_open will have freed it */ |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2603 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2604 | if (!s.target) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2605 | ret = -1; |
| 2606 | goto out; |
| 2607 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2608 | out_bs = blk_bs(s.target); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2609 | |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2610 | if (bitmaps && !bdrv_supports_persistent_dirty_bitmap(out_bs)) { |
| 2611 | error_report("Format driver '%s' does not support bitmaps", |
| 2612 | out_bs->drv->format_name); |
| 2613 | ret = -1; |
| 2614 | goto out; |
| 2615 | } |
| 2616 | |
Vladimir Sementsov-Ogievskiy | ac850bf | 2019-06-04 19:15:06 +0300 | [diff] [blame] | 2617 | if (s.compressed && !block_driver_can_compress(out_bs->drv)) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2618 | error_report("Compression not supported for this file format"); |
| 2619 | ret = -1; |
| 2620 | goto out; |
| 2621 | } |
| 2622 | |
Eric Blake | 5def6b8 | 2016-06-23 16:37:19 -0600 | [diff] [blame] | 2623 | /* increase bufsectors from the default 4096 (2M) if opt_transfer |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 2624 | * or discard_alignment of the out_bs is greater. Limit to |
| 2625 | * MAX_BUF_SECTORS as maximum which is currently 32768 (16MB). */ |
| 2626 | s.buf_sectors = MIN(MAX_BUF_SECTORS, |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2627 | MAX(s.buf_sectors, |
| 2628 | MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS, |
| 2629 | out_bs->bl.pdiscard_alignment >> |
| 2630 | BDRV_SECTOR_BITS))); |
Peter Lieven | f2521c9 | 2013-11-27 11:07:06 +0100 | [diff] [blame] | 2631 | |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 2632 | /* try to align the write requests to the destination to avoid unnecessary |
| 2633 | * RMW cycles. */ |
| 2634 | s.alignment = MAX(pow2floor(s.min_sparse), |
| 2635 | DIV_ROUND_UP(out_bs->bl.request_alignment, |
| 2636 | BDRV_SECTOR_SIZE)); |
| 2637 | assert(is_power_of_2(s.alignment)); |
| 2638 | |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2639 | if (skip_create) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2640 | int64_t output_sectors = blk_nb_sectors(s.target); |
Markus Armbruster | 43716fa | 2014-06-26 13:23:21 +0200 | [diff] [blame] | 2641 | if (output_sectors < 0) { |
Gonglei | eec5eb4 | 2015-02-25 12:22:27 +0800 | [diff] [blame] | 2642 | error_report("unable to get output image length: %s", |
Markus Armbruster | 43716fa | 2014-06-26 13:23:21 +0200 | [diff] [blame] | 2643 | strerror(-output_sectors)); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2644 | ret = -1; |
| 2645 | goto out; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2646 | } else if (output_sectors < s.total_sectors) { |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2647 | error_report("output file is smaller than input file"); |
| 2648 | ret = -1; |
| 2649 | goto out; |
| 2650 | } |
| 2651 | } |
| 2652 | |
Max Reitz | c69291e | 2020-01-21 16:59:14 +0100 | [diff] [blame] | 2653 | if (s.target_has_backing && s.target_is_new) { |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 2654 | /* Errors are treated as "backing length unknown" (which means |
| 2655 | * s.target_backing_sectors has to be negative, which it will |
| 2656 | * be automatically). The backing file length is used only |
| 2657 | * for optimizations, so such a case is not fatal. */ |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 2658 | s.target_backing_sectors = |
| 2659 | bdrv_nb_sectors(bdrv_backing_chain_next(out_bs)); |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 2660 | } else { |
| 2661 | s.target_backing_sectors = -1; |
| 2662 | } |
| 2663 | |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2664 | ret = bdrv_get_info(out_bs, &bdi); |
| 2665 | if (ret < 0) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2666 | if (s.compressed) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2667 | error_report("could not get block driver info"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2668 | goto out; |
| 2669 | } |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2670 | } else { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2671 | s.compressed = s.compressed || bdi.needs_compressed_writes; |
| 2672 | s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE; |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2673 | } |
| 2674 | |
Zhengui | 0c8c489 | 2020-10-20 14:47:44 +0000 | [diff] [blame] | 2675 | if (rate_limit) { |
| 2676 | set_rate_limit(s.target, rate_limit); |
| 2677 | } |
| 2678 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2679 | ret = convert_do_copy(&s); |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2680 | |
| 2681 | /* Now copy the bitmaps */ |
| 2682 | if (bitmaps && ret == 0) { |
| 2683 | ret = convert_copy_bitmaps(blk_bs(s.src[0]), out_bs); |
| 2684 | } |
| 2685 | |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2686 | out: |
Peter Lieven | 13c28af | 2013-11-27 11:07:01 +0100 | [diff] [blame] | 2687 | if (!ret) { |
| 2688 | qemu_progress_print(100, 0); |
| 2689 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2690 | qemu_progress_end(); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2691 | qemu_opts_del(opts); |
| 2692 | qemu_opts_free(create_opts); |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2693 | qobject_unref(open_opts); |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2694 | blk_unref(s.target); |
| 2695 | if (s.src) { |
| 2696 | for (bs_i = 0; bs_i < s.src_num; bs_i++) { |
| 2697 | blk_unref(s.src[bs_i]); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2698 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2699 | g_free(s.src); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2700 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2701 | g_free(s.src_sectors); |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 2702 | g_free(s.src_alignment); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2703 | fail_getopt: |
Tuguoyi | 6aec830 | 2020-11-02 09:04:57 +0000 | [diff] [blame] | 2704 | qemu_opts_del(sn_opts); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2705 | g_free(options); |
| 2706 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2707 | return !!ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2708 | } |
| 2709 | |
bellard | 57d1a2b | 2004-08-03 21:15:11 +0000 | [diff] [blame] | 2710 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2711 | static void dump_snapshots(BlockDriverState *bs) |
| 2712 | { |
| 2713 | QEMUSnapshotInfo *sn_tab, *sn; |
| 2714 | int nb_sns, i; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2715 | |
| 2716 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 2717 | if (nb_sns <= 0) |
| 2718 | return; |
| 2719 | printf("Snapshot list:\n"); |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2720 | bdrv_snapshot_dump(NULL); |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2721 | printf("\n"); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2722 | for(i = 0; i < nb_sns; i++) { |
| 2723 | sn = &sn_tab[i]; |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2724 | bdrv_snapshot_dump(sn); |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2725 | printf("\n"); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2726 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2727 | g_free(sn_tab); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2728 | } |
| 2729 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2730 | static void dump_json_image_info_list(ImageInfoList *list) |
| 2731 | { |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2732 | GString *str; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2733 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 2734 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2735 | |
| 2736 | visit_type_ImageInfoList(v, NULL, &list, &error_abort); |
| 2737 | visit_complete(v, &obj); |
Markus Armbruster | 6589f45 | 2020-12-11 18:11:35 +0100 | [diff] [blame] | 2738 | str = qobject_to_json_pretty(obj, true); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2739 | assert(str != NULL); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2740 | printf("%s\n", str->str); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2741 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2742 | visit_free(v); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2743 | g_string_free(str, true); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2744 | } |
| 2745 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2746 | static void dump_json_image_info(ImageInfo *info) |
| 2747 | { |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2748 | GString *str; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2749 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 2750 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2751 | |
| 2752 | visit_type_ImageInfo(v, NULL, &info, &error_abort); |
| 2753 | visit_complete(v, &obj); |
Markus Armbruster | 6589f45 | 2020-12-11 18:11:35 +0100 | [diff] [blame] | 2754 | str = qobject_to_json_pretty(obj, true); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2755 | assert(str != NULL); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2756 | printf("%s\n", str->str); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2757 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2758 | visit_free(v); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 2759 | g_string_free(str, true); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2760 | } |
| 2761 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2762 | static void dump_human_image_info_list(ImageInfoList *list) |
| 2763 | { |
| 2764 | ImageInfoList *elem; |
| 2765 | bool delim = false; |
| 2766 | |
| 2767 | for (elem = list; elem; elem = elem->next) { |
| 2768 | if (delim) { |
| 2769 | printf("\n"); |
| 2770 | } |
| 2771 | delim = true; |
| 2772 | |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2773 | bdrv_image_info_dump(elem->value); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2774 | } |
| 2775 | } |
| 2776 | |
| 2777 | static gboolean str_equal_func(gconstpointer a, gconstpointer b) |
| 2778 | { |
| 2779 | return strcmp(a, b) == 0; |
| 2780 | } |
| 2781 | |
| 2782 | /** |
| 2783 | * Open an image file chain and return an ImageInfoList |
| 2784 | * |
| 2785 | * @filename: topmost image filename |
| 2786 | * @fmt: topmost image format (may be NULL to autodetect) |
| 2787 | * @chain: true - enumerate entire backing file chain |
| 2788 | * false - only topmost image file |
| 2789 | * |
| 2790 | * Returns a list of ImageInfo objects or NULL if there was an error opening an |
| 2791 | * image file. If there was an error a message will have been printed to |
| 2792 | * stderr. |
| 2793 | */ |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2794 | static ImageInfoList *collect_image_info_list(bool image_opts, |
| 2795 | const char *filename, |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2796 | const char *fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2797 | bool chain, bool force_share) |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2798 | { |
| 2799 | ImageInfoList *head = NULL; |
Eric Blake | c3033fd | 2021-01-13 16:10:12 -0600 | [diff] [blame] | 2800 | ImageInfoList **tail = &head; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2801 | GHashTable *filenames; |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2802 | Error *err = NULL; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2803 | |
| 2804 | filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL); |
| 2805 | |
| 2806 | while (filename) { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2807 | BlockBackend *blk; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2808 | BlockDriverState *bs; |
| 2809 | ImageInfo *info; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2810 | |
| 2811 | if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) { |
| 2812 | error_report("Backing file '%s' creates an infinite loop.", |
| 2813 | filename); |
| 2814 | goto err; |
| 2815 | } |
| 2816 | g_hash_table_insert(filenames, (gpointer)filename, NULL); |
| 2817 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 2818 | blk = img_open(image_opts, filename, fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2819 | BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false, |
| 2820 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2821 | if (!blk) { |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2822 | goto err; |
| 2823 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2824 | bs = blk_bs(blk); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2825 | |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2826 | bdrv_query_image_info(bs, &info, &err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2827 | if (err) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 2828 | error_report_err(err); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2829 | blk_unref(blk); |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2830 | goto err; |
Wenchao Xia | fb0ed45 | 2013-06-06 12:27:57 +0800 | [diff] [blame] | 2831 | } |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2832 | |
Eric Blake | c3033fd | 2021-01-13 16:10:12 -0600 | [diff] [blame] | 2833 | QAPI_LIST_APPEND(tail, info); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2834 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2835 | blk_unref(blk); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2836 | |
Stefan Hajnoczi | 0da7d13 | 2019-12-05 13:46:46 +0000 | [diff] [blame] | 2837 | /* Clear parameters that only apply to the topmost image */ |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2838 | filename = fmt = NULL; |
Stefan Hajnoczi | 0da7d13 | 2019-12-05 13:46:46 +0000 | [diff] [blame] | 2839 | image_opts = false; |
| 2840 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2841 | if (chain) { |
| 2842 | if (info->has_full_backing_filename) { |
| 2843 | filename = info->full_backing_filename; |
| 2844 | } else if (info->has_backing_filename) { |
John Snow | 92d617a | 2015-12-14 14:55:15 -0500 | [diff] [blame] | 2845 | error_report("Could not determine absolute backing filename," |
| 2846 | " but backing filename '%s' present", |
| 2847 | info->backing_filename); |
| 2848 | goto err; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2849 | } |
| 2850 | if (info->has_backing_filename_format) { |
| 2851 | fmt = info->backing_filename_format; |
| 2852 | } |
| 2853 | } |
| 2854 | } |
| 2855 | g_hash_table_destroy(filenames); |
| 2856 | return head; |
| 2857 | |
| 2858 | err: |
| 2859 | qapi_free_ImageInfoList(head); |
| 2860 | g_hash_table_destroy(filenames); |
| 2861 | return NULL; |
| 2862 | } |
| 2863 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2864 | static int img_info(int argc, char **argv) |
| 2865 | { |
| 2866 | int c; |
| 2867 | OutputFormat output_format = OFORMAT_HUMAN; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2868 | bool chain = false; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2869 | const char *filename, *fmt, *output; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2870 | ImageInfoList *list; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2871 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2872 | bool force_share = false; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2873 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2874 | fmt = NULL; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2875 | output = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2876 | for(;;) { |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2877 | int option_index = 0; |
| 2878 | static const struct option long_options[] = { |
| 2879 | {"help", no_argument, 0, 'h'}, |
| 2880 | {"format", required_argument, 0, 'f'}, |
| 2881 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2882 | {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2883 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2884 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2885 | {"force-share", no_argument, 0, 'U'}, |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2886 | {0, 0, 0, 0} |
| 2887 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2888 | c = getopt_long(argc, argv, ":f:hU", |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2889 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2890 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2891 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2892 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2893 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2894 | case ':': |
| 2895 | missing_argument(argv[optind - 1]); |
| 2896 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2897 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2898 | unrecognized_option(argv[optind - 1]); |
| 2899 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2900 | case 'h': |
| 2901 | help(); |
| 2902 | break; |
| 2903 | case 'f': |
| 2904 | fmt = optarg; |
| 2905 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2906 | case 'U': |
| 2907 | force_share = true; |
| 2908 | break; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2909 | case OPTION_OUTPUT: |
| 2910 | output = optarg; |
| 2911 | break; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2912 | case OPTION_BACKING_CHAIN: |
| 2913 | chain = true; |
| 2914 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 2915 | case OPTION_OBJECT: |
| 2916 | user_creatable_process_cmdline(optarg); |
| 2917 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2918 | case OPTION_IMAGE_OPTS: |
| 2919 | image_opts = true; |
| 2920 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2921 | } |
| 2922 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 2923 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2924 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2925 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2926 | filename = argv[optind++]; |
| 2927 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2928 | if (output && !strcmp(output, "json")) { |
| 2929 | output_format = OFORMAT_JSON; |
| 2930 | } else if (output && !strcmp(output, "human")) { |
| 2931 | output_format = OFORMAT_HUMAN; |
| 2932 | } else if (output) { |
| 2933 | error_report("--output must be used with human or json as argument."); |
| 2934 | return 1; |
| 2935 | } |
| 2936 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2937 | list = collect_image_info_list(image_opts, filename, fmt, chain, |
| 2938 | force_share); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2939 | if (!list) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2940 | return 1; |
| 2941 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2942 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2943 | switch (output_format) { |
| 2944 | case OFORMAT_HUMAN: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2945 | dump_human_image_info_list(list); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2946 | break; |
| 2947 | case OFORMAT_JSON: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2948 | if (chain) { |
| 2949 | dump_json_image_info_list(list); |
| 2950 | } else { |
| 2951 | dump_json_image_info(list->value); |
| 2952 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2953 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2954 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2955 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2956 | qapi_free_ImageInfoList(list); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2957 | return 0; |
| 2958 | } |
| 2959 | |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 2960 | static int dump_map_entry(OutputFormat output_format, MapEntry *e, |
| 2961 | MapEntry *next) |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2962 | { |
| 2963 | switch (output_format) { |
| 2964 | case OFORMAT_HUMAN: |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2965 | if (e->data && !e->has_offset) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2966 | error_report("File contains external, encrypted or compressed clusters."); |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 2967 | return -1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2968 | } |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2969 | if (e->data && !e->zero) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2970 | printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n", |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2971 | e->start, e->length, |
| 2972 | e->has_offset ? e->offset : 0, |
| 2973 | e->has_filename ? e->filename : ""); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2974 | } |
| 2975 | /* This format ignores the distinction between 0, ZERO and ZERO|DATA. |
| 2976 | * Modify the flags here to allow more coalescing. |
| 2977 | */ |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2978 | if (next && (!next->data || next->zero)) { |
| 2979 | next->data = false; |
| 2980 | next->zero = true; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2981 | } |
| 2982 | break; |
| 2983 | case OFORMAT_JSON: |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 2984 | printf("{ \"start\": %"PRId64", \"length\": %"PRId64"," |
Eric Blake | 8417e13 | 2021-07-01 14:06:55 -0500 | [diff] [blame^] | 2985 | " \"depth\": %"PRId64", \"present\": %s, \"zero\": %s," |
| 2986 | " \"data\": %s", e->start, e->length, e->depth, |
| 2987 | e->present ? "true" : "false", |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2988 | e->zero ? "true" : "false", |
| 2989 | e->data ? "true" : "false"); |
| 2990 | if (e->has_offset) { |
Paolo Bonzini | c745bfb | 2013-09-11 18:47:52 +0200 | [diff] [blame] | 2991 | printf(", \"offset\": %"PRId64"", e->offset); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2992 | } |
| 2993 | putchar('}'); |
| 2994 | |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 2995 | if (next) { |
| 2996 | puts(","); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2997 | } |
| 2998 | break; |
| 2999 | } |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3000 | return 0; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3001 | } |
| 3002 | |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3003 | static int get_block_status(BlockDriverState *bs, int64_t offset, |
| 3004 | int64_t bytes, MapEntry *e) |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3005 | { |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3006 | int ret; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3007 | int depth; |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 3008 | BlockDriverState *file; |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3009 | bool has_offset; |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3010 | int64_t map; |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3011 | char *filename = NULL; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3012 | |
| 3013 | /* As an optimization, we could cache the current range of unallocated |
| 3014 | * clusters in each file of the chain, and avoid querying the same |
| 3015 | * range repeatedly. |
| 3016 | */ |
| 3017 | |
| 3018 | depth = 0; |
| 3019 | for (;;) { |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3020 | bs = bdrv_skip_filters(bs); |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3021 | ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3022 | if (ret < 0) { |
| 3023 | return ret; |
| 3024 | } |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3025 | assert(bytes); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3026 | if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) { |
| 3027 | break; |
| 3028 | } |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3029 | bs = bdrv_cow_bs(bs); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3030 | if (bs == NULL) { |
| 3031 | ret = 0; |
| 3032 | break; |
| 3033 | } |
| 3034 | |
| 3035 | depth++; |
| 3036 | } |
| 3037 | |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3038 | has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID); |
| 3039 | |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3040 | if (file && has_offset) { |
| 3041 | bdrv_refresh_filename(file); |
| 3042 | filename = file->filename; |
| 3043 | } |
| 3044 | |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3045 | *e = (MapEntry) { |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3046 | .start = offset, |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3047 | .length = bytes, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3048 | .data = !!(ret & BDRV_BLOCK_DATA), |
| 3049 | .zero = !!(ret & BDRV_BLOCK_ZERO), |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3050 | .offset = map, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3051 | .has_offset = has_offset, |
| 3052 | .depth = depth, |
Eric Blake | 8417e13 | 2021-07-01 14:06:55 -0500 | [diff] [blame^] | 3053 | .present = !!(ret & BDRV_BLOCK_ALLOCATED), |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3054 | .has_filename = filename, |
| 3055 | .filename = filename, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3056 | }; |
| 3057 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3058 | return 0; |
| 3059 | } |
| 3060 | |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3061 | static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next) |
| 3062 | { |
| 3063 | if (curr->length == 0) { |
| 3064 | return false; |
| 3065 | } |
| 3066 | if (curr->zero != next->zero || |
| 3067 | curr->data != next->data || |
| 3068 | curr->depth != next->depth || |
Eric Blake | 8417e13 | 2021-07-01 14:06:55 -0500 | [diff] [blame^] | 3069 | curr->present != next->present || |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3070 | curr->has_filename != next->has_filename || |
| 3071 | curr->has_offset != next->has_offset) { |
| 3072 | return false; |
| 3073 | } |
| 3074 | if (curr->has_filename && strcmp(curr->filename, next->filename)) { |
| 3075 | return false; |
| 3076 | } |
| 3077 | if (curr->has_offset && curr->offset + curr->length != next->offset) { |
| 3078 | return false; |
| 3079 | } |
| 3080 | return true; |
| 3081 | } |
| 3082 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3083 | static int img_map(int argc, char **argv) |
| 3084 | { |
| 3085 | int c; |
| 3086 | OutputFormat output_format = OFORMAT_HUMAN; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3087 | BlockBackend *blk; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3088 | BlockDriverState *bs; |
| 3089 | const char *filename, *fmt, *output; |
| 3090 | int64_t length; |
| 3091 | MapEntry curr = { .length = 0 }, next; |
| 3092 | int ret = 0; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3093 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3094 | bool force_share = false; |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3095 | int64_t start_offset = 0; |
| 3096 | int64_t max_length = -1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3097 | |
| 3098 | fmt = NULL; |
| 3099 | output = NULL; |
| 3100 | for (;;) { |
| 3101 | int option_index = 0; |
| 3102 | static const struct option long_options[] = { |
| 3103 | {"help", no_argument, 0, 'h'}, |
| 3104 | {"format", required_argument, 0, 'f'}, |
| 3105 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3106 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3107 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3108 | {"force-share", no_argument, 0, 'U'}, |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3109 | {"start-offset", required_argument, 0, 's'}, |
| 3110 | {"max-length", required_argument, 0, 'l'}, |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3111 | {0, 0, 0, 0} |
| 3112 | }; |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3113 | c = getopt_long(argc, argv, ":f:s:l:hU", |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3114 | long_options, &option_index); |
| 3115 | if (c == -1) { |
| 3116 | break; |
| 3117 | } |
| 3118 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3119 | case ':': |
| 3120 | missing_argument(argv[optind - 1]); |
| 3121 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3122 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3123 | unrecognized_option(argv[optind - 1]); |
| 3124 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3125 | case 'h': |
| 3126 | help(); |
| 3127 | break; |
| 3128 | case 'f': |
| 3129 | fmt = optarg; |
| 3130 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3131 | case 'U': |
| 3132 | force_share = true; |
| 3133 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3134 | case OPTION_OUTPUT: |
| 3135 | output = optarg; |
| 3136 | break; |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3137 | case 's': |
| 3138 | start_offset = cvtnum("start offset", optarg); |
| 3139 | if (start_offset < 0) { |
| 3140 | return 1; |
| 3141 | } |
| 3142 | break; |
| 3143 | case 'l': |
| 3144 | max_length = cvtnum("max length", optarg); |
| 3145 | if (max_length < 0) { |
| 3146 | return 1; |
| 3147 | } |
| 3148 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 3149 | case OPTION_OBJECT: |
| 3150 | user_creatable_process_cmdline(optarg); |
| 3151 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3152 | case OPTION_IMAGE_OPTS: |
| 3153 | image_opts = true; |
| 3154 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3155 | } |
| 3156 | } |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3157 | if (optind != argc - 1) { |
| 3158 | error_exit("Expecting one image file name"); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3159 | } |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3160 | filename = argv[optind]; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3161 | |
| 3162 | if (output && !strcmp(output, "json")) { |
| 3163 | output_format = OFORMAT_JSON; |
| 3164 | } else if (output && !strcmp(output, "human")) { |
| 3165 | output_format = OFORMAT_HUMAN; |
| 3166 | } else if (output) { |
| 3167 | error_report("--output must be used with human or json as argument."); |
| 3168 | return 1; |
| 3169 | } |
| 3170 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3171 | blk = img_open(image_opts, filename, fmt, 0, false, false, force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3172 | if (!blk) { |
| 3173 | return 1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3174 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3175 | bs = blk_bs(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3176 | |
| 3177 | if (output_format == OFORMAT_HUMAN) { |
| 3178 | printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File"); |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 3179 | } else if (output_format == OFORMAT_JSON) { |
| 3180 | putchar('['); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3181 | } |
| 3182 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3183 | length = blk_getlength(blk); |
Eyal Moscovici | 8f282e8 | 2020-05-13 16:36:27 +0300 | [diff] [blame] | 3184 | if (length < 0) { |
| 3185 | error_report("Failed to get size for '%s'", filename); |
| 3186 | return 1; |
| 3187 | } |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3188 | if (max_length != -1) { |
| 3189 | length = MIN(start_offset + max_length, length); |
| 3190 | } |
Eyal Moscovici | 8f282e8 | 2020-05-13 16:36:27 +0300 | [diff] [blame] | 3191 | |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3192 | curr.start = start_offset; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3193 | while (curr.start + curr.length < length) { |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3194 | int64_t offset = curr.start + curr.length; |
Kevin Wolf | d0ceea8 | 2020-07-07 16:46:29 +0200 | [diff] [blame] | 3195 | int64_t n = length - offset; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3196 | |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3197 | ret = get_block_status(bs, offset, n, &next); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3198 | if (ret < 0) { |
| 3199 | error_report("Could not read file metadata: %s", strerror(-ret)); |
| 3200 | goto out; |
| 3201 | } |
| 3202 | |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3203 | if (entry_mergeable(&curr, &next)) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3204 | curr.length += next.length; |
| 3205 | continue; |
| 3206 | } |
| 3207 | |
| 3208 | if (curr.length > 0) { |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3209 | ret = dump_map_entry(output_format, &curr, &next); |
| 3210 | if (ret < 0) { |
| 3211 | goto out; |
| 3212 | } |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3213 | } |
| 3214 | curr = next; |
| 3215 | } |
| 3216 | |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3217 | ret = dump_map_entry(output_format, &curr, NULL); |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 3218 | if (output_format == OFORMAT_JSON) { |
| 3219 | puts("]"); |
| 3220 | } |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3221 | |
| 3222 | out: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3223 | blk_unref(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3224 | return ret < 0; |
| 3225 | } |
| 3226 | |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3227 | #define SNAPSHOT_LIST 1 |
| 3228 | #define SNAPSHOT_CREATE 2 |
| 3229 | #define SNAPSHOT_APPLY 3 |
| 3230 | #define SNAPSHOT_DELETE 4 |
| 3231 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3232 | static int img_snapshot(int argc, char **argv) |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3233 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3234 | BlockBackend *blk; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3235 | BlockDriverState *bs; |
| 3236 | QEMUSnapshotInfo sn; |
| 3237 | char *filename, *snapshot_name = NULL; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3238 | int c, ret = 0, bdrv_oflags; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3239 | int action = 0; |
| 3240 | qemu_timeval tv; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3241 | bool quiet = false; |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 3242 | Error *err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3243 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3244 | bool force_share = false; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3245 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3246 | bdrv_oflags = BDRV_O_RDWR; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3247 | /* Parse commandline parameters */ |
| 3248 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3249 | static const struct option long_options[] = { |
| 3250 | {"help", no_argument, 0, 'h'}, |
| 3251 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3252 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3253 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3254 | {0, 0, 0, 0} |
| 3255 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3256 | c = getopt_long(argc, argv, ":la:c:d:hqU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3257 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3258 | if (c == -1) { |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3259 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3260 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3261 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3262 | case ':': |
| 3263 | missing_argument(argv[optind - 1]); |
| 3264 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3265 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3266 | unrecognized_option(argv[optind - 1]); |
| 3267 | break; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3268 | case 'h': |
| 3269 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3270 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3271 | case 'l': |
| 3272 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3273 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3274 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3275 | } |
| 3276 | action = SNAPSHOT_LIST; |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 3277 | bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */ |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3278 | break; |
| 3279 | case 'a': |
| 3280 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3281 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3282 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3283 | } |
| 3284 | action = SNAPSHOT_APPLY; |
| 3285 | snapshot_name = optarg; |
| 3286 | break; |
| 3287 | case 'c': |
| 3288 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3289 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3290 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3291 | } |
| 3292 | action = SNAPSHOT_CREATE; |
| 3293 | snapshot_name = optarg; |
| 3294 | break; |
| 3295 | case 'd': |
| 3296 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3297 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3298 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3299 | } |
| 3300 | action = SNAPSHOT_DELETE; |
| 3301 | snapshot_name = optarg; |
| 3302 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3303 | case 'q': |
| 3304 | quiet = true; |
| 3305 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3306 | case 'U': |
| 3307 | force_share = true; |
| 3308 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 3309 | case OPTION_OBJECT: |
| 3310 | user_creatable_process_cmdline(optarg); |
| 3311 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3312 | case OPTION_IMAGE_OPTS: |
| 3313 | image_opts = true; |
| 3314 | break; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3315 | } |
| 3316 | } |
| 3317 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 3318 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3319 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3320 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3321 | filename = argv[optind++]; |
| 3322 | |
| 3323 | /* Open the image */ |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3324 | blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet, |
| 3325 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3326 | if (!blk) { |
| 3327 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3328 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3329 | bs = blk_bs(blk); |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3330 | |
| 3331 | /* Perform the requested action */ |
| 3332 | switch(action) { |
| 3333 | case SNAPSHOT_LIST: |
| 3334 | dump_snapshots(bs); |
| 3335 | break; |
| 3336 | |
| 3337 | case SNAPSHOT_CREATE: |
| 3338 | memset(&sn, 0, sizeof(sn)); |
| 3339 | pstrcpy(sn.name, sizeof(sn.name), snapshot_name); |
| 3340 | |
| 3341 | qemu_gettimeofday(&tv); |
| 3342 | sn.date_sec = tv.tv_sec; |
| 3343 | sn.date_nsec = tv.tv_usec * 1000; |
| 3344 | |
| 3345 | ret = bdrv_snapshot_create(bs, &sn); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3346 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3347 | error_report("Could not create snapshot '%s': %d (%s)", |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3348 | snapshot_name, ret, strerror(-ret)); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3349 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3350 | break; |
| 3351 | |
| 3352 | case SNAPSHOT_APPLY: |
Kevin Wolf | 0b62bcb | 2017-11-20 15:28:41 +0100 | [diff] [blame] | 3353 | ret = bdrv_snapshot_goto(bs, snapshot_name, &err); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3354 | if (ret) { |
Kevin Wolf | 0b62bcb | 2017-11-20 15:28:41 +0100 | [diff] [blame] | 3355 | error_reportf_err(err, "Could not apply snapshot '%s': ", |
| 3356 | snapshot_name); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3357 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3358 | break; |
| 3359 | |
| 3360 | case SNAPSHOT_DELETE: |
Daniel Henrique Barboza | 8c04093 | 2018-11-07 11:09:59 -0200 | [diff] [blame] | 3361 | ret = bdrv_snapshot_find(bs, &sn, snapshot_name); |
| 3362 | if (ret < 0) { |
| 3363 | error_report("Could not delete snapshot '%s': snapshot not " |
| 3364 | "found", snapshot_name); |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 3365 | ret = 1; |
Daniel Henrique Barboza | 8c04093 | 2018-11-07 11:09:59 -0200 | [diff] [blame] | 3366 | } else { |
| 3367 | ret = bdrv_snapshot_delete(bs, sn.id_str, sn.name, &err); |
| 3368 | if (ret < 0) { |
| 3369 | error_reportf_err(err, "Could not delete snapshot '%s': ", |
| 3370 | snapshot_name); |
| 3371 | ret = 1; |
| 3372 | } |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3373 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3374 | break; |
| 3375 | } |
| 3376 | |
| 3377 | /* Cleanup */ |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3378 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3379 | if (ret) { |
| 3380 | return 1; |
| 3381 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3382 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3383 | } |
| 3384 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3385 | static int img_rebase(int argc, char **argv) |
| 3386 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3387 | BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL; |
Paolo Bonzini | 396374c | 2016-02-25 23:53:54 +0100 | [diff] [blame] | 3388 | uint8_t *buf_old = NULL; |
| 3389 | uint8_t *buf_new = NULL; |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3390 | BlockDriverState *bs = NULL, *prefix_chain_bs = NULL; |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3391 | BlockDriverState *unfiltered_bs; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3392 | char *filename; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3393 | const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg; |
| 3394 | int c, flags, src_flags, ret; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3395 | bool writethrough, src_writethrough; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3396 | int unsafe = 0; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3397 | bool force_share = false; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3398 | int progress = 0; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3399 | bool quiet = false; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 3400 | Error *local_err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3401 | bool image_opts = false; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3402 | |
| 3403 | /* Parse commandline parameters */ |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 3404 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3405 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3406 | src_cache = BDRV_DEFAULT_CACHE; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3407 | out_baseimg = NULL; |
| 3408 | out_basefmt = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3409 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3410 | static const struct option long_options[] = { |
| 3411 | {"help", no_argument, 0, 'h'}, |
| 3412 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3413 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3414 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3415 | {0, 0, 0, 0} |
| 3416 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3417 | c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3418 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3419 | if (c == -1) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3420 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3421 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3422 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3423 | case ':': |
| 3424 | missing_argument(argv[optind - 1]); |
| 3425 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3426 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3427 | unrecognized_option(argv[optind - 1]); |
| 3428 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3429 | case 'h': |
| 3430 | help(); |
| 3431 | return 0; |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 3432 | case 'f': |
| 3433 | fmt = optarg; |
| 3434 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3435 | case 'F': |
| 3436 | out_basefmt = optarg; |
| 3437 | break; |
| 3438 | case 'b': |
| 3439 | out_baseimg = optarg; |
| 3440 | break; |
| 3441 | case 'u': |
| 3442 | unsafe = 1; |
| 3443 | break; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3444 | case 'p': |
| 3445 | progress = 1; |
| 3446 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3447 | case 't': |
| 3448 | cache = optarg; |
| 3449 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3450 | case 'T': |
| 3451 | src_cache = optarg; |
| 3452 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3453 | case 'q': |
| 3454 | quiet = true; |
| 3455 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 3456 | case OPTION_OBJECT: |
| 3457 | user_creatable_process_cmdline(optarg); |
| 3458 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3459 | case OPTION_IMAGE_OPTS: |
| 3460 | image_opts = true; |
| 3461 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3462 | case 'U': |
| 3463 | force_share = true; |
| 3464 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3465 | } |
| 3466 | } |
| 3467 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3468 | if (quiet) { |
| 3469 | progress = 0; |
| 3470 | } |
| 3471 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3472 | if (optind != argc - 1) { |
| 3473 | error_exit("Expecting one image file name"); |
| 3474 | } |
| 3475 | if (!unsafe && !out_baseimg) { |
| 3476 | error_exit("Must specify backing file (-b) or use unsafe mode (-u)"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3477 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3478 | filename = argv[optind++]; |
| 3479 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3480 | qemu_progress_init(progress, 2.0); |
| 3481 | qemu_progress_print(0, 100); |
| 3482 | |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3483 | flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0); |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3484 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3485 | if (ret < 0) { |
| 3486 | error_report("Invalid cache option: %s", cache); |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3487 | goto out; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3488 | } |
| 3489 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3490 | src_flags = 0; |
| 3491 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3492 | if (ret < 0) { |
| 3493 | error_report("Invalid source cache option: %s", src_cache); |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3494 | goto out; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3495 | } |
| 3496 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3497 | /* The source files are opened read-only, don't care about WCE */ |
| 3498 | assert((src_flags & BDRV_O_RDWR) == 0); |
| 3499 | (void) src_writethrough; |
| 3500 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3501 | /* |
| 3502 | * Open the images. |
| 3503 | * |
| 3504 | * Ignore the old backing file for unsafe rebase in case we want to correct |
| 3505 | * the reference to a renamed or moved backing file. |
| 3506 | */ |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3507 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 3508 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3509 | if (!blk) { |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3510 | ret = -1; |
| 3511 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3512 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3513 | bs = blk_bs(blk); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3514 | |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3515 | unfiltered_bs = bdrv_skip_filters(bs); |
| 3516 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3517 | if (out_basefmt != NULL) { |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3518 | if (bdrv_find_format(out_basefmt) == NULL) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3519 | error_report("Invalid format name: '%s'", out_basefmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3520 | ret = -1; |
| 3521 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3522 | } |
| 3523 | } |
| 3524 | |
| 3525 | /* For safe rebasing we need to compare old and new backing file */ |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3526 | if (!unsafe) { |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3527 | QDict *options = NULL; |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3528 | BlockDriverState *base_bs = bdrv_cow_bs(unfiltered_bs); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3529 | |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3530 | if (base_bs) { |
Kevin Wolf | d861ab3 | 2019-04-25 14:25:10 +0200 | [diff] [blame] | 3531 | blk_old_backing = blk_new(qemu_get_aio_context(), |
| 3532 | BLK_PERM_CONSISTENT_READ, |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3533 | BLK_PERM_ALL); |
| 3534 | ret = blk_insert_bs(blk_old_backing, base_bs, |
| 3535 | &local_err); |
| 3536 | if (ret < 0) { |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3537 | error_reportf_err(local_err, |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3538 | "Could not reuse old backing file '%s': ", |
| 3539 | base_bs->filename); |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3540 | goto out; |
| 3541 | } |
| 3542 | } else { |
| 3543 | blk_old_backing = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3544 | } |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3545 | |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3546 | if (out_baseimg[0]) { |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3547 | const char *overlay_filename; |
| 3548 | char *out_real_path; |
| 3549 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3550 | options = qdict_new(); |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3551 | if (out_basefmt) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 3552 | qdict_put_str(options, "driver", out_basefmt); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3553 | } |
| 3554 | if (force_share) { |
| 3555 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3556 | } |
| 3557 | |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3558 | bdrv_refresh_filename(bs); |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3559 | overlay_filename = bs->exact_filename[0] ? bs->exact_filename |
| 3560 | : bs->filename; |
Max Reitz | 645ae7d | 2019-02-01 20:29:14 +0100 | [diff] [blame] | 3561 | out_real_path = |
| 3562 | bdrv_get_full_backing_filename_from_filename(overlay_filename, |
| 3563 | out_baseimg, |
| 3564 | &local_err); |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3565 | if (local_err) { |
Max Reitz | f22356d | 2019-05-28 21:53:38 +0200 | [diff] [blame] | 3566 | qobject_unref(options); |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3567 | error_reportf_err(local_err, |
| 3568 | "Could not resolve backing filename: "); |
| 3569 | ret = -1; |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3570 | goto out; |
| 3571 | } |
| 3572 | |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3573 | /* |
| 3574 | * Find out whether we rebase an image on top of a previous image |
| 3575 | * in its chain. |
| 3576 | */ |
| 3577 | prefix_chain_bs = bdrv_find_backing_image(bs, out_real_path); |
Sam Eiderman | 330c729 | 2019-05-23 19:33:37 +0300 | [diff] [blame] | 3578 | if (prefix_chain_bs) { |
Max Reitz | f22356d | 2019-05-28 21:53:38 +0200 | [diff] [blame] | 3579 | qobject_unref(options); |
Sam Eiderman | 330c729 | 2019-05-23 19:33:37 +0300 | [diff] [blame] | 3580 | g_free(out_real_path); |
Max Reitz | f22356d | 2019-05-28 21:53:38 +0200 | [diff] [blame] | 3581 | |
Kevin Wolf | d861ab3 | 2019-04-25 14:25:10 +0200 | [diff] [blame] | 3582 | blk_new_backing = blk_new(qemu_get_aio_context(), |
| 3583 | BLK_PERM_CONSISTENT_READ, |
Sam Eiderman | 330c729 | 2019-05-23 19:33:37 +0300 | [diff] [blame] | 3584 | BLK_PERM_ALL); |
| 3585 | ret = blk_insert_bs(blk_new_backing, prefix_chain_bs, |
| 3586 | &local_err); |
| 3587 | if (ret < 0) { |
| 3588 | error_reportf_err(local_err, |
| 3589 | "Could not reuse backing file '%s': ", |
| 3590 | out_baseimg); |
| 3591 | goto out; |
| 3592 | } |
| 3593 | } else { |
| 3594 | blk_new_backing = blk_new_open(out_real_path, NULL, |
| 3595 | options, src_flags, &local_err); |
| 3596 | g_free(out_real_path); |
| 3597 | if (!blk_new_backing) { |
| 3598 | error_reportf_err(local_err, |
| 3599 | "Could not open new backing file '%s': ", |
| 3600 | out_baseimg); |
| 3601 | ret = -1; |
| 3602 | goto out; |
| 3603 | } |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3604 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3605 | } |
| 3606 | } |
| 3607 | |
| 3608 | /* |
| 3609 | * Check each unallocated cluster in the COW file. If it is unallocated, |
| 3610 | * accesses go to the backing file. We must therefore compare this cluster |
| 3611 | * in the old and new backing file, and if they differ we need to copy it |
| 3612 | * from the old backing file into the COW file. |
| 3613 | * |
| 3614 | * If qemu-img crashes during this step, no harm is done. The content of |
| 3615 | * the image is the same as the original one at any time. |
| 3616 | */ |
| 3617 | if (!unsafe) { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3618 | int64_t size; |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3619 | int64_t old_backing_size = 0; |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3620 | int64_t new_backing_size = 0; |
| 3621 | uint64_t offset; |
| 3622 | int64_t n; |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 3623 | float local_progress = 0; |
TeLeMan | d6771bf | 2010-02-08 16:20:00 +0800 | [diff] [blame] | 3624 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3625 | buf_old = blk_blockalign(blk, IO_BUF_SIZE); |
| 3626 | buf_new = blk_blockalign(blk, IO_BUF_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3627 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3628 | size = blk_getlength(blk); |
| 3629 | if (size < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3630 | error_report("Could not get size of '%s': %s", |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3631 | filename, strerror(-size)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3632 | ret = -1; |
| 3633 | goto out; |
| 3634 | } |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3635 | if (blk_old_backing) { |
| 3636 | old_backing_size = blk_getlength(blk_old_backing); |
| 3637 | if (old_backing_size < 0) { |
| 3638 | char backing_name[PATH_MAX]; |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3639 | |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3640 | bdrv_get_backing_filename(bs, backing_name, |
| 3641 | sizeof(backing_name)); |
| 3642 | error_report("Could not get size of '%s': %s", |
| 3643 | backing_name, strerror(-old_backing_size)); |
| 3644 | ret = -1; |
| 3645 | goto out; |
| 3646 | } |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3647 | } |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3648 | if (blk_new_backing) { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3649 | new_backing_size = blk_getlength(blk_new_backing); |
| 3650 | if (new_backing_size < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3651 | error_report("Could not get size of '%s': %s", |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3652 | out_baseimg, strerror(-new_backing_size)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 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 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3658 | if (size != 0) { |
| 3659 | local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE)); |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 3660 | } |
| 3661 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3662 | for (offset = 0; offset < size; offset += n) { |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3663 | bool buf_old_is_zero = false; |
| 3664 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3665 | /* How many bytes can we handle with the next read? */ |
| 3666 | n = MIN(IO_BUF_SIZE, size - offset); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3667 | |
| 3668 | /* If the cluster is allocated, we don't need to take action */ |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3669 | ret = bdrv_is_allocated(unfiltered_bs, offset, n, &n); |
Paolo Bonzini | d663640 | 2013-09-04 19:00:25 +0200 | [diff] [blame] | 3670 | if (ret < 0) { |
| 3671 | error_report("error while reading image metadata: %s", |
| 3672 | strerror(-ret)); |
| 3673 | goto out; |
| 3674 | } |
Kevin Wolf | cc60e32 | 2010-04-29 14:47:48 +0200 | [diff] [blame] | 3675 | if (ret) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3676 | continue; |
| 3677 | } |
| 3678 | |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3679 | if (prefix_chain_bs) { |
| 3680 | /* |
| 3681 | * If cluster wasn't changed since prefix_chain, we don't need |
| 3682 | * to take action |
| 3683 | */ |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3684 | ret = bdrv_is_allocated_above(bdrv_cow_bs(unfiltered_bs), |
| 3685 | prefix_chain_bs, false, |
| 3686 | offset, n, &n); |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3687 | if (ret < 0) { |
| 3688 | error_report("error while reading image metadata: %s", |
| 3689 | strerror(-ret)); |
| 3690 | goto out; |
| 3691 | } |
| 3692 | if (!ret) { |
| 3693 | continue; |
| 3694 | } |
| 3695 | } |
| 3696 | |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3697 | /* |
| 3698 | * Read old and new backing file and take into consideration that |
| 3699 | * backing files may be smaller than the COW image. |
| 3700 | */ |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3701 | if (offset >= old_backing_size) { |
| 3702 | memset(buf_old, 0, n); |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3703 | buf_old_is_zero = true; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3704 | } else { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3705 | if (offset + n > old_backing_size) { |
| 3706 | n = old_backing_size - offset; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3707 | } |
| 3708 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3709 | ret = blk_pread(blk_old_backing, offset, buf_old, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3710 | if (ret < 0) { |
| 3711 | error_report("error while reading from old backing file"); |
| 3712 | goto out; |
| 3713 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3714 | } |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3715 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3716 | if (offset >= new_backing_size || !blk_new_backing) { |
| 3717 | memset(buf_new, 0, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3718 | } else { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3719 | if (offset + n > new_backing_size) { |
| 3720 | n = new_backing_size - offset; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3721 | } |
| 3722 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3723 | ret = blk_pread(blk_new_backing, offset, buf_new, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3724 | if (ret < 0) { |
| 3725 | error_report("error while reading from new backing file"); |
| 3726 | goto out; |
| 3727 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3728 | } |
| 3729 | |
| 3730 | /* If they differ, we need to write to the COW file */ |
| 3731 | uint64_t written = 0; |
| 3732 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3733 | while (written < n) { |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 3734 | int64_t pnum; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3735 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3736 | if (compare_buffers(buf_old + written, buf_new + written, |
| 3737 | n - written, &pnum)) |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3738 | { |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3739 | if (buf_old_is_zero) { |
| 3740 | ret = blk_pwrite_zeroes(blk, offset + written, pnum, 0); |
| 3741 | } else { |
| 3742 | ret = blk_pwrite(blk, offset + written, |
| 3743 | buf_old + written, pnum, 0); |
| 3744 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3745 | if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3746 | error_report("Error while writing to COW image: %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3747 | strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3748 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3749 | } |
| 3750 | } |
| 3751 | |
| 3752 | written += pnum; |
| 3753 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3754 | qemu_progress_print(local_progress, 100); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3755 | } |
| 3756 | } |
| 3757 | |
| 3758 | /* |
| 3759 | * Change the backing file. All clusters that are different from the old |
| 3760 | * backing file are overwritten in the COW file now, so the visible content |
| 3761 | * doesn't change when we switch the backing file. |
| 3762 | */ |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3763 | if (out_baseimg && *out_baseimg) { |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3764 | ret = bdrv_change_backing_file(unfiltered_bs, out_baseimg, out_basefmt, |
| 3765 | true); |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3766 | } else { |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3767 | ret = bdrv_change_backing_file(unfiltered_bs, NULL, NULL, false); |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3768 | } |
| 3769 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3770 | if (ret == -ENOSPC) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3771 | error_report("Could not change the backing file to '%s': No " |
| 3772 | "space left in the file header", out_baseimg); |
Eric Blake | a7cd44b | 2021-07-08 10:52:28 -0500 | [diff] [blame] | 3773 | } else if (ret == -EINVAL && out_baseimg && !out_basefmt) { |
| 3774 | error_report("Could not change the backing file to '%s': backing " |
| 3775 | "format must be specified", out_baseimg); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3776 | } else if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3777 | error_report("Could not change the backing file to '%s': %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3778 | out_baseimg, strerror(-ret)); |
| 3779 | } |
| 3780 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3781 | qemu_progress_print(100, 0); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3782 | /* |
| 3783 | * TODO At this point it is possible to check if any clusters that are |
| 3784 | * allocated in the COW file are the same in the backing file. If so, they |
| 3785 | * could be dropped from the COW file. Don't do this before switching the |
| 3786 | * backing file, in case of a crash this would lead to corruption. |
| 3787 | */ |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3788 | out: |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3789 | qemu_progress_end(); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3790 | /* Cleanup */ |
| 3791 | if (!unsafe) { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3792 | blk_unref(blk_old_backing); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3793 | blk_unref(blk_new_backing); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3794 | } |
Paolo Bonzini | 396374c | 2016-02-25 23:53:54 +0100 | [diff] [blame] | 3795 | qemu_vfree(buf_old); |
| 3796 | qemu_vfree(buf_new); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3797 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3798 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3799 | if (ret) { |
| 3800 | return 1; |
| 3801 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3802 | return 0; |
| 3803 | } |
| 3804 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3805 | static int img_resize(int argc, char **argv) |
| 3806 | { |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 3807 | Error *err = NULL; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3808 | int c, ret, relative; |
| 3809 | const char *filename, *fmt, *size; |
Max Reitz | 09c5c6d | 2019-09-18 11:51:44 +0200 | [diff] [blame] | 3810 | int64_t n, total_size, current_size; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3811 | bool quiet = false; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3812 | BlockBackend *blk = NULL; |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3813 | PreallocMode prealloc = PREALLOC_MODE_OFF; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3814 | QemuOpts *param; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3815 | |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3816 | static QemuOptsList resize_options = { |
| 3817 | .name = "resize_options", |
| 3818 | .head = QTAILQ_HEAD_INITIALIZER(resize_options.head), |
| 3819 | .desc = { |
| 3820 | { |
| 3821 | .name = BLOCK_OPT_SIZE, |
| 3822 | .type = QEMU_OPT_SIZE, |
| 3823 | .help = "Virtual disk size" |
| 3824 | }, { |
| 3825 | /* end of list */ |
| 3826 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3827 | }, |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3828 | }; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3829 | bool image_opts = false; |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3830 | bool shrink = false; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3831 | |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 3832 | /* Remove size from argv manually so that negative numbers are not treated |
| 3833 | * as options by getopt. */ |
| 3834 | if (argc < 3) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3835 | error_exit("Not enough arguments"); |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 3836 | return 1; |
| 3837 | } |
| 3838 | |
| 3839 | size = argv[--argc]; |
| 3840 | |
| 3841 | /* Parse getopt arguments */ |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3842 | fmt = NULL; |
| 3843 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3844 | static const struct option long_options[] = { |
| 3845 | {"help", no_argument, 0, 'h'}, |
| 3846 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3847 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3848 | {"preallocation", required_argument, 0, OPTION_PREALLOCATION}, |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3849 | {"shrink", no_argument, 0, OPTION_SHRINK}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3850 | {0, 0, 0, 0} |
| 3851 | }; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3852 | c = getopt_long(argc, argv, ":f:hq", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3853 | long_options, NULL); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3854 | if (c == -1) { |
| 3855 | break; |
| 3856 | } |
| 3857 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3858 | case ':': |
| 3859 | missing_argument(argv[optind - 1]); |
| 3860 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3861 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3862 | unrecognized_option(argv[optind - 1]); |
| 3863 | break; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3864 | case 'h': |
| 3865 | help(); |
| 3866 | break; |
| 3867 | case 'f': |
| 3868 | fmt = optarg; |
| 3869 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3870 | case 'q': |
| 3871 | quiet = true; |
| 3872 | break; |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 3873 | case OPTION_OBJECT: |
| 3874 | user_creatable_process_cmdline(optarg); |
| 3875 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3876 | case OPTION_IMAGE_OPTS: |
| 3877 | image_opts = true; |
| 3878 | break; |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3879 | case OPTION_PREALLOCATION: |
Marc-André Lureau | f7abe0e | 2017-08-24 10:46:10 +0200 | [diff] [blame] | 3880 | prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg, |
Markus Armbruster | 06c60b6 | 2017-08-24 10:45:57 +0200 | [diff] [blame] | 3881 | PREALLOC_MODE__MAX, NULL); |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3882 | if (prealloc == PREALLOC_MODE__MAX) { |
| 3883 | error_report("Invalid preallocation mode '%s'", optarg); |
| 3884 | return 1; |
| 3885 | } |
| 3886 | break; |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3887 | case OPTION_SHRINK: |
| 3888 | shrink = true; |
| 3889 | break; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3890 | } |
| 3891 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 3892 | if (optind != argc - 1) { |
Max Reitz | be8fbd4 | 2018-02-05 17:27:45 +0100 | [diff] [blame] | 3893 | error_exit("Expecting image file name and size"); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3894 | } |
| 3895 | filename = argv[optind++]; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3896 | |
| 3897 | /* Choose grow, shrink, or absolute resize mode */ |
| 3898 | switch (size[0]) { |
| 3899 | case '+': |
| 3900 | relative = 1; |
| 3901 | size++; |
| 3902 | break; |
| 3903 | case '-': |
| 3904 | relative = -1; |
| 3905 | size++; |
| 3906 | break; |
| 3907 | default: |
| 3908 | relative = 0; |
| 3909 | break; |
| 3910 | } |
| 3911 | |
| 3912 | /* Parse size */ |
Peter Crosthwaite | 87ea75d | 2014-01-01 18:49:17 -0800 | [diff] [blame] | 3913 | param = qemu_opts_create(&resize_options, NULL, 0, &error_abort); |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 3914 | if (!qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err)) { |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 3915 | error_report_err(err); |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3916 | ret = -1; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3917 | qemu_opts_del(param); |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3918 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3919 | } |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3920 | n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0); |
| 3921 | qemu_opts_del(param); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3922 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 3923 | blk = img_open(image_opts, filename, fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3924 | BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet, |
| 3925 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3926 | if (!blk) { |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3927 | ret = -1; |
| 3928 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3929 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3930 | |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3931 | current_size = blk_getlength(blk); |
| 3932 | if (current_size < 0) { |
| 3933 | error_report("Failed to inquire current image length: %s", |
| 3934 | strerror(-current_size)); |
| 3935 | ret = -1; |
| 3936 | goto out; |
| 3937 | } |
| 3938 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3939 | if (relative) { |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3940 | total_size = current_size + n * relative; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3941 | } else { |
| 3942 | total_size = n; |
| 3943 | } |
| 3944 | if (total_size <= 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3945 | error_report("New image size must be positive"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3946 | ret = -1; |
| 3947 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3948 | } |
| 3949 | |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3950 | if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) { |
| 3951 | error_report("Preallocation can only be used for growing images"); |
| 3952 | ret = -1; |
| 3953 | goto out; |
| 3954 | } |
| 3955 | |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3956 | if (total_size < current_size && !shrink) { |
Kevin Wolf | 1c404d7 | 2020-07-10 14:17:17 +0200 | [diff] [blame] | 3957 | error_report("Use the --shrink option to perform a shrink operation."); |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3958 | warn_report("Shrinking an image will delete all data beyond the " |
| 3959 | "shrunken image's end. Before performing such an " |
| 3960 | "operation, make sure there is no important data there."); |
Kevin Wolf | 1c404d7 | 2020-07-10 14:17:17 +0200 | [diff] [blame] | 3961 | ret = -1; |
| 3962 | goto out; |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3963 | } |
| 3964 | |
Max Reitz | e8d04f9 | 2019-09-18 11:51:43 +0200 | [diff] [blame] | 3965 | /* |
| 3966 | * The user expects the image to have the desired size after |
| 3967 | * resizing, so pass @exact=true. It is of no use to report |
| 3968 | * success when the image has not actually been resized. |
| 3969 | */ |
Kevin Wolf | 8c6242b | 2020-04-24 14:54:41 +0200 | [diff] [blame] | 3970 | ret = blk_truncate(blk, total_size, true, prealloc, 0, &err); |
Max Reitz | 09c5c6d | 2019-09-18 11:51:44 +0200 | [diff] [blame] | 3971 | if (!ret) { |
| 3972 | qprintf(quiet, "Image resized.\n"); |
| 3973 | } else { |
Max Reitz | ed3d2ec | 2017-03-28 22:51:27 +0200 | [diff] [blame] | 3974 | error_report_err(err); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3975 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3976 | out: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3977 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3978 | if (ret) { |
| 3979 | return 1; |
| 3980 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3981 | return 0; |
| 3982 | } |
| 3983 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3984 | static void amend_status_cb(BlockDriverState *bs, |
Max Reitz | 8b13976 | 2015-07-27 17:51:32 +0200 | [diff] [blame] | 3985 | int64_t offset, int64_t total_work_size, |
| 3986 | void *opaque) |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3987 | { |
| 3988 | qemu_progress_print(100.f * offset / total_work_size, 0); |
| 3989 | } |
| 3990 | |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 3991 | static int print_amend_option_help(const char *format) |
| 3992 | { |
| 3993 | BlockDriver *drv; |
| 3994 | |
| 3995 | /* Find driver and parse its options */ |
| 3996 | drv = bdrv_find_format(format); |
| 3997 | if (!drv) { |
| 3998 | error_report("Unknown file format '%s'", format); |
| 3999 | return 1; |
| 4000 | } |
| 4001 | |
| 4002 | if (!drv->bdrv_amend_options) { |
| 4003 | error_report("Format driver '%s' does not support option amendment", |
| 4004 | format); |
| 4005 | return 1; |
| 4006 | } |
| 4007 | |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4008 | /* Every driver supporting amendment must have amend_opts */ |
| 4009 | assert(drv->amend_opts); |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4010 | |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4011 | printf("Amend options for '%s':\n", format); |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4012 | qemu_opts_print_help(drv->amend_opts, false); |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4013 | return 0; |
| 4014 | } |
| 4015 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4016 | static int img_amend(int argc, char **argv) |
| 4017 | { |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 4018 | Error *err = NULL; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4019 | int c, ret = 0; |
| 4020 | char *options = NULL; |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4021 | QemuOptsList *amend_opts = NULL; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 4022 | QemuOpts *opts = NULL; |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 4023 | const char *fmt = NULL, *filename, *cache; |
| 4024 | int flags; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 4025 | bool writethrough; |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4026 | bool quiet = false, progress = false; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 4027 | BlockBackend *blk = NULL; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4028 | BlockDriverState *bs = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 4029 | bool image_opts = false; |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4030 | bool force = false; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4031 | |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 4032 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4033 | for (;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4034 | static const struct option long_options[] = { |
| 4035 | {"help", no_argument, 0, 'h'}, |
| 4036 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 4037 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4038 | {"force", no_argument, 0, OPTION_FORCE}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4039 | {0, 0, 0, 0} |
| 4040 | }; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4041 | c = getopt_long(argc, argv, ":ho:f:t:pq", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4042 | long_options, NULL); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4043 | if (c == -1) { |
| 4044 | break; |
| 4045 | } |
| 4046 | |
| 4047 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4048 | case ':': |
| 4049 | missing_argument(argv[optind - 1]); |
| 4050 | break; |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4051 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4052 | unrecognized_option(argv[optind - 1]); |
| 4053 | break; |
| 4054 | case 'h': |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4055 | help(); |
| 4056 | break; |
| 4057 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 4058 | if (accumulate_options(&options, optarg) < 0) { |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4059 | ret = -1; |
| 4060 | goto out_no_progress; |
| 4061 | } |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4062 | break; |
| 4063 | case 'f': |
| 4064 | fmt = optarg; |
| 4065 | break; |
| 4066 | case 't': |
| 4067 | cache = optarg; |
| 4068 | break; |
| 4069 | case 'p': |
| 4070 | progress = true; |
| 4071 | break; |
| 4072 | case 'q': |
| 4073 | quiet = true; |
| 4074 | break; |
| 4075 | case OPTION_OBJECT: |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 4076 | user_creatable_process_cmdline(optarg); |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4077 | break; |
| 4078 | case OPTION_IMAGE_OPTS: |
| 4079 | image_opts = true; |
| 4080 | break; |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4081 | case OPTION_FORCE: |
| 4082 | force = true; |
| 4083 | break; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4084 | } |
| 4085 | } |
| 4086 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4087 | if (!options) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 4088 | error_exit("Must specify options (-o)"); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4089 | } |
| 4090 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4091 | if (quiet) { |
| 4092 | progress = false; |
| 4093 | } |
| 4094 | qemu_progress_init(progress, 1.0); |
| 4095 | |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4096 | filename = (optind == argc - 1) ? argv[argc - 1] : NULL; |
| 4097 | if (fmt && has_help_option(options)) { |
| 4098 | /* If a format is explicitly specified (and possibly no filename is |
| 4099 | * given), print option help here */ |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4100 | ret = print_amend_option_help(fmt); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4101 | goto out; |
| 4102 | } |
| 4103 | |
| 4104 | if (optind != argc - 1) { |
Max Reitz | b2f27e4 | 2014-10-27 11:12:52 +0100 | [diff] [blame] | 4105 | error_report("Expecting one image file name"); |
| 4106 | ret = -1; |
| 4107 | goto out; |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4108 | } |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4109 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 4110 | flags = BDRV_O_RDWR; |
| 4111 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 4112 | if (ret < 0) { |
| 4113 | error_report("Invalid cache option: %s", cache); |
| 4114 | goto out; |
| 4115 | } |
| 4116 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4117 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 4118 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 4119 | if (!blk) { |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4120 | ret = -1; |
| 4121 | goto out; |
| 4122 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 4123 | bs = blk_bs(blk); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4124 | |
| 4125 | fmt = bs->drv->format_name; |
| 4126 | |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 4127 | if (has_help_option(options)) { |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4128 | /* If the format was auto-detected, print option help here */ |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4129 | ret = print_amend_option_help(fmt); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4130 | goto out; |
| 4131 | } |
| 4132 | |
Max Reitz | 1f99668 | 2018-05-09 23:00:17 +0200 | [diff] [blame] | 4133 | if (!bs->drv->bdrv_amend_options) { |
| 4134 | error_report("Format driver '%s' does not support option amendment", |
Max Reitz | b2439d2 | 2014-12-02 18:32:47 +0100 | [diff] [blame] | 4135 | fmt); |
| 4136 | ret = -1; |
| 4137 | goto out; |
| 4138 | } |
| 4139 | |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4140 | /* Every driver supporting amendment must have amend_opts */ |
| 4141 | assert(bs->drv->amend_opts); |
Max Reitz | 1f99668 | 2018-05-09 23:00:17 +0200 | [diff] [blame] | 4142 | |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4143 | amend_opts = qemu_opts_append(amend_opts, bs->drv->amend_opts); |
| 4144 | opts = qemu_opts_create(amend_opts, NULL, 0, &error_abort); |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 4145 | if (!qemu_opts_do_parse(opts, options, NULL, &err)) { |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4146 | /* Try to parse options using the create options */ |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4147 | amend_opts = qemu_opts_append(amend_opts, bs->drv->create_opts); |
| 4148 | qemu_opts_del(opts); |
| 4149 | opts = qemu_opts_create(amend_opts, NULL, 0, &error_abort); |
Markus Armbruster | 9e194e0 | 2020-07-07 18:06:11 +0200 | [diff] [blame] | 4150 | if (qemu_opts_do_parse(opts, options, NULL, NULL)) { |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4151 | error_append_hint(&err, |
| 4152 | "This option is only supported for image creation\n"); |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4153 | } |
| 4154 | |
Paolo Bonzini | ece9086 | 2017-01-04 15:56:24 +0100 | [diff] [blame] | 4155 | error_report_err(err); |
| 4156 | ret = -1; |
| 4157 | goto out; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4158 | } |
| 4159 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4160 | /* In case the driver does not call amend_status_cb() */ |
| 4161 | qemu_progress_print(0.f, 0); |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4162 | ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL, force, &err); |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4163 | qemu_progress_print(100.f, 0); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4164 | if (ret < 0) { |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 4165 | error_report_err(err); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4166 | goto out; |
| 4167 | } |
| 4168 | |
| 4169 | out: |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4170 | qemu_progress_end(); |
| 4171 | |
Max Reitz | e814dff | 2015-08-20 16:00:38 -0700 | [diff] [blame] | 4172 | out_no_progress: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 4173 | blk_unref(blk); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 4174 | qemu_opts_del(opts); |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4175 | qemu_opts_free(amend_opts); |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 4176 | g_free(options); |
| 4177 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4178 | if (ret) { |
| 4179 | return 1; |
| 4180 | } |
| 4181 | return 0; |
| 4182 | } |
| 4183 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4184 | typedef struct BenchData { |
| 4185 | BlockBackend *blk; |
| 4186 | uint64_t image_size; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4187 | bool write; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4188 | int bufsize; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4189 | int step; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4190 | int nrreq; |
| 4191 | int n; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4192 | int flush_interval; |
| 4193 | bool drain_on_flush; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4194 | uint8_t *buf; |
| 4195 | QEMUIOVector *qiov; |
| 4196 | |
| 4197 | int in_flight; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4198 | bool in_flush; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4199 | uint64_t offset; |
| 4200 | } BenchData; |
| 4201 | |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4202 | static void bench_undrained_flush_cb(void *opaque, int ret) |
| 4203 | { |
| 4204 | if (ret < 0) { |
Markus Armbruster | df3c286 | 2016-08-03 13:37:51 +0200 | [diff] [blame] | 4205 | error_report("Failed flush request: %s", strerror(-ret)); |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4206 | exit(EXIT_FAILURE); |
| 4207 | } |
| 4208 | } |
| 4209 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4210 | static void bench_cb(void *opaque, int ret) |
| 4211 | { |
| 4212 | BenchData *b = opaque; |
| 4213 | BlockAIOCB *acb; |
| 4214 | |
| 4215 | if (ret < 0) { |
Markus Armbruster | df3c286 | 2016-08-03 13:37:51 +0200 | [diff] [blame] | 4216 | error_report("Failed request: %s", strerror(-ret)); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4217 | exit(EXIT_FAILURE); |
| 4218 | } |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4219 | |
| 4220 | if (b->in_flush) { |
| 4221 | /* Just finished a flush with drained queue: Start next requests */ |
| 4222 | assert(b->in_flight == 0); |
| 4223 | b->in_flush = false; |
| 4224 | } else if (b->in_flight > 0) { |
| 4225 | int remaining = b->n - b->in_flight; |
| 4226 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4227 | b->n--; |
| 4228 | b->in_flight--; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4229 | |
| 4230 | /* Time for flush? Drain queue if requested, then flush */ |
| 4231 | if (b->flush_interval && remaining % b->flush_interval == 0) { |
| 4232 | if (!b->in_flight || !b->drain_on_flush) { |
| 4233 | BlockCompletionFunc *cb; |
| 4234 | |
| 4235 | if (b->drain_on_flush) { |
| 4236 | b->in_flush = true; |
| 4237 | cb = bench_cb; |
| 4238 | } else { |
| 4239 | cb = bench_undrained_flush_cb; |
| 4240 | } |
| 4241 | |
| 4242 | acb = blk_aio_flush(b->blk, cb, b); |
| 4243 | if (!acb) { |
| 4244 | error_report("Failed to issue flush request"); |
| 4245 | exit(EXIT_FAILURE); |
| 4246 | } |
| 4247 | } |
| 4248 | if (b->drain_on_flush) { |
| 4249 | return; |
| 4250 | } |
| 4251 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4252 | } |
| 4253 | |
| 4254 | while (b->n > b->in_flight && b->in_flight < b->nrreq) { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4255 | int64_t offset = b->offset; |
| 4256 | /* blk_aio_* might look for completed I/Os and kick bench_cb |
| 4257 | * again, so make sure this operation is counted by in_flight |
| 4258 | * and b->offset is ready for the next submission. |
| 4259 | */ |
| 4260 | b->in_flight++; |
| 4261 | b->offset += b->step; |
| 4262 | b->offset %= b->image_size; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4263 | if (b->write) { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4264 | 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] | 4265 | } else { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4266 | 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] | 4267 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4268 | if (!acb) { |
| 4269 | error_report("Failed to issue request"); |
| 4270 | exit(EXIT_FAILURE); |
| 4271 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4272 | } |
| 4273 | } |
| 4274 | |
| 4275 | static int img_bench(int argc, char **argv) |
| 4276 | { |
| 4277 | int c, ret = 0; |
| 4278 | const char *fmt = NULL, *filename; |
| 4279 | bool quiet = false; |
| 4280 | bool image_opts = false; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4281 | bool is_write = false; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4282 | int count = 75000; |
| 4283 | int depth = 64; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4284 | int64_t offset = 0; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4285 | size_t bufsize = 4096; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4286 | int pattern = 0; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4287 | size_t step = 0; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4288 | int flush_interval = 0; |
| 4289 | bool drain_on_flush = true; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4290 | int64_t image_size; |
| 4291 | BlockBackend *blk = NULL; |
| 4292 | BenchData data = {}; |
| 4293 | int flags = 0; |
Kevin Wolf | 604e861 | 2016-06-14 11:29:32 +0200 | [diff] [blame] | 4294 | bool writethrough = false; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4295 | struct timeval t1, t2; |
| 4296 | int i; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4297 | bool force_share = false; |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4298 | size_t buf_size; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4299 | |
| 4300 | for (;;) { |
| 4301 | static const struct option long_options[] = { |
| 4302 | {"help", no_argument, 0, 'h'}, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4303 | {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL}, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4304 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4305 | {"pattern", required_argument, 0, OPTION_PATTERN}, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4306 | {"no-drain", no_argument, 0, OPTION_NO_DRAIN}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4307 | {"force-share", no_argument, 0, 'U'}, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4308 | {0, 0, 0, 0} |
| 4309 | }; |
Aarushi Mehta | cdd2677 | 2020-01-20 14:18:55 +0000 | [diff] [blame] | 4310 | c = getopt_long(argc, argv, ":hc:d:f:ni:o:qs:S:t:wU", long_options, |
| 4311 | NULL); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4312 | if (c == -1) { |
| 4313 | break; |
| 4314 | } |
| 4315 | |
| 4316 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4317 | case ':': |
| 4318 | missing_argument(argv[optind - 1]); |
| 4319 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4320 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4321 | unrecognized_option(argv[optind - 1]); |
| 4322 | break; |
| 4323 | case 'h': |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4324 | help(); |
| 4325 | break; |
| 4326 | case 'c': |
| 4327 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4328 | unsigned long res; |
| 4329 | |
| 4330 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4331 | error_report("Invalid request count specified"); |
| 4332 | return 1; |
| 4333 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4334 | count = res; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4335 | break; |
| 4336 | } |
| 4337 | case 'd': |
| 4338 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4339 | unsigned long res; |
| 4340 | |
| 4341 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4342 | error_report("Invalid queue depth specified"); |
| 4343 | return 1; |
| 4344 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4345 | depth = res; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4346 | break; |
| 4347 | } |
| 4348 | case 'f': |
| 4349 | fmt = optarg; |
| 4350 | break; |
| 4351 | case 'n': |
| 4352 | flags |= BDRV_O_NATIVE_AIO; |
| 4353 | break; |
Aarushi Mehta | cdd2677 | 2020-01-20 14:18:55 +0000 | [diff] [blame] | 4354 | case 'i': |
| 4355 | ret = bdrv_parse_aio(optarg, &flags); |
| 4356 | if (ret < 0) { |
| 4357 | error_report("Invalid aio option: %s", optarg); |
| 4358 | ret = -1; |
| 4359 | goto out; |
| 4360 | } |
| 4361 | break; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4362 | case 'o': |
| 4363 | { |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4364 | offset = cvtnum("offset", optarg); |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4365 | if (offset < 0) { |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4366 | return 1; |
| 4367 | } |
| 4368 | break; |
| 4369 | } |
| 4370 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4371 | case 'q': |
| 4372 | quiet = true; |
| 4373 | break; |
| 4374 | case 's': |
| 4375 | { |
| 4376 | int64_t sval; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4377 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4378 | sval = cvtnum_full("buffer size", optarg, 0, INT_MAX); |
| 4379 | if (sval < 0) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4380 | return 1; |
| 4381 | } |
| 4382 | |
| 4383 | bufsize = sval; |
| 4384 | break; |
| 4385 | } |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4386 | case 'S': |
| 4387 | { |
| 4388 | int64_t sval; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4389 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4390 | sval = cvtnum_full("step_size", optarg, 0, INT_MAX); |
| 4391 | if (sval < 0) { |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4392 | return 1; |
| 4393 | } |
| 4394 | |
| 4395 | step = sval; |
| 4396 | break; |
| 4397 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4398 | case 't': |
| 4399 | ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough); |
| 4400 | if (ret < 0) { |
| 4401 | error_report("Invalid cache mode"); |
| 4402 | ret = -1; |
| 4403 | goto out; |
| 4404 | } |
| 4405 | break; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4406 | case 'w': |
| 4407 | flags |= BDRV_O_RDWR; |
| 4408 | is_write = true; |
| 4409 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4410 | case 'U': |
| 4411 | force_share = true; |
| 4412 | break; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4413 | case OPTION_PATTERN: |
| 4414 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4415 | unsigned long res; |
| 4416 | |
| 4417 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) { |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4418 | error_report("Invalid pattern byte specified"); |
| 4419 | return 1; |
| 4420 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4421 | pattern = res; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4422 | break; |
| 4423 | } |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4424 | case OPTION_FLUSH_INTERVAL: |
| 4425 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4426 | unsigned long res; |
| 4427 | |
| 4428 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4429 | error_report("Invalid flush interval specified"); |
| 4430 | return 1; |
| 4431 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4432 | flush_interval = res; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4433 | break; |
| 4434 | } |
| 4435 | case OPTION_NO_DRAIN: |
| 4436 | drain_on_flush = false; |
| 4437 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4438 | case OPTION_IMAGE_OPTS: |
| 4439 | image_opts = true; |
| 4440 | break; |
| 4441 | } |
| 4442 | } |
| 4443 | |
| 4444 | if (optind != argc - 1) { |
| 4445 | error_exit("Expecting one image file name"); |
| 4446 | } |
| 4447 | filename = argv[argc - 1]; |
| 4448 | |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4449 | if (!is_write && flush_interval) { |
| 4450 | error_report("--flush-interval is only available in write tests"); |
| 4451 | ret = -1; |
| 4452 | goto out; |
| 4453 | } |
| 4454 | if (flush_interval && flush_interval < depth) { |
| 4455 | error_report("Flush interval can't be smaller than depth"); |
| 4456 | ret = -1; |
| 4457 | goto out; |
| 4458 | } |
| 4459 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4460 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 4461 | force_share); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4462 | if (!blk) { |
| 4463 | ret = -1; |
| 4464 | goto out; |
| 4465 | } |
| 4466 | |
| 4467 | image_size = blk_getlength(blk); |
| 4468 | if (image_size < 0) { |
| 4469 | ret = image_size; |
| 4470 | goto out; |
| 4471 | } |
| 4472 | |
| 4473 | data = (BenchData) { |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4474 | .blk = blk, |
| 4475 | .image_size = image_size, |
| 4476 | .bufsize = bufsize, |
| 4477 | .step = step ?: bufsize, |
| 4478 | .nrreq = depth, |
| 4479 | .n = count, |
| 4480 | .offset = offset, |
| 4481 | .write = is_write, |
| 4482 | .flush_interval = flush_interval, |
| 4483 | .drain_on_flush = drain_on_flush, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4484 | }; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4485 | printf("Sending %d %s requests, %d bytes each, %d in parallel " |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4486 | "(starting at offset %" PRId64 ", step size %d)\n", |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4487 | data.n, data.write ? "write" : "read", data.bufsize, data.nrreq, |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4488 | data.offset, data.step); |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4489 | if (flush_interval) { |
| 4490 | printf("Sending flush every %d requests\n", flush_interval); |
| 4491 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4492 | |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4493 | buf_size = data.nrreq * data.bufsize; |
| 4494 | data.buf = blk_blockalign(blk, buf_size); |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4495 | memset(data.buf, pattern, data.nrreq * data.bufsize); |
| 4496 | |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4497 | blk_register_buf(blk, data.buf, buf_size); |
| 4498 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4499 | data.qiov = g_new(QEMUIOVector, data.nrreq); |
| 4500 | for (i = 0; i < data.nrreq; i++) { |
| 4501 | qemu_iovec_init(&data.qiov[i], 1); |
| 4502 | qemu_iovec_add(&data.qiov[i], |
| 4503 | data.buf + i * data.bufsize, data.bufsize); |
| 4504 | } |
| 4505 | |
| 4506 | gettimeofday(&t1, NULL); |
| 4507 | bench_cb(&data, 0); |
| 4508 | |
| 4509 | while (data.n > 0) { |
| 4510 | main_loop_wait(false); |
| 4511 | } |
| 4512 | gettimeofday(&t2, NULL); |
| 4513 | |
| 4514 | printf("Run completed in %3.3f seconds.\n", |
| 4515 | (t2.tv_sec - t1.tv_sec) |
| 4516 | + ((double)(t2.tv_usec - t1.tv_usec) / 1000000)); |
| 4517 | |
| 4518 | out: |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4519 | if (data.buf) { |
| 4520 | blk_unregister_buf(blk, data.buf); |
| 4521 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4522 | qemu_vfree(data.buf); |
| 4523 | blk_unref(blk); |
| 4524 | |
| 4525 | if (ret) { |
| 4526 | return 1; |
| 4527 | } |
| 4528 | return 0; |
| 4529 | } |
| 4530 | |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4531 | enum ImgBitmapAct { |
| 4532 | BITMAP_ADD, |
| 4533 | BITMAP_REMOVE, |
| 4534 | BITMAP_CLEAR, |
| 4535 | BITMAP_ENABLE, |
| 4536 | BITMAP_DISABLE, |
| 4537 | BITMAP_MERGE, |
| 4538 | }; |
| 4539 | typedef struct ImgBitmapAction { |
| 4540 | enum ImgBitmapAct act; |
| 4541 | const char *src; /* only used for merge */ |
| 4542 | QSIMPLEQ_ENTRY(ImgBitmapAction) next; |
| 4543 | } ImgBitmapAction; |
| 4544 | |
| 4545 | static int img_bitmap(int argc, char **argv) |
| 4546 | { |
| 4547 | Error *err = NULL; |
| 4548 | int c, ret = 1; |
| 4549 | QemuOpts *opts = NULL; |
| 4550 | const char *fmt = NULL, *src_fmt = NULL, *src_filename = NULL; |
| 4551 | const char *filename, *bitmap; |
| 4552 | BlockBackend *blk = NULL, *src = NULL; |
| 4553 | BlockDriverState *bs = NULL, *src_bs = NULL; |
| 4554 | bool image_opts = false; |
| 4555 | int64_t granularity = 0; |
| 4556 | bool add = false, merge = false; |
| 4557 | QSIMPLEQ_HEAD(, ImgBitmapAction) actions; |
| 4558 | ImgBitmapAction *act, *act_next; |
| 4559 | const char *op; |
| 4560 | |
| 4561 | QSIMPLEQ_INIT(&actions); |
| 4562 | |
| 4563 | for (;;) { |
| 4564 | static const struct option long_options[] = { |
| 4565 | {"help", no_argument, 0, 'h'}, |
| 4566 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 4567 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
| 4568 | {"add", no_argument, 0, OPTION_ADD}, |
| 4569 | {"remove", no_argument, 0, OPTION_REMOVE}, |
| 4570 | {"clear", no_argument, 0, OPTION_CLEAR}, |
| 4571 | {"enable", no_argument, 0, OPTION_ENABLE}, |
| 4572 | {"disable", no_argument, 0, OPTION_DISABLE}, |
| 4573 | {"merge", required_argument, 0, OPTION_MERGE}, |
| 4574 | {"granularity", required_argument, 0, 'g'}, |
| 4575 | {"source-file", required_argument, 0, 'b'}, |
| 4576 | {"source-format", required_argument, 0, 'F'}, |
| 4577 | {0, 0, 0, 0} |
| 4578 | }; |
| 4579 | c = getopt_long(argc, argv, ":b:f:F:g:h", long_options, NULL); |
| 4580 | if (c == -1) { |
| 4581 | break; |
| 4582 | } |
| 4583 | |
| 4584 | switch (c) { |
| 4585 | case ':': |
| 4586 | missing_argument(argv[optind - 1]); |
| 4587 | break; |
| 4588 | case '?': |
| 4589 | unrecognized_option(argv[optind - 1]); |
| 4590 | break; |
| 4591 | case 'h': |
| 4592 | help(); |
| 4593 | break; |
| 4594 | case 'b': |
| 4595 | src_filename = optarg; |
| 4596 | break; |
| 4597 | case 'f': |
| 4598 | fmt = optarg; |
| 4599 | break; |
| 4600 | case 'F': |
| 4601 | src_fmt = optarg; |
| 4602 | break; |
| 4603 | case 'g': |
| 4604 | granularity = cvtnum("granularity", optarg); |
| 4605 | if (granularity < 0) { |
| 4606 | return 1; |
| 4607 | } |
| 4608 | break; |
| 4609 | case OPTION_ADD: |
| 4610 | act = g_new0(ImgBitmapAction, 1); |
| 4611 | act->act = BITMAP_ADD; |
| 4612 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4613 | add = true; |
| 4614 | break; |
| 4615 | case OPTION_REMOVE: |
| 4616 | act = g_new0(ImgBitmapAction, 1); |
| 4617 | act->act = BITMAP_REMOVE; |
| 4618 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4619 | break; |
| 4620 | case OPTION_CLEAR: |
| 4621 | act = g_new0(ImgBitmapAction, 1); |
| 4622 | act->act = BITMAP_CLEAR; |
| 4623 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4624 | break; |
| 4625 | case OPTION_ENABLE: |
| 4626 | act = g_new0(ImgBitmapAction, 1); |
| 4627 | act->act = BITMAP_ENABLE; |
| 4628 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4629 | break; |
| 4630 | case OPTION_DISABLE: |
| 4631 | act = g_new0(ImgBitmapAction, 1); |
| 4632 | act->act = BITMAP_DISABLE; |
| 4633 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4634 | break; |
| 4635 | case OPTION_MERGE: |
| 4636 | act = g_new0(ImgBitmapAction, 1); |
| 4637 | act->act = BITMAP_MERGE; |
| 4638 | act->src = optarg; |
| 4639 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4640 | merge = true; |
| 4641 | break; |
| 4642 | case OPTION_OBJECT: |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 4643 | user_creatable_process_cmdline(optarg); |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4644 | break; |
| 4645 | case OPTION_IMAGE_OPTS: |
| 4646 | image_opts = true; |
| 4647 | break; |
| 4648 | } |
| 4649 | } |
| 4650 | |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4651 | if (QSIMPLEQ_EMPTY(&actions)) { |
| 4652 | error_report("Need at least one of --add, --remove, --clear, " |
| 4653 | "--enable, --disable, or --merge"); |
| 4654 | goto out; |
| 4655 | } |
| 4656 | |
| 4657 | if (granularity && !add) { |
| 4658 | error_report("granularity only supported with --add"); |
| 4659 | goto out; |
| 4660 | } |
| 4661 | if (src_fmt && !src_filename) { |
| 4662 | error_report("-F only supported with -b"); |
| 4663 | goto out; |
| 4664 | } |
| 4665 | if (src_filename && !merge) { |
| 4666 | error_report("Merge bitmap source file only supported with " |
| 4667 | "--merge"); |
| 4668 | goto out; |
| 4669 | } |
| 4670 | |
| 4671 | if (optind != argc - 2) { |
| 4672 | error_report("Expecting filename and bitmap name"); |
| 4673 | goto out; |
| 4674 | } |
| 4675 | |
| 4676 | filename = argv[optind]; |
| 4677 | bitmap = argv[optind + 1]; |
| 4678 | |
Eric Blake | 14f16bf | 2020-09-14 14:10:09 -0500 | [diff] [blame] | 4679 | /* |
| 4680 | * No need to open backing chains; we will be manipulating bitmaps |
| 4681 | * directly in this image without reference to image contents. |
| 4682 | */ |
| 4683 | blk = img_open(image_opts, filename, fmt, BDRV_O_RDWR | BDRV_O_NO_BACKING, |
| 4684 | false, false, false); |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4685 | if (!blk) { |
| 4686 | goto out; |
| 4687 | } |
| 4688 | bs = blk_bs(blk); |
| 4689 | if (src_filename) { |
Eric Blake | 14f16bf | 2020-09-14 14:10:09 -0500 | [diff] [blame] | 4690 | src = img_open(false, src_filename, src_fmt, BDRV_O_NO_BACKING, |
| 4691 | false, false, false); |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4692 | if (!src) { |
| 4693 | goto out; |
| 4694 | } |
| 4695 | src_bs = blk_bs(src); |
| 4696 | } else { |
| 4697 | src_bs = bs; |
| 4698 | } |
| 4699 | |
| 4700 | QSIMPLEQ_FOREACH_SAFE(act, &actions, next, act_next) { |
| 4701 | switch (act->act) { |
| 4702 | case BITMAP_ADD: |
| 4703 | qmp_block_dirty_bitmap_add(bs->node_name, bitmap, |
| 4704 | !!granularity, granularity, true, true, |
| 4705 | false, false, &err); |
| 4706 | op = "add"; |
| 4707 | break; |
| 4708 | case BITMAP_REMOVE: |
| 4709 | qmp_block_dirty_bitmap_remove(bs->node_name, bitmap, &err); |
| 4710 | op = "remove"; |
| 4711 | break; |
| 4712 | case BITMAP_CLEAR: |
| 4713 | qmp_block_dirty_bitmap_clear(bs->node_name, bitmap, &err); |
| 4714 | op = "clear"; |
| 4715 | break; |
| 4716 | case BITMAP_ENABLE: |
| 4717 | qmp_block_dirty_bitmap_enable(bs->node_name, bitmap, &err); |
| 4718 | op = "enable"; |
| 4719 | break; |
| 4720 | case BITMAP_DISABLE: |
| 4721 | qmp_block_dirty_bitmap_disable(bs->node_name, bitmap, &err); |
| 4722 | op = "disable"; |
| 4723 | break; |
Eric Blake | 6c729dd | 2020-05-21 14:21:35 -0500 | [diff] [blame] | 4724 | case BITMAP_MERGE: |
| 4725 | do_dirty_bitmap_merge(bs->node_name, bitmap, src_bs->node_name, |
| 4726 | act->src, &err); |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4727 | op = "merge"; |
| 4728 | break; |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4729 | default: |
| 4730 | g_assert_not_reached(); |
| 4731 | } |
| 4732 | |
| 4733 | if (err) { |
| 4734 | error_reportf_err(err, "Operation %s on bitmap %s failed: ", |
| 4735 | op, bitmap); |
| 4736 | goto out; |
| 4737 | } |
| 4738 | g_free(act); |
| 4739 | } |
| 4740 | |
| 4741 | ret = 0; |
| 4742 | |
| 4743 | out: |
| 4744 | blk_unref(src); |
| 4745 | blk_unref(blk); |
| 4746 | qemu_opts_del(opts); |
| 4747 | return ret; |
| 4748 | } |
| 4749 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4750 | #define C_BS 01 |
| 4751 | #define C_COUNT 02 |
| 4752 | #define C_IF 04 |
| 4753 | #define C_OF 010 |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4754 | #define C_SKIP 020 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4755 | |
| 4756 | struct DdInfo { |
| 4757 | unsigned int flags; |
| 4758 | int64_t count; |
| 4759 | }; |
| 4760 | |
| 4761 | struct DdIo { |
| 4762 | int bsz; /* Block size */ |
| 4763 | char *filename; |
| 4764 | uint8_t *buf; |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4765 | int64_t offset; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4766 | }; |
| 4767 | |
| 4768 | struct DdOpts { |
| 4769 | const char *name; |
| 4770 | int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *); |
| 4771 | unsigned int flag; |
| 4772 | }; |
| 4773 | |
| 4774 | static int img_dd_bs(const char *arg, |
| 4775 | struct DdIo *in, struct DdIo *out, |
| 4776 | struct DdInfo *dd) |
| 4777 | { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4778 | int64_t res; |
| 4779 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4780 | res = cvtnum_full("bs", arg, 1, INT_MAX); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4781 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4782 | if (res < 0) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4783 | return 1; |
| 4784 | } |
| 4785 | in->bsz = out->bsz = res; |
| 4786 | |
| 4787 | return 0; |
| 4788 | } |
| 4789 | |
| 4790 | static int img_dd_count(const char *arg, |
| 4791 | struct DdIo *in, struct DdIo *out, |
| 4792 | struct DdInfo *dd) |
| 4793 | { |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4794 | dd->count = cvtnum("count", arg); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4795 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4796 | if (dd->count < 0) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4797 | return 1; |
| 4798 | } |
| 4799 | |
| 4800 | return 0; |
| 4801 | } |
| 4802 | |
| 4803 | static int img_dd_if(const char *arg, |
| 4804 | struct DdIo *in, struct DdIo *out, |
| 4805 | struct DdInfo *dd) |
| 4806 | { |
| 4807 | in->filename = g_strdup(arg); |
| 4808 | |
| 4809 | return 0; |
| 4810 | } |
| 4811 | |
| 4812 | static int img_dd_of(const char *arg, |
| 4813 | struct DdIo *in, struct DdIo *out, |
| 4814 | struct DdInfo *dd) |
| 4815 | { |
| 4816 | out->filename = g_strdup(arg); |
| 4817 | |
| 4818 | return 0; |
| 4819 | } |
| 4820 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4821 | static int img_dd_skip(const char *arg, |
| 4822 | struct DdIo *in, struct DdIo *out, |
| 4823 | struct DdInfo *dd) |
| 4824 | { |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4825 | in->offset = cvtnum("skip", arg); |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4826 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4827 | if (in->offset < 0) { |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4828 | return 1; |
| 4829 | } |
| 4830 | |
| 4831 | return 0; |
| 4832 | } |
| 4833 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4834 | static int img_dd(int argc, char **argv) |
| 4835 | { |
| 4836 | int ret = 0; |
| 4837 | char *arg = NULL; |
| 4838 | char *tmp; |
| 4839 | BlockDriver *drv = NULL, *proto_drv = NULL; |
| 4840 | BlockBackend *blk1 = NULL, *blk2 = NULL; |
| 4841 | QemuOpts *opts = NULL; |
| 4842 | QemuOptsList *create_opts = NULL; |
| 4843 | Error *local_err = NULL; |
| 4844 | bool image_opts = false; |
| 4845 | int c, i; |
| 4846 | const char *out_fmt = "raw"; |
| 4847 | const char *fmt = NULL; |
| 4848 | int64_t size = 0; |
| 4849 | int64_t block_count = 0, out_pos, in_pos; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4850 | bool force_share = false; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4851 | struct DdInfo dd = { |
| 4852 | .flags = 0, |
| 4853 | .count = 0, |
| 4854 | }; |
| 4855 | struct DdIo in = { |
| 4856 | .bsz = 512, /* Block size is by default 512 bytes */ |
| 4857 | .filename = NULL, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4858 | .buf = NULL, |
| 4859 | .offset = 0 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4860 | }; |
| 4861 | struct DdIo out = { |
| 4862 | .bsz = 512, |
| 4863 | .filename = NULL, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4864 | .buf = NULL, |
| 4865 | .offset = 0 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4866 | }; |
| 4867 | |
| 4868 | const struct DdOpts options[] = { |
| 4869 | { "bs", img_dd_bs, C_BS }, |
| 4870 | { "count", img_dd_count, C_COUNT }, |
| 4871 | { "if", img_dd_if, C_IF }, |
| 4872 | { "of", img_dd_of, C_OF }, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4873 | { "skip", img_dd_skip, C_SKIP }, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4874 | { NULL, NULL, 0 } |
| 4875 | }; |
| 4876 | const struct option long_options[] = { |
| 4877 | { "help", no_argument, 0, 'h'}, |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 4878 | { "object", required_argument, 0, OPTION_OBJECT}, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4879 | { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4880 | { "force-share", no_argument, 0, 'U'}, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4881 | { 0, 0, 0, 0 } |
| 4882 | }; |
| 4883 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4884 | while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4885 | if (c == EOF) { |
| 4886 | break; |
| 4887 | } |
| 4888 | switch (c) { |
| 4889 | case 'O': |
| 4890 | out_fmt = optarg; |
| 4891 | break; |
| 4892 | case 'f': |
| 4893 | fmt = optarg; |
| 4894 | break; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4895 | case ':': |
| 4896 | missing_argument(argv[optind - 1]); |
| 4897 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4898 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4899 | unrecognized_option(argv[optind - 1]); |
| 4900 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4901 | case 'h': |
| 4902 | help(); |
| 4903 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4904 | case 'U': |
| 4905 | force_share = true; |
| 4906 | break; |
Stefan Hajnoczi | 2a24570 | 2017-06-19 16:00:02 +0100 | [diff] [blame] | 4907 | case OPTION_OBJECT: |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 4908 | user_creatable_process_cmdline(optarg); |
Stefan Hajnoczi | 2a24570 | 2017-06-19 16:00:02 +0100 | [diff] [blame] | 4909 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4910 | case OPTION_IMAGE_OPTS: |
| 4911 | image_opts = true; |
| 4912 | break; |
| 4913 | } |
| 4914 | } |
| 4915 | |
| 4916 | for (i = optind; i < argc; i++) { |
| 4917 | int j; |
| 4918 | arg = g_strdup(argv[i]); |
| 4919 | |
| 4920 | tmp = strchr(arg, '='); |
| 4921 | if (tmp == NULL) { |
| 4922 | error_report("unrecognized operand %s", arg); |
| 4923 | ret = -1; |
| 4924 | goto out; |
| 4925 | } |
| 4926 | |
| 4927 | *tmp++ = '\0'; |
| 4928 | |
| 4929 | for (j = 0; options[j].name != NULL; j++) { |
| 4930 | if (!strcmp(arg, options[j].name)) { |
| 4931 | break; |
| 4932 | } |
| 4933 | } |
| 4934 | if (options[j].name == NULL) { |
| 4935 | error_report("unrecognized operand %s", arg); |
| 4936 | ret = -1; |
| 4937 | goto out; |
| 4938 | } |
| 4939 | |
| 4940 | if (options[j].f(tmp, &in, &out, &dd) != 0) { |
| 4941 | ret = -1; |
| 4942 | goto out; |
| 4943 | } |
| 4944 | dd.flags |= options[j].flag; |
| 4945 | g_free(arg); |
| 4946 | arg = NULL; |
| 4947 | } |
| 4948 | |
| 4949 | if (!(dd.flags & C_IF && dd.flags & C_OF)) { |
| 4950 | error_report("Must specify both input and output files"); |
| 4951 | ret = -1; |
| 4952 | goto out; |
| 4953 | } |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 4954 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4955 | blk1 = img_open(image_opts, in.filename, fmt, 0, false, false, |
| 4956 | force_share); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4957 | |
| 4958 | if (!blk1) { |
| 4959 | ret = -1; |
| 4960 | goto out; |
| 4961 | } |
| 4962 | |
| 4963 | drv = bdrv_find_format(out_fmt); |
| 4964 | if (!drv) { |
| 4965 | error_report("Unknown file format"); |
| 4966 | ret = -1; |
| 4967 | goto out; |
| 4968 | } |
| 4969 | proto_drv = bdrv_find_protocol(out.filename, true, &local_err); |
| 4970 | |
| 4971 | if (!proto_drv) { |
| 4972 | error_report_err(local_err); |
| 4973 | ret = -1; |
| 4974 | goto out; |
| 4975 | } |
| 4976 | if (!drv->create_opts) { |
| 4977 | error_report("Format driver '%s' does not support image creation", |
| 4978 | drv->format_name); |
| 4979 | ret = -1; |
| 4980 | goto out; |
| 4981 | } |
| 4982 | if (!proto_drv->create_opts) { |
| 4983 | error_report("Protocol driver '%s' does not support image creation", |
| 4984 | proto_drv->format_name); |
| 4985 | ret = -1; |
| 4986 | goto out; |
| 4987 | } |
| 4988 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 4989 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
| 4990 | |
| 4991 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
| 4992 | |
| 4993 | size = blk_getlength(blk1); |
| 4994 | if (size < 0) { |
| 4995 | error_report("Failed to get size for '%s'", in.filename); |
| 4996 | ret = -1; |
| 4997 | goto out; |
| 4998 | } |
| 4999 | |
| 5000 | if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz && |
| 5001 | dd.count * in.bsz < size) { |
| 5002 | size = dd.count * in.bsz; |
| 5003 | } |
| 5004 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 5005 | /* Overflow means the specified offset is beyond input image's size */ |
| 5006 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || |
| 5007 | size < in.bsz * in.offset)) { |
| 5008 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort); |
| 5009 | } else { |
| 5010 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, |
| 5011 | size - in.bsz * in.offset, &error_abort); |
| 5012 | } |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5013 | |
| 5014 | ret = bdrv_create(drv, out.filename, opts, &local_err); |
| 5015 | if (ret < 0) { |
| 5016 | error_reportf_err(local_err, |
| 5017 | "%s: error while creating output image: ", |
| 5018 | out.filename); |
| 5019 | ret = -1; |
| 5020 | goto out; |
| 5021 | } |
| 5022 | |
Daniel P. Berrange | ea204dd | 2017-05-15 17:47:10 +0100 | [diff] [blame] | 5023 | /* TODO, we can't honour --image-opts for the target, |
| 5024 | * since it needs to be given in a format compatible |
| 5025 | * with the bdrv_create() call above which does not |
| 5026 | * support image-opts style. |
| 5027 | */ |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 5028 | 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] | 5029 | false, false, false); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5030 | |
| 5031 | if (!blk2) { |
| 5032 | ret = -1; |
| 5033 | goto out; |
| 5034 | } |
| 5035 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 5036 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || |
| 5037 | size < in.offset * in.bsz)) { |
| 5038 | /* We give a warning if the skip option is bigger than the input |
| 5039 | * size and create an empty output disk image (i.e. like dd(1)). |
| 5040 | */ |
| 5041 | error_report("%s: cannot skip to specified offset", in.filename); |
| 5042 | in_pos = size; |
| 5043 | } else { |
| 5044 | in_pos = in.offset * in.bsz; |
| 5045 | } |
| 5046 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5047 | in.buf = g_new(uint8_t, in.bsz); |
| 5048 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 5049 | for (out_pos = 0; in_pos < size; block_count++) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5050 | int in_ret, out_ret; |
| 5051 | |
| 5052 | if (in_pos + in.bsz > size) { |
| 5053 | in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos); |
| 5054 | } else { |
| 5055 | in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz); |
| 5056 | } |
| 5057 | if (in_ret < 0) { |
| 5058 | error_report("error while reading from input image file: %s", |
| 5059 | strerror(-in_ret)); |
| 5060 | ret = -1; |
| 5061 | goto out; |
| 5062 | } |
| 5063 | in_pos += in_ret; |
| 5064 | |
| 5065 | out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0); |
| 5066 | |
| 5067 | if (out_ret < 0) { |
| 5068 | error_report("error while writing to output image file: %s", |
| 5069 | strerror(-out_ret)); |
| 5070 | ret = -1; |
| 5071 | goto out; |
| 5072 | } |
| 5073 | out_pos += out_ret; |
| 5074 | } |
| 5075 | |
| 5076 | out: |
| 5077 | g_free(arg); |
| 5078 | qemu_opts_del(opts); |
| 5079 | qemu_opts_free(create_opts); |
| 5080 | blk_unref(blk1); |
| 5081 | blk_unref(blk2); |
| 5082 | g_free(in.filename); |
| 5083 | g_free(out.filename); |
| 5084 | g_free(in.buf); |
| 5085 | g_free(out.buf); |
| 5086 | |
| 5087 | if (ret) { |
| 5088 | return 1; |
| 5089 | } |
| 5090 | return 0; |
| 5091 | } |
| 5092 | |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5093 | static void dump_json_block_measure_info(BlockMeasureInfo *info) |
| 5094 | { |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 5095 | GString *str; |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5096 | QObject *obj; |
| 5097 | Visitor *v = qobject_output_visitor_new(&obj); |
| 5098 | |
| 5099 | visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort); |
| 5100 | visit_complete(v, &obj); |
Markus Armbruster | 6589f45 | 2020-12-11 18:11:35 +0100 | [diff] [blame] | 5101 | str = qobject_to_json_pretty(obj, true); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5102 | assert(str != NULL); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 5103 | printf("%s\n", str->str); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 5104 | qobject_unref(obj); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5105 | visit_free(v); |
Markus Armbruster | eab3a46 | 2020-12-11 18:11:37 +0100 | [diff] [blame] | 5106 | g_string_free(str, true); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5107 | } |
| 5108 | |
| 5109 | static int img_measure(int argc, char **argv) |
| 5110 | { |
| 5111 | static const struct option long_options[] = { |
| 5112 | {"help", no_argument, 0, 'h'}, |
| 5113 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
| 5114 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 5115 | {"output", required_argument, 0, OPTION_OUTPUT}, |
| 5116 | {"size", required_argument, 0, OPTION_SIZE}, |
| 5117 | {"force-share", no_argument, 0, 'U'}, |
| 5118 | {0, 0, 0, 0} |
| 5119 | }; |
| 5120 | OutputFormat output_format = OFORMAT_HUMAN; |
| 5121 | BlockBackend *in_blk = NULL; |
| 5122 | BlockDriver *drv; |
| 5123 | const char *filename = NULL; |
| 5124 | const char *fmt = NULL; |
| 5125 | const char *out_fmt = "raw"; |
| 5126 | char *options = NULL; |
| 5127 | char *snapshot_name = NULL; |
| 5128 | bool force_share = false; |
| 5129 | QemuOpts *opts = NULL; |
| 5130 | QemuOpts *object_opts = NULL; |
| 5131 | QemuOpts *sn_opts = NULL; |
| 5132 | QemuOptsList *create_opts = NULL; |
| 5133 | bool image_opts = false; |
| 5134 | uint64_t img_size = UINT64_MAX; |
| 5135 | BlockMeasureInfo *info = NULL; |
| 5136 | Error *local_err = NULL; |
| 5137 | int ret = 1; |
| 5138 | int c; |
| 5139 | |
| 5140 | while ((c = getopt_long(argc, argv, "hf:O:o:l:U", |
| 5141 | long_options, NULL)) != -1) { |
| 5142 | switch (c) { |
| 5143 | case '?': |
| 5144 | case 'h': |
| 5145 | help(); |
| 5146 | break; |
| 5147 | case 'f': |
| 5148 | fmt = optarg; |
| 5149 | break; |
| 5150 | case 'O': |
| 5151 | out_fmt = optarg; |
| 5152 | break; |
| 5153 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 5154 | if (accumulate_options(&options, optarg) < 0) { |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5155 | goto out; |
| 5156 | } |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5157 | break; |
| 5158 | case 'l': |
| 5159 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { |
| 5160 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
| 5161 | optarg, false); |
| 5162 | if (!sn_opts) { |
| 5163 | error_report("Failed in parsing snapshot param '%s'", |
| 5164 | optarg); |
| 5165 | goto out; |
| 5166 | } |
| 5167 | } else { |
| 5168 | snapshot_name = optarg; |
| 5169 | } |
| 5170 | break; |
| 5171 | case 'U': |
| 5172 | force_share = true; |
| 5173 | break; |
| 5174 | case OPTION_OBJECT: |
Kevin Wolf | 99b1e64 | 2021-02-17 12:56:45 +0100 | [diff] [blame] | 5175 | user_creatable_process_cmdline(optarg); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5176 | break; |
| 5177 | case OPTION_IMAGE_OPTS: |
| 5178 | image_opts = true; |
| 5179 | break; |
| 5180 | case OPTION_OUTPUT: |
| 5181 | if (!strcmp(optarg, "json")) { |
| 5182 | output_format = OFORMAT_JSON; |
| 5183 | } else if (!strcmp(optarg, "human")) { |
| 5184 | output_format = OFORMAT_HUMAN; |
| 5185 | } else { |
| 5186 | error_report("--output must be used with human or json " |
| 5187 | "as argument."); |
| 5188 | goto out; |
| 5189 | } |
| 5190 | break; |
| 5191 | case OPTION_SIZE: |
| 5192 | { |
| 5193 | int64_t sval; |
| 5194 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 5195 | sval = cvtnum("image size", optarg); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5196 | if (sval < 0) { |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5197 | goto out; |
| 5198 | } |
| 5199 | img_size = (uint64_t)sval; |
| 5200 | } |
| 5201 | break; |
| 5202 | } |
| 5203 | } |
| 5204 | |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5205 | if (argc - optind > 1) { |
| 5206 | error_report("At most one filename argument is allowed."); |
| 5207 | goto out; |
| 5208 | } else if (argc - optind == 1) { |
| 5209 | filename = argv[optind]; |
| 5210 | } |
| 5211 | |
Stefan Hajnoczi | c3673dc | 2020-02-21 11:25:21 +0000 | [diff] [blame] | 5212 | if (!filename && (image_opts || fmt || snapshot_name || sn_opts)) { |
| 5213 | error_report("--image-opts, -f, and -l require a filename argument."); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5214 | goto out; |
| 5215 | } |
| 5216 | if (filename && img_size != UINT64_MAX) { |
| 5217 | error_report("--size N cannot be used together with a filename."); |
| 5218 | goto out; |
| 5219 | } |
| 5220 | if (!filename && img_size == UINT64_MAX) { |
| 5221 | error_report("Either --size N or one filename must be specified."); |
| 5222 | goto out; |
| 5223 | } |
| 5224 | |
| 5225 | if (filename) { |
| 5226 | in_blk = img_open(image_opts, filename, fmt, 0, |
| 5227 | false, false, force_share); |
| 5228 | if (!in_blk) { |
| 5229 | goto out; |
| 5230 | } |
| 5231 | |
| 5232 | if (sn_opts) { |
| 5233 | bdrv_snapshot_load_tmp(blk_bs(in_blk), |
| 5234 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), |
| 5235 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), |
| 5236 | &local_err); |
| 5237 | } else if (snapshot_name != NULL) { |
| 5238 | bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk), |
| 5239 | snapshot_name, &local_err); |
| 5240 | } |
| 5241 | if (local_err) { |
| 5242 | error_reportf_err(local_err, "Failed to load snapshot: "); |
| 5243 | goto out; |
| 5244 | } |
| 5245 | } |
| 5246 | |
| 5247 | drv = bdrv_find_format(out_fmt); |
| 5248 | if (!drv) { |
| 5249 | error_report("Unknown file format '%s'", out_fmt); |
| 5250 | goto out; |
| 5251 | } |
| 5252 | if (!drv->create_opts) { |
| 5253 | error_report("Format driver '%s' does not support image creation", |
| 5254 | drv->format_name); |
| 5255 | goto out; |
| 5256 | } |
| 5257 | |
| 5258 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 5259 | create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts); |
| 5260 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
| 5261 | if (options) { |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 5262 | if (!qemu_opts_do_parse(opts, options, NULL, &local_err)) { |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5263 | error_report_err(local_err); |
| 5264 | error_report("Invalid options for file format '%s'", out_fmt); |
| 5265 | goto out; |
| 5266 | } |
| 5267 | } |
| 5268 | if (img_size != UINT64_MAX) { |
| 5269 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort); |
| 5270 | } |
| 5271 | |
| 5272 | info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err); |
| 5273 | if (local_err) { |
| 5274 | error_report_err(local_err); |
| 5275 | goto out; |
| 5276 | } |
| 5277 | |
| 5278 | if (output_format == OFORMAT_HUMAN) { |
| 5279 | printf("required size: %" PRIu64 "\n", info->required); |
| 5280 | printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated); |
Eric Blake | 5d72c68 | 2020-05-21 14:21:34 -0500 | [diff] [blame] | 5281 | if (info->has_bitmaps) { |
| 5282 | printf("bitmaps size: %" PRIu64 "\n", info->bitmaps); |
| 5283 | } |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5284 | } else { |
| 5285 | dump_json_block_measure_info(info); |
| 5286 | } |
| 5287 | |
| 5288 | ret = 0; |
| 5289 | |
| 5290 | out: |
| 5291 | qapi_free_BlockMeasureInfo(info); |
| 5292 | qemu_opts_del(object_opts); |
| 5293 | qemu_opts_del(opts); |
| 5294 | qemu_opts_del(sn_opts); |
| 5295 | qemu_opts_free(create_opts); |
| 5296 | g_free(options); |
| 5297 | blk_unref(in_blk); |
| 5298 | return ret; |
| 5299 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 5300 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 5301 | static const img_cmd_t img_cmds[] = { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5302 | #define DEF(option, callback, arg_string) \ |
| 5303 | { option, callback }, |
| 5304 | #include "qemu-img-cmds.h" |
| 5305 | #undef DEF |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5306 | { NULL, NULL, }, |
| 5307 | }; |
| 5308 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5309 | int main(int argc, char **argv) |
| 5310 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 5311 | const img_cmd_t *cmd; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5312 | const char *cmdname; |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 5313 | Error *local_error = NULL; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5314 | int c; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5315 | static const struct option long_options[] = { |
| 5316 | {"help", no_argument, 0, 'h'}, |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5317 | {"version", no_argument, 0, 'V'}, |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5318 | {"trace", required_argument, NULL, 'T'}, |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5319 | {0, 0, 0, 0} |
| 5320 | }; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5321 | |
MORITA Kazutaka | 526eda1 | 2013-07-23 17:30:11 +0900 | [diff] [blame] | 5322 | #ifdef CONFIG_POSIX |
| 5323 | signal(SIGPIPE, SIG_IGN); |
| 5324 | #endif |
| 5325 | |
Daniel P. Berrangé | 98c5d2e | 2020-08-25 11:38:48 +0100 | [diff] [blame] | 5326 | socket_init(); |
Christophe Fergeau | f5852ef | 2019-01-31 17:46:14 +0100 | [diff] [blame] | 5327 | error_init(argv[0]); |
Daniel P. Berrange | fe4db84 | 2016-10-04 14:35:52 +0100 | [diff] [blame] | 5328 | module_call_init(MODULE_INIT_TRACE); |
Fam Zheng | 10f5bff | 2014-02-10 14:48:51 +0800 | [diff] [blame] | 5329 | qemu_init_exec_dir(argv[0]); |
Kevin Wolf | 53f76e5 | 2010-12-16 15:10:32 +0100 | [diff] [blame] | 5330 | |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 5331 | if (qemu_init_main_loop(&local_error)) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 5332 | error_report_err(local_error); |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 5333 | exit(EXIT_FAILURE); |
| 5334 | } |
| 5335 | |
Eduardo Habkost | e8f2d27 | 2016-05-12 11:10:04 -0300 | [diff] [blame] | 5336 | qcrypto_init(&error_fatal); |
Daniel P. Berrange | c229708 | 2016-04-06 12:12:06 +0100 | [diff] [blame] | 5337 | |
Daniel P. Berrange | 064097d | 2016-02-10 18:41:01 +0000 | [diff] [blame] | 5338 | module_call_init(MODULE_INIT_QOM); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5339 | bdrv_init(); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 5340 | if (argc < 2) { |
| 5341 | error_exit("Not enough arguments"); |
| 5342 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5343 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 5344 | qemu_add_opts(&qemu_source_opts); |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5345 | qemu_add_opts(&qemu_trace_opts); |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 5346 | |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 5347 | while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) { |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5348 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 5349 | case ':': |
| 5350 | missing_argument(argv[optind - 1]); |
| 5351 | return 0; |
Stefan Hajnoczi | 4581c16 | 2017-03-17 18:45:39 +0800 | [diff] [blame] | 5352 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 5353 | unrecognized_option(argv[optind - 1]); |
| 5354 | return 0; |
| 5355 | case 'h': |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5356 | help(); |
| 5357 | return 0; |
| 5358 | case 'V': |
| 5359 | printf(QEMU_IMG_VERSION); |
| 5360 | return 0; |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5361 | case 'T': |
Paolo Bonzini | 92eecff | 2020-11-02 06:58:41 -0500 | [diff] [blame] | 5362 | trace_opt_parse(optarg); |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5363 | break; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5364 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5365 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5366 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5367 | cmdname = argv[optind]; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5368 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5369 | /* reset getopt_long scanning */ |
| 5370 | argc -= optind; |
| 5371 | if (argc < 1) { |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 5372 | return 0; |
| 5373 | } |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5374 | argv += optind; |
Richard W.M. Jones | d339d76 | 2019-01-18 10:11:14 +0000 | [diff] [blame] | 5375 | qemu_reset_optind(); |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5376 | |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5377 | if (!trace_init_backends()) { |
| 5378 | exit(1); |
| 5379 | } |
Paolo Bonzini | 92eecff | 2020-11-02 06:58:41 -0500 | [diff] [blame] | 5380 | trace_init_file(); |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5381 | qemu_set_log(LOG_TRACE); |
| 5382 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5383 | /* find the command */ |
| 5384 | for (cmd = img_cmds; cmd->name != NULL; cmd++) { |
| 5385 | if (!strcmp(cmdname, cmd->name)) { |
| 5386 | return cmd->handler(argc, argv); |
| 5387 | } |
| 5388 | } |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5389 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5390 | /* not found */ |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 5391 | error_exit("Command not found: %s", cmdname); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5392 | } |