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
)
42 blk_unref(qemuio_blk
);
48 static const cmdinfo_t close_cmd
= {
52 .oneline
= "close the current open file",
55 static int openfile(char *name
, int flags
, int growable
, QDict
*opts
)
57 Error
*local_err
= NULL
;
60 fprintf(stderr
, "file open already, try 'help close'\n");
65 qemuio_blk
= blk_new("hda", &error_abort
);
66 qemuio_bs
= bdrv_new_root("hda", &error_abort
);
69 flags
|= BDRV_O_PROTOCOL
;
72 if (bdrv_open(&qemuio_bs
, name
, NULL
, opts
, flags
, NULL
, &local_err
) < 0) {
73 fprintf(stderr
, "%s: can't open%s%s: %s\n", progname
,
74 name
? " device " : "", name
?: "",
75 error_get_pretty(local_err
));
76 error_free(local_err
);
77 bdrv_unref(qemuio_bs
);
78 blk_unref(qemuio_blk
);
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
= {
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 if (!qemu_opts_parse(&empty_opts
, optarg
, 0)) {
154 printf("could not parse option list -- %s\n", optarg
);
155 qemu_opts_reset(&empty_opts
);
160 qemu_opts_reset(&empty_opts
);
161 return qemuio_command_usage(&open_cmd
);
166 flags
|= BDRV_O_RDWR
;
169 qopts
= qemu_opts_find(&empty_opts
, NULL
);
170 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
171 qemu_opts_reset(&empty_opts
);
173 if (optind
== argc
- 1) {
174 return openfile(argv
[optind
], flags
, growable
, opts
);
175 } else if (optind
== argc
) {
176 return openfile(NULL
, flags
, growable
, opts
);
179 return qemuio_command_usage(&open_cmd
);
183 static int quit_f(BlockDriverState
*bs
, int argc
, char **argv
)
188 static const cmdinfo_t quit_cmd
= {
194 .flags
= CMD_FLAG_GLOBAL
,
195 .oneline
= "exit the program",
198 static void usage(const char *name
)
201 "Usage: %s [-h] [-V] [-rsnm] [-c STRING] ... [file]\n"
202 "QEMU Disk exerciser\n"
204 " -c, --cmd STRING execute command with its arguments\n"
205 " from the given string\n"
206 " -r, --read-only export read-only\n"
207 " -s, --snapshot use snapshot file\n"
208 " -n, --nocache disable host cache\n"
209 " -g, --growable allow file to grow (only applies to protocols)\n"
210 " -m, --misalign misalign allocations for O_DIRECT\n"
211 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
212 " -t, --cache=MODE use the given cache mode for the image\n"
213 " -T, --trace FILE enable trace events listed in the given file\n"
214 " -h, --help display this help and exit\n"
215 " -V, --version output version information and exit\n"
217 "See '%s -c help' for information on available commands."
222 static char *get_prompt(void)
224 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
227 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
233 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque
,
234 const char *fmt
, ...)
242 static void readline_flush_func(void *opaque
)
247 static void readline_func(void *opaque
, const char *str
, void *readline_opaque
)
249 char **line
= readline_opaque
;
250 *line
= g_strdup(str
);
253 static void completion_match(const char *cmd
, void *opaque
)
255 readline_add_completion(readline_state
, cmd
);
258 static void readline_completion_func(void *opaque
, const char *str
)
260 readline_set_completion_index(readline_state
, strlen(str
));
261 qemuio_complete_command(str
, completion_match
, NULL
);
264 static char *fetchline_readline(void)
268 readline_start(readline_state
, get_prompt(), 0, readline_func
, &line
);
274 readline_handle_byte(readline_state
, ch
);
279 #define MAXREADLINESZ 1024
280 static char *fetchline_fgets(void)
282 char *p
, *line
= g_malloc(MAXREADLINESZ
);
284 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
289 p
= line
+ strlen(line
);
290 if (p
!= line
&& p
[-1] == '\n') {
297 static char *fetchline(void)
299 if (readline_state
) {
300 return fetchline_readline();
302 return fetchline_fgets();
306 static void prep_fetchline(void *opaque
)
308 int *fetchable
= opaque
;
310 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
314 static void command_loop(void)
316 int i
, done
= 0, fetchable
= 0, prompted
= 0;
319 for (i
= 0; !done
&& i
< ncmdline
; i
++) {
320 done
= qemuio_command(qemuio_bs
, cmdline
[i
]);
329 printf("%s", get_prompt());
331 qemu_set_fd_handler(STDIN_FILENO
, prep_fetchline
, NULL
, &fetchable
);
335 main_loop_wait(false);
345 done
= qemuio_command(qemuio_bs
, input
);
351 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
354 static void add_user_command(char *optarg
)
356 cmdline
= g_renew(char *, cmdline
, ++ncmdline
);
357 cmdline
[ncmdline
-1] = optarg
;
360 static void reenable_tty_echo(void)
362 qemu_set_tty_echo(STDIN_FILENO
, true);
365 int main(int argc
, char **argv
)
369 const char *sopt
= "hVc:d:rsnmgkt:T:";
370 const struct option lopt
[] = {
371 { "help", 0, NULL
, 'h' },
372 { "version", 0, NULL
, 'V' },
373 { "offset", 1, NULL
, 'o' },
374 { "cmd", 1, NULL
, 'c' },
375 { "read-only", 0, NULL
, 'r' },
376 { "snapshot", 0, NULL
, 's' },
377 { "nocache", 0, NULL
, 'n' },
378 { "misalign", 0, NULL
, 'm' },
379 { "growable", 0, NULL
, 'g' },
380 { "native-aio", 0, NULL
, 'k' },
381 { "discard", 1, NULL
, 'd' },
382 { "cache", 1, NULL
, 't' },
383 { "trace", 1, NULL
, 'T' },
388 int flags
= BDRV_O_UNMAP
;
389 Error
*local_error
= NULL
;
392 signal(SIGPIPE
, SIG_IGN
);
395 progname
= basename(argv
[0]);
396 qemu_init_exec_dir(argv
[0]);
398 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
401 flags
|= BDRV_O_SNAPSHOT
;
404 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
407 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
408 error_report("Invalid discard option: %s", optarg
);
413 add_user_command(optarg
);
419 qemuio_misalign
= true;
425 flags
|= BDRV_O_NATIVE_AIO
;
428 if (bdrv_parse_cache_flags(optarg
, &flags
) < 0) {
429 error_report("Invalid cache option: %s", optarg
);
434 if (!trace_init_backends(optarg
, NULL
)) {
435 exit(1); /* error message will have been printed */
439 printf("%s version %s\n", progname
, QEMU_VERSION
);
450 if ((argc
- optind
) > 1) {
455 if (qemu_init_main_loop(&local_error
)) {
456 error_report("%s", error_get_pretty(local_error
));
457 error_free(local_error
);
462 /* initialize commands */
463 qemuio_add_command(&quit_cmd
);
464 qemuio_add_command(&open_cmd
);
465 qemuio_add_command(&close_cmd
);
467 if (isatty(STDIN_FILENO
)) {
468 readline_state
= readline_init(readline_printf_func
,
471 readline_completion_func
);
472 qemu_set_tty_echo(STDIN_FILENO
, false);
473 atexit(reenable_tty_echo
);
476 /* open the device */
478 flags
|= BDRV_O_RDWR
;
481 if ((argc
- optind
) == 1) {
482 openfile(argv
[optind
], flags
, growable
, NULL
);
487 * Make sure all outstanding requests complete before the program exits.
492 bdrv_unref(qemuio_bs
);
494 blk_unref(qemuio_blk
);
495 g_free(readline_state
);