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"
21 #include "qapi/qmp/qstring.h"
22 #include "qom/object_interfaces.h"
23 #include "sysemu/block-backend.h"
24 #include "block/block_int.h"
25 #include "trace/control.h"
26 #include "crypto/init.h"
28 #define CMD_NOFILE_OK 0x01
30 static char *progname
;
32 static BlockBackend
*qemuio_blk
;
34 /* qemu-io commands passed using -c */
36 static char **cmdline
;
37 static bool imageOpts
;
39 static ReadLineState
*readline_state
;
41 static int close_f(BlockBackend
*blk
, int argc
, char **argv
)
43 blk_unref(qemuio_blk
);
48 static const cmdinfo_t close_cmd
= {
52 .oneline
= "close the current open file",
55 static int openfile(char *name
, int flags
, bool writethrough
, QDict
*opts
)
57 Error
*local_err
= NULL
;
61 error_report("file open already, try 'help close'");
66 qemuio_blk
= blk_new_open(name
, NULL
, opts
, flags
, &local_err
);
68 error_reportf_err(local_err
, "can't open%s%s: ",
69 name
? " device " : "", name
?: "");
73 bs
= blk_bs(qemuio_blk
);
74 if (bdrv_is_encrypted(bs
) && bdrv_key_required(bs
)) {
76 printf("Disk image '%s' is encrypted.\n", name
);
77 if (qemu_read_password(password
, sizeof(password
)) < 0) {
78 error_report("No password given");
81 if (bdrv_set_key(bs
, password
) < 0) {
82 error_report("invalid password");
87 blk_set_enable_write_cache(qemuio_blk
, !writethrough
);
92 blk_unref(qemuio_blk
);
97 static void open_help(void)
101 " opens a new file in the requested mode\n"
104 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
106 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
107 " -r, -- open file read-only\n"
108 " -s, -- use snapshot file\n"
109 " -n, -- disable host cache, short for -t none\n"
110 " -k, -- use kernel AIO implementation (on Linux only)\n"
111 " -t, -- use the given cache mode for the image\n"
112 " -d, -- use the given discard mode for the image\n"
113 " -o, -- options to be given to the block driver"
117 static int open_f(BlockBackend
*blk
, int argc
, char **argv
);
119 static const cmdinfo_t open_cmd
= {
125 .flags
= CMD_NOFILE_OK
,
126 .args
= "[-rsnk] [-t cache] [-d discard] [-o options] [path]",
127 .oneline
= "open the file specified by path",
131 static QemuOptsList empty_opts
= {
134 .head
= QTAILQ_HEAD_INITIALIZER(empty_opts
.head
),
136 /* no elements => accept any params */
137 { /* end of list */ }
141 static int open_f(BlockBackend
*blk
, int argc
, char **argv
)
143 int flags
= BDRV_O_UNMAP
;
145 bool writethrough
= true;
150 while ((c
= getopt(argc
, argv
, "snro:kt:d:")) != -1) {
153 flags
|= BDRV_O_SNAPSHOT
;
156 flags
|= BDRV_O_NOCACHE
;
157 writethrough
= false;
163 flags
|= BDRV_O_NATIVE_AIO
;
166 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
167 error_report("Invalid cache option: %s", optarg
);
168 qemu_opts_reset(&empty_opts
);
173 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
174 error_report("Invalid discard option: %s", optarg
);
175 qemu_opts_reset(&empty_opts
);
181 printf("--image-opts and 'open -o' are mutually exclusive\n");
182 qemu_opts_reset(&empty_opts
);
185 if (!qemu_opts_parse_noisily(&empty_opts
, optarg
, false)) {
186 qemu_opts_reset(&empty_opts
);
191 qemu_opts_reset(&empty_opts
);
192 return qemuio_command_usage(&open_cmd
);
197 flags
|= BDRV_O_RDWR
;
200 if (imageOpts
&& (optind
== argc
- 1)) {
201 if (!qemu_opts_parse_noisily(&empty_opts
, argv
[optind
], false)) {
202 qemu_opts_reset(&empty_opts
);
208 qopts
= qemu_opts_find(&empty_opts
, NULL
);
209 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
210 qemu_opts_reset(&empty_opts
);
212 if (optind
== argc
- 1) {
213 return openfile(argv
[optind
], flags
, writethrough
, opts
);
214 } else if (optind
== argc
) {
215 return openfile(NULL
, flags
, writethrough
, opts
);
218 return qemuio_command_usage(&open_cmd
);
222 static int quit_f(BlockBackend
*blk
, int argc
, char **argv
)
227 static const cmdinfo_t quit_cmd
= {
233 .flags
= CMD_FLAG_GLOBAL
,
234 .oneline
= "exit the program",
237 static void usage(const char *name
)
240 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
241 "QEMU Disk exerciser\n"
243 " --object OBJECTDEF define an object such as 'secret' for\n"
244 " passwords and/or encryption keys\n"
245 " --image-opts treat file as option string\n"
246 " -c, --cmd STRING execute command with its arguments\n"
247 " from the given string\n"
248 " -f, --format FMT specifies the block driver to use\n"
249 " -r, --read-only export read-only\n"
250 " -s, --snapshot use snapshot file\n"
251 " -n, --nocache disable host cache, short for -t none\n"
252 " -m, --misalign misalign allocations for O_DIRECT\n"
253 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
254 " -t, --cache=MODE use the given cache mode for the image\n"
255 " -d, --discard=MODE use the given discard mode for the image\n"
256 " -T, --trace FILE enable trace events listed in the given file\n"
257 " -h, --help display this help and exit\n"
258 " -V, --version output version information and exit\n"
260 "See '%s -c help' for information on available commands."
265 static char *get_prompt(void)
267 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
270 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
276 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque
,
277 const char *fmt
, ...)
285 static void readline_flush_func(void *opaque
)
290 static void readline_func(void *opaque
, const char *str
, void *readline_opaque
)
292 char **line
= readline_opaque
;
293 *line
= g_strdup(str
);
296 static void completion_match(const char *cmd
, void *opaque
)
298 readline_add_completion(readline_state
, cmd
);
301 static void readline_completion_func(void *opaque
, const char *str
)
303 readline_set_completion_index(readline_state
, strlen(str
));
304 qemuio_complete_command(str
, completion_match
, NULL
);
307 static char *fetchline_readline(void)
311 readline_start(readline_state
, get_prompt(), 0, readline_func
, &line
);
317 readline_handle_byte(readline_state
, ch
);
322 #define MAXREADLINESZ 1024
323 static char *fetchline_fgets(void)
325 char *p
, *line
= g_malloc(MAXREADLINESZ
);
327 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
332 p
= line
+ strlen(line
);
333 if (p
!= line
&& p
[-1] == '\n') {
340 static char *fetchline(void)
342 if (readline_state
) {
343 return fetchline_readline();
345 return fetchline_fgets();
349 static void prep_fetchline(void *opaque
)
351 int *fetchable
= opaque
;
353 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
357 static void command_loop(void)
359 int i
, done
= 0, fetchable
= 0, prompted
= 0;
362 for (i
= 0; !done
&& i
< ncmdline
; i
++) {
363 done
= qemuio_command(qemuio_blk
, cmdline
[i
]);
372 printf("%s", get_prompt());
374 qemu_set_fd_handler(STDIN_FILENO
, prep_fetchline
, NULL
, &fetchable
);
378 main_loop_wait(false);
388 done
= qemuio_command(qemuio_blk
, input
);
394 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
397 static void add_user_command(char *optarg
)
399 cmdline
= g_renew(char *, cmdline
, ++ncmdline
);
400 cmdline
[ncmdline
-1] = optarg
;
403 static void reenable_tty_echo(void)
405 qemu_set_tty_echo(STDIN_FILENO
, true);
410 OPTION_IMAGE_OPTS
= 257,
413 static QemuOptsList qemu_object_opts
= {
415 .implied_opt_name
= "qom-type",
416 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
423 static QemuOptsList file_opts
= {
425 .implied_opt_name
= "file",
426 .head
= QTAILQ_HEAD_INITIALIZER(file_opts
.head
),
428 /* no elements => accept any params */
429 { /* end of list */ }
433 int main(int argc
, char **argv
)
436 const char *sopt
= "hVc:d:f:rsnmkt:T:";
437 const struct option lopt
[] = {
438 { "help", no_argument
, NULL
, 'h' },
439 { "version", no_argument
, NULL
, 'V' },
440 { "cmd", required_argument
, NULL
, 'c' },
441 { "format", required_argument
, NULL
, 'f' },
442 { "read-only", no_argument
, NULL
, 'r' },
443 { "snapshot", no_argument
, NULL
, 's' },
444 { "nocache", no_argument
, NULL
, 'n' },
445 { "misalign", no_argument
, NULL
, 'm' },
446 { "native-aio", no_argument
, NULL
, 'k' },
447 { "discard", required_argument
, NULL
, 'd' },
448 { "cache", required_argument
, NULL
, 't' },
449 { "trace", required_argument
, NULL
, 'T' },
450 { "object", required_argument
, NULL
, OPTION_OBJECT
},
451 { "image-opts", no_argument
, NULL
, OPTION_IMAGE_OPTS
},
456 int flags
= BDRV_O_UNMAP
;
457 bool writethrough
= true;
458 Error
*local_error
= NULL
;
460 const char *format
= NULL
;
463 signal(SIGPIPE
, SIG_IGN
);
466 progname
= basename(argv
[0]);
467 qemu_init_exec_dir(argv
[0]);
469 qcrypto_init(&error_fatal
);
471 module_call_init(MODULE_INIT_QOM
);
472 qemu_add_opts(&qemu_object_opts
);
475 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
478 flags
|= BDRV_O_SNAPSHOT
;
481 flags
|= BDRV_O_NOCACHE
;
482 writethrough
= false;
485 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
486 error_report("Invalid discard option: %s", optarg
);
494 add_user_command(optarg
);
500 qemuio_misalign
= true;
503 flags
|= BDRV_O_NATIVE_AIO
;
506 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
507 error_report("Invalid cache option: %s", optarg
);
512 if (!trace_init_backends()) {
513 exit(1); /* error message will have been printed */
517 printf("%s version %s\n", progname
, QEMU_VERSION
);
522 case OPTION_OBJECT
: {
524 qopts
= qemu_opts_parse_noisily(&qemu_object_opts
,
530 case OPTION_IMAGE_OPTS
:
539 if ((argc
- optind
) > 1) {
544 if (format
&& imageOpts
) {
545 error_report("--image-opts and -f are mutually exclusive");
549 if (qemu_init_main_loop(&local_error
)) {
550 error_report_err(local_error
);
554 if (qemu_opts_foreach(&qemu_object_opts
,
555 user_creatable_add_opts_foreach
,
560 /* initialize commands */
561 qemuio_add_command(&quit_cmd
);
562 qemuio_add_command(&open_cmd
);
563 qemuio_add_command(&close_cmd
);
565 if (isatty(STDIN_FILENO
)) {
566 readline_state
= readline_init(readline_printf_func
,
569 readline_completion_func
);
570 qemu_set_tty_echo(STDIN_FILENO
, false);
571 atexit(reenable_tty_echo
);
574 /* open the device */
576 flags
|= BDRV_O_RDWR
;
579 if ((argc
- optind
) == 1) {
581 QemuOpts
*qopts
= NULL
;
582 qopts
= qemu_opts_parse_noisily(&file_opts
, argv
[optind
], false);
586 opts
= qemu_opts_to_qdict(qopts
, NULL
);
587 openfile(NULL
, flags
, writethrough
, opts
);
591 qdict_put(opts
, "driver", qstring_from_str(format
));
593 openfile(argv
[optind
], flags
, writethrough
, opts
);
599 * Make sure all outstanding requests complete before the program exits.
603 blk_unref(qemuio_blk
);
604 g_free(readline_state
);