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"
15 #include "qemu/error-report.h"
16 #include "qemu/main-loop.h"
17 #include "qemu/option.h"
18 #include "qemu/config-file.h"
19 #include "qemu/readline.h"
20 #include "qapi/qmp/qstring.h"
21 #include "qom/object_interfaces.h"
22 #include "sysemu/block-backend.h"
23 #include "block/block_int.h"
24 #include "trace/control.h"
26 #define CMD_NOFILE_OK 0x01
28 static char *progname
;
30 static BlockBackend
*qemuio_blk
;
32 /* qemu-io commands passed using -c */
34 static char **cmdline
;
36 static ReadLineState
*readline_state
;
38 static int close_f(BlockBackend
*blk
, int argc
, char **argv
)
40 blk_unref(qemuio_blk
);
45 static const cmdinfo_t close_cmd
= {
49 .oneline
= "close the current open file",
52 static int openfile(char *name
, int flags
, QDict
*opts
)
54 Error
*local_err
= NULL
;
58 error_report("file open already, try 'help close'");
63 qemuio_blk
= blk_new_open("hda", name
, NULL
, opts
, flags
, &local_err
);
65 error_reportf_err(local_err
, "can't open%s%s: ",
66 name
? " device " : "", name
?: "");
70 bs
= blk_bs(qemuio_blk
);
71 if (bdrv_is_encrypted(bs
)) {
73 printf("Disk image '%s' is encrypted.\n", name
);
74 if (qemu_read_password(password
, sizeof(password
)) < 0) {
75 error_report("No password given");
78 if (bdrv_set_key(bs
, password
) < 0) {
79 error_report("invalid password");
88 blk_unref(qemuio_blk
);
93 static void open_help(void)
97 " opens a new file in the requested mode\n"
100 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
102 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
103 " -r, -- open file read-only\n"
104 " -s, -- use snapshot file\n"
105 " -n, -- disable host cache\n"
106 " -o, -- options to be given to the block driver"
110 static int open_f(BlockBackend
*blk
, int argc
, char **argv
);
112 static const cmdinfo_t open_cmd
= {
118 .flags
= CMD_NOFILE_OK
,
119 .args
= "[-Crsn] [-o options] [path]",
120 .oneline
= "open the file specified by path",
124 static QemuOptsList empty_opts
= {
127 .head
= QTAILQ_HEAD_INITIALIZER(empty_opts
.head
),
129 /* no elements => accept any params */
130 { /* end of list */ }
134 static int open_f(BlockBackend
*blk
, int argc
, char **argv
)
142 while ((c
= getopt(argc
, argv
, "snrgo:")) != -1) {
145 flags
|= BDRV_O_SNAPSHOT
;
148 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
154 if (!qemu_opts_parse_noisily(&empty_opts
, optarg
, false)) {
155 qemu_opts_reset(&empty_opts
);
160 qemu_opts_reset(&empty_opts
);
161 return qemuio_command_usage(&open_cmd
);
166 flags
|= BDRV_O_RDWR
;
169 qopts
= qemu_opts_find(&empty_opts
, NULL
);
170 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
171 qemu_opts_reset(&empty_opts
);
173 if (optind
== argc
- 1) {
174 return openfile(argv
[optind
], flags
, opts
);
175 } else if (optind
== argc
) {
176 return openfile(NULL
, flags
, opts
);
179 return qemuio_command_usage(&open_cmd
);
183 static int quit_f(BlockBackend
*blk
, int argc
, char **argv
)
188 static const cmdinfo_t quit_cmd
= {
194 .flags
= CMD_FLAG_GLOBAL
,
195 .oneline
= "exit the program",
198 static void usage(const char *name
)
201 "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
202 "QEMU Disk exerciser\n"
204 " --object OBJECTDEF define an object such as 'secret' for\n"
205 " passwords and/or encryption keys\n"
206 " -c, --cmd STRING execute command with its arguments\n"
207 " from the given string\n"
208 " -f, --format FMT specifies the block driver to use\n"
209 " -r, --read-only export read-only\n"
210 " -s, --snapshot use snapshot file\n"
211 " -n, --nocache disable host cache\n"
212 " -m, --misalign misalign allocations for O_DIRECT\n"
213 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
214 " -t, --cache=MODE use the given cache mode for the image\n"
215 " -T, --trace FILE enable trace events listed in the given file\n"
216 " -h, --help display this help and exit\n"
217 " -V, --version output version information and exit\n"
219 "See '%s -c help' for information on available commands."
224 static char *get_prompt(void)
226 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
229 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
235 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque
,
236 const char *fmt
, ...)
244 static void readline_flush_func(void *opaque
)
249 static void readline_func(void *opaque
, const char *str
, void *readline_opaque
)
251 char **line
= readline_opaque
;
252 *line
= g_strdup(str
);
255 static void completion_match(const char *cmd
, void *opaque
)
257 readline_add_completion(readline_state
, cmd
);
260 static void readline_completion_func(void *opaque
, const char *str
)
262 readline_set_completion_index(readline_state
, strlen(str
));
263 qemuio_complete_command(str
, completion_match
, NULL
);
266 static char *fetchline_readline(void)
270 readline_start(readline_state
, get_prompt(), 0, readline_func
, &line
);
276 readline_handle_byte(readline_state
, ch
);
281 #define MAXREADLINESZ 1024
282 static char *fetchline_fgets(void)
284 char *p
, *line
= g_malloc(MAXREADLINESZ
);
286 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
291 p
= line
+ strlen(line
);
292 if (p
!= line
&& p
[-1] == '\n') {
299 static char *fetchline(void)
301 if (readline_state
) {
302 return fetchline_readline();
304 return fetchline_fgets();
308 static void prep_fetchline(void *opaque
)
310 int *fetchable
= opaque
;
312 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
316 static void command_loop(void)
318 int i
, done
= 0, fetchable
= 0, prompted
= 0;
321 for (i
= 0; !done
&& i
< ncmdline
; i
++) {
322 done
= qemuio_command(qemuio_blk
, cmdline
[i
]);
331 printf("%s", get_prompt());
333 qemu_set_fd_handler(STDIN_FILENO
, prep_fetchline
, NULL
, &fetchable
);
337 main_loop_wait(false);
347 done
= qemuio_command(qemuio_blk
, input
);
353 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
356 static void add_user_command(char *optarg
)
358 cmdline
= g_renew(char *, cmdline
, ++ncmdline
);
359 cmdline
[ncmdline
-1] = optarg
;
362 static void reenable_tty_echo(void)
364 qemu_set_tty_echo(STDIN_FILENO
, true);
371 static QemuOptsList qemu_object_opts
= {
373 .implied_opt_name
= "qom-type",
374 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
381 int main(int argc
, char **argv
)
384 const char *sopt
= "hVc:d:f:rsnmgkt:T:";
385 const struct option lopt
[] = {
386 { "help", 0, NULL
, 'h' },
387 { "version", 0, NULL
, 'V' },
388 { "offset", 1, NULL
, 'o' },
389 { "cmd", 1, NULL
, 'c' },
390 { "format", 1, NULL
, 'f' },
391 { "read-only", 0, NULL
, 'r' },
392 { "snapshot", 0, NULL
, 's' },
393 { "nocache", 0, NULL
, 'n' },
394 { "misalign", 0, NULL
, 'm' },
395 { "native-aio", 0, NULL
, 'k' },
396 { "discard", 1, NULL
, 'd' },
397 { "cache", 1, NULL
, 't' },
398 { "trace", 1, NULL
, 'T' },
399 { "object", 1, NULL
, OPTION_OBJECT
},
404 int flags
= BDRV_O_UNMAP
;
405 Error
*local_error
= NULL
;
409 signal(SIGPIPE
, SIG_IGN
);
412 progname
= basename(argv
[0]);
413 qemu_init_exec_dir(argv
[0]);
415 module_call_init(MODULE_INIT_QOM
);
416 qemu_add_opts(&qemu_object_opts
);
419 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
422 flags
|= BDRV_O_SNAPSHOT
;
425 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
428 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
429 error_report("Invalid discard option: %s", optarg
);
437 qdict_put(opts
, "driver", qstring_from_str(optarg
));
440 add_user_command(optarg
);
446 qemuio_misalign
= true;
449 flags
|= BDRV_O_NATIVE_AIO
;
452 if (bdrv_parse_cache_flags(optarg
, &flags
) < 0) {
453 error_report("Invalid cache option: %s", optarg
);
458 if (!trace_init_backends()) {
459 exit(1); /* error message will have been printed */
463 printf("%s version %s\n", progname
, QEMU_VERSION
);
468 case OPTION_OBJECT
: {
469 QemuOpts
*qopts
= NULL
;
470 qopts
= qemu_opts_parse_noisily(&qemu_object_opts
,
482 if ((argc
- optind
) > 1) {
487 if (qemu_init_main_loop(&local_error
)) {
488 error_report_err(local_error
);
492 if (qemu_opts_foreach(&qemu_object_opts
,
493 user_creatable_add_opts_foreach
,
494 NULL
, &local_error
)) {
495 error_report_err(local_error
);
499 /* initialize commands */
500 qemuio_add_command(&quit_cmd
);
501 qemuio_add_command(&open_cmd
);
502 qemuio_add_command(&close_cmd
);
504 if (isatty(STDIN_FILENO
)) {
505 readline_state
= readline_init(readline_printf_func
,
508 readline_completion_func
);
509 qemu_set_tty_echo(STDIN_FILENO
, false);
510 atexit(reenable_tty_echo
);
513 /* open the device */
515 flags
|= BDRV_O_RDWR
;
518 if ((argc
- optind
) == 1) {
519 openfile(argv
[optind
], flags
, opts
);
524 * Make sure all outstanding requests complete before the program exits.
528 blk_unref(qemuio_blk
);
529 g_free(readline_state
);