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