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
;
31 static BlockDriverState
*qemuio_bs
;
33 /* qemu-io commands passed using -c */
35 static char **cmdline
;
37 static ReadLineState
*readline_state
;
39 static int close_f(BlockDriverState
*bs
, int argc
, char **argv
)
41 blk_unref(qemuio_blk
);
47 static const cmdinfo_t close_cmd
= {
51 .oneline
= "close the current open file",
54 static int openfile(char *name
, int flags
, int growable
, QDict
*opts
)
56 Error
*local_err
= NULL
;
59 fprintf(stderr
, "file open already, try 'help close'\n");
64 qemuio_blk
= blk_new_with_bs("hda", &error_abort
);
65 qemuio_bs
= blk_bs(qemuio_blk
);
68 flags
|= BDRV_O_PROTOCOL
;
71 if (bdrv_open(&qemuio_bs
, name
, NULL
, opts
, flags
, NULL
, &local_err
) < 0) {
72 fprintf(stderr
, "%s: can't open%s%s: %s\n", progname
,
73 name
? " device " : "", name
?: "",
74 error_get_pretty(local_err
));
75 error_free(local_err
);
76 blk_unref(qemuio_blk
);
85 static void open_help(void)
89 " opens a new file in the requested mode\n"
92 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
94 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
95 " -r, -- open file read-only\n"
96 " -s, -- use snapshot file\n"
97 " -n, -- disable host cache\n"
98 " -g, -- allow file to grow (only applies to protocols)\n"
99 " -o, -- options to be given to the block driver"
103 static int open_f(BlockDriverState
*bs
, int argc
, char **argv
);
105 static const cmdinfo_t open_cmd
= {
111 .flags
= CMD_NOFILE_OK
,
112 .args
= "[-Crsn] [-o options] [path]",
113 .oneline
= "open the file specified by path",
117 static QemuOptsList empty_opts
= {
120 .head
= QTAILQ_HEAD_INITIALIZER(empty_opts
.head
),
122 /* no elements => accept any params */
123 { /* end of list */ }
127 static int open_f(BlockDriverState
*bs
, int argc
, char **argv
)
136 while ((c
= getopt(argc
, argv
, "snrgo:")) != EOF
) {
139 flags
|= BDRV_O_SNAPSHOT
;
142 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
151 if (!qemu_opts_parse(&empty_opts
, optarg
, 0)) {
152 printf("could not parse option list -- %s\n", optarg
);
153 qemu_opts_reset(&empty_opts
);
158 qemu_opts_reset(&empty_opts
);
159 return qemuio_command_usage(&open_cmd
);
164 flags
|= BDRV_O_RDWR
;
167 qopts
= qemu_opts_find(&empty_opts
, NULL
);
168 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
169 qemu_opts_reset(&empty_opts
);
171 if (optind
== argc
- 1) {
172 return openfile(argv
[optind
], flags
, growable
, opts
);
173 } else if (optind
== argc
) {
174 return openfile(NULL
, flags
, growable
, opts
);
177 return qemuio_command_usage(&open_cmd
);
181 static int quit_f(BlockDriverState
*bs
, int argc
, char **argv
)
186 #if defined(CONFIG_FVD)
187 # include "qemu-io-sim.c"
190 static const cmdinfo_t quit_cmd
= {
196 .flags
= CMD_FLAG_GLOBAL
,
197 .oneline
= "exit the program",
200 static void usage(const char *name
)
203 "Usage: %s [-h] [-V] [-rsnm] [-c STRING] ... [file]\n"
204 "QEMU Disk exerciser\n"
206 " -c, --cmd STRING execute command with its arguments\n"
207 " from the given string\n"
208 " -r, --read-only export read-only\n"
209 " -s, --snapshot use snapshot file\n"
210 " -n, --nocache disable host cache\n"
211 " -g, --growable allow file to grow (only applies to protocols)\n"
212 " -m, --misalign misalign allocations for O_DIRECT\n"
213 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
214 " -t, --cache=MODE use the given cache mode for the image\n"
215 " -T, --trace FILE enable trace events listed in the given file\n"
216 " -h, --help display this help and exit\n"
217 " -V, --version output version information and exit\n"
219 "See '%s -c help' for information on available commands."
224 static char *get_prompt(void)
226 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
229 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
235 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque
,
236 const char *fmt
, ...)
244 static void readline_flush_func(void *opaque
)
249 static void readline_func(void *opaque
, const char *str
, void *readline_opaque
)
251 char **line
= readline_opaque
;
252 *line
= g_strdup(str
);
255 static void completion_match(const char *cmd
, void *opaque
)
257 readline_add_completion(readline_state
, cmd
);
260 static void readline_completion_func(void *opaque
, const char *str
)
262 readline_set_completion_index(readline_state
, strlen(str
));
263 qemuio_complete_command(str
, completion_match
, NULL
);
266 static char *fetchline_readline(void)
270 readline_start(readline_state
, get_prompt(), 0, readline_func
, &line
);
276 readline_handle_byte(readline_state
, ch
);
281 #define MAXREADLINESZ 1024
282 static char *fetchline_fgets(void)
284 char *p
, *line
= g_malloc(MAXREADLINESZ
);
286 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
291 p
= line
+ strlen(line
);
292 if (p
!= line
&& p
[-1] == '\n') {
299 static char *fetchline(void)
301 if (readline_state
) {
302 return fetchline_readline();
304 return fetchline_fgets();
308 static void prep_fetchline(void *opaque
)
310 int *fetchable
= opaque
;
312 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
316 static void command_loop(void)
318 int i
, done
= 0, fetchable
= 0, prompted
= 0;
321 for (i
= 0; !done
&& i
< ncmdline
; i
++) {
322 done
= qemuio_command(qemuio_bs
, cmdline
[i
]);
331 printf("%s", get_prompt());
333 qemu_set_fd_handler(STDIN_FILENO
, prep_fetchline
, NULL
, &fetchable
);
337 main_loop_wait(false);
347 done
= qemuio_command(qemuio_bs
, input
);
353 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
356 static void add_user_command(char *optarg
)
358 cmdline
= g_renew(char *, cmdline
, ++ncmdline
);
359 cmdline
[ncmdline
-1] = optarg
;
362 static void reenable_tty_echo(void)
364 qemu_set_tty_echo(STDIN_FILENO
, true);
367 int main(int argc
, char **argv
)
371 const char *sopt
= "hVc:d:rsnmgkt:T:";
372 const struct option lopt
[] = {
373 { "help", 0, NULL
, 'h' },
374 { "version", 0, NULL
, 'V' },
375 { "offset", 1, NULL
, 'o' },
376 { "cmd", 1, NULL
, 'c' },
377 { "read-only", 0, NULL
, 'r' },
378 { "snapshot", 0, NULL
, 's' },
379 { "nocache", 0, NULL
, 'n' },
380 { "misalign", 0, NULL
, 'm' },
381 { "growable", 0, NULL
, 'g' },
382 { "native-aio", 0, NULL
, 'k' },
383 { "discard", 1, NULL
, 'd' },
384 { "cache", 1, NULL
, 't' },
385 { "trace", 1, NULL
, 'T' },
390 int flags
= BDRV_O_UNMAP
;
391 Error
*local_error
= NULL
;
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 if (qemu_init_main_loop(&local_error
)) {
458 error_report("%s", error_get_pretty(local_error
));
459 error_free(local_error
);
464 /* initialize commands */
465 qemuio_add_command(&quit_cmd
);
466 qemuio_add_command(&open_cmd
);
467 qemuio_add_command(&close_cmd
);
469 if (isatty(STDIN_FILENO
)) {
470 readline_state
= readline_init(readline_printf_func
,
473 readline_completion_func
);
474 qemu_set_tty_echo(STDIN_FILENO
, false);
475 atexit(reenable_tty_echo
);
478 /* open the device */
480 flags
|= BDRV_O_RDWR
;
483 if ((argc
- optind
) == 1) {
484 openfile(argv
[optind
], flags
, growable
, NULL
);
489 * Make sure all outstanding requests complete before the program exits.
493 blk_unref(qemuio_blk
);
494 g_free(readline_state
);