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"
27 #define CMD_NOFILE_OK 0x01
29 static char *progname
;
31 static BlockBackend
*qemuio_blk
;
33 /* qemu-io commands passed using -c */
35 static char **cmdline
;
36 static bool imageOpts
;
38 static ReadLineState
*readline_state
;
40 static int close_f(BlockBackend
*blk
, int argc
, char **argv
)
42 blk_unref(qemuio_blk
);
47 static const cmdinfo_t close_cmd
= {
51 .oneline
= "close the current open file",
54 static int openfile(char *name
, int flags
, bool writethrough
, QDict
*opts
)
56 Error
*local_err
= NULL
;
60 error_report("file open already, try 'help close'");
65 qemuio_blk
= blk_new_open(name
, NULL
, opts
, flags
, &local_err
);
67 error_reportf_err(local_err
, "can't open%s%s: ",
68 name
? " device " : "", name
?: "");
72 bs
= blk_bs(qemuio_blk
);
73 if (bdrv_is_encrypted(bs
) && bdrv_key_required(bs
)) {
75 printf("Disk image '%s' is encrypted.\n", name
);
76 if (qemu_read_password(password
, sizeof(password
)) < 0) {
77 error_report("No password given");
80 if (bdrv_set_key(bs
, password
) < 0) {
81 error_report("invalid password");
86 blk_set_enable_write_cache(qemuio_blk
, !writethrough
);
91 blk_unref(qemuio_blk
);
96 static void open_help(void)
100 " opens a new file in the requested mode\n"
103 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
105 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
106 " -r, -- open file read-only\n"
107 " -s, -- use snapshot file\n"
108 " -n, -- disable host cache\n"
109 " -o, -- options to be given to the block driver"
113 static int open_f(BlockBackend
*blk
, int argc
, char **argv
);
115 static const cmdinfo_t open_cmd
= {
121 .flags
= CMD_NOFILE_OK
,
122 .args
= "[-Crsn] [-o options] [path]",
123 .oneline
= "open the file specified by path",
127 static QemuOptsList empty_opts
= {
130 .head
= QTAILQ_HEAD_INITIALIZER(empty_opts
.head
),
132 /* no elements => accept any params */
133 { /* end of list */ }
137 static int open_f(BlockBackend
*blk
, int argc
, char **argv
)
141 bool writethrough
= true;
146 while ((c
= getopt(argc
, argv
, "snrgo:")) != -1) {
149 flags
|= BDRV_O_SNAPSHOT
;
152 flags
|= BDRV_O_NOCACHE
;
153 writethrough
= false;
160 printf("--image-opts and 'open -o' are mutually exclusive\n");
163 if (!qemu_opts_parse_noisily(&empty_opts
, optarg
, false)) {
164 qemu_opts_reset(&empty_opts
);
169 qemu_opts_reset(&empty_opts
);
170 return qemuio_command_usage(&open_cmd
);
175 flags
|= BDRV_O_RDWR
;
178 if (imageOpts
&& (optind
== argc
- 1)) {
179 if (!qemu_opts_parse_noisily(&empty_opts
, argv
[optind
], false)) {
180 qemu_opts_reset(&empty_opts
);
186 qopts
= qemu_opts_find(&empty_opts
, NULL
);
187 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
188 qemu_opts_reset(&empty_opts
);
190 if (optind
== argc
- 1) {
191 return openfile(argv
[optind
], flags
, writethrough
, opts
);
192 } else if (optind
== argc
) {
193 return openfile(NULL
, flags
, writethrough
, opts
);
196 return qemuio_command_usage(&open_cmd
);
200 static int quit_f(BlockBackend
*blk
, int argc
, char **argv
)
205 #if defined(CONFIG_FVD)
206 # include "qemu-io-sim.c"
209 static const cmdinfo_t quit_cmd
= {
215 .flags
= CMD_FLAG_GLOBAL
,
216 .oneline
= "exit the program",
219 static void usage(const char *name
)
222 "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
223 "QEMU Disk exerciser\n"
225 " --object OBJECTDEF define an object such as 'secret' for\n"
226 " passwords and/or encryption keys\n"
227 " -c, --cmd STRING execute command with its arguments\n"
228 " from the given string\n"
229 " -f, --format FMT specifies the block driver to use\n"
230 " -r, --read-only export read-only\n"
231 " -s, --snapshot use snapshot file\n"
232 " -n, --nocache disable host cache\n"
233 " -m, --misalign misalign allocations for O_DIRECT\n"
234 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
235 " -t, --cache=MODE use the given cache mode for the image\n"
236 " -T, --trace FILE enable trace events listed in the given file\n"
237 " -h, --help display this help and exit\n"
238 " -V, --version output version information and exit\n"
240 "See '%s -c help' for information on available commands."
245 static char *get_prompt(void)
247 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
250 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
256 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque
,
257 const char *fmt
, ...)
265 static void readline_flush_func(void *opaque
)
270 static void readline_func(void *opaque
, const char *str
, void *readline_opaque
)
272 char **line
= readline_opaque
;
273 *line
= g_strdup(str
);
276 static void completion_match(const char *cmd
, void *opaque
)
278 readline_add_completion(readline_state
, cmd
);
281 static void readline_completion_func(void *opaque
, const char *str
)
283 readline_set_completion_index(readline_state
, strlen(str
));
284 qemuio_complete_command(str
, completion_match
, NULL
);
287 static char *fetchline_readline(void)
291 readline_start(readline_state
, get_prompt(), 0, readline_func
, &line
);
297 readline_handle_byte(readline_state
, ch
);
302 #define MAXREADLINESZ 1024
303 static char *fetchline_fgets(void)
305 char *p
, *line
= g_malloc(MAXREADLINESZ
);
307 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
312 p
= line
+ strlen(line
);
313 if (p
!= line
&& p
[-1] == '\n') {
320 static char *fetchline(void)
322 if (readline_state
) {
323 return fetchline_readline();
325 return fetchline_fgets();
329 static void prep_fetchline(void *opaque
)
331 int *fetchable
= opaque
;
333 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
337 static void command_loop(void)
339 int i
, done
= 0, fetchable
= 0, prompted
= 0;
342 for (i
= 0; !done
&& i
< ncmdline
; i
++) {
343 done
= qemuio_command(qemuio_blk
, cmdline
[i
]);
352 printf("%s", get_prompt());
354 qemu_set_fd_handler(STDIN_FILENO
, prep_fetchline
, NULL
, &fetchable
);
358 main_loop_wait(false);
368 done
= qemuio_command(qemuio_blk
, input
);
374 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
377 static void add_user_command(char *optarg
)
379 cmdline
= g_renew(char *, cmdline
, ++ncmdline
);
380 cmdline
[ncmdline
-1] = optarg
;
383 static void reenable_tty_echo(void)
385 qemu_set_tty_echo(STDIN_FILENO
, true);
390 OPTION_IMAGE_OPTS
= 257,
393 static QemuOptsList qemu_object_opts
= {
395 .implied_opt_name
= "qom-type",
396 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
403 static QemuOptsList file_opts
= {
405 .implied_opt_name
= "file",
406 .head
= QTAILQ_HEAD_INITIALIZER(file_opts
.head
),
408 /* no elements => accept any params */
409 { /* end of list */ }
413 int main(int argc
, char **argv
)
416 const char *sopt
= "hVc:d:f:rsnmgkt:T:";
417 const struct option lopt
[] = {
418 { "help", no_argument
, NULL
, 'h' },
419 { "version", no_argument
, NULL
, 'V' },
420 { "offset", required_argument
, NULL
, 'o' },
421 { "cmd", required_argument
, NULL
, 'c' },
422 { "format", required_argument
, NULL
, 'f' },
423 { "read-only", no_argument
, NULL
, 'r' },
424 { "snapshot", no_argument
, NULL
, 's' },
425 { "nocache", no_argument
, NULL
, 'n' },
426 { "misalign", no_argument
, NULL
, 'm' },
427 { "native-aio", no_argument
, NULL
, 'k' },
428 { "discard", required_argument
, NULL
, 'd' },
429 { "cache", required_argument
, NULL
, 't' },
430 { "trace", required_argument
, NULL
, 'T' },
431 { "object", required_argument
, NULL
, OPTION_OBJECT
},
432 { "image-opts", no_argument
, NULL
, OPTION_IMAGE_OPTS
},
437 int flags
= BDRV_O_UNMAP
;
438 bool writethrough
= true;
439 Error
*local_error
= NULL
;
441 const char *format
= NULL
;
444 signal(SIGPIPE
, SIG_IGN
);
447 progname
= basename(argv
[0]);
448 qemu_init_exec_dir(argv
[0]);
450 module_call_init(MODULE_INIT_QOM
);
451 qemu_add_opts(&qemu_object_opts
);
454 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
457 flags
|= BDRV_O_SNAPSHOT
;
460 flags
|= BDRV_O_NOCACHE
;
461 writethrough
= false;
464 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
465 error_report("Invalid discard option: %s", optarg
);
473 add_user_command(optarg
);
479 qemuio_misalign
= true;
482 flags
|= BDRV_O_NATIVE_AIO
;
485 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
486 error_report("Invalid cache option: %s", optarg
);
491 if (!trace_init_backends()) {
492 exit(1); /* error message will have been printed */
496 printf("%s version %s\n", progname
, QEMU_VERSION
);
501 case OPTION_OBJECT
: {
503 qopts
= qemu_opts_parse_noisily(&qemu_object_opts
,
509 case OPTION_IMAGE_OPTS
:
518 if ((argc
- optind
) > 1) {
523 if (format
&& imageOpts
) {
524 error_report("--image-opts and -f are mutually exclusive");
528 if (qemu_init_main_loop(&local_error
)) {
529 error_report_err(local_error
);
533 if (qemu_opts_foreach(&qemu_object_opts
,
534 user_creatable_add_opts_foreach
,
535 NULL
, &local_error
)) {
536 error_report_err(local_error
);
540 /* initialize commands */
541 qemuio_add_command(&quit_cmd
);
542 qemuio_add_command(&open_cmd
);
543 qemuio_add_command(&close_cmd
);
545 if (isatty(STDIN_FILENO
)) {
546 readline_state
= readline_init(readline_printf_func
,
549 readline_completion_func
);
550 qemu_set_tty_echo(STDIN_FILENO
, false);
551 atexit(reenable_tty_echo
);
554 /* open the device */
556 flags
|= BDRV_O_RDWR
;
559 if ((argc
- optind
) == 1) {
561 QemuOpts
*qopts
= NULL
;
562 qopts
= qemu_opts_parse_noisily(&file_opts
, argv
[optind
], false);
566 opts
= qemu_opts_to_qdict(qopts
, NULL
);
567 openfile(NULL
, flags
, writethrough
, opts
);
571 qdict_put(opts
, "driver", qstring_from_str(format
));
573 openfile(argv
[optind
], flags
, writethrough
, opts
);
579 * Make sure all outstanding requests complete before the program exits.
583 blk_unref(qemuio_blk
);
584 g_free(readline_state
);