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