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 | */ |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 24 | #include "qemu/osdep.h" |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 25 | #include "qapi-visit.h" |
| 26 | #include "qapi/qmp-output-visitor.h" |
Markus Armbruster | cc7a8ea | 2015-03-17 17:22:46 +0100 | [diff] [blame] | 27 | #include "qapi/qmp/qerror.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 28 | #include "qapi/qmp/qjson.h" |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 29 | #include "qemu-common.h" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 30 | #include "qemu/config-file.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 31 | #include "qemu/option.h" |
| 32 | #include "qemu/error-report.h" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 33 | #include "qom/object_interfaces.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 34 | #include "sysemu/sysemu.h" |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 35 | #include "sysemu/block-backend.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 36 | #include "block/block_int.h" |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 37 | #include "block/blockjob.h" |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 38 | #include "block/qapi.h" |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 39 | #include <getopt.h> |
bellard | e844533 | 2006-06-14 15:32:10 +0000 | [diff] [blame] | 40 | |
Don Slutz | 61979a6 | 2015-01-09 10:17:35 -0500 | [diff] [blame] | 41 | #define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \ |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 42 | ", Copyright (c) 2004-2008 Fabrice Bellard\n" |
| 43 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 44 | typedef struct img_cmd_t { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 45 | const char *name; |
| 46 | int (*handler)(int argc, char **argv); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 47 | } img_cmd_t; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 48 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 49 | enum { |
| 50 | OPTION_OUTPUT = 256, |
| 51 | OPTION_BACKING_CHAIN = 257, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 52 | OPTION_OBJECT = 258, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 53 | OPTION_IMAGE_OPTS = 259, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | typedef enum OutputFormat { |
| 57 | OFORMAT_JSON, |
| 58 | OFORMAT_HUMAN, |
| 59 | } OutputFormat; |
| 60 | |
aurel32 | 137519c | 2008-11-30 19:12:49 +0000 | [diff] [blame] | 61 | /* Default to cache=writeback as data integrity is not important for qemu-tcg. */ |
Stefan Hajnoczi | adfe078 | 2010-04-13 10:29:35 +0100 | [diff] [blame] | 62 | #define BDRV_O_FLAGS BDRV_O_CACHE_WB |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 63 | #define BDRV_DEFAULT_CACHE "writeback" |
aurel32 | 137519c | 2008-11-30 19:12:49 +0000 | [diff] [blame] | 64 | |
Stefan Hajnoczi | 00c6d40 | 2014-08-27 12:08:56 +0100 | [diff] [blame] | 65 | static void format_print(void *opaque, const char *name) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 66 | { |
Stefan Hajnoczi | 00c6d40 | 2014-08-27 12:08:56 +0100 | [diff] [blame] | 67 | printf(" %s", name); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 70 | static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...) |
| 71 | { |
| 72 | va_list ap; |
| 73 | |
| 74 | error_printf("qemu-img: "); |
| 75 | |
| 76 | va_start(ap, fmt); |
| 77 | error_vprintf(fmt, ap); |
| 78 | va_end(ap); |
| 79 | |
| 80 | error_printf("\nTry 'qemu-img --help' for more information\n"); |
| 81 | exit(EXIT_FAILURE); |
| 82 | } |
| 83 | |
blueswir1 | d2c639d | 2009-01-24 18:19:25 +0000 | [diff] [blame] | 84 | /* Please keep in synch with qemu-img.texi */ |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 85 | static void QEMU_NORETURN help(void) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 86 | { |
Paolo Bonzini | e00291c | 2010-02-04 16:49:56 +0100 | [diff] [blame] | 87 | const char *help_msg = |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 88 | QEMU_IMG_VERSION |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 89 | "usage: qemu-img command [command options]\n" |
| 90 | "QEMU disk image utility\n" |
| 91 | "\n" |
| 92 | "Command syntax:\n" |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 93 | #define DEF(option, callback, arg_string) \ |
| 94 | " " arg_string "\n" |
| 95 | #include "qemu-img-cmds.h" |
| 96 | #undef DEF |
| 97 | #undef GEN_DOCS |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 98 | "\n" |
| 99 | "Command parameters:\n" |
| 100 | " 'filename' is a disk image filename\n" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 101 | " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n" |
| 102 | " manual page for a description of the object properties. The most common\n" |
| 103 | " object type is a 'secret', which is used to supply passwords and/or\n" |
| 104 | " encryption keys.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 105 | " '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] | 106 | " '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] | 107 | " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n" |
| 108 | " 'directsync' and 'unsafe' (default for convert)\n" |
Stefan Hajnoczi | bb87fdf | 2014-09-02 11:01:02 +0100 | [diff] [blame] | 109 | " 'src_cache' is the cache mode used to read input disk images, the valid\n" |
| 110 | " options are the same as for the 'cache' option\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 111 | " 'size' is the disk image size in bytes. Optional suffixes\n" |
Kevin Wolf | 5e00984 | 2013-06-05 14:19:27 +0200 | [diff] [blame] | 112 | " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n" |
| 113 | " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n" |
| 114 | " supported. 'b' is ignored.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 115 | " 'output_filename' is the destination disk image filename\n" |
| 116 | " 'output_fmt' is the destination format\n" |
| 117 | " 'options' is a comma separated list of format specific options in a\n" |
| 118 | " name=value format. Use -o ? for an overview of the options supported by the\n" |
| 119 | " used format\n" |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 120 | " 'snapshot_param' is param used for internal snapshot, format\n" |
| 121 | " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n" |
| 122 | " '[ID_OR_NAME]'\n" |
| 123 | " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n" |
| 124 | " instead\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 125 | " '-c' indicates that target image must be compressed (qcow format only)\n" |
| 126 | " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n" |
| 127 | " match exactly. The image doesn't need a working backing file before\n" |
| 128 | " rebasing in this case (useful for renaming the backing file)\n" |
| 129 | " '-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] | 130 | " '-p' show progress of command (only certain commands)\n" |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 131 | " '-q' use Quiet mode - do not print any output (except errors)\n" |
Peter Lieven | 11b6699 | 2013-10-24 12:07:05 +0200 | [diff] [blame] | 132 | " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n" |
| 133 | " contain only zeros for qemu-img to create a sparse image during\n" |
| 134 | " conversion. If the number of bytes is 0, the source will not be scanned for\n" |
| 135 | " unallocated or zero sectors, and the destination image will always be\n" |
| 136 | " fully allocated\n" |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 137 | " '--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] | 138 | " '-n' skips the target volume creation (useful if the volume is created\n" |
| 139 | " prior to running qemu-img)\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 140 | "\n" |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 141 | "Parameters to check subcommand:\n" |
| 142 | " '-r' tries to repair any inconsistencies that are found during the check.\n" |
| 143 | " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n" |
| 144 | " 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] | 145 | " hiding corruption that has already occurred.\n" |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 146 | "\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 147 | "Parameters to snapshot subcommand:\n" |
| 148 | " 'snapshot' is the name of the snapshot to create, apply or delete\n" |
| 149 | " '-a' applies a snapshot (revert disk to saved state)\n" |
| 150 | " '-c' creates a snapshot\n" |
| 151 | " '-d' deletes a snapshot\n" |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 152 | " '-l' lists all snapshots in the given image\n" |
| 153 | "\n" |
| 154 | "Parameters to compare subcommand:\n" |
| 155 | " '-f' first image format\n" |
| 156 | " '-F' second image format\n" |
| 157 | " '-s' run in Strict mode - fail on different image size or sector allocation\n"; |
Paolo Bonzini | e00291c | 2010-02-04 16:49:56 +0100 | [diff] [blame] | 158 | |
| 159 | printf("%s\nSupported formats:", help_msg); |
Stefan Hajnoczi | 00c6d40 | 2014-08-27 12:08:56 +0100 | [diff] [blame] | 160 | bdrv_iterate_format(format_print, NULL); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 161 | printf("\n"); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 162 | exit(EXIT_SUCCESS); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 165 | static QemuOptsList qemu_object_opts = { |
| 166 | .name = "object", |
| 167 | .implied_opt_name = "qom-type", |
| 168 | .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head), |
| 169 | .desc = { |
| 170 | { } |
| 171 | }, |
| 172 | }; |
| 173 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 174 | static QemuOptsList qemu_source_opts = { |
| 175 | .name = "source", |
| 176 | .implied_opt_name = "file", |
| 177 | .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head), |
| 178 | .desc = { |
| 179 | { } |
| 180 | }, |
| 181 | }; |
| 182 | |
Stefan Weil | 7c30f65 | 2013-06-16 17:01:05 +0200 | [diff] [blame] | 183 | 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] | 184 | { |
| 185 | int ret = 0; |
| 186 | if (!quiet) { |
| 187 | va_list args; |
| 188 | va_start(args, fmt); |
| 189 | ret = vprintf(fmt, args); |
| 190 | va_end(args); |
| 191 | } |
| 192 | return ret; |
| 193 | } |
| 194 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 195 | |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 196 | static int print_block_option_help(const char *filename, const char *fmt) |
| 197 | { |
| 198 | BlockDriver *drv, *proto_drv; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 199 | QemuOptsList *create_opts = NULL; |
Max Reitz | b65a5e1 | 2015-02-05 13:58:12 -0500 | [diff] [blame] | 200 | Error *local_err = NULL; |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 201 | |
| 202 | /* Find driver and parse its options */ |
| 203 | drv = bdrv_find_format(fmt); |
| 204 | if (!drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 205 | error_report("Unknown file format '%s'", fmt); |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 206 | return 1; |
| 207 | } |
| 208 | |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 209 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 210 | if (filename) { |
Max Reitz | b65a5e1 | 2015-02-05 13:58:12 -0500 | [diff] [blame] | 211 | proto_drv = bdrv_find_protocol(filename, true, &local_err); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 212 | if (!proto_drv) { |
Markus Armbruster | 2867ce4 | 2015-03-12 16:08:02 +0100 | [diff] [blame] | 213 | error_report_err(local_err); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 214 | qemu_opts_free(create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 215 | return 1; |
| 216 | } |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 217 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 218 | } |
| 219 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 220 | qemu_opts_print_help(create_opts); |
| 221 | qemu_opts_free(create_opts); |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 222 | return 0; |
| 223 | } |
| 224 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 225 | |
| 226 | static int img_open_password(BlockBackend *blk, const char *filename, |
| 227 | bool require_io, bool quiet) |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 228 | { |
| 229 | BlockDriverState *bs; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 230 | char password[256]; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 231 | |
| 232 | bs = blk_bs(blk); |
| 233 | if (bdrv_is_encrypted(bs) && require_io) { |
| 234 | qprintf(quiet, "Disk image '%s' is encrypted.\n", filename); |
| 235 | if (qemu_read_password(password, sizeof(password)) < 0) { |
| 236 | error_report("No password given"); |
| 237 | return -1; |
| 238 | } |
| 239 | if (bdrv_set_key(bs, password) < 0) { |
| 240 | error_report("invalid password"); |
| 241 | return -1; |
| 242 | } |
| 243 | } |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | |
| 248 | static BlockBackend *img_open_opts(const char *id, |
| 249 | const char *optstr, |
| 250 | QemuOpts *opts, int flags, |
| 251 | bool require_io, bool quiet) |
| 252 | { |
| 253 | QDict *options; |
| 254 | Error *local_err = NULL; |
| 255 | BlockBackend *blk; |
| 256 | options = qemu_opts_to_qdict(opts, NULL); |
| 257 | blk = blk_new_open(id, NULL, NULL, options, flags, &local_err); |
| 258 | if (!blk) { |
| 259 | error_reportf_err(local_err, "Could not open '%s'", optstr); |
| 260 | return NULL; |
| 261 | } |
| 262 | |
| 263 | if (img_open_password(blk, optstr, require_io, quiet) < 0) { |
| 264 | blk_unref(blk); |
| 265 | return NULL; |
| 266 | } |
| 267 | return blk; |
| 268 | } |
| 269 | |
| 270 | static BlockBackend *img_open_file(const char *id, const char *filename, |
| 271 | const char *fmt, int flags, |
| 272 | bool require_io, bool quiet) |
| 273 | { |
| 274 | BlockBackend *blk; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 275 | Error *local_err = NULL; |
Max Reitz | 5bd3132 | 2015-02-05 13:58:16 -0500 | [diff] [blame] | 276 | QDict *options = NULL; |
Kevin Wolf | ad71713 | 2010-12-16 15:37:41 +0100 | [diff] [blame] | 277 | |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 278 | if (fmt) { |
Max Reitz | 5bd3132 | 2015-02-05 13:58:16 -0500 | [diff] [blame] | 279 | options = qdict_new(); |
| 280 | qdict_put(options, "driver", qstring_from_str(fmt)); |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 281 | } |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 282 | |
Max Reitz | 5bd3132 | 2015-02-05 13:58:16 -0500 | [diff] [blame] | 283 | blk = blk_new_open(id, filename, NULL, options, flags, &local_err); |
| 284 | if (!blk) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 285 | error_reportf_err(local_err, "Could not open '%s': ", filename); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 286 | return NULL; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 287 | } |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 288 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 289 | if (img_open_password(blk, filename, require_io, quiet) < 0) { |
| 290 | blk_unref(blk); |
| 291 | return NULL; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 292 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 293 | return blk; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 296 | |
| 297 | static BlockBackend *img_open(const char *id, |
| 298 | bool image_opts, |
| 299 | const char *filename, |
| 300 | const char *fmt, int flags, |
| 301 | bool require_io, bool quiet) |
| 302 | { |
| 303 | BlockBackend *blk; |
| 304 | if (image_opts) { |
| 305 | QemuOpts *opts; |
| 306 | if (fmt) { |
| 307 | error_report("--image-opts and --format are mutually exclusive"); |
| 308 | return NULL; |
| 309 | } |
| 310 | opts = qemu_opts_parse_noisily(qemu_find_opts("source"), |
| 311 | filename, true); |
| 312 | if (!opts) { |
| 313 | return NULL; |
| 314 | } |
| 315 | blk = img_open_opts(id, filename, opts, flags, true, quiet); |
| 316 | } else { |
| 317 | blk = img_open_file(id, filename, fmt, flags, true, quiet); |
| 318 | } |
| 319 | return blk; |
| 320 | } |
| 321 | |
| 322 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 323 | static int add_old_style_options(const char *fmt, QemuOpts *opts, |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 324 | const char *base_filename, |
| 325 | const char *base_fmt) |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 326 | { |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 327 | Error *err = NULL; |
| 328 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 329 | if (base_filename) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 330 | qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err); |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 331 | if (err) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 332 | error_report("Backing file not supported for file format '%s'", |
| 333 | fmt); |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 334 | error_free(err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 335 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | if (base_fmt) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 339 | qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err); |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 340 | if (err) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 341 | error_report("Backing file format not supported for file " |
| 342 | "format '%s'", fmt); |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 343 | error_free(err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 344 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 345 | } |
| 346 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 347 | return 0; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 348 | } |
| 349 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 350 | static int img_create(int argc, char **argv) |
| 351 | { |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 352 | int c; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 353 | uint64_t img_size = -1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 354 | const char *fmt = "raw"; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 355 | const char *base_fmt = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 356 | const char *filename; |
| 357 | const char *base_filename = NULL; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 358 | char *options = NULL; |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 359 | Error *local_err = NULL; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 360 | bool quiet = false; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 361 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 362 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 363 | static const struct option long_options[] = { |
| 364 | {"help", no_argument, 0, 'h'}, |
| 365 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 366 | {0, 0, 0, 0} |
| 367 | }; |
| 368 | c = getopt_long(argc, argv, "F:b:f:he6o:q", |
| 369 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 370 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 371 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 372 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 373 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 374 | case '?': |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 375 | case 'h': |
| 376 | help(); |
| 377 | break; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 378 | case 'F': |
| 379 | base_fmt = optarg; |
| 380 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 381 | case 'b': |
| 382 | base_filename = optarg; |
| 383 | break; |
| 384 | case 'f': |
| 385 | fmt = optarg; |
| 386 | break; |
| 387 | case 'e': |
Markus Armbruster | 9d42e15 | 2011-06-22 14:03:55 +0200 | [diff] [blame] | 388 | error_report("option -e is deprecated, please use \'-o " |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 389 | "encryption\' instead!"); |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 390 | goto fail; |
ths | d8871c5 | 2007-10-24 16:11:42 +0000 | [diff] [blame] | 391 | case '6': |
Markus Armbruster | 9d42e15 | 2011-06-22 14:03:55 +0200 | [diff] [blame] | 392 | error_report("option -6 is deprecated, please use \'-o " |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 393 | "compat6\' instead!"); |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 394 | goto fail; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 395 | case 'o': |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 396 | if (!is_valid_option_list(optarg)) { |
| 397 | error_report("Invalid option list: %s", optarg); |
| 398 | goto fail; |
| 399 | } |
| 400 | if (!options) { |
| 401 | options = g_strdup(optarg); |
| 402 | } else { |
| 403 | char *old_options = options; |
| 404 | options = g_strdup_printf("%s,%s", options, optarg); |
| 405 | g_free(old_options); |
| 406 | } |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 407 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 408 | case 'q': |
| 409 | quiet = true; |
| 410 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 411 | case OPTION_OBJECT: { |
| 412 | QemuOpts *opts; |
| 413 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 414 | optarg, true); |
| 415 | if (!opts) { |
| 416 | goto fail; |
| 417 | } |
| 418 | } break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 419 | } |
| 420 | } |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 421 | |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 422 | /* Get the filename */ |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 423 | filename = (optind < argc) ? argv[optind] : NULL; |
| 424 | if (options && has_help_option(options)) { |
| 425 | g_free(options); |
| 426 | return print_block_option_help(filename, fmt); |
| 427 | } |
| 428 | |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 429 | if (optind >= argc) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 430 | error_exit("Expecting image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 431 | } |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 432 | optind++; |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 433 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 434 | if (qemu_opts_foreach(&qemu_object_opts, |
| 435 | user_creatable_add_opts_foreach, |
| 436 | NULL, &local_err)) { |
| 437 | error_report_err(local_err); |
| 438 | goto fail; |
| 439 | } |
| 440 | |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 441 | /* Get image size, if specified */ |
| 442 | if (optind < argc) { |
Jes Sorensen | 70b4f4b | 2011-01-05 11:41:02 +0100 | [diff] [blame] | 443 | int64_t sval; |
Markus Armbruster | e36b369 | 2011-11-22 09:46:05 +0100 | [diff] [blame] | 444 | char *end; |
Marc-André Lureau | 4677bb4 | 2015-09-16 18:02:56 +0200 | [diff] [blame] | 445 | sval = qemu_strtosz_suffix(argv[optind++], &end, |
| 446 | QEMU_STRTOSZ_DEFSUFFIX_B); |
Markus Armbruster | e36b369 | 2011-11-22 09:46:05 +0100 | [diff] [blame] | 447 | if (sval < 0 || *end) { |
liguang | 7944339 | 2012-12-17 09:49:23 +0800 | [diff] [blame] | 448 | if (sval == -ERANGE) { |
| 449 | error_report("Image size must be less than 8 EiB!"); |
| 450 | } else { |
| 451 | error_report("Invalid image size specified! You may use k, M, " |
Kevin Wolf | 5e00984 | 2013-06-05 14:19:27 +0200 | [diff] [blame] | 452 | "G, T, P or E suffixes for "); |
| 453 | error_report("kilobytes, megabytes, gigabytes, terabytes, " |
| 454 | "petabytes and exabytes."); |
liguang | 7944339 | 2012-12-17 09:49:23 +0800 | [diff] [blame] | 455 | } |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 456 | goto fail; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 457 | } |
| 458 | img_size = (uint64_t)sval; |
| 459 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 460 | if (optind != argc) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 461 | error_exit("Unexpected argument: %s", argv[optind]); |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 462 | } |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 463 | |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 464 | bdrv_img_create(filename, fmt, base_filename, base_fmt, |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 465 | options, img_size, BDRV_O_FLAGS, &local_err, quiet); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 466 | if (local_err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 467 | error_reportf_err(local_err, "%s: ", filename); |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 468 | goto fail; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 469 | } |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 470 | |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 471 | g_free(options); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 472 | return 0; |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 473 | |
| 474 | fail: |
| 475 | g_free(options); |
| 476 | return 1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 479 | static void dump_json_image_check(ImageCheck *check, bool quiet) |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 480 | { |
Markus Armbruster | 4399c43 | 2014-04-25 16:50:32 +0200 | [diff] [blame] | 481 | Error *local_err = NULL; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 482 | QString *str; |
| 483 | QmpOutputVisitor *ov = qmp_output_visitor_new(); |
| 484 | QObject *obj; |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 485 | visit_type_ImageCheck(qmp_output_get_visitor(ov), NULL, &check, |
| 486 | &local_err); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 487 | obj = qmp_output_get_qobject(ov); |
| 488 | str = qobject_to_json_pretty(obj); |
| 489 | assert(str != NULL); |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 490 | qprintf(quiet, "%s\n", qstring_get_str(str)); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 491 | qobject_decref(obj); |
| 492 | qmp_output_visitor_cleanup(ov); |
| 493 | QDECREF(str); |
| 494 | } |
| 495 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 496 | static void dump_human_image_check(ImageCheck *check, bool quiet) |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 497 | { |
| 498 | if (!(check->corruptions || check->leaks || check->check_errors)) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 499 | qprintf(quiet, "No errors were found on the image.\n"); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 500 | } else { |
| 501 | if (check->corruptions) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 502 | qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n" |
| 503 | "Data may be corrupted, or further writes to the image " |
| 504 | "may corrupt it.\n", |
| 505 | check->corruptions); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | if (check->leaks) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 509 | qprintf(quiet, |
| 510 | "\n%" PRId64 " leaked clusters were found on the image.\n" |
| 511 | "This means waste of disk space, but no harm to data.\n", |
| 512 | check->leaks); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | if (check->check_errors) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 516 | qprintf(quiet, |
| 517 | "\n%" PRId64 |
| 518 | " internal errors have occurred during the check.\n", |
| 519 | check->check_errors); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
| 523 | if (check->total_clusters != 0 && check->allocated_clusters != 0) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 524 | qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, " |
| 525 | "%0.2f%% fragmented, %0.2f%% compressed clusters\n", |
| 526 | check->allocated_clusters, check->total_clusters, |
| 527 | check->allocated_clusters * 100.0 / check->total_clusters, |
| 528 | check->fragmented_clusters * 100.0 / check->allocated_clusters, |
| 529 | check->compressed_clusters * 100.0 / |
| 530 | check->allocated_clusters); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | if (check->image_end_offset) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 534 | qprintf(quiet, |
| 535 | "Image end offset: %" PRId64 "\n", check->image_end_offset); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
| 539 | static int collect_image_check(BlockDriverState *bs, |
| 540 | ImageCheck *check, |
| 541 | const char *filename, |
| 542 | const char *fmt, |
| 543 | int fix) |
| 544 | { |
| 545 | int ret; |
| 546 | BdrvCheckResult result; |
| 547 | |
| 548 | ret = bdrv_check(bs, &result, fix); |
| 549 | if (ret < 0) { |
| 550 | return ret; |
| 551 | } |
| 552 | |
| 553 | check->filename = g_strdup(filename); |
| 554 | check->format = g_strdup(bdrv_get_format_name(bs)); |
| 555 | check->check_errors = result.check_errors; |
| 556 | check->corruptions = result.corruptions; |
| 557 | check->has_corruptions = result.corruptions != 0; |
| 558 | check->leaks = result.leaks; |
| 559 | check->has_leaks = result.leaks != 0; |
| 560 | check->corruptions_fixed = result.corruptions_fixed; |
| 561 | check->has_corruptions_fixed = result.corruptions != 0; |
| 562 | check->leaks_fixed = result.leaks_fixed; |
| 563 | check->has_leaks_fixed = result.leaks != 0; |
| 564 | check->image_end_offset = result.image_end_offset; |
| 565 | check->has_image_end_offset = result.image_end_offset != 0; |
| 566 | check->total_clusters = result.bfi.total_clusters; |
| 567 | check->has_total_clusters = result.bfi.total_clusters != 0; |
| 568 | check->allocated_clusters = result.bfi.allocated_clusters; |
| 569 | check->has_allocated_clusters = result.bfi.allocated_clusters != 0; |
| 570 | check->fragmented_clusters = result.bfi.fragmented_clusters; |
| 571 | check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0; |
Stefan Hajnoczi | e6439d7 | 2013-02-07 17:15:04 +0100 | [diff] [blame] | 572 | check->compressed_clusters = result.bfi.compressed_clusters; |
| 573 | check->has_compressed_clusters = result.bfi.compressed_clusters != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 574 | |
| 575 | return 0; |
| 576 | } |
| 577 | |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 578 | /* |
| 579 | * Checks an image for consistency. Exit codes: |
| 580 | * |
Max Reitz | d6635c4 | 2014-06-02 22:15:21 +0200 | [diff] [blame] | 581 | * 0 - Check completed, image is good |
| 582 | * 1 - Check not completed because of internal errors |
| 583 | * 2 - Check completed, image is corrupted |
| 584 | * 3 - Check completed, image has leaked clusters, but is good otherwise |
| 585 | * 63 - Checks are not supported by the image format |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 586 | */ |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 587 | static int img_check(int argc, char **argv) |
| 588 | { |
| 589 | int c, ret; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 590 | OutputFormat output_format = OFORMAT_HUMAN; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 591 | const char *filename, *fmt, *output, *cache; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 592 | BlockBackend *blk; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 593 | BlockDriverState *bs; |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 594 | int fix = 0; |
Stefan Hajnoczi | 058f8f1 | 2012-08-09 13:05:56 +0100 | [diff] [blame] | 595 | int flags = BDRV_O_FLAGS | BDRV_O_CHECK; |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 596 | ImageCheck *check; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 597 | bool quiet = false; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 598 | Error *local_err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 599 | bool image_opts = false; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 600 | |
| 601 | fmt = NULL; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 602 | output = NULL; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 603 | cache = BDRV_DEFAULT_CACHE; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 604 | for(;;) { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 605 | int option_index = 0; |
| 606 | static const struct option long_options[] = { |
| 607 | {"help", no_argument, 0, 'h'}, |
| 608 | {"format", required_argument, 0, 'f'}, |
Prasad Joshi | 4fd6a98 | 2014-03-25 00:08:54 +0530 | [diff] [blame] | 609 | {"repair", required_argument, 0, 'r'}, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 610 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 611 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 612 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 613 | {0, 0, 0, 0} |
| 614 | }; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 615 | c = getopt_long(argc, argv, "hf:r:T:q", |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 616 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 617 | if (c == -1) { |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 618 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 619 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 620 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 621 | case '?': |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 622 | case 'h': |
| 623 | help(); |
| 624 | break; |
| 625 | case 'f': |
| 626 | fmt = optarg; |
| 627 | break; |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 628 | case 'r': |
| 629 | flags |= BDRV_O_RDWR; |
| 630 | |
| 631 | if (!strcmp(optarg, "leaks")) { |
| 632 | fix = BDRV_FIX_LEAKS; |
| 633 | } else if (!strcmp(optarg, "all")) { |
| 634 | fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS; |
| 635 | } else { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 636 | error_exit("Unknown option value for -r " |
| 637 | "(expecting 'leaks' or 'all'): %s", optarg); |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 638 | } |
| 639 | break; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 640 | case OPTION_OUTPUT: |
| 641 | output = optarg; |
| 642 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 643 | case 'T': |
| 644 | cache = optarg; |
| 645 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 646 | case 'q': |
| 647 | quiet = true; |
| 648 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 649 | case OPTION_OBJECT: { |
| 650 | QemuOpts *opts; |
| 651 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 652 | optarg, true); |
| 653 | if (!opts) { |
| 654 | return 1; |
| 655 | } |
| 656 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 657 | case OPTION_IMAGE_OPTS: |
| 658 | image_opts = true; |
| 659 | break; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 660 | } |
| 661 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 662 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 663 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 664 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 665 | filename = argv[optind++]; |
| 666 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 667 | if (output && !strcmp(output, "json")) { |
| 668 | output_format = OFORMAT_JSON; |
| 669 | } else if (output && !strcmp(output, "human")) { |
| 670 | output_format = OFORMAT_HUMAN; |
| 671 | } else if (output) { |
| 672 | error_report("--output must be used with human or json as argument."); |
| 673 | return 1; |
| 674 | } |
| 675 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 676 | if (qemu_opts_foreach(&qemu_object_opts, |
| 677 | user_creatable_add_opts_foreach, |
| 678 | NULL, &local_err)) { |
| 679 | error_report_err(local_err); |
| 680 | return 1; |
| 681 | } |
| 682 | |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 683 | ret = bdrv_parse_cache_flags(cache, &flags); |
| 684 | if (ret < 0) { |
| 685 | error_report("Invalid source cache option: %s", cache); |
| 686 | return 1; |
| 687 | } |
| 688 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 689 | blk = img_open("image", image_opts, filename, fmt, flags, true, quiet); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 690 | if (!blk) { |
| 691 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 692 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 693 | bs = blk_bs(blk); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 694 | |
| 695 | check = g_new0(ImageCheck, 1); |
| 696 | ret = collect_image_check(bs, check, filename, fmt, fix); |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 697 | |
| 698 | if (ret == -ENOTSUP) { |
Max Reitz | 55d492d | 2014-05-31 21:33:30 +0200 | [diff] [blame] | 699 | error_report("This image format does not support checks"); |
Peter Lieven | fefddf9 | 2013-10-24 08:53:34 +0200 | [diff] [blame] | 700 | ret = 63; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 701 | goto fail; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 702 | } |
| 703 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 704 | if (check->corruptions_fixed || check->leaks_fixed) { |
| 705 | int corruptions_fixed, leaks_fixed; |
| 706 | |
| 707 | leaks_fixed = check->leaks_fixed; |
| 708 | corruptions_fixed = check->corruptions_fixed; |
| 709 | |
| 710 | if (output_format == OFORMAT_HUMAN) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 711 | qprintf(quiet, |
| 712 | "The following inconsistencies were found and repaired:\n\n" |
| 713 | " %" PRId64 " leaked clusters\n" |
| 714 | " %" PRId64 " corruptions\n\n" |
| 715 | "Double checking the fixed image now...\n", |
| 716 | check->leaks_fixed, |
| 717 | check->corruptions_fixed); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | ret = collect_image_check(bs, check, filename, fmt, 0); |
| 721 | |
| 722 | check->leaks_fixed = leaks_fixed; |
| 723 | check->corruptions_fixed = corruptions_fixed; |
Kevin Wolf | ccf3471 | 2012-05-11 18:16:54 +0200 | [diff] [blame] | 724 | } |
| 725 | |
Max Reitz | 832390a | 2014-10-23 15:29:12 +0200 | [diff] [blame] | 726 | if (!ret) { |
| 727 | switch (output_format) { |
| 728 | case OFORMAT_HUMAN: |
| 729 | dump_human_image_check(check, quiet); |
| 730 | break; |
| 731 | case OFORMAT_JSON: |
| 732 | dump_json_image_check(check, quiet); |
| 733 | break; |
| 734 | } |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | if (ret || check->check_errors) { |
Max Reitz | 832390a | 2014-10-23 15:29:12 +0200 | [diff] [blame] | 738 | if (ret) { |
| 739 | error_report("Check failed: %s", strerror(-ret)); |
| 740 | } else { |
| 741 | error_report("Check failed"); |
| 742 | } |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 743 | ret = 1; |
| 744 | goto fail; |
| 745 | } |
| 746 | |
| 747 | if (check->corruptions) { |
| 748 | ret = 2; |
| 749 | } else if (check->leaks) { |
| 750 | ret = 3; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 751 | } else { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 752 | ret = 0; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 755 | fail: |
| 756 | qapi_free_ImageCheck(check); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 757 | blk_unref(blk); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 758 | return ret; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 761 | typedef struct CommonBlockJobCBInfo { |
| 762 | BlockDriverState *bs; |
| 763 | Error **errp; |
| 764 | } CommonBlockJobCBInfo; |
| 765 | |
| 766 | static void common_block_job_cb(void *opaque, int ret) |
| 767 | { |
| 768 | CommonBlockJobCBInfo *cbi = opaque; |
| 769 | |
| 770 | if (ret < 0) { |
| 771 | error_setg_errno(cbi->errp, -ret, "Block job failed"); |
| 772 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | static void run_block_job(BlockJob *job, Error **errp) |
| 776 | { |
| 777 | AioContext *aio_context = bdrv_get_aio_context(job->bs); |
| 778 | |
| 779 | do { |
| 780 | aio_poll(aio_context, true); |
John Snow | 62547b8 | 2015-11-02 18:28:20 -0500 | [diff] [blame] | 781 | qemu_progress_print(job->len ? |
| 782 | ((float)job->offset / job->len * 100.f) : 0.0f, 0); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 783 | } while (!job->ready); |
| 784 | |
| 785 | block_job_complete_sync(job, errp); |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 786 | |
| 787 | /* A block job may finish instantaneously without publishing any progress, |
| 788 | * so just signal completion here */ |
| 789 | qemu_progress_print(100.f, 0); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 790 | } |
| 791 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 792 | static int img_commit(int argc, char **argv) |
| 793 | { |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 794 | int c, ret, flags; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 795 | const char *filename, *fmt, *cache, *base; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 796 | BlockBackend *blk; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 797 | BlockDriverState *bs, *base_bs; |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 798 | bool progress = false, quiet = false, drop = false; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 799 | Error *local_err = NULL; |
| 800 | CommonBlockJobCBInfo cbi; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 801 | bool image_opts = false; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 802 | |
| 803 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 804 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 805 | base = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 806 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 807 | static const struct option long_options[] = { |
| 808 | {"help", no_argument, 0, 'h'}, |
| 809 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 810 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 811 | {0, 0, 0, 0} |
| 812 | }; |
| 813 | c = getopt_long(argc, argv, "f:ht:b:dpq", |
| 814 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 815 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 816 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 817 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 818 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 819 | case '?': |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 820 | case 'h': |
| 821 | help(); |
| 822 | break; |
| 823 | case 'f': |
| 824 | fmt = optarg; |
| 825 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 826 | case 't': |
| 827 | cache = optarg; |
| 828 | break; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 829 | case 'b': |
| 830 | base = optarg; |
| 831 | /* -b implies -d */ |
| 832 | drop = true; |
| 833 | break; |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 834 | case 'd': |
| 835 | drop = true; |
| 836 | break; |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 837 | case 'p': |
| 838 | progress = true; |
| 839 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 840 | case 'q': |
| 841 | quiet = true; |
| 842 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 843 | case OPTION_OBJECT: { |
| 844 | QemuOpts *opts; |
| 845 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 846 | optarg, true); |
| 847 | if (!opts) { |
| 848 | return 1; |
| 849 | } |
| 850 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 851 | case OPTION_IMAGE_OPTS: |
| 852 | image_opts = true; |
| 853 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 854 | } |
| 855 | } |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 856 | |
| 857 | /* Progress is not shown in Quiet mode */ |
| 858 | if (quiet) { |
| 859 | progress = false; |
| 860 | } |
| 861 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 862 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 863 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 864 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 865 | filename = argv[optind++]; |
| 866 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 867 | if (qemu_opts_foreach(&qemu_object_opts, |
| 868 | user_creatable_add_opts_foreach, |
| 869 | NULL, &local_err)) { |
| 870 | error_report_err(local_err); |
| 871 | return 1; |
| 872 | } |
| 873 | |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 874 | flags = BDRV_O_RDWR | BDRV_O_UNMAP; |
Stefan Hajnoczi | c3993cd | 2011-08-04 12:26:51 +0100 | [diff] [blame] | 875 | ret = bdrv_parse_cache_flags(cache, &flags); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 876 | if (ret < 0) { |
| 877 | error_report("Invalid cache option: %s", cache); |
Stefan Hajnoczi | a3981eb | 2014-08-26 19:17:54 +0100 | [diff] [blame] | 878 | return 1; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 879 | } |
| 880 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 881 | blk = img_open("image", image_opts, filename, fmt, flags, true, quiet); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 882 | if (!blk) { |
| 883 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 884 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 885 | bs = blk_bs(blk); |
| 886 | |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 887 | qemu_progress_init(progress, 1.f); |
| 888 | qemu_progress_print(0.f, 100); |
| 889 | |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 890 | if (base) { |
| 891 | base_bs = bdrv_find_backing_image(bs, base); |
| 892 | if (!base_bs) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 893 | error_setg(&local_err, QERR_BASE_NOT_FOUND, base); |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 894 | goto done; |
| 895 | } |
| 896 | } else { |
| 897 | /* This is different from QMP, which by default uses the deepest file in |
| 898 | * the backing chain (i.e., the very base); however, the traditional |
| 899 | * behavior of qemu-img commit is using the immediate backing file. */ |
Kevin Wolf | 760e006 | 2015-06-17 14:55:21 +0200 | [diff] [blame] | 900 | base_bs = backing_bs(bs); |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 901 | if (!base_bs) { |
| 902 | error_setg(&local_err, "Image does not have a backing file"); |
| 903 | goto done; |
| 904 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 907 | cbi = (CommonBlockJobCBInfo){ |
| 908 | .errp = &local_err, |
| 909 | .bs = bs, |
| 910 | }; |
| 911 | |
| 912 | commit_active_start(bs, base_bs, 0, BLOCKDEV_ON_ERROR_REPORT, |
| 913 | common_block_job_cb, &cbi, &local_err); |
| 914 | if (local_err) { |
| 915 | goto done; |
| 916 | } |
| 917 | |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 918 | /* When the block job completes, the BlockBackend reference will point to |
| 919 | * the old backing file. In order to avoid that the top image is already |
| 920 | * deleted, so we can still empty it afterwards, increment the reference |
| 921 | * counter here preemptively. */ |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 922 | if (!drop) { |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 923 | bdrv_ref(bs); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 924 | } |
| 925 | |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 926 | run_block_job(bs->job, &local_err); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 927 | if (local_err) { |
| 928 | goto unref_backing; |
| 929 | } |
| 930 | |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 931 | if (!drop && bs->drv->bdrv_make_empty) { |
| 932 | ret = bs->drv->bdrv_make_empty(bs); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 933 | if (ret) { |
| 934 | error_setg_errno(&local_err, -ret, "Could not empty %s", |
| 935 | filename); |
| 936 | goto unref_backing; |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | unref_backing: |
| 941 | if (!drop) { |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 942 | bdrv_unref(bs); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 943 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 944 | |
| 945 | done: |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 946 | qemu_progress_end(); |
| 947 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 948 | blk_unref(blk); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 949 | |
| 950 | if (local_err) { |
Markus Armbruster | 6936f29 | 2015-02-10 15:14:02 +0100 | [diff] [blame] | 951 | error_report_err(local_err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 952 | return 1; |
| 953 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 954 | |
| 955 | qprintf(quiet, "Image committed.\n"); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 956 | return 0; |
| 957 | } |
| 958 | |
Dmitry Konishchev | f6a00aa | 2011-05-18 15:03:59 +0400 | [diff] [blame] | 959 | /* |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 960 | * Returns true iff the first sector pointed to by 'buf' contains at least |
| 961 | * a non-NUL byte. |
| 962 | * |
| 963 | * 'pnum' is set to the number of sectors (including and immediately following |
| 964 | * the first one) that are known to be in the same allocated/unallocated state. |
| 965 | */ |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 966 | static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum) |
| 967 | { |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 968 | bool is_zero; |
| 969 | int i; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 970 | |
| 971 | if (n <= 0) { |
| 972 | *pnum = 0; |
| 973 | return 0; |
| 974 | } |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 975 | is_zero = buffer_is_zero(buf, 512); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 976 | for(i = 1; i < n; i++) { |
| 977 | buf += 512; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 978 | if (is_zero != buffer_is_zero(buf, 512)) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 979 | break; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 980 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 981 | } |
| 982 | *pnum = i; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 983 | return !is_zero; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 986 | /* |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 987 | * Like is_allocated_sectors, but if the buffer starts with a used sector, |
| 988 | * up to 'min' consecutive sectors containing zeros are ignored. This avoids |
| 989 | * breaking up write requests for only small sparse areas. |
| 990 | */ |
| 991 | static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum, |
| 992 | int min) |
| 993 | { |
| 994 | int ret; |
| 995 | int num_checked, num_used; |
| 996 | |
| 997 | if (n < min) { |
| 998 | min = n; |
| 999 | } |
| 1000 | |
| 1001 | ret = is_allocated_sectors(buf, n, pnum); |
| 1002 | if (!ret) { |
| 1003 | return ret; |
| 1004 | } |
| 1005 | |
| 1006 | num_used = *pnum; |
| 1007 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 1008 | n -= *pnum; |
| 1009 | num_checked = num_used; |
| 1010 | |
| 1011 | while (n > 0) { |
| 1012 | ret = is_allocated_sectors(buf, n, pnum); |
| 1013 | |
| 1014 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 1015 | n -= *pnum; |
| 1016 | num_checked += *pnum; |
| 1017 | if (ret) { |
| 1018 | num_used = num_checked; |
| 1019 | } else if (*pnum >= min) { |
| 1020 | break; |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | *pnum = num_used; |
| 1025 | return 1; |
| 1026 | } |
| 1027 | |
| 1028 | /* |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1029 | * Compares two buffers sector by sector. Returns 0 if the first sector of both |
| 1030 | * buffers matches, non-zero otherwise. |
| 1031 | * |
| 1032 | * pnum is set to the number of sectors (including and immediately following |
| 1033 | * the first one) that are known to have the same comparison result |
| 1034 | */ |
| 1035 | static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n, |
| 1036 | int *pnum) |
| 1037 | { |
Radim Krčmář | 8c1ac47 | 2015-02-20 17:06:15 +0100 | [diff] [blame] | 1038 | bool res; |
| 1039 | int i; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1040 | |
| 1041 | if (n <= 0) { |
| 1042 | *pnum = 0; |
| 1043 | return 0; |
| 1044 | } |
| 1045 | |
| 1046 | res = !!memcmp(buf1, buf2, 512); |
| 1047 | for(i = 1; i < n; i++) { |
| 1048 | buf1 += 512; |
| 1049 | buf2 += 512; |
| 1050 | |
| 1051 | if (!!memcmp(buf1, buf2, 512) != res) { |
| 1052 | break; |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | *pnum = i; |
| 1057 | return res; |
| 1058 | } |
| 1059 | |
Kevin Wolf | 80ee15a | 2009-09-15 12:30:43 +0200 | [diff] [blame] | 1060 | #define IO_BUF_SIZE (2 * 1024 * 1024) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1061 | |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1062 | static int64_t sectors_to_bytes(int64_t sectors) |
| 1063 | { |
| 1064 | return sectors << BDRV_SECTOR_BITS; |
| 1065 | } |
| 1066 | |
| 1067 | static int64_t sectors_to_process(int64_t total, int64_t from) |
| 1068 | { |
| 1069 | return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS); |
| 1070 | } |
| 1071 | |
| 1072 | /* |
| 1073 | * Check if passed sectors are empty (not allocated or contain only 0 bytes) |
| 1074 | * |
| 1075 | * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero |
| 1076 | * data and negative value on error. |
| 1077 | * |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1078 | * @param blk: BlockBackend for the image |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1079 | * @param sect_num: Number of first sector to check |
| 1080 | * @param sect_count: Number of sectors to check |
| 1081 | * @param filename: Name of disk file we are checking (logging purpose) |
| 1082 | * @param buffer: Allocated buffer for storing read data |
| 1083 | * @param quiet: Flag for quiet mode |
| 1084 | */ |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1085 | static int check_empty_sectors(BlockBackend *blk, int64_t sect_num, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1086 | int sect_count, const char *filename, |
| 1087 | uint8_t *buffer, bool quiet) |
| 1088 | { |
| 1089 | int pnum, ret = 0; |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1090 | ret = blk_read(blk, sect_num, buffer, sect_count); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1091 | if (ret < 0) { |
| 1092 | error_report("Error while reading offset %" PRId64 " of %s: %s", |
| 1093 | sectors_to_bytes(sect_num), filename, strerror(-ret)); |
| 1094 | return ret; |
| 1095 | } |
| 1096 | ret = is_allocated_sectors(buffer, sect_count, &pnum); |
| 1097 | if (ret || pnum != sect_count) { |
| 1098 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
| 1099 | sectors_to_bytes(ret ? sect_num : sect_num + pnum)); |
| 1100 | return 1; |
| 1101 | } |
| 1102 | |
| 1103 | return 0; |
| 1104 | } |
| 1105 | |
| 1106 | /* |
| 1107 | * Compares two images. Exit codes: |
| 1108 | * |
| 1109 | * 0 - Images are identical |
| 1110 | * 1 - Images differ |
| 1111 | * >1 - Error occurred |
| 1112 | */ |
| 1113 | static int img_compare(int argc, char **argv) |
| 1114 | { |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1115 | const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1116 | BlockBackend *blk1, *blk2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1117 | BlockDriverState *bs1, *bs2; |
| 1118 | int64_t total_sectors1, total_sectors2; |
| 1119 | uint8_t *buf1 = NULL, *buf2 = NULL; |
| 1120 | int pnum1, pnum2; |
| 1121 | int allocated1, allocated2; |
| 1122 | int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */ |
| 1123 | bool progress = false, quiet = false, strict = false; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1124 | int flags; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1125 | int64_t total_sectors; |
| 1126 | int64_t sector_num = 0; |
| 1127 | int64_t nb_sectors; |
| 1128 | int c, pnum; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1129 | uint64_t progress_base; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1130 | Error *local_err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 1131 | bool image_opts = false; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1132 | |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1133 | cache = BDRV_DEFAULT_CACHE; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1134 | for (;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1135 | static const struct option long_options[] = { |
| 1136 | {"help", no_argument, 0, 'h'}, |
| 1137 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 1138 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1139 | {0, 0, 0, 0} |
| 1140 | }; |
| 1141 | c = getopt_long(argc, argv, "hf:F:T:pqs", |
| 1142 | long_options, NULL); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1143 | if (c == -1) { |
| 1144 | break; |
| 1145 | } |
| 1146 | switch (c) { |
| 1147 | case '?': |
| 1148 | case 'h': |
| 1149 | help(); |
| 1150 | break; |
| 1151 | case 'f': |
| 1152 | fmt1 = optarg; |
| 1153 | break; |
| 1154 | case 'F': |
| 1155 | fmt2 = optarg; |
| 1156 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1157 | case 'T': |
| 1158 | cache = optarg; |
| 1159 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1160 | case 'p': |
| 1161 | progress = true; |
| 1162 | break; |
| 1163 | case 'q': |
| 1164 | quiet = true; |
| 1165 | break; |
| 1166 | case 's': |
| 1167 | strict = true; |
| 1168 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1169 | case OPTION_OBJECT: { |
| 1170 | QemuOpts *opts; |
| 1171 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 1172 | optarg, true); |
| 1173 | if (!opts) { |
| 1174 | ret = 2; |
| 1175 | goto out4; |
| 1176 | } |
| 1177 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 1178 | case OPTION_IMAGE_OPTS: |
| 1179 | image_opts = true; |
| 1180 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | /* Progress is not shown in Quiet mode */ |
| 1185 | if (quiet) { |
| 1186 | progress = false; |
| 1187 | } |
| 1188 | |
| 1189 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 1190 | if (optind != argc - 2) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 1191 | error_exit("Expecting two image file names"); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1192 | } |
| 1193 | filename1 = argv[optind++]; |
| 1194 | filename2 = argv[optind++]; |
| 1195 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1196 | if (qemu_opts_foreach(&qemu_object_opts, |
| 1197 | user_creatable_add_opts_foreach, |
| 1198 | NULL, &local_err)) { |
| 1199 | error_report_err(local_err); |
| 1200 | ret = 2; |
| 1201 | goto out4; |
| 1202 | } |
| 1203 | |
Stefan Hajnoczi | cbda016 | 2014-08-26 19:17:55 +0100 | [diff] [blame] | 1204 | /* Initialize before goto out */ |
| 1205 | qemu_progress_init(progress, 2.0); |
| 1206 | |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1207 | flags = BDRV_O_FLAGS; |
| 1208 | ret = bdrv_parse_cache_flags(cache, &flags); |
| 1209 | if (ret < 0) { |
| 1210 | error_report("Invalid source cache option: %s", cache); |
| 1211 | ret = 2; |
| 1212 | goto out3; |
| 1213 | } |
| 1214 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 1215 | blk1 = img_open("image_1", image_opts, filename1, fmt1, flags, true, quiet); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1216 | if (!blk1) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1217 | ret = 2; |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1218 | goto out3; |
| 1219 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1220 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 1221 | blk2 = img_open("image_2", image_opts, filename2, fmt2, flags, true, quiet); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1222 | if (!blk2) { |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1223 | ret = 2; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1224 | goto out2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1225 | } |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 1226 | bs1 = blk_bs(blk1); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1227 | bs2 = blk_bs(blk2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1228 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1229 | buf1 = blk_blockalign(blk1, IO_BUF_SIZE); |
| 1230 | buf2 = blk_blockalign(blk2, IO_BUF_SIZE); |
| 1231 | total_sectors1 = blk_nb_sectors(blk1); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1232 | if (total_sectors1 < 0) { |
| 1233 | error_report("Can't get size of %s: %s", |
| 1234 | filename1, strerror(-total_sectors1)); |
| 1235 | ret = 4; |
| 1236 | goto out; |
| 1237 | } |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1238 | total_sectors2 = blk_nb_sectors(blk2); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1239 | if (total_sectors2 < 0) { |
| 1240 | error_report("Can't get size of %s: %s", |
| 1241 | filename2, strerror(-total_sectors2)); |
| 1242 | ret = 4; |
| 1243 | goto out; |
| 1244 | } |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1245 | total_sectors = MIN(total_sectors1, total_sectors2); |
| 1246 | progress_base = MAX(total_sectors1, total_sectors2); |
| 1247 | |
| 1248 | qemu_progress_print(0, 100); |
| 1249 | |
| 1250 | if (strict && total_sectors1 != total_sectors2) { |
| 1251 | ret = 1; |
| 1252 | qprintf(quiet, "Strict mode: Image size mismatch!\n"); |
| 1253 | goto out; |
| 1254 | } |
| 1255 | |
| 1256 | for (;;) { |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1257 | int64_t status1, status2; |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 1258 | BlockDriverState *file; |
| 1259 | |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1260 | nb_sectors = sectors_to_process(total_sectors, sector_num); |
| 1261 | if (nb_sectors <= 0) { |
| 1262 | break; |
| 1263 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1264 | status1 = bdrv_get_block_status_above(bs1, NULL, sector_num, |
| 1265 | total_sectors1 - sector_num, |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 1266 | &pnum1, &file); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1267 | if (status1 < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1268 | ret = 3; |
| 1269 | error_report("Sector allocation test failed for %s", filename1); |
| 1270 | goto out; |
| 1271 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1272 | allocated1 = status1 & BDRV_BLOCK_ALLOCATED; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1273 | |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1274 | status2 = bdrv_get_block_status_above(bs2, NULL, sector_num, |
| 1275 | total_sectors2 - sector_num, |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 1276 | &pnum2, &file); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1277 | if (status2 < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1278 | ret = 3; |
| 1279 | error_report("Sector allocation test failed for %s", filename2); |
| 1280 | goto out; |
| 1281 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1282 | allocated2 = status2 & BDRV_BLOCK_ALLOCATED; |
| 1283 | if (pnum1) { |
| 1284 | nb_sectors = MIN(nb_sectors, pnum1); |
| 1285 | } |
| 1286 | if (pnum2) { |
| 1287 | nb_sectors = MIN(nb_sectors, pnum2); |
| 1288 | } |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1289 | |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1290 | if (strict) { |
| 1291 | if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) != |
| 1292 | (status2 & ~BDRV_BLOCK_OFFSET_MASK)) { |
| 1293 | ret = 1; |
| 1294 | qprintf(quiet, "Strict mode: Offset %" PRId64 |
| 1295 | " block status mismatch!\n", |
| 1296 | sectors_to_bytes(sector_num)); |
| 1297 | goto out; |
| 1298 | } |
| 1299 | } |
| 1300 | if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) { |
| 1301 | nb_sectors = MIN(pnum1, pnum2); |
| 1302 | } else if (allocated1 == allocated2) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1303 | if (allocated1) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1304 | ret = blk_read(blk1, sector_num, buf1, nb_sectors); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1305 | if (ret < 0) { |
| 1306 | error_report("Error while reading offset %" PRId64 " of %s:" |
| 1307 | " %s", sectors_to_bytes(sector_num), filename1, |
| 1308 | strerror(-ret)); |
| 1309 | ret = 4; |
| 1310 | goto out; |
| 1311 | } |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1312 | ret = blk_read(blk2, sector_num, buf2, nb_sectors); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1313 | if (ret < 0) { |
| 1314 | error_report("Error while reading offset %" PRId64 |
| 1315 | " of %s: %s", sectors_to_bytes(sector_num), |
| 1316 | filename2, strerror(-ret)); |
| 1317 | ret = 4; |
| 1318 | goto out; |
| 1319 | } |
| 1320 | ret = compare_sectors(buf1, buf2, nb_sectors, &pnum); |
| 1321 | if (ret || pnum != nb_sectors) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1322 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
| 1323 | sectors_to_bytes( |
| 1324 | ret ? sector_num : sector_num + pnum)); |
Fam Zheng | 36452f1 | 2013-11-13 20:26:49 +0800 | [diff] [blame] | 1325 | ret = 1; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1326 | goto out; |
| 1327 | } |
| 1328 | } |
| 1329 | } else { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1330 | |
| 1331 | if (allocated1) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1332 | ret = check_empty_sectors(blk1, sector_num, nb_sectors, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1333 | filename1, buf1, quiet); |
| 1334 | } else { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1335 | ret = check_empty_sectors(blk2, sector_num, nb_sectors, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1336 | filename2, buf1, quiet); |
| 1337 | } |
| 1338 | if (ret) { |
| 1339 | if (ret < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1340 | error_report("Error while reading offset %" PRId64 ": %s", |
| 1341 | sectors_to_bytes(sector_num), strerror(-ret)); |
Fam Zheng | 36452f1 | 2013-11-13 20:26:49 +0800 | [diff] [blame] | 1342 | ret = 4; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1343 | } |
| 1344 | goto out; |
| 1345 | } |
| 1346 | } |
| 1347 | sector_num += nb_sectors; |
| 1348 | qemu_progress_print(((float) nb_sectors / progress_base)*100, 100); |
| 1349 | } |
| 1350 | |
| 1351 | if (total_sectors1 != total_sectors2) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1352 | BlockBackend *blk_over; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1353 | int64_t total_sectors_over; |
| 1354 | const char *filename_over; |
| 1355 | |
| 1356 | qprintf(quiet, "Warning: Image size mismatch!\n"); |
| 1357 | if (total_sectors1 > total_sectors2) { |
| 1358 | total_sectors_over = total_sectors1; |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1359 | blk_over = blk1; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1360 | filename_over = filename1; |
| 1361 | } else { |
| 1362 | total_sectors_over = total_sectors2; |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1363 | blk_over = blk2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1364 | filename_over = filename2; |
| 1365 | } |
| 1366 | |
| 1367 | for (;;) { |
| 1368 | nb_sectors = sectors_to_process(total_sectors_over, sector_num); |
| 1369 | if (nb_sectors <= 0) { |
| 1370 | break; |
| 1371 | } |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1372 | ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL, sector_num, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1373 | nb_sectors, &pnum); |
| 1374 | if (ret < 0) { |
| 1375 | ret = 3; |
| 1376 | error_report("Sector allocation test failed for %s", |
| 1377 | filename_over); |
| 1378 | goto out; |
| 1379 | |
| 1380 | } |
| 1381 | nb_sectors = pnum; |
| 1382 | if (ret) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1383 | ret = check_empty_sectors(blk_over, sector_num, nb_sectors, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1384 | filename_over, buf1, quiet); |
| 1385 | if (ret) { |
| 1386 | if (ret < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1387 | error_report("Error while reading offset %" PRId64 |
| 1388 | " of %s: %s", sectors_to_bytes(sector_num), |
| 1389 | filename_over, strerror(-ret)); |
Fam Zheng | 36452f1 | 2013-11-13 20:26:49 +0800 | [diff] [blame] | 1390 | ret = 4; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1391 | } |
| 1392 | goto out; |
| 1393 | } |
| 1394 | } |
| 1395 | sector_num += nb_sectors; |
| 1396 | qemu_progress_print(((float) nb_sectors / progress_base)*100, 100); |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | qprintf(quiet, "Images are identical.\n"); |
| 1401 | ret = 0; |
| 1402 | |
| 1403 | out: |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1404 | qemu_vfree(buf1); |
| 1405 | qemu_vfree(buf2); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1406 | blk_unref(blk2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1407 | out2: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1408 | blk_unref(blk1); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1409 | out3: |
| 1410 | qemu_progress_end(); |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1411 | out4: |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1412 | return ret; |
| 1413 | } |
| 1414 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1415 | enum ImgConvertBlockStatus { |
| 1416 | BLK_DATA, |
| 1417 | BLK_ZERO, |
| 1418 | BLK_BACKING_FILE, |
| 1419 | }; |
| 1420 | |
| 1421 | typedef struct ImgConvertState { |
| 1422 | BlockBackend **src; |
| 1423 | int64_t *src_sectors; |
| 1424 | int src_cur, src_num; |
| 1425 | int64_t src_cur_offset; |
| 1426 | int64_t total_sectors; |
| 1427 | int64_t allocated_sectors; |
| 1428 | enum ImgConvertBlockStatus status; |
| 1429 | int64_t sector_next_status; |
| 1430 | BlockBackend *target; |
| 1431 | bool has_zero_init; |
| 1432 | bool compressed; |
| 1433 | bool target_has_backing; |
| 1434 | int min_sparse; |
| 1435 | size_t cluster_sectors; |
| 1436 | size_t buf_sectors; |
| 1437 | } ImgConvertState; |
| 1438 | |
| 1439 | static void convert_select_part(ImgConvertState *s, int64_t sector_num) |
| 1440 | { |
| 1441 | assert(sector_num >= s->src_cur_offset); |
| 1442 | while (sector_num - s->src_cur_offset >= s->src_sectors[s->src_cur]) { |
| 1443 | s->src_cur_offset += s->src_sectors[s->src_cur]; |
| 1444 | s->src_cur++; |
| 1445 | assert(s->src_cur < s->src_num); |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num) |
| 1450 | { |
| 1451 | int64_t ret; |
| 1452 | int n; |
| 1453 | |
| 1454 | convert_select_part(s, sector_num); |
| 1455 | |
| 1456 | assert(s->total_sectors > sector_num); |
| 1457 | n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS); |
| 1458 | |
| 1459 | if (s->sector_next_status <= sector_num) { |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 1460 | BlockDriverState *file; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1461 | ret = bdrv_get_block_status(blk_bs(s->src[s->src_cur]), |
| 1462 | sector_num - s->src_cur_offset, |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 1463 | n, &n, &file); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1464 | if (ret < 0) { |
| 1465 | return ret; |
| 1466 | } |
| 1467 | |
| 1468 | if (ret & BDRV_BLOCK_ZERO) { |
| 1469 | s->status = BLK_ZERO; |
| 1470 | } else if (ret & BDRV_BLOCK_DATA) { |
| 1471 | s->status = BLK_DATA; |
| 1472 | } else if (!s->target_has_backing) { |
| 1473 | /* Without a target backing file we must copy over the contents of |
| 1474 | * the backing file as well. */ |
| 1475 | /* TODO Check block status of the backing file chain to avoid |
| 1476 | * needlessly reading zeroes and limiting the iteration to the |
| 1477 | * buffer size */ |
| 1478 | s->status = BLK_DATA; |
| 1479 | } else { |
| 1480 | s->status = BLK_BACKING_FILE; |
| 1481 | } |
| 1482 | |
| 1483 | s->sector_next_status = sector_num + n; |
| 1484 | } |
| 1485 | |
| 1486 | n = MIN(n, s->sector_next_status - sector_num); |
| 1487 | if (s->status == BLK_DATA) { |
| 1488 | n = MIN(n, s->buf_sectors); |
| 1489 | } |
| 1490 | |
| 1491 | /* We need to write complete clusters for compressed images, so if an |
| 1492 | * unallocated area is shorter than that, we must consider the whole |
| 1493 | * cluster allocated. */ |
| 1494 | if (s->compressed) { |
| 1495 | if (n < s->cluster_sectors) { |
| 1496 | n = MIN(s->cluster_sectors, s->total_sectors - sector_num); |
| 1497 | s->status = BLK_DATA; |
| 1498 | } else { |
| 1499 | n = QEMU_ALIGN_DOWN(n, s->cluster_sectors); |
| 1500 | } |
| 1501 | } |
| 1502 | |
| 1503 | return n; |
| 1504 | } |
| 1505 | |
| 1506 | static int convert_read(ImgConvertState *s, int64_t sector_num, int nb_sectors, |
| 1507 | uint8_t *buf) |
| 1508 | { |
| 1509 | int n; |
| 1510 | int ret; |
| 1511 | |
| 1512 | if (s->status == BLK_ZERO || s->status == BLK_BACKING_FILE) { |
| 1513 | return 0; |
| 1514 | } |
| 1515 | |
| 1516 | assert(nb_sectors <= s->buf_sectors); |
| 1517 | while (nb_sectors > 0) { |
| 1518 | BlockBackend *blk; |
| 1519 | int64_t bs_sectors; |
| 1520 | |
| 1521 | /* In the case of compression with multiple source files, we can get a |
| 1522 | * nb_sectors that spreads into the next part. So we must be able to |
| 1523 | * read across multiple BDSes for one convert_read() call. */ |
| 1524 | convert_select_part(s, sector_num); |
| 1525 | blk = s->src[s->src_cur]; |
| 1526 | bs_sectors = s->src_sectors[s->src_cur]; |
| 1527 | |
| 1528 | n = MIN(nb_sectors, bs_sectors - (sector_num - s->src_cur_offset)); |
| 1529 | ret = blk_read(blk, sector_num - s->src_cur_offset, buf, n); |
| 1530 | if (ret < 0) { |
| 1531 | return ret; |
| 1532 | } |
| 1533 | |
| 1534 | sector_num += n; |
| 1535 | nb_sectors -= n; |
| 1536 | buf += n * BDRV_SECTOR_SIZE; |
| 1537 | } |
| 1538 | |
| 1539 | return 0; |
| 1540 | } |
| 1541 | |
| 1542 | static int convert_write(ImgConvertState *s, int64_t sector_num, int nb_sectors, |
| 1543 | const uint8_t *buf) |
| 1544 | { |
| 1545 | int ret; |
| 1546 | |
| 1547 | while (nb_sectors > 0) { |
| 1548 | int n = nb_sectors; |
| 1549 | |
| 1550 | switch (s->status) { |
| 1551 | case BLK_BACKING_FILE: |
| 1552 | /* If we have a backing file, leave clusters unallocated that are |
| 1553 | * unallocated in the source image, so that the backing file is |
| 1554 | * visible at the respective offset. */ |
| 1555 | assert(s->target_has_backing); |
| 1556 | break; |
| 1557 | |
| 1558 | case BLK_DATA: |
| 1559 | /* We must always write compressed clusters as a whole, so don't |
| 1560 | * try to find zeroed parts in the buffer. We can only save the |
| 1561 | * write if the buffer is completely zeroed and we're allowed to |
| 1562 | * keep the target sparse. */ |
| 1563 | if (s->compressed) { |
| 1564 | if (s->has_zero_init && s->min_sparse && |
| 1565 | buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)) |
| 1566 | { |
| 1567 | assert(!s->target_has_backing); |
| 1568 | break; |
| 1569 | } |
| 1570 | |
| 1571 | ret = blk_write_compressed(s->target, sector_num, buf, n); |
| 1572 | if (ret < 0) { |
| 1573 | return ret; |
| 1574 | } |
| 1575 | break; |
| 1576 | } |
| 1577 | |
| 1578 | /* If there is real non-zero data or we're told to keep the target |
| 1579 | * fully allocated (-S 0), we must write it. Otherwise we can treat |
| 1580 | * it as zero sectors. */ |
| 1581 | if (!s->min_sparse || |
| 1582 | is_allocated_sectors_min(buf, n, &n, s->min_sparse)) |
| 1583 | { |
| 1584 | ret = blk_write(s->target, sector_num, buf, n); |
| 1585 | if (ret < 0) { |
| 1586 | return ret; |
| 1587 | } |
| 1588 | break; |
| 1589 | } |
| 1590 | /* fall-through */ |
| 1591 | |
| 1592 | case BLK_ZERO: |
| 1593 | if (s->has_zero_init) { |
| 1594 | break; |
| 1595 | } |
| 1596 | ret = blk_write_zeroes(s->target, sector_num, n, 0); |
| 1597 | if (ret < 0) { |
| 1598 | return ret; |
| 1599 | } |
| 1600 | break; |
| 1601 | } |
| 1602 | |
| 1603 | sector_num += n; |
| 1604 | nb_sectors -= n; |
| 1605 | buf += n * BDRV_SECTOR_SIZE; |
| 1606 | } |
| 1607 | |
| 1608 | return 0; |
| 1609 | } |
| 1610 | |
| 1611 | static int convert_do_copy(ImgConvertState *s) |
| 1612 | { |
| 1613 | uint8_t *buf = NULL; |
| 1614 | int64_t sector_num, allocated_done; |
| 1615 | int ret; |
| 1616 | int n; |
| 1617 | |
| 1618 | /* Check whether we have zero initialisation or can get it efficiently */ |
| 1619 | s->has_zero_init = s->min_sparse && !s->target_has_backing |
| 1620 | ? bdrv_has_zero_init(blk_bs(s->target)) |
| 1621 | : false; |
| 1622 | |
| 1623 | if (!s->has_zero_init && !s->target_has_backing && |
| 1624 | bdrv_can_write_zeroes_with_unmap(blk_bs(s->target))) |
| 1625 | { |
| 1626 | ret = bdrv_make_zero(blk_bs(s->target), BDRV_REQ_MAY_UNMAP); |
| 1627 | if (ret == 0) { |
| 1628 | s->has_zero_init = true; |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | /* Allocate buffer for copied data. For compressed images, only one cluster |
| 1633 | * can be copied at a time. */ |
| 1634 | if (s->compressed) { |
| 1635 | if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) { |
| 1636 | error_report("invalid cluster size"); |
| 1637 | ret = -EINVAL; |
| 1638 | goto fail; |
| 1639 | } |
| 1640 | s->buf_sectors = s->cluster_sectors; |
| 1641 | } |
| 1642 | buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE); |
| 1643 | |
| 1644 | /* Calculate allocated sectors for progress */ |
| 1645 | s->allocated_sectors = 0; |
| 1646 | sector_num = 0; |
| 1647 | while (sector_num < s->total_sectors) { |
| 1648 | n = convert_iteration_sectors(s, sector_num); |
| 1649 | if (n < 0) { |
| 1650 | ret = n; |
| 1651 | goto fail; |
| 1652 | } |
| 1653 | if (s->status == BLK_DATA) { |
| 1654 | s->allocated_sectors += n; |
| 1655 | } |
| 1656 | sector_num += n; |
| 1657 | } |
| 1658 | |
| 1659 | /* Do the copy */ |
| 1660 | s->src_cur = 0; |
| 1661 | s->src_cur_offset = 0; |
| 1662 | s->sector_next_status = 0; |
| 1663 | |
| 1664 | sector_num = 0; |
| 1665 | allocated_done = 0; |
| 1666 | |
| 1667 | while (sector_num < s->total_sectors) { |
| 1668 | n = convert_iteration_sectors(s, sector_num); |
| 1669 | if (n < 0) { |
| 1670 | ret = n; |
| 1671 | goto fail; |
| 1672 | } |
| 1673 | if (s->status == BLK_DATA) { |
| 1674 | allocated_done += n; |
| 1675 | qemu_progress_print(100.0 * allocated_done / s->allocated_sectors, |
| 1676 | 0); |
| 1677 | } |
| 1678 | |
| 1679 | ret = convert_read(s, sector_num, n, buf); |
| 1680 | if (ret < 0) { |
| 1681 | error_report("error while reading sector %" PRId64 |
| 1682 | ": %s", sector_num, strerror(-ret)); |
| 1683 | goto fail; |
| 1684 | } |
| 1685 | |
| 1686 | ret = convert_write(s, sector_num, n, buf); |
| 1687 | if (ret < 0) { |
| 1688 | error_report("error while writing sector %" PRId64 |
| 1689 | ": %s", sector_num, strerror(-ret)); |
| 1690 | goto fail; |
| 1691 | } |
| 1692 | |
| 1693 | sector_num += n; |
| 1694 | } |
| 1695 | |
| 1696 | if (s->compressed) { |
| 1697 | /* signal EOF to align */ |
| 1698 | ret = blk_write_compressed(s->target, 0, NULL, 0); |
| 1699 | if (ret < 0) { |
| 1700 | goto fail; |
| 1701 | } |
| 1702 | } |
| 1703 | |
| 1704 | ret = 0; |
| 1705 | fail: |
| 1706 | qemu_vfree(buf); |
| 1707 | return ret; |
| 1708 | } |
| 1709 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1710 | static int img_convert(int argc, char **argv) |
| 1711 | { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1712 | int c, bs_n, bs_i, compress, cluster_sectors, skip_create; |
Peter Lieven | 13c28af | 2013-11-27 11:07:01 +0100 | [diff] [blame] | 1713 | int64_t ret = 0; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1714 | int progress = 0, flags, src_flags; |
| 1715 | const char *fmt, *out_fmt, *cache, *src_cache, *out_baseimg, *out_filename; |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 1716 | BlockDriver *drv, *proto_drv; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1717 | BlockBackend **blk = NULL, *out_blk = NULL; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1718 | BlockDriverState **bs = NULL, *out_bs = NULL; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1719 | int64_t total_sectors; |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1720 | int64_t *bs_sectors = NULL; |
Peter Lieven | f2521c9 | 2013-11-27 11:07:06 +0100 | [diff] [blame] | 1721 | size_t bufsectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1722 | BlockDriverInfo bdi; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 1723 | QemuOpts *opts = NULL; |
| 1724 | QemuOptsList *create_opts = NULL; |
| 1725 | const char *out_baseimg_param; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1726 | char *options = NULL; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 1727 | const char *snapshot_name = NULL; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1728 | int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */ |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 1729 | bool quiet = false; |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 1730 | Error *local_err = NULL; |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 1731 | QemuOpts *sn_opts = NULL; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1732 | ImgConvertState state; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 1733 | bool image_opts = false; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1734 | |
| 1735 | fmt = NULL; |
| 1736 | out_fmt = "raw"; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1737 | cache = "unsafe"; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1738 | src_cache = BDRV_DEFAULT_CACHE; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1739 | out_baseimg = NULL; |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 1740 | compress = 0; |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 1741 | skip_create = 0; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1742 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1743 | static const struct option long_options[] = { |
| 1744 | {"help", no_argument, 0, 'h'}, |
| 1745 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 1746 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1747 | {0, 0, 0, 0} |
| 1748 | }; |
| 1749 | c = getopt_long(argc, argv, "hf:O:B:ce6o:s:l:S:pt:T:qn", |
| 1750 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1751 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1752 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1753 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1754 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 1755 | case '?': |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1756 | case 'h': |
| 1757 | help(); |
| 1758 | break; |
| 1759 | case 'f': |
| 1760 | fmt = optarg; |
| 1761 | break; |
| 1762 | case 'O': |
| 1763 | out_fmt = optarg; |
| 1764 | break; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1765 | case 'B': |
| 1766 | out_baseimg = optarg; |
| 1767 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1768 | case 'c': |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 1769 | compress = 1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1770 | break; |
| 1771 | case 'e': |
Markus Armbruster | 9d42e15 | 2011-06-22 14:03:55 +0200 | [diff] [blame] | 1772 | error_report("option -e is deprecated, please use \'-o " |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 1773 | "encryption\' instead!"); |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 1774 | ret = -1; |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 1775 | goto fail_getopt; |
ths | ec36ba1 | 2007-09-16 21:59:02 +0000 | [diff] [blame] | 1776 | case '6': |
Markus Armbruster | 9d42e15 | 2011-06-22 14:03:55 +0200 | [diff] [blame] | 1777 | error_report("option -6 is deprecated, please use \'-o " |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 1778 | "compat6\' instead!"); |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 1779 | ret = -1; |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 1780 | goto fail_getopt; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1781 | case 'o': |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 1782 | if (!is_valid_option_list(optarg)) { |
| 1783 | error_report("Invalid option list: %s", optarg); |
| 1784 | ret = -1; |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 1785 | goto fail_getopt; |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 1786 | } |
| 1787 | if (!options) { |
| 1788 | options = g_strdup(optarg); |
| 1789 | } else { |
| 1790 | char *old_options = options; |
| 1791 | options = g_strdup_printf("%s,%s", options, optarg); |
| 1792 | g_free(old_options); |
| 1793 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1794 | break; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 1795 | case 's': |
| 1796 | snapshot_name = optarg; |
| 1797 | break; |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 1798 | case 'l': |
| 1799 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { |
Markus Armbruster | 70b9433 | 2015-02-13 12:50:26 +0100 | [diff] [blame] | 1800 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
| 1801 | optarg, false); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 1802 | if (!sn_opts) { |
| 1803 | error_report("Failed in parsing snapshot param '%s'", |
| 1804 | optarg); |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 1805 | ret = -1; |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 1806 | goto fail_getopt; |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 1807 | } |
| 1808 | } else { |
| 1809 | snapshot_name = optarg; |
| 1810 | } |
| 1811 | break; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1812 | case 'S': |
| 1813 | { |
| 1814 | int64_t sval; |
Markus Armbruster | e36b369 | 2011-11-22 09:46:05 +0100 | [diff] [blame] | 1815 | char *end; |
Marc-André Lureau | 4677bb4 | 2015-09-16 18:02:56 +0200 | [diff] [blame] | 1816 | sval = qemu_strtosz_suffix(optarg, &end, QEMU_STRTOSZ_DEFSUFFIX_B); |
Markus Armbruster | e36b369 | 2011-11-22 09:46:05 +0100 | [diff] [blame] | 1817 | if (sval < 0 || *end) { |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1818 | error_report("Invalid minimum zero buffer size for sparse output specified"); |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 1819 | ret = -1; |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 1820 | goto fail_getopt; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1821 | } |
| 1822 | |
| 1823 | min_sparse = sval / BDRV_SECTOR_SIZE; |
| 1824 | break; |
| 1825 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1826 | case 'p': |
| 1827 | progress = 1; |
| 1828 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1829 | case 't': |
| 1830 | cache = optarg; |
| 1831 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1832 | case 'T': |
| 1833 | src_cache = optarg; |
| 1834 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 1835 | case 'q': |
| 1836 | quiet = true; |
| 1837 | break; |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 1838 | case 'n': |
| 1839 | skip_create = 1; |
| 1840 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1841 | case OPTION_OBJECT: |
| 1842 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 1843 | optarg, true); |
| 1844 | if (!opts) { |
| 1845 | goto fail_getopt; |
| 1846 | } |
| 1847 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 1848 | case OPTION_IMAGE_OPTS: |
| 1849 | image_opts = true; |
| 1850 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1851 | } |
| 1852 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1853 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1854 | if (qemu_opts_foreach(&qemu_object_opts, |
| 1855 | user_creatable_add_opts_foreach, |
| 1856 | NULL, &local_err)) { |
| 1857 | error_report_err(local_err); |
| 1858 | goto fail_getopt; |
| 1859 | } |
| 1860 | |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 1861 | /* Initialize before goto out */ |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 1862 | if (quiet) { |
| 1863 | progress = 0; |
| 1864 | } |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 1865 | qemu_progress_init(progress, 1.0); |
| 1866 | |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1867 | bs_n = argc - optind - 1; |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 1868 | out_filename = bs_n >= 1 ? argv[argc - 1] : NULL; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1869 | |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 1870 | if (options && has_help_option(options)) { |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 1871 | ret = print_block_option_help(out_filename, out_fmt); |
| 1872 | goto out; |
| 1873 | } |
| 1874 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1875 | if (bs_n < 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 1876 | error_exit("Must specify image file name"); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1877 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1878 | |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1879 | |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1880 | if (bs_n > 1 && out_baseimg) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1881 | error_report("-B makes no sense when concatenating multiple input " |
| 1882 | "images"); |
Jes Sorensen | 31ca34b | 2010-12-06 15:25:36 +0100 | [diff] [blame] | 1883 | ret = -1; |
| 1884 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1885 | } |
Dong Xu Wang | f8111c2 | 2012-03-15 20:13:31 +0800 | [diff] [blame] | 1886 | |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1887 | src_flags = BDRV_O_FLAGS; |
| 1888 | ret = bdrv_parse_cache_flags(src_cache, &src_flags); |
| 1889 | if (ret < 0) { |
| 1890 | error_report("Invalid source cache option: %s", src_cache); |
| 1891 | goto out; |
| 1892 | } |
| 1893 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1894 | qemu_progress_print(0, 100); |
| 1895 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1896 | blk = g_new0(BlockBackend *, bs_n); |
Markus Armbruster | d739f1c | 2014-06-26 13:23:24 +0200 | [diff] [blame] | 1897 | bs = g_new0(BlockDriverState *, bs_n); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1898 | bs_sectors = g_new(int64_t, bs_n); |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1899 | |
| 1900 | total_sectors = 0; |
| 1901 | for (bs_i = 0; bs_i < bs_n; bs_i++) { |
Kevin Wolf | 9aebf3b | 2014-09-25 09:54:02 +0200 | [diff] [blame] | 1902 | char *id = bs_n > 1 ? g_strdup_printf("source_%d", bs_i) |
Kevin Wolf | 9ffe333 | 2014-04-17 16:57:13 +0200 | [diff] [blame] | 1903 | : g_strdup("source"); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 1904 | blk[bs_i] = img_open(id, image_opts, argv[optind + bs_i], |
| 1905 | fmt, src_flags, true, quiet); |
Kevin Wolf | 9ffe333 | 2014-04-17 16:57:13 +0200 | [diff] [blame] | 1906 | g_free(id); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1907 | if (!blk[bs_i]) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1908 | ret = -1; |
| 1909 | goto out; |
| 1910 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1911 | bs[bs_i] = blk_bs(blk[bs_i]); |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1912 | bs_sectors[bs_i] = blk_nb_sectors(blk[bs_i]); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1913 | if (bs_sectors[bs_i] < 0) { |
| 1914 | error_report("Could not get size of %s: %s", |
| 1915 | argv[optind + bs_i], strerror(-bs_sectors[bs_i])); |
| 1916 | ret = -1; |
| 1917 | goto out; |
| 1918 | } |
Markus Armbruster | d739f1c | 2014-06-26 13:23:24 +0200 | [diff] [blame] | 1919 | total_sectors += bs_sectors[bs_i]; |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1920 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1921 | |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 1922 | if (sn_opts) { |
| 1923 | ret = bdrv_snapshot_load_tmp(bs[0], |
| 1924 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), |
| 1925 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), |
| 1926 | &local_err); |
| 1927 | } else if (snapshot_name != NULL) { |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 1928 | if (bs_n > 1) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 1929 | error_report("No support for concatenating multiple snapshot"); |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 1930 | ret = -1; |
| 1931 | goto out; |
| 1932 | } |
Wenchao Xia | 7b4c478 | 2013-12-04 17:10:54 +0800 | [diff] [blame] | 1933 | |
| 1934 | bdrv_snapshot_load_tmp_by_id_or_name(bs[0], snapshot_name, &local_err); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 1935 | } |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 1936 | if (local_err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 1937 | error_reportf_err(local_err, "Failed to load snapshot: "); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 1938 | ret = -1; |
| 1939 | goto out; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 1940 | } |
| 1941 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1942 | /* Find driver and parse its options */ |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1943 | drv = bdrv_find_format(out_fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1944 | if (!drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1945 | error_report("Unknown file format '%s'", out_fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1946 | ret = -1; |
| 1947 | goto out; |
| 1948 | } |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1949 | |
Max Reitz | b65a5e1 | 2015-02-05 13:58:12 -0500 | [diff] [blame] | 1950 | proto_drv = bdrv_find_protocol(out_filename, true, &local_err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1951 | if (!proto_drv) { |
Markus Armbruster | 2867ce4 | 2015-03-12 16:08:02 +0100 | [diff] [blame] | 1952 | error_report_err(local_err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1953 | ret = -1; |
| 1954 | goto out; |
| 1955 | } |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 1956 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 1957 | if (!skip_create) { |
| 1958 | if (!drv->create_opts) { |
| 1959 | error_report("Format driver '%s' does not support image creation", |
| 1960 | drv->format_name); |
| 1961 | ret = -1; |
| 1962 | goto out; |
| 1963 | } |
Max Reitz | f75613c | 2014-12-02 18:32:46 +0100 | [diff] [blame] | 1964 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 1965 | if (!proto_drv->create_opts) { |
| 1966 | error_report("Protocol driver '%s' does not support image creation", |
| 1967 | proto_drv->format_name); |
| 1968 | ret = -1; |
| 1969 | goto out; |
| 1970 | } |
Max Reitz | f75613c | 2014-12-02 18:32:46 +0100 | [diff] [blame] | 1971 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 1972 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 1973 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
Kevin Wolf | db08adf | 2009-06-04 15:39:38 +0200 | [diff] [blame] | 1974 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 1975 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 1976 | if (options) { |
| 1977 | qemu_opts_do_parse(opts, options, NULL, &local_err); |
| 1978 | if (local_err) { |
Markus Armbruster | 97a2ca7 | 2015-03-14 10:23:15 +0100 | [diff] [blame] | 1979 | error_report_err(local_err); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 1980 | ret = -1; |
| 1981 | goto out; |
| 1982 | } |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 1983 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1984 | |
Markus Armbruster | 39101f2 | 2015-02-12 16:46:36 +0100 | [diff] [blame] | 1985 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_sectors * 512, |
| 1986 | &error_abort); |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 1987 | ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL); |
| 1988 | if (ret < 0) { |
| 1989 | goto out; |
| 1990 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1991 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1992 | |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 1993 | /* Get backing file name if -o backing_file was used */ |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 1994 | out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 1995 | if (out_baseimg_param) { |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 1996 | out_baseimg = out_baseimg_param; |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 1997 | } |
| 1998 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1999 | /* Check if compression is supported */ |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 2000 | if (compress) { |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2001 | bool encryption = |
| 2002 | qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false); |
| 2003 | const char *preallocation = |
| 2004 | qemu_opt_get(opts, BLOCK_OPT_PREALLOC); |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2005 | |
| 2006 | if (!drv->bdrv_write_compressed) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2007 | error_report("Compression not supported for this file format"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2008 | ret = -1; |
| 2009 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2010 | } |
| 2011 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2012 | if (encryption) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2013 | error_report("Compression and encryption not supported at " |
| 2014 | "the same time"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2015 | ret = -1; |
| 2016 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2017 | } |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 2018 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2019 | if (preallocation |
| 2020 | && strcmp(preallocation, "off")) |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 2021 | { |
| 2022 | error_report("Compression and preallocation not supported at " |
| 2023 | "the same time"); |
| 2024 | ret = -1; |
| 2025 | goto out; |
| 2026 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2027 | } |
| 2028 | |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2029 | if (!skip_create) { |
| 2030 | /* Create the new image */ |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 2031 | ret = bdrv_create(drv, out_filename, opts, &local_err); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2032 | if (ret < 0) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2033 | error_reportf_err(local_err, "%s: error while converting %s: ", |
| 2034 | out_filename, out_fmt); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2035 | goto out; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2036 | } |
| 2037 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2038 | |
Peter Lieven | 5a37b60 | 2013-10-24 12:07:06 +0200 | [diff] [blame] | 2039 | flags = min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR; |
Stefan Hajnoczi | c3993cd | 2011-08-04 12:26:51 +0100 | [diff] [blame] | 2040 | ret = bdrv_parse_cache_flags(cache, &flags); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2041 | if (ret < 0) { |
| 2042 | error_report("Invalid cache option: %s", cache); |
Markus Armbruster | bb9cd2e | 2014-05-28 11:17:07 +0200 | [diff] [blame] | 2043 | goto out; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2044 | } |
| 2045 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 2046 | /* XXX we should allow --image-opts to trigger use of |
| 2047 | * img_open() here, but then we have trouble with |
| 2048 | * the bdrv_create() call which takes different params. |
| 2049 | * Not critical right now, so fix can wait... |
| 2050 | */ |
| 2051 | out_blk = img_open_file("target", out_filename, |
| 2052 | out_fmt, flags, true, quiet); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2053 | if (!out_blk) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2054 | ret = -1; |
| 2055 | goto out; |
| 2056 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2057 | out_bs = blk_bs(out_blk); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2058 | |
Peter Lieven | f2521c9 | 2013-11-27 11:07:06 +0100 | [diff] [blame] | 2059 | /* increase bufsectors from the default 4096 (2M) if opt_transfer_length |
| 2060 | * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB) |
| 2061 | * as maximum. */ |
| 2062 | bufsectors = MIN(32768, |
| 2063 | MAX(bufsectors, MAX(out_bs->bl.opt_transfer_length, |
| 2064 | out_bs->bl.discard_alignment)) |
| 2065 | ); |
| 2066 | |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2067 | if (skip_create) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 2068 | int64_t output_sectors = blk_nb_sectors(out_blk); |
Markus Armbruster | 43716fa | 2014-06-26 13:23:21 +0200 | [diff] [blame] | 2069 | if (output_sectors < 0) { |
Gonglei | eec5eb4 | 2015-02-25 12:22:27 +0800 | [diff] [blame] | 2070 | error_report("unable to get output image length: %s", |
Markus Armbruster | 43716fa | 2014-06-26 13:23:21 +0200 | [diff] [blame] | 2071 | strerror(-output_sectors)); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2072 | ret = -1; |
| 2073 | goto out; |
Markus Armbruster | 43716fa | 2014-06-26 13:23:21 +0200 | [diff] [blame] | 2074 | } else if (output_sectors < total_sectors) { |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2075 | error_report("output file is smaller than input file"); |
| 2076 | ret = -1; |
| 2077 | goto out; |
| 2078 | } |
| 2079 | } |
| 2080 | |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2081 | cluster_sectors = 0; |
| 2082 | ret = bdrv_get_info(out_bs, &bdi); |
| 2083 | if (ret < 0) { |
| 2084 | if (compress) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2085 | error_report("could not get block driver info"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2086 | goto out; |
| 2087 | } |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2088 | } else { |
Fam Zheng | 85f49ca | 2014-05-06 21:08:43 +0800 | [diff] [blame] | 2089 | compress = compress || bdi.needs_compressed_writes; |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2090 | cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE; |
| 2091 | } |
| 2092 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2093 | state = (ImgConvertState) { |
| 2094 | .src = blk, |
| 2095 | .src_sectors = bs_sectors, |
| 2096 | .src_num = bs_n, |
| 2097 | .total_sectors = total_sectors, |
| 2098 | .target = out_blk, |
| 2099 | .compressed = compress, |
| 2100 | .target_has_backing = (bool) out_baseimg, |
| 2101 | .min_sparse = min_sparse, |
| 2102 | .cluster_sectors = cluster_sectors, |
| 2103 | .buf_sectors = bufsectors, |
| 2104 | }; |
| 2105 | ret = convert_do_copy(&state); |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2106 | |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2107 | out: |
Peter Lieven | 13c28af | 2013-11-27 11:07:01 +0100 | [diff] [blame] | 2108 | if (!ret) { |
| 2109 | qemu_progress_print(100, 0); |
| 2110 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2111 | qemu_progress_end(); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2112 | qemu_opts_del(opts); |
| 2113 | qemu_opts_free(create_opts); |
Markus Armbruster | fbf28a4 | 2014-09-29 16:07:55 +0200 | [diff] [blame] | 2114 | qemu_opts_del(sn_opts); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2115 | blk_unref(out_blk); |
Markus Armbruster | 9ba10c9 | 2014-10-07 13:59:08 +0200 | [diff] [blame] | 2116 | g_free(bs); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2117 | if (blk) { |
| 2118 | for (bs_i = 0; bs_i < bs_n; bs_i++) { |
| 2119 | blk_unref(blk[bs_i]); |
| 2120 | } |
| 2121 | g_free(blk); |
| 2122 | } |
Markus Armbruster | d739f1c | 2014-06-26 13:23:24 +0200 | [diff] [blame] | 2123 | g_free(bs_sectors); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2124 | fail_getopt: |
| 2125 | g_free(options); |
| 2126 | |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2127 | if (ret) { |
| 2128 | return 1; |
| 2129 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2130 | return 0; |
| 2131 | } |
| 2132 | |
bellard | 57d1a2b | 2004-08-03 21:15:11 +0000 | [diff] [blame] | 2133 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2134 | static void dump_snapshots(BlockDriverState *bs) |
| 2135 | { |
| 2136 | QEMUSnapshotInfo *sn_tab, *sn; |
| 2137 | int nb_sns, i; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2138 | |
| 2139 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 2140 | if (nb_sns <= 0) |
| 2141 | return; |
| 2142 | printf("Snapshot list:\n"); |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2143 | bdrv_snapshot_dump(fprintf, stdout, NULL); |
| 2144 | printf("\n"); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2145 | for(i = 0; i < nb_sns; i++) { |
| 2146 | sn = &sn_tab[i]; |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2147 | bdrv_snapshot_dump(fprintf, stdout, sn); |
| 2148 | printf("\n"); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2149 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2150 | g_free(sn_tab); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2151 | } |
| 2152 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2153 | static void dump_json_image_info_list(ImageInfoList *list) |
| 2154 | { |
Markus Armbruster | 4399c43 | 2014-04-25 16:50:32 +0200 | [diff] [blame] | 2155 | Error *local_err = NULL; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2156 | QString *str; |
| 2157 | QmpOutputVisitor *ov = qmp_output_visitor_new(); |
| 2158 | QObject *obj; |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 2159 | visit_type_ImageInfoList(qmp_output_get_visitor(ov), NULL, &list, |
| 2160 | &local_err); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2161 | obj = qmp_output_get_qobject(ov); |
| 2162 | str = qobject_to_json_pretty(obj); |
| 2163 | assert(str != NULL); |
| 2164 | printf("%s\n", qstring_get_str(str)); |
| 2165 | qobject_decref(obj); |
| 2166 | qmp_output_visitor_cleanup(ov); |
| 2167 | QDECREF(str); |
| 2168 | } |
| 2169 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2170 | static void dump_json_image_info(ImageInfo *info) |
| 2171 | { |
Markus Armbruster | 4399c43 | 2014-04-25 16:50:32 +0200 | [diff] [blame] | 2172 | Error *local_err = NULL; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2173 | QString *str; |
| 2174 | QmpOutputVisitor *ov = qmp_output_visitor_new(); |
| 2175 | QObject *obj; |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 2176 | visit_type_ImageInfo(qmp_output_get_visitor(ov), NULL, &info, &local_err); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2177 | obj = qmp_output_get_qobject(ov); |
| 2178 | str = qobject_to_json_pretty(obj); |
| 2179 | assert(str != NULL); |
| 2180 | printf("%s\n", qstring_get_str(str)); |
| 2181 | qobject_decref(obj); |
| 2182 | qmp_output_visitor_cleanup(ov); |
| 2183 | QDECREF(str); |
| 2184 | } |
| 2185 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2186 | static void dump_human_image_info_list(ImageInfoList *list) |
| 2187 | { |
| 2188 | ImageInfoList *elem; |
| 2189 | bool delim = false; |
| 2190 | |
| 2191 | for (elem = list; elem; elem = elem->next) { |
| 2192 | if (delim) { |
| 2193 | printf("\n"); |
| 2194 | } |
| 2195 | delim = true; |
| 2196 | |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2197 | bdrv_image_info_dump(fprintf, stdout, elem->value); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2198 | } |
| 2199 | } |
| 2200 | |
| 2201 | static gboolean str_equal_func(gconstpointer a, gconstpointer b) |
| 2202 | { |
| 2203 | return strcmp(a, b) == 0; |
| 2204 | } |
| 2205 | |
| 2206 | /** |
| 2207 | * Open an image file chain and return an ImageInfoList |
| 2208 | * |
| 2209 | * @filename: topmost image filename |
| 2210 | * @fmt: topmost image format (may be NULL to autodetect) |
| 2211 | * @chain: true - enumerate entire backing file chain |
| 2212 | * false - only topmost image file |
| 2213 | * |
| 2214 | * Returns a list of ImageInfo objects or NULL if there was an error opening an |
| 2215 | * image file. If there was an error a message will have been printed to |
| 2216 | * stderr. |
| 2217 | */ |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 2218 | static ImageInfoList *collect_image_info_list(bool image_opts, |
| 2219 | const char *filename, |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2220 | const char *fmt, |
| 2221 | bool chain) |
| 2222 | { |
| 2223 | ImageInfoList *head = NULL; |
| 2224 | ImageInfoList **last = &head; |
| 2225 | GHashTable *filenames; |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2226 | Error *err = NULL; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2227 | |
| 2228 | filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL); |
| 2229 | |
| 2230 | while (filename) { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2231 | BlockBackend *blk; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2232 | BlockDriverState *bs; |
| 2233 | ImageInfo *info; |
| 2234 | ImageInfoList *elem; |
| 2235 | |
| 2236 | if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) { |
| 2237 | error_report("Backing file '%s' creates an infinite loop.", |
| 2238 | filename); |
| 2239 | goto err; |
| 2240 | } |
| 2241 | g_hash_table_insert(filenames, (gpointer)filename, NULL); |
| 2242 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 2243 | blk = img_open("image", image_opts, filename, fmt, |
| 2244 | BDRV_O_FLAGS | BDRV_O_NO_BACKING, |
| 2245 | false, false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2246 | if (!blk) { |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2247 | goto err; |
| 2248 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2249 | bs = blk_bs(blk); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2250 | |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2251 | bdrv_query_image_info(bs, &info, &err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2252 | if (err) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 2253 | error_report_err(err); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2254 | blk_unref(blk); |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2255 | goto err; |
Wenchao Xia | fb0ed45 | 2013-06-06 12:27:57 +0800 | [diff] [blame] | 2256 | } |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2257 | |
| 2258 | elem = g_new0(ImageInfoList, 1); |
| 2259 | elem->value = info; |
| 2260 | *last = elem; |
| 2261 | last = &elem->next; |
| 2262 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2263 | blk_unref(blk); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2264 | |
| 2265 | filename = fmt = NULL; |
| 2266 | if (chain) { |
| 2267 | if (info->has_full_backing_filename) { |
| 2268 | filename = info->full_backing_filename; |
| 2269 | } else if (info->has_backing_filename) { |
John Snow | 92d617a | 2015-12-14 14:55:15 -0500 | [diff] [blame] | 2270 | error_report("Could not determine absolute backing filename," |
| 2271 | " but backing filename '%s' present", |
| 2272 | info->backing_filename); |
| 2273 | goto err; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2274 | } |
| 2275 | if (info->has_backing_filename_format) { |
| 2276 | fmt = info->backing_filename_format; |
| 2277 | } |
| 2278 | } |
| 2279 | } |
| 2280 | g_hash_table_destroy(filenames); |
| 2281 | return head; |
| 2282 | |
| 2283 | err: |
| 2284 | qapi_free_ImageInfoList(head); |
| 2285 | g_hash_table_destroy(filenames); |
| 2286 | return NULL; |
| 2287 | } |
| 2288 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2289 | static int img_info(int argc, char **argv) |
| 2290 | { |
| 2291 | int c; |
| 2292 | OutputFormat output_format = OFORMAT_HUMAN; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2293 | bool chain = false; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2294 | const char *filename, *fmt, *output; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2295 | ImageInfoList *list; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2296 | Error *local_err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 2297 | bool image_opts = false; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2298 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2299 | fmt = NULL; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2300 | output = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2301 | for(;;) { |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2302 | int option_index = 0; |
| 2303 | static const struct option long_options[] = { |
| 2304 | {"help", no_argument, 0, 'h'}, |
| 2305 | {"format", required_argument, 0, 'f'}, |
| 2306 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2307 | {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2308 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 2309 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2310 | {0, 0, 0, 0} |
| 2311 | }; |
| 2312 | c = getopt_long(argc, argv, "f:h", |
| 2313 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2314 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2315 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2316 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2317 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2318 | case '?': |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2319 | case 'h': |
| 2320 | help(); |
| 2321 | break; |
| 2322 | case 'f': |
| 2323 | fmt = optarg; |
| 2324 | break; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2325 | case OPTION_OUTPUT: |
| 2326 | output = optarg; |
| 2327 | break; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2328 | case OPTION_BACKING_CHAIN: |
| 2329 | chain = true; |
| 2330 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2331 | case OPTION_OBJECT: { |
| 2332 | QemuOpts *opts; |
| 2333 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 2334 | optarg, true); |
| 2335 | if (!opts) { |
| 2336 | return 1; |
| 2337 | } |
| 2338 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 2339 | case OPTION_IMAGE_OPTS: |
| 2340 | image_opts = true; |
| 2341 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2342 | } |
| 2343 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 2344 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2345 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2346 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2347 | filename = argv[optind++]; |
| 2348 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2349 | if (output && !strcmp(output, "json")) { |
| 2350 | output_format = OFORMAT_JSON; |
| 2351 | } else if (output && !strcmp(output, "human")) { |
| 2352 | output_format = OFORMAT_HUMAN; |
| 2353 | } else if (output) { |
| 2354 | error_report("--output must be used with human or json as argument."); |
| 2355 | return 1; |
| 2356 | } |
| 2357 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2358 | if (qemu_opts_foreach(&qemu_object_opts, |
| 2359 | user_creatable_add_opts_foreach, |
| 2360 | NULL, &local_err)) { |
| 2361 | error_report_err(local_err); |
| 2362 | return 1; |
| 2363 | } |
| 2364 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 2365 | list = collect_image_info_list(image_opts, filename, fmt, chain); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2366 | if (!list) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2367 | return 1; |
| 2368 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2369 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2370 | switch (output_format) { |
| 2371 | case OFORMAT_HUMAN: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2372 | dump_human_image_info_list(list); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2373 | break; |
| 2374 | case OFORMAT_JSON: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2375 | if (chain) { |
| 2376 | dump_json_image_info_list(list); |
| 2377 | } else { |
| 2378 | dump_json_image_info(list->value); |
| 2379 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2380 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2381 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2382 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2383 | qapi_free_ImageInfoList(list); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2384 | return 0; |
| 2385 | } |
| 2386 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2387 | static void dump_map_entry(OutputFormat output_format, MapEntry *e, |
| 2388 | MapEntry *next) |
| 2389 | { |
| 2390 | switch (output_format) { |
| 2391 | case OFORMAT_HUMAN: |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2392 | if (e->data && !e->has_offset) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2393 | error_report("File contains external, encrypted or compressed clusters."); |
| 2394 | exit(1); |
| 2395 | } |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2396 | if (e->data && !e->zero) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2397 | printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n", |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2398 | e->start, e->length, |
| 2399 | e->has_offset ? e->offset : 0, |
| 2400 | e->has_filename ? e->filename : ""); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2401 | } |
| 2402 | /* This format ignores the distinction between 0, ZERO and ZERO|DATA. |
| 2403 | * Modify the flags here to allow more coalescing. |
| 2404 | */ |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2405 | if (next && (!next->data || next->zero)) { |
| 2406 | next->data = false; |
| 2407 | next->zero = true; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2408 | } |
| 2409 | break; |
| 2410 | case OFORMAT_JSON: |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2411 | printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64"," |
| 2412 | " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s", |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2413 | (e->start == 0 ? "[" : ",\n"), |
| 2414 | e->start, e->length, e->depth, |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2415 | e->zero ? "true" : "false", |
| 2416 | e->data ? "true" : "false"); |
| 2417 | if (e->has_offset) { |
Paolo Bonzini | c745bfb | 2013-09-11 18:47:52 +0200 | [diff] [blame] | 2418 | printf(", \"offset\": %"PRId64"", e->offset); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2419 | } |
| 2420 | putchar('}'); |
| 2421 | |
| 2422 | if (!next) { |
| 2423 | printf("]\n"); |
| 2424 | } |
| 2425 | break; |
| 2426 | } |
| 2427 | } |
| 2428 | |
| 2429 | static int get_block_status(BlockDriverState *bs, int64_t sector_num, |
| 2430 | int nb_sectors, MapEntry *e) |
| 2431 | { |
| 2432 | int64_t ret; |
| 2433 | int depth; |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 2434 | BlockDriverState *file; |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 2435 | bool has_offset; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2436 | |
| 2437 | /* As an optimization, we could cache the current range of unallocated |
| 2438 | * clusters in each file of the chain, and avoid querying the same |
| 2439 | * range repeatedly. |
| 2440 | */ |
| 2441 | |
| 2442 | depth = 0; |
| 2443 | for (;;) { |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 2444 | ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors, |
| 2445 | &file); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2446 | if (ret < 0) { |
| 2447 | return ret; |
| 2448 | } |
| 2449 | assert(nb_sectors); |
| 2450 | if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) { |
| 2451 | break; |
| 2452 | } |
Kevin Wolf | 760e006 | 2015-06-17 14:55:21 +0200 | [diff] [blame] | 2453 | bs = backing_bs(bs); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2454 | if (bs == NULL) { |
| 2455 | ret = 0; |
| 2456 | break; |
| 2457 | } |
| 2458 | |
| 2459 | depth++; |
| 2460 | } |
| 2461 | |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 2462 | has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID); |
| 2463 | |
| 2464 | *e = (MapEntry) { |
| 2465 | .start = sector_num * BDRV_SECTOR_SIZE, |
| 2466 | .length = nb_sectors * BDRV_SECTOR_SIZE, |
| 2467 | .data = !!(ret & BDRV_BLOCK_DATA), |
| 2468 | .zero = !!(ret & BDRV_BLOCK_ZERO), |
| 2469 | .offset = ret & BDRV_BLOCK_OFFSET_MASK, |
| 2470 | .has_offset = has_offset, |
| 2471 | .depth = depth, |
| 2472 | .has_filename = file && has_offset, |
| 2473 | .filename = file && has_offset ? file->filename : NULL, |
| 2474 | }; |
| 2475 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2476 | return 0; |
| 2477 | } |
| 2478 | |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2479 | static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next) |
| 2480 | { |
| 2481 | if (curr->length == 0) { |
| 2482 | return false; |
| 2483 | } |
| 2484 | if (curr->zero != next->zero || |
| 2485 | curr->data != next->data || |
| 2486 | curr->depth != next->depth || |
| 2487 | curr->has_filename != next->has_filename || |
| 2488 | curr->has_offset != next->has_offset) { |
| 2489 | return false; |
| 2490 | } |
| 2491 | if (curr->has_filename && strcmp(curr->filename, next->filename)) { |
| 2492 | return false; |
| 2493 | } |
| 2494 | if (curr->has_offset && curr->offset + curr->length != next->offset) { |
| 2495 | return false; |
| 2496 | } |
| 2497 | return true; |
| 2498 | } |
| 2499 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2500 | static int img_map(int argc, char **argv) |
| 2501 | { |
| 2502 | int c; |
| 2503 | OutputFormat output_format = OFORMAT_HUMAN; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2504 | BlockBackend *blk; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2505 | BlockDriverState *bs; |
| 2506 | const char *filename, *fmt, *output; |
| 2507 | int64_t length; |
| 2508 | MapEntry curr = { .length = 0 }, next; |
| 2509 | int ret = 0; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2510 | Error *local_err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 2511 | bool image_opts = false; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2512 | |
| 2513 | fmt = NULL; |
| 2514 | output = NULL; |
| 2515 | for (;;) { |
| 2516 | int option_index = 0; |
| 2517 | static const struct option long_options[] = { |
| 2518 | {"help", no_argument, 0, 'h'}, |
| 2519 | {"format", required_argument, 0, 'f'}, |
| 2520 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2521 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 2522 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2523 | {0, 0, 0, 0} |
| 2524 | }; |
| 2525 | c = getopt_long(argc, argv, "f:h", |
| 2526 | long_options, &option_index); |
| 2527 | if (c == -1) { |
| 2528 | break; |
| 2529 | } |
| 2530 | switch (c) { |
| 2531 | case '?': |
| 2532 | case 'h': |
| 2533 | help(); |
| 2534 | break; |
| 2535 | case 'f': |
| 2536 | fmt = optarg; |
| 2537 | break; |
| 2538 | case OPTION_OUTPUT: |
| 2539 | output = optarg; |
| 2540 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2541 | case OPTION_OBJECT: { |
| 2542 | QemuOpts *opts; |
| 2543 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 2544 | optarg, true); |
| 2545 | if (!opts) { |
| 2546 | return 1; |
| 2547 | } |
| 2548 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 2549 | case OPTION_IMAGE_OPTS: |
| 2550 | image_opts = true; |
| 2551 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2552 | } |
| 2553 | } |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2554 | if (optind != argc - 1) { |
| 2555 | error_exit("Expecting one image file name"); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2556 | } |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2557 | filename = argv[optind]; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2558 | |
| 2559 | if (output && !strcmp(output, "json")) { |
| 2560 | output_format = OFORMAT_JSON; |
| 2561 | } else if (output && !strcmp(output, "human")) { |
| 2562 | output_format = OFORMAT_HUMAN; |
| 2563 | } else if (output) { |
| 2564 | error_report("--output must be used with human or json as argument."); |
| 2565 | return 1; |
| 2566 | } |
| 2567 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2568 | if (qemu_opts_foreach(&qemu_object_opts, |
| 2569 | user_creatable_add_opts_foreach, |
| 2570 | NULL, &local_err)) { |
| 2571 | error_report_err(local_err); |
| 2572 | return 1; |
| 2573 | } |
| 2574 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 2575 | blk = img_open("image", image_opts, filename, fmt, |
| 2576 | BDRV_O_FLAGS, true, false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2577 | if (!blk) { |
| 2578 | return 1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2579 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2580 | bs = blk_bs(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2581 | |
| 2582 | if (output_format == OFORMAT_HUMAN) { |
| 2583 | printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File"); |
| 2584 | } |
| 2585 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 2586 | length = blk_getlength(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2587 | while (curr.start + curr.length < length) { |
| 2588 | int64_t nsectors_left; |
| 2589 | int64_t sector_num; |
| 2590 | int n; |
| 2591 | |
| 2592 | sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS; |
| 2593 | |
| 2594 | /* Probe up to 1 GiB at a time. */ |
| 2595 | nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num; |
| 2596 | n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left); |
| 2597 | ret = get_block_status(bs, sector_num, n, &next); |
| 2598 | |
| 2599 | if (ret < 0) { |
| 2600 | error_report("Could not read file metadata: %s", strerror(-ret)); |
| 2601 | goto out; |
| 2602 | } |
| 2603 | |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2604 | if (entry_mergeable(&curr, &next)) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2605 | curr.length += next.length; |
| 2606 | continue; |
| 2607 | } |
| 2608 | |
| 2609 | if (curr.length > 0) { |
| 2610 | dump_map_entry(output_format, &curr, &next); |
| 2611 | } |
| 2612 | curr = next; |
| 2613 | } |
| 2614 | |
| 2615 | dump_map_entry(output_format, &curr, NULL); |
| 2616 | |
| 2617 | out: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2618 | blk_unref(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2619 | return ret < 0; |
| 2620 | } |
| 2621 | |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2622 | #define SNAPSHOT_LIST 1 |
| 2623 | #define SNAPSHOT_CREATE 2 |
| 2624 | #define SNAPSHOT_APPLY 3 |
| 2625 | #define SNAPSHOT_DELETE 4 |
| 2626 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2627 | static int img_snapshot(int argc, char **argv) |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2628 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2629 | BlockBackend *blk; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2630 | BlockDriverState *bs; |
| 2631 | QEMUSnapshotInfo sn; |
| 2632 | char *filename, *snapshot_name = NULL; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2633 | int c, ret = 0, bdrv_oflags; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2634 | int action = 0; |
| 2635 | qemu_timeval tv; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2636 | bool quiet = false; |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 2637 | Error *err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 2638 | bool image_opts = false; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2639 | |
Kevin Wolf | 710da70 | 2011-01-10 12:33:02 +0100 | [diff] [blame] | 2640 | bdrv_oflags = BDRV_O_FLAGS | BDRV_O_RDWR; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2641 | /* Parse commandline parameters */ |
| 2642 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2643 | static const struct option long_options[] = { |
| 2644 | {"help", no_argument, 0, 'h'}, |
| 2645 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 2646 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2647 | {0, 0, 0, 0} |
| 2648 | }; |
| 2649 | c = getopt_long(argc, argv, "la:c:d:hq", |
| 2650 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2651 | if (c == -1) { |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2652 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2653 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2654 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2655 | case '?': |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2656 | case 'h': |
| 2657 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2658 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2659 | case 'l': |
| 2660 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2661 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2662 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2663 | } |
| 2664 | action = SNAPSHOT_LIST; |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 2665 | bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */ |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2666 | break; |
| 2667 | case 'a': |
| 2668 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2669 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2670 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2671 | } |
| 2672 | action = SNAPSHOT_APPLY; |
| 2673 | snapshot_name = optarg; |
| 2674 | break; |
| 2675 | case 'c': |
| 2676 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2677 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2678 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2679 | } |
| 2680 | action = SNAPSHOT_CREATE; |
| 2681 | snapshot_name = optarg; |
| 2682 | break; |
| 2683 | case 'd': |
| 2684 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2685 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2686 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2687 | } |
| 2688 | action = SNAPSHOT_DELETE; |
| 2689 | snapshot_name = optarg; |
| 2690 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2691 | case 'q': |
| 2692 | quiet = true; |
| 2693 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2694 | case OPTION_OBJECT: { |
| 2695 | QemuOpts *opts; |
| 2696 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 2697 | optarg, true); |
| 2698 | if (!opts) { |
| 2699 | return 1; |
| 2700 | } |
| 2701 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 2702 | case OPTION_IMAGE_OPTS: |
| 2703 | image_opts = true; |
| 2704 | break; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2705 | } |
| 2706 | } |
| 2707 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 2708 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2709 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2710 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2711 | filename = argv[optind++]; |
| 2712 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2713 | if (qemu_opts_foreach(&qemu_object_opts, |
| 2714 | user_creatable_add_opts_foreach, |
| 2715 | NULL, &err)) { |
| 2716 | error_report_err(err); |
| 2717 | return 1; |
| 2718 | } |
| 2719 | |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2720 | /* Open the image */ |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 2721 | blk = img_open("image", image_opts, filename, NULL, |
| 2722 | bdrv_oflags, true, quiet); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2723 | if (!blk) { |
| 2724 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2725 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2726 | bs = blk_bs(blk); |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2727 | |
| 2728 | /* Perform the requested action */ |
| 2729 | switch(action) { |
| 2730 | case SNAPSHOT_LIST: |
| 2731 | dump_snapshots(bs); |
| 2732 | break; |
| 2733 | |
| 2734 | case SNAPSHOT_CREATE: |
| 2735 | memset(&sn, 0, sizeof(sn)); |
| 2736 | pstrcpy(sn.name, sizeof(sn.name), snapshot_name); |
| 2737 | |
| 2738 | qemu_gettimeofday(&tv); |
| 2739 | sn.date_sec = tv.tv_sec; |
| 2740 | sn.date_nsec = tv.tv_usec * 1000; |
| 2741 | |
| 2742 | ret = bdrv_snapshot_create(bs, &sn); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2743 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2744 | error_report("Could not create snapshot '%s': %d (%s)", |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2745 | snapshot_name, ret, strerror(-ret)); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2746 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2747 | break; |
| 2748 | |
| 2749 | case SNAPSHOT_APPLY: |
| 2750 | ret = bdrv_snapshot_goto(bs, snapshot_name); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2751 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2752 | error_report("Could not apply snapshot '%s': %d (%s)", |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2753 | snapshot_name, ret, strerror(-ret)); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2754 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2755 | break; |
| 2756 | |
| 2757 | case SNAPSHOT_DELETE: |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 2758 | bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2759 | if (err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2760 | error_reportf_err(err, "Could not delete snapshot '%s': ", |
| 2761 | snapshot_name); |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 2762 | ret = 1; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2763 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2764 | break; |
| 2765 | } |
| 2766 | |
| 2767 | /* Cleanup */ |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2768 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2769 | if (ret) { |
| 2770 | return 1; |
| 2771 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2772 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2773 | } |
| 2774 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2775 | static int img_rebase(int argc, char **argv) |
| 2776 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2777 | BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL; |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 2778 | BlockDriverState *bs = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2779 | char *filename; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 2780 | const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg; |
| 2781 | int c, flags, src_flags, ret; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2782 | int unsafe = 0; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2783 | int progress = 0; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2784 | bool quiet = false; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 2785 | Error *local_err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 2786 | bool image_opts = false; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2787 | |
| 2788 | /* Parse commandline parameters */ |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 2789 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2790 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 2791 | src_cache = BDRV_DEFAULT_CACHE; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2792 | out_baseimg = NULL; |
| 2793 | out_basefmt = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2794 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2795 | static const struct option long_options[] = { |
| 2796 | {"help", no_argument, 0, 'h'}, |
| 2797 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 2798 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2799 | {0, 0, 0, 0} |
| 2800 | }; |
| 2801 | c = getopt_long(argc, argv, "hf:F:b:upt:T:q", |
| 2802 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2803 | if (c == -1) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2804 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2805 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2806 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2807 | case '?': |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2808 | case 'h': |
| 2809 | help(); |
| 2810 | return 0; |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 2811 | case 'f': |
| 2812 | fmt = optarg; |
| 2813 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2814 | case 'F': |
| 2815 | out_basefmt = optarg; |
| 2816 | break; |
| 2817 | case 'b': |
| 2818 | out_baseimg = optarg; |
| 2819 | break; |
| 2820 | case 'u': |
| 2821 | unsafe = 1; |
| 2822 | break; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2823 | case 'p': |
| 2824 | progress = 1; |
| 2825 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2826 | case 't': |
| 2827 | cache = optarg; |
| 2828 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 2829 | case 'T': |
| 2830 | src_cache = optarg; |
| 2831 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2832 | case 'q': |
| 2833 | quiet = true; |
| 2834 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2835 | case OPTION_OBJECT: { |
| 2836 | QemuOpts *opts; |
| 2837 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 2838 | optarg, true); |
| 2839 | if (!opts) { |
| 2840 | return 1; |
| 2841 | } |
| 2842 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 2843 | case OPTION_IMAGE_OPTS: |
| 2844 | image_opts = true; |
| 2845 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2846 | } |
| 2847 | } |
| 2848 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2849 | if (quiet) { |
| 2850 | progress = 0; |
| 2851 | } |
| 2852 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2853 | if (optind != argc - 1) { |
| 2854 | error_exit("Expecting one image file name"); |
| 2855 | } |
| 2856 | if (!unsafe && !out_baseimg) { |
| 2857 | error_exit("Must specify backing file (-b) or use unsafe mode (-u)"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2858 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2859 | filename = argv[optind++]; |
| 2860 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2861 | if (qemu_opts_foreach(&qemu_object_opts, |
| 2862 | user_creatable_add_opts_foreach, |
| 2863 | NULL, &local_err)) { |
| 2864 | error_report_err(local_err); |
| 2865 | return 1; |
| 2866 | } |
| 2867 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2868 | qemu_progress_init(progress, 2.0); |
| 2869 | qemu_progress_print(0, 100); |
| 2870 | |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2871 | flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0); |
Stefan Hajnoczi | c3993cd | 2011-08-04 12:26:51 +0100 | [diff] [blame] | 2872 | ret = bdrv_parse_cache_flags(cache, &flags); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2873 | if (ret < 0) { |
| 2874 | error_report("Invalid cache option: %s", cache); |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 2875 | goto out; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2876 | } |
| 2877 | |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 2878 | src_flags = BDRV_O_FLAGS; |
| 2879 | ret = bdrv_parse_cache_flags(src_cache, &src_flags); |
| 2880 | if (ret < 0) { |
| 2881 | error_report("Invalid source cache option: %s", src_cache); |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 2882 | goto out; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 2883 | } |
| 2884 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2885 | /* |
| 2886 | * Open the images. |
| 2887 | * |
| 2888 | * Ignore the old backing file for unsafe rebase in case we want to correct |
| 2889 | * the reference to a renamed or moved backing file. |
| 2890 | */ |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 2891 | blk = img_open("image", image_opts, filename, fmt, flags, true, quiet); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2892 | if (!blk) { |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 2893 | ret = -1; |
| 2894 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2895 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2896 | bs = blk_bs(blk); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2897 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2898 | if (out_basefmt != NULL) { |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 2899 | if (bdrv_find_format(out_basefmt) == NULL) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2900 | error_report("Invalid format name: '%s'", out_basefmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2901 | ret = -1; |
| 2902 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2903 | } |
| 2904 | } |
| 2905 | |
| 2906 | /* For safe rebasing we need to compare old and new backing file */ |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 2907 | if (!unsafe) { |
Jeff Cody | 9a29e18 | 2015-01-22 08:03:30 -0500 | [diff] [blame] | 2908 | char backing_name[PATH_MAX]; |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 2909 | QDict *options = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2910 | |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 2911 | if (bs->backing_format[0] != '\0') { |
| 2912 | options = qdict_new(); |
| 2913 | qdict_put(options, "driver", qstring_from_str(bs->backing_format)); |
| 2914 | } |
| 2915 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2916 | bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name)); |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 2917 | blk_old_backing = blk_new_open("old_backing", backing_name, NULL, |
| 2918 | options, src_flags, &local_err); |
| 2919 | if (!blk_old_backing) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2920 | error_reportf_err(local_err, |
| 2921 | "Could not open old backing file '%s': ", |
| 2922 | backing_name); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2923 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2924 | } |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 2925 | |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 2926 | if (out_baseimg[0]) { |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 2927 | if (out_basefmt) { |
| 2928 | options = qdict_new(); |
| 2929 | qdict_put(options, "driver", qstring_from_str(out_basefmt)); |
| 2930 | } else { |
| 2931 | options = NULL; |
| 2932 | } |
| 2933 | |
| 2934 | blk_new_backing = blk_new_open("new_backing", out_baseimg, NULL, |
| 2935 | options, src_flags, &local_err); |
| 2936 | if (!blk_new_backing) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2937 | error_reportf_err(local_err, |
| 2938 | "Could not open new backing file '%s': ", |
| 2939 | out_baseimg); |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 2940 | goto out; |
| 2941 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2942 | } |
| 2943 | } |
| 2944 | |
| 2945 | /* |
| 2946 | * Check each unallocated cluster in the COW file. If it is unallocated, |
| 2947 | * accesses go to the backing file. We must therefore compare this cluster |
| 2948 | * in the old and new backing file, and if they differ we need to copy it |
| 2949 | * from the old backing file into the COW file. |
| 2950 | * |
| 2951 | * If qemu-img crashes during this step, no harm is done. The content of |
| 2952 | * the image is the same as the original one at any time. |
| 2953 | */ |
| 2954 | if (!unsafe) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2955 | int64_t num_sectors; |
| 2956 | int64_t old_backing_num_sectors; |
| 2957 | int64_t new_backing_num_sectors = 0; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2958 | uint64_t sector; |
Kevin Wolf | cc60e32 | 2010-04-29 14:47:48 +0200 | [diff] [blame] | 2959 | int n; |
TeLeMan | d6771bf | 2010-02-08 16:20:00 +0800 | [diff] [blame] | 2960 | uint8_t * buf_old; |
| 2961 | uint8_t * buf_new; |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 2962 | float local_progress = 0; |
TeLeMan | d6771bf | 2010-02-08 16:20:00 +0800 | [diff] [blame] | 2963 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 2964 | buf_old = blk_blockalign(blk, IO_BUF_SIZE); |
| 2965 | buf_new = blk_blockalign(blk, IO_BUF_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2966 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 2967 | num_sectors = blk_nb_sectors(blk); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2968 | if (num_sectors < 0) { |
| 2969 | error_report("Could not get size of '%s': %s", |
| 2970 | filename, strerror(-num_sectors)); |
| 2971 | ret = -1; |
| 2972 | goto out; |
| 2973 | } |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 2974 | old_backing_num_sectors = blk_nb_sectors(blk_old_backing); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2975 | if (old_backing_num_sectors < 0) { |
Jeff Cody | 9a29e18 | 2015-01-22 08:03:30 -0500 | [diff] [blame] | 2976 | char backing_name[PATH_MAX]; |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2977 | |
| 2978 | bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name)); |
| 2979 | error_report("Could not get size of '%s': %s", |
| 2980 | backing_name, strerror(-old_backing_num_sectors)); |
| 2981 | ret = -1; |
| 2982 | goto out; |
| 2983 | } |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 2984 | if (blk_new_backing) { |
| 2985 | new_backing_num_sectors = blk_nb_sectors(blk_new_backing); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2986 | if (new_backing_num_sectors < 0) { |
| 2987 | error_report("Could not get size of '%s': %s", |
| 2988 | out_baseimg, strerror(-new_backing_num_sectors)); |
| 2989 | ret = -1; |
| 2990 | goto out; |
| 2991 | } |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 2992 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2993 | |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 2994 | if (num_sectors != 0) { |
| 2995 | local_progress = (float)100 / |
| 2996 | (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512)); |
| 2997 | } |
| 2998 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2999 | for (sector = 0; sector < num_sectors; sector += n) { |
| 3000 | |
| 3001 | /* How many sectors can we handle with the next read? */ |
| 3002 | if (sector + (IO_BUF_SIZE / 512) <= num_sectors) { |
| 3003 | n = (IO_BUF_SIZE / 512); |
| 3004 | } else { |
| 3005 | n = num_sectors - sector; |
| 3006 | } |
| 3007 | |
| 3008 | /* If the cluster is allocated, we don't need to take action */ |
Kevin Wolf | cc60e32 | 2010-04-29 14:47:48 +0200 | [diff] [blame] | 3009 | ret = bdrv_is_allocated(bs, sector, n, &n); |
Paolo Bonzini | d663640 | 2013-09-04 19:00:25 +0200 | [diff] [blame] | 3010 | if (ret < 0) { |
| 3011 | error_report("error while reading image metadata: %s", |
| 3012 | strerror(-ret)); |
| 3013 | goto out; |
| 3014 | } |
Kevin Wolf | cc60e32 | 2010-04-29 14:47:48 +0200 | [diff] [blame] | 3015 | if (ret) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3016 | continue; |
| 3017 | } |
| 3018 | |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3019 | /* |
| 3020 | * Read old and new backing file and take into consideration that |
| 3021 | * backing files may be smaller than the COW image. |
| 3022 | */ |
| 3023 | if (sector >= old_backing_num_sectors) { |
| 3024 | memset(buf_old, 0, n * BDRV_SECTOR_SIZE); |
| 3025 | } else { |
| 3026 | if (sector + n > old_backing_num_sectors) { |
| 3027 | n = old_backing_num_sectors - sector; |
| 3028 | } |
| 3029 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3030 | ret = blk_read(blk_old_backing, sector, buf_old, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3031 | if (ret < 0) { |
| 3032 | error_report("error while reading from old backing file"); |
| 3033 | goto out; |
| 3034 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3035 | } |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3036 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3037 | if (sector >= new_backing_num_sectors || !blk_new_backing) { |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3038 | memset(buf_new, 0, n * BDRV_SECTOR_SIZE); |
| 3039 | } else { |
| 3040 | if (sector + n > new_backing_num_sectors) { |
| 3041 | n = new_backing_num_sectors - sector; |
| 3042 | } |
| 3043 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3044 | ret = blk_read(blk_new_backing, sector, buf_new, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3045 | if (ret < 0) { |
| 3046 | error_report("error while reading from new backing file"); |
| 3047 | goto out; |
| 3048 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3049 | } |
| 3050 | |
| 3051 | /* If they differ, we need to write to the COW file */ |
| 3052 | uint64_t written = 0; |
| 3053 | |
| 3054 | while (written < n) { |
| 3055 | int pnum; |
| 3056 | |
| 3057 | if (compare_sectors(buf_old + written * 512, |
Kevin Wolf | 60b1bd4 | 2010-02-17 12:32:59 +0100 | [diff] [blame] | 3058 | buf_new + written * 512, n - written, &pnum)) |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3059 | { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3060 | ret = blk_write(blk, sector + written, |
| 3061 | buf_old + written * 512, pnum); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3062 | if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3063 | error_report("Error while writing to COW image: %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3064 | strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3065 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3066 | } |
| 3067 | } |
| 3068 | |
| 3069 | written += pnum; |
| 3070 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3071 | qemu_progress_print(local_progress, 100); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3072 | } |
TeLeMan | d6771bf | 2010-02-08 16:20:00 +0800 | [diff] [blame] | 3073 | |
Kevin Wolf | bb1c059 | 2011-08-08 14:09:12 +0200 | [diff] [blame] | 3074 | qemu_vfree(buf_old); |
| 3075 | qemu_vfree(buf_new); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3076 | } |
| 3077 | |
| 3078 | /* |
| 3079 | * Change the backing file. All clusters that are different from the old |
| 3080 | * backing file are overwritten in the COW file now, so the visible content |
| 3081 | * doesn't change when we switch the backing file. |
| 3082 | */ |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3083 | if (out_baseimg && *out_baseimg) { |
| 3084 | ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt); |
| 3085 | } else { |
| 3086 | ret = bdrv_change_backing_file(bs, NULL, NULL); |
| 3087 | } |
| 3088 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3089 | if (ret == -ENOSPC) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3090 | error_report("Could not change the backing file to '%s': No " |
| 3091 | "space left in the file header", out_baseimg); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3092 | } else if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3093 | error_report("Could not change the backing file to '%s': %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3094 | out_baseimg, strerror(-ret)); |
| 3095 | } |
| 3096 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3097 | qemu_progress_print(100, 0); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3098 | /* |
| 3099 | * TODO At this point it is possible to check if any clusters that are |
| 3100 | * allocated in the COW file are the same in the backing file. If so, they |
| 3101 | * could be dropped from the COW file. Don't do this before switching the |
| 3102 | * backing file, in case of a crash this would lead to corruption. |
| 3103 | */ |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3104 | out: |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3105 | qemu_progress_end(); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3106 | /* Cleanup */ |
| 3107 | if (!unsafe) { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3108 | blk_unref(blk_old_backing); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3109 | blk_unref(blk_new_backing); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3110 | } |
| 3111 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3112 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3113 | if (ret) { |
| 3114 | return 1; |
| 3115 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3116 | return 0; |
| 3117 | } |
| 3118 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3119 | static int img_resize(int argc, char **argv) |
| 3120 | { |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 3121 | Error *err = NULL; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3122 | int c, ret, relative; |
| 3123 | const char *filename, *fmt, *size; |
| 3124 | int64_t n, total_size; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3125 | bool quiet = false; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3126 | BlockBackend *blk = NULL; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3127 | QemuOpts *param; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3128 | Error *local_err = NULL; |
| 3129 | |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3130 | static QemuOptsList resize_options = { |
| 3131 | .name = "resize_options", |
| 3132 | .head = QTAILQ_HEAD_INITIALIZER(resize_options.head), |
| 3133 | .desc = { |
| 3134 | { |
| 3135 | .name = BLOCK_OPT_SIZE, |
| 3136 | .type = QEMU_OPT_SIZE, |
| 3137 | .help = "Virtual disk size" |
| 3138 | }, { |
| 3139 | /* end of list */ |
| 3140 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3141 | }, |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3142 | }; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 3143 | bool image_opts = false; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3144 | |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 3145 | /* Remove size from argv manually so that negative numbers are not treated |
| 3146 | * as options by getopt. */ |
| 3147 | if (argc < 3) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3148 | error_exit("Not enough arguments"); |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 3149 | return 1; |
| 3150 | } |
| 3151 | |
| 3152 | size = argv[--argc]; |
| 3153 | |
| 3154 | /* Parse getopt arguments */ |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3155 | fmt = NULL; |
| 3156 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3157 | static const struct option long_options[] = { |
| 3158 | {"help", no_argument, 0, 'h'}, |
| 3159 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 3160 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3161 | {0, 0, 0, 0} |
| 3162 | }; |
| 3163 | c = getopt_long(argc, argv, "f:hq", |
| 3164 | long_options, NULL); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3165 | if (c == -1) { |
| 3166 | break; |
| 3167 | } |
| 3168 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3169 | case '?': |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3170 | case 'h': |
| 3171 | help(); |
| 3172 | break; |
| 3173 | case 'f': |
| 3174 | fmt = optarg; |
| 3175 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3176 | case 'q': |
| 3177 | quiet = true; |
| 3178 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3179 | case OPTION_OBJECT: { |
| 3180 | QemuOpts *opts; |
| 3181 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 3182 | optarg, true); |
| 3183 | if (!opts) { |
| 3184 | return 1; |
| 3185 | } |
| 3186 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 3187 | case OPTION_IMAGE_OPTS: |
| 3188 | image_opts = true; |
| 3189 | break; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3190 | } |
| 3191 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 3192 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3193 | error_exit("Expecting one image file name"); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3194 | } |
| 3195 | filename = argv[optind++]; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3196 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3197 | if (qemu_opts_foreach(&qemu_object_opts, |
| 3198 | user_creatable_add_opts_foreach, |
| 3199 | NULL, &local_err)) { |
| 3200 | error_report_err(local_err); |
| 3201 | return 1; |
| 3202 | } |
| 3203 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3204 | /* Choose grow, shrink, or absolute resize mode */ |
| 3205 | switch (size[0]) { |
| 3206 | case '+': |
| 3207 | relative = 1; |
| 3208 | size++; |
| 3209 | break; |
| 3210 | case '-': |
| 3211 | relative = -1; |
| 3212 | size++; |
| 3213 | break; |
| 3214 | default: |
| 3215 | relative = 0; |
| 3216 | break; |
| 3217 | } |
| 3218 | |
| 3219 | /* Parse size */ |
Peter Crosthwaite | 87ea75d | 2014-01-01 18:49:17 -0800 | [diff] [blame] | 3220 | param = qemu_opts_create(&resize_options, NULL, 0, &error_abort); |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 3221 | qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err); |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 3222 | if (err) { |
| 3223 | error_report_err(err); |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3224 | ret = -1; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3225 | qemu_opts_del(param); |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3226 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3227 | } |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3228 | n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0); |
| 3229 | qemu_opts_del(param); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3230 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 3231 | blk = img_open("image", image_opts, filename, fmt, |
| 3232 | BDRV_O_FLAGS | BDRV_O_RDWR, true, quiet); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3233 | if (!blk) { |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3234 | ret = -1; |
| 3235 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3236 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3237 | |
| 3238 | if (relative) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3239 | total_size = blk_getlength(blk) + n * relative; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3240 | } else { |
| 3241 | total_size = n; |
| 3242 | } |
| 3243 | if (total_size <= 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3244 | error_report("New image size must be positive"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3245 | ret = -1; |
| 3246 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3247 | } |
| 3248 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3249 | ret = blk_truncate(blk, total_size); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3250 | switch (ret) { |
| 3251 | case 0: |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3252 | qprintf(quiet, "Image resized.\n"); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3253 | break; |
| 3254 | case -ENOTSUP: |
Kevin Wolf | 259b217 | 2012-03-06 12:44:45 +0100 | [diff] [blame] | 3255 | error_report("This image does not support resize"); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3256 | break; |
| 3257 | case -EACCES: |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3258 | error_report("Image is read-only"); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3259 | break; |
| 3260 | default: |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3261 | error_report("Error resizing image (%d)", -ret); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3262 | break; |
| 3263 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3264 | out: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3265 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3266 | if (ret) { |
| 3267 | return 1; |
| 3268 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3269 | return 0; |
| 3270 | } |
| 3271 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3272 | static void amend_status_cb(BlockDriverState *bs, |
Max Reitz | 8b13976 | 2015-07-27 17:51:32 +0200 | [diff] [blame] | 3273 | int64_t offset, int64_t total_work_size, |
| 3274 | void *opaque) |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3275 | { |
| 3276 | qemu_progress_print(100.f * offset / total_work_size, 0); |
| 3277 | } |
| 3278 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3279 | static int img_amend(int argc, char **argv) |
| 3280 | { |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 3281 | Error *err = NULL; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3282 | int c, ret = 0; |
| 3283 | char *options = NULL; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 3284 | QemuOptsList *create_opts = NULL; |
| 3285 | QemuOpts *opts = NULL; |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 3286 | const char *fmt = NULL, *filename, *cache; |
| 3287 | int flags; |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3288 | bool quiet = false, progress = false; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3289 | BlockBackend *blk = NULL; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3290 | BlockDriverState *bs = NULL; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3291 | Error *local_err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 3292 | bool image_opts = false; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3293 | |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 3294 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3295 | for (;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3296 | static const struct option long_options[] = { |
| 3297 | {"help", no_argument, 0, 'h'}, |
| 3298 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 3299 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3300 | {0, 0, 0, 0} |
| 3301 | }; |
| 3302 | c = getopt_long(argc, argv, "ho:f:t:pq", |
| 3303 | long_options, NULL); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3304 | if (c == -1) { |
| 3305 | break; |
| 3306 | } |
| 3307 | |
| 3308 | switch (c) { |
| 3309 | case 'h': |
| 3310 | case '?': |
| 3311 | help(); |
| 3312 | break; |
| 3313 | case 'o': |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 3314 | if (!is_valid_option_list(optarg)) { |
| 3315 | error_report("Invalid option list: %s", optarg); |
| 3316 | ret = -1; |
Max Reitz | e814dff | 2015-08-20 16:00:38 -0700 | [diff] [blame] | 3317 | goto out_no_progress; |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 3318 | } |
| 3319 | if (!options) { |
| 3320 | options = g_strdup(optarg); |
| 3321 | } else { |
| 3322 | char *old_options = options; |
| 3323 | options = g_strdup_printf("%s,%s", options, optarg); |
| 3324 | g_free(old_options); |
| 3325 | } |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3326 | break; |
| 3327 | case 'f': |
| 3328 | fmt = optarg; |
| 3329 | break; |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 3330 | case 't': |
| 3331 | cache = optarg; |
| 3332 | break; |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3333 | case 'p': |
| 3334 | progress = true; |
| 3335 | break; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3336 | case 'q': |
| 3337 | quiet = true; |
| 3338 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3339 | case OPTION_OBJECT: |
| 3340 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 3341 | optarg, true); |
| 3342 | if (!opts) { |
| 3343 | ret = -1; |
| 3344 | goto out_no_progress; |
| 3345 | } |
| 3346 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 3347 | case OPTION_IMAGE_OPTS: |
| 3348 | image_opts = true; |
| 3349 | break; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3350 | } |
| 3351 | } |
| 3352 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3353 | if (!options) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3354 | error_exit("Must specify options (-o)"); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3355 | } |
| 3356 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3357 | if (qemu_opts_foreach(&qemu_object_opts, |
| 3358 | user_creatable_add_opts_foreach, |
| 3359 | NULL, &local_err)) { |
| 3360 | error_report_err(local_err); |
| 3361 | ret = -1; |
| 3362 | goto out_no_progress; |
| 3363 | } |
| 3364 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3365 | if (quiet) { |
| 3366 | progress = false; |
| 3367 | } |
| 3368 | qemu_progress_init(progress, 1.0); |
| 3369 | |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 3370 | filename = (optind == argc - 1) ? argv[argc - 1] : NULL; |
| 3371 | if (fmt && has_help_option(options)) { |
| 3372 | /* If a format is explicitly specified (and possibly no filename is |
| 3373 | * given), print option help here */ |
| 3374 | ret = print_block_option_help(filename, fmt); |
| 3375 | goto out; |
| 3376 | } |
| 3377 | |
| 3378 | if (optind != argc - 1) { |
Max Reitz | b2f27e4 | 2014-10-27 11:12:52 +0100 | [diff] [blame] | 3379 | error_report("Expecting one image file name"); |
| 3380 | ret = -1; |
| 3381 | goto out; |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 3382 | } |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3383 | |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 3384 | flags = BDRV_O_FLAGS | BDRV_O_RDWR; |
| 3385 | ret = bdrv_parse_cache_flags(cache, &flags); |
| 3386 | if (ret < 0) { |
| 3387 | error_report("Invalid cache option: %s", cache); |
| 3388 | goto out; |
| 3389 | } |
| 3390 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 3391 | blk = img_open("image", image_opts, filename, fmt, flags, true, quiet); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3392 | if (!blk) { |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3393 | ret = -1; |
| 3394 | goto out; |
| 3395 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3396 | bs = blk_bs(blk); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3397 | |
| 3398 | fmt = bs->drv->format_name; |
| 3399 | |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 3400 | if (has_help_option(options)) { |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 3401 | /* If the format was auto-detected, print option help here */ |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3402 | ret = print_block_option_help(filename, fmt); |
| 3403 | goto out; |
| 3404 | } |
| 3405 | |
Max Reitz | b2439d2 | 2014-12-02 18:32:47 +0100 | [diff] [blame] | 3406 | if (!bs->drv->create_opts) { |
| 3407 | error_report("Format driver '%s' does not support any options to amend", |
| 3408 | fmt); |
| 3409 | ret = -1; |
| 3410 | goto out; |
| 3411 | } |
| 3412 | |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 3413 | create_opts = qemu_opts_append(create_opts, bs->drv->create_opts); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 3414 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 3415 | if (options) { |
| 3416 | qemu_opts_do_parse(opts, options, NULL, &err); |
| 3417 | if (err) { |
Markus Armbruster | 97a2ca7 | 2015-03-14 10:23:15 +0100 | [diff] [blame] | 3418 | error_report_err(err); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 3419 | ret = -1; |
| 3420 | goto out; |
| 3421 | } |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3422 | } |
| 3423 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3424 | /* In case the driver does not call amend_status_cb() */ |
| 3425 | qemu_progress_print(0.f, 0); |
Max Reitz | 8b13976 | 2015-07-27 17:51:32 +0200 | [diff] [blame] | 3426 | ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL); |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3427 | qemu_progress_print(100.f, 0); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3428 | if (ret < 0) { |
| 3429 | error_report("Error while amending options: %s", strerror(-ret)); |
| 3430 | goto out; |
| 3431 | } |
| 3432 | |
| 3433 | out: |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3434 | qemu_progress_end(); |
| 3435 | |
Max Reitz | e814dff | 2015-08-20 16:00:38 -0700 | [diff] [blame] | 3436 | out_no_progress: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3437 | blk_unref(blk); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 3438 | qemu_opts_del(opts); |
| 3439 | qemu_opts_free(create_opts); |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 3440 | g_free(options); |
| 3441 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3442 | if (ret) { |
| 3443 | return 1; |
| 3444 | } |
| 3445 | return 0; |
| 3446 | } |
| 3447 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3448 | static const img_cmd_t img_cmds[] = { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3449 | #define DEF(option, callback, arg_string) \ |
| 3450 | { option, callback }, |
| 3451 | #include "qemu-img-cmds.h" |
| 3452 | #undef DEF |
| 3453 | #undef GEN_DOCS |
| 3454 | { NULL, NULL, }, |
| 3455 | }; |
| 3456 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 3457 | int main(int argc, char **argv) |
| 3458 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3459 | const img_cmd_t *cmd; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3460 | const char *cmdname; |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 3461 | Error *local_error = NULL; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 3462 | int c; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 3463 | static const struct option long_options[] = { |
| 3464 | {"help", no_argument, 0, 'h'}, |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 3465 | {"version", no_argument, 0, 'v'}, |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 3466 | {0, 0, 0, 0} |
| 3467 | }; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 3468 | |
MORITA Kazutaka | 526eda1 | 2013-07-23 17:30:11 +0900 | [diff] [blame] | 3469 | #ifdef CONFIG_POSIX |
| 3470 | signal(SIGPIPE, SIG_IGN); |
| 3471 | #endif |
| 3472 | |
Kevin Wolf | 53f76e5 | 2010-12-16 15:10:32 +0100 | [diff] [blame] | 3473 | error_set_progname(argv[0]); |
Fam Zheng | 10f5bff | 2014-02-10 14:48:51 +0800 | [diff] [blame] | 3474 | qemu_init_exec_dir(argv[0]); |
Kevin Wolf | 53f76e5 | 2010-12-16 15:10:32 +0100 | [diff] [blame] | 3475 | |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 3476 | if (qemu_init_main_loop(&local_error)) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 3477 | error_report_err(local_error); |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 3478 | exit(EXIT_FAILURE); |
| 3479 | } |
| 3480 | |
Daniel P. Berrange | 064097d | 2016-02-10 18:41:01 +0000 | [diff] [blame] | 3481 | module_call_init(MODULE_INIT_QOM); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 3482 | bdrv_init(); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3483 | if (argc < 2) { |
| 3484 | error_exit("Not enough arguments"); |
| 3485 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3486 | cmdname = argv[1]; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3487 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3488 | qemu_add_opts(&qemu_object_opts); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame^] | 3489 | qemu_add_opts(&qemu_source_opts); |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3490 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3491 | /* find the command */ |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 3492 | for (cmd = img_cmds; cmd->name != NULL; cmd++) { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3493 | if (!strcmp(cmdname, cmd->name)) { |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 3494 | return cmd->handler(argc - 1, argv + 1); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3495 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 3496 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3497 | |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 3498 | c = getopt_long(argc, argv, "h", long_options, NULL); |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 3499 | |
| 3500 | if (c == 'h') { |
| 3501 | help(); |
| 3502 | } |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 3503 | if (c == 'v') { |
| 3504 | printf(QEMU_IMG_VERSION); |
| 3505 | return 0; |
| 3506 | } |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 3507 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3508 | /* not found */ |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3509 | error_exit("Command not found: %s", cmdname); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 3510 | } |