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>
17 #include "qemu-common.h"
18 #include "qemu/main-loop.h"
19 #include "block/block_int.h"
21 #include "trace/control.h"
23 #define VERSION "0.0.1"
25 #define CMD_NOFILE_OK 0x01
29 BlockDriverState
*qemuio_bs
;
30 extern int qemuio_misalign
;
32 static int close_f(BlockDriverState
*bs
, int argc
, char **argv
)
39 static const cmdinfo_t close_cmd
= {
43 .oneline
= "close the current open file",
46 static int openfile(char *name
, int flags
, int growable
)
49 fprintf(stderr
, "file open already, try 'help close'\n");
54 if (bdrv_file_open(&qemuio_bs
, name
, NULL
, flags
)) {
55 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
59 qemuio_bs
= bdrv_new("hda");
61 if (bdrv_open(qemuio_bs
, name
, NULL
, flags
, NULL
) < 0) {
62 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
63 bdrv_delete(qemuio_bs
);
72 static void open_help(void)
76 " opens a new file in the requested mode\n"
79 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
81 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
82 " -r, -- open file read-only\n"
83 " -s, -- use snapshot file\n"
84 " -n, -- disable host cache\n"
85 " -g, -- allow file to grow (only applies to protocols)"
89 static int open_f(BlockDriverState
*bs
, int argc
, char **argv
);
91 static const cmdinfo_t open_cmd
= {
97 .flags
= CMD_NOFILE_OK
,
98 .args
= "[-Crsn] [path]",
99 .oneline
= "open the file specified by path",
103 static int open_f(BlockDriverState
*bs
, int argc
, char **argv
)
110 while ((c
= getopt(argc
, argv
, "snrg")) != EOF
) {
113 flags
|= BDRV_O_SNAPSHOT
;
116 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
125 return qemuio_command_usage(&open_cmd
);
130 flags
|= BDRV_O_RDWR
;
133 if (optind
!= argc
- 1) {
134 return qemuio_command_usage(&open_cmd
);
137 return openfile(argv
[optind
], flags
, growable
);
140 static int quit_f(BlockDriverState
*bs
, int argc
, char **argv
)
145 static const cmdinfo_t quit_cmd
= {
151 .flags
= CMD_FLAG_GLOBAL
,
152 .oneline
= "exit the program",
155 static void usage(const char *name
)
158 "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
159 "QEMU Disk exerciser\n"
161 " -c, --cmd command to execute\n"
162 " -r, --read-only export read-only\n"
163 " -s, --snapshot use snapshot file\n"
164 " -n, --nocache disable host cache\n"
165 " -g, --growable allow file to grow (only applies to protocols)\n"
166 " -m, --misalign misalign allocations for O_DIRECT\n"
167 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
168 " -t, --cache=MODE use the given cache mode for the image\n"
169 " -T, --trace FILE enable trace events listed in the given file\n"
170 " -h, --help display this help and exit\n"
171 " -V, --version output version information and exit\n"
177 int main(int argc
, char **argv
)
181 const char *sopt
= "hVc:d:rsnmgkt:T:";
182 const struct option lopt
[] = {
183 { "help", 0, NULL
, 'h' },
184 { "version", 0, NULL
, 'V' },
185 { "offset", 1, NULL
, 'o' },
186 { "cmd", 1, NULL
, 'c' },
187 { "read-only", 0, NULL
, 'r' },
188 { "snapshot", 0, NULL
, 's' },
189 { "nocache", 0, NULL
, 'n' },
190 { "misalign", 0, NULL
, 'm' },
191 { "growable", 0, NULL
, 'g' },
192 { "native-aio", 0, NULL
, 'k' },
193 { "discard", 1, NULL
, 'd' },
194 { "cache", 1, NULL
, 't' },
195 { "trace", 1, NULL
, 'T' },
200 int flags
= BDRV_O_UNMAP
;
202 progname
= basename(argv
[0]);
204 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
207 flags
|= BDRV_O_SNAPSHOT
;
210 flags
|= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
;
213 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
214 error_report("Invalid discard option: %s", optarg
);
219 add_user_command(optarg
);
231 flags
|= BDRV_O_NATIVE_AIO
;
234 if (bdrv_parse_cache_flags(optarg
, &flags
) < 0) {
235 error_report("Invalid cache option: %s", optarg
);
240 if (!trace_backend_init(optarg
, NULL
)) {
241 exit(1); /* error message will have been printed */
245 printf("%s version %s\n", progname
, VERSION
);
256 if ((argc
- optind
) > 1) {
261 qemu_init_main_loop();
264 /* initialize commands */
265 qemuio_add_command(&quit_cmd
);
266 qemuio_add_command(&open_cmd
);
267 qemuio_add_command(&close_cmd
);
269 /* open the device */
271 flags
|= BDRV_O_RDWR
;
274 if ((argc
- optind
) == 1) {
275 openfile(argv
[optind
], flags
, growable
);
280 * Make sure all outstanding requests complete before the program exits.
285 bdrv_delete(qemuio_bs
);