]>
Commit | Line | Data |
---|---|---|
e3aff4f6 AL |
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 | */ | |
c32d766a | 10 | #include <sys/time.h> |
e3aff4f6 AL |
11 | #include <sys/types.h> |
12 | #include <stdarg.h> | |
13 | #include <stdio.h> | |
14 | #include <getopt.h> | |
c32d766a | 15 | #include <libgen.h> |
e3aff4f6 | 16 | |
3d21994f | 17 | #include "qemu-io.h" |
1de7afc9 | 18 | #include "qemu/main-loop.h" |
737e150e | 19 | #include "block/block_int.h" |
d7bb72c8 | 20 | #include "trace/control.h" |
e3aff4f6 | 21 | |
43642b38 | 22 | #define CMD_NOFILE_OK 0x01 |
e3aff4f6 AL |
23 | |
24 | char *progname; | |
e3aff4f6 | 25 | |
734c3b85 | 26 | BlockDriverState *qemuio_bs; |
797ac58c | 27 | extern int qemuio_misalign; |
191c2890 | 28 | |
d1174f13 KW |
29 | /* qemu-io commands passed using -c */ |
30 | static int ncmdline; | |
31 | static char **cmdline; | |
32 | ||
734c3b85 | 33 | static int close_f(BlockDriverState *bs, int argc, char **argv) |
e3aff4f6 | 34 | { |
4f6fd349 | 35 | bdrv_unref(bs); |
734c3b85 | 36 | qemuio_bs = NULL; |
43642b38 | 37 | return 0; |
e3aff4f6 AL |
38 | } |
39 | ||
40 | static const cmdinfo_t close_cmd = { | |
43642b38 DN |
41 | .name = "close", |
42 | .altname = "c", | |
43 | .cfunc = close_f, | |
44 | .oneline = "close the current open file", | |
e3aff4f6 AL |
45 | }; |
46 | ||
9c4bab26 | 47 | static int openfile(char *name, int flags, int growable) |
e3aff4f6 | 48 | { |
34b5d2c6 HR |
49 | Error *local_err = NULL; |
50 | ||
734c3b85 | 51 | if (qemuio_bs) { |
43642b38 DN |
52 | fprintf(stderr, "file open already, try 'help close'\n"); |
53 | return 1; | |
54 | } | |
55 | ||
56 | if (growable) { | |
34b5d2c6 HR |
57 | if (bdrv_file_open(&qemuio_bs, name, NULL, flags, &local_err)) { |
58 | fprintf(stderr, "%s: can't open device %s: %s\n", progname, name, | |
59 | error_get_pretty(local_err)); | |
60 | error_free(local_err); | |
43642b38 DN |
61 | return 1; |
62 | } | |
63 | } else { | |
734c3b85 | 64 | qemuio_bs = bdrv_new("hda"); |
43642b38 | 65 | |
34b5d2c6 HR |
66 | if (bdrv_open(qemuio_bs, name, NULL, flags, NULL, &local_err) < 0) { |
67 | fprintf(stderr, "%s: can't open device %s: %s\n", progname, name, | |
68 | error_get_pretty(local_err)); | |
69 | error_free(local_err); | |
4f6fd349 | 70 | bdrv_unref(qemuio_bs); |
734c3b85 | 71 | qemuio_bs = NULL; |
43642b38 DN |
72 | return 1; |
73 | } | |
74 | } | |
75 | ||
76 | return 0; | |
e3aff4f6 AL |
77 | } |
78 | ||
43642b38 | 79 | static void open_help(void) |
e3aff4f6 | 80 | { |
43642b38 | 81 | printf( |
e3aff4f6 AL |
82 | "\n" |
83 | " opens a new file in the requested mode\n" | |
84 | "\n" | |
85 | " Example:\n" | |
86 | " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n" | |
87 | "\n" | |
88 | " Opens a file for subsequent use by all of the other qemu-io commands.\n" | |
e3aff4f6 AL |
89 | " -r, -- open file read-only\n" |
90 | " -s, -- use snapshot file\n" | |
91 | " -n, -- disable host cache\n" | |
9c4bab26 | 92 | " -g, -- allow file to grow (only applies to protocols)" |
e3aff4f6 AL |
93 | "\n"); |
94 | } | |
95 | ||
734c3b85 | 96 | static int open_f(BlockDriverState *bs, int argc, char **argv); |
22a2bdcb BS |
97 | |
98 | static const cmdinfo_t open_cmd = { | |
43642b38 DN |
99 | .name = "open", |
100 | .altname = "o", | |
101 | .cfunc = open_f, | |
102 | .argmin = 1, | |
103 | .argmax = -1, | |
104 | .flags = CMD_NOFILE_OK, | |
105 | .args = "[-Crsn] [path]", | |
106 | .oneline = "open the file specified by path", | |
107 | .help = open_help, | |
22a2bdcb | 108 | }; |
e3aff4f6 | 109 | |
734c3b85 | 110 | static int open_f(BlockDriverState *bs, int argc, char **argv) |
e3aff4f6 | 111 | { |
43642b38 DN |
112 | int flags = 0; |
113 | int readonly = 0; | |
114 | int growable = 0; | |
115 | int c; | |
116 | ||
117 | while ((c = getopt(argc, argv, "snrg")) != EOF) { | |
118 | switch (c) { | |
119 | case 's': | |
120 | flags |= BDRV_O_SNAPSHOT; | |
121 | break; | |
122 | case 'n': | |
123 | flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; | |
124 | break; | |
125 | case 'r': | |
126 | readonly = 1; | |
127 | break; | |
128 | case 'g': | |
129 | growable = 1; | |
130 | break; | |
131 | default: | |
c2cdf5c5 | 132 | return qemuio_command_usage(&open_cmd); |
f5edb014 | 133 | } |
43642b38 DN |
134 | } |
135 | ||
136 | if (!readonly) { | |
137 | flags |= BDRV_O_RDWR; | |
138 | } | |
e3aff4f6 | 139 | |
43642b38 | 140 | if (optind != argc - 1) { |
c2cdf5c5 | 141 | return qemuio_command_usage(&open_cmd); |
43642b38 | 142 | } |
e3aff4f6 | 143 | |
43642b38 | 144 | return openfile(argv[optind], flags, growable); |
e3aff4f6 AL |
145 | } |
146 | ||
e681be7e KW |
147 | static int quit_f(BlockDriverState *bs, int argc, char **argv) |
148 | { | |
149 | return 1; | |
150 | } | |
151 | ||
152 | static const cmdinfo_t quit_cmd = { | |
153 | .name = "quit", | |
154 | .altname = "q", | |
155 | .cfunc = quit_f, | |
156 | .argmin = -1, | |
157 | .argmax = -1, | |
158 | .flags = CMD_FLAG_GLOBAL, | |
159 | .oneline = "exit the program", | |
160 | }; | |
161 | ||
e3aff4f6 AL |
162 | static void usage(const char *name) |
163 | { | |
43642b38 | 164 | printf( |
9a2d77ad | 165 | "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n" |
84844a20 | 166 | "QEMU Disk exerciser\n" |
e3aff4f6 | 167 | "\n" |
e3aff4f6 AL |
168 | " -c, --cmd command to execute\n" |
169 | " -r, --read-only export read-only\n" | |
170 | " -s, --snapshot use snapshot file\n" | |
171 | " -n, --nocache disable host cache\n" | |
1db6947d | 172 | " -g, --growable allow file to grow (only applies to protocols)\n" |
e3aff4f6 | 173 | " -m, --misalign misalign allocations for O_DIRECT\n" |
5c6c3a6c | 174 | " -k, --native-aio use kernel AIO implementation (on Linux only)\n" |
592fa070 | 175 | " -t, --cache=MODE use the given cache mode for the image\n" |
d7bb72c8 | 176 | " -T, --trace FILE enable trace events listed in the given file\n" |
e3aff4f6 AL |
177 | " -h, --help display this help and exit\n" |
178 | " -V, --version output version information and exit\n" | |
179 | "\n", | |
43642b38 | 180 | name); |
e3aff4f6 AL |
181 | } |
182 | ||
183 | ||
d1174f13 KW |
184 | #if defined(ENABLE_READLINE) |
185 | # include <readline/history.h> | |
186 | # include <readline/readline.h> | |
187 | #elif defined(ENABLE_EDITLINE) | |
188 | # include <histedit.h> | |
189 | #endif | |
190 | ||
191 | static char *get_prompt(void) | |
192 | { | |
193 | static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ]; | |
194 | ||
195 | if (!prompt[0]) { | |
196 | snprintf(prompt, sizeof(prompt), "%s> ", progname); | |
197 | } | |
198 | ||
199 | return prompt; | |
200 | } | |
201 | ||
202 | #if defined(ENABLE_READLINE) | |
203 | static char *fetchline(void) | |
204 | { | |
205 | char *line = readline(get_prompt()); | |
206 | if (line && *line) { | |
207 | add_history(line); | |
208 | } | |
209 | return line; | |
210 | } | |
211 | #elif defined(ENABLE_EDITLINE) | |
212 | static char *el_get_prompt(EditLine *e) | |
213 | { | |
214 | return get_prompt(); | |
215 | } | |
216 | ||
217 | static char *fetchline(void) | |
218 | { | |
219 | static EditLine *el; | |
220 | static History *hist; | |
221 | HistEvent hevent; | |
222 | char *line; | |
223 | int count; | |
224 | ||
225 | if (!el) { | |
226 | hist = history_init(); | |
227 | history(hist, &hevent, H_SETSIZE, 100); | |
228 | el = el_init(progname, stdin, stdout, stderr); | |
229 | el_source(el, NULL); | |
230 | el_set(el, EL_SIGNAL, 1); | |
231 | el_set(el, EL_PROMPT, el_get_prompt); | |
232 | el_set(el, EL_HIST, history, (const char *)hist); | |
233 | } | |
234 | line = strdup(el_gets(el, &count)); | |
235 | if (line) { | |
236 | if (count > 0) { | |
237 | line[count-1] = '\0'; | |
238 | } | |
239 | if (*line) { | |
240 | history(hist, &hevent, H_ENTER, line); | |
241 | } | |
242 | } | |
243 | return line; | |
244 | } | |
245 | #else | |
246 | # define MAXREADLINESZ 1024 | |
247 | static char *fetchline(void) | |
248 | { | |
249 | char *p, *line = g_malloc(MAXREADLINESZ); | |
250 | ||
251 | if (!fgets(line, MAXREADLINESZ, stdin)) { | |
252 | g_free(line); | |
253 | return NULL; | |
254 | } | |
255 | ||
256 | p = line + strlen(line); | |
257 | if (p != line && p[-1] == '\n') { | |
258 | p[-1] = '\0'; | |
259 | } | |
260 | ||
261 | return line; | |
262 | } | |
263 | #endif | |
264 | ||
265 | static void prep_fetchline(void *opaque) | |
266 | { | |
267 | int *fetchable = opaque; | |
268 | ||
269 | qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL); | |
270 | *fetchable= 1; | |
271 | } | |
272 | ||
273 | static void command_loop(void) | |
274 | { | |
275 | int i, done = 0, fetchable = 0, prompted = 0; | |
276 | char *input; | |
277 | ||
278 | for (i = 0; !done && i < ncmdline; i++) { | |
3d21994f | 279 | done = qemuio_command(qemuio_bs, cmdline[i]); |
d1174f13 KW |
280 | } |
281 | if (cmdline) { | |
282 | g_free(cmdline); | |
283 | return; | |
284 | } | |
285 | ||
286 | while (!done) { | |
287 | if (!prompted) { | |
288 | printf("%s", get_prompt()); | |
289 | fflush(stdout); | |
290 | qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable); | |
291 | prompted = 1; | |
292 | } | |
293 | ||
294 | main_loop_wait(false); | |
295 | ||
296 | if (!fetchable) { | |
297 | continue; | |
298 | } | |
299 | ||
300 | input = fetchline(); | |
301 | if (input == NULL) { | |
302 | break; | |
303 | } | |
3d21994f | 304 | done = qemuio_command(qemuio_bs, input); |
d1174f13 KW |
305 | g_free(input); |
306 | ||
307 | prompted = 0; | |
308 | fetchable = 0; | |
309 | } | |
310 | qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL); | |
311 | } | |
312 | ||
313 | static void add_user_command(char *optarg) | |
314 | { | |
315 | cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *)); | |
316 | cmdline[ncmdline-1] = optarg; | |
317 | } | |
318 | ||
e3aff4f6 AL |
319 | int main(int argc, char **argv) |
320 | { | |
43642b38 DN |
321 | int readonly = 0; |
322 | int growable = 0; | |
9e8f1835 | 323 | const char *sopt = "hVc:d:rsnmgkt:T:"; |
43642b38 DN |
324 | const struct option lopt[] = { |
325 | { "help", 0, NULL, 'h' }, | |
326 | { "version", 0, NULL, 'V' }, | |
327 | { "offset", 1, NULL, 'o' }, | |
328 | { "cmd", 1, NULL, 'c' }, | |
329 | { "read-only", 0, NULL, 'r' }, | |
330 | { "snapshot", 0, NULL, 's' }, | |
331 | { "nocache", 0, NULL, 'n' }, | |
332 | { "misalign", 0, NULL, 'm' }, | |
333 | { "growable", 0, NULL, 'g' }, | |
334 | { "native-aio", 0, NULL, 'k' }, | |
9e8f1835 | 335 | { "discard", 1, NULL, 'd' }, |
592fa070 | 336 | { "cache", 1, NULL, 't' }, |
d7bb72c8 | 337 | { "trace", 1, NULL, 'T' }, |
43642b38 DN |
338 | { NULL, 0, NULL, 0 } |
339 | }; | |
340 | int c; | |
341 | int opt_index = 0; | |
9e8f1835 | 342 | int flags = BDRV_O_UNMAP; |
43642b38 | 343 | |
526eda14 MK |
344 | #ifdef CONFIG_POSIX |
345 | signal(SIGPIPE, SIG_IGN); | |
346 | #endif | |
347 | ||
43642b38 DN |
348 | progname = basename(argv[0]); |
349 | ||
350 | while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) { | |
351 | switch (c) { | |
352 | case 's': | |
353 | flags |= BDRV_O_SNAPSHOT; | |
354 | break; | |
355 | case 'n': | |
356 | flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; | |
357 | break; | |
9e8f1835 PB |
358 | case 'd': |
359 | if (bdrv_parse_discard_flags(optarg, &flags) < 0) { | |
360 | error_report("Invalid discard option: %s", optarg); | |
361 | exit(1); | |
362 | } | |
363 | break; | |
43642b38 DN |
364 | case 'c': |
365 | add_user_command(optarg); | |
366 | break; | |
367 | case 'r': | |
368 | readonly = 1; | |
369 | break; | |
370 | case 'm': | |
797ac58c | 371 | qemuio_misalign = 1; |
43642b38 DN |
372 | break; |
373 | case 'g': | |
374 | growable = 1; | |
375 | break; | |
376 | case 'k': | |
377 | flags |= BDRV_O_NATIVE_AIO; | |
378 | break; | |
592fa070 KW |
379 | case 't': |
380 | if (bdrv_parse_cache_flags(optarg, &flags) < 0) { | |
381 | error_report("Invalid cache option: %s", optarg); | |
382 | exit(1); | |
383 | } | |
384 | break; | |
d7bb72c8 SH |
385 | case 'T': |
386 | if (!trace_backend_init(optarg, NULL)) { | |
387 | exit(1); /* error message will have been printed */ | |
388 | } | |
389 | break; | |
43642b38 | 390 | case 'V': |
02da386a | 391 | printf("%s version %s\n", progname, QEMU_VERSION); |
43642b38 DN |
392 | exit(0); |
393 | case 'h': | |
394 | usage(progname); | |
395 | exit(0); | |
396 | default: | |
397 | usage(progname); | |
398 | exit(1); | |
f5edb014 | 399 | } |
43642b38 DN |
400 | } |
401 | ||
402 | if ((argc - optind) > 1) { | |
403 | usage(progname); | |
404 | exit(1); | |
405 | } | |
e3aff4f6 | 406 | |
a57d1143 | 407 | qemu_init_main_loop(); |
2592c59a | 408 | bdrv_init(); |
a57d1143 | 409 | |
43642b38 | 410 | /* initialize commands */ |
c2cdf5c5 KW |
411 | qemuio_add_command(&quit_cmd); |
412 | qemuio_add_command(&open_cmd); | |
413 | qemuio_add_command(&close_cmd); | |
43642b38 DN |
414 | |
415 | /* open the device */ | |
416 | if (!readonly) { | |
417 | flags |= BDRV_O_RDWR; | |
418 | } | |
419 | ||
420 | if ((argc - optind) == 1) { | |
421 | openfile(argv[optind], flags, growable); | |
422 | } | |
423 | command_loop(); | |
e3aff4f6 | 424 | |
43642b38 | 425 | /* |
922453bc | 426 | * Make sure all outstanding requests complete before the program exits. |
43642b38 | 427 | */ |
922453bc | 428 | bdrv_drain_all(); |
95533d5f | 429 | |
734c3b85 | 430 | if (qemuio_bs) { |
4f6fd349 | 431 | bdrv_unref(qemuio_bs); |
43642b38 DN |
432 | } |
433 | return 0; | |
e3aff4f6 | 434 | } |