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"
14 #include "qapi/error.h"
16 #include "qemu/error-report.h"
17 #include "qemu/main-loop.h"
18 #include "qemu/option.h"
19 #include "qemu/config-file.h"
20 #include "qemu/readline.h"
22 #include "qapi/qmp/qstring.h"
23 #include "qapi/qmp/qbool.h"
24 #include "qom/object_interfaces.h"
25 #include "sysemu/block-backend.h"
26 #include "block/block_int.h"
27 #include "trace/control.h"
28 #include "crypto/init.h"
29 #include "qemu-version.h"
31 #define CMD_NOFILE_OK 0x01
33 static char *progname
;
35 static BlockBackend
*qemuio_blk
;
37 /* qemu-io commands passed using -c */
39 static char **cmdline
;
40 static bool imageOpts
;
42 static ReadLineState
*readline_state
;
44 static int close_f(BlockBackend
*blk
, int argc
, char **argv
)
46 blk_unref(qemuio_blk
);
51 static const cmdinfo_t close_cmd
= {
55 .oneline
= "close the current open file",
58 static int openfile(char *name
, int flags
, bool writethrough
, bool force_share
,
61 Error
*local_err
= NULL
;
64 error_report("file open already, try 'help close'");
73 if (qdict_haskey(opts
, BDRV_OPT_FORCE_SHARE
)
74 && !qdict_get_bool(opts
, BDRV_OPT_FORCE_SHARE
)) {
75 error_report("-U conflicts with image options");
79 qdict_put_bool(opts
, BDRV_OPT_FORCE_SHARE
, true);
81 qemuio_blk
= blk_new_open(name
, NULL
, opts
, flags
, &local_err
);
83 error_reportf_err(local_err
, "can't open%s%s: ",
84 name
? " device " : "", name
?: "");
88 blk_set_enable_write_cache(qemuio_blk
, !writethrough
);
93 static void open_help(void)
97 " opens a new file in the requested mode\n"
100 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
102 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
103 " -r, -- open file read-only\n"
104 " -s, -- use snapshot file\n"
105 " -C, -- use copy-on-read\n"
106 " -n, -- disable host cache, short for -t none\n"
107 " -U, -- force shared permissions\n"
108 " -k, -- use kernel AIO implementation (on Linux only)\n"
109 " -t, -- use the given cache mode for the image\n"
110 " -d, -- use the given discard mode for the image\n"
111 " -o, -- options to be given to the block driver"
115 static int open_f(BlockBackend
*blk
, int argc
, char **argv
);
117 static const cmdinfo_t open_cmd
= {
123 .flags
= CMD_NOFILE_OK
,
124 .args
= "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
125 .oneline
= "open the file specified by path",
129 static QemuOptsList empty_opts
= {
132 .head
= QTAILQ_HEAD_INITIALIZER(empty_opts
.head
),
134 /* no elements => accept any params */
135 { /* end of list */ }
139 static int open_f(BlockBackend
*blk
, int argc
, char **argv
)
141 int flags
= BDRV_O_UNMAP
;
143 bool writethrough
= true;
147 bool force_share
= false;
149 while ((c
= getopt(argc
, argv
, "snCro:kt:d:U")) != -1) {
152 flags
|= BDRV_O_SNAPSHOT
;
155 flags
|= BDRV_O_NOCACHE
;
156 writethrough
= false;
159 flags
|= BDRV_O_COPY_ON_READ
;
165 flags
|= BDRV_O_NATIVE_AIO
;
168 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
169 error_report("Invalid cache option: %s", optarg
);
170 qemu_opts_reset(&empty_opts
);
175 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
176 error_report("Invalid discard option: %s", optarg
);
177 qemu_opts_reset(&empty_opts
);
183 printf("--image-opts and 'open -o' are mutually exclusive\n");
184 qemu_opts_reset(&empty_opts
);
187 if (!qemu_opts_parse_noisily(&empty_opts
, optarg
, false)) {
188 qemu_opts_reset(&empty_opts
);
196 qemu_opts_reset(&empty_opts
);
197 return qemuio_command_usage(&open_cmd
);
202 flags
|= BDRV_O_RDWR
;
205 if (imageOpts
&& (optind
== argc
- 1)) {
206 if (!qemu_opts_parse_noisily(&empty_opts
, argv
[optind
], false)) {
207 qemu_opts_reset(&empty_opts
);
213 qopts
= qemu_opts_find(&empty_opts
, NULL
);
214 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
215 qemu_opts_reset(&empty_opts
);
217 if (optind
== argc
- 1) {
218 openfile(argv
[optind
], flags
, writethrough
, force_share
, opts
);
219 } else if (optind
== argc
) {
220 openfile(NULL
, flags
, writethrough
, force_share
, opts
);
223 qemuio_command_usage(&open_cmd
);
228 static int quit_f(BlockBackend
*blk
, int argc
, char **argv
)
233 static const cmdinfo_t quit_cmd
= {
239 .flags
= CMD_FLAG_GLOBAL
,
240 .oneline
= "exit the program",
243 static void usage(const char *name
)
246 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
247 "QEMU Disk exerciser\n"
249 " --object OBJECTDEF define an object such as 'secret' for\n"
250 " passwords and/or encryption keys\n"
251 " --image-opts treat file as option string\n"
252 " -c, --cmd STRING execute command with its arguments\n"
253 " from the given string\n"
254 " -f, --format FMT specifies the block driver to use\n"
255 " -r, --read-only export read-only\n"
256 " -s, --snapshot use snapshot file\n"
257 " -n, --nocache disable host cache, short for -t none\n"
258 " -C, --copy-on-read enable copy-on-read\n"
259 " -m, --misalign misalign allocations for O_DIRECT\n"
260 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
261 " -t, --cache=MODE use the given cache mode for the image\n"
262 " -d, --discard=MODE use the given discard mode for the image\n"
263 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
264 " specify tracing options\n"
265 " see qemu-img(1) man page for full description\n"
266 " -U, --force-share force shared permissions\n"
267 " -h, --help display this help and exit\n"
268 " -V, --version output version information and exit\n"
270 "See '%s -c help' for information on available commands.\n"
272 QEMU_HELP_BOTTOM
"\n",
276 static char *get_prompt(void)
278 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
281 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
287 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque
,
288 const char *fmt
, ...)
296 static void readline_flush_func(void *opaque
)
301 static void readline_func(void *opaque
, const char *str
, void *readline_opaque
)
303 char **line
= readline_opaque
;
304 *line
= g_strdup(str
);
307 static void completion_match(const char *cmd
, void *opaque
)
309 readline_add_completion(readline_state
, cmd
);
312 static void readline_completion_func(void *opaque
, const char *str
)
314 readline_set_completion_index(readline_state
, strlen(str
));
315 qemuio_complete_command(str
, completion_match
, NULL
);
318 static char *fetchline_readline(void)
322 readline_start(readline_state
, get_prompt(), 0, readline_func
, &line
);
328 readline_handle_byte(readline_state
, ch
);
333 #define MAXREADLINESZ 1024
334 static char *fetchline_fgets(void)
336 char *p
, *line
= g_malloc(MAXREADLINESZ
);
338 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
343 p
= line
+ strlen(line
);
344 if (p
!= line
&& p
[-1] == '\n') {
351 static char *fetchline(void)
353 if (readline_state
) {
354 return fetchline_readline();
356 return fetchline_fgets();
360 static void prep_fetchline(void *opaque
)
362 int *fetchable
= opaque
;
364 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
368 static void command_loop(void)
370 int i
, done
= 0, fetchable
= 0, prompted
= 0;
373 for (i
= 0; !done
&& i
< ncmdline
; i
++) {
374 done
= qemuio_command(qemuio_blk
, cmdline
[i
]);
383 printf("%s", get_prompt());
385 qemu_set_fd_handler(STDIN_FILENO
, prep_fetchline
, NULL
, &fetchable
);
389 main_loop_wait(false);
399 done
= qemuio_command(qemuio_blk
, input
);
405 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
408 static void add_user_command(char *optarg
)
410 cmdline
= g_renew(char *, cmdline
, ++ncmdline
);
411 cmdline
[ncmdline
-1] = optarg
;
414 static void reenable_tty_echo(void)
416 qemu_set_tty_echo(STDIN_FILENO
, true);
421 OPTION_IMAGE_OPTS
= 257,
424 static QemuOptsList qemu_object_opts
= {
426 .implied_opt_name
= "qom-type",
427 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
434 static QemuOptsList file_opts
= {
436 .implied_opt_name
= "file",
437 .head
= QTAILQ_HEAD_INITIALIZER(file_opts
.head
),
439 /* no elements => accept any params */
440 { /* end of list */ }
444 int main(int argc
, char **argv
)
447 const char *sopt
= "hVc:d:f:rsnCmkt:T:U";
448 const struct option lopt
[] = {
449 { "help", no_argument
, NULL
, 'h' },
450 { "version", no_argument
, NULL
, 'V' },
451 { "cmd", required_argument
, NULL
, 'c' },
452 { "format", required_argument
, NULL
, 'f' },
453 { "read-only", no_argument
, NULL
, 'r' },
454 { "snapshot", no_argument
, NULL
, 's' },
455 { "nocache", no_argument
, NULL
, 'n' },
456 { "copy-on-read", no_argument
, NULL
, 'C' },
457 { "misalign", no_argument
, NULL
, 'm' },
458 { "native-aio", no_argument
, NULL
, 'k' },
459 { "discard", required_argument
, NULL
, 'd' },
460 { "cache", required_argument
, NULL
, 't' },
461 { "trace", required_argument
, NULL
, 'T' },
462 { "object", required_argument
, NULL
, OPTION_OBJECT
},
463 { "image-opts", no_argument
, NULL
, OPTION_IMAGE_OPTS
},
464 { "force-share", no_argument
, 0, 'U'},
469 int flags
= BDRV_O_UNMAP
;
470 bool writethrough
= true;
471 Error
*local_error
= NULL
;
473 const char *format
= NULL
;
474 char *trace_file
= NULL
;
475 bool force_share
= false;
478 signal(SIGPIPE
, SIG_IGN
);
481 module_call_init(MODULE_INIT_TRACE
);
482 progname
= basename(argv
[0]);
483 qemu_init_exec_dir(argv
[0]);
485 qcrypto_init(&error_fatal
);
487 module_call_init(MODULE_INIT_QOM
);
488 qemu_add_opts(&qemu_object_opts
);
489 qemu_add_opts(&qemu_trace_opts
);
492 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
495 flags
|= BDRV_O_SNAPSHOT
;
498 flags
|= BDRV_O_NOCACHE
;
499 writethrough
= false;
502 flags
|= BDRV_O_COPY_ON_READ
;
505 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
506 error_report("Invalid discard option: %s", optarg
);
514 add_user_command(optarg
);
520 qemuio_misalign
= true;
523 flags
|= BDRV_O_NATIVE_AIO
;
526 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
527 error_report("Invalid cache option: %s", optarg
);
533 trace_file
= trace_opt_parse(optarg
);
536 printf("%s version " QEMU_VERSION QEMU_PKGVERSION
"\n"
537 QEMU_COPYRIGHT
"\n", progname
);
545 case OPTION_OBJECT
: {
547 qopts
= qemu_opts_parse_noisily(&qemu_object_opts
,
553 case OPTION_IMAGE_OPTS
:
562 if ((argc
- optind
) > 1) {
567 if (format
&& imageOpts
) {
568 error_report("--image-opts and -f are mutually exclusive");
572 if (qemu_init_main_loop(&local_error
)) {
573 error_report_err(local_error
);
577 if (qemu_opts_foreach(&qemu_object_opts
,
578 user_creatable_add_opts_foreach
,
583 if (!trace_init_backends()) {
586 trace_init_file(trace_file
);
587 qemu_set_log(LOG_TRACE
);
589 /* initialize commands */
590 qemuio_add_command(&quit_cmd
);
591 qemuio_add_command(&open_cmd
);
592 qemuio_add_command(&close_cmd
);
594 if (isatty(STDIN_FILENO
)) {
595 readline_state
= readline_init(readline_printf_func
,
598 readline_completion_func
);
599 qemu_set_tty_echo(STDIN_FILENO
, false);
600 atexit(reenable_tty_echo
);
603 /* open the device */
605 flags
|= BDRV_O_RDWR
;
608 if ((argc
- optind
) == 1) {
610 QemuOpts
*qopts
= NULL
;
611 qopts
= qemu_opts_parse_noisily(&file_opts
, argv
[optind
], false);
615 opts
= qemu_opts_to_qdict(qopts
, NULL
);
616 if (openfile(NULL
, flags
, writethrough
, force_share
, opts
)) {
622 qdict_put_str(opts
, "driver", format
);
624 if (openfile(argv
[optind
], flags
, writethrough
,
625 force_share
, opts
)) {
633 * Make sure all outstanding requests complete before the program exits.
637 blk_unref(qemuio_blk
);
638 g_free(readline_state
);