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" |
Markus Armbruster | fc81fa1 | 2018-02-01 12:18:40 +0100 | [diff] [blame] | 36 | #include "qapi/qmp/qstring.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 37 | #include "qemu/cutils.h" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 38 | #include "qemu/config-file.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 39 | #include "qemu/option.h" |
| 40 | #include "qemu/error-report.h" |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 41 | #include "qemu/log.h" |
Markus Armbruster | db72581 | 2019-08-12 07:23:50 +0200 | [diff] [blame] | 42 | #include "qemu/main-loop.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 43 | #include "qemu/module.h" |
Daniel P. Berrangé | 98c5d2e | 2020-08-25 11:38:48 +0100 | [diff] [blame] | 44 | #include "qemu/sockets.h" |
Stefano Garzarella | 97ede57 | 2019-05-08 12:43:24 +0200 | [diff] [blame] | 45 | #include "qemu/units.h" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 46 | #include "qom/object_interfaces.h" |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 47 | #include "sysemu/block-backend.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 48 | #include "block/block_int.h" |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 49 | #include "block/blockjob.h" |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 50 | #include "block/qapi.h" |
Daniel P. Berrange | c229708 | 2016-04-06 12:12:06 +0100 | [diff] [blame] | 51 | #include "crypto/init.h" |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 52 | #include "trace/control.h" |
bellard | e844533 | 2006-06-14 15:32:10 +0000 | [diff] [blame] | 53 | |
Thomas Huth | 7e563bf | 2018-02-15 12:06:47 +0100 | [diff] [blame] | 54 | #define QEMU_IMG_VERSION "qemu-img version " QEMU_FULL_VERSION \ |
Thomas Huth | 0781dd6 | 2016-10-05 11:54:44 +0200 | [diff] [blame] | 55 | "\n" QEMU_COPYRIGHT "\n" |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 56 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 57 | typedef struct img_cmd_t { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 58 | const char *name; |
| 59 | int (*handler)(int argc, char **argv); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 60 | } img_cmd_t; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 61 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 62 | enum { |
| 63 | OPTION_OUTPUT = 256, |
| 64 | OPTION_BACKING_CHAIN = 257, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 65 | OPTION_OBJECT = 258, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 66 | OPTION_IMAGE_OPTS = 259, |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 67 | OPTION_PATTERN = 260, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 68 | OPTION_FLUSH_INTERVAL = 261, |
| 69 | OPTION_NO_DRAIN = 262, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 70 | OPTION_TARGET_IMAGE_OPTS = 263, |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 71 | OPTION_SIZE = 264, |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 72 | OPTION_PREALLOCATION = 265, |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 73 | OPTION_SHRINK = 266, |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 74 | OPTION_SALVAGE = 267, |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 75 | OPTION_TARGET_IS_ZERO = 268, |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 76 | OPTION_ADD = 269, |
| 77 | OPTION_REMOVE = 270, |
| 78 | OPTION_CLEAR = 271, |
| 79 | OPTION_ENABLE = 272, |
| 80 | OPTION_DISABLE = 273, |
| 81 | OPTION_MERGE = 274, |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 82 | OPTION_BITMAPS = 275, |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 83 | OPTION_FORCE = 276, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | typedef enum OutputFormat { |
| 87 | OFORMAT_JSON, |
| 88 | OFORMAT_HUMAN, |
| 89 | } OutputFormat; |
| 90 | |
Kevin Wolf | e699614 | 2016-03-15 13:03:11 +0100 | [diff] [blame] | 91 | /* 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] | 92 | #define BDRV_DEFAULT_CACHE "writeback" |
aurel32 | 137519c | 2008-11-30 19:12:49 +0000 | [diff] [blame] | 93 | |
Stefan Hajnoczi | 00c6d40 | 2014-08-27 12:08:56 +0100 | [diff] [blame] | 94 | static void format_print(void *opaque, const char *name) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 95 | { |
Stefan Hajnoczi | 00c6d40 | 2014-08-27 12:08:56 +0100 | [diff] [blame] | 96 | printf(" %s", name); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 99 | static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...) |
| 100 | { |
| 101 | va_list ap; |
| 102 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 103 | va_start(ap, fmt); |
Markus Armbruster | e9e1d92 | 2019-04-17 21:06:27 +0200 | [diff] [blame] | 104 | error_vreport(fmt, ap); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 105 | va_end(ap); |
| 106 | |
Markus Armbruster | e9e1d92 | 2019-04-17 21:06:27 +0200 | [diff] [blame] | 107 | error_printf("Try 'qemu-img --help' for more information\n"); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 108 | exit(EXIT_FAILURE); |
| 109 | } |
| 110 | |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 111 | static void QEMU_NORETURN missing_argument(const char *option) |
| 112 | { |
| 113 | error_exit("missing argument for option '%s'", option); |
| 114 | } |
| 115 | |
| 116 | static void QEMU_NORETURN unrecognized_option(const char *option) |
| 117 | { |
| 118 | error_exit("unrecognized option '%s'", option); |
| 119 | } |
| 120 | |
Eric Blake | 0562adf | 2020-05-12 20:16:41 -0500 | [diff] [blame] | 121 | /* Please keep in synch with docs/tools/qemu-img.rst */ |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 122 | static void QEMU_NORETURN help(void) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 123 | { |
Paolo Bonzini | e00291c | 2010-02-04 16:49:56 +0100 | [diff] [blame] | 124 | const char *help_msg = |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 125 | QEMU_IMG_VERSION |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 126 | "usage: qemu-img [standard options] command [command options]\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 127 | "QEMU disk image utility\n" |
| 128 | "\n" |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 129 | " '-h', '--help' display this help and exit\n" |
| 130 | " '-V', '--version' output version information and exit\n" |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 131 | " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n" |
| 132 | " specify tracing options\n" |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 133 | "\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 134 | "Command syntax:\n" |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 135 | #define DEF(option, callback, arg_string) \ |
| 136 | " " arg_string "\n" |
| 137 | #include "qemu-img-cmds.h" |
| 138 | #undef DEF |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 139 | "\n" |
| 140 | "Command parameters:\n" |
| 141 | " 'filename' is a disk image filename\n" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 142 | " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n" |
| 143 | " manual page for a description of the object properties. The most common\n" |
| 144 | " object type is a 'secret', which is used to supply passwords and/or\n" |
| 145 | " encryption keys.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 146 | " '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] | 147 | " '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] | 148 | " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n" |
| 149 | " 'directsync' and 'unsafe' (default for convert)\n" |
Stefan Hajnoczi | bb87fdf | 2014-09-02 11:01:02 +0100 | [diff] [blame] | 150 | " 'src_cache' is the cache mode used to read input disk images, the valid\n" |
| 151 | " options are the same as for the 'cache' option\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 152 | " 'size' is the disk image size in bytes. Optional suffixes\n" |
Kevin Wolf | 5e00984 | 2013-06-05 14:19:27 +0200 | [diff] [blame] | 153 | " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n" |
| 154 | " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n" |
| 155 | " supported. 'b' is ignored.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 156 | " 'output_filename' is the destination disk image filename\n" |
| 157 | " 'output_fmt' is the destination format\n" |
| 158 | " 'options' is a comma separated list of format specific options in a\n" |
| 159 | " name=value format. Use -o ? for an overview of the options supported by the\n" |
| 160 | " used format\n" |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 161 | " 'snapshot_param' is param used for internal snapshot, format\n" |
| 162 | " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n" |
| 163 | " '[ID_OR_NAME]'\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 164 | " '-c' indicates that target image must be compressed (qcow format only)\n" |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 165 | " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n" |
| 166 | " new backing file match exactly. The image doesn't need a working\n" |
| 167 | " backing file before rebasing in this case (useful for renaming the\n" |
| 168 | " backing file). For image creation, allow creating without attempting\n" |
| 169 | " to open the backing file.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 170 | " '-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] | 171 | " '-p' show progress of command (only certain commands)\n" |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 172 | " '-q' use Quiet mode - do not print any output (except errors)\n" |
Peter Lieven | 11b6699 | 2013-10-24 12:07:05 +0200 | [diff] [blame] | 173 | " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n" |
| 174 | " contain only zeros for qemu-img to create a sparse image during\n" |
| 175 | " conversion. If the number of bytes is 0, the source will not be scanned for\n" |
| 176 | " unallocated or zero sectors, and the destination image will always be\n" |
| 177 | " fully allocated\n" |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 178 | " '--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] | 179 | " '-n' skips the target volume creation (useful if the volume is created\n" |
| 180 | " prior to running qemu-img)\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 181 | "\n" |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 182 | "Parameters to bitmap subcommand:\n" |
| 183 | " 'bitmap' is the name of the bitmap to manipulate, through one or more\n" |
| 184 | " actions from '--add', '--remove', '--clear', '--enable', '--disable',\n" |
| 185 | " or '--merge source'\n" |
| 186 | " '-g granularity' sets the granularity for '--add' actions\n" |
| 187 | " '-b source' and '-F src_fmt' tell '--merge' actions to find the source\n" |
| 188 | " bitmaps from an alternative file\n" |
| 189 | "\n" |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 190 | "Parameters to check subcommand:\n" |
| 191 | " '-r' tries to repair any inconsistencies that are found during the check.\n" |
| 192 | " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n" |
| 193 | " 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] | 194 | " hiding corruption that has already occurred.\n" |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 195 | "\n" |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 196 | "Parameters to convert subcommand:\n" |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 197 | " '--bitmaps' copies all top-level persistent bitmaps to destination\n" |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 198 | " '-m' specifies how many coroutines work in parallel during the convert\n" |
| 199 | " process (defaults to 8)\n" |
| 200 | " '-W' allow to write to the target out of order rather than sequential\n" |
| 201 | "\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 202 | "Parameters to snapshot subcommand:\n" |
| 203 | " 'snapshot' is the name of the snapshot to create, apply or delete\n" |
| 204 | " '-a' applies a snapshot (revert disk to saved state)\n" |
| 205 | " '-c' creates a snapshot\n" |
| 206 | " '-d' deletes a snapshot\n" |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 207 | " '-l' lists all snapshots in the given image\n" |
| 208 | "\n" |
| 209 | "Parameters to compare subcommand:\n" |
| 210 | " '-f' first image format\n" |
| 211 | " '-F' second image format\n" |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 212 | " '-s' run in Strict mode - fail on different image size or sector allocation\n" |
| 213 | "\n" |
| 214 | "Parameters to dd subcommand:\n" |
| 215 | " 'bs=BYTES' read and write up to BYTES bytes at a time " |
| 216 | "(default: 512)\n" |
| 217 | " 'count=N' copy only N input blocks\n" |
| 218 | " 'if=FILE' read from FILE\n" |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 219 | " 'of=FILE' write to FILE\n" |
| 220 | " '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] | 221 | |
| 222 | printf("%s\nSupported formats:", help_msg); |
Andrey Shinkevich | 9ac404c | 2019-03-07 16:33:58 +0300 | [diff] [blame] | 223 | bdrv_iterate_format(format_print, NULL, false); |
Eric Blake | f5048cb | 2017-08-03 11:33:53 -0500 | [diff] [blame] | 224 | printf("\n\n" QEMU_HELP_BOTTOM "\n"); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 225 | exit(EXIT_SUCCESS); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 228 | static QemuOptsList qemu_object_opts = { |
| 229 | .name = "object", |
| 230 | .implied_opt_name = "qom-type", |
| 231 | .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head), |
| 232 | .desc = { |
| 233 | { } |
| 234 | }, |
| 235 | }; |
| 236 | |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 237 | static bool qemu_img_object_print_help(const char *type, QemuOpts *opts) |
| 238 | { |
| 239 | if (user_creatable_print_help(type, opts)) { |
| 240 | exit(0); |
| 241 | } |
| 242 | return true; |
| 243 | } |
| 244 | |
Markus Armbruster | 80c710c | 2020-04-15 09:49:26 +0200 | [diff] [blame] | 245 | /* |
| 246 | * Is @optarg safe for accumulate_options()? |
| 247 | * It is when multiple of them can be joined together separated by ','. |
| 248 | * To make that work, @optarg must not start with ',' (or else a |
| 249 | * separating ',' preceding it gets escaped), and it must not end with |
| 250 | * an odd number of ',' (or else a separating ',' following it gets |
Markus Armbruster | f62514b | 2020-04-15 09:49:27 +0200 | [diff] [blame] | 251 | * escaped), or be empty (or else a separating ',' preceding it can |
| 252 | * escape a separating ',' following it). |
| 253 | * |
Markus Armbruster | 80c710c | 2020-04-15 09:49:26 +0200 | [diff] [blame] | 254 | */ |
| 255 | static bool is_valid_option_list(const char *optarg) |
| 256 | { |
| 257 | size_t len = strlen(optarg); |
| 258 | size_t i; |
| 259 | |
Markus Armbruster | f62514b | 2020-04-15 09:49:27 +0200 | [diff] [blame] | 260 | if (!optarg[0] || optarg[0] == ',') { |
Markus Armbruster | 80c710c | 2020-04-15 09:49:26 +0200 | [diff] [blame] | 261 | return false; |
| 262 | } |
| 263 | |
| 264 | for (i = len; i > 0 && optarg[i - 1] == ','; i--) { |
| 265 | } |
| 266 | if ((len - i) % 2) { |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | return true; |
| 271 | } |
| 272 | |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 273 | static int accumulate_options(char **options, char *optarg) |
| 274 | { |
| 275 | char *new_options; |
| 276 | |
| 277 | if (!is_valid_option_list(optarg)) { |
| 278 | error_report("Invalid option list: %s", optarg); |
| 279 | return -1; |
| 280 | } |
| 281 | |
| 282 | if (!*options) { |
| 283 | *options = g_strdup(optarg); |
| 284 | } else { |
| 285 | new_options = g_strdup_printf("%s,%s", *options, optarg); |
| 286 | g_free(*options); |
| 287 | *options = new_options; |
| 288 | } |
| 289 | return 0; |
| 290 | } |
| 291 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 292 | static QemuOptsList qemu_source_opts = { |
| 293 | .name = "source", |
| 294 | .implied_opt_name = "file", |
| 295 | .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head), |
| 296 | .desc = { |
| 297 | { } |
| 298 | }, |
| 299 | }; |
| 300 | |
Stefan Weil | 7c30f65 | 2013-06-16 17:01:05 +0200 | [diff] [blame] | 301 | 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] | 302 | { |
| 303 | int ret = 0; |
| 304 | if (!quiet) { |
| 305 | va_list args; |
| 306 | va_start(args, fmt); |
| 307 | ret = vprintf(fmt, args); |
| 308 | va_end(args); |
| 309 | } |
| 310 | return ret; |
| 311 | } |
| 312 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 313 | |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 314 | static int print_block_option_help(const char *filename, const char *fmt) |
| 315 | { |
| 316 | BlockDriver *drv, *proto_drv; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 317 | QemuOptsList *create_opts = NULL; |
Max Reitz | b65a5e1 | 2015-02-05 13:58:12 -0500 | [diff] [blame] | 318 | Error *local_err = NULL; |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 319 | |
| 320 | /* Find driver and parse its options */ |
| 321 | drv = bdrv_find_format(fmt); |
| 322 | if (!drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 323 | error_report("Unknown file format '%s'", fmt); |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 324 | return 1; |
| 325 | } |
| 326 | |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 327 | if (!drv->create_opts) { |
| 328 | error_report("Format driver '%s' does not support image creation", fmt); |
| 329 | return 1; |
| 330 | } |
| 331 | |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 332 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 333 | if (filename) { |
Max Reitz | b65a5e1 | 2015-02-05 13:58:12 -0500 | [diff] [blame] | 334 | proto_drv = bdrv_find_protocol(filename, true, &local_err); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 335 | if (!proto_drv) { |
Markus Armbruster | 2867ce4 | 2015-03-12 16:08:02 +0100 | [diff] [blame] | 336 | error_report_err(local_err); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 337 | qemu_opts_free(create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 338 | return 1; |
| 339 | } |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 340 | if (!proto_drv->create_opts) { |
Max Reitz | f099887 | 2018-11-19 11:19:20 +0100 | [diff] [blame] | 341 | error_report("Protocol driver '%s' does not support image creation", |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 342 | proto_drv->format_name); |
Max Reitz | 3ecd5a4 | 2018-11-19 11:19:21 +0100 | [diff] [blame] | 343 | qemu_opts_free(create_opts); |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 344 | return 1; |
| 345 | } |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 346 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 347 | } |
| 348 | |
Max Reitz | f4619af | 2019-04-13 17:20:37 +0200 | [diff] [blame] | 349 | if (filename) { |
| 350 | printf("Supported options:\n"); |
| 351 | } else { |
| 352 | printf("Supported %s options:\n", fmt); |
| 353 | } |
Max Reitz | 6389871 | 2018-10-19 18:49:25 +0200 | [diff] [blame] | 354 | qemu_opts_print_help(create_opts, false); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 355 | qemu_opts_free(create_opts); |
Max Reitz | f4619af | 2019-04-13 17:20:37 +0200 | [diff] [blame] | 356 | |
| 357 | if (!filename) { |
| 358 | printf("\n" |
| 359 | "The protocol level may support further options.\n" |
| 360 | "Specify the target filename to include those options.\n"); |
| 361 | } |
| 362 | |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 363 | return 0; |
| 364 | } |
| 365 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 366 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 367 | static BlockBackend *img_open_opts(const char *optstr, |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 368 | QemuOpts *opts, int flags, bool writethrough, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 369 | bool quiet, bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 370 | { |
| 371 | QDict *options; |
| 372 | Error *local_err = NULL; |
| 373 | BlockBackend *blk; |
| 374 | options = qemu_opts_to_qdict(opts, NULL); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 375 | if (force_share) { |
| 376 | if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE) |
Max Reitz | 4615f87 | 2018-05-02 22:20:50 +0200 | [diff] [blame] | 377 | && strcmp(qdict_get_str(options, BDRV_OPT_FORCE_SHARE), "on")) { |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 378 | error_report("--force-share/-U conflicts with image options"); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 379 | qobject_unref(options); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 380 | return NULL; |
| 381 | } |
Max Reitz | 4615f87 | 2018-05-02 22:20:50 +0200 | [diff] [blame] | 382 | qdict_put_str(options, BDRV_OPT_FORCE_SHARE, "on"); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 383 | } |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 384 | blk = blk_new_open(NULL, NULL, options, flags, &local_err); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 385 | if (!blk) { |
Daniel P. Berrange | 143605a | 2016-04-06 10:16:18 +0100 | [diff] [blame] | 386 | error_reportf_err(local_err, "Could not open '%s': ", optstr); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 387 | return NULL; |
| 388 | } |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 389 | blk_set_enable_write_cache(blk, !writethrough); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 390 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 391 | return blk; |
| 392 | } |
| 393 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 394 | static BlockBackend *img_open_file(const char *filename, |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 395 | QDict *options, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 396 | const char *fmt, int flags, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 397 | bool writethrough, bool quiet, |
| 398 | bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 399 | { |
| 400 | BlockBackend *blk; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 401 | Error *local_err = NULL; |
Kevin Wolf | ad71713 | 2010-12-16 15:37:41 +0100 | [diff] [blame] | 402 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 403 | if (!options) { |
| 404 | options = qdict_new(); |
| 405 | } |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 406 | if (fmt) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 407 | qdict_put_str(options, "driver", fmt); |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 408 | } |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 409 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 410 | if (force_share) { |
Eric Blake | 579cf1d | 2017-05-15 14:54:39 -0500 | [diff] [blame] | 411 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 412 | } |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 413 | blk = blk_new_open(filename, NULL, options, flags, &local_err); |
Max Reitz | 5bd3132 | 2015-02-05 13:58:16 -0500 | [diff] [blame] | 414 | if (!blk) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 415 | error_reportf_err(local_err, "Could not open '%s': ", filename); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 416 | return NULL; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 417 | } |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 418 | blk_set_enable_write_cache(blk, !writethrough); |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 419 | |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 420 | return blk; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 423 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 424 | static int img_add_key_secrets(void *opaque, |
| 425 | const char *name, const char *value, |
| 426 | Error **errp) |
| 427 | { |
| 428 | QDict *options = opaque; |
| 429 | |
| 430 | if (g_str_has_suffix(name, "key-secret")) { |
Eric Blake | 187f47e | 2017-06-24 12:10:07 -0600 | [diff] [blame] | 431 | qdict_put_str(options, name, value); |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 437 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 438 | static BlockBackend *img_open(bool image_opts, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 439 | const char *filename, |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 440 | const char *fmt, int flags, bool writethrough, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 441 | bool quiet, bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 442 | { |
| 443 | BlockBackend *blk; |
| 444 | if (image_opts) { |
| 445 | QemuOpts *opts; |
| 446 | if (fmt) { |
| 447 | error_report("--image-opts and --format are mutually exclusive"); |
| 448 | return NULL; |
| 449 | } |
| 450 | opts = qemu_opts_parse_noisily(qemu_find_opts("source"), |
| 451 | filename, true); |
| 452 | if (!opts) { |
| 453 | return NULL; |
| 454 | } |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 455 | blk = img_open_opts(filename, opts, flags, writethrough, quiet, |
| 456 | force_share); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 457 | } else { |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 458 | blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 459 | force_share); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 460 | } |
| 461 | return blk; |
| 462 | } |
| 463 | |
| 464 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 465 | static int add_old_style_options(const char *fmt, QemuOpts *opts, |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 466 | const char *base_filename, |
| 467 | const char *base_fmt) |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 468 | { |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 469 | if (base_filename) { |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 470 | if (!qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, |
Markus Armbruster | 9e194e0 | 2020-07-07 18:06:11 +0200 | [diff] [blame] | 471 | NULL)) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 472 | error_report("Backing file not supported for file format '%s'", |
| 473 | fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 474 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | if (base_fmt) { |
Markus Armbruster | 9e194e0 | 2020-07-07 18:06:11 +0200 | [diff] [blame] | 478 | if (!qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, NULL)) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 479 | error_report("Backing file format not supported for file " |
| 480 | "format '%s'", fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 481 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 482 | } |
| 483 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 484 | return 0; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 485 | } |
| 486 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 487 | static int64_t cvtnum_full(const char *name, const char *value, int64_t min, |
| 488 | int64_t max) |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 489 | { |
Markus Armbruster | f17fd4f | 2017-02-21 21:14:06 +0100 | [diff] [blame] | 490 | int err; |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 491 | uint64_t res; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 492 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 493 | err = qemu_strtosz(value, NULL, &res); |
| 494 | if (err < 0 && err != -ERANGE) { |
| 495 | error_report("Invalid %s specified. You may use " |
| 496 | "k, M, G, T, P or E suffixes for", name); |
| 497 | error_report("kilobytes, megabytes, gigabytes, terabytes, " |
| 498 | "petabytes and exabytes."); |
Markus Armbruster | f17fd4f | 2017-02-21 21:14:06 +0100 | [diff] [blame] | 499 | return err; |
| 500 | } |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 501 | if (err == -ERANGE || res > max || res < min) { |
| 502 | error_report("Invalid %s specified. Must be between %" PRId64 |
| 503 | " and %" PRId64 ".", name, min, max); |
Markus Armbruster | f46bfdb | 2017-02-21 21:14:07 +0100 | [diff] [blame] | 504 | return -ERANGE; |
| 505 | } |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 506 | return res; |
| 507 | } |
| 508 | |
| 509 | static int64_t cvtnum(const char *name, const char *value) |
| 510 | { |
| 511 | return cvtnum_full(name, value, 0, INT64_MAX); |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 512 | } |
| 513 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 514 | static int img_create(int argc, char **argv) |
| 515 | { |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 516 | int c; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 517 | uint64_t img_size = -1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 518 | const char *fmt = "raw"; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 519 | const char *base_fmt = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 520 | const char *filename; |
| 521 | const char *base_filename = NULL; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 522 | char *options = NULL; |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 523 | Error *local_err = NULL; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 524 | bool quiet = false; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 525 | int flags = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 526 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 527 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 528 | static const struct option long_options[] = { |
| 529 | {"help", no_argument, 0, 'h'}, |
| 530 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 531 | {0, 0, 0, 0} |
| 532 | }; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 533 | c = getopt_long(argc, argv, ":F:b:f:ho:qu", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 534 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 535 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 536 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 537 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 538 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 539 | case ':': |
| 540 | missing_argument(argv[optind - 1]); |
| 541 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 542 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 543 | unrecognized_option(argv[optind - 1]); |
| 544 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 545 | case 'h': |
| 546 | help(); |
| 547 | break; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 548 | case 'F': |
| 549 | base_fmt = optarg; |
| 550 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 551 | case 'b': |
| 552 | base_filename = optarg; |
| 553 | break; |
| 554 | case 'f': |
| 555 | fmt = optarg; |
| 556 | break; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 557 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 558 | if (accumulate_options(&options, optarg) < 0) { |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 559 | goto fail; |
| 560 | } |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 561 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 562 | case 'q': |
| 563 | quiet = true; |
| 564 | break; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 565 | case 'u': |
| 566 | flags |= BDRV_O_NO_BACKING; |
| 567 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 568 | case OPTION_OBJECT: { |
| 569 | QemuOpts *opts; |
| 570 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 571 | optarg, true); |
| 572 | if (!opts) { |
| 573 | goto fail; |
| 574 | } |
| 575 | } break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 576 | } |
| 577 | } |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 578 | |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 579 | /* Get the filename */ |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 580 | filename = (optind < argc) ? argv[optind] : NULL; |
| 581 | if (options && has_help_option(options)) { |
| 582 | g_free(options); |
| 583 | return print_block_option_help(filename, fmt); |
| 584 | } |
| 585 | |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 586 | if (optind >= argc) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 587 | error_exit("Expecting image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 588 | } |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 589 | optind++; |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 590 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 591 | if (qemu_opts_foreach(&qemu_object_opts, |
| 592 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 593 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 594 | goto fail; |
| 595 | } |
| 596 | |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 597 | /* Get image size, if specified */ |
| 598 | if (optind < argc) { |
Jes Sorensen | 70b4f4b | 2011-01-05 11:41:02 +0100 | [diff] [blame] | 599 | int64_t sval; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 600 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 601 | sval = cvtnum("image size", argv[optind++]); |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 602 | if (sval < 0) { |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 603 | goto fail; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 604 | } |
| 605 | img_size = (uint64_t)sval; |
| 606 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 607 | if (optind != argc) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 608 | error_exit("Unexpected argument: %s", argv[optind]); |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 609 | } |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 610 | |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 611 | bdrv_img_create(filename, fmt, base_filename, base_fmt, |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 612 | options, img_size, flags, quiet, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 613 | if (local_err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 614 | error_reportf_err(local_err, "%s: ", filename); |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 615 | goto fail; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 616 | } |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 617 | |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 618 | g_free(options); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 619 | return 0; |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 620 | |
| 621 | fail: |
| 622 | g_free(options); |
| 623 | return 1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 626 | static void dump_json_image_check(ImageCheck *check, bool quiet) |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 627 | { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 628 | QString *str; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 629 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 630 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 631 | |
| 632 | visit_type_ImageCheck(v, NULL, &check, &error_abort); |
| 633 | visit_complete(v, &obj); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 634 | str = qobject_to_json_pretty(obj); |
| 635 | assert(str != NULL); |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 636 | qprintf(quiet, "%s\n", qstring_get_str(str)); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 637 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 638 | visit_free(v); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 639 | qobject_unref(str); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 640 | } |
| 641 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 642 | static void dump_human_image_check(ImageCheck *check, bool quiet) |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 643 | { |
| 644 | if (!(check->corruptions || check->leaks || check->check_errors)) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 645 | qprintf(quiet, "No errors were found on the image.\n"); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 646 | } else { |
| 647 | if (check->corruptions) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 648 | qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n" |
| 649 | "Data may be corrupted, or further writes to the image " |
| 650 | "may corrupt it.\n", |
| 651 | check->corruptions); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | if (check->leaks) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 655 | qprintf(quiet, |
| 656 | "\n%" PRId64 " leaked clusters were found on the image.\n" |
| 657 | "This means waste of disk space, but no harm to data.\n", |
| 658 | check->leaks); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | if (check->check_errors) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 662 | qprintf(quiet, |
| 663 | "\n%" PRId64 |
| 664 | " internal errors have occurred during the check.\n", |
| 665 | check->check_errors); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | |
| 669 | if (check->total_clusters != 0 && check->allocated_clusters != 0) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 670 | qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, " |
| 671 | "%0.2f%% fragmented, %0.2f%% compressed clusters\n", |
| 672 | check->allocated_clusters, check->total_clusters, |
| 673 | check->allocated_clusters * 100.0 / check->total_clusters, |
| 674 | check->fragmented_clusters * 100.0 / check->allocated_clusters, |
| 675 | check->compressed_clusters * 100.0 / |
| 676 | check->allocated_clusters); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | if (check->image_end_offset) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 680 | qprintf(quiet, |
| 681 | "Image end offset: %" PRId64 "\n", check->image_end_offset); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | |
| 685 | static int collect_image_check(BlockDriverState *bs, |
| 686 | ImageCheck *check, |
| 687 | const char *filename, |
| 688 | const char *fmt, |
| 689 | int fix) |
| 690 | { |
| 691 | int ret; |
| 692 | BdrvCheckResult result; |
| 693 | |
| 694 | ret = bdrv_check(bs, &result, fix); |
| 695 | if (ret < 0) { |
| 696 | return ret; |
| 697 | } |
| 698 | |
| 699 | check->filename = g_strdup(filename); |
| 700 | check->format = g_strdup(bdrv_get_format_name(bs)); |
| 701 | check->check_errors = result.check_errors; |
| 702 | check->corruptions = result.corruptions; |
| 703 | check->has_corruptions = result.corruptions != 0; |
| 704 | check->leaks = result.leaks; |
| 705 | check->has_leaks = result.leaks != 0; |
| 706 | check->corruptions_fixed = result.corruptions_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 707 | check->has_corruptions_fixed = result.corruptions_fixed != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 708 | check->leaks_fixed = result.leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 709 | check->has_leaks_fixed = result.leaks_fixed != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 710 | check->image_end_offset = result.image_end_offset; |
| 711 | check->has_image_end_offset = result.image_end_offset != 0; |
| 712 | check->total_clusters = result.bfi.total_clusters; |
| 713 | check->has_total_clusters = result.bfi.total_clusters != 0; |
| 714 | check->allocated_clusters = result.bfi.allocated_clusters; |
| 715 | check->has_allocated_clusters = result.bfi.allocated_clusters != 0; |
| 716 | check->fragmented_clusters = result.bfi.fragmented_clusters; |
| 717 | check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0; |
Stefan Hajnoczi | e6439d7 | 2013-02-07 17:15:04 +0100 | [diff] [blame] | 718 | check->compressed_clusters = result.bfi.compressed_clusters; |
| 719 | check->has_compressed_clusters = result.bfi.compressed_clusters != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 720 | |
| 721 | return 0; |
| 722 | } |
| 723 | |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 724 | /* |
| 725 | * Checks an image for consistency. Exit codes: |
| 726 | * |
Max Reitz | d6635c4 | 2014-06-02 22:15:21 +0200 | [diff] [blame] | 727 | * 0 - Check completed, image is good |
| 728 | * 1 - Check not completed because of internal errors |
| 729 | * 2 - Check completed, image is corrupted |
| 730 | * 3 - Check completed, image has leaked clusters, but is good otherwise |
| 731 | * 63 - Checks are not supported by the image format |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 732 | */ |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 733 | static int img_check(int argc, char **argv) |
| 734 | { |
| 735 | int c, ret; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 736 | OutputFormat output_format = OFORMAT_HUMAN; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 737 | const char *filename, *fmt, *output, *cache; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 738 | BlockBackend *blk; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 739 | BlockDriverState *bs; |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 740 | int fix = 0; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 741 | int flags = BDRV_O_CHECK; |
| 742 | bool writethrough; |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 743 | ImageCheck *check; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 744 | bool quiet = false; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 745 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 746 | bool force_share = false; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 747 | |
| 748 | fmt = NULL; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 749 | output = NULL; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 750 | cache = BDRV_DEFAULT_CACHE; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 751 | |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 752 | for(;;) { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 753 | int option_index = 0; |
| 754 | static const struct option long_options[] = { |
| 755 | {"help", no_argument, 0, 'h'}, |
| 756 | {"format", required_argument, 0, 'f'}, |
Prasad Joshi | 4fd6a98 | 2014-03-25 00:08:54 +0530 | [diff] [blame] | 757 | {"repair", required_argument, 0, 'r'}, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 758 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 759 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 760 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 761 | {"force-share", no_argument, 0, 'U'}, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 762 | {0, 0, 0, 0} |
| 763 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 764 | c = getopt_long(argc, argv, ":hf:r:T:qU", |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 765 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 766 | if (c == -1) { |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 767 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 768 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 769 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 770 | case ':': |
| 771 | missing_argument(argv[optind - 1]); |
| 772 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 773 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 774 | unrecognized_option(argv[optind - 1]); |
| 775 | break; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 776 | case 'h': |
| 777 | help(); |
| 778 | break; |
| 779 | case 'f': |
| 780 | fmt = optarg; |
| 781 | break; |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 782 | case 'r': |
| 783 | flags |= BDRV_O_RDWR; |
| 784 | |
| 785 | if (!strcmp(optarg, "leaks")) { |
| 786 | fix = BDRV_FIX_LEAKS; |
| 787 | } else if (!strcmp(optarg, "all")) { |
| 788 | fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS; |
| 789 | } else { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 790 | error_exit("Unknown option value for -r " |
| 791 | "(expecting 'leaks' or 'all'): %s", optarg); |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 792 | } |
| 793 | break; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 794 | case OPTION_OUTPUT: |
| 795 | output = optarg; |
| 796 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 797 | case 'T': |
| 798 | cache = optarg; |
| 799 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 800 | case 'q': |
| 801 | quiet = true; |
| 802 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 803 | case 'U': |
| 804 | force_share = true; |
| 805 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 806 | case OPTION_OBJECT: { |
| 807 | QemuOpts *opts; |
| 808 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 809 | optarg, true); |
| 810 | if (!opts) { |
| 811 | return 1; |
| 812 | } |
| 813 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 814 | case OPTION_IMAGE_OPTS: |
| 815 | image_opts = true; |
| 816 | break; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 817 | } |
| 818 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 819 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 820 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 821 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 822 | filename = argv[optind++]; |
| 823 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 824 | if (output && !strcmp(output, "json")) { |
| 825 | output_format = OFORMAT_JSON; |
| 826 | } else if (output && !strcmp(output, "human")) { |
| 827 | output_format = OFORMAT_HUMAN; |
| 828 | } else if (output) { |
| 829 | error_report("--output must be used with human or json as argument."); |
| 830 | return 1; |
| 831 | } |
| 832 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 833 | if (qemu_opts_foreach(&qemu_object_opts, |
| 834 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 835 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 836 | return 1; |
| 837 | } |
| 838 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 839 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 840 | if (ret < 0) { |
| 841 | error_report("Invalid source cache option: %s", cache); |
| 842 | return 1; |
| 843 | } |
| 844 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 845 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 846 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 847 | if (!blk) { |
| 848 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 849 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 850 | bs = blk_bs(blk); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 851 | |
| 852 | check = g_new0(ImageCheck, 1); |
| 853 | ret = collect_image_check(bs, check, filename, fmt, fix); |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 854 | |
| 855 | if (ret == -ENOTSUP) { |
Max Reitz | 55d492d | 2014-05-31 21:33:30 +0200 | [diff] [blame] | 856 | error_report("This image format does not support checks"); |
Peter Lieven | fefddf9 | 2013-10-24 08:53:34 +0200 | [diff] [blame] | 857 | ret = 63; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 858 | goto fail; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 859 | } |
| 860 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 861 | if (check->corruptions_fixed || check->leaks_fixed) { |
| 862 | int corruptions_fixed, leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 863 | bool has_leaks_fixed, has_corruptions_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 864 | |
| 865 | leaks_fixed = check->leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 866 | has_leaks_fixed = check->has_leaks_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 867 | corruptions_fixed = check->corruptions_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 868 | has_corruptions_fixed = check->has_corruptions_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 869 | |
| 870 | if (output_format == OFORMAT_HUMAN) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 871 | qprintf(quiet, |
| 872 | "The following inconsistencies were found and repaired:\n\n" |
| 873 | " %" PRId64 " leaked clusters\n" |
| 874 | " %" PRId64 " corruptions\n\n" |
| 875 | "Double checking the fixed image now...\n", |
| 876 | check->leaks_fixed, |
| 877 | check->corruptions_fixed); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 878 | } |
| 879 | |
Pan Nengyuan | fc124ea | 2020-02-27 09:29:50 +0800 | [diff] [blame] | 880 | qapi_free_ImageCheck(check); |
| 881 | check = g_new0(ImageCheck, 1); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 882 | ret = collect_image_check(bs, check, filename, fmt, 0); |
| 883 | |
| 884 | check->leaks_fixed = leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 885 | check->has_leaks_fixed = has_leaks_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 886 | check->corruptions_fixed = corruptions_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 887 | check->has_corruptions_fixed = has_corruptions_fixed; |
Kevin Wolf | ccf3471 | 2012-05-11 18:16:54 +0200 | [diff] [blame] | 888 | } |
| 889 | |
Max Reitz | 832390a | 2014-10-23 15:29:12 +0200 | [diff] [blame] | 890 | if (!ret) { |
| 891 | switch (output_format) { |
| 892 | case OFORMAT_HUMAN: |
| 893 | dump_human_image_check(check, quiet); |
| 894 | break; |
| 895 | case OFORMAT_JSON: |
| 896 | dump_json_image_check(check, quiet); |
| 897 | break; |
| 898 | } |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | if (ret || check->check_errors) { |
Max Reitz | 832390a | 2014-10-23 15:29:12 +0200 | [diff] [blame] | 902 | if (ret) { |
| 903 | error_report("Check failed: %s", strerror(-ret)); |
| 904 | } else { |
| 905 | error_report("Check failed"); |
| 906 | } |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 907 | ret = 1; |
| 908 | goto fail; |
| 909 | } |
| 910 | |
| 911 | if (check->corruptions) { |
| 912 | ret = 2; |
| 913 | } else if (check->leaks) { |
| 914 | ret = 3; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 915 | } else { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 916 | ret = 0; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 919 | fail: |
| 920 | qapi_free_ImageCheck(check); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 921 | blk_unref(blk); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 922 | return ret; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 923 | } |
| 924 | |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 925 | typedef struct CommonBlockJobCBInfo { |
| 926 | BlockDriverState *bs; |
| 927 | Error **errp; |
| 928 | } CommonBlockJobCBInfo; |
| 929 | |
| 930 | static void common_block_job_cb(void *opaque, int ret) |
| 931 | { |
| 932 | CommonBlockJobCBInfo *cbi = opaque; |
| 933 | |
| 934 | if (ret < 0) { |
| 935 | error_setg_errno(cbi->errp, -ret, "Block job failed"); |
| 936 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | static void run_block_job(BlockJob *job, Error **errp) |
| 940 | { |
Kevin Wolf | b75536c | 2016-04-18 17:30:17 +0200 | [diff] [blame] | 941 | AioContext *aio_context = blk_get_aio_context(job->blk); |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 942 | int ret = 0; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 943 | |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 944 | aio_context_acquire(aio_context); |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 945 | job_ref(&job->job); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 946 | do { |
Kevin Wolf | 30a5c88 | 2018-05-04 12:17:20 +0200 | [diff] [blame] | 947 | float progress = 0.0f; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 948 | aio_poll(aio_context, true); |
Vladimir Sementsov-Ogievskiy | 01fe1ca | 2020-03-11 13:29:56 +0300 | [diff] [blame] | 949 | if (job->job.progress.total) { |
| 950 | progress = (float)job->job.progress.current / |
| 951 | job->job.progress.total * 100.f; |
Kevin Wolf | 30a5c88 | 2018-05-04 12:17:20 +0200 | [diff] [blame] | 952 | } |
| 953 | qemu_progress_print(progress, 0); |
Kevin Wolf | df956ae | 2018-04-25 15:09:58 +0200 | [diff] [blame] | 954 | } while (!job_is_ready(&job->job) && !job_is_completed(&job->job)); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 955 | |
Kevin Wolf | dbe5e6c | 2018-04-19 13:04:01 +0200 | [diff] [blame] | 956 | if (!job_is_completed(&job->job)) { |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 957 | ret = job_complete_sync(&job->job, errp); |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 958 | } else { |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 959 | ret = job->job.ret; |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 960 | } |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 961 | job_unref(&job->job); |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 962 | aio_context_release(aio_context); |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 963 | |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 964 | /* publish completion progress only when success */ |
| 965 | if (!ret) { |
| 966 | qemu_progress_print(100.f, 0); |
| 967 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 968 | } |
| 969 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 970 | static int img_commit(int argc, char **argv) |
| 971 | { |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 972 | int c, ret, flags; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 973 | const char *filename, *fmt, *cache, *base; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 974 | BlockBackend *blk; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 975 | BlockDriverState *bs, *base_bs; |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 976 | BlockJob *job; |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 977 | bool progress = false, quiet = false, drop = false; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 978 | bool writethrough; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 979 | Error *local_err = NULL; |
| 980 | CommonBlockJobCBInfo cbi; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 981 | bool image_opts = false; |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 982 | AioContext *aio_context; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 983 | |
| 984 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 985 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 986 | base = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 987 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 988 | static const struct option long_options[] = { |
| 989 | {"help", no_argument, 0, 'h'}, |
| 990 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 991 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 992 | {0, 0, 0, 0} |
| 993 | }; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 994 | c = getopt_long(argc, argv, ":f:ht:b:dpq", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 995 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 996 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 997 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 998 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 999 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 1000 | case ':': |
| 1001 | missing_argument(argv[optind - 1]); |
| 1002 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 1003 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 1004 | unrecognized_option(argv[optind - 1]); |
| 1005 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1006 | case 'h': |
| 1007 | help(); |
| 1008 | break; |
| 1009 | case 'f': |
| 1010 | fmt = optarg; |
| 1011 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1012 | case 't': |
| 1013 | cache = optarg; |
| 1014 | break; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1015 | case 'b': |
| 1016 | base = optarg; |
| 1017 | /* -b implies -d */ |
| 1018 | drop = true; |
| 1019 | break; |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1020 | case 'd': |
| 1021 | drop = true; |
| 1022 | break; |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 1023 | case 'p': |
| 1024 | progress = true; |
| 1025 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 1026 | case 'q': |
| 1027 | quiet = true; |
| 1028 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1029 | case OPTION_OBJECT: { |
| 1030 | QemuOpts *opts; |
| 1031 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 1032 | optarg, true); |
| 1033 | if (!opts) { |
| 1034 | return 1; |
| 1035 | } |
| 1036 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1037 | case OPTION_IMAGE_OPTS: |
| 1038 | image_opts = true; |
| 1039 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1040 | } |
| 1041 | } |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 1042 | |
| 1043 | /* Progress is not shown in Quiet mode */ |
| 1044 | if (quiet) { |
| 1045 | progress = false; |
| 1046 | } |
| 1047 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 1048 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 1049 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1050 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1051 | filename = argv[optind++]; |
| 1052 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1053 | if (qemu_opts_foreach(&qemu_object_opts, |
| 1054 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 1055 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1056 | return 1; |
| 1057 | } |
| 1058 | |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1059 | flags = BDRV_O_RDWR | BDRV_O_UNMAP; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1060 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1061 | if (ret < 0) { |
| 1062 | error_report("Invalid cache option: %s", cache); |
Stefan Hajnoczi | a3981eb | 2014-08-26 19:17:54 +0100 | [diff] [blame] | 1063 | return 1; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1064 | } |
| 1065 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1066 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 1067 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1068 | if (!blk) { |
| 1069 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1070 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1071 | bs = blk_bs(blk); |
| 1072 | |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 1073 | qemu_progress_init(progress, 1.f); |
| 1074 | qemu_progress_print(0.f, 100); |
| 1075 | |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1076 | if (base) { |
| 1077 | base_bs = bdrv_find_backing_image(bs, base); |
| 1078 | if (!base_bs) { |
Max Reitz | 6b33f3a | 2016-12-01 03:05:08 +0100 | [diff] [blame] | 1079 | error_setg(&local_err, |
| 1080 | "Did not find '%s' in the backing chain of '%s'", |
| 1081 | base, filename); |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1082 | goto done; |
| 1083 | } |
| 1084 | } else { |
| 1085 | /* This is different from QMP, which by default uses the deepest file in |
| 1086 | * the backing chain (i.e., the very base); however, the traditional |
| 1087 | * behavior of qemu-img commit is using the immediate backing file. */ |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 1088 | base_bs = bdrv_backing_chain_next(bs); |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1089 | if (!base_bs) { |
| 1090 | error_setg(&local_err, "Image does not have a backing file"); |
| 1091 | goto done; |
| 1092 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1095 | cbi = (CommonBlockJobCBInfo){ |
| 1096 | .errp = &local_err, |
| 1097 | .bs = bs, |
| 1098 | }; |
| 1099 | |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 1100 | aio_context = bdrv_get_aio_context(bs); |
| 1101 | aio_context_acquire(aio_context); |
Kevin Wolf | bb02b65 | 2018-04-19 17:54:56 +0200 | [diff] [blame] | 1102 | commit_active_start("commit", bs, base_bs, JOB_DEFAULT, 0, |
Kevin Wolf | 0db832f | 2017-02-20 18:10:05 +0100 | [diff] [blame] | 1103 | BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb, |
Fam Zheng | 78bbd91 | 2017-04-21 20:27:04 +0800 | [diff] [blame] | 1104 | &cbi, false, &local_err); |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 1105 | aio_context_release(aio_context); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1106 | if (local_err) { |
| 1107 | goto done; |
| 1108 | } |
| 1109 | |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1110 | /* When the block job completes, the BlockBackend reference will point to |
| 1111 | * the old backing file. In order to avoid that the top image is already |
| 1112 | * deleted, so we can still empty it afterwards, increment the reference |
| 1113 | * counter here preemptively. */ |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1114 | if (!drop) { |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1115 | bdrv_ref(bs); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1116 | } |
| 1117 | |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 1118 | job = block_job_get("commit"); |
Liam Merwick | 2e2db26 | 2018-11-05 21:38:37 +0000 | [diff] [blame] | 1119 | assert(job); |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 1120 | run_block_job(job, &local_err); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1121 | if (local_err) { |
| 1122 | goto unref_backing; |
| 1123 | } |
| 1124 | |
Max Reitz | 2d97fde | 2020-04-29 16:11:26 +0200 | [diff] [blame] | 1125 | if (!drop) { |
| 1126 | BlockBackend *old_backing_blk; |
| 1127 | |
| 1128 | old_backing_blk = blk_new_with_bs(bs, BLK_PERM_WRITE, BLK_PERM_ALL, |
| 1129 | &local_err); |
| 1130 | if (!old_backing_blk) { |
| 1131 | goto unref_backing; |
| 1132 | } |
| 1133 | ret = blk_make_empty(old_backing_blk, &local_err); |
| 1134 | blk_unref(old_backing_blk); |
| 1135 | if (ret == -ENOTSUP) { |
| 1136 | error_free(local_err); |
| 1137 | local_err = NULL; |
| 1138 | } else if (ret < 0) { |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1139 | goto unref_backing; |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | unref_backing: |
| 1144 | if (!drop) { |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1145 | bdrv_unref(bs); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1146 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1147 | |
| 1148 | done: |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 1149 | qemu_progress_end(); |
| 1150 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1151 | blk_unref(blk); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1152 | |
| 1153 | if (local_err) { |
Markus Armbruster | 6936f29 | 2015-02-10 15:14:02 +0100 | [diff] [blame] | 1154 | error_report_err(local_err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1155 | return 1; |
| 1156 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1157 | |
| 1158 | qprintf(quiet, "Image committed.\n"); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1159 | return 0; |
| 1160 | } |
| 1161 | |
Dmitry Konishchev | f6a00aa | 2011-05-18 15:03:59 +0400 | [diff] [blame] | 1162 | /* |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1163 | * Returns -1 if 'buf' contains only zeroes, otherwise the byte index |
| 1164 | * of the first sector boundary within buf where the sector contains a |
| 1165 | * non-zero byte. This function is robust to a buffer that is not |
| 1166 | * sector-aligned. |
| 1167 | */ |
| 1168 | static int64_t find_nonzero(const uint8_t *buf, int64_t n) |
| 1169 | { |
| 1170 | int64_t i; |
| 1171 | int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE); |
| 1172 | |
| 1173 | for (i = 0; i < end; i += BDRV_SECTOR_SIZE) { |
| 1174 | if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) { |
| 1175 | return i; |
| 1176 | } |
| 1177 | } |
| 1178 | if (i < n && !buffer_is_zero(buf + i, n - end)) { |
| 1179 | return i; |
| 1180 | } |
| 1181 | return -1; |
| 1182 | } |
| 1183 | |
| 1184 | /* |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1185 | * Returns true iff the first sector pointed to by 'buf' contains at least |
| 1186 | * a non-NUL byte. |
| 1187 | * |
| 1188 | * 'pnum' is set to the number of sectors (including and immediately following |
| 1189 | * 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] | 1190 | * The function will try to align the end offset to alignment boundaries so |
zhaolichang | e3a6e0d | 2020-09-17 15:50:20 +0800 | [diff] [blame^] | 1191 | * that the request will at least end aligned and consecutive requests will |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1192 | * also start at an aligned offset. |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1193 | */ |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1194 | static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum, |
| 1195 | int64_t sector_num, int alignment) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1196 | { |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1197 | bool is_zero; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1198 | int i, tail; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1199 | |
| 1200 | if (n <= 0) { |
| 1201 | *pnum = 0; |
| 1202 | return 0; |
| 1203 | } |
Yi Li | c075c42 | 2020-08-19 09:36:07 +0800 | [diff] [blame] | 1204 | is_zero = buffer_is_zero(buf, BDRV_SECTOR_SIZE); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1205 | for(i = 1; i < n; i++) { |
Yi Li | c075c42 | 2020-08-19 09:36:07 +0800 | [diff] [blame] | 1206 | buf += BDRV_SECTOR_SIZE; |
| 1207 | if (is_zero != buffer_is_zero(buf, BDRV_SECTOR_SIZE)) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1208 | break; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1209 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1210 | } |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1211 | |
| 1212 | tail = (sector_num + i) & (alignment - 1); |
| 1213 | if (tail) { |
| 1214 | if (is_zero && i <= tail) { |
| 1215 | /* treat unallocated areas which only consist |
| 1216 | * of a small tail as allocated. */ |
| 1217 | is_zero = false; |
| 1218 | } |
| 1219 | if (!is_zero) { |
| 1220 | /* align up end offset of allocated areas. */ |
| 1221 | i += alignment - tail; |
| 1222 | i = MIN(i, n); |
| 1223 | } else { |
| 1224 | /* align down end offset of zero areas. */ |
| 1225 | i -= tail; |
| 1226 | } |
| 1227 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1228 | *pnum = i; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1229 | return !is_zero; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1232 | /* |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1233 | * Like is_allocated_sectors, but if the buffer starts with a used sector, |
| 1234 | * up to 'min' consecutive sectors containing zeros are ignored. This avoids |
| 1235 | * breaking up write requests for only small sparse areas. |
| 1236 | */ |
| 1237 | 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] | 1238 | int min, int64_t sector_num, int alignment) |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1239 | { |
| 1240 | int ret; |
| 1241 | int num_checked, num_used; |
| 1242 | |
| 1243 | if (n < min) { |
| 1244 | min = n; |
| 1245 | } |
| 1246 | |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1247 | ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment); |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1248 | if (!ret) { |
| 1249 | return ret; |
| 1250 | } |
| 1251 | |
| 1252 | num_used = *pnum; |
| 1253 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 1254 | n -= *pnum; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1255 | sector_num += *pnum; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1256 | num_checked = num_used; |
| 1257 | |
| 1258 | while (n > 0) { |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1259 | ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment); |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1260 | |
| 1261 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 1262 | n -= *pnum; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1263 | sector_num += *pnum; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1264 | num_checked += *pnum; |
| 1265 | if (ret) { |
| 1266 | num_used = num_checked; |
| 1267 | } else if (*pnum >= min) { |
| 1268 | break; |
| 1269 | } |
| 1270 | } |
| 1271 | |
| 1272 | *pnum = num_used; |
| 1273 | return 1; |
| 1274 | } |
| 1275 | |
| 1276 | /* |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1277 | * Compares two buffers sector by sector. Returns 0 if the first |
| 1278 | * sector of each buffer matches, non-zero otherwise. |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1279 | * |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1280 | * pnum is set to the sector-aligned size of the buffer prefix that |
| 1281 | * has the same matching status as the first sector. |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1282 | */ |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1283 | static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2, |
| 1284 | int64_t bytes, int64_t *pnum) |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1285 | { |
Radim Krčmář | 8c1ac47 | 2015-02-20 17:06:15 +0100 | [diff] [blame] | 1286 | bool res; |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1287 | int64_t i = MIN(bytes, BDRV_SECTOR_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1288 | |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1289 | assert(bytes > 0); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1290 | |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1291 | res = !!memcmp(buf1, buf2, i); |
| 1292 | while (i < bytes) { |
| 1293 | int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1294 | |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1295 | if (!!memcmp(buf1 + i, buf2 + i, len) != res) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1296 | break; |
| 1297 | } |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1298 | i += len; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | *pnum = i; |
| 1302 | return res; |
| 1303 | } |
| 1304 | |
Stefano Garzarella | 97ede57 | 2019-05-08 12:43:24 +0200 | [diff] [blame] | 1305 | #define IO_BUF_SIZE (2 * MiB) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1306 | |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1307 | /* |
| 1308 | * Check if passed sectors are empty (not allocated or contain only 0 bytes) |
| 1309 | * |
Eric Blake | 0608e40 | 2017-10-11 22:47:12 -0500 | [diff] [blame] | 1310 | * Intended for use by 'qemu-img compare': Returns 0 in case sectors are |
| 1311 | * filled with 0, 1 if sectors contain non-zero data (this is a comparison |
| 1312 | * failure), and 4 on error (the exit status for read errors), after emitting |
| 1313 | * an error message. |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1314 | * |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1315 | * @param blk: BlockBackend for the image |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1316 | * @param offset: Starting offset to check |
| 1317 | * @param bytes: Number of bytes to check |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1318 | * @param filename: Name of disk file we are checking (logging purpose) |
| 1319 | * @param buffer: Allocated buffer for storing read data |
| 1320 | * @param quiet: Flag for quiet mode |
| 1321 | */ |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1322 | static int check_empty_sectors(BlockBackend *blk, int64_t offset, |
| 1323 | int64_t bytes, const char *filename, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1324 | uint8_t *buffer, bool quiet) |
| 1325 | { |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1326 | int ret = 0; |
| 1327 | int64_t idx; |
| 1328 | |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1329 | ret = blk_pread(blk, offset, buffer, bytes); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1330 | if (ret < 0) { |
| 1331 | error_report("Error while reading offset %" PRId64 " of %s: %s", |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1332 | offset, filename, strerror(-ret)); |
Eric Blake | 0608e40 | 2017-10-11 22:47:12 -0500 | [diff] [blame] | 1333 | return 4; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1334 | } |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1335 | idx = find_nonzero(buffer, bytes); |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1336 | if (idx >= 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1337 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1338 | offset + idx); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1339 | return 1; |
| 1340 | } |
| 1341 | |
| 1342 | return 0; |
| 1343 | } |
| 1344 | |
| 1345 | /* |
| 1346 | * Compares two images. Exit codes: |
| 1347 | * |
| 1348 | * 0 - Images are identical |
| 1349 | * 1 - Images differ |
| 1350 | * >1 - Error occurred |
| 1351 | */ |
| 1352 | static int img_compare(int argc, char **argv) |
| 1353 | { |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1354 | const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1355 | BlockBackend *blk1, *blk2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1356 | BlockDriverState *bs1, *bs2; |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1357 | int64_t total_size1, total_size2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1358 | uint8_t *buf1 = NULL, *buf2 = NULL; |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1359 | int64_t pnum1, pnum2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1360 | int allocated1, allocated2; |
| 1361 | int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */ |
| 1362 | bool progress = false, quiet = false, strict = false; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1363 | int flags; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1364 | bool writethrough; |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1365 | int64_t total_size; |
| 1366 | int64_t offset = 0; |
| 1367 | int64_t chunk; |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1368 | int c; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1369 | uint64_t progress_base; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1370 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1371 | bool force_share = false; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1372 | |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1373 | cache = BDRV_DEFAULT_CACHE; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1374 | for (;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1375 | static const struct option long_options[] = { |
| 1376 | {"help", no_argument, 0, 'h'}, |
| 1377 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1378 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1379 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1380 | {0, 0, 0, 0} |
| 1381 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1382 | c = getopt_long(argc, argv, ":hf:F:T:pqsU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1383 | long_options, NULL); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1384 | if (c == -1) { |
| 1385 | break; |
| 1386 | } |
| 1387 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 1388 | case ':': |
| 1389 | missing_argument(argv[optind - 1]); |
| 1390 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1391 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 1392 | unrecognized_option(argv[optind - 1]); |
| 1393 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1394 | case 'h': |
| 1395 | help(); |
| 1396 | break; |
| 1397 | case 'f': |
| 1398 | fmt1 = optarg; |
| 1399 | break; |
| 1400 | case 'F': |
| 1401 | fmt2 = optarg; |
| 1402 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1403 | case 'T': |
| 1404 | cache = optarg; |
| 1405 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1406 | case 'p': |
| 1407 | progress = true; |
| 1408 | break; |
| 1409 | case 'q': |
| 1410 | quiet = true; |
| 1411 | break; |
| 1412 | case 's': |
| 1413 | strict = true; |
| 1414 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1415 | case 'U': |
| 1416 | force_share = true; |
| 1417 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1418 | case OPTION_OBJECT: { |
| 1419 | QemuOpts *opts; |
| 1420 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 1421 | optarg, true); |
| 1422 | if (!opts) { |
| 1423 | ret = 2; |
| 1424 | goto out4; |
| 1425 | } |
| 1426 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1427 | case OPTION_IMAGE_OPTS: |
| 1428 | image_opts = true; |
| 1429 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1430 | } |
| 1431 | } |
| 1432 | |
| 1433 | /* Progress is not shown in Quiet mode */ |
| 1434 | if (quiet) { |
| 1435 | progress = false; |
| 1436 | } |
| 1437 | |
| 1438 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 1439 | if (optind != argc - 2) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 1440 | error_exit("Expecting two image file names"); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1441 | } |
| 1442 | filename1 = argv[optind++]; |
| 1443 | filename2 = argv[optind++]; |
| 1444 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1445 | if (qemu_opts_foreach(&qemu_object_opts, |
| 1446 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 1447 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1448 | ret = 2; |
| 1449 | goto out4; |
| 1450 | } |
| 1451 | |
Stefan Hajnoczi | cbda016 | 2014-08-26 19:17:55 +0100 | [diff] [blame] | 1452 | /* Initialize before goto out */ |
| 1453 | qemu_progress_init(progress, 2.0); |
| 1454 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1455 | flags = 0; |
| 1456 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1457 | if (ret < 0) { |
| 1458 | error_report("Invalid source cache option: %s", cache); |
| 1459 | ret = 2; |
| 1460 | goto out3; |
| 1461 | } |
| 1462 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1463 | blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet, |
| 1464 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1465 | if (!blk1) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1466 | ret = 2; |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1467 | goto out3; |
| 1468 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1469 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1470 | blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet, |
| 1471 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1472 | if (!blk2) { |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1473 | ret = 2; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1474 | goto out2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1475 | } |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1476 | bs1 = blk_bs(blk1); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1477 | bs2 = blk_bs(blk2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1478 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1479 | buf1 = blk_blockalign(blk1, IO_BUF_SIZE); |
| 1480 | buf2 = blk_blockalign(blk2, IO_BUF_SIZE); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1481 | total_size1 = blk_getlength(blk1); |
| 1482 | if (total_size1 < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1483 | error_report("Can't get size of %s: %s", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1484 | filename1, strerror(-total_size1)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1485 | ret = 4; |
| 1486 | goto out; |
| 1487 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1488 | total_size2 = blk_getlength(blk2); |
| 1489 | if (total_size2 < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1490 | error_report("Can't get size of %s: %s", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1491 | filename2, strerror(-total_size2)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1492 | ret = 4; |
| 1493 | goto out; |
| 1494 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1495 | total_size = MIN(total_size1, total_size2); |
| 1496 | progress_base = MAX(total_size1, total_size2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1497 | |
| 1498 | qemu_progress_print(0, 100); |
| 1499 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1500 | if (strict && total_size1 != total_size2) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1501 | ret = 1; |
| 1502 | qprintf(quiet, "Strict mode: Image size mismatch!\n"); |
| 1503 | goto out; |
| 1504 | } |
| 1505 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1506 | while (offset < total_size) { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1507 | int status1, status2; |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 1508 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1509 | status1 = bdrv_block_status_above(bs1, NULL, offset, |
| 1510 | total_size1 - offset, &pnum1, NULL, |
| 1511 | NULL); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1512 | if (status1 < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1513 | ret = 3; |
| 1514 | error_report("Sector allocation test failed for %s", filename1); |
| 1515 | goto out; |
| 1516 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1517 | allocated1 = status1 & BDRV_BLOCK_ALLOCATED; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1518 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1519 | status2 = bdrv_block_status_above(bs2, NULL, offset, |
| 1520 | total_size2 - offset, &pnum2, NULL, |
| 1521 | NULL); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1522 | if (status2 < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1523 | ret = 3; |
| 1524 | error_report("Sector allocation test failed for %s", filename2); |
| 1525 | goto out; |
| 1526 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1527 | allocated2 = status2 & BDRV_BLOCK_ALLOCATED; |
Eric Blake | 7daddc6 | 2017-10-11 22:47:09 -0500 | [diff] [blame] | 1528 | |
| 1529 | assert(pnum1 && pnum2); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1530 | chunk = MIN(pnum1, pnum2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1531 | |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1532 | if (strict) { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1533 | if (status1 != status2) { |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1534 | ret = 1; |
| 1535 | qprintf(quiet, "Strict mode: Offset %" PRId64 |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1536 | " block status mismatch!\n", offset); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1537 | goto out; |
| 1538 | } |
| 1539 | } |
| 1540 | if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) { |
Eric Blake | 7daddc6 | 2017-10-11 22:47:09 -0500 | [diff] [blame] | 1541 | /* nothing to do */ |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1542 | } else if (allocated1 == allocated2) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1543 | if (allocated1) { |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1544 | int64_t pnum; |
| 1545 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1546 | chunk = MIN(chunk, IO_BUF_SIZE); |
| 1547 | ret = blk_pread(blk1, offset, buf1, chunk); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1548 | if (ret < 0) { |
| 1549 | error_report("Error while reading offset %" PRId64 |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1550 | " of %s: %s", |
| 1551 | offset, filename1, strerror(-ret)); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1552 | ret = 4; |
| 1553 | goto out; |
| 1554 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1555 | ret = blk_pread(blk2, offset, buf2, chunk); |
| 1556 | if (ret < 0) { |
| 1557 | error_report("Error while reading offset %" PRId64 |
| 1558 | " of %s: %s", |
| 1559 | offset, filename2, strerror(-ret)); |
| 1560 | ret = 4; |
| 1561 | goto out; |
| 1562 | } |
| 1563 | ret = compare_buffers(buf1, buf2, chunk, &pnum); |
| 1564 | if (ret || pnum != chunk) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1565 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1566 | offset + (ret ? 0 : pnum)); |
Fam Zheng | 36452f1 | 2013-11-13 20:26:49 +0800 | [diff] [blame] | 1567 | ret = 1; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1568 | goto out; |
| 1569 | } |
| 1570 | } |
| 1571 | } else { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1572 | chunk = MIN(chunk, IO_BUF_SIZE); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1573 | if (allocated1) { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1574 | ret = check_empty_sectors(blk1, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1575 | filename1, buf1, quiet); |
| 1576 | } else { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1577 | ret = check_empty_sectors(blk2, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1578 | filename2, buf1, quiet); |
| 1579 | } |
| 1580 | if (ret) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1581 | goto out; |
| 1582 | } |
| 1583 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1584 | offset += chunk; |
| 1585 | qemu_progress_print(((float) chunk / progress_base) * 100, 100); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1586 | } |
| 1587 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1588 | if (total_size1 != total_size2) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1589 | BlockBackend *blk_over; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1590 | const char *filename_over; |
| 1591 | |
| 1592 | qprintf(quiet, "Warning: Image size mismatch!\n"); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1593 | if (total_size1 > total_size2) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1594 | blk_over = blk1; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1595 | filename_over = filename1; |
| 1596 | } else { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1597 | blk_over = blk2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1598 | filename_over = filename2; |
| 1599 | } |
| 1600 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1601 | while (offset < progress_base) { |
| 1602 | ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset, |
| 1603 | progress_base - offset, &chunk, |
| 1604 | NULL, NULL); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1605 | if (ret < 0) { |
| 1606 | ret = 3; |
| 1607 | error_report("Sector allocation test failed for %s", |
| 1608 | filename_over); |
| 1609 | goto out; |
| 1610 | |
| 1611 | } |
Eric Blake | 391cb1a | 2017-10-11 22:47:10 -0500 | [diff] [blame] | 1612 | if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1613 | chunk = MIN(chunk, IO_BUF_SIZE); |
| 1614 | ret = check_empty_sectors(blk_over, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1615 | filename_over, buf1, quiet); |
| 1616 | if (ret) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1617 | goto out; |
| 1618 | } |
| 1619 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1620 | offset += chunk; |
| 1621 | qemu_progress_print(((float) chunk / progress_base) * 100, 100); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1622 | } |
| 1623 | } |
| 1624 | |
| 1625 | qprintf(quiet, "Images are identical.\n"); |
| 1626 | ret = 0; |
| 1627 | |
| 1628 | out: |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1629 | qemu_vfree(buf1); |
| 1630 | qemu_vfree(buf2); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1631 | blk_unref(blk2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1632 | out2: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1633 | blk_unref(blk1); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1634 | out3: |
| 1635 | qemu_progress_end(); |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1636 | out4: |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1637 | return ret; |
| 1638 | } |
| 1639 | |
Eric Blake | 6c729dd | 2020-05-21 14:21:35 -0500 | [diff] [blame] | 1640 | /* Convenience wrapper around qmp_block_dirty_bitmap_merge */ |
| 1641 | static void do_dirty_bitmap_merge(const char *dst_node, const char *dst_name, |
| 1642 | const char *src_node, const char *src_name, |
| 1643 | Error **errp) |
| 1644 | { |
| 1645 | BlockDirtyBitmapMergeSource *merge_src; |
| 1646 | BlockDirtyBitmapMergeSourceList *list; |
| 1647 | |
| 1648 | merge_src = g_new0(BlockDirtyBitmapMergeSource, 1); |
| 1649 | merge_src->type = QTYPE_QDICT; |
| 1650 | merge_src->u.external.node = g_strdup(src_node); |
| 1651 | merge_src->u.external.name = g_strdup(src_name); |
| 1652 | list = g_new0(BlockDirtyBitmapMergeSourceList, 1); |
| 1653 | list->value = merge_src; |
| 1654 | qmp_block_dirty_bitmap_merge(dst_node, dst_name, list, errp); |
| 1655 | qapi_free_BlockDirtyBitmapMergeSourceList(list); |
| 1656 | } |
| 1657 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1658 | enum ImgConvertBlockStatus { |
| 1659 | BLK_DATA, |
| 1660 | BLK_ZERO, |
| 1661 | BLK_BACKING_FILE, |
| 1662 | }; |
| 1663 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1664 | #define MAX_COROUTINES 16 |
| 1665 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1666 | typedef struct ImgConvertState { |
| 1667 | BlockBackend **src; |
| 1668 | int64_t *src_sectors; |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 1669 | int *src_alignment; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1670 | int src_num; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1671 | int64_t total_sectors; |
| 1672 | int64_t allocated_sectors; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1673 | int64_t allocated_done; |
| 1674 | int64_t sector_num; |
| 1675 | int64_t wr_offs; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1676 | enum ImgConvertBlockStatus status; |
| 1677 | int64_t sector_next_status; |
| 1678 | BlockBackend *target; |
| 1679 | bool has_zero_init; |
| 1680 | bool compressed; |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 1681 | bool target_is_new; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1682 | bool target_has_backing; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1683 | int64_t target_backing_sectors; /* negative if unknown */ |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1684 | bool wr_in_order; |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1685 | bool copy_range; |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1686 | bool salvage; |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 1687 | bool quiet; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1688 | int min_sparse; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1689 | int alignment; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1690 | size_t cluster_sectors; |
| 1691 | size_t buf_sectors; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 1692 | long num_coroutines; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1693 | int running_coroutines; |
| 1694 | Coroutine *co[MAX_COROUTINES]; |
| 1695 | int64_t wait_sector_num[MAX_COROUTINES]; |
| 1696 | CoMutex lock; |
| 1697 | int ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1698 | } ImgConvertState; |
| 1699 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1700 | static void convert_select_part(ImgConvertState *s, int64_t sector_num, |
| 1701 | int *src_cur, int64_t *src_cur_offset) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1702 | { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1703 | *src_cur = 0; |
| 1704 | *src_cur_offset = 0; |
| 1705 | while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) { |
| 1706 | *src_cur_offset += s->src_sectors[*src_cur]; |
| 1707 | (*src_cur)++; |
| 1708 | assert(*src_cur < s->src_num); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num) |
| 1713 | { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1714 | int64_t src_cur_offset; |
| 1715 | int ret, n, src_cur; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1716 | bool post_backing_zero = false; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1717 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1718 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1719 | |
| 1720 | assert(s->total_sectors > sector_num); |
| 1721 | n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS); |
| 1722 | |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1723 | if (s->target_backing_sectors >= 0) { |
| 1724 | if (sector_num >= s->target_backing_sectors) { |
Vladimir Sementsov-Ogievskiy | 2253d86 | 2020-05-28 12:43:56 +0300 | [diff] [blame] | 1725 | post_backing_zero = true; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1726 | } else if (sector_num + n > s->target_backing_sectors) { |
| 1727 | /* Split requests around target_backing_sectors (because |
| 1728 | * starting from there, zeros are handled differently) */ |
| 1729 | n = s->target_backing_sectors - sector_num; |
| 1730 | } |
| 1731 | } |
| 1732 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1733 | if (s->sector_next_status <= sector_num) { |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1734 | uint64_t offset = (sector_num - src_cur_offset) * BDRV_SECTOR_SIZE; |
| 1735 | int64_t count; |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 1736 | int tail; |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 1737 | BlockDriverState *src_bs = blk_bs(s->src[src_cur]); |
| 1738 | BlockDriverState *base; |
| 1739 | |
| 1740 | if (s->target_has_backing) { |
| 1741 | base = bdrv_cow_bs(bdrv_skip_filters(src_bs)); |
| 1742 | } else { |
| 1743 | base = NULL; |
| 1744 | } |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1745 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1746 | do { |
| 1747 | count = n * BDRV_SECTOR_SIZE; |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 1748 | |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 1749 | ret = bdrv_block_status_above(src_bs, base, offset, count, &count, |
| 1750 | NULL, NULL); |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1751 | |
| 1752 | if (ret < 0) { |
| 1753 | if (s->salvage) { |
| 1754 | if (n == 1) { |
| 1755 | if (!s->quiet) { |
| 1756 | warn_report("error while reading block status at " |
| 1757 | "offset %" PRIu64 ": %s", offset, |
| 1758 | strerror(-ret)); |
| 1759 | } |
| 1760 | /* Just try to read the data, then */ |
| 1761 | ret = BDRV_BLOCK_DATA; |
| 1762 | count = BDRV_SECTOR_SIZE; |
| 1763 | } else { |
| 1764 | /* Retry on a shorter range */ |
| 1765 | n = DIV_ROUND_UP(n, 4); |
| 1766 | } |
| 1767 | } else { |
| 1768 | error_report("error while reading block status at offset " |
| 1769 | "%" PRIu64 ": %s", offset, strerror(-ret)); |
| 1770 | return ret; |
| 1771 | } |
| 1772 | } |
| 1773 | } while (ret < 0); |
| 1774 | |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1775 | n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1776 | |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 1777 | /* |
| 1778 | * Avoid that s->sector_next_status becomes unaligned to the source |
| 1779 | * request alignment and/or cluster size to avoid unnecessary read |
| 1780 | * cycles. |
| 1781 | */ |
| 1782 | tail = (sector_num - src_cur_offset + n) % s->src_alignment[src_cur]; |
| 1783 | if (n > tail) { |
| 1784 | n -= tail; |
| 1785 | } |
| 1786 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1787 | if (ret & BDRV_BLOCK_ZERO) { |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1788 | s->status = post_backing_zero ? BLK_BACKING_FILE : BLK_ZERO; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1789 | } else if (ret & BDRV_BLOCK_DATA) { |
| 1790 | s->status = BLK_DATA; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1791 | } else { |
Vladimir Sementsov-Ogievskiy | 9f1b92a | 2017-04-07 14:34:04 +0300 | [diff] [blame] | 1792 | s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | s->sector_next_status = sector_num + n; |
| 1796 | } |
| 1797 | |
| 1798 | n = MIN(n, s->sector_next_status - sector_num); |
| 1799 | if (s->status == BLK_DATA) { |
| 1800 | n = MIN(n, s->buf_sectors); |
| 1801 | } |
| 1802 | |
| 1803 | /* We need to write complete clusters for compressed images, so if an |
| 1804 | * unallocated area is shorter than that, we must consider the whole |
| 1805 | * cluster allocated. */ |
| 1806 | if (s->compressed) { |
| 1807 | if (n < s->cluster_sectors) { |
| 1808 | n = MIN(s->cluster_sectors, s->total_sectors - sector_num); |
| 1809 | s->status = BLK_DATA; |
| 1810 | } else { |
| 1811 | n = QEMU_ALIGN_DOWN(n, s->cluster_sectors); |
| 1812 | } |
| 1813 | } |
| 1814 | |
| 1815 | return n; |
| 1816 | } |
| 1817 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1818 | static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num, |
| 1819 | int nb_sectors, uint8_t *buf) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1820 | { |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1821 | uint64_t single_read_until = 0; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1822 | int n, ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1823 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1824 | assert(nb_sectors <= s->buf_sectors); |
| 1825 | while (nb_sectors > 0) { |
| 1826 | BlockBackend *blk; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1827 | int src_cur; |
| 1828 | int64_t bs_sectors, src_cur_offset; |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1829 | uint64_t offset; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1830 | |
| 1831 | /* In the case of compression with multiple source files, we can get a |
| 1832 | * nb_sectors that spreads into the next part. So we must be able to |
| 1833 | * read across multiple BDSes for one convert_read() call. */ |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1834 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
| 1835 | blk = s->src[src_cur]; |
| 1836 | bs_sectors = s->src_sectors[src_cur]; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1837 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1838 | offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1839 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1840 | n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset)); |
| 1841 | if (single_read_until > offset) { |
| 1842 | n = 1; |
| 1843 | } |
| 1844 | |
| 1845 | ret = blk_co_pread(blk, offset, n << BDRV_SECTOR_BITS, buf, 0); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1846 | if (ret < 0) { |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1847 | if (s->salvage) { |
| 1848 | if (n > 1) { |
| 1849 | single_read_until = offset + (n << BDRV_SECTOR_BITS); |
| 1850 | continue; |
| 1851 | } else { |
| 1852 | if (!s->quiet) { |
| 1853 | warn_report("error while reading offset %" PRIu64 |
| 1854 | ": %s", offset, strerror(-ret)); |
| 1855 | } |
| 1856 | memset(buf, 0, BDRV_SECTOR_SIZE); |
| 1857 | } |
| 1858 | } else { |
| 1859 | return ret; |
| 1860 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1861 | } |
| 1862 | |
| 1863 | sector_num += n; |
| 1864 | nb_sectors -= n; |
| 1865 | buf += n * BDRV_SECTOR_SIZE; |
| 1866 | } |
| 1867 | |
| 1868 | return 0; |
| 1869 | } |
| 1870 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1871 | |
| 1872 | static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num, |
| 1873 | int nb_sectors, uint8_t *buf, |
| 1874 | enum ImgConvertBlockStatus status) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1875 | { |
| 1876 | int ret; |
| 1877 | |
| 1878 | while (nb_sectors > 0) { |
| 1879 | int n = nb_sectors; |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1880 | BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0; |
| 1881 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1882 | switch (status) { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1883 | case BLK_BACKING_FILE: |
| 1884 | /* If we have a backing file, leave clusters unallocated that are |
| 1885 | * unallocated in the source image, so that the backing file is |
| 1886 | * visible at the respective offset. */ |
| 1887 | assert(s->target_has_backing); |
| 1888 | break; |
| 1889 | |
| 1890 | case BLK_DATA: |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1891 | /* If we're told to keep the target fully allocated (-S 0) or there |
| 1892 | * is real non-zero data, we must write it. Otherwise we can treat |
| 1893 | * it as zero sectors. |
| 1894 | * Compressed clusters need to be written as a whole, so in that |
| 1895 | * case we can only save the write if the buffer is completely |
| 1896 | * zeroed. */ |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1897 | if (!s->min_sparse || |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1898 | (!s->compressed && |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1899 | is_allocated_sectors_min(buf, n, &n, s->min_sparse, |
| 1900 | sector_num, s->alignment)) || |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1901 | (s->compressed && |
| 1902 | !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1903 | { |
Vladimir Sementsov-Ogievskiy | 265a7e5 | 2019-04-22 17:58:38 +0300 | [diff] [blame] | 1904 | ret = blk_co_pwrite(s->target, sector_num << BDRV_SECTOR_BITS, |
| 1905 | n << BDRV_SECTOR_BITS, buf, flags); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1906 | if (ret < 0) { |
| 1907 | return ret; |
| 1908 | } |
| 1909 | break; |
| 1910 | } |
| 1911 | /* fall-through */ |
| 1912 | |
| 1913 | case BLK_ZERO: |
| 1914 | if (s->has_zero_init) { |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1915 | assert(!s->target_has_backing); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1916 | break; |
| 1917 | } |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1918 | ret = blk_co_pwrite_zeroes(s->target, |
| 1919 | sector_num << BDRV_SECTOR_BITS, |
Nir Soffer | a3d6ae2 | 2019-03-24 02:20:12 +0200 | [diff] [blame] | 1920 | n << BDRV_SECTOR_BITS, |
| 1921 | BDRV_REQ_MAY_UNMAP); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1922 | if (ret < 0) { |
| 1923 | return ret; |
| 1924 | } |
| 1925 | break; |
| 1926 | } |
| 1927 | |
| 1928 | sector_num += n; |
| 1929 | nb_sectors -= n; |
| 1930 | buf += n * BDRV_SECTOR_SIZE; |
| 1931 | } |
| 1932 | |
| 1933 | return 0; |
| 1934 | } |
| 1935 | |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1936 | static int coroutine_fn convert_co_copy_range(ImgConvertState *s, int64_t sector_num, |
| 1937 | int nb_sectors) |
| 1938 | { |
| 1939 | int n, ret; |
| 1940 | |
| 1941 | while (nb_sectors > 0) { |
| 1942 | BlockBackend *blk; |
| 1943 | int src_cur; |
| 1944 | int64_t bs_sectors, src_cur_offset; |
| 1945 | int64_t offset; |
| 1946 | |
| 1947 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
| 1948 | offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS; |
| 1949 | blk = s->src[src_cur]; |
| 1950 | bs_sectors = s->src_sectors[src_cur]; |
| 1951 | |
| 1952 | n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset)); |
| 1953 | |
| 1954 | ret = blk_co_copy_range(blk, offset, s->target, |
| 1955 | sector_num << BDRV_SECTOR_BITS, |
Vladimir Sementsov-Ogievskiy | 67b51fb | 2018-07-09 19:37:17 +0300 | [diff] [blame] | 1956 | n << BDRV_SECTOR_BITS, 0, 0); |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1957 | if (ret < 0) { |
| 1958 | return ret; |
| 1959 | } |
| 1960 | |
| 1961 | sector_num += n; |
| 1962 | nb_sectors -= n; |
| 1963 | } |
| 1964 | return 0; |
| 1965 | } |
| 1966 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1967 | static void coroutine_fn convert_co_do_copy(void *opaque) |
| 1968 | { |
| 1969 | ImgConvertState *s = opaque; |
| 1970 | uint8_t *buf = NULL; |
| 1971 | int ret, i; |
| 1972 | int index = -1; |
| 1973 | |
| 1974 | for (i = 0; i < s->num_coroutines; i++) { |
| 1975 | if (s->co[i] == qemu_coroutine_self()) { |
| 1976 | index = i; |
| 1977 | break; |
| 1978 | } |
| 1979 | } |
| 1980 | assert(index >= 0); |
| 1981 | |
| 1982 | s->running_coroutines++; |
| 1983 | buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE); |
| 1984 | |
| 1985 | while (1) { |
| 1986 | int n; |
| 1987 | int64_t sector_num; |
| 1988 | enum ImgConvertBlockStatus status; |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1989 | bool copy_range; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1990 | |
| 1991 | qemu_co_mutex_lock(&s->lock); |
| 1992 | if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) { |
| 1993 | qemu_co_mutex_unlock(&s->lock); |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1994 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1995 | } |
| 1996 | n = convert_iteration_sectors(s, s->sector_num); |
| 1997 | if (n < 0) { |
| 1998 | qemu_co_mutex_unlock(&s->lock); |
| 1999 | s->ret = n; |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2000 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2001 | } |
| 2002 | /* save current sector and allocation status to local variables */ |
| 2003 | sector_num = s->sector_num; |
| 2004 | status = s->status; |
| 2005 | if (!s->min_sparse && s->status == BLK_ZERO) { |
| 2006 | n = MIN(n, s->buf_sectors); |
| 2007 | } |
| 2008 | /* increment global sector counter so that other coroutines can |
| 2009 | * already continue reading beyond this request */ |
| 2010 | s->sector_num += n; |
| 2011 | qemu_co_mutex_unlock(&s->lock); |
| 2012 | |
| 2013 | if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) { |
| 2014 | s->allocated_done += n; |
| 2015 | qemu_progress_print(100.0 * s->allocated_done / |
| 2016 | s->allocated_sectors, 0); |
| 2017 | } |
| 2018 | |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 2019 | retry: |
| 2020 | copy_range = s->copy_range && s->status == BLK_DATA; |
| 2021 | if (status == BLK_DATA && !copy_range) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2022 | ret = convert_co_read(s, sector_num, n, buf); |
| 2023 | if (ret < 0) { |
Eric Blake | 39f77cb | 2020-04-02 08:57:17 -0500 | [diff] [blame] | 2024 | error_report("error while reading at byte %lld: %s", |
| 2025 | sector_num * BDRV_SECTOR_SIZE, strerror(-ret)); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2026 | s->ret = ret; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2027 | } |
| 2028 | } else if (!s->min_sparse && status == BLK_ZERO) { |
| 2029 | status = BLK_DATA; |
| 2030 | memset(buf, 0x00, n * BDRV_SECTOR_SIZE); |
| 2031 | } |
| 2032 | |
| 2033 | if (s->wr_in_order) { |
| 2034 | /* keep writes in order */ |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2035 | while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2036 | s->wait_sector_num[index] = sector_num; |
| 2037 | qemu_coroutine_yield(); |
| 2038 | } |
| 2039 | s->wait_sector_num[index] = -1; |
| 2040 | } |
| 2041 | |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2042 | if (s->ret == -EINPROGRESS) { |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 2043 | if (copy_range) { |
| 2044 | ret = convert_co_copy_range(s, sector_num, n); |
| 2045 | if (ret) { |
| 2046 | s->copy_range = false; |
| 2047 | goto retry; |
| 2048 | } |
| 2049 | } else { |
| 2050 | ret = convert_co_write(s, sector_num, n, buf, status); |
| 2051 | } |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2052 | if (ret < 0) { |
Eric Blake | 39f77cb | 2020-04-02 08:57:17 -0500 | [diff] [blame] | 2053 | error_report("error while writing at byte %lld: %s", |
| 2054 | sector_num * BDRV_SECTOR_SIZE, strerror(-ret)); |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2055 | s->ret = ret; |
| 2056 | } |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | if (s->wr_in_order) { |
| 2060 | /* reenter the coroutine that might have waited |
| 2061 | * for this write to complete */ |
| 2062 | s->wr_offs = sector_num + n; |
| 2063 | for (i = 0; i < s->num_coroutines; i++) { |
| 2064 | if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) { |
| 2065 | /* |
| 2066 | * A -> B -> A cannot occur because A has |
| 2067 | * s->wait_sector_num[i] == -1 during A -> B. Therefore |
| 2068 | * B will never enter A during this time window. |
| 2069 | */ |
| 2070 | qemu_coroutine_enter(s->co[i]); |
| 2071 | break; |
| 2072 | } |
| 2073 | } |
| 2074 | } |
| 2075 | } |
| 2076 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2077 | qemu_vfree(buf); |
| 2078 | s->co[index] = NULL; |
| 2079 | s->running_coroutines--; |
| 2080 | if (!s->running_coroutines && s->ret == -EINPROGRESS) { |
| 2081 | /* the convert job finished successfully */ |
| 2082 | s->ret = 0; |
| 2083 | } |
| 2084 | } |
| 2085 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2086 | static int convert_do_copy(ImgConvertState *s) |
| 2087 | { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2088 | int ret, i, n; |
| 2089 | int64_t sector_num = 0; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2090 | |
| 2091 | /* Check whether we have zero initialisation or can get it efficiently */ |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2092 | if (!s->has_zero_init && s->target_is_new && s->min_sparse && |
| 2093 | !s->target_has_backing) { |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 2094 | s->has_zero_init = bdrv_has_zero_init(blk_bs(s->target)); |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 2095 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2096 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2097 | /* Allocate buffer for copied data. For compressed images, only one cluster |
| 2098 | * can be copied at a time. */ |
| 2099 | if (s->compressed) { |
| 2100 | if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) { |
| 2101 | error_report("invalid cluster size"); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2102 | return -EINVAL; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2103 | } |
| 2104 | s->buf_sectors = s->cluster_sectors; |
| 2105 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2106 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2107 | while (sector_num < s->total_sectors) { |
| 2108 | n = convert_iteration_sectors(s, sector_num); |
| 2109 | if (n < 0) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2110 | return n; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2111 | } |
Max Reitz | aad15de | 2016-03-24 23:33:57 +0100 | [diff] [blame] | 2112 | if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO)) |
| 2113 | { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2114 | s->allocated_sectors += n; |
| 2115 | } |
| 2116 | sector_num += n; |
| 2117 | } |
| 2118 | |
| 2119 | /* Do the copy */ |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2120 | s->sector_next_status = 0; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2121 | s->ret = -EINPROGRESS; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2122 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2123 | qemu_co_mutex_init(&s->lock); |
| 2124 | for (i = 0; i < s->num_coroutines; i++) { |
| 2125 | s->co[i] = qemu_coroutine_create(convert_co_do_copy, s); |
| 2126 | s->wait_sector_num[i] = -1; |
| 2127 | qemu_coroutine_enter(s->co[i]); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2128 | } |
| 2129 | |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2130 | while (s->running_coroutines) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2131 | main_loop_wait(false); |
| 2132 | } |
| 2133 | |
| 2134 | if (s->compressed && !s->ret) { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2135 | /* signal EOF to align */ |
Pavel Butsykin | fe5c135 | 2016-07-22 11:17:40 +0300 | [diff] [blame] | 2136 | ret = blk_pwrite_compressed(s->target, 0, NULL, 0); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2137 | if (ret < 0) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2138 | return ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2139 | } |
| 2140 | } |
| 2141 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2142 | return s->ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2143 | } |
| 2144 | |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2145 | static int convert_copy_bitmaps(BlockDriverState *src, BlockDriverState *dst) |
| 2146 | { |
| 2147 | BdrvDirtyBitmap *bm; |
| 2148 | Error *err = NULL; |
| 2149 | |
| 2150 | FOR_EACH_DIRTY_BITMAP(src, bm) { |
| 2151 | const char *name; |
| 2152 | |
| 2153 | if (!bdrv_dirty_bitmap_get_persistence(bm)) { |
| 2154 | continue; |
| 2155 | } |
| 2156 | name = bdrv_dirty_bitmap_name(bm); |
| 2157 | qmp_block_dirty_bitmap_add(dst->node_name, name, |
| 2158 | true, bdrv_dirty_bitmap_granularity(bm), |
| 2159 | true, true, |
| 2160 | true, !bdrv_dirty_bitmap_enabled(bm), |
| 2161 | &err); |
| 2162 | if (err) { |
| 2163 | error_reportf_err(err, "Failed to create bitmap %s: ", name); |
| 2164 | return -1; |
| 2165 | } |
| 2166 | |
| 2167 | do_dirty_bitmap_merge(dst->node_name, name, src->node_name, name, |
| 2168 | &err); |
| 2169 | if (err) { |
| 2170 | error_reportf_err(err, "Failed to populate bitmap %s: ", name); |
| 2171 | return -1; |
| 2172 | } |
| 2173 | } |
| 2174 | |
| 2175 | return 0; |
| 2176 | } |
| 2177 | |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 2178 | #define MAX_BUF_SECTORS 32768 |
| 2179 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2180 | static int img_convert(int argc, char **argv) |
| 2181 | { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2182 | int c, bs_i, flags, src_flags = 0; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2183 | const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe", |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2184 | *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL, |
| 2185 | *out_filename, *out_baseimg_param, *snapshot_name = NULL; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2186 | BlockDriver *drv = NULL, *proto_drv = NULL; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2187 | BlockDriverInfo bdi; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2188 | BlockDriverState *out_bs; |
| 2189 | QemuOpts *opts = NULL, *sn_opts = NULL; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2190 | QemuOptsList *create_opts = NULL; |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2191 | QDict *open_opts = NULL; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2192 | char *options = NULL; |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 2193 | Error *local_err = NULL; |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2194 | bool writethrough, src_writethrough, image_opts = false, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2195 | skip_create = false, progress = false, tgt_image_opts = false; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2196 | int64_t ret = -EINVAL; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2197 | bool force_share = false; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2198 | bool explict_min_sparse = false; |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2199 | bool bitmaps = false; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2200 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2201 | ImgConvertState s = (ImgConvertState) { |
| 2202 | /* Need at least 4k of zeros for sparse detection */ |
| 2203 | .min_sparse = 8, |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2204 | .copy_range = false, |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2205 | .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE, |
| 2206 | .wr_in_order = true, |
| 2207 | .num_coroutines = 8, |
| 2208 | }; |
| 2209 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2210 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2211 | static const struct option long_options[] = { |
| 2212 | {"help", no_argument, 0, 'h'}, |
| 2213 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2214 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2215 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2216 | {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS}, |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 2217 | {"salvage", no_argument, 0, OPTION_SALVAGE}, |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2218 | {"target-is-zero", no_argument, 0, OPTION_TARGET_IS_ZERO}, |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2219 | {"bitmaps", no_argument, 0, OPTION_BITMAPS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2220 | {0, 0, 0, 0} |
| 2221 | }; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2222 | c = getopt_long(argc, argv, ":hf:O:B:Cco:l:S:pt:T:qnm:WU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2223 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2224 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2225 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2226 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2227 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2228 | case ':': |
| 2229 | missing_argument(argv[optind - 1]); |
| 2230 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2231 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2232 | unrecognized_option(argv[optind - 1]); |
| 2233 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2234 | case 'h': |
| 2235 | help(); |
| 2236 | break; |
| 2237 | case 'f': |
| 2238 | fmt = optarg; |
| 2239 | break; |
| 2240 | case 'O': |
| 2241 | out_fmt = optarg; |
| 2242 | break; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 2243 | case 'B': |
| 2244 | out_baseimg = optarg; |
| 2245 | break; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2246 | case 'C': |
| 2247 | s.copy_range = true; |
| 2248 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2249 | case 'c': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2250 | s.compressed = true; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2251 | break; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2252 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 2253 | if (accumulate_options(&options, optarg) < 0) { |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2254 | goto fail_getopt; |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 2255 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2256 | break; |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2257 | case 'l': |
| 2258 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { |
Markus Armbruster | 70b9433 | 2015-02-13 12:50:26 +0100 | [diff] [blame] | 2259 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
| 2260 | optarg, false); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2261 | if (!sn_opts) { |
| 2262 | error_report("Failed in parsing snapshot param '%s'", |
| 2263 | optarg); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2264 | goto fail_getopt; |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2265 | } |
| 2266 | } else { |
| 2267 | snapshot_name = optarg; |
| 2268 | } |
| 2269 | break; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2270 | case 'S': |
| 2271 | { |
| 2272 | int64_t sval; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 2273 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 2274 | sval = cvtnum("buffer size for sparse output", optarg); |
| 2275 | if (sval < 0) { |
| 2276 | goto fail_getopt; |
| 2277 | } else if (!QEMU_IS_ALIGNED(sval, BDRV_SECTOR_SIZE) || |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 2278 | sval / BDRV_SECTOR_SIZE > MAX_BUF_SECTORS) { |
| 2279 | error_report("Invalid buffer size for sparse output specified. " |
| 2280 | "Valid sizes are multiples of %llu up to %llu. Select " |
| 2281 | "0 to disable sparse detection (fully allocates output).", |
| 2282 | BDRV_SECTOR_SIZE, MAX_BUF_SECTORS * BDRV_SECTOR_SIZE); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2283 | goto fail_getopt; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2284 | } |
| 2285 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2286 | s.min_sparse = sval / BDRV_SECTOR_SIZE; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2287 | explict_min_sparse = true; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2288 | break; |
| 2289 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2290 | case 'p': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2291 | progress = true; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2292 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2293 | case 't': |
| 2294 | cache = optarg; |
| 2295 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 2296 | case 'T': |
| 2297 | src_cache = optarg; |
| 2298 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2299 | case 'q': |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2300 | s.quiet = true; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2301 | break; |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2302 | case 'n': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2303 | skip_create = true; |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2304 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2305 | case 'm': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2306 | if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) || |
| 2307 | s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2308 | error_report("Invalid number of coroutines. Allowed number of" |
| 2309 | " coroutines is between 1 and %d", MAX_COROUTINES); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2310 | goto fail_getopt; |
| 2311 | } |
| 2312 | break; |
| 2313 | case 'W': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2314 | s.wr_in_order = false; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2315 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2316 | case 'U': |
| 2317 | force_share = true; |
| 2318 | break; |
Max Reitz | 3258b91 | 2017-04-26 15:46:47 +0200 | [diff] [blame] | 2319 | case OPTION_OBJECT: { |
| 2320 | QemuOpts *object_opts; |
| 2321 | object_opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 2322 | optarg, true); |
| 2323 | if (!object_opts) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2324 | goto fail_getopt; |
| 2325 | } |
| 2326 | break; |
Max Reitz | 3258b91 | 2017-04-26 15:46:47 +0200 | [diff] [blame] | 2327 | } |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2328 | case OPTION_IMAGE_OPTS: |
| 2329 | image_opts = true; |
| 2330 | break; |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 2331 | case OPTION_SALVAGE: |
| 2332 | s.salvage = true; |
| 2333 | break; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2334 | case OPTION_TARGET_IMAGE_OPTS: |
| 2335 | tgt_image_opts = true; |
| 2336 | break; |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2337 | case OPTION_TARGET_IS_ZERO: |
| 2338 | /* |
| 2339 | * The user asserting that the target is blank has the |
| 2340 | * same effect as the target driver supporting zero |
| 2341 | * initialisation. |
| 2342 | */ |
| 2343 | s.has_zero_init = true; |
| 2344 | break; |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2345 | case OPTION_BITMAPS: |
| 2346 | bitmaps = true; |
| 2347 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2348 | } |
| 2349 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2350 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2351 | if (!out_fmt && !tgt_image_opts) { |
| 2352 | out_fmt = "raw"; |
| 2353 | } |
| 2354 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2355 | if (qemu_opts_foreach(&qemu_object_opts, |
| 2356 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 2357 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2358 | goto fail_getopt; |
| 2359 | } |
| 2360 | |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2361 | if (s.compressed && s.copy_range) { |
| 2362 | error_report("Cannot enable copy offloading when -c is used"); |
| 2363 | goto fail_getopt; |
| 2364 | } |
| 2365 | |
| 2366 | if (explict_min_sparse && s.copy_range) { |
| 2367 | error_report("Cannot enable copy offloading when -S is used"); |
| 2368 | goto fail_getopt; |
| 2369 | } |
| 2370 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 2371 | if (s.copy_range && s.salvage) { |
| 2372 | error_report("Cannot use copy offloading in salvaging mode"); |
| 2373 | goto fail_getopt; |
| 2374 | } |
| 2375 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2376 | if (tgt_image_opts && !skip_create) { |
| 2377 | error_report("--target-image-opts requires use of -n flag"); |
| 2378 | goto fail_getopt; |
| 2379 | } |
| 2380 | |
Kevin Wolf | ffd8e8f | 2019-08-09 11:09:21 +0200 | [diff] [blame] | 2381 | if (skip_create && options) { |
Eric Blake | 25956af | 2020-07-06 15:39:46 -0500 | [diff] [blame] | 2382 | error_report("-o has no effect when skipping image creation"); |
| 2383 | goto fail_getopt; |
Kevin Wolf | ffd8e8f | 2019-08-09 11:09:21 +0200 | [diff] [blame] | 2384 | } |
| 2385 | |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2386 | if (s.has_zero_init && !skip_create) { |
| 2387 | error_report("--target-is-zero requires use of -n flag"); |
| 2388 | goto fail_getopt; |
| 2389 | } |
| 2390 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2391 | s.src_num = argc - optind - 1; |
| 2392 | out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL; |
| 2393 | |
| 2394 | if (options && has_help_option(options)) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2395 | if (out_fmt) { |
| 2396 | ret = print_block_option_help(out_filename, out_fmt); |
| 2397 | goto fail_getopt; |
| 2398 | } else { |
| 2399 | error_report("Option help requires a format be specified"); |
| 2400 | goto fail_getopt; |
| 2401 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2402 | } |
| 2403 | |
| 2404 | if (s.src_num < 1) { |
| 2405 | error_report("Must specify image file name"); |
| 2406 | goto fail_getopt; |
| 2407 | } |
| 2408 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2409 | /* ret is still -EINVAL until here */ |
| 2410 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); |
| 2411 | if (ret < 0) { |
| 2412 | error_report("Invalid source cache option: %s", src_cache); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2413 | goto fail_getopt; |
| 2414 | } |
| 2415 | |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2416 | /* Initialize before goto out */ |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2417 | if (s.quiet) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2418 | progress = false; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2419 | } |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2420 | qemu_progress_init(progress, 1.0); |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2421 | qemu_progress_print(0, 100); |
| 2422 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2423 | s.src = g_new0(BlockBackend *, s.src_num); |
| 2424 | s.src_sectors = g_new(int64_t, s.src_num); |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 2425 | s.src_alignment = g_new(int, s.src_num); |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 2426 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2427 | for (bs_i = 0; bs_i < s.src_num; bs_i++) { |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 2428 | BlockDriverState *src_bs; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2429 | s.src[bs_i] = img_open(image_opts, argv[optind + bs_i], |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2430 | fmt, src_flags, src_writethrough, s.quiet, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2431 | force_share); |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2432 | if (!s.src[bs_i]) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2433 | ret = -1; |
| 2434 | goto out; |
| 2435 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2436 | s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]); |
| 2437 | if (s.src_sectors[bs_i] < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2438 | error_report("Could not get size of %s: %s", |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2439 | argv[optind + bs_i], strerror(-s.src_sectors[bs_i])); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2440 | ret = -1; |
| 2441 | goto out; |
| 2442 | } |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 2443 | src_bs = blk_bs(s.src[bs_i]); |
| 2444 | s.src_alignment[bs_i] = DIV_ROUND_UP(src_bs->bl.request_alignment, |
| 2445 | BDRV_SECTOR_SIZE); |
| 2446 | if (!bdrv_get_info(src_bs, &bdi)) { |
| 2447 | s.src_alignment[bs_i] = MAX(s.src_alignment[bs_i], |
| 2448 | bdi.cluster_size / BDRV_SECTOR_SIZE); |
| 2449 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2450 | s.total_sectors += s.src_sectors[bs_i]; |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 2451 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2452 | |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2453 | if (sn_opts) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2454 | bdrv_snapshot_load_tmp(blk_bs(s.src[0]), |
Peter Maydell | 10d6eda | 2017-02-10 16:28:24 +0000 | [diff] [blame] | 2455 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), |
| 2456 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), |
| 2457 | &local_err); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2458 | } else if (snapshot_name != NULL) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2459 | if (s.src_num > 1) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 2460 | error_report("No support for concatenating multiple snapshot"); |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 2461 | ret = -1; |
| 2462 | goto out; |
| 2463 | } |
Wenchao Xia | 7b4c478 | 2013-12-04 17:10:54 +0800 | [diff] [blame] | 2464 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2465 | bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name, |
| 2466 | &local_err); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2467 | } |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2468 | if (local_err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2469 | error_reportf_err(local_err, "Failed to load snapshot: "); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2470 | ret = -1; |
| 2471 | goto out; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 2472 | } |
| 2473 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2474 | if (!skip_create) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2475 | /* Find driver and parse its options */ |
| 2476 | drv = bdrv_find_format(out_fmt); |
| 2477 | if (!drv) { |
| 2478 | error_report("Unknown file format '%s'", out_fmt); |
| 2479 | ret = -1; |
| 2480 | goto out; |
| 2481 | } |
| 2482 | |
| 2483 | proto_drv = bdrv_find_protocol(out_filename, true, &local_err); |
| 2484 | if (!proto_drv) { |
| 2485 | error_report_err(local_err); |
| 2486 | ret = -1; |
| 2487 | goto out; |
| 2488 | } |
| 2489 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2490 | if (!drv->create_opts) { |
| 2491 | error_report("Format driver '%s' does not support image creation", |
| 2492 | drv->format_name); |
| 2493 | ret = -1; |
| 2494 | goto out; |
| 2495 | } |
Max Reitz | f75613c | 2014-12-02 18:32:46 +0100 | [diff] [blame] | 2496 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2497 | if (!proto_drv->create_opts) { |
| 2498 | error_report("Protocol driver '%s' does not support image creation", |
| 2499 | proto_drv->format_name); |
| 2500 | ret = -1; |
| 2501 | goto out; |
| 2502 | } |
Max Reitz | f75613c | 2014-12-02 18:32:46 +0100 | [diff] [blame] | 2503 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2504 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 2505 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
Kevin Wolf | db08adf | 2009-06-04 15:39:38 +0200 | [diff] [blame] | 2506 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2507 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 2508 | if (options) { |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 2509 | if (!qemu_opts_do_parse(opts, options, NULL, &local_err)) { |
Markus Armbruster | 97a2ca7 | 2015-03-14 10:23:15 +0100 | [diff] [blame] | 2510 | error_report_err(local_err); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 2511 | ret = -1; |
| 2512 | goto out; |
| 2513 | } |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2514 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2515 | |
Yi Li | c075c42 | 2020-08-19 09:36:07 +0800 | [diff] [blame] | 2516 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, |
| 2517 | s.total_sectors * BDRV_SECTOR_SIZE, &error_abort); |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2518 | ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL); |
| 2519 | if (ret < 0) { |
| 2520 | goto out; |
| 2521 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2522 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2523 | |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2524 | /* Get backing file name if -o backing_file was used */ |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2525 | out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2526 | if (out_baseimg_param) { |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2527 | out_baseimg = out_baseimg_param; |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2528 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2529 | s.target_has_backing = (bool) out_baseimg; |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2530 | |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2531 | if (s.has_zero_init && s.target_has_backing) { |
| 2532 | error_report("Cannot use --target-is-zero when the destination " |
| 2533 | "image has a backing file"); |
| 2534 | goto out; |
| 2535 | } |
| 2536 | |
Max Reitz | 48758a8 | 2017-04-26 15:46:48 +0200 | [diff] [blame] | 2537 | if (s.src_num > 1 && out_baseimg) { |
| 2538 | error_report("Having a backing file for the target makes no sense when " |
| 2539 | "concatenating multiple input images"); |
| 2540 | ret = -1; |
| 2541 | goto out; |
| 2542 | } |
| 2543 | |
Eric Blake | d9f059a | 2020-07-06 15:39:54 -0500 | [diff] [blame] | 2544 | if (out_baseimg_param) { |
| 2545 | if (!qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT)) { |
| 2546 | warn_report("Deprecated use of backing file without explicit " |
| 2547 | "backing format"); |
| 2548 | } |
| 2549 | } |
| 2550 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2551 | /* Check if compression is supported */ |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2552 | if (s.compressed) { |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2553 | bool encryption = |
| 2554 | qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false); |
Daniel P. Berrange | 0cb8d47 | 2017-06-23 17:24:06 +0100 | [diff] [blame] | 2555 | const char *encryptfmt = |
| 2556 | qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2557 | const char *preallocation = |
| 2558 | qemu_opt_get(opts, BLOCK_OPT_PREALLOC); |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2559 | |
Vladimir Sementsov-Ogievskiy | ac850bf | 2019-06-04 19:15:06 +0300 | [diff] [blame] | 2560 | if (drv && !block_driver_can_compress(drv)) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2561 | error_report("Compression not supported for this file format"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2562 | ret = -1; |
| 2563 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2564 | } |
| 2565 | |
Daniel P. Berrange | 0cb8d47 | 2017-06-23 17:24:06 +0100 | [diff] [blame] | 2566 | if (encryption || encryptfmt) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2567 | error_report("Compression and encryption not supported at " |
| 2568 | "the same time"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2569 | ret = -1; |
| 2570 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2571 | } |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 2572 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2573 | if (preallocation |
| 2574 | && strcmp(preallocation, "off")) |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 2575 | { |
| 2576 | error_report("Compression and preallocation not supported at " |
| 2577 | "the same time"); |
| 2578 | ret = -1; |
| 2579 | goto out; |
| 2580 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2581 | } |
| 2582 | |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2583 | /* Determine if bitmaps need copying */ |
| 2584 | if (bitmaps) { |
| 2585 | if (s.src_num > 1) { |
| 2586 | error_report("Copying bitmaps only possible with single source"); |
| 2587 | ret = -1; |
| 2588 | goto out; |
| 2589 | } |
| 2590 | if (!bdrv_supports_persistent_dirty_bitmap(blk_bs(s.src[0]))) { |
| 2591 | error_report("Source lacks bitmap support"); |
| 2592 | ret = -1; |
| 2593 | goto out; |
| 2594 | } |
| 2595 | } |
| 2596 | |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2597 | /* |
| 2598 | * The later open call will need any decryption secrets, and |
| 2599 | * bdrv_create() will purge "opts", so extract them now before |
| 2600 | * they are lost. |
| 2601 | */ |
| 2602 | if (!skip_create) { |
| 2603 | open_opts = qdict_new(); |
| 2604 | 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] | 2605 | |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2606 | /* Create the new image */ |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 2607 | ret = bdrv_create(drv, out_filename, opts, &local_err); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2608 | if (ret < 0) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2609 | error_reportf_err(local_err, "%s: error while converting %s: ", |
| 2610 | out_filename, out_fmt); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2611 | goto out; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2612 | } |
| 2613 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2614 | |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 2615 | s.target_is_new = !skip_create; |
| 2616 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2617 | 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] | 2618 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2619 | if (ret < 0) { |
| 2620 | error_report("Invalid cache option: %s", cache); |
Markus Armbruster | bb9cd2e | 2014-05-28 11:17:07 +0200 | [diff] [blame] | 2621 | goto out; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2622 | } |
| 2623 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2624 | if (skip_create) { |
| 2625 | s.target = img_open(tgt_image_opts, out_filename, out_fmt, |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2626 | flags, writethrough, s.quiet, false); |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2627 | } else { |
| 2628 | /* TODO ultimately we should allow --target-image-opts |
| 2629 | * to be used even when -n is not given. |
| 2630 | * That has to wait for bdrv_create to be improved |
| 2631 | * to allow filenames in option syntax |
| 2632 | */ |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2633 | s.target = img_open_file(out_filename, open_opts, out_fmt, |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2634 | flags, writethrough, s.quiet, false); |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2635 | open_opts = NULL; /* blk_new_open will have freed it */ |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2636 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2637 | if (!s.target) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2638 | ret = -1; |
| 2639 | goto out; |
| 2640 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2641 | out_bs = blk_bs(s.target); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2642 | |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2643 | if (bitmaps && !bdrv_supports_persistent_dirty_bitmap(out_bs)) { |
| 2644 | error_report("Format driver '%s' does not support bitmaps", |
| 2645 | out_bs->drv->format_name); |
| 2646 | ret = -1; |
| 2647 | goto out; |
| 2648 | } |
| 2649 | |
Vladimir Sementsov-Ogievskiy | ac850bf | 2019-06-04 19:15:06 +0300 | [diff] [blame] | 2650 | if (s.compressed && !block_driver_can_compress(out_bs->drv)) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2651 | error_report("Compression not supported for this file format"); |
| 2652 | ret = -1; |
| 2653 | goto out; |
| 2654 | } |
| 2655 | |
Eric Blake | 5def6b8 | 2016-06-23 16:37:19 -0600 | [diff] [blame] | 2656 | /* increase bufsectors from the default 4096 (2M) if opt_transfer |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 2657 | * or discard_alignment of the out_bs is greater. Limit to |
| 2658 | * MAX_BUF_SECTORS as maximum which is currently 32768 (16MB). */ |
| 2659 | s.buf_sectors = MIN(MAX_BUF_SECTORS, |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2660 | MAX(s.buf_sectors, |
| 2661 | MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS, |
| 2662 | out_bs->bl.pdiscard_alignment >> |
| 2663 | BDRV_SECTOR_BITS))); |
Peter Lieven | f2521c9 | 2013-11-27 11:07:06 +0100 | [diff] [blame] | 2664 | |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 2665 | /* try to align the write requests to the destination to avoid unnecessary |
| 2666 | * RMW cycles. */ |
| 2667 | s.alignment = MAX(pow2floor(s.min_sparse), |
| 2668 | DIV_ROUND_UP(out_bs->bl.request_alignment, |
| 2669 | BDRV_SECTOR_SIZE)); |
| 2670 | assert(is_power_of_2(s.alignment)); |
| 2671 | |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2672 | if (skip_create) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2673 | int64_t output_sectors = blk_nb_sectors(s.target); |
Markus Armbruster | 43716fa | 2014-06-26 13:23:21 +0200 | [diff] [blame] | 2674 | if (output_sectors < 0) { |
Gonglei | eec5eb4 | 2015-02-25 12:22:27 +0800 | [diff] [blame] | 2675 | error_report("unable to get output image length: %s", |
Markus Armbruster | 43716fa | 2014-06-26 13:23:21 +0200 | [diff] [blame] | 2676 | strerror(-output_sectors)); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2677 | ret = -1; |
| 2678 | goto out; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2679 | } else if (output_sectors < s.total_sectors) { |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2680 | error_report("output file is smaller than input file"); |
| 2681 | ret = -1; |
| 2682 | goto out; |
| 2683 | } |
| 2684 | } |
| 2685 | |
Max Reitz | c69291e | 2020-01-21 16:59:14 +0100 | [diff] [blame] | 2686 | if (s.target_has_backing && s.target_is_new) { |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 2687 | /* Errors are treated as "backing length unknown" (which means |
| 2688 | * s.target_backing_sectors has to be negative, which it will |
| 2689 | * be automatically). The backing file length is used only |
| 2690 | * for optimizations, so such a case is not fatal. */ |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 2691 | s.target_backing_sectors = |
| 2692 | bdrv_nb_sectors(bdrv_backing_chain_next(out_bs)); |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 2693 | } else { |
| 2694 | s.target_backing_sectors = -1; |
| 2695 | } |
| 2696 | |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2697 | ret = bdrv_get_info(out_bs, &bdi); |
| 2698 | if (ret < 0) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2699 | if (s.compressed) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2700 | error_report("could not get block driver info"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2701 | goto out; |
| 2702 | } |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2703 | } else { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2704 | s.compressed = s.compressed || bdi.needs_compressed_writes; |
| 2705 | s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE; |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2706 | } |
| 2707 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2708 | ret = convert_do_copy(&s); |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2709 | |
| 2710 | /* Now copy the bitmaps */ |
| 2711 | if (bitmaps && ret == 0) { |
| 2712 | ret = convert_copy_bitmaps(blk_bs(s.src[0]), out_bs); |
| 2713 | } |
| 2714 | |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2715 | out: |
Peter Lieven | 13c28af | 2013-11-27 11:07:01 +0100 | [diff] [blame] | 2716 | if (!ret) { |
| 2717 | qemu_progress_print(100, 0); |
| 2718 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2719 | qemu_progress_end(); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2720 | qemu_opts_del(opts); |
| 2721 | qemu_opts_free(create_opts); |
Markus Armbruster | fbf28a4 | 2014-09-29 16:07:55 +0200 | [diff] [blame] | 2722 | qemu_opts_del(sn_opts); |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2723 | qobject_unref(open_opts); |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2724 | blk_unref(s.target); |
| 2725 | if (s.src) { |
| 2726 | for (bs_i = 0; bs_i < s.src_num; bs_i++) { |
| 2727 | blk_unref(s.src[bs_i]); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2728 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2729 | g_free(s.src); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2730 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2731 | g_free(s.src_sectors); |
Peter Lieven | af8d43d | 2020-09-01 14:51:29 +0200 | [diff] [blame] | 2732 | g_free(s.src_alignment); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2733 | fail_getopt: |
| 2734 | g_free(options); |
| 2735 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2736 | return !!ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
bellard | 57d1a2b | 2004-08-03 21:15:11 +0000 | [diff] [blame] | 2739 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2740 | static void dump_snapshots(BlockDriverState *bs) |
| 2741 | { |
| 2742 | QEMUSnapshotInfo *sn_tab, *sn; |
| 2743 | int nb_sns, i; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2744 | |
| 2745 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 2746 | if (nb_sns <= 0) |
| 2747 | return; |
| 2748 | printf("Snapshot list:\n"); |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2749 | bdrv_snapshot_dump(NULL); |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2750 | printf("\n"); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2751 | for(i = 0; i < nb_sns; i++) { |
| 2752 | sn = &sn_tab[i]; |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2753 | bdrv_snapshot_dump(sn); |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2754 | printf("\n"); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2755 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2756 | g_free(sn_tab); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2757 | } |
| 2758 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2759 | static void dump_json_image_info_list(ImageInfoList *list) |
| 2760 | { |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2761 | QString *str; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2762 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 2763 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2764 | |
| 2765 | visit_type_ImageInfoList(v, NULL, &list, &error_abort); |
| 2766 | visit_complete(v, &obj); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2767 | str = qobject_to_json_pretty(obj); |
| 2768 | assert(str != NULL); |
| 2769 | printf("%s\n", qstring_get_str(str)); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2770 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2771 | visit_free(v); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2772 | qobject_unref(str); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2773 | } |
| 2774 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2775 | static void dump_json_image_info(ImageInfo *info) |
| 2776 | { |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2777 | QString *str; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2778 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 2779 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2780 | |
| 2781 | visit_type_ImageInfo(v, NULL, &info, &error_abort); |
| 2782 | visit_complete(v, &obj); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2783 | str = qobject_to_json_pretty(obj); |
| 2784 | assert(str != NULL); |
| 2785 | printf("%s\n", qstring_get_str(str)); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2786 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2787 | visit_free(v); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2788 | qobject_unref(str); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2789 | } |
| 2790 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2791 | static void dump_human_image_info_list(ImageInfoList *list) |
| 2792 | { |
| 2793 | ImageInfoList *elem; |
| 2794 | bool delim = false; |
| 2795 | |
| 2796 | for (elem = list; elem; elem = elem->next) { |
| 2797 | if (delim) { |
| 2798 | printf("\n"); |
| 2799 | } |
| 2800 | delim = true; |
| 2801 | |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2802 | bdrv_image_info_dump(elem->value); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2803 | } |
| 2804 | } |
| 2805 | |
| 2806 | static gboolean str_equal_func(gconstpointer a, gconstpointer b) |
| 2807 | { |
| 2808 | return strcmp(a, b) == 0; |
| 2809 | } |
| 2810 | |
| 2811 | /** |
| 2812 | * Open an image file chain and return an ImageInfoList |
| 2813 | * |
| 2814 | * @filename: topmost image filename |
| 2815 | * @fmt: topmost image format (may be NULL to autodetect) |
| 2816 | * @chain: true - enumerate entire backing file chain |
| 2817 | * false - only topmost image file |
| 2818 | * |
| 2819 | * Returns a list of ImageInfo objects or NULL if there was an error opening an |
| 2820 | * image file. If there was an error a message will have been printed to |
| 2821 | * stderr. |
| 2822 | */ |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2823 | static ImageInfoList *collect_image_info_list(bool image_opts, |
| 2824 | const char *filename, |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2825 | const char *fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2826 | bool chain, bool force_share) |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2827 | { |
| 2828 | ImageInfoList *head = NULL; |
| 2829 | ImageInfoList **last = &head; |
| 2830 | GHashTable *filenames; |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2831 | Error *err = NULL; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2832 | |
| 2833 | filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL); |
| 2834 | |
| 2835 | while (filename) { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2836 | BlockBackend *blk; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2837 | BlockDriverState *bs; |
| 2838 | ImageInfo *info; |
| 2839 | ImageInfoList *elem; |
| 2840 | |
| 2841 | if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) { |
| 2842 | error_report("Backing file '%s' creates an infinite loop.", |
| 2843 | filename); |
| 2844 | goto err; |
| 2845 | } |
| 2846 | g_hash_table_insert(filenames, (gpointer)filename, NULL); |
| 2847 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 2848 | blk = img_open(image_opts, filename, fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2849 | BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false, |
| 2850 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2851 | if (!blk) { |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2852 | goto err; |
| 2853 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2854 | bs = blk_bs(blk); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2855 | |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2856 | bdrv_query_image_info(bs, &info, &err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2857 | if (err) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 2858 | error_report_err(err); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2859 | blk_unref(blk); |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2860 | goto err; |
Wenchao Xia | fb0ed45 | 2013-06-06 12:27:57 +0800 | [diff] [blame] | 2861 | } |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2862 | |
| 2863 | elem = g_new0(ImageInfoList, 1); |
| 2864 | elem->value = info; |
| 2865 | *last = elem; |
| 2866 | last = &elem->next; |
| 2867 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2868 | blk_unref(blk); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2869 | |
Stefan Hajnoczi | 0da7d13 | 2019-12-05 13:46:46 +0000 | [diff] [blame] | 2870 | /* Clear parameters that only apply to the topmost image */ |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2871 | filename = fmt = NULL; |
Stefan Hajnoczi | 0da7d13 | 2019-12-05 13:46:46 +0000 | [diff] [blame] | 2872 | image_opts = false; |
| 2873 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2874 | if (chain) { |
| 2875 | if (info->has_full_backing_filename) { |
| 2876 | filename = info->full_backing_filename; |
| 2877 | } else if (info->has_backing_filename) { |
John Snow | 92d617a | 2015-12-14 14:55:15 -0500 | [diff] [blame] | 2878 | error_report("Could not determine absolute backing filename," |
| 2879 | " but backing filename '%s' present", |
| 2880 | info->backing_filename); |
| 2881 | goto err; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2882 | } |
| 2883 | if (info->has_backing_filename_format) { |
| 2884 | fmt = info->backing_filename_format; |
| 2885 | } |
| 2886 | } |
| 2887 | } |
| 2888 | g_hash_table_destroy(filenames); |
| 2889 | return head; |
| 2890 | |
| 2891 | err: |
| 2892 | qapi_free_ImageInfoList(head); |
| 2893 | g_hash_table_destroy(filenames); |
| 2894 | return NULL; |
| 2895 | } |
| 2896 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2897 | static int img_info(int argc, char **argv) |
| 2898 | { |
| 2899 | int c; |
| 2900 | OutputFormat output_format = OFORMAT_HUMAN; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2901 | bool chain = false; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2902 | const char *filename, *fmt, *output; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2903 | ImageInfoList *list; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2904 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2905 | bool force_share = false; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2906 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2907 | fmt = NULL; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2908 | output = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2909 | for(;;) { |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2910 | int option_index = 0; |
| 2911 | static const struct option long_options[] = { |
| 2912 | {"help", no_argument, 0, 'h'}, |
| 2913 | {"format", required_argument, 0, 'f'}, |
| 2914 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2915 | {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2916 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2917 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2918 | {"force-share", no_argument, 0, 'U'}, |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2919 | {0, 0, 0, 0} |
| 2920 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2921 | c = getopt_long(argc, argv, ":f:hU", |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2922 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2923 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2924 | break; |
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 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2927 | case ':': |
| 2928 | missing_argument(argv[optind - 1]); |
| 2929 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2930 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2931 | unrecognized_option(argv[optind - 1]); |
| 2932 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2933 | case 'h': |
| 2934 | help(); |
| 2935 | break; |
| 2936 | case 'f': |
| 2937 | fmt = optarg; |
| 2938 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2939 | case 'U': |
| 2940 | force_share = true; |
| 2941 | break; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2942 | case OPTION_OUTPUT: |
| 2943 | output = optarg; |
| 2944 | break; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2945 | case OPTION_BACKING_CHAIN: |
| 2946 | chain = true; |
| 2947 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2948 | case OPTION_OBJECT: { |
| 2949 | QemuOpts *opts; |
| 2950 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 2951 | optarg, true); |
| 2952 | if (!opts) { |
| 2953 | return 1; |
| 2954 | } |
| 2955 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2956 | case OPTION_IMAGE_OPTS: |
| 2957 | image_opts = true; |
| 2958 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2959 | } |
| 2960 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 2961 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2962 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2963 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2964 | filename = argv[optind++]; |
| 2965 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2966 | if (output && !strcmp(output, "json")) { |
| 2967 | output_format = OFORMAT_JSON; |
| 2968 | } else if (output && !strcmp(output, "human")) { |
| 2969 | output_format = OFORMAT_HUMAN; |
| 2970 | } else if (output) { |
| 2971 | error_report("--output must be used with human or json as argument."); |
| 2972 | return 1; |
| 2973 | } |
| 2974 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2975 | if (qemu_opts_foreach(&qemu_object_opts, |
| 2976 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 2977 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2978 | return 1; |
| 2979 | } |
| 2980 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2981 | list = collect_image_info_list(image_opts, filename, fmt, chain, |
| 2982 | force_share); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2983 | if (!list) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2984 | return 1; |
| 2985 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2986 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2987 | switch (output_format) { |
| 2988 | case OFORMAT_HUMAN: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2989 | dump_human_image_info_list(list); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2990 | break; |
| 2991 | case OFORMAT_JSON: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2992 | if (chain) { |
| 2993 | dump_json_image_info_list(list); |
| 2994 | } else { |
| 2995 | dump_json_image_info(list->value); |
| 2996 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2997 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2998 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2999 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 3000 | qapi_free_ImageInfoList(list); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 3001 | return 0; |
| 3002 | } |
| 3003 | |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3004 | static int dump_map_entry(OutputFormat output_format, MapEntry *e, |
| 3005 | MapEntry *next) |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3006 | { |
| 3007 | switch (output_format) { |
| 3008 | case OFORMAT_HUMAN: |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3009 | if (e->data && !e->has_offset) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3010 | error_report("File contains external, encrypted or compressed clusters."); |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3011 | return -1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3012 | } |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3013 | if (e->data && !e->zero) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3014 | printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n", |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3015 | e->start, e->length, |
| 3016 | e->has_offset ? e->offset : 0, |
| 3017 | e->has_filename ? e->filename : ""); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3018 | } |
| 3019 | /* This format ignores the distinction between 0, ZERO and ZERO|DATA. |
| 3020 | * Modify the flags here to allow more coalescing. |
| 3021 | */ |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3022 | if (next && (!next->data || next->zero)) { |
| 3023 | next->data = false; |
| 3024 | next->zero = true; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3025 | } |
| 3026 | break; |
| 3027 | case OFORMAT_JSON: |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 3028 | printf("{ \"start\": %"PRId64", \"length\": %"PRId64"," |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3029 | " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s", |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3030 | e->start, e->length, e->depth, |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3031 | e->zero ? "true" : "false", |
| 3032 | e->data ? "true" : "false"); |
| 3033 | if (e->has_offset) { |
Paolo Bonzini | c745bfb | 2013-09-11 18:47:52 +0200 | [diff] [blame] | 3034 | printf(", \"offset\": %"PRId64"", e->offset); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3035 | } |
| 3036 | putchar('}'); |
| 3037 | |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 3038 | if (next) { |
| 3039 | puts(","); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3040 | } |
| 3041 | break; |
| 3042 | } |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3043 | return 0; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3044 | } |
| 3045 | |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3046 | static int get_block_status(BlockDriverState *bs, int64_t offset, |
| 3047 | int64_t bytes, MapEntry *e) |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3048 | { |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3049 | int ret; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3050 | int depth; |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 3051 | BlockDriverState *file; |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3052 | bool has_offset; |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3053 | int64_t map; |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3054 | char *filename = NULL; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3055 | |
| 3056 | /* As an optimization, we could cache the current range of unallocated |
| 3057 | * clusters in each file of the chain, and avoid querying the same |
| 3058 | * range repeatedly. |
| 3059 | */ |
| 3060 | |
| 3061 | depth = 0; |
| 3062 | for (;;) { |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3063 | bs = bdrv_skip_filters(bs); |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3064 | ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3065 | if (ret < 0) { |
| 3066 | return ret; |
| 3067 | } |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3068 | assert(bytes); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3069 | if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) { |
| 3070 | break; |
| 3071 | } |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3072 | bs = bdrv_cow_bs(bs); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3073 | if (bs == NULL) { |
| 3074 | ret = 0; |
| 3075 | break; |
| 3076 | } |
| 3077 | |
| 3078 | depth++; |
| 3079 | } |
| 3080 | |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3081 | has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID); |
| 3082 | |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3083 | if (file && has_offset) { |
| 3084 | bdrv_refresh_filename(file); |
| 3085 | filename = file->filename; |
| 3086 | } |
| 3087 | |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3088 | *e = (MapEntry) { |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3089 | .start = offset, |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3090 | .length = bytes, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3091 | .data = !!(ret & BDRV_BLOCK_DATA), |
| 3092 | .zero = !!(ret & BDRV_BLOCK_ZERO), |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3093 | .offset = map, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3094 | .has_offset = has_offset, |
| 3095 | .depth = depth, |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3096 | .has_filename = filename, |
| 3097 | .filename = filename, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3098 | }; |
| 3099 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3100 | return 0; |
| 3101 | } |
| 3102 | |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3103 | static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next) |
| 3104 | { |
| 3105 | if (curr->length == 0) { |
| 3106 | return false; |
| 3107 | } |
| 3108 | if (curr->zero != next->zero || |
| 3109 | curr->data != next->data || |
| 3110 | curr->depth != next->depth || |
| 3111 | curr->has_filename != next->has_filename || |
| 3112 | curr->has_offset != next->has_offset) { |
| 3113 | return false; |
| 3114 | } |
| 3115 | if (curr->has_filename && strcmp(curr->filename, next->filename)) { |
| 3116 | return false; |
| 3117 | } |
| 3118 | if (curr->has_offset && curr->offset + curr->length != next->offset) { |
| 3119 | return false; |
| 3120 | } |
| 3121 | return true; |
| 3122 | } |
| 3123 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3124 | static int img_map(int argc, char **argv) |
| 3125 | { |
| 3126 | int c; |
| 3127 | OutputFormat output_format = OFORMAT_HUMAN; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3128 | BlockBackend *blk; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3129 | BlockDriverState *bs; |
| 3130 | const char *filename, *fmt, *output; |
| 3131 | int64_t length; |
| 3132 | MapEntry curr = { .length = 0 }, next; |
| 3133 | int ret = 0; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3134 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3135 | bool force_share = false; |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3136 | int64_t start_offset = 0; |
| 3137 | int64_t max_length = -1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3138 | |
| 3139 | fmt = NULL; |
| 3140 | output = NULL; |
| 3141 | for (;;) { |
| 3142 | int option_index = 0; |
| 3143 | static const struct option long_options[] = { |
| 3144 | {"help", no_argument, 0, 'h'}, |
| 3145 | {"format", required_argument, 0, 'f'}, |
| 3146 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3147 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3148 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3149 | {"force-share", no_argument, 0, 'U'}, |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3150 | {"start-offset", required_argument, 0, 's'}, |
| 3151 | {"max-length", required_argument, 0, 'l'}, |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3152 | {0, 0, 0, 0} |
| 3153 | }; |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3154 | c = getopt_long(argc, argv, ":f:s:l:hU", |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3155 | long_options, &option_index); |
| 3156 | if (c == -1) { |
| 3157 | break; |
| 3158 | } |
| 3159 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3160 | case ':': |
| 3161 | missing_argument(argv[optind - 1]); |
| 3162 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3163 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3164 | unrecognized_option(argv[optind - 1]); |
| 3165 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3166 | case 'h': |
| 3167 | help(); |
| 3168 | break; |
| 3169 | case 'f': |
| 3170 | fmt = optarg; |
| 3171 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3172 | case 'U': |
| 3173 | force_share = true; |
| 3174 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3175 | case OPTION_OUTPUT: |
| 3176 | output = optarg; |
| 3177 | break; |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3178 | case 's': |
| 3179 | start_offset = cvtnum("start offset", optarg); |
| 3180 | if (start_offset < 0) { |
| 3181 | return 1; |
| 3182 | } |
| 3183 | break; |
| 3184 | case 'l': |
| 3185 | max_length = cvtnum("max length", optarg); |
| 3186 | if (max_length < 0) { |
| 3187 | return 1; |
| 3188 | } |
| 3189 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3190 | case OPTION_OBJECT: { |
| 3191 | QemuOpts *opts; |
| 3192 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 3193 | optarg, true); |
| 3194 | if (!opts) { |
| 3195 | return 1; |
| 3196 | } |
| 3197 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3198 | case OPTION_IMAGE_OPTS: |
| 3199 | image_opts = true; |
| 3200 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3201 | } |
| 3202 | } |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3203 | if (optind != argc - 1) { |
| 3204 | error_exit("Expecting one image file name"); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3205 | } |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3206 | filename = argv[optind]; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3207 | |
| 3208 | if (output && !strcmp(output, "json")) { |
| 3209 | output_format = OFORMAT_JSON; |
| 3210 | } else if (output && !strcmp(output, "human")) { |
| 3211 | output_format = OFORMAT_HUMAN; |
| 3212 | } else if (output) { |
| 3213 | error_report("--output must be used with human or json as argument."); |
| 3214 | return 1; |
| 3215 | } |
| 3216 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3217 | if (qemu_opts_foreach(&qemu_object_opts, |
| 3218 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 3219 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3220 | return 1; |
| 3221 | } |
| 3222 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3223 | blk = img_open(image_opts, filename, fmt, 0, false, false, force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3224 | if (!blk) { |
| 3225 | return 1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3226 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3227 | bs = blk_bs(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3228 | |
| 3229 | if (output_format == OFORMAT_HUMAN) { |
| 3230 | printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File"); |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 3231 | } else if (output_format == OFORMAT_JSON) { |
| 3232 | putchar('['); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3233 | } |
| 3234 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3235 | length = blk_getlength(blk); |
Eyal Moscovici | 8f282e8 | 2020-05-13 16:36:27 +0300 | [diff] [blame] | 3236 | if (length < 0) { |
| 3237 | error_report("Failed to get size for '%s'", filename); |
| 3238 | return 1; |
| 3239 | } |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3240 | if (max_length != -1) { |
| 3241 | length = MIN(start_offset + max_length, length); |
| 3242 | } |
Eyal Moscovici | 8f282e8 | 2020-05-13 16:36:27 +0300 | [diff] [blame] | 3243 | |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3244 | curr.start = start_offset; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3245 | while (curr.start + curr.length < length) { |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3246 | int64_t offset = curr.start + curr.length; |
Kevin Wolf | d0ceea8 | 2020-07-07 16:46:29 +0200 | [diff] [blame] | 3247 | int64_t n = length - offset; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3248 | |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3249 | ret = get_block_status(bs, offset, n, &next); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3250 | if (ret < 0) { |
| 3251 | error_report("Could not read file metadata: %s", strerror(-ret)); |
| 3252 | goto out; |
| 3253 | } |
| 3254 | |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3255 | if (entry_mergeable(&curr, &next)) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3256 | curr.length += next.length; |
| 3257 | continue; |
| 3258 | } |
| 3259 | |
| 3260 | if (curr.length > 0) { |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3261 | ret = dump_map_entry(output_format, &curr, &next); |
| 3262 | if (ret < 0) { |
| 3263 | goto out; |
| 3264 | } |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3265 | } |
| 3266 | curr = next; |
| 3267 | } |
| 3268 | |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3269 | ret = dump_map_entry(output_format, &curr, NULL); |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 3270 | if (output_format == OFORMAT_JSON) { |
| 3271 | puts("]"); |
| 3272 | } |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3273 | |
| 3274 | out: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3275 | blk_unref(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3276 | return ret < 0; |
| 3277 | } |
| 3278 | |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3279 | #define SNAPSHOT_LIST 1 |
| 3280 | #define SNAPSHOT_CREATE 2 |
| 3281 | #define SNAPSHOT_APPLY 3 |
| 3282 | #define SNAPSHOT_DELETE 4 |
| 3283 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3284 | static int img_snapshot(int argc, char **argv) |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3285 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3286 | BlockBackend *blk; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3287 | BlockDriverState *bs; |
| 3288 | QEMUSnapshotInfo sn; |
| 3289 | char *filename, *snapshot_name = NULL; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3290 | int c, ret = 0, bdrv_oflags; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3291 | int action = 0; |
| 3292 | qemu_timeval tv; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3293 | bool quiet = false; |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 3294 | Error *err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3295 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3296 | bool force_share = false; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3297 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3298 | bdrv_oflags = BDRV_O_RDWR; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3299 | /* Parse commandline parameters */ |
| 3300 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3301 | static const struct option long_options[] = { |
| 3302 | {"help", no_argument, 0, 'h'}, |
| 3303 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3304 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3305 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3306 | {0, 0, 0, 0} |
| 3307 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3308 | c = getopt_long(argc, argv, ":la:c:d:hqU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3309 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3310 | if (c == -1) { |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3311 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3312 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3313 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3314 | case ':': |
| 3315 | missing_argument(argv[optind - 1]); |
| 3316 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3317 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3318 | unrecognized_option(argv[optind - 1]); |
| 3319 | break; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3320 | case 'h': |
| 3321 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3322 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3323 | case 'l': |
| 3324 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3325 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3326 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3327 | } |
| 3328 | action = SNAPSHOT_LIST; |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 3329 | bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */ |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3330 | break; |
| 3331 | case 'a': |
| 3332 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3333 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3334 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3335 | } |
| 3336 | action = SNAPSHOT_APPLY; |
| 3337 | snapshot_name = optarg; |
| 3338 | break; |
| 3339 | case 'c': |
| 3340 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3341 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3342 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3343 | } |
| 3344 | action = SNAPSHOT_CREATE; |
| 3345 | snapshot_name = optarg; |
| 3346 | break; |
| 3347 | case 'd': |
| 3348 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3349 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3350 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3351 | } |
| 3352 | action = SNAPSHOT_DELETE; |
| 3353 | snapshot_name = optarg; |
| 3354 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3355 | case 'q': |
| 3356 | quiet = true; |
| 3357 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3358 | case 'U': |
| 3359 | force_share = true; |
| 3360 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3361 | case OPTION_OBJECT: { |
| 3362 | QemuOpts *opts; |
| 3363 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 3364 | optarg, true); |
| 3365 | if (!opts) { |
| 3366 | return 1; |
| 3367 | } |
| 3368 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3369 | case OPTION_IMAGE_OPTS: |
| 3370 | image_opts = true; |
| 3371 | break; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3372 | } |
| 3373 | } |
| 3374 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 3375 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3376 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3377 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3378 | filename = argv[optind++]; |
| 3379 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3380 | if (qemu_opts_foreach(&qemu_object_opts, |
| 3381 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 3382 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3383 | return 1; |
| 3384 | } |
| 3385 | |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3386 | /* Open the image */ |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3387 | blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet, |
| 3388 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3389 | if (!blk) { |
| 3390 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3391 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3392 | bs = blk_bs(blk); |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3393 | |
| 3394 | /* Perform the requested action */ |
| 3395 | switch(action) { |
| 3396 | case SNAPSHOT_LIST: |
| 3397 | dump_snapshots(bs); |
| 3398 | break; |
| 3399 | |
| 3400 | case SNAPSHOT_CREATE: |
| 3401 | memset(&sn, 0, sizeof(sn)); |
| 3402 | pstrcpy(sn.name, sizeof(sn.name), snapshot_name); |
| 3403 | |
| 3404 | qemu_gettimeofday(&tv); |
| 3405 | sn.date_sec = tv.tv_sec; |
| 3406 | sn.date_nsec = tv.tv_usec * 1000; |
| 3407 | |
| 3408 | ret = bdrv_snapshot_create(bs, &sn); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3409 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3410 | error_report("Could not create snapshot '%s': %d (%s)", |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3411 | snapshot_name, ret, strerror(-ret)); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3412 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3413 | break; |
| 3414 | |
| 3415 | case SNAPSHOT_APPLY: |
Kevin Wolf | 0b62bcb | 2017-11-20 15:28:41 +0100 | [diff] [blame] | 3416 | ret = bdrv_snapshot_goto(bs, snapshot_name, &err); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3417 | if (ret) { |
Kevin Wolf | 0b62bcb | 2017-11-20 15:28:41 +0100 | [diff] [blame] | 3418 | error_reportf_err(err, "Could not apply snapshot '%s': ", |
| 3419 | snapshot_name); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3420 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3421 | break; |
| 3422 | |
| 3423 | case SNAPSHOT_DELETE: |
Daniel Henrique Barboza | 8c04093 | 2018-11-07 11:09:59 -0200 | [diff] [blame] | 3424 | ret = bdrv_snapshot_find(bs, &sn, snapshot_name); |
| 3425 | if (ret < 0) { |
| 3426 | error_report("Could not delete snapshot '%s': snapshot not " |
| 3427 | "found", snapshot_name); |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 3428 | ret = 1; |
Daniel Henrique Barboza | 8c04093 | 2018-11-07 11:09:59 -0200 | [diff] [blame] | 3429 | } else { |
| 3430 | ret = bdrv_snapshot_delete(bs, sn.id_str, sn.name, &err); |
| 3431 | if (ret < 0) { |
| 3432 | error_reportf_err(err, "Could not delete snapshot '%s': ", |
| 3433 | snapshot_name); |
| 3434 | ret = 1; |
| 3435 | } |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3436 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3437 | break; |
| 3438 | } |
| 3439 | |
| 3440 | /* Cleanup */ |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3441 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3442 | if (ret) { |
| 3443 | return 1; |
| 3444 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3445 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3446 | } |
| 3447 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3448 | static int img_rebase(int argc, char **argv) |
| 3449 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3450 | BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL; |
Paolo Bonzini | 396374c | 2016-02-25 23:53:54 +0100 | [diff] [blame] | 3451 | uint8_t *buf_old = NULL; |
| 3452 | uint8_t *buf_new = NULL; |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3453 | BlockDriverState *bs = NULL, *prefix_chain_bs = NULL; |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3454 | BlockDriverState *unfiltered_bs; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3455 | char *filename; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3456 | const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg; |
| 3457 | int c, flags, src_flags, ret; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3458 | bool writethrough, src_writethrough; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3459 | int unsafe = 0; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3460 | bool force_share = false; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3461 | int progress = 0; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3462 | bool quiet = false; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 3463 | Error *local_err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3464 | bool image_opts = false; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3465 | |
| 3466 | /* Parse commandline parameters */ |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 3467 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3468 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3469 | src_cache = BDRV_DEFAULT_CACHE; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3470 | out_baseimg = NULL; |
| 3471 | out_basefmt = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3472 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3473 | static const struct option long_options[] = { |
| 3474 | {"help", no_argument, 0, 'h'}, |
| 3475 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3476 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3477 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3478 | {0, 0, 0, 0} |
| 3479 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3480 | c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3481 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3482 | if (c == -1) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3483 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3484 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3485 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3486 | case ':': |
| 3487 | missing_argument(argv[optind - 1]); |
| 3488 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3489 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3490 | unrecognized_option(argv[optind - 1]); |
| 3491 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3492 | case 'h': |
| 3493 | help(); |
| 3494 | return 0; |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 3495 | case 'f': |
| 3496 | fmt = optarg; |
| 3497 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3498 | case 'F': |
| 3499 | out_basefmt = optarg; |
| 3500 | break; |
| 3501 | case 'b': |
| 3502 | out_baseimg = optarg; |
| 3503 | break; |
| 3504 | case 'u': |
| 3505 | unsafe = 1; |
| 3506 | break; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3507 | case 'p': |
| 3508 | progress = 1; |
| 3509 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3510 | case 't': |
| 3511 | cache = optarg; |
| 3512 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3513 | case 'T': |
| 3514 | src_cache = optarg; |
| 3515 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3516 | case 'q': |
| 3517 | quiet = true; |
| 3518 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3519 | case OPTION_OBJECT: { |
| 3520 | QemuOpts *opts; |
| 3521 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 3522 | optarg, true); |
| 3523 | if (!opts) { |
| 3524 | return 1; |
| 3525 | } |
| 3526 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3527 | case OPTION_IMAGE_OPTS: |
| 3528 | image_opts = true; |
| 3529 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3530 | case 'U': |
| 3531 | force_share = true; |
| 3532 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3533 | } |
| 3534 | } |
| 3535 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3536 | if (quiet) { |
| 3537 | progress = 0; |
| 3538 | } |
| 3539 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3540 | if (optind != argc - 1) { |
| 3541 | error_exit("Expecting one image file name"); |
| 3542 | } |
| 3543 | if (!unsafe && !out_baseimg) { |
| 3544 | error_exit("Must specify backing file (-b) or use unsafe mode (-u)"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3545 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3546 | filename = argv[optind++]; |
| 3547 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3548 | if (qemu_opts_foreach(&qemu_object_opts, |
| 3549 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 3550 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3551 | return 1; |
| 3552 | } |
| 3553 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3554 | qemu_progress_init(progress, 2.0); |
| 3555 | qemu_progress_print(0, 100); |
| 3556 | |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3557 | flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0); |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3558 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3559 | if (ret < 0) { |
| 3560 | error_report("Invalid cache option: %s", cache); |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3561 | goto out; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3562 | } |
| 3563 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3564 | src_flags = 0; |
| 3565 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3566 | if (ret < 0) { |
| 3567 | error_report("Invalid source cache option: %s", src_cache); |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3568 | goto out; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3569 | } |
| 3570 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3571 | /* The source files are opened read-only, don't care about WCE */ |
| 3572 | assert((src_flags & BDRV_O_RDWR) == 0); |
| 3573 | (void) src_writethrough; |
| 3574 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3575 | /* |
| 3576 | * Open the images. |
| 3577 | * |
| 3578 | * Ignore the old backing file for unsafe rebase in case we want to correct |
| 3579 | * the reference to a renamed or moved backing file. |
| 3580 | */ |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3581 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 3582 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3583 | if (!blk) { |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3584 | ret = -1; |
| 3585 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3586 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3587 | bs = blk_bs(blk); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3588 | |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3589 | unfiltered_bs = bdrv_skip_filters(bs); |
| 3590 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3591 | if (out_basefmt != NULL) { |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3592 | if (bdrv_find_format(out_basefmt) == NULL) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3593 | error_report("Invalid format name: '%s'", out_basefmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3594 | ret = -1; |
| 3595 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3596 | } |
| 3597 | } |
| 3598 | |
| 3599 | /* For safe rebasing we need to compare old and new backing file */ |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3600 | if (!unsafe) { |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3601 | QDict *options = NULL; |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3602 | BlockDriverState *base_bs = bdrv_cow_bs(unfiltered_bs); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3603 | |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3604 | if (base_bs) { |
Kevin Wolf | d861ab3 | 2019-04-25 14:25:10 +0200 | [diff] [blame] | 3605 | blk_old_backing = blk_new(qemu_get_aio_context(), |
| 3606 | BLK_PERM_CONSISTENT_READ, |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3607 | BLK_PERM_ALL); |
| 3608 | ret = blk_insert_bs(blk_old_backing, base_bs, |
| 3609 | &local_err); |
| 3610 | if (ret < 0) { |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3611 | error_reportf_err(local_err, |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3612 | "Could not reuse old backing file '%s': ", |
| 3613 | base_bs->filename); |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3614 | goto out; |
| 3615 | } |
| 3616 | } else { |
| 3617 | blk_old_backing = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3618 | } |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3619 | |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3620 | if (out_baseimg[0]) { |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3621 | const char *overlay_filename; |
| 3622 | char *out_real_path; |
| 3623 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3624 | options = qdict_new(); |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3625 | if (out_basefmt) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 3626 | qdict_put_str(options, "driver", out_basefmt); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3627 | } |
| 3628 | if (force_share) { |
| 3629 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3630 | } |
| 3631 | |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3632 | bdrv_refresh_filename(bs); |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3633 | overlay_filename = bs->exact_filename[0] ? bs->exact_filename |
| 3634 | : bs->filename; |
Max Reitz | 645ae7d | 2019-02-01 20:29:14 +0100 | [diff] [blame] | 3635 | out_real_path = |
| 3636 | bdrv_get_full_backing_filename_from_filename(overlay_filename, |
| 3637 | out_baseimg, |
| 3638 | &local_err); |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3639 | if (local_err) { |
Max Reitz | f22356d | 2019-05-28 21:53:38 +0200 | [diff] [blame] | 3640 | qobject_unref(options); |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3641 | error_reportf_err(local_err, |
| 3642 | "Could not resolve backing filename: "); |
| 3643 | ret = -1; |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3644 | goto out; |
| 3645 | } |
| 3646 | |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3647 | /* |
| 3648 | * Find out whether we rebase an image on top of a previous image |
| 3649 | * in its chain. |
| 3650 | */ |
| 3651 | prefix_chain_bs = bdrv_find_backing_image(bs, out_real_path); |
Sam Eiderman | 330c729 | 2019-05-23 19:33:37 +0300 | [diff] [blame] | 3652 | if (prefix_chain_bs) { |
Max Reitz | f22356d | 2019-05-28 21:53:38 +0200 | [diff] [blame] | 3653 | qobject_unref(options); |
Sam Eiderman | 330c729 | 2019-05-23 19:33:37 +0300 | [diff] [blame] | 3654 | g_free(out_real_path); |
Max Reitz | f22356d | 2019-05-28 21:53:38 +0200 | [diff] [blame] | 3655 | |
Kevin Wolf | d861ab3 | 2019-04-25 14:25:10 +0200 | [diff] [blame] | 3656 | blk_new_backing = blk_new(qemu_get_aio_context(), |
| 3657 | BLK_PERM_CONSISTENT_READ, |
Sam Eiderman | 330c729 | 2019-05-23 19:33:37 +0300 | [diff] [blame] | 3658 | BLK_PERM_ALL); |
| 3659 | ret = blk_insert_bs(blk_new_backing, prefix_chain_bs, |
| 3660 | &local_err); |
| 3661 | if (ret < 0) { |
| 3662 | error_reportf_err(local_err, |
| 3663 | "Could not reuse backing file '%s': ", |
| 3664 | out_baseimg); |
| 3665 | goto out; |
| 3666 | } |
| 3667 | } else { |
| 3668 | blk_new_backing = blk_new_open(out_real_path, NULL, |
| 3669 | options, src_flags, &local_err); |
| 3670 | g_free(out_real_path); |
| 3671 | if (!blk_new_backing) { |
| 3672 | error_reportf_err(local_err, |
| 3673 | "Could not open new backing file '%s': ", |
| 3674 | out_baseimg); |
| 3675 | ret = -1; |
| 3676 | goto out; |
| 3677 | } |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3678 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3679 | } |
| 3680 | } |
| 3681 | |
| 3682 | /* |
| 3683 | * Check each unallocated cluster in the COW file. If it is unallocated, |
| 3684 | * accesses go to the backing file. We must therefore compare this cluster |
| 3685 | * in the old and new backing file, and if they differ we need to copy it |
| 3686 | * from the old backing file into the COW file. |
| 3687 | * |
| 3688 | * If qemu-img crashes during this step, no harm is done. The content of |
| 3689 | * the image is the same as the original one at any time. |
| 3690 | */ |
| 3691 | if (!unsafe) { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3692 | int64_t size; |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3693 | int64_t old_backing_size = 0; |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3694 | int64_t new_backing_size = 0; |
| 3695 | uint64_t offset; |
| 3696 | int64_t n; |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 3697 | float local_progress = 0; |
TeLeMan | d6771bf | 2010-02-08 16:20:00 +0800 | [diff] [blame] | 3698 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3699 | buf_old = blk_blockalign(blk, IO_BUF_SIZE); |
| 3700 | buf_new = blk_blockalign(blk, IO_BUF_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3701 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3702 | size = blk_getlength(blk); |
| 3703 | if (size < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3704 | error_report("Could not get size of '%s': %s", |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3705 | filename, strerror(-size)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3706 | ret = -1; |
| 3707 | goto out; |
| 3708 | } |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3709 | if (blk_old_backing) { |
| 3710 | old_backing_size = blk_getlength(blk_old_backing); |
| 3711 | if (old_backing_size < 0) { |
| 3712 | char backing_name[PATH_MAX]; |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3713 | |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3714 | bdrv_get_backing_filename(bs, backing_name, |
| 3715 | sizeof(backing_name)); |
| 3716 | error_report("Could not get size of '%s': %s", |
| 3717 | backing_name, strerror(-old_backing_size)); |
| 3718 | ret = -1; |
| 3719 | goto out; |
| 3720 | } |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3721 | } |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3722 | if (blk_new_backing) { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3723 | new_backing_size = blk_getlength(blk_new_backing); |
| 3724 | if (new_backing_size < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3725 | error_report("Could not get size of '%s': %s", |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3726 | out_baseimg, strerror(-new_backing_size)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3727 | ret = -1; |
| 3728 | goto out; |
| 3729 | } |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3730 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3731 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3732 | if (size != 0) { |
| 3733 | local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE)); |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 3734 | } |
| 3735 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3736 | for (offset = 0; offset < size; offset += n) { |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3737 | bool buf_old_is_zero = false; |
| 3738 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3739 | /* How many bytes can we handle with the next read? */ |
| 3740 | n = MIN(IO_BUF_SIZE, size - offset); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3741 | |
| 3742 | /* If the cluster is allocated, we don't need to take action */ |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3743 | ret = bdrv_is_allocated(unfiltered_bs, offset, n, &n); |
Paolo Bonzini | d663640 | 2013-09-04 19:00:25 +0200 | [diff] [blame] | 3744 | if (ret < 0) { |
| 3745 | error_report("error while reading image metadata: %s", |
| 3746 | strerror(-ret)); |
| 3747 | goto out; |
| 3748 | } |
Kevin Wolf | cc60e32 | 2010-04-29 14:47:48 +0200 | [diff] [blame] | 3749 | if (ret) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3750 | continue; |
| 3751 | } |
| 3752 | |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3753 | if (prefix_chain_bs) { |
| 3754 | /* |
| 3755 | * If cluster wasn't changed since prefix_chain, we don't need |
| 3756 | * to take action |
| 3757 | */ |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3758 | ret = bdrv_is_allocated_above(bdrv_cow_bs(unfiltered_bs), |
| 3759 | prefix_chain_bs, false, |
| 3760 | offset, n, &n); |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3761 | if (ret < 0) { |
| 3762 | error_report("error while reading image metadata: %s", |
| 3763 | strerror(-ret)); |
| 3764 | goto out; |
| 3765 | } |
| 3766 | if (!ret) { |
| 3767 | continue; |
| 3768 | } |
| 3769 | } |
| 3770 | |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3771 | /* |
| 3772 | * Read old and new backing file and take into consideration that |
| 3773 | * backing files may be smaller than the COW image. |
| 3774 | */ |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3775 | if (offset >= old_backing_size) { |
| 3776 | memset(buf_old, 0, n); |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3777 | buf_old_is_zero = true; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3778 | } else { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3779 | if (offset + n > old_backing_size) { |
| 3780 | n = old_backing_size - offset; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3781 | } |
| 3782 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3783 | ret = blk_pread(blk_old_backing, offset, buf_old, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3784 | if (ret < 0) { |
| 3785 | error_report("error while reading from old backing file"); |
| 3786 | goto out; |
| 3787 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3788 | } |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3789 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3790 | if (offset >= new_backing_size || !blk_new_backing) { |
| 3791 | memset(buf_new, 0, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3792 | } else { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3793 | if (offset + n > new_backing_size) { |
| 3794 | n = new_backing_size - offset; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3795 | } |
| 3796 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3797 | ret = blk_pread(blk_new_backing, offset, buf_new, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3798 | if (ret < 0) { |
| 3799 | error_report("error while reading from new backing file"); |
| 3800 | goto out; |
| 3801 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3802 | } |
| 3803 | |
| 3804 | /* If they differ, we need to write to the COW file */ |
| 3805 | uint64_t written = 0; |
| 3806 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3807 | while (written < n) { |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 3808 | int64_t pnum; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3809 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3810 | if (compare_buffers(buf_old + written, buf_new + written, |
| 3811 | n - written, &pnum)) |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3812 | { |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3813 | if (buf_old_is_zero) { |
| 3814 | ret = blk_pwrite_zeroes(blk, offset + written, pnum, 0); |
| 3815 | } else { |
| 3816 | ret = blk_pwrite(blk, offset + written, |
| 3817 | buf_old + written, pnum, 0); |
| 3818 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3819 | if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3820 | error_report("Error while writing to COW image: %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3821 | strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3822 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3823 | } |
| 3824 | } |
| 3825 | |
| 3826 | written += pnum; |
| 3827 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3828 | qemu_progress_print(local_progress, 100); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3829 | } |
| 3830 | } |
| 3831 | |
| 3832 | /* |
| 3833 | * Change the backing file. All clusters that are different from the old |
| 3834 | * backing file are overwritten in the COW file now, so the visible content |
| 3835 | * doesn't change when we switch the backing file. |
| 3836 | */ |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3837 | if (out_baseimg && *out_baseimg) { |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3838 | ret = bdrv_change_backing_file(unfiltered_bs, out_baseimg, out_basefmt, |
| 3839 | true); |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3840 | } else { |
Max Reitz | 4a2061e | 2019-06-12 19:00:30 +0200 | [diff] [blame] | 3841 | ret = bdrv_change_backing_file(unfiltered_bs, NULL, NULL, false); |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3842 | } |
| 3843 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3844 | if (ret == -ENOSPC) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3845 | error_report("Could not change the backing file to '%s': No " |
| 3846 | "space left in the file header", out_baseimg); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3847 | } else if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3848 | error_report("Could not change the backing file to '%s': %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3849 | out_baseimg, strerror(-ret)); |
| 3850 | } |
| 3851 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3852 | qemu_progress_print(100, 0); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3853 | /* |
| 3854 | * TODO At this point it is possible to check if any clusters that are |
| 3855 | * allocated in the COW file are the same in the backing file. If so, they |
| 3856 | * could be dropped from the COW file. Don't do this before switching the |
| 3857 | * backing file, in case of a crash this would lead to corruption. |
| 3858 | */ |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3859 | out: |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3860 | qemu_progress_end(); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3861 | /* Cleanup */ |
| 3862 | if (!unsafe) { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3863 | blk_unref(blk_old_backing); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3864 | blk_unref(blk_new_backing); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3865 | } |
Paolo Bonzini | 396374c | 2016-02-25 23:53:54 +0100 | [diff] [blame] | 3866 | qemu_vfree(buf_old); |
| 3867 | qemu_vfree(buf_new); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3868 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3869 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3870 | if (ret) { |
| 3871 | return 1; |
| 3872 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3873 | return 0; |
| 3874 | } |
| 3875 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3876 | static int img_resize(int argc, char **argv) |
| 3877 | { |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 3878 | Error *err = NULL; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3879 | int c, ret, relative; |
| 3880 | const char *filename, *fmt, *size; |
Max Reitz | 09c5c6d | 2019-09-18 11:51:44 +0200 | [diff] [blame] | 3881 | int64_t n, total_size, current_size; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3882 | bool quiet = false; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3883 | BlockBackend *blk = NULL; |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3884 | PreallocMode prealloc = PREALLOC_MODE_OFF; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3885 | QemuOpts *param; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3886 | |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3887 | static QemuOptsList resize_options = { |
| 3888 | .name = "resize_options", |
| 3889 | .head = QTAILQ_HEAD_INITIALIZER(resize_options.head), |
| 3890 | .desc = { |
| 3891 | { |
| 3892 | .name = BLOCK_OPT_SIZE, |
| 3893 | .type = QEMU_OPT_SIZE, |
| 3894 | .help = "Virtual disk size" |
| 3895 | }, { |
| 3896 | /* end of list */ |
| 3897 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3898 | }, |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3899 | }; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3900 | bool image_opts = false; |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3901 | bool shrink = false; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3902 | |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 3903 | /* Remove size from argv manually so that negative numbers are not treated |
| 3904 | * as options by getopt. */ |
| 3905 | if (argc < 3) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3906 | error_exit("Not enough arguments"); |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 3907 | return 1; |
| 3908 | } |
| 3909 | |
| 3910 | size = argv[--argc]; |
| 3911 | |
| 3912 | /* Parse getopt arguments */ |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3913 | fmt = NULL; |
| 3914 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3915 | static const struct option long_options[] = { |
| 3916 | {"help", no_argument, 0, 'h'}, |
| 3917 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3918 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3919 | {"preallocation", required_argument, 0, OPTION_PREALLOCATION}, |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3920 | {"shrink", no_argument, 0, OPTION_SHRINK}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3921 | {0, 0, 0, 0} |
| 3922 | }; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3923 | c = getopt_long(argc, argv, ":f:hq", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3924 | long_options, NULL); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3925 | if (c == -1) { |
| 3926 | break; |
| 3927 | } |
| 3928 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3929 | case ':': |
| 3930 | missing_argument(argv[optind - 1]); |
| 3931 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3932 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3933 | unrecognized_option(argv[optind - 1]); |
| 3934 | break; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3935 | case 'h': |
| 3936 | help(); |
| 3937 | break; |
| 3938 | case 'f': |
| 3939 | fmt = optarg; |
| 3940 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3941 | case 'q': |
| 3942 | quiet = true; |
| 3943 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3944 | case OPTION_OBJECT: { |
| 3945 | QemuOpts *opts; |
| 3946 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 3947 | optarg, true); |
| 3948 | if (!opts) { |
| 3949 | return 1; |
| 3950 | } |
| 3951 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3952 | case OPTION_IMAGE_OPTS: |
| 3953 | image_opts = true; |
| 3954 | break; |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3955 | case OPTION_PREALLOCATION: |
Marc-André Lureau | f7abe0e | 2017-08-24 10:46:10 +0200 | [diff] [blame] | 3956 | prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg, |
Markus Armbruster | 06c60b6 | 2017-08-24 10:45:57 +0200 | [diff] [blame] | 3957 | PREALLOC_MODE__MAX, NULL); |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3958 | if (prealloc == PREALLOC_MODE__MAX) { |
| 3959 | error_report("Invalid preallocation mode '%s'", optarg); |
| 3960 | return 1; |
| 3961 | } |
| 3962 | break; |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3963 | case OPTION_SHRINK: |
| 3964 | shrink = true; |
| 3965 | break; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3966 | } |
| 3967 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 3968 | if (optind != argc - 1) { |
Max Reitz | be8fbd4 | 2018-02-05 17:27:45 +0100 | [diff] [blame] | 3969 | error_exit("Expecting image file name and size"); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3970 | } |
| 3971 | filename = argv[optind++]; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3972 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3973 | if (qemu_opts_foreach(&qemu_object_opts, |
| 3974 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 3975 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3976 | return 1; |
| 3977 | } |
| 3978 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3979 | /* Choose grow, shrink, or absolute resize mode */ |
| 3980 | switch (size[0]) { |
| 3981 | case '+': |
| 3982 | relative = 1; |
| 3983 | size++; |
| 3984 | break; |
| 3985 | case '-': |
| 3986 | relative = -1; |
| 3987 | size++; |
| 3988 | break; |
| 3989 | default: |
| 3990 | relative = 0; |
| 3991 | break; |
| 3992 | } |
| 3993 | |
| 3994 | /* Parse size */ |
Peter Crosthwaite | 87ea75d | 2014-01-01 18:49:17 -0800 | [diff] [blame] | 3995 | param = qemu_opts_create(&resize_options, NULL, 0, &error_abort); |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 3996 | if (!qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err)) { |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 3997 | error_report_err(err); |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3998 | ret = -1; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3999 | qemu_opts_del(param); |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 4000 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4001 | } |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 4002 | n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0); |
| 4003 | qemu_opts_del(param); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4004 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 4005 | blk = img_open(image_opts, filename, fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4006 | BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet, |
| 4007 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 4008 | if (!blk) { |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 4009 | ret = -1; |
| 4010 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 4011 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4012 | |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 4013 | current_size = blk_getlength(blk); |
| 4014 | if (current_size < 0) { |
| 4015 | error_report("Failed to inquire current image length: %s", |
| 4016 | strerror(-current_size)); |
| 4017 | ret = -1; |
| 4018 | goto out; |
| 4019 | } |
| 4020 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4021 | if (relative) { |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 4022 | total_size = current_size + n * relative; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4023 | } else { |
| 4024 | total_size = n; |
| 4025 | } |
| 4026 | if (total_size <= 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 4027 | error_report("New image size must be positive"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 4028 | ret = -1; |
| 4029 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4030 | } |
| 4031 | |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 4032 | if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) { |
| 4033 | error_report("Preallocation can only be used for growing images"); |
| 4034 | ret = -1; |
| 4035 | goto out; |
| 4036 | } |
| 4037 | |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 4038 | if (total_size < current_size && !shrink) { |
Kevin Wolf | 1c404d7 | 2020-07-10 14:17:17 +0200 | [diff] [blame] | 4039 | error_report("Use the --shrink option to perform a shrink operation."); |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 4040 | warn_report("Shrinking an image will delete all data beyond the " |
| 4041 | "shrunken image's end. Before performing such an " |
| 4042 | "operation, make sure there is no important data there."); |
Kevin Wolf | 1c404d7 | 2020-07-10 14:17:17 +0200 | [diff] [blame] | 4043 | ret = -1; |
| 4044 | goto out; |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 4045 | } |
| 4046 | |
Max Reitz | e8d04f9 | 2019-09-18 11:51:43 +0200 | [diff] [blame] | 4047 | /* |
| 4048 | * The user expects the image to have the desired size after |
| 4049 | * resizing, so pass @exact=true. It is of no use to report |
| 4050 | * success when the image has not actually been resized. |
| 4051 | */ |
Kevin Wolf | 8c6242b | 2020-04-24 14:54:41 +0200 | [diff] [blame] | 4052 | ret = blk_truncate(blk, total_size, true, prealloc, 0, &err); |
Max Reitz | 09c5c6d | 2019-09-18 11:51:44 +0200 | [diff] [blame] | 4053 | if (!ret) { |
| 4054 | qprintf(quiet, "Image resized.\n"); |
| 4055 | } else { |
Max Reitz | ed3d2ec | 2017-03-28 22:51:27 +0200 | [diff] [blame] | 4056 | error_report_err(err); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4057 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 4058 | out: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 4059 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 4060 | if (ret) { |
| 4061 | return 1; |
| 4062 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4063 | return 0; |
| 4064 | } |
| 4065 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4066 | static void amend_status_cb(BlockDriverState *bs, |
Max Reitz | 8b13976 | 2015-07-27 17:51:32 +0200 | [diff] [blame] | 4067 | int64_t offset, int64_t total_work_size, |
| 4068 | void *opaque) |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4069 | { |
| 4070 | qemu_progress_print(100.f * offset / total_work_size, 0); |
| 4071 | } |
| 4072 | |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4073 | static int print_amend_option_help(const char *format) |
| 4074 | { |
| 4075 | BlockDriver *drv; |
| 4076 | |
| 4077 | /* Find driver and parse its options */ |
| 4078 | drv = bdrv_find_format(format); |
| 4079 | if (!drv) { |
| 4080 | error_report("Unknown file format '%s'", format); |
| 4081 | return 1; |
| 4082 | } |
| 4083 | |
| 4084 | if (!drv->bdrv_amend_options) { |
| 4085 | error_report("Format driver '%s' does not support option amendment", |
| 4086 | format); |
| 4087 | return 1; |
| 4088 | } |
| 4089 | |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4090 | /* Every driver supporting amendment must have amend_opts */ |
| 4091 | assert(drv->amend_opts); |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4092 | |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4093 | printf("Amend options for '%s':\n", format); |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4094 | qemu_opts_print_help(drv->amend_opts, false); |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4095 | return 0; |
| 4096 | } |
| 4097 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4098 | static int img_amend(int argc, char **argv) |
| 4099 | { |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 4100 | Error *err = NULL; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4101 | int c, ret = 0; |
| 4102 | char *options = NULL; |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4103 | QemuOptsList *amend_opts = NULL; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 4104 | QemuOpts *opts = NULL; |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 4105 | const char *fmt = NULL, *filename, *cache; |
| 4106 | int flags; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 4107 | bool writethrough; |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4108 | bool quiet = false, progress = false; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 4109 | BlockBackend *blk = NULL; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4110 | BlockDriverState *bs = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 4111 | bool image_opts = false; |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4112 | bool force = false; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4113 | |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 4114 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4115 | for (;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4116 | static const struct option long_options[] = { |
| 4117 | {"help", no_argument, 0, 'h'}, |
| 4118 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 4119 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4120 | {"force", no_argument, 0, OPTION_FORCE}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4121 | {0, 0, 0, 0} |
| 4122 | }; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4123 | c = getopt_long(argc, argv, ":ho:f:t:pq", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4124 | long_options, NULL); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4125 | if (c == -1) { |
| 4126 | break; |
| 4127 | } |
| 4128 | |
| 4129 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4130 | case ':': |
| 4131 | missing_argument(argv[optind - 1]); |
| 4132 | break; |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4133 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4134 | unrecognized_option(argv[optind - 1]); |
| 4135 | break; |
| 4136 | case 'h': |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4137 | help(); |
| 4138 | break; |
| 4139 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 4140 | if (accumulate_options(&options, optarg) < 0) { |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4141 | ret = -1; |
| 4142 | goto out_no_progress; |
| 4143 | } |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4144 | break; |
| 4145 | case 'f': |
| 4146 | fmt = optarg; |
| 4147 | break; |
| 4148 | case 't': |
| 4149 | cache = optarg; |
| 4150 | break; |
| 4151 | case 'p': |
| 4152 | progress = true; |
| 4153 | break; |
| 4154 | case 'q': |
| 4155 | quiet = true; |
| 4156 | break; |
| 4157 | case OPTION_OBJECT: |
| 4158 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 4159 | optarg, true); |
| 4160 | if (!opts) { |
| 4161 | ret = -1; |
| 4162 | goto out_no_progress; |
| 4163 | } |
| 4164 | break; |
| 4165 | case OPTION_IMAGE_OPTS: |
| 4166 | image_opts = true; |
| 4167 | break; |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4168 | case OPTION_FORCE: |
| 4169 | force = true; |
| 4170 | break; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4171 | } |
| 4172 | } |
| 4173 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4174 | if (!options) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 4175 | error_exit("Must specify options (-o)"); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4176 | } |
| 4177 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4178 | if (qemu_opts_foreach(&qemu_object_opts, |
| 4179 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 4180 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4181 | ret = -1; |
| 4182 | goto out_no_progress; |
| 4183 | } |
| 4184 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4185 | if (quiet) { |
| 4186 | progress = false; |
| 4187 | } |
| 4188 | qemu_progress_init(progress, 1.0); |
| 4189 | |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4190 | filename = (optind == argc - 1) ? argv[argc - 1] : NULL; |
| 4191 | if (fmt && has_help_option(options)) { |
| 4192 | /* If a format is explicitly specified (and possibly no filename is |
| 4193 | * given), print option help here */ |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4194 | ret = print_amend_option_help(fmt); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4195 | goto out; |
| 4196 | } |
| 4197 | |
| 4198 | if (optind != argc - 1) { |
Max Reitz | b2f27e4 | 2014-10-27 11:12:52 +0100 | [diff] [blame] | 4199 | error_report("Expecting one image file name"); |
| 4200 | ret = -1; |
| 4201 | goto out; |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4202 | } |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4203 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 4204 | flags = BDRV_O_RDWR; |
| 4205 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 4206 | if (ret < 0) { |
| 4207 | error_report("Invalid cache option: %s", cache); |
| 4208 | goto out; |
| 4209 | } |
| 4210 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4211 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 4212 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 4213 | if (!blk) { |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4214 | ret = -1; |
| 4215 | goto out; |
| 4216 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 4217 | bs = blk_bs(blk); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4218 | |
| 4219 | fmt = bs->drv->format_name; |
| 4220 | |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 4221 | if (has_help_option(options)) { |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4222 | /* If the format was auto-detected, print option help here */ |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4223 | ret = print_amend_option_help(fmt); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4224 | goto out; |
| 4225 | } |
| 4226 | |
Max Reitz | 1f99668 | 2018-05-09 23:00:17 +0200 | [diff] [blame] | 4227 | if (!bs->drv->bdrv_amend_options) { |
| 4228 | error_report("Format driver '%s' does not support option amendment", |
Max Reitz | b2439d2 | 2014-12-02 18:32:47 +0100 | [diff] [blame] | 4229 | fmt); |
| 4230 | ret = -1; |
| 4231 | goto out; |
| 4232 | } |
| 4233 | |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4234 | /* Every driver supporting amendment must have amend_opts */ |
| 4235 | assert(bs->drv->amend_opts); |
Max Reitz | 1f99668 | 2018-05-09 23:00:17 +0200 | [diff] [blame] | 4236 | |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4237 | amend_opts = qemu_opts_append(amend_opts, bs->drv->amend_opts); |
| 4238 | opts = qemu_opts_create(amend_opts, NULL, 0, &error_abort); |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 4239 | if (!qemu_opts_do_parse(opts, options, NULL, &err)) { |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4240 | /* Try to parse options using the create options */ |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4241 | amend_opts = qemu_opts_append(amend_opts, bs->drv->create_opts); |
| 4242 | qemu_opts_del(opts); |
| 4243 | opts = qemu_opts_create(amend_opts, NULL, 0, &error_abort); |
Markus Armbruster | 9e194e0 | 2020-07-07 18:06:11 +0200 | [diff] [blame] | 4244 | if (qemu_opts_do_parse(opts, options, NULL, NULL)) { |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4245 | error_append_hint(&err, |
| 4246 | "This option is only supported for image creation\n"); |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4247 | } |
| 4248 | |
Paolo Bonzini | ece9086 | 2017-01-04 15:56:24 +0100 | [diff] [blame] | 4249 | error_report_err(err); |
| 4250 | ret = -1; |
| 4251 | goto out; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4252 | } |
| 4253 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4254 | /* In case the driver does not call amend_status_cb() */ |
| 4255 | qemu_progress_print(0.f, 0); |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4256 | ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL, force, &err); |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4257 | qemu_progress_print(100.f, 0); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4258 | if (ret < 0) { |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 4259 | error_report_err(err); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4260 | goto out; |
| 4261 | } |
| 4262 | |
| 4263 | out: |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4264 | qemu_progress_end(); |
| 4265 | |
Max Reitz | e814dff | 2015-08-20 16:00:38 -0700 | [diff] [blame] | 4266 | out_no_progress: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 4267 | blk_unref(blk); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 4268 | qemu_opts_del(opts); |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4269 | qemu_opts_free(amend_opts); |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 4270 | g_free(options); |
| 4271 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4272 | if (ret) { |
| 4273 | return 1; |
| 4274 | } |
| 4275 | return 0; |
| 4276 | } |
| 4277 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4278 | typedef struct BenchData { |
| 4279 | BlockBackend *blk; |
| 4280 | uint64_t image_size; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4281 | bool write; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4282 | int bufsize; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4283 | int step; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4284 | int nrreq; |
| 4285 | int n; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4286 | int flush_interval; |
| 4287 | bool drain_on_flush; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4288 | uint8_t *buf; |
| 4289 | QEMUIOVector *qiov; |
| 4290 | |
| 4291 | int in_flight; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4292 | bool in_flush; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4293 | uint64_t offset; |
| 4294 | } BenchData; |
| 4295 | |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4296 | static void bench_undrained_flush_cb(void *opaque, int ret) |
| 4297 | { |
| 4298 | if (ret < 0) { |
Markus Armbruster | df3c286 | 2016-08-03 13:37:51 +0200 | [diff] [blame] | 4299 | error_report("Failed flush request: %s", strerror(-ret)); |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4300 | exit(EXIT_FAILURE); |
| 4301 | } |
| 4302 | } |
| 4303 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4304 | static void bench_cb(void *opaque, int ret) |
| 4305 | { |
| 4306 | BenchData *b = opaque; |
| 4307 | BlockAIOCB *acb; |
| 4308 | |
| 4309 | if (ret < 0) { |
Markus Armbruster | df3c286 | 2016-08-03 13:37:51 +0200 | [diff] [blame] | 4310 | error_report("Failed request: %s", strerror(-ret)); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4311 | exit(EXIT_FAILURE); |
| 4312 | } |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4313 | |
| 4314 | if (b->in_flush) { |
| 4315 | /* Just finished a flush with drained queue: Start next requests */ |
| 4316 | assert(b->in_flight == 0); |
| 4317 | b->in_flush = false; |
| 4318 | } else if (b->in_flight > 0) { |
| 4319 | int remaining = b->n - b->in_flight; |
| 4320 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4321 | b->n--; |
| 4322 | b->in_flight--; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4323 | |
| 4324 | /* Time for flush? Drain queue if requested, then flush */ |
| 4325 | if (b->flush_interval && remaining % b->flush_interval == 0) { |
| 4326 | if (!b->in_flight || !b->drain_on_flush) { |
| 4327 | BlockCompletionFunc *cb; |
| 4328 | |
| 4329 | if (b->drain_on_flush) { |
| 4330 | b->in_flush = true; |
| 4331 | cb = bench_cb; |
| 4332 | } else { |
| 4333 | cb = bench_undrained_flush_cb; |
| 4334 | } |
| 4335 | |
| 4336 | acb = blk_aio_flush(b->blk, cb, b); |
| 4337 | if (!acb) { |
| 4338 | error_report("Failed to issue flush request"); |
| 4339 | exit(EXIT_FAILURE); |
| 4340 | } |
| 4341 | } |
| 4342 | if (b->drain_on_flush) { |
| 4343 | return; |
| 4344 | } |
| 4345 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4346 | } |
| 4347 | |
| 4348 | while (b->n > b->in_flight && b->in_flight < b->nrreq) { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4349 | int64_t offset = b->offset; |
| 4350 | /* blk_aio_* might look for completed I/Os and kick bench_cb |
| 4351 | * again, so make sure this operation is counted by in_flight |
| 4352 | * and b->offset is ready for the next submission. |
| 4353 | */ |
| 4354 | b->in_flight++; |
| 4355 | b->offset += b->step; |
| 4356 | b->offset %= b->image_size; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4357 | if (b->write) { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4358 | 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] | 4359 | } else { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4360 | 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] | 4361 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4362 | if (!acb) { |
| 4363 | error_report("Failed to issue request"); |
| 4364 | exit(EXIT_FAILURE); |
| 4365 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4366 | } |
| 4367 | } |
| 4368 | |
| 4369 | static int img_bench(int argc, char **argv) |
| 4370 | { |
| 4371 | int c, ret = 0; |
| 4372 | const char *fmt = NULL, *filename; |
| 4373 | bool quiet = false; |
| 4374 | bool image_opts = false; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4375 | bool is_write = false; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4376 | int count = 75000; |
| 4377 | int depth = 64; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4378 | int64_t offset = 0; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4379 | size_t bufsize = 4096; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4380 | int pattern = 0; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4381 | size_t step = 0; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4382 | int flush_interval = 0; |
| 4383 | bool drain_on_flush = true; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4384 | int64_t image_size; |
| 4385 | BlockBackend *blk = NULL; |
| 4386 | BenchData data = {}; |
| 4387 | int flags = 0; |
Kevin Wolf | 604e861 | 2016-06-14 11:29:32 +0200 | [diff] [blame] | 4388 | bool writethrough = false; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4389 | struct timeval t1, t2; |
| 4390 | int i; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4391 | bool force_share = false; |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4392 | size_t buf_size; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4393 | |
| 4394 | for (;;) { |
| 4395 | static const struct option long_options[] = { |
| 4396 | {"help", no_argument, 0, 'h'}, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4397 | {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL}, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4398 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4399 | {"pattern", required_argument, 0, OPTION_PATTERN}, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4400 | {"no-drain", no_argument, 0, OPTION_NO_DRAIN}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4401 | {"force-share", no_argument, 0, 'U'}, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4402 | {0, 0, 0, 0} |
| 4403 | }; |
Aarushi Mehta | cdd2677 | 2020-01-20 14:18:55 +0000 | [diff] [blame] | 4404 | c = getopt_long(argc, argv, ":hc:d:f:ni:o:qs:S:t:wU", long_options, |
| 4405 | NULL); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4406 | if (c == -1) { |
| 4407 | break; |
| 4408 | } |
| 4409 | |
| 4410 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4411 | case ':': |
| 4412 | missing_argument(argv[optind - 1]); |
| 4413 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4414 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4415 | unrecognized_option(argv[optind - 1]); |
| 4416 | break; |
| 4417 | case 'h': |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4418 | help(); |
| 4419 | break; |
| 4420 | case 'c': |
| 4421 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4422 | unsigned long res; |
| 4423 | |
| 4424 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4425 | error_report("Invalid request count specified"); |
| 4426 | return 1; |
| 4427 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4428 | count = res; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4429 | break; |
| 4430 | } |
| 4431 | case 'd': |
| 4432 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4433 | unsigned long res; |
| 4434 | |
| 4435 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4436 | error_report("Invalid queue depth specified"); |
| 4437 | return 1; |
| 4438 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4439 | depth = res; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4440 | break; |
| 4441 | } |
| 4442 | case 'f': |
| 4443 | fmt = optarg; |
| 4444 | break; |
| 4445 | case 'n': |
| 4446 | flags |= BDRV_O_NATIVE_AIO; |
| 4447 | break; |
Aarushi Mehta | cdd2677 | 2020-01-20 14:18:55 +0000 | [diff] [blame] | 4448 | case 'i': |
| 4449 | ret = bdrv_parse_aio(optarg, &flags); |
| 4450 | if (ret < 0) { |
| 4451 | error_report("Invalid aio option: %s", optarg); |
| 4452 | ret = -1; |
| 4453 | goto out; |
| 4454 | } |
| 4455 | break; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4456 | case 'o': |
| 4457 | { |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4458 | offset = cvtnum("offset", optarg); |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4459 | if (offset < 0) { |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4460 | return 1; |
| 4461 | } |
| 4462 | break; |
| 4463 | } |
| 4464 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4465 | case 'q': |
| 4466 | quiet = true; |
| 4467 | break; |
| 4468 | case 's': |
| 4469 | { |
| 4470 | int64_t sval; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4471 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4472 | sval = cvtnum_full("buffer size", optarg, 0, INT_MAX); |
| 4473 | if (sval < 0) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4474 | return 1; |
| 4475 | } |
| 4476 | |
| 4477 | bufsize = sval; |
| 4478 | break; |
| 4479 | } |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4480 | case 'S': |
| 4481 | { |
| 4482 | int64_t sval; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4483 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4484 | sval = cvtnum_full("step_size", optarg, 0, INT_MAX); |
| 4485 | if (sval < 0) { |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4486 | return 1; |
| 4487 | } |
| 4488 | |
| 4489 | step = sval; |
| 4490 | break; |
| 4491 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4492 | case 't': |
| 4493 | ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough); |
| 4494 | if (ret < 0) { |
| 4495 | error_report("Invalid cache mode"); |
| 4496 | ret = -1; |
| 4497 | goto out; |
| 4498 | } |
| 4499 | break; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4500 | case 'w': |
| 4501 | flags |= BDRV_O_RDWR; |
| 4502 | is_write = true; |
| 4503 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4504 | case 'U': |
| 4505 | force_share = true; |
| 4506 | break; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4507 | case OPTION_PATTERN: |
| 4508 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4509 | unsigned long res; |
| 4510 | |
| 4511 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) { |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4512 | error_report("Invalid pattern byte specified"); |
| 4513 | return 1; |
| 4514 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4515 | pattern = res; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4516 | break; |
| 4517 | } |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4518 | case OPTION_FLUSH_INTERVAL: |
| 4519 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4520 | unsigned long res; |
| 4521 | |
| 4522 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4523 | error_report("Invalid flush interval specified"); |
| 4524 | return 1; |
| 4525 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4526 | flush_interval = res; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4527 | break; |
| 4528 | } |
| 4529 | case OPTION_NO_DRAIN: |
| 4530 | drain_on_flush = false; |
| 4531 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4532 | case OPTION_IMAGE_OPTS: |
| 4533 | image_opts = true; |
| 4534 | break; |
| 4535 | } |
| 4536 | } |
| 4537 | |
| 4538 | if (optind != argc - 1) { |
| 4539 | error_exit("Expecting one image file name"); |
| 4540 | } |
| 4541 | filename = argv[argc - 1]; |
| 4542 | |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4543 | if (!is_write && flush_interval) { |
| 4544 | error_report("--flush-interval is only available in write tests"); |
| 4545 | ret = -1; |
| 4546 | goto out; |
| 4547 | } |
| 4548 | if (flush_interval && flush_interval < depth) { |
| 4549 | error_report("Flush interval can't be smaller than depth"); |
| 4550 | ret = -1; |
| 4551 | goto out; |
| 4552 | } |
| 4553 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4554 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 4555 | force_share); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4556 | if (!blk) { |
| 4557 | ret = -1; |
| 4558 | goto out; |
| 4559 | } |
| 4560 | |
| 4561 | image_size = blk_getlength(blk); |
| 4562 | if (image_size < 0) { |
| 4563 | ret = image_size; |
| 4564 | goto out; |
| 4565 | } |
| 4566 | |
| 4567 | data = (BenchData) { |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4568 | .blk = blk, |
| 4569 | .image_size = image_size, |
| 4570 | .bufsize = bufsize, |
| 4571 | .step = step ?: bufsize, |
| 4572 | .nrreq = depth, |
| 4573 | .n = count, |
| 4574 | .offset = offset, |
| 4575 | .write = is_write, |
| 4576 | .flush_interval = flush_interval, |
| 4577 | .drain_on_flush = drain_on_flush, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4578 | }; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4579 | printf("Sending %d %s requests, %d bytes each, %d in parallel " |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4580 | "(starting at offset %" PRId64 ", step size %d)\n", |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4581 | data.n, data.write ? "write" : "read", data.bufsize, data.nrreq, |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4582 | data.offset, data.step); |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4583 | if (flush_interval) { |
| 4584 | printf("Sending flush every %d requests\n", flush_interval); |
| 4585 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4586 | |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4587 | buf_size = data.nrreq * data.bufsize; |
| 4588 | data.buf = blk_blockalign(blk, buf_size); |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4589 | memset(data.buf, pattern, data.nrreq * data.bufsize); |
| 4590 | |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4591 | blk_register_buf(blk, data.buf, buf_size); |
| 4592 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4593 | data.qiov = g_new(QEMUIOVector, data.nrreq); |
| 4594 | for (i = 0; i < data.nrreq; i++) { |
| 4595 | qemu_iovec_init(&data.qiov[i], 1); |
| 4596 | qemu_iovec_add(&data.qiov[i], |
| 4597 | data.buf + i * data.bufsize, data.bufsize); |
| 4598 | } |
| 4599 | |
| 4600 | gettimeofday(&t1, NULL); |
| 4601 | bench_cb(&data, 0); |
| 4602 | |
| 4603 | while (data.n > 0) { |
| 4604 | main_loop_wait(false); |
| 4605 | } |
| 4606 | gettimeofday(&t2, NULL); |
| 4607 | |
| 4608 | printf("Run completed in %3.3f seconds.\n", |
| 4609 | (t2.tv_sec - t1.tv_sec) |
| 4610 | + ((double)(t2.tv_usec - t1.tv_usec) / 1000000)); |
| 4611 | |
| 4612 | out: |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4613 | if (data.buf) { |
| 4614 | blk_unregister_buf(blk, data.buf); |
| 4615 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4616 | qemu_vfree(data.buf); |
| 4617 | blk_unref(blk); |
| 4618 | |
| 4619 | if (ret) { |
| 4620 | return 1; |
| 4621 | } |
| 4622 | return 0; |
| 4623 | } |
| 4624 | |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4625 | enum ImgBitmapAct { |
| 4626 | BITMAP_ADD, |
| 4627 | BITMAP_REMOVE, |
| 4628 | BITMAP_CLEAR, |
| 4629 | BITMAP_ENABLE, |
| 4630 | BITMAP_DISABLE, |
| 4631 | BITMAP_MERGE, |
| 4632 | }; |
| 4633 | typedef struct ImgBitmapAction { |
| 4634 | enum ImgBitmapAct act; |
| 4635 | const char *src; /* only used for merge */ |
| 4636 | QSIMPLEQ_ENTRY(ImgBitmapAction) next; |
| 4637 | } ImgBitmapAction; |
| 4638 | |
| 4639 | static int img_bitmap(int argc, char **argv) |
| 4640 | { |
| 4641 | Error *err = NULL; |
| 4642 | int c, ret = 1; |
| 4643 | QemuOpts *opts = NULL; |
| 4644 | const char *fmt = NULL, *src_fmt = NULL, *src_filename = NULL; |
| 4645 | const char *filename, *bitmap; |
| 4646 | BlockBackend *blk = NULL, *src = NULL; |
| 4647 | BlockDriverState *bs = NULL, *src_bs = NULL; |
| 4648 | bool image_opts = false; |
| 4649 | int64_t granularity = 0; |
| 4650 | bool add = false, merge = false; |
| 4651 | QSIMPLEQ_HEAD(, ImgBitmapAction) actions; |
| 4652 | ImgBitmapAction *act, *act_next; |
| 4653 | const char *op; |
| 4654 | |
| 4655 | QSIMPLEQ_INIT(&actions); |
| 4656 | |
| 4657 | for (;;) { |
| 4658 | static const struct option long_options[] = { |
| 4659 | {"help", no_argument, 0, 'h'}, |
| 4660 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 4661 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
| 4662 | {"add", no_argument, 0, OPTION_ADD}, |
| 4663 | {"remove", no_argument, 0, OPTION_REMOVE}, |
| 4664 | {"clear", no_argument, 0, OPTION_CLEAR}, |
| 4665 | {"enable", no_argument, 0, OPTION_ENABLE}, |
| 4666 | {"disable", no_argument, 0, OPTION_DISABLE}, |
| 4667 | {"merge", required_argument, 0, OPTION_MERGE}, |
| 4668 | {"granularity", required_argument, 0, 'g'}, |
| 4669 | {"source-file", required_argument, 0, 'b'}, |
| 4670 | {"source-format", required_argument, 0, 'F'}, |
| 4671 | {0, 0, 0, 0} |
| 4672 | }; |
| 4673 | c = getopt_long(argc, argv, ":b:f:F:g:h", long_options, NULL); |
| 4674 | if (c == -1) { |
| 4675 | break; |
| 4676 | } |
| 4677 | |
| 4678 | switch (c) { |
| 4679 | case ':': |
| 4680 | missing_argument(argv[optind - 1]); |
| 4681 | break; |
| 4682 | case '?': |
| 4683 | unrecognized_option(argv[optind - 1]); |
| 4684 | break; |
| 4685 | case 'h': |
| 4686 | help(); |
| 4687 | break; |
| 4688 | case 'b': |
| 4689 | src_filename = optarg; |
| 4690 | break; |
| 4691 | case 'f': |
| 4692 | fmt = optarg; |
| 4693 | break; |
| 4694 | case 'F': |
| 4695 | src_fmt = optarg; |
| 4696 | break; |
| 4697 | case 'g': |
| 4698 | granularity = cvtnum("granularity", optarg); |
| 4699 | if (granularity < 0) { |
| 4700 | return 1; |
| 4701 | } |
| 4702 | break; |
| 4703 | case OPTION_ADD: |
| 4704 | act = g_new0(ImgBitmapAction, 1); |
| 4705 | act->act = BITMAP_ADD; |
| 4706 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4707 | add = true; |
| 4708 | break; |
| 4709 | case OPTION_REMOVE: |
| 4710 | act = g_new0(ImgBitmapAction, 1); |
| 4711 | act->act = BITMAP_REMOVE; |
| 4712 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4713 | break; |
| 4714 | case OPTION_CLEAR: |
| 4715 | act = g_new0(ImgBitmapAction, 1); |
| 4716 | act->act = BITMAP_CLEAR; |
| 4717 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4718 | break; |
| 4719 | case OPTION_ENABLE: |
| 4720 | act = g_new0(ImgBitmapAction, 1); |
| 4721 | act->act = BITMAP_ENABLE; |
| 4722 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4723 | break; |
| 4724 | case OPTION_DISABLE: |
| 4725 | act = g_new0(ImgBitmapAction, 1); |
| 4726 | act->act = BITMAP_DISABLE; |
| 4727 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4728 | break; |
| 4729 | case OPTION_MERGE: |
| 4730 | act = g_new0(ImgBitmapAction, 1); |
| 4731 | act->act = BITMAP_MERGE; |
| 4732 | act->src = optarg; |
| 4733 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4734 | merge = true; |
| 4735 | break; |
| 4736 | case OPTION_OBJECT: |
| 4737 | opts = qemu_opts_parse_noisily(&qemu_object_opts, optarg, true); |
| 4738 | if (!opts) { |
| 4739 | goto out; |
| 4740 | } |
| 4741 | break; |
| 4742 | case OPTION_IMAGE_OPTS: |
| 4743 | image_opts = true; |
| 4744 | break; |
| 4745 | } |
| 4746 | } |
| 4747 | |
| 4748 | if (qemu_opts_foreach(&qemu_object_opts, |
| 4749 | user_creatable_add_opts_foreach, |
| 4750 | qemu_img_object_print_help, &error_fatal)) { |
| 4751 | goto out; |
| 4752 | } |
| 4753 | |
| 4754 | if (QSIMPLEQ_EMPTY(&actions)) { |
| 4755 | error_report("Need at least one of --add, --remove, --clear, " |
| 4756 | "--enable, --disable, or --merge"); |
| 4757 | goto out; |
| 4758 | } |
| 4759 | |
| 4760 | if (granularity && !add) { |
| 4761 | error_report("granularity only supported with --add"); |
| 4762 | goto out; |
| 4763 | } |
| 4764 | if (src_fmt && !src_filename) { |
| 4765 | error_report("-F only supported with -b"); |
| 4766 | goto out; |
| 4767 | } |
| 4768 | if (src_filename && !merge) { |
| 4769 | error_report("Merge bitmap source file only supported with " |
| 4770 | "--merge"); |
| 4771 | goto out; |
| 4772 | } |
| 4773 | |
| 4774 | if (optind != argc - 2) { |
| 4775 | error_report("Expecting filename and bitmap name"); |
| 4776 | goto out; |
| 4777 | } |
| 4778 | |
| 4779 | filename = argv[optind]; |
| 4780 | bitmap = argv[optind + 1]; |
| 4781 | |
| 4782 | blk = img_open(image_opts, filename, fmt, BDRV_O_RDWR, false, false, |
| 4783 | false); |
| 4784 | if (!blk) { |
| 4785 | goto out; |
| 4786 | } |
| 4787 | bs = blk_bs(blk); |
| 4788 | if (src_filename) { |
| 4789 | src = img_open(false, src_filename, src_fmt, 0, false, false, false); |
| 4790 | if (!src) { |
| 4791 | goto out; |
| 4792 | } |
| 4793 | src_bs = blk_bs(src); |
| 4794 | } else { |
| 4795 | src_bs = bs; |
| 4796 | } |
| 4797 | |
| 4798 | QSIMPLEQ_FOREACH_SAFE(act, &actions, next, act_next) { |
| 4799 | switch (act->act) { |
| 4800 | case BITMAP_ADD: |
| 4801 | qmp_block_dirty_bitmap_add(bs->node_name, bitmap, |
| 4802 | !!granularity, granularity, true, true, |
| 4803 | false, false, &err); |
| 4804 | op = "add"; |
| 4805 | break; |
| 4806 | case BITMAP_REMOVE: |
| 4807 | qmp_block_dirty_bitmap_remove(bs->node_name, bitmap, &err); |
| 4808 | op = "remove"; |
| 4809 | break; |
| 4810 | case BITMAP_CLEAR: |
| 4811 | qmp_block_dirty_bitmap_clear(bs->node_name, bitmap, &err); |
| 4812 | op = "clear"; |
| 4813 | break; |
| 4814 | case BITMAP_ENABLE: |
| 4815 | qmp_block_dirty_bitmap_enable(bs->node_name, bitmap, &err); |
| 4816 | op = "enable"; |
| 4817 | break; |
| 4818 | case BITMAP_DISABLE: |
| 4819 | qmp_block_dirty_bitmap_disable(bs->node_name, bitmap, &err); |
| 4820 | op = "disable"; |
| 4821 | break; |
Eric Blake | 6c729dd | 2020-05-21 14:21:35 -0500 | [diff] [blame] | 4822 | case BITMAP_MERGE: |
| 4823 | do_dirty_bitmap_merge(bs->node_name, bitmap, src_bs->node_name, |
| 4824 | act->src, &err); |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4825 | op = "merge"; |
| 4826 | break; |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4827 | default: |
| 4828 | g_assert_not_reached(); |
| 4829 | } |
| 4830 | |
| 4831 | if (err) { |
| 4832 | error_reportf_err(err, "Operation %s on bitmap %s failed: ", |
| 4833 | op, bitmap); |
| 4834 | goto out; |
| 4835 | } |
| 4836 | g_free(act); |
| 4837 | } |
| 4838 | |
| 4839 | ret = 0; |
| 4840 | |
| 4841 | out: |
| 4842 | blk_unref(src); |
| 4843 | blk_unref(blk); |
| 4844 | qemu_opts_del(opts); |
| 4845 | return ret; |
| 4846 | } |
| 4847 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4848 | #define C_BS 01 |
| 4849 | #define C_COUNT 02 |
| 4850 | #define C_IF 04 |
| 4851 | #define C_OF 010 |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4852 | #define C_SKIP 020 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4853 | |
| 4854 | struct DdInfo { |
| 4855 | unsigned int flags; |
| 4856 | int64_t count; |
| 4857 | }; |
| 4858 | |
| 4859 | struct DdIo { |
| 4860 | int bsz; /* Block size */ |
| 4861 | char *filename; |
| 4862 | uint8_t *buf; |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4863 | int64_t offset; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4864 | }; |
| 4865 | |
| 4866 | struct DdOpts { |
| 4867 | const char *name; |
| 4868 | int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *); |
| 4869 | unsigned int flag; |
| 4870 | }; |
| 4871 | |
| 4872 | static int img_dd_bs(const char *arg, |
| 4873 | struct DdIo *in, struct DdIo *out, |
| 4874 | struct DdInfo *dd) |
| 4875 | { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4876 | int64_t res; |
| 4877 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4878 | res = cvtnum_full("bs", arg, 1, INT_MAX); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4879 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4880 | if (res < 0) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4881 | return 1; |
| 4882 | } |
| 4883 | in->bsz = out->bsz = res; |
| 4884 | |
| 4885 | return 0; |
| 4886 | } |
| 4887 | |
| 4888 | static int img_dd_count(const char *arg, |
| 4889 | struct DdIo *in, struct DdIo *out, |
| 4890 | struct DdInfo *dd) |
| 4891 | { |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4892 | dd->count = cvtnum("count", arg); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4893 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4894 | if (dd->count < 0) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4895 | return 1; |
| 4896 | } |
| 4897 | |
| 4898 | return 0; |
| 4899 | } |
| 4900 | |
| 4901 | static int img_dd_if(const char *arg, |
| 4902 | struct DdIo *in, struct DdIo *out, |
| 4903 | struct DdInfo *dd) |
| 4904 | { |
| 4905 | in->filename = g_strdup(arg); |
| 4906 | |
| 4907 | return 0; |
| 4908 | } |
| 4909 | |
| 4910 | static int img_dd_of(const char *arg, |
| 4911 | struct DdIo *in, struct DdIo *out, |
| 4912 | struct DdInfo *dd) |
| 4913 | { |
| 4914 | out->filename = g_strdup(arg); |
| 4915 | |
| 4916 | return 0; |
| 4917 | } |
| 4918 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4919 | static int img_dd_skip(const char *arg, |
| 4920 | struct DdIo *in, struct DdIo *out, |
| 4921 | struct DdInfo *dd) |
| 4922 | { |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4923 | in->offset = cvtnum("skip", arg); |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4924 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4925 | if (in->offset < 0) { |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4926 | return 1; |
| 4927 | } |
| 4928 | |
| 4929 | return 0; |
| 4930 | } |
| 4931 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4932 | static int img_dd(int argc, char **argv) |
| 4933 | { |
| 4934 | int ret = 0; |
| 4935 | char *arg = NULL; |
| 4936 | char *tmp; |
| 4937 | BlockDriver *drv = NULL, *proto_drv = NULL; |
| 4938 | BlockBackend *blk1 = NULL, *blk2 = NULL; |
| 4939 | QemuOpts *opts = NULL; |
| 4940 | QemuOptsList *create_opts = NULL; |
| 4941 | Error *local_err = NULL; |
| 4942 | bool image_opts = false; |
| 4943 | int c, i; |
| 4944 | const char *out_fmt = "raw"; |
| 4945 | const char *fmt = NULL; |
| 4946 | int64_t size = 0; |
| 4947 | int64_t block_count = 0, out_pos, in_pos; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4948 | bool force_share = false; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4949 | struct DdInfo dd = { |
| 4950 | .flags = 0, |
| 4951 | .count = 0, |
| 4952 | }; |
| 4953 | struct DdIo in = { |
| 4954 | .bsz = 512, /* Block size is by default 512 bytes */ |
| 4955 | .filename = NULL, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4956 | .buf = NULL, |
| 4957 | .offset = 0 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4958 | }; |
| 4959 | struct DdIo out = { |
| 4960 | .bsz = 512, |
| 4961 | .filename = NULL, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4962 | .buf = NULL, |
| 4963 | .offset = 0 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4964 | }; |
| 4965 | |
| 4966 | const struct DdOpts options[] = { |
| 4967 | { "bs", img_dd_bs, C_BS }, |
| 4968 | { "count", img_dd_count, C_COUNT }, |
| 4969 | { "if", img_dd_if, C_IF }, |
| 4970 | { "of", img_dd_of, C_OF }, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4971 | { "skip", img_dd_skip, C_SKIP }, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4972 | { NULL, NULL, 0 } |
| 4973 | }; |
| 4974 | const struct option long_options[] = { |
| 4975 | { "help", no_argument, 0, 'h'}, |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 4976 | { "object", required_argument, 0, OPTION_OBJECT}, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4977 | { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4978 | { "force-share", no_argument, 0, 'U'}, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4979 | { 0, 0, 0, 0 } |
| 4980 | }; |
| 4981 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4982 | while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4983 | if (c == EOF) { |
| 4984 | break; |
| 4985 | } |
| 4986 | switch (c) { |
| 4987 | case 'O': |
| 4988 | out_fmt = optarg; |
| 4989 | break; |
| 4990 | case 'f': |
| 4991 | fmt = optarg; |
| 4992 | break; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4993 | case ':': |
| 4994 | missing_argument(argv[optind - 1]); |
| 4995 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4996 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4997 | unrecognized_option(argv[optind - 1]); |
| 4998 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4999 | case 'h': |
| 5000 | help(); |
| 5001 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 5002 | case 'U': |
| 5003 | force_share = true; |
| 5004 | break; |
Stefan Hajnoczi | 2a24570 | 2017-06-19 16:00:02 +0100 | [diff] [blame] | 5005 | case OPTION_OBJECT: |
| 5006 | if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) { |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 5007 | ret = -1; |
| 5008 | goto out; |
| 5009 | } |
Stefan Hajnoczi | 2a24570 | 2017-06-19 16:00:02 +0100 | [diff] [blame] | 5010 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5011 | case OPTION_IMAGE_OPTS: |
| 5012 | image_opts = true; |
| 5013 | break; |
| 5014 | } |
| 5015 | } |
| 5016 | |
| 5017 | for (i = optind; i < argc; i++) { |
| 5018 | int j; |
| 5019 | arg = g_strdup(argv[i]); |
| 5020 | |
| 5021 | tmp = strchr(arg, '='); |
| 5022 | if (tmp == NULL) { |
| 5023 | error_report("unrecognized operand %s", arg); |
| 5024 | ret = -1; |
| 5025 | goto out; |
| 5026 | } |
| 5027 | |
| 5028 | *tmp++ = '\0'; |
| 5029 | |
| 5030 | for (j = 0; options[j].name != NULL; j++) { |
| 5031 | if (!strcmp(arg, options[j].name)) { |
| 5032 | break; |
| 5033 | } |
| 5034 | } |
| 5035 | if (options[j].name == NULL) { |
| 5036 | error_report("unrecognized operand %s", arg); |
| 5037 | ret = -1; |
| 5038 | goto out; |
| 5039 | } |
| 5040 | |
| 5041 | if (options[j].f(tmp, &in, &out, &dd) != 0) { |
| 5042 | ret = -1; |
| 5043 | goto out; |
| 5044 | } |
| 5045 | dd.flags |= options[j].flag; |
| 5046 | g_free(arg); |
| 5047 | arg = NULL; |
| 5048 | } |
| 5049 | |
| 5050 | if (!(dd.flags & C_IF && dd.flags & C_OF)) { |
| 5051 | error_report("Must specify both input and output files"); |
| 5052 | ret = -1; |
| 5053 | goto out; |
| 5054 | } |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 5055 | |
| 5056 | if (qemu_opts_foreach(&qemu_object_opts, |
| 5057 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 5058 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 5059 | ret = -1; |
| 5060 | goto out; |
| 5061 | } |
| 5062 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 5063 | blk1 = img_open(image_opts, in.filename, fmt, 0, false, false, |
| 5064 | force_share); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5065 | |
| 5066 | if (!blk1) { |
| 5067 | ret = -1; |
| 5068 | goto out; |
| 5069 | } |
| 5070 | |
| 5071 | drv = bdrv_find_format(out_fmt); |
| 5072 | if (!drv) { |
| 5073 | error_report("Unknown file format"); |
| 5074 | ret = -1; |
| 5075 | goto out; |
| 5076 | } |
| 5077 | proto_drv = bdrv_find_protocol(out.filename, true, &local_err); |
| 5078 | |
| 5079 | if (!proto_drv) { |
| 5080 | error_report_err(local_err); |
| 5081 | ret = -1; |
| 5082 | goto out; |
| 5083 | } |
| 5084 | if (!drv->create_opts) { |
| 5085 | error_report("Format driver '%s' does not support image creation", |
| 5086 | drv->format_name); |
| 5087 | ret = -1; |
| 5088 | goto out; |
| 5089 | } |
| 5090 | if (!proto_drv->create_opts) { |
| 5091 | error_report("Protocol driver '%s' does not support image creation", |
| 5092 | proto_drv->format_name); |
| 5093 | ret = -1; |
| 5094 | goto out; |
| 5095 | } |
| 5096 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 5097 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
| 5098 | |
| 5099 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
| 5100 | |
| 5101 | size = blk_getlength(blk1); |
| 5102 | if (size < 0) { |
| 5103 | error_report("Failed to get size for '%s'", in.filename); |
| 5104 | ret = -1; |
| 5105 | goto out; |
| 5106 | } |
| 5107 | |
| 5108 | if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz && |
| 5109 | dd.count * in.bsz < size) { |
| 5110 | size = dd.count * in.bsz; |
| 5111 | } |
| 5112 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 5113 | /* Overflow means the specified offset is beyond input image's size */ |
| 5114 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || |
| 5115 | size < in.bsz * in.offset)) { |
| 5116 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort); |
| 5117 | } else { |
| 5118 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, |
| 5119 | size - in.bsz * in.offset, &error_abort); |
| 5120 | } |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5121 | |
| 5122 | ret = bdrv_create(drv, out.filename, opts, &local_err); |
| 5123 | if (ret < 0) { |
| 5124 | error_reportf_err(local_err, |
| 5125 | "%s: error while creating output image: ", |
| 5126 | out.filename); |
| 5127 | ret = -1; |
| 5128 | goto out; |
| 5129 | } |
| 5130 | |
Daniel P. Berrange | ea204dd | 2017-05-15 17:47:10 +0100 | [diff] [blame] | 5131 | /* TODO, we can't honour --image-opts for the target, |
| 5132 | * since it needs to be given in a format compatible |
| 5133 | * with the bdrv_create() call above which does not |
| 5134 | * support image-opts style. |
| 5135 | */ |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 5136 | 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] | 5137 | false, false, false); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5138 | |
| 5139 | if (!blk2) { |
| 5140 | ret = -1; |
| 5141 | goto out; |
| 5142 | } |
| 5143 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 5144 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || |
| 5145 | size < in.offset * in.bsz)) { |
| 5146 | /* We give a warning if the skip option is bigger than the input |
| 5147 | * size and create an empty output disk image (i.e. like dd(1)). |
| 5148 | */ |
| 5149 | error_report("%s: cannot skip to specified offset", in.filename); |
| 5150 | in_pos = size; |
| 5151 | } else { |
| 5152 | in_pos = in.offset * in.bsz; |
| 5153 | } |
| 5154 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5155 | in.buf = g_new(uint8_t, in.bsz); |
| 5156 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 5157 | for (out_pos = 0; in_pos < size; block_count++) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5158 | int in_ret, out_ret; |
| 5159 | |
| 5160 | if (in_pos + in.bsz > size) { |
| 5161 | in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos); |
| 5162 | } else { |
| 5163 | in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz); |
| 5164 | } |
| 5165 | if (in_ret < 0) { |
| 5166 | error_report("error while reading from input image file: %s", |
| 5167 | strerror(-in_ret)); |
| 5168 | ret = -1; |
| 5169 | goto out; |
| 5170 | } |
| 5171 | in_pos += in_ret; |
| 5172 | |
| 5173 | out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0); |
| 5174 | |
| 5175 | if (out_ret < 0) { |
| 5176 | error_report("error while writing to output image file: %s", |
| 5177 | strerror(-out_ret)); |
| 5178 | ret = -1; |
| 5179 | goto out; |
| 5180 | } |
| 5181 | out_pos += out_ret; |
| 5182 | } |
| 5183 | |
| 5184 | out: |
| 5185 | g_free(arg); |
| 5186 | qemu_opts_del(opts); |
| 5187 | qemu_opts_free(create_opts); |
| 5188 | blk_unref(blk1); |
| 5189 | blk_unref(blk2); |
| 5190 | g_free(in.filename); |
| 5191 | g_free(out.filename); |
| 5192 | g_free(in.buf); |
| 5193 | g_free(out.buf); |
| 5194 | |
| 5195 | if (ret) { |
| 5196 | return 1; |
| 5197 | } |
| 5198 | return 0; |
| 5199 | } |
| 5200 | |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5201 | static void dump_json_block_measure_info(BlockMeasureInfo *info) |
| 5202 | { |
| 5203 | QString *str; |
| 5204 | QObject *obj; |
| 5205 | Visitor *v = qobject_output_visitor_new(&obj); |
| 5206 | |
| 5207 | visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort); |
| 5208 | visit_complete(v, &obj); |
| 5209 | str = qobject_to_json_pretty(obj); |
| 5210 | assert(str != NULL); |
| 5211 | printf("%s\n", qstring_get_str(str)); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 5212 | qobject_unref(obj); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5213 | visit_free(v); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 5214 | qobject_unref(str); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5215 | } |
| 5216 | |
| 5217 | static int img_measure(int argc, char **argv) |
| 5218 | { |
| 5219 | static const struct option long_options[] = { |
| 5220 | {"help", no_argument, 0, 'h'}, |
| 5221 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
| 5222 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 5223 | {"output", required_argument, 0, OPTION_OUTPUT}, |
| 5224 | {"size", required_argument, 0, OPTION_SIZE}, |
| 5225 | {"force-share", no_argument, 0, 'U'}, |
| 5226 | {0, 0, 0, 0} |
| 5227 | }; |
| 5228 | OutputFormat output_format = OFORMAT_HUMAN; |
| 5229 | BlockBackend *in_blk = NULL; |
| 5230 | BlockDriver *drv; |
| 5231 | const char *filename = NULL; |
| 5232 | const char *fmt = NULL; |
| 5233 | const char *out_fmt = "raw"; |
| 5234 | char *options = NULL; |
| 5235 | char *snapshot_name = NULL; |
| 5236 | bool force_share = false; |
| 5237 | QemuOpts *opts = NULL; |
| 5238 | QemuOpts *object_opts = NULL; |
| 5239 | QemuOpts *sn_opts = NULL; |
| 5240 | QemuOptsList *create_opts = NULL; |
| 5241 | bool image_opts = false; |
| 5242 | uint64_t img_size = UINT64_MAX; |
| 5243 | BlockMeasureInfo *info = NULL; |
| 5244 | Error *local_err = NULL; |
| 5245 | int ret = 1; |
| 5246 | int c; |
| 5247 | |
| 5248 | while ((c = getopt_long(argc, argv, "hf:O:o:l:U", |
| 5249 | long_options, NULL)) != -1) { |
| 5250 | switch (c) { |
| 5251 | case '?': |
| 5252 | case 'h': |
| 5253 | help(); |
| 5254 | break; |
| 5255 | case 'f': |
| 5256 | fmt = optarg; |
| 5257 | break; |
| 5258 | case 'O': |
| 5259 | out_fmt = optarg; |
| 5260 | break; |
| 5261 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 5262 | if (accumulate_options(&options, optarg) < 0) { |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5263 | goto out; |
| 5264 | } |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5265 | break; |
| 5266 | case 'l': |
| 5267 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { |
| 5268 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
| 5269 | optarg, false); |
| 5270 | if (!sn_opts) { |
| 5271 | error_report("Failed in parsing snapshot param '%s'", |
| 5272 | optarg); |
| 5273 | goto out; |
| 5274 | } |
| 5275 | } else { |
| 5276 | snapshot_name = optarg; |
| 5277 | } |
| 5278 | break; |
| 5279 | case 'U': |
| 5280 | force_share = true; |
| 5281 | break; |
| 5282 | case OPTION_OBJECT: |
| 5283 | object_opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 5284 | optarg, true); |
| 5285 | if (!object_opts) { |
| 5286 | goto out; |
| 5287 | } |
| 5288 | break; |
| 5289 | case OPTION_IMAGE_OPTS: |
| 5290 | image_opts = true; |
| 5291 | break; |
| 5292 | case OPTION_OUTPUT: |
| 5293 | if (!strcmp(optarg, "json")) { |
| 5294 | output_format = OFORMAT_JSON; |
| 5295 | } else if (!strcmp(optarg, "human")) { |
| 5296 | output_format = OFORMAT_HUMAN; |
| 5297 | } else { |
| 5298 | error_report("--output must be used with human or json " |
| 5299 | "as argument."); |
| 5300 | goto out; |
| 5301 | } |
| 5302 | break; |
| 5303 | case OPTION_SIZE: |
| 5304 | { |
| 5305 | int64_t sval; |
| 5306 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 5307 | sval = cvtnum("image size", optarg); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5308 | if (sval < 0) { |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5309 | goto out; |
| 5310 | } |
| 5311 | img_size = (uint64_t)sval; |
| 5312 | } |
| 5313 | break; |
| 5314 | } |
| 5315 | } |
| 5316 | |
| 5317 | if (qemu_opts_foreach(&qemu_object_opts, |
| 5318 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 5319 | qemu_img_object_print_help, &error_fatal)) { |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5320 | goto out; |
| 5321 | } |
| 5322 | |
| 5323 | if (argc - optind > 1) { |
| 5324 | error_report("At most one filename argument is allowed."); |
| 5325 | goto out; |
| 5326 | } else if (argc - optind == 1) { |
| 5327 | filename = argv[optind]; |
| 5328 | } |
| 5329 | |
Stefan Hajnoczi | c3673dc | 2020-02-21 11:25:21 +0000 | [diff] [blame] | 5330 | if (!filename && (image_opts || fmt || snapshot_name || sn_opts)) { |
| 5331 | error_report("--image-opts, -f, and -l require a filename argument."); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5332 | goto out; |
| 5333 | } |
| 5334 | if (filename && img_size != UINT64_MAX) { |
| 5335 | error_report("--size N cannot be used together with a filename."); |
| 5336 | goto out; |
| 5337 | } |
| 5338 | if (!filename && img_size == UINT64_MAX) { |
| 5339 | error_report("Either --size N or one filename must be specified."); |
| 5340 | goto out; |
| 5341 | } |
| 5342 | |
| 5343 | if (filename) { |
| 5344 | in_blk = img_open(image_opts, filename, fmt, 0, |
| 5345 | false, false, force_share); |
| 5346 | if (!in_blk) { |
| 5347 | goto out; |
| 5348 | } |
| 5349 | |
| 5350 | if (sn_opts) { |
| 5351 | bdrv_snapshot_load_tmp(blk_bs(in_blk), |
| 5352 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), |
| 5353 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), |
| 5354 | &local_err); |
| 5355 | } else if (snapshot_name != NULL) { |
| 5356 | bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk), |
| 5357 | snapshot_name, &local_err); |
| 5358 | } |
| 5359 | if (local_err) { |
| 5360 | error_reportf_err(local_err, "Failed to load snapshot: "); |
| 5361 | goto out; |
| 5362 | } |
| 5363 | } |
| 5364 | |
| 5365 | drv = bdrv_find_format(out_fmt); |
| 5366 | if (!drv) { |
| 5367 | error_report("Unknown file format '%s'", out_fmt); |
| 5368 | goto out; |
| 5369 | } |
| 5370 | if (!drv->create_opts) { |
| 5371 | error_report("Format driver '%s' does not support image creation", |
| 5372 | drv->format_name); |
| 5373 | goto out; |
| 5374 | } |
| 5375 | |
| 5376 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 5377 | create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts); |
| 5378 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
| 5379 | if (options) { |
Markus Armbruster | 235e59c | 2020-07-07 18:05:42 +0200 | [diff] [blame] | 5380 | if (!qemu_opts_do_parse(opts, options, NULL, &local_err)) { |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5381 | error_report_err(local_err); |
| 5382 | error_report("Invalid options for file format '%s'", out_fmt); |
| 5383 | goto out; |
| 5384 | } |
| 5385 | } |
| 5386 | if (img_size != UINT64_MAX) { |
| 5387 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort); |
| 5388 | } |
| 5389 | |
| 5390 | info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err); |
| 5391 | if (local_err) { |
| 5392 | error_report_err(local_err); |
| 5393 | goto out; |
| 5394 | } |
| 5395 | |
| 5396 | if (output_format == OFORMAT_HUMAN) { |
| 5397 | printf("required size: %" PRIu64 "\n", info->required); |
| 5398 | printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated); |
Eric Blake | 5d72c68 | 2020-05-21 14:21:34 -0500 | [diff] [blame] | 5399 | if (info->has_bitmaps) { |
| 5400 | printf("bitmaps size: %" PRIu64 "\n", info->bitmaps); |
| 5401 | } |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5402 | } else { |
| 5403 | dump_json_block_measure_info(info); |
| 5404 | } |
| 5405 | |
| 5406 | ret = 0; |
| 5407 | |
| 5408 | out: |
| 5409 | qapi_free_BlockMeasureInfo(info); |
| 5410 | qemu_opts_del(object_opts); |
| 5411 | qemu_opts_del(opts); |
| 5412 | qemu_opts_del(sn_opts); |
| 5413 | qemu_opts_free(create_opts); |
| 5414 | g_free(options); |
| 5415 | blk_unref(in_blk); |
| 5416 | return ret; |
| 5417 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 5418 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 5419 | static const img_cmd_t img_cmds[] = { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5420 | #define DEF(option, callback, arg_string) \ |
| 5421 | { option, callback }, |
| 5422 | #include "qemu-img-cmds.h" |
| 5423 | #undef DEF |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5424 | { NULL, NULL, }, |
| 5425 | }; |
| 5426 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5427 | int main(int argc, char **argv) |
| 5428 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 5429 | const img_cmd_t *cmd; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5430 | const char *cmdname; |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 5431 | Error *local_error = NULL; |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5432 | char *trace_file = NULL; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5433 | int c; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5434 | static const struct option long_options[] = { |
| 5435 | {"help", no_argument, 0, 'h'}, |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5436 | {"version", no_argument, 0, 'V'}, |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5437 | {"trace", required_argument, NULL, 'T'}, |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5438 | {0, 0, 0, 0} |
| 5439 | }; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5440 | |
MORITA Kazutaka | 526eda1 | 2013-07-23 17:30:11 +0900 | [diff] [blame] | 5441 | #ifdef CONFIG_POSIX |
| 5442 | signal(SIGPIPE, SIG_IGN); |
| 5443 | #endif |
| 5444 | |
Daniel P. Berrangé | 98c5d2e | 2020-08-25 11:38:48 +0100 | [diff] [blame] | 5445 | socket_init(); |
Christophe Fergeau | f5852ef | 2019-01-31 17:46:14 +0100 | [diff] [blame] | 5446 | error_init(argv[0]); |
Daniel P. Berrange | fe4db84 | 2016-10-04 14:35:52 +0100 | [diff] [blame] | 5447 | module_call_init(MODULE_INIT_TRACE); |
Fam Zheng | 10f5bff | 2014-02-10 14:48:51 +0800 | [diff] [blame] | 5448 | qemu_init_exec_dir(argv[0]); |
Kevin Wolf | 53f76e5 | 2010-12-16 15:10:32 +0100 | [diff] [blame] | 5449 | |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 5450 | if (qemu_init_main_loop(&local_error)) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 5451 | error_report_err(local_error); |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 5452 | exit(EXIT_FAILURE); |
| 5453 | } |
| 5454 | |
Eduardo Habkost | e8f2d27 | 2016-05-12 11:10:04 -0300 | [diff] [blame] | 5455 | qcrypto_init(&error_fatal); |
Daniel P. Berrange | c229708 | 2016-04-06 12:12:06 +0100 | [diff] [blame] | 5456 | |
Daniel P. Berrange | 064097d | 2016-02-10 18:41:01 +0000 | [diff] [blame] | 5457 | module_call_init(MODULE_INIT_QOM); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5458 | bdrv_init(); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 5459 | if (argc < 2) { |
| 5460 | error_exit("Not enough arguments"); |
| 5461 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5462 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 5463 | qemu_add_opts(&qemu_object_opts); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 5464 | qemu_add_opts(&qemu_source_opts); |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5465 | qemu_add_opts(&qemu_trace_opts); |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 5466 | |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 5467 | while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) { |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5468 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 5469 | case ':': |
| 5470 | missing_argument(argv[optind - 1]); |
| 5471 | return 0; |
Stefan Hajnoczi | 4581c16 | 2017-03-17 18:45:39 +0800 | [diff] [blame] | 5472 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 5473 | unrecognized_option(argv[optind - 1]); |
| 5474 | return 0; |
| 5475 | case 'h': |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5476 | help(); |
| 5477 | return 0; |
| 5478 | case 'V': |
| 5479 | printf(QEMU_IMG_VERSION); |
| 5480 | return 0; |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5481 | case 'T': |
| 5482 | g_free(trace_file); |
| 5483 | trace_file = trace_opt_parse(optarg); |
| 5484 | break; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5485 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5486 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5487 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5488 | cmdname = argv[optind]; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5489 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5490 | /* reset getopt_long scanning */ |
| 5491 | argc -= optind; |
| 5492 | if (argc < 1) { |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 5493 | return 0; |
| 5494 | } |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5495 | argv += optind; |
Richard W.M. Jones | d339d76 | 2019-01-18 10:11:14 +0000 | [diff] [blame] | 5496 | qemu_reset_optind(); |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5497 | |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5498 | if (!trace_init_backends()) { |
| 5499 | exit(1); |
| 5500 | } |
| 5501 | trace_init_file(trace_file); |
| 5502 | qemu_set_log(LOG_TRACE); |
| 5503 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5504 | /* find the command */ |
| 5505 | for (cmd = img_cmds; cmd->name != NULL; cmd++) { |
| 5506 | if (!strcmp(cmdname, cmd->name)) { |
| 5507 | return cmd->handler(argc, argv); |
| 5508 | } |
| 5509 | } |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5510 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5511 | /* not found */ |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 5512 | error_exit("Command not found: %s", cmdname); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5513 | } |