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
29 BlockDriverState
*qemuio_bs
;
30 extern int qemuio_misalign
;
32 /* qemu-io commands passed using -c */
34 static char **cmdline
;
36 static ReadLineState
*readline_state
;
38 static int close_f(BlockDriverState
*bs
, int argc
, char **argv
)
45 static const cmdinfo_t close_cmd
= {
49 .oneline
= "close the current open file",
52 static int openfile(char *name
, int flags
, int growable
, QDict
*opts
)
54 Error
*local_err
= NULL
;
57 fprintf(stderr
, "file open already, try 'help close'\n");
62 if (bdrv_file_open(&qemuio_bs
, name
, NULL
, opts
, flags
, &local_err
)) {
63 fprintf(stderr
, "%s: can't open device %s: %s\n", progname
, name
,
64 error_get_pretty(local_err
));
65 error_free(local_err
);
69 qemuio_bs
= bdrv_new("hda");
71 if (bdrv_open(qemuio_bs
, name
, opts
, flags
, NULL
, &local_err
) < 0) {
72 fprintf(stderr
, "%s: can't open device %s: %s\n", progname
, name
,
73 error_get_pretty(local_err
));
74 error_free(local_err
);
75 bdrv_unref(qemuio_bs
);
84 static void open_help(void)
88 " opens a new file in the requested mode\n"
91 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
93 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
94 " -r, -- open file read-only\n"
95 " -s, -- use snapshot file\n"
96 " -n, -- disable host cache\n"
97 " -g, -- allow file to grow (only applies to protocols)\n"
98 " -o, -- options to be given to the block driver"
102 static int open_f(BlockDriverState
*bs
, int argc
, char **argv
);
104 static const cmdinfo_t open_cmd
= {
110 .flags
= CMD_NOFILE_OK
,
111 .args
= "[-Crsn] [-o options] [path]",
112 .oneline
= "open the file specified by path",
116 static QemuOptsList empty_opts
= {
118 .head
= QTAILQ_HEAD_INITIALIZER(empty_opts
.head
),
120 /* no elements => accept any params */
121 { /* end of list */ }
125 static int open_f(BlockDriverState
*bs
, int argc
, char **argv
)
134 while ((c
= getopt(argc
, argv
, "snrgo:")) != EOF
) {
137 flags
|= BDRV_O_SNAPSHOT
;
140 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
149 qopts
= qemu_opts_parse(&empty_opts
, optarg
, 0);
151 printf("could not parse option list -- %s\n", optarg
);
154 opts
= qemu_opts_to_qdict(qopts
, opts
);
155 qemu_opts_del(qopts
);
158 return qemuio_command_usage(&open_cmd
);
163 flags
|= BDRV_O_RDWR
;
166 if (optind
== argc
- 1) {
167 return openfile(argv
[optind
], flags
, growable
, opts
);
168 } else if (optind
== argc
) {
169 return openfile(NULL
, flags
, growable
, opts
);
171 return qemuio_command_usage(&open_cmd
);
175 static int quit_f(BlockDriverState
*bs
, int argc
, char **argv
)
180 static const cmdinfo_t quit_cmd
= {
186 .flags
= CMD_FLAG_GLOBAL
,
187 .oneline
= "exit the program",
190 static void usage(const char *name
)
193 "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
194 "QEMU Disk exerciser\n"
196 " -c, --cmd command to execute\n"
197 " -r, --read-only export read-only\n"
198 " -s, --snapshot use snapshot file\n"
199 " -n, --nocache disable host cache\n"
200 " -g, --growable allow file to grow (only applies to protocols)\n"
201 " -m, --misalign misalign allocations for O_DIRECT\n"
202 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
203 " -t, --cache=MODE use the given cache mode for the image\n"
204 " -T, --trace FILE enable trace events listed in the given file\n"
205 " -h, --help display this help and exit\n"
206 " -V, --version output version information and exit\n"
211 static char *get_prompt(void)
213 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
216 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
222 static void readline_printf_func(void *opaque
, const char *fmt
, ...)
230 static void readline_flush_func(void *opaque
)
235 static void readline_func(void *opaque
, const char *str
, void *readline_opaque
)
237 char **line
= readline_opaque
;
238 *line
= g_strdup(str
);
241 static void completion_match(const char *cmd
, void *opaque
)
243 readline_add_completion(readline_state
, cmd
);
246 static void readline_completion_func(void *opaque
, const char *str
)
248 readline_set_completion_index(readline_state
, strlen(str
));
249 qemuio_complete_command(str
, completion_match
, NULL
);
252 static char *fetchline_readline(void)
256 readline_start(readline_state
, get_prompt(), 0, readline_func
, &line
);
262 readline_handle_byte(readline_state
, ch
);
267 #define MAXREADLINESZ 1024
268 static char *fetchline_fgets(void)
270 char *p
, *line
= g_malloc(MAXREADLINESZ
);
272 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
277 p
= line
+ strlen(line
);
278 if (p
!= line
&& p
[-1] == '\n') {
285 static char *fetchline(void)
287 if (readline_state
) {
288 return fetchline_readline();
290 return fetchline_fgets();
294 static void prep_fetchline(void *opaque
)
296 int *fetchable
= opaque
;
298 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
302 static void command_loop(void)
304 int i
, done
= 0, fetchable
= 0, prompted
= 0;
307 for (i
= 0; !done
&& i
< ncmdline
; i
++) {
308 done
= qemuio_command(qemuio_bs
, cmdline
[i
]);
317 printf("%s", get_prompt());
319 qemu_set_fd_handler(STDIN_FILENO
, prep_fetchline
, NULL
, &fetchable
);
323 main_loop_wait(false);
333 done
= qemuio_command(qemuio_bs
, input
);
339 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
342 static void add_user_command(char *optarg
)
344 cmdline
= g_realloc(cmdline
, ++ncmdline
* sizeof(char *));
345 cmdline
[ncmdline
-1] = optarg
;
348 static void reenable_tty_echo(void)
350 qemu_set_tty_echo(STDIN_FILENO
, true);
353 int main(int argc
, char **argv
)
357 const char *sopt
= "hVc:d:rsnmgkt:T:";
358 const struct option lopt
[] = {
359 { "help", 0, NULL
, 'h' },
360 { "version", 0, NULL
, 'V' },
361 { "offset", 1, NULL
, 'o' },
362 { "cmd", 1, NULL
, 'c' },
363 { "read-only", 0, NULL
, 'r' },
364 { "snapshot", 0, NULL
, 's' },
365 { "nocache", 0, NULL
, 'n' },
366 { "misalign", 0, NULL
, 'm' },
367 { "growable", 0, NULL
, 'g' },
368 { "native-aio", 0, NULL
, 'k' },
369 { "discard", 1, NULL
, 'd' },
370 { "cache", 1, NULL
, 't' },
371 { "trace", 1, NULL
, 'T' },
376 int flags
= BDRV_O_UNMAP
;
379 signal(SIGPIPE
, SIG_IGN
);
382 progname
= basename(argv
[0]);
384 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
387 flags
|= BDRV_O_SNAPSHOT
;
390 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
393 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
394 error_report("Invalid discard option: %s", optarg
);
399 add_user_command(optarg
);
411 flags
|= BDRV_O_NATIVE_AIO
;
414 if (bdrv_parse_cache_flags(optarg
, &flags
) < 0) {
415 error_report("Invalid cache option: %s", optarg
);
420 if (!trace_backend_init(optarg
, NULL
)) {
421 exit(1); /* error message will have been printed */
425 printf("%s version %s\n", progname
, QEMU_VERSION
);
436 if ((argc
- optind
) > 1) {
441 qemu_init_main_loop();
444 /* initialize commands */
445 qemuio_add_command(&quit_cmd
);
446 qemuio_add_command(&open_cmd
);
447 qemuio_add_command(&close_cmd
);
449 if (isatty(STDIN_FILENO
)) {
450 readline_state
= readline_init(readline_printf_func
,
453 readline_completion_func
);
454 qemu_set_tty_echo(STDIN_FILENO
, false);
455 atexit(reenable_tty_echo
);
458 /* open the device */
460 flags
|= BDRV_O_RDWR
;
463 if ((argc
- optind
) == 1) {
464 openfile(argv
[optind
], flags
, growable
, NULL
);
469 * Make sure all outstanding requests complete before the program exits.
474 bdrv_unref(qemuio_bs
);
476 g_free(readline_state
);