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"
30 #define CMD_NOFILE_OK 0x01
32 static char *progname
;
34 static BlockBackend
*qemuio_blk
;
36 /* qemu-io commands passed using -c */
38 static char **cmdline
;
39 static bool imageOpts
;
41 static ReadLineState
*readline_state
;
43 static int close_f(BlockBackend
*blk
, int argc
, char **argv
)
45 blk_unref(qemuio_blk
);
50 static const cmdinfo_t close_cmd
= {
54 .oneline
= "close the current open file",
57 static int openfile(char *name
, int flags
, bool writethrough
, bool force_share
,
60 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(opts
, BDRV_OPT_FORCE_SHARE
, qbool_from_bool(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 bs
= blk_bs(qemuio_blk
);
89 if (bdrv_is_encrypted(bs
) && bdrv_key_required(bs
)) {
91 printf("Disk image '%s' is encrypted.\n", name
);
92 if (qemu_read_password(password
, sizeof(password
)) < 0) {
93 error_report("No password given");
96 if (bdrv_set_key(bs
, password
) < 0) {
97 error_report("invalid password");
102 blk_set_enable_write_cache(qemuio_blk
, !writethrough
);
107 blk_unref(qemuio_blk
);
112 static void open_help(void)
116 " opens a new file in the requested mode\n"
119 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
121 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
122 " -r, -- open file read-only\n"
123 " -s, -- use snapshot file\n"
124 " -n, -- disable host cache, short for -t none\n"
125 " -U, -- force shared permissions\n"
126 " -k, -- use kernel AIO implementation (on Linux only)\n"
127 " -t, -- use the given cache mode for the image\n"
128 " -d, -- use the given discard mode for the image\n"
129 " -o, -- options to be given to the block driver"
133 static int open_f(BlockBackend
*blk
, int argc
, char **argv
);
135 static const cmdinfo_t open_cmd
= {
141 .flags
= CMD_NOFILE_OK
,
142 .args
= "[-rsnkU] [-t cache] [-d discard] [-o options] [path]",
143 .oneline
= "open the file specified by path",
147 static QemuOptsList empty_opts
= {
150 .head
= QTAILQ_HEAD_INITIALIZER(empty_opts
.head
),
152 /* no elements => accept any params */
153 { /* end of list */ }
157 static int open_f(BlockBackend
*blk
, int argc
, char **argv
)
159 int flags
= BDRV_O_UNMAP
;
161 bool writethrough
= true;
165 bool force_share
= false;
167 while ((c
= getopt(argc
, argv
, "snro:kt:d:U")) != -1) {
170 flags
|= BDRV_O_SNAPSHOT
;
173 flags
|= BDRV_O_NOCACHE
;
174 writethrough
= false;
180 flags
|= BDRV_O_NATIVE_AIO
;
183 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
184 error_report("Invalid cache option: %s", optarg
);
185 qemu_opts_reset(&empty_opts
);
190 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
191 error_report("Invalid discard option: %s", optarg
);
192 qemu_opts_reset(&empty_opts
);
198 printf("--image-opts and 'open -o' are mutually exclusive\n");
199 qemu_opts_reset(&empty_opts
);
202 if (!qemu_opts_parse_noisily(&empty_opts
, optarg
, false)) {
203 qemu_opts_reset(&empty_opts
);
211 qemu_opts_reset(&empty_opts
);
212 return qemuio_command_usage(&open_cmd
);
217 flags
|= BDRV_O_RDWR
;
220 if (imageOpts
&& (optind
== argc
- 1)) {
221 if (!qemu_opts_parse_noisily(&empty_opts
, argv
[optind
], false)) {
222 qemu_opts_reset(&empty_opts
);
228 qopts
= qemu_opts_find(&empty_opts
, NULL
);
229 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
230 qemu_opts_reset(&empty_opts
);
232 if (optind
== argc
- 1) {
233 return openfile(argv
[optind
], flags
, writethrough
, force_share
, opts
);
234 } else if (optind
== argc
) {
235 return openfile(NULL
, flags
, writethrough
, force_share
, opts
);
238 return qemuio_command_usage(&open_cmd
);
242 static int quit_f(BlockBackend
*blk
, int argc
, char **argv
)
247 static const cmdinfo_t quit_cmd
= {
253 .flags
= CMD_FLAG_GLOBAL
,
254 .oneline
= "exit the program",
257 static void usage(const char *name
)
260 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
261 "QEMU Disk exerciser\n"
263 " --object OBJECTDEF define an object such as 'secret' for\n"
264 " passwords and/or encryption keys\n"
265 " --image-opts treat file as option string\n"
266 " -c, --cmd STRING execute command with its arguments\n"
267 " from the given string\n"
268 " -f, --format FMT specifies the block driver to use\n"
269 " -r, --read-only export read-only\n"
270 " -s, --snapshot use snapshot file\n"
271 " -n, --nocache disable host cache, short for -t none\n"
272 " -m, --misalign misalign allocations for O_DIRECT\n"
273 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
274 " -t, --cache=MODE use the given cache mode for the image\n"
275 " -d, --discard=MODE use the given discard mode for the image\n"
276 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
277 " specify tracing options\n"
278 " see qemu-img(1) man page for full description\n"
279 " -U, --force-share force shared permissions\n"
280 " -h, --help display this help and exit\n"
281 " -V, --version output version information and exit\n"
283 "See '%s -c help' for information on available commands."
288 static char *get_prompt(void)
290 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
293 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
299 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque
,
300 const char *fmt
, ...)
308 static void readline_flush_func(void *opaque
)
313 static void readline_func(void *opaque
, const char *str
, void *readline_opaque
)
315 char **line
= readline_opaque
;
316 *line
= g_strdup(str
);
319 static void completion_match(const char *cmd
, void *opaque
)
321 readline_add_completion(readline_state
, cmd
);
324 static void readline_completion_func(void *opaque
, const char *str
)
326 readline_set_completion_index(readline_state
, strlen(str
));
327 qemuio_complete_command(str
, completion_match
, NULL
);
330 static char *fetchline_readline(void)
334 readline_start(readline_state
, get_prompt(), 0, readline_func
, &line
);
340 readline_handle_byte(readline_state
, ch
);
345 #define MAXREADLINESZ 1024
346 static char *fetchline_fgets(void)
348 char *p
, *line
= g_malloc(MAXREADLINESZ
);
350 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
355 p
= line
+ strlen(line
);
356 if (p
!= line
&& p
[-1] == '\n') {
363 static char *fetchline(void)
365 if (readline_state
) {
366 return fetchline_readline();
368 return fetchline_fgets();
372 static void prep_fetchline(void *opaque
)
374 int *fetchable
= opaque
;
376 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
380 static void command_loop(void)
382 int i
, done
= 0, fetchable
= 0, prompted
= 0;
385 for (i
= 0; !done
&& i
< ncmdline
; i
++) {
386 done
= qemuio_command(qemuio_blk
, cmdline
[i
]);
395 printf("%s", get_prompt());
397 qemu_set_fd_handler(STDIN_FILENO
, prep_fetchline
, NULL
, &fetchable
);
401 main_loop_wait(false);
411 done
= qemuio_command(qemuio_blk
, input
);
417 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
420 static void add_user_command(char *optarg
)
422 cmdline
= g_renew(char *, cmdline
, ++ncmdline
);
423 cmdline
[ncmdline
-1] = optarg
;
426 static void reenable_tty_echo(void)
428 qemu_set_tty_echo(STDIN_FILENO
, true);
433 OPTION_IMAGE_OPTS
= 257,
436 static QemuOptsList qemu_object_opts
= {
438 .implied_opt_name
= "qom-type",
439 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
446 static QemuOptsList file_opts
= {
448 .implied_opt_name
= "file",
449 .head
= QTAILQ_HEAD_INITIALIZER(file_opts
.head
),
451 /* no elements => accept any params */
452 { /* end of list */ }
456 int main(int argc
, char **argv
)
459 const char *sopt
= "hVc:d:f:rsnmkt:T:U";
460 const struct option lopt
[] = {
461 { "help", no_argument
, NULL
, 'h' },
462 { "version", no_argument
, NULL
, 'V' },
463 { "cmd", required_argument
, NULL
, 'c' },
464 { "format", required_argument
, NULL
, 'f' },
465 { "read-only", no_argument
, NULL
, 'r' },
466 { "snapshot", no_argument
, NULL
, 's' },
467 { "nocache", no_argument
, NULL
, 'n' },
468 { "misalign", no_argument
, NULL
, 'm' },
469 { "native-aio", no_argument
, NULL
, 'k' },
470 { "discard", required_argument
, NULL
, 'd' },
471 { "cache", required_argument
, NULL
, 't' },
472 { "trace", required_argument
, NULL
, 'T' },
473 { "object", required_argument
, NULL
, OPTION_OBJECT
},
474 { "image-opts", no_argument
, NULL
, OPTION_IMAGE_OPTS
},
475 { "force-share", no_argument
, 0, 'U'},
480 int flags
= BDRV_O_UNMAP
;
481 bool writethrough
= true;
482 Error
*local_error
= NULL
;
484 const char *format
= NULL
;
485 char *trace_file
= NULL
;
486 bool force_share
= false;
489 signal(SIGPIPE
, SIG_IGN
);
492 module_call_init(MODULE_INIT_TRACE
);
493 progname
= basename(argv
[0]);
494 qemu_init_exec_dir(argv
[0]);
496 qcrypto_init(&error_fatal
);
498 module_call_init(MODULE_INIT_QOM
);
499 qemu_add_opts(&qemu_object_opts
);
500 qemu_add_opts(&qemu_trace_opts
);
503 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
506 flags
|= BDRV_O_SNAPSHOT
;
509 flags
|= BDRV_O_NOCACHE
;
510 writethrough
= false;
513 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
514 error_report("Invalid discard option: %s", optarg
);
522 add_user_command(optarg
);
528 qemuio_misalign
= true;
531 flags
|= BDRV_O_NATIVE_AIO
;
534 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
535 error_report("Invalid cache option: %s", optarg
);
541 trace_file
= trace_opt_parse(optarg
);
544 printf("%s version %s\n", progname
, QEMU_VERSION
);
552 case OPTION_OBJECT
: {
554 qopts
= qemu_opts_parse_noisily(&qemu_object_opts
,
560 case OPTION_IMAGE_OPTS
:
569 if ((argc
- optind
) > 1) {
574 if (format
&& imageOpts
) {
575 error_report("--image-opts and -f are mutually exclusive");
579 if (qemu_init_main_loop(&local_error
)) {
580 error_report_err(local_error
);
584 if (qemu_opts_foreach(&qemu_object_opts
,
585 user_creatable_add_opts_foreach
,
590 if (!trace_init_backends()) {
593 trace_init_file(trace_file
);
594 qemu_set_log(LOG_TRACE
);
596 /* initialize commands */
597 qemuio_add_command(&quit_cmd
);
598 qemuio_add_command(&open_cmd
);
599 qemuio_add_command(&close_cmd
);
601 if (isatty(STDIN_FILENO
)) {
602 readline_state
= readline_init(readline_printf_func
,
605 readline_completion_func
);
606 qemu_set_tty_echo(STDIN_FILENO
, false);
607 atexit(reenable_tty_echo
);
610 /* open the device */
612 flags
|= BDRV_O_RDWR
;
615 if ((argc
- optind
) == 1) {
617 QemuOpts
*qopts
= NULL
;
618 qopts
= qemu_opts_parse_noisily(&file_opts
, argv
[optind
], false);
622 opts
= qemu_opts_to_qdict(qopts
, NULL
);
623 if (openfile(NULL
, flags
, writethrough
, force_share
, opts
)) {
629 qdict_put_str(opts
, "driver", format
);
631 if (openfile(argv
[optind
], flags
, writethrough
,
632 force_share
, opts
)) {
640 * Make sure all outstanding requests complete before the program exits.
644 blk_unref(qemuio_blk
);
645 g_free(readline_state
);