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 | |
Kevin Wolf | 3d21994 | 2013-06-05 14:19:39 +0200 | [diff] [blame] | 14 | #include "qemu-io.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 15 | #include "qemu/error-report.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 16 | #include "qemu/main-loop.h" |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 17 | #include "qemu/option.h" |
| 18 | #include "qemu/config-file.h" |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 19 | #include "qemu/readline.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 20 | #include "qapi/qmp/qstring.h" |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 21 | #include "sysemu/block-backend.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 22 | #include "block/block_int.h" |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 23 | #include "trace/control.h" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 24 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 25 | #define CMD_NOFILE_OK 0x01 |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 26 | |
Stefan Weil | f988388 | 2014-03-05 22:23:00 +0100 | [diff] [blame] | 27 | static char *progname; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 28 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 29 | static BlockBackend *qemuio_blk; |
Kevin Wolf | 191c289 | 2010-09-16 13:18:08 +0200 | [diff] [blame] | 30 | |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 31 | /* qemu-io commands passed using -c */ |
| 32 | static int ncmdline; |
| 33 | static char **cmdline; |
| 34 | |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 35 | static ReadLineState *readline_state; |
| 36 | |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 37 | static int close_f(BlockBackend *blk, int argc, char **argv) |
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 | blk_unref(qemuio_blk); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 40 | qemuio_blk = NULL; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 41 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static const cmdinfo_t close_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 45 | .name = "close", |
| 46 | .altname = "c", |
| 47 | .cfunc = close_f, |
| 48 | .oneline = "close the current open file", |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
Max Reitz | 10d9d75 | 2015-02-05 13:58:21 -0500 | [diff] [blame] | 51 | static int openfile(char *name, int flags, QDict *opts) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 52 | { |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 53 | Error *local_err = NULL; |
Daniel P. Berrange | 8caf021 | 2015-05-12 17:09:21 +0100 | [diff] [blame] | 54 | BlockDriverState *bs; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 55 | |
Max Reitz | 1b58b43 | 2015-02-05 13:58:20 -0500 | [diff] [blame] | 56 | if (qemuio_blk) { |
Markus Armbruster | b988468 | 2015-12-18 16:35:18 +0100 | [diff] [blame] | 57 | error_report("file open already, try 'help close'"); |
Markus Armbruster | 29f2601 | 2014-05-28 11:16:59 +0200 | [diff] [blame] | 58 | QDECREF(opts); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 59 | return 1; |
| 60 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 61 | |
Max Reitz | 1b58b43 | 2015-02-05 13:58:20 -0500 | [diff] [blame] | 62 | qemuio_blk = blk_new_open("hda", name, NULL, opts, flags, &local_err); |
| 63 | if (!qemuio_blk) { |
Markus Armbruster | b988468 | 2015-12-18 16:35:18 +0100 | [diff] [blame] | 64 | error_reportf_err(local_err, "can't open%s%s: ", |
| 65 | name ? " device " : "", name ?: ""); |
Markus Armbruster | dbb651c | 2014-09-08 18:50:58 +0200 | [diff] [blame] | 66 | return 1; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 67 | } |
Christoph Hellwig | 1db6947 | 2009-07-15 23:11:21 +0200 | [diff] [blame] | 68 | |
Daniel P. Berrange | 8caf021 | 2015-05-12 17:09:21 +0100 | [diff] [blame] | 69 | bs = blk_bs(qemuio_blk); |
| 70 | if (bdrv_is_encrypted(bs)) { |
| 71 | char password[256]; |
| 72 | printf("Disk image '%s' is encrypted.\n", name); |
| 73 | if (qemu_read_password(password, sizeof(password)) < 0) { |
| 74 | error_report("No password given"); |
| 75 | goto error; |
| 76 | } |
| 77 | if (bdrv_set_key(bs, password) < 0) { |
| 78 | error_report("invalid password"); |
| 79 | goto error; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 84 | return 0; |
Daniel P. Berrange | 8caf021 | 2015-05-12 17:09:21 +0100 | [diff] [blame] | 85 | |
| 86 | error: |
| 87 | blk_unref(qemuio_blk); |
| 88 | qemuio_blk = NULL; |
| 89 | return 1; |
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" |
| 99 | " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n" |
| 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" |
| 104 | " -n, -- disable host cache\n" |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 105 | " -o, -- options to be given to the block driver" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 106 | "\n"); |
| 107 | } |
| 108 | |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 109 | static int open_f(BlockBackend *blk, int argc, char **argv); |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 110 | |
| 111 | static const cmdinfo_t open_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 112 | .name = "open", |
| 113 | .altname = "o", |
| 114 | .cfunc = open_f, |
| 115 | .argmin = 1, |
| 116 | .argmax = -1, |
| 117 | .flags = CMD_NOFILE_OK, |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 118 | .args = "[-Crsn] [-o options] [path]", |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 119 | .oneline = "open the file specified by path", |
| 120 | .help = open_help, |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 121 | }; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 122 | |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 123 | static QemuOptsList empty_opts = { |
| 124 | .name = "drive", |
Markus Armbruster | 443422f | 2014-05-28 11:16:58 +0200 | [diff] [blame] | 125 | .merge_lists = true, |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 126 | .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head), |
| 127 | .desc = { |
| 128 | /* no elements => accept any params */ |
| 129 | { /* end of list */ } |
| 130 | }, |
| 131 | }; |
| 132 | |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 133 | static int open_f(BlockBackend *blk, int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 134 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 135 | int flags = 0; |
| 136 | int readonly = 0; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 137 | int c; |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 138 | QemuOpts *qopts; |
Markus Armbruster | 443422f | 2014-05-28 11:16:58 +0200 | [diff] [blame] | 139 | QDict *opts; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 140 | |
Eric Blake | b062ad8 | 2015-05-12 09:10:56 -0600 | [diff] [blame] | 141 | while ((c = getopt(argc, argv, "snrgo:")) != -1) { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 142 | switch (c) { |
| 143 | case 's': |
| 144 | flags |= BDRV_O_SNAPSHOT; |
| 145 | break; |
| 146 | case 'n': |
| 147 | flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; |
| 148 | break; |
| 149 | case 'r': |
| 150 | readonly = 1; |
| 151 | break; |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 152 | case 'o': |
Markus Armbruster | 70b9433 | 2015-02-13 12:50:26 +0100 | [diff] [blame] | 153 | if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) { |
Markus Armbruster | 443422f | 2014-05-28 11:16:58 +0200 | [diff] [blame] | 154 | qemu_opts_reset(&empty_opts); |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 155 | return 0; |
| 156 | } |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 157 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 158 | default: |
Markus Armbruster | 443422f | 2014-05-28 11:16:58 +0200 | [diff] [blame] | 159 | qemu_opts_reset(&empty_opts); |
Kevin Wolf | c2cdf5c | 2013-06-05 14:19:36 +0200 | [diff] [blame] | 160 | return qemuio_command_usage(&open_cmd); |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 161 | } |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 162 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 163 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 164 | if (!readonly) { |
| 165 | flags |= BDRV_O_RDWR; |
| 166 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 167 | |
Markus Armbruster | 443422f | 2014-05-28 11:16:58 +0200 | [diff] [blame] | 168 | qopts = qemu_opts_find(&empty_opts, NULL); |
| 169 | opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL; |
| 170 | qemu_opts_reset(&empty_opts); |
| 171 | |
Max Reitz | fd0fee3 | 2013-12-20 19:28:20 +0100 | [diff] [blame] | 172 | if (optind == argc - 1) { |
Max Reitz | 10d9d75 | 2015-02-05 13:58:21 -0500 | [diff] [blame] | 173 | return openfile(argv[optind], flags, opts); |
Max Reitz | fd0fee3 | 2013-12-20 19:28:20 +0100 | [diff] [blame] | 174 | } else if (optind == argc) { |
Max Reitz | 10d9d75 | 2015-02-05 13:58:21 -0500 | [diff] [blame] | 175 | return openfile(NULL, flags, opts); |
Max Reitz | fd0fee3 | 2013-12-20 19:28:20 +0100 | [diff] [blame] | 176 | } else { |
Markus Armbruster | 29f2601 | 2014-05-28 11:16:59 +0200 | [diff] [blame] | 177 | QDECREF(opts); |
Kevin Wolf | c2cdf5c | 2013-06-05 14:19:36 +0200 | [diff] [blame] | 178 | return qemuio_command_usage(&open_cmd); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 179 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 182 | static int quit_f(BlockBackend *blk, int argc, char **argv) |
Kevin Wolf | e681be7 | 2013-06-05 14:19:34 +0200 | [diff] [blame] | 183 | { |
| 184 | return 1; |
| 185 | } |
| 186 | |
| 187 | static const cmdinfo_t quit_cmd = { |
| 188 | .name = "quit", |
| 189 | .altname = "q", |
| 190 | .cfunc = quit_f, |
| 191 | .argmin = -1, |
| 192 | .argmax = -1, |
| 193 | .flags = CMD_FLAG_GLOBAL, |
| 194 | .oneline = "exit the program", |
| 195 | }; |
| 196 | |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 197 | static void usage(const char *name) |
| 198 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 199 | printf( |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 200 | "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n" |
Stefan Weil | 84844a2 | 2009-06-22 15:08:47 +0200 | [diff] [blame] | 201 | "QEMU Disk exerciser\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 202 | "\n" |
Maria Kustova | d208cc3 | 2014-03-18 09:59:19 +0400 | [diff] [blame] | 203 | " -c, --cmd STRING execute command with its arguments\n" |
| 204 | " from the given string\n" |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 205 | " -f, --format FMT specifies the block driver to use\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 206 | " -r, --read-only export read-only\n" |
| 207 | " -s, --snapshot use snapshot file\n" |
| 208 | " -n, --nocache disable host cache\n" |
| 209 | " -m, --misalign misalign allocations for O_DIRECT\n" |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 210 | " -k, --native-aio use kernel AIO implementation (on Linux only)\n" |
Kevin Wolf | 592fa07 | 2012-04-18 12:07:39 +0200 | [diff] [blame] | 211 | " -t, --cache=MODE use the given cache mode for the image\n" |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 212 | " -T, --trace FILE enable trace events listed in the given file\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 213 | " -h, --help display this help and exit\n" |
| 214 | " -V, --version output version information and exit\n" |
Maria Kustova | d208cc3 | 2014-03-18 09:59:19 +0400 | [diff] [blame] | 215 | "\n" |
| 216 | "See '%s -c help' for information on available commands." |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 217 | "\n", |
Maria Kustova | d208cc3 | 2014-03-18 09:59:19 +0400 | [diff] [blame] | 218 | name, name); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 221 | static char *get_prompt(void) |
| 222 | { |
| 223 | static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ]; |
| 224 | |
| 225 | if (!prompt[0]) { |
| 226 | snprintf(prompt, sizeof(prompt), "%s> ", progname); |
| 227 | } |
| 228 | |
| 229 | return prompt; |
| 230 | } |
| 231 | |
Stefan Weil | d5d1507 | 2014-01-25 18:18:23 +0100 | [diff] [blame] | 232 | static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque, |
| 233 | const char *fmt, ...) |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 234 | { |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 235 | va_list ap; |
| 236 | va_start(ap, fmt); |
| 237 | vprintf(fmt, ap); |
| 238 | va_end(ap); |
| 239 | } |
| 240 | |
| 241 | static void readline_flush_func(void *opaque) |
| 242 | { |
| 243 | fflush(stdout); |
| 244 | } |
| 245 | |
| 246 | static void readline_func(void *opaque, const char *str, void *readline_opaque) |
| 247 | { |
| 248 | char **line = readline_opaque; |
| 249 | *line = g_strdup(str); |
| 250 | } |
| 251 | |
Stefan Hajnoczi | 4694020 | 2013-11-14 11:54:18 +0100 | [diff] [blame] | 252 | static void completion_match(const char *cmd, void *opaque) |
| 253 | { |
| 254 | readline_add_completion(readline_state, cmd); |
| 255 | } |
| 256 | |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 257 | static void readline_completion_func(void *opaque, const char *str) |
| 258 | { |
Stefan Hajnoczi | 4694020 | 2013-11-14 11:54:18 +0100 | [diff] [blame] | 259 | readline_set_completion_index(readline_state, strlen(str)); |
| 260 | qemuio_complete_command(str, completion_match, NULL); |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | static char *fetchline_readline(void) |
| 264 | { |
| 265 | char *line = NULL; |
| 266 | |
| 267 | readline_start(readline_state, get_prompt(), 0, readline_func, &line); |
| 268 | while (!line) { |
| 269 | int ch = getchar(); |
| 270 | if (ch == EOF) { |
| 271 | break; |
| 272 | } |
| 273 | readline_handle_byte(readline_state, ch); |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 274 | } |
| 275 | return line; |
| 276 | } |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 277 | |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 278 | #define MAXREADLINESZ 1024 |
| 279 | static char *fetchline_fgets(void) |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 280 | { |
| 281 | char *p, *line = g_malloc(MAXREADLINESZ); |
| 282 | |
| 283 | if (!fgets(line, MAXREADLINESZ, stdin)) { |
| 284 | g_free(line); |
| 285 | return NULL; |
| 286 | } |
| 287 | |
| 288 | p = line + strlen(line); |
| 289 | if (p != line && p[-1] == '\n') { |
| 290 | p[-1] = '\0'; |
| 291 | } |
| 292 | |
| 293 | return line; |
| 294 | } |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 295 | |
| 296 | static char *fetchline(void) |
| 297 | { |
| 298 | if (readline_state) { |
| 299 | return fetchline_readline(); |
| 300 | } else { |
| 301 | return fetchline_fgets(); |
| 302 | } |
| 303 | } |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 304 | |
| 305 | static void prep_fetchline(void *opaque) |
| 306 | { |
| 307 | int *fetchable = opaque; |
| 308 | |
| 309 | qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL); |
| 310 | *fetchable= 1; |
| 311 | } |
| 312 | |
| 313 | static void command_loop(void) |
| 314 | { |
| 315 | int i, done = 0, fetchable = 0, prompted = 0; |
| 316 | char *input; |
| 317 | |
| 318 | for (i = 0; !done && i < ncmdline; i++) { |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 319 | done = qemuio_command(qemuio_blk, cmdline[i]); |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 320 | } |
| 321 | if (cmdline) { |
| 322 | g_free(cmdline); |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | while (!done) { |
| 327 | if (!prompted) { |
| 328 | printf("%s", get_prompt()); |
| 329 | fflush(stdout); |
| 330 | qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable); |
| 331 | prompted = 1; |
| 332 | } |
| 333 | |
| 334 | main_loop_wait(false); |
| 335 | |
| 336 | if (!fetchable) { |
| 337 | continue; |
| 338 | } |
| 339 | |
| 340 | input = fetchline(); |
| 341 | if (input == NULL) { |
| 342 | break; |
| 343 | } |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 344 | done = qemuio_command(qemuio_blk, input); |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 345 | g_free(input); |
| 346 | |
| 347 | prompted = 0; |
| 348 | fetchable = 0; |
| 349 | } |
| 350 | qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL); |
| 351 | } |
| 352 | |
| 353 | static void add_user_command(char *optarg) |
| 354 | { |
Markus Armbruster | 5839e53 | 2014-08-19 10:31:08 +0200 | [diff] [blame] | 355 | cmdline = g_renew(char *, cmdline, ++ncmdline); |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 356 | cmdline[ncmdline-1] = optarg; |
| 357 | } |
| 358 | |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 359 | static void reenable_tty_echo(void) |
| 360 | { |
| 361 | qemu_set_tty_echo(STDIN_FILENO, true); |
| 362 | } |
| 363 | |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 364 | int main(int argc, char **argv) |
| 365 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 366 | int readonly = 0; |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 367 | const char *sopt = "hVc:d:f:rsnmgkt:T:"; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 368 | const struct option lopt[] = { |
| 369 | { "help", 0, NULL, 'h' }, |
| 370 | { "version", 0, NULL, 'V' }, |
| 371 | { "offset", 1, NULL, 'o' }, |
| 372 | { "cmd", 1, NULL, 'c' }, |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 373 | { "format", 1, NULL, 'f' }, |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 374 | { "read-only", 0, NULL, 'r' }, |
| 375 | { "snapshot", 0, NULL, 's' }, |
| 376 | { "nocache", 0, NULL, 'n' }, |
| 377 | { "misalign", 0, NULL, 'm' }, |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 378 | { "native-aio", 0, NULL, 'k' }, |
Paolo Bonzini | 9e8f183 | 2013-02-08 14:06:11 +0100 | [diff] [blame] | 379 | { "discard", 1, NULL, 'd' }, |
Kevin Wolf | 592fa07 | 2012-04-18 12:07:39 +0200 | [diff] [blame] | 380 | { "cache", 1, NULL, 't' }, |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 381 | { "trace", 1, NULL, 'T' }, |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 382 | { NULL, 0, NULL, 0 } |
| 383 | }; |
| 384 | int c; |
| 385 | int opt_index = 0; |
Paolo Bonzini | 9e8f183 | 2013-02-08 14:06:11 +0100 | [diff] [blame] | 386 | int flags = BDRV_O_UNMAP; |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 387 | Error *local_error = NULL; |
Max Reitz | 1b58b43 | 2015-02-05 13:58:20 -0500 | [diff] [blame] | 388 | QDict *opts = NULL; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 389 | |
MORITA Kazutaka | 526eda1 | 2013-07-23 17:30:11 +0900 | [diff] [blame] | 390 | #ifdef CONFIG_POSIX |
| 391 | signal(SIGPIPE, SIG_IGN); |
| 392 | #endif |
| 393 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 394 | progname = basename(argv[0]); |
Fam Zheng | 10f5bff | 2014-02-10 14:48:51 +0800 | [diff] [blame] | 395 | qemu_init_exec_dir(argv[0]); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 396 | |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 397 | bdrv_init(); |
| 398 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 399 | while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) { |
| 400 | switch (c) { |
| 401 | case 's': |
| 402 | flags |= BDRV_O_SNAPSHOT; |
| 403 | break; |
| 404 | case 'n': |
| 405 | flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; |
| 406 | break; |
Paolo Bonzini | 9e8f183 | 2013-02-08 14:06:11 +0100 | [diff] [blame] | 407 | case 'd': |
| 408 | if (bdrv_parse_discard_flags(optarg, &flags) < 0) { |
| 409 | error_report("Invalid discard option: %s", optarg); |
| 410 | exit(1); |
| 411 | } |
| 412 | break; |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 413 | case 'f': |
Max Reitz | 1b58b43 | 2015-02-05 13:58:20 -0500 | [diff] [blame] | 414 | if (!opts) { |
| 415 | opts = qdict_new(); |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 416 | } |
Max Reitz | 1b58b43 | 2015-02-05 13:58:20 -0500 | [diff] [blame] | 417 | qdict_put(opts, "driver", qstring_from_str(optarg)); |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 418 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 419 | case 'c': |
| 420 | add_user_command(optarg); |
| 421 | break; |
| 422 | case 'r': |
| 423 | readonly = 1; |
| 424 | break; |
| 425 | case 'm': |
Stefan Weil | f988388 | 2014-03-05 22:23:00 +0100 | [diff] [blame] | 426 | qemuio_misalign = true; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 427 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 428 | case 'k': |
| 429 | flags |= BDRV_O_NATIVE_AIO; |
| 430 | break; |
Kevin Wolf | 592fa07 | 2012-04-18 12:07:39 +0200 | [diff] [blame] | 431 | case 't': |
| 432 | if (bdrv_parse_cache_flags(optarg, &flags) < 0) { |
| 433 | error_report("Invalid cache option: %s", optarg); |
| 434 | exit(1); |
| 435 | } |
| 436 | break; |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 437 | case 'T': |
Paolo Bonzini | 41fc57e | 2016-01-07 16:55:24 +0300 | [diff] [blame] | 438 | if (!trace_init_backends()) { |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 439 | exit(1); /* error message will have been printed */ |
| 440 | } |
| 441 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 442 | case 'V': |
Kevin Wolf | 02da386 | 2013-06-05 14:19:40 +0200 | [diff] [blame] | 443 | printf("%s version %s\n", progname, QEMU_VERSION); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 444 | exit(0); |
| 445 | case 'h': |
| 446 | usage(progname); |
| 447 | exit(0); |
| 448 | default: |
| 449 | usage(progname); |
| 450 | exit(1); |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 451 | } |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 452 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 453 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 454 | if ((argc - optind) > 1) { |
| 455 | usage(progname); |
| 456 | exit(1); |
| 457 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 458 | |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 459 | if (qemu_init_main_loop(&local_error)) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 460 | error_report_err(local_error); |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 461 | exit(1); |
| 462 | } |
Zhi Yong Wu | a57d114 | 2012-02-19 22:24:59 +0800 | [diff] [blame] | 463 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 464 | /* initialize commands */ |
Kevin Wolf | c2cdf5c | 2013-06-05 14:19:36 +0200 | [diff] [blame] | 465 | qemuio_add_command(&quit_cmd); |
| 466 | qemuio_add_command(&open_cmd); |
| 467 | qemuio_add_command(&close_cmd); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 468 | |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 469 | if (isatty(STDIN_FILENO)) { |
| 470 | readline_state = readline_init(readline_printf_func, |
| 471 | readline_flush_func, |
| 472 | NULL, |
| 473 | readline_completion_func); |
| 474 | qemu_set_tty_echo(STDIN_FILENO, false); |
| 475 | atexit(reenable_tty_echo); |
| 476 | } |
| 477 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 478 | /* open the device */ |
| 479 | if (!readonly) { |
| 480 | flags |= BDRV_O_RDWR; |
| 481 | } |
| 482 | |
| 483 | if ((argc - optind) == 1) { |
Max Reitz | 10d9d75 | 2015-02-05 13:58:21 -0500 | [diff] [blame] | 484 | openfile(argv[optind], flags, opts); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 485 | } |
| 486 | command_loop(); |
| 487 | |
| 488 | /* |
Stefan Hajnoczi | 922453b | 2011-11-30 12:23:43 +0000 | [diff] [blame] | 489 | * Make sure all outstanding requests complete before the program exits. |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 490 | */ |
Stefan Hajnoczi | 922453b | 2011-11-30 12:23:43 +0000 | [diff] [blame] | 491 | bdrv_drain_all(); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 492 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 493 | blk_unref(qemuio_blk); |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 494 | g_free(readline_state); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 495 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 496 | } |