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 qemuio_command_usage(&open_cmd
);
170 return openfile(argv
[optind
], flags
, growable
, opts
);
173 static int quit_f(BlockDriverState
*bs
, int argc
, char **argv
)
178 static const cmdinfo_t quit_cmd
= {
184 .flags
= CMD_FLAG_GLOBAL
,
185 .oneline
= "exit the program",
188 static void usage(const char *name
)
191 "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
192 "QEMU Disk exerciser\n"
194 " -c, --cmd command to execute\n"
195 " -r, --read-only export read-only\n"
196 " -s, --snapshot use snapshot file\n"
197 " -n, --nocache disable host cache\n"
198 " -g, --growable allow file to grow (only applies to protocols)\n"
199 " -m, --misalign misalign allocations for O_DIRECT\n"
200 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
201 " -t, --cache=MODE use the given cache mode for the image\n"
202 " -T, --trace FILE enable trace events listed in the given file\n"
203 " -h, --help display this help and exit\n"
204 " -V, --version output version information and exit\n"
209 static char *get_prompt(void)
211 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
214 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
220 static void readline_printf_func(void *opaque
, 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_bs
, 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_bs
, input
);
337 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
340 static void add_user_command(char *optarg
)
342 cmdline
= g_realloc(cmdline
, ++ncmdline
* sizeof(char *));
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
)
355 const char *sopt
= "hVc:d:rsnmgkt:T:";
356 const struct option lopt
[] = {
357 { "help", 0, NULL
, 'h' },
358 { "version", 0, NULL
, 'V' },
359 { "offset", 1, NULL
, 'o' },
360 { "cmd", 1, NULL
, 'c' },
361 { "read-only", 0, NULL
, 'r' },
362 { "snapshot", 0, NULL
, 's' },
363 { "nocache", 0, NULL
, 'n' },
364 { "misalign", 0, NULL
, 'm' },
365 { "growable", 0, NULL
, 'g' },
366 { "native-aio", 0, NULL
, 'k' },
367 { "discard", 1, NULL
, 'd' },
368 { "cache", 1, NULL
, 't' },
369 { "trace", 1, NULL
, 'T' },
374 int flags
= BDRV_O_UNMAP
;
377 signal(SIGPIPE
, SIG_IGN
);
380 progname
= basename(argv
[0]);
382 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
385 flags
|= BDRV_O_SNAPSHOT
;
388 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
391 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
392 error_report("Invalid discard option: %s", optarg
);
397 add_user_command(optarg
);
409 flags
|= BDRV_O_NATIVE_AIO
;
412 if (bdrv_parse_cache_flags(optarg
, &flags
) < 0) {
413 error_report("Invalid cache option: %s", optarg
);
418 if (!trace_backend_init(optarg
, NULL
)) {
419 exit(1); /* error message will have been printed */
423 printf("%s version %s\n", progname
, QEMU_VERSION
);
434 if ((argc
- optind
) > 1) {
439 qemu_init_main_loop();
442 /* initialize commands */
443 qemuio_add_command(&quit_cmd
);
444 qemuio_add_command(&open_cmd
);
445 qemuio_add_command(&close_cmd
);
447 if (isatty(STDIN_FILENO
)) {
448 readline_state
= readline_init(readline_printf_func
,
451 readline_completion_func
);
452 qemu_set_tty_echo(STDIN_FILENO
, false);
453 atexit(reenable_tty_echo
);
456 /* open the device */
458 flags
|= BDRV_O_RDWR
;
461 if ((argc
- optind
) == 1) {
462 openfile(argv
[optind
], flags
, growable
, NULL
);
467 * Make sure all outstanding requests complete before the program exits.
472 bdrv_unref(qemuio_bs
);
474 g_free(readline_state
);