aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Command line utility to exercise the QEMU I/O path. |
| 3 | * |
| 4 | * Copyright (C) 2009 Red Hat, Inc. |
| 5 | * Copyright (c) 2003-2005 Silicon Graphics, Inc. |
| 6 | * |
| 7 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 8 | * See the COPYING file in the top-level directory. |
| 9 | */ |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 10 | #include "qemu/osdep.h" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 11 | #include <getopt.h> |
Stefan Weil | c32d766 | 2009-08-31 22:16:16 +0200 | [diff] [blame] | 12 | #include <libgen.h> |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 13 | |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 14 | #include "qapi/error.h" |
Kevin Wolf | 3d21994 | 2013-06-05 14:19:39 +0200 | [diff] [blame] | 15 | #include "qemu-io.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 16 | #include "qemu/error-report.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 17 | #include "qemu/main-loop.h" |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 18 | #include "qemu/option.h" |
| 19 | #include "qemu/config-file.h" |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 20 | #include "qemu/readline.h" |
Denis V. Lunev | e9a8085 | 2016-06-17 17:44:11 +0300 | [diff] [blame] | 21 | #include "qemu/log.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 22 | #include "qapi/qmp/qstring.h" |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 23 | #include "qapi/qmp/qbool.h" |
Daniel P. Berrange | 9ba371b | 2016-02-17 10:10:16 +0000 | [diff] [blame] | 24 | #include "qom/object_interfaces.h" |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 25 | #include "sysemu/block-backend.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 26 | #include "block/block_int.h" |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 27 | #include "trace/control.h" |
Daniel P. Berrange | c229708 | 2016-04-06 12:12:06 +0100 | [diff] [blame] | 28 | #include "crypto/init.h" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 29 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 30 | #define CMD_NOFILE_OK 0x01 |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 31 | |
Stefan Weil | f988388 | 2014-03-05 22:23:00 +0100 | [diff] [blame] | 32 | static char *progname; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 33 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 34 | static BlockBackend *qemuio_blk; |
Kevin Wolf | 191c289 | 2010-09-16 13:18:08 +0200 | [diff] [blame] | 35 | |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 36 | /* qemu-io commands passed using -c */ |
| 37 | static int ncmdline; |
| 38 | static char **cmdline; |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 39 | static bool imageOpts; |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 40 | |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 41 | static ReadLineState *readline_state; |
| 42 | |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 43 | static int close_f(BlockBackend *blk, int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 44 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 45 | blk_unref(qemuio_blk); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 46 | qemuio_blk = NULL; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 47 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static const cmdinfo_t close_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 51 | .name = "close", |
| 52 | .altname = "c", |
| 53 | .cfunc = close_f, |
| 54 | .oneline = "close the current open file", |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 57 | static int openfile(char *name, int flags, bool writethrough, bool force_share, |
| 58 | QDict *opts) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 59 | { |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 60 | Error *local_err = NULL; |
| 61 | |
Max Reitz | 1b58b43 | 2015-02-05 13:58:20 -0500 | [diff] [blame] | 62 | if (qemuio_blk) { |
Markus Armbruster | b988468 | 2015-12-18 16:35:18 +0100 | [diff] [blame] | 63 | error_report("file open already, try 'help close'"); |
Markus Armbruster | 29f2601 | 2014-05-28 11:16:59 +0200 | [diff] [blame] | 64 | QDECREF(opts); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 65 | return 1; |
| 66 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 67 | |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 68 | if (force_share) { |
| 69 | if (!opts) { |
| 70 | opts = qdict_new(); |
| 71 | } |
| 72 | if (qdict_haskey(opts, BDRV_OPT_FORCE_SHARE) |
| 73 | && !qdict_get_bool(opts, BDRV_OPT_FORCE_SHARE)) { |
| 74 | error_report("-U conflicts with image options"); |
| 75 | QDECREF(opts); |
| 76 | return 1; |
| 77 | } |
Eric Blake | 579cf1d | 2017-05-15 14:54:39 -0500 | [diff] [blame] | 78 | qdict_put_bool(opts, BDRV_OPT_FORCE_SHARE, true); |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 79 | } |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 80 | qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err); |
Max Reitz | 1b58b43 | 2015-02-05 13:58:20 -0500 | [diff] [blame] | 81 | if (!qemuio_blk) { |
Markus Armbruster | b988468 | 2015-12-18 16:35:18 +0100 | [diff] [blame] | 82 | error_reportf_err(local_err, "can't open%s%s: ", |
| 83 | name ? " device " : "", name ?: ""); |
Markus Armbruster | dbb651c | 2014-09-08 18:50:58 +0200 | [diff] [blame] | 84 | return 1; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 85 | } |
Christoph Hellwig | 1db6947 | 2009-07-15 23:11:21 +0200 | [diff] [blame] | 86 | |
Kevin Wolf | e151fc1 | 2016-03-15 12:52:30 +0100 | [diff] [blame] | 87 | blk_set_enable_write_cache(qemuio_blk, !writethrough); |
Daniel P. Berrange | 8caf021 | 2015-05-12 17:09:21 +0100 | [diff] [blame] | 88 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 89 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 92 | static void open_help(void) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 93 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 94 | printf( |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 95 | "\n" |
| 96 | " opens a new file in the requested mode\n" |
| 97 | "\n" |
| 98 | " Example:\n" |
Eric Blake | e4e12bb | 2016-05-07 21:16:40 -0600 | [diff] [blame] | 99 | " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 100 | "\n" |
| 101 | " Opens a file for subsequent use by all of the other qemu-io commands.\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 102 | " -r, -- open file read-only\n" |
| 103 | " -s, -- use snapshot file\n" |
Eric Blake | b8d970f | 2016-05-07 21:16:41 -0600 | [diff] [blame] | 104 | " -n, -- disable host cache, short for -t none\n" |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 105 | " -U, -- force shared permissions\n" |
Eric Blake | b8d970f | 2016-05-07 21:16:41 -0600 | [diff] [blame] | 106 | " -k, -- use kernel AIO implementation (on Linux only)\n" |
| 107 | " -t, -- use the given cache mode for the image\n" |
| 108 | " -d, -- use the given discard mode for the image\n" |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 109 | " -o, -- options to be given to the block driver" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 110 | "\n"); |
| 111 | } |
| 112 | |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 113 | static int open_f(BlockBackend *blk, int argc, char **argv); |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 114 | |
| 115 | static const cmdinfo_t open_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 116 | .name = "open", |
| 117 | .altname = "o", |
| 118 | .cfunc = open_f, |
| 119 | .argmin = 1, |
| 120 | .argmax = -1, |
| 121 | .flags = CMD_NOFILE_OK, |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 122 | .args = "[-rsnkU] [-t cache] [-d discard] [-o options] [path]", |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 123 | .oneline = "open the file specified by path", |
| 124 | .help = open_help, |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 125 | }; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 126 | |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 127 | static QemuOptsList empty_opts = { |
| 128 | .name = "drive", |
Markus Armbruster | 443422f | 2014-05-28 11:16:58 +0200 | [diff] [blame] | 129 | .merge_lists = true, |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 130 | .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head), |
| 131 | .desc = { |
| 132 | /* no elements => accept any params */ |
| 133 | { /* end of list */ } |
| 134 | }, |
| 135 | }; |
| 136 | |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 137 | static int open_f(BlockBackend *blk, int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 138 | { |
Eric Blake | b8d970f | 2016-05-07 21:16:41 -0600 | [diff] [blame] | 139 | int flags = BDRV_O_UNMAP; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 140 | int readonly = 0; |
Kevin Wolf | e151fc1 | 2016-03-15 12:52:30 +0100 | [diff] [blame] | 141 | bool writethrough = true; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 142 | int c; |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 143 | QemuOpts *qopts; |
Markus Armbruster | 443422f | 2014-05-28 11:16:58 +0200 | [diff] [blame] | 144 | QDict *opts; |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 145 | bool force_share = false; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 146 | |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 147 | while ((c = getopt(argc, argv, "snro:kt:d:U")) != -1) { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 148 | switch (c) { |
| 149 | case 's': |
| 150 | flags |= BDRV_O_SNAPSHOT; |
| 151 | break; |
| 152 | case 'n': |
Kevin Wolf | e151fc1 | 2016-03-15 12:52:30 +0100 | [diff] [blame] | 153 | flags |= BDRV_O_NOCACHE; |
| 154 | writethrough = false; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 155 | break; |
| 156 | case 'r': |
| 157 | readonly = 1; |
| 158 | break; |
Eric Blake | b8d970f | 2016-05-07 21:16:41 -0600 | [diff] [blame] | 159 | case 'k': |
| 160 | flags |= BDRV_O_NATIVE_AIO; |
| 161 | break; |
| 162 | case 't': |
| 163 | if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) { |
| 164 | error_report("Invalid cache option: %s", optarg); |
| 165 | qemu_opts_reset(&empty_opts); |
| 166 | return 0; |
| 167 | } |
| 168 | break; |
| 169 | case 'd': |
| 170 | if (bdrv_parse_discard_flags(optarg, &flags) < 0) { |
| 171 | error_report("Invalid discard option: %s", optarg); |
| 172 | qemu_opts_reset(&empty_opts); |
| 173 | return 0; |
| 174 | } |
| 175 | break; |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 176 | case 'o': |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 177 | if (imageOpts) { |
| 178 | printf("--image-opts and 'open -o' are mutually exclusive\n"); |
Eric Blake | b8d970f | 2016-05-07 21:16:41 -0600 | [diff] [blame] | 179 | qemu_opts_reset(&empty_opts); |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 180 | return 0; |
| 181 | } |
Markus Armbruster | 70b9433 | 2015-02-13 12:50:26 +0100 | [diff] [blame] | 182 | if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) { |
Markus Armbruster | 443422f | 2014-05-28 11:16:58 +0200 | [diff] [blame] | 183 | qemu_opts_reset(&empty_opts); |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 184 | return 0; |
| 185 | } |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 186 | break; |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 187 | case 'U': |
| 188 | force_share = true; |
| 189 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 190 | default: |
Markus Armbruster | 443422f | 2014-05-28 11:16:58 +0200 | [diff] [blame] | 191 | qemu_opts_reset(&empty_opts); |
Kevin Wolf | c2cdf5c | 2013-06-05 14:19:36 +0200 | [diff] [blame] | 192 | return qemuio_command_usage(&open_cmd); |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 193 | } |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 194 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 195 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 196 | if (!readonly) { |
| 197 | flags |= BDRV_O_RDWR; |
| 198 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 199 | |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 200 | if (imageOpts && (optind == argc - 1)) { |
| 201 | if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) { |
| 202 | qemu_opts_reset(&empty_opts); |
| 203 | return 0; |
| 204 | } |
| 205 | optind++; |
| 206 | } |
| 207 | |
Markus Armbruster | 443422f | 2014-05-28 11:16:58 +0200 | [diff] [blame] | 208 | qopts = qemu_opts_find(&empty_opts, NULL); |
| 209 | opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL; |
| 210 | qemu_opts_reset(&empty_opts); |
| 211 | |
Max Reitz | fd0fee3 | 2013-12-20 19:28:20 +0100 | [diff] [blame] | 212 | if (optind == argc - 1) { |
Eric Blake | 64ebf55 | 2017-06-05 15:38:42 -0500 | [diff] [blame] | 213 | openfile(argv[optind], flags, writethrough, force_share, opts); |
Max Reitz | fd0fee3 | 2013-12-20 19:28:20 +0100 | [diff] [blame] | 214 | } else if (optind == argc) { |
Eric Blake | 64ebf55 | 2017-06-05 15:38:42 -0500 | [diff] [blame] | 215 | openfile(NULL, flags, writethrough, force_share, opts); |
Max Reitz | fd0fee3 | 2013-12-20 19:28:20 +0100 | [diff] [blame] | 216 | } else { |
Markus Armbruster | 29f2601 | 2014-05-28 11:16:59 +0200 | [diff] [blame] | 217 | QDECREF(opts); |
Eric Blake | 64ebf55 | 2017-06-05 15:38:42 -0500 | [diff] [blame] | 218 | qemuio_command_usage(&open_cmd); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 219 | } |
Eric Blake | 64ebf55 | 2017-06-05 15:38:42 -0500 | [diff] [blame] | 220 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 223 | static int quit_f(BlockBackend *blk, int argc, char **argv) |
Kevin Wolf | e681be7 | 2013-06-05 14:19:34 +0200 | [diff] [blame] | 224 | { |
| 225 | return 1; |
| 226 | } |
| 227 | |
| 228 | static const cmdinfo_t quit_cmd = { |
| 229 | .name = "quit", |
| 230 | .altname = "q", |
| 231 | .cfunc = quit_f, |
| 232 | .argmin = -1, |
| 233 | .argmax = -1, |
| 234 | .flags = CMD_FLAG_GLOBAL, |
| 235 | .oneline = "exit the program", |
| 236 | }; |
| 237 | |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 238 | static void usage(const char *name) |
| 239 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 240 | printf( |
Eric Blake | e4e12bb | 2016-05-07 21:16:40 -0600 | [diff] [blame] | 241 | "Usage: %s [OPTIONS]... [-c STRING]... [file]\n" |
Stefan Weil | 84844a2 | 2009-06-22 15:08:47 +0200 | [diff] [blame] | 242 | "QEMU Disk exerciser\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 243 | "\n" |
Daniel P. Berrange | 9ba371b | 2016-02-17 10:10:16 +0000 | [diff] [blame] | 244 | " --object OBJECTDEF define an object such as 'secret' for\n" |
| 245 | " passwords and/or encryption keys\n" |
Eric Blake | e4e12bb | 2016-05-07 21:16:40 -0600 | [diff] [blame] | 246 | " --image-opts treat file as option string\n" |
Maria Kustova | d208cc3 | 2014-03-18 09:59:19 +0400 | [diff] [blame] | 247 | " -c, --cmd STRING execute command with its arguments\n" |
| 248 | " from the given string\n" |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 249 | " -f, --format FMT specifies the block driver to use\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 250 | " -r, --read-only export read-only\n" |
| 251 | " -s, --snapshot use snapshot file\n" |
Eric Blake | e4e12bb | 2016-05-07 21:16:40 -0600 | [diff] [blame] | 252 | " -n, --nocache disable host cache, short for -t none\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 253 | " -m, --misalign misalign allocations for O_DIRECT\n" |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 254 | " -k, --native-aio use kernel AIO implementation (on Linux only)\n" |
Kevin Wolf | 592fa07 | 2012-04-18 12:07:39 +0200 | [diff] [blame] | 255 | " -t, --cache=MODE use the given cache mode for the image\n" |
Eric Blake | e4e12bb | 2016-05-07 21:16:40 -0600 | [diff] [blame] | 256 | " -d, --discard=MODE use the given discard mode for the image\n" |
Denis V. Lunev | e9a8085 | 2016-06-17 17:44:11 +0300 | [diff] [blame] | 257 | " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n" |
| 258 | " specify tracing options\n" |
| 259 | " see qemu-img(1) man page for full description\n" |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 260 | " -U, --force-share force shared permissions\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 261 | " -h, --help display this help and exit\n" |
| 262 | " -V, --version output version information and exit\n" |
Maria Kustova | d208cc3 | 2014-03-18 09:59:19 +0400 | [diff] [blame] | 263 | "\n" |
| 264 | "See '%s -c help' for information on available commands." |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 265 | "\n", |
Maria Kustova | d208cc3 | 2014-03-18 09:59:19 +0400 | [diff] [blame] | 266 | name, name); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 269 | static char *get_prompt(void) |
| 270 | { |
| 271 | static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ]; |
| 272 | |
| 273 | if (!prompt[0]) { |
| 274 | snprintf(prompt, sizeof(prompt), "%s> ", progname); |
| 275 | } |
| 276 | |
| 277 | return prompt; |
| 278 | } |
| 279 | |
Stefan Weil | d5d1507 | 2014-01-25 18:18:23 +0100 | [diff] [blame] | 280 | static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque, |
| 281 | const char *fmt, ...) |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 282 | { |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 283 | va_list ap; |
| 284 | va_start(ap, fmt); |
| 285 | vprintf(fmt, ap); |
| 286 | va_end(ap); |
| 287 | } |
| 288 | |
| 289 | static void readline_flush_func(void *opaque) |
| 290 | { |
| 291 | fflush(stdout); |
| 292 | } |
| 293 | |
| 294 | static void readline_func(void *opaque, const char *str, void *readline_opaque) |
| 295 | { |
| 296 | char **line = readline_opaque; |
| 297 | *line = g_strdup(str); |
| 298 | } |
| 299 | |
Stefan Hajnoczi | 4694020 | 2013-11-14 11:54:18 +0100 | [diff] [blame] | 300 | static void completion_match(const char *cmd, void *opaque) |
| 301 | { |
| 302 | readline_add_completion(readline_state, cmd); |
| 303 | } |
| 304 | |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 305 | static void readline_completion_func(void *opaque, const char *str) |
| 306 | { |
Stefan Hajnoczi | 4694020 | 2013-11-14 11:54:18 +0100 | [diff] [blame] | 307 | readline_set_completion_index(readline_state, strlen(str)); |
| 308 | qemuio_complete_command(str, completion_match, NULL); |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | static char *fetchline_readline(void) |
| 312 | { |
| 313 | char *line = NULL; |
| 314 | |
| 315 | readline_start(readline_state, get_prompt(), 0, readline_func, &line); |
| 316 | while (!line) { |
| 317 | int ch = getchar(); |
| 318 | if (ch == EOF) { |
| 319 | break; |
| 320 | } |
| 321 | readline_handle_byte(readline_state, ch); |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 322 | } |
| 323 | return line; |
| 324 | } |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 325 | |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 326 | #define MAXREADLINESZ 1024 |
| 327 | static char *fetchline_fgets(void) |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 328 | { |
| 329 | char *p, *line = g_malloc(MAXREADLINESZ); |
| 330 | |
| 331 | if (!fgets(line, MAXREADLINESZ, stdin)) { |
| 332 | g_free(line); |
| 333 | return NULL; |
| 334 | } |
| 335 | |
| 336 | p = line + strlen(line); |
| 337 | if (p != line && p[-1] == '\n') { |
| 338 | p[-1] = '\0'; |
| 339 | } |
| 340 | |
| 341 | return line; |
| 342 | } |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 343 | |
| 344 | static char *fetchline(void) |
| 345 | { |
| 346 | if (readline_state) { |
| 347 | return fetchline_readline(); |
| 348 | } else { |
| 349 | return fetchline_fgets(); |
| 350 | } |
| 351 | } |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 352 | |
| 353 | static void prep_fetchline(void *opaque) |
| 354 | { |
| 355 | int *fetchable = opaque; |
| 356 | |
| 357 | qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL); |
| 358 | *fetchable= 1; |
| 359 | } |
| 360 | |
| 361 | static void command_loop(void) |
| 362 | { |
| 363 | int i, done = 0, fetchable = 0, prompted = 0; |
| 364 | char *input; |
| 365 | |
| 366 | for (i = 0; !done && i < ncmdline; i++) { |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 367 | done = qemuio_command(qemuio_blk, cmdline[i]); |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 368 | } |
| 369 | if (cmdline) { |
| 370 | g_free(cmdline); |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | while (!done) { |
| 375 | if (!prompted) { |
| 376 | printf("%s", get_prompt()); |
| 377 | fflush(stdout); |
| 378 | qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable); |
| 379 | prompted = 1; |
| 380 | } |
| 381 | |
| 382 | main_loop_wait(false); |
| 383 | |
| 384 | if (!fetchable) { |
| 385 | continue; |
| 386 | } |
| 387 | |
| 388 | input = fetchline(); |
| 389 | if (input == NULL) { |
| 390 | break; |
| 391 | } |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 392 | done = qemuio_command(qemuio_blk, input); |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 393 | g_free(input); |
| 394 | |
| 395 | prompted = 0; |
| 396 | fetchable = 0; |
| 397 | } |
| 398 | qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL); |
| 399 | } |
| 400 | |
| 401 | static void add_user_command(char *optarg) |
| 402 | { |
Markus Armbruster | 5839e53 | 2014-08-19 10:31:08 +0200 | [diff] [blame] | 403 | cmdline = g_renew(char *, cmdline, ++ncmdline); |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 404 | cmdline[ncmdline-1] = optarg; |
| 405 | } |
| 406 | |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 407 | static void reenable_tty_echo(void) |
| 408 | { |
| 409 | qemu_set_tty_echo(STDIN_FILENO, true); |
| 410 | } |
| 411 | |
Daniel P. Berrange | 9ba371b | 2016-02-17 10:10:16 +0000 | [diff] [blame] | 412 | enum { |
| 413 | OPTION_OBJECT = 256, |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 414 | OPTION_IMAGE_OPTS = 257, |
Daniel P. Berrange | 9ba371b | 2016-02-17 10:10:16 +0000 | [diff] [blame] | 415 | }; |
| 416 | |
| 417 | static QemuOptsList qemu_object_opts = { |
| 418 | .name = "object", |
| 419 | .implied_opt_name = "qom-type", |
| 420 | .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head), |
| 421 | .desc = { |
| 422 | { } |
| 423 | }, |
| 424 | }; |
| 425 | |
| 426 | |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 427 | static QemuOptsList file_opts = { |
| 428 | .name = "file", |
| 429 | .implied_opt_name = "file", |
| 430 | .head = QTAILQ_HEAD_INITIALIZER(file_opts.head), |
| 431 | .desc = { |
| 432 | /* no elements => accept any params */ |
| 433 | { /* end of list */ } |
| 434 | }, |
| 435 | }; |
| 436 | |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 437 | int main(int argc, char **argv) |
| 438 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 439 | int readonly = 0; |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 440 | const char *sopt = "hVc:d:f:rsnmkt:T:U"; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 441 | const struct option lopt[] = { |
Daniel P. Berrange | a513416 | 2016-02-17 10:10:23 +0000 | [diff] [blame] | 442 | { "help", no_argument, NULL, 'h' }, |
| 443 | { "version", no_argument, NULL, 'V' }, |
Daniel P. Berrange | a513416 | 2016-02-17 10:10:23 +0000 | [diff] [blame] | 444 | { "cmd", required_argument, NULL, 'c' }, |
| 445 | { "format", required_argument, NULL, 'f' }, |
| 446 | { "read-only", no_argument, NULL, 'r' }, |
| 447 | { "snapshot", no_argument, NULL, 's' }, |
| 448 | { "nocache", no_argument, NULL, 'n' }, |
| 449 | { "misalign", no_argument, NULL, 'm' }, |
| 450 | { "native-aio", no_argument, NULL, 'k' }, |
| 451 | { "discard", required_argument, NULL, 'd' }, |
| 452 | { "cache", required_argument, NULL, 't' }, |
| 453 | { "trace", required_argument, NULL, 'T' }, |
| 454 | { "object", required_argument, NULL, OPTION_OBJECT }, |
| 455 | { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS }, |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 456 | { "force-share", no_argument, 0, 'U'}, |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 457 | { NULL, 0, NULL, 0 } |
| 458 | }; |
| 459 | int c; |
| 460 | int opt_index = 0; |
Paolo Bonzini | 9e8f183 | 2013-02-08 14:06:11 +0100 | [diff] [blame] | 461 | int flags = BDRV_O_UNMAP; |
Kevin Wolf | e151fc1 | 2016-03-15 12:52:30 +0100 | [diff] [blame] | 462 | bool writethrough = true; |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 463 | Error *local_error = NULL; |
Max Reitz | 1b58b43 | 2015-02-05 13:58:20 -0500 | [diff] [blame] | 464 | QDict *opts = NULL; |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 465 | const char *format = NULL; |
Denis V. Lunev | e9a8085 | 2016-06-17 17:44:11 +0300 | [diff] [blame] | 466 | char *trace_file = NULL; |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 467 | bool force_share = false; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 468 | |
MORITA Kazutaka | 526eda1 | 2013-07-23 17:30:11 +0900 | [diff] [blame] | 469 | #ifdef CONFIG_POSIX |
| 470 | signal(SIGPIPE, SIG_IGN); |
| 471 | #endif |
| 472 | |
Daniel P. Berrange | fe4db84 | 2016-10-04 14:35:52 +0100 | [diff] [blame] | 473 | module_call_init(MODULE_INIT_TRACE); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 474 | progname = basename(argv[0]); |
Fam Zheng | 10f5bff | 2014-02-10 14:48:51 +0800 | [diff] [blame] | 475 | qemu_init_exec_dir(argv[0]); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 476 | |
Eduardo Habkost | e8f2d27 | 2016-05-12 11:10:04 -0300 | [diff] [blame] | 477 | qcrypto_init(&error_fatal); |
Daniel P. Berrange | c229708 | 2016-04-06 12:12:06 +0100 | [diff] [blame] | 478 | |
Daniel P. Berrange | 064097d | 2016-02-10 18:41:01 +0000 | [diff] [blame] | 479 | module_call_init(MODULE_INIT_QOM); |
Daniel P. Berrange | 9ba371b | 2016-02-17 10:10:16 +0000 | [diff] [blame] | 480 | qemu_add_opts(&qemu_object_opts); |
Denis V. Lunev | e9a8085 | 2016-06-17 17:44:11 +0300 | [diff] [blame] | 481 | qemu_add_opts(&qemu_trace_opts); |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 482 | bdrv_init(); |
| 483 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 484 | while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) { |
| 485 | switch (c) { |
| 486 | case 's': |
| 487 | flags |= BDRV_O_SNAPSHOT; |
| 488 | break; |
| 489 | case 'n': |
Kevin Wolf | e151fc1 | 2016-03-15 12:52:30 +0100 | [diff] [blame] | 490 | flags |= BDRV_O_NOCACHE; |
| 491 | writethrough = false; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 492 | break; |
Paolo Bonzini | 9e8f183 | 2013-02-08 14:06:11 +0100 | [diff] [blame] | 493 | case 'd': |
| 494 | if (bdrv_parse_discard_flags(optarg, &flags) < 0) { |
| 495 | error_report("Invalid discard option: %s", optarg); |
| 496 | exit(1); |
| 497 | } |
| 498 | break; |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 499 | case 'f': |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 500 | format = optarg; |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 501 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 502 | case 'c': |
| 503 | add_user_command(optarg); |
| 504 | break; |
| 505 | case 'r': |
| 506 | readonly = 1; |
| 507 | break; |
| 508 | case 'm': |
Stefan Weil | f988388 | 2014-03-05 22:23:00 +0100 | [diff] [blame] | 509 | qemuio_misalign = true; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 510 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 511 | case 'k': |
| 512 | flags |= BDRV_O_NATIVE_AIO; |
| 513 | break; |
Kevin Wolf | 592fa07 | 2012-04-18 12:07:39 +0200 | [diff] [blame] | 514 | case 't': |
Kevin Wolf | e151fc1 | 2016-03-15 12:52:30 +0100 | [diff] [blame] | 515 | if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) { |
Kevin Wolf | 592fa07 | 2012-04-18 12:07:39 +0200 | [diff] [blame] | 516 | error_report("Invalid cache option: %s", optarg); |
| 517 | exit(1); |
| 518 | } |
| 519 | break; |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 520 | case 'T': |
Denis V. Lunev | e9a8085 | 2016-06-17 17:44:11 +0300 | [diff] [blame] | 521 | g_free(trace_file); |
| 522 | trace_file = trace_opt_parse(optarg); |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 523 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 524 | case 'V': |
Kevin Wolf | 02da386 | 2013-06-05 14:19:40 +0200 | [diff] [blame] | 525 | printf("%s version %s\n", progname, QEMU_VERSION); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 526 | exit(0); |
| 527 | case 'h': |
| 528 | usage(progname); |
| 529 | exit(0); |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 530 | case 'U': |
| 531 | force_share = true; |
| 532 | break; |
Daniel P. Berrange | 9ba371b | 2016-02-17 10:10:16 +0000 | [diff] [blame] | 533 | case OPTION_OBJECT: { |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 534 | QemuOpts *qopts; |
Daniel P. Berrange | 9ba371b | 2016-02-17 10:10:16 +0000 | [diff] [blame] | 535 | qopts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 536 | optarg, true); |
| 537 | if (!qopts) { |
| 538 | exit(1); |
| 539 | } |
| 540 | } break; |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 541 | case OPTION_IMAGE_OPTS: |
| 542 | imageOpts = true; |
| 543 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 544 | default: |
| 545 | usage(progname); |
| 546 | exit(1); |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 547 | } |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 548 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 549 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 550 | if ((argc - optind) > 1) { |
| 551 | usage(progname); |
| 552 | exit(1); |
| 553 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 554 | |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 555 | if (format && imageOpts) { |
| 556 | error_report("--image-opts and -f are mutually exclusive"); |
| 557 | exit(1); |
| 558 | } |
| 559 | |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 560 | if (qemu_init_main_loop(&local_error)) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 561 | error_report_err(local_error); |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 562 | exit(1); |
| 563 | } |
Zhi Yong Wu | a57d114 | 2012-02-19 22:24:59 +0800 | [diff] [blame] | 564 | |
Daniel P. Berrange | 9ba371b | 2016-02-17 10:10:16 +0000 | [diff] [blame] | 565 | if (qemu_opts_foreach(&qemu_object_opts, |
| 566 | user_creatable_add_opts_foreach, |
Markus Armbruster | 51b9b47 | 2016-04-27 16:29:09 +0200 | [diff] [blame] | 567 | NULL, NULL)) { |
Daniel P. Berrange | 9ba371b | 2016-02-17 10:10:16 +0000 | [diff] [blame] | 568 | exit(1); |
| 569 | } |
| 570 | |
Denis V. Lunev | e9a8085 | 2016-06-17 17:44:11 +0300 | [diff] [blame] | 571 | if (!trace_init_backends()) { |
| 572 | exit(1); |
| 573 | } |
| 574 | trace_init_file(trace_file); |
| 575 | qemu_set_log(LOG_TRACE); |
| 576 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 577 | /* initialize commands */ |
Kevin Wolf | c2cdf5c | 2013-06-05 14:19:36 +0200 | [diff] [blame] | 578 | qemuio_add_command(&quit_cmd); |
| 579 | qemuio_add_command(&open_cmd); |
| 580 | qemuio_add_command(&close_cmd); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 581 | |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 582 | if (isatty(STDIN_FILENO)) { |
| 583 | readline_state = readline_init(readline_printf_func, |
| 584 | readline_flush_func, |
| 585 | NULL, |
| 586 | readline_completion_func); |
| 587 | qemu_set_tty_echo(STDIN_FILENO, false); |
| 588 | atexit(reenable_tty_echo); |
| 589 | } |
| 590 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 591 | /* open the device */ |
| 592 | if (!readonly) { |
| 593 | flags |= BDRV_O_RDWR; |
| 594 | } |
| 595 | |
| 596 | if ((argc - optind) == 1) { |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 597 | if (imageOpts) { |
| 598 | QemuOpts *qopts = NULL; |
| 599 | qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false); |
| 600 | if (!qopts) { |
| 601 | exit(1); |
| 602 | } |
| 603 | opts = qemu_opts_to_qdict(qopts, NULL); |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 604 | if (openfile(NULL, flags, writethrough, force_share, opts)) { |
Nir Soffer | b7aa131 | 2017-02-01 02:31:18 +0200 | [diff] [blame] | 605 | exit(1); |
| 606 | } |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 607 | } else { |
| 608 | if (format) { |
| 609 | opts = qdict_new(); |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 610 | qdict_put_str(opts, "driver", format); |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 611 | } |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 612 | if (openfile(argv[optind], flags, writethrough, |
| 613 | force_share, opts)) { |
Nir Soffer | b7aa131 | 2017-02-01 02:31:18 +0200 | [diff] [blame] | 614 | exit(1); |
| 615 | } |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 616 | } |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 617 | } |
| 618 | command_loop(); |
| 619 | |
| 620 | /* |
Stefan Hajnoczi | 922453b | 2011-11-30 12:23:43 +0000 | [diff] [blame] | 621 | * Make sure all outstanding requests complete before the program exits. |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 622 | */ |
Stefan Hajnoczi | 922453b | 2011-11-30 12:23:43 +0000 | [diff] [blame] | 623 | bdrv_drain_all(); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 624 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 625 | blk_unref(qemuio_blk); |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 626 | g_free(readline_state); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 627 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 628 | } |