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/main-loop.h"
19 #include "block/block_int.h"
20 #include "trace/control.h"
22 #define CMD_NOFILE_OK 0x01
26 BlockDriverState
*qemuio_bs
;
27 extern int qemuio_misalign
;
29 /* qemu-io commands passed using -c */
31 static char **cmdline
;
33 static int close_f(BlockDriverState
*bs
, int argc
, char **argv
)
40 static const cmdinfo_t close_cmd
= {
44 .oneline
= "close the current open file",
47 static int openfile(char *name
, int flags
, int growable
)
49 Error
*local_err
= NULL
;
52 fprintf(stderr
, "file open already, try 'help close'\n");
57 if (bdrv_file_open(&qemuio_bs
, name
, NULL
, flags
, &local_err
)) {
58 fprintf(stderr
, "%s: can't open device %s: %s\n", progname
, name
,
59 error_get_pretty(local_err
));
60 error_free(local_err
);
64 qemuio_bs
= bdrv_new("hda");
66 if (bdrv_open(qemuio_bs
, name
, NULL
, flags
, NULL
, &local_err
) < 0) {
67 fprintf(stderr
, "%s: can't open device %s: %s\n", progname
, name
,
68 error_get_pretty(local_err
));
69 error_free(local_err
);
70 bdrv_unref(qemuio_bs
);
79 static void open_help(void)
83 " opens a new file in the requested mode\n"
86 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
88 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
89 " -r, -- open file read-only\n"
90 " -s, -- use snapshot file\n"
91 " -n, -- disable host cache\n"
92 " -g, -- allow file to grow (only applies to protocols)"
96 static int open_f(BlockDriverState
*bs
, int argc
, char **argv
);
98 static const cmdinfo_t open_cmd
= {
104 .flags
= CMD_NOFILE_OK
,
105 .args
= "[-Crsn] [path]",
106 .oneline
= "open the file specified by path",
110 static int open_f(BlockDriverState
*bs
, int argc
, char **argv
)
117 while ((c
= getopt(argc
, argv
, "snrg")) != EOF
) {
120 flags
|= BDRV_O_SNAPSHOT
;
123 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
132 return qemuio_command_usage(&open_cmd
);
137 flags
|= BDRV_O_RDWR
;
140 if (optind
!= argc
- 1) {
141 return qemuio_command_usage(&open_cmd
);
144 return openfile(argv
[optind
], flags
, growable
);
147 static int quit_f(BlockDriverState
*bs
, int argc
, char **argv
)
152 static const cmdinfo_t quit_cmd
= {
158 .flags
= CMD_FLAG_GLOBAL
,
159 .oneline
= "exit the program",
162 static void usage(const char *name
)
165 "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
166 "QEMU Disk exerciser\n"
168 " -c, --cmd command to execute\n"
169 " -r, --read-only export read-only\n"
170 " -s, --snapshot use snapshot file\n"
171 " -n, --nocache disable host cache\n"
172 " -g, --growable allow file to grow (only applies to protocols)\n"
173 " -m, --misalign misalign allocations for O_DIRECT\n"
174 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
175 " -t, --cache=MODE use the given cache mode for the image\n"
176 " -T, --trace FILE enable trace events listed in the given file\n"
177 " -h, --help display this help and exit\n"
178 " -V, --version output version information and exit\n"
184 #if defined(ENABLE_READLINE)
185 # include <readline/history.h>
186 # include <readline/readline.h>
187 #elif defined(ENABLE_EDITLINE)
188 # include <histedit.h>
191 static char *get_prompt(void)
193 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
196 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
202 #if defined(ENABLE_READLINE)
203 static char *fetchline(void)
205 char *line
= readline(get_prompt());
211 #elif defined(ENABLE_EDITLINE)
212 static char *el_get_prompt(EditLine
*e
)
217 static char *fetchline(void)
220 static History
*hist
;
226 hist
= history_init();
227 history(hist
, &hevent
, H_SETSIZE
, 100);
228 el
= el_init(progname
, stdin
, stdout
, stderr
);
230 el_set(el
, EL_SIGNAL
, 1);
231 el_set(el
, EL_PROMPT
, el_get_prompt
);
232 el_set(el
, EL_HIST
, history
, (const char *)hist
);
234 line
= strdup(el_gets(el
, &count
));
237 line
[count
-1] = '\0';
240 history(hist
, &hevent
, H_ENTER
, line
);
246 # define MAXREADLINESZ 1024
247 static char *fetchline(void)
249 char *p
, *line
= g_malloc(MAXREADLINESZ
);
251 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
256 p
= line
+ strlen(line
);
257 if (p
!= line
&& p
[-1] == '\n') {
265 static void prep_fetchline(void *opaque
)
267 int *fetchable
= opaque
;
269 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
273 static void command_loop(void)
275 int i
, done
= 0, fetchable
= 0, prompted
= 0;
278 for (i
= 0; !done
&& i
< ncmdline
; i
++) {
279 done
= qemuio_command(qemuio_bs
, cmdline
[i
]);
288 printf("%s", get_prompt());
290 qemu_set_fd_handler(STDIN_FILENO
, prep_fetchline
, NULL
, &fetchable
);
294 main_loop_wait(false);
304 done
= qemuio_command(qemuio_bs
, input
);
310 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
313 static void add_user_command(char *optarg
)
315 cmdline
= g_realloc(cmdline
, ++ncmdline
* sizeof(char *));
316 cmdline
[ncmdline
-1] = optarg
;
319 int main(int argc
, char **argv
)
323 const char *sopt
= "hVc:d:rsnmgkt:T:";
324 const struct option lopt
[] = {
325 { "help", 0, NULL
, 'h' },
326 { "version", 0, NULL
, 'V' },
327 { "offset", 1, NULL
, 'o' },
328 { "cmd", 1, NULL
, 'c' },
329 { "read-only", 0, NULL
, 'r' },
330 { "snapshot", 0, NULL
, 's' },
331 { "nocache", 0, NULL
, 'n' },
332 { "misalign", 0, NULL
, 'm' },
333 { "growable", 0, NULL
, 'g' },
334 { "native-aio", 0, NULL
, 'k' },
335 { "discard", 1, NULL
, 'd' },
336 { "cache", 1, NULL
, 't' },
337 { "trace", 1, NULL
, 'T' },
342 int flags
= BDRV_O_UNMAP
;
345 signal(SIGPIPE
, SIG_IGN
);
348 progname
= basename(argv
[0]);
350 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
353 flags
|= BDRV_O_SNAPSHOT
;
356 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
359 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
360 error_report("Invalid discard option: %s", optarg
);
365 add_user_command(optarg
);
377 flags
|= BDRV_O_NATIVE_AIO
;
380 if (bdrv_parse_cache_flags(optarg
, &flags
) < 0) {
381 error_report("Invalid cache option: %s", optarg
);
386 if (!trace_backend_init(optarg
, NULL
)) {
387 exit(1); /* error message will have been printed */
391 printf("%s version %s\n", progname
, QEMU_VERSION
);
402 if ((argc
- optind
) > 1) {
407 qemu_init_main_loop();
410 /* initialize commands */
411 qemuio_add_command(&quit_cmd
);
412 qemuio_add_command(&open_cmd
);
413 qemuio_add_command(&close_cmd
);
415 /* open the device */
417 flags
|= BDRV_O_RDWR
;
420 if ((argc
- optind
) == 1) {
421 openfile(argv
[optind
], flags
, growable
);
426 * Make sure all outstanding requests complete before the program exits.
431 bdrv_unref(qemuio_bs
);