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