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
, QDict
*opts
)
56 Error
*local_err
= NULL
;
59 fprintf(stderr
, "file open already, try 'help close'\n");
64 qemuio_blk
= blk_new_open("hda", name
, NULL
, opts
, flags
, &local_err
);
66 fprintf(stderr
, "%s: can't open%s%s: %s\n", progname
,
67 name
? " device " : "", name
?: "",
68 error_get_pretty(local_err
));
69 error_free(local_err
);
72 qemuio_bs
= blk_bs(qemuio_blk
);
77 static void open_help(void)
81 " opens a new file in the requested mode\n"
84 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
86 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
87 " -r, -- open file read-only\n"
88 " -s, -- use snapshot file\n"
89 " -n, -- disable host cache\n"
90 " -o, -- options to be given to the block driver"
94 static int open_f(BlockDriverState
*bs
, int argc
, char **argv
);
96 static const cmdinfo_t open_cmd
= {
102 .flags
= CMD_NOFILE_OK
,
103 .args
= "[-Crsn] [-o options] [path]",
104 .oneline
= "open the file specified by path",
108 static QemuOptsList empty_opts
= {
111 .head
= QTAILQ_HEAD_INITIALIZER(empty_opts
.head
),
113 /* no elements => accept any params */
114 { /* end of list */ }
118 static int open_f(BlockDriverState
*bs
, int argc
, char **argv
)
126 while ((c
= getopt(argc
, argv
, "snrgo:")) != EOF
) {
129 flags
|= BDRV_O_SNAPSHOT
;
132 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
138 if (!qemu_opts_parse(&empty_opts
, optarg
, 0)) {
139 printf("could not parse option list -- %s\n", optarg
);
140 qemu_opts_reset(&empty_opts
);
145 qemu_opts_reset(&empty_opts
);
146 return qemuio_command_usage(&open_cmd
);
151 flags
|= BDRV_O_RDWR
;
154 qopts
= qemu_opts_find(&empty_opts
, NULL
);
155 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
156 qemu_opts_reset(&empty_opts
);
158 if (optind
== argc
- 1) {
159 return openfile(argv
[optind
], flags
, opts
);
160 } else if (optind
== argc
) {
161 return openfile(NULL
, flags
, opts
);
164 return qemuio_command_usage(&open_cmd
);
168 static int quit_f(BlockDriverState
*bs
, int argc
, char **argv
)
173 static const cmdinfo_t quit_cmd
= {
179 .flags
= CMD_FLAG_GLOBAL
,
180 .oneline
= "exit the program",
183 static void usage(const char *name
)
186 "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
187 "QEMU Disk exerciser\n"
189 " -c, --cmd STRING execute command with its arguments\n"
190 " from the given string\n"
191 " -f, --format FMT specifies the block driver to use\n"
192 " -r, --read-only export read-only\n"
193 " -s, --snapshot use snapshot file\n"
194 " -n, --nocache disable host cache\n"
195 " -m, --misalign misalign allocations for O_DIRECT\n"
196 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
197 " -t, --cache=MODE use the given cache mode for the image\n"
198 " -T, --trace FILE enable trace events listed in the given file\n"
199 " -h, --help display this help and exit\n"
200 " -V, --version output version information and exit\n"
202 "See '%s -c help' for information on available commands."
207 static char *get_prompt(void)
209 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
212 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
218 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque
,
219 const char *fmt
, ...)
227 static void readline_flush_func(void *opaque
)
232 static void readline_func(void *opaque
, const char *str
, void *readline_opaque
)
234 char **line
= readline_opaque
;
235 *line
= g_strdup(str
);
238 static void completion_match(const char *cmd
, void *opaque
)
240 readline_add_completion(readline_state
, cmd
);
243 static void readline_completion_func(void *opaque
, const char *str
)
245 readline_set_completion_index(readline_state
, strlen(str
));
246 qemuio_complete_command(str
, completion_match
, NULL
);
249 static char *fetchline_readline(void)
253 readline_start(readline_state
, get_prompt(), 0, readline_func
, &line
);
259 readline_handle_byte(readline_state
, ch
);
264 #define MAXREADLINESZ 1024
265 static char *fetchline_fgets(void)
267 char *p
, *line
= g_malloc(MAXREADLINESZ
);
269 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
274 p
= line
+ strlen(line
);
275 if (p
!= line
&& p
[-1] == '\n') {
282 static char *fetchline(void)
284 if (readline_state
) {
285 return fetchline_readline();
287 return fetchline_fgets();
291 static void prep_fetchline(void *opaque
)
293 int *fetchable
= opaque
;
295 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
299 static void command_loop(void)
301 int i
, done
= 0, fetchable
= 0, prompted
= 0;
304 for (i
= 0; !done
&& i
< ncmdline
; i
++) {
305 done
= qemuio_command(qemuio_bs
, cmdline
[i
]);
314 printf("%s", get_prompt());
316 qemu_set_fd_handler(STDIN_FILENO
, prep_fetchline
, NULL
, &fetchable
);
320 main_loop_wait(false);
330 done
= qemuio_command(qemuio_bs
, input
);
336 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
339 static void add_user_command(char *optarg
)
341 cmdline
= g_renew(char *, cmdline
, ++ncmdline
);
342 cmdline
[ncmdline
-1] = optarg
;
345 static void reenable_tty_echo(void)
347 qemu_set_tty_echo(STDIN_FILENO
, true);
350 int main(int argc
, char **argv
)
353 const char *sopt
= "hVc:d:f:rsnmgkt:T:";
354 const struct option lopt
[] = {
355 { "help", 0, NULL
, 'h' },
356 { "version", 0, NULL
, 'V' },
357 { "offset", 1, NULL
, 'o' },
358 { "cmd", 1, NULL
, 'c' },
359 { "format", 1, NULL
, 'f' },
360 { "read-only", 0, NULL
, 'r' },
361 { "snapshot", 0, NULL
, 's' },
362 { "nocache", 0, NULL
, 'n' },
363 { "misalign", 0, NULL
, 'm' },
364 { "native-aio", 0, NULL
, 'k' },
365 { "discard", 1, NULL
, 'd' },
366 { "cache", 1, NULL
, 't' },
367 { "trace", 1, NULL
, 'T' },
372 int flags
= BDRV_O_UNMAP
;
373 Error
*local_error
= NULL
;
377 signal(SIGPIPE
, SIG_IGN
);
380 progname
= basename(argv
[0]);
381 qemu_init_exec_dir(argv
[0]);
385 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
388 flags
|= BDRV_O_SNAPSHOT
;
391 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
394 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
395 error_report("Invalid discard option: %s", optarg
);
403 qdict_put(opts
, "driver", qstring_from_str(optarg
));
406 add_user_command(optarg
);
412 qemuio_misalign
= true;
415 flags
|= BDRV_O_NATIVE_AIO
;
418 if (bdrv_parse_cache_flags(optarg
, &flags
) < 0) {
419 error_report("Invalid cache option: %s", optarg
);
424 if (!trace_init_backends(optarg
, NULL
)) {
425 exit(1); /* error message will have been printed */
429 printf("%s version %s\n", progname
, QEMU_VERSION
);
440 if ((argc
- optind
) > 1) {
445 if (qemu_init_main_loop(&local_error
)) {
446 error_report("%s", error_get_pretty(local_error
));
447 error_free(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
);