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 "sysemu/block-backend.h"
22 #include "block/block_int.h"
23 #include "trace/control.h"
25 #define CMD_NOFILE_OK 0x01
27 static char *progname
;
29 static BlockBackend
*qemuio_blk
;
31 /* qemu-io commands passed using -c */
33 static char **cmdline
;
35 static ReadLineState
*readline_state
;
37 static int close_f(BlockBackend
*blk
, int argc
, char **argv
)
39 blk_unref(qemuio_blk
);
44 static const cmdinfo_t close_cmd
= {
48 .oneline
= "close the current open file",
51 static int openfile(char *name
, int flags
, QDict
*opts
)
53 Error
*local_err
= NULL
;
57 error_report("file open already, try 'help close'");
62 qemuio_blk
= blk_new_open("hda", name
, NULL
, opts
, flags
, &local_err
);
64 error_reportf_err(local_err
, "can't open%s%s: ",
65 name
? " device " : "", name
?: "");
69 bs
= blk_bs(qemuio_blk
);
70 if (bdrv_is_encrypted(bs
)) {
72 printf("Disk image '%s' is encrypted.\n", name
);
73 if (qemu_read_password(password
, sizeof(password
)) < 0) {
74 error_report("No password given");
77 if (bdrv_set_key(bs
, password
) < 0) {
78 error_report("invalid password");
87 blk_unref(qemuio_blk
);
92 static void open_help(void)
96 " opens a new file in the requested mode\n"
99 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
101 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
102 " -r, -- open file read-only\n"
103 " -s, -- use snapshot file\n"
104 " -n, -- disable host cache\n"
105 " -o, -- options to be given to the block driver"
109 static int open_f(BlockBackend
*blk
, int argc
, char **argv
);
111 static const cmdinfo_t open_cmd
= {
117 .flags
= CMD_NOFILE_OK
,
118 .args
= "[-Crsn] [-o options] [path]",
119 .oneline
= "open the file specified by path",
123 static QemuOptsList empty_opts
= {
126 .head
= QTAILQ_HEAD_INITIALIZER(empty_opts
.head
),
128 /* no elements => accept any params */
129 { /* end of list */ }
133 static int open_f(BlockBackend
*blk
, int argc
, char **argv
)
141 while ((c
= getopt(argc
, argv
, "snrgo:")) != -1) {
144 flags
|= BDRV_O_SNAPSHOT
;
147 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
153 if (!qemu_opts_parse_noisily(&empty_opts
, optarg
, false)) {
154 qemu_opts_reset(&empty_opts
);
159 qemu_opts_reset(&empty_opts
);
160 return qemuio_command_usage(&open_cmd
);
165 flags
|= BDRV_O_RDWR
;
168 qopts
= qemu_opts_find(&empty_opts
, NULL
);
169 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
170 qemu_opts_reset(&empty_opts
);
172 if (optind
== argc
- 1) {
173 return openfile(argv
[optind
], flags
, opts
);
174 } else if (optind
== argc
) {
175 return openfile(NULL
, flags
, opts
);
178 return qemuio_command_usage(&open_cmd
);
182 static int quit_f(BlockBackend
*blk
, int argc
, char **argv
)
187 static const cmdinfo_t quit_cmd
= {
193 .flags
= CMD_FLAG_GLOBAL
,
194 .oneline
= "exit the program",
197 static void usage(const char *name
)
200 "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
201 "QEMU Disk exerciser\n"
203 " -c, --cmd STRING execute command with its arguments\n"
204 " from the given string\n"
205 " -f, --format FMT specifies the block driver to use\n"
206 " -r, --read-only export read-only\n"
207 " -s, --snapshot use snapshot file\n"
208 " -n, --nocache disable host cache\n"
209 " -m, --misalign misalign allocations for O_DIRECT\n"
210 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
211 " -t, --cache=MODE use the given cache mode for the image\n"
212 " -T, --trace FILE enable trace events listed in the given file\n"
213 " -h, --help display this help and exit\n"
214 " -V, --version output version information and exit\n"
216 "See '%s -c help' for information on available commands."
221 static char *get_prompt(void)
223 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
226 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
232 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque
,
233 const char *fmt
, ...)
241 static void readline_flush_func(void *opaque
)
246 static void readline_func(void *opaque
, const char *str
, void *readline_opaque
)
248 char **line
= readline_opaque
;
249 *line
= g_strdup(str
);
252 static void completion_match(const char *cmd
, void *opaque
)
254 readline_add_completion(readline_state
, cmd
);
257 static void readline_completion_func(void *opaque
, const char *str
)
259 readline_set_completion_index(readline_state
, strlen(str
));
260 qemuio_complete_command(str
, completion_match
, NULL
);
263 static char *fetchline_readline(void)
267 readline_start(readline_state
, get_prompt(), 0, readline_func
, &line
);
273 readline_handle_byte(readline_state
, ch
);
278 #define MAXREADLINESZ 1024
279 static char *fetchline_fgets(void)
281 char *p
, *line
= g_malloc(MAXREADLINESZ
);
283 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
288 p
= line
+ strlen(line
);
289 if (p
!= line
&& p
[-1] == '\n') {
296 static char *fetchline(void)
298 if (readline_state
) {
299 return fetchline_readline();
301 return fetchline_fgets();
305 static void prep_fetchline(void *opaque
)
307 int *fetchable
= opaque
;
309 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
313 static void command_loop(void)
315 int i
, done
= 0, fetchable
= 0, prompted
= 0;
318 for (i
= 0; !done
&& i
< ncmdline
; i
++) {
319 done
= qemuio_command(qemuio_blk
, cmdline
[i
]);
328 printf("%s", get_prompt());
330 qemu_set_fd_handler(STDIN_FILENO
, prep_fetchline
, NULL
, &fetchable
);
334 main_loop_wait(false);
344 done
= qemuio_command(qemuio_blk
, input
);
350 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
353 static void add_user_command(char *optarg
)
355 cmdline
= g_renew(char *, cmdline
, ++ncmdline
);
356 cmdline
[ncmdline
-1] = optarg
;
359 static void reenable_tty_echo(void)
361 qemu_set_tty_echo(STDIN_FILENO
, true);
364 int main(int argc
, char **argv
)
367 const char *sopt
= "hVc:d:f:rsnmgkt:T:";
368 const struct option lopt
[] = {
369 { "help", 0, NULL
, 'h' },
370 { "version", 0, NULL
, 'V' },
371 { "offset", 1, NULL
, 'o' },
372 { "cmd", 1, NULL
, 'c' },
373 { "format", 1, NULL
, 'f' },
374 { "read-only", 0, NULL
, 'r' },
375 { "snapshot", 0, NULL
, 's' },
376 { "nocache", 0, NULL
, 'n' },
377 { "misalign", 0, NULL
, 'm' },
378 { "native-aio", 0, NULL
, 'k' },
379 { "discard", 1, NULL
, 'd' },
380 { "cache", 1, NULL
, 't' },
381 { "trace", 1, NULL
, 'T' },
386 int flags
= BDRV_O_UNMAP
;
387 Error
*local_error
= NULL
;
391 signal(SIGPIPE
, SIG_IGN
);
394 progname
= basename(argv
[0]);
395 qemu_init_exec_dir(argv
[0]);
399 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
402 flags
|= BDRV_O_SNAPSHOT
;
405 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
408 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
409 error_report("Invalid discard option: %s", optarg
);
417 qdict_put(opts
, "driver", qstring_from_str(optarg
));
420 add_user_command(optarg
);
426 qemuio_misalign
= true;
429 flags
|= BDRV_O_NATIVE_AIO
;
432 if (bdrv_parse_cache_flags(optarg
, &flags
) < 0) {
433 error_report("Invalid cache option: %s", optarg
);
438 if (!trace_init_backends()) {
439 exit(1); /* error message will have been printed */
443 printf("%s version %s\n", progname
, QEMU_VERSION
);
454 if ((argc
- optind
) > 1) {
459 if (qemu_init_main_loop(&local_error
)) {
460 error_report_err(local_error
);
464 /* initialize commands */
465 qemuio_add_command(&quit_cmd
);
466 qemuio_add_command(&open_cmd
);
467 qemuio_add_command(&close_cmd
);
469 if (isatty(STDIN_FILENO
)) {
470 readline_state
= readline_init(readline_printf_func
,
473 readline_completion_func
);
474 qemu_set_tty_echo(STDIN_FILENO
, false);
475 atexit(reenable_tty_echo
);
478 /* open the device */
480 flags
|= BDRV_O_RDWR
;
483 if ((argc
- optind
) == 1) {
484 openfile(argv
[optind
], flags
, opts
);
489 * Make sure all outstanding requests complete before the program exits.
493 blk_unref(qemuio_blk
);
494 g_free(readline_state
);