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