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