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 "sysemu/block-backend.h"
23 #include "block/block_int.h"
24 #include "trace/control.h"
26 #define CMD_NOFILE_OK 0x01
28 static char *progname;
30 static BlockBackend *qemuio_blk;
32 /* qemu-io commands passed using -c */
34 static char **cmdline;
36 static ReadLineState *readline_state;
38 static int close_f(BlockBackend *blk, int argc, char **argv)
40 blk_unref(qemuio_blk);
45 static const cmdinfo_t close_cmd = {
49 .oneline = "close the current open file",
52 static int openfile(char *name, int flags, QDict *opts)
54 Error *local_err = NULL;
57 fprintf(stderr, "file open already, try 'help close'\n");
62 qemuio_blk = blk_new_open("hda", name, NULL, opts, flags, &local_err);
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);
74 static void open_help(void)
78 " opens a new file in the requested mode\n"
81 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
83 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
84 " -r, -- open file read-only\n"
85 " -s, -- use snapshot file\n"
86 " -n, -- disable host cache\n"
87 " -o, -- options to be given to the block driver"
91 static int open_f(BlockBackend *blk, int argc, char **argv);
93 static const cmdinfo_t open_cmd = {
99 .flags = CMD_NOFILE_OK,
100 .args = "[-Crsn] [-o options] [path]",
101 .oneline = "open the file specified by path",
105 static QemuOptsList empty_opts = {
108 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
110 /* no elements => accept any params */
111 { /* end of list */ }
115 static int open_f(BlockBackend *blk, int argc, char **argv)
123 while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
126 flags |= BDRV_O_SNAPSHOT;
129 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
135 if (!qemu_opts_parse(&empty_opts, optarg, 0)) {
136 printf("could not parse option list -- %s\n", optarg);
137 qemu_opts_reset(&empty_opts);
142 qemu_opts_reset(&empty_opts);
143 return qemuio_command_usage(&open_cmd);
148 flags |= BDRV_O_RDWR;
151 qopts = qemu_opts_find(&empty_opts, NULL);
152 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
153 qemu_opts_reset(&empty_opts);
155 if (optind == argc - 1) {
156 return openfile(argv[optind], flags, opts);
157 } else if (optind == argc) {
158 return openfile(NULL, flags, opts);
161 return qemuio_command_usage(&open_cmd);
165 static int quit_f(BlockBackend *blk, int argc, char **argv)
170 static const cmdinfo_t quit_cmd = {
176 .flags = CMD_FLAG_GLOBAL,
177 .oneline = "exit the program",
180 static void usage(const char *name)
183 "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
184 "QEMU Disk exerciser\n"
186 " -c, --cmd STRING execute command with its arguments\n"
187 " from the given string\n"
188 " -f, --format FMT specifies the block driver to use\n"
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"
193 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
194 " -t, --cache=MODE use the given cache mode for the image\n"
195 " -T, --trace FILE enable trace events listed in the given file\n"
196 " -h, --help display this help and exit\n"
197 " -V, --version output version information and exit\n"
199 "See '%s -c help' for information on available commands."
204 static char *get_prompt(void)
206 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
209 snprintf(prompt, sizeof(prompt), "%s> ", progname);
215 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
216 const char *fmt, ...)
224 static void readline_flush_func(void *opaque)
229 static void readline_func(void *opaque, const char *str, void *readline_opaque)
231 char **line = readline_opaque;
232 *line = g_strdup(str);
235 static void completion_match(const char *cmd, void *opaque)
237 readline_add_completion(readline_state, cmd);
240 static void readline_completion_func(void *opaque, const char *str)
242 readline_set_completion_index(readline_state, strlen(str));
243 qemuio_complete_command(str, completion_match, NULL);
246 static char *fetchline_readline(void)
250 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
256 readline_handle_byte(readline_state, ch);
261 #define MAXREADLINESZ 1024
262 static char *fetchline_fgets(void)
264 char *p, *line = g_malloc(MAXREADLINESZ);
266 if (!fgets(line, MAXREADLINESZ, stdin)) {
271 p = line + strlen(line);
272 if (p != line && p[-1] == '\n') {
279 static char *fetchline(void)
281 if (readline_state) {
282 return fetchline_readline();
284 return fetchline_fgets();
288 static void prep_fetchline(void *opaque)
290 int *fetchable = opaque;
292 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
296 static void command_loop(void)
298 int i, done = 0, fetchable = 0, prompted = 0;
301 for (i = 0; !done && i < ncmdline; i++) {
302 done = qemuio_command(qemuio_blk, cmdline[i]);
311 printf("%s", get_prompt());
313 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
317 main_loop_wait(false);
327 done = qemuio_command(qemuio_blk, input);
333 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
336 static void add_user_command(char *optarg)
338 cmdline = g_renew(char *, cmdline, ++ncmdline);
339 cmdline[ncmdline-1] = optarg;
342 static void reenable_tty_echo(void)
344 qemu_set_tty_echo(STDIN_FILENO, true);
347 int main(int argc, char **argv)
350 const char *sopt = "hVc:d:f:rsnmgkt:T:";
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' },
356 { "format", 1, NULL, 'f' },
357 { "read-only", 0, NULL, 'r' },
358 { "snapshot", 0, NULL, 's' },
359 { "nocache", 0, NULL, 'n' },
360 { "misalign", 0, NULL, 'm' },
361 { "native-aio", 0, NULL, 'k' },
362 { "discard", 1, NULL, 'd' },
363 { "cache", 1, NULL, 't' },
364 { "trace", 1, NULL, 'T' },
369 int flags = BDRV_O_UNMAP;
370 Error *local_error = NULL;
374 signal(SIGPIPE, SIG_IGN);
377 progname = basename(argv[0]);
378 qemu_init_exec_dir(argv[0]);
382 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
385 flags |= BDRV_O_SNAPSHOT;
388 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
391 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
392 error_report("Invalid discard option: %s", optarg);
400 qdict_put(opts, "driver", qstring_from_str(optarg));
403 add_user_command(optarg);
409 qemuio_misalign = true;
412 flags |= BDRV_O_NATIVE_AIO;
415 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
416 error_report("Invalid cache option: %s", optarg);
421 if (!trace_init_backends(optarg, NULL)) {
422 exit(1); /* error message will have been printed */
426 printf("%s version %s\n", progname, QEMU_VERSION);
437 if ((argc - optind) > 1) {
442 if (qemu_init_main_loop(&local_error)) {
443 error_report_err(local_error);
447 /* initialize commands */
448 qemuio_add_command(&quit_cmd);
449 qemuio_add_command(&open_cmd);
450 qemuio_add_command(&close_cmd);
452 if (isatty(STDIN_FILENO)) {
453 readline_state = readline_init(readline_printf_func,
456 readline_completion_func);
457 qemu_set_tty_echo(STDIN_FILENO, false);
458 atexit(reenable_tty_echo);
461 /* open the device */
463 flags |= BDRV_O_RDWR;
466 if ((argc - optind) == 1) {
467 openfile(argv[optind], flags, opts);
472 * Make sure all outstanding requests complete before the program exits.
476 blk_unref(qemuio_blk);
477 g_free(readline_state);