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