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 | */ |
Stefan Weil | c32d766 | 2009-08-31 22:16:16 +0200 | [diff] [blame] | 10 | #include <sys/time.h> |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 11 | #include <sys/types.h> |
| 12 | #include <stdarg.h> |
| 13 | #include <stdio.h> |
| 14 | #include <getopt.h> |
Stefan Weil | c32d766 | 2009-08-31 22:16:16 +0200 | [diff] [blame] | 15 | #include <libgen.h> |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 16 | |
Kevin Wolf | 3d21994 | 2013-06-05 14:19:39 +0200 | [diff] [blame] | 17 | #include "qemu-io.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 18 | #include "qemu/main-loop.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 19 | #include "block/block_int.h" |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 20 | #include "trace/control.h" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 21 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 22 | #define CMD_NOFILE_OK 0x01 |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 23 | |
| 24 | char *progname; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 25 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame] | 26 | BlockDriverState *qemuio_bs; |
Kevin Wolf | 797ac58 | 2013-06-05 14:19:31 +0200 | [diff] [blame] | 27 | extern int qemuio_misalign; |
Kevin Wolf | 191c289 | 2010-09-16 13:18:08 +0200 | [diff] [blame] | 28 | |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 29 | /* qemu-io commands passed using -c */ |
| 30 | static int ncmdline; |
| 31 | static char **cmdline; |
| 32 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame] | 33 | static int close_f(BlockDriverState *bs, int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 34 | { |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 35 | bdrv_unref(bs); |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame] | 36 | qemuio_bs = NULL; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 37 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static const cmdinfo_t close_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 41 | .name = "close", |
| 42 | .altname = "c", |
| 43 | .cfunc = close_f, |
| 44 | .oneline = "close the current open file", |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
Christoph Hellwig | 9c4bab2 | 2009-07-10 13:33:47 +0200 | [diff] [blame] | 47 | static int openfile(char *name, int flags, int growable) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 48 | { |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame^] | 49 | Error *local_err = NULL; |
| 50 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame] | 51 | if (qemuio_bs) { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 52 | fprintf(stderr, "file open already, try 'help close'\n"); |
| 53 | return 1; |
| 54 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 55 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 56 | if (growable) { |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame^] | 57 | if (bdrv_file_open(&qemuio_bs, name, NULL, flags, &local_err)) { |
| 58 | fprintf(stderr, "%s: can't open device %s: %s\n", progname, name, |
| 59 | error_get_pretty(local_err)); |
| 60 | error_free(local_err); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 61 | return 1; |
| 62 | } |
| 63 | } else { |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame] | 64 | qemuio_bs = bdrv_new("hda"); |
Christoph Hellwig | 6db9560 | 2010-04-05 16:53:57 +0200 | [diff] [blame] | 65 | |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame^] | 66 | if (bdrv_open(qemuio_bs, name, NULL, flags, NULL, &local_err) < 0) { |
| 67 | fprintf(stderr, "%s: can't open device %s: %s\n", progname, name, |
| 68 | error_get_pretty(local_err)); |
| 69 | error_free(local_err); |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 70 | bdrv_unref(qemuio_bs); |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame] | 71 | qemuio_bs = NULL; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 72 | return 1; |
| 73 | } |
| 74 | } |
Christoph Hellwig | 1db6947 | 2009-07-15 23:11:21 +0200 | [diff] [blame] | 75 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 76 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 79 | static void open_help(void) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 80 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 81 | printf( |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 82 | "\n" |
| 83 | " opens a new file in the requested mode\n" |
| 84 | "\n" |
| 85 | " Example:\n" |
| 86 | " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n" |
| 87 | "\n" |
| 88 | " 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] | 89 | " -r, -- open file read-only\n" |
| 90 | " -s, -- use snapshot file\n" |
| 91 | " -n, -- disable host cache\n" |
Christoph Hellwig | 9c4bab2 | 2009-07-10 13:33:47 +0200 | [diff] [blame] | 92 | " -g, -- allow file to grow (only applies to protocols)" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 93 | "\n"); |
| 94 | } |
| 95 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame] | 96 | static int open_f(BlockDriverState *bs, int argc, char **argv); |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 97 | |
| 98 | static const cmdinfo_t open_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 99 | .name = "open", |
| 100 | .altname = "o", |
| 101 | .cfunc = open_f, |
| 102 | .argmin = 1, |
| 103 | .argmax = -1, |
| 104 | .flags = CMD_NOFILE_OK, |
| 105 | .args = "[-Crsn] [path]", |
| 106 | .oneline = "open the file specified by path", |
| 107 | .help = open_help, |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 108 | }; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 109 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame] | 110 | static int open_f(BlockDriverState *bs, int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 111 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 112 | int flags = 0; |
| 113 | int readonly = 0; |
| 114 | int growable = 0; |
| 115 | int c; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 116 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 117 | while ((c = getopt(argc, argv, "snrg")) != EOF) { |
| 118 | switch (c) { |
| 119 | case 's': |
| 120 | flags |= BDRV_O_SNAPSHOT; |
| 121 | break; |
| 122 | case 'n': |
| 123 | flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; |
| 124 | break; |
| 125 | case 'r': |
| 126 | readonly = 1; |
| 127 | break; |
| 128 | case 'g': |
| 129 | growable = 1; |
| 130 | break; |
| 131 | default: |
Kevin Wolf | c2cdf5c | 2013-06-05 14:19:36 +0200 | [diff] [blame] | 132 | return qemuio_command_usage(&open_cmd); |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 133 | } |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 134 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 135 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 136 | if (!readonly) { |
| 137 | flags |= BDRV_O_RDWR; |
| 138 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 139 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 140 | if (optind != argc - 1) { |
Kevin Wolf | c2cdf5c | 2013-06-05 14:19:36 +0200 | [diff] [blame] | 141 | return qemuio_command_usage(&open_cmd); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | return openfile(argv[optind], flags, growable); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Kevin Wolf | e681be7 | 2013-06-05 14:19:34 +0200 | [diff] [blame] | 147 | static int quit_f(BlockDriverState *bs, int argc, char **argv) |
| 148 | { |
| 149 | return 1; |
| 150 | } |
| 151 | |
| 152 | static const cmdinfo_t quit_cmd = { |
| 153 | .name = "quit", |
| 154 | .altname = "q", |
| 155 | .cfunc = quit_f, |
| 156 | .argmin = -1, |
| 157 | .argmax = -1, |
| 158 | .flags = CMD_FLAG_GLOBAL, |
| 159 | .oneline = "exit the program", |
| 160 | }; |
| 161 | |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 162 | static void usage(const char *name) |
| 163 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 164 | printf( |
Christoph Hellwig | 9a2d77a | 2010-01-20 18:13:42 +0100 | [diff] [blame] | 165 | "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n" |
Stefan Weil | 84844a2 | 2009-06-22 15:08:47 +0200 | [diff] [blame] | 166 | "QEMU Disk exerciser\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 167 | "\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 168 | " -c, --cmd command to execute\n" |
| 169 | " -r, --read-only export read-only\n" |
| 170 | " -s, --snapshot use snapshot file\n" |
| 171 | " -n, --nocache disable host cache\n" |
Christoph Hellwig | 1db6947 | 2009-07-15 23:11:21 +0200 | [diff] [blame] | 172 | " -g, --growable allow file to grow (only applies to protocols)\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 173 | " -m, --misalign misalign allocations for O_DIRECT\n" |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 174 | " -k, --native-aio use kernel AIO implementation (on Linux only)\n" |
Kevin Wolf | 592fa07 | 2012-04-18 12:07:39 +0200 | [diff] [blame] | 175 | " -t, --cache=MODE use the given cache mode for the image\n" |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 176 | " -T, --trace FILE enable trace events listed in the given file\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 177 | " -h, --help display this help and exit\n" |
| 178 | " -V, --version output version information and exit\n" |
| 179 | "\n", |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 180 | name); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 184 | #if defined(ENABLE_READLINE) |
| 185 | # include <readline/history.h> |
| 186 | # include <readline/readline.h> |
| 187 | #elif defined(ENABLE_EDITLINE) |
| 188 | # include <histedit.h> |
| 189 | #endif |
| 190 | |
| 191 | static char *get_prompt(void) |
| 192 | { |
| 193 | static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ]; |
| 194 | |
| 195 | if (!prompt[0]) { |
| 196 | snprintf(prompt, sizeof(prompt), "%s> ", progname); |
| 197 | } |
| 198 | |
| 199 | return prompt; |
| 200 | } |
| 201 | |
| 202 | #if defined(ENABLE_READLINE) |
| 203 | static char *fetchline(void) |
| 204 | { |
| 205 | char *line = readline(get_prompt()); |
| 206 | if (line && *line) { |
| 207 | add_history(line); |
| 208 | } |
| 209 | return line; |
| 210 | } |
| 211 | #elif defined(ENABLE_EDITLINE) |
| 212 | static char *el_get_prompt(EditLine *e) |
| 213 | { |
| 214 | return get_prompt(); |
| 215 | } |
| 216 | |
| 217 | static char *fetchline(void) |
| 218 | { |
| 219 | static EditLine *el; |
| 220 | static History *hist; |
| 221 | HistEvent hevent; |
| 222 | char *line; |
| 223 | int count; |
| 224 | |
| 225 | if (!el) { |
| 226 | hist = history_init(); |
| 227 | history(hist, &hevent, H_SETSIZE, 100); |
| 228 | el = el_init(progname, stdin, stdout, stderr); |
| 229 | el_source(el, NULL); |
| 230 | el_set(el, EL_SIGNAL, 1); |
| 231 | el_set(el, EL_PROMPT, el_get_prompt); |
| 232 | el_set(el, EL_HIST, history, (const char *)hist); |
| 233 | } |
| 234 | line = strdup(el_gets(el, &count)); |
| 235 | if (line) { |
| 236 | if (count > 0) { |
| 237 | line[count-1] = '\0'; |
| 238 | } |
| 239 | if (*line) { |
| 240 | history(hist, &hevent, H_ENTER, line); |
| 241 | } |
| 242 | } |
| 243 | return line; |
| 244 | } |
| 245 | #else |
| 246 | # define MAXREADLINESZ 1024 |
| 247 | static char *fetchline(void) |
| 248 | { |
| 249 | char *p, *line = g_malloc(MAXREADLINESZ); |
| 250 | |
| 251 | if (!fgets(line, MAXREADLINESZ, stdin)) { |
| 252 | g_free(line); |
| 253 | return NULL; |
| 254 | } |
| 255 | |
| 256 | p = line + strlen(line); |
| 257 | if (p != line && p[-1] == '\n') { |
| 258 | p[-1] = '\0'; |
| 259 | } |
| 260 | |
| 261 | return line; |
| 262 | } |
| 263 | #endif |
| 264 | |
| 265 | static void prep_fetchline(void *opaque) |
| 266 | { |
| 267 | int *fetchable = opaque; |
| 268 | |
| 269 | qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL); |
| 270 | *fetchable= 1; |
| 271 | } |
| 272 | |
| 273 | static void command_loop(void) |
| 274 | { |
| 275 | int i, done = 0, fetchable = 0, prompted = 0; |
| 276 | char *input; |
| 277 | |
| 278 | for (i = 0; !done && i < ncmdline; i++) { |
Kevin Wolf | 3d21994 | 2013-06-05 14:19:39 +0200 | [diff] [blame] | 279 | done = qemuio_command(qemuio_bs, cmdline[i]); |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 280 | } |
| 281 | if (cmdline) { |
| 282 | g_free(cmdline); |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | while (!done) { |
| 287 | if (!prompted) { |
| 288 | printf("%s", get_prompt()); |
| 289 | fflush(stdout); |
| 290 | qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable); |
| 291 | prompted = 1; |
| 292 | } |
| 293 | |
| 294 | main_loop_wait(false); |
| 295 | |
| 296 | if (!fetchable) { |
| 297 | continue; |
| 298 | } |
| 299 | |
| 300 | input = fetchline(); |
| 301 | if (input == NULL) { |
| 302 | break; |
| 303 | } |
Kevin Wolf | 3d21994 | 2013-06-05 14:19:39 +0200 | [diff] [blame] | 304 | done = qemuio_command(qemuio_bs, input); |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 305 | g_free(input); |
| 306 | |
| 307 | prompted = 0; |
| 308 | fetchable = 0; |
| 309 | } |
| 310 | qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL); |
| 311 | } |
| 312 | |
| 313 | static void add_user_command(char *optarg) |
| 314 | { |
| 315 | cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *)); |
| 316 | cmdline[ncmdline-1] = optarg; |
| 317 | } |
| 318 | |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 319 | int main(int argc, char **argv) |
| 320 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 321 | int readonly = 0; |
| 322 | int growable = 0; |
Paolo Bonzini | 9e8f183 | 2013-02-08 14:06:11 +0100 | [diff] [blame] | 323 | const char *sopt = "hVc:d:rsnmgkt:T:"; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 324 | const struct option lopt[] = { |
| 325 | { "help", 0, NULL, 'h' }, |
| 326 | { "version", 0, NULL, 'V' }, |
| 327 | { "offset", 1, NULL, 'o' }, |
| 328 | { "cmd", 1, NULL, 'c' }, |
| 329 | { "read-only", 0, NULL, 'r' }, |
| 330 | { "snapshot", 0, NULL, 's' }, |
| 331 | { "nocache", 0, NULL, 'n' }, |
| 332 | { "misalign", 0, NULL, 'm' }, |
| 333 | { "growable", 0, NULL, 'g' }, |
| 334 | { "native-aio", 0, NULL, 'k' }, |
Paolo Bonzini | 9e8f183 | 2013-02-08 14:06:11 +0100 | [diff] [blame] | 335 | { "discard", 1, NULL, 'd' }, |
Kevin Wolf | 592fa07 | 2012-04-18 12:07:39 +0200 | [diff] [blame] | 336 | { "cache", 1, NULL, 't' }, |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 337 | { "trace", 1, NULL, 'T' }, |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 338 | { NULL, 0, NULL, 0 } |
| 339 | }; |
| 340 | int c; |
| 341 | int opt_index = 0; |
Paolo Bonzini | 9e8f183 | 2013-02-08 14:06:11 +0100 | [diff] [blame] | 342 | int flags = BDRV_O_UNMAP; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 343 | |
MORITA Kazutaka | 526eda1 | 2013-07-23 17:30:11 +0900 | [diff] [blame] | 344 | #ifdef CONFIG_POSIX |
| 345 | signal(SIGPIPE, SIG_IGN); |
| 346 | #endif |
| 347 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 348 | progname = basename(argv[0]); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 349 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 350 | while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) { |
| 351 | switch (c) { |
| 352 | case 's': |
| 353 | flags |= BDRV_O_SNAPSHOT; |
| 354 | break; |
| 355 | case 'n': |
| 356 | flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; |
| 357 | break; |
Paolo Bonzini | 9e8f183 | 2013-02-08 14:06:11 +0100 | [diff] [blame] | 358 | case 'd': |
| 359 | if (bdrv_parse_discard_flags(optarg, &flags) < 0) { |
| 360 | error_report("Invalid discard option: %s", optarg); |
| 361 | exit(1); |
| 362 | } |
| 363 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 364 | case 'c': |
| 365 | add_user_command(optarg); |
| 366 | break; |
| 367 | case 'r': |
| 368 | readonly = 1; |
| 369 | break; |
| 370 | case 'm': |
Kevin Wolf | 797ac58 | 2013-06-05 14:19:31 +0200 | [diff] [blame] | 371 | qemuio_misalign = 1; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 372 | break; |
| 373 | case 'g': |
| 374 | growable = 1; |
| 375 | break; |
| 376 | case 'k': |
| 377 | flags |= BDRV_O_NATIVE_AIO; |
| 378 | break; |
Kevin Wolf | 592fa07 | 2012-04-18 12:07:39 +0200 | [diff] [blame] | 379 | case 't': |
| 380 | if (bdrv_parse_cache_flags(optarg, &flags) < 0) { |
| 381 | error_report("Invalid cache option: %s", optarg); |
| 382 | exit(1); |
| 383 | } |
| 384 | break; |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 385 | case 'T': |
| 386 | if (!trace_backend_init(optarg, NULL)) { |
| 387 | exit(1); /* error message will have been printed */ |
| 388 | } |
| 389 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 390 | case 'V': |
Kevin Wolf | 02da386 | 2013-06-05 14:19:40 +0200 | [diff] [blame] | 391 | printf("%s version %s\n", progname, QEMU_VERSION); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 392 | exit(0); |
| 393 | case 'h': |
| 394 | usage(progname); |
| 395 | exit(0); |
| 396 | default: |
| 397 | usage(progname); |
| 398 | exit(1); |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 399 | } |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 400 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 401 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 402 | if ((argc - optind) > 1) { |
| 403 | usage(progname); |
| 404 | exit(1); |
| 405 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 406 | |
Zhi Yong Wu | a57d114 | 2012-02-19 22:24:59 +0800 | [diff] [blame] | 407 | qemu_init_main_loop(); |
Paolo Bonzini | 2592c59 | 2012-11-03 18:10:17 +0100 | [diff] [blame] | 408 | bdrv_init(); |
Zhi Yong Wu | a57d114 | 2012-02-19 22:24:59 +0800 | [diff] [blame] | 409 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 410 | /* initialize commands */ |
Kevin Wolf | c2cdf5c | 2013-06-05 14:19:36 +0200 | [diff] [blame] | 411 | qemuio_add_command(&quit_cmd); |
| 412 | qemuio_add_command(&open_cmd); |
| 413 | qemuio_add_command(&close_cmd); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 414 | |
| 415 | /* open the device */ |
| 416 | if (!readonly) { |
| 417 | flags |= BDRV_O_RDWR; |
| 418 | } |
| 419 | |
| 420 | if ((argc - optind) == 1) { |
| 421 | openfile(argv[optind], flags, growable); |
| 422 | } |
| 423 | command_loop(); |
| 424 | |
| 425 | /* |
Stefan Hajnoczi | 922453b | 2011-11-30 12:23:43 +0000 | [diff] [blame] | 426 | * Make sure all outstanding requests complete before the program exits. |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 427 | */ |
Stefan Hajnoczi | 922453b | 2011-11-30 12:23:43 +0000 | [diff] [blame] | 428 | bdrv_drain_all(); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 429 | |
Kevin Wolf | 734c3b8 | 2013-06-05 14:19:30 +0200 | [diff] [blame] | 430 | if (qemuio_bs) { |
Fam Zheng | 4f6fd34 | 2013-08-23 09:14:47 +0800 | [diff] [blame] | 431 | bdrv_unref(qemuio_bs); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 432 | } |
| 433 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 434 | } |