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