]>
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" |
b543c5cd HR |
19 | #include "qemu/option.h" |
20 | #include "qemu/config-file.h" | |
0cf17e18 | 21 | #include "qemu/readline.h" |
737e150e | 22 | #include "block/block_int.h" |
d7bb72c8 | 23 | #include "trace/control.h" |
e3aff4f6 | 24 | |
43642b38 | 25 | #define CMD_NOFILE_OK 0x01 |
e3aff4f6 | 26 | |
f9883880 | 27 | static char *progname; |
e3aff4f6 | 28 | |
f9883880 | 29 | static BlockDriverState *qemuio_bs; |
191c2890 | 30 | |
d1174f13 KW |
31 | /* qemu-io commands passed using -c */ |
32 | static int ncmdline; | |
33 | static char **cmdline; | |
34 | ||
0cf17e18 SH |
35 | static ReadLineState *readline_state; |
36 | ||
734c3b85 | 37 | static int close_f(BlockDriverState *bs, int argc, char **argv) |
e3aff4f6 | 38 | { |
4f6fd349 | 39 | bdrv_unref(bs); |
734c3b85 | 40 | qemuio_bs = NULL; |
43642b38 | 41 | return 0; |
e3aff4f6 AL |
42 | } |
43 | ||
44 | static const cmdinfo_t close_cmd = { | |
43642b38 DN |
45 | .name = "close", |
46 | .altname = "c", | |
47 | .cfunc = close_f, | |
48 | .oneline = "close the current open file", | |
e3aff4f6 AL |
49 | }; |
50 | ||
b543c5cd | 51 | static int openfile(char *name, int flags, int growable, QDict *opts) |
e3aff4f6 | 52 | { |
34b5d2c6 HR |
53 | Error *local_err = NULL; |
54 | ||
734c3b85 | 55 | if (qemuio_bs) { |
43642b38 DN |
56 | fprintf(stderr, "file open already, try 'help close'\n"); |
57 | return 1; | |
58 | } | |
59 | ||
60 | if (growable) { | |
2e40134b HR |
61 | if (bdrv_open(&qemuio_bs, name, NULL, opts, flags | BDRV_O_PROTOCOL, |
62 | NULL, &local_err)) | |
63 | { | |
34b5d2c6 HR |
64 | fprintf(stderr, "%s: can't open device %s: %s\n", progname, name, |
65 | error_get_pretty(local_err)); | |
66 | error_free(local_err); | |
43642b38 DN |
67 | return 1; |
68 | } | |
69 | } else { | |
98522f63 | 70 | qemuio_bs = bdrv_new("hda", &error_abort); |
43642b38 | 71 | |
ddf5636d HR |
72 | if (bdrv_open(&qemuio_bs, name, NULL, opts, flags, NULL, &local_err) |
73 | < 0) | |
74 | { | |
34b5d2c6 HR |
75 | fprintf(stderr, "%s: can't open device %s: %s\n", progname, name, |
76 | error_get_pretty(local_err)); | |
77 | error_free(local_err); | |
4f6fd349 | 78 | bdrv_unref(qemuio_bs); |
734c3b85 | 79 | qemuio_bs = NULL; |
43642b38 DN |
80 | return 1; |
81 | } | |
82 | } | |
83 | ||
84 | return 0; | |
e3aff4f6 AL |
85 | } |
86 | ||
43642b38 | 87 | static void open_help(void) |
e3aff4f6 | 88 | { |
43642b38 | 89 | printf( |
e3aff4f6 AL |
90 | "\n" |
91 | " opens a new file in the requested mode\n" | |
92 | "\n" | |
93 | " Example:\n" | |
94 | " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n" | |
95 | "\n" | |
96 | " Opens a file for subsequent use by all of the other qemu-io commands.\n" | |
e3aff4f6 AL |
97 | " -r, -- open file read-only\n" |
98 | " -s, -- use snapshot file\n" | |
99 | " -n, -- disable host cache\n" | |
b543c5cd HR |
100 | " -g, -- allow file to grow (only applies to protocols)\n" |
101 | " -o, -- options to be given to the block driver" | |
e3aff4f6 AL |
102 | "\n"); |
103 | } | |
104 | ||
734c3b85 | 105 | static int open_f(BlockDriverState *bs, int argc, char **argv); |
22a2bdcb BS |
106 | |
107 | static const cmdinfo_t open_cmd = { | |
43642b38 DN |
108 | .name = "open", |
109 | .altname = "o", | |
110 | .cfunc = open_f, | |
111 | .argmin = 1, | |
112 | .argmax = -1, | |
113 | .flags = CMD_NOFILE_OK, | |
b543c5cd | 114 | .args = "[-Crsn] [-o options] [path]", |
43642b38 DN |
115 | .oneline = "open the file specified by path", |
116 | .help = open_help, | |
22a2bdcb | 117 | }; |
e3aff4f6 | 118 | |
b543c5cd HR |
119 | static QemuOptsList empty_opts = { |
120 | .name = "drive", | |
121 | .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head), | |
122 | .desc = { | |
123 | /* no elements => accept any params */ | |
124 | { /* end of list */ } | |
125 | }, | |
126 | }; | |
127 | ||
734c3b85 | 128 | static int open_f(BlockDriverState *bs, int argc, char **argv) |
e3aff4f6 | 129 | { |
43642b38 DN |
130 | int flags = 0; |
131 | int readonly = 0; | |
132 | int growable = 0; | |
133 | int c; | |
b543c5cd HR |
134 | QemuOpts *qopts; |
135 | QDict *opts = NULL; | |
43642b38 | 136 | |
b543c5cd | 137 | while ((c = getopt(argc, argv, "snrgo:")) != EOF) { |
43642b38 DN |
138 | switch (c) { |
139 | case 's': | |
140 | flags |= BDRV_O_SNAPSHOT; | |
141 | break; | |
142 | case 'n': | |
143 | flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; | |
144 | break; | |
145 | case 'r': | |
146 | readonly = 1; | |
147 | break; | |
148 | case 'g': | |
149 | growable = 1; | |
150 | break; | |
b543c5cd HR |
151 | case 'o': |
152 | qopts = qemu_opts_parse(&empty_opts, optarg, 0); | |
153 | if (qopts == NULL) { | |
154 | printf("could not parse option list -- %s\n", optarg); | |
155 | return 0; | |
156 | } | |
157 | opts = qemu_opts_to_qdict(qopts, opts); | |
158 | qemu_opts_del(qopts); | |
159 | break; | |
43642b38 | 160 | default: |
c2cdf5c5 | 161 | return qemuio_command_usage(&open_cmd); |
f5edb014 | 162 | } |
43642b38 DN |
163 | } |
164 | ||
165 | if (!readonly) { | |
166 | flags |= BDRV_O_RDWR; | |
167 | } | |
e3aff4f6 | 168 | |
fd0fee34 HR |
169 | if (optind == argc - 1) { |
170 | return openfile(argv[optind], flags, growable, opts); | |
171 | } else if (optind == argc) { | |
172 | return openfile(NULL, flags, growable, opts); | |
173 | } else { | |
c2cdf5c5 | 174 | return qemuio_command_usage(&open_cmd); |
43642b38 | 175 | } |
e3aff4f6 AL |
176 | } |
177 | ||
e681be7e KW |
178 | static int quit_f(BlockDriverState *bs, int argc, char **argv) |
179 | { | |
180 | return 1; | |
181 | } | |
182 | ||
183 | static const cmdinfo_t quit_cmd = { | |
184 | .name = "quit", | |
185 | .altname = "q", | |
186 | .cfunc = quit_f, | |
187 | .argmin = -1, | |
188 | .argmax = -1, | |
189 | .flags = CMD_FLAG_GLOBAL, | |
190 | .oneline = "exit the program", | |
191 | }; | |
192 | ||
e3aff4f6 AL |
193 | static void usage(const char *name) |
194 | { | |
43642b38 | 195 | printf( |
d208cc35 | 196 | "Usage: %s [-h] [-V] [-rsnm] [-c STRING] ... [file]\n" |
84844a20 | 197 | "QEMU Disk exerciser\n" |
e3aff4f6 | 198 | "\n" |
d208cc35 MK |
199 | " -c, --cmd STRING execute command with its arguments\n" |
200 | " from the given string\n" | |
e3aff4f6 AL |
201 | " -r, --read-only export read-only\n" |
202 | " -s, --snapshot use snapshot file\n" | |
203 | " -n, --nocache disable host cache\n" | |
1db6947d | 204 | " -g, --growable allow file to grow (only applies to protocols)\n" |
e3aff4f6 | 205 | " -m, --misalign misalign allocations for O_DIRECT\n" |
5c6c3a6c | 206 | " -k, --native-aio use kernel AIO implementation (on Linux only)\n" |
592fa070 | 207 | " -t, --cache=MODE use the given cache mode for the image\n" |
d7bb72c8 | 208 | " -T, --trace FILE enable trace events listed in the given file\n" |
e3aff4f6 AL |
209 | " -h, --help display this help and exit\n" |
210 | " -V, --version output version information and exit\n" | |
d208cc35 MK |
211 | "\n" |
212 | "See '%s -c help' for information on available commands." | |
e3aff4f6 | 213 | "\n", |
d208cc35 | 214 | name, name); |
e3aff4f6 AL |
215 | } |
216 | ||
d1174f13 KW |
217 | static char *get_prompt(void) |
218 | { | |
219 | static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ]; | |
220 | ||
221 | if (!prompt[0]) { | |
222 | snprintf(prompt, sizeof(prompt), "%s> ", progname); | |
223 | } | |
224 | ||
225 | return prompt; | |
226 | } | |
227 | ||
d5d1507b SW |
228 | static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque, |
229 | const char *fmt, ...) | |
d1174f13 | 230 | { |
0cf17e18 SH |
231 | va_list ap; |
232 | va_start(ap, fmt); | |
233 | vprintf(fmt, ap); | |
234 | va_end(ap); | |
d1174f13 | 235 | } |
0cf17e18 SH |
236 | |
237 | static void readline_flush_func(void *opaque) | |
d1174f13 | 238 | { |
0cf17e18 | 239 | fflush(stdout); |
d1174f13 KW |
240 | } |
241 | ||
0cf17e18 | 242 | static void readline_func(void *opaque, const char *str, void *readline_opaque) |
d1174f13 | 243 | { |
0cf17e18 SH |
244 | char **line = readline_opaque; |
245 | *line = g_strdup(str); | |
246 | } | |
247 | ||
4694020d SH |
248 | static void completion_match(const char *cmd, void *opaque) |
249 | { | |
250 | readline_add_completion(readline_state, cmd); | |
251 | } | |
252 | ||
0cf17e18 SH |
253 | static void readline_completion_func(void *opaque, const char *str) |
254 | { | |
4694020d SH |
255 | readline_set_completion_index(readline_state, strlen(str)); |
256 | qemuio_complete_command(str, completion_match, NULL); | |
0cf17e18 SH |
257 | } |
258 | ||
259 | static char *fetchline_readline(void) | |
260 | { | |
261 | char *line = NULL; | |
262 | ||
263 | readline_start(readline_state, get_prompt(), 0, readline_func, &line); | |
264 | while (!line) { | |
265 | int ch = getchar(); | |
266 | if (ch == EOF) { | |
267 | break; | |
d1174f13 | 268 | } |
0cf17e18 | 269 | readline_handle_byte(readline_state, ch); |
d1174f13 KW |
270 | } |
271 | return line; | |
272 | } | |
0cf17e18 SH |
273 | |
274 | #define MAXREADLINESZ 1024 | |
275 | static char *fetchline_fgets(void) | |
d1174f13 KW |
276 | { |
277 | char *p, *line = g_malloc(MAXREADLINESZ); | |
278 | ||
279 | if (!fgets(line, MAXREADLINESZ, stdin)) { | |
280 | g_free(line); | |
281 | return NULL; | |
282 | } | |
283 | ||
284 | p = line + strlen(line); | |
285 | if (p != line && p[-1] == '\n') { | |
286 | p[-1] = '\0'; | |
287 | } | |
288 | ||
289 | return line; | |
290 | } | |
0cf17e18 SH |
291 | |
292 | static char *fetchline(void) | |
293 | { | |
294 | if (readline_state) { | |
295 | return fetchline_readline(); | |
296 | } else { | |
297 | return fetchline_fgets(); | |
298 | } | |
299 | } | |
d1174f13 KW |
300 | |
301 | static void prep_fetchline(void *opaque) | |
302 | { | |
303 | int *fetchable = opaque; | |
304 | ||
305 | qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL); | |
306 | *fetchable= 1; | |
307 | } | |
308 | ||
309 | static void command_loop(void) | |
310 | { | |
311 | int i, done = 0, fetchable = 0, prompted = 0; | |
312 | char *input; | |
313 | ||
314 | for (i = 0; !done && i < ncmdline; i++) { | |
3d21994f | 315 | done = qemuio_command(qemuio_bs, cmdline[i]); |
d1174f13 KW |
316 | } |
317 | if (cmdline) { | |
318 | g_free(cmdline); | |
319 | return; | |
320 | } | |
321 | ||
322 | while (!done) { | |
323 | if (!prompted) { | |
324 | printf("%s", get_prompt()); | |
325 | fflush(stdout); | |
326 | qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable); | |
327 | prompted = 1; | |
328 | } | |
329 | ||
330 | main_loop_wait(false); | |
331 | ||
332 | if (!fetchable) { | |
333 | continue; | |
334 | } | |
335 | ||
336 | input = fetchline(); | |
337 | if (input == NULL) { | |
338 | break; | |
339 | } | |
3d21994f | 340 | done = qemuio_command(qemuio_bs, input); |
d1174f13 KW |
341 | g_free(input); |
342 | ||
343 | prompted = 0; | |
344 | fetchable = 0; | |
345 | } | |
346 | qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL); | |
347 | } | |
348 | ||
349 | static void add_user_command(char *optarg) | |
350 | { | |
351 | cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *)); | |
352 | cmdline[ncmdline-1] = optarg; | |
353 | } | |
354 | ||
0cf17e18 SH |
355 | static void reenable_tty_echo(void) |
356 | { | |
357 | qemu_set_tty_echo(STDIN_FILENO, true); | |
358 | } | |
359 | ||
e3aff4f6 AL |
360 | int main(int argc, char **argv) |
361 | { | |
43642b38 DN |
362 | int readonly = 0; |
363 | int growable = 0; | |
9e8f1835 | 364 | const char *sopt = "hVc:d:rsnmgkt:T:"; |
43642b38 DN |
365 | const struct option lopt[] = { |
366 | { "help", 0, NULL, 'h' }, | |
367 | { "version", 0, NULL, 'V' }, | |
368 | { "offset", 1, NULL, 'o' }, | |
369 | { "cmd", 1, NULL, 'c' }, | |
370 | { "read-only", 0, NULL, 'r' }, | |
371 | { "snapshot", 0, NULL, 's' }, | |
372 | { "nocache", 0, NULL, 'n' }, | |
373 | { "misalign", 0, NULL, 'm' }, | |
374 | { "growable", 0, NULL, 'g' }, | |
375 | { "native-aio", 0, NULL, 'k' }, | |
9e8f1835 | 376 | { "discard", 1, NULL, 'd' }, |
592fa070 | 377 | { "cache", 1, NULL, 't' }, |
d7bb72c8 | 378 | { "trace", 1, NULL, 'T' }, |
43642b38 DN |
379 | { NULL, 0, NULL, 0 } |
380 | }; | |
381 | int c; | |
382 | int opt_index = 0; | |
9e8f1835 | 383 | int flags = BDRV_O_UNMAP; |
43642b38 | 384 | |
526eda14 MK |
385 | #ifdef CONFIG_POSIX |
386 | signal(SIGPIPE, SIG_IGN); | |
387 | #endif | |
388 | ||
43642b38 | 389 | progname = basename(argv[0]); |
10f5bff6 | 390 | qemu_init_exec_dir(argv[0]); |
43642b38 DN |
391 | |
392 | while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) { | |
393 | switch (c) { | |
394 | case 's': | |
395 | flags |= BDRV_O_SNAPSHOT; | |
396 | break; | |
397 | case 'n': | |
398 | flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; | |
399 | break; | |
9e8f1835 PB |
400 | case 'd': |
401 | if (bdrv_parse_discard_flags(optarg, &flags) < 0) { | |
402 | error_report("Invalid discard option: %s", optarg); | |
403 | exit(1); | |
404 | } | |
405 | break; | |
43642b38 DN |
406 | case 'c': |
407 | add_user_command(optarg); | |
408 | break; | |
409 | case 'r': | |
410 | readonly = 1; | |
411 | break; | |
412 | case 'm': | |
f9883880 | 413 | qemuio_misalign = true; |
43642b38 DN |
414 | break; |
415 | case 'g': | |
416 | growable = 1; | |
417 | break; | |
418 | case 'k': | |
419 | flags |= BDRV_O_NATIVE_AIO; | |
420 | break; | |
592fa070 KW |
421 | case 't': |
422 | if (bdrv_parse_cache_flags(optarg, &flags) < 0) { | |
423 | error_report("Invalid cache option: %s", optarg); | |
424 | exit(1); | |
425 | } | |
426 | break; | |
d7bb72c8 SH |
427 | case 'T': |
428 | if (!trace_backend_init(optarg, NULL)) { | |
429 | exit(1); /* error message will have been printed */ | |
430 | } | |
431 | break; | |
43642b38 | 432 | case 'V': |
02da386a | 433 | printf("%s version %s\n", progname, QEMU_VERSION); |
43642b38 DN |
434 | exit(0); |
435 | case 'h': | |
436 | usage(progname); | |
437 | exit(0); | |
438 | default: | |
439 | usage(progname); | |
440 | exit(1); | |
f5edb014 | 441 | } |
43642b38 DN |
442 | } |
443 | ||
444 | if ((argc - optind) > 1) { | |
445 | usage(progname); | |
446 | exit(1); | |
447 | } | |
e3aff4f6 | 448 | |
a57d1143 | 449 | qemu_init_main_loop(); |
2592c59a | 450 | bdrv_init(); |
a57d1143 | 451 | |
43642b38 | 452 | /* initialize commands */ |
c2cdf5c5 KW |
453 | qemuio_add_command(&quit_cmd); |
454 | qemuio_add_command(&open_cmd); | |
455 | qemuio_add_command(&close_cmd); | |
43642b38 | 456 | |
0cf17e18 SH |
457 | if (isatty(STDIN_FILENO)) { |
458 | readline_state = readline_init(readline_printf_func, | |
459 | readline_flush_func, | |
460 | NULL, | |
461 | readline_completion_func); | |
462 | qemu_set_tty_echo(STDIN_FILENO, false); | |
463 | atexit(reenable_tty_echo); | |
464 | } | |
465 | ||
43642b38 DN |
466 | /* open the device */ |
467 | if (!readonly) { | |
468 | flags |= BDRV_O_RDWR; | |
469 | } | |
470 | ||
471 | if ((argc - optind) == 1) { | |
b543c5cd | 472 | openfile(argv[optind], flags, growable, NULL); |
43642b38 DN |
473 | } |
474 | command_loop(); | |
e3aff4f6 | 475 | |
43642b38 | 476 | /* |
922453bc | 477 | * Make sure all outstanding requests complete before the program exits. |
43642b38 | 478 | */ |
922453bc | 479 | bdrv_drain_all(); |
95533d5f | 480 | |
734c3b85 | 481 | if (qemuio_bs) { |
4f6fd349 | 482 | bdrv_unref(qemuio_bs); |
43642b38 | 483 | } |
0cf17e18 | 484 | g_free(readline_state); |
43642b38 | 485 | return 0; |
e3aff4f6 | 486 | } |