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");
61 if (bdrv_open(&qemuio_bs, name, NULL, opts, flags | BDRV_O_PROTOCOL,
64 fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
65 error_get_pretty(local_err));
66 error_free(local_err);
70 qemuio_bs = bdrv_new("hda");
72 if (bdrv_open(&qemuio_bs, name, NULL, opts, flags, NULL, &local_err)
75 fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
76 error_get_pretty(local_err));
77 error_free(local_err);
78 bdrv_unref(qemuio_bs);
87 static void open_help(void)
91 " opens a new file in the requested mode\n"
94 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
96 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
97 " -r, -- open file read-only\n"
98 " -s, -- use snapshot file\n"
99 " -n, -- disable host cache\n"
100 " -g, -- allow file to grow (only applies to protocols)\n"
101 " -o, -- options to be given to the block driver"
105 static int open_f(BlockDriverState *bs, int argc, char **argv);
107 static const cmdinfo_t open_cmd = {
113 .flags = CMD_NOFILE_OK,
114 .args = "[-Crsn] [-o options] [path]",
115 .oneline = "open the file specified by path",
119 static QemuOptsList empty_opts = {
121 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
123 /* no elements => accept any params */
124 { /* end of list */ }
128 static int open_f(BlockDriverState *bs, int argc, char **argv)
137 while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
140 flags |= BDRV_O_SNAPSHOT;
143 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
152 qopts = qemu_opts_parse(&empty_opts, optarg, 0);
154 printf("could not parse option list -- %s\n", optarg);
157 opts = qemu_opts_to_qdict(qopts, opts);
158 qemu_opts_del(qopts);
161 return qemuio_command_usage(&open_cmd);
166 flags |= BDRV_O_RDWR;
169 if (optind == argc - 1) {
170 return openfile(argv[optind], flags, growable, opts);
171 } else if (optind == argc) {
172 return openfile(NULL, flags, growable, opts);
174 return qemuio_command_usage(&open_cmd);
178 static int quit_f(BlockDriverState *bs, int argc, char **argv)
183 static const cmdinfo_t quit_cmd = {
189 .flags = CMD_FLAG_GLOBAL,
190 .oneline = "exit the program",
193 static void usage(const char *name)
196 "Usage: %s [-h] [-V] [-rsnm] [-c STRING] ... [file]\n"
197 "QEMU Disk exerciser\n"
199 " -c, --cmd STRING execute command with its arguments\n"
200 " from the given string\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"
212 "See '%s -c help' for information on available commands."
217 static char *get_prompt(void)
219 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
222 snprintf(prompt, sizeof(prompt), "%s> ", progname);
228 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
229 const char *fmt, ...)
237 static void readline_flush_func(void *opaque)
242 static void readline_func(void *opaque, const char *str, void *readline_opaque)
244 char **line = readline_opaque;
245 *line = g_strdup(str);
248 static void completion_match(const char *cmd, void *opaque)
250 readline_add_completion(readline_state, cmd);
253 static void readline_completion_func(void *opaque, const char *str)
255 readline_set_completion_index(readline_state, strlen(str));
256 qemuio_complete_command(str, completion_match, NULL);
259 static char *fetchline_readline(void)
263 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
269 readline_handle_byte(readline_state, ch);
274 #define MAXREADLINESZ 1024
275 static char *fetchline_fgets(void)
277 char *p, *line = g_malloc(MAXREADLINESZ);
279 if (!fgets(line, MAXREADLINESZ, stdin)) {
284 p = line + strlen(line);
285 if (p != line && p[-1] == '\n') {
292 static char *fetchline(void)
294 if (readline_state) {
295 return fetchline_readline();
297 return fetchline_fgets();
301 static void prep_fetchline(void *opaque)
303 int *fetchable = opaque;
305 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
309 static void command_loop(void)
311 int i, done = 0, fetchable = 0, prompted = 0;
314 for (i = 0; !done && i < ncmdline; i++) {
315 done = qemuio_command(qemuio_bs, cmdline[i]);
324 printf("%s", get_prompt());
326 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
330 main_loop_wait(false);
340 done = qemuio_command(qemuio_bs, input);
346 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
349 static void add_user_command(char *optarg)
351 cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
352 cmdline[ncmdline-1] = optarg;
355 static void reenable_tty_echo(void)
357 qemu_set_tty_echo(STDIN_FILENO, true);
360 int main(int argc, char **argv)
364 const char *sopt = "hVc:d:rsnmgkt:T:";
365 const struct option lopt[] = {
366 { "help", 0, NULL, 'h' },
367 { "version", 0, NULL, 'V' },
368 { "offset", 1, NULL, 'o' },
369 { "cmd", 1, NULL, 'c' },
370 { "read-only", 0, NULL, 'r' },
371 { "snapshot", 0, NULL, 's' },
372 { "nocache", 0, NULL, 'n' },
373 { "misalign", 0, NULL, 'm' },
374 { "growable", 0, NULL, 'g' },
375 { "native-aio", 0, NULL, 'k' },
376 { "discard", 1, NULL, 'd' },
377 { "cache", 1, NULL, 't' },
378 { "trace", 1, NULL, 'T' },
383 int flags = BDRV_O_UNMAP;
386 signal(SIGPIPE, SIG_IGN);
389 progname = basename(argv[0]);
390 qemu_init_exec_dir(argv[0]);
392 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
395 flags |= BDRV_O_SNAPSHOT;
398 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
401 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
402 error_report("Invalid discard option: %s", optarg);
407 add_user_command(optarg);
413 qemuio_misalign = true;
419 flags |= BDRV_O_NATIVE_AIO;
422 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
423 error_report("Invalid cache option: %s", optarg);
428 if (!trace_backend_init(optarg, NULL)) {
429 exit(1); /* error message will have been printed */
433 printf("%s version %s\n", progname, QEMU_VERSION);
444 if ((argc - optind) > 1) {
449 qemu_init_main_loop();
452 /* initialize commands */
453 qemuio_add_command(&quit_cmd);
454 qemuio_add_command(&open_cmd);
455 qemuio_add_command(&close_cmd);
457 if (isatty(STDIN_FILENO)) {
458 readline_state = readline_init(readline_printf_func,
461 readline_completion_func);
462 qemu_set_tty_echo(STDIN_FILENO, false);
463 atexit(reenable_tty_echo);
466 /* open the device */
468 flags |= BDRV_O_RDWR;
471 if ((argc - optind) == 1) {
472 openfile(argv[optind], flags, growable, NULL);
477 * Make sure all outstanding requests complete before the program exits.
482 bdrv_unref(qemuio_bs);
484 g_free(readline_state);