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
)
50 fprintf(stderr
, "file open already, try 'help close'\n");
55 if (bdrv_file_open(&qemuio_bs
, name
, NULL
, flags
)) {
56 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
60 qemuio_bs
= bdrv_new("hda");
62 if (bdrv_open(qemuio_bs
, name
, NULL
, flags
, NULL
) < 0) {
63 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
64 bdrv_delete(qemuio_bs
);
73 static void open_help(void)
77 " opens a new file in the requested mode\n"
80 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
82 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
83 " -r, -- open file read-only\n"
84 " -s, -- use snapshot file\n"
85 " -n, -- disable host cache\n"
86 " -g, -- allow file to grow (only applies to protocols)"
90 static int open_f(BlockDriverState
*bs
, int argc
, char **argv
);
92 static const cmdinfo_t open_cmd
= {
98 .flags
= CMD_NOFILE_OK
,
99 .args
= "[-Crsn] [path]",
100 .oneline
= "open the file specified by path",
104 static int open_f(BlockDriverState
*bs
, int argc
, char **argv
)
111 while ((c
= getopt(argc
, argv
, "snrg")) != EOF
) {
114 flags
|= BDRV_O_SNAPSHOT
;
117 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
126 return qemuio_command_usage(&open_cmd
);
131 flags
|= BDRV_O_RDWR
;
134 if (optind
!= argc
- 1) {
135 return qemuio_command_usage(&open_cmd
);
138 return openfile(argv
[optind
], flags
, growable
);
141 static int quit_f(BlockDriverState
*bs
, int argc
, char **argv
)
146 static const cmdinfo_t quit_cmd
= {
152 .flags
= CMD_FLAG_GLOBAL
,
153 .oneline
= "exit the program",
156 static void usage(const char *name
)
159 "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
160 "QEMU Disk exerciser\n"
162 " -c, --cmd command to execute\n"
163 " -r, --read-only export read-only\n"
164 " -s, --snapshot use snapshot file\n"
165 " -n, --nocache disable host cache\n"
166 " -g, --growable allow file to grow (only applies to protocols)\n"
167 " -m, --misalign misalign allocations for O_DIRECT\n"
168 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
169 " -t, --cache=MODE use the given cache mode for the image\n"
170 " -T, --trace FILE enable trace events listed in the given file\n"
171 " -h, --help display this help and exit\n"
172 " -V, --version output version information and exit\n"
178 #if defined(ENABLE_READLINE)
179 # include <readline/history.h>
180 # include <readline/readline.h>
181 #elif defined(ENABLE_EDITLINE)
182 # include <histedit.h>
185 static char *get_prompt(void)
187 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
190 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
196 #if defined(ENABLE_READLINE)
197 static char *fetchline(void)
199 char *line
= readline(get_prompt());
205 #elif defined(ENABLE_EDITLINE)
206 static char *el_get_prompt(EditLine
*e
)
211 static char *fetchline(void)
214 static History
*hist
;
220 hist
= history_init();
221 history(hist
, &hevent
, H_SETSIZE
, 100);
222 el
= el_init(progname
, stdin
, stdout
, stderr
);
224 el_set(el
, EL_SIGNAL
, 1);
225 el_set(el
, EL_PROMPT
, el_get_prompt
);
226 el_set(el
, EL_HIST
, history
, (const char *)hist
);
228 line
= strdup(el_gets(el
, &count
));
231 line
[count
-1] = '\0';
234 history(hist
, &hevent
, H_ENTER
, line
);
240 # define MAXREADLINESZ 1024
241 static char *fetchline(void)
243 char *p
, *line
= g_malloc(MAXREADLINESZ
);
245 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
250 p
= line
+ strlen(line
);
251 if (p
!= line
&& p
[-1] == '\n') {
259 static void prep_fetchline(void *opaque
)
261 int *fetchable
= opaque
;
263 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
267 static void command_loop(void)
269 int i
, done
= 0, fetchable
= 0, prompted
= 0;
272 for (i
= 0; !done
&& i
< ncmdline
; i
++) {
273 done
= qemuio_command(qemuio_bs
, cmdline
[i
]);
282 printf("%s", get_prompt());
284 qemu_set_fd_handler(STDIN_FILENO
, prep_fetchline
, NULL
, &fetchable
);
288 main_loop_wait(false);
298 done
= qemuio_command(qemuio_bs
, input
);
304 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
307 static void add_user_command(char *optarg
)
309 cmdline
= g_realloc(cmdline
, ++ncmdline
* sizeof(char *));
310 cmdline
[ncmdline
-1] = optarg
;
313 int main(int argc
, char **argv
)
317 const char *sopt
= "hVc:d:rsnmgkt:T:";
318 const struct option lopt
[] = {
319 { "help", 0, NULL
, 'h' },
320 { "version", 0, NULL
, 'V' },
321 { "offset", 1, NULL
, 'o' },
322 { "cmd", 1, NULL
, 'c' },
323 { "read-only", 0, NULL
, 'r' },
324 { "snapshot", 0, NULL
, 's' },
325 { "nocache", 0, NULL
, 'n' },
326 { "misalign", 0, NULL
, 'm' },
327 { "growable", 0, NULL
, 'g' },
328 { "native-aio", 0, NULL
, 'k' },
329 { "discard", 1, NULL
, 'd' },
330 { "cache", 1, NULL
, 't' },
331 { "trace", 1, NULL
, 'T' },
336 int flags
= BDRV_O_UNMAP
;
339 signal(SIGPIPE
, SIG_IGN
);
342 progname
= basename(argv
[0]);
344 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
347 flags
|= BDRV_O_SNAPSHOT
;
350 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
353 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
354 error_report("Invalid discard option: %s", optarg
);
359 add_user_command(optarg
);
371 flags
|= BDRV_O_NATIVE_AIO
;
374 if (bdrv_parse_cache_flags(optarg
, &flags
) < 0) {
375 error_report("Invalid cache option: %s", optarg
);
380 if (!trace_backend_init(optarg
, NULL
)) {
381 exit(1); /* error message will have been printed */
385 printf("%s version %s\n", progname
, QEMU_VERSION
);
396 if ((argc
- optind
) > 1) {
401 qemu_init_main_loop();
404 /* initialize commands */
405 qemuio_add_command(&quit_cmd
);
406 qemuio_add_command(&open_cmd
);
407 qemuio_add_command(&close_cmd
);
409 /* open the device */
411 flags
|= BDRV_O_RDWR
;
414 if ((argc
- optind
) == 1) {
415 openfile(argv
[optind
], flags
, growable
);
420 * Make sure all outstanding requests complete before the program exits.
425 bdrv_delete(qemuio_bs
);