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