blob: 3bf5aec0de84d2e4a84e34fab8a22bb8b60c38b5 [file] [log] [blame]
aliguorie3aff4f2009-04-05 19:14:04 +00001/*
2 * Command line utility to exercise the QEMU I/O path.
3 *
4 * Copyright (C) 2009 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
6 *
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.
9 */
Stefan Weilc32d7662009-08-31 22:16:16 +020010#include <sys/time.h>
aliguorie3aff4f2009-04-05 19:14:04 +000011#include <sys/types.h>
12#include <stdarg.h>
13#include <stdio.h>
14#include <getopt.h>
Stefan Weilc32d7662009-08-31 22:16:16 +020015#include <libgen.h>
aliguorie3aff4f2009-04-05 19:14:04 +000016
17#include "qemu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010018#include "qemu/main-loop.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010019#include "block/block_int.h"
aliguorie3aff4f2009-04-05 19:14:04 +000020#include "cmd.h"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +000021#include "trace/control.h"
aliguorie3aff4f2009-04-05 19:14:04 +000022
23#define VERSION "0.0.1"
24
Devin Nakamura43642b32011-07-11 11:22:16 -040025#define CMD_NOFILE_OK 0x01
aliguorie3aff4f2009-04-05 19:14:04 +000026
27char *progname;
aliguorie3aff4f2009-04-05 19:14:04 +000028
Kevin Wolf734c3b82013-06-05 14:19:30 +020029BlockDriverState *qemuio_bs;
Kevin Wolf797ac582013-06-05 14:19:31 +020030extern int qemuio_misalign;
Kevin Wolf191c2892010-09-16 13:18:08 +020031
Kevin Wolf734c3b82013-06-05 14:19:30 +020032static int close_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +000033{
Stefan Hajnoczib4657852011-10-27 10:54:26 +010034 bdrv_delete(bs);
Kevin Wolf734c3b82013-06-05 14:19:30 +020035 qemuio_bs = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -040036 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000037}
38
39static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -040040 .name = "close",
41 .altname = "c",
42 .cfunc = close_f,
43 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +000044};
45
Christoph Hellwig9c4bab22009-07-10 13:33:47 +020046static int openfile(char *name, int flags, int growable)
aliguorie3aff4f2009-04-05 19:14:04 +000047{
Kevin Wolf734c3b82013-06-05 14:19:30 +020048 if (qemuio_bs) {
Devin Nakamura43642b32011-07-11 11:22:16 -040049 fprintf(stderr, "file open already, try 'help close'\n");
50 return 1;
51 }
aliguorie3aff4f2009-04-05 19:14:04 +000052
Devin Nakamura43642b32011-07-11 11:22:16 -040053 if (growable) {
Kevin Wolf734c3b82013-06-05 14:19:30 +020054 if (bdrv_file_open(&qemuio_bs, name, NULL, flags)) {
Devin Nakamura43642b32011-07-11 11:22:16 -040055 fprintf(stderr, "%s: can't open device %s\n", progname, name);
56 return 1;
57 }
58 } else {
Kevin Wolf734c3b82013-06-05 14:19:30 +020059 qemuio_bs = bdrv_new("hda");
Christoph Hellwig6db95602010-04-05 16:53:57 +020060
Kevin Wolf734c3b82013-06-05 14:19:30 +020061 if (bdrv_open(qemuio_bs, name, NULL, flags, NULL) < 0) {
Devin Nakamura43642b32011-07-11 11:22:16 -040062 fprintf(stderr, "%s: can't open device %s\n", progname, name);
Kevin Wolf734c3b82013-06-05 14:19:30 +020063 bdrv_delete(qemuio_bs);
64 qemuio_bs = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -040065 return 1;
66 }
67 }
Christoph Hellwig1db69472009-07-15 23:11:21 +020068
Devin Nakamura43642b32011-07-11 11:22:16 -040069 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000070}
71
Devin Nakamura43642b32011-07-11 11:22:16 -040072static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +000073{
Devin Nakamura43642b32011-07-11 11:22:16 -040074 printf(
aliguorie3aff4f2009-04-05 19:14:04 +000075"\n"
76" opens a new file in the requested mode\n"
77"\n"
78" Example:\n"
79" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
80"\n"
81" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +000082" -r, -- open file read-only\n"
83" -s, -- use snapshot file\n"
84" -n, -- disable host cache\n"
Christoph Hellwig9c4bab22009-07-10 13:33:47 +020085" -g, -- allow file to grow (only applies to protocols)"
aliguorie3aff4f2009-04-05 19:14:04 +000086"\n");
87}
88
Kevin Wolf734c3b82013-06-05 14:19:30 +020089static int open_f(BlockDriverState *bs, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +000090
91static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -040092 .name = "open",
93 .altname = "o",
94 .cfunc = open_f,
95 .argmin = 1,
96 .argmax = -1,
97 .flags = CMD_NOFILE_OK,
98 .args = "[-Crsn] [path]",
99 .oneline = "open the file specified by path",
100 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000101};
aliguorie3aff4f2009-04-05 19:14:04 +0000102
Kevin Wolf734c3b82013-06-05 14:19:30 +0200103static int open_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000104{
Devin Nakamura43642b32011-07-11 11:22:16 -0400105 int flags = 0;
106 int readonly = 0;
107 int growable = 0;
108 int c;
aliguorie3aff4f2009-04-05 19:14:04 +0000109
Devin Nakamura43642b32011-07-11 11:22:16 -0400110 while ((c = getopt(argc, argv, "snrg")) != EOF) {
111 switch (c) {
112 case 's':
113 flags |= BDRV_O_SNAPSHOT;
114 break;
115 case 'n':
116 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
117 break;
118 case 'r':
119 readonly = 1;
120 break;
121 case 'g':
122 growable = 1;
123 break;
124 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200125 return qemuio_command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200126 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400127 }
aliguorie3aff4f2009-04-05 19:14:04 +0000128
Devin Nakamura43642b32011-07-11 11:22:16 -0400129 if (!readonly) {
130 flags |= BDRV_O_RDWR;
131 }
aliguorie3aff4f2009-04-05 19:14:04 +0000132
Devin Nakamura43642b32011-07-11 11:22:16 -0400133 if (optind != argc - 1) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200134 return qemuio_command_usage(&open_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400135 }
136
137 return openfile(argv[optind], flags, growable);
aliguorie3aff4f2009-04-05 19:14:04 +0000138}
139
Kevin Wolfe681be72013-06-05 14:19:34 +0200140static int quit_f(BlockDriverState *bs, int argc, char **argv)
141{
142 return 1;
143}
144
145static const cmdinfo_t quit_cmd = {
146 .name = "quit",
147 .altname = "q",
148 .cfunc = quit_f,
149 .argmin = -1,
150 .argmax = -1,
151 .flags = CMD_FLAG_GLOBAL,
152 .oneline = "exit the program",
153};
154
aliguorie3aff4f2009-04-05 19:14:04 +0000155static void usage(const char *name)
156{
Devin Nakamura43642b32011-07-11 11:22:16 -0400157 printf(
Christoph Hellwig9a2d77a2010-01-20 18:13:42 +0100158"Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200159"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000160"\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000161" -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"
Christoph Hellwig1db69472009-07-15 23:11:21 +0200165" -g, --growable allow file to grow (only applies to protocols)\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000166" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200167" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200168" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000169" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000170" -h, --help display this help and exit\n"
171" -V, --version output version information and exit\n"
172"\n",
Devin Nakamura43642b32011-07-11 11:22:16 -0400173 name);
aliguorie3aff4f2009-04-05 19:14:04 +0000174}
175
176
177int main(int argc, char **argv)
178{
Devin Nakamura43642b32011-07-11 11:22:16 -0400179 int readonly = 0;
180 int growable = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100181 const char *sopt = "hVc:d:rsnmgkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -0400182 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' },
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100193 { "discard", 1, NULL, 'd' },
Kevin Wolf592fa072012-04-18 12:07:39 +0200194 { "cache", 1, NULL, 't' },
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000195 { "trace", 1, NULL, 'T' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400196 { NULL, 0, NULL, 0 }
197 };
198 int c;
199 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100200 int flags = BDRV_O_UNMAP;
aliguorie3aff4f2009-04-05 19:14:04 +0000201
Devin Nakamura43642b32011-07-11 11:22:16 -0400202 progname = basename(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000203
Devin Nakamura43642b32011-07-11 11:22:16 -0400204 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
205 switch (c) {
206 case 's':
207 flags |= BDRV_O_SNAPSHOT;
208 break;
209 case 'n':
210 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
211 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100212 case 'd':
213 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
214 error_report("Invalid discard option: %s", optarg);
215 exit(1);
216 }
217 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400218 case 'c':
219 add_user_command(optarg);
220 break;
221 case 'r':
222 readonly = 1;
223 break;
224 case 'm':
Kevin Wolf797ac582013-06-05 14:19:31 +0200225 qemuio_misalign = 1;
Devin Nakamura43642b32011-07-11 11:22:16 -0400226 break;
227 case 'g':
228 growable = 1;
229 break;
230 case 'k':
231 flags |= BDRV_O_NATIVE_AIO;
232 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200233 case 't':
234 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
235 error_report("Invalid cache option: %s", optarg);
236 exit(1);
237 }
238 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000239 case 'T':
240 if (!trace_backend_init(optarg, NULL)) {
241 exit(1); /* error message will have been printed */
242 }
243 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400244 case 'V':
245 printf("%s version %s\n", progname, VERSION);
246 exit(0);
247 case 'h':
248 usage(progname);
249 exit(0);
250 default:
251 usage(progname);
252 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200253 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400254 }
aliguorie3aff4f2009-04-05 19:14:04 +0000255
Devin Nakamura43642b32011-07-11 11:22:16 -0400256 if ((argc - optind) > 1) {
257 usage(progname);
258 exit(1);
259 }
aliguorie3aff4f2009-04-05 19:14:04 +0000260
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800261 qemu_init_main_loop();
Paolo Bonzini2592c592012-11-03 18:10:17 +0100262 bdrv_init();
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800263
Devin Nakamura43642b32011-07-11 11:22:16 -0400264 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200265 qemuio_add_command(&quit_cmd);
266 qemuio_add_command(&open_cmd);
267 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400268
269 /* open the device */
270 if (!readonly) {
271 flags |= BDRV_O_RDWR;
272 }
273
274 if ((argc - optind) == 1) {
275 openfile(argv[optind], flags, growable);
276 }
277 command_loop();
278
279 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000280 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400281 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000282 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400283
Kevin Wolf734c3b82013-06-05 14:19:30 +0200284 if (qemuio_bs) {
285 bdrv_delete(qemuio_bs);
Devin Nakamura43642b32011-07-11 11:22:16 -0400286 }
287 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000288}