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.
10 #include "qemu/osdep.h"
15 #include "qemu/error-report.h"
16 #include "qemu/main-loop.h"
17 #include "qemu/option.h"
18 #include "qemu/config-file.h"
19 #include "qemu/readline.h"
20 #include "qapi/qmp/qstring.h"
21 #include "qom/object_interfaces.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
;
32 /* qemu-io commands passed using -c */
34 static char **cmdline
;
35 static bool imageOpts
;
37 static ReadLineState
*readline_state
;
39 static int close_f(BlockBackend
*blk
, int argc
, char **argv
)
41 blk_unref(qemuio_blk
);
46 static const cmdinfo_t close_cmd
= {
50 .oneline
= "close the current open file",
53 static int openfile(char *name
, int flags
, QDict
*opts
)
55 Error
*local_err
= NULL
;
59 error_report("file open already, try 'help close'");
64 qemuio_blk
= blk_new_open(name
, NULL
, opts
, flags
, &local_err
);
66 error_reportf_err(local_err
, "can't open%s%s: ",
67 name
? " device " : "", name
?: "");
71 bs
= blk_bs(qemuio_blk
);
72 if (bdrv_is_encrypted(bs
)) {
74 printf("Disk image '%s' is encrypted.\n", name
);
75 if (qemu_read_password(password
, sizeof(password
)) < 0) {
76 error_report("No password given");
79 if (bdrv_set_key(bs
, password
) < 0) {
80 error_report("invalid password");
89 blk_unref(qemuio_blk
);
94 static void open_help(void)
98 " opens a new file in the requested mode\n"
101 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
103 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
104 " -r, -- open file read-only\n"
105 " -s, -- use snapshot file\n"
106 " -n, -- disable host cache\n"
107 " -o, -- options to be given to the block driver"
111 static int open_f(BlockBackend
*blk
, int argc
, char **argv
);
113 static const cmdinfo_t open_cmd
= {
119 .flags
= CMD_NOFILE_OK
,
120 .args
= "[-Crsn] [-o options] [path]",
121 .oneline
= "open the file specified by path",
125 static QemuOptsList empty_opts
= {
128 .head
= QTAILQ_HEAD_INITIALIZER(empty_opts
.head
),
130 /* no elements => accept any params */
131 { /* end of list */ }
135 static int open_f(BlockBackend
*blk
, int argc
, char **argv
)
143 while ((c
= getopt(argc
, argv
, "snrgo:")) != -1) {
146 flags
|= BDRV_O_SNAPSHOT
;
149 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
156 printf("--image-opts and 'open -o' are mutually exclusive\n");
159 if (!qemu_opts_parse_noisily(&empty_opts
, optarg
, false)) {
160 qemu_opts_reset(&empty_opts
);
165 qemu_opts_reset(&empty_opts
);
166 return qemuio_command_usage(&open_cmd
);
171 flags
|= BDRV_O_RDWR
;
174 if (imageOpts
&& (optind
== argc
- 1)) {
175 if (!qemu_opts_parse_noisily(&empty_opts
, argv
[optind
], false)) {
176 qemu_opts_reset(&empty_opts
);
182 qopts
= qemu_opts_find(&empty_opts
, NULL
);
183 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
184 qemu_opts_reset(&empty_opts
);
186 if (optind
== argc
- 1) {
187 return openfile(argv
[optind
], flags
, opts
);
188 } else if (optind
== argc
) {
189 return openfile(NULL
, flags
, opts
);
192 return qemuio_command_usage(&open_cmd
);
196 static int quit_f(BlockBackend
*blk
, int argc
, char **argv
)
201 static const cmdinfo_t quit_cmd
= {
207 .flags
= CMD_FLAG_GLOBAL
,
208 .oneline
= "exit the program",
211 static void usage(const char *name
)
214 "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
215 "QEMU Disk exerciser\n"
217 " --object OBJECTDEF define an object such as 'secret' for\n"
218 " passwords and/or encryption keys\n"
219 " -c, --cmd STRING execute command with its arguments\n"
220 " from the given string\n"
221 " -f, --format FMT specifies the block driver to use\n"
222 " -r, --read-only export read-only\n"
223 " -s, --snapshot use snapshot file\n"
224 " -n, --nocache disable host cache\n"
225 " -m, --misalign misalign allocations for O_DIRECT\n"
226 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
227 " -t, --cache=MODE use the given cache mode for the image\n"
228 " -T, --trace FILE enable trace events listed in the given file\n"
229 " -h, --help display this help and exit\n"
230 " -V, --version output version information and exit\n"
232 "See '%s -c help' for information on available commands."
237 static char *get_prompt(void)
239 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
242 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
248 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque
,
249 const char *fmt
, ...)
257 static void readline_flush_func(void *opaque
)
262 static void readline_func(void *opaque
, const char *str
, void *readline_opaque
)
264 char **line
= readline_opaque
;
265 *line
= g_strdup(str
);
268 static void completion_match(const char *cmd
, void *opaque
)
270 readline_add_completion(readline_state
, cmd
);
273 static void readline_completion_func(void *opaque
, const char *str
)
275 readline_set_completion_index(readline_state
, strlen(str
));
276 qemuio_complete_command(str
, completion_match
, NULL
);
279 static char *fetchline_readline(void)
283 readline_start(readline_state
, get_prompt(), 0, readline_func
, &line
);
289 readline_handle_byte(readline_state
, ch
);
294 #define MAXREADLINESZ 1024
295 static char *fetchline_fgets(void)
297 char *p
, *line
= g_malloc(MAXREADLINESZ
);
299 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
304 p
= line
+ strlen(line
);
305 if (p
!= line
&& p
[-1] == '\n') {
312 static char *fetchline(void)
314 if (readline_state
) {
315 return fetchline_readline();
317 return fetchline_fgets();
321 static void prep_fetchline(void *opaque
)
323 int *fetchable
= opaque
;
325 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
329 static void command_loop(void)
331 int i
, done
= 0, fetchable
= 0, prompted
= 0;
334 for (i
= 0; !done
&& i
< ncmdline
; i
++) {
335 done
= qemuio_command(qemuio_blk
, cmdline
[i
]);
344 printf("%s", get_prompt());
346 qemu_set_fd_handler(STDIN_FILENO
, prep_fetchline
, NULL
, &fetchable
);
350 main_loop_wait(false);
360 done
= qemuio_command(qemuio_blk
, input
);
366 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
369 static void add_user_command(char *optarg
)
371 cmdline
= g_renew(char *, cmdline
, ++ncmdline
);
372 cmdline
[ncmdline
-1] = optarg
;
375 static void reenable_tty_echo(void)
377 qemu_set_tty_echo(STDIN_FILENO
, true);
382 OPTION_IMAGE_OPTS
= 257,
385 static QemuOptsList qemu_object_opts
= {
387 .implied_opt_name
= "qom-type",
388 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
395 static QemuOptsList file_opts
= {
397 .implied_opt_name
= "file",
398 .head
= QTAILQ_HEAD_INITIALIZER(file_opts
.head
),
400 /* no elements => accept any params */
401 { /* end of list */ }
405 int main(int argc
, char **argv
)
408 const char *sopt
= "hVc:d:f:rsnmgkt:T:";
409 const struct option lopt
[] = {
410 { "help", no_argument
, NULL
, 'h' },
411 { "version", no_argument
, NULL
, 'V' },
412 { "offset", required_argument
, NULL
, 'o' },
413 { "cmd", required_argument
, NULL
, 'c' },
414 { "format", required_argument
, NULL
, 'f' },
415 { "read-only", no_argument
, NULL
, 'r' },
416 { "snapshot", no_argument
, NULL
, 's' },
417 { "nocache", no_argument
, NULL
, 'n' },
418 { "misalign", no_argument
, NULL
, 'm' },
419 { "native-aio", no_argument
, NULL
, 'k' },
420 { "discard", required_argument
, NULL
, 'd' },
421 { "cache", required_argument
, NULL
, 't' },
422 { "trace", required_argument
, NULL
, 'T' },
423 { "object", required_argument
, NULL
, OPTION_OBJECT
},
424 { "image-opts", no_argument
, NULL
, OPTION_IMAGE_OPTS
},
429 int flags
= BDRV_O_UNMAP
;
430 Error
*local_error
= NULL
;
432 const char *format
= NULL
;
435 signal(SIGPIPE
, SIG_IGN
);
438 progname
= basename(argv
[0]);
439 qemu_init_exec_dir(argv
[0]);
441 module_call_init(MODULE_INIT_QOM
);
442 qemu_add_opts(&qemu_object_opts
);
445 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
448 flags
|= BDRV_O_SNAPSHOT
;
451 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
454 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
455 error_report("Invalid discard option: %s", optarg
);
463 add_user_command(optarg
);
469 qemuio_misalign
= true;
472 flags
|= BDRV_O_NATIVE_AIO
;
475 if (bdrv_parse_cache_flags(optarg
, &flags
) < 0) {
476 error_report("Invalid cache option: %s", optarg
);
481 if (!trace_init_backends()) {
482 exit(1); /* error message will have been printed */
486 printf("%s version %s\n", progname
, QEMU_VERSION
);
491 case OPTION_OBJECT
: {
493 qopts
= qemu_opts_parse_noisily(&qemu_object_opts
,
499 case OPTION_IMAGE_OPTS
:
508 if ((argc
- optind
) > 1) {
513 if (format
&& imageOpts
) {
514 error_report("--image-opts and -f are mutually exclusive");
518 if (qemu_init_main_loop(&local_error
)) {
519 error_report_err(local_error
);
523 if (qemu_opts_foreach(&qemu_object_opts
,
524 user_creatable_add_opts_foreach
,
525 NULL
, &local_error
)) {
526 error_report_err(local_error
);
530 /* initialize commands */
531 qemuio_add_command(&quit_cmd
);
532 qemuio_add_command(&open_cmd
);
533 qemuio_add_command(&close_cmd
);
535 if (isatty(STDIN_FILENO
)) {
536 readline_state
= readline_init(readline_printf_func
,
539 readline_completion_func
);
540 qemu_set_tty_echo(STDIN_FILENO
, false);
541 atexit(reenable_tty_echo
);
544 /* open the device */
546 flags
|= BDRV_O_RDWR
;
549 if ((argc
- optind
) == 1) {
551 QemuOpts
*qopts
= NULL
;
552 qopts
= qemu_opts_parse_noisily(&file_opts
, argv
[optind
], false);
556 opts
= qemu_opts_to_qdict(qopts
, NULL
);
557 openfile(NULL
, flags
, opts
);
561 qdict_put(opts
, "driver", qstring_from_str(format
));
563 openfile(argv
[optind
], flags
, opts
);
569 * Make sure all outstanding requests complete before the program exits.
573 blk_unref(qemuio_blk
);
574 g_free(readline_state
);