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