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