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 command_usage(&open_cmd);
130 flags |= BDRV_O_RDWR;
133 if (optind != argc - 1) {
134 return command_usage(&open_cmd);
137 return openfile(argv[optind], flags, growable);
140 static void usage(const char *name)
143 "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
144 "QEMU Disk exerciser\n"
146 " -c, --cmd command to execute\n"
147 " -r, --read-only export read-only\n"
148 " -s, --snapshot use snapshot file\n"
149 " -n, --nocache disable host cache\n"
150 " -g, --growable allow file to grow (only applies to protocols)\n"
151 " -m, --misalign misalign allocations for O_DIRECT\n"
152 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
153 " -t, --cache=MODE use the given cache mode for the image\n"
154 " -T, --trace FILE enable trace events listed in the given file\n"
155 " -h, --help display this help and exit\n"
156 " -V, --version output version information and exit\n"
162 int main(int argc, char **argv)
166 const char *sopt = "hVc:d:rsnmgkt:T:";
167 const struct option lopt[] = {
168 { "help", 0, NULL, 'h' },
169 { "version", 0, NULL, 'V' },
170 { "offset", 1, NULL, 'o' },
171 { "cmd", 1, NULL, 'c' },
172 { "read-only", 0, NULL, 'r' },
173 { "snapshot", 0, NULL, 's' },
174 { "nocache", 0, NULL, 'n' },
175 { "misalign", 0, NULL, 'm' },
176 { "growable", 0, NULL, 'g' },
177 { "native-aio", 0, NULL, 'k' },
178 { "discard", 1, NULL, 'd' },
179 { "cache", 1, NULL, 't' },
180 { "trace", 1, NULL, 'T' },
185 int flags = BDRV_O_UNMAP;
187 progname = basename(argv[0]);
189 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
192 flags |= BDRV_O_SNAPSHOT;
195 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
198 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
199 error_report("Invalid discard option: %s", optarg);
204 add_user_command(optarg);
216 flags |= BDRV_O_NATIVE_AIO;
219 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
220 error_report("Invalid cache option: %s", optarg);
225 if (!trace_backend_init(optarg, NULL)) {
226 exit(1); /* error message will have been printed */
230 printf("%s version %s\n", progname, VERSION);
241 if ((argc - optind) > 1) {
246 qemu_init_main_loop();
249 /* initialize commands */
251 add_command(&open_cmd);
252 add_command(&close_cmd);
254 /* open the device */
256 flags |= BDRV_O_RDWR;
259 if ((argc - optind) == 1) {
260 openfile(argv[optind], flags, growable);
265 * Make sure all outstanding requests complete before the program exits.
270 bdrv_delete(qemuio_bs);