blob: d977a6e5537edadcce5ad5f011f9b3d2ac88d733 [file] [log] [blame]
aliguorie3aff4f2009-04-05 19:14:04 +00001/*
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 */
Peter Maydell80c71a22016-01-18 18:01:42 +000010#include "qemu/osdep.h"
aliguorie3aff4f2009-04-05 19:14:04 +000011#include <getopt.h>
Stefan Weilc32d7662009-08-31 22:16:16 +020012#include <libgen.h>
aliguorie3aff4f2009-04-05 19:14:04 +000013
Markus Armbrusterda34e652016-03-14 09:01:28 +010014#include "qapi/error.h"
Kevin Wolf3d219942013-06-05 14:19:39 +020015#include "qemu-io.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010016#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010017#include "qemu/main-loop.h"
Max Reitzb543c5c2013-10-11 14:02:10 +020018#include "qemu/option.h"
19#include "qemu/config-file.h"
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010020#include "qemu/readline.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010021#include "qapi/qmp/qstring.h"
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +000022#include "qom/object_interfaces.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020023#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010024#include "block/block_int.h"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +000025#include "trace/control.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010026#include "crypto/init.h"
aliguorie3aff4f2009-04-05 19:14:04 +000027
Devin Nakamura43642b32011-07-11 11:22:16 -040028#define CMD_NOFILE_OK 0x01
aliguorie3aff4f2009-04-05 19:14:04 +000029
Stefan Weilf9883882014-03-05 22:23:00 +010030static char *progname;
aliguorie3aff4f2009-04-05 19:14:04 +000031
Markus Armbruster26f54e92014-10-07 13:59:04 +020032static BlockBackend *qemuio_blk;
Kevin Wolf191c2892010-09-16 13:18:08 +020033
Kevin Wolfd1174f12013-06-05 14:19:37 +020034/* qemu-io commands passed using -c */
35static int ncmdline;
36static char **cmdline;
Daniel P. Berrange499afa22016-02-17 10:10:18 +000037static bool imageOpts;
Kevin Wolfd1174f12013-06-05 14:19:37 +020038
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010039static ReadLineState *readline_state;
40
Max Reitz4c7b7e92015-02-05 13:58:22 -050041static int close_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +000042{
Markus Armbruster26f54e92014-10-07 13:59:04 +020043 blk_unref(qemuio_blk);
Markus Armbruster26f54e92014-10-07 13:59:04 +020044 qemuio_blk = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -040045 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000046}
47
48static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -040049 .name = "close",
50 .altname = "c",
51 .cfunc = close_f,
52 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +000053};
54
Kevin Wolfe151fc12016-03-15 12:52:30 +010055static int openfile(char *name, int flags, bool writethrough, QDict *opts)
aliguorie3aff4f2009-04-05 19:14:04 +000056{
Max Reitz34b5d2c2013-09-05 14:45:29 +020057 Error *local_err = NULL;
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010058 BlockDriverState *bs;
Max Reitz34b5d2c2013-09-05 14:45:29 +020059
Max Reitz1b58b432015-02-05 13:58:20 -050060 if (qemuio_blk) {
Markus Armbrusterb9884682015-12-18 16:35:18 +010061 error_report("file open already, try 'help close'");
Markus Armbruster29f26012014-05-28 11:16:59 +020062 QDECREF(opts);
Devin Nakamura43642b32011-07-11 11:22:16 -040063 return 1;
64 }
aliguorie3aff4f2009-04-05 19:14:04 +000065
Max Reitzefaa7c42016-03-16 19:54:38 +010066 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
Max Reitz1b58b432015-02-05 13:58:20 -050067 if (!qemuio_blk) {
Markus Armbrusterb9884682015-12-18 16:35:18 +010068 error_reportf_err(local_err, "can't open%s%s: ",
69 name ? " device " : "", name ?: "");
Markus Armbrusterdbb651c2014-09-08 18:50:58 +020070 return 1;
Devin Nakamura43642b32011-07-11 11:22:16 -040071 }
Christoph Hellwig1db69472009-07-15 23:11:21 +020072
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010073 bs = blk_bs(qemuio_blk);
Daniel P. Berrange4ef130f2016-03-21 14:11:43 +000074 if (bdrv_is_encrypted(bs) && bdrv_key_required(bs)) {
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010075 char password[256];
76 printf("Disk image '%s' is encrypted.\n", name);
77 if (qemu_read_password(password, sizeof(password)) < 0) {
78 error_report("No password given");
79 goto error;
80 }
81 if (bdrv_set_key(bs, password) < 0) {
82 error_report("invalid password");
83 goto error;
84 }
85 }
86
Kevin Wolfe151fc12016-03-15 12:52:30 +010087 blk_set_enable_write_cache(qemuio_blk, !writethrough);
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010088
Devin Nakamura43642b32011-07-11 11:22:16 -040089 return 0;
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010090
91 error:
92 blk_unref(qemuio_blk);
93 qemuio_blk = NULL;
94 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +000095}
96
Devin Nakamura43642b32011-07-11 11:22:16 -040097static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +000098{
Devin Nakamura43642b32011-07-11 11:22:16 -040099 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000100"\n"
101" opens a new file in the requested mode\n"
102"\n"
103" Example:\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600104" 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000105"\n"
106" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000107" -r, -- open file read-only\n"
108" -s, -- use snapshot file\n"
Eric Blakeb8d970f2016-05-07 21:16:41 -0600109" -n, -- disable host cache, short for -t none\n"
110" -k, -- use kernel AIO implementation (on Linux only)\n"
111" -t, -- use the given cache mode for the image\n"
112" -d, -- use the given discard mode for the image\n"
Max Reitzb543c5c2013-10-11 14:02:10 +0200113" -o, -- options to be given to the block driver"
aliguorie3aff4f2009-04-05 19:14:04 +0000114"\n");
115}
116
Max Reitz4c7b7e92015-02-05 13:58:22 -0500117static int open_f(BlockBackend *blk, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000118
119static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400120 .name = "open",
121 .altname = "o",
122 .cfunc = open_f,
123 .argmin = 1,
124 .argmax = -1,
125 .flags = CMD_NOFILE_OK,
Eric Blakeb8d970f2016-05-07 21:16:41 -0600126 .args = "[-rsnk] [-t cache] [-d discard] [-o options] [path]",
Devin Nakamura43642b32011-07-11 11:22:16 -0400127 .oneline = "open the file specified by path",
128 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000129};
aliguorie3aff4f2009-04-05 19:14:04 +0000130
Max Reitzb543c5c2013-10-11 14:02:10 +0200131static QemuOptsList empty_opts = {
132 .name = "drive",
Markus Armbruster443422f2014-05-28 11:16:58 +0200133 .merge_lists = true,
Max Reitzb543c5c2013-10-11 14:02:10 +0200134 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
135 .desc = {
136 /* no elements => accept any params */
137 { /* end of list */ }
138 },
139};
140
Max Reitz4c7b7e92015-02-05 13:58:22 -0500141static int open_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000142{
Eric Blakeb8d970f2016-05-07 21:16:41 -0600143 int flags = BDRV_O_UNMAP;
Devin Nakamura43642b32011-07-11 11:22:16 -0400144 int readonly = 0;
Kevin Wolfe151fc12016-03-15 12:52:30 +0100145 bool writethrough = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400146 int c;
Max Reitzb543c5c2013-10-11 14:02:10 +0200147 QemuOpts *qopts;
Markus Armbruster443422f2014-05-28 11:16:58 +0200148 QDict *opts;
aliguorie3aff4f2009-04-05 19:14:04 +0000149
Eric Blakeb8d970f2016-05-07 21:16:41 -0600150 while ((c = getopt(argc, argv, "snro:kt:d:")) != -1) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400151 switch (c) {
152 case 's':
153 flags |= BDRV_O_SNAPSHOT;
154 break;
155 case 'n':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100156 flags |= BDRV_O_NOCACHE;
157 writethrough = false;
Devin Nakamura43642b32011-07-11 11:22:16 -0400158 break;
159 case 'r':
160 readonly = 1;
161 break;
Eric Blakeb8d970f2016-05-07 21:16:41 -0600162 case 'k':
163 flags |= BDRV_O_NATIVE_AIO;
164 break;
165 case 't':
166 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
167 error_report("Invalid cache option: %s", optarg);
168 qemu_opts_reset(&empty_opts);
169 return 0;
170 }
171 break;
172 case 'd':
173 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
174 error_report("Invalid discard option: %s", optarg);
175 qemu_opts_reset(&empty_opts);
176 return 0;
177 }
178 break;
Max Reitzb543c5c2013-10-11 14:02:10 +0200179 case 'o':
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000180 if (imageOpts) {
181 printf("--image-opts and 'open -o' are mutually exclusive\n");
Eric Blakeb8d970f2016-05-07 21:16:41 -0600182 qemu_opts_reset(&empty_opts);
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000183 return 0;
184 }
Markus Armbruster70b94332015-02-13 12:50:26 +0100185 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
Markus Armbruster443422f2014-05-28 11:16:58 +0200186 qemu_opts_reset(&empty_opts);
Max Reitzb543c5c2013-10-11 14:02:10 +0200187 return 0;
188 }
Max Reitzb543c5c2013-10-11 14:02:10 +0200189 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400190 default:
Markus Armbruster443422f2014-05-28 11:16:58 +0200191 qemu_opts_reset(&empty_opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200192 return qemuio_command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200193 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400194 }
aliguorie3aff4f2009-04-05 19:14:04 +0000195
Devin Nakamura43642b32011-07-11 11:22:16 -0400196 if (!readonly) {
197 flags |= BDRV_O_RDWR;
198 }
aliguorie3aff4f2009-04-05 19:14:04 +0000199
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000200 if (imageOpts && (optind == argc - 1)) {
201 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
202 qemu_opts_reset(&empty_opts);
203 return 0;
204 }
205 optind++;
206 }
207
Markus Armbruster443422f2014-05-28 11:16:58 +0200208 qopts = qemu_opts_find(&empty_opts, NULL);
209 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
210 qemu_opts_reset(&empty_opts);
211
Max Reitzfd0fee32013-12-20 19:28:20 +0100212 if (optind == argc - 1) {
Kevin Wolfe151fc12016-03-15 12:52:30 +0100213 return openfile(argv[optind], flags, writethrough, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100214 } else if (optind == argc) {
Kevin Wolfe151fc12016-03-15 12:52:30 +0100215 return openfile(NULL, flags, writethrough, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100216 } else {
Markus Armbruster29f26012014-05-28 11:16:59 +0200217 QDECREF(opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200218 return qemuio_command_usage(&open_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400219 }
aliguorie3aff4f2009-04-05 19:14:04 +0000220}
221
Max Reitz4c7b7e92015-02-05 13:58:22 -0500222static int quit_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfe681be72013-06-05 14:19:34 +0200223{
224 return 1;
225}
226
227static const cmdinfo_t quit_cmd = {
228 .name = "quit",
229 .altname = "q",
230 .cfunc = quit_f,
231 .argmin = -1,
232 .argmax = -1,
233 .flags = CMD_FLAG_GLOBAL,
234 .oneline = "exit the program",
235};
236
aliguorie3aff4f2009-04-05 19:14:04 +0000237static void usage(const char *name)
238{
Devin Nakamura43642b32011-07-11 11:22:16 -0400239 printf(
Eric Blakee4e12bb2016-05-07 21:16:40 -0600240"Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200241"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000242"\n"
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000243" --object OBJECTDEF define an object such as 'secret' for\n"
244" passwords and/or encryption keys\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600245" --image-opts treat file as option string\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400246" -c, --cmd STRING execute command with its arguments\n"
247" from the given string\n"
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100248" -f, --format FMT specifies the block driver to use\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000249" -r, --read-only export read-only\n"
250" -s, --snapshot use snapshot file\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600251" -n, --nocache disable host cache, short for -t none\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000252" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200253" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200254" -t, --cache=MODE use the given cache mode for the image\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600255" -d, --discard=MODE use the given discard mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000256" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000257" -h, --help display this help and exit\n"
258" -V, --version output version information and exit\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400259"\n"
260"See '%s -c help' for information on available commands."
aliguorie3aff4f2009-04-05 19:14:04 +0000261"\n",
Maria Kustovad208cc32014-03-18 09:59:19 +0400262 name, name);
aliguorie3aff4f2009-04-05 19:14:04 +0000263}
264
Kevin Wolfd1174f12013-06-05 14:19:37 +0200265static char *get_prompt(void)
266{
267 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
268
269 if (!prompt[0]) {
270 snprintf(prompt, sizeof(prompt), "%s> ", progname);
271 }
272
273 return prompt;
274}
275
Stefan Weild5d15072014-01-25 18:18:23 +0100276static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
277 const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200278{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100279 va_list ap;
280 va_start(ap, fmt);
281 vprintf(fmt, ap);
282 va_end(ap);
283}
284
285static void readline_flush_func(void *opaque)
286{
287 fflush(stdout);
288}
289
290static void readline_func(void *opaque, const char *str, void *readline_opaque)
291{
292 char **line = readline_opaque;
293 *line = g_strdup(str);
294}
295
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100296static void completion_match(const char *cmd, void *opaque)
297{
298 readline_add_completion(readline_state, cmd);
299}
300
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100301static void readline_completion_func(void *opaque, const char *str)
302{
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100303 readline_set_completion_index(readline_state, strlen(str));
304 qemuio_complete_command(str, completion_match, NULL);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100305}
306
307static char *fetchline_readline(void)
308{
309 char *line = NULL;
310
311 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
312 while (!line) {
313 int ch = getchar();
314 if (ch == EOF) {
315 break;
316 }
317 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200318 }
319 return line;
320}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200321
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100322#define MAXREADLINESZ 1024
323static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200324{
325 char *p, *line = g_malloc(MAXREADLINESZ);
326
327 if (!fgets(line, MAXREADLINESZ, stdin)) {
328 g_free(line);
329 return NULL;
330 }
331
332 p = line + strlen(line);
333 if (p != line && p[-1] == '\n') {
334 p[-1] = '\0';
335 }
336
337 return line;
338}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100339
340static char *fetchline(void)
341{
342 if (readline_state) {
343 return fetchline_readline();
344 } else {
345 return fetchline_fgets();
346 }
347}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200348
349static void prep_fetchline(void *opaque)
350{
351 int *fetchable = opaque;
352
353 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
354 *fetchable= 1;
355}
356
357static void command_loop(void)
358{
359 int i, done = 0, fetchable = 0, prompted = 0;
360 char *input;
361
362 for (i = 0; !done && i < ncmdline; i++) {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500363 done = qemuio_command(qemuio_blk, cmdline[i]);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200364 }
365 if (cmdline) {
366 g_free(cmdline);
367 return;
368 }
369
370 while (!done) {
371 if (!prompted) {
372 printf("%s", get_prompt());
373 fflush(stdout);
374 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
375 prompted = 1;
376 }
377
378 main_loop_wait(false);
379
380 if (!fetchable) {
381 continue;
382 }
383
384 input = fetchline();
385 if (input == NULL) {
386 break;
387 }
Max Reitz4c7b7e92015-02-05 13:58:22 -0500388 done = qemuio_command(qemuio_blk, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200389 g_free(input);
390
391 prompted = 0;
392 fetchable = 0;
393 }
394 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
395}
396
397static void add_user_command(char *optarg)
398{
Markus Armbruster5839e532014-08-19 10:31:08 +0200399 cmdline = g_renew(char *, cmdline, ++ncmdline);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200400 cmdline[ncmdline-1] = optarg;
401}
402
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100403static void reenable_tty_echo(void)
404{
405 qemu_set_tty_echo(STDIN_FILENO, true);
406}
407
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000408enum {
409 OPTION_OBJECT = 256,
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000410 OPTION_IMAGE_OPTS = 257,
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000411};
412
413static QemuOptsList qemu_object_opts = {
414 .name = "object",
415 .implied_opt_name = "qom-type",
416 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
417 .desc = {
418 { }
419 },
420};
421
422
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000423static QemuOptsList file_opts = {
424 .name = "file",
425 .implied_opt_name = "file",
426 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
427 .desc = {
428 /* no elements => accept any params */
429 { /* end of list */ }
430 },
431};
432
aliguorie3aff4f2009-04-05 19:14:04 +0000433int main(int argc, char **argv)
434{
Devin Nakamura43642b32011-07-11 11:22:16 -0400435 int readonly = 0;
Eric Blakee4e12bb2016-05-07 21:16:40 -0600436 const char *sopt = "hVc:d:f:rsnmkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -0400437 const struct option lopt[] = {
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000438 { "help", no_argument, NULL, 'h' },
439 { "version", no_argument, NULL, 'V' },
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000440 { "cmd", required_argument, NULL, 'c' },
441 { "format", required_argument, NULL, 'f' },
442 { "read-only", no_argument, NULL, 'r' },
443 { "snapshot", no_argument, NULL, 's' },
444 { "nocache", no_argument, NULL, 'n' },
445 { "misalign", no_argument, NULL, 'm' },
446 { "native-aio", no_argument, NULL, 'k' },
447 { "discard", required_argument, NULL, 'd' },
448 { "cache", required_argument, NULL, 't' },
449 { "trace", required_argument, NULL, 'T' },
450 { "object", required_argument, NULL, OPTION_OBJECT },
451 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
Devin Nakamura43642b32011-07-11 11:22:16 -0400452 { NULL, 0, NULL, 0 }
453 };
454 int c;
455 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100456 int flags = BDRV_O_UNMAP;
Kevin Wolfe151fc12016-03-15 12:52:30 +0100457 bool writethrough = true;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300458 Error *local_error = NULL;
Max Reitz1b58b432015-02-05 13:58:20 -0500459 QDict *opts = NULL;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000460 const char *format = NULL;
aliguorie3aff4f2009-04-05 19:14:04 +0000461
MORITA Kazutaka526eda12013-07-23 17:30:11 +0900462#ifdef CONFIG_POSIX
463 signal(SIGPIPE, SIG_IGN);
464#endif
465
Devin Nakamura43642b32011-07-11 11:22:16 -0400466 progname = basename(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800467 qemu_init_exec_dir(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000468
Eduardo Habkoste8f2d272016-05-12 11:10:04 -0300469 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100470
Daniel P. Berrange064097d2016-02-10 18:41:01 +0000471 module_call_init(MODULE_INIT_QOM);
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000472 qemu_add_opts(&qemu_object_opts);
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100473 bdrv_init();
474
Devin Nakamura43642b32011-07-11 11:22:16 -0400475 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
476 switch (c) {
477 case 's':
478 flags |= BDRV_O_SNAPSHOT;
479 break;
480 case 'n':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100481 flags |= BDRV_O_NOCACHE;
482 writethrough = false;
Devin Nakamura43642b32011-07-11 11:22:16 -0400483 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100484 case 'd':
485 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
486 error_report("Invalid discard option: %s", optarg);
487 exit(1);
488 }
489 break;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100490 case 'f':
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000491 format = optarg;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100492 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400493 case 'c':
494 add_user_command(optarg);
495 break;
496 case 'r':
497 readonly = 1;
498 break;
499 case 'm':
Stefan Weilf9883882014-03-05 22:23:00 +0100500 qemuio_misalign = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400501 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400502 case 'k':
503 flags |= BDRV_O_NATIVE_AIO;
504 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200505 case 't':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100506 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
Kevin Wolf592fa072012-04-18 12:07:39 +0200507 error_report("Invalid cache option: %s", optarg);
508 exit(1);
509 }
510 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000511 case 'T':
Paolo Bonzini41fc57e2016-01-07 16:55:24 +0300512 if (!trace_init_backends()) {
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000513 exit(1); /* error message will have been printed */
514 }
515 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400516 case 'V':
Kevin Wolf02da3862013-06-05 14:19:40 +0200517 printf("%s version %s\n", progname, QEMU_VERSION);
Devin Nakamura43642b32011-07-11 11:22:16 -0400518 exit(0);
519 case 'h':
520 usage(progname);
521 exit(0);
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000522 case OPTION_OBJECT: {
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000523 QemuOpts *qopts;
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000524 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
525 optarg, true);
526 if (!qopts) {
527 exit(1);
528 }
529 } break;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000530 case OPTION_IMAGE_OPTS:
531 imageOpts = true;
532 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400533 default:
534 usage(progname);
535 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200536 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400537 }
aliguorie3aff4f2009-04-05 19:14:04 +0000538
Devin Nakamura43642b32011-07-11 11:22:16 -0400539 if ((argc - optind) > 1) {
540 usage(progname);
541 exit(1);
542 }
aliguorie3aff4f2009-04-05 19:14:04 +0000543
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000544 if (format && imageOpts) {
545 error_report("--image-opts and -f are mutually exclusive");
546 exit(1);
547 }
548
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300549 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100550 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300551 exit(1);
552 }
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800553
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000554 if (qemu_opts_foreach(&qemu_object_opts,
555 user_creatable_add_opts_foreach,
Markus Armbruster51b9b472016-04-27 16:29:09 +0200556 NULL, NULL)) {
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000557 exit(1);
558 }
559
Devin Nakamura43642b32011-07-11 11:22:16 -0400560 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200561 qemuio_add_command(&quit_cmd);
562 qemuio_add_command(&open_cmd);
563 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400564
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100565 if (isatty(STDIN_FILENO)) {
566 readline_state = readline_init(readline_printf_func,
567 readline_flush_func,
568 NULL,
569 readline_completion_func);
570 qemu_set_tty_echo(STDIN_FILENO, false);
571 atexit(reenable_tty_echo);
572 }
573
Devin Nakamura43642b32011-07-11 11:22:16 -0400574 /* open the device */
575 if (!readonly) {
576 flags |= BDRV_O_RDWR;
577 }
578
579 if ((argc - optind) == 1) {
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000580 if (imageOpts) {
581 QemuOpts *qopts = NULL;
582 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
583 if (!qopts) {
584 exit(1);
585 }
586 opts = qemu_opts_to_qdict(qopts, NULL);
Kevin Wolfe151fc12016-03-15 12:52:30 +0100587 openfile(NULL, flags, writethrough, opts);
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000588 } else {
589 if (format) {
590 opts = qdict_new();
591 qdict_put(opts, "driver", qstring_from_str(format));
592 }
Kevin Wolfe151fc12016-03-15 12:52:30 +0100593 openfile(argv[optind], flags, writethrough, opts);
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000594 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400595 }
596 command_loop();
597
598 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000599 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400600 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000601 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400602
Markus Armbruster26f54e92014-10-07 13:59:04 +0200603 blk_unref(qemuio_blk);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100604 g_free(readline_state);
Devin Nakamura43642b32011-07-11 11:22:16 -0400605 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000606}