2 * Command line utility to exercise the QEMU I/O path.
4 * Copyright (C) 2009 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
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.
11 #include <sys/types.h>
18 #include "qemu/main-loop.h"
19 #include "qemu/option.h"
20 #include "qemu/config-file.h"
21 #include "qemu/readline.h"
22 #include "block/block_int.h"
23 #include "trace/control.h"
25 #define CMD_NOFILE_OK 0x01
27 static char *progname;
29 static BlockDriverState *qemuio_bs;
31 /* qemu-io commands passed using -c */
33 static char **cmdline;
35 static ReadLineState *readline_state;
37 static int close_f(BlockDriverState *bs, int argc, char **argv)
44 static const cmdinfo_t close_cmd = {
48 .oneline = "close the current open file",
51 static int openfile(char *name, int flags, int growable, QDict *opts)
53 Error *local_err = NULL;
56 fprintf(stderr, "file open already, try 'help close'\n");
62 if (bdrv_open(&qemuio_bs, name, NULL, opts, flags | BDRV_O_PROTOCOL,
65 fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
66 name ? " device " : "", name ?: "",
67 error_get_pretty(local_err));
68 error_free(local_err);
72 qemuio_bs = bdrv_new("hda", &error_abort);
74 if (bdrv_open(&qemuio_bs, name, NULL, opts, flags, NULL, &local_err)
77 fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
78 name ? " device " : "", name ?: "",
79 error_get_pretty(local_err));
80 error_free(local_err);
81 bdrv_unref(qemuio_bs);
90 static void open_help(void)
94 " opens a new file in the requested mode\n"
97 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
99 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
100 " -r, -- open file read-only\n"
101 " -s, -- use snapshot file\n"
102 " -n, -- disable host cache\n"
103 " -g, -- allow file to grow (only applies to protocols)\n"
104 " -o, -- options to be given to the block driver"
108 static int open_f(BlockDriverState *bs, int argc, char **argv);
110 static const cmdinfo_t open_cmd = {
116 .flags = CMD_NOFILE_OK,
117 .args = "[-Crsn] [-o options] [path]",
118 .oneline = "open the file specified by path",
122 static QemuOptsList empty_opts = {
125 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
127 /* no elements => accept any params */
128 { /* end of list */ }
132 static int open_f(BlockDriverState *bs, int argc, char **argv)
141 while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
144 flags |= BDRV_O_SNAPSHOT;
147 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
156 if (!qemu_opts_parse(&empty_opts, optarg, 0)) {
157 printf("could not parse option list -- %s\n", optarg);
158 qemu_opts_reset(&empty_opts);
163 qemu_opts_reset(&empty_opts);
164 return qemuio_command_usage(&open_cmd);
169 flags |= BDRV_O_RDWR;
172 qopts = qemu_opts_find(&empty_opts, NULL);
173 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
174 qemu_opts_reset(&empty_opts);
176 if (optind == argc - 1) {
177 return openfile(argv[optind], flags, growable, opts);
178 } else if (optind == argc) {
179 return openfile(NULL, flags, growable, opts);
182 return qemuio_command_usage(&open_cmd);
186 static int quit_f(BlockDriverState *bs, int argc, char **argv)
191 static const cmdinfo_t quit_cmd = {
197 .flags = CMD_FLAG_GLOBAL,
198 .oneline = "exit the program",
201 static void usage(const char *name)
204 "Usage: %s [-h] [-V] [-rsnm] [-c STRING] ... [file]\n"
205 "QEMU Disk exerciser\n"
207 " -c, --cmd STRING execute command with its arguments\n"
208 " from the given string\n"
209 " -r, --read-only export read-only\n"
210 " -s, --snapshot use snapshot file\n"
211 " -n, --nocache disable host cache\n"
212 " -g, --growable allow file to grow (only applies to protocols)\n"
213 " -m, --misalign misalign allocations for O_DIRECT\n"
214 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
215 " -t, --cache=MODE use the given cache mode for the image\n"
216 " -T, --trace FILE enable trace events listed in the given file\n"
217 " -h, --help display this help and exit\n"
218 " -V, --version output version information and exit\n"
220 "See '%s -c help' for information on available commands."
225 static char *get_prompt(void)
227 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
230 snprintf(prompt, sizeof(prompt), "%s> ", progname);
236 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
237 const char *fmt, ...)
245 static void readline_flush_func(void *opaque)
250 static void readline_func(void *opaque, const char *str, void *readline_opaque)
252 char **line = readline_opaque;
253 *line = g_strdup(str);
256 static void completion_match(const char *cmd, void *opaque)
258 readline_add_completion(readline_state, cmd);
261 static void readline_completion_func(void *opaque, const char *str)
263 readline_set_completion_index(readline_state, strlen(str));
264 qemuio_complete_command(str, completion_match, NULL);
267 static char *fetchline_readline(void)
271 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
277 readline_handle_byte(readline_state, ch);
282 #define MAXREADLINESZ 1024
283 static char *fetchline_fgets(void)
285 char *p, *line = g_malloc(MAXREADLINESZ);
287 if (!fgets(line, MAXREADLINESZ, stdin)) {
292 p = line + strlen(line);
293 if (p != line && p[-1] == '\n') {
300 static char *fetchline(void)
302 if (readline_state) {
303 return fetchline_readline();
305 return fetchline_fgets();
309 static void prep_fetchline(void *opaque)
311 int *fetchable = opaque;
313 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
317 static void command_loop(void)
319 int i, done = 0, fetchable = 0, prompted = 0;
322 for (i = 0; !done && i < ncmdline; i++) {
323 done = qemuio_command(qemuio_bs, cmdline[i]);
332 printf("%s", get_prompt());
334 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
338 main_loop_wait(false);
348 done = qemuio_command(qemuio_bs, input);
354 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
357 static void add_user_command(char *optarg)
359 cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
360 cmdline[ncmdline-1] = optarg;
363 static void reenable_tty_echo(void)
365 qemu_set_tty_echo(STDIN_FILENO, true);
368 int main(int argc, char **argv)
372 const char *sopt = "hVc:d:rsnmgkt:T:";
373 const struct option lopt[] = {
374 { "help", 0, NULL, 'h' },
375 { "version", 0, NULL, 'V' },
376 { "offset", 1, NULL, 'o' },
377 { "cmd", 1, NULL, 'c' },
378 { "read-only", 0, NULL, 'r' },
379 { "snapshot", 0, NULL, 's' },
380 { "nocache", 0, NULL, 'n' },
381 { "misalign", 0, NULL, 'm' },
382 { "growable", 0, NULL, 'g' },
383 { "native-aio", 0, NULL, 'k' },
384 { "discard", 1, NULL, 'd' },
385 { "cache", 1, NULL, 't' },
386 { "trace", 1, NULL, 'T' },
391 int flags = BDRV_O_UNMAP;
394 signal(SIGPIPE, SIG_IGN);
397 progname = basename(argv[0]);
398 qemu_init_exec_dir(argv[0]);
400 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
403 flags |= BDRV_O_SNAPSHOT;
406 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
409 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
410 error_report("Invalid discard option: %s", optarg);
415 add_user_command(optarg);
421 qemuio_misalign = true;
427 flags |= BDRV_O_NATIVE_AIO;
430 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
431 error_report("Invalid cache option: %s", optarg);
436 if (!trace_init_backends(optarg, NULL)) {
437 exit(1); /* error message will have been printed */
441 printf("%s version %s\n", progname, QEMU_VERSION);
452 if ((argc - optind) > 1) {
457 qemu_init_main_loop();
460 /* initialize commands */
461 qemuio_add_command(&quit_cmd);
462 qemuio_add_command(&open_cmd);
463 qemuio_add_command(&close_cmd);
465 if (isatty(STDIN_FILENO)) {
466 readline_state = readline_init(readline_printf_func,
469 readline_completion_func);
470 qemu_set_tty_echo(STDIN_FILENO, false);
471 atexit(reenable_tty_echo);
474 /* open the device */
476 flags |= BDRV_O_RDWR;
479 if ((argc - optind) == 1) {
480 openfile(argv[optind], flags, growable, NULL);
485 * Make sure all outstanding requests complete before the program exits.
490 bdrv_unref(qemuio_bs);
492 g_free(readline_state);