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 -Cn /tmp/data' - creates/opens data file read-write and 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\n"
110 " -o, -- options to be given to the block driver"
114 static int open_f(BlockBackend
*blk
, int argc
, char **argv
);
116 static const cmdinfo_t open_cmd
= {
122 .flags
= CMD_NOFILE_OK
,
123 .args
= "[-Crsn] [-o options] [path]",
124 .oneline
= "open the file specified by path",
128 static QemuOptsList empty_opts
= {
131 .head
= QTAILQ_HEAD_INITIALIZER(empty_opts
.head
),
133 /* no elements => accept any params */
134 { /* end of list */ }
138 static int open_f(BlockBackend
*blk
, int argc
, char **argv
)
142 bool writethrough
= true;
147 while ((c
= getopt(argc
, argv
, "snrgo:")) != -1) {
150 flags
|= BDRV_O_SNAPSHOT
;
153 flags
|= BDRV_O_NOCACHE
;
154 writethrough
= false;
161 printf("--image-opts and 'open -o' are mutually exclusive\n");
164 if (!qemu_opts_parse_noisily(&empty_opts
, optarg
, false)) {
165 qemu_opts_reset(&empty_opts
);
170 qemu_opts_reset(&empty_opts
);
171 return qemuio_command_usage(&open_cmd
);
176 flags
|= BDRV_O_RDWR
;
179 if (imageOpts
&& (optind
== argc
- 1)) {
180 if (!qemu_opts_parse_noisily(&empty_opts
, argv
[optind
], false)) {
181 qemu_opts_reset(&empty_opts
);
187 qopts
= qemu_opts_find(&empty_opts
, NULL
);
188 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
189 qemu_opts_reset(&empty_opts
);
191 if (optind
== argc
- 1) {
192 return openfile(argv
[optind
], flags
, writethrough
, opts
);
193 } else if (optind
== argc
) {
194 return openfile(NULL
, flags
, writethrough
, opts
);
197 return qemuio_command_usage(&open_cmd
);
201 static int quit_f(BlockBackend
*blk
, int argc
, char **argv
)
206 #if defined(CONFIG_FVD)
207 # include "qemu-io-sim.c"
210 static const cmdinfo_t quit_cmd
= {
216 .flags
= CMD_FLAG_GLOBAL
,
217 .oneline
= "exit the program",
220 static void usage(const char *name
)
223 "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
224 "QEMU Disk exerciser\n"
226 " --object OBJECTDEF define an object such as 'secret' for\n"
227 " passwords and/or encryption keys\n"
228 " -c, --cmd STRING execute command with its arguments\n"
229 " from the given string\n"
230 " -f, --format FMT specifies the block driver to use\n"
231 " -r, --read-only export read-only\n"
232 " -s, --snapshot use snapshot file\n"
233 " -n, --nocache disable host cache\n"
234 " -m, --misalign misalign allocations for O_DIRECT\n"
235 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
236 " -t, --cache=MODE use the given cache mode for the image\n"
237 " -T, --trace FILE enable trace events listed in the given file\n"
238 " -h, --help display this help and exit\n"
239 " -V, --version output version information and exit\n"
241 "See '%s -c help' for information on available commands."
246 static char *get_prompt(void)
248 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
251 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
257 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque
,
258 const char *fmt
, ...)
266 static void readline_flush_func(void *opaque
)
271 static void readline_func(void *opaque
, const char *str
, void *readline_opaque
)
273 char **line
= readline_opaque
;
274 *line
= g_strdup(str
);
277 static void completion_match(const char *cmd
, void *opaque
)
279 readline_add_completion(readline_state
, cmd
);
282 static void readline_completion_func(void *opaque
, const char *str
)
284 readline_set_completion_index(readline_state
, strlen(str
));
285 qemuio_complete_command(str
, completion_match
, NULL
);
288 static char *fetchline_readline(void)
292 readline_start(readline_state
, get_prompt(), 0, readline_func
, &line
);
298 readline_handle_byte(readline_state
, ch
);
303 #define MAXREADLINESZ 1024
304 static char *fetchline_fgets(void)
306 char *p
, *line
= g_malloc(MAXREADLINESZ
);
308 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
313 p
= line
+ strlen(line
);
314 if (p
!= line
&& p
[-1] == '\n') {
321 static char *fetchline(void)
323 if (readline_state
) {
324 return fetchline_readline();
326 return fetchline_fgets();
330 static void prep_fetchline(void *opaque
)
332 int *fetchable
= opaque
;
334 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
338 static void command_loop(void)
340 int i
, done
= 0, fetchable
= 0, prompted
= 0;
343 for (i
= 0; !done
&& i
< ncmdline
; i
++) {
344 done
= qemuio_command(qemuio_blk
, cmdline
[i
]);
353 printf("%s", get_prompt());
355 qemu_set_fd_handler(STDIN_FILENO
, prep_fetchline
, NULL
, &fetchable
);
359 main_loop_wait(false);
369 done
= qemuio_command(qemuio_blk
, input
);
375 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
378 static void add_user_command(char *optarg
)
380 cmdline
= g_renew(char *, cmdline
, ++ncmdline
);
381 cmdline
[ncmdline
-1] = optarg
;
384 static void reenable_tty_echo(void)
386 qemu_set_tty_echo(STDIN_FILENO
, true);
391 OPTION_IMAGE_OPTS
= 257,
394 static QemuOptsList qemu_object_opts
= {
396 .implied_opt_name
= "qom-type",
397 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
404 static QemuOptsList file_opts
= {
406 .implied_opt_name
= "file",
407 .head
= QTAILQ_HEAD_INITIALIZER(file_opts
.head
),
409 /* no elements => accept any params */
410 { /* end of list */ }
414 int main(int argc
, char **argv
)
417 const char *sopt
= "hVc:d:f:rsnmgkt:T:";
418 const struct option lopt
[] = {
419 { "help", no_argument
, NULL
, 'h' },
420 { "version", no_argument
, NULL
, 'V' },
421 { "offset", required_argument
, NULL
, 'o' },
422 { "cmd", required_argument
, NULL
, 'c' },
423 { "format", required_argument
, NULL
, 'f' },
424 { "read-only", no_argument
, NULL
, 'r' },
425 { "snapshot", no_argument
, NULL
, 's' },
426 { "nocache", no_argument
, NULL
, 'n' },
427 { "misalign", no_argument
, NULL
, 'm' },
428 { "native-aio", no_argument
, NULL
, 'k' },
429 { "discard", required_argument
, NULL
, 'd' },
430 { "cache", required_argument
, NULL
, 't' },
431 { "trace", required_argument
, NULL
, 'T' },
432 { "object", required_argument
, NULL
, OPTION_OBJECT
},
433 { "image-opts", no_argument
, NULL
, OPTION_IMAGE_OPTS
},
438 int flags
= BDRV_O_UNMAP
;
439 bool writethrough
= true;
440 Error
*local_error
= NULL
;
442 const char *format
= NULL
;
445 signal(SIGPIPE
, SIG_IGN
);
448 progname
= basename(argv
[0]);
449 qemu_init_exec_dir(argv
[0]);
451 if (qcrypto_init(&local_error
) < 0) {
452 error_reportf_err(local_error
, "cannot initialize crypto: ");
456 module_call_init(MODULE_INIT_QOM
);
457 qemu_add_opts(&qemu_object_opts
);
460 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
463 flags
|= BDRV_O_SNAPSHOT
;
466 flags
|= BDRV_O_NOCACHE
;
467 writethrough
= false;
470 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
471 error_report("Invalid discard option: %s", optarg
);
479 add_user_command(optarg
);
485 qemuio_misalign
= true;
488 flags
|= BDRV_O_NATIVE_AIO
;
491 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
492 error_report("Invalid cache option: %s", optarg
);
497 if (!trace_init_backends()) {
498 exit(1); /* error message will have been printed */
502 printf("%s version %s\n", progname
, QEMU_VERSION
);
507 case OPTION_OBJECT
: {
509 qopts
= qemu_opts_parse_noisily(&qemu_object_opts
,
515 case OPTION_IMAGE_OPTS
:
524 if ((argc
- optind
) > 1) {
529 if (format
&& imageOpts
) {
530 error_report("--image-opts and -f are mutually exclusive");
534 if (qemu_init_main_loop(&local_error
)) {
535 error_report_err(local_error
);
539 if (qemu_opts_foreach(&qemu_object_opts
,
540 user_creatable_add_opts_foreach
,
545 /* initialize commands */
546 qemuio_add_command(&quit_cmd
);
547 qemuio_add_command(&open_cmd
);
548 qemuio_add_command(&close_cmd
);
550 if (isatty(STDIN_FILENO
)) {
551 readline_state
= readline_init(readline_printf_func
,
554 readline_completion_func
);
555 qemu_set_tty_echo(STDIN_FILENO
, false);
556 atexit(reenable_tty_echo
);
559 /* open the device */
561 flags
|= BDRV_O_RDWR
;
564 if ((argc
- optind
) == 1) {
566 QemuOpts
*qopts
= NULL
;
567 qopts
= qemu_opts_parse_noisily(&file_opts
, argv
[optind
], false);
571 opts
= qemu_opts_to_qdict(qopts
, NULL
);
572 openfile(NULL
, flags
, writethrough
, opts
);
576 qdict_put(opts
, "driver", qstring_from_str(format
));
578 openfile(argv
[optind
], flags
, writethrough
, opts
);
584 * Make sure all outstanding requests complete before the program exits.
588 blk_unref(qemuio_blk
);
589 g_free(readline_state
);