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 | |
Fam Zheng | 67a1de0 | 2016-06-01 17:44:21 +0800 | [diff] [blame] | 28 | #include "qemu-version.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 29 | #include "qapi/error.h" |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 30 | #include "qapi/qapi-visit-block-core.h" |
Daniel P. Berrange | b3db211 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 31 | #include "qapi/qobject-output-visitor.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 32 | #include "qapi/qmp/qjson.h" |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 33 | #include "qapi/qmp/qdict.h" |
Markus Armbruster | fc81fa1 | 2018-02-01 12:18:40 +0100 | [diff] [blame] | 34 | #include "qapi/qmp/qstring.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 35 | #include "qemu/cutils.h" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 36 | #include "qemu/config-file.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 37 | #include "qemu/option.h" |
| 38 | #include "qemu/error-report.h" |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 39 | #include "qemu/log.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame^] | 40 | #include "qemu/module.h" |
Stefano Garzarella | 97ede57 | 2019-05-08 12:43:24 +0200 | [diff] [blame] | 41 | #include "qemu/units.h" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 42 | #include "qom/object_interfaces.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 43 | #include "sysemu/sysemu.h" |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 44 | #include "sysemu/block-backend.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 45 | #include "block/block_int.h" |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 46 | #include "block/blockjob.h" |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 47 | #include "block/qapi.h" |
Daniel P. Berrange | c229708 | 2016-04-06 12:12:06 +0100 | [diff] [blame] | 48 | #include "crypto/init.h" |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 49 | #include "trace/control.h" |
bellard | e844533 | 2006-06-14 15:32:10 +0000 | [diff] [blame] | 50 | |
Thomas Huth | 7e563bf | 2018-02-15 12:06:47 +0100 | [diff] [blame] | 51 | #define QEMU_IMG_VERSION "qemu-img version " QEMU_FULL_VERSION \ |
Thomas Huth | 0781dd6 | 2016-10-05 11:54:44 +0200 | [diff] [blame] | 52 | "\n" QEMU_COPYRIGHT "\n" |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 53 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 54 | typedef struct img_cmd_t { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 55 | const char *name; |
| 56 | int (*handler)(int argc, char **argv); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 57 | } img_cmd_t; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 58 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 59 | enum { |
| 60 | OPTION_OUTPUT = 256, |
| 61 | OPTION_BACKING_CHAIN = 257, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 62 | OPTION_OBJECT = 258, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 63 | OPTION_IMAGE_OPTS = 259, |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 64 | OPTION_PATTERN = 260, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 65 | OPTION_FLUSH_INTERVAL = 261, |
| 66 | OPTION_NO_DRAIN = 262, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 67 | OPTION_TARGET_IMAGE_OPTS = 263, |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 68 | OPTION_SIZE = 264, |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 69 | OPTION_PREALLOCATION = 265, |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 70 | OPTION_SHRINK = 266, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | typedef enum OutputFormat { |
| 74 | OFORMAT_JSON, |
| 75 | OFORMAT_HUMAN, |
| 76 | } OutputFormat; |
| 77 | |
Kevin Wolf | e699614 | 2016-03-15 13:03:11 +0100 | [diff] [blame] | 78 | /* 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] | 79 | #define BDRV_DEFAULT_CACHE "writeback" |
aurel32 | 137519c | 2008-11-30 19:12:49 +0000 | [diff] [blame] | 80 | |
Stefan Hajnoczi | 00c6d40 | 2014-08-27 12:08:56 +0100 | [diff] [blame] | 81 | static void format_print(void *opaque, const char *name) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 82 | { |
Stefan Hajnoczi | 00c6d40 | 2014-08-27 12:08:56 +0100 | [diff] [blame] | 83 | printf(" %s", name); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 86 | static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...) |
| 87 | { |
| 88 | va_list ap; |
| 89 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 90 | va_start(ap, fmt); |
Markus Armbruster | e9e1d92 | 2019-04-17 21:06:27 +0200 | [diff] [blame] | 91 | error_vreport(fmt, ap); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 92 | va_end(ap); |
| 93 | |
Markus Armbruster | e9e1d92 | 2019-04-17 21:06:27 +0200 | [diff] [blame] | 94 | error_printf("Try 'qemu-img --help' for more information\n"); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 95 | exit(EXIT_FAILURE); |
| 96 | } |
| 97 | |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 98 | static void QEMU_NORETURN missing_argument(const char *option) |
| 99 | { |
| 100 | error_exit("missing argument for option '%s'", option); |
| 101 | } |
| 102 | |
| 103 | static void QEMU_NORETURN unrecognized_option(const char *option) |
| 104 | { |
| 105 | error_exit("unrecognized option '%s'", option); |
| 106 | } |
| 107 | |
blueswir1 | d2c639d | 2009-01-24 18:19:25 +0000 | [diff] [blame] | 108 | /* Please keep in synch with qemu-img.texi */ |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 109 | static void QEMU_NORETURN help(void) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 110 | { |
Paolo Bonzini | e00291c | 2010-02-04 16:49:56 +0100 | [diff] [blame] | 111 | const char *help_msg = |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 112 | QEMU_IMG_VERSION |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 113 | "usage: qemu-img [standard options] command [command options]\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 114 | "QEMU disk image utility\n" |
| 115 | "\n" |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 116 | " '-h', '--help' display this help and exit\n" |
| 117 | " '-V', '--version' output version information and exit\n" |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 118 | " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n" |
| 119 | " specify tracing options\n" |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 120 | "\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 121 | "Command syntax:\n" |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 122 | #define DEF(option, callback, arg_string) \ |
| 123 | " " arg_string "\n" |
| 124 | #include "qemu-img-cmds.h" |
| 125 | #undef DEF |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 126 | "\n" |
| 127 | "Command parameters:\n" |
| 128 | " 'filename' is a disk image filename\n" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 129 | " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n" |
| 130 | " manual page for a description of the object properties. The most common\n" |
| 131 | " object type is a 'secret', which is used to supply passwords and/or\n" |
| 132 | " encryption keys.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 133 | " '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] | 134 | " '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] | 135 | " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n" |
| 136 | " 'directsync' and 'unsafe' (default for convert)\n" |
Stefan Hajnoczi | bb87fdf | 2014-09-02 11:01:02 +0100 | [diff] [blame] | 137 | " 'src_cache' is the cache mode used to read input disk images, the valid\n" |
| 138 | " options are the same as for the 'cache' option\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 139 | " 'size' is the disk image size in bytes. Optional suffixes\n" |
Kevin Wolf | 5e00984 | 2013-06-05 14:19:27 +0200 | [diff] [blame] | 140 | " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n" |
| 141 | " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n" |
| 142 | " supported. 'b' is ignored.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 143 | " 'output_filename' is the destination disk image filename\n" |
| 144 | " 'output_fmt' is the destination format\n" |
| 145 | " 'options' is a comma separated list of format specific options in a\n" |
| 146 | " name=value format. Use -o ? for an overview of the options supported by the\n" |
| 147 | " used format\n" |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 148 | " 'snapshot_param' is param used for internal snapshot, format\n" |
| 149 | " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n" |
| 150 | " '[ID_OR_NAME]'\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 151 | " '-c' indicates that target image must be compressed (qcow format only)\n" |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 152 | " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n" |
| 153 | " new backing file match exactly. The image doesn't need a working\n" |
| 154 | " backing file before rebasing in this case (useful for renaming the\n" |
| 155 | " backing file). For image creation, allow creating without attempting\n" |
| 156 | " to open the backing file.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 157 | " '-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] | 158 | " '-p' show progress of command (only certain commands)\n" |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 159 | " '-q' use Quiet mode - do not print any output (except errors)\n" |
Peter Lieven | 11b6699 | 2013-10-24 12:07:05 +0200 | [diff] [blame] | 160 | " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n" |
| 161 | " contain only zeros for qemu-img to create a sparse image during\n" |
| 162 | " conversion. If the number of bytes is 0, the source will not be scanned for\n" |
| 163 | " unallocated or zero sectors, and the destination image will always be\n" |
| 164 | " fully allocated\n" |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 165 | " '--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] | 166 | " '-n' skips the target volume creation (useful if the volume is created\n" |
| 167 | " prior to running qemu-img)\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 168 | "\n" |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 169 | "Parameters to check subcommand:\n" |
| 170 | " '-r' tries to repair any inconsistencies that are found during the check.\n" |
| 171 | " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n" |
| 172 | " 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] | 173 | " hiding corruption that has already occurred.\n" |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 174 | "\n" |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 175 | "Parameters to convert subcommand:\n" |
| 176 | " '-m' specifies how many coroutines work in parallel during the convert\n" |
| 177 | " process (defaults to 8)\n" |
| 178 | " '-W' allow to write to the target out of order rather than sequential\n" |
| 179 | "\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 180 | "Parameters to snapshot subcommand:\n" |
| 181 | " 'snapshot' is the name of the snapshot to create, apply or delete\n" |
| 182 | " '-a' applies a snapshot (revert disk to saved state)\n" |
| 183 | " '-c' creates a snapshot\n" |
| 184 | " '-d' deletes a snapshot\n" |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 185 | " '-l' lists all snapshots in the given image\n" |
| 186 | "\n" |
| 187 | "Parameters to compare subcommand:\n" |
| 188 | " '-f' first image format\n" |
| 189 | " '-F' second image format\n" |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 190 | " '-s' run in Strict mode - fail on different image size or sector allocation\n" |
| 191 | "\n" |
| 192 | "Parameters to dd subcommand:\n" |
| 193 | " 'bs=BYTES' read and write up to BYTES bytes at a time " |
| 194 | "(default: 512)\n" |
| 195 | " 'count=N' copy only N input blocks\n" |
| 196 | " 'if=FILE' read from FILE\n" |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 197 | " 'of=FILE' write to FILE\n" |
| 198 | " '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] | 199 | |
| 200 | printf("%s\nSupported formats:", help_msg); |
Andrey Shinkevich | 9ac404c | 2019-03-07 16:33:58 +0300 | [diff] [blame] | 201 | bdrv_iterate_format(format_print, NULL, false); |
Eric Blake | f5048cb | 2017-08-03 11:33:53 -0500 | [diff] [blame] | 202 | printf("\n\n" QEMU_HELP_BOTTOM "\n"); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 203 | exit(EXIT_SUCCESS); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 206 | static QemuOptsList qemu_object_opts = { |
| 207 | .name = "object", |
| 208 | .implied_opt_name = "qom-type", |
| 209 | .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head), |
| 210 | .desc = { |
| 211 | { } |
| 212 | }, |
| 213 | }; |
| 214 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 215 | static QemuOptsList qemu_source_opts = { |
| 216 | .name = "source", |
| 217 | .implied_opt_name = "file", |
| 218 | .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head), |
| 219 | .desc = { |
| 220 | { } |
| 221 | }, |
| 222 | }; |
| 223 | |
Stefan Weil | 7c30f65 | 2013-06-16 17:01:05 +0200 | [diff] [blame] | 224 | 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] | 225 | { |
| 226 | int ret = 0; |
| 227 | if (!quiet) { |
| 228 | va_list args; |
| 229 | va_start(args, fmt); |
| 230 | ret = vprintf(fmt, args); |
| 231 | va_end(args); |
| 232 | } |
| 233 | return ret; |
| 234 | } |
| 235 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 236 | |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 237 | static int print_block_option_help(const char *filename, const char *fmt) |
| 238 | { |
| 239 | BlockDriver *drv, *proto_drv; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 240 | QemuOptsList *create_opts = NULL; |
Max Reitz | b65a5e1 | 2015-02-05 13:58:12 -0500 | [diff] [blame] | 241 | Error *local_err = NULL; |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 242 | |
| 243 | /* Find driver and parse its options */ |
| 244 | drv = bdrv_find_format(fmt); |
| 245 | if (!drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 246 | error_report("Unknown file format '%s'", fmt); |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 247 | return 1; |
| 248 | } |
| 249 | |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 250 | if (!drv->create_opts) { |
| 251 | error_report("Format driver '%s' does not support image creation", fmt); |
| 252 | return 1; |
| 253 | } |
| 254 | |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 255 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 256 | if (filename) { |
Max Reitz | b65a5e1 | 2015-02-05 13:58:12 -0500 | [diff] [blame] | 257 | proto_drv = bdrv_find_protocol(filename, true, &local_err); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 258 | if (!proto_drv) { |
Markus Armbruster | 2867ce4 | 2015-03-12 16:08:02 +0100 | [diff] [blame] | 259 | error_report_err(local_err); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 260 | qemu_opts_free(create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 261 | return 1; |
| 262 | } |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 263 | if (!proto_drv->create_opts) { |
Max Reitz | f099887 | 2018-11-19 11:19:20 +0100 | [diff] [blame] | 264 | error_report("Protocol driver '%s' does not support image creation", |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 265 | proto_drv->format_name); |
Max Reitz | 3ecd5a4 | 2018-11-19 11:19:21 +0100 | [diff] [blame] | 266 | qemu_opts_free(create_opts); |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 267 | return 1; |
| 268 | } |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 269 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 270 | } |
| 271 | |
Max Reitz | f4619af | 2019-04-13 17:20:37 +0200 | [diff] [blame] | 272 | if (filename) { |
| 273 | printf("Supported options:\n"); |
| 274 | } else { |
| 275 | printf("Supported %s options:\n", fmt); |
| 276 | } |
Max Reitz | 6389871 | 2018-10-19 18:49:25 +0200 | [diff] [blame] | 277 | qemu_opts_print_help(create_opts, false); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 278 | qemu_opts_free(create_opts); |
Max Reitz | f4619af | 2019-04-13 17:20:37 +0200 | [diff] [blame] | 279 | |
| 280 | if (!filename) { |
| 281 | printf("\n" |
| 282 | "The protocol level may support further options.\n" |
| 283 | "Specify the target filename to include those options.\n"); |
| 284 | } |
| 285 | |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 286 | return 0; |
| 287 | } |
| 288 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 289 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 290 | static BlockBackend *img_open_opts(const char *optstr, |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 291 | QemuOpts *opts, int flags, bool writethrough, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 292 | bool quiet, bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 293 | { |
| 294 | QDict *options; |
| 295 | Error *local_err = NULL; |
| 296 | BlockBackend *blk; |
| 297 | options = qemu_opts_to_qdict(opts, NULL); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 298 | if (force_share) { |
| 299 | if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE) |
Max Reitz | 4615f87 | 2018-05-02 22:20:50 +0200 | [diff] [blame] | 300 | && strcmp(qdict_get_str(options, BDRV_OPT_FORCE_SHARE), "on")) { |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 301 | error_report("--force-share/-U conflicts with image options"); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 302 | qobject_unref(options); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 303 | return NULL; |
| 304 | } |
Max Reitz | 4615f87 | 2018-05-02 22:20:50 +0200 | [diff] [blame] | 305 | qdict_put_str(options, BDRV_OPT_FORCE_SHARE, "on"); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 306 | } |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 307 | blk = blk_new_open(NULL, NULL, options, flags, &local_err); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 308 | if (!blk) { |
Daniel P. Berrange | 143605a | 2016-04-06 10:16:18 +0100 | [diff] [blame] | 309 | error_reportf_err(local_err, "Could not open '%s': ", optstr); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 310 | return NULL; |
| 311 | } |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 312 | blk_set_enable_write_cache(blk, !writethrough); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 313 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 314 | return blk; |
| 315 | } |
| 316 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 317 | static BlockBackend *img_open_file(const char *filename, |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 318 | QDict *options, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 319 | const char *fmt, int flags, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 320 | bool writethrough, bool quiet, |
| 321 | bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 322 | { |
| 323 | BlockBackend *blk; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 324 | Error *local_err = NULL; |
Kevin Wolf | ad71713 | 2010-12-16 15:37:41 +0100 | [diff] [blame] | 325 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 326 | if (!options) { |
| 327 | options = qdict_new(); |
| 328 | } |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 329 | if (fmt) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 330 | qdict_put_str(options, "driver", fmt); |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 331 | } |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 332 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 333 | if (force_share) { |
Eric Blake | 579cf1d | 2017-05-15 14:54:39 -0500 | [diff] [blame] | 334 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 335 | } |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 336 | blk = blk_new_open(filename, NULL, options, flags, &local_err); |
Max Reitz | 5bd3132 | 2015-02-05 13:58:16 -0500 | [diff] [blame] | 337 | if (!blk) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 338 | error_reportf_err(local_err, "Could not open '%s': ", filename); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 339 | return NULL; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 340 | } |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 341 | blk_set_enable_write_cache(blk, !writethrough); |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 342 | |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 343 | return blk; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 346 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 347 | static int img_add_key_secrets(void *opaque, |
| 348 | const char *name, const char *value, |
| 349 | Error **errp) |
| 350 | { |
| 351 | QDict *options = opaque; |
| 352 | |
| 353 | if (g_str_has_suffix(name, "key-secret")) { |
Eric Blake | 187f47e | 2017-06-24 12:10:07 -0600 | [diff] [blame] | 354 | qdict_put_str(options, name, value); |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 360 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 361 | static BlockBackend *img_open(bool image_opts, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 362 | const char *filename, |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 363 | const char *fmt, int flags, bool writethrough, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 364 | bool quiet, bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 365 | { |
| 366 | BlockBackend *blk; |
| 367 | if (image_opts) { |
| 368 | QemuOpts *opts; |
| 369 | if (fmt) { |
| 370 | error_report("--image-opts and --format are mutually exclusive"); |
| 371 | return NULL; |
| 372 | } |
| 373 | opts = qemu_opts_parse_noisily(qemu_find_opts("source"), |
| 374 | filename, true); |
| 375 | if (!opts) { |
| 376 | return NULL; |
| 377 | } |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 378 | blk = img_open_opts(filename, opts, flags, writethrough, quiet, |
| 379 | force_share); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 380 | } else { |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 381 | blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 382 | force_share); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 383 | } |
| 384 | return blk; |
| 385 | } |
| 386 | |
| 387 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 388 | static int add_old_style_options(const char *fmt, QemuOpts *opts, |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 389 | const char *base_filename, |
| 390 | const char *base_fmt) |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 391 | { |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 392 | Error *err = NULL; |
| 393 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 394 | if (base_filename) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 395 | qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err); |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 396 | if (err) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 397 | error_report("Backing file not supported for file format '%s'", |
| 398 | fmt); |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 399 | error_free(err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 400 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | if (base_fmt) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 404 | qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err); |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 405 | if (err) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 406 | error_report("Backing file format not supported for file " |
| 407 | "format '%s'", fmt); |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 408 | error_free(err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 409 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 410 | } |
| 411 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 412 | return 0; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 413 | } |
| 414 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 415 | static int64_t cvtnum(const char *s) |
| 416 | { |
Markus Armbruster | f17fd4f | 2017-02-21 21:14:06 +0100 | [diff] [blame] | 417 | int err; |
Markus Armbruster | f46bfdb | 2017-02-21 21:14:07 +0100 | [diff] [blame] | 418 | uint64_t value; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 419 | |
Markus Armbruster | f17fd4f | 2017-02-21 21:14:06 +0100 | [diff] [blame] | 420 | err = qemu_strtosz(s, NULL, &value); |
| 421 | if (err < 0) { |
| 422 | return err; |
| 423 | } |
Markus Armbruster | f46bfdb | 2017-02-21 21:14:07 +0100 | [diff] [blame] | 424 | if (value > INT64_MAX) { |
| 425 | return -ERANGE; |
| 426 | } |
Markus Armbruster | f17fd4f | 2017-02-21 21:14:06 +0100 | [diff] [blame] | 427 | return value; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 428 | } |
| 429 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 430 | static int img_create(int argc, char **argv) |
| 431 | { |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 432 | int c; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 433 | uint64_t img_size = -1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 434 | const char *fmt = "raw"; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 435 | const char *base_fmt = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 436 | const char *filename; |
| 437 | const char *base_filename = NULL; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 438 | char *options = NULL; |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 439 | Error *local_err = NULL; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 440 | bool quiet = false; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 441 | int flags = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 442 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 443 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 444 | static const struct option long_options[] = { |
| 445 | {"help", no_argument, 0, 'h'}, |
| 446 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 447 | {0, 0, 0, 0} |
| 448 | }; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 449 | c = getopt_long(argc, argv, ":F:b:f:ho:qu", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 450 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 451 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 452 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 453 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 454 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 455 | case ':': |
| 456 | missing_argument(argv[optind - 1]); |
| 457 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 458 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 459 | unrecognized_option(argv[optind - 1]); |
| 460 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 461 | case 'h': |
| 462 | help(); |
| 463 | break; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 464 | case 'F': |
| 465 | base_fmt = optarg; |
| 466 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 467 | case 'b': |
| 468 | base_filename = optarg; |
| 469 | break; |
| 470 | case 'f': |
| 471 | fmt = optarg; |
| 472 | break; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 473 | case 'o': |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 474 | if (!is_valid_option_list(optarg)) { |
| 475 | error_report("Invalid option list: %s", optarg); |
| 476 | goto fail; |
| 477 | } |
| 478 | if (!options) { |
| 479 | options = g_strdup(optarg); |
| 480 | } else { |
| 481 | char *old_options = options; |
| 482 | options = g_strdup_printf("%s,%s", options, optarg); |
| 483 | g_free(old_options); |
| 484 | } |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 485 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 486 | case 'q': |
| 487 | quiet = true; |
| 488 | break; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 489 | case 'u': |
| 490 | flags |= BDRV_O_NO_BACKING; |
| 491 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 492 | case OPTION_OBJECT: { |
| 493 | QemuOpts *opts; |
| 494 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 495 | optarg, true); |
| 496 | if (!opts) { |
| 497 | goto fail; |
| 498 | } |
| 499 | } break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 500 | } |
| 501 | } |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 502 | |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 503 | /* Get the filename */ |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 504 | filename = (optind < argc) ? argv[optind] : NULL; |
| 505 | if (options && has_help_option(options)) { |
| 506 | g_free(options); |
| 507 | return print_block_option_help(filename, fmt); |
| 508 | } |
| 509 | |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 510 | if (optind >= argc) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 511 | error_exit("Expecting image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 512 | } |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 513 | optind++; |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 514 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 515 | if (qemu_opts_foreach(&qemu_object_opts, |
| 516 | user_creatable_add_opts_foreach, |
Daniel P. Berrangé | 334c43e | 2019-02-19 11:46:09 +0000 | [diff] [blame] | 517 | NULL, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 518 | goto fail; |
| 519 | } |
| 520 | |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 521 | /* Get image size, if specified */ |
| 522 | if (optind < argc) { |
Jes Sorensen | 70b4f4b | 2011-01-05 11:41:02 +0100 | [diff] [blame] | 523 | int64_t sval; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 524 | |
| 525 | sval = cvtnum(argv[optind++]); |
| 526 | if (sval < 0) { |
liguang | 7944339 | 2012-12-17 09:49:23 +0800 | [diff] [blame] | 527 | if (sval == -ERANGE) { |
| 528 | error_report("Image size must be less than 8 EiB!"); |
| 529 | } else { |
| 530 | error_report("Invalid image size specified! You may use k, M, " |
Kevin Wolf | 5e00984 | 2013-06-05 14:19:27 +0200 | [diff] [blame] | 531 | "G, T, P or E suffixes for "); |
| 532 | error_report("kilobytes, megabytes, gigabytes, terabytes, " |
| 533 | "petabytes and exabytes."); |
liguang | 7944339 | 2012-12-17 09:49:23 +0800 | [diff] [blame] | 534 | } |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 535 | goto fail; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 536 | } |
| 537 | img_size = (uint64_t)sval; |
| 538 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 539 | if (optind != argc) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 540 | error_exit("Unexpected argument: %s", argv[optind]); |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 541 | } |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 542 | |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 543 | bdrv_img_create(filename, fmt, base_filename, base_fmt, |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 544 | options, img_size, flags, quiet, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 545 | if (local_err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 546 | error_reportf_err(local_err, "%s: ", filename); |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 547 | goto fail; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 548 | } |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 549 | |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 550 | g_free(options); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 551 | return 0; |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 552 | |
| 553 | fail: |
| 554 | g_free(options); |
| 555 | return 1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 558 | static void dump_json_image_check(ImageCheck *check, bool quiet) |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 559 | { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 560 | QString *str; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 561 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 562 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 563 | |
| 564 | visit_type_ImageCheck(v, NULL, &check, &error_abort); |
| 565 | visit_complete(v, &obj); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 566 | str = qobject_to_json_pretty(obj); |
| 567 | assert(str != NULL); |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 568 | qprintf(quiet, "%s\n", qstring_get_str(str)); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 569 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 570 | visit_free(v); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 571 | qobject_unref(str); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 572 | } |
| 573 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 574 | static void dump_human_image_check(ImageCheck *check, bool quiet) |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 575 | { |
| 576 | if (!(check->corruptions || check->leaks || check->check_errors)) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 577 | qprintf(quiet, "No errors were found on the image.\n"); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 578 | } else { |
| 579 | if (check->corruptions) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 580 | qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n" |
| 581 | "Data may be corrupted, or further writes to the image " |
| 582 | "may corrupt it.\n", |
| 583 | check->corruptions); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | if (check->leaks) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 587 | qprintf(quiet, |
| 588 | "\n%" PRId64 " leaked clusters were found on the image.\n" |
| 589 | "This means waste of disk space, but no harm to data.\n", |
| 590 | check->leaks); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | if (check->check_errors) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 594 | qprintf(quiet, |
| 595 | "\n%" PRId64 |
| 596 | " internal errors have occurred during the check.\n", |
| 597 | check->check_errors); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | |
| 601 | if (check->total_clusters != 0 && check->allocated_clusters != 0) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 602 | qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, " |
| 603 | "%0.2f%% fragmented, %0.2f%% compressed clusters\n", |
| 604 | check->allocated_clusters, check->total_clusters, |
| 605 | check->allocated_clusters * 100.0 / check->total_clusters, |
| 606 | check->fragmented_clusters * 100.0 / check->allocated_clusters, |
| 607 | check->compressed_clusters * 100.0 / |
| 608 | check->allocated_clusters); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | if (check->image_end_offset) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 612 | qprintf(quiet, |
| 613 | "Image end offset: %" PRId64 "\n", check->image_end_offset); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | |
| 617 | static int collect_image_check(BlockDriverState *bs, |
| 618 | ImageCheck *check, |
| 619 | const char *filename, |
| 620 | const char *fmt, |
| 621 | int fix) |
| 622 | { |
| 623 | int ret; |
| 624 | BdrvCheckResult result; |
| 625 | |
| 626 | ret = bdrv_check(bs, &result, fix); |
| 627 | if (ret < 0) { |
| 628 | return ret; |
| 629 | } |
| 630 | |
| 631 | check->filename = g_strdup(filename); |
| 632 | check->format = g_strdup(bdrv_get_format_name(bs)); |
| 633 | check->check_errors = result.check_errors; |
| 634 | check->corruptions = result.corruptions; |
| 635 | check->has_corruptions = result.corruptions != 0; |
| 636 | check->leaks = result.leaks; |
| 637 | check->has_leaks = result.leaks != 0; |
| 638 | check->corruptions_fixed = result.corruptions_fixed; |
| 639 | check->has_corruptions_fixed = result.corruptions != 0; |
| 640 | check->leaks_fixed = result.leaks_fixed; |
| 641 | check->has_leaks_fixed = result.leaks != 0; |
| 642 | check->image_end_offset = result.image_end_offset; |
| 643 | check->has_image_end_offset = result.image_end_offset != 0; |
| 644 | check->total_clusters = result.bfi.total_clusters; |
| 645 | check->has_total_clusters = result.bfi.total_clusters != 0; |
| 646 | check->allocated_clusters = result.bfi.allocated_clusters; |
| 647 | check->has_allocated_clusters = result.bfi.allocated_clusters != 0; |
| 648 | check->fragmented_clusters = result.bfi.fragmented_clusters; |
| 649 | check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0; |
Stefan Hajnoczi | e6439d7 | 2013-02-07 17:15:04 +0100 | [diff] [blame] | 650 | check->compressed_clusters = result.bfi.compressed_clusters; |
| 651 | check->has_compressed_clusters = result.bfi.compressed_clusters != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 652 | |
| 653 | return 0; |
| 654 | } |
| 655 | |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 656 | /* |
| 657 | * Checks an image for consistency. Exit codes: |
| 658 | * |
Max Reitz | d6635c4 | 2014-06-02 22:15:21 +0200 | [diff] [blame] | 659 | * 0 - Check completed, image is good |
| 660 | * 1 - Check not completed because of internal errors |
| 661 | * 2 - Check completed, image is corrupted |
| 662 | * 3 - Check completed, image has leaked clusters, but is good otherwise |
| 663 | * 63 - Checks are not supported by the image format |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 664 | */ |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 665 | static int img_check(int argc, char **argv) |
| 666 | { |
| 667 | int c, ret; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 668 | OutputFormat output_format = OFORMAT_HUMAN; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 669 | const char *filename, *fmt, *output, *cache; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 670 | BlockBackend *blk; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 671 | BlockDriverState *bs; |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 672 | int fix = 0; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 673 | int flags = BDRV_O_CHECK; |
| 674 | bool writethrough; |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 675 | ImageCheck *check; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 676 | bool quiet = false; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 677 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 678 | bool force_share = false; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 679 | |
| 680 | fmt = NULL; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 681 | output = NULL; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 682 | cache = BDRV_DEFAULT_CACHE; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 683 | |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 684 | for(;;) { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 685 | int option_index = 0; |
| 686 | static const struct option long_options[] = { |
| 687 | {"help", no_argument, 0, 'h'}, |
| 688 | {"format", required_argument, 0, 'f'}, |
Prasad Joshi | 4fd6a98 | 2014-03-25 00:08:54 +0530 | [diff] [blame] | 689 | {"repair", required_argument, 0, 'r'}, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 690 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 691 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 692 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 693 | {"force-share", no_argument, 0, 'U'}, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 694 | {0, 0, 0, 0} |
| 695 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 696 | c = getopt_long(argc, argv, ":hf:r:T:qU", |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 697 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 698 | if (c == -1) { |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 699 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 700 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 701 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 702 | case ':': |
| 703 | missing_argument(argv[optind - 1]); |
| 704 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 705 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 706 | unrecognized_option(argv[optind - 1]); |
| 707 | break; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 708 | case 'h': |
| 709 | help(); |
| 710 | break; |
| 711 | case 'f': |
| 712 | fmt = optarg; |
| 713 | break; |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 714 | case 'r': |
| 715 | flags |= BDRV_O_RDWR; |
| 716 | |
| 717 | if (!strcmp(optarg, "leaks")) { |
| 718 | fix = BDRV_FIX_LEAKS; |
| 719 | } else if (!strcmp(optarg, "all")) { |
| 720 | fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS; |
| 721 | } else { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 722 | error_exit("Unknown option value for -r " |
| 723 | "(expecting 'leaks' or 'all'): %s", optarg); |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 724 | } |
| 725 | break; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 726 | case OPTION_OUTPUT: |
| 727 | output = optarg; |
| 728 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 729 | case 'T': |
| 730 | cache = optarg; |
| 731 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 732 | case 'q': |
| 733 | quiet = true; |
| 734 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 735 | case 'U': |
| 736 | force_share = true; |
| 737 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 738 | case OPTION_OBJECT: { |
| 739 | QemuOpts *opts; |
| 740 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 741 | optarg, true); |
| 742 | if (!opts) { |
| 743 | return 1; |
| 744 | } |
| 745 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 746 | case OPTION_IMAGE_OPTS: |
| 747 | image_opts = true; |
| 748 | break; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 749 | } |
| 750 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 751 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 752 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 753 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 754 | filename = argv[optind++]; |
| 755 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 756 | if (output && !strcmp(output, "json")) { |
| 757 | output_format = OFORMAT_JSON; |
| 758 | } else if (output && !strcmp(output, "human")) { |
| 759 | output_format = OFORMAT_HUMAN; |
| 760 | } else if (output) { |
| 761 | error_report("--output must be used with human or json as argument."); |
| 762 | return 1; |
| 763 | } |
| 764 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 765 | if (qemu_opts_foreach(&qemu_object_opts, |
| 766 | user_creatable_add_opts_foreach, |
Daniel P. Berrangé | 334c43e | 2019-02-19 11:46:09 +0000 | [diff] [blame] | 767 | NULL, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 768 | return 1; |
| 769 | } |
| 770 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 771 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 772 | if (ret < 0) { |
| 773 | error_report("Invalid source cache option: %s", cache); |
| 774 | return 1; |
| 775 | } |
| 776 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 777 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 778 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 779 | if (!blk) { |
| 780 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 781 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 782 | bs = blk_bs(blk); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 783 | |
| 784 | check = g_new0(ImageCheck, 1); |
| 785 | ret = collect_image_check(bs, check, filename, fmt, fix); |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 786 | |
| 787 | if (ret == -ENOTSUP) { |
Max Reitz | 55d492d | 2014-05-31 21:33:30 +0200 | [diff] [blame] | 788 | error_report("This image format does not support checks"); |
Peter Lieven | fefddf9 | 2013-10-24 08:53:34 +0200 | [diff] [blame] | 789 | ret = 63; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 790 | goto fail; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 791 | } |
| 792 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 793 | if (check->corruptions_fixed || check->leaks_fixed) { |
| 794 | int corruptions_fixed, leaks_fixed; |
| 795 | |
| 796 | leaks_fixed = check->leaks_fixed; |
| 797 | corruptions_fixed = check->corruptions_fixed; |
| 798 | |
| 799 | if (output_format == OFORMAT_HUMAN) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 800 | qprintf(quiet, |
| 801 | "The following inconsistencies were found and repaired:\n\n" |
| 802 | " %" PRId64 " leaked clusters\n" |
| 803 | " %" PRId64 " corruptions\n\n" |
| 804 | "Double checking the fixed image now...\n", |
| 805 | check->leaks_fixed, |
| 806 | check->corruptions_fixed); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | ret = collect_image_check(bs, check, filename, fmt, 0); |
| 810 | |
| 811 | check->leaks_fixed = leaks_fixed; |
| 812 | check->corruptions_fixed = corruptions_fixed; |
Kevin Wolf | ccf3471 | 2012-05-11 18:16:54 +0200 | [diff] [blame] | 813 | } |
| 814 | |
Max Reitz | 832390a | 2014-10-23 15:29:12 +0200 | [diff] [blame] | 815 | if (!ret) { |
| 816 | switch (output_format) { |
| 817 | case OFORMAT_HUMAN: |
| 818 | dump_human_image_check(check, quiet); |
| 819 | break; |
| 820 | case OFORMAT_JSON: |
| 821 | dump_json_image_check(check, quiet); |
| 822 | break; |
| 823 | } |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | if (ret || check->check_errors) { |
Max Reitz | 832390a | 2014-10-23 15:29:12 +0200 | [diff] [blame] | 827 | if (ret) { |
| 828 | error_report("Check failed: %s", strerror(-ret)); |
| 829 | } else { |
| 830 | error_report("Check failed"); |
| 831 | } |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 832 | ret = 1; |
| 833 | goto fail; |
| 834 | } |
| 835 | |
| 836 | if (check->corruptions) { |
| 837 | ret = 2; |
| 838 | } else if (check->leaks) { |
| 839 | ret = 3; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 840 | } else { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 841 | ret = 0; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 844 | fail: |
| 845 | qapi_free_ImageCheck(check); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 846 | blk_unref(blk); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 847 | return ret; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 850 | typedef struct CommonBlockJobCBInfo { |
| 851 | BlockDriverState *bs; |
| 852 | Error **errp; |
| 853 | } CommonBlockJobCBInfo; |
| 854 | |
| 855 | static void common_block_job_cb(void *opaque, int ret) |
| 856 | { |
| 857 | CommonBlockJobCBInfo *cbi = opaque; |
| 858 | |
| 859 | if (ret < 0) { |
| 860 | error_setg_errno(cbi->errp, -ret, "Block job failed"); |
| 861 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | static void run_block_job(BlockJob *job, Error **errp) |
| 865 | { |
Kevin Wolf | b75536c | 2016-04-18 17:30:17 +0200 | [diff] [blame] | 866 | AioContext *aio_context = blk_get_aio_context(job->blk); |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 867 | int ret = 0; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 868 | |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 869 | aio_context_acquire(aio_context); |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 870 | job_ref(&job->job); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 871 | do { |
Kevin Wolf | 30a5c88 | 2018-05-04 12:17:20 +0200 | [diff] [blame] | 872 | float progress = 0.0f; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 873 | aio_poll(aio_context, true); |
Kevin Wolf | 30a5c88 | 2018-05-04 12:17:20 +0200 | [diff] [blame] | 874 | if (job->job.progress_total) { |
| 875 | progress = (float)job->job.progress_current / |
| 876 | job->job.progress_total * 100.f; |
| 877 | } |
| 878 | qemu_progress_print(progress, 0); |
Kevin Wolf | df956ae | 2018-04-25 15:09:58 +0200 | [diff] [blame] | 879 | } while (!job_is_ready(&job->job) && !job_is_completed(&job->job)); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 880 | |
Kevin Wolf | dbe5e6c | 2018-04-19 13:04:01 +0200 | [diff] [blame] | 881 | if (!job_is_completed(&job->job)) { |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 882 | ret = job_complete_sync(&job->job, errp); |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 883 | } else { |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 884 | ret = job->job.ret; |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 885 | } |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 886 | job_unref(&job->job); |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 887 | aio_context_release(aio_context); |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 888 | |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 889 | /* publish completion progress only when success */ |
| 890 | if (!ret) { |
| 891 | qemu_progress_print(100.f, 0); |
| 892 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 893 | } |
| 894 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 895 | static int img_commit(int argc, char **argv) |
| 896 | { |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 897 | int c, ret, flags; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 898 | const char *filename, *fmt, *cache, *base; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 899 | BlockBackend *blk; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 900 | BlockDriverState *bs, *base_bs; |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 901 | BlockJob *job; |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 902 | bool progress = false, quiet = false, drop = false; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 903 | bool writethrough; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 904 | Error *local_err = NULL; |
| 905 | CommonBlockJobCBInfo cbi; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 906 | bool image_opts = false; |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 907 | AioContext *aio_context; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 908 | |
| 909 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 910 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 911 | base = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 912 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 913 | static const struct option long_options[] = { |
| 914 | {"help", no_argument, 0, 'h'}, |
| 915 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 916 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 917 | {0, 0, 0, 0} |
| 918 | }; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 919 | c = getopt_long(argc, argv, ":f:ht:b:dpq", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 920 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 921 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 922 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 923 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 924 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 925 | case ':': |
| 926 | missing_argument(argv[optind - 1]); |
| 927 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 928 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 929 | unrecognized_option(argv[optind - 1]); |
| 930 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 931 | case 'h': |
| 932 | help(); |
| 933 | break; |
| 934 | case 'f': |
| 935 | fmt = optarg; |
| 936 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 937 | case 't': |
| 938 | cache = optarg; |
| 939 | break; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 940 | case 'b': |
| 941 | base = optarg; |
| 942 | /* -b implies -d */ |
| 943 | drop = true; |
| 944 | break; |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 945 | case 'd': |
| 946 | drop = true; |
| 947 | break; |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 948 | case 'p': |
| 949 | progress = true; |
| 950 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 951 | case 'q': |
| 952 | quiet = true; |
| 953 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 954 | case OPTION_OBJECT: { |
| 955 | QemuOpts *opts; |
| 956 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 957 | optarg, true); |
| 958 | if (!opts) { |
| 959 | return 1; |
| 960 | } |
| 961 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 962 | case OPTION_IMAGE_OPTS: |
| 963 | image_opts = true; |
| 964 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 965 | } |
| 966 | } |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 967 | |
| 968 | /* Progress is not shown in Quiet mode */ |
| 969 | if (quiet) { |
| 970 | progress = false; |
| 971 | } |
| 972 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 973 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 974 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 975 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 976 | filename = argv[optind++]; |
| 977 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 978 | if (qemu_opts_foreach(&qemu_object_opts, |
| 979 | user_creatable_add_opts_foreach, |
Daniel P. Berrangé | 334c43e | 2019-02-19 11:46:09 +0000 | [diff] [blame] | 980 | NULL, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 981 | return 1; |
| 982 | } |
| 983 | |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 984 | flags = BDRV_O_RDWR | BDRV_O_UNMAP; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 985 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 986 | if (ret < 0) { |
| 987 | error_report("Invalid cache option: %s", cache); |
Stefan Hajnoczi | a3981eb | 2014-08-26 19:17:54 +0100 | [diff] [blame] | 988 | return 1; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 989 | } |
| 990 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 991 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 992 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 993 | if (!blk) { |
| 994 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 995 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 996 | bs = blk_bs(blk); |
| 997 | |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 998 | qemu_progress_init(progress, 1.f); |
| 999 | qemu_progress_print(0.f, 100); |
| 1000 | |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1001 | if (base) { |
| 1002 | base_bs = bdrv_find_backing_image(bs, base); |
| 1003 | if (!base_bs) { |
Max Reitz | 6b33f3a | 2016-12-01 03:05:08 +0100 | [diff] [blame] | 1004 | error_setg(&local_err, |
| 1005 | "Did not find '%s' in the backing chain of '%s'", |
| 1006 | base, filename); |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1007 | goto done; |
| 1008 | } |
| 1009 | } else { |
| 1010 | /* This is different from QMP, which by default uses the deepest file in |
| 1011 | * the backing chain (i.e., the very base); however, the traditional |
| 1012 | * behavior of qemu-img commit is using the immediate backing file. */ |
Kevin Wolf | 760e006 | 2015-06-17 14:55:21 +0200 | [diff] [blame] | 1013 | base_bs = backing_bs(bs); |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1014 | if (!base_bs) { |
| 1015 | error_setg(&local_err, "Image does not have a backing file"); |
| 1016 | goto done; |
| 1017 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1020 | cbi = (CommonBlockJobCBInfo){ |
| 1021 | .errp = &local_err, |
| 1022 | .bs = bs, |
| 1023 | }; |
| 1024 | |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 1025 | aio_context = bdrv_get_aio_context(bs); |
| 1026 | aio_context_acquire(aio_context); |
Kevin Wolf | bb02b65 | 2018-04-19 17:54:56 +0200 | [diff] [blame] | 1027 | commit_active_start("commit", bs, base_bs, JOB_DEFAULT, 0, |
Kevin Wolf | 0db832f | 2017-02-20 18:10:05 +0100 | [diff] [blame] | 1028 | BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb, |
Fam Zheng | 78bbd91 | 2017-04-21 20:27:04 +0800 | [diff] [blame] | 1029 | &cbi, false, &local_err); |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 1030 | aio_context_release(aio_context); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1031 | if (local_err) { |
| 1032 | goto done; |
| 1033 | } |
| 1034 | |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1035 | /* When the block job completes, the BlockBackend reference will point to |
| 1036 | * the old backing file. In order to avoid that the top image is already |
| 1037 | * deleted, so we can still empty it afterwards, increment the reference |
| 1038 | * counter here preemptively. */ |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1039 | if (!drop) { |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1040 | bdrv_ref(bs); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1041 | } |
| 1042 | |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 1043 | job = block_job_get("commit"); |
Liam Merwick | 2e2db26 | 2018-11-05 21:38:37 +0000 | [diff] [blame] | 1044 | assert(job); |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 1045 | run_block_job(job, &local_err); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1046 | if (local_err) { |
| 1047 | goto unref_backing; |
| 1048 | } |
| 1049 | |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1050 | if (!drop && bs->drv->bdrv_make_empty) { |
| 1051 | ret = bs->drv->bdrv_make_empty(bs); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1052 | if (ret) { |
| 1053 | error_setg_errno(&local_err, -ret, "Could not empty %s", |
| 1054 | filename); |
| 1055 | goto unref_backing; |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | unref_backing: |
| 1060 | if (!drop) { |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1061 | bdrv_unref(bs); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1062 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1063 | |
| 1064 | done: |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 1065 | qemu_progress_end(); |
| 1066 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1067 | blk_unref(blk); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1068 | |
| 1069 | if (local_err) { |
Markus Armbruster | 6936f29 | 2015-02-10 15:14:02 +0100 | [diff] [blame] | 1070 | error_report_err(local_err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1071 | return 1; |
| 1072 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1073 | |
| 1074 | qprintf(quiet, "Image committed.\n"); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1075 | return 0; |
| 1076 | } |
| 1077 | |
Dmitry Konishchev | f6a00aa | 2011-05-18 15:03:59 +0400 | [diff] [blame] | 1078 | /* |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1079 | * Returns -1 if 'buf' contains only zeroes, otherwise the byte index |
| 1080 | * of the first sector boundary within buf where the sector contains a |
| 1081 | * non-zero byte. This function is robust to a buffer that is not |
| 1082 | * sector-aligned. |
| 1083 | */ |
| 1084 | static int64_t find_nonzero(const uint8_t *buf, int64_t n) |
| 1085 | { |
| 1086 | int64_t i; |
| 1087 | int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE); |
| 1088 | |
| 1089 | for (i = 0; i < end; i += BDRV_SECTOR_SIZE) { |
| 1090 | if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) { |
| 1091 | return i; |
| 1092 | } |
| 1093 | } |
| 1094 | if (i < n && !buffer_is_zero(buf + i, n - end)) { |
| 1095 | return i; |
| 1096 | } |
| 1097 | return -1; |
| 1098 | } |
| 1099 | |
| 1100 | /* |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1101 | * Returns true iff the first sector pointed to by 'buf' contains at least |
| 1102 | * a non-NUL byte. |
| 1103 | * |
| 1104 | * 'pnum' is set to the number of sectors (including and immediately following |
| 1105 | * 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] | 1106 | * The function will try to align the end offset to alignment boundaries so |
| 1107 | * that the request will at least end aligned and consequtive requests will |
| 1108 | * also start at an aligned offset. |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1109 | */ |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1110 | static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum, |
| 1111 | int64_t sector_num, int alignment) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1112 | { |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1113 | bool is_zero; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1114 | int i, tail; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1115 | |
| 1116 | if (n <= 0) { |
| 1117 | *pnum = 0; |
| 1118 | return 0; |
| 1119 | } |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1120 | is_zero = buffer_is_zero(buf, 512); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1121 | for(i = 1; i < n; i++) { |
| 1122 | buf += 512; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1123 | if (is_zero != buffer_is_zero(buf, 512)) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1124 | break; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1125 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1126 | } |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1127 | |
| 1128 | tail = (sector_num + i) & (alignment - 1); |
| 1129 | if (tail) { |
| 1130 | if (is_zero && i <= tail) { |
| 1131 | /* treat unallocated areas which only consist |
| 1132 | * of a small tail as allocated. */ |
| 1133 | is_zero = false; |
| 1134 | } |
| 1135 | if (!is_zero) { |
| 1136 | /* align up end offset of allocated areas. */ |
| 1137 | i += alignment - tail; |
| 1138 | i = MIN(i, n); |
| 1139 | } else { |
| 1140 | /* align down end offset of zero areas. */ |
| 1141 | i -= tail; |
| 1142 | } |
| 1143 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1144 | *pnum = i; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1145 | return !is_zero; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1148 | /* |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1149 | * Like is_allocated_sectors, but if the buffer starts with a used sector, |
| 1150 | * up to 'min' consecutive sectors containing zeros are ignored. This avoids |
| 1151 | * breaking up write requests for only small sparse areas. |
| 1152 | */ |
| 1153 | 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] | 1154 | int min, int64_t sector_num, int alignment) |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1155 | { |
| 1156 | int ret; |
| 1157 | int num_checked, num_used; |
| 1158 | |
| 1159 | if (n < min) { |
| 1160 | min = n; |
| 1161 | } |
| 1162 | |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1163 | ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment); |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1164 | if (!ret) { |
| 1165 | return ret; |
| 1166 | } |
| 1167 | |
| 1168 | num_used = *pnum; |
| 1169 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 1170 | n -= *pnum; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1171 | sector_num += *pnum; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1172 | num_checked = num_used; |
| 1173 | |
| 1174 | while (n > 0) { |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1175 | ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment); |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1176 | |
| 1177 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 1178 | n -= *pnum; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1179 | sector_num += *pnum; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1180 | num_checked += *pnum; |
| 1181 | if (ret) { |
| 1182 | num_used = num_checked; |
| 1183 | } else if (*pnum >= min) { |
| 1184 | break; |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | *pnum = num_used; |
| 1189 | return 1; |
| 1190 | } |
| 1191 | |
| 1192 | /* |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1193 | * Compares two buffers sector by sector. Returns 0 if the first |
| 1194 | * sector of each buffer matches, non-zero otherwise. |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1195 | * |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1196 | * pnum is set to the sector-aligned size of the buffer prefix that |
| 1197 | * has the same matching status as the first sector. |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1198 | */ |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1199 | static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2, |
| 1200 | int64_t bytes, int64_t *pnum) |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1201 | { |
Radim Krčmář | 8c1ac47 | 2015-02-20 17:06:15 +0100 | [diff] [blame] | 1202 | bool res; |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1203 | int64_t i = MIN(bytes, BDRV_SECTOR_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1204 | |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1205 | assert(bytes > 0); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1206 | |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1207 | res = !!memcmp(buf1, buf2, i); |
| 1208 | while (i < bytes) { |
| 1209 | int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1210 | |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1211 | if (!!memcmp(buf1 + i, buf2 + i, len) != res) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1212 | break; |
| 1213 | } |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1214 | i += len; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | *pnum = i; |
| 1218 | return res; |
| 1219 | } |
| 1220 | |
Stefano Garzarella | 97ede57 | 2019-05-08 12:43:24 +0200 | [diff] [blame] | 1221 | #define IO_BUF_SIZE (2 * MiB) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1222 | |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1223 | /* |
| 1224 | * Check if passed sectors are empty (not allocated or contain only 0 bytes) |
| 1225 | * |
Eric Blake | 0608e40 | 2017-10-11 22:47:12 -0500 | [diff] [blame] | 1226 | * Intended for use by 'qemu-img compare': Returns 0 in case sectors are |
| 1227 | * filled with 0, 1 if sectors contain non-zero data (this is a comparison |
| 1228 | * failure), and 4 on error (the exit status for read errors), after emitting |
| 1229 | * an error message. |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1230 | * |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1231 | * @param blk: BlockBackend for the image |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1232 | * @param offset: Starting offset to check |
| 1233 | * @param bytes: Number of bytes to check |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1234 | * @param filename: Name of disk file we are checking (logging purpose) |
| 1235 | * @param buffer: Allocated buffer for storing read data |
| 1236 | * @param quiet: Flag for quiet mode |
| 1237 | */ |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1238 | static int check_empty_sectors(BlockBackend *blk, int64_t offset, |
| 1239 | int64_t bytes, const char *filename, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1240 | uint8_t *buffer, bool quiet) |
| 1241 | { |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1242 | int ret = 0; |
| 1243 | int64_t idx; |
| 1244 | |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1245 | ret = blk_pread(blk, offset, buffer, bytes); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1246 | if (ret < 0) { |
| 1247 | error_report("Error while reading offset %" PRId64 " of %s: %s", |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1248 | offset, filename, strerror(-ret)); |
Eric Blake | 0608e40 | 2017-10-11 22:47:12 -0500 | [diff] [blame] | 1249 | return 4; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1250 | } |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1251 | idx = find_nonzero(buffer, bytes); |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1252 | if (idx >= 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1253 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1254 | offset + idx); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1255 | return 1; |
| 1256 | } |
| 1257 | |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
| 1261 | /* |
| 1262 | * Compares two images. Exit codes: |
| 1263 | * |
| 1264 | * 0 - Images are identical |
| 1265 | * 1 - Images differ |
| 1266 | * >1 - Error occurred |
| 1267 | */ |
| 1268 | static int img_compare(int argc, char **argv) |
| 1269 | { |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1270 | const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1271 | BlockBackend *blk1, *blk2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1272 | BlockDriverState *bs1, *bs2; |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1273 | int64_t total_size1, total_size2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1274 | uint8_t *buf1 = NULL, *buf2 = NULL; |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1275 | int64_t pnum1, pnum2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1276 | int allocated1, allocated2; |
| 1277 | int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */ |
| 1278 | bool progress = false, quiet = false, strict = false; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1279 | int flags; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1280 | bool writethrough; |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1281 | int64_t total_size; |
| 1282 | int64_t offset = 0; |
| 1283 | int64_t chunk; |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1284 | int c; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1285 | uint64_t progress_base; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1286 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1287 | bool force_share = false; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1288 | |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1289 | cache = BDRV_DEFAULT_CACHE; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1290 | for (;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1291 | static const struct option long_options[] = { |
| 1292 | {"help", no_argument, 0, 'h'}, |
| 1293 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1294 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1295 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1296 | {0, 0, 0, 0} |
| 1297 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1298 | c = getopt_long(argc, argv, ":hf:F:T:pqsU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1299 | long_options, NULL); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1300 | if (c == -1) { |
| 1301 | break; |
| 1302 | } |
| 1303 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 1304 | case ':': |
| 1305 | missing_argument(argv[optind - 1]); |
| 1306 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1307 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 1308 | unrecognized_option(argv[optind - 1]); |
| 1309 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1310 | case 'h': |
| 1311 | help(); |
| 1312 | break; |
| 1313 | case 'f': |
| 1314 | fmt1 = optarg; |
| 1315 | break; |
| 1316 | case 'F': |
| 1317 | fmt2 = optarg; |
| 1318 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1319 | case 'T': |
| 1320 | cache = optarg; |
| 1321 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1322 | case 'p': |
| 1323 | progress = true; |
| 1324 | break; |
| 1325 | case 'q': |
| 1326 | quiet = true; |
| 1327 | break; |
| 1328 | case 's': |
| 1329 | strict = true; |
| 1330 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1331 | case 'U': |
| 1332 | force_share = true; |
| 1333 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1334 | case OPTION_OBJECT: { |
| 1335 | QemuOpts *opts; |
| 1336 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 1337 | optarg, true); |
| 1338 | if (!opts) { |
| 1339 | ret = 2; |
| 1340 | goto out4; |
| 1341 | } |
| 1342 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1343 | case OPTION_IMAGE_OPTS: |
| 1344 | image_opts = true; |
| 1345 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | /* Progress is not shown in Quiet mode */ |
| 1350 | if (quiet) { |
| 1351 | progress = false; |
| 1352 | } |
| 1353 | |
| 1354 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 1355 | if (optind != argc - 2) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 1356 | error_exit("Expecting two image file names"); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1357 | } |
| 1358 | filename1 = argv[optind++]; |
| 1359 | filename2 = argv[optind++]; |
| 1360 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1361 | if (qemu_opts_foreach(&qemu_object_opts, |
| 1362 | user_creatable_add_opts_foreach, |
Daniel P. Berrangé | 334c43e | 2019-02-19 11:46:09 +0000 | [diff] [blame] | 1363 | NULL, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1364 | ret = 2; |
| 1365 | goto out4; |
| 1366 | } |
| 1367 | |
Stefan Hajnoczi | cbda016 | 2014-08-26 19:17:55 +0100 | [diff] [blame] | 1368 | /* Initialize before goto out */ |
| 1369 | qemu_progress_init(progress, 2.0); |
| 1370 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1371 | flags = 0; |
| 1372 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1373 | if (ret < 0) { |
| 1374 | error_report("Invalid source cache option: %s", cache); |
| 1375 | ret = 2; |
| 1376 | goto out3; |
| 1377 | } |
| 1378 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1379 | blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet, |
| 1380 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1381 | if (!blk1) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1382 | ret = 2; |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1383 | goto out3; |
| 1384 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1385 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1386 | blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet, |
| 1387 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1388 | if (!blk2) { |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1389 | ret = 2; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1390 | goto out2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1391 | } |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1392 | bs1 = blk_bs(blk1); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1393 | bs2 = blk_bs(blk2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1394 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1395 | buf1 = blk_blockalign(blk1, IO_BUF_SIZE); |
| 1396 | buf2 = blk_blockalign(blk2, IO_BUF_SIZE); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1397 | total_size1 = blk_getlength(blk1); |
| 1398 | if (total_size1 < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1399 | error_report("Can't get size of %s: %s", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1400 | filename1, strerror(-total_size1)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1401 | ret = 4; |
| 1402 | goto out; |
| 1403 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1404 | total_size2 = blk_getlength(blk2); |
| 1405 | if (total_size2 < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1406 | error_report("Can't get size of %s: %s", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1407 | filename2, strerror(-total_size2)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1408 | ret = 4; |
| 1409 | goto out; |
| 1410 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1411 | total_size = MIN(total_size1, total_size2); |
| 1412 | progress_base = MAX(total_size1, total_size2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1413 | |
| 1414 | qemu_progress_print(0, 100); |
| 1415 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1416 | if (strict && total_size1 != total_size2) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1417 | ret = 1; |
| 1418 | qprintf(quiet, "Strict mode: Image size mismatch!\n"); |
| 1419 | goto out; |
| 1420 | } |
| 1421 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1422 | while (offset < total_size) { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1423 | int status1, status2; |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 1424 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1425 | status1 = bdrv_block_status_above(bs1, NULL, offset, |
| 1426 | total_size1 - offset, &pnum1, NULL, |
| 1427 | NULL); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1428 | if (status1 < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1429 | ret = 3; |
| 1430 | error_report("Sector allocation test failed for %s", filename1); |
| 1431 | goto out; |
| 1432 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1433 | allocated1 = status1 & BDRV_BLOCK_ALLOCATED; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1434 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1435 | status2 = bdrv_block_status_above(bs2, NULL, offset, |
| 1436 | total_size2 - offset, &pnum2, NULL, |
| 1437 | NULL); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1438 | if (status2 < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1439 | ret = 3; |
| 1440 | error_report("Sector allocation test failed for %s", filename2); |
| 1441 | goto out; |
| 1442 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1443 | allocated2 = status2 & BDRV_BLOCK_ALLOCATED; |
Eric Blake | 7daddc6 | 2017-10-11 22:47:09 -0500 | [diff] [blame] | 1444 | |
| 1445 | assert(pnum1 && pnum2); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1446 | chunk = MIN(pnum1, pnum2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1447 | |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1448 | if (strict) { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1449 | if (status1 != status2) { |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1450 | ret = 1; |
| 1451 | qprintf(quiet, "Strict mode: Offset %" PRId64 |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1452 | " block status mismatch!\n", offset); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1453 | goto out; |
| 1454 | } |
| 1455 | } |
| 1456 | if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) { |
Eric Blake | 7daddc6 | 2017-10-11 22:47:09 -0500 | [diff] [blame] | 1457 | /* nothing to do */ |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1458 | } else if (allocated1 == allocated2) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1459 | if (allocated1) { |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1460 | int64_t pnum; |
| 1461 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1462 | chunk = MIN(chunk, IO_BUF_SIZE); |
| 1463 | ret = blk_pread(blk1, offset, buf1, chunk); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1464 | if (ret < 0) { |
| 1465 | error_report("Error while reading offset %" PRId64 |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1466 | " of %s: %s", |
| 1467 | offset, filename1, strerror(-ret)); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1468 | ret = 4; |
| 1469 | goto out; |
| 1470 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1471 | ret = blk_pread(blk2, offset, buf2, chunk); |
| 1472 | if (ret < 0) { |
| 1473 | error_report("Error while reading offset %" PRId64 |
| 1474 | " of %s: %s", |
| 1475 | offset, filename2, strerror(-ret)); |
| 1476 | ret = 4; |
| 1477 | goto out; |
| 1478 | } |
| 1479 | ret = compare_buffers(buf1, buf2, chunk, &pnum); |
| 1480 | if (ret || pnum != chunk) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1481 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1482 | offset + (ret ? 0 : pnum)); |
Fam Zheng | 36452f1 | 2013-11-13 20:26:49 +0800 | [diff] [blame] | 1483 | ret = 1; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1484 | goto out; |
| 1485 | } |
| 1486 | } |
| 1487 | } else { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1488 | chunk = MIN(chunk, IO_BUF_SIZE); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1489 | if (allocated1) { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1490 | ret = check_empty_sectors(blk1, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1491 | filename1, buf1, quiet); |
| 1492 | } else { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1493 | ret = check_empty_sectors(blk2, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1494 | filename2, buf1, quiet); |
| 1495 | } |
| 1496 | if (ret) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1497 | goto out; |
| 1498 | } |
| 1499 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1500 | offset += chunk; |
| 1501 | qemu_progress_print(((float) chunk / progress_base) * 100, 100); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1502 | } |
| 1503 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1504 | if (total_size1 != total_size2) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1505 | BlockBackend *blk_over; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1506 | const char *filename_over; |
| 1507 | |
| 1508 | qprintf(quiet, "Warning: Image size mismatch!\n"); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1509 | if (total_size1 > total_size2) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1510 | blk_over = blk1; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1511 | filename_over = filename1; |
| 1512 | } else { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1513 | blk_over = blk2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1514 | filename_over = filename2; |
| 1515 | } |
| 1516 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1517 | while (offset < progress_base) { |
| 1518 | ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset, |
| 1519 | progress_base - offset, &chunk, |
| 1520 | NULL, NULL); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1521 | if (ret < 0) { |
| 1522 | ret = 3; |
| 1523 | error_report("Sector allocation test failed for %s", |
| 1524 | filename_over); |
| 1525 | goto out; |
| 1526 | |
| 1527 | } |
Eric Blake | 391cb1a | 2017-10-11 22:47:10 -0500 | [diff] [blame] | 1528 | if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1529 | chunk = MIN(chunk, IO_BUF_SIZE); |
| 1530 | ret = check_empty_sectors(blk_over, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1531 | filename_over, buf1, quiet); |
| 1532 | if (ret) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1533 | goto out; |
| 1534 | } |
| 1535 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1536 | offset += chunk; |
| 1537 | qemu_progress_print(((float) chunk / progress_base) * 100, 100); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | qprintf(quiet, "Images are identical.\n"); |
| 1542 | ret = 0; |
| 1543 | |
| 1544 | out: |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1545 | qemu_vfree(buf1); |
| 1546 | qemu_vfree(buf2); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1547 | blk_unref(blk2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1548 | out2: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1549 | blk_unref(blk1); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1550 | out3: |
| 1551 | qemu_progress_end(); |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1552 | out4: |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1553 | return ret; |
| 1554 | } |
| 1555 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1556 | enum ImgConvertBlockStatus { |
| 1557 | BLK_DATA, |
| 1558 | BLK_ZERO, |
| 1559 | BLK_BACKING_FILE, |
| 1560 | }; |
| 1561 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1562 | #define MAX_COROUTINES 16 |
| 1563 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1564 | typedef struct ImgConvertState { |
| 1565 | BlockBackend **src; |
| 1566 | int64_t *src_sectors; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1567 | int src_num; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1568 | int64_t total_sectors; |
| 1569 | int64_t allocated_sectors; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1570 | int64_t allocated_done; |
| 1571 | int64_t sector_num; |
| 1572 | int64_t wr_offs; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1573 | enum ImgConvertBlockStatus status; |
| 1574 | int64_t sector_next_status; |
| 1575 | BlockBackend *target; |
| 1576 | bool has_zero_init; |
| 1577 | bool compressed; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1578 | bool unallocated_blocks_are_zero; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1579 | bool target_has_backing; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1580 | int64_t target_backing_sectors; /* negative if unknown */ |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1581 | bool wr_in_order; |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1582 | bool copy_range; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1583 | int min_sparse; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1584 | int alignment; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1585 | size_t cluster_sectors; |
| 1586 | size_t buf_sectors; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 1587 | long num_coroutines; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1588 | int running_coroutines; |
| 1589 | Coroutine *co[MAX_COROUTINES]; |
| 1590 | int64_t wait_sector_num[MAX_COROUTINES]; |
| 1591 | CoMutex lock; |
| 1592 | int ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1593 | } ImgConvertState; |
| 1594 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1595 | static void convert_select_part(ImgConvertState *s, int64_t sector_num, |
| 1596 | int *src_cur, int64_t *src_cur_offset) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1597 | { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1598 | *src_cur = 0; |
| 1599 | *src_cur_offset = 0; |
| 1600 | while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) { |
| 1601 | *src_cur_offset += s->src_sectors[*src_cur]; |
| 1602 | (*src_cur)++; |
| 1603 | assert(*src_cur < s->src_num); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num) |
| 1608 | { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1609 | int64_t src_cur_offset; |
| 1610 | int ret, n, src_cur; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1611 | bool post_backing_zero = false; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1612 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1613 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1614 | |
| 1615 | assert(s->total_sectors > sector_num); |
| 1616 | n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS); |
| 1617 | |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1618 | if (s->target_backing_sectors >= 0) { |
| 1619 | if (sector_num >= s->target_backing_sectors) { |
| 1620 | post_backing_zero = s->unallocated_blocks_are_zero; |
| 1621 | } else if (sector_num + n > s->target_backing_sectors) { |
| 1622 | /* Split requests around target_backing_sectors (because |
| 1623 | * starting from there, zeros are handled differently) */ |
| 1624 | n = s->target_backing_sectors - sector_num; |
| 1625 | } |
| 1626 | } |
| 1627 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1628 | if (s->sector_next_status <= sector_num) { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1629 | int64_t count = n * BDRV_SECTOR_SIZE; |
| 1630 | |
Vladimir Sementsov-Ogievskiy | 9f1b92a | 2017-04-07 14:34:04 +0300 | [diff] [blame] | 1631 | if (s->target_has_backing) { |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 1632 | |
| 1633 | ret = bdrv_block_status(blk_bs(s->src[src_cur]), |
| 1634 | (sector_num - src_cur_offset) * |
| 1635 | BDRV_SECTOR_SIZE, |
| 1636 | count, &count, NULL, NULL); |
Vladimir Sementsov-Ogievskiy | 9f1b92a | 2017-04-07 14:34:04 +0300 | [diff] [blame] | 1637 | } else { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1638 | ret = bdrv_block_status_above(blk_bs(s->src[src_cur]), NULL, |
| 1639 | (sector_num - src_cur_offset) * |
| 1640 | BDRV_SECTOR_SIZE, |
| 1641 | count, &count, NULL, NULL); |
Vladimir Sementsov-Ogievskiy | 9f1b92a | 2017-04-07 14:34:04 +0300 | [diff] [blame] | 1642 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1643 | if (ret < 0) { |
Eric Blake | 2058c2a | 2019-03-23 16:26:38 -0500 | [diff] [blame] | 1644 | error_report("error while reading block status of sector %" PRId64 |
| 1645 | ": %s", sector_num, strerror(-ret)); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1646 | return ret; |
| 1647 | } |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1648 | n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1649 | |
| 1650 | if (ret & BDRV_BLOCK_ZERO) { |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1651 | s->status = post_backing_zero ? BLK_BACKING_FILE : BLK_ZERO; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1652 | } else if (ret & BDRV_BLOCK_DATA) { |
| 1653 | s->status = BLK_DATA; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1654 | } else { |
Vladimir Sementsov-Ogievskiy | 9f1b92a | 2017-04-07 14:34:04 +0300 | [diff] [blame] | 1655 | s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | s->sector_next_status = sector_num + n; |
| 1659 | } |
| 1660 | |
| 1661 | n = MIN(n, s->sector_next_status - sector_num); |
| 1662 | if (s->status == BLK_DATA) { |
| 1663 | n = MIN(n, s->buf_sectors); |
| 1664 | } |
| 1665 | |
| 1666 | /* We need to write complete clusters for compressed images, so if an |
| 1667 | * unallocated area is shorter than that, we must consider the whole |
| 1668 | * cluster allocated. */ |
| 1669 | if (s->compressed) { |
| 1670 | if (n < s->cluster_sectors) { |
| 1671 | n = MIN(s->cluster_sectors, s->total_sectors - sector_num); |
| 1672 | s->status = BLK_DATA; |
| 1673 | } else { |
| 1674 | n = QEMU_ALIGN_DOWN(n, s->cluster_sectors); |
| 1675 | } |
| 1676 | } |
| 1677 | |
| 1678 | return n; |
| 1679 | } |
| 1680 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1681 | static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num, |
| 1682 | int nb_sectors, uint8_t *buf) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1683 | { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1684 | int n, ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1685 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1686 | assert(nb_sectors <= s->buf_sectors); |
| 1687 | while (nb_sectors > 0) { |
| 1688 | BlockBackend *blk; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1689 | int src_cur; |
| 1690 | int64_t bs_sectors, src_cur_offset; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1691 | |
| 1692 | /* In the case of compression with multiple source files, we can get a |
| 1693 | * nb_sectors that spreads into the next part. So we must be able to |
| 1694 | * read across multiple BDSes for one convert_read() call. */ |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1695 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
| 1696 | blk = s->src[src_cur]; |
| 1697 | bs_sectors = s->src_sectors[src_cur]; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1698 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1699 | n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset)); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1700 | |
Vladimir Sementsov-Ogievskiy | 265a7e5 | 2019-04-22 17:58:38 +0300 | [diff] [blame] | 1701 | ret = blk_co_pread( |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1702 | blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS, |
Vladimir Sementsov-Ogievskiy | 265a7e5 | 2019-04-22 17:58:38 +0300 | [diff] [blame] | 1703 | n << BDRV_SECTOR_BITS, buf, 0); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1704 | if (ret < 0) { |
| 1705 | return ret; |
| 1706 | } |
| 1707 | |
| 1708 | sector_num += n; |
| 1709 | nb_sectors -= n; |
| 1710 | buf += n * BDRV_SECTOR_SIZE; |
| 1711 | } |
| 1712 | |
| 1713 | return 0; |
| 1714 | } |
| 1715 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1716 | |
| 1717 | static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num, |
| 1718 | int nb_sectors, uint8_t *buf, |
| 1719 | enum ImgConvertBlockStatus status) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1720 | { |
| 1721 | int ret; |
| 1722 | |
| 1723 | while (nb_sectors > 0) { |
| 1724 | int n = nb_sectors; |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1725 | BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0; |
| 1726 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1727 | switch (status) { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1728 | case BLK_BACKING_FILE: |
| 1729 | /* If we have a backing file, leave clusters unallocated that are |
| 1730 | * unallocated in the source image, so that the backing file is |
| 1731 | * visible at the respective offset. */ |
| 1732 | assert(s->target_has_backing); |
| 1733 | break; |
| 1734 | |
| 1735 | case BLK_DATA: |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1736 | /* If we're told to keep the target fully allocated (-S 0) or there |
| 1737 | * is real non-zero data, we must write it. Otherwise we can treat |
| 1738 | * it as zero sectors. |
| 1739 | * Compressed clusters need to be written as a whole, so in that |
| 1740 | * case we can only save the write if the buffer is completely |
| 1741 | * zeroed. */ |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1742 | if (!s->min_sparse || |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1743 | (!s->compressed && |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1744 | is_allocated_sectors_min(buf, n, &n, s->min_sparse, |
| 1745 | sector_num, s->alignment)) || |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1746 | (s->compressed && |
| 1747 | !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1748 | { |
Vladimir Sementsov-Ogievskiy | 265a7e5 | 2019-04-22 17:58:38 +0300 | [diff] [blame] | 1749 | ret = blk_co_pwrite(s->target, sector_num << BDRV_SECTOR_BITS, |
| 1750 | n << BDRV_SECTOR_BITS, buf, flags); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1751 | if (ret < 0) { |
| 1752 | return ret; |
| 1753 | } |
| 1754 | break; |
| 1755 | } |
| 1756 | /* fall-through */ |
| 1757 | |
| 1758 | case BLK_ZERO: |
| 1759 | if (s->has_zero_init) { |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1760 | assert(!s->target_has_backing); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1761 | break; |
| 1762 | } |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1763 | ret = blk_co_pwrite_zeroes(s->target, |
| 1764 | sector_num << BDRV_SECTOR_BITS, |
Nir Soffer | a3d6ae2 | 2019-03-24 02:20:12 +0200 | [diff] [blame] | 1765 | n << BDRV_SECTOR_BITS, |
| 1766 | BDRV_REQ_MAY_UNMAP); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1767 | if (ret < 0) { |
| 1768 | return ret; |
| 1769 | } |
| 1770 | break; |
| 1771 | } |
| 1772 | |
| 1773 | sector_num += n; |
| 1774 | nb_sectors -= n; |
| 1775 | buf += n * BDRV_SECTOR_SIZE; |
| 1776 | } |
| 1777 | |
| 1778 | return 0; |
| 1779 | } |
| 1780 | |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1781 | static int coroutine_fn convert_co_copy_range(ImgConvertState *s, int64_t sector_num, |
| 1782 | int nb_sectors) |
| 1783 | { |
| 1784 | int n, ret; |
| 1785 | |
| 1786 | while (nb_sectors > 0) { |
| 1787 | BlockBackend *blk; |
| 1788 | int src_cur; |
| 1789 | int64_t bs_sectors, src_cur_offset; |
| 1790 | int64_t offset; |
| 1791 | |
| 1792 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
| 1793 | offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS; |
| 1794 | blk = s->src[src_cur]; |
| 1795 | bs_sectors = s->src_sectors[src_cur]; |
| 1796 | |
| 1797 | n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset)); |
| 1798 | |
| 1799 | ret = blk_co_copy_range(blk, offset, s->target, |
| 1800 | sector_num << BDRV_SECTOR_BITS, |
Vladimir Sementsov-Ogievskiy | 67b51fb | 2018-07-09 19:37:17 +0300 | [diff] [blame] | 1801 | n << BDRV_SECTOR_BITS, 0, 0); |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1802 | if (ret < 0) { |
| 1803 | return ret; |
| 1804 | } |
| 1805 | |
| 1806 | sector_num += n; |
| 1807 | nb_sectors -= n; |
| 1808 | } |
| 1809 | return 0; |
| 1810 | } |
| 1811 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1812 | static void coroutine_fn convert_co_do_copy(void *opaque) |
| 1813 | { |
| 1814 | ImgConvertState *s = opaque; |
| 1815 | uint8_t *buf = NULL; |
| 1816 | int ret, i; |
| 1817 | int index = -1; |
| 1818 | |
| 1819 | for (i = 0; i < s->num_coroutines; i++) { |
| 1820 | if (s->co[i] == qemu_coroutine_self()) { |
| 1821 | index = i; |
| 1822 | break; |
| 1823 | } |
| 1824 | } |
| 1825 | assert(index >= 0); |
| 1826 | |
| 1827 | s->running_coroutines++; |
| 1828 | buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE); |
| 1829 | |
| 1830 | while (1) { |
| 1831 | int n; |
| 1832 | int64_t sector_num; |
| 1833 | enum ImgConvertBlockStatus status; |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1834 | bool copy_range; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1835 | |
| 1836 | qemu_co_mutex_lock(&s->lock); |
| 1837 | if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) { |
| 1838 | qemu_co_mutex_unlock(&s->lock); |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1839 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1840 | } |
| 1841 | n = convert_iteration_sectors(s, s->sector_num); |
| 1842 | if (n < 0) { |
| 1843 | qemu_co_mutex_unlock(&s->lock); |
| 1844 | s->ret = n; |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1845 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1846 | } |
| 1847 | /* save current sector and allocation status to local variables */ |
| 1848 | sector_num = s->sector_num; |
| 1849 | status = s->status; |
| 1850 | if (!s->min_sparse && s->status == BLK_ZERO) { |
| 1851 | n = MIN(n, s->buf_sectors); |
| 1852 | } |
| 1853 | /* increment global sector counter so that other coroutines can |
| 1854 | * already continue reading beyond this request */ |
| 1855 | s->sector_num += n; |
| 1856 | qemu_co_mutex_unlock(&s->lock); |
| 1857 | |
| 1858 | if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) { |
| 1859 | s->allocated_done += n; |
| 1860 | qemu_progress_print(100.0 * s->allocated_done / |
| 1861 | s->allocated_sectors, 0); |
| 1862 | } |
| 1863 | |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1864 | retry: |
| 1865 | copy_range = s->copy_range && s->status == BLK_DATA; |
| 1866 | if (status == BLK_DATA && !copy_range) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1867 | ret = convert_co_read(s, sector_num, n, buf); |
| 1868 | if (ret < 0) { |
| 1869 | error_report("error while reading sector %" PRId64 |
| 1870 | ": %s", sector_num, strerror(-ret)); |
| 1871 | s->ret = ret; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1872 | } |
| 1873 | } else if (!s->min_sparse && status == BLK_ZERO) { |
| 1874 | status = BLK_DATA; |
| 1875 | memset(buf, 0x00, n * BDRV_SECTOR_SIZE); |
| 1876 | } |
| 1877 | |
| 1878 | if (s->wr_in_order) { |
| 1879 | /* keep writes in order */ |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1880 | while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1881 | s->wait_sector_num[index] = sector_num; |
| 1882 | qemu_coroutine_yield(); |
| 1883 | } |
| 1884 | s->wait_sector_num[index] = -1; |
| 1885 | } |
| 1886 | |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1887 | if (s->ret == -EINPROGRESS) { |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1888 | if (copy_range) { |
| 1889 | ret = convert_co_copy_range(s, sector_num, n); |
| 1890 | if (ret) { |
| 1891 | s->copy_range = false; |
| 1892 | goto retry; |
| 1893 | } |
| 1894 | } else { |
| 1895 | ret = convert_co_write(s, sector_num, n, buf, status); |
| 1896 | } |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1897 | if (ret < 0) { |
| 1898 | error_report("error while writing sector %" PRId64 |
| 1899 | ": %s", sector_num, strerror(-ret)); |
| 1900 | s->ret = ret; |
| 1901 | } |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1902 | } |
| 1903 | |
| 1904 | if (s->wr_in_order) { |
| 1905 | /* reenter the coroutine that might have waited |
| 1906 | * for this write to complete */ |
| 1907 | s->wr_offs = sector_num + n; |
| 1908 | for (i = 0; i < s->num_coroutines; i++) { |
| 1909 | if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) { |
| 1910 | /* |
| 1911 | * A -> B -> A cannot occur because A has |
| 1912 | * s->wait_sector_num[i] == -1 during A -> B. Therefore |
| 1913 | * B will never enter A during this time window. |
| 1914 | */ |
| 1915 | qemu_coroutine_enter(s->co[i]); |
| 1916 | break; |
| 1917 | } |
| 1918 | } |
| 1919 | } |
| 1920 | } |
| 1921 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1922 | qemu_vfree(buf); |
| 1923 | s->co[index] = NULL; |
| 1924 | s->running_coroutines--; |
| 1925 | if (!s->running_coroutines && s->ret == -EINPROGRESS) { |
| 1926 | /* the convert job finished successfully */ |
| 1927 | s->ret = 0; |
| 1928 | } |
| 1929 | } |
| 1930 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1931 | static int convert_do_copy(ImgConvertState *s) |
| 1932 | { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1933 | int ret, i, n; |
| 1934 | int64_t sector_num = 0; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1935 | |
| 1936 | /* Check whether we have zero initialisation or can get it efficiently */ |
| 1937 | s->has_zero_init = s->min_sparse && !s->target_has_backing |
| 1938 | ? bdrv_has_zero_init(blk_bs(s->target)) |
| 1939 | : false; |
| 1940 | |
| 1941 | if (!s->has_zero_init && !s->target_has_backing && |
| 1942 | bdrv_can_write_zeroes_with_unmap(blk_bs(s->target))) |
| 1943 | { |
Kevin Wolf | c9fdcf2 | 2019-03-22 13:49:28 +0100 | [diff] [blame] | 1944 | ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1945 | if (ret == 0) { |
| 1946 | s->has_zero_init = true; |
| 1947 | } |
| 1948 | } |
| 1949 | |
| 1950 | /* Allocate buffer for copied data. For compressed images, only one cluster |
| 1951 | * can be copied at a time. */ |
| 1952 | if (s->compressed) { |
| 1953 | if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) { |
| 1954 | error_report("invalid cluster size"); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1955 | return -EINVAL; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1956 | } |
| 1957 | s->buf_sectors = s->cluster_sectors; |
| 1958 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1959 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1960 | while (sector_num < s->total_sectors) { |
| 1961 | n = convert_iteration_sectors(s, sector_num); |
| 1962 | if (n < 0) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1963 | return n; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1964 | } |
Max Reitz | aad15de | 2016-03-24 23:33:57 +0100 | [diff] [blame] | 1965 | if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO)) |
| 1966 | { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1967 | s->allocated_sectors += n; |
| 1968 | } |
| 1969 | sector_num += n; |
| 1970 | } |
| 1971 | |
| 1972 | /* Do the copy */ |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1973 | s->sector_next_status = 0; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1974 | s->ret = -EINPROGRESS; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1975 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1976 | qemu_co_mutex_init(&s->lock); |
| 1977 | for (i = 0; i < s->num_coroutines; i++) { |
| 1978 | s->co[i] = qemu_coroutine_create(convert_co_do_copy, s); |
| 1979 | s->wait_sector_num[i] = -1; |
| 1980 | qemu_coroutine_enter(s->co[i]); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1981 | } |
| 1982 | |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1983 | while (s->running_coroutines) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1984 | main_loop_wait(false); |
| 1985 | } |
| 1986 | |
| 1987 | if (s->compressed && !s->ret) { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1988 | /* signal EOF to align */ |
Pavel Butsykin | fe5c135 | 2016-07-22 11:17:40 +0300 | [diff] [blame] | 1989 | ret = blk_pwrite_compressed(s->target, 0, NULL, 0); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1990 | if (ret < 0) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1991 | return ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1992 | } |
| 1993 | } |
| 1994 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1995 | return s->ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1996 | } |
| 1997 | |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 1998 | #define MAX_BUF_SECTORS 32768 |
| 1999 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2000 | static int img_convert(int argc, char **argv) |
| 2001 | { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2002 | int c, bs_i, flags, src_flags = 0; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2003 | const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe", |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2004 | *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL, |
| 2005 | *out_filename, *out_baseimg_param, *snapshot_name = NULL; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2006 | BlockDriver *drv = NULL, *proto_drv = NULL; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2007 | BlockDriverInfo bdi; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2008 | BlockDriverState *out_bs; |
| 2009 | QemuOpts *opts = NULL, *sn_opts = NULL; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2010 | QemuOptsList *create_opts = NULL; |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2011 | QDict *open_opts = NULL; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2012 | char *options = NULL; |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 2013 | Error *local_err = NULL; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2014 | bool writethrough, src_writethrough, quiet = false, image_opts = false, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2015 | skip_create = false, progress = false, tgt_image_opts = false; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2016 | int64_t ret = -EINVAL; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2017 | bool force_share = false; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2018 | bool explict_min_sparse = false; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2019 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2020 | ImgConvertState s = (ImgConvertState) { |
| 2021 | /* Need at least 4k of zeros for sparse detection */ |
| 2022 | .min_sparse = 8, |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2023 | .copy_range = false, |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2024 | .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE, |
| 2025 | .wr_in_order = true, |
| 2026 | .num_coroutines = 8, |
| 2027 | }; |
| 2028 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2029 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2030 | static const struct option long_options[] = { |
| 2031 | {"help", no_argument, 0, 'h'}, |
| 2032 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2033 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2034 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2035 | {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2036 | {0, 0, 0, 0} |
| 2037 | }; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2038 | 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] | 2039 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2040 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2041 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2042 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2043 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2044 | case ':': |
| 2045 | missing_argument(argv[optind - 1]); |
| 2046 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2047 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2048 | unrecognized_option(argv[optind - 1]); |
| 2049 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2050 | case 'h': |
| 2051 | help(); |
| 2052 | break; |
| 2053 | case 'f': |
| 2054 | fmt = optarg; |
| 2055 | break; |
| 2056 | case 'O': |
| 2057 | out_fmt = optarg; |
| 2058 | break; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 2059 | case 'B': |
| 2060 | out_baseimg = optarg; |
| 2061 | break; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2062 | case 'C': |
| 2063 | s.copy_range = true; |
| 2064 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2065 | case 'c': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2066 | s.compressed = true; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2067 | break; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2068 | case 'o': |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 2069 | if (!is_valid_option_list(optarg)) { |
| 2070 | error_report("Invalid option list: %s", optarg); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2071 | goto fail_getopt; |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 2072 | } |
| 2073 | if (!options) { |
| 2074 | options = g_strdup(optarg); |
| 2075 | } else { |
| 2076 | char *old_options = options; |
| 2077 | options = g_strdup_printf("%s,%s", options, optarg); |
| 2078 | g_free(old_options); |
| 2079 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2080 | break; |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2081 | case 'l': |
| 2082 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { |
Markus Armbruster | 70b9433 | 2015-02-13 12:50:26 +0100 | [diff] [blame] | 2083 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
| 2084 | optarg, false); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2085 | if (!sn_opts) { |
| 2086 | error_report("Failed in parsing snapshot param '%s'", |
| 2087 | optarg); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2088 | goto fail_getopt; |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2089 | } |
| 2090 | } else { |
| 2091 | snapshot_name = optarg; |
| 2092 | } |
| 2093 | break; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2094 | case 'S': |
| 2095 | { |
| 2096 | int64_t sval; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 2097 | |
| 2098 | sval = cvtnum(optarg); |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 2099 | if (sval < 0 || sval & (BDRV_SECTOR_SIZE - 1) || |
| 2100 | sval / BDRV_SECTOR_SIZE > MAX_BUF_SECTORS) { |
| 2101 | error_report("Invalid buffer size for sparse output specified. " |
| 2102 | "Valid sizes are multiples of %llu up to %llu. Select " |
| 2103 | "0 to disable sparse detection (fully allocates output).", |
| 2104 | BDRV_SECTOR_SIZE, MAX_BUF_SECTORS * BDRV_SECTOR_SIZE); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2105 | goto fail_getopt; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2106 | } |
| 2107 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2108 | s.min_sparse = sval / BDRV_SECTOR_SIZE; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2109 | explict_min_sparse = true; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2110 | break; |
| 2111 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2112 | case 'p': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2113 | progress = true; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2114 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2115 | case 't': |
| 2116 | cache = optarg; |
| 2117 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 2118 | case 'T': |
| 2119 | src_cache = optarg; |
| 2120 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2121 | case 'q': |
| 2122 | quiet = true; |
| 2123 | break; |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2124 | case 'n': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2125 | skip_create = true; |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2126 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2127 | case 'm': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2128 | if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) || |
| 2129 | s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2130 | error_report("Invalid number of coroutines. Allowed number of" |
| 2131 | " coroutines is between 1 and %d", MAX_COROUTINES); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2132 | goto fail_getopt; |
| 2133 | } |
| 2134 | break; |
| 2135 | case 'W': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2136 | s.wr_in_order = false; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2137 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2138 | case 'U': |
| 2139 | force_share = true; |
| 2140 | break; |
Max Reitz | 3258b91 | 2017-04-26 15:46:47 +0200 | [diff] [blame] | 2141 | case OPTION_OBJECT: { |
| 2142 | QemuOpts *object_opts; |
| 2143 | object_opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 2144 | optarg, true); |
| 2145 | if (!object_opts) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2146 | goto fail_getopt; |
| 2147 | } |
| 2148 | break; |
Max Reitz | 3258b91 | 2017-04-26 15:46:47 +0200 | [diff] [blame] | 2149 | } |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2150 | case OPTION_IMAGE_OPTS: |
| 2151 | image_opts = true; |
| 2152 | break; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2153 | case OPTION_TARGET_IMAGE_OPTS: |
| 2154 | tgt_image_opts = true; |
| 2155 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2156 | } |
| 2157 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2158 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2159 | if (!out_fmt && !tgt_image_opts) { |
| 2160 | out_fmt = "raw"; |
| 2161 | } |
| 2162 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2163 | if (qemu_opts_foreach(&qemu_object_opts, |
| 2164 | user_creatable_add_opts_foreach, |
Daniel P. Berrangé | 334c43e | 2019-02-19 11:46:09 +0000 | [diff] [blame] | 2165 | NULL, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2166 | goto fail_getopt; |
| 2167 | } |
| 2168 | |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2169 | if (s.compressed && s.copy_range) { |
| 2170 | error_report("Cannot enable copy offloading when -c is used"); |
| 2171 | goto fail_getopt; |
| 2172 | } |
| 2173 | |
| 2174 | if (explict_min_sparse && s.copy_range) { |
| 2175 | error_report("Cannot enable copy offloading when -S is used"); |
| 2176 | goto fail_getopt; |
| 2177 | } |
| 2178 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2179 | if (tgt_image_opts && !skip_create) { |
| 2180 | error_report("--target-image-opts requires use of -n flag"); |
| 2181 | goto fail_getopt; |
| 2182 | } |
| 2183 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2184 | s.src_num = argc - optind - 1; |
| 2185 | out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL; |
| 2186 | |
| 2187 | if (options && has_help_option(options)) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2188 | if (out_fmt) { |
| 2189 | ret = print_block_option_help(out_filename, out_fmt); |
| 2190 | goto fail_getopt; |
| 2191 | } else { |
| 2192 | error_report("Option help requires a format be specified"); |
| 2193 | goto fail_getopt; |
| 2194 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2195 | } |
| 2196 | |
| 2197 | if (s.src_num < 1) { |
| 2198 | error_report("Must specify image file name"); |
| 2199 | goto fail_getopt; |
| 2200 | } |
| 2201 | |
| 2202 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2203 | /* ret is still -EINVAL until here */ |
| 2204 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); |
| 2205 | if (ret < 0) { |
| 2206 | error_report("Invalid source cache option: %s", src_cache); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2207 | goto fail_getopt; |
| 2208 | } |
| 2209 | |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2210 | /* Initialize before goto out */ |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2211 | if (quiet) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2212 | progress = false; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2213 | } |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2214 | qemu_progress_init(progress, 1.0); |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2215 | qemu_progress_print(0, 100); |
| 2216 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2217 | s.src = g_new0(BlockBackend *, s.src_num); |
| 2218 | s.src_sectors = g_new(int64_t, s.src_num); |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 2219 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2220 | for (bs_i = 0; bs_i < s.src_num; bs_i++) { |
| 2221 | s.src[bs_i] = img_open(image_opts, argv[optind + bs_i], |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2222 | fmt, src_flags, src_writethrough, quiet, |
| 2223 | force_share); |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2224 | if (!s.src[bs_i]) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2225 | ret = -1; |
| 2226 | goto out; |
| 2227 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2228 | s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]); |
| 2229 | if (s.src_sectors[bs_i] < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2230 | error_report("Could not get size of %s: %s", |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2231 | argv[optind + bs_i], strerror(-s.src_sectors[bs_i])); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2232 | ret = -1; |
| 2233 | goto out; |
| 2234 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2235 | s.total_sectors += s.src_sectors[bs_i]; |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 2236 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2237 | |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2238 | if (sn_opts) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2239 | bdrv_snapshot_load_tmp(blk_bs(s.src[0]), |
Peter Maydell | 10d6eda | 2017-02-10 16:28:24 +0000 | [diff] [blame] | 2240 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), |
| 2241 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), |
| 2242 | &local_err); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2243 | } else if (snapshot_name != NULL) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2244 | if (s.src_num > 1) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 2245 | error_report("No support for concatenating multiple snapshot"); |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 2246 | ret = -1; |
| 2247 | goto out; |
| 2248 | } |
Wenchao Xia | 7b4c478 | 2013-12-04 17:10:54 +0800 | [diff] [blame] | 2249 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2250 | bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name, |
| 2251 | &local_err); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2252 | } |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2253 | if (local_err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2254 | error_reportf_err(local_err, "Failed to load snapshot: "); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2255 | ret = -1; |
| 2256 | goto out; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 2257 | } |
| 2258 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2259 | if (!skip_create) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2260 | /* Find driver and parse its options */ |
| 2261 | drv = bdrv_find_format(out_fmt); |
| 2262 | if (!drv) { |
| 2263 | error_report("Unknown file format '%s'", out_fmt); |
| 2264 | ret = -1; |
| 2265 | goto out; |
| 2266 | } |
| 2267 | |
| 2268 | proto_drv = bdrv_find_protocol(out_filename, true, &local_err); |
| 2269 | if (!proto_drv) { |
| 2270 | error_report_err(local_err); |
| 2271 | ret = -1; |
| 2272 | goto out; |
| 2273 | } |
| 2274 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2275 | if (!drv->create_opts) { |
| 2276 | error_report("Format driver '%s' does not support image creation", |
| 2277 | drv->format_name); |
| 2278 | ret = -1; |
| 2279 | goto out; |
| 2280 | } |
Max Reitz | f75613c | 2014-12-02 18:32:46 +0100 | [diff] [blame] | 2281 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2282 | if (!proto_drv->create_opts) { |
| 2283 | error_report("Protocol driver '%s' does not support image creation", |
| 2284 | proto_drv->format_name); |
| 2285 | ret = -1; |
| 2286 | goto out; |
| 2287 | } |
Max Reitz | f75613c | 2014-12-02 18:32:46 +0100 | [diff] [blame] | 2288 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2289 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 2290 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
Kevin Wolf | db08adf | 2009-06-04 15:39:38 +0200 | [diff] [blame] | 2291 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2292 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 2293 | if (options) { |
| 2294 | qemu_opts_do_parse(opts, options, NULL, &local_err); |
| 2295 | if (local_err) { |
Markus Armbruster | 97a2ca7 | 2015-03-14 10:23:15 +0100 | [diff] [blame] | 2296 | error_report_err(local_err); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 2297 | ret = -1; |
| 2298 | goto out; |
| 2299 | } |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2300 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2301 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2302 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512, |
Markus Armbruster | 39101f2 | 2015-02-12 16:46:36 +0100 | [diff] [blame] | 2303 | &error_abort); |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2304 | ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL); |
| 2305 | if (ret < 0) { |
| 2306 | goto out; |
| 2307 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2308 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2309 | |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2310 | /* Get backing file name if -o backing_file was used */ |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2311 | out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2312 | if (out_baseimg_param) { |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2313 | out_baseimg = out_baseimg_param; |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2314 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2315 | s.target_has_backing = (bool) out_baseimg; |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2316 | |
Max Reitz | 48758a8 | 2017-04-26 15:46:48 +0200 | [diff] [blame] | 2317 | if (s.src_num > 1 && out_baseimg) { |
| 2318 | error_report("Having a backing file for the target makes no sense when " |
| 2319 | "concatenating multiple input images"); |
| 2320 | ret = -1; |
| 2321 | goto out; |
| 2322 | } |
| 2323 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2324 | /* Check if compression is supported */ |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2325 | if (s.compressed) { |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2326 | bool encryption = |
| 2327 | qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false); |
Daniel P. Berrange | 0cb8d47 | 2017-06-23 17:24:06 +0100 | [diff] [blame] | 2328 | const char *encryptfmt = |
| 2329 | qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2330 | const char *preallocation = |
| 2331 | qemu_opt_get(opts, BLOCK_OPT_PREALLOC); |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2332 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2333 | if (drv && !drv->bdrv_co_pwritev_compressed) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2334 | error_report("Compression not supported for this file format"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2335 | ret = -1; |
| 2336 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2337 | } |
| 2338 | |
Daniel P. Berrange | 0cb8d47 | 2017-06-23 17:24:06 +0100 | [diff] [blame] | 2339 | if (encryption || encryptfmt) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2340 | error_report("Compression and encryption not supported at " |
| 2341 | "the same time"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2342 | ret = -1; |
| 2343 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2344 | } |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 2345 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2346 | if (preallocation |
| 2347 | && strcmp(preallocation, "off")) |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 2348 | { |
| 2349 | error_report("Compression and preallocation not supported at " |
| 2350 | "the same time"); |
| 2351 | ret = -1; |
| 2352 | goto out; |
| 2353 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2354 | } |
| 2355 | |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2356 | /* |
| 2357 | * The later open call will need any decryption secrets, and |
| 2358 | * bdrv_create() will purge "opts", so extract them now before |
| 2359 | * they are lost. |
| 2360 | */ |
| 2361 | if (!skip_create) { |
| 2362 | open_opts = qdict_new(); |
| 2363 | qemu_opt_foreach(opts, img_add_key_secrets, open_opts, &error_abort); |
| 2364 | } |
| 2365 | |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2366 | if (!skip_create) { |
| 2367 | /* Create the new image */ |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 2368 | ret = bdrv_create(drv, out_filename, opts, &local_err); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2369 | if (ret < 0) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2370 | error_reportf_err(local_err, "%s: error while converting %s: ", |
| 2371 | out_filename, out_fmt); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2372 | goto out; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2373 | } |
| 2374 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2375 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2376 | 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] | 2377 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2378 | if (ret < 0) { |
| 2379 | error_report("Invalid cache option: %s", cache); |
Markus Armbruster | bb9cd2e | 2014-05-28 11:17:07 +0200 | [diff] [blame] | 2380 | goto out; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2381 | } |
| 2382 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2383 | if (skip_create) { |
| 2384 | s.target = img_open(tgt_image_opts, out_filename, out_fmt, |
| 2385 | flags, writethrough, quiet, false); |
| 2386 | } else { |
| 2387 | /* TODO ultimately we should allow --target-image-opts |
| 2388 | * to be used even when -n is not given. |
| 2389 | * That has to wait for bdrv_create to be improved |
| 2390 | * to allow filenames in option syntax |
| 2391 | */ |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2392 | s.target = img_open_file(out_filename, open_opts, out_fmt, |
| 2393 | flags, writethrough, quiet, false); |
| 2394 | open_opts = NULL; /* blk_new_open will have freed it */ |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2395 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2396 | if (!s.target) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2397 | ret = -1; |
| 2398 | goto out; |
| 2399 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2400 | out_bs = blk_bs(s.target); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2401 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2402 | if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) { |
| 2403 | error_report("Compression not supported for this file format"); |
| 2404 | ret = -1; |
| 2405 | goto out; |
| 2406 | } |
| 2407 | |
Eric Blake | 5def6b8 | 2016-06-23 16:37:19 -0600 | [diff] [blame] | 2408 | /* increase bufsectors from the default 4096 (2M) if opt_transfer |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 2409 | * or discard_alignment of the out_bs is greater. Limit to |
| 2410 | * MAX_BUF_SECTORS as maximum which is currently 32768 (16MB). */ |
| 2411 | s.buf_sectors = MIN(MAX_BUF_SECTORS, |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2412 | MAX(s.buf_sectors, |
| 2413 | MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS, |
| 2414 | out_bs->bl.pdiscard_alignment >> |
| 2415 | BDRV_SECTOR_BITS))); |
Peter Lieven | f2521c9 | 2013-11-27 11:07:06 +0100 | [diff] [blame] | 2416 | |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 2417 | /* try to align the write requests to the destination to avoid unnecessary |
| 2418 | * RMW cycles. */ |
| 2419 | s.alignment = MAX(pow2floor(s.min_sparse), |
| 2420 | DIV_ROUND_UP(out_bs->bl.request_alignment, |
| 2421 | BDRV_SECTOR_SIZE)); |
| 2422 | assert(is_power_of_2(s.alignment)); |
| 2423 | |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2424 | if (skip_create) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2425 | int64_t output_sectors = blk_nb_sectors(s.target); |
Markus Armbruster | 43716fa | 2014-06-26 13:23:21 +0200 | [diff] [blame] | 2426 | if (output_sectors < 0) { |
Gonglei | eec5eb4 | 2015-02-25 12:22:27 +0800 | [diff] [blame] | 2427 | error_report("unable to get output image length: %s", |
Markus Armbruster | 43716fa | 2014-06-26 13:23:21 +0200 | [diff] [blame] | 2428 | strerror(-output_sectors)); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2429 | ret = -1; |
| 2430 | goto out; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2431 | } else if (output_sectors < s.total_sectors) { |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2432 | error_report("output file is smaller than input file"); |
| 2433 | ret = -1; |
| 2434 | goto out; |
| 2435 | } |
| 2436 | } |
| 2437 | |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 2438 | if (s.target_has_backing) { |
| 2439 | /* Errors are treated as "backing length unknown" (which means |
| 2440 | * s.target_backing_sectors has to be negative, which it will |
| 2441 | * be automatically). The backing file length is used only |
| 2442 | * for optimizations, so such a case is not fatal. */ |
| 2443 | s.target_backing_sectors = bdrv_nb_sectors(out_bs->backing->bs); |
| 2444 | } else { |
| 2445 | s.target_backing_sectors = -1; |
| 2446 | } |
| 2447 | |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2448 | ret = bdrv_get_info(out_bs, &bdi); |
| 2449 | if (ret < 0) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2450 | if (s.compressed) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2451 | error_report("could not get block driver info"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2452 | goto out; |
| 2453 | } |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2454 | } else { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2455 | s.compressed = s.compressed || bdi.needs_compressed_writes; |
| 2456 | s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 2457 | s.unallocated_blocks_are_zero = bdi.unallocated_blocks_are_zero; |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2458 | } |
| 2459 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2460 | ret = convert_do_copy(&s); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2461 | out: |
Peter Lieven | 13c28af | 2013-11-27 11:07:01 +0100 | [diff] [blame] | 2462 | if (!ret) { |
| 2463 | qemu_progress_print(100, 0); |
| 2464 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2465 | qemu_progress_end(); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2466 | qemu_opts_del(opts); |
| 2467 | qemu_opts_free(create_opts); |
Markus Armbruster | fbf28a4 | 2014-09-29 16:07:55 +0200 | [diff] [blame] | 2468 | qemu_opts_del(sn_opts); |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2469 | qobject_unref(open_opts); |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2470 | blk_unref(s.target); |
| 2471 | if (s.src) { |
| 2472 | for (bs_i = 0; bs_i < s.src_num; bs_i++) { |
| 2473 | blk_unref(s.src[bs_i]); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2474 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2475 | g_free(s.src); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2476 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2477 | g_free(s.src_sectors); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2478 | fail_getopt: |
| 2479 | g_free(options); |
| 2480 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2481 | return !!ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
bellard | 57d1a2b | 2004-08-03 21:15:11 +0000 | [diff] [blame] | 2484 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2485 | static void dump_snapshots(BlockDriverState *bs) |
| 2486 | { |
| 2487 | QEMUSnapshotInfo *sn_tab, *sn; |
| 2488 | int nb_sns, i; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2489 | |
| 2490 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 2491 | if (nb_sns <= 0) |
| 2492 | return; |
| 2493 | printf("Snapshot list:\n"); |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2494 | bdrv_snapshot_dump(NULL); |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2495 | printf("\n"); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2496 | for(i = 0; i < nb_sns; i++) { |
| 2497 | sn = &sn_tab[i]; |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2498 | bdrv_snapshot_dump(sn); |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2499 | printf("\n"); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2500 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2501 | g_free(sn_tab); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2502 | } |
| 2503 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2504 | static void dump_json_image_info_list(ImageInfoList *list) |
| 2505 | { |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2506 | QString *str; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2507 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 2508 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2509 | |
| 2510 | visit_type_ImageInfoList(v, NULL, &list, &error_abort); |
| 2511 | visit_complete(v, &obj); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2512 | str = qobject_to_json_pretty(obj); |
| 2513 | assert(str != NULL); |
| 2514 | printf("%s\n", qstring_get_str(str)); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2515 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2516 | visit_free(v); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2517 | qobject_unref(str); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2518 | } |
| 2519 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2520 | static void dump_json_image_info(ImageInfo *info) |
| 2521 | { |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2522 | QString *str; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2523 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 2524 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2525 | |
| 2526 | visit_type_ImageInfo(v, NULL, &info, &error_abort); |
| 2527 | visit_complete(v, &obj); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2528 | str = qobject_to_json_pretty(obj); |
| 2529 | assert(str != NULL); |
| 2530 | printf("%s\n", qstring_get_str(str)); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2531 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2532 | visit_free(v); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2533 | qobject_unref(str); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2534 | } |
| 2535 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2536 | static void dump_human_image_info_list(ImageInfoList *list) |
| 2537 | { |
| 2538 | ImageInfoList *elem; |
| 2539 | bool delim = false; |
| 2540 | |
| 2541 | for (elem = list; elem; elem = elem->next) { |
| 2542 | if (delim) { |
| 2543 | printf("\n"); |
| 2544 | } |
| 2545 | delim = true; |
| 2546 | |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2547 | bdrv_image_info_dump(elem->value); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2548 | } |
| 2549 | } |
| 2550 | |
| 2551 | static gboolean str_equal_func(gconstpointer a, gconstpointer b) |
| 2552 | { |
| 2553 | return strcmp(a, b) == 0; |
| 2554 | } |
| 2555 | |
| 2556 | /** |
| 2557 | * Open an image file chain and return an ImageInfoList |
| 2558 | * |
| 2559 | * @filename: topmost image filename |
| 2560 | * @fmt: topmost image format (may be NULL to autodetect) |
| 2561 | * @chain: true - enumerate entire backing file chain |
| 2562 | * false - only topmost image file |
| 2563 | * |
| 2564 | * Returns a list of ImageInfo objects or NULL if there was an error opening an |
| 2565 | * image file. If there was an error a message will have been printed to |
| 2566 | * stderr. |
| 2567 | */ |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2568 | static ImageInfoList *collect_image_info_list(bool image_opts, |
| 2569 | const char *filename, |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2570 | const char *fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2571 | bool chain, bool force_share) |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2572 | { |
| 2573 | ImageInfoList *head = NULL; |
| 2574 | ImageInfoList **last = &head; |
| 2575 | GHashTable *filenames; |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2576 | Error *err = NULL; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2577 | |
| 2578 | filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL); |
| 2579 | |
| 2580 | while (filename) { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2581 | BlockBackend *blk; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2582 | BlockDriverState *bs; |
| 2583 | ImageInfo *info; |
| 2584 | ImageInfoList *elem; |
| 2585 | |
| 2586 | if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) { |
| 2587 | error_report("Backing file '%s' creates an infinite loop.", |
| 2588 | filename); |
| 2589 | goto err; |
| 2590 | } |
| 2591 | g_hash_table_insert(filenames, (gpointer)filename, NULL); |
| 2592 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 2593 | blk = img_open(image_opts, filename, fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2594 | BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false, |
| 2595 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2596 | if (!blk) { |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2597 | goto err; |
| 2598 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2599 | bs = blk_bs(blk); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2600 | |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2601 | bdrv_query_image_info(bs, &info, &err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2602 | if (err) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 2603 | error_report_err(err); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2604 | blk_unref(blk); |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2605 | goto err; |
Wenchao Xia | fb0ed45 | 2013-06-06 12:27:57 +0800 | [diff] [blame] | 2606 | } |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2607 | |
| 2608 | elem = g_new0(ImageInfoList, 1); |
| 2609 | elem->value = info; |
| 2610 | *last = elem; |
| 2611 | last = &elem->next; |
| 2612 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2613 | blk_unref(blk); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2614 | |
| 2615 | filename = fmt = NULL; |
| 2616 | if (chain) { |
| 2617 | if (info->has_full_backing_filename) { |
| 2618 | filename = info->full_backing_filename; |
| 2619 | } else if (info->has_backing_filename) { |
John Snow | 92d617a | 2015-12-14 14:55:15 -0500 | [diff] [blame] | 2620 | error_report("Could not determine absolute backing filename," |
| 2621 | " but backing filename '%s' present", |
| 2622 | info->backing_filename); |
| 2623 | goto err; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2624 | } |
| 2625 | if (info->has_backing_filename_format) { |
| 2626 | fmt = info->backing_filename_format; |
| 2627 | } |
| 2628 | } |
| 2629 | } |
| 2630 | g_hash_table_destroy(filenames); |
| 2631 | return head; |
| 2632 | |
| 2633 | err: |
| 2634 | qapi_free_ImageInfoList(head); |
| 2635 | g_hash_table_destroy(filenames); |
| 2636 | return NULL; |
| 2637 | } |
| 2638 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2639 | static int img_info(int argc, char **argv) |
| 2640 | { |
| 2641 | int c; |
| 2642 | OutputFormat output_format = OFORMAT_HUMAN; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2643 | bool chain = false; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2644 | const char *filename, *fmt, *output; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2645 | ImageInfoList *list; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2646 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2647 | bool force_share = false; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2648 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2649 | fmt = NULL; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2650 | output = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2651 | for(;;) { |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2652 | int option_index = 0; |
| 2653 | static const struct option long_options[] = { |
| 2654 | {"help", no_argument, 0, 'h'}, |
| 2655 | {"format", required_argument, 0, 'f'}, |
| 2656 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2657 | {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2658 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2659 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2660 | {"force-share", no_argument, 0, 'U'}, |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2661 | {0, 0, 0, 0} |
| 2662 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2663 | c = getopt_long(argc, argv, ":f:hU", |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2664 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2665 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2666 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2667 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2668 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2669 | case ':': |
| 2670 | missing_argument(argv[optind - 1]); |
| 2671 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2672 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2673 | unrecognized_option(argv[optind - 1]); |
| 2674 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2675 | case 'h': |
| 2676 | help(); |
| 2677 | break; |
| 2678 | case 'f': |
| 2679 | fmt = optarg; |
| 2680 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2681 | case 'U': |
| 2682 | force_share = true; |
| 2683 | break; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2684 | case OPTION_OUTPUT: |
| 2685 | output = optarg; |
| 2686 | break; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2687 | case OPTION_BACKING_CHAIN: |
| 2688 | chain = true; |
| 2689 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2690 | case OPTION_OBJECT: { |
| 2691 | QemuOpts *opts; |
| 2692 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 2693 | optarg, true); |
| 2694 | if (!opts) { |
| 2695 | return 1; |
| 2696 | } |
| 2697 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2698 | case OPTION_IMAGE_OPTS: |
| 2699 | image_opts = true; |
| 2700 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2701 | } |
| 2702 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 2703 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2704 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2705 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2706 | filename = argv[optind++]; |
| 2707 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2708 | if (output && !strcmp(output, "json")) { |
| 2709 | output_format = OFORMAT_JSON; |
| 2710 | } else if (output && !strcmp(output, "human")) { |
| 2711 | output_format = OFORMAT_HUMAN; |
| 2712 | } else if (output) { |
| 2713 | error_report("--output must be used with human or json as argument."); |
| 2714 | return 1; |
| 2715 | } |
| 2716 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2717 | if (qemu_opts_foreach(&qemu_object_opts, |
| 2718 | user_creatable_add_opts_foreach, |
Daniel P. Berrangé | 334c43e | 2019-02-19 11:46:09 +0000 | [diff] [blame] | 2719 | NULL, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2720 | return 1; |
| 2721 | } |
| 2722 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2723 | list = collect_image_info_list(image_opts, filename, fmt, chain, |
| 2724 | force_share); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2725 | if (!list) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2726 | return 1; |
| 2727 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2728 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2729 | switch (output_format) { |
| 2730 | case OFORMAT_HUMAN: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2731 | dump_human_image_info_list(list); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2732 | break; |
| 2733 | case OFORMAT_JSON: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2734 | if (chain) { |
| 2735 | dump_json_image_info_list(list); |
| 2736 | } else { |
| 2737 | dump_json_image_info(list->value); |
| 2738 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2739 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2740 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2741 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2742 | qapi_free_ImageInfoList(list); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2743 | return 0; |
| 2744 | } |
| 2745 | |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 2746 | static int dump_map_entry(OutputFormat output_format, MapEntry *e, |
| 2747 | MapEntry *next) |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2748 | { |
| 2749 | switch (output_format) { |
| 2750 | case OFORMAT_HUMAN: |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2751 | if (e->data && !e->has_offset) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2752 | error_report("File contains external, encrypted or compressed clusters."); |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 2753 | return -1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2754 | } |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2755 | if (e->data && !e->zero) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2756 | printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n", |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2757 | e->start, e->length, |
| 2758 | e->has_offset ? e->offset : 0, |
| 2759 | e->has_filename ? e->filename : ""); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2760 | } |
| 2761 | /* This format ignores the distinction between 0, ZERO and ZERO|DATA. |
| 2762 | * Modify the flags here to allow more coalescing. |
| 2763 | */ |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2764 | if (next && (!next->data || next->zero)) { |
| 2765 | next->data = false; |
| 2766 | next->zero = true; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2767 | } |
| 2768 | break; |
| 2769 | case OFORMAT_JSON: |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2770 | printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64"," |
| 2771 | " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s", |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2772 | (e->start == 0 ? "[" : ",\n"), |
| 2773 | e->start, e->length, e->depth, |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2774 | e->zero ? "true" : "false", |
| 2775 | e->data ? "true" : "false"); |
| 2776 | if (e->has_offset) { |
Paolo Bonzini | c745bfb | 2013-09-11 18:47:52 +0200 | [diff] [blame] | 2777 | printf(", \"offset\": %"PRId64"", e->offset); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2778 | } |
| 2779 | putchar('}'); |
| 2780 | |
| 2781 | if (!next) { |
| 2782 | printf("]\n"); |
| 2783 | } |
| 2784 | break; |
| 2785 | } |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 2786 | return 0; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2787 | } |
| 2788 | |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 2789 | static int get_block_status(BlockDriverState *bs, int64_t offset, |
| 2790 | int64_t bytes, MapEntry *e) |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2791 | { |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 2792 | int ret; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2793 | int depth; |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 2794 | BlockDriverState *file; |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 2795 | bool has_offset; |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 2796 | int64_t map; |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 2797 | char *filename = NULL; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2798 | |
| 2799 | /* As an optimization, we could cache the current range of unallocated |
| 2800 | * clusters in each file of the chain, and avoid querying the same |
| 2801 | * range repeatedly. |
| 2802 | */ |
| 2803 | |
| 2804 | depth = 0; |
| 2805 | for (;;) { |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 2806 | ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2807 | if (ret < 0) { |
| 2808 | return ret; |
| 2809 | } |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 2810 | assert(bytes); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2811 | if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) { |
| 2812 | break; |
| 2813 | } |
Kevin Wolf | 760e006 | 2015-06-17 14:55:21 +0200 | [diff] [blame] | 2814 | bs = backing_bs(bs); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2815 | if (bs == NULL) { |
| 2816 | ret = 0; |
| 2817 | break; |
| 2818 | } |
| 2819 | |
| 2820 | depth++; |
| 2821 | } |
| 2822 | |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 2823 | has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID); |
| 2824 | |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 2825 | if (file && has_offset) { |
| 2826 | bdrv_refresh_filename(file); |
| 2827 | filename = file->filename; |
| 2828 | } |
| 2829 | |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 2830 | *e = (MapEntry) { |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 2831 | .start = offset, |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 2832 | .length = bytes, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 2833 | .data = !!(ret & BDRV_BLOCK_DATA), |
| 2834 | .zero = !!(ret & BDRV_BLOCK_ZERO), |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 2835 | .offset = map, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 2836 | .has_offset = has_offset, |
| 2837 | .depth = depth, |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 2838 | .has_filename = filename, |
| 2839 | .filename = filename, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 2840 | }; |
| 2841 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2842 | return 0; |
| 2843 | } |
| 2844 | |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2845 | static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next) |
| 2846 | { |
| 2847 | if (curr->length == 0) { |
| 2848 | return false; |
| 2849 | } |
| 2850 | if (curr->zero != next->zero || |
| 2851 | curr->data != next->data || |
| 2852 | curr->depth != next->depth || |
| 2853 | curr->has_filename != next->has_filename || |
| 2854 | curr->has_offset != next->has_offset) { |
| 2855 | return false; |
| 2856 | } |
| 2857 | if (curr->has_filename && strcmp(curr->filename, next->filename)) { |
| 2858 | return false; |
| 2859 | } |
| 2860 | if (curr->has_offset && curr->offset + curr->length != next->offset) { |
| 2861 | return false; |
| 2862 | } |
| 2863 | return true; |
| 2864 | } |
| 2865 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2866 | static int img_map(int argc, char **argv) |
| 2867 | { |
| 2868 | int c; |
| 2869 | OutputFormat output_format = OFORMAT_HUMAN; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2870 | BlockBackend *blk; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2871 | BlockDriverState *bs; |
| 2872 | const char *filename, *fmt, *output; |
| 2873 | int64_t length; |
| 2874 | MapEntry curr = { .length = 0 }, next; |
| 2875 | int ret = 0; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2876 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2877 | bool force_share = false; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2878 | |
| 2879 | fmt = NULL; |
| 2880 | output = NULL; |
| 2881 | for (;;) { |
| 2882 | int option_index = 0; |
| 2883 | static const struct option long_options[] = { |
| 2884 | {"help", no_argument, 0, 'h'}, |
| 2885 | {"format", required_argument, 0, 'f'}, |
| 2886 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2887 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2888 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2889 | {"force-share", no_argument, 0, 'U'}, |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2890 | {0, 0, 0, 0} |
| 2891 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2892 | c = getopt_long(argc, argv, ":f:hU", |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2893 | long_options, &option_index); |
| 2894 | if (c == -1) { |
| 2895 | break; |
| 2896 | } |
| 2897 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2898 | case ':': |
| 2899 | missing_argument(argv[optind - 1]); |
| 2900 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2901 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2902 | unrecognized_option(argv[optind - 1]); |
| 2903 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2904 | case 'h': |
| 2905 | help(); |
| 2906 | break; |
| 2907 | case 'f': |
| 2908 | fmt = optarg; |
| 2909 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2910 | case 'U': |
| 2911 | force_share = true; |
| 2912 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2913 | case OPTION_OUTPUT: |
| 2914 | output = optarg; |
| 2915 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2916 | case OPTION_OBJECT: { |
| 2917 | QemuOpts *opts; |
| 2918 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 2919 | optarg, true); |
| 2920 | if (!opts) { |
| 2921 | return 1; |
| 2922 | } |
| 2923 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2924 | case OPTION_IMAGE_OPTS: |
| 2925 | image_opts = true; |
| 2926 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2927 | } |
| 2928 | } |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2929 | if (optind != argc - 1) { |
| 2930 | error_exit("Expecting one image file name"); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2931 | } |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2932 | filename = argv[optind]; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2933 | |
| 2934 | if (output && !strcmp(output, "json")) { |
| 2935 | output_format = OFORMAT_JSON; |
| 2936 | } else if (output && !strcmp(output, "human")) { |
| 2937 | output_format = OFORMAT_HUMAN; |
| 2938 | } else if (output) { |
| 2939 | error_report("--output must be used with human or json as argument."); |
| 2940 | return 1; |
| 2941 | } |
| 2942 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2943 | if (qemu_opts_foreach(&qemu_object_opts, |
| 2944 | user_creatable_add_opts_foreach, |
Daniel P. Berrangé | 334c43e | 2019-02-19 11:46:09 +0000 | [diff] [blame] | 2945 | NULL, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2946 | return 1; |
| 2947 | } |
| 2948 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2949 | blk = img_open(image_opts, filename, fmt, 0, false, false, force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2950 | if (!blk) { |
| 2951 | return 1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2952 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2953 | bs = blk_bs(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2954 | |
| 2955 | if (output_format == OFORMAT_HUMAN) { |
| 2956 | printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File"); |
| 2957 | } |
| 2958 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 2959 | length = blk_getlength(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2960 | while (curr.start + curr.length < length) { |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 2961 | int64_t offset = curr.start + curr.length; |
| 2962 | int64_t n; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2963 | |
| 2964 | /* Probe up to 1 GiB at a time. */ |
Stefano Garzarella | 97ede57 | 2019-05-08 12:43:24 +0200 | [diff] [blame] | 2965 | n = MIN(1 * GiB, length - offset); |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 2966 | ret = get_block_status(bs, offset, n, &next); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2967 | |
| 2968 | if (ret < 0) { |
| 2969 | error_report("Could not read file metadata: %s", strerror(-ret)); |
| 2970 | goto out; |
| 2971 | } |
| 2972 | |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2973 | if (entry_mergeable(&curr, &next)) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2974 | curr.length += next.length; |
| 2975 | continue; |
| 2976 | } |
| 2977 | |
| 2978 | if (curr.length > 0) { |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 2979 | ret = dump_map_entry(output_format, &curr, &next); |
| 2980 | if (ret < 0) { |
| 2981 | goto out; |
| 2982 | } |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2983 | } |
| 2984 | curr = next; |
| 2985 | } |
| 2986 | |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 2987 | ret = dump_map_entry(output_format, &curr, NULL); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2988 | |
| 2989 | out: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2990 | blk_unref(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2991 | return ret < 0; |
| 2992 | } |
| 2993 | |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2994 | #define SNAPSHOT_LIST 1 |
| 2995 | #define SNAPSHOT_CREATE 2 |
| 2996 | #define SNAPSHOT_APPLY 3 |
| 2997 | #define SNAPSHOT_DELETE 4 |
| 2998 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2999 | static int img_snapshot(int argc, char **argv) |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3000 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3001 | BlockBackend *blk; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3002 | BlockDriverState *bs; |
| 3003 | QEMUSnapshotInfo sn; |
| 3004 | char *filename, *snapshot_name = NULL; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3005 | int c, ret = 0, bdrv_oflags; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3006 | int action = 0; |
| 3007 | qemu_timeval tv; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3008 | bool quiet = false; |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 3009 | Error *err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3010 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3011 | bool force_share = false; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3012 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3013 | bdrv_oflags = BDRV_O_RDWR; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3014 | /* Parse commandline parameters */ |
| 3015 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3016 | static const struct option long_options[] = { |
| 3017 | {"help", no_argument, 0, 'h'}, |
| 3018 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3019 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3020 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3021 | {0, 0, 0, 0} |
| 3022 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3023 | c = getopt_long(argc, argv, ":la:c:d:hqU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3024 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3025 | if (c == -1) { |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3026 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3027 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3028 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3029 | case ':': |
| 3030 | missing_argument(argv[optind - 1]); |
| 3031 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3032 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3033 | unrecognized_option(argv[optind - 1]); |
| 3034 | break; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3035 | case 'h': |
| 3036 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3037 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3038 | case 'l': |
| 3039 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3040 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3041 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3042 | } |
| 3043 | action = SNAPSHOT_LIST; |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 3044 | bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */ |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3045 | break; |
| 3046 | case 'a': |
| 3047 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3048 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3049 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3050 | } |
| 3051 | action = SNAPSHOT_APPLY; |
| 3052 | snapshot_name = optarg; |
| 3053 | break; |
| 3054 | case 'c': |
| 3055 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3056 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3057 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3058 | } |
| 3059 | action = SNAPSHOT_CREATE; |
| 3060 | snapshot_name = optarg; |
| 3061 | break; |
| 3062 | case 'd': |
| 3063 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3064 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3065 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3066 | } |
| 3067 | action = SNAPSHOT_DELETE; |
| 3068 | snapshot_name = optarg; |
| 3069 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3070 | case 'q': |
| 3071 | quiet = true; |
| 3072 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3073 | case 'U': |
| 3074 | force_share = true; |
| 3075 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3076 | case OPTION_OBJECT: { |
| 3077 | QemuOpts *opts; |
| 3078 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 3079 | optarg, true); |
| 3080 | if (!opts) { |
| 3081 | return 1; |
| 3082 | } |
| 3083 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3084 | case OPTION_IMAGE_OPTS: |
| 3085 | image_opts = true; |
| 3086 | break; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3087 | } |
| 3088 | } |
| 3089 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 3090 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3091 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3092 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3093 | filename = argv[optind++]; |
| 3094 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3095 | if (qemu_opts_foreach(&qemu_object_opts, |
| 3096 | user_creatable_add_opts_foreach, |
Daniel P. Berrangé | 334c43e | 2019-02-19 11:46:09 +0000 | [diff] [blame] | 3097 | NULL, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3098 | return 1; |
| 3099 | } |
| 3100 | |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3101 | /* Open the image */ |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3102 | blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet, |
| 3103 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3104 | if (!blk) { |
| 3105 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3106 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3107 | bs = blk_bs(blk); |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3108 | |
| 3109 | /* Perform the requested action */ |
| 3110 | switch(action) { |
| 3111 | case SNAPSHOT_LIST: |
| 3112 | dump_snapshots(bs); |
| 3113 | break; |
| 3114 | |
| 3115 | case SNAPSHOT_CREATE: |
| 3116 | memset(&sn, 0, sizeof(sn)); |
| 3117 | pstrcpy(sn.name, sizeof(sn.name), snapshot_name); |
| 3118 | |
| 3119 | qemu_gettimeofday(&tv); |
| 3120 | sn.date_sec = tv.tv_sec; |
| 3121 | sn.date_nsec = tv.tv_usec * 1000; |
| 3122 | |
| 3123 | ret = bdrv_snapshot_create(bs, &sn); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3124 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3125 | error_report("Could not create snapshot '%s': %d (%s)", |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3126 | snapshot_name, ret, strerror(-ret)); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3127 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3128 | break; |
| 3129 | |
| 3130 | case SNAPSHOT_APPLY: |
Kevin Wolf | 0b62bcb | 2017-11-20 15:28:41 +0100 | [diff] [blame] | 3131 | ret = bdrv_snapshot_goto(bs, snapshot_name, &err); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3132 | if (ret) { |
Kevin Wolf | 0b62bcb | 2017-11-20 15:28:41 +0100 | [diff] [blame] | 3133 | error_reportf_err(err, "Could not apply snapshot '%s': ", |
| 3134 | snapshot_name); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3135 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3136 | break; |
| 3137 | |
| 3138 | case SNAPSHOT_DELETE: |
Daniel Henrique Barboza | 8c04093 | 2018-11-07 11:09:59 -0200 | [diff] [blame] | 3139 | ret = bdrv_snapshot_find(bs, &sn, snapshot_name); |
| 3140 | if (ret < 0) { |
| 3141 | error_report("Could not delete snapshot '%s': snapshot not " |
| 3142 | "found", snapshot_name); |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 3143 | ret = 1; |
Daniel Henrique Barboza | 8c04093 | 2018-11-07 11:09:59 -0200 | [diff] [blame] | 3144 | } else { |
| 3145 | ret = bdrv_snapshot_delete(bs, sn.id_str, sn.name, &err); |
| 3146 | if (ret < 0) { |
| 3147 | error_reportf_err(err, "Could not delete snapshot '%s': ", |
| 3148 | snapshot_name); |
| 3149 | ret = 1; |
| 3150 | } |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3151 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3152 | break; |
| 3153 | } |
| 3154 | |
| 3155 | /* Cleanup */ |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3156 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3157 | if (ret) { |
| 3158 | return 1; |
| 3159 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3160 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3161 | } |
| 3162 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3163 | static int img_rebase(int argc, char **argv) |
| 3164 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3165 | BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL; |
Paolo Bonzini | 396374c | 2016-02-25 23:53:54 +0100 | [diff] [blame] | 3166 | uint8_t *buf_old = NULL; |
| 3167 | uint8_t *buf_new = NULL; |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3168 | BlockDriverState *bs = NULL, *prefix_chain_bs = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3169 | char *filename; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3170 | const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg; |
| 3171 | int c, flags, src_flags, ret; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3172 | bool writethrough, src_writethrough; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3173 | int unsafe = 0; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3174 | bool force_share = false; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3175 | int progress = 0; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3176 | bool quiet = false; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 3177 | Error *local_err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3178 | bool image_opts = false; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3179 | |
| 3180 | /* Parse commandline parameters */ |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 3181 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3182 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3183 | src_cache = BDRV_DEFAULT_CACHE; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3184 | out_baseimg = NULL; |
| 3185 | out_basefmt = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3186 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3187 | static const struct option long_options[] = { |
| 3188 | {"help", no_argument, 0, 'h'}, |
| 3189 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3190 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3191 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3192 | {0, 0, 0, 0} |
| 3193 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3194 | c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3195 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3196 | if (c == -1) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3197 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3198 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3199 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3200 | case ':': |
| 3201 | missing_argument(argv[optind - 1]); |
| 3202 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3203 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3204 | unrecognized_option(argv[optind - 1]); |
| 3205 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3206 | case 'h': |
| 3207 | help(); |
| 3208 | return 0; |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 3209 | case 'f': |
| 3210 | fmt = optarg; |
| 3211 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3212 | case 'F': |
| 3213 | out_basefmt = optarg; |
| 3214 | break; |
| 3215 | case 'b': |
| 3216 | out_baseimg = optarg; |
| 3217 | break; |
| 3218 | case 'u': |
| 3219 | unsafe = 1; |
| 3220 | break; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3221 | case 'p': |
| 3222 | progress = 1; |
| 3223 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3224 | case 't': |
| 3225 | cache = optarg; |
| 3226 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3227 | case 'T': |
| 3228 | src_cache = optarg; |
| 3229 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3230 | case 'q': |
| 3231 | quiet = true; |
| 3232 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3233 | case OPTION_OBJECT: { |
| 3234 | QemuOpts *opts; |
| 3235 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 3236 | optarg, true); |
| 3237 | if (!opts) { |
| 3238 | return 1; |
| 3239 | } |
| 3240 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3241 | case OPTION_IMAGE_OPTS: |
| 3242 | image_opts = true; |
| 3243 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3244 | case 'U': |
| 3245 | force_share = true; |
| 3246 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3247 | } |
| 3248 | } |
| 3249 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3250 | if (quiet) { |
| 3251 | progress = 0; |
| 3252 | } |
| 3253 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3254 | if (optind != argc - 1) { |
| 3255 | error_exit("Expecting one image file name"); |
| 3256 | } |
| 3257 | if (!unsafe && !out_baseimg) { |
| 3258 | error_exit("Must specify backing file (-b) or use unsafe mode (-u)"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3259 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3260 | filename = argv[optind++]; |
| 3261 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3262 | if (qemu_opts_foreach(&qemu_object_opts, |
| 3263 | user_creatable_add_opts_foreach, |
Daniel P. Berrangé | 334c43e | 2019-02-19 11:46:09 +0000 | [diff] [blame] | 3264 | NULL, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3265 | return 1; |
| 3266 | } |
| 3267 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3268 | qemu_progress_init(progress, 2.0); |
| 3269 | qemu_progress_print(0, 100); |
| 3270 | |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3271 | flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0); |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3272 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3273 | if (ret < 0) { |
| 3274 | error_report("Invalid cache option: %s", cache); |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3275 | goto out; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3276 | } |
| 3277 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3278 | src_flags = 0; |
| 3279 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3280 | if (ret < 0) { |
| 3281 | error_report("Invalid source cache option: %s", src_cache); |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3282 | goto out; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3283 | } |
| 3284 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3285 | /* The source files are opened read-only, don't care about WCE */ |
| 3286 | assert((src_flags & BDRV_O_RDWR) == 0); |
| 3287 | (void) src_writethrough; |
| 3288 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3289 | /* |
| 3290 | * Open the images. |
| 3291 | * |
| 3292 | * Ignore the old backing file for unsafe rebase in case we want to correct |
| 3293 | * the reference to a renamed or moved backing file. |
| 3294 | */ |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3295 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 3296 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3297 | if (!blk) { |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3298 | ret = -1; |
| 3299 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3300 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3301 | bs = blk_bs(blk); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3302 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3303 | if (out_basefmt != NULL) { |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3304 | if (bdrv_find_format(out_basefmt) == NULL) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3305 | error_report("Invalid format name: '%s'", out_basefmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3306 | ret = -1; |
| 3307 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3308 | } |
| 3309 | } |
| 3310 | |
| 3311 | /* For safe rebasing we need to compare old and new backing file */ |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3312 | if (!unsafe) { |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3313 | QDict *options = NULL; |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3314 | BlockDriverState *base_bs = backing_bs(bs); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3315 | |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3316 | if (base_bs) { |
Kevin Wolf | d861ab3 | 2019-04-25 14:25:10 +0200 | [diff] [blame] | 3317 | blk_old_backing = blk_new(qemu_get_aio_context(), |
| 3318 | BLK_PERM_CONSISTENT_READ, |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3319 | BLK_PERM_ALL); |
| 3320 | ret = blk_insert_bs(blk_old_backing, base_bs, |
| 3321 | &local_err); |
| 3322 | if (ret < 0) { |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3323 | error_reportf_err(local_err, |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3324 | "Could not reuse old backing file '%s': ", |
| 3325 | base_bs->filename); |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3326 | goto out; |
| 3327 | } |
| 3328 | } else { |
| 3329 | blk_old_backing = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3330 | } |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3331 | |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3332 | if (out_baseimg[0]) { |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3333 | const char *overlay_filename; |
| 3334 | char *out_real_path; |
| 3335 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3336 | options = qdict_new(); |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3337 | if (out_basefmt) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 3338 | qdict_put_str(options, "driver", out_basefmt); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3339 | } |
| 3340 | if (force_share) { |
| 3341 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3342 | } |
| 3343 | |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3344 | bdrv_refresh_filename(bs); |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3345 | overlay_filename = bs->exact_filename[0] ? bs->exact_filename |
| 3346 | : bs->filename; |
Max Reitz | 645ae7d | 2019-02-01 20:29:14 +0100 | [diff] [blame] | 3347 | out_real_path = |
| 3348 | bdrv_get_full_backing_filename_from_filename(overlay_filename, |
| 3349 | out_baseimg, |
| 3350 | &local_err); |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3351 | if (local_err) { |
| 3352 | error_reportf_err(local_err, |
| 3353 | "Could not resolve backing filename: "); |
| 3354 | ret = -1; |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3355 | goto out; |
| 3356 | } |
| 3357 | |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3358 | /* |
| 3359 | * Find out whether we rebase an image on top of a previous image |
| 3360 | * in its chain. |
| 3361 | */ |
| 3362 | prefix_chain_bs = bdrv_find_backing_image(bs, out_real_path); |
Sam Eiderman | 330c729 | 2019-05-23 19:33:37 +0300 | [diff] [blame] | 3363 | if (prefix_chain_bs) { |
| 3364 | g_free(out_real_path); |
Kevin Wolf | d861ab3 | 2019-04-25 14:25:10 +0200 | [diff] [blame] | 3365 | blk_new_backing = blk_new(qemu_get_aio_context(), |
| 3366 | BLK_PERM_CONSISTENT_READ, |
Sam Eiderman | 330c729 | 2019-05-23 19:33:37 +0300 | [diff] [blame] | 3367 | BLK_PERM_ALL); |
| 3368 | ret = blk_insert_bs(blk_new_backing, prefix_chain_bs, |
| 3369 | &local_err); |
| 3370 | if (ret < 0) { |
| 3371 | error_reportf_err(local_err, |
| 3372 | "Could not reuse backing file '%s': ", |
| 3373 | out_baseimg); |
| 3374 | goto out; |
| 3375 | } |
| 3376 | } else { |
| 3377 | blk_new_backing = blk_new_open(out_real_path, NULL, |
| 3378 | options, src_flags, &local_err); |
| 3379 | g_free(out_real_path); |
| 3380 | if (!blk_new_backing) { |
| 3381 | error_reportf_err(local_err, |
| 3382 | "Could not open new backing file '%s': ", |
| 3383 | out_baseimg); |
| 3384 | ret = -1; |
| 3385 | goto out; |
| 3386 | } |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3387 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3388 | } |
| 3389 | } |
| 3390 | |
| 3391 | /* |
| 3392 | * Check each unallocated cluster in the COW file. If it is unallocated, |
| 3393 | * accesses go to the backing file. We must therefore compare this cluster |
| 3394 | * in the old and new backing file, and if they differ we need to copy it |
| 3395 | * from the old backing file into the COW file. |
| 3396 | * |
| 3397 | * If qemu-img crashes during this step, no harm is done. The content of |
| 3398 | * the image is the same as the original one at any time. |
| 3399 | */ |
| 3400 | if (!unsafe) { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3401 | int64_t size; |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3402 | int64_t old_backing_size = 0; |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3403 | int64_t new_backing_size = 0; |
| 3404 | uint64_t offset; |
| 3405 | int64_t n; |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 3406 | float local_progress = 0; |
TeLeMan | d6771bf | 2010-02-08 16:20:00 +0800 | [diff] [blame] | 3407 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3408 | buf_old = blk_blockalign(blk, IO_BUF_SIZE); |
| 3409 | buf_new = blk_blockalign(blk, IO_BUF_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3410 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3411 | size = blk_getlength(blk); |
| 3412 | if (size < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3413 | error_report("Could not get size of '%s': %s", |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3414 | filename, strerror(-size)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3415 | ret = -1; |
| 3416 | goto out; |
| 3417 | } |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3418 | if (blk_old_backing) { |
| 3419 | old_backing_size = blk_getlength(blk_old_backing); |
| 3420 | if (old_backing_size < 0) { |
| 3421 | char backing_name[PATH_MAX]; |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3422 | |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3423 | bdrv_get_backing_filename(bs, backing_name, |
| 3424 | sizeof(backing_name)); |
| 3425 | error_report("Could not get size of '%s': %s", |
| 3426 | backing_name, strerror(-old_backing_size)); |
| 3427 | ret = -1; |
| 3428 | goto out; |
| 3429 | } |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3430 | } |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3431 | if (blk_new_backing) { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3432 | new_backing_size = blk_getlength(blk_new_backing); |
| 3433 | if (new_backing_size < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3434 | error_report("Could not get size of '%s': %s", |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3435 | out_baseimg, strerror(-new_backing_size)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3436 | ret = -1; |
| 3437 | goto out; |
| 3438 | } |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3439 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3440 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3441 | if (size != 0) { |
| 3442 | local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE)); |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 3443 | } |
| 3444 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3445 | for (offset = 0; offset < size; offset += n) { |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3446 | bool buf_old_is_zero = false; |
| 3447 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3448 | /* How many bytes can we handle with the next read? */ |
| 3449 | n = MIN(IO_BUF_SIZE, size - offset); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3450 | |
| 3451 | /* If the cluster is allocated, we don't need to take action */ |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3452 | ret = bdrv_is_allocated(bs, offset, n, &n); |
Paolo Bonzini | d663640 | 2013-09-04 19:00:25 +0200 | [diff] [blame] | 3453 | if (ret < 0) { |
| 3454 | error_report("error while reading image metadata: %s", |
| 3455 | strerror(-ret)); |
| 3456 | goto out; |
| 3457 | } |
Kevin Wolf | cc60e32 | 2010-04-29 14:47:48 +0200 | [diff] [blame] | 3458 | if (ret) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3459 | continue; |
| 3460 | } |
| 3461 | |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3462 | if (prefix_chain_bs) { |
| 3463 | /* |
| 3464 | * If cluster wasn't changed since prefix_chain, we don't need |
| 3465 | * to take action |
| 3466 | */ |
| 3467 | ret = bdrv_is_allocated_above(backing_bs(bs), prefix_chain_bs, |
| 3468 | offset, n, &n); |
| 3469 | if (ret < 0) { |
| 3470 | error_report("error while reading image metadata: %s", |
| 3471 | strerror(-ret)); |
| 3472 | goto out; |
| 3473 | } |
| 3474 | if (!ret) { |
| 3475 | continue; |
| 3476 | } |
| 3477 | } |
| 3478 | |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3479 | /* |
| 3480 | * Read old and new backing file and take into consideration that |
| 3481 | * backing files may be smaller than the COW image. |
| 3482 | */ |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3483 | if (offset >= old_backing_size) { |
| 3484 | memset(buf_old, 0, n); |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3485 | buf_old_is_zero = true; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3486 | } else { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3487 | if (offset + n > old_backing_size) { |
| 3488 | n = old_backing_size - offset; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3489 | } |
| 3490 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3491 | ret = blk_pread(blk_old_backing, offset, buf_old, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3492 | if (ret < 0) { |
| 3493 | error_report("error while reading from old backing file"); |
| 3494 | goto out; |
| 3495 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3496 | } |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3497 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3498 | if (offset >= new_backing_size || !blk_new_backing) { |
| 3499 | memset(buf_new, 0, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3500 | } else { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3501 | if (offset + n > new_backing_size) { |
| 3502 | n = new_backing_size - offset; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3503 | } |
| 3504 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3505 | ret = blk_pread(blk_new_backing, offset, buf_new, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3506 | if (ret < 0) { |
| 3507 | error_report("error while reading from new backing file"); |
| 3508 | goto out; |
| 3509 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3510 | } |
| 3511 | |
| 3512 | /* If they differ, we need to write to the COW file */ |
| 3513 | uint64_t written = 0; |
| 3514 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3515 | while (written < n) { |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 3516 | int64_t pnum; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3517 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3518 | if (compare_buffers(buf_old + written, buf_new + written, |
| 3519 | n - written, &pnum)) |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3520 | { |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3521 | if (buf_old_is_zero) { |
| 3522 | ret = blk_pwrite_zeroes(blk, offset + written, pnum, 0); |
| 3523 | } else { |
| 3524 | ret = blk_pwrite(blk, offset + written, |
| 3525 | buf_old + written, pnum, 0); |
| 3526 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3527 | if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3528 | error_report("Error while writing to COW image: %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3529 | strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3530 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3531 | } |
| 3532 | } |
| 3533 | |
| 3534 | written += pnum; |
| 3535 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3536 | qemu_progress_print(local_progress, 100); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3537 | } |
| 3538 | } |
| 3539 | |
| 3540 | /* |
| 3541 | * Change the backing file. All clusters that are different from the old |
| 3542 | * backing file are overwritten in the COW file now, so the visible content |
| 3543 | * doesn't change when we switch the backing file. |
| 3544 | */ |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3545 | if (out_baseimg && *out_baseimg) { |
| 3546 | ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt); |
| 3547 | } else { |
| 3548 | ret = bdrv_change_backing_file(bs, NULL, NULL); |
| 3549 | } |
| 3550 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3551 | if (ret == -ENOSPC) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3552 | error_report("Could not change the backing file to '%s': No " |
| 3553 | "space left in the file header", out_baseimg); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3554 | } else if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3555 | error_report("Could not change the backing file to '%s': %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3556 | out_baseimg, strerror(-ret)); |
| 3557 | } |
| 3558 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3559 | qemu_progress_print(100, 0); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3560 | /* |
| 3561 | * TODO At this point it is possible to check if any clusters that are |
| 3562 | * allocated in the COW file are the same in the backing file. If so, they |
| 3563 | * could be dropped from the COW file. Don't do this before switching the |
| 3564 | * backing file, in case of a crash this would lead to corruption. |
| 3565 | */ |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3566 | out: |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3567 | qemu_progress_end(); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3568 | /* Cleanup */ |
| 3569 | if (!unsafe) { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3570 | blk_unref(blk_old_backing); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3571 | blk_unref(blk_new_backing); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3572 | } |
Paolo Bonzini | 396374c | 2016-02-25 23:53:54 +0100 | [diff] [blame] | 3573 | qemu_vfree(buf_old); |
| 3574 | qemu_vfree(buf_new); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3575 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3576 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3577 | if (ret) { |
| 3578 | return 1; |
| 3579 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3580 | return 0; |
| 3581 | } |
| 3582 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3583 | static int img_resize(int argc, char **argv) |
| 3584 | { |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 3585 | Error *err = NULL; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3586 | int c, ret, relative; |
| 3587 | const char *filename, *fmt, *size; |
Max Reitz | 5279b30 | 2018-04-21 18:39:57 +0200 | [diff] [blame] | 3588 | int64_t n, total_size, current_size, new_size; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3589 | bool quiet = false; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3590 | BlockBackend *blk = NULL; |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3591 | PreallocMode prealloc = PREALLOC_MODE_OFF; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3592 | QemuOpts *param; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3593 | |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3594 | static QemuOptsList resize_options = { |
| 3595 | .name = "resize_options", |
| 3596 | .head = QTAILQ_HEAD_INITIALIZER(resize_options.head), |
| 3597 | .desc = { |
| 3598 | { |
| 3599 | .name = BLOCK_OPT_SIZE, |
| 3600 | .type = QEMU_OPT_SIZE, |
| 3601 | .help = "Virtual disk size" |
| 3602 | }, { |
| 3603 | /* end of list */ |
| 3604 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3605 | }, |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3606 | }; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3607 | bool image_opts = false; |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3608 | bool shrink = false; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3609 | |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 3610 | /* Remove size from argv manually so that negative numbers are not treated |
| 3611 | * as options by getopt. */ |
| 3612 | if (argc < 3) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3613 | error_exit("Not enough arguments"); |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 3614 | return 1; |
| 3615 | } |
| 3616 | |
| 3617 | size = argv[--argc]; |
| 3618 | |
| 3619 | /* Parse getopt arguments */ |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3620 | fmt = NULL; |
| 3621 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3622 | static const struct option long_options[] = { |
| 3623 | {"help", no_argument, 0, 'h'}, |
| 3624 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3625 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3626 | {"preallocation", required_argument, 0, OPTION_PREALLOCATION}, |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3627 | {"shrink", no_argument, 0, OPTION_SHRINK}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3628 | {0, 0, 0, 0} |
| 3629 | }; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3630 | c = getopt_long(argc, argv, ":f:hq", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3631 | long_options, NULL); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3632 | if (c == -1) { |
| 3633 | break; |
| 3634 | } |
| 3635 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3636 | case ':': |
| 3637 | missing_argument(argv[optind - 1]); |
| 3638 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3639 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3640 | unrecognized_option(argv[optind - 1]); |
| 3641 | break; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3642 | case 'h': |
| 3643 | help(); |
| 3644 | break; |
| 3645 | case 'f': |
| 3646 | fmt = optarg; |
| 3647 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3648 | case 'q': |
| 3649 | quiet = true; |
| 3650 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3651 | case OPTION_OBJECT: { |
| 3652 | QemuOpts *opts; |
| 3653 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 3654 | optarg, true); |
| 3655 | if (!opts) { |
| 3656 | return 1; |
| 3657 | } |
| 3658 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3659 | case OPTION_IMAGE_OPTS: |
| 3660 | image_opts = true; |
| 3661 | break; |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3662 | case OPTION_PREALLOCATION: |
Marc-André Lureau | f7abe0e | 2017-08-24 10:46:10 +0200 | [diff] [blame] | 3663 | prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg, |
Markus Armbruster | 06c60b6 | 2017-08-24 10:45:57 +0200 | [diff] [blame] | 3664 | PREALLOC_MODE__MAX, NULL); |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3665 | if (prealloc == PREALLOC_MODE__MAX) { |
| 3666 | error_report("Invalid preallocation mode '%s'", optarg); |
| 3667 | return 1; |
| 3668 | } |
| 3669 | break; |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3670 | case OPTION_SHRINK: |
| 3671 | shrink = true; |
| 3672 | break; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3673 | } |
| 3674 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 3675 | if (optind != argc - 1) { |
Max Reitz | be8fbd4 | 2018-02-05 17:27:45 +0100 | [diff] [blame] | 3676 | error_exit("Expecting image file name and size"); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3677 | } |
| 3678 | filename = argv[optind++]; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3679 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3680 | if (qemu_opts_foreach(&qemu_object_opts, |
| 3681 | user_creatable_add_opts_foreach, |
Daniel P. Berrangé | 334c43e | 2019-02-19 11:46:09 +0000 | [diff] [blame] | 3682 | NULL, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3683 | return 1; |
| 3684 | } |
| 3685 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3686 | /* Choose grow, shrink, or absolute resize mode */ |
| 3687 | switch (size[0]) { |
| 3688 | case '+': |
| 3689 | relative = 1; |
| 3690 | size++; |
| 3691 | break; |
| 3692 | case '-': |
| 3693 | relative = -1; |
| 3694 | size++; |
| 3695 | break; |
| 3696 | default: |
| 3697 | relative = 0; |
| 3698 | break; |
| 3699 | } |
| 3700 | |
| 3701 | /* Parse size */ |
Peter Crosthwaite | 87ea75d | 2014-01-01 18:49:17 -0800 | [diff] [blame] | 3702 | param = qemu_opts_create(&resize_options, NULL, 0, &error_abort); |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 3703 | qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err); |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 3704 | if (err) { |
| 3705 | error_report_err(err); |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3706 | ret = -1; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3707 | qemu_opts_del(param); |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3708 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3709 | } |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3710 | n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0); |
| 3711 | qemu_opts_del(param); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3712 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 3713 | blk = img_open(image_opts, filename, fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3714 | BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet, |
| 3715 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3716 | if (!blk) { |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3717 | ret = -1; |
| 3718 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3719 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3720 | |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3721 | current_size = blk_getlength(blk); |
| 3722 | if (current_size < 0) { |
| 3723 | error_report("Failed to inquire current image length: %s", |
| 3724 | strerror(-current_size)); |
| 3725 | ret = -1; |
| 3726 | goto out; |
| 3727 | } |
| 3728 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3729 | if (relative) { |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3730 | total_size = current_size + n * relative; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3731 | } else { |
| 3732 | total_size = n; |
| 3733 | } |
| 3734 | if (total_size <= 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3735 | error_report("New image size must be positive"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3736 | ret = -1; |
| 3737 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3738 | } |
| 3739 | |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3740 | if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) { |
| 3741 | error_report("Preallocation can only be used for growing images"); |
| 3742 | ret = -1; |
| 3743 | goto out; |
| 3744 | } |
| 3745 | |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3746 | if (total_size < current_size && !shrink) { |
| 3747 | warn_report("Shrinking an image will delete all data beyond the " |
| 3748 | "shrunken image's end. Before performing such an " |
| 3749 | "operation, make sure there is no important data there."); |
| 3750 | |
| 3751 | if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") != 0) { |
| 3752 | error_report( |
| 3753 | "Use the --shrink option to perform a shrink operation."); |
| 3754 | ret = -1; |
| 3755 | goto out; |
| 3756 | } else { |
| 3757 | warn_report("Using the --shrink option will suppress this message. " |
| 3758 | "Note that future versions of qemu-img may refuse to " |
| 3759 | "shrink images without this option."); |
| 3760 | } |
| 3761 | } |
| 3762 | |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3763 | ret = blk_truncate(blk, total_size, prealloc, &err); |
Max Reitz | 5279b30 | 2018-04-21 18:39:57 +0200 | [diff] [blame] | 3764 | if (ret < 0) { |
Max Reitz | ed3d2ec | 2017-03-28 22:51:27 +0200 | [diff] [blame] | 3765 | error_report_err(err); |
Max Reitz | 5279b30 | 2018-04-21 18:39:57 +0200 | [diff] [blame] | 3766 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3767 | } |
Max Reitz | 5279b30 | 2018-04-21 18:39:57 +0200 | [diff] [blame] | 3768 | |
| 3769 | new_size = blk_getlength(blk); |
| 3770 | if (new_size < 0) { |
| 3771 | error_report("Failed to verify truncated image length: %s", |
| 3772 | strerror(-new_size)); |
| 3773 | ret = -1; |
| 3774 | goto out; |
| 3775 | } |
| 3776 | |
| 3777 | /* Some block drivers implement a truncation method, but only so |
| 3778 | * the user can cause qemu to refresh the image's size from disk. |
| 3779 | * The idea is that the user resizes the image outside of qemu and |
| 3780 | * then invokes block_resize to inform qemu about it. |
| 3781 | * (This includes iscsi and file-posix for device files.) |
| 3782 | * Of course, that is not the behavior someone invoking |
| 3783 | * qemu-img resize would find useful, so we catch that behavior |
| 3784 | * here and tell the user. */ |
| 3785 | if (new_size != total_size && new_size == current_size) { |
| 3786 | error_report("Image was not resized; resizing may not be supported " |
| 3787 | "for this image"); |
| 3788 | ret = -1; |
| 3789 | goto out; |
| 3790 | } |
| 3791 | |
| 3792 | if (new_size != total_size) { |
| 3793 | warn_report("Image should have been resized to %" PRIi64 |
| 3794 | " bytes, but was resized to %" PRIi64 " bytes", |
| 3795 | total_size, new_size); |
| 3796 | } |
| 3797 | |
| 3798 | qprintf(quiet, "Image resized.\n"); |
| 3799 | |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3800 | out: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3801 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3802 | if (ret) { |
| 3803 | return 1; |
| 3804 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3805 | return 0; |
| 3806 | } |
| 3807 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3808 | static void amend_status_cb(BlockDriverState *bs, |
Max Reitz | 8b13976 | 2015-07-27 17:51:32 +0200 | [diff] [blame] | 3809 | int64_t offset, int64_t total_work_size, |
| 3810 | void *opaque) |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3811 | { |
| 3812 | qemu_progress_print(100.f * offset / total_work_size, 0); |
| 3813 | } |
| 3814 | |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 3815 | static int print_amend_option_help(const char *format) |
| 3816 | { |
| 3817 | BlockDriver *drv; |
| 3818 | |
| 3819 | /* Find driver and parse its options */ |
| 3820 | drv = bdrv_find_format(format); |
| 3821 | if (!drv) { |
| 3822 | error_report("Unknown file format '%s'", format); |
| 3823 | return 1; |
| 3824 | } |
| 3825 | |
| 3826 | if (!drv->bdrv_amend_options) { |
| 3827 | error_report("Format driver '%s' does not support option amendment", |
| 3828 | format); |
| 3829 | return 1; |
| 3830 | } |
| 3831 | |
| 3832 | /* Every driver supporting amendment must have create_opts */ |
| 3833 | assert(drv->create_opts); |
| 3834 | |
| 3835 | printf("Creation options for '%s':\n", format); |
Max Reitz | 6389871 | 2018-10-19 18:49:25 +0200 | [diff] [blame] | 3836 | qemu_opts_print_help(drv->create_opts, false); |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 3837 | printf("\nNote that not all of these options may be amendable.\n"); |
| 3838 | return 0; |
| 3839 | } |
| 3840 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3841 | static int img_amend(int argc, char **argv) |
| 3842 | { |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 3843 | Error *err = NULL; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3844 | int c, ret = 0; |
| 3845 | char *options = NULL; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 3846 | QemuOptsList *create_opts = NULL; |
| 3847 | QemuOpts *opts = NULL; |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 3848 | const char *fmt = NULL, *filename, *cache; |
| 3849 | int flags; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3850 | bool writethrough; |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3851 | bool quiet = false, progress = false; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3852 | BlockBackend *blk = NULL; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3853 | BlockDriverState *bs = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3854 | bool image_opts = false; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3855 | |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 3856 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3857 | for (;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3858 | static const struct option long_options[] = { |
| 3859 | {"help", no_argument, 0, 'h'}, |
| 3860 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3861 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3862 | {0, 0, 0, 0} |
| 3863 | }; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3864 | c = getopt_long(argc, argv, ":ho:f:t:pq", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3865 | long_options, NULL); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3866 | if (c == -1) { |
| 3867 | break; |
| 3868 | } |
| 3869 | |
| 3870 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3871 | case ':': |
| 3872 | missing_argument(argv[optind - 1]); |
| 3873 | break; |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 3874 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3875 | unrecognized_option(argv[optind - 1]); |
| 3876 | break; |
| 3877 | case 'h': |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 3878 | help(); |
| 3879 | break; |
| 3880 | case 'o': |
| 3881 | if (!is_valid_option_list(optarg)) { |
| 3882 | error_report("Invalid option list: %s", optarg); |
| 3883 | ret = -1; |
| 3884 | goto out_no_progress; |
| 3885 | } |
| 3886 | if (!options) { |
| 3887 | options = g_strdup(optarg); |
| 3888 | } else { |
| 3889 | char *old_options = options; |
| 3890 | options = g_strdup_printf("%s,%s", options, optarg); |
| 3891 | g_free(old_options); |
| 3892 | } |
| 3893 | break; |
| 3894 | case 'f': |
| 3895 | fmt = optarg; |
| 3896 | break; |
| 3897 | case 't': |
| 3898 | cache = optarg; |
| 3899 | break; |
| 3900 | case 'p': |
| 3901 | progress = true; |
| 3902 | break; |
| 3903 | case 'q': |
| 3904 | quiet = true; |
| 3905 | break; |
| 3906 | case OPTION_OBJECT: |
| 3907 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 3908 | optarg, true); |
| 3909 | if (!opts) { |
| 3910 | ret = -1; |
| 3911 | goto out_no_progress; |
| 3912 | } |
| 3913 | break; |
| 3914 | case OPTION_IMAGE_OPTS: |
| 3915 | image_opts = true; |
| 3916 | break; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3917 | } |
| 3918 | } |
| 3919 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3920 | if (!options) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3921 | error_exit("Must specify options (-o)"); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3922 | } |
| 3923 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3924 | if (qemu_opts_foreach(&qemu_object_opts, |
| 3925 | user_creatable_add_opts_foreach, |
Daniel P. Berrangé | 334c43e | 2019-02-19 11:46:09 +0000 | [diff] [blame] | 3926 | NULL, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3927 | ret = -1; |
| 3928 | goto out_no_progress; |
| 3929 | } |
| 3930 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3931 | if (quiet) { |
| 3932 | progress = false; |
| 3933 | } |
| 3934 | qemu_progress_init(progress, 1.0); |
| 3935 | |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 3936 | filename = (optind == argc - 1) ? argv[argc - 1] : NULL; |
| 3937 | if (fmt && has_help_option(options)) { |
| 3938 | /* If a format is explicitly specified (and possibly no filename is |
| 3939 | * given), print option help here */ |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 3940 | ret = print_amend_option_help(fmt); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 3941 | goto out; |
| 3942 | } |
| 3943 | |
| 3944 | if (optind != argc - 1) { |
Max Reitz | b2f27e4 | 2014-10-27 11:12:52 +0100 | [diff] [blame] | 3945 | error_report("Expecting one image file name"); |
| 3946 | ret = -1; |
| 3947 | goto out; |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 3948 | } |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3949 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3950 | flags = BDRV_O_RDWR; |
| 3951 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 3952 | if (ret < 0) { |
| 3953 | error_report("Invalid cache option: %s", cache); |
| 3954 | goto out; |
| 3955 | } |
| 3956 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3957 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 3958 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3959 | if (!blk) { |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3960 | ret = -1; |
| 3961 | goto out; |
| 3962 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3963 | bs = blk_bs(blk); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3964 | |
| 3965 | fmt = bs->drv->format_name; |
| 3966 | |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 3967 | if (has_help_option(options)) { |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 3968 | /* If the format was auto-detected, print option help here */ |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 3969 | ret = print_amend_option_help(fmt); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3970 | goto out; |
| 3971 | } |
| 3972 | |
Max Reitz | 1f99668 | 2018-05-09 23:00:17 +0200 | [diff] [blame] | 3973 | if (!bs->drv->bdrv_amend_options) { |
| 3974 | error_report("Format driver '%s' does not support option amendment", |
Max Reitz | b2439d2 | 2014-12-02 18:32:47 +0100 | [diff] [blame] | 3975 | fmt); |
| 3976 | ret = -1; |
| 3977 | goto out; |
| 3978 | } |
| 3979 | |
Max Reitz | 1f99668 | 2018-05-09 23:00:17 +0200 | [diff] [blame] | 3980 | /* Every driver supporting amendment must have create_opts */ |
| 3981 | assert(bs->drv->create_opts); |
| 3982 | |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 3983 | create_opts = qemu_opts_append(create_opts, bs->drv->create_opts); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 3984 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
Paolo Bonzini | ece9086 | 2017-01-04 15:56:24 +0100 | [diff] [blame] | 3985 | qemu_opts_do_parse(opts, options, NULL, &err); |
| 3986 | if (err) { |
| 3987 | error_report_err(err); |
| 3988 | ret = -1; |
| 3989 | goto out; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3990 | } |
| 3991 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3992 | /* In case the driver does not call amend_status_cb() */ |
| 3993 | qemu_progress_print(0.f, 0); |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 3994 | ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL, &err); |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3995 | qemu_progress_print(100.f, 0); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3996 | if (ret < 0) { |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 3997 | error_report_err(err); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3998 | goto out; |
| 3999 | } |
| 4000 | |
| 4001 | out: |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4002 | qemu_progress_end(); |
| 4003 | |
Max Reitz | e814dff | 2015-08-20 16:00:38 -0700 | [diff] [blame] | 4004 | out_no_progress: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 4005 | blk_unref(blk); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 4006 | qemu_opts_del(opts); |
| 4007 | qemu_opts_free(create_opts); |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 4008 | g_free(options); |
| 4009 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4010 | if (ret) { |
| 4011 | return 1; |
| 4012 | } |
| 4013 | return 0; |
| 4014 | } |
| 4015 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4016 | typedef struct BenchData { |
| 4017 | BlockBackend *blk; |
| 4018 | uint64_t image_size; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4019 | bool write; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4020 | int bufsize; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4021 | int step; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4022 | int nrreq; |
| 4023 | int n; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4024 | int flush_interval; |
| 4025 | bool drain_on_flush; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4026 | uint8_t *buf; |
| 4027 | QEMUIOVector *qiov; |
| 4028 | |
| 4029 | int in_flight; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4030 | bool in_flush; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4031 | uint64_t offset; |
| 4032 | } BenchData; |
| 4033 | |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4034 | static void bench_undrained_flush_cb(void *opaque, int ret) |
| 4035 | { |
| 4036 | if (ret < 0) { |
Markus Armbruster | df3c286 | 2016-08-03 13:37:51 +0200 | [diff] [blame] | 4037 | error_report("Failed flush request: %s", strerror(-ret)); |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4038 | exit(EXIT_FAILURE); |
| 4039 | } |
| 4040 | } |
| 4041 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4042 | static void bench_cb(void *opaque, int ret) |
| 4043 | { |
| 4044 | BenchData *b = opaque; |
| 4045 | BlockAIOCB *acb; |
| 4046 | |
| 4047 | if (ret < 0) { |
Markus Armbruster | df3c286 | 2016-08-03 13:37:51 +0200 | [diff] [blame] | 4048 | error_report("Failed request: %s", strerror(-ret)); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4049 | exit(EXIT_FAILURE); |
| 4050 | } |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4051 | |
| 4052 | if (b->in_flush) { |
| 4053 | /* Just finished a flush with drained queue: Start next requests */ |
| 4054 | assert(b->in_flight == 0); |
| 4055 | b->in_flush = false; |
| 4056 | } else if (b->in_flight > 0) { |
| 4057 | int remaining = b->n - b->in_flight; |
| 4058 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4059 | b->n--; |
| 4060 | b->in_flight--; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4061 | |
| 4062 | /* Time for flush? Drain queue if requested, then flush */ |
| 4063 | if (b->flush_interval && remaining % b->flush_interval == 0) { |
| 4064 | if (!b->in_flight || !b->drain_on_flush) { |
| 4065 | BlockCompletionFunc *cb; |
| 4066 | |
| 4067 | if (b->drain_on_flush) { |
| 4068 | b->in_flush = true; |
| 4069 | cb = bench_cb; |
| 4070 | } else { |
| 4071 | cb = bench_undrained_flush_cb; |
| 4072 | } |
| 4073 | |
| 4074 | acb = blk_aio_flush(b->blk, cb, b); |
| 4075 | if (!acb) { |
| 4076 | error_report("Failed to issue flush request"); |
| 4077 | exit(EXIT_FAILURE); |
| 4078 | } |
| 4079 | } |
| 4080 | if (b->drain_on_flush) { |
| 4081 | return; |
| 4082 | } |
| 4083 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4084 | } |
| 4085 | |
| 4086 | while (b->n > b->in_flight && b->in_flight < b->nrreq) { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4087 | int64_t offset = b->offset; |
| 4088 | /* blk_aio_* might look for completed I/Os and kick bench_cb |
| 4089 | * again, so make sure this operation is counted by in_flight |
| 4090 | * and b->offset is ready for the next submission. |
| 4091 | */ |
| 4092 | b->in_flight++; |
| 4093 | b->offset += b->step; |
| 4094 | b->offset %= b->image_size; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4095 | if (b->write) { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4096 | 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] | 4097 | } else { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4098 | 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] | 4099 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4100 | if (!acb) { |
| 4101 | error_report("Failed to issue request"); |
| 4102 | exit(EXIT_FAILURE); |
| 4103 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4104 | } |
| 4105 | } |
| 4106 | |
| 4107 | static int img_bench(int argc, char **argv) |
| 4108 | { |
| 4109 | int c, ret = 0; |
| 4110 | const char *fmt = NULL, *filename; |
| 4111 | bool quiet = false; |
| 4112 | bool image_opts = false; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4113 | bool is_write = false; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4114 | int count = 75000; |
| 4115 | int depth = 64; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4116 | int64_t offset = 0; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4117 | size_t bufsize = 4096; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4118 | int pattern = 0; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4119 | size_t step = 0; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4120 | int flush_interval = 0; |
| 4121 | bool drain_on_flush = true; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4122 | int64_t image_size; |
| 4123 | BlockBackend *blk = NULL; |
| 4124 | BenchData data = {}; |
| 4125 | int flags = 0; |
Kevin Wolf | 604e861 | 2016-06-14 11:29:32 +0200 | [diff] [blame] | 4126 | bool writethrough = false; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4127 | struct timeval t1, t2; |
| 4128 | int i; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4129 | bool force_share = false; |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4130 | size_t buf_size; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4131 | |
| 4132 | for (;;) { |
| 4133 | static const struct option long_options[] = { |
| 4134 | {"help", no_argument, 0, 'h'}, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4135 | {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL}, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4136 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4137 | {"pattern", required_argument, 0, OPTION_PATTERN}, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4138 | {"no-drain", no_argument, 0, OPTION_NO_DRAIN}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4139 | {"force-share", no_argument, 0, 'U'}, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4140 | {0, 0, 0, 0} |
| 4141 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4142 | c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4143 | if (c == -1) { |
| 4144 | break; |
| 4145 | } |
| 4146 | |
| 4147 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4148 | case ':': |
| 4149 | missing_argument(argv[optind - 1]); |
| 4150 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4151 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4152 | unrecognized_option(argv[optind - 1]); |
| 4153 | break; |
| 4154 | case 'h': |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4155 | help(); |
| 4156 | break; |
| 4157 | case 'c': |
| 4158 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4159 | unsigned long res; |
| 4160 | |
| 4161 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4162 | error_report("Invalid request count specified"); |
| 4163 | return 1; |
| 4164 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4165 | count = res; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4166 | break; |
| 4167 | } |
| 4168 | case 'd': |
| 4169 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4170 | unsigned long res; |
| 4171 | |
| 4172 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4173 | error_report("Invalid queue depth specified"); |
| 4174 | return 1; |
| 4175 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4176 | depth = res; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4177 | break; |
| 4178 | } |
| 4179 | case 'f': |
| 4180 | fmt = optarg; |
| 4181 | break; |
| 4182 | case 'n': |
| 4183 | flags |= BDRV_O_NATIVE_AIO; |
| 4184 | break; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4185 | case 'o': |
| 4186 | { |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4187 | offset = cvtnum(optarg); |
| 4188 | if (offset < 0) { |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4189 | error_report("Invalid offset specified"); |
| 4190 | return 1; |
| 4191 | } |
| 4192 | break; |
| 4193 | } |
| 4194 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4195 | case 'q': |
| 4196 | quiet = true; |
| 4197 | break; |
| 4198 | case 's': |
| 4199 | { |
| 4200 | int64_t sval; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4201 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4202 | sval = cvtnum(optarg); |
| 4203 | if (sval < 0 || sval > INT_MAX) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4204 | error_report("Invalid buffer size specified"); |
| 4205 | return 1; |
| 4206 | } |
| 4207 | |
| 4208 | bufsize = sval; |
| 4209 | break; |
| 4210 | } |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4211 | case 'S': |
| 4212 | { |
| 4213 | int64_t sval; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4214 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4215 | sval = cvtnum(optarg); |
| 4216 | if (sval < 0 || sval > INT_MAX) { |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4217 | error_report("Invalid step size specified"); |
| 4218 | return 1; |
| 4219 | } |
| 4220 | |
| 4221 | step = sval; |
| 4222 | break; |
| 4223 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4224 | case 't': |
| 4225 | ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough); |
| 4226 | if (ret < 0) { |
| 4227 | error_report("Invalid cache mode"); |
| 4228 | ret = -1; |
| 4229 | goto out; |
| 4230 | } |
| 4231 | break; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4232 | case 'w': |
| 4233 | flags |= BDRV_O_RDWR; |
| 4234 | is_write = true; |
| 4235 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4236 | case 'U': |
| 4237 | force_share = true; |
| 4238 | break; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4239 | case OPTION_PATTERN: |
| 4240 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4241 | unsigned long res; |
| 4242 | |
| 4243 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) { |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4244 | error_report("Invalid pattern byte specified"); |
| 4245 | return 1; |
| 4246 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4247 | pattern = res; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4248 | break; |
| 4249 | } |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4250 | case OPTION_FLUSH_INTERVAL: |
| 4251 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4252 | unsigned long res; |
| 4253 | |
| 4254 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4255 | error_report("Invalid flush interval specified"); |
| 4256 | return 1; |
| 4257 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4258 | flush_interval = res; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4259 | break; |
| 4260 | } |
| 4261 | case OPTION_NO_DRAIN: |
| 4262 | drain_on_flush = false; |
| 4263 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4264 | case OPTION_IMAGE_OPTS: |
| 4265 | image_opts = true; |
| 4266 | break; |
| 4267 | } |
| 4268 | } |
| 4269 | |
| 4270 | if (optind != argc - 1) { |
| 4271 | error_exit("Expecting one image file name"); |
| 4272 | } |
| 4273 | filename = argv[argc - 1]; |
| 4274 | |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4275 | if (!is_write && flush_interval) { |
| 4276 | error_report("--flush-interval is only available in write tests"); |
| 4277 | ret = -1; |
| 4278 | goto out; |
| 4279 | } |
| 4280 | if (flush_interval && flush_interval < depth) { |
| 4281 | error_report("Flush interval can't be smaller than depth"); |
| 4282 | ret = -1; |
| 4283 | goto out; |
| 4284 | } |
| 4285 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4286 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 4287 | force_share); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4288 | if (!blk) { |
| 4289 | ret = -1; |
| 4290 | goto out; |
| 4291 | } |
| 4292 | |
| 4293 | image_size = blk_getlength(blk); |
| 4294 | if (image_size < 0) { |
| 4295 | ret = image_size; |
| 4296 | goto out; |
| 4297 | } |
| 4298 | |
| 4299 | data = (BenchData) { |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4300 | .blk = blk, |
| 4301 | .image_size = image_size, |
| 4302 | .bufsize = bufsize, |
| 4303 | .step = step ?: bufsize, |
| 4304 | .nrreq = depth, |
| 4305 | .n = count, |
| 4306 | .offset = offset, |
| 4307 | .write = is_write, |
| 4308 | .flush_interval = flush_interval, |
| 4309 | .drain_on_flush = drain_on_flush, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4310 | }; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4311 | printf("Sending %d %s requests, %d bytes each, %d in parallel " |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4312 | "(starting at offset %" PRId64 ", step size %d)\n", |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4313 | data.n, data.write ? "write" : "read", data.bufsize, data.nrreq, |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4314 | data.offset, data.step); |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4315 | if (flush_interval) { |
| 4316 | printf("Sending flush every %d requests\n", flush_interval); |
| 4317 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4318 | |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4319 | buf_size = data.nrreq * data.bufsize; |
| 4320 | data.buf = blk_blockalign(blk, buf_size); |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4321 | memset(data.buf, pattern, data.nrreq * data.bufsize); |
| 4322 | |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4323 | blk_register_buf(blk, data.buf, buf_size); |
| 4324 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4325 | data.qiov = g_new(QEMUIOVector, data.nrreq); |
| 4326 | for (i = 0; i < data.nrreq; i++) { |
| 4327 | qemu_iovec_init(&data.qiov[i], 1); |
| 4328 | qemu_iovec_add(&data.qiov[i], |
| 4329 | data.buf + i * data.bufsize, data.bufsize); |
| 4330 | } |
| 4331 | |
| 4332 | gettimeofday(&t1, NULL); |
| 4333 | bench_cb(&data, 0); |
| 4334 | |
| 4335 | while (data.n > 0) { |
| 4336 | main_loop_wait(false); |
| 4337 | } |
| 4338 | gettimeofday(&t2, NULL); |
| 4339 | |
| 4340 | printf("Run completed in %3.3f seconds.\n", |
| 4341 | (t2.tv_sec - t1.tv_sec) |
| 4342 | + ((double)(t2.tv_usec - t1.tv_usec) / 1000000)); |
| 4343 | |
| 4344 | out: |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4345 | if (data.buf) { |
| 4346 | blk_unregister_buf(blk, data.buf); |
| 4347 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4348 | qemu_vfree(data.buf); |
| 4349 | blk_unref(blk); |
| 4350 | |
| 4351 | if (ret) { |
| 4352 | return 1; |
| 4353 | } |
| 4354 | return 0; |
| 4355 | } |
| 4356 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4357 | #define C_BS 01 |
| 4358 | #define C_COUNT 02 |
| 4359 | #define C_IF 04 |
| 4360 | #define C_OF 010 |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4361 | #define C_SKIP 020 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4362 | |
| 4363 | struct DdInfo { |
| 4364 | unsigned int flags; |
| 4365 | int64_t count; |
| 4366 | }; |
| 4367 | |
| 4368 | struct DdIo { |
| 4369 | int bsz; /* Block size */ |
| 4370 | char *filename; |
| 4371 | uint8_t *buf; |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4372 | int64_t offset; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4373 | }; |
| 4374 | |
| 4375 | struct DdOpts { |
| 4376 | const char *name; |
| 4377 | int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *); |
| 4378 | unsigned int flag; |
| 4379 | }; |
| 4380 | |
| 4381 | static int img_dd_bs(const char *arg, |
| 4382 | struct DdIo *in, struct DdIo *out, |
| 4383 | struct DdInfo *dd) |
| 4384 | { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4385 | int64_t res; |
| 4386 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4387 | res = cvtnum(arg); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4388 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4389 | if (res <= 0 || res > INT_MAX) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4390 | error_report("invalid number: '%s'", arg); |
| 4391 | return 1; |
| 4392 | } |
| 4393 | in->bsz = out->bsz = res; |
| 4394 | |
| 4395 | return 0; |
| 4396 | } |
| 4397 | |
| 4398 | static int img_dd_count(const char *arg, |
| 4399 | struct DdIo *in, struct DdIo *out, |
| 4400 | struct DdInfo *dd) |
| 4401 | { |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4402 | dd->count = cvtnum(arg); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4403 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4404 | if (dd->count < 0) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4405 | error_report("invalid number: '%s'", arg); |
| 4406 | return 1; |
| 4407 | } |
| 4408 | |
| 4409 | return 0; |
| 4410 | } |
| 4411 | |
| 4412 | static int img_dd_if(const char *arg, |
| 4413 | struct DdIo *in, struct DdIo *out, |
| 4414 | struct DdInfo *dd) |
| 4415 | { |
| 4416 | in->filename = g_strdup(arg); |
| 4417 | |
| 4418 | return 0; |
| 4419 | } |
| 4420 | |
| 4421 | static int img_dd_of(const char *arg, |
| 4422 | struct DdIo *in, struct DdIo *out, |
| 4423 | struct DdInfo *dd) |
| 4424 | { |
| 4425 | out->filename = g_strdup(arg); |
| 4426 | |
| 4427 | return 0; |
| 4428 | } |
| 4429 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4430 | static int img_dd_skip(const char *arg, |
| 4431 | struct DdIo *in, struct DdIo *out, |
| 4432 | struct DdInfo *dd) |
| 4433 | { |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4434 | in->offset = cvtnum(arg); |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4435 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4436 | if (in->offset < 0) { |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4437 | error_report("invalid number: '%s'", arg); |
| 4438 | return 1; |
| 4439 | } |
| 4440 | |
| 4441 | return 0; |
| 4442 | } |
| 4443 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4444 | static int img_dd(int argc, char **argv) |
| 4445 | { |
| 4446 | int ret = 0; |
| 4447 | char *arg = NULL; |
| 4448 | char *tmp; |
| 4449 | BlockDriver *drv = NULL, *proto_drv = NULL; |
| 4450 | BlockBackend *blk1 = NULL, *blk2 = NULL; |
| 4451 | QemuOpts *opts = NULL; |
| 4452 | QemuOptsList *create_opts = NULL; |
| 4453 | Error *local_err = NULL; |
| 4454 | bool image_opts = false; |
| 4455 | int c, i; |
| 4456 | const char *out_fmt = "raw"; |
| 4457 | const char *fmt = NULL; |
| 4458 | int64_t size = 0; |
| 4459 | int64_t block_count = 0, out_pos, in_pos; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4460 | bool force_share = false; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4461 | struct DdInfo dd = { |
| 4462 | .flags = 0, |
| 4463 | .count = 0, |
| 4464 | }; |
| 4465 | struct DdIo in = { |
| 4466 | .bsz = 512, /* Block size is by default 512 bytes */ |
| 4467 | .filename = NULL, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4468 | .buf = NULL, |
| 4469 | .offset = 0 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4470 | }; |
| 4471 | struct DdIo out = { |
| 4472 | .bsz = 512, |
| 4473 | .filename = NULL, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4474 | .buf = NULL, |
| 4475 | .offset = 0 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4476 | }; |
| 4477 | |
| 4478 | const struct DdOpts options[] = { |
| 4479 | { "bs", img_dd_bs, C_BS }, |
| 4480 | { "count", img_dd_count, C_COUNT }, |
| 4481 | { "if", img_dd_if, C_IF }, |
| 4482 | { "of", img_dd_of, C_OF }, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4483 | { "skip", img_dd_skip, C_SKIP }, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4484 | { NULL, NULL, 0 } |
| 4485 | }; |
| 4486 | const struct option long_options[] = { |
| 4487 | { "help", no_argument, 0, 'h'}, |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 4488 | { "object", required_argument, 0, OPTION_OBJECT}, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4489 | { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4490 | { "force-share", no_argument, 0, 'U'}, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4491 | { 0, 0, 0, 0 } |
| 4492 | }; |
| 4493 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4494 | while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4495 | if (c == EOF) { |
| 4496 | break; |
| 4497 | } |
| 4498 | switch (c) { |
| 4499 | case 'O': |
| 4500 | out_fmt = optarg; |
| 4501 | break; |
| 4502 | case 'f': |
| 4503 | fmt = optarg; |
| 4504 | break; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4505 | case ':': |
| 4506 | missing_argument(argv[optind - 1]); |
| 4507 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4508 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4509 | unrecognized_option(argv[optind - 1]); |
| 4510 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4511 | case 'h': |
| 4512 | help(); |
| 4513 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4514 | case 'U': |
| 4515 | force_share = true; |
| 4516 | break; |
Stefan Hajnoczi | 2a24570 | 2017-06-19 16:00:02 +0100 | [diff] [blame] | 4517 | case OPTION_OBJECT: |
| 4518 | if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) { |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 4519 | ret = -1; |
| 4520 | goto out; |
| 4521 | } |
Stefan Hajnoczi | 2a24570 | 2017-06-19 16:00:02 +0100 | [diff] [blame] | 4522 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4523 | case OPTION_IMAGE_OPTS: |
| 4524 | image_opts = true; |
| 4525 | break; |
| 4526 | } |
| 4527 | } |
| 4528 | |
| 4529 | for (i = optind; i < argc; i++) { |
| 4530 | int j; |
| 4531 | arg = g_strdup(argv[i]); |
| 4532 | |
| 4533 | tmp = strchr(arg, '='); |
| 4534 | if (tmp == NULL) { |
| 4535 | error_report("unrecognized operand %s", arg); |
| 4536 | ret = -1; |
| 4537 | goto out; |
| 4538 | } |
| 4539 | |
| 4540 | *tmp++ = '\0'; |
| 4541 | |
| 4542 | for (j = 0; options[j].name != NULL; j++) { |
| 4543 | if (!strcmp(arg, options[j].name)) { |
| 4544 | break; |
| 4545 | } |
| 4546 | } |
| 4547 | if (options[j].name == NULL) { |
| 4548 | error_report("unrecognized operand %s", arg); |
| 4549 | ret = -1; |
| 4550 | goto out; |
| 4551 | } |
| 4552 | |
| 4553 | if (options[j].f(tmp, &in, &out, &dd) != 0) { |
| 4554 | ret = -1; |
| 4555 | goto out; |
| 4556 | } |
| 4557 | dd.flags |= options[j].flag; |
| 4558 | g_free(arg); |
| 4559 | arg = NULL; |
| 4560 | } |
| 4561 | |
| 4562 | if (!(dd.flags & C_IF && dd.flags & C_OF)) { |
| 4563 | error_report("Must specify both input and output files"); |
| 4564 | ret = -1; |
| 4565 | goto out; |
| 4566 | } |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 4567 | |
| 4568 | if (qemu_opts_foreach(&qemu_object_opts, |
| 4569 | user_creatable_add_opts_foreach, |
Daniel P. Berrangé | 334c43e | 2019-02-19 11:46:09 +0000 | [diff] [blame] | 4570 | NULL, &error_fatal)) { |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 4571 | ret = -1; |
| 4572 | goto out; |
| 4573 | } |
| 4574 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4575 | blk1 = img_open(image_opts, in.filename, fmt, 0, false, false, |
| 4576 | force_share); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4577 | |
| 4578 | if (!blk1) { |
| 4579 | ret = -1; |
| 4580 | goto out; |
| 4581 | } |
| 4582 | |
| 4583 | drv = bdrv_find_format(out_fmt); |
| 4584 | if (!drv) { |
| 4585 | error_report("Unknown file format"); |
| 4586 | ret = -1; |
| 4587 | goto out; |
| 4588 | } |
| 4589 | proto_drv = bdrv_find_protocol(out.filename, true, &local_err); |
| 4590 | |
| 4591 | if (!proto_drv) { |
| 4592 | error_report_err(local_err); |
| 4593 | ret = -1; |
| 4594 | goto out; |
| 4595 | } |
| 4596 | if (!drv->create_opts) { |
| 4597 | error_report("Format driver '%s' does not support image creation", |
| 4598 | drv->format_name); |
| 4599 | ret = -1; |
| 4600 | goto out; |
| 4601 | } |
| 4602 | if (!proto_drv->create_opts) { |
| 4603 | error_report("Protocol driver '%s' does not support image creation", |
| 4604 | proto_drv->format_name); |
| 4605 | ret = -1; |
| 4606 | goto out; |
| 4607 | } |
| 4608 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 4609 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
| 4610 | |
| 4611 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
| 4612 | |
| 4613 | size = blk_getlength(blk1); |
| 4614 | if (size < 0) { |
| 4615 | error_report("Failed to get size for '%s'", in.filename); |
| 4616 | ret = -1; |
| 4617 | goto out; |
| 4618 | } |
| 4619 | |
| 4620 | if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz && |
| 4621 | dd.count * in.bsz < size) { |
| 4622 | size = dd.count * in.bsz; |
| 4623 | } |
| 4624 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4625 | /* Overflow means the specified offset is beyond input image's size */ |
| 4626 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || |
| 4627 | size < in.bsz * in.offset)) { |
| 4628 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort); |
| 4629 | } else { |
| 4630 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, |
| 4631 | size - in.bsz * in.offset, &error_abort); |
| 4632 | } |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4633 | |
| 4634 | ret = bdrv_create(drv, out.filename, opts, &local_err); |
| 4635 | if (ret < 0) { |
| 4636 | error_reportf_err(local_err, |
| 4637 | "%s: error while creating output image: ", |
| 4638 | out.filename); |
| 4639 | ret = -1; |
| 4640 | goto out; |
| 4641 | } |
| 4642 | |
Daniel P. Berrange | ea204dd | 2017-05-15 17:47:10 +0100 | [diff] [blame] | 4643 | /* TODO, we can't honour --image-opts for the target, |
| 4644 | * since it needs to be given in a format compatible |
| 4645 | * with the bdrv_create() call above which does not |
| 4646 | * support image-opts style. |
| 4647 | */ |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 4648 | 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] | 4649 | false, false, false); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4650 | |
| 4651 | if (!blk2) { |
| 4652 | ret = -1; |
| 4653 | goto out; |
| 4654 | } |
| 4655 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4656 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || |
| 4657 | size < in.offset * in.bsz)) { |
| 4658 | /* We give a warning if the skip option is bigger than the input |
| 4659 | * size and create an empty output disk image (i.e. like dd(1)). |
| 4660 | */ |
| 4661 | error_report("%s: cannot skip to specified offset", in.filename); |
| 4662 | in_pos = size; |
| 4663 | } else { |
| 4664 | in_pos = in.offset * in.bsz; |
| 4665 | } |
| 4666 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4667 | in.buf = g_new(uint8_t, in.bsz); |
| 4668 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4669 | for (out_pos = 0; in_pos < size; block_count++) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4670 | int in_ret, out_ret; |
| 4671 | |
| 4672 | if (in_pos + in.bsz > size) { |
| 4673 | in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos); |
| 4674 | } else { |
| 4675 | in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz); |
| 4676 | } |
| 4677 | if (in_ret < 0) { |
| 4678 | error_report("error while reading from input image file: %s", |
| 4679 | strerror(-in_ret)); |
| 4680 | ret = -1; |
| 4681 | goto out; |
| 4682 | } |
| 4683 | in_pos += in_ret; |
| 4684 | |
| 4685 | out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0); |
| 4686 | |
| 4687 | if (out_ret < 0) { |
| 4688 | error_report("error while writing to output image file: %s", |
| 4689 | strerror(-out_ret)); |
| 4690 | ret = -1; |
| 4691 | goto out; |
| 4692 | } |
| 4693 | out_pos += out_ret; |
| 4694 | } |
| 4695 | |
| 4696 | out: |
| 4697 | g_free(arg); |
| 4698 | qemu_opts_del(opts); |
| 4699 | qemu_opts_free(create_opts); |
| 4700 | blk_unref(blk1); |
| 4701 | blk_unref(blk2); |
| 4702 | g_free(in.filename); |
| 4703 | g_free(out.filename); |
| 4704 | g_free(in.buf); |
| 4705 | g_free(out.buf); |
| 4706 | |
| 4707 | if (ret) { |
| 4708 | return 1; |
| 4709 | } |
| 4710 | return 0; |
| 4711 | } |
| 4712 | |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 4713 | static void dump_json_block_measure_info(BlockMeasureInfo *info) |
| 4714 | { |
| 4715 | QString *str; |
| 4716 | QObject *obj; |
| 4717 | Visitor *v = qobject_output_visitor_new(&obj); |
| 4718 | |
| 4719 | visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort); |
| 4720 | visit_complete(v, &obj); |
| 4721 | str = qobject_to_json_pretty(obj); |
| 4722 | assert(str != NULL); |
| 4723 | printf("%s\n", qstring_get_str(str)); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 4724 | qobject_unref(obj); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 4725 | visit_free(v); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 4726 | qobject_unref(str); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 4727 | } |
| 4728 | |
| 4729 | static int img_measure(int argc, char **argv) |
| 4730 | { |
| 4731 | static const struct option long_options[] = { |
| 4732 | {"help", no_argument, 0, 'h'}, |
| 4733 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
| 4734 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 4735 | {"output", required_argument, 0, OPTION_OUTPUT}, |
| 4736 | {"size", required_argument, 0, OPTION_SIZE}, |
| 4737 | {"force-share", no_argument, 0, 'U'}, |
| 4738 | {0, 0, 0, 0} |
| 4739 | }; |
| 4740 | OutputFormat output_format = OFORMAT_HUMAN; |
| 4741 | BlockBackend *in_blk = NULL; |
| 4742 | BlockDriver *drv; |
| 4743 | const char *filename = NULL; |
| 4744 | const char *fmt = NULL; |
| 4745 | const char *out_fmt = "raw"; |
| 4746 | char *options = NULL; |
| 4747 | char *snapshot_name = NULL; |
| 4748 | bool force_share = false; |
| 4749 | QemuOpts *opts = NULL; |
| 4750 | QemuOpts *object_opts = NULL; |
| 4751 | QemuOpts *sn_opts = NULL; |
| 4752 | QemuOptsList *create_opts = NULL; |
| 4753 | bool image_opts = false; |
| 4754 | uint64_t img_size = UINT64_MAX; |
| 4755 | BlockMeasureInfo *info = NULL; |
| 4756 | Error *local_err = NULL; |
| 4757 | int ret = 1; |
| 4758 | int c; |
| 4759 | |
| 4760 | while ((c = getopt_long(argc, argv, "hf:O:o:l:U", |
| 4761 | long_options, NULL)) != -1) { |
| 4762 | switch (c) { |
| 4763 | case '?': |
| 4764 | case 'h': |
| 4765 | help(); |
| 4766 | break; |
| 4767 | case 'f': |
| 4768 | fmt = optarg; |
| 4769 | break; |
| 4770 | case 'O': |
| 4771 | out_fmt = optarg; |
| 4772 | break; |
| 4773 | case 'o': |
| 4774 | if (!is_valid_option_list(optarg)) { |
| 4775 | error_report("Invalid option list: %s", optarg); |
| 4776 | goto out; |
| 4777 | } |
| 4778 | if (!options) { |
| 4779 | options = g_strdup(optarg); |
| 4780 | } else { |
| 4781 | char *old_options = options; |
| 4782 | options = g_strdup_printf("%s,%s", options, optarg); |
| 4783 | g_free(old_options); |
| 4784 | } |
| 4785 | break; |
| 4786 | case 'l': |
| 4787 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { |
| 4788 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
| 4789 | optarg, false); |
| 4790 | if (!sn_opts) { |
| 4791 | error_report("Failed in parsing snapshot param '%s'", |
| 4792 | optarg); |
| 4793 | goto out; |
| 4794 | } |
| 4795 | } else { |
| 4796 | snapshot_name = optarg; |
| 4797 | } |
| 4798 | break; |
| 4799 | case 'U': |
| 4800 | force_share = true; |
| 4801 | break; |
| 4802 | case OPTION_OBJECT: |
| 4803 | object_opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 4804 | optarg, true); |
| 4805 | if (!object_opts) { |
| 4806 | goto out; |
| 4807 | } |
| 4808 | break; |
| 4809 | case OPTION_IMAGE_OPTS: |
| 4810 | image_opts = true; |
| 4811 | break; |
| 4812 | case OPTION_OUTPUT: |
| 4813 | if (!strcmp(optarg, "json")) { |
| 4814 | output_format = OFORMAT_JSON; |
| 4815 | } else if (!strcmp(optarg, "human")) { |
| 4816 | output_format = OFORMAT_HUMAN; |
| 4817 | } else { |
| 4818 | error_report("--output must be used with human or json " |
| 4819 | "as argument."); |
| 4820 | goto out; |
| 4821 | } |
| 4822 | break; |
| 4823 | case OPTION_SIZE: |
| 4824 | { |
| 4825 | int64_t sval; |
| 4826 | |
| 4827 | sval = cvtnum(optarg); |
| 4828 | if (sval < 0) { |
| 4829 | if (sval == -ERANGE) { |
| 4830 | error_report("Image size must be less than 8 EiB!"); |
| 4831 | } else { |
| 4832 | error_report("Invalid image size specified! You may use " |
| 4833 | "k, M, G, T, P or E suffixes for "); |
| 4834 | error_report("kilobytes, megabytes, gigabytes, terabytes, " |
| 4835 | "petabytes and exabytes."); |
| 4836 | } |
| 4837 | goto out; |
| 4838 | } |
| 4839 | img_size = (uint64_t)sval; |
| 4840 | } |
| 4841 | break; |
| 4842 | } |
| 4843 | } |
| 4844 | |
| 4845 | if (qemu_opts_foreach(&qemu_object_opts, |
| 4846 | user_creatable_add_opts_foreach, |
Daniel P. Berrangé | 334c43e | 2019-02-19 11:46:09 +0000 | [diff] [blame] | 4847 | NULL, &error_fatal)) { |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 4848 | goto out; |
| 4849 | } |
| 4850 | |
| 4851 | if (argc - optind > 1) { |
| 4852 | error_report("At most one filename argument is allowed."); |
| 4853 | goto out; |
| 4854 | } else if (argc - optind == 1) { |
| 4855 | filename = argv[optind]; |
| 4856 | } |
| 4857 | |
| 4858 | if (!filename && |
| 4859 | (object_opts || image_opts || fmt || snapshot_name || sn_opts)) { |
| 4860 | error_report("--object, --image-opts, -f, and -l " |
| 4861 | "require a filename argument."); |
| 4862 | goto out; |
| 4863 | } |
| 4864 | if (filename && img_size != UINT64_MAX) { |
| 4865 | error_report("--size N cannot be used together with a filename."); |
| 4866 | goto out; |
| 4867 | } |
| 4868 | if (!filename && img_size == UINT64_MAX) { |
| 4869 | error_report("Either --size N or one filename must be specified."); |
| 4870 | goto out; |
| 4871 | } |
| 4872 | |
| 4873 | if (filename) { |
| 4874 | in_blk = img_open(image_opts, filename, fmt, 0, |
| 4875 | false, false, force_share); |
| 4876 | if (!in_blk) { |
| 4877 | goto out; |
| 4878 | } |
| 4879 | |
| 4880 | if (sn_opts) { |
| 4881 | bdrv_snapshot_load_tmp(blk_bs(in_blk), |
| 4882 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), |
| 4883 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), |
| 4884 | &local_err); |
| 4885 | } else if (snapshot_name != NULL) { |
| 4886 | bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk), |
| 4887 | snapshot_name, &local_err); |
| 4888 | } |
| 4889 | if (local_err) { |
| 4890 | error_reportf_err(local_err, "Failed to load snapshot: "); |
| 4891 | goto out; |
| 4892 | } |
| 4893 | } |
| 4894 | |
| 4895 | drv = bdrv_find_format(out_fmt); |
| 4896 | if (!drv) { |
| 4897 | error_report("Unknown file format '%s'", out_fmt); |
| 4898 | goto out; |
| 4899 | } |
| 4900 | if (!drv->create_opts) { |
| 4901 | error_report("Format driver '%s' does not support image creation", |
| 4902 | drv->format_name); |
| 4903 | goto out; |
| 4904 | } |
| 4905 | |
| 4906 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 4907 | create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts); |
| 4908 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
| 4909 | if (options) { |
| 4910 | qemu_opts_do_parse(opts, options, NULL, &local_err); |
| 4911 | if (local_err) { |
| 4912 | error_report_err(local_err); |
| 4913 | error_report("Invalid options for file format '%s'", out_fmt); |
| 4914 | goto out; |
| 4915 | } |
| 4916 | } |
| 4917 | if (img_size != UINT64_MAX) { |
| 4918 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort); |
| 4919 | } |
| 4920 | |
| 4921 | info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err); |
| 4922 | if (local_err) { |
| 4923 | error_report_err(local_err); |
| 4924 | goto out; |
| 4925 | } |
| 4926 | |
| 4927 | if (output_format == OFORMAT_HUMAN) { |
| 4928 | printf("required size: %" PRIu64 "\n", info->required); |
| 4929 | printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated); |
| 4930 | } else { |
| 4931 | dump_json_block_measure_info(info); |
| 4932 | } |
| 4933 | |
| 4934 | ret = 0; |
| 4935 | |
| 4936 | out: |
| 4937 | qapi_free_BlockMeasureInfo(info); |
| 4938 | qemu_opts_del(object_opts); |
| 4939 | qemu_opts_del(opts); |
| 4940 | qemu_opts_del(sn_opts); |
| 4941 | qemu_opts_free(create_opts); |
| 4942 | g_free(options); |
| 4943 | blk_unref(in_blk); |
| 4944 | return ret; |
| 4945 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4946 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 4947 | static const img_cmd_t img_cmds[] = { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 4948 | #define DEF(option, callback, arg_string) \ |
| 4949 | { option, callback }, |
| 4950 | #include "qemu-img-cmds.h" |
| 4951 | #undef DEF |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 4952 | { NULL, NULL, }, |
| 4953 | }; |
| 4954 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 4955 | int main(int argc, char **argv) |
| 4956 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 4957 | const img_cmd_t *cmd; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 4958 | const char *cmdname; |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 4959 | Error *local_error = NULL; |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 4960 | char *trace_file = NULL; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 4961 | int c; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 4962 | static const struct option long_options[] = { |
| 4963 | {"help", no_argument, 0, 'h'}, |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 4964 | {"version", no_argument, 0, 'V'}, |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 4965 | {"trace", required_argument, NULL, 'T'}, |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 4966 | {0, 0, 0, 0} |
| 4967 | }; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 4968 | |
MORITA Kazutaka | 526eda1 | 2013-07-23 17:30:11 +0900 | [diff] [blame] | 4969 | #ifdef CONFIG_POSIX |
| 4970 | signal(SIGPIPE, SIG_IGN); |
| 4971 | #endif |
| 4972 | |
Christophe Fergeau | f5852ef | 2019-01-31 17:46:14 +0100 | [diff] [blame] | 4973 | error_init(argv[0]); |
Daniel P. Berrange | fe4db84 | 2016-10-04 14:35:52 +0100 | [diff] [blame] | 4974 | module_call_init(MODULE_INIT_TRACE); |
Fam Zheng | 10f5bff | 2014-02-10 14:48:51 +0800 | [diff] [blame] | 4975 | qemu_init_exec_dir(argv[0]); |
Kevin Wolf | 53f76e5 | 2010-12-16 15:10:32 +0100 | [diff] [blame] | 4976 | |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 4977 | if (qemu_init_main_loop(&local_error)) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 4978 | error_report_err(local_error); |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 4979 | exit(EXIT_FAILURE); |
| 4980 | } |
| 4981 | |
Eduardo Habkost | e8f2d27 | 2016-05-12 11:10:04 -0300 | [diff] [blame] | 4982 | qcrypto_init(&error_fatal); |
Daniel P. Berrange | c229708 | 2016-04-06 12:12:06 +0100 | [diff] [blame] | 4983 | |
Daniel P. Berrange | 064097d | 2016-02-10 18:41:01 +0000 | [diff] [blame] | 4984 | module_call_init(MODULE_INIT_QOM); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 4985 | bdrv_init(); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 4986 | if (argc < 2) { |
| 4987 | error_exit("Not enough arguments"); |
| 4988 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 4989 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4990 | qemu_add_opts(&qemu_object_opts); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 4991 | qemu_add_opts(&qemu_source_opts); |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 4992 | qemu_add_opts(&qemu_trace_opts); |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4993 | |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4994 | while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) { |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 4995 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4996 | case ':': |
| 4997 | missing_argument(argv[optind - 1]); |
| 4998 | return 0; |
Stefan Hajnoczi | 4581c16 | 2017-03-17 18:45:39 +0800 | [diff] [blame] | 4999 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 5000 | unrecognized_option(argv[optind - 1]); |
| 5001 | return 0; |
| 5002 | case 'h': |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5003 | help(); |
| 5004 | return 0; |
| 5005 | case 'V': |
| 5006 | printf(QEMU_IMG_VERSION); |
| 5007 | return 0; |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5008 | case 'T': |
| 5009 | g_free(trace_file); |
| 5010 | trace_file = trace_opt_parse(optarg); |
| 5011 | break; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5012 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5013 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5014 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5015 | cmdname = argv[optind]; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5016 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5017 | /* reset getopt_long scanning */ |
| 5018 | argc -= optind; |
| 5019 | if (argc < 1) { |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 5020 | return 0; |
| 5021 | } |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5022 | argv += optind; |
Richard W.M. Jones | d339d76 | 2019-01-18 10:11:14 +0000 | [diff] [blame] | 5023 | qemu_reset_optind(); |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5024 | |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5025 | if (!trace_init_backends()) { |
| 5026 | exit(1); |
| 5027 | } |
| 5028 | trace_init_file(trace_file); |
| 5029 | qemu_set_log(LOG_TRACE); |
| 5030 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5031 | /* find the command */ |
| 5032 | for (cmd = img_cmds; cmd->name != NULL; cmd++) { |
| 5033 | if (!strcmp(cmdname, cmd->name)) { |
| 5034 | return cmd->handler(argc, argv); |
| 5035 | } |
| 5036 | } |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5037 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5038 | /* not found */ |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 5039 | error_exit("Command not found: %s", cmdname); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5040 | } |