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 #if defined(CONFIG_FVD)
171 # include "qemu-io-sim.c"
174 static const cmdinfo_t quit_cmd
= {
180 .flags
= CMD_FLAG_GLOBAL
,
181 .oneline
= "exit the program",
184 static void usage(const char *name
)
187 "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
188 "QEMU Disk exerciser\n"
190 " -c, --cmd STRING execute command with its arguments\n"
191 " from the given string\n"
192 " -f, --format FMT specifies the block driver to use\n"
193 " -r, --read-only export read-only\n"
194 " -s, --snapshot use snapshot file\n"
195 " -n, --nocache disable host cache\n"
196 " -m, --misalign misalign allocations for O_DIRECT\n"
197 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
198 " -t, --cache=MODE use the given cache mode for the image\n"
199 " -T, --trace FILE enable trace events listed in the given file\n"
200 " -h, --help display this help and exit\n"
201 " -V, --version output version information and exit\n"
203 "See '%s -c help' for information on available commands."
208 static char *get_prompt(void)
210 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
213 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
219 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque
,
220 const char *fmt
, ...)
228 static void readline_flush_func(void *opaque
)
233 static void readline_func(void *opaque
, const char *str
, void *readline_opaque
)
235 char **line
= readline_opaque
;
236 *line
= g_strdup(str
);
239 static void completion_match(const char *cmd
, void *opaque
)
241 readline_add_completion(readline_state
, cmd
);
244 static void readline_completion_func(void *opaque
, const char *str
)
246 readline_set_completion_index(readline_state
, strlen(str
));
247 qemuio_complete_command(str
, completion_match
, NULL
);
250 static char *fetchline_readline(void)
254 readline_start(readline_state
, get_prompt(), 0, readline_func
, &line
);
260 readline_handle_byte(readline_state
, ch
);
265 #define MAXREADLINESZ 1024
266 static char *fetchline_fgets(void)
268 char *p
, *line
= g_malloc(MAXREADLINESZ
);
270 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
275 p
= line
+ strlen(line
);
276 if (p
!= line
&& p
[-1] == '\n') {
283 static char *fetchline(void)
285 if (readline_state
) {
286 return fetchline_readline();
288 return fetchline_fgets();
292 static void prep_fetchline(void *opaque
)
294 int *fetchable
= opaque
;
296 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
300 static void command_loop(void)
302 int i
, done
= 0, fetchable
= 0, prompted
= 0;
305 for (i
= 0; !done
&& i
< ncmdline
; i
++) {
306 done
= qemuio_command(qemuio_blk
, cmdline
[i
]);
315 printf("%s", get_prompt());
317 qemu_set_fd_handler(STDIN_FILENO
, prep_fetchline
, NULL
, &fetchable
);
321 main_loop_wait(false);
331 done
= qemuio_command(qemuio_blk
, input
);
337 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
340 static void add_user_command(char *optarg
)
342 cmdline
= g_renew(char *, cmdline
, ++ncmdline
);
343 cmdline
[ncmdline
-1] = optarg
;
346 static void reenable_tty_echo(void)
348 qemu_set_tty_echo(STDIN_FILENO
, true);
351 int main(int argc
, char **argv
)
354 const char *sopt
= "hVc:d:f:rsnmgkt:T:";
355 const struct option lopt
[] = {
356 { "help", 0, NULL
, 'h' },
357 { "version", 0, NULL
, 'V' },
358 { "offset", 1, NULL
, 'o' },
359 { "cmd", 1, NULL
, 'c' },
360 { "format", 1, NULL
, 'f' },
361 { "read-only", 0, NULL
, 'r' },
362 { "snapshot", 0, NULL
, 's' },
363 { "nocache", 0, NULL
, 'n' },
364 { "misalign", 0, NULL
, 'm' },
365 { "native-aio", 0, NULL
, 'k' },
366 { "discard", 1, NULL
, 'd' },
367 { "cache", 1, NULL
, 't' },
368 { "trace", 1, NULL
, 'T' },
373 int flags
= BDRV_O_UNMAP
;
374 Error
*local_error
= NULL
;
378 signal(SIGPIPE
, SIG_IGN
);
381 progname
= basename(argv
[0]);
382 qemu_init_exec_dir(argv
[0]);
386 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
389 flags
|= BDRV_O_SNAPSHOT
;
392 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
395 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
396 error_report("Invalid discard option: %s", optarg
);
404 qdict_put(opts
, "driver", qstring_from_str(optarg
));
407 add_user_command(optarg
);
413 qemuio_misalign
= true;
416 flags
|= BDRV_O_NATIVE_AIO
;
419 if (bdrv_parse_cache_flags(optarg
, &flags
) < 0) {
420 error_report("Invalid cache option: %s", optarg
);
425 if (!trace_init_backends(optarg
, NULL
)) {
426 exit(1); /* error message will have been printed */
430 printf("%s version %s\n", progname
, QEMU_VERSION
);
441 if ((argc
- optind
) > 1) {
446 if (qemu_init_main_loop(&local_error
)) {
447 error_report_err(local_error
);
451 /* initialize commands */
452 qemuio_add_command(&quit_cmd
);
453 qemuio_add_command(&open_cmd
);
454 qemuio_add_command(&close_cmd
);
456 if (isatty(STDIN_FILENO
)) {
457 readline_state
= readline_init(readline_printf_func
,
460 readline_completion_func
);
461 qemu_set_tty_echo(STDIN_FILENO
, false);
462 atexit(reenable_tty_echo
);
465 /* open the device */
467 flags
|= BDRV_O_RDWR
;
470 if ((argc
- optind
) == 1) {
471 openfile(argv
[optind
], flags
, opts
);
476 * Make sure all outstanding requests complete before the program exits.
480 blk_unref(qemuio_blk
);
481 g_free(readline_state
);