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
29 BlockDriverState *qemuio_bs;
30 extern int qemuio_misalign;
32 /* qemu-io commands passed using -c */
34 static char **cmdline;
36 static ReadLineState *readline_state;
38 static int close_f(BlockDriverState *bs, int argc, char **argv)
45 static const cmdinfo_t close_cmd = {
49 .oneline = "close the current open file",
52 static int openfile(char *name, int flags, int growable, QDict *opts)
54 Error *local_err = NULL;
57 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 device %s: %s\n", progname, name,
66 error_get_pretty(local_err));
67 error_free(local_err);
71 qemuio_bs = bdrv_new("hda");
73 if (bdrv_open(&qemuio_bs, name, NULL, opts, flags, NULL, &local_err)
76 fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
77 error_get_pretty(local_err));
78 error_free(local_err);
79 bdrv_unref(qemuio_bs);
88 static void open_help(void)
92 " opens a new file in the requested mode\n"
95 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
97 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
98 " -r, -- open file read-only\n"
99 " -s, -- use snapshot file\n"
100 " -n, -- disable host cache\n"
101 " -g, -- allow file to grow (only applies to protocols)\n"
102 " -o, -- options to be given to the block driver"
106 static int open_f(BlockDriverState *bs, int argc, char **argv);
108 static const cmdinfo_t open_cmd = {
114 .flags = CMD_NOFILE_OK,
115 .args = "[-Crsn] [-o options] [path]",
116 .oneline = "open the file specified by path",
120 static QemuOptsList empty_opts = {
122 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
124 /* no elements => accept any params */
125 { /* end of list */ }
129 static int open_f(BlockDriverState *bs, int argc, char **argv)
138 while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
141 flags |= BDRV_O_SNAPSHOT;
144 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
153 qopts = qemu_opts_parse(&empty_opts, optarg, 0);
155 printf("could not parse option list -- %s\n", optarg);
158 opts = qemu_opts_to_qdict(qopts, opts);
159 qemu_opts_del(qopts);
162 return qemuio_command_usage(&open_cmd);
167 flags |= BDRV_O_RDWR;
170 if (optind == argc - 1) {
171 return openfile(argv[optind], flags, growable, opts);
172 } else if (optind == argc) {
173 return openfile(NULL, flags, growable, opts);
175 return qemuio_command_usage(&open_cmd);
179 static int quit_f(BlockDriverState *bs, int argc, char **argv)
184 static const cmdinfo_t quit_cmd = {
190 .flags = CMD_FLAG_GLOBAL,
191 .oneline = "exit the program",
194 static void usage(const char *name)
197 "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
198 "QEMU Disk exerciser\n"
200 " -c, --cmd command to execute\n"
201 " -r, --read-only export read-only\n"
202 " -s, --snapshot use snapshot file\n"
203 " -n, --nocache disable host cache\n"
204 " -g, --growable allow file to grow (only applies to protocols)\n"
205 " -m, --misalign misalign allocations for O_DIRECT\n"
206 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
207 " -t, --cache=MODE use the given cache mode for the image\n"
208 " -T, --trace FILE enable trace events listed in the given file\n"
209 " -h, --help display this help and exit\n"
210 " -V, --version output version information and exit\n"
215 static char *get_prompt(void)
217 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
220 snprintf(prompt, sizeof(prompt), "%s> ", progname);
226 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
227 const char *fmt, ...)
235 static void readline_flush_func(void *opaque)
240 static void readline_func(void *opaque, const char *str, void *readline_opaque)
242 char **line = readline_opaque;
243 *line = g_strdup(str);
246 static void completion_match(const char *cmd, void *opaque)
248 readline_add_completion(readline_state, cmd);
251 static void readline_completion_func(void *opaque, const char *str)
253 readline_set_completion_index(readline_state, strlen(str));
254 qemuio_complete_command(str, completion_match, NULL);
257 static char *fetchline_readline(void)
261 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
267 readline_handle_byte(readline_state, ch);
272 #define MAXREADLINESZ 1024
273 static char *fetchline_fgets(void)
275 char *p, *line = g_malloc(MAXREADLINESZ);
277 if (!fgets(line, MAXREADLINESZ, stdin)) {
282 p = line + strlen(line);
283 if (p != line && p[-1] == '\n') {
290 static char *fetchline(void)
292 if (readline_state) {
293 return fetchline_readline();
295 return fetchline_fgets();
299 static void prep_fetchline(void *opaque)
301 int *fetchable = opaque;
303 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
307 static void command_loop(void)
309 int i, done = 0, fetchable = 0, prompted = 0;
312 for (i = 0; !done && i < ncmdline; i++) {
313 done = qemuio_command(qemuio_bs, cmdline[i]);
322 printf("%s", get_prompt());
324 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
328 main_loop_wait(false);
338 done = qemuio_command(qemuio_bs, input);
344 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
347 static void add_user_command(char *optarg)
349 cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
350 cmdline[ncmdline-1] = optarg;
353 static void reenable_tty_echo(void)
355 qemu_set_tty_echo(STDIN_FILENO, true);
358 int main(int argc, char **argv)
362 const char *sopt = "hVc:d:rsnmgkt:T:";
363 const struct option lopt[] = {
364 { "help", 0, NULL, 'h' },
365 { "version", 0, NULL, 'V' },
366 { "offset", 1, NULL, 'o' },
367 { "cmd", 1, NULL, 'c' },
368 { "read-only", 0, NULL, 'r' },
369 { "snapshot", 0, NULL, 's' },
370 { "nocache", 0, NULL, 'n' },
371 { "misalign", 0, NULL, 'm' },
372 { "growable", 0, NULL, 'g' },
373 { "native-aio", 0, NULL, 'k' },
374 { "discard", 1, NULL, 'd' },
375 { "cache", 1, NULL, 't' },
376 { "trace", 1, NULL, 'T' },
381 int flags = BDRV_O_UNMAP;
384 signal(SIGPIPE, SIG_IGN);
387 progname = basename(argv[0]);
388 qemu_init_exec_dir(argv[0]);
390 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
393 flags |= BDRV_O_SNAPSHOT;
396 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
399 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
400 error_report("Invalid discard option: %s", optarg);
405 add_user_command(optarg);
417 flags |= BDRV_O_NATIVE_AIO;
420 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
421 error_report("Invalid cache option: %s", optarg);
426 if (!trace_backend_init(optarg, NULL)) {
427 exit(1); /* error message will have been printed */
431 printf("%s version %s\n", progname, QEMU_VERSION);
442 if ((argc - optind) > 1) {
447 qemu_init_main_loop();
450 /* initialize commands */
451 qemuio_add_command(&quit_cmd);
452 qemuio_add_command(&open_cmd);
453 qemuio_add_command(&close_cmd);
455 if (isatty(STDIN_FILENO)) {
456 readline_state = readline_init(readline_printf_func,
459 readline_completion_func);
460 qemu_set_tty_echo(STDIN_FILENO, false);
461 atexit(reenable_tty_echo);
464 /* open the device */
466 flags |= BDRV_O_RDWR;
469 if ((argc - optind) == 1) {
470 openfile(argv[optind], flags, growable, NULL);
475 * Make sure all outstanding requests complete before the program exits.
480 bdrv_unref(qemuio_bs);
482 g_free(readline_state);