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