]> git.proxmox.com Git - qemu.git/blob - qemu-io.c
qemu-io: Move functions for registering and running commands
[qemu.git] / qemu-io.c
1 /*
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 */
10 #include <sys/time.h>
11 #include <sys/types.h>
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include <getopt.h>
15 #include <libgen.h>
16
17 #include "qemu-common.h"
18 #include "qemu/main-loop.h"
19 #include "block/block_int.h"
20 #include "cmd.h"
21 #include "trace/control.h"
22
23 #define VERSION "0.0.1"
24
25 #define CMD_NOFILE_OK 0x01
26
27 char *progname;
28
29 BlockDriverState *qemuio_bs;
30 extern int qemuio_misalign;
31
32 static int close_f(BlockDriverState *bs, int argc, char **argv)
33 {
34 bdrv_delete(bs);
35 qemuio_bs = NULL;
36 return 0;
37 }
38
39 static const cmdinfo_t close_cmd = {
40 .name = "close",
41 .altname = "c",
42 .cfunc = close_f,
43 .oneline = "close the current open file",
44 };
45
46 static int openfile(char *name, int flags, int growable)
47 {
48 if (qemuio_bs) {
49 fprintf(stderr, "file open already, try 'help close'\n");
50 return 1;
51 }
52
53 if (growable) {
54 if (bdrv_file_open(&qemuio_bs, name, NULL, flags)) {
55 fprintf(stderr, "%s: can't open device %s\n", progname, name);
56 return 1;
57 }
58 } else {
59 qemuio_bs = bdrv_new("hda");
60
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);
64 qemuio_bs = NULL;
65 return 1;
66 }
67 }
68
69 return 0;
70 }
71
72 static void open_help(void)
73 {
74 printf(
75 "\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"
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)"
86 "\n");
87 }
88
89 static int open_f(BlockDriverState *bs, int argc, char **argv);
90
91 static const cmdinfo_t open_cmd = {
92 .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,
101 };
102
103 static int open_f(BlockDriverState *bs, int argc, char **argv)
104 {
105 int flags = 0;
106 int readonly = 0;
107 int growable = 0;
108 int c;
109
110 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:
125 return qemuio_command_usage(&open_cmd);
126 }
127 }
128
129 if (!readonly) {
130 flags |= BDRV_O_RDWR;
131 }
132
133 if (optind != argc - 1) {
134 return qemuio_command_usage(&open_cmd);
135 }
136
137 return openfile(argv[optind], flags, growable);
138 }
139
140 static int quit_f(BlockDriverState *bs, int argc, char **argv)
141 {
142 return 1;
143 }
144
145 static 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
155 static void usage(const char *name)
156 {
157 printf(
158 "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
159 "QEMU Disk exerciser\n"
160 "\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"
172 "\n",
173 name);
174 }
175
176
177 int main(int argc, char **argv)
178 {
179 int readonly = 0;
180 int growable = 0;
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' },
196 { NULL, 0, NULL, 0 }
197 };
198 int c;
199 int opt_index = 0;
200 int flags = BDRV_O_UNMAP;
201
202 progname = basename(argv[0]);
203
204 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;
212 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;
218 case 'c':
219 add_user_command(optarg);
220 break;
221 case 'r':
222 readonly = 1;
223 break;
224 case 'm':
225 qemuio_misalign = 1;
226 break;
227 case 'g':
228 growable = 1;
229 break;
230 case 'k':
231 flags |= BDRV_O_NATIVE_AIO;
232 break;
233 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;
239 case 'T':
240 if (!trace_backend_init(optarg, NULL)) {
241 exit(1); /* error message will have been printed */
242 }
243 break;
244 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);
253 }
254 }
255
256 if ((argc - optind) > 1) {
257 usage(progname);
258 exit(1);
259 }
260
261 qemu_init_main_loop();
262 bdrv_init();
263
264 /* initialize commands */
265 qemuio_add_command(&quit_cmd);
266 qemuio_add_command(&open_cmd);
267 qemuio_add_command(&close_cmd);
268
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 /*
280 * Make sure all outstanding requests complete before the program exits.
281 */
282 bdrv_drain_all();
283
284 if (qemuio_bs) {
285 bdrv_delete(qemuio_bs);
286 }
287 return 0;
288 }