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 "qemu/osdep.h"
18 #include "qapi/error.h"
20 #include "qemu/error-report.h"
21 #include "qemu/main-loop.h"
22 #include "qemu/option.h"
23 #include "qemu/config-file.h"
24 #include "qemu/readline.h"
26 #include "qapi/qmp/qstring.h"
27 #include "qapi/qmp/qdict.h"
28 #include "qom/object_interfaces.h"
29 #include "sysemu/block-backend.h"
30 #include "block/block_int.h"
31 #include "trace/control.h"
32 #include "crypto/init.h"
33 #include "qemu-version.h"
35 #define CMD_NOFILE_OK 0x01
37 static char *progname
;
39 static BlockBackend
*qemuio_blk
;
41 /* qemu-io commands passed using -c */
43 static char **cmdline
;
44 static bool imageOpts
;
46 static ReadLineState
*readline_state
;
50 static int get_eof_char(void)
53 return 0x4; /* Ctrl-D */
56 if (tcgetattr(STDIN_FILENO
, &tty
) != 0) {
57 if (errno
== ENOTTY
) {
58 return 0x0; /* just expect read() == 0 */
60 return 0x4; /* Ctrl-D */
64 return tty
.c_cc
[VEOF
];
68 static int close_f(BlockBackend
*blk
, int argc
, char **argv
)
70 blk_unref(qemuio_blk
);
75 static const cmdinfo_t close_cmd
= {
79 .oneline
= "close the current open file",
82 static int openfile(char *name
, int flags
, bool writethrough
, bool force_share
,
85 Error
*local_err
= NULL
;
88 error_report("file open already, try 'help close'");
97 if (qdict_haskey(opts
, BDRV_OPT_FORCE_SHARE
)
98 && !qdict_get_bool(opts
, BDRV_OPT_FORCE_SHARE
)) {
99 error_report("-U conflicts with image options");
103 qdict_put_bool(opts
, BDRV_OPT_FORCE_SHARE
, true);
105 qemuio_blk
= blk_new_open(name
, NULL
, opts
, flags
, &local_err
);
107 error_reportf_err(local_err
, "can't open%s%s: ",
108 name
? " device " : "", name
?: "");
112 blk_set_enable_write_cache(qemuio_blk
, !writethrough
);
117 static void open_help(void)
121 " opens a new file in the requested mode\n"
124 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
126 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
127 " -r, -- open file read-only\n"
128 " -s, -- use snapshot file\n"
129 " -C, -- use copy-on-read\n"
130 " -n, -- disable host cache, short for -t none\n"
131 " -U, -- force shared permissions\n"
132 " -k, -- use kernel AIO implementation (on Linux only)\n"
133 " -t, -- use the given cache mode for the image\n"
134 " -d, -- use the given discard mode for the image\n"
135 " -o, -- options to be given to the block driver"
139 static int open_f(BlockBackend
*blk
, int argc
, char **argv
);
141 static const cmdinfo_t open_cmd
= {
147 .flags
= CMD_NOFILE_OK
,
148 .args
= "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
149 .oneline
= "open the file specified by path",
153 static QemuOptsList empty_opts
= {
156 .head
= QTAILQ_HEAD_INITIALIZER(empty_opts
.head
),
158 /* no elements => accept any params */
159 { /* end of list */ }
163 static int open_f(BlockBackend
*blk
, int argc
, char **argv
)
165 int flags
= BDRV_O_UNMAP
;
167 bool writethrough
= true;
171 bool force_share
= false;
173 while ((c
= getopt(argc
, argv
, "snCro:kt:d:U")) != -1) {
176 flags
|= BDRV_O_SNAPSHOT
;
179 flags
|= BDRV_O_NOCACHE
;
180 writethrough
= false;
183 flags
|= BDRV_O_COPY_ON_READ
;
189 flags
|= BDRV_O_NATIVE_AIO
;
192 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
193 error_report("Invalid cache option: %s", optarg
);
194 qemu_opts_reset(&empty_opts
);
199 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
200 error_report("Invalid discard option: %s", optarg
);
201 qemu_opts_reset(&empty_opts
);
207 printf("--image-opts and 'open -o' are mutually exclusive\n");
208 qemu_opts_reset(&empty_opts
);
211 if (!qemu_opts_parse_noisily(&empty_opts
, optarg
, false)) {
212 qemu_opts_reset(&empty_opts
);
220 qemu_opts_reset(&empty_opts
);
221 return qemuio_command_usage(&open_cmd
);
226 flags
|= BDRV_O_RDWR
;
229 if (imageOpts
&& (optind
== argc
- 1)) {
230 if (!qemu_opts_parse_noisily(&empty_opts
, argv
[optind
], false)) {
231 qemu_opts_reset(&empty_opts
);
237 qopts
= qemu_opts_find(&empty_opts
, NULL
);
238 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
239 qemu_opts_reset(&empty_opts
);
241 if (optind
== argc
- 1) {
242 openfile(argv
[optind
], flags
, writethrough
, force_share
, opts
);
243 } else if (optind
== argc
) {
244 openfile(NULL
, flags
, writethrough
, force_share
, opts
);
247 qemuio_command_usage(&open_cmd
);
252 static int quit_f(BlockBackend
*blk
, int argc
, char **argv
)
257 #if defined(CONFIG_FVD)
258 # include "qemu-io-sim.c"
261 static const cmdinfo_t quit_cmd
= {
267 .flags
= CMD_FLAG_GLOBAL
,
268 .oneline
= "exit the program",
271 static void usage(const char *name
)
274 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
275 "QEMU Disk exerciser\n"
277 " --object OBJECTDEF define an object such as 'secret' for\n"
278 " passwords and/or encryption keys\n"
279 " --image-opts treat file as option string\n"
280 " -c, --cmd STRING execute command with its arguments\n"
281 " from the given string\n"
282 " -f, --format FMT specifies the block driver to use\n"
283 " -r, --read-only export read-only\n"
284 " -s, --snapshot use snapshot file\n"
285 " -n, --nocache disable host cache, short for -t none\n"
286 " -C, --copy-on-read enable copy-on-read\n"
287 " -m, --misalign misalign allocations for O_DIRECT\n"
288 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
289 " -t, --cache=MODE use the given cache mode for the image\n"
290 " -d, --discard=MODE use the given discard mode for the image\n"
291 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
292 " specify tracing options\n"
293 " see qemu-img(1) man page for full description\n"
294 " -U, --force-share force shared permissions\n"
295 " -h, --help display this help and exit\n"
296 " -V, --version output version information and exit\n"
298 "See '%s -c help' for information on available commands.\n"
300 QEMU_HELP_BOTTOM
"\n",
304 static char *get_prompt(void)
306 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
309 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
315 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque
,
316 const char *fmt
, ...)
324 static void readline_flush_func(void *opaque
)
329 static void readline_func(void *opaque
, const char *str
, void *readline_opaque
)
331 char **line
= readline_opaque
;
332 *line
= g_strdup(str
);
335 static void completion_match(const char *cmd
, void *opaque
)
337 readline_add_completion(readline_state
, cmd
);
340 static void readline_completion_func(void *opaque
, const char *str
)
342 readline_set_completion_index(readline_state
, strlen(str
));
343 qemuio_complete_command(str
, completion_match
, NULL
);
346 static char *fetchline_readline(void)
350 readline_start(readline_state
, get_prompt(), 0, readline_func
, &line
);
353 if (ttyEOF
!= 0x0 && ch
== ttyEOF
) {
357 readline_handle_byte(readline_state
, ch
);
362 #define MAXREADLINESZ 1024
363 static char *fetchline_fgets(void)
365 char *p
, *line
= g_malloc(MAXREADLINESZ
);
367 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
372 p
= line
+ strlen(line
);
373 if (p
!= line
&& p
[-1] == '\n') {
380 static char *fetchline(void)
382 if (readline_state
) {
383 return fetchline_readline();
385 return fetchline_fgets();
389 static void prep_fetchline(void *opaque
)
391 int *fetchable
= opaque
;
393 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
397 static void command_loop(void)
399 int i
, done
= 0, fetchable
= 0, prompted
= 0;
402 for (i
= 0; !done
&& i
< ncmdline
; i
++) {
403 done
= qemuio_command(qemuio_blk
, cmdline
[i
]);
412 printf("%s", get_prompt());
414 qemu_set_fd_handler(STDIN_FILENO
, prep_fetchline
, NULL
, &fetchable
);
418 main_loop_wait(false);
428 done
= qemuio_command(qemuio_blk
, input
);
434 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
437 static void add_user_command(char *optarg
)
439 cmdline
= g_renew(char *, cmdline
, ++ncmdline
);
440 cmdline
[ncmdline
-1] = optarg
;
443 static void reenable_tty_echo(void)
445 qemu_set_tty_echo(STDIN_FILENO
, true);
450 OPTION_IMAGE_OPTS
= 257,
453 static QemuOptsList qemu_object_opts
= {
455 .implied_opt_name
= "qom-type",
456 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
463 static QemuOptsList file_opts
= {
465 .implied_opt_name
= "file",
466 .head
= QTAILQ_HEAD_INITIALIZER(file_opts
.head
),
468 /* no elements => accept any params */
469 { /* end of list */ }
473 int main(int argc
, char **argv
)
476 const char *sopt
= "hVc:d:f:rsnCmkt:T:U";
477 const struct option lopt
[] = {
478 { "help", no_argument
, NULL
, 'h' },
479 { "version", no_argument
, NULL
, 'V' },
480 { "cmd", required_argument
, NULL
, 'c' },
481 { "format", required_argument
, NULL
, 'f' },
482 { "read-only", no_argument
, NULL
, 'r' },
483 { "snapshot", no_argument
, NULL
, 's' },
484 { "nocache", no_argument
, NULL
, 'n' },
485 { "copy-on-read", no_argument
, NULL
, 'C' },
486 { "misalign", no_argument
, NULL
, 'm' },
487 { "native-aio", no_argument
, NULL
, 'k' },
488 { "discard", required_argument
, NULL
, 'd' },
489 { "cache", required_argument
, NULL
, 't' },
490 { "trace", required_argument
, NULL
, 'T' },
491 { "object", required_argument
, NULL
, OPTION_OBJECT
},
492 { "image-opts", no_argument
, NULL
, OPTION_IMAGE_OPTS
},
493 { "force-share", no_argument
, 0, 'U'},
498 int flags
= BDRV_O_UNMAP
;
499 bool writethrough
= true;
500 Error
*local_error
= NULL
;
502 const char *format
= NULL
;
503 char *trace_file
= NULL
;
504 bool force_share
= false;
507 signal(SIGPIPE
, SIG_IGN
);
510 module_call_init(MODULE_INIT_TRACE
);
511 progname
= g_path_get_basename(argv
[0]);
512 qemu_init_exec_dir(argv
[0]);
514 qcrypto_init(&error_fatal
);
516 module_call_init(MODULE_INIT_QOM
);
517 qemu_add_opts(&qemu_object_opts
);
518 qemu_add_opts(&qemu_trace_opts
);
521 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
524 flags
|= BDRV_O_SNAPSHOT
;
527 flags
|= BDRV_O_NOCACHE
;
528 writethrough
= false;
531 flags
|= BDRV_O_COPY_ON_READ
;
534 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
535 error_report("Invalid discard option: %s", optarg
);
543 add_user_command(optarg
);
549 qemuio_misalign
= true;
552 flags
|= BDRV_O_NATIVE_AIO
;
555 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
556 error_report("Invalid cache option: %s", optarg
);
562 trace_file
= trace_opt_parse(optarg
);
565 printf("%s version " QEMU_FULL_VERSION
"\n"
566 QEMU_COPYRIGHT
"\n", progname
);
574 case OPTION_OBJECT
: {
576 qopts
= qemu_opts_parse_noisily(&qemu_object_opts
,
582 case OPTION_IMAGE_OPTS
:
591 if ((argc
- optind
) > 1) {
596 if (format
&& imageOpts
) {
597 error_report("--image-opts and -f are mutually exclusive");
601 if (qemu_init_main_loop(&local_error
)) {
602 error_report_err(local_error
);
606 if (qemu_opts_foreach(&qemu_object_opts
,
607 user_creatable_add_opts_foreach
,
612 if (!trace_init_backends()) {
615 trace_init_file(trace_file
);
616 qemu_set_log(LOG_TRACE
);
618 /* initialize commands */
619 qemuio_add_command(&quit_cmd
);
620 qemuio_add_command(&open_cmd
);
621 qemuio_add_command(&close_cmd
);
623 if (isatty(STDIN_FILENO
)) {
624 ttyEOF
= get_eof_char();
625 readline_state
= readline_init(readline_printf_func
,
628 readline_completion_func
);
629 qemu_set_tty_echo(STDIN_FILENO
, false);
630 atexit(reenable_tty_echo
);
633 /* open the device */
635 flags
|= BDRV_O_RDWR
;
638 if ((argc
- optind
) == 1) {
640 QemuOpts
*qopts
= NULL
;
641 qopts
= qemu_opts_parse_noisily(&file_opts
, argv
[optind
], false);
645 opts
= qemu_opts_to_qdict(qopts
, NULL
);
646 if (openfile(NULL
, flags
, writethrough
, force_share
, opts
)) {
652 qdict_put_str(opts
, "driver", format
);
654 if (openfile(argv
[optind
], flags
, writethrough
,
655 force_share
, opts
)) {
663 * Make sure all outstanding requests complete before the program exits.
667 blk_unref(qemuio_blk
);
668 g_free(readline_state
);