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" |
Stefano Garzarella | 97ede57 | 2019-05-08 12:43:24 +0200 | [diff] [blame] | 44 | #include "qemu/units.h" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 45 | #include "qom/object_interfaces.h" |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 46 | #include "sysemu/block-backend.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 47 | #include "block/block_int.h" |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 48 | #include "block/blockjob.h" |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 49 | #include "block/qapi.h" |
Daniel P. Berrange | c229708 | 2016-04-06 12:12:06 +0100 | [diff] [blame] | 50 | #include "crypto/init.h" |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 51 | #include "trace/control.h" |
bellard | e844533 | 2006-06-14 15:32:10 +0000 | [diff] [blame] | 52 | |
Thomas Huth | 7e563bf | 2018-02-15 12:06:47 +0100 | [diff] [blame] | 53 | #define QEMU_IMG_VERSION "qemu-img version " QEMU_FULL_VERSION \ |
Thomas Huth | 0781dd6 | 2016-10-05 11:54:44 +0200 | [diff] [blame] | 54 | "\n" QEMU_COPYRIGHT "\n" |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 55 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 56 | typedef struct img_cmd_t { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 57 | const char *name; |
| 58 | int (*handler)(int argc, char **argv); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 59 | } img_cmd_t; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 60 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 61 | enum { |
| 62 | OPTION_OUTPUT = 256, |
| 63 | OPTION_BACKING_CHAIN = 257, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 64 | OPTION_OBJECT = 258, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 65 | OPTION_IMAGE_OPTS = 259, |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 66 | OPTION_PATTERN = 260, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 67 | OPTION_FLUSH_INTERVAL = 261, |
| 68 | OPTION_NO_DRAIN = 262, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 69 | OPTION_TARGET_IMAGE_OPTS = 263, |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 70 | OPTION_SIZE = 264, |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 71 | OPTION_PREALLOCATION = 265, |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 72 | OPTION_SHRINK = 266, |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 73 | OPTION_SALVAGE = 267, |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 74 | OPTION_TARGET_IS_ZERO = 268, |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 75 | OPTION_ADD = 269, |
| 76 | OPTION_REMOVE = 270, |
| 77 | OPTION_CLEAR = 271, |
| 78 | OPTION_ENABLE = 272, |
| 79 | OPTION_DISABLE = 273, |
| 80 | OPTION_MERGE = 274, |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 81 | OPTION_BITMAPS = 275, |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 82 | OPTION_FORCE = 276, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | typedef enum OutputFormat { |
| 86 | OFORMAT_JSON, |
| 87 | OFORMAT_HUMAN, |
| 88 | } OutputFormat; |
| 89 | |
Kevin Wolf | e699614 | 2016-03-15 13:03:11 +0100 | [diff] [blame] | 90 | /* 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] | 91 | #define BDRV_DEFAULT_CACHE "writeback" |
aurel32 | 137519c | 2008-11-30 19:12:49 +0000 | [diff] [blame] | 92 | |
Stefan Hajnoczi | 00c6d40 | 2014-08-27 12:08:56 +0100 | [diff] [blame] | 93 | static void format_print(void *opaque, const char *name) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 94 | { |
Stefan Hajnoczi | 00c6d40 | 2014-08-27 12:08:56 +0100 | [diff] [blame] | 95 | printf(" %s", name); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 98 | static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...) |
| 99 | { |
| 100 | va_list ap; |
| 101 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 102 | va_start(ap, fmt); |
Markus Armbruster | e9e1d92 | 2019-04-17 21:06:27 +0200 | [diff] [blame] | 103 | error_vreport(fmt, ap); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 104 | va_end(ap); |
| 105 | |
Markus Armbruster | e9e1d92 | 2019-04-17 21:06:27 +0200 | [diff] [blame] | 106 | error_printf("Try 'qemu-img --help' for more information\n"); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 107 | exit(EXIT_FAILURE); |
| 108 | } |
| 109 | |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 110 | static void QEMU_NORETURN missing_argument(const char *option) |
| 111 | { |
| 112 | error_exit("missing argument for option '%s'", option); |
| 113 | } |
| 114 | |
| 115 | static void QEMU_NORETURN unrecognized_option(const char *option) |
| 116 | { |
| 117 | error_exit("unrecognized option '%s'", option); |
| 118 | } |
| 119 | |
Eric Blake | 0562adf | 2020-05-12 20:16:41 -0500 | [diff] [blame] | 120 | /* Please keep in synch with docs/tools/qemu-img.rst */ |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 121 | static void QEMU_NORETURN help(void) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 122 | { |
Paolo Bonzini | e00291c | 2010-02-04 16:49:56 +0100 | [diff] [blame] | 123 | const char *help_msg = |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 124 | QEMU_IMG_VERSION |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 125 | "usage: qemu-img [standard options] command [command options]\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 126 | "QEMU disk image utility\n" |
| 127 | "\n" |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 128 | " '-h', '--help' display this help and exit\n" |
| 129 | " '-V', '--version' output version information and exit\n" |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 130 | " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n" |
| 131 | " specify tracing options\n" |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 132 | "\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 133 | "Command syntax:\n" |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 134 | #define DEF(option, callback, arg_string) \ |
| 135 | " " arg_string "\n" |
| 136 | #include "qemu-img-cmds.h" |
| 137 | #undef DEF |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 138 | "\n" |
| 139 | "Command parameters:\n" |
| 140 | " 'filename' is a disk image filename\n" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 141 | " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n" |
| 142 | " manual page for a description of the object properties. The most common\n" |
| 143 | " object type is a 'secret', which is used to supply passwords and/or\n" |
| 144 | " encryption keys.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 145 | " '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] | 146 | " '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] | 147 | " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n" |
| 148 | " 'directsync' and 'unsafe' (default for convert)\n" |
Stefan Hajnoczi | bb87fdf | 2014-09-02 11:01:02 +0100 | [diff] [blame] | 149 | " 'src_cache' is the cache mode used to read input disk images, the valid\n" |
| 150 | " options are the same as for the 'cache' option\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 151 | " 'size' is the disk image size in bytes. Optional suffixes\n" |
Kevin Wolf | 5e00984 | 2013-06-05 14:19:27 +0200 | [diff] [blame] | 152 | " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n" |
| 153 | " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n" |
| 154 | " supported. 'b' is ignored.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 155 | " 'output_filename' is the destination disk image filename\n" |
| 156 | " 'output_fmt' is the destination format\n" |
| 157 | " 'options' is a comma separated list of format specific options in a\n" |
| 158 | " name=value format. Use -o ? for an overview of the options supported by the\n" |
| 159 | " used format\n" |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 160 | " 'snapshot_param' is param used for internal snapshot, format\n" |
| 161 | " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n" |
| 162 | " '[ID_OR_NAME]'\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 163 | " '-c' indicates that target image must be compressed (qcow format only)\n" |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 164 | " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n" |
| 165 | " new backing file match exactly. The image doesn't need a working\n" |
| 166 | " backing file before rebasing in this case (useful for renaming the\n" |
| 167 | " backing file). For image creation, allow creating without attempting\n" |
| 168 | " to open the backing file.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 169 | " '-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] | 170 | " '-p' show progress of command (only certain commands)\n" |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 171 | " '-q' use Quiet mode - do not print any output (except errors)\n" |
Peter Lieven | 11b6699 | 2013-10-24 12:07:05 +0200 | [diff] [blame] | 172 | " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n" |
| 173 | " contain only zeros for qemu-img to create a sparse image during\n" |
| 174 | " conversion. If the number of bytes is 0, the source will not be scanned for\n" |
| 175 | " unallocated or zero sectors, and the destination image will always be\n" |
| 176 | " fully allocated\n" |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 177 | " '--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] | 178 | " '-n' skips the target volume creation (useful if the volume is created\n" |
| 179 | " prior to running qemu-img)\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 180 | "\n" |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 181 | "Parameters to bitmap subcommand:\n" |
| 182 | " 'bitmap' is the name of the bitmap to manipulate, through one or more\n" |
| 183 | " actions from '--add', '--remove', '--clear', '--enable', '--disable',\n" |
| 184 | " or '--merge source'\n" |
| 185 | " '-g granularity' sets the granularity for '--add' actions\n" |
| 186 | " '-b source' and '-F src_fmt' tell '--merge' actions to find the source\n" |
| 187 | " bitmaps from an alternative file\n" |
| 188 | "\n" |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 189 | "Parameters to check subcommand:\n" |
| 190 | " '-r' tries to repair any inconsistencies that are found during the check.\n" |
| 191 | " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n" |
| 192 | " 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] | 193 | " hiding corruption that has already occurred.\n" |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 194 | "\n" |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 195 | "Parameters to convert subcommand:\n" |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 196 | " '--bitmaps' copies all top-level persistent bitmaps to destination\n" |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 197 | " '-m' specifies how many coroutines work in parallel during the convert\n" |
| 198 | " process (defaults to 8)\n" |
| 199 | " '-W' allow to write to the target out of order rather than sequential\n" |
| 200 | "\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 201 | "Parameters to snapshot subcommand:\n" |
| 202 | " 'snapshot' is the name of the snapshot to create, apply or delete\n" |
| 203 | " '-a' applies a snapshot (revert disk to saved state)\n" |
| 204 | " '-c' creates a snapshot\n" |
| 205 | " '-d' deletes a snapshot\n" |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 206 | " '-l' lists all snapshots in the given image\n" |
| 207 | "\n" |
| 208 | "Parameters to compare subcommand:\n" |
| 209 | " '-f' first image format\n" |
| 210 | " '-F' second image format\n" |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 211 | " '-s' run in Strict mode - fail on different image size or sector allocation\n" |
| 212 | "\n" |
| 213 | "Parameters to dd subcommand:\n" |
| 214 | " 'bs=BYTES' read and write up to BYTES bytes at a time " |
| 215 | "(default: 512)\n" |
| 216 | " 'count=N' copy only N input blocks\n" |
| 217 | " 'if=FILE' read from FILE\n" |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 218 | " 'of=FILE' write to FILE\n" |
| 219 | " '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] | 220 | |
| 221 | printf("%s\nSupported formats:", help_msg); |
Andrey Shinkevich | 9ac404c | 2019-03-07 16:33:58 +0300 | [diff] [blame] | 222 | bdrv_iterate_format(format_print, NULL, false); |
Eric Blake | f5048cb | 2017-08-03 11:33:53 -0500 | [diff] [blame] | 223 | printf("\n\n" QEMU_HELP_BOTTOM "\n"); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 224 | exit(EXIT_SUCCESS); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 227 | static QemuOptsList qemu_object_opts = { |
| 228 | .name = "object", |
| 229 | .implied_opt_name = "qom-type", |
| 230 | .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head), |
| 231 | .desc = { |
| 232 | { } |
| 233 | }, |
| 234 | }; |
| 235 | |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 236 | static bool qemu_img_object_print_help(const char *type, QemuOpts *opts) |
| 237 | { |
| 238 | if (user_creatable_print_help(type, opts)) { |
| 239 | exit(0); |
| 240 | } |
| 241 | return true; |
| 242 | } |
| 243 | |
Markus Armbruster | 80c710c | 2020-04-15 09:49:26 +0200 | [diff] [blame] | 244 | /* |
| 245 | * Is @optarg safe for accumulate_options()? |
| 246 | * It is when multiple of them can be joined together separated by ','. |
| 247 | * To make that work, @optarg must not start with ',' (or else a |
| 248 | * separating ',' preceding it gets escaped), and it must not end with |
| 249 | * an odd number of ',' (or else a separating ',' following it gets |
Markus Armbruster | f62514b | 2020-04-15 09:49:27 +0200 | [diff] [blame] | 250 | * escaped), or be empty (or else a separating ',' preceding it can |
| 251 | * escape a separating ',' following it). |
| 252 | * |
Markus Armbruster | 80c710c | 2020-04-15 09:49:26 +0200 | [diff] [blame] | 253 | */ |
| 254 | static bool is_valid_option_list(const char *optarg) |
| 255 | { |
| 256 | size_t len = strlen(optarg); |
| 257 | size_t i; |
| 258 | |
Markus Armbruster | f62514b | 2020-04-15 09:49:27 +0200 | [diff] [blame] | 259 | if (!optarg[0] || optarg[0] == ',') { |
Markus Armbruster | 80c710c | 2020-04-15 09:49:26 +0200 | [diff] [blame] | 260 | return false; |
| 261 | } |
| 262 | |
| 263 | for (i = len; i > 0 && optarg[i - 1] == ','; i--) { |
| 264 | } |
| 265 | if ((len - i) % 2) { |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | return true; |
| 270 | } |
| 271 | |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 272 | static int accumulate_options(char **options, char *optarg) |
| 273 | { |
| 274 | char *new_options; |
| 275 | |
| 276 | if (!is_valid_option_list(optarg)) { |
| 277 | error_report("Invalid option list: %s", optarg); |
| 278 | return -1; |
| 279 | } |
| 280 | |
| 281 | if (!*options) { |
| 282 | *options = g_strdup(optarg); |
| 283 | } else { |
| 284 | new_options = g_strdup_printf("%s,%s", *options, optarg); |
| 285 | g_free(*options); |
| 286 | *options = new_options; |
| 287 | } |
| 288 | return 0; |
| 289 | } |
| 290 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 291 | static QemuOptsList qemu_source_opts = { |
| 292 | .name = "source", |
| 293 | .implied_opt_name = "file", |
| 294 | .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head), |
| 295 | .desc = { |
| 296 | { } |
| 297 | }, |
| 298 | }; |
| 299 | |
Stefan Weil | 7c30f65 | 2013-06-16 17:01:05 +0200 | [diff] [blame] | 300 | 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] | 301 | { |
| 302 | int ret = 0; |
| 303 | if (!quiet) { |
| 304 | va_list args; |
| 305 | va_start(args, fmt); |
| 306 | ret = vprintf(fmt, args); |
| 307 | va_end(args); |
| 308 | } |
| 309 | return ret; |
| 310 | } |
| 311 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 312 | |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 313 | static int print_block_option_help(const char *filename, const char *fmt) |
| 314 | { |
| 315 | BlockDriver *drv, *proto_drv; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 316 | QemuOptsList *create_opts = NULL; |
Max Reitz | b65a5e1 | 2015-02-05 13:58:12 -0500 | [diff] [blame] | 317 | Error *local_err = NULL; |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 318 | |
| 319 | /* Find driver and parse its options */ |
| 320 | drv = bdrv_find_format(fmt); |
| 321 | if (!drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 322 | error_report("Unknown file format '%s'", fmt); |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 323 | return 1; |
| 324 | } |
| 325 | |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 326 | if (!drv->create_opts) { |
| 327 | error_report("Format driver '%s' does not support image creation", fmt); |
| 328 | return 1; |
| 329 | } |
| 330 | |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 331 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 332 | if (filename) { |
Max Reitz | b65a5e1 | 2015-02-05 13:58:12 -0500 | [diff] [blame] | 333 | proto_drv = bdrv_find_protocol(filename, true, &local_err); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 334 | if (!proto_drv) { |
Markus Armbruster | 2867ce4 | 2015-03-12 16:08:02 +0100 | [diff] [blame] | 335 | error_report_err(local_err); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 336 | qemu_opts_free(create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 337 | return 1; |
| 338 | } |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 339 | if (!proto_drv->create_opts) { |
Max Reitz | f099887 | 2018-11-19 11:19:20 +0100 | [diff] [blame] | 340 | error_report("Protocol driver '%s' does not support image creation", |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 341 | proto_drv->format_name); |
Max Reitz | 3ecd5a4 | 2018-11-19 11:19:21 +0100 | [diff] [blame] | 342 | qemu_opts_free(create_opts); |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 343 | return 1; |
| 344 | } |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 345 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 346 | } |
| 347 | |
Max Reitz | f4619af | 2019-04-13 17:20:37 +0200 | [diff] [blame] | 348 | if (filename) { |
| 349 | printf("Supported options:\n"); |
| 350 | } else { |
| 351 | printf("Supported %s options:\n", fmt); |
| 352 | } |
Max Reitz | 6389871 | 2018-10-19 18:49:25 +0200 | [diff] [blame] | 353 | qemu_opts_print_help(create_opts, false); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 354 | qemu_opts_free(create_opts); |
Max Reitz | f4619af | 2019-04-13 17:20:37 +0200 | [diff] [blame] | 355 | |
| 356 | if (!filename) { |
| 357 | printf("\n" |
| 358 | "The protocol level may support further options.\n" |
| 359 | "Specify the target filename to include those options.\n"); |
| 360 | } |
| 361 | |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 362 | return 0; |
| 363 | } |
| 364 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 365 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 366 | static BlockBackend *img_open_opts(const char *optstr, |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 367 | QemuOpts *opts, int flags, bool writethrough, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 368 | bool quiet, bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 369 | { |
| 370 | QDict *options; |
| 371 | Error *local_err = NULL; |
| 372 | BlockBackend *blk; |
| 373 | options = qemu_opts_to_qdict(opts, NULL); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 374 | if (force_share) { |
| 375 | if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE) |
Max Reitz | 4615f87 | 2018-05-02 22:20:50 +0200 | [diff] [blame] | 376 | && strcmp(qdict_get_str(options, BDRV_OPT_FORCE_SHARE), "on")) { |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 377 | error_report("--force-share/-U conflicts with image options"); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 378 | qobject_unref(options); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 379 | return NULL; |
| 380 | } |
Max Reitz | 4615f87 | 2018-05-02 22:20:50 +0200 | [diff] [blame] | 381 | qdict_put_str(options, BDRV_OPT_FORCE_SHARE, "on"); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 382 | } |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 383 | blk = blk_new_open(NULL, NULL, options, flags, &local_err); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 384 | if (!blk) { |
Daniel P. Berrange | 143605a | 2016-04-06 10:16:18 +0100 | [diff] [blame] | 385 | error_reportf_err(local_err, "Could not open '%s': ", optstr); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 386 | return NULL; |
| 387 | } |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 388 | blk_set_enable_write_cache(blk, !writethrough); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 389 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 390 | return blk; |
| 391 | } |
| 392 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 393 | static BlockBackend *img_open_file(const char *filename, |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 394 | QDict *options, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 395 | const char *fmt, int flags, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 396 | bool writethrough, bool quiet, |
| 397 | bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 398 | { |
| 399 | BlockBackend *blk; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 400 | Error *local_err = NULL; |
Kevin Wolf | ad71713 | 2010-12-16 15:37:41 +0100 | [diff] [blame] | 401 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 402 | if (!options) { |
| 403 | options = qdict_new(); |
| 404 | } |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 405 | if (fmt) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 406 | qdict_put_str(options, "driver", fmt); |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 407 | } |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 408 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 409 | if (force_share) { |
Eric Blake | 579cf1d | 2017-05-15 14:54:39 -0500 | [diff] [blame] | 410 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 411 | } |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 412 | blk = blk_new_open(filename, NULL, options, flags, &local_err); |
Max Reitz | 5bd3132 | 2015-02-05 13:58:16 -0500 | [diff] [blame] | 413 | if (!blk) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 414 | error_reportf_err(local_err, "Could not open '%s': ", filename); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 415 | return NULL; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 416 | } |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 417 | blk_set_enable_write_cache(blk, !writethrough); |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 418 | |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 419 | return blk; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 422 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 423 | static int img_add_key_secrets(void *opaque, |
| 424 | const char *name, const char *value, |
| 425 | Error **errp) |
| 426 | { |
| 427 | QDict *options = opaque; |
| 428 | |
| 429 | if (g_str_has_suffix(name, "key-secret")) { |
Eric Blake | 187f47e | 2017-06-24 12:10:07 -0600 | [diff] [blame] | 430 | qdict_put_str(options, name, value); |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 436 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 437 | static BlockBackend *img_open(bool image_opts, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 438 | const char *filename, |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 439 | const char *fmt, int flags, bool writethrough, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 440 | bool quiet, bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 441 | { |
| 442 | BlockBackend *blk; |
| 443 | if (image_opts) { |
| 444 | QemuOpts *opts; |
| 445 | if (fmt) { |
| 446 | error_report("--image-opts and --format are mutually exclusive"); |
| 447 | return NULL; |
| 448 | } |
| 449 | opts = qemu_opts_parse_noisily(qemu_find_opts("source"), |
| 450 | filename, true); |
| 451 | if (!opts) { |
| 452 | return NULL; |
| 453 | } |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 454 | blk = img_open_opts(filename, opts, flags, writethrough, quiet, |
| 455 | force_share); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 456 | } else { |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 457 | blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 458 | force_share); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 459 | } |
| 460 | return blk; |
| 461 | } |
| 462 | |
| 463 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 464 | static int add_old_style_options(const char *fmt, QemuOpts *opts, |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 465 | const char *base_filename, |
| 466 | const char *base_fmt) |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 467 | { |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 468 | Error *err = NULL; |
| 469 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 470 | if (base_filename) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 471 | qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err); |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 472 | if (err) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 473 | error_report("Backing file not supported for file format '%s'", |
| 474 | fmt); |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 475 | error_free(err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 476 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | if (base_fmt) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 480 | qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err); |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 481 | if (err) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 482 | error_report("Backing file format not supported for file " |
| 483 | "format '%s'", fmt); |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 484 | error_free(err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 485 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 486 | } |
| 487 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 488 | return 0; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 489 | } |
| 490 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 491 | static int64_t cvtnum_full(const char *name, const char *value, int64_t min, |
| 492 | int64_t max) |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 493 | { |
Markus Armbruster | f17fd4f | 2017-02-21 21:14:06 +0100 | [diff] [blame] | 494 | int err; |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 495 | uint64_t res; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 496 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 497 | err = qemu_strtosz(value, NULL, &res); |
| 498 | if (err < 0 && err != -ERANGE) { |
| 499 | error_report("Invalid %s specified. You may use " |
| 500 | "k, M, G, T, P or E suffixes for", name); |
| 501 | error_report("kilobytes, megabytes, gigabytes, terabytes, " |
| 502 | "petabytes and exabytes."); |
Markus Armbruster | f17fd4f | 2017-02-21 21:14:06 +0100 | [diff] [blame] | 503 | return err; |
| 504 | } |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 505 | if (err == -ERANGE || res > max || res < min) { |
| 506 | error_report("Invalid %s specified. Must be between %" PRId64 |
| 507 | " and %" PRId64 ".", name, min, max); |
Markus Armbruster | f46bfdb | 2017-02-21 21:14:07 +0100 | [diff] [blame] | 508 | return -ERANGE; |
| 509 | } |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 510 | return res; |
| 511 | } |
| 512 | |
| 513 | static int64_t cvtnum(const char *name, const char *value) |
| 514 | { |
| 515 | return cvtnum_full(name, value, 0, INT64_MAX); |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 516 | } |
| 517 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 518 | static int img_create(int argc, char **argv) |
| 519 | { |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 520 | int c; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 521 | uint64_t img_size = -1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 522 | const char *fmt = "raw"; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 523 | const char *base_fmt = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 524 | const char *filename; |
| 525 | const char *base_filename = NULL; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 526 | char *options = NULL; |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 527 | Error *local_err = NULL; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 528 | bool quiet = false; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 529 | int flags = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 530 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 531 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 532 | static const struct option long_options[] = { |
| 533 | {"help", no_argument, 0, 'h'}, |
| 534 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 535 | {0, 0, 0, 0} |
| 536 | }; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 537 | c = getopt_long(argc, argv, ":F:b:f:ho:qu", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 538 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 539 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 540 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 541 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 542 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 543 | case ':': |
| 544 | missing_argument(argv[optind - 1]); |
| 545 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 546 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 547 | unrecognized_option(argv[optind - 1]); |
| 548 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 549 | case 'h': |
| 550 | help(); |
| 551 | break; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 552 | case 'F': |
| 553 | base_fmt = optarg; |
| 554 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 555 | case 'b': |
| 556 | base_filename = optarg; |
| 557 | break; |
| 558 | case 'f': |
| 559 | fmt = optarg; |
| 560 | break; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 561 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 562 | if (accumulate_options(&options, optarg) < 0) { |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 563 | goto fail; |
| 564 | } |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 565 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 566 | case 'q': |
| 567 | quiet = true; |
| 568 | break; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 569 | case 'u': |
| 570 | flags |= BDRV_O_NO_BACKING; |
| 571 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 572 | case OPTION_OBJECT: { |
| 573 | QemuOpts *opts; |
| 574 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 575 | optarg, true); |
| 576 | if (!opts) { |
| 577 | goto fail; |
| 578 | } |
| 579 | } break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 580 | } |
| 581 | } |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 582 | |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 583 | /* Get the filename */ |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 584 | filename = (optind < argc) ? argv[optind] : NULL; |
| 585 | if (options && has_help_option(options)) { |
| 586 | g_free(options); |
| 587 | return print_block_option_help(filename, fmt); |
| 588 | } |
| 589 | |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 590 | if (optind >= argc) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 591 | error_exit("Expecting image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 592 | } |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 593 | optind++; |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 594 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 595 | if (qemu_opts_foreach(&qemu_object_opts, |
| 596 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 597 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 598 | goto fail; |
| 599 | } |
| 600 | |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 601 | /* Get image size, if specified */ |
| 602 | if (optind < argc) { |
Jes Sorensen | 70b4f4b | 2011-01-05 11:41:02 +0100 | [diff] [blame] | 603 | int64_t sval; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 604 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 605 | sval = cvtnum("image size", argv[optind++]); |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 606 | if (sval < 0) { |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 607 | goto fail; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 608 | } |
| 609 | img_size = (uint64_t)sval; |
| 610 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 611 | if (optind != argc) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 612 | error_exit("Unexpected argument: %s", argv[optind]); |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 613 | } |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 614 | |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 615 | bdrv_img_create(filename, fmt, base_filename, base_fmt, |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 616 | options, img_size, flags, quiet, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 617 | if (local_err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 618 | error_reportf_err(local_err, "%s: ", filename); |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 619 | goto fail; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 620 | } |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 621 | |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 622 | g_free(options); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 623 | return 0; |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 624 | |
| 625 | fail: |
| 626 | g_free(options); |
| 627 | return 1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 630 | static void dump_json_image_check(ImageCheck *check, bool quiet) |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 631 | { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 632 | QString *str; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 633 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 634 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 635 | |
| 636 | visit_type_ImageCheck(v, NULL, &check, &error_abort); |
| 637 | visit_complete(v, &obj); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 638 | str = qobject_to_json_pretty(obj); |
| 639 | assert(str != NULL); |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 640 | qprintf(quiet, "%s\n", qstring_get_str(str)); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 641 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 642 | visit_free(v); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 643 | qobject_unref(str); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 644 | } |
| 645 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 646 | static void dump_human_image_check(ImageCheck *check, bool quiet) |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 647 | { |
| 648 | if (!(check->corruptions || check->leaks || check->check_errors)) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 649 | qprintf(quiet, "No errors were found on the image.\n"); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 650 | } else { |
| 651 | if (check->corruptions) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 652 | qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n" |
| 653 | "Data may be corrupted, or further writes to the image " |
| 654 | "may corrupt it.\n", |
| 655 | check->corruptions); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | if (check->leaks) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 659 | qprintf(quiet, |
| 660 | "\n%" PRId64 " leaked clusters were found on the image.\n" |
| 661 | "This means waste of disk space, but no harm to data.\n", |
| 662 | check->leaks); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | if (check->check_errors) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 666 | qprintf(quiet, |
| 667 | "\n%" PRId64 |
| 668 | " internal errors have occurred during the check.\n", |
| 669 | check->check_errors); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | |
| 673 | if (check->total_clusters != 0 && check->allocated_clusters != 0) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 674 | qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, " |
| 675 | "%0.2f%% fragmented, %0.2f%% compressed clusters\n", |
| 676 | check->allocated_clusters, check->total_clusters, |
| 677 | check->allocated_clusters * 100.0 / check->total_clusters, |
| 678 | check->fragmented_clusters * 100.0 / check->allocated_clusters, |
| 679 | check->compressed_clusters * 100.0 / |
| 680 | check->allocated_clusters); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | if (check->image_end_offset) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 684 | qprintf(quiet, |
| 685 | "Image end offset: %" PRId64 "\n", check->image_end_offset); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
| 689 | static int collect_image_check(BlockDriverState *bs, |
| 690 | ImageCheck *check, |
| 691 | const char *filename, |
| 692 | const char *fmt, |
| 693 | int fix) |
| 694 | { |
| 695 | int ret; |
| 696 | BdrvCheckResult result; |
| 697 | |
| 698 | ret = bdrv_check(bs, &result, fix); |
| 699 | if (ret < 0) { |
| 700 | return ret; |
| 701 | } |
| 702 | |
| 703 | check->filename = g_strdup(filename); |
| 704 | check->format = g_strdup(bdrv_get_format_name(bs)); |
| 705 | check->check_errors = result.check_errors; |
| 706 | check->corruptions = result.corruptions; |
| 707 | check->has_corruptions = result.corruptions != 0; |
| 708 | check->leaks = result.leaks; |
| 709 | check->has_leaks = result.leaks != 0; |
| 710 | check->corruptions_fixed = result.corruptions_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 711 | check->has_corruptions_fixed = result.corruptions_fixed != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 712 | check->leaks_fixed = result.leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 713 | check->has_leaks_fixed = result.leaks_fixed != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 714 | check->image_end_offset = result.image_end_offset; |
| 715 | check->has_image_end_offset = result.image_end_offset != 0; |
| 716 | check->total_clusters = result.bfi.total_clusters; |
| 717 | check->has_total_clusters = result.bfi.total_clusters != 0; |
| 718 | check->allocated_clusters = result.bfi.allocated_clusters; |
| 719 | check->has_allocated_clusters = result.bfi.allocated_clusters != 0; |
| 720 | check->fragmented_clusters = result.bfi.fragmented_clusters; |
| 721 | check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0; |
Stefan Hajnoczi | e6439d7 | 2013-02-07 17:15:04 +0100 | [diff] [blame] | 722 | check->compressed_clusters = result.bfi.compressed_clusters; |
| 723 | check->has_compressed_clusters = result.bfi.compressed_clusters != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 724 | |
| 725 | return 0; |
| 726 | } |
| 727 | |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 728 | /* |
| 729 | * Checks an image for consistency. Exit codes: |
| 730 | * |
Max Reitz | d6635c4 | 2014-06-02 22:15:21 +0200 | [diff] [blame] | 731 | * 0 - Check completed, image is good |
| 732 | * 1 - Check not completed because of internal errors |
| 733 | * 2 - Check completed, image is corrupted |
| 734 | * 3 - Check completed, image has leaked clusters, but is good otherwise |
| 735 | * 63 - Checks are not supported by the image format |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 736 | */ |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 737 | static int img_check(int argc, char **argv) |
| 738 | { |
| 739 | int c, ret; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 740 | OutputFormat output_format = OFORMAT_HUMAN; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 741 | const char *filename, *fmt, *output, *cache; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 742 | BlockBackend *blk; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 743 | BlockDriverState *bs; |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 744 | int fix = 0; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 745 | int flags = BDRV_O_CHECK; |
| 746 | bool writethrough; |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 747 | ImageCheck *check; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 748 | bool quiet = false; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 749 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 750 | bool force_share = false; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 751 | |
| 752 | fmt = NULL; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 753 | output = NULL; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 754 | cache = BDRV_DEFAULT_CACHE; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 755 | |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 756 | for(;;) { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 757 | int option_index = 0; |
| 758 | static const struct option long_options[] = { |
| 759 | {"help", no_argument, 0, 'h'}, |
| 760 | {"format", required_argument, 0, 'f'}, |
Prasad Joshi | 4fd6a98 | 2014-03-25 00:08:54 +0530 | [diff] [blame] | 761 | {"repair", required_argument, 0, 'r'}, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 762 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 763 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 764 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 765 | {"force-share", no_argument, 0, 'U'}, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 766 | {0, 0, 0, 0} |
| 767 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 768 | c = getopt_long(argc, argv, ":hf:r:T:qU", |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 769 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 770 | if (c == -1) { |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 771 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 772 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 773 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 774 | case ':': |
| 775 | missing_argument(argv[optind - 1]); |
| 776 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 777 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 778 | unrecognized_option(argv[optind - 1]); |
| 779 | break; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 780 | case 'h': |
| 781 | help(); |
| 782 | break; |
| 783 | case 'f': |
| 784 | fmt = optarg; |
| 785 | break; |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 786 | case 'r': |
| 787 | flags |= BDRV_O_RDWR; |
| 788 | |
| 789 | if (!strcmp(optarg, "leaks")) { |
| 790 | fix = BDRV_FIX_LEAKS; |
| 791 | } else if (!strcmp(optarg, "all")) { |
| 792 | fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS; |
| 793 | } else { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 794 | error_exit("Unknown option value for -r " |
| 795 | "(expecting 'leaks' or 'all'): %s", optarg); |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 796 | } |
| 797 | break; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 798 | case OPTION_OUTPUT: |
| 799 | output = optarg; |
| 800 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 801 | case 'T': |
| 802 | cache = optarg; |
| 803 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 804 | case 'q': |
| 805 | quiet = true; |
| 806 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 807 | case 'U': |
| 808 | force_share = true; |
| 809 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 810 | case OPTION_OBJECT: { |
| 811 | QemuOpts *opts; |
| 812 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 813 | optarg, true); |
| 814 | if (!opts) { |
| 815 | return 1; |
| 816 | } |
| 817 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 818 | case OPTION_IMAGE_OPTS: |
| 819 | image_opts = true; |
| 820 | break; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 821 | } |
| 822 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 823 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 824 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 825 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 826 | filename = argv[optind++]; |
| 827 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 828 | if (output && !strcmp(output, "json")) { |
| 829 | output_format = OFORMAT_JSON; |
| 830 | } else if (output && !strcmp(output, "human")) { |
| 831 | output_format = OFORMAT_HUMAN; |
| 832 | } else if (output) { |
| 833 | error_report("--output must be used with human or json as argument."); |
| 834 | return 1; |
| 835 | } |
| 836 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 837 | if (qemu_opts_foreach(&qemu_object_opts, |
| 838 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 839 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 840 | return 1; |
| 841 | } |
| 842 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 843 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 844 | if (ret < 0) { |
| 845 | error_report("Invalid source cache option: %s", cache); |
| 846 | return 1; |
| 847 | } |
| 848 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 849 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 850 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 851 | if (!blk) { |
| 852 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 853 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 854 | bs = blk_bs(blk); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 855 | |
| 856 | check = g_new0(ImageCheck, 1); |
| 857 | ret = collect_image_check(bs, check, filename, fmt, fix); |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 858 | |
| 859 | if (ret == -ENOTSUP) { |
Max Reitz | 55d492d | 2014-05-31 21:33:30 +0200 | [diff] [blame] | 860 | error_report("This image format does not support checks"); |
Peter Lieven | fefddf9 | 2013-10-24 08:53:34 +0200 | [diff] [blame] | 861 | ret = 63; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 862 | goto fail; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 863 | } |
| 864 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 865 | if (check->corruptions_fixed || check->leaks_fixed) { |
| 866 | int corruptions_fixed, leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 867 | bool has_leaks_fixed, has_corruptions_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 868 | |
| 869 | leaks_fixed = check->leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 870 | has_leaks_fixed = check->has_leaks_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 871 | corruptions_fixed = check->corruptions_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 872 | has_corruptions_fixed = check->has_corruptions_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 873 | |
| 874 | if (output_format == OFORMAT_HUMAN) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 875 | qprintf(quiet, |
| 876 | "The following inconsistencies were found and repaired:\n\n" |
| 877 | " %" PRId64 " leaked clusters\n" |
| 878 | " %" PRId64 " corruptions\n\n" |
| 879 | "Double checking the fixed image now...\n", |
| 880 | check->leaks_fixed, |
| 881 | check->corruptions_fixed); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 882 | } |
| 883 | |
Pan Nengyuan | fc124ea | 2020-02-27 09:29:50 +0800 | [diff] [blame] | 884 | qapi_free_ImageCheck(check); |
| 885 | check = g_new0(ImageCheck, 1); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 886 | ret = collect_image_check(bs, check, filename, fmt, 0); |
| 887 | |
| 888 | check->leaks_fixed = leaks_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 889 | check->has_leaks_fixed = has_leaks_fixed; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 890 | check->corruptions_fixed = corruptions_fixed; |
Max Reitz | 1656324 | 2020-03-24 18:27:55 +0100 | [diff] [blame] | 891 | check->has_corruptions_fixed = has_corruptions_fixed; |
Kevin Wolf | ccf3471 | 2012-05-11 18:16:54 +0200 | [diff] [blame] | 892 | } |
| 893 | |
Max Reitz | 832390a | 2014-10-23 15:29:12 +0200 | [diff] [blame] | 894 | if (!ret) { |
| 895 | switch (output_format) { |
| 896 | case OFORMAT_HUMAN: |
| 897 | dump_human_image_check(check, quiet); |
| 898 | break; |
| 899 | case OFORMAT_JSON: |
| 900 | dump_json_image_check(check, quiet); |
| 901 | break; |
| 902 | } |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | if (ret || check->check_errors) { |
Max Reitz | 832390a | 2014-10-23 15:29:12 +0200 | [diff] [blame] | 906 | if (ret) { |
| 907 | error_report("Check failed: %s", strerror(-ret)); |
| 908 | } else { |
| 909 | error_report("Check failed"); |
| 910 | } |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 911 | ret = 1; |
| 912 | goto fail; |
| 913 | } |
| 914 | |
| 915 | if (check->corruptions) { |
| 916 | ret = 2; |
| 917 | } else if (check->leaks) { |
| 918 | ret = 3; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 919 | } else { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 920 | ret = 0; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 921 | } |
| 922 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 923 | fail: |
| 924 | qapi_free_ImageCheck(check); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 925 | blk_unref(blk); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 926 | return ret; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 927 | } |
| 928 | |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 929 | typedef struct CommonBlockJobCBInfo { |
| 930 | BlockDriverState *bs; |
| 931 | Error **errp; |
| 932 | } CommonBlockJobCBInfo; |
| 933 | |
| 934 | static void common_block_job_cb(void *opaque, int ret) |
| 935 | { |
| 936 | CommonBlockJobCBInfo *cbi = opaque; |
| 937 | |
| 938 | if (ret < 0) { |
| 939 | error_setg_errno(cbi->errp, -ret, "Block job failed"); |
| 940 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | static void run_block_job(BlockJob *job, Error **errp) |
| 944 | { |
Kevin Wolf | b75536c | 2016-04-18 17:30:17 +0200 | [diff] [blame] | 945 | AioContext *aio_context = blk_get_aio_context(job->blk); |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 946 | int ret = 0; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 947 | |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 948 | aio_context_acquire(aio_context); |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 949 | job_ref(&job->job); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 950 | do { |
Kevin Wolf | 30a5c88 | 2018-05-04 12:17:20 +0200 | [diff] [blame] | 951 | float progress = 0.0f; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 952 | aio_poll(aio_context, true); |
Vladimir Sementsov-Ogievskiy | 01fe1ca | 2020-03-11 13:29:56 +0300 | [diff] [blame] | 953 | if (job->job.progress.total) { |
| 954 | progress = (float)job->job.progress.current / |
| 955 | job->job.progress.total * 100.f; |
Kevin Wolf | 30a5c88 | 2018-05-04 12:17:20 +0200 | [diff] [blame] | 956 | } |
| 957 | qemu_progress_print(progress, 0); |
Kevin Wolf | df956ae | 2018-04-25 15:09:58 +0200 | [diff] [blame] | 958 | } while (!job_is_ready(&job->job) && !job_is_completed(&job->job)); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 959 | |
Kevin Wolf | dbe5e6c | 2018-04-19 13:04:01 +0200 | [diff] [blame] | 960 | if (!job_is_completed(&job->job)) { |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 961 | ret = job_complete_sync(&job->job, errp); |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 962 | } else { |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 963 | ret = job->job.ret; |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 964 | } |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 965 | job_unref(&job->job); |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 966 | aio_context_release(aio_context); |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 967 | |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 968 | /* publish completion progress only when success */ |
| 969 | if (!ret) { |
| 970 | qemu_progress_print(100.f, 0); |
| 971 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 972 | } |
| 973 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 974 | static int img_commit(int argc, char **argv) |
| 975 | { |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 976 | int c, ret, flags; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 977 | const char *filename, *fmt, *cache, *base; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 978 | BlockBackend *blk; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 979 | BlockDriverState *bs, *base_bs; |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 980 | BlockJob *job; |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 981 | bool progress = false, quiet = false, drop = false; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 982 | bool writethrough; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 983 | Error *local_err = NULL; |
| 984 | CommonBlockJobCBInfo cbi; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 985 | bool image_opts = false; |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 986 | AioContext *aio_context; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 987 | |
| 988 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 989 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 990 | base = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 991 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 992 | static const struct option long_options[] = { |
| 993 | {"help", no_argument, 0, 'h'}, |
| 994 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 995 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 996 | {0, 0, 0, 0} |
| 997 | }; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 998 | c = getopt_long(argc, argv, ":f:ht:b:dpq", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 999 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1000 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1001 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1002 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1003 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 1004 | case ':': |
| 1005 | missing_argument(argv[optind - 1]); |
| 1006 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 1007 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 1008 | unrecognized_option(argv[optind - 1]); |
| 1009 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1010 | case 'h': |
| 1011 | help(); |
| 1012 | break; |
| 1013 | case 'f': |
| 1014 | fmt = optarg; |
| 1015 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1016 | case 't': |
| 1017 | cache = optarg; |
| 1018 | break; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1019 | case 'b': |
| 1020 | base = optarg; |
| 1021 | /* -b implies -d */ |
| 1022 | drop = true; |
| 1023 | break; |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1024 | case 'd': |
| 1025 | drop = true; |
| 1026 | break; |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 1027 | case 'p': |
| 1028 | progress = true; |
| 1029 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 1030 | case 'q': |
| 1031 | quiet = true; |
| 1032 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1033 | case OPTION_OBJECT: { |
| 1034 | QemuOpts *opts; |
| 1035 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 1036 | optarg, true); |
| 1037 | if (!opts) { |
| 1038 | return 1; |
| 1039 | } |
| 1040 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1041 | case OPTION_IMAGE_OPTS: |
| 1042 | image_opts = true; |
| 1043 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1044 | } |
| 1045 | } |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 1046 | |
| 1047 | /* Progress is not shown in Quiet mode */ |
| 1048 | if (quiet) { |
| 1049 | progress = false; |
| 1050 | } |
| 1051 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 1052 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 1053 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1054 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1055 | filename = argv[optind++]; |
| 1056 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1057 | if (qemu_opts_foreach(&qemu_object_opts, |
| 1058 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 1059 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1060 | return 1; |
| 1061 | } |
| 1062 | |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1063 | flags = BDRV_O_RDWR | BDRV_O_UNMAP; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1064 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1065 | if (ret < 0) { |
| 1066 | error_report("Invalid cache option: %s", cache); |
Stefan Hajnoczi | a3981eb | 2014-08-26 19:17:54 +0100 | [diff] [blame] | 1067 | return 1; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1068 | } |
| 1069 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1070 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 1071 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1072 | if (!blk) { |
| 1073 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1074 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1075 | bs = blk_bs(blk); |
| 1076 | |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 1077 | qemu_progress_init(progress, 1.f); |
| 1078 | qemu_progress_print(0.f, 100); |
| 1079 | |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1080 | if (base) { |
| 1081 | base_bs = bdrv_find_backing_image(bs, base); |
| 1082 | if (!base_bs) { |
Max Reitz | 6b33f3a | 2016-12-01 03:05:08 +0100 | [diff] [blame] | 1083 | error_setg(&local_err, |
| 1084 | "Did not find '%s' in the backing chain of '%s'", |
| 1085 | base, filename); |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1086 | goto done; |
| 1087 | } |
| 1088 | } else { |
| 1089 | /* This is different from QMP, which by default uses the deepest file in |
| 1090 | * the backing chain (i.e., the very base); however, the traditional |
| 1091 | * behavior of qemu-img commit is using the immediate backing file. */ |
Kevin Wolf | 760e006 | 2015-06-17 14:55:21 +0200 | [diff] [blame] | 1092 | base_bs = backing_bs(bs); |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1093 | if (!base_bs) { |
| 1094 | error_setg(&local_err, "Image does not have a backing file"); |
| 1095 | goto done; |
| 1096 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1099 | cbi = (CommonBlockJobCBInfo){ |
| 1100 | .errp = &local_err, |
| 1101 | .bs = bs, |
| 1102 | }; |
| 1103 | |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 1104 | aio_context = bdrv_get_aio_context(bs); |
| 1105 | aio_context_acquire(aio_context); |
Kevin Wolf | bb02b65 | 2018-04-19 17:54:56 +0200 | [diff] [blame] | 1106 | commit_active_start("commit", bs, base_bs, JOB_DEFAULT, 0, |
Kevin Wolf | 0db832f | 2017-02-20 18:10:05 +0100 | [diff] [blame] | 1107 | BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb, |
Fam Zheng | 78bbd91 | 2017-04-21 20:27:04 +0800 | [diff] [blame] | 1108 | &cbi, false, &local_err); |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 1109 | aio_context_release(aio_context); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1110 | if (local_err) { |
| 1111 | goto done; |
| 1112 | } |
| 1113 | |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1114 | /* When the block job completes, the BlockBackend reference will point to |
| 1115 | * the old backing file. In order to avoid that the top image is already |
| 1116 | * deleted, so we can still empty it afterwards, increment the reference |
| 1117 | * counter here preemptively. */ |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1118 | if (!drop) { |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1119 | bdrv_ref(bs); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1120 | } |
| 1121 | |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 1122 | job = block_job_get("commit"); |
Liam Merwick | 2e2db26 | 2018-11-05 21:38:37 +0000 | [diff] [blame] | 1123 | assert(job); |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 1124 | run_block_job(job, &local_err); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1125 | if (local_err) { |
| 1126 | goto unref_backing; |
| 1127 | } |
| 1128 | |
Max Reitz | 2d97fde | 2020-04-29 16:11:26 +0200 | [diff] [blame] | 1129 | if (!drop) { |
| 1130 | BlockBackend *old_backing_blk; |
| 1131 | |
| 1132 | old_backing_blk = blk_new_with_bs(bs, BLK_PERM_WRITE, BLK_PERM_ALL, |
| 1133 | &local_err); |
| 1134 | if (!old_backing_blk) { |
| 1135 | goto unref_backing; |
| 1136 | } |
| 1137 | ret = blk_make_empty(old_backing_blk, &local_err); |
| 1138 | blk_unref(old_backing_blk); |
| 1139 | if (ret == -ENOTSUP) { |
| 1140 | error_free(local_err); |
| 1141 | local_err = NULL; |
| 1142 | } else if (ret < 0) { |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1143 | goto unref_backing; |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | unref_backing: |
| 1148 | if (!drop) { |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1149 | bdrv_unref(bs); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1150 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1151 | |
| 1152 | done: |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 1153 | qemu_progress_end(); |
| 1154 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1155 | blk_unref(blk); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1156 | |
| 1157 | if (local_err) { |
Markus Armbruster | 6936f29 | 2015-02-10 15:14:02 +0100 | [diff] [blame] | 1158 | error_report_err(local_err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1159 | return 1; |
| 1160 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1161 | |
| 1162 | qprintf(quiet, "Image committed.\n"); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1163 | return 0; |
| 1164 | } |
| 1165 | |
Dmitry Konishchev | f6a00aa | 2011-05-18 15:03:59 +0400 | [diff] [blame] | 1166 | /* |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1167 | * Returns -1 if 'buf' contains only zeroes, otherwise the byte index |
| 1168 | * of the first sector boundary within buf where the sector contains a |
| 1169 | * non-zero byte. This function is robust to a buffer that is not |
| 1170 | * sector-aligned. |
| 1171 | */ |
| 1172 | static int64_t find_nonzero(const uint8_t *buf, int64_t n) |
| 1173 | { |
| 1174 | int64_t i; |
| 1175 | int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE); |
| 1176 | |
| 1177 | for (i = 0; i < end; i += BDRV_SECTOR_SIZE) { |
| 1178 | if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) { |
| 1179 | return i; |
| 1180 | } |
| 1181 | } |
| 1182 | if (i < n && !buffer_is_zero(buf + i, n - end)) { |
| 1183 | return i; |
| 1184 | } |
| 1185 | return -1; |
| 1186 | } |
| 1187 | |
| 1188 | /* |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1189 | * Returns true iff the first sector pointed to by 'buf' contains at least |
| 1190 | * a non-NUL byte. |
| 1191 | * |
| 1192 | * 'pnum' is set to the number of sectors (including and immediately following |
| 1193 | * 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] | 1194 | * The function will try to align the end offset to alignment boundaries so |
| 1195 | * that the request will at least end aligned and consequtive requests will |
| 1196 | * also start at an aligned offset. |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1197 | */ |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1198 | static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum, |
| 1199 | int64_t sector_num, int alignment) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1200 | { |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1201 | bool is_zero; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1202 | int i, tail; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1203 | |
| 1204 | if (n <= 0) { |
| 1205 | *pnum = 0; |
| 1206 | return 0; |
| 1207 | } |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1208 | is_zero = buffer_is_zero(buf, 512); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1209 | for(i = 1; i < n; i++) { |
| 1210 | buf += 512; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1211 | if (is_zero != buffer_is_zero(buf, 512)) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1212 | break; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1213 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1214 | } |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1215 | |
| 1216 | tail = (sector_num + i) & (alignment - 1); |
| 1217 | if (tail) { |
| 1218 | if (is_zero && i <= tail) { |
| 1219 | /* treat unallocated areas which only consist |
| 1220 | * of a small tail as allocated. */ |
| 1221 | is_zero = false; |
| 1222 | } |
| 1223 | if (!is_zero) { |
| 1224 | /* align up end offset of allocated areas. */ |
| 1225 | i += alignment - tail; |
| 1226 | i = MIN(i, n); |
| 1227 | } else { |
| 1228 | /* align down end offset of zero areas. */ |
| 1229 | i -= tail; |
| 1230 | } |
| 1231 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1232 | *pnum = i; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1233 | return !is_zero; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1236 | /* |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1237 | * Like is_allocated_sectors, but if the buffer starts with a used sector, |
| 1238 | * up to 'min' consecutive sectors containing zeros are ignored. This avoids |
| 1239 | * breaking up write requests for only small sparse areas. |
| 1240 | */ |
| 1241 | 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] | 1242 | int min, int64_t sector_num, int alignment) |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1243 | { |
| 1244 | int ret; |
| 1245 | int num_checked, num_used; |
| 1246 | |
| 1247 | if (n < min) { |
| 1248 | min = n; |
| 1249 | } |
| 1250 | |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1251 | ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment); |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1252 | if (!ret) { |
| 1253 | return ret; |
| 1254 | } |
| 1255 | |
| 1256 | num_used = *pnum; |
| 1257 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 1258 | n -= *pnum; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1259 | sector_num += *pnum; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1260 | num_checked = num_used; |
| 1261 | |
| 1262 | while (n > 0) { |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1263 | ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment); |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1264 | |
| 1265 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 1266 | n -= *pnum; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1267 | sector_num += *pnum; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1268 | num_checked += *pnum; |
| 1269 | if (ret) { |
| 1270 | num_used = num_checked; |
| 1271 | } else if (*pnum >= min) { |
| 1272 | break; |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | *pnum = num_used; |
| 1277 | return 1; |
| 1278 | } |
| 1279 | |
| 1280 | /* |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1281 | * Compares two buffers sector by sector. Returns 0 if the first |
| 1282 | * sector of each buffer matches, non-zero otherwise. |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1283 | * |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1284 | * pnum is set to the sector-aligned size of the buffer prefix that |
| 1285 | * has the same matching status as the first sector. |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1286 | */ |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1287 | static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2, |
| 1288 | int64_t bytes, int64_t *pnum) |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1289 | { |
Radim Krčmář | 8c1ac47 | 2015-02-20 17:06:15 +0100 | [diff] [blame] | 1290 | bool res; |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1291 | int64_t i = MIN(bytes, BDRV_SECTOR_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1292 | |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1293 | assert(bytes > 0); |
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 | res = !!memcmp(buf1, buf2, i); |
| 1296 | while (i < bytes) { |
| 1297 | int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1298 | |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1299 | if (!!memcmp(buf1 + i, buf2 + i, len) != res) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1300 | break; |
| 1301 | } |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1302 | i += len; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | *pnum = i; |
| 1306 | return res; |
| 1307 | } |
| 1308 | |
Stefano Garzarella | 97ede57 | 2019-05-08 12:43:24 +0200 | [diff] [blame] | 1309 | #define IO_BUF_SIZE (2 * MiB) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1310 | |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1311 | /* |
| 1312 | * Check if passed sectors are empty (not allocated or contain only 0 bytes) |
| 1313 | * |
Eric Blake | 0608e40 | 2017-10-11 22:47:12 -0500 | [diff] [blame] | 1314 | * Intended for use by 'qemu-img compare': Returns 0 in case sectors are |
| 1315 | * filled with 0, 1 if sectors contain non-zero data (this is a comparison |
| 1316 | * failure), and 4 on error (the exit status for read errors), after emitting |
| 1317 | * an error message. |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1318 | * |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1319 | * @param blk: BlockBackend for the image |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1320 | * @param offset: Starting offset to check |
| 1321 | * @param bytes: Number of bytes to check |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1322 | * @param filename: Name of disk file we are checking (logging purpose) |
| 1323 | * @param buffer: Allocated buffer for storing read data |
| 1324 | * @param quiet: Flag for quiet mode |
| 1325 | */ |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1326 | static int check_empty_sectors(BlockBackend *blk, int64_t offset, |
| 1327 | int64_t bytes, const char *filename, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1328 | uint8_t *buffer, bool quiet) |
| 1329 | { |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1330 | int ret = 0; |
| 1331 | int64_t idx; |
| 1332 | |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1333 | ret = blk_pread(blk, offset, buffer, bytes); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1334 | if (ret < 0) { |
| 1335 | error_report("Error while reading offset %" PRId64 " of %s: %s", |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1336 | offset, filename, strerror(-ret)); |
Eric Blake | 0608e40 | 2017-10-11 22:47:12 -0500 | [diff] [blame] | 1337 | return 4; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1338 | } |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1339 | idx = find_nonzero(buffer, bytes); |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1340 | if (idx >= 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1341 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1342 | offset + idx); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1343 | return 1; |
| 1344 | } |
| 1345 | |
| 1346 | return 0; |
| 1347 | } |
| 1348 | |
| 1349 | /* |
| 1350 | * Compares two images. Exit codes: |
| 1351 | * |
| 1352 | * 0 - Images are identical |
| 1353 | * 1 - Images differ |
| 1354 | * >1 - Error occurred |
| 1355 | */ |
| 1356 | static int img_compare(int argc, char **argv) |
| 1357 | { |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1358 | const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1359 | BlockBackend *blk1, *blk2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1360 | BlockDriverState *bs1, *bs2; |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1361 | int64_t total_size1, total_size2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1362 | uint8_t *buf1 = NULL, *buf2 = NULL; |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1363 | int64_t pnum1, pnum2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1364 | int allocated1, allocated2; |
| 1365 | int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */ |
| 1366 | bool progress = false, quiet = false, strict = false; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1367 | int flags; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1368 | bool writethrough; |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1369 | int64_t total_size; |
| 1370 | int64_t offset = 0; |
| 1371 | int64_t chunk; |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1372 | int c; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1373 | uint64_t progress_base; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1374 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1375 | bool force_share = false; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1376 | |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1377 | cache = BDRV_DEFAULT_CACHE; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1378 | for (;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1379 | static const struct option long_options[] = { |
| 1380 | {"help", no_argument, 0, 'h'}, |
| 1381 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1382 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1383 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1384 | {0, 0, 0, 0} |
| 1385 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1386 | c = getopt_long(argc, argv, ":hf:F:T:pqsU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1387 | long_options, NULL); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1388 | if (c == -1) { |
| 1389 | break; |
| 1390 | } |
| 1391 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 1392 | case ':': |
| 1393 | missing_argument(argv[optind - 1]); |
| 1394 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1395 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 1396 | unrecognized_option(argv[optind - 1]); |
| 1397 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1398 | case 'h': |
| 1399 | help(); |
| 1400 | break; |
| 1401 | case 'f': |
| 1402 | fmt1 = optarg; |
| 1403 | break; |
| 1404 | case 'F': |
| 1405 | fmt2 = optarg; |
| 1406 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1407 | case 'T': |
| 1408 | cache = optarg; |
| 1409 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1410 | case 'p': |
| 1411 | progress = true; |
| 1412 | break; |
| 1413 | case 'q': |
| 1414 | quiet = true; |
| 1415 | break; |
| 1416 | case 's': |
| 1417 | strict = true; |
| 1418 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1419 | case 'U': |
| 1420 | force_share = true; |
| 1421 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1422 | case OPTION_OBJECT: { |
| 1423 | QemuOpts *opts; |
| 1424 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 1425 | optarg, true); |
| 1426 | if (!opts) { |
| 1427 | ret = 2; |
| 1428 | goto out4; |
| 1429 | } |
| 1430 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1431 | case OPTION_IMAGE_OPTS: |
| 1432 | image_opts = true; |
| 1433 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | /* Progress is not shown in Quiet mode */ |
| 1438 | if (quiet) { |
| 1439 | progress = false; |
| 1440 | } |
| 1441 | |
| 1442 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 1443 | if (optind != argc - 2) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 1444 | error_exit("Expecting two image file names"); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1445 | } |
| 1446 | filename1 = argv[optind++]; |
| 1447 | filename2 = argv[optind++]; |
| 1448 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1449 | if (qemu_opts_foreach(&qemu_object_opts, |
| 1450 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 1451 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1452 | ret = 2; |
| 1453 | goto out4; |
| 1454 | } |
| 1455 | |
Stefan Hajnoczi | cbda016 | 2014-08-26 19:17:55 +0100 | [diff] [blame] | 1456 | /* Initialize before goto out */ |
| 1457 | qemu_progress_init(progress, 2.0); |
| 1458 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1459 | flags = 0; |
| 1460 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1461 | if (ret < 0) { |
| 1462 | error_report("Invalid source cache option: %s", cache); |
| 1463 | ret = 2; |
| 1464 | goto out3; |
| 1465 | } |
| 1466 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1467 | blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet, |
| 1468 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1469 | if (!blk1) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1470 | ret = 2; |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1471 | goto out3; |
| 1472 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1473 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1474 | blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet, |
| 1475 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1476 | if (!blk2) { |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1477 | ret = 2; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1478 | goto out2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1479 | } |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1480 | bs1 = blk_bs(blk1); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1481 | bs2 = blk_bs(blk2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1482 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1483 | buf1 = blk_blockalign(blk1, IO_BUF_SIZE); |
| 1484 | buf2 = blk_blockalign(blk2, IO_BUF_SIZE); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1485 | total_size1 = blk_getlength(blk1); |
| 1486 | if (total_size1 < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1487 | error_report("Can't get size of %s: %s", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1488 | filename1, strerror(-total_size1)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1489 | ret = 4; |
| 1490 | goto out; |
| 1491 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1492 | total_size2 = blk_getlength(blk2); |
| 1493 | if (total_size2 < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1494 | error_report("Can't get size of %s: %s", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1495 | filename2, strerror(-total_size2)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1496 | ret = 4; |
| 1497 | goto out; |
| 1498 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1499 | total_size = MIN(total_size1, total_size2); |
| 1500 | progress_base = MAX(total_size1, total_size2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1501 | |
| 1502 | qemu_progress_print(0, 100); |
| 1503 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1504 | if (strict && total_size1 != total_size2) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1505 | ret = 1; |
| 1506 | qprintf(quiet, "Strict mode: Image size mismatch!\n"); |
| 1507 | goto out; |
| 1508 | } |
| 1509 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1510 | while (offset < total_size) { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1511 | int status1, status2; |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 1512 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1513 | status1 = bdrv_block_status_above(bs1, NULL, offset, |
| 1514 | total_size1 - offset, &pnum1, NULL, |
| 1515 | NULL); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1516 | if (status1 < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1517 | ret = 3; |
| 1518 | error_report("Sector allocation test failed for %s", filename1); |
| 1519 | goto out; |
| 1520 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1521 | allocated1 = status1 & BDRV_BLOCK_ALLOCATED; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1522 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1523 | status2 = bdrv_block_status_above(bs2, NULL, offset, |
| 1524 | total_size2 - offset, &pnum2, NULL, |
| 1525 | NULL); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1526 | if (status2 < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1527 | ret = 3; |
| 1528 | error_report("Sector allocation test failed for %s", filename2); |
| 1529 | goto out; |
| 1530 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1531 | allocated2 = status2 & BDRV_BLOCK_ALLOCATED; |
Eric Blake | 7daddc6 | 2017-10-11 22:47:09 -0500 | [diff] [blame] | 1532 | |
| 1533 | assert(pnum1 && pnum2); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1534 | chunk = MIN(pnum1, pnum2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1535 | |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1536 | if (strict) { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1537 | if (status1 != status2) { |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1538 | ret = 1; |
| 1539 | qprintf(quiet, "Strict mode: Offset %" PRId64 |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1540 | " block status mismatch!\n", offset); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1541 | goto out; |
| 1542 | } |
| 1543 | } |
| 1544 | if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) { |
Eric Blake | 7daddc6 | 2017-10-11 22:47:09 -0500 | [diff] [blame] | 1545 | /* nothing to do */ |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1546 | } else if (allocated1 == allocated2) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1547 | if (allocated1) { |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1548 | int64_t pnum; |
| 1549 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1550 | chunk = MIN(chunk, IO_BUF_SIZE); |
| 1551 | ret = blk_pread(blk1, offset, buf1, chunk); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1552 | if (ret < 0) { |
| 1553 | error_report("Error while reading offset %" PRId64 |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1554 | " of %s: %s", |
| 1555 | offset, filename1, strerror(-ret)); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1556 | ret = 4; |
| 1557 | goto out; |
| 1558 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1559 | ret = blk_pread(blk2, offset, buf2, chunk); |
| 1560 | if (ret < 0) { |
| 1561 | error_report("Error while reading offset %" PRId64 |
| 1562 | " of %s: %s", |
| 1563 | offset, filename2, strerror(-ret)); |
| 1564 | ret = 4; |
| 1565 | goto out; |
| 1566 | } |
| 1567 | ret = compare_buffers(buf1, buf2, chunk, &pnum); |
| 1568 | if (ret || pnum != chunk) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1569 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1570 | offset + (ret ? 0 : pnum)); |
Fam Zheng | 36452f1 | 2013-11-13 20:26:49 +0800 | [diff] [blame] | 1571 | ret = 1; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1572 | goto out; |
| 1573 | } |
| 1574 | } |
| 1575 | } else { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1576 | chunk = MIN(chunk, IO_BUF_SIZE); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1577 | if (allocated1) { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1578 | ret = check_empty_sectors(blk1, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1579 | filename1, buf1, quiet); |
| 1580 | } else { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1581 | ret = check_empty_sectors(blk2, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1582 | filename2, buf1, quiet); |
| 1583 | } |
| 1584 | if (ret) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1585 | goto out; |
| 1586 | } |
| 1587 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1588 | offset += chunk; |
| 1589 | qemu_progress_print(((float) chunk / progress_base) * 100, 100); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1590 | } |
| 1591 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1592 | if (total_size1 != total_size2) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1593 | BlockBackend *blk_over; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1594 | const char *filename_over; |
| 1595 | |
| 1596 | qprintf(quiet, "Warning: Image size mismatch!\n"); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1597 | if (total_size1 > total_size2) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1598 | blk_over = blk1; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1599 | filename_over = filename1; |
| 1600 | } else { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1601 | blk_over = blk2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1602 | filename_over = filename2; |
| 1603 | } |
| 1604 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1605 | while (offset < progress_base) { |
| 1606 | ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset, |
| 1607 | progress_base - offset, &chunk, |
| 1608 | NULL, NULL); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1609 | if (ret < 0) { |
| 1610 | ret = 3; |
| 1611 | error_report("Sector allocation test failed for %s", |
| 1612 | filename_over); |
| 1613 | goto out; |
| 1614 | |
| 1615 | } |
Eric Blake | 391cb1a | 2017-10-11 22:47:10 -0500 | [diff] [blame] | 1616 | if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1617 | chunk = MIN(chunk, IO_BUF_SIZE); |
| 1618 | ret = check_empty_sectors(blk_over, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1619 | filename_over, buf1, quiet); |
| 1620 | if (ret) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1621 | goto out; |
| 1622 | } |
| 1623 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1624 | offset += chunk; |
| 1625 | qemu_progress_print(((float) chunk / progress_base) * 100, 100); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | qprintf(quiet, "Images are identical.\n"); |
| 1630 | ret = 0; |
| 1631 | |
| 1632 | out: |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1633 | qemu_vfree(buf1); |
| 1634 | qemu_vfree(buf2); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1635 | blk_unref(blk2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1636 | out2: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1637 | blk_unref(blk1); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1638 | out3: |
| 1639 | qemu_progress_end(); |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1640 | out4: |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1641 | return ret; |
| 1642 | } |
| 1643 | |
Eric Blake | 6c729dd | 2020-05-21 14:21:35 -0500 | [diff] [blame] | 1644 | /* Convenience wrapper around qmp_block_dirty_bitmap_merge */ |
| 1645 | static void do_dirty_bitmap_merge(const char *dst_node, const char *dst_name, |
| 1646 | const char *src_node, const char *src_name, |
| 1647 | Error **errp) |
| 1648 | { |
| 1649 | BlockDirtyBitmapMergeSource *merge_src; |
| 1650 | BlockDirtyBitmapMergeSourceList *list; |
| 1651 | |
| 1652 | merge_src = g_new0(BlockDirtyBitmapMergeSource, 1); |
| 1653 | merge_src->type = QTYPE_QDICT; |
| 1654 | merge_src->u.external.node = g_strdup(src_node); |
| 1655 | merge_src->u.external.name = g_strdup(src_name); |
| 1656 | list = g_new0(BlockDirtyBitmapMergeSourceList, 1); |
| 1657 | list->value = merge_src; |
| 1658 | qmp_block_dirty_bitmap_merge(dst_node, dst_name, list, errp); |
| 1659 | qapi_free_BlockDirtyBitmapMergeSourceList(list); |
| 1660 | } |
| 1661 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1662 | enum ImgConvertBlockStatus { |
| 1663 | BLK_DATA, |
| 1664 | BLK_ZERO, |
| 1665 | BLK_BACKING_FILE, |
| 1666 | }; |
| 1667 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1668 | #define MAX_COROUTINES 16 |
| 1669 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1670 | typedef struct ImgConvertState { |
| 1671 | BlockBackend **src; |
| 1672 | int64_t *src_sectors; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1673 | int src_num; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1674 | int64_t total_sectors; |
| 1675 | int64_t allocated_sectors; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1676 | int64_t allocated_done; |
| 1677 | int64_t sector_num; |
| 1678 | int64_t wr_offs; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1679 | enum ImgConvertBlockStatus status; |
| 1680 | int64_t sector_next_status; |
| 1681 | BlockBackend *target; |
| 1682 | bool has_zero_init; |
| 1683 | bool compressed; |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 1684 | bool target_is_new; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1685 | bool target_has_backing; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1686 | int64_t target_backing_sectors; /* negative if unknown */ |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1687 | bool wr_in_order; |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1688 | bool copy_range; |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1689 | bool salvage; |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 1690 | bool quiet; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1691 | int min_sparse; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1692 | int alignment; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1693 | size_t cluster_sectors; |
| 1694 | size_t buf_sectors; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 1695 | long num_coroutines; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1696 | int running_coroutines; |
| 1697 | Coroutine *co[MAX_COROUTINES]; |
| 1698 | int64_t wait_sector_num[MAX_COROUTINES]; |
| 1699 | CoMutex lock; |
| 1700 | int ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1701 | } ImgConvertState; |
| 1702 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1703 | static void convert_select_part(ImgConvertState *s, int64_t sector_num, |
| 1704 | int *src_cur, int64_t *src_cur_offset) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1705 | { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1706 | *src_cur = 0; |
| 1707 | *src_cur_offset = 0; |
| 1708 | while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) { |
| 1709 | *src_cur_offset += s->src_sectors[*src_cur]; |
| 1710 | (*src_cur)++; |
| 1711 | assert(*src_cur < s->src_num); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1712 | } |
| 1713 | } |
| 1714 | |
| 1715 | static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num) |
| 1716 | { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1717 | int64_t src_cur_offset; |
| 1718 | int ret, n, src_cur; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1719 | bool post_backing_zero = false; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1720 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1721 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1722 | |
| 1723 | assert(s->total_sectors > sector_num); |
| 1724 | n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS); |
| 1725 | |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1726 | if (s->target_backing_sectors >= 0) { |
| 1727 | if (sector_num >= s->target_backing_sectors) { |
Vladimir Sementsov-Ogievskiy | 2253d86 | 2020-05-28 12:43:56 +0300 | [diff] [blame^] | 1728 | post_backing_zero = true; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1729 | } else if (sector_num + n > s->target_backing_sectors) { |
| 1730 | /* Split requests around target_backing_sectors (because |
| 1731 | * starting from there, zeros are handled differently) */ |
| 1732 | n = s->target_backing_sectors - sector_num; |
| 1733 | } |
| 1734 | } |
| 1735 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1736 | if (s->sector_next_status <= sector_num) { |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1737 | uint64_t offset = (sector_num - src_cur_offset) * BDRV_SECTOR_SIZE; |
| 1738 | int64_t count; |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1739 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1740 | do { |
| 1741 | count = n * BDRV_SECTOR_SIZE; |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 1742 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1743 | if (s->target_has_backing) { |
| 1744 | ret = bdrv_block_status(blk_bs(s->src[src_cur]), offset, |
| 1745 | count, &count, NULL, NULL); |
| 1746 | } else { |
| 1747 | ret = bdrv_block_status_above(blk_bs(s->src[src_cur]), NULL, |
| 1748 | offset, count, &count, NULL, |
| 1749 | NULL); |
| 1750 | } |
| 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 | |
| 1777 | if (ret & BDRV_BLOCK_ZERO) { |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1778 | s->status = post_backing_zero ? BLK_BACKING_FILE : BLK_ZERO; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1779 | } else if (ret & BDRV_BLOCK_DATA) { |
| 1780 | s->status = BLK_DATA; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1781 | } else { |
Vladimir Sementsov-Ogievskiy | 9f1b92a | 2017-04-07 14:34:04 +0300 | [diff] [blame] | 1782 | s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | s->sector_next_status = sector_num + n; |
| 1786 | } |
| 1787 | |
| 1788 | n = MIN(n, s->sector_next_status - sector_num); |
| 1789 | if (s->status == BLK_DATA) { |
| 1790 | n = MIN(n, s->buf_sectors); |
| 1791 | } |
| 1792 | |
| 1793 | /* We need to write complete clusters for compressed images, so if an |
| 1794 | * unallocated area is shorter than that, we must consider the whole |
| 1795 | * cluster allocated. */ |
| 1796 | if (s->compressed) { |
| 1797 | if (n < s->cluster_sectors) { |
| 1798 | n = MIN(s->cluster_sectors, s->total_sectors - sector_num); |
| 1799 | s->status = BLK_DATA; |
| 1800 | } else { |
| 1801 | n = QEMU_ALIGN_DOWN(n, s->cluster_sectors); |
| 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | return n; |
| 1806 | } |
| 1807 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1808 | static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num, |
| 1809 | int nb_sectors, uint8_t *buf) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1810 | { |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1811 | uint64_t single_read_until = 0; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1812 | int n, ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1813 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1814 | assert(nb_sectors <= s->buf_sectors); |
| 1815 | while (nb_sectors > 0) { |
| 1816 | BlockBackend *blk; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1817 | int src_cur; |
| 1818 | int64_t bs_sectors, src_cur_offset; |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1819 | uint64_t offset; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1820 | |
| 1821 | /* In the case of compression with multiple source files, we can get a |
| 1822 | * nb_sectors that spreads into the next part. So we must be able to |
| 1823 | * read across multiple BDSes for one convert_read() call. */ |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1824 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
| 1825 | blk = s->src[src_cur]; |
| 1826 | bs_sectors = s->src_sectors[src_cur]; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1827 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1828 | offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1829 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1830 | n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset)); |
| 1831 | if (single_read_until > offset) { |
| 1832 | n = 1; |
| 1833 | } |
| 1834 | |
| 1835 | ret = blk_co_pread(blk, offset, n << BDRV_SECTOR_BITS, buf, 0); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1836 | if (ret < 0) { |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1837 | if (s->salvage) { |
| 1838 | if (n > 1) { |
| 1839 | single_read_until = offset + (n << BDRV_SECTOR_BITS); |
| 1840 | continue; |
| 1841 | } else { |
| 1842 | if (!s->quiet) { |
| 1843 | warn_report("error while reading offset %" PRIu64 |
| 1844 | ": %s", offset, strerror(-ret)); |
| 1845 | } |
| 1846 | memset(buf, 0, BDRV_SECTOR_SIZE); |
| 1847 | } |
| 1848 | } else { |
| 1849 | return ret; |
| 1850 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | sector_num += n; |
| 1854 | nb_sectors -= n; |
| 1855 | buf += n * BDRV_SECTOR_SIZE; |
| 1856 | } |
| 1857 | |
| 1858 | return 0; |
| 1859 | } |
| 1860 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1861 | |
| 1862 | static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num, |
| 1863 | int nb_sectors, uint8_t *buf, |
| 1864 | enum ImgConvertBlockStatus status) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1865 | { |
| 1866 | int ret; |
| 1867 | |
| 1868 | while (nb_sectors > 0) { |
| 1869 | int n = nb_sectors; |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1870 | BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0; |
| 1871 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1872 | switch (status) { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1873 | case BLK_BACKING_FILE: |
| 1874 | /* If we have a backing file, leave clusters unallocated that are |
| 1875 | * unallocated in the source image, so that the backing file is |
| 1876 | * visible at the respective offset. */ |
| 1877 | assert(s->target_has_backing); |
| 1878 | break; |
| 1879 | |
| 1880 | case BLK_DATA: |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1881 | /* If we're told to keep the target fully allocated (-S 0) or there |
| 1882 | * is real non-zero data, we must write it. Otherwise we can treat |
| 1883 | * it as zero sectors. |
| 1884 | * Compressed clusters need to be written as a whole, so in that |
| 1885 | * case we can only save the write if the buffer is completely |
| 1886 | * zeroed. */ |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1887 | if (!s->min_sparse || |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1888 | (!s->compressed && |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1889 | is_allocated_sectors_min(buf, n, &n, s->min_sparse, |
| 1890 | sector_num, s->alignment)) || |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1891 | (s->compressed && |
| 1892 | !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1893 | { |
Vladimir Sementsov-Ogievskiy | 265a7e5 | 2019-04-22 17:58:38 +0300 | [diff] [blame] | 1894 | ret = blk_co_pwrite(s->target, sector_num << BDRV_SECTOR_BITS, |
| 1895 | n << BDRV_SECTOR_BITS, buf, flags); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1896 | if (ret < 0) { |
| 1897 | return ret; |
| 1898 | } |
| 1899 | break; |
| 1900 | } |
| 1901 | /* fall-through */ |
| 1902 | |
| 1903 | case BLK_ZERO: |
| 1904 | if (s->has_zero_init) { |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1905 | assert(!s->target_has_backing); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1906 | break; |
| 1907 | } |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1908 | ret = blk_co_pwrite_zeroes(s->target, |
| 1909 | sector_num << BDRV_SECTOR_BITS, |
Nir Soffer | a3d6ae2 | 2019-03-24 02:20:12 +0200 | [diff] [blame] | 1910 | n << BDRV_SECTOR_BITS, |
| 1911 | BDRV_REQ_MAY_UNMAP); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1912 | if (ret < 0) { |
| 1913 | return ret; |
| 1914 | } |
| 1915 | break; |
| 1916 | } |
| 1917 | |
| 1918 | sector_num += n; |
| 1919 | nb_sectors -= n; |
| 1920 | buf += n * BDRV_SECTOR_SIZE; |
| 1921 | } |
| 1922 | |
| 1923 | return 0; |
| 1924 | } |
| 1925 | |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1926 | static int coroutine_fn convert_co_copy_range(ImgConvertState *s, int64_t sector_num, |
| 1927 | int nb_sectors) |
| 1928 | { |
| 1929 | int n, ret; |
| 1930 | |
| 1931 | while (nb_sectors > 0) { |
| 1932 | BlockBackend *blk; |
| 1933 | int src_cur; |
| 1934 | int64_t bs_sectors, src_cur_offset; |
| 1935 | int64_t offset; |
| 1936 | |
| 1937 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
| 1938 | offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS; |
| 1939 | blk = s->src[src_cur]; |
| 1940 | bs_sectors = s->src_sectors[src_cur]; |
| 1941 | |
| 1942 | n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset)); |
| 1943 | |
| 1944 | ret = blk_co_copy_range(blk, offset, s->target, |
| 1945 | sector_num << BDRV_SECTOR_BITS, |
Vladimir Sementsov-Ogievskiy | 67b51fb | 2018-07-09 19:37:17 +0300 | [diff] [blame] | 1946 | n << BDRV_SECTOR_BITS, 0, 0); |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1947 | if (ret < 0) { |
| 1948 | return ret; |
| 1949 | } |
| 1950 | |
| 1951 | sector_num += n; |
| 1952 | nb_sectors -= n; |
| 1953 | } |
| 1954 | return 0; |
| 1955 | } |
| 1956 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1957 | static void coroutine_fn convert_co_do_copy(void *opaque) |
| 1958 | { |
| 1959 | ImgConvertState *s = opaque; |
| 1960 | uint8_t *buf = NULL; |
| 1961 | int ret, i; |
| 1962 | int index = -1; |
| 1963 | |
| 1964 | for (i = 0; i < s->num_coroutines; i++) { |
| 1965 | if (s->co[i] == qemu_coroutine_self()) { |
| 1966 | index = i; |
| 1967 | break; |
| 1968 | } |
| 1969 | } |
| 1970 | assert(index >= 0); |
| 1971 | |
| 1972 | s->running_coroutines++; |
| 1973 | buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE); |
| 1974 | |
| 1975 | while (1) { |
| 1976 | int n; |
| 1977 | int64_t sector_num; |
| 1978 | enum ImgConvertBlockStatus status; |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1979 | bool copy_range; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1980 | |
| 1981 | qemu_co_mutex_lock(&s->lock); |
| 1982 | if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) { |
| 1983 | qemu_co_mutex_unlock(&s->lock); |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1984 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1985 | } |
| 1986 | n = convert_iteration_sectors(s, s->sector_num); |
| 1987 | if (n < 0) { |
| 1988 | qemu_co_mutex_unlock(&s->lock); |
| 1989 | s->ret = n; |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1990 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1991 | } |
| 1992 | /* save current sector and allocation status to local variables */ |
| 1993 | sector_num = s->sector_num; |
| 1994 | status = s->status; |
| 1995 | if (!s->min_sparse && s->status == BLK_ZERO) { |
| 1996 | n = MIN(n, s->buf_sectors); |
| 1997 | } |
| 1998 | /* increment global sector counter so that other coroutines can |
| 1999 | * already continue reading beyond this request */ |
| 2000 | s->sector_num += n; |
| 2001 | qemu_co_mutex_unlock(&s->lock); |
| 2002 | |
| 2003 | if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) { |
| 2004 | s->allocated_done += n; |
| 2005 | qemu_progress_print(100.0 * s->allocated_done / |
| 2006 | s->allocated_sectors, 0); |
| 2007 | } |
| 2008 | |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 2009 | retry: |
| 2010 | copy_range = s->copy_range && s->status == BLK_DATA; |
| 2011 | if (status == BLK_DATA && !copy_range) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2012 | ret = convert_co_read(s, sector_num, n, buf); |
| 2013 | if (ret < 0) { |
Eric Blake | 39f77cb | 2020-04-02 08:57:17 -0500 | [diff] [blame] | 2014 | error_report("error while reading at byte %lld: %s", |
| 2015 | sector_num * BDRV_SECTOR_SIZE, strerror(-ret)); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2016 | s->ret = ret; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2017 | } |
| 2018 | } else if (!s->min_sparse && status == BLK_ZERO) { |
| 2019 | status = BLK_DATA; |
| 2020 | memset(buf, 0x00, n * BDRV_SECTOR_SIZE); |
| 2021 | } |
| 2022 | |
| 2023 | if (s->wr_in_order) { |
| 2024 | /* keep writes in order */ |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2025 | while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2026 | s->wait_sector_num[index] = sector_num; |
| 2027 | qemu_coroutine_yield(); |
| 2028 | } |
| 2029 | s->wait_sector_num[index] = -1; |
| 2030 | } |
| 2031 | |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2032 | if (s->ret == -EINPROGRESS) { |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 2033 | if (copy_range) { |
| 2034 | ret = convert_co_copy_range(s, sector_num, n); |
| 2035 | if (ret) { |
| 2036 | s->copy_range = false; |
| 2037 | goto retry; |
| 2038 | } |
| 2039 | } else { |
| 2040 | ret = convert_co_write(s, sector_num, n, buf, status); |
| 2041 | } |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2042 | if (ret < 0) { |
Eric Blake | 39f77cb | 2020-04-02 08:57:17 -0500 | [diff] [blame] | 2043 | error_report("error while writing at byte %lld: %s", |
| 2044 | sector_num * BDRV_SECTOR_SIZE, strerror(-ret)); |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2045 | s->ret = ret; |
| 2046 | } |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | if (s->wr_in_order) { |
| 2050 | /* reenter the coroutine that might have waited |
| 2051 | * for this write to complete */ |
| 2052 | s->wr_offs = sector_num + n; |
| 2053 | for (i = 0; i < s->num_coroutines; i++) { |
| 2054 | if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) { |
| 2055 | /* |
| 2056 | * A -> B -> A cannot occur because A has |
| 2057 | * s->wait_sector_num[i] == -1 during A -> B. Therefore |
| 2058 | * B will never enter A during this time window. |
| 2059 | */ |
| 2060 | qemu_coroutine_enter(s->co[i]); |
| 2061 | break; |
| 2062 | } |
| 2063 | } |
| 2064 | } |
| 2065 | } |
| 2066 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2067 | qemu_vfree(buf); |
| 2068 | s->co[index] = NULL; |
| 2069 | s->running_coroutines--; |
| 2070 | if (!s->running_coroutines && s->ret == -EINPROGRESS) { |
| 2071 | /* the convert job finished successfully */ |
| 2072 | s->ret = 0; |
| 2073 | } |
| 2074 | } |
| 2075 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2076 | static int convert_do_copy(ImgConvertState *s) |
| 2077 | { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2078 | int ret, i, n; |
| 2079 | int64_t sector_num = 0; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2080 | |
| 2081 | /* Check whether we have zero initialisation or can get it efficiently */ |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2082 | if (!s->has_zero_init && s->target_is_new && s->min_sparse && |
| 2083 | !s->target_has_backing) { |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 2084 | s->has_zero_init = bdrv_has_zero_init(blk_bs(s->target)); |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 2085 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2086 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2087 | /* Allocate buffer for copied data. For compressed images, only one cluster |
| 2088 | * can be copied at a time. */ |
| 2089 | if (s->compressed) { |
| 2090 | if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) { |
| 2091 | error_report("invalid cluster size"); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2092 | return -EINVAL; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2093 | } |
| 2094 | s->buf_sectors = s->cluster_sectors; |
| 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 | while (sector_num < s->total_sectors) { |
| 2098 | n = convert_iteration_sectors(s, sector_num); |
| 2099 | if (n < 0) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2100 | return n; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2101 | } |
Max Reitz | aad15de | 2016-03-24 23:33:57 +0100 | [diff] [blame] | 2102 | if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO)) |
| 2103 | { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2104 | s->allocated_sectors += n; |
| 2105 | } |
| 2106 | sector_num += n; |
| 2107 | } |
| 2108 | |
| 2109 | /* Do the copy */ |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2110 | s->sector_next_status = 0; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2111 | s->ret = -EINPROGRESS; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2112 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2113 | qemu_co_mutex_init(&s->lock); |
| 2114 | for (i = 0; i < s->num_coroutines; i++) { |
| 2115 | s->co[i] = qemu_coroutine_create(convert_co_do_copy, s); |
| 2116 | s->wait_sector_num[i] = -1; |
| 2117 | qemu_coroutine_enter(s->co[i]); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2118 | } |
| 2119 | |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2120 | while (s->running_coroutines) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2121 | main_loop_wait(false); |
| 2122 | } |
| 2123 | |
| 2124 | if (s->compressed && !s->ret) { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2125 | /* signal EOF to align */ |
Pavel Butsykin | fe5c135 | 2016-07-22 11:17:40 +0300 | [diff] [blame] | 2126 | ret = blk_pwrite_compressed(s->target, 0, NULL, 0); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2127 | if (ret < 0) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2128 | return ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2129 | } |
| 2130 | } |
| 2131 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2132 | return s->ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2133 | } |
| 2134 | |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2135 | static int convert_copy_bitmaps(BlockDriverState *src, BlockDriverState *dst) |
| 2136 | { |
| 2137 | BdrvDirtyBitmap *bm; |
| 2138 | Error *err = NULL; |
| 2139 | |
| 2140 | FOR_EACH_DIRTY_BITMAP(src, bm) { |
| 2141 | const char *name; |
| 2142 | |
| 2143 | if (!bdrv_dirty_bitmap_get_persistence(bm)) { |
| 2144 | continue; |
| 2145 | } |
| 2146 | name = bdrv_dirty_bitmap_name(bm); |
| 2147 | qmp_block_dirty_bitmap_add(dst->node_name, name, |
| 2148 | true, bdrv_dirty_bitmap_granularity(bm), |
| 2149 | true, true, |
| 2150 | true, !bdrv_dirty_bitmap_enabled(bm), |
| 2151 | &err); |
| 2152 | if (err) { |
| 2153 | error_reportf_err(err, "Failed to create bitmap %s: ", name); |
| 2154 | return -1; |
| 2155 | } |
| 2156 | |
| 2157 | do_dirty_bitmap_merge(dst->node_name, name, src->node_name, name, |
| 2158 | &err); |
| 2159 | if (err) { |
| 2160 | error_reportf_err(err, "Failed to populate bitmap %s: ", name); |
| 2161 | return -1; |
| 2162 | } |
| 2163 | } |
| 2164 | |
| 2165 | return 0; |
| 2166 | } |
| 2167 | |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 2168 | #define MAX_BUF_SECTORS 32768 |
| 2169 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2170 | static int img_convert(int argc, char **argv) |
| 2171 | { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2172 | int c, bs_i, flags, src_flags = 0; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2173 | const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe", |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2174 | *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL, |
| 2175 | *out_filename, *out_baseimg_param, *snapshot_name = NULL; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2176 | BlockDriver *drv = NULL, *proto_drv = NULL; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2177 | BlockDriverInfo bdi; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2178 | BlockDriverState *out_bs; |
| 2179 | QemuOpts *opts = NULL, *sn_opts = NULL; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2180 | QemuOptsList *create_opts = NULL; |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2181 | QDict *open_opts = NULL; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2182 | char *options = NULL; |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 2183 | Error *local_err = NULL; |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2184 | bool writethrough, src_writethrough, image_opts = false, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2185 | skip_create = false, progress = false, tgt_image_opts = false; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2186 | int64_t ret = -EINVAL; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2187 | bool force_share = false; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2188 | bool explict_min_sparse = false; |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2189 | bool bitmaps = false; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2190 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2191 | ImgConvertState s = (ImgConvertState) { |
| 2192 | /* Need at least 4k of zeros for sparse detection */ |
| 2193 | .min_sparse = 8, |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2194 | .copy_range = false, |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2195 | .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE, |
| 2196 | .wr_in_order = true, |
| 2197 | .num_coroutines = 8, |
| 2198 | }; |
| 2199 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2200 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2201 | static const struct option long_options[] = { |
| 2202 | {"help", no_argument, 0, 'h'}, |
| 2203 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2204 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2205 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2206 | {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS}, |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 2207 | {"salvage", no_argument, 0, OPTION_SALVAGE}, |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2208 | {"target-is-zero", no_argument, 0, OPTION_TARGET_IS_ZERO}, |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2209 | {"bitmaps", no_argument, 0, OPTION_BITMAPS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2210 | {0, 0, 0, 0} |
| 2211 | }; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2212 | 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] | 2213 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2214 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2215 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2216 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2217 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2218 | case ':': |
| 2219 | missing_argument(argv[optind - 1]); |
| 2220 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2221 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2222 | unrecognized_option(argv[optind - 1]); |
| 2223 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2224 | case 'h': |
| 2225 | help(); |
| 2226 | break; |
| 2227 | case 'f': |
| 2228 | fmt = optarg; |
| 2229 | break; |
| 2230 | case 'O': |
| 2231 | out_fmt = optarg; |
| 2232 | break; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 2233 | case 'B': |
| 2234 | out_baseimg = optarg; |
| 2235 | break; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2236 | case 'C': |
| 2237 | s.copy_range = true; |
| 2238 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2239 | case 'c': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2240 | s.compressed = true; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2241 | break; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2242 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 2243 | if (accumulate_options(&options, optarg) < 0) { |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2244 | goto fail_getopt; |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 2245 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2246 | break; |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2247 | case 'l': |
| 2248 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { |
Markus Armbruster | 70b9433 | 2015-02-13 12:50:26 +0100 | [diff] [blame] | 2249 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
| 2250 | optarg, false); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2251 | if (!sn_opts) { |
| 2252 | error_report("Failed in parsing snapshot param '%s'", |
| 2253 | optarg); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2254 | goto fail_getopt; |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2255 | } |
| 2256 | } else { |
| 2257 | snapshot_name = optarg; |
| 2258 | } |
| 2259 | break; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2260 | case 'S': |
| 2261 | { |
| 2262 | int64_t sval; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 2263 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 2264 | sval = cvtnum("buffer size for sparse output", optarg); |
| 2265 | if (sval < 0) { |
| 2266 | goto fail_getopt; |
| 2267 | } else if (!QEMU_IS_ALIGNED(sval, BDRV_SECTOR_SIZE) || |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 2268 | sval / BDRV_SECTOR_SIZE > MAX_BUF_SECTORS) { |
| 2269 | error_report("Invalid buffer size for sparse output specified. " |
| 2270 | "Valid sizes are multiples of %llu up to %llu. Select " |
| 2271 | "0 to disable sparse detection (fully allocates output).", |
| 2272 | BDRV_SECTOR_SIZE, MAX_BUF_SECTORS * BDRV_SECTOR_SIZE); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2273 | goto fail_getopt; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2274 | } |
| 2275 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2276 | s.min_sparse = sval / BDRV_SECTOR_SIZE; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2277 | explict_min_sparse = true; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2278 | break; |
| 2279 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2280 | case 'p': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2281 | progress = true; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2282 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2283 | case 't': |
| 2284 | cache = optarg; |
| 2285 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 2286 | case 'T': |
| 2287 | src_cache = optarg; |
| 2288 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2289 | case 'q': |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2290 | s.quiet = true; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2291 | break; |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2292 | case 'n': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2293 | skip_create = true; |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2294 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2295 | case 'm': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2296 | if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) || |
| 2297 | s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2298 | error_report("Invalid number of coroutines. Allowed number of" |
| 2299 | " coroutines is between 1 and %d", MAX_COROUTINES); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2300 | goto fail_getopt; |
| 2301 | } |
| 2302 | break; |
| 2303 | case 'W': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2304 | s.wr_in_order = false; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2305 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2306 | case 'U': |
| 2307 | force_share = true; |
| 2308 | break; |
Max Reitz | 3258b91 | 2017-04-26 15:46:47 +0200 | [diff] [blame] | 2309 | case OPTION_OBJECT: { |
| 2310 | QemuOpts *object_opts; |
| 2311 | object_opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 2312 | optarg, true); |
| 2313 | if (!object_opts) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2314 | goto fail_getopt; |
| 2315 | } |
| 2316 | break; |
Max Reitz | 3258b91 | 2017-04-26 15:46:47 +0200 | [diff] [blame] | 2317 | } |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2318 | case OPTION_IMAGE_OPTS: |
| 2319 | image_opts = true; |
| 2320 | break; |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 2321 | case OPTION_SALVAGE: |
| 2322 | s.salvage = true; |
| 2323 | break; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2324 | case OPTION_TARGET_IMAGE_OPTS: |
| 2325 | tgt_image_opts = true; |
| 2326 | break; |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2327 | case OPTION_TARGET_IS_ZERO: |
| 2328 | /* |
| 2329 | * The user asserting that the target is blank has the |
| 2330 | * same effect as the target driver supporting zero |
| 2331 | * initialisation. |
| 2332 | */ |
| 2333 | s.has_zero_init = true; |
| 2334 | break; |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2335 | case OPTION_BITMAPS: |
| 2336 | bitmaps = true; |
| 2337 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2338 | } |
| 2339 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2340 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2341 | if (!out_fmt && !tgt_image_opts) { |
| 2342 | out_fmt = "raw"; |
| 2343 | } |
| 2344 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2345 | if (qemu_opts_foreach(&qemu_object_opts, |
| 2346 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 2347 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2348 | goto fail_getopt; |
| 2349 | } |
| 2350 | |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2351 | if (s.compressed && s.copy_range) { |
| 2352 | error_report("Cannot enable copy offloading when -c is used"); |
| 2353 | goto fail_getopt; |
| 2354 | } |
| 2355 | |
| 2356 | if (explict_min_sparse && s.copy_range) { |
| 2357 | error_report("Cannot enable copy offloading when -S is used"); |
| 2358 | goto fail_getopt; |
| 2359 | } |
| 2360 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 2361 | if (s.copy_range && s.salvage) { |
| 2362 | error_report("Cannot use copy offloading in salvaging mode"); |
| 2363 | goto fail_getopt; |
| 2364 | } |
| 2365 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2366 | if (tgt_image_opts && !skip_create) { |
| 2367 | error_report("--target-image-opts requires use of -n flag"); |
| 2368 | goto fail_getopt; |
| 2369 | } |
| 2370 | |
Kevin Wolf | ffd8e8f | 2019-08-09 11:09:21 +0200 | [diff] [blame] | 2371 | if (skip_create && options) { |
| 2372 | warn_report("-o has no effect when skipping image creation"); |
| 2373 | warn_report("This will become an error in future QEMU versions."); |
| 2374 | } |
| 2375 | |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2376 | if (s.has_zero_init && !skip_create) { |
| 2377 | error_report("--target-is-zero requires use of -n flag"); |
| 2378 | goto fail_getopt; |
| 2379 | } |
| 2380 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2381 | s.src_num = argc - optind - 1; |
| 2382 | out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL; |
| 2383 | |
| 2384 | if (options && has_help_option(options)) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2385 | if (out_fmt) { |
| 2386 | ret = print_block_option_help(out_filename, out_fmt); |
| 2387 | goto fail_getopt; |
| 2388 | } else { |
| 2389 | error_report("Option help requires a format be specified"); |
| 2390 | goto fail_getopt; |
| 2391 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | if (s.src_num < 1) { |
| 2395 | error_report("Must specify image file name"); |
| 2396 | goto fail_getopt; |
| 2397 | } |
| 2398 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2399 | /* ret is still -EINVAL until here */ |
| 2400 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); |
| 2401 | if (ret < 0) { |
| 2402 | error_report("Invalid source cache option: %s", src_cache); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2403 | goto fail_getopt; |
| 2404 | } |
| 2405 | |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2406 | /* Initialize before goto out */ |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2407 | if (s.quiet) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2408 | progress = false; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2409 | } |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2410 | qemu_progress_init(progress, 1.0); |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2411 | qemu_progress_print(0, 100); |
| 2412 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2413 | s.src = g_new0(BlockBackend *, s.src_num); |
| 2414 | s.src_sectors = g_new(int64_t, s.src_num); |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 2415 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2416 | for (bs_i = 0; bs_i < s.src_num; bs_i++) { |
| 2417 | s.src[bs_i] = img_open(image_opts, argv[optind + bs_i], |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2418 | fmt, src_flags, src_writethrough, s.quiet, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2419 | force_share); |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2420 | if (!s.src[bs_i]) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2421 | ret = -1; |
| 2422 | goto out; |
| 2423 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2424 | s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]); |
| 2425 | if (s.src_sectors[bs_i] < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2426 | error_report("Could not get size of %s: %s", |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2427 | argv[optind + bs_i], strerror(-s.src_sectors[bs_i])); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2428 | ret = -1; |
| 2429 | goto out; |
| 2430 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2431 | s.total_sectors += s.src_sectors[bs_i]; |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 2432 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2433 | |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2434 | if (sn_opts) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2435 | bdrv_snapshot_load_tmp(blk_bs(s.src[0]), |
Peter Maydell | 10d6eda | 2017-02-10 16:28:24 +0000 | [diff] [blame] | 2436 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), |
| 2437 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), |
| 2438 | &local_err); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2439 | } else if (snapshot_name != NULL) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2440 | if (s.src_num > 1) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 2441 | error_report("No support for concatenating multiple snapshot"); |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 2442 | ret = -1; |
| 2443 | goto out; |
| 2444 | } |
Wenchao Xia | 7b4c478 | 2013-12-04 17:10:54 +0800 | [diff] [blame] | 2445 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2446 | bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name, |
| 2447 | &local_err); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2448 | } |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2449 | if (local_err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2450 | error_reportf_err(local_err, "Failed to load snapshot: "); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2451 | ret = -1; |
| 2452 | goto out; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 2453 | } |
| 2454 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2455 | if (!skip_create) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2456 | /* Find driver and parse its options */ |
| 2457 | drv = bdrv_find_format(out_fmt); |
| 2458 | if (!drv) { |
| 2459 | error_report("Unknown file format '%s'", out_fmt); |
| 2460 | ret = -1; |
| 2461 | goto out; |
| 2462 | } |
| 2463 | |
| 2464 | proto_drv = bdrv_find_protocol(out_filename, true, &local_err); |
| 2465 | if (!proto_drv) { |
| 2466 | error_report_err(local_err); |
| 2467 | ret = -1; |
| 2468 | goto out; |
| 2469 | } |
| 2470 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2471 | if (!drv->create_opts) { |
| 2472 | error_report("Format driver '%s' does not support image creation", |
| 2473 | drv->format_name); |
| 2474 | ret = -1; |
| 2475 | goto out; |
| 2476 | } |
Max Reitz | f75613c | 2014-12-02 18:32:46 +0100 | [diff] [blame] | 2477 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2478 | if (!proto_drv->create_opts) { |
| 2479 | error_report("Protocol driver '%s' does not support image creation", |
| 2480 | proto_drv->format_name); |
| 2481 | ret = -1; |
| 2482 | goto out; |
| 2483 | } |
Max Reitz | f75613c | 2014-12-02 18:32:46 +0100 | [diff] [blame] | 2484 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2485 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 2486 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
Kevin Wolf | db08adf | 2009-06-04 15:39:38 +0200 | [diff] [blame] | 2487 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2488 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 2489 | if (options) { |
| 2490 | qemu_opts_do_parse(opts, options, NULL, &local_err); |
| 2491 | if (local_err) { |
Markus Armbruster | 97a2ca7 | 2015-03-14 10:23:15 +0100 | [diff] [blame] | 2492 | error_report_err(local_err); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 2493 | ret = -1; |
| 2494 | goto out; |
| 2495 | } |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2496 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2497 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2498 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512, |
Markus Armbruster | 39101f2 | 2015-02-12 16:46:36 +0100 | [diff] [blame] | 2499 | &error_abort); |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2500 | ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL); |
| 2501 | if (ret < 0) { |
| 2502 | goto out; |
| 2503 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2504 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2505 | |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2506 | /* Get backing file name if -o backing_file was used */ |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2507 | out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2508 | if (out_baseimg_param) { |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2509 | out_baseimg = out_baseimg_param; |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2510 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2511 | s.target_has_backing = (bool) out_baseimg; |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2512 | |
David Edmondson | 168468f | 2020-02-05 11:02:48 +0000 | [diff] [blame] | 2513 | if (s.has_zero_init && s.target_has_backing) { |
| 2514 | error_report("Cannot use --target-is-zero when the destination " |
| 2515 | "image has a backing file"); |
| 2516 | goto out; |
| 2517 | } |
| 2518 | |
Max Reitz | 48758a8 | 2017-04-26 15:46:48 +0200 | [diff] [blame] | 2519 | if (s.src_num > 1 && out_baseimg) { |
| 2520 | error_report("Having a backing file for the target makes no sense when " |
| 2521 | "concatenating multiple input images"); |
| 2522 | ret = -1; |
| 2523 | goto out; |
| 2524 | } |
| 2525 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2526 | /* Check if compression is supported */ |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2527 | if (s.compressed) { |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2528 | bool encryption = |
| 2529 | qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false); |
Daniel P. Berrange | 0cb8d47 | 2017-06-23 17:24:06 +0100 | [diff] [blame] | 2530 | const char *encryptfmt = |
| 2531 | qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2532 | const char *preallocation = |
| 2533 | qemu_opt_get(opts, BLOCK_OPT_PREALLOC); |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2534 | |
Vladimir Sementsov-Ogievskiy | ac850bf | 2019-06-04 19:15:06 +0300 | [diff] [blame] | 2535 | if (drv && !block_driver_can_compress(drv)) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2536 | error_report("Compression not supported for this file format"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2537 | ret = -1; |
| 2538 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2539 | } |
| 2540 | |
Daniel P. Berrange | 0cb8d47 | 2017-06-23 17:24:06 +0100 | [diff] [blame] | 2541 | if (encryption || encryptfmt) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2542 | error_report("Compression and encryption not supported at " |
| 2543 | "the same time"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2544 | ret = -1; |
| 2545 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2546 | } |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 2547 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2548 | if (preallocation |
| 2549 | && strcmp(preallocation, "off")) |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 2550 | { |
| 2551 | error_report("Compression and preallocation not supported at " |
| 2552 | "the same time"); |
| 2553 | ret = -1; |
| 2554 | goto out; |
| 2555 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2556 | } |
| 2557 | |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2558 | /* Determine if bitmaps need copying */ |
| 2559 | if (bitmaps) { |
| 2560 | if (s.src_num > 1) { |
| 2561 | error_report("Copying bitmaps only possible with single source"); |
| 2562 | ret = -1; |
| 2563 | goto out; |
| 2564 | } |
| 2565 | if (!bdrv_supports_persistent_dirty_bitmap(blk_bs(s.src[0]))) { |
| 2566 | error_report("Source lacks bitmap support"); |
| 2567 | ret = -1; |
| 2568 | goto out; |
| 2569 | } |
| 2570 | } |
| 2571 | |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2572 | /* |
| 2573 | * The later open call will need any decryption secrets, and |
| 2574 | * bdrv_create() will purge "opts", so extract them now before |
| 2575 | * they are lost. |
| 2576 | */ |
| 2577 | if (!skip_create) { |
| 2578 | open_opts = qdict_new(); |
| 2579 | 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] | 2580 | |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2581 | /* Create the new image */ |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 2582 | ret = bdrv_create(drv, out_filename, opts, &local_err); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2583 | if (ret < 0) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2584 | error_reportf_err(local_err, "%s: error while converting %s: ", |
| 2585 | out_filename, out_fmt); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2586 | goto out; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2587 | } |
| 2588 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2589 | |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 2590 | s.target_is_new = !skip_create; |
| 2591 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2592 | 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] | 2593 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2594 | if (ret < 0) { |
| 2595 | error_report("Invalid cache option: %s", cache); |
Markus Armbruster | bb9cd2e | 2014-05-28 11:17:07 +0200 | [diff] [blame] | 2596 | goto out; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2597 | } |
| 2598 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2599 | if (skip_create) { |
| 2600 | s.target = img_open(tgt_image_opts, out_filename, out_fmt, |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2601 | flags, writethrough, s.quiet, false); |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2602 | } else { |
| 2603 | /* TODO ultimately we should allow --target-image-opts |
| 2604 | * to be used even when -n is not given. |
| 2605 | * That has to wait for bdrv_create to be improved |
| 2606 | * to allow filenames in option syntax |
| 2607 | */ |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2608 | s.target = img_open_file(out_filename, open_opts, out_fmt, |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2609 | flags, writethrough, s.quiet, false); |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2610 | open_opts = NULL; /* blk_new_open will have freed it */ |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2611 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2612 | if (!s.target) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2613 | ret = -1; |
| 2614 | goto out; |
| 2615 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2616 | out_bs = blk_bs(s.target); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2617 | |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2618 | if (bitmaps && !bdrv_supports_persistent_dirty_bitmap(out_bs)) { |
| 2619 | error_report("Format driver '%s' does not support bitmaps", |
| 2620 | out_bs->drv->format_name); |
| 2621 | ret = -1; |
| 2622 | goto out; |
| 2623 | } |
| 2624 | |
Vladimir Sementsov-Ogievskiy | ac850bf | 2019-06-04 19:15:06 +0300 | [diff] [blame] | 2625 | if (s.compressed && !block_driver_can_compress(out_bs->drv)) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2626 | error_report("Compression not supported for this file format"); |
| 2627 | ret = -1; |
| 2628 | goto out; |
| 2629 | } |
| 2630 | |
Eric Blake | 5def6b8 | 2016-06-23 16:37:19 -0600 | [diff] [blame] | 2631 | /* increase bufsectors from the default 4096 (2M) if opt_transfer |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 2632 | * or discard_alignment of the out_bs is greater. Limit to |
| 2633 | * MAX_BUF_SECTORS as maximum which is currently 32768 (16MB). */ |
| 2634 | s.buf_sectors = MIN(MAX_BUF_SECTORS, |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2635 | MAX(s.buf_sectors, |
| 2636 | MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS, |
| 2637 | out_bs->bl.pdiscard_alignment >> |
| 2638 | BDRV_SECTOR_BITS))); |
Peter Lieven | f2521c9 | 2013-11-27 11:07:06 +0100 | [diff] [blame] | 2639 | |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 2640 | /* try to align the write requests to the destination to avoid unnecessary |
| 2641 | * RMW cycles. */ |
| 2642 | s.alignment = MAX(pow2floor(s.min_sparse), |
| 2643 | DIV_ROUND_UP(out_bs->bl.request_alignment, |
| 2644 | BDRV_SECTOR_SIZE)); |
| 2645 | assert(is_power_of_2(s.alignment)); |
| 2646 | |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2647 | if (skip_create) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2648 | int64_t output_sectors = blk_nb_sectors(s.target); |
Markus Armbruster | 43716fa | 2014-06-26 13:23:21 +0200 | [diff] [blame] | 2649 | if (output_sectors < 0) { |
Gonglei | eec5eb4 | 2015-02-25 12:22:27 +0800 | [diff] [blame] | 2650 | error_report("unable to get output image length: %s", |
Markus Armbruster | 43716fa | 2014-06-26 13:23:21 +0200 | [diff] [blame] | 2651 | strerror(-output_sectors)); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2652 | ret = -1; |
| 2653 | goto out; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2654 | } else if (output_sectors < s.total_sectors) { |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2655 | error_report("output file is smaller than input file"); |
| 2656 | ret = -1; |
| 2657 | goto out; |
| 2658 | } |
| 2659 | } |
| 2660 | |
Max Reitz | c69291e | 2020-01-21 16:59:14 +0100 | [diff] [blame] | 2661 | if (s.target_has_backing && s.target_is_new) { |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 2662 | /* Errors are treated as "backing length unknown" (which means |
| 2663 | * s.target_backing_sectors has to be negative, which it will |
| 2664 | * be automatically). The backing file length is used only |
| 2665 | * for optimizations, so such a case is not fatal. */ |
| 2666 | s.target_backing_sectors = bdrv_nb_sectors(out_bs->backing->bs); |
| 2667 | } else { |
| 2668 | s.target_backing_sectors = -1; |
| 2669 | } |
| 2670 | |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2671 | ret = bdrv_get_info(out_bs, &bdi); |
| 2672 | if (ret < 0) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2673 | if (s.compressed) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2674 | error_report("could not get block driver info"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2675 | goto out; |
| 2676 | } |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2677 | } else { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2678 | s.compressed = s.compressed || bdi.needs_compressed_writes; |
| 2679 | s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE; |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2680 | } |
| 2681 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2682 | ret = convert_do_copy(&s); |
Eric Blake | 15e39ad | 2020-05-21 14:21:36 -0500 | [diff] [blame] | 2683 | |
| 2684 | /* Now copy the bitmaps */ |
| 2685 | if (bitmaps && ret == 0) { |
| 2686 | ret = convert_copy_bitmaps(blk_bs(s.src[0]), out_bs); |
| 2687 | } |
| 2688 | |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2689 | out: |
Peter Lieven | 13c28af | 2013-11-27 11:07:01 +0100 | [diff] [blame] | 2690 | if (!ret) { |
| 2691 | qemu_progress_print(100, 0); |
| 2692 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2693 | qemu_progress_end(); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2694 | qemu_opts_del(opts); |
| 2695 | qemu_opts_free(create_opts); |
Markus Armbruster | fbf28a4 | 2014-09-29 16:07:55 +0200 | [diff] [blame] | 2696 | qemu_opts_del(sn_opts); |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2697 | qobject_unref(open_opts); |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2698 | blk_unref(s.target); |
| 2699 | if (s.src) { |
| 2700 | for (bs_i = 0; bs_i < s.src_num; bs_i++) { |
| 2701 | blk_unref(s.src[bs_i]); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2702 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2703 | g_free(s.src); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2704 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2705 | g_free(s.src_sectors); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2706 | fail_getopt: |
| 2707 | g_free(options); |
| 2708 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2709 | return !!ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
bellard | 57d1a2b | 2004-08-03 21:15:11 +0000 | [diff] [blame] | 2712 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2713 | static void dump_snapshots(BlockDriverState *bs) |
| 2714 | { |
| 2715 | QEMUSnapshotInfo *sn_tab, *sn; |
| 2716 | int nb_sns, i; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2717 | |
| 2718 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 2719 | if (nb_sns <= 0) |
| 2720 | return; |
| 2721 | printf("Snapshot list:\n"); |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2722 | bdrv_snapshot_dump(NULL); |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2723 | printf("\n"); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2724 | for(i = 0; i < nb_sns; i++) { |
| 2725 | sn = &sn_tab[i]; |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2726 | bdrv_snapshot_dump(sn); |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2727 | printf("\n"); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2728 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2729 | g_free(sn_tab); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2730 | } |
| 2731 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2732 | static void dump_json_image_info_list(ImageInfoList *list) |
| 2733 | { |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2734 | QString *str; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2735 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 2736 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2737 | |
| 2738 | visit_type_ImageInfoList(v, NULL, &list, &error_abort); |
| 2739 | visit_complete(v, &obj); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2740 | str = qobject_to_json_pretty(obj); |
| 2741 | assert(str != NULL); |
| 2742 | printf("%s\n", qstring_get_str(str)); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2743 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2744 | visit_free(v); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2745 | qobject_unref(str); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2746 | } |
| 2747 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2748 | static void dump_json_image_info(ImageInfo *info) |
| 2749 | { |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2750 | QString *str; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2751 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 2752 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2753 | |
| 2754 | visit_type_ImageInfo(v, NULL, &info, &error_abort); |
| 2755 | visit_complete(v, &obj); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2756 | str = qobject_to_json_pretty(obj); |
| 2757 | assert(str != NULL); |
| 2758 | printf("%s\n", qstring_get_str(str)); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2759 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2760 | visit_free(v); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2761 | qobject_unref(str); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2762 | } |
| 2763 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2764 | static void dump_human_image_info_list(ImageInfoList *list) |
| 2765 | { |
| 2766 | ImageInfoList *elem; |
| 2767 | bool delim = false; |
| 2768 | |
| 2769 | for (elem = list; elem; elem = elem->next) { |
| 2770 | if (delim) { |
| 2771 | printf("\n"); |
| 2772 | } |
| 2773 | delim = true; |
| 2774 | |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2775 | bdrv_image_info_dump(elem->value); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2776 | } |
| 2777 | } |
| 2778 | |
| 2779 | static gboolean str_equal_func(gconstpointer a, gconstpointer b) |
| 2780 | { |
| 2781 | return strcmp(a, b) == 0; |
| 2782 | } |
| 2783 | |
| 2784 | /** |
| 2785 | * Open an image file chain and return an ImageInfoList |
| 2786 | * |
| 2787 | * @filename: topmost image filename |
| 2788 | * @fmt: topmost image format (may be NULL to autodetect) |
| 2789 | * @chain: true - enumerate entire backing file chain |
| 2790 | * false - only topmost image file |
| 2791 | * |
| 2792 | * Returns a list of ImageInfo objects or NULL if there was an error opening an |
| 2793 | * image file. If there was an error a message will have been printed to |
| 2794 | * stderr. |
| 2795 | */ |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2796 | static ImageInfoList *collect_image_info_list(bool image_opts, |
| 2797 | const char *filename, |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2798 | const char *fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2799 | bool chain, bool force_share) |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2800 | { |
| 2801 | ImageInfoList *head = NULL; |
| 2802 | ImageInfoList **last = &head; |
| 2803 | GHashTable *filenames; |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2804 | Error *err = NULL; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2805 | |
| 2806 | filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL); |
| 2807 | |
| 2808 | while (filename) { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2809 | BlockBackend *blk; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2810 | BlockDriverState *bs; |
| 2811 | ImageInfo *info; |
| 2812 | ImageInfoList *elem; |
| 2813 | |
| 2814 | if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) { |
| 2815 | error_report("Backing file '%s' creates an infinite loop.", |
| 2816 | filename); |
| 2817 | goto err; |
| 2818 | } |
| 2819 | g_hash_table_insert(filenames, (gpointer)filename, NULL); |
| 2820 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 2821 | blk = img_open(image_opts, filename, fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2822 | BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false, |
| 2823 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2824 | if (!blk) { |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2825 | goto err; |
| 2826 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2827 | bs = blk_bs(blk); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2828 | |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2829 | bdrv_query_image_info(bs, &info, &err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2830 | if (err) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 2831 | error_report_err(err); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2832 | blk_unref(blk); |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2833 | goto err; |
Wenchao Xia | fb0ed45 | 2013-06-06 12:27:57 +0800 | [diff] [blame] | 2834 | } |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2835 | |
| 2836 | elem = g_new0(ImageInfoList, 1); |
| 2837 | elem->value = info; |
| 2838 | *last = elem; |
| 2839 | last = &elem->next; |
| 2840 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2841 | blk_unref(blk); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2842 | |
Stefan Hajnoczi | 0da7d13 | 2019-12-05 13:46:46 +0000 | [diff] [blame] | 2843 | /* Clear parameters that only apply to the topmost image */ |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2844 | filename = fmt = NULL; |
Stefan Hajnoczi | 0da7d13 | 2019-12-05 13:46:46 +0000 | [diff] [blame] | 2845 | image_opts = false; |
| 2846 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2847 | if (chain) { |
| 2848 | if (info->has_full_backing_filename) { |
| 2849 | filename = info->full_backing_filename; |
| 2850 | } else if (info->has_backing_filename) { |
John Snow | 92d617a | 2015-12-14 14:55:15 -0500 | [diff] [blame] | 2851 | error_report("Could not determine absolute backing filename," |
| 2852 | " but backing filename '%s' present", |
| 2853 | info->backing_filename); |
| 2854 | goto err; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2855 | } |
| 2856 | if (info->has_backing_filename_format) { |
| 2857 | fmt = info->backing_filename_format; |
| 2858 | } |
| 2859 | } |
| 2860 | } |
| 2861 | g_hash_table_destroy(filenames); |
| 2862 | return head; |
| 2863 | |
| 2864 | err: |
| 2865 | qapi_free_ImageInfoList(head); |
| 2866 | g_hash_table_destroy(filenames); |
| 2867 | return NULL; |
| 2868 | } |
| 2869 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2870 | static int img_info(int argc, char **argv) |
| 2871 | { |
| 2872 | int c; |
| 2873 | OutputFormat output_format = OFORMAT_HUMAN; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2874 | bool chain = false; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2875 | const char *filename, *fmt, *output; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2876 | ImageInfoList *list; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2877 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2878 | bool force_share = false; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2879 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2880 | fmt = NULL; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2881 | output = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2882 | for(;;) { |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2883 | int option_index = 0; |
| 2884 | static const struct option long_options[] = { |
| 2885 | {"help", no_argument, 0, 'h'}, |
| 2886 | {"format", required_argument, 0, 'f'}, |
| 2887 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2888 | {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2889 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2890 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2891 | {"force-share", no_argument, 0, 'U'}, |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2892 | {0, 0, 0, 0} |
| 2893 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2894 | c = getopt_long(argc, argv, ":f:hU", |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2895 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2896 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2897 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2898 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2899 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2900 | case ':': |
| 2901 | missing_argument(argv[optind - 1]); |
| 2902 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2903 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2904 | unrecognized_option(argv[optind - 1]); |
| 2905 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2906 | case 'h': |
| 2907 | help(); |
| 2908 | break; |
| 2909 | case 'f': |
| 2910 | fmt = optarg; |
| 2911 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2912 | case 'U': |
| 2913 | force_share = true; |
| 2914 | break; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2915 | case OPTION_OUTPUT: |
| 2916 | output = optarg; |
| 2917 | break; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2918 | case OPTION_BACKING_CHAIN: |
| 2919 | chain = true; |
| 2920 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2921 | case OPTION_OBJECT: { |
| 2922 | QemuOpts *opts; |
| 2923 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 2924 | optarg, true); |
| 2925 | if (!opts) { |
| 2926 | return 1; |
| 2927 | } |
| 2928 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2929 | case OPTION_IMAGE_OPTS: |
| 2930 | image_opts = true; |
| 2931 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2932 | } |
| 2933 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 2934 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2935 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2936 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2937 | filename = argv[optind++]; |
| 2938 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2939 | if (output && !strcmp(output, "json")) { |
| 2940 | output_format = OFORMAT_JSON; |
| 2941 | } else if (output && !strcmp(output, "human")) { |
| 2942 | output_format = OFORMAT_HUMAN; |
| 2943 | } else if (output) { |
| 2944 | error_report("--output must be used with human or json as argument."); |
| 2945 | return 1; |
| 2946 | } |
| 2947 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2948 | if (qemu_opts_foreach(&qemu_object_opts, |
| 2949 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 2950 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2951 | return 1; |
| 2952 | } |
| 2953 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2954 | list = collect_image_info_list(image_opts, filename, fmt, chain, |
| 2955 | force_share); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2956 | if (!list) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2957 | return 1; |
| 2958 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2959 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2960 | switch (output_format) { |
| 2961 | case OFORMAT_HUMAN: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2962 | dump_human_image_info_list(list); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2963 | break; |
| 2964 | case OFORMAT_JSON: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2965 | if (chain) { |
| 2966 | dump_json_image_info_list(list); |
| 2967 | } else { |
| 2968 | dump_json_image_info(list->value); |
| 2969 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2970 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2971 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2972 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2973 | qapi_free_ImageInfoList(list); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2974 | return 0; |
| 2975 | } |
| 2976 | |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 2977 | static int dump_map_entry(OutputFormat output_format, MapEntry *e, |
| 2978 | MapEntry *next) |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2979 | { |
| 2980 | switch (output_format) { |
| 2981 | case OFORMAT_HUMAN: |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2982 | if (e->data && !e->has_offset) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2983 | error_report("File contains external, encrypted or compressed clusters."); |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 2984 | return -1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2985 | } |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2986 | if (e->data && !e->zero) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2987 | printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n", |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2988 | e->start, e->length, |
| 2989 | e->has_offset ? e->offset : 0, |
| 2990 | e->has_filename ? e->filename : ""); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2991 | } |
| 2992 | /* This format ignores the distinction between 0, ZERO and ZERO|DATA. |
| 2993 | * Modify the flags here to allow more coalescing. |
| 2994 | */ |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2995 | if (next && (!next->data || next->zero)) { |
| 2996 | next->data = false; |
| 2997 | next->zero = true; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2998 | } |
| 2999 | break; |
| 3000 | case OFORMAT_JSON: |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 3001 | printf("{ \"start\": %"PRId64", \"length\": %"PRId64"," |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3002 | " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s", |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3003 | e->start, e->length, e->depth, |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3004 | e->zero ? "true" : "false", |
| 3005 | e->data ? "true" : "false"); |
| 3006 | if (e->has_offset) { |
Paolo Bonzini | c745bfb | 2013-09-11 18:47:52 +0200 | [diff] [blame] | 3007 | printf(", \"offset\": %"PRId64"", e->offset); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3008 | } |
| 3009 | putchar('}'); |
| 3010 | |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 3011 | if (next) { |
| 3012 | puts(","); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3013 | } |
| 3014 | break; |
| 3015 | } |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3016 | return 0; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3017 | } |
| 3018 | |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3019 | static int get_block_status(BlockDriverState *bs, int64_t offset, |
| 3020 | int64_t bytes, MapEntry *e) |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3021 | { |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3022 | int ret; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3023 | int depth; |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 3024 | BlockDriverState *file; |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3025 | bool has_offset; |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3026 | int64_t map; |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3027 | char *filename = NULL; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3028 | |
| 3029 | /* As an optimization, we could cache the current range of unallocated |
| 3030 | * clusters in each file of the chain, and avoid querying the same |
| 3031 | * range repeatedly. |
| 3032 | */ |
| 3033 | |
| 3034 | depth = 0; |
| 3035 | for (;;) { |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3036 | ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3037 | if (ret < 0) { |
| 3038 | return ret; |
| 3039 | } |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3040 | assert(bytes); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3041 | if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) { |
| 3042 | break; |
| 3043 | } |
Kevin Wolf | 760e006 | 2015-06-17 14:55:21 +0200 | [diff] [blame] | 3044 | bs = backing_bs(bs); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3045 | if (bs == NULL) { |
| 3046 | ret = 0; |
| 3047 | break; |
| 3048 | } |
| 3049 | |
| 3050 | depth++; |
| 3051 | } |
| 3052 | |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3053 | has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID); |
| 3054 | |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3055 | if (file && has_offset) { |
| 3056 | bdrv_refresh_filename(file); |
| 3057 | filename = file->filename; |
| 3058 | } |
| 3059 | |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3060 | *e = (MapEntry) { |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3061 | .start = offset, |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3062 | .length = bytes, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3063 | .data = !!(ret & BDRV_BLOCK_DATA), |
| 3064 | .zero = !!(ret & BDRV_BLOCK_ZERO), |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 3065 | .offset = map, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3066 | .has_offset = has_offset, |
| 3067 | .depth = depth, |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3068 | .has_filename = filename, |
| 3069 | .filename = filename, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 3070 | }; |
| 3071 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3072 | return 0; |
| 3073 | } |
| 3074 | |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3075 | static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next) |
| 3076 | { |
| 3077 | if (curr->length == 0) { |
| 3078 | return false; |
| 3079 | } |
| 3080 | if (curr->zero != next->zero || |
| 3081 | curr->data != next->data || |
| 3082 | curr->depth != next->depth || |
| 3083 | curr->has_filename != next->has_filename || |
| 3084 | curr->has_offset != next->has_offset) { |
| 3085 | return false; |
| 3086 | } |
| 3087 | if (curr->has_filename && strcmp(curr->filename, next->filename)) { |
| 3088 | return false; |
| 3089 | } |
| 3090 | if (curr->has_offset && curr->offset + curr->length != next->offset) { |
| 3091 | return false; |
| 3092 | } |
| 3093 | return true; |
| 3094 | } |
| 3095 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3096 | static int img_map(int argc, char **argv) |
| 3097 | { |
| 3098 | int c; |
| 3099 | OutputFormat output_format = OFORMAT_HUMAN; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3100 | BlockBackend *blk; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3101 | BlockDriverState *bs; |
| 3102 | const char *filename, *fmt, *output; |
| 3103 | int64_t length; |
| 3104 | MapEntry curr = { .length = 0 }, next; |
| 3105 | int ret = 0; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3106 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3107 | bool force_share = false; |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3108 | int64_t start_offset = 0; |
| 3109 | int64_t max_length = -1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3110 | |
| 3111 | fmt = NULL; |
| 3112 | output = NULL; |
| 3113 | for (;;) { |
| 3114 | int option_index = 0; |
| 3115 | static const struct option long_options[] = { |
| 3116 | {"help", no_argument, 0, 'h'}, |
| 3117 | {"format", required_argument, 0, 'f'}, |
| 3118 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3119 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3120 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3121 | {"force-share", no_argument, 0, 'U'}, |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3122 | {"start-offset", required_argument, 0, 's'}, |
| 3123 | {"max-length", required_argument, 0, 'l'}, |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3124 | {0, 0, 0, 0} |
| 3125 | }; |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3126 | c = getopt_long(argc, argv, ":f:s:l:hU", |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3127 | long_options, &option_index); |
| 3128 | if (c == -1) { |
| 3129 | break; |
| 3130 | } |
| 3131 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3132 | case ':': |
| 3133 | missing_argument(argv[optind - 1]); |
| 3134 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3135 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3136 | unrecognized_option(argv[optind - 1]); |
| 3137 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3138 | case 'h': |
| 3139 | help(); |
| 3140 | break; |
| 3141 | case 'f': |
| 3142 | fmt = optarg; |
| 3143 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3144 | case 'U': |
| 3145 | force_share = true; |
| 3146 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3147 | case OPTION_OUTPUT: |
| 3148 | output = optarg; |
| 3149 | break; |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3150 | case 's': |
| 3151 | start_offset = cvtnum("start offset", optarg); |
| 3152 | if (start_offset < 0) { |
| 3153 | return 1; |
| 3154 | } |
| 3155 | break; |
| 3156 | case 'l': |
| 3157 | max_length = cvtnum("max length", optarg); |
| 3158 | if (max_length < 0) { |
| 3159 | return 1; |
| 3160 | } |
| 3161 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3162 | case OPTION_OBJECT: { |
| 3163 | QemuOpts *opts; |
| 3164 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 3165 | optarg, true); |
| 3166 | if (!opts) { |
| 3167 | return 1; |
| 3168 | } |
| 3169 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3170 | case OPTION_IMAGE_OPTS: |
| 3171 | image_opts = true; |
| 3172 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3173 | } |
| 3174 | } |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3175 | if (optind != argc - 1) { |
| 3176 | error_exit("Expecting one image file name"); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3177 | } |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3178 | filename = argv[optind]; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3179 | |
| 3180 | if (output && !strcmp(output, "json")) { |
| 3181 | output_format = OFORMAT_JSON; |
| 3182 | } else if (output && !strcmp(output, "human")) { |
| 3183 | output_format = OFORMAT_HUMAN; |
| 3184 | } else if (output) { |
| 3185 | error_report("--output must be used with human or json as argument."); |
| 3186 | return 1; |
| 3187 | } |
| 3188 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3189 | if (qemu_opts_foreach(&qemu_object_opts, |
| 3190 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 3191 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3192 | return 1; |
| 3193 | } |
| 3194 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3195 | blk = img_open(image_opts, filename, fmt, 0, false, false, force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3196 | if (!blk) { |
| 3197 | return 1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3198 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3199 | bs = blk_bs(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3200 | |
| 3201 | if (output_format == OFORMAT_HUMAN) { |
| 3202 | printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File"); |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 3203 | } else if (output_format == OFORMAT_JSON) { |
| 3204 | putchar('['); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3205 | } |
| 3206 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3207 | length = blk_getlength(blk); |
Eyal Moscovici | 8f282e8 | 2020-05-13 16:36:27 +0300 | [diff] [blame] | 3208 | if (length < 0) { |
| 3209 | error_report("Failed to get size for '%s'", filename); |
| 3210 | return 1; |
| 3211 | } |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3212 | if (max_length != -1) { |
| 3213 | length = MIN(start_offset + max_length, length); |
| 3214 | } |
Eyal Moscovici | 8f282e8 | 2020-05-13 16:36:27 +0300 | [diff] [blame] | 3215 | |
Eyal Moscovici | c046949 | 2020-05-13 16:36:29 +0300 | [diff] [blame] | 3216 | curr.start = start_offset; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3217 | while (curr.start + curr.length < length) { |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3218 | int64_t offset = curr.start + curr.length; |
| 3219 | int64_t n; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3220 | |
| 3221 | /* Probe up to 1 GiB at a time. */ |
Stefano Garzarella | 97ede57 | 2019-05-08 12:43:24 +0200 | [diff] [blame] | 3222 | n = MIN(1 * GiB, length - offset); |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3223 | ret = get_block_status(bs, offset, n, &next); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3224 | |
| 3225 | if (ret < 0) { |
| 3226 | error_report("Could not read file metadata: %s", strerror(-ret)); |
| 3227 | goto out; |
| 3228 | } |
| 3229 | |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3230 | if (entry_mergeable(&curr, &next)) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3231 | curr.length += next.length; |
| 3232 | continue; |
| 3233 | } |
| 3234 | |
| 3235 | if (curr.length > 0) { |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3236 | ret = dump_map_entry(output_format, &curr, &next); |
| 3237 | if (ret < 0) { |
| 3238 | goto out; |
| 3239 | } |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3240 | } |
| 3241 | curr = next; |
| 3242 | } |
| 3243 | |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3244 | ret = dump_map_entry(output_format, &curr, NULL); |
Eyal Moscovici | e46c0b1 | 2020-05-13 16:36:28 +0300 | [diff] [blame] | 3245 | if (output_format == OFORMAT_JSON) { |
| 3246 | puts("]"); |
| 3247 | } |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3248 | |
| 3249 | out: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3250 | blk_unref(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3251 | return ret < 0; |
| 3252 | } |
| 3253 | |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3254 | #define SNAPSHOT_LIST 1 |
| 3255 | #define SNAPSHOT_CREATE 2 |
| 3256 | #define SNAPSHOT_APPLY 3 |
| 3257 | #define SNAPSHOT_DELETE 4 |
| 3258 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3259 | static int img_snapshot(int argc, char **argv) |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3260 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3261 | BlockBackend *blk; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3262 | BlockDriverState *bs; |
| 3263 | QEMUSnapshotInfo sn; |
| 3264 | char *filename, *snapshot_name = NULL; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3265 | int c, ret = 0, bdrv_oflags; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3266 | int action = 0; |
| 3267 | qemu_timeval tv; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3268 | bool quiet = false; |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 3269 | Error *err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3270 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3271 | bool force_share = false; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3272 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3273 | bdrv_oflags = BDRV_O_RDWR; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3274 | /* Parse commandline parameters */ |
| 3275 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3276 | static const struct option long_options[] = { |
| 3277 | {"help", no_argument, 0, 'h'}, |
| 3278 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3279 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3280 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3281 | {0, 0, 0, 0} |
| 3282 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3283 | c = getopt_long(argc, argv, ":la:c:d:hqU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3284 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3285 | if (c == -1) { |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3286 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3287 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3288 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3289 | case ':': |
| 3290 | missing_argument(argv[optind - 1]); |
| 3291 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3292 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3293 | unrecognized_option(argv[optind - 1]); |
| 3294 | break; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3295 | case 'h': |
| 3296 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3297 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3298 | case 'l': |
| 3299 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3300 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3301 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3302 | } |
| 3303 | action = SNAPSHOT_LIST; |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 3304 | bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */ |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3305 | break; |
| 3306 | case 'a': |
| 3307 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3308 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3309 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3310 | } |
| 3311 | action = SNAPSHOT_APPLY; |
| 3312 | snapshot_name = optarg; |
| 3313 | break; |
| 3314 | case 'c': |
| 3315 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3316 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3317 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3318 | } |
| 3319 | action = SNAPSHOT_CREATE; |
| 3320 | snapshot_name = optarg; |
| 3321 | break; |
| 3322 | case 'd': |
| 3323 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3324 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3325 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3326 | } |
| 3327 | action = SNAPSHOT_DELETE; |
| 3328 | snapshot_name = optarg; |
| 3329 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3330 | case 'q': |
| 3331 | quiet = true; |
| 3332 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3333 | case 'U': |
| 3334 | force_share = true; |
| 3335 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3336 | case OPTION_OBJECT: { |
| 3337 | QemuOpts *opts; |
| 3338 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 3339 | optarg, true); |
| 3340 | if (!opts) { |
| 3341 | return 1; |
| 3342 | } |
| 3343 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3344 | case OPTION_IMAGE_OPTS: |
| 3345 | image_opts = true; |
| 3346 | break; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3347 | } |
| 3348 | } |
| 3349 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 3350 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3351 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3352 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3353 | filename = argv[optind++]; |
| 3354 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3355 | if (qemu_opts_foreach(&qemu_object_opts, |
| 3356 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 3357 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3358 | return 1; |
| 3359 | } |
| 3360 | |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3361 | /* Open the image */ |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3362 | blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet, |
| 3363 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3364 | if (!blk) { |
| 3365 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3366 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3367 | bs = blk_bs(blk); |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3368 | |
| 3369 | /* Perform the requested action */ |
| 3370 | switch(action) { |
| 3371 | case SNAPSHOT_LIST: |
| 3372 | dump_snapshots(bs); |
| 3373 | break; |
| 3374 | |
| 3375 | case SNAPSHOT_CREATE: |
| 3376 | memset(&sn, 0, sizeof(sn)); |
| 3377 | pstrcpy(sn.name, sizeof(sn.name), snapshot_name); |
| 3378 | |
| 3379 | qemu_gettimeofday(&tv); |
| 3380 | sn.date_sec = tv.tv_sec; |
| 3381 | sn.date_nsec = tv.tv_usec * 1000; |
| 3382 | |
| 3383 | ret = bdrv_snapshot_create(bs, &sn); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3384 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3385 | error_report("Could not create snapshot '%s': %d (%s)", |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3386 | snapshot_name, ret, strerror(-ret)); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3387 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3388 | break; |
| 3389 | |
| 3390 | case SNAPSHOT_APPLY: |
Kevin Wolf | 0b62bcb | 2017-11-20 15:28:41 +0100 | [diff] [blame] | 3391 | ret = bdrv_snapshot_goto(bs, snapshot_name, &err); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3392 | if (ret) { |
Kevin Wolf | 0b62bcb | 2017-11-20 15:28:41 +0100 | [diff] [blame] | 3393 | error_reportf_err(err, "Could not apply snapshot '%s': ", |
| 3394 | snapshot_name); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3395 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3396 | break; |
| 3397 | |
| 3398 | case SNAPSHOT_DELETE: |
Daniel Henrique Barboza | 8c04093 | 2018-11-07 11:09:59 -0200 | [diff] [blame] | 3399 | ret = bdrv_snapshot_find(bs, &sn, snapshot_name); |
| 3400 | if (ret < 0) { |
| 3401 | error_report("Could not delete snapshot '%s': snapshot not " |
| 3402 | "found", snapshot_name); |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 3403 | ret = 1; |
Daniel Henrique Barboza | 8c04093 | 2018-11-07 11:09:59 -0200 | [diff] [blame] | 3404 | } else { |
| 3405 | ret = bdrv_snapshot_delete(bs, sn.id_str, sn.name, &err); |
| 3406 | if (ret < 0) { |
| 3407 | error_reportf_err(err, "Could not delete snapshot '%s': ", |
| 3408 | snapshot_name); |
| 3409 | ret = 1; |
| 3410 | } |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3411 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3412 | break; |
| 3413 | } |
| 3414 | |
| 3415 | /* Cleanup */ |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3416 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3417 | if (ret) { |
| 3418 | return 1; |
| 3419 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3420 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3421 | } |
| 3422 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3423 | static int img_rebase(int argc, char **argv) |
| 3424 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3425 | BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL; |
Paolo Bonzini | 396374c | 2016-02-25 23:53:54 +0100 | [diff] [blame] | 3426 | uint8_t *buf_old = NULL; |
| 3427 | uint8_t *buf_new = NULL; |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3428 | BlockDriverState *bs = NULL, *prefix_chain_bs = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3429 | char *filename; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3430 | const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg; |
| 3431 | int c, flags, src_flags, ret; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3432 | bool writethrough, src_writethrough; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3433 | int unsafe = 0; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3434 | bool force_share = false; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3435 | int progress = 0; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3436 | bool quiet = false; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 3437 | Error *local_err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3438 | bool image_opts = false; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3439 | |
| 3440 | /* Parse commandline parameters */ |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 3441 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3442 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3443 | src_cache = BDRV_DEFAULT_CACHE; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3444 | out_baseimg = NULL; |
| 3445 | out_basefmt = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3446 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3447 | static const struct option long_options[] = { |
| 3448 | {"help", no_argument, 0, 'h'}, |
| 3449 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3450 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3451 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3452 | {0, 0, 0, 0} |
| 3453 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3454 | c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3455 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3456 | if (c == -1) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3457 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3458 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3459 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3460 | case ':': |
| 3461 | missing_argument(argv[optind - 1]); |
| 3462 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3463 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3464 | unrecognized_option(argv[optind - 1]); |
| 3465 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3466 | case 'h': |
| 3467 | help(); |
| 3468 | return 0; |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 3469 | case 'f': |
| 3470 | fmt = optarg; |
| 3471 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3472 | case 'F': |
| 3473 | out_basefmt = optarg; |
| 3474 | break; |
| 3475 | case 'b': |
| 3476 | out_baseimg = optarg; |
| 3477 | break; |
| 3478 | case 'u': |
| 3479 | unsafe = 1; |
| 3480 | break; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3481 | case 'p': |
| 3482 | progress = 1; |
| 3483 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3484 | case 't': |
| 3485 | cache = optarg; |
| 3486 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3487 | case 'T': |
| 3488 | src_cache = optarg; |
| 3489 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3490 | case 'q': |
| 3491 | quiet = true; |
| 3492 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3493 | case OPTION_OBJECT: { |
| 3494 | QemuOpts *opts; |
| 3495 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 3496 | optarg, true); |
| 3497 | if (!opts) { |
| 3498 | return 1; |
| 3499 | } |
| 3500 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3501 | case OPTION_IMAGE_OPTS: |
| 3502 | image_opts = true; |
| 3503 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3504 | case 'U': |
| 3505 | force_share = true; |
| 3506 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3507 | } |
| 3508 | } |
| 3509 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3510 | if (quiet) { |
| 3511 | progress = 0; |
| 3512 | } |
| 3513 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3514 | if (optind != argc - 1) { |
| 3515 | error_exit("Expecting one image file name"); |
| 3516 | } |
| 3517 | if (!unsafe && !out_baseimg) { |
| 3518 | error_exit("Must specify backing file (-b) or use unsafe mode (-u)"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3519 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3520 | filename = argv[optind++]; |
| 3521 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3522 | if (qemu_opts_foreach(&qemu_object_opts, |
| 3523 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 3524 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3525 | return 1; |
| 3526 | } |
| 3527 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3528 | qemu_progress_init(progress, 2.0); |
| 3529 | qemu_progress_print(0, 100); |
| 3530 | |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3531 | flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0); |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3532 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3533 | if (ret < 0) { |
| 3534 | error_report("Invalid cache option: %s", cache); |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3535 | goto out; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3536 | } |
| 3537 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3538 | src_flags = 0; |
| 3539 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3540 | if (ret < 0) { |
| 3541 | error_report("Invalid source cache option: %s", src_cache); |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3542 | goto out; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3543 | } |
| 3544 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3545 | /* The source files are opened read-only, don't care about WCE */ |
| 3546 | assert((src_flags & BDRV_O_RDWR) == 0); |
| 3547 | (void) src_writethrough; |
| 3548 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3549 | /* |
| 3550 | * Open the images. |
| 3551 | * |
| 3552 | * Ignore the old backing file for unsafe rebase in case we want to correct |
| 3553 | * the reference to a renamed or moved backing file. |
| 3554 | */ |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3555 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 3556 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3557 | if (!blk) { |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3558 | ret = -1; |
| 3559 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3560 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3561 | bs = blk_bs(blk); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3562 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3563 | if (out_basefmt != NULL) { |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3564 | if (bdrv_find_format(out_basefmt) == NULL) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3565 | error_report("Invalid format name: '%s'", out_basefmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3566 | ret = -1; |
| 3567 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3568 | } |
| 3569 | } |
| 3570 | |
| 3571 | /* For safe rebasing we need to compare old and new backing file */ |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3572 | if (!unsafe) { |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3573 | QDict *options = NULL; |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3574 | BlockDriverState *base_bs = backing_bs(bs); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3575 | |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3576 | if (base_bs) { |
Kevin Wolf | d861ab3 | 2019-04-25 14:25:10 +0200 | [diff] [blame] | 3577 | blk_old_backing = blk_new(qemu_get_aio_context(), |
| 3578 | BLK_PERM_CONSISTENT_READ, |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3579 | BLK_PERM_ALL); |
| 3580 | ret = blk_insert_bs(blk_old_backing, base_bs, |
| 3581 | &local_err); |
| 3582 | if (ret < 0) { |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3583 | error_reportf_err(local_err, |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3584 | "Could not reuse old backing file '%s': ", |
| 3585 | base_bs->filename); |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3586 | goto out; |
| 3587 | } |
| 3588 | } else { |
| 3589 | blk_old_backing = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3590 | } |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3591 | |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3592 | if (out_baseimg[0]) { |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3593 | const char *overlay_filename; |
| 3594 | char *out_real_path; |
| 3595 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3596 | options = qdict_new(); |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3597 | if (out_basefmt) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 3598 | qdict_put_str(options, "driver", out_basefmt); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3599 | } |
| 3600 | if (force_share) { |
| 3601 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3602 | } |
| 3603 | |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3604 | bdrv_refresh_filename(bs); |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3605 | overlay_filename = bs->exact_filename[0] ? bs->exact_filename |
| 3606 | : bs->filename; |
Max Reitz | 645ae7d | 2019-02-01 20:29:14 +0100 | [diff] [blame] | 3607 | out_real_path = |
| 3608 | bdrv_get_full_backing_filename_from_filename(overlay_filename, |
| 3609 | out_baseimg, |
| 3610 | &local_err); |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3611 | if (local_err) { |
Max Reitz | f22356d | 2019-05-28 21:53:38 +0200 | [diff] [blame] | 3612 | qobject_unref(options); |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3613 | error_reportf_err(local_err, |
| 3614 | "Could not resolve backing filename: "); |
| 3615 | ret = -1; |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3616 | goto out; |
| 3617 | } |
| 3618 | |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3619 | /* |
| 3620 | * Find out whether we rebase an image on top of a previous image |
| 3621 | * in its chain. |
| 3622 | */ |
| 3623 | prefix_chain_bs = bdrv_find_backing_image(bs, out_real_path); |
Sam Eiderman | 330c729 | 2019-05-23 19:33:37 +0300 | [diff] [blame] | 3624 | if (prefix_chain_bs) { |
Max Reitz | f22356d | 2019-05-28 21:53:38 +0200 | [diff] [blame] | 3625 | qobject_unref(options); |
Sam Eiderman | 330c729 | 2019-05-23 19:33:37 +0300 | [diff] [blame] | 3626 | g_free(out_real_path); |
Max Reitz | f22356d | 2019-05-28 21:53:38 +0200 | [diff] [blame] | 3627 | |
Kevin Wolf | d861ab3 | 2019-04-25 14:25:10 +0200 | [diff] [blame] | 3628 | blk_new_backing = blk_new(qemu_get_aio_context(), |
| 3629 | BLK_PERM_CONSISTENT_READ, |
Sam Eiderman | 330c729 | 2019-05-23 19:33:37 +0300 | [diff] [blame] | 3630 | BLK_PERM_ALL); |
| 3631 | ret = blk_insert_bs(blk_new_backing, prefix_chain_bs, |
| 3632 | &local_err); |
| 3633 | if (ret < 0) { |
| 3634 | error_reportf_err(local_err, |
| 3635 | "Could not reuse backing file '%s': ", |
| 3636 | out_baseimg); |
| 3637 | goto out; |
| 3638 | } |
| 3639 | } else { |
| 3640 | blk_new_backing = blk_new_open(out_real_path, NULL, |
| 3641 | options, src_flags, &local_err); |
| 3642 | g_free(out_real_path); |
| 3643 | if (!blk_new_backing) { |
| 3644 | error_reportf_err(local_err, |
| 3645 | "Could not open new backing file '%s': ", |
| 3646 | out_baseimg); |
| 3647 | ret = -1; |
| 3648 | goto out; |
| 3649 | } |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3650 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3651 | } |
| 3652 | } |
| 3653 | |
| 3654 | /* |
| 3655 | * Check each unallocated cluster in the COW file. If it is unallocated, |
| 3656 | * accesses go to the backing file. We must therefore compare this cluster |
| 3657 | * in the old and new backing file, and if they differ we need to copy it |
| 3658 | * from the old backing file into the COW file. |
| 3659 | * |
| 3660 | * If qemu-img crashes during this step, no harm is done. The content of |
| 3661 | * the image is the same as the original one at any time. |
| 3662 | */ |
| 3663 | if (!unsafe) { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3664 | int64_t size; |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3665 | int64_t old_backing_size = 0; |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3666 | int64_t new_backing_size = 0; |
| 3667 | uint64_t offset; |
| 3668 | int64_t n; |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 3669 | float local_progress = 0; |
TeLeMan | d6771bf | 2010-02-08 16:20:00 +0800 | [diff] [blame] | 3670 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3671 | buf_old = blk_blockalign(blk, IO_BUF_SIZE); |
| 3672 | buf_new = blk_blockalign(blk, IO_BUF_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3673 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3674 | size = blk_getlength(blk); |
| 3675 | if (size < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3676 | error_report("Could not get size of '%s': %s", |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3677 | filename, strerror(-size)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3678 | ret = -1; |
| 3679 | goto out; |
| 3680 | } |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3681 | if (blk_old_backing) { |
| 3682 | old_backing_size = blk_getlength(blk_old_backing); |
| 3683 | if (old_backing_size < 0) { |
| 3684 | char backing_name[PATH_MAX]; |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3685 | |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3686 | bdrv_get_backing_filename(bs, backing_name, |
| 3687 | sizeof(backing_name)); |
| 3688 | error_report("Could not get size of '%s': %s", |
| 3689 | backing_name, strerror(-old_backing_size)); |
| 3690 | ret = -1; |
| 3691 | goto out; |
| 3692 | } |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3693 | } |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3694 | if (blk_new_backing) { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3695 | new_backing_size = blk_getlength(blk_new_backing); |
| 3696 | if (new_backing_size < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3697 | error_report("Could not get size of '%s': %s", |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3698 | out_baseimg, strerror(-new_backing_size)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3699 | ret = -1; |
| 3700 | goto out; |
| 3701 | } |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3702 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3703 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3704 | if (size != 0) { |
| 3705 | local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE)); |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 3706 | } |
| 3707 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3708 | for (offset = 0; offset < size; offset += n) { |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3709 | bool buf_old_is_zero = false; |
| 3710 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3711 | /* How many bytes can we handle with the next read? */ |
| 3712 | n = MIN(IO_BUF_SIZE, size - offset); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3713 | |
| 3714 | /* If the cluster is allocated, we don't need to take action */ |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3715 | ret = bdrv_is_allocated(bs, offset, n, &n); |
Paolo Bonzini | d663640 | 2013-09-04 19:00:25 +0200 | [diff] [blame] | 3716 | if (ret < 0) { |
| 3717 | error_report("error while reading image metadata: %s", |
| 3718 | strerror(-ret)); |
| 3719 | goto out; |
| 3720 | } |
Kevin Wolf | cc60e32 | 2010-04-29 14:47:48 +0200 | [diff] [blame] | 3721 | if (ret) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3722 | continue; |
| 3723 | } |
| 3724 | |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3725 | if (prefix_chain_bs) { |
| 3726 | /* |
| 3727 | * If cluster wasn't changed since prefix_chain, we don't need |
| 3728 | * to take action |
| 3729 | */ |
| 3730 | ret = bdrv_is_allocated_above(backing_bs(bs), prefix_chain_bs, |
Andrey Shinkevich | 170d3bd | 2019-05-29 20:56:14 +0300 | [diff] [blame] | 3731 | false, offset, n, &n); |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3732 | if (ret < 0) { |
| 3733 | error_report("error while reading image metadata: %s", |
| 3734 | strerror(-ret)); |
| 3735 | goto out; |
| 3736 | } |
| 3737 | if (!ret) { |
| 3738 | continue; |
| 3739 | } |
| 3740 | } |
| 3741 | |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3742 | /* |
| 3743 | * Read old and new backing file and take into consideration that |
| 3744 | * backing files may be smaller than the COW image. |
| 3745 | */ |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3746 | if (offset >= old_backing_size) { |
| 3747 | memset(buf_old, 0, n); |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3748 | buf_old_is_zero = true; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3749 | } else { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3750 | if (offset + n > old_backing_size) { |
| 3751 | n = old_backing_size - offset; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3752 | } |
| 3753 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3754 | ret = blk_pread(blk_old_backing, offset, buf_old, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3755 | if (ret < 0) { |
| 3756 | error_report("error while reading from old backing file"); |
| 3757 | goto out; |
| 3758 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3759 | } |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3760 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3761 | if (offset >= new_backing_size || !blk_new_backing) { |
| 3762 | memset(buf_new, 0, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3763 | } else { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3764 | if (offset + n > new_backing_size) { |
| 3765 | n = new_backing_size - offset; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3766 | } |
| 3767 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3768 | ret = blk_pread(blk_new_backing, offset, buf_new, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3769 | if (ret < 0) { |
| 3770 | error_report("error while reading from new backing file"); |
| 3771 | goto out; |
| 3772 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3773 | } |
| 3774 | |
| 3775 | /* If they differ, we need to write to the COW file */ |
| 3776 | uint64_t written = 0; |
| 3777 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3778 | while (written < n) { |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 3779 | int64_t pnum; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3780 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3781 | if (compare_buffers(buf_old + written, buf_new + written, |
| 3782 | n - written, &pnum)) |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3783 | { |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3784 | if (buf_old_is_zero) { |
| 3785 | ret = blk_pwrite_zeroes(blk, offset + written, pnum, 0); |
| 3786 | } else { |
| 3787 | ret = blk_pwrite(blk, offset + written, |
| 3788 | buf_old + written, pnum, 0); |
| 3789 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3790 | if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3791 | error_report("Error while writing to COW image: %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3792 | strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3793 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3794 | } |
| 3795 | } |
| 3796 | |
| 3797 | written += pnum; |
| 3798 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3799 | qemu_progress_print(local_progress, 100); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3800 | } |
| 3801 | } |
| 3802 | |
| 3803 | /* |
| 3804 | * Change the backing file. All clusters that are different from the old |
| 3805 | * backing file are overwritten in the COW file now, so the visible content |
| 3806 | * doesn't change when we switch the backing file. |
| 3807 | */ |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3808 | if (out_baseimg && *out_baseimg) { |
| 3809 | ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt); |
| 3810 | } else { |
| 3811 | ret = bdrv_change_backing_file(bs, NULL, NULL); |
| 3812 | } |
| 3813 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3814 | if (ret == -ENOSPC) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3815 | error_report("Could not change the backing file to '%s': No " |
| 3816 | "space left in the file header", out_baseimg); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3817 | } else if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3818 | error_report("Could not change the backing file to '%s': %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3819 | out_baseimg, strerror(-ret)); |
| 3820 | } |
| 3821 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3822 | qemu_progress_print(100, 0); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3823 | /* |
| 3824 | * TODO At this point it is possible to check if any clusters that are |
| 3825 | * allocated in the COW file are the same in the backing file. If so, they |
| 3826 | * could be dropped from the COW file. Don't do this before switching the |
| 3827 | * backing file, in case of a crash this would lead to corruption. |
| 3828 | */ |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3829 | out: |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3830 | qemu_progress_end(); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3831 | /* Cleanup */ |
| 3832 | if (!unsafe) { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3833 | blk_unref(blk_old_backing); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3834 | blk_unref(blk_new_backing); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3835 | } |
Paolo Bonzini | 396374c | 2016-02-25 23:53:54 +0100 | [diff] [blame] | 3836 | qemu_vfree(buf_old); |
| 3837 | qemu_vfree(buf_new); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3838 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3839 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3840 | if (ret) { |
| 3841 | return 1; |
| 3842 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3843 | return 0; |
| 3844 | } |
| 3845 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3846 | static int img_resize(int argc, char **argv) |
| 3847 | { |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 3848 | Error *err = NULL; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3849 | int c, ret, relative; |
| 3850 | const char *filename, *fmt, *size; |
Max Reitz | 09c5c6d | 2019-09-18 11:51:44 +0200 | [diff] [blame] | 3851 | int64_t n, total_size, current_size; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3852 | bool quiet = false; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3853 | BlockBackend *blk = NULL; |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3854 | PreallocMode prealloc = PREALLOC_MODE_OFF; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3855 | QemuOpts *param; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3856 | |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3857 | static QemuOptsList resize_options = { |
| 3858 | .name = "resize_options", |
| 3859 | .head = QTAILQ_HEAD_INITIALIZER(resize_options.head), |
| 3860 | .desc = { |
| 3861 | { |
| 3862 | .name = BLOCK_OPT_SIZE, |
| 3863 | .type = QEMU_OPT_SIZE, |
| 3864 | .help = "Virtual disk size" |
| 3865 | }, { |
| 3866 | /* end of list */ |
| 3867 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3868 | }, |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3869 | }; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3870 | bool image_opts = false; |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3871 | bool shrink = false; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3872 | |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 3873 | /* Remove size from argv manually so that negative numbers are not treated |
| 3874 | * as options by getopt. */ |
| 3875 | if (argc < 3) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3876 | error_exit("Not enough arguments"); |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 3877 | return 1; |
| 3878 | } |
| 3879 | |
| 3880 | size = argv[--argc]; |
| 3881 | |
| 3882 | /* Parse getopt arguments */ |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3883 | fmt = NULL; |
| 3884 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3885 | static const struct option long_options[] = { |
| 3886 | {"help", no_argument, 0, 'h'}, |
| 3887 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3888 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3889 | {"preallocation", required_argument, 0, OPTION_PREALLOCATION}, |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3890 | {"shrink", no_argument, 0, OPTION_SHRINK}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3891 | {0, 0, 0, 0} |
| 3892 | }; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3893 | c = getopt_long(argc, argv, ":f:hq", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3894 | long_options, NULL); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3895 | if (c == -1) { |
| 3896 | break; |
| 3897 | } |
| 3898 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3899 | case ':': |
| 3900 | missing_argument(argv[optind - 1]); |
| 3901 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3902 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3903 | unrecognized_option(argv[optind - 1]); |
| 3904 | break; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3905 | case 'h': |
| 3906 | help(); |
| 3907 | break; |
| 3908 | case 'f': |
| 3909 | fmt = optarg; |
| 3910 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3911 | case 'q': |
| 3912 | quiet = true; |
| 3913 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3914 | case OPTION_OBJECT: { |
| 3915 | QemuOpts *opts; |
| 3916 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 3917 | optarg, true); |
| 3918 | if (!opts) { |
| 3919 | return 1; |
| 3920 | } |
| 3921 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3922 | case OPTION_IMAGE_OPTS: |
| 3923 | image_opts = true; |
| 3924 | break; |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3925 | case OPTION_PREALLOCATION: |
Marc-André Lureau | f7abe0e | 2017-08-24 10:46:10 +0200 | [diff] [blame] | 3926 | prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg, |
Markus Armbruster | 06c60b6 | 2017-08-24 10:45:57 +0200 | [diff] [blame] | 3927 | PREALLOC_MODE__MAX, NULL); |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3928 | if (prealloc == PREALLOC_MODE__MAX) { |
| 3929 | error_report("Invalid preallocation mode '%s'", optarg); |
| 3930 | return 1; |
| 3931 | } |
| 3932 | break; |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3933 | case OPTION_SHRINK: |
| 3934 | shrink = true; |
| 3935 | break; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3936 | } |
| 3937 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 3938 | if (optind != argc - 1) { |
Max Reitz | be8fbd4 | 2018-02-05 17:27:45 +0100 | [diff] [blame] | 3939 | error_exit("Expecting image file name and size"); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3940 | } |
| 3941 | filename = argv[optind++]; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3942 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3943 | if (qemu_opts_foreach(&qemu_object_opts, |
| 3944 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 3945 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3946 | return 1; |
| 3947 | } |
| 3948 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3949 | /* Choose grow, shrink, or absolute resize mode */ |
| 3950 | switch (size[0]) { |
| 3951 | case '+': |
| 3952 | relative = 1; |
| 3953 | size++; |
| 3954 | break; |
| 3955 | case '-': |
| 3956 | relative = -1; |
| 3957 | size++; |
| 3958 | break; |
| 3959 | default: |
| 3960 | relative = 0; |
| 3961 | break; |
| 3962 | } |
| 3963 | |
| 3964 | /* Parse size */ |
Peter Crosthwaite | 87ea75d | 2014-01-01 18:49:17 -0800 | [diff] [blame] | 3965 | param = qemu_opts_create(&resize_options, NULL, 0, &error_abort); |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 3966 | qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err); |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 3967 | if (err) { |
| 3968 | error_report_err(err); |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3969 | ret = -1; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3970 | qemu_opts_del(param); |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3971 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3972 | } |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3973 | n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0); |
| 3974 | qemu_opts_del(param); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3975 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 3976 | blk = img_open(image_opts, filename, fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3977 | BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet, |
| 3978 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3979 | if (!blk) { |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3980 | ret = -1; |
| 3981 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3982 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3983 | |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3984 | current_size = blk_getlength(blk); |
| 3985 | if (current_size < 0) { |
| 3986 | error_report("Failed to inquire current image length: %s", |
| 3987 | strerror(-current_size)); |
| 3988 | ret = -1; |
| 3989 | goto out; |
| 3990 | } |
| 3991 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3992 | if (relative) { |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3993 | total_size = current_size + n * relative; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3994 | } else { |
| 3995 | total_size = n; |
| 3996 | } |
| 3997 | if (total_size <= 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3998 | error_report("New image size must be positive"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3999 | ret = -1; |
| 4000 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4001 | } |
| 4002 | |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 4003 | if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) { |
| 4004 | error_report("Preallocation can only be used for growing images"); |
| 4005 | ret = -1; |
| 4006 | goto out; |
| 4007 | } |
| 4008 | |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 4009 | if (total_size < current_size && !shrink) { |
| 4010 | warn_report("Shrinking an image will delete all data beyond the " |
| 4011 | "shrunken image's end. Before performing such an " |
| 4012 | "operation, make sure there is no important data there."); |
| 4013 | |
| 4014 | if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") != 0) { |
| 4015 | error_report( |
| 4016 | "Use the --shrink option to perform a shrink operation."); |
| 4017 | ret = -1; |
| 4018 | goto out; |
| 4019 | } else { |
| 4020 | warn_report("Using the --shrink option will suppress this message. " |
| 4021 | "Note that future versions of qemu-img may refuse to " |
| 4022 | "shrink images without this option."); |
| 4023 | } |
| 4024 | } |
| 4025 | |
Max Reitz | e8d04f9 | 2019-09-18 11:51:43 +0200 | [diff] [blame] | 4026 | /* |
| 4027 | * The user expects the image to have the desired size after |
| 4028 | * resizing, so pass @exact=true. It is of no use to report |
| 4029 | * success when the image has not actually been resized. |
| 4030 | */ |
Kevin Wolf | 8c6242b | 2020-04-24 14:54:41 +0200 | [diff] [blame] | 4031 | ret = blk_truncate(blk, total_size, true, prealloc, 0, &err); |
Max Reitz | 09c5c6d | 2019-09-18 11:51:44 +0200 | [diff] [blame] | 4032 | if (!ret) { |
| 4033 | qprintf(quiet, "Image resized.\n"); |
| 4034 | } else { |
Max Reitz | ed3d2ec | 2017-03-28 22:51:27 +0200 | [diff] [blame] | 4035 | error_report_err(err); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4036 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 4037 | out: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 4038 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 4039 | if (ret) { |
| 4040 | return 1; |
| 4041 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 4042 | return 0; |
| 4043 | } |
| 4044 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4045 | static void amend_status_cb(BlockDriverState *bs, |
Max Reitz | 8b13976 | 2015-07-27 17:51:32 +0200 | [diff] [blame] | 4046 | int64_t offset, int64_t total_work_size, |
| 4047 | void *opaque) |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4048 | { |
| 4049 | qemu_progress_print(100.f * offset / total_work_size, 0); |
| 4050 | } |
| 4051 | |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4052 | static int print_amend_option_help(const char *format) |
| 4053 | { |
| 4054 | BlockDriver *drv; |
| 4055 | |
| 4056 | /* Find driver and parse its options */ |
| 4057 | drv = bdrv_find_format(format); |
| 4058 | if (!drv) { |
| 4059 | error_report("Unknown file format '%s'", format); |
| 4060 | return 1; |
| 4061 | } |
| 4062 | |
| 4063 | if (!drv->bdrv_amend_options) { |
| 4064 | error_report("Format driver '%s' does not support option amendment", |
| 4065 | format); |
| 4066 | return 1; |
| 4067 | } |
| 4068 | |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4069 | /* Every driver supporting amendment must have amend_opts */ |
| 4070 | assert(drv->amend_opts); |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4071 | |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4072 | printf("Amend options for '%s':\n", format); |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4073 | qemu_opts_print_help(drv->amend_opts, false); |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4074 | return 0; |
| 4075 | } |
| 4076 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4077 | static int img_amend(int argc, char **argv) |
| 4078 | { |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 4079 | Error *err = NULL; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4080 | int c, ret = 0; |
| 4081 | char *options = NULL; |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4082 | QemuOptsList *amend_opts = NULL; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 4083 | QemuOpts *opts = NULL; |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 4084 | const char *fmt = NULL, *filename, *cache; |
| 4085 | int flags; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 4086 | bool writethrough; |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4087 | bool quiet = false, progress = false; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 4088 | BlockBackend *blk = NULL; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4089 | BlockDriverState *bs = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 4090 | bool image_opts = false; |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4091 | bool force = false; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4092 | |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 4093 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4094 | for (;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4095 | static const struct option long_options[] = { |
| 4096 | {"help", no_argument, 0, 'h'}, |
| 4097 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 4098 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4099 | {"force", no_argument, 0, OPTION_FORCE}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4100 | {0, 0, 0, 0} |
| 4101 | }; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4102 | c = getopt_long(argc, argv, ":ho:f:t:pq", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4103 | long_options, NULL); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4104 | if (c == -1) { |
| 4105 | break; |
| 4106 | } |
| 4107 | |
| 4108 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4109 | case ':': |
| 4110 | missing_argument(argv[optind - 1]); |
| 4111 | break; |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4112 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4113 | unrecognized_option(argv[optind - 1]); |
| 4114 | break; |
| 4115 | case 'h': |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4116 | help(); |
| 4117 | break; |
| 4118 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 4119 | if (accumulate_options(&options, optarg) < 0) { |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4120 | ret = -1; |
| 4121 | goto out_no_progress; |
| 4122 | } |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 4123 | break; |
| 4124 | case 'f': |
| 4125 | fmt = optarg; |
| 4126 | break; |
| 4127 | case 't': |
| 4128 | cache = optarg; |
| 4129 | break; |
| 4130 | case 'p': |
| 4131 | progress = true; |
| 4132 | break; |
| 4133 | case 'q': |
| 4134 | quiet = true; |
| 4135 | break; |
| 4136 | case OPTION_OBJECT: |
| 4137 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 4138 | optarg, true); |
| 4139 | if (!opts) { |
| 4140 | ret = -1; |
| 4141 | goto out_no_progress; |
| 4142 | } |
| 4143 | break; |
| 4144 | case OPTION_IMAGE_OPTS: |
| 4145 | image_opts = true; |
| 4146 | break; |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4147 | case OPTION_FORCE: |
| 4148 | force = true; |
| 4149 | break; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4150 | } |
| 4151 | } |
| 4152 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4153 | if (!options) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 4154 | error_exit("Must specify options (-o)"); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4155 | } |
| 4156 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4157 | if (qemu_opts_foreach(&qemu_object_opts, |
| 4158 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 4159 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4160 | ret = -1; |
| 4161 | goto out_no_progress; |
| 4162 | } |
| 4163 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4164 | if (quiet) { |
| 4165 | progress = false; |
| 4166 | } |
| 4167 | qemu_progress_init(progress, 1.0); |
| 4168 | |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4169 | filename = (optind == argc - 1) ? argv[argc - 1] : NULL; |
| 4170 | if (fmt && has_help_option(options)) { |
| 4171 | /* If a format is explicitly specified (and possibly no filename is |
| 4172 | * given), print option help here */ |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4173 | ret = print_amend_option_help(fmt); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4174 | goto out; |
| 4175 | } |
| 4176 | |
| 4177 | if (optind != argc - 1) { |
Max Reitz | b2f27e4 | 2014-10-27 11:12:52 +0100 | [diff] [blame] | 4178 | error_report("Expecting one image file name"); |
| 4179 | ret = -1; |
| 4180 | goto out; |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4181 | } |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4182 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 4183 | flags = BDRV_O_RDWR; |
| 4184 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 4185 | if (ret < 0) { |
| 4186 | error_report("Invalid cache option: %s", cache); |
| 4187 | goto out; |
| 4188 | } |
| 4189 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4190 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 4191 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 4192 | if (!blk) { |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4193 | ret = -1; |
| 4194 | goto out; |
| 4195 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 4196 | bs = blk_bs(blk); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4197 | |
| 4198 | fmt = bs->drv->format_name; |
| 4199 | |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 4200 | if (has_help_option(options)) { |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4201 | /* If the format was auto-detected, print option help here */ |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4202 | ret = print_amend_option_help(fmt); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4203 | goto out; |
| 4204 | } |
| 4205 | |
Max Reitz | 1f99668 | 2018-05-09 23:00:17 +0200 | [diff] [blame] | 4206 | if (!bs->drv->bdrv_amend_options) { |
| 4207 | error_report("Format driver '%s' does not support option amendment", |
Max Reitz | b2439d2 | 2014-12-02 18:32:47 +0100 | [diff] [blame] | 4208 | fmt); |
| 4209 | ret = -1; |
| 4210 | goto out; |
| 4211 | } |
| 4212 | |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4213 | /* Every driver supporting amendment must have amend_opts */ |
| 4214 | assert(bs->drv->amend_opts); |
Max Reitz | 1f99668 | 2018-05-09 23:00:17 +0200 | [diff] [blame] | 4215 | |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4216 | amend_opts = qemu_opts_append(amend_opts, bs->drv->amend_opts); |
| 4217 | opts = qemu_opts_create(amend_opts, NULL, 0, &error_abort); |
Paolo Bonzini | ece9086 | 2017-01-04 15:56:24 +0100 | [diff] [blame] | 4218 | qemu_opts_do_parse(opts, options, NULL, &err); |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4219 | |
Paolo Bonzini | ece9086 | 2017-01-04 15:56:24 +0100 | [diff] [blame] | 4220 | if (err) { |
Maxim Levitsky | 0b6786a | 2020-06-25 14:55:40 +0200 | [diff] [blame] | 4221 | /* Try to parse options using the create options */ |
| 4222 | Error *err1 = NULL; |
| 4223 | amend_opts = qemu_opts_append(amend_opts, bs->drv->create_opts); |
| 4224 | qemu_opts_del(opts); |
| 4225 | opts = qemu_opts_create(amend_opts, NULL, 0, &error_abort); |
| 4226 | qemu_opts_do_parse(opts, options, NULL, &err1); |
| 4227 | |
| 4228 | if (!err1) { |
| 4229 | error_append_hint(&err, |
| 4230 | "This option is only supported for image creation\n"); |
| 4231 | } else { |
| 4232 | error_free(err1); |
| 4233 | } |
| 4234 | |
Paolo Bonzini | ece9086 | 2017-01-04 15:56:24 +0100 | [diff] [blame] | 4235 | error_report_err(err); |
| 4236 | ret = -1; |
| 4237 | goto out; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4238 | } |
| 4239 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4240 | /* In case the driver does not call amend_status_cb() */ |
| 4241 | qemu_progress_print(0.f, 0); |
Maxim Levitsky | a3579bf | 2020-06-25 14:55:38 +0200 | [diff] [blame] | 4242 | ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL, force, &err); |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4243 | qemu_progress_print(100.f, 0); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4244 | if (ret < 0) { |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 4245 | error_report_err(err); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4246 | goto out; |
| 4247 | } |
| 4248 | |
| 4249 | out: |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4250 | qemu_progress_end(); |
| 4251 | |
Max Reitz | e814dff | 2015-08-20 16:00:38 -0700 | [diff] [blame] | 4252 | out_no_progress: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 4253 | blk_unref(blk); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 4254 | qemu_opts_del(opts); |
Maxim Levitsky | df373fb | 2020-06-25 14:55:39 +0200 | [diff] [blame] | 4255 | qemu_opts_free(amend_opts); |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 4256 | g_free(options); |
| 4257 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4258 | if (ret) { |
| 4259 | return 1; |
| 4260 | } |
| 4261 | return 0; |
| 4262 | } |
| 4263 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4264 | typedef struct BenchData { |
| 4265 | BlockBackend *blk; |
| 4266 | uint64_t image_size; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4267 | bool write; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4268 | int bufsize; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4269 | int step; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4270 | int nrreq; |
| 4271 | int n; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4272 | int flush_interval; |
| 4273 | bool drain_on_flush; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4274 | uint8_t *buf; |
| 4275 | QEMUIOVector *qiov; |
| 4276 | |
| 4277 | int in_flight; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4278 | bool in_flush; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4279 | uint64_t offset; |
| 4280 | } BenchData; |
| 4281 | |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4282 | static void bench_undrained_flush_cb(void *opaque, int ret) |
| 4283 | { |
| 4284 | if (ret < 0) { |
Markus Armbruster | df3c286 | 2016-08-03 13:37:51 +0200 | [diff] [blame] | 4285 | error_report("Failed flush request: %s", strerror(-ret)); |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4286 | exit(EXIT_FAILURE); |
| 4287 | } |
| 4288 | } |
| 4289 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4290 | static void bench_cb(void *opaque, int ret) |
| 4291 | { |
| 4292 | BenchData *b = opaque; |
| 4293 | BlockAIOCB *acb; |
| 4294 | |
| 4295 | if (ret < 0) { |
Markus Armbruster | df3c286 | 2016-08-03 13:37:51 +0200 | [diff] [blame] | 4296 | error_report("Failed request: %s", strerror(-ret)); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4297 | exit(EXIT_FAILURE); |
| 4298 | } |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4299 | |
| 4300 | if (b->in_flush) { |
| 4301 | /* Just finished a flush with drained queue: Start next requests */ |
| 4302 | assert(b->in_flight == 0); |
| 4303 | b->in_flush = false; |
| 4304 | } else if (b->in_flight > 0) { |
| 4305 | int remaining = b->n - b->in_flight; |
| 4306 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4307 | b->n--; |
| 4308 | b->in_flight--; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4309 | |
| 4310 | /* Time for flush? Drain queue if requested, then flush */ |
| 4311 | if (b->flush_interval && remaining % b->flush_interval == 0) { |
| 4312 | if (!b->in_flight || !b->drain_on_flush) { |
| 4313 | BlockCompletionFunc *cb; |
| 4314 | |
| 4315 | if (b->drain_on_flush) { |
| 4316 | b->in_flush = true; |
| 4317 | cb = bench_cb; |
| 4318 | } else { |
| 4319 | cb = bench_undrained_flush_cb; |
| 4320 | } |
| 4321 | |
| 4322 | acb = blk_aio_flush(b->blk, cb, b); |
| 4323 | if (!acb) { |
| 4324 | error_report("Failed to issue flush request"); |
| 4325 | exit(EXIT_FAILURE); |
| 4326 | } |
| 4327 | } |
| 4328 | if (b->drain_on_flush) { |
| 4329 | return; |
| 4330 | } |
| 4331 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4332 | } |
| 4333 | |
| 4334 | while (b->n > b->in_flight && b->in_flight < b->nrreq) { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4335 | int64_t offset = b->offset; |
| 4336 | /* blk_aio_* might look for completed I/Os and kick bench_cb |
| 4337 | * again, so make sure this operation is counted by in_flight |
| 4338 | * and b->offset is ready for the next submission. |
| 4339 | */ |
| 4340 | b->in_flight++; |
| 4341 | b->offset += b->step; |
| 4342 | b->offset %= b->image_size; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4343 | if (b->write) { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4344 | 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] | 4345 | } else { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4346 | 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] | 4347 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4348 | if (!acb) { |
| 4349 | error_report("Failed to issue request"); |
| 4350 | exit(EXIT_FAILURE); |
| 4351 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4352 | } |
| 4353 | } |
| 4354 | |
| 4355 | static int img_bench(int argc, char **argv) |
| 4356 | { |
| 4357 | int c, ret = 0; |
| 4358 | const char *fmt = NULL, *filename; |
| 4359 | bool quiet = false; |
| 4360 | bool image_opts = false; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4361 | bool is_write = false; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4362 | int count = 75000; |
| 4363 | int depth = 64; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4364 | int64_t offset = 0; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4365 | size_t bufsize = 4096; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4366 | int pattern = 0; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4367 | size_t step = 0; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4368 | int flush_interval = 0; |
| 4369 | bool drain_on_flush = true; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4370 | int64_t image_size; |
| 4371 | BlockBackend *blk = NULL; |
| 4372 | BenchData data = {}; |
| 4373 | int flags = 0; |
Kevin Wolf | 604e861 | 2016-06-14 11:29:32 +0200 | [diff] [blame] | 4374 | bool writethrough = false; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4375 | struct timeval t1, t2; |
| 4376 | int i; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4377 | bool force_share = false; |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4378 | size_t buf_size; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4379 | |
| 4380 | for (;;) { |
| 4381 | static const struct option long_options[] = { |
| 4382 | {"help", no_argument, 0, 'h'}, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4383 | {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL}, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4384 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4385 | {"pattern", required_argument, 0, OPTION_PATTERN}, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4386 | {"no-drain", no_argument, 0, OPTION_NO_DRAIN}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4387 | {"force-share", no_argument, 0, 'U'}, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4388 | {0, 0, 0, 0} |
| 4389 | }; |
Aarushi Mehta | cdd2677 | 2020-01-20 14:18:55 +0000 | [diff] [blame] | 4390 | c = getopt_long(argc, argv, ":hc:d:f:ni:o:qs:S:t:wU", long_options, |
| 4391 | NULL); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4392 | if (c == -1) { |
| 4393 | break; |
| 4394 | } |
| 4395 | |
| 4396 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4397 | case ':': |
| 4398 | missing_argument(argv[optind - 1]); |
| 4399 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4400 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4401 | unrecognized_option(argv[optind - 1]); |
| 4402 | break; |
| 4403 | case 'h': |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4404 | help(); |
| 4405 | break; |
| 4406 | case 'c': |
| 4407 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4408 | unsigned long res; |
| 4409 | |
| 4410 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4411 | error_report("Invalid request count specified"); |
| 4412 | return 1; |
| 4413 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4414 | count = res; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4415 | break; |
| 4416 | } |
| 4417 | case 'd': |
| 4418 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4419 | unsigned long res; |
| 4420 | |
| 4421 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4422 | error_report("Invalid queue depth specified"); |
| 4423 | return 1; |
| 4424 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4425 | depth = res; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4426 | break; |
| 4427 | } |
| 4428 | case 'f': |
| 4429 | fmt = optarg; |
| 4430 | break; |
| 4431 | case 'n': |
| 4432 | flags |= BDRV_O_NATIVE_AIO; |
| 4433 | break; |
Aarushi Mehta | cdd2677 | 2020-01-20 14:18:55 +0000 | [diff] [blame] | 4434 | case 'i': |
| 4435 | ret = bdrv_parse_aio(optarg, &flags); |
| 4436 | if (ret < 0) { |
| 4437 | error_report("Invalid aio option: %s", optarg); |
| 4438 | ret = -1; |
| 4439 | goto out; |
| 4440 | } |
| 4441 | break; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4442 | case 'o': |
| 4443 | { |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4444 | offset = cvtnum("offset", optarg); |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4445 | if (offset < 0) { |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4446 | return 1; |
| 4447 | } |
| 4448 | break; |
| 4449 | } |
| 4450 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4451 | case 'q': |
| 4452 | quiet = true; |
| 4453 | break; |
| 4454 | case 's': |
| 4455 | { |
| 4456 | int64_t sval; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4457 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4458 | sval = cvtnum_full("buffer size", optarg, 0, INT_MAX); |
| 4459 | if (sval < 0) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4460 | return 1; |
| 4461 | } |
| 4462 | |
| 4463 | bufsize = sval; |
| 4464 | break; |
| 4465 | } |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4466 | case 'S': |
| 4467 | { |
| 4468 | int64_t sval; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4469 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4470 | sval = cvtnum_full("step_size", optarg, 0, INT_MAX); |
| 4471 | if (sval < 0) { |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4472 | return 1; |
| 4473 | } |
| 4474 | |
| 4475 | step = sval; |
| 4476 | break; |
| 4477 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4478 | case 't': |
| 4479 | ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough); |
| 4480 | if (ret < 0) { |
| 4481 | error_report("Invalid cache mode"); |
| 4482 | ret = -1; |
| 4483 | goto out; |
| 4484 | } |
| 4485 | break; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4486 | case 'w': |
| 4487 | flags |= BDRV_O_RDWR; |
| 4488 | is_write = true; |
| 4489 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4490 | case 'U': |
| 4491 | force_share = true; |
| 4492 | break; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4493 | case OPTION_PATTERN: |
| 4494 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4495 | unsigned long res; |
| 4496 | |
| 4497 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) { |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4498 | error_report("Invalid pattern byte specified"); |
| 4499 | return 1; |
| 4500 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4501 | pattern = res; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4502 | break; |
| 4503 | } |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4504 | case OPTION_FLUSH_INTERVAL: |
| 4505 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4506 | unsigned long res; |
| 4507 | |
| 4508 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4509 | error_report("Invalid flush interval specified"); |
| 4510 | return 1; |
| 4511 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4512 | flush_interval = res; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4513 | break; |
| 4514 | } |
| 4515 | case OPTION_NO_DRAIN: |
| 4516 | drain_on_flush = false; |
| 4517 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4518 | case OPTION_IMAGE_OPTS: |
| 4519 | image_opts = true; |
| 4520 | break; |
| 4521 | } |
| 4522 | } |
| 4523 | |
| 4524 | if (optind != argc - 1) { |
| 4525 | error_exit("Expecting one image file name"); |
| 4526 | } |
| 4527 | filename = argv[argc - 1]; |
| 4528 | |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4529 | if (!is_write && flush_interval) { |
| 4530 | error_report("--flush-interval is only available in write tests"); |
| 4531 | ret = -1; |
| 4532 | goto out; |
| 4533 | } |
| 4534 | if (flush_interval && flush_interval < depth) { |
| 4535 | error_report("Flush interval can't be smaller than depth"); |
| 4536 | ret = -1; |
| 4537 | goto out; |
| 4538 | } |
| 4539 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4540 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 4541 | force_share); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4542 | if (!blk) { |
| 4543 | ret = -1; |
| 4544 | goto out; |
| 4545 | } |
| 4546 | |
| 4547 | image_size = blk_getlength(blk); |
| 4548 | if (image_size < 0) { |
| 4549 | ret = image_size; |
| 4550 | goto out; |
| 4551 | } |
| 4552 | |
| 4553 | data = (BenchData) { |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4554 | .blk = blk, |
| 4555 | .image_size = image_size, |
| 4556 | .bufsize = bufsize, |
| 4557 | .step = step ?: bufsize, |
| 4558 | .nrreq = depth, |
| 4559 | .n = count, |
| 4560 | .offset = offset, |
| 4561 | .write = is_write, |
| 4562 | .flush_interval = flush_interval, |
| 4563 | .drain_on_flush = drain_on_flush, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4564 | }; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4565 | printf("Sending %d %s requests, %d bytes each, %d in parallel " |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4566 | "(starting at offset %" PRId64 ", step size %d)\n", |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4567 | data.n, data.write ? "write" : "read", data.bufsize, data.nrreq, |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4568 | data.offset, data.step); |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4569 | if (flush_interval) { |
| 4570 | printf("Sending flush every %d requests\n", flush_interval); |
| 4571 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4572 | |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4573 | buf_size = data.nrreq * data.bufsize; |
| 4574 | data.buf = blk_blockalign(blk, buf_size); |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4575 | memset(data.buf, pattern, data.nrreq * data.bufsize); |
| 4576 | |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4577 | blk_register_buf(blk, data.buf, buf_size); |
| 4578 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4579 | data.qiov = g_new(QEMUIOVector, data.nrreq); |
| 4580 | for (i = 0; i < data.nrreq; i++) { |
| 4581 | qemu_iovec_init(&data.qiov[i], 1); |
| 4582 | qemu_iovec_add(&data.qiov[i], |
| 4583 | data.buf + i * data.bufsize, data.bufsize); |
| 4584 | } |
| 4585 | |
| 4586 | gettimeofday(&t1, NULL); |
| 4587 | bench_cb(&data, 0); |
| 4588 | |
| 4589 | while (data.n > 0) { |
| 4590 | main_loop_wait(false); |
| 4591 | } |
| 4592 | gettimeofday(&t2, NULL); |
| 4593 | |
| 4594 | printf("Run completed in %3.3f seconds.\n", |
| 4595 | (t2.tv_sec - t1.tv_sec) |
| 4596 | + ((double)(t2.tv_usec - t1.tv_usec) / 1000000)); |
| 4597 | |
| 4598 | out: |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4599 | if (data.buf) { |
| 4600 | blk_unregister_buf(blk, data.buf); |
| 4601 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4602 | qemu_vfree(data.buf); |
| 4603 | blk_unref(blk); |
| 4604 | |
| 4605 | if (ret) { |
| 4606 | return 1; |
| 4607 | } |
| 4608 | return 0; |
| 4609 | } |
| 4610 | |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4611 | enum ImgBitmapAct { |
| 4612 | BITMAP_ADD, |
| 4613 | BITMAP_REMOVE, |
| 4614 | BITMAP_CLEAR, |
| 4615 | BITMAP_ENABLE, |
| 4616 | BITMAP_DISABLE, |
| 4617 | BITMAP_MERGE, |
| 4618 | }; |
| 4619 | typedef struct ImgBitmapAction { |
| 4620 | enum ImgBitmapAct act; |
| 4621 | const char *src; /* only used for merge */ |
| 4622 | QSIMPLEQ_ENTRY(ImgBitmapAction) next; |
| 4623 | } ImgBitmapAction; |
| 4624 | |
| 4625 | static int img_bitmap(int argc, char **argv) |
| 4626 | { |
| 4627 | Error *err = NULL; |
| 4628 | int c, ret = 1; |
| 4629 | QemuOpts *opts = NULL; |
| 4630 | const char *fmt = NULL, *src_fmt = NULL, *src_filename = NULL; |
| 4631 | const char *filename, *bitmap; |
| 4632 | BlockBackend *blk = NULL, *src = NULL; |
| 4633 | BlockDriverState *bs = NULL, *src_bs = NULL; |
| 4634 | bool image_opts = false; |
| 4635 | int64_t granularity = 0; |
| 4636 | bool add = false, merge = false; |
| 4637 | QSIMPLEQ_HEAD(, ImgBitmapAction) actions; |
| 4638 | ImgBitmapAction *act, *act_next; |
| 4639 | const char *op; |
| 4640 | |
| 4641 | QSIMPLEQ_INIT(&actions); |
| 4642 | |
| 4643 | for (;;) { |
| 4644 | static const struct option long_options[] = { |
| 4645 | {"help", no_argument, 0, 'h'}, |
| 4646 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 4647 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
| 4648 | {"add", no_argument, 0, OPTION_ADD}, |
| 4649 | {"remove", no_argument, 0, OPTION_REMOVE}, |
| 4650 | {"clear", no_argument, 0, OPTION_CLEAR}, |
| 4651 | {"enable", no_argument, 0, OPTION_ENABLE}, |
| 4652 | {"disable", no_argument, 0, OPTION_DISABLE}, |
| 4653 | {"merge", required_argument, 0, OPTION_MERGE}, |
| 4654 | {"granularity", required_argument, 0, 'g'}, |
| 4655 | {"source-file", required_argument, 0, 'b'}, |
| 4656 | {"source-format", required_argument, 0, 'F'}, |
| 4657 | {0, 0, 0, 0} |
| 4658 | }; |
| 4659 | c = getopt_long(argc, argv, ":b:f:F:g:h", long_options, NULL); |
| 4660 | if (c == -1) { |
| 4661 | break; |
| 4662 | } |
| 4663 | |
| 4664 | switch (c) { |
| 4665 | case ':': |
| 4666 | missing_argument(argv[optind - 1]); |
| 4667 | break; |
| 4668 | case '?': |
| 4669 | unrecognized_option(argv[optind - 1]); |
| 4670 | break; |
| 4671 | case 'h': |
| 4672 | help(); |
| 4673 | break; |
| 4674 | case 'b': |
| 4675 | src_filename = optarg; |
| 4676 | break; |
| 4677 | case 'f': |
| 4678 | fmt = optarg; |
| 4679 | break; |
| 4680 | case 'F': |
| 4681 | src_fmt = optarg; |
| 4682 | break; |
| 4683 | case 'g': |
| 4684 | granularity = cvtnum("granularity", optarg); |
| 4685 | if (granularity < 0) { |
| 4686 | return 1; |
| 4687 | } |
| 4688 | break; |
| 4689 | case OPTION_ADD: |
| 4690 | act = g_new0(ImgBitmapAction, 1); |
| 4691 | act->act = BITMAP_ADD; |
| 4692 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4693 | add = true; |
| 4694 | break; |
| 4695 | case OPTION_REMOVE: |
| 4696 | act = g_new0(ImgBitmapAction, 1); |
| 4697 | act->act = BITMAP_REMOVE; |
| 4698 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4699 | break; |
| 4700 | case OPTION_CLEAR: |
| 4701 | act = g_new0(ImgBitmapAction, 1); |
| 4702 | act->act = BITMAP_CLEAR; |
| 4703 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4704 | break; |
| 4705 | case OPTION_ENABLE: |
| 4706 | act = g_new0(ImgBitmapAction, 1); |
| 4707 | act->act = BITMAP_ENABLE; |
| 4708 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4709 | break; |
| 4710 | case OPTION_DISABLE: |
| 4711 | act = g_new0(ImgBitmapAction, 1); |
| 4712 | act->act = BITMAP_DISABLE; |
| 4713 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4714 | break; |
| 4715 | case OPTION_MERGE: |
| 4716 | act = g_new0(ImgBitmapAction, 1); |
| 4717 | act->act = BITMAP_MERGE; |
| 4718 | act->src = optarg; |
| 4719 | QSIMPLEQ_INSERT_TAIL(&actions, act, next); |
| 4720 | merge = true; |
| 4721 | break; |
| 4722 | case OPTION_OBJECT: |
| 4723 | opts = qemu_opts_parse_noisily(&qemu_object_opts, optarg, true); |
| 4724 | if (!opts) { |
| 4725 | goto out; |
| 4726 | } |
| 4727 | break; |
| 4728 | case OPTION_IMAGE_OPTS: |
| 4729 | image_opts = true; |
| 4730 | break; |
| 4731 | } |
| 4732 | } |
| 4733 | |
| 4734 | if (qemu_opts_foreach(&qemu_object_opts, |
| 4735 | user_creatable_add_opts_foreach, |
| 4736 | qemu_img_object_print_help, &error_fatal)) { |
| 4737 | goto out; |
| 4738 | } |
| 4739 | |
| 4740 | if (QSIMPLEQ_EMPTY(&actions)) { |
| 4741 | error_report("Need at least one of --add, --remove, --clear, " |
| 4742 | "--enable, --disable, or --merge"); |
| 4743 | goto out; |
| 4744 | } |
| 4745 | |
| 4746 | if (granularity && !add) { |
| 4747 | error_report("granularity only supported with --add"); |
| 4748 | goto out; |
| 4749 | } |
| 4750 | if (src_fmt && !src_filename) { |
| 4751 | error_report("-F only supported with -b"); |
| 4752 | goto out; |
| 4753 | } |
| 4754 | if (src_filename && !merge) { |
| 4755 | error_report("Merge bitmap source file only supported with " |
| 4756 | "--merge"); |
| 4757 | goto out; |
| 4758 | } |
| 4759 | |
| 4760 | if (optind != argc - 2) { |
| 4761 | error_report("Expecting filename and bitmap name"); |
| 4762 | goto out; |
| 4763 | } |
| 4764 | |
| 4765 | filename = argv[optind]; |
| 4766 | bitmap = argv[optind + 1]; |
| 4767 | |
| 4768 | blk = img_open(image_opts, filename, fmt, BDRV_O_RDWR, false, false, |
| 4769 | false); |
| 4770 | if (!blk) { |
| 4771 | goto out; |
| 4772 | } |
| 4773 | bs = blk_bs(blk); |
| 4774 | if (src_filename) { |
| 4775 | src = img_open(false, src_filename, src_fmt, 0, false, false, false); |
| 4776 | if (!src) { |
| 4777 | goto out; |
| 4778 | } |
| 4779 | src_bs = blk_bs(src); |
| 4780 | } else { |
| 4781 | src_bs = bs; |
| 4782 | } |
| 4783 | |
| 4784 | QSIMPLEQ_FOREACH_SAFE(act, &actions, next, act_next) { |
| 4785 | switch (act->act) { |
| 4786 | case BITMAP_ADD: |
| 4787 | qmp_block_dirty_bitmap_add(bs->node_name, bitmap, |
| 4788 | !!granularity, granularity, true, true, |
| 4789 | false, false, &err); |
| 4790 | op = "add"; |
| 4791 | break; |
| 4792 | case BITMAP_REMOVE: |
| 4793 | qmp_block_dirty_bitmap_remove(bs->node_name, bitmap, &err); |
| 4794 | op = "remove"; |
| 4795 | break; |
| 4796 | case BITMAP_CLEAR: |
| 4797 | qmp_block_dirty_bitmap_clear(bs->node_name, bitmap, &err); |
| 4798 | op = "clear"; |
| 4799 | break; |
| 4800 | case BITMAP_ENABLE: |
| 4801 | qmp_block_dirty_bitmap_enable(bs->node_name, bitmap, &err); |
| 4802 | op = "enable"; |
| 4803 | break; |
| 4804 | case BITMAP_DISABLE: |
| 4805 | qmp_block_dirty_bitmap_disable(bs->node_name, bitmap, &err); |
| 4806 | op = "disable"; |
| 4807 | break; |
Eric Blake | 6c729dd | 2020-05-21 14:21:35 -0500 | [diff] [blame] | 4808 | case BITMAP_MERGE: |
| 4809 | do_dirty_bitmap_merge(bs->node_name, bitmap, src_bs->node_name, |
| 4810 | act->src, &err); |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4811 | op = "merge"; |
| 4812 | break; |
Eric Blake | 3b51ab4 | 2020-05-12 20:16:45 -0500 | [diff] [blame] | 4813 | default: |
| 4814 | g_assert_not_reached(); |
| 4815 | } |
| 4816 | |
| 4817 | if (err) { |
| 4818 | error_reportf_err(err, "Operation %s on bitmap %s failed: ", |
| 4819 | op, bitmap); |
| 4820 | goto out; |
| 4821 | } |
| 4822 | g_free(act); |
| 4823 | } |
| 4824 | |
| 4825 | ret = 0; |
| 4826 | |
| 4827 | out: |
| 4828 | blk_unref(src); |
| 4829 | blk_unref(blk); |
| 4830 | qemu_opts_del(opts); |
| 4831 | return ret; |
| 4832 | } |
| 4833 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4834 | #define C_BS 01 |
| 4835 | #define C_COUNT 02 |
| 4836 | #define C_IF 04 |
| 4837 | #define C_OF 010 |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4838 | #define C_SKIP 020 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4839 | |
| 4840 | struct DdInfo { |
| 4841 | unsigned int flags; |
| 4842 | int64_t count; |
| 4843 | }; |
| 4844 | |
| 4845 | struct DdIo { |
| 4846 | int bsz; /* Block size */ |
| 4847 | char *filename; |
| 4848 | uint8_t *buf; |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4849 | int64_t offset; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4850 | }; |
| 4851 | |
| 4852 | struct DdOpts { |
| 4853 | const char *name; |
| 4854 | int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *); |
| 4855 | unsigned int flag; |
| 4856 | }; |
| 4857 | |
| 4858 | static int img_dd_bs(const char *arg, |
| 4859 | struct DdIo *in, struct DdIo *out, |
| 4860 | struct DdInfo *dd) |
| 4861 | { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4862 | int64_t res; |
| 4863 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4864 | res = cvtnum_full("bs", arg, 1, INT_MAX); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4865 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4866 | if (res < 0) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4867 | return 1; |
| 4868 | } |
| 4869 | in->bsz = out->bsz = res; |
| 4870 | |
| 4871 | return 0; |
| 4872 | } |
| 4873 | |
| 4874 | static int img_dd_count(const char *arg, |
| 4875 | struct DdIo *in, struct DdIo *out, |
| 4876 | struct DdInfo *dd) |
| 4877 | { |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4878 | dd->count = cvtnum("count", arg); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4879 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4880 | if (dd->count < 0) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4881 | return 1; |
| 4882 | } |
| 4883 | |
| 4884 | return 0; |
| 4885 | } |
| 4886 | |
| 4887 | static int img_dd_if(const char *arg, |
| 4888 | struct DdIo *in, struct DdIo *out, |
| 4889 | struct DdInfo *dd) |
| 4890 | { |
| 4891 | in->filename = g_strdup(arg); |
| 4892 | |
| 4893 | return 0; |
| 4894 | } |
| 4895 | |
| 4896 | static int img_dd_of(const char *arg, |
| 4897 | struct DdIo *in, struct DdIo *out, |
| 4898 | struct DdInfo *dd) |
| 4899 | { |
| 4900 | out->filename = g_strdup(arg); |
| 4901 | |
| 4902 | return 0; |
| 4903 | } |
| 4904 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4905 | static int img_dd_skip(const char *arg, |
| 4906 | struct DdIo *in, struct DdIo *out, |
| 4907 | struct DdInfo *dd) |
| 4908 | { |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 4909 | in->offset = cvtnum("skip", arg); |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4910 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4911 | if (in->offset < 0) { |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4912 | return 1; |
| 4913 | } |
| 4914 | |
| 4915 | return 0; |
| 4916 | } |
| 4917 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4918 | static int img_dd(int argc, char **argv) |
| 4919 | { |
| 4920 | int ret = 0; |
| 4921 | char *arg = NULL; |
| 4922 | char *tmp; |
| 4923 | BlockDriver *drv = NULL, *proto_drv = NULL; |
| 4924 | BlockBackend *blk1 = NULL, *blk2 = NULL; |
| 4925 | QemuOpts *opts = NULL; |
| 4926 | QemuOptsList *create_opts = NULL; |
| 4927 | Error *local_err = NULL; |
| 4928 | bool image_opts = false; |
| 4929 | int c, i; |
| 4930 | const char *out_fmt = "raw"; |
| 4931 | const char *fmt = NULL; |
| 4932 | int64_t size = 0; |
| 4933 | int64_t block_count = 0, out_pos, in_pos; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4934 | bool force_share = false; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4935 | struct DdInfo dd = { |
| 4936 | .flags = 0, |
| 4937 | .count = 0, |
| 4938 | }; |
| 4939 | struct DdIo in = { |
| 4940 | .bsz = 512, /* Block size is by default 512 bytes */ |
| 4941 | .filename = NULL, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4942 | .buf = NULL, |
| 4943 | .offset = 0 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4944 | }; |
| 4945 | struct DdIo out = { |
| 4946 | .bsz = 512, |
| 4947 | .filename = NULL, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4948 | .buf = NULL, |
| 4949 | .offset = 0 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4950 | }; |
| 4951 | |
| 4952 | const struct DdOpts options[] = { |
| 4953 | { "bs", img_dd_bs, C_BS }, |
| 4954 | { "count", img_dd_count, C_COUNT }, |
| 4955 | { "if", img_dd_if, C_IF }, |
| 4956 | { "of", img_dd_of, C_OF }, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4957 | { "skip", img_dd_skip, C_SKIP }, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4958 | { NULL, NULL, 0 } |
| 4959 | }; |
| 4960 | const struct option long_options[] = { |
| 4961 | { "help", no_argument, 0, 'h'}, |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 4962 | { "object", required_argument, 0, OPTION_OBJECT}, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4963 | { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4964 | { "force-share", no_argument, 0, 'U'}, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4965 | { 0, 0, 0, 0 } |
| 4966 | }; |
| 4967 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4968 | while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4969 | if (c == EOF) { |
| 4970 | break; |
| 4971 | } |
| 4972 | switch (c) { |
| 4973 | case 'O': |
| 4974 | out_fmt = optarg; |
| 4975 | break; |
| 4976 | case 'f': |
| 4977 | fmt = optarg; |
| 4978 | break; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4979 | case ':': |
| 4980 | missing_argument(argv[optind - 1]); |
| 4981 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4982 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4983 | unrecognized_option(argv[optind - 1]); |
| 4984 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4985 | case 'h': |
| 4986 | help(); |
| 4987 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4988 | case 'U': |
| 4989 | force_share = true; |
| 4990 | break; |
Stefan Hajnoczi | 2a24570 | 2017-06-19 16:00:02 +0100 | [diff] [blame] | 4991 | case OPTION_OBJECT: |
| 4992 | if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) { |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 4993 | ret = -1; |
| 4994 | goto out; |
| 4995 | } |
Stefan Hajnoczi | 2a24570 | 2017-06-19 16:00:02 +0100 | [diff] [blame] | 4996 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4997 | case OPTION_IMAGE_OPTS: |
| 4998 | image_opts = true; |
| 4999 | break; |
| 5000 | } |
| 5001 | } |
| 5002 | |
| 5003 | for (i = optind; i < argc; i++) { |
| 5004 | int j; |
| 5005 | arg = g_strdup(argv[i]); |
| 5006 | |
| 5007 | tmp = strchr(arg, '='); |
| 5008 | if (tmp == NULL) { |
| 5009 | error_report("unrecognized operand %s", arg); |
| 5010 | ret = -1; |
| 5011 | goto out; |
| 5012 | } |
| 5013 | |
| 5014 | *tmp++ = '\0'; |
| 5015 | |
| 5016 | for (j = 0; options[j].name != NULL; j++) { |
| 5017 | if (!strcmp(arg, options[j].name)) { |
| 5018 | break; |
| 5019 | } |
| 5020 | } |
| 5021 | if (options[j].name == NULL) { |
| 5022 | error_report("unrecognized operand %s", arg); |
| 5023 | ret = -1; |
| 5024 | goto out; |
| 5025 | } |
| 5026 | |
| 5027 | if (options[j].f(tmp, &in, &out, &dd) != 0) { |
| 5028 | ret = -1; |
| 5029 | goto out; |
| 5030 | } |
| 5031 | dd.flags |= options[j].flag; |
| 5032 | g_free(arg); |
| 5033 | arg = NULL; |
| 5034 | } |
| 5035 | |
| 5036 | if (!(dd.flags & C_IF && dd.flags & C_OF)) { |
| 5037 | error_report("Must specify both input and output files"); |
| 5038 | ret = -1; |
| 5039 | goto out; |
| 5040 | } |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 5041 | |
| 5042 | if (qemu_opts_foreach(&qemu_object_opts, |
| 5043 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 5044 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 5045 | ret = -1; |
| 5046 | goto out; |
| 5047 | } |
| 5048 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 5049 | blk1 = img_open(image_opts, in.filename, fmt, 0, false, false, |
| 5050 | force_share); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5051 | |
| 5052 | if (!blk1) { |
| 5053 | ret = -1; |
| 5054 | goto out; |
| 5055 | } |
| 5056 | |
| 5057 | drv = bdrv_find_format(out_fmt); |
| 5058 | if (!drv) { |
| 5059 | error_report("Unknown file format"); |
| 5060 | ret = -1; |
| 5061 | goto out; |
| 5062 | } |
| 5063 | proto_drv = bdrv_find_protocol(out.filename, true, &local_err); |
| 5064 | |
| 5065 | if (!proto_drv) { |
| 5066 | error_report_err(local_err); |
| 5067 | ret = -1; |
| 5068 | goto out; |
| 5069 | } |
| 5070 | if (!drv->create_opts) { |
| 5071 | error_report("Format driver '%s' does not support image creation", |
| 5072 | drv->format_name); |
| 5073 | ret = -1; |
| 5074 | goto out; |
| 5075 | } |
| 5076 | if (!proto_drv->create_opts) { |
| 5077 | error_report("Protocol driver '%s' does not support image creation", |
| 5078 | proto_drv->format_name); |
| 5079 | ret = -1; |
| 5080 | goto out; |
| 5081 | } |
| 5082 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 5083 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
| 5084 | |
| 5085 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
| 5086 | |
| 5087 | size = blk_getlength(blk1); |
| 5088 | if (size < 0) { |
| 5089 | error_report("Failed to get size for '%s'", in.filename); |
| 5090 | ret = -1; |
| 5091 | goto out; |
| 5092 | } |
| 5093 | |
| 5094 | if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz && |
| 5095 | dd.count * in.bsz < size) { |
| 5096 | size = dd.count * in.bsz; |
| 5097 | } |
| 5098 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 5099 | /* Overflow means the specified offset is beyond input image's size */ |
| 5100 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || |
| 5101 | size < in.bsz * in.offset)) { |
| 5102 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort); |
| 5103 | } else { |
| 5104 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, |
| 5105 | size - in.bsz * in.offset, &error_abort); |
| 5106 | } |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5107 | |
| 5108 | ret = bdrv_create(drv, out.filename, opts, &local_err); |
| 5109 | if (ret < 0) { |
| 5110 | error_reportf_err(local_err, |
| 5111 | "%s: error while creating output image: ", |
| 5112 | out.filename); |
| 5113 | ret = -1; |
| 5114 | goto out; |
| 5115 | } |
| 5116 | |
Daniel P. Berrange | ea204dd | 2017-05-15 17:47:10 +0100 | [diff] [blame] | 5117 | /* TODO, we can't honour --image-opts for the target, |
| 5118 | * since it needs to be given in a format compatible |
| 5119 | * with the bdrv_create() call above which does not |
| 5120 | * support image-opts style. |
| 5121 | */ |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 5122 | 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] | 5123 | false, false, false); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5124 | |
| 5125 | if (!blk2) { |
| 5126 | ret = -1; |
| 5127 | goto out; |
| 5128 | } |
| 5129 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 5130 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || |
| 5131 | size < in.offset * in.bsz)) { |
| 5132 | /* We give a warning if the skip option is bigger than the input |
| 5133 | * size and create an empty output disk image (i.e. like dd(1)). |
| 5134 | */ |
| 5135 | error_report("%s: cannot skip to specified offset", in.filename); |
| 5136 | in_pos = size; |
| 5137 | } else { |
| 5138 | in_pos = in.offset * in.bsz; |
| 5139 | } |
| 5140 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5141 | in.buf = g_new(uint8_t, in.bsz); |
| 5142 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 5143 | for (out_pos = 0; in_pos < size; block_count++) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 5144 | int in_ret, out_ret; |
| 5145 | |
| 5146 | if (in_pos + in.bsz > size) { |
| 5147 | in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos); |
| 5148 | } else { |
| 5149 | in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz); |
| 5150 | } |
| 5151 | if (in_ret < 0) { |
| 5152 | error_report("error while reading from input image file: %s", |
| 5153 | strerror(-in_ret)); |
| 5154 | ret = -1; |
| 5155 | goto out; |
| 5156 | } |
| 5157 | in_pos += in_ret; |
| 5158 | |
| 5159 | out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0); |
| 5160 | |
| 5161 | if (out_ret < 0) { |
| 5162 | error_report("error while writing to output image file: %s", |
| 5163 | strerror(-out_ret)); |
| 5164 | ret = -1; |
| 5165 | goto out; |
| 5166 | } |
| 5167 | out_pos += out_ret; |
| 5168 | } |
| 5169 | |
| 5170 | out: |
| 5171 | g_free(arg); |
| 5172 | qemu_opts_del(opts); |
| 5173 | qemu_opts_free(create_opts); |
| 5174 | blk_unref(blk1); |
| 5175 | blk_unref(blk2); |
| 5176 | g_free(in.filename); |
| 5177 | g_free(out.filename); |
| 5178 | g_free(in.buf); |
| 5179 | g_free(out.buf); |
| 5180 | |
| 5181 | if (ret) { |
| 5182 | return 1; |
| 5183 | } |
| 5184 | return 0; |
| 5185 | } |
| 5186 | |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5187 | static void dump_json_block_measure_info(BlockMeasureInfo *info) |
| 5188 | { |
| 5189 | QString *str; |
| 5190 | QObject *obj; |
| 5191 | Visitor *v = qobject_output_visitor_new(&obj); |
| 5192 | |
| 5193 | visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort); |
| 5194 | visit_complete(v, &obj); |
| 5195 | str = qobject_to_json_pretty(obj); |
| 5196 | assert(str != NULL); |
| 5197 | printf("%s\n", qstring_get_str(str)); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 5198 | qobject_unref(obj); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5199 | visit_free(v); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 5200 | qobject_unref(str); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5201 | } |
| 5202 | |
| 5203 | static int img_measure(int argc, char **argv) |
| 5204 | { |
| 5205 | static const struct option long_options[] = { |
| 5206 | {"help", no_argument, 0, 'h'}, |
| 5207 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
| 5208 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 5209 | {"output", required_argument, 0, OPTION_OUTPUT}, |
| 5210 | {"size", required_argument, 0, OPTION_SIZE}, |
| 5211 | {"force-share", no_argument, 0, 'U'}, |
| 5212 | {0, 0, 0, 0} |
| 5213 | }; |
| 5214 | OutputFormat output_format = OFORMAT_HUMAN; |
| 5215 | BlockBackend *in_blk = NULL; |
| 5216 | BlockDriver *drv; |
| 5217 | const char *filename = NULL; |
| 5218 | const char *fmt = NULL; |
| 5219 | const char *out_fmt = "raw"; |
| 5220 | char *options = NULL; |
| 5221 | char *snapshot_name = NULL; |
| 5222 | bool force_share = false; |
| 5223 | QemuOpts *opts = NULL; |
| 5224 | QemuOpts *object_opts = NULL; |
| 5225 | QemuOpts *sn_opts = NULL; |
| 5226 | QemuOptsList *create_opts = NULL; |
| 5227 | bool image_opts = false; |
| 5228 | uint64_t img_size = UINT64_MAX; |
| 5229 | BlockMeasureInfo *info = NULL; |
| 5230 | Error *local_err = NULL; |
| 5231 | int ret = 1; |
| 5232 | int c; |
| 5233 | |
| 5234 | while ((c = getopt_long(argc, argv, "hf:O:o:l:U", |
| 5235 | long_options, NULL)) != -1) { |
| 5236 | switch (c) { |
| 5237 | case '?': |
| 5238 | case 'h': |
| 5239 | help(); |
| 5240 | break; |
| 5241 | case 'f': |
| 5242 | fmt = optarg; |
| 5243 | break; |
| 5244 | case 'O': |
| 5245 | out_fmt = optarg; |
| 5246 | break; |
| 5247 | case 'o': |
Markus Armbruster | 6d2b5cb | 2020-04-15 09:49:25 +0200 | [diff] [blame] | 5248 | if (accumulate_options(&options, optarg) < 0) { |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5249 | goto out; |
| 5250 | } |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5251 | break; |
| 5252 | case 'l': |
| 5253 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { |
| 5254 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
| 5255 | optarg, false); |
| 5256 | if (!sn_opts) { |
| 5257 | error_report("Failed in parsing snapshot param '%s'", |
| 5258 | optarg); |
| 5259 | goto out; |
| 5260 | } |
| 5261 | } else { |
| 5262 | snapshot_name = optarg; |
| 5263 | } |
| 5264 | break; |
| 5265 | case 'U': |
| 5266 | force_share = true; |
| 5267 | break; |
| 5268 | case OPTION_OBJECT: |
| 5269 | object_opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 5270 | optarg, true); |
| 5271 | if (!object_opts) { |
| 5272 | goto out; |
| 5273 | } |
| 5274 | break; |
| 5275 | case OPTION_IMAGE_OPTS: |
| 5276 | image_opts = true; |
| 5277 | break; |
| 5278 | case OPTION_OUTPUT: |
| 5279 | if (!strcmp(optarg, "json")) { |
| 5280 | output_format = OFORMAT_JSON; |
| 5281 | } else if (!strcmp(optarg, "human")) { |
| 5282 | output_format = OFORMAT_HUMAN; |
| 5283 | } else { |
| 5284 | error_report("--output must be used with human or json " |
| 5285 | "as argument."); |
| 5286 | goto out; |
| 5287 | } |
| 5288 | break; |
| 5289 | case OPTION_SIZE: |
| 5290 | { |
| 5291 | int64_t sval; |
| 5292 | |
Eyal Moscovici | 43d589b | 2020-05-13 16:36:26 +0300 | [diff] [blame] | 5293 | sval = cvtnum("image size", optarg); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5294 | if (sval < 0) { |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5295 | goto out; |
| 5296 | } |
| 5297 | img_size = (uint64_t)sval; |
| 5298 | } |
| 5299 | break; |
| 5300 | } |
| 5301 | } |
| 5302 | |
| 5303 | if (qemu_opts_foreach(&qemu_object_opts, |
| 5304 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 5305 | qemu_img_object_print_help, &error_fatal)) { |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5306 | goto out; |
| 5307 | } |
| 5308 | |
| 5309 | if (argc - optind > 1) { |
| 5310 | error_report("At most one filename argument is allowed."); |
| 5311 | goto out; |
| 5312 | } else if (argc - optind == 1) { |
| 5313 | filename = argv[optind]; |
| 5314 | } |
| 5315 | |
Stefan Hajnoczi | c3673dc | 2020-02-21 11:25:21 +0000 | [diff] [blame] | 5316 | if (!filename && (image_opts || fmt || snapshot_name || sn_opts)) { |
| 5317 | error_report("--image-opts, -f, and -l require a filename argument."); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5318 | goto out; |
| 5319 | } |
| 5320 | if (filename && img_size != UINT64_MAX) { |
| 5321 | error_report("--size N cannot be used together with a filename."); |
| 5322 | goto out; |
| 5323 | } |
| 5324 | if (!filename && img_size == UINT64_MAX) { |
| 5325 | error_report("Either --size N or one filename must be specified."); |
| 5326 | goto out; |
| 5327 | } |
| 5328 | |
| 5329 | if (filename) { |
| 5330 | in_blk = img_open(image_opts, filename, fmt, 0, |
| 5331 | false, false, force_share); |
| 5332 | if (!in_blk) { |
| 5333 | goto out; |
| 5334 | } |
| 5335 | |
| 5336 | if (sn_opts) { |
| 5337 | bdrv_snapshot_load_tmp(blk_bs(in_blk), |
| 5338 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), |
| 5339 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), |
| 5340 | &local_err); |
| 5341 | } else if (snapshot_name != NULL) { |
| 5342 | bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk), |
| 5343 | snapshot_name, &local_err); |
| 5344 | } |
| 5345 | if (local_err) { |
| 5346 | error_reportf_err(local_err, "Failed to load snapshot: "); |
| 5347 | goto out; |
| 5348 | } |
| 5349 | } |
| 5350 | |
| 5351 | drv = bdrv_find_format(out_fmt); |
| 5352 | if (!drv) { |
| 5353 | error_report("Unknown file format '%s'", out_fmt); |
| 5354 | goto out; |
| 5355 | } |
| 5356 | if (!drv->create_opts) { |
| 5357 | error_report("Format driver '%s' does not support image creation", |
| 5358 | drv->format_name); |
| 5359 | goto out; |
| 5360 | } |
| 5361 | |
| 5362 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 5363 | create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts); |
| 5364 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
| 5365 | if (options) { |
| 5366 | qemu_opts_do_parse(opts, options, NULL, &local_err); |
| 5367 | if (local_err) { |
| 5368 | error_report_err(local_err); |
| 5369 | error_report("Invalid options for file format '%s'", out_fmt); |
| 5370 | goto out; |
| 5371 | } |
| 5372 | } |
| 5373 | if (img_size != UINT64_MAX) { |
| 5374 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort); |
| 5375 | } |
| 5376 | |
| 5377 | info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err); |
| 5378 | if (local_err) { |
| 5379 | error_report_err(local_err); |
| 5380 | goto out; |
| 5381 | } |
| 5382 | |
| 5383 | if (output_format == OFORMAT_HUMAN) { |
| 5384 | printf("required size: %" PRIu64 "\n", info->required); |
| 5385 | printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated); |
Eric Blake | 5d72c68 | 2020-05-21 14:21:34 -0500 | [diff] [blame] | 5386 | if (info->has_bitmaps) { |
| 5387 | printf("bitmaps size: %" PRIu64 "\n", info->bitmaps); |
| 5388 | } |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 5389 | } else { |
| 5390 | dump_json_block_measure_info(info); |
| 5391 | } |
| 5392 | |
| 5393 | ret = 0; |
| 5394 | |
| 5395 | out: |
| 5396 | qapi_free_BlockMeasureInfo(info); |
| 5397 | qemu_opts_del(object_opts); |
| 5398 | qemu_opts_del(opts); |
| 5399 | qemu_opts_del(sn_opts); |
| 5400 | qemu_opts_free(create_opts); |
| 5401 | g_free(options); |
| 5402 | blk_unref(in_blk); |
| 5403 | return ret; |
| 5404 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 5405 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 5406 | static const img_cmd_t img_cmds[] = { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5407 | #define DEF(option, callback, arg_string) \ |
| 5408 | { option, callback }, |
| 5409 | #include "qemu-img-cmds.h" |
| 5410 | #undef DEF |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5411 | { NULL, NULL, }, |
| 5412 | }; |
| 5413 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5414 | int main(int argc, char **argv) |
| 5415 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 5416 | const img_cmd_t *cmd; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5417 | const char *cmdname; |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 5418 | Error *local_error = NULL; |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5419 | char *trace_file = NULL; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5420 | int c; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5421 | static const struct option long_options[] = { |
| 5422 | {"help", no_argument, 0, 'h'}, |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5423 | {"version", no_argument, 0, 'V'}, |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5424 | {"trace", required_argument, NULL, 'T'}, |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5425 | {0, 0, 0, 0} |
| 5426 | }; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5427 | |
MORITA Kazutaka | 526eda1 | 2013-07-23 17:30:11 +0900 | [diff] [blame] | 5428 | #ifdef CONFIG_POSIX |
| 5429 | signal(SIGPIPE, SIG_IGN); |
| 5430 | #endif |
| 5431 | |
Christophe Fergeau | f5852ef | 2019-01-31 17:46:14 +0100 | [diff] [blame] | 5432 | error_init(argv[0]); |
Daniel P. Berrange | fe4db84 | 2016-10-04 14:35:52 +0100 | [diff] [blame] | 5433 | module_call_init(MODULE_INIT_TRACE); |
Fam Zheng | 10f5bff | 2014-02-10 14:48:51 +0800 | [diff] [blame] | 5434 | qemu_init_exec_dir(argv[0]); |
Kevin Wolf | 53f76e5 | 2010-12-16 15:10:32 +0100 | [diff] [blame] | 5435 | |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 5436 | if (qemu_init_main_loop(&local_error)) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 5437 | error_report_err(local_error); |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 5438 | exit(EXIT_FAILURE); |
| 5439 | } |
| 5440 | |
Eduardo Habkost | e8f2d27 | 2016-05-12 11:10:04 -0300 | [diff] [blame] | 5441 | qcrypto_init(&error_fatal); |
Daniel P. Berrange | c229708 | 2016-04-06 12:12:06 +0100 | [diff] [blame] | 5442 | |
Daniel P. Berrange | 064097d | 2016-02-10 18:41:01 +0000 | [diff] [blame] | 5443 | module_call_init(MODULE_INIT_QOM); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5444 | bdrv_init(); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 5445 | if (argc < 2) { |
| 5446 | error_exit("Not enough arguments"); |
| 5447 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5448 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 5449 | qemu_add_opts(&qemu_object_opts); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 5450 | qemu_add_opts(&qemu_source_opts); |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5451 | qemu_add_opts(&qemu_trace_opts); |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 5452 | |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 5453 | while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) { |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5454 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 5455 | case ':': |
| 5456 | missing_argument(argv[optind - 1]); |
| 5457 | return 0; |
Stefan Hajnoczi | 4581c16 | 2017-03-17 18:45:39 +0800 | [diff] [blame] | 5458 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 5459 | unrecognized_option(argv[optind - 1]); |
| 5460 | return 0; |
| 5461 | case 'h': |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5462 | help(); |
| 5463 | return 0; |
| 5464 | case 'V': |
| 5465 | printf(QEMU_IMG_VERSION); |
| 5466 | return 0; |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5467 | case 'T': |
| 5468 | g_free(trace_file); |
| 5469 | trace_file = trace_opt_parse(optarg); |
| 5470 | break; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5471 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5472 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5473 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5474 | cmdname = argv[optind]; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5475 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5476 | /* reset getopt_long scanning */ |
| 5477 | argc -= optind; |
| 5478 | if (argc < 1) { |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 5479 | return 0; |
| 5480 | } |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5481 | argv += optind; |
Richard W.M. Jones | d339d76 | 2019-01-18 10:11:14 +0000 | [diff] [blame] | 5482 | qemu_reset_optind(); |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5483 | |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5484 | if (!trace_init_backends()) { |
| 5485 | exit(1); |
| 5486 | } |
| 5487 | trace_init_file(trace_file); |
| 5488 | qemu_set_log(LOG_TRACE); |
| 5489 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5490 | /* find the command */ |
| 5491 | for (cmd = img_cmds; cmd->name != NULL; cmd++) { |
| 5492 | if (!strcmp(cmdname, cmd->name)) { |
| 5493 | return cmd->handler(argc, argv); |
| 5494 | } |
| 5495 | } |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5496 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5497 | /* not found */ |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 5498 | error_exit("Command not found: %s", cmdname); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5499 | } |