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