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.
11 #include <sys/types.h>
18 #include "qemu/error-report.h"
19 #include "qemu/main-loop.h"
20 #include "qemu/option.h"
21 #include "qemu/config-file.h"
22 #include "qemu/readline.h"
23 #include "qapi/qmp/qstring.h"
24 #include "sysemu/block-backend.h"
25 #include "block/block_int.h"
26 #include "trace/control.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
;
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
, QDict
*opts
)
56 Error
*local_err
= NULL
;
60 error_report("file open already, try 'help close'");
65 qemuio_blk
= blk_new_open("hda", 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
)) {
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");
90 blk_unref(qemuio_blk
);
95 static void open_help(void)
99 " opens a new file in the requested mode\n"
102 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
104 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
105 " -r, -- open file read-only\n"
106 " -s, -- use snapshot file\n"
107 " -n, -- disable host cache\n"
108 " -o, -- options to be given to the block driver"
112 static int open_f(BlockBackend
*blk
, int argc
, char **argv
);
114 static const cmdinfo_t open_cmd
= {
120 .flags
= CMD_NOFILE_OK
,
121 .args
= "[-Crsn] [-o options] [path]",
122 .oneline
= "open the file specified by path",
126 static QemuOptsList empty_opts
= {
129 .head
= QTAILQ_HEAD_INITIALIZER(empty_opts
.head
),
131 /* no elements => accept any params */
132 { /* end of list */ }
136 static int open_f(BlockBackend
*blk
, int argc
, char **argv
)
144 while ((c
= getopt(argc
, argv
, "snrgo:")) != -1) {
147 flags
|= BDRV_O_SNAPSHOT
;
150 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
156 if (!qemu_opts_parse_noisily(&empty_opts
, optarg
, false)) {
157 qemu_opts_reset(&empty_opts
);
162 qemu_opts_reset(&empty_opts
);
163 return qemuio_command_usage(&open_cmd
);
168 flags
|= BDRV_O_RDWR
;
171 qopts
= qemu_opts_find(&empty_opts
, NULL
);
172 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
173 qemu_opts_reset(&empty_opts
);
175 if (optind
== argc
- 1) {
176 return openfile(argv
[optind
], flags
, opts
);
177 } else if (optind
== argc
) {
178 return openfile(NULL
, flags
, opts
);
181 return qemuio_command_usage(&open_cmd
);
185 static int quit_f(BlockBackend
*blk
, int argc
, char **argv
)
190 static const cmdinfo_t quit_cmd
= {
196 .flags
= CMD_FLAG_GLOBAL
,
197 .oneline
= "exit the program",
200 static void usage(const char *name
)
203 "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
204 "QEMU Disk exerciser\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);
367 int main(int argc
, char **argv
)
370 const char *sopt
= "hVc:d:f:rsnmgkt:T:";
371 const struct option lopt
[] = {
372 { "help", 0, NULL
, 'h' },
373 { "version", 0, NULL
, 'V' },
374 { "offset", 1, NULL
, 'o' },
375 { "cmd", 1, NULL
, 'c' },
376 { "format", 1, NULL
, 'f' },
377 { "read-only", 0, NULL
, 'r' },
378 { "snapshot", 0, NULL
, 's' },
379 { "nocache", 0, NULL
, 'n' },
380 { "misalign", 0, NULL
, 'm' },
381 { "native-aio", 0, NULL
, 'k' },
382 { "discard", 1, NULL
, 'd' },
383 { "cache", 1, NULL
, 't' },
384 { "trace", 1, NULL
, 'T' },
389 int flags
= BDRV_O_UNMAP
;
390 Error
*local_error
= NULL
;
394 signal(SIGPIPE
, SIG_IGN
);
397 progname
= basename(argv
[0]);
398 qemu_init_exec_dir(argv
[0]);
402 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
405 flags
|= BDRV_O_SNAPSHOT
;
408 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
411 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
412 error_report("Invalid discard option: %s", optarg
);
420 qdict_put(opts
, "driver", qstring_from_str(optarg
));
423 add_user_command(optarg
);
429 qemuio_misalign
= true;
432 flags
|= BDRV_O_NATIVE_AIO
;
435 if (bdrv_parse_cache_flags(optarg
, &flags
) < 0) {
436 error_report("Invalid cache option: %s", optarg
);
441 if (!trace_init_backends(optarg
, NULL
)) {
442 exit(1); /* error message will have been printed */
446 printf("%s version %s\n", progname
, QEMU_VERSION
);
457 if ((argc
- optind
) > 1) {
462 if (qemu_init_main_loop(&local_error
)) {
463 error_report_err(local_error
);
467 /* initialize commands */
468 qemuio_add_command(&quit_cmd
);
469 qemuio_add_command(&open_cmd
);
470 qemuio_add_command(&close_cmd
);
472 if (isatty(STDIN_FILENO
)) {
473 readline_state
= readline_init(readline_printf_func
,
476 readline_completion_func
);
477 qemu_set_tty_echo(STDIN_FILENO
, false);
478 atexit(reenable_tty_echo
);
481 /* open the device */
483 flags
|= BDRV_O_RDWR
;
486 if ((argc
- optind
) == 1) {
487 openfile(argv
[optind
], flags
, opts
);
492 * Make sure all outstanding requests complete before the program exits.
496 blk_unref(qemuio_blk
);
497 g_free(readline_state
);