]>
Commit | Line | Data |
---|---|---|
e3aff4f6 AL |
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 | */ | |
c32d766a | 10 | #include <sys/time.h> |
e3aff4f6 AL |
11 | #include <sys/types.h> |
12 | #include <stdarg.h> | |
13 | #include <stdio.h> | |
14 | #include <getopt.h> | |
c32d766a | 15 | #include <libgen.h> |
e3aff4f6 | 16 | |
3d21994f | 17 | #include "qemu-io.h" |
1de7afc9 | 18 | #include "qemu/main-loop.h" |
b543c5cd HR |
19 | #include "qemu/option.h" |
20 | #include "qemu/config-file.h" | |
0cf17e18 | 21 | #include "qemu/readline.h" |
26f54e9a | 22 | #include "sysemu/block-backend.h" |
737e150e | 23 | #include "block/block_int.h" |
d7bb72c8 | 24 | #include "trace/control.h" |
e3aff4f6 | 25 | |
43642b38 | 26 | #define CMD_NOFILE_OK 0x01 |
e3aff4f6 | 27 | |
f9883880 | 28 | static char *progname; |
e3aff4f6 | 29 | |
26f54e9a | 30 | static BlockBackend *qemuio_blk; |
191c2890 | 31 | |
d1174f13 KW |
32 | /* qemu-io commands passed using -c */ |
33 | static int ncmdline; | |
34 | static char **cmdline; | |
35 | ||
0cf17e18 SH |
36 | static ReadLineState *readline_state; |
37 | ||
4c7b7e9b | 38 | static int close_f(BlockBackend *blk, int argc, char **argv) |
e3aff4f6 | 39 | { |
26f54e9a | 40 | blk_unref(qemuio_blk); |
26f54e9a | 41 | qemuio_blk = NULL; |
43642b38 | 42 | return 0; |
e3aff4f6 AL |
43 | } |
44 | ||
45 | static const cmdinfo_t close_cmd = { | |
43642b38 DN |
46 | .name = "close", |
47 | .altname = "c", | |
48 | .cfunc = close_f, | |
49 | .oneline = "close the current open file", | |
e3aff4f6 AL |
50 | }; |
51 | ||
10d9d75c | 52 | static int openfile(char *name, int flags, QDict *opts) |
e3aff4f6 | 53 | { |
34b5d2c6 HR |
54 | Error *local_err = NULL; |
55 | ||
1b58b438 | 56 | if (qemuio_blk) { |
43642b38 | 57 | fprintf(stderr, "file open already, try 'help close'\n"); |
29f2601a | 58 | QDECREF(opts); |
43642b38 DN |
59 | return 1; |
60 | } | |
61 | ||
1b58b438 HR |
62 | qemuio_blk = blk_new_open("hda", name, NULL, opts, flags, &local_err); |
63 | if (!qemuio_blk) { | |
dbb651c4 MA |
64 | fprintf(stderr, "%s: can't open%s%s: %s\n", progname, |
65 | name ? " device " : "", name ?: "", | |
66 | error_get_pretty(local_err)); | |
67 | error_free(local_err); | |
dbb651c4 | 68 | return 1; |
43642b38 DN |
69 | } |
70 | ||
71 | return 0; | |
e3aff4f6 AL |
72 | } |
73 | ||
43642b38 | 74 | static void open_help(void) |
e3aff4f6 | 75 | { |
43642b38 | 76 | printf( |
e3aff4f6 AL |
77 | "\n" |
78 | " opens a new file in the requested mode\n" | |
79 | "\n" | |
80 | " Example:\n" | |
81 | " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n" | |
82 | "\n" | |
83 | " Opens a file for subsequent use by all of the other qemu-io commands.\n" | |
e3aff4f6 AL |
84 | " -r, -- open file read-only\n" |
85 | " -s, -- use snapshot file\n" | |
86 | " -n, -- disable host cache\n" | |
b543c5cd | 87 | " -o, -- options to be given to the block driver" |
e3aff4f6 AL |
88 | "\n"); |
89 | } | |
90 | ||
4c7b7e9b | 91 | static int open_f(BlockBackend *blk, int argc, char **argv); |
22a2bdcb BS |
92 | |
93 | static const cmdinfo_t open_cmd = { | |
43642b38 DN |
94 | .name = "open", |
95 | .altname = "o", | |
96 | .cfunc = open_f, | |
97 | .argmin = 1, | |
98 | .argmax = -1, | |
99 | .flags = CMD_NOFILE_OK, | |
b543c5cd | 100 | .args = "[-Crsn] [-o options] [path]", |
43642b38 DN |
101 | .oneline = "open the file specified by path", |
102 | .help = open_help, | |
22a2bdcb | 103 | }; |
e3aff4f6 | 104 | |
b543c5cd HR |
105 | static QemuOptsList empty_opts = { |
106 | .name = "drive", | |
443422fd | 107 | .merge_lists = true, |
b543c5cd HR |
108 | .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head), |
109 | .desc = { | |
110 | /* no elements => accept any params */ | |
111 | { /* end of list */ } | |
112 | }, | |
113 | }; | |
114 | ||
4c7b7e9b | 115 | static int open_f(BlockBackend *blk, int argc, char **argv) |
e3aff4f6 | 116 | { |
43642b38 DN |
117 | int flags = 0; |
118 | int readonly = 0; | |
43642b38 | 119 | int c; |
b543c5cd | 120 | QemuOpts *qopts; |
443422fd | 121 | QDict *opts; |
43642b38 | 122 | |
b543c5cd | 123 | while ((c = getopt(argc, argv, "snrgo:")) != EOF) { |
43642b38 DN |
124 | switch (c) { |
125 | case 's': | |
126 | flags |= BDRV_O_SNAPSHOT; | |
127 | break; | |
128 | case 'n': | |
129 | flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; | |
130 | break; | |
131 | case 'r': | |
132 | readonly = 1; | |
133 | break; | |
b543c5cd | 134 | case 'o': |
443422fd | 135 | if (!qemu_opts_parse(&empty_opts, optarg, 0)) { |
b543c5cd | 136 | printf("could not parse option list -- %s\n", optarg); |
443422fd | 137 | qemu_opts_reset(&empty_opts); |
b543c5cd HR |
138 | return 0; |
139 | } | |
b543c5cd | 140 | break; |
43642b38 | 141 | default: |
443422fd | 142 | qemu_opts_reset(&empty_opts); |
c2cdf5c5 | 143 | return qemuio_command_usage(&open_cmd); |
f5edb014 | 144 | } |
43642b38 DN |
145 | } |
146 | ||
147 | if (!readonly) { | |
148 | flags |= BDRV_O_RDWR; | |
149 | } | |
e3aff4f6 | 150 | |
443422fd MA |
151 | qopts = qemu_opts_find(&empty_opts, NULL); |
152 | opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL; | |
153 | qemu_opts_reset(&empty_opts); | |
154 | ||
fd0fee34 | 155 | if (optind == argc - 1) { |
10d9d75c | 156 | return openfile(argv[optind], flags, opts); |
fd0fee34 | 157 | } else if (optind == argc) { |
10d9d75c | 158 | return openfile(NULL, flags, opts); |
fd0fee34 | 159 | } else { |
29f2601a | 160 | QDECREF(opts); |
c2cdf5c5 | 161 | return qemuio_command_usage(&open_cmd); |
43642b38 | 162 | } |
e3aff4f6 AL |
163 | } |
164 | ||
4c7b7e9b | 165 | static int quit_f(BlockBackend *blk, int argc, char **argv) |
e681be7e KW |
166 | { |
167 | return 1; | |
168 | } | |
169 | ||
170 | static const cmdinfo_t quit_cmd = { | |
171 | .name = "quit", | |
172 | .altname = "q", | |
173 | .cfunc = quit_f, | |
174 | .argmin = -1, | |
175 | .argmax = -1, | |
176 | .flags = CMD_FLAG_GLOBAL, | |
177 | .oneline = "exit the program", | |
178 | }; | |
179 | ||
e3aff4f6 AL |
180 | static void usage(const char *name) |
181 | { | |
43642b38 | 182 | printf( |
be6273da | 183 | "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n" |
84844a20 | 184 | "QEMU Disk exerciser\n" |
e3aff4f6 | 185 | "\n" |
d208cc35 MK |
186 | " -c, --cmd STRING execute command with its arguments\n" |
187 | " from the given string\n" | |
be6273da | 188 | " -f, --format FMT specifies the block driver to use\n" |
e3aff4f6 AL |
189 | " -r, --read-only export read-only\n" |
190 | " -s, --snapshot use snapshot file\n" | |
191 | " -n, --nocache disable host cache\n" | |
192 | " -m, --misalign misalign allocations for O_DIRECT\n" | |
5c6c3a6c | 193 | " -k, --native-aio use kernel AIO implementation (on Linux only)\n" |
592fa070 | 194 | " -t, --cache=MODE use the given cache mode for the image\n" |
d7bb72c8 | 195 | " -T, --trace FILE enable trace events listed in the given file\n" |
e3aff4f6 AL |
196 | " -h, --help display this help and exit\n" |
197 | " -V, --version output version information and exit\n" | |
d208cc35 MK |
198 | "\n" |
199 | "See '%s -c help' for information on available commands." | |
e3aff4f6 | 200 | "\n", |
d208cc35 | 201 | name, name); |
e3aff4f6 AL |
202 | } |
203 | ||
d1174f13 KW |
204 | static char *get_prompt(void) |
205 | { | |
206 | static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ]; | |
207 | ||
208 | if (!prompt[0]) { | |
209 | snprintf(prompt, sizeof(prompt), "%s> ", progname); | |
210 | } | |
211 | ||
212 | return prompt; | |
213 | } | |
214 | ||
d5d1507b SW |
215 | static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque, |
216 | const char *fmt, ...) | |
d1174f13 | 217 | { |
0cf17e18 SH |
218 | va_list ap; |
219 | va_start(ap, fmt); | |
220 | vprintf(fmt, ap); | |
221 | va_end(ap); | |
d1174f13 | 222 | } |
0cf17e18 SH |
223 | |
224 | static void readline_flush_func(void *opaque) | |
d1174f13 | 225 | { |
0cf17e18 | 226 | fflush(stdout); |
d1174f13 KW |
227 | } |
228 | ||
0cf17e18 | 229 | static void readline_func(void *opaque, const char *str, void *readline_opaque) |
d1174f13 | 230 | { |
0cf17e18 SH |
231 | char **line = readline_opaque; |
232 | *line = g_strdup(str); | |
233 | } | |
234 | ||
4694020d SH |
235 | static void completion_match(const char *cmd, void *opaque) |
236 | { | |
237 | readline_add_completion(readline_state, cmd); | |
238 | } | |
239 | ||
0cf17e18 SH |
240 | static void readline_completion_func(void *opaque, const char *str) |
241 | { | |
4694020d SH |
242 | readline_set_completion_index(readline_state, strlen(str)); |
243 | qemuio_complete_command(str, completion_match, NULL); | |
0cf17e18 SH |
244 | } |
245 | ||
246 | static char *fetchline_readline(void) | |
247 | { | |
248 | char *line = NULL; | |
249 | ||
250 | readline_start(readline_state, get_prompt(), 0, readline_func, &line); | |
251 | while (!line) { | |
252 | int ch = getchar(); | |
253 | if (ch == EOF) { | |
254 | break; | |
d1174f13 | 255 | } |
0cf17e18 | 256 | readline_handle_byte(readline_state, ch); |
d1174f13 KW |
257 | } |
258 | return line; | |
259 | } | |
0cf17e18 SH |
260 | |
261 | #define MAXREADLINESZ 1024 | |
262 | static char *fetchline_fgets(void) | |
d1174f13 KW |
263 | { |
264 | char *p, *line = g_malloc(MAXREADLINESZ); | |
265 | ||
266 | if (!fgets(line, MAXREADLINESZ, stdin)) { | |
267 | g_free(line); | |
268 | return NULL; | |
269 | } | |
270 | ||
271 | p = line + strlen(line); | |
272 | if (p != line && p[-1] == '\n') { | |
273 | p[-1] = '\0'; | |
274 | } | |
275 | ||
276 | return line; | |
277 | } | |
0cf17e18 SH |
278 | |
279 | static char *fetchline(void) | |
280 | { | |
281 | if (readline_state) { | |
282 | return fetchline_readline(); | |
283 | } else { | |
284 | return fetchline_fgets(); | |
285 | } | |
286 | } | |
d1174f13 KW |
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++) { | |
4c7b7e9b | 302 | done = qemuio_command(qemuio_blk, cmdline[i]); |
d1174f13 KW |
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 | } | |
4c7b7e9b | 327 | done = qemuio_command(qemuio_blk, input); |
d1174f13 KW |
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 | { | |
5839e53b | 338 | cmdline = g_renew(char *, cmdline, ++ncmdline); |
d1174f13 KW |
339 | cmdline[ncmdline-1] = optarg; |
340 | } | |
341 | ||
0cf17e18 SH |
342 | static void reenable_tty_echo(void) |
343 | { | |
344 | qemu_set_tty_echo(STDIN_FILENO, true); | |
345 | } | |
346 | ||
e3aff4f6 AL |
347 | int main(int argc, char **argv) |
348 | { | |
43642b38 | 349 | int readonly = 0; |
be6273da | 350 | const char *sopt = "hVc:d:f:rsnmgkt:T:"; |
43642b38 DN |
351 | const struct option lopt[] = { |
352 | { "help", 0, NULL, 'h' }, | |
353 | { "version", 0, NULL, 'V' }, | |
354 | { "offset", 1, NULL, 'o' }, | |
355 | { "cmd", 1, NULL, 'c' }, | |
be6273da | 356 | { "format", 1, NULL, 'f' }, |
43642b38 DN |
357 | { "read-only", 0, NULL, 'r' }, |
358 | { "snapshot", 0, NULL, 's' }, | |
359 | { "nocache", 0, NULL, 'n' }, | |
360 | { "misalign", 0, NULL, 'm' }, | |
43642b38 | 361 | { "native-aio", 0, NULL, 'k' }, |
9e8f1835 | 362 | { "discard", 1, NULL, 'd' }, |
592fa070 | 363 | { "cache", 1, NULL, 't' }, |
d7bb72c8 | 364 | { "trace", 1, NULL, 'T' }, |
43642b38 DN |
365 | { NULL, 0, NULL, 0 } |
366 | }; | |
367 | int c; | |
368 | int opt_index = 0; | |
9e8f1835 | 369 | int flags = BDRV_O_UNMAP; |
2f78e491 | 370 | Error *local_error = NULL; |
1b58b438 | 371 | QDict *opts = NULL; |
43642b38 | 372 | |
526eda14 MK |
373 | #ifdef CONFIG_POSIX |
374 | signal(SIGPIPE, SIG_IGN); | |
375 | #endif | |
376 | ||
43642b38 | 377 | progname = basename(argv[0]); |
10f5bff6 | 378 | qemu_init_exec_dir(argv[0]); |
43642b38 | 379 | |
be6273da KW |
380 | bdrv_init(); |
381 | ||
43642b38 DN |
382 | while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) { |
383 | switch (c) { | |
384 | case 's': | |
385 | flags |= BDRV_O_SNAPSHOT; | |
386 | break; | |
387 | case 'n': | |
388 | flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; | |
389 | break; | |
9e8f1835 PB |
390 | case 'd': |
391 | if (bdrv_parse_discard_flags(optarg, &flags) < 0) { | |
392 | error_report("Invalid discard option: %s", optarg); | |
393 | exit(1); | |
394 | } | |
395 | break; | |
be6273da | 396 | case 'f': |
1b58b438 HR |
397 | if (!opts) { |
398 | opts = qdict_new(); | |
be6273da | 399 | } |
1b58b438 | 400 | qdict_put(opts, "driver", qstring_from_str(optarg)); |
be6273da | 401 | break; |
43642b38 DN |
402 | case 'c': |
403 | add_user_command(optarg); | |
404 | break; | |
405 | case 'r': | |
406 | readonly = 1; | |
407 | break; | |
408 | case 'm': | |
f9883880 | 409 | qemuio_misalign = true; |
43642b38 | 410 | break; |
43642b38 DN |
411 | case 'k': |
412 | flags |= BDRV_O_NATIVE_AIO; | |
413 | break; | |
592fa070 KW |
414 | case 't': |
415 | if (bdrv_parse_cache_flags(optarg, &flags) < 0) { | |
416 | error_report("Invalid cache option: %s", optarg); | |
417 | exit(1); | |
418 | } | |
419 | break; | |
d7bb72c8 | 420 | case 'T': |
5b808275 | 421 | if (!trace_init_backends(optarg, NULL)) { |
d7bb72c8 SH |
422 | exit(1); /* error message will have been printed */ |
423 | } | |
424 | break; | |
43642b38 | 425 | case 'V': |
02da386a | 426 | printf("%s version %s\n", progname, QEMU_VERSION); |
43642b38 DN |
427 | exit(0); |
428 | case 'h': | |
429 | usage(progname); | |
430 | exit(0); | |
431 | default: | |
432 | usage(progname); | |
433 | exit(1); | |
f5edb014 | 434 | } |
43642b38 DN |
435 | } |
436 | ||
437 | if ((argc - optind) > 1) { | |
438 | usage(progname); | |
439 | exit(1); | |
440 | } | |
e3aff4f6 | 441 | |
2f78e491 | 442 | if (qemu_init_main_loop(&local_error)) { |
565f65d2 | 443 | error_report_err(local_error); |
2f78e491 CN |
444 | exit(1); |
445 | } | |
a57d1143 | 446 | |
43642b38 | 447 | /* initialize commands */ |
c2cdf5c5 KW |
448 | qemuio_add_command(&quit_cmd); |
449 | qemuio_add_command(&open_cmd); | |
450 | qemuio_add_command(&close_cmd); | |
43642b38 | 451 | |
0cf17e18 SH |
452 | if (isatty(STDIN_FILENO)) { |
453 | readline_state = readline_init(readline_printf_func, | |
454 | readline_flush_func, | |
455 | NULL, | |
456 | readline_completion_func); | |
457 | qemu_set_tty_echo(STDIN_FILENO, false); | |
458 | atexit(reenable_tty_echo); | |
459 | } | |
460 | ||
43642b38 DN |
461 | /* open the device */ |
462 | if (!readonly) { | |
463 | flags |= BDRV_O_RDWR; | |
464 | } | |
465 | ||
466 | if ((argc - optind) == 1) { | |
10d9d75c | 467 | openfile(argv[optind], flags, opts); |
43642b38 DN |
468 | } |
469 | command_loop(); | |
e3aff4f6 | 470 | |
43642b38 | 471 | /* |
922453bc | 472 | * Make sure all outstanding requests complete before the program exits. |
43642b38 | 473 | */ |
922453bc | 474 | bdrv_drain_all(); |
95533d5f | 475 | |
26f54e9a | 476 | blk_unref(qemuio_blk); |
0cf17e18 | 477 | g_free(readline_state); |
43642b38 | 478 | return 0; |
e3aff4f6 | 479 | } |