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 "qom/object_interfaces.h"
24 #include "sysemu/block-backend.h"
25 #include "block/block_int.h"
26 #include "trace/control.h"
27 #include "crypto/init.h"
29 #define CMD_NOFILE_OK 0x01
31 static char *progname
;
33 static BlockBackend
*qemuio_blk
;
35 /* qemu-io commands passed using -c */
37 static char **cmdline
;
38 static bool imageOpts
;
40 static ReadLineState
*readline_state
;
42 static int close_f(BlockBackend
*blk
, int argc
, char **argv
)
44 blk_unref(qemuio_blk
);
49 static const cmdinfo_t close_cmd
= {
53 .oneline
= "close the current open file",
56 static int openfile(char *name
, int flags
, bool writethrough
, QDict
*opts
)
58 Error
*local_err
= NULL
;
62 error_report("file open already, try 'help close'");
67 qemuio_blk
= blk_new_open(name
, NULL
, opts
, flags
, &local_err
);
69 error_reportf_err(local_err
, "can't open%s%s: ",
70 name
? " device " : "", name
?: "");
74 bs
= blk_bs(qemuio_blk
);
75 if (bdrv_is_encrypted(bs
) && bdrv_key_required(bs
)) {
77 printf("Disk image '%s' is encrypted.\n", name
);
78 if (qemu_read_password(password
, sizeof(password
)) < 0) {
79 error_report("No password given");
82 if (bdrv_set_key(bs
, password
) < 0) {
83 error_report("invalid password");
88 blk_set_enable_write_cache(qemuio_blk
, !writethrough
);
93 blk_unref(qemuio_blk
);
98 static void open_help(void)
102 " opens a new file in the requested mode\n"
105 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
107 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
108 " -r, -- open file read-only\n"
109 " -s, -- use snapshot file\n"
110 " -n, -- disable host cache, short for -t none\n"
111 " -k, -- use kernel AIO implementation (on Linux only)\n"
112 " -t, -- use the given cache mode for the image\n"
113 " -d, -- use the given discard mode for the image\n"
114 " -o, -- options to be given to the block driver"
118 static int open_f(BlockBackend
*blk
, int argc
, char **argv
);
120 static const cmdinfo_t open_cmd
= {
126 .flags
= CMD_NOFILE_OK
,
127 .args
= "[-rsnk] [-t cache] [-d discard] [-o options] [path]",
128 .oneline
= "open the file specified by path",
132 static QemuOptsList empty_opts
= {
135 .head
= QTAILQ_HEAD_INITIALIZER(empty_opts
.head
),
137 /* no elements => accept any params */
138 { /* end of list */ }
142 static int open_f(BlockBackend
*blk
, int argc
, char **argv
)
144 int flags
= BDRV_O_UNMAP
;
146 bool writethrough
= true;
151 while ((c
= getopt(argc
, argv
, "snro:kt:d:")) != -1) {
154 flags
|= BDRV_O_SNAPSHOT
;
157 flags
|= BDRV_O_NOCACHE
;
158 writethrough
= false;
164 flags
|= BDRV_O_NATIVE_AIO
;
167 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
168 error_report("Invalid cache option: %s", optarg
);
169 qemu_opts_reset(&empty_opts
);
174 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
175 error_report("Invalid discard option: %s", optarg
);
176 qemu_opts_reset(&empty_opts
);
182 printf("--image-opts and 'open -o' are mutually exclusive\n");
183 qemu_opts_reset(&empty_opts
);
186 if (!qemu_opts_parse_noisily(&empty_opts
, optarg
, false)) {
187 qemu_opts_reset(&empty_opts
);
192 qemu_opts_reset(&empty_opts
);
193 return qemuio_command_usage(&open_cmd
);
198 flags
|= BDRV_O_RDWR
;
201 if (imageOpts
&& (optind
== argc
- 1)) {
202 if (!qemu_opts_parse_noisily(&empty_opts
, argv
[optind
], false)) {
203 qemu_opts_reset(&empty_opts
);
209 qopts
= qemu_opts_find(&empty_opts
, NULL
);
210 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
211 qemu_opts_reset(&empty_opts
);
213 if (optind
== argc
- 1) {
214 return openfile(argv
[optind
], flags
, writethrough
, opts
);
215 } else if (optind
== argc
) {
216 return openfile(NULL
, flags
, writethrough
, opts
);
219 return qemuio_command_usage(&open_cmd
);
223 static int quit_f(BlockBackend
*blk
, int argc
, char **argv
)
228 #if defined(CONFIG_FVD)
229 # include "qemu-io-sim.c"
232 static const cmdinfo_t quit_cmd
= {
238 .flags
= CMD_FLAG_GLOBAL
,
239 .oneline
= "exit the program",
242 static void usage(const char *name
)
245 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
246 "QEMU Disk exerciser\n"
248 " --object OBJECTDEF define an object such as 'secret' for\n"
249 " passwords and/or encryption keys\n"
250 " --image-opts treat file as option string\n"
251 " -c, --cmd STRING execute command with its arguments\n"
252 " from the given string\n"
253 " -f, --format FMT specifies the block driver to use\n"
254 " -r, --read-only export read-only\n"
255 " -s, --snapshot use snapshot file\n"
256 " -n, --nocache disable host cache, short for -t none\n"
257 " -m, --misalign misalign allocations for O_DIRECT\n"
258 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
259 " -t, --cache=MODE use the given cache mode for the image\n"
260 " -d, --discard=MODE use the given discard mode for the image\n"
261 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
262 " specify tracing options\n"
263 " see qemu-img(1) man page for full description\n"
264 " -h, --help display this help and exit\n"
265 " -V, --version output version information and exit\n"
267 "See '%s -c help' for information on available commands."
272 static char *get_prompt(void)
274 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
277 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
283 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque
,
284 const char *fmt
, ...)
292 static void readline_flush_func(void *opaque
)
297 static void readline_func(void *opaque
, const char *str
, void *readline_opaque
)
299 char **line
= readline_opaque
;
300 *line
= g_strdup(str
);
303 static void completion_match(const char *cmd
, void *opaque
)
305 readline_add_completion(readline_state
, cmd
);
308 static void readline_completion_func(void *opaque
, const char *str
)
310 readline_set_completion_index(readline_state
, strlen(str
));
311 qemuio_complete_command(str
, completion_match
, NULL
);
314 static char *fetchline_readline(void)
318 readline_start(readline_state
, get_prompt(), 0, readline_func
, &line
);
324 readline_handle_byte(readline_state
, ch
);
329 #define MAXREADLINESZ 1024
330 static char *fetchline_fgets(void)
332 char *p
, *line
= g_malloc(MAXREADLINESZ
);
334 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
339 p
= line
+ strlen(line
);
340 if (p
!= line
&& p
[-1] == '\n') {
347 static char *fetchline(void)
349 if (readline_state
) {
350 return fetchline_readline();
352 return fetchline_fgets();
356 static void prep_fetchline(void *opaque
)
358 int *fetchable
= opaque
;
360 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
364 static void command_loop(void)
366 int i
, done
= 0, fetchable
= 0, prompted
= 0;
369 for (i
= 0; !done
&& i
< ncmdline
; i
++) {
370 done
= qemuio_command(qemuio_blk
, cmdline
[i
]);
379 printf("%s", get_prompt());
381 qemu_set_fd_handler(STDIN_FILENO
, prep_fetchline
, NULL
, &fetchable
);
385 main_loop_wait(false);
395 done
= qemuio_command(qemuio_blk
, input
);
401 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
404 static void add_user_command(char *optarg
)
406 cmdline
= g_renew(char *, cmdline
, ++ncmdline
);
407 cmdline
[ncmdline
-1] = optarg
;
410 static void reenable_tty_echo(void)
412 qemu_set_tty_echo(STDIN_FILENO
, true);
417 OPTION_IMAGE_OPTS
= 257,
420 static QemuOptsList qemu_object_opts
= {
422 .implied_opt_name
= "qom-type",
423 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
430 static QemuOptsList file_opts
= {
432 .implied_opt_name
= "file",
433 .head
= QTAILQ_HEAD_INITIALIZER(file_opts
.head
),
435 /* no elements => accept any params */
436 { /* end of list */ }
440 int main(int argc
, char **argv
)
443 const char *sopt
= "hVc:d:f:rsnmkt:T:";
444 const struct option lopt
[] = {
445 { "help", no_argument
, NULL
, 'h' },
446 { "version", no_argument
, NULL
, 'V' },
447 { "cmd", required_argument
, NULL
, 'c' },
448 { "format", required_argument
, NULL
, 'f' },
449 { "read-only", no_argument
, NULL
, 'r' },
450 { "snapshot", no_argument
, NULL
, 's' },
451 { "nocache", no_argument
, NULL
, 'n' },
452 { "misalign", no_argument
, NULL
, 'm' },
453 { "native-aio", no_argument
, NULL
, 'k' },
454 { "discard", required_argument
, NULL
, 'd' },
455 { "cache", required_argument
, NULL
, 't' },
456 { "trace", required_argument
, NULL
, 'T' },
457 { "object", required_argument
, NULL
, OPTION_OBJECT
},
458 { "image-opts", no_argument
, NULL
, OPTION_IMAGE_OPTS
},
463 int flags
= BDRV_O_UNMAP
;
464 bool writethrough
= true;
465 Error
*local_error
= NULL
;
467 const char *format
= NULL
;
468 char *trace_file
= NULL
;
471 signal(SIGPIPE
, SIG_IGN
);
474 module_call_init(MODULE_INIT_TRACE
);
475 progname
= basename(argv
[0]);
476 qemu_init_exec_dir(argv
[0]);
478 qcrypto_init(&error_fatal
);
480 module_call_init(MODULE_INIT_QOM
);
481 qemu_add_opts(&qemu_object_opts
);
482 qemu_add_opts(&qemu_trace_opts
);
485 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
488 flags
|= BDRV_O_SNAPSHOT
;
491 flags
|= BDRV_O_NOCACHE
;
492 writethrough
= false;
495 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
496 error_report("Invalid discard option: %s", optarg
);
504 add_user_command(optarg
);
510 qemuio_misalign
= true;
513 flags
|= BDRV_O_NATIVE_AIO
;
516 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
517 error_report("Invalid cache option: %s", optarg
);
523 trace_file
= trace_opt_parse(optarg
);
526 printf("%s version %s\n", progname
, QEMU_VERSION
);
531 case OPTION_OBJECT
: {
533 qopts
= qemu_opts_parse_noisily(&qemu_object_opts
,
539 case OPTION_IMAGE_OPTS
:
548 if ((argc
- optind
) > 1) {
553 if (format
&& imageOpts
) {
554 error_report("--image-opts and -f are mutually exclusive");
558 if (qemu_init_main_loop(&local_error
)) {
559 error_report_err(local_error
);
563 if (qemu_opts_foreach(&qemu_object_opts
,
564 user_creatable_add_opts_foreach
,
569 if (!trace_init_backends()) {
572 trace_init_file(trace_file
);
573 qemu_set_log(LOG_TRACE
);
575 /* initialize commands */
576 qemuio_add_command(&quit_cmd
);
577 qemuio_add_command(&open_cmd
);
578 qemuio_add_command(&close_cmd
);
580 if (isatty(STDIN_FILENO
)) {
581 readline_state
= readline_init(readline_printf_func
,
584 readline_completion_func
);
585 qemu_set_tty_echo(STDIN_FILENO
, false);
586 atexit(reenable_tty_echo
);
589 /* open the device */
591 flags
|= BDRV_O_RDWR
;
594 if ((argc
- optind
) == 1) {
596 QemuOpts
*qopts
= NULL
;
597 qopts
= qemu_opts_parse_noisily(&file_opts
, argv
[optind
], false);
601 opts
= qemu_opts_to_qdict(qopts
, NULL
);
602 openfile(NULL
, flags
, writethrough
, opts
);
606 qdict_put(opts
, "driver", qstring_from_str(format
));
608 openfile(argv
[optind
], flags
, writethrough
, opts
);
614 * Make sure all outstanding requests complete before the program exits.
618 blk_unref(qemuio_blk
);
619 g_free(readline_state
);