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