blob: 288bba84b9c229c4158777bc28c287881dd0bf0a [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"
104" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
105"\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"
109" -n, -- disable host cache\n"
Max Reitzb543c5c2013-10-11 14:02:10 +0200110" -o, -- options to be given to the block driver"
aliguorie3aff4f2009-04-05 19:14:04 +0000111"\n");
112}
113
Max Reitz4c7b7e92015-02-05 13:58:22 -0500114static int open_f(BlockBackend *blk, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000115
116static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400117 .name = "open",
118 .altname = "o",
119 .cfunc = open_f,
120 .argmin = 1,
121 .argmax = -1,
122 .flags = CMD_NOFILE_OK,
Max Reitzb543c5c2013-10-11 14:02:10 +0200123 .args = "[-Crsn] [-o options] [path]",
Devin Nakamura43642b32011-07-11 11:22:16 -0400124 .oneline = "open the file specified by path",
125 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000126};
aliguorie3aff4f2009-04-05 19:14:04 +0000127
Max Reitzb543c5c2013-10-11 14:02:10 +0200128static QemuOptsList empty_opts = {
129 .name = "drive",
Markus Armbruster443422f2014-05-28 11:16:58 +0200130 .merge_lists = true,
Max Reitzb543c5c2013-10-11 14:02:10 +0200131 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
132 .desc = {
133 /* no elements => accept any params */
134 { /* end of list */ }
135 },
136};
137
Max Reitz4c7b7e92015-02-05 13:58:22 -0500138static int open_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000139{
Devin Nakamura43642b32011-07-11 11:22:16 -0400140 int flags = 0;
141 int readonly = 0;
Kevin Wolfe151fc12016-03-15 12:52:30 +0100142 bool writethrough = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400143 int c;
Max Reitzb543c5c2013-10-11 14:02:10 +0200144 QemuOpts *qopts;
Markus Armbruster443422f2014-05-28 11:16:58 +0200145 QDict *opts;
aliguorie3aff4f2009-04-05 19:14:04 +0000146
Eric Blakeb062ad82015-05-12 09:10:56 -0600147 while ((c = getopt(argc, argv, "snrgo:")) != -1) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400148 switch (c) {
149 case 's':
150 flags |= BDRV_O_SNAPSHOT;
151 break;
152 case 'n':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100153 flags |= BDRV_O_NOCACHE;
154 writethrough = false;
Devin Nakamura43642b32011-07-11 11:22:16 -0400155 break;
156 case 'r':
157 readonly = 1;
158 break;
Max Reitzb543c5c2013-10-11 14:02:10 +0200159 case 'o':
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000160 if (imageOpts) {
161 printf("--image-opts and 'open -o' are mutually exclusive\n");
162 return 0;
163 }
Markus Armbruster70b94332015-02-13 12:50:26 +0100164 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
Markus Armbruster443422f2014-05-28 11:16:58 +0200165 qemu_opts_reset(&empty_opts);
Max Reitzb543c5c2013-10-11 14:02:10 +0200166 return 0;
167 }
Max Reitzb543c5c2013-10-11 14:02:10 +0200168 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400169 default:
Markus Armbruster443422f2014-05-28 11:16:58 +0200170 qemu_opts_reset(&empty_opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200171 return qemuio_command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200172 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400173 }
aliguorie3aff4f2009-04-05 19:14:04 +0000174
Devin Nakamura43642b32011-07-11 11:22:16 -0400175 if (!readonly) {
176 flags |= BDRV_O_RDWR;
177 }
aliguorie3aff4f2009-04-05 19:14:04 +0000178
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000179 if (imageOpts && (optind == argc - 1)) {
180 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
181 qemu_opts_reset(&empty_opts);
182 return 0;
183 }
184 optind++;
185 }
186
Markus Armbruster443422f2014-05-28 11:16:58 +0200187 qopts = qemu_opts_find(&empty_opts, NULL);
188 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
189 qemu_opts_reset(&empty_opts);
190
Max Reitzfd0fee32013-12-20 19:28:20 +0100191 if (optind == argc - 1) {
Kevin Wolfe151fc12016-03-15 12:52:30 +0100192 return openfile(argv[optind], flags, writethrough, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100193 } else if (optind == argc) {
Kevin Wolfe151fc12016-03-15 12:52:30 +0100194 return openfile(NULL, flags, writethrough, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100195 } else {
Markus Armbruster29f26012014-05-28 11:16:59 +0200196 QDECREF(opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200197 return qemuio_command_usage(&open_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400198 }
aliguorie3aff4f2009-04-05 19:14:04 +0000199}
200
Max Reitz4c7b7e92015-02-05 13:58:22 -0500201static int quit_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfe681be72013-06-05 14:19:34 +0200202{
203 return 1;
204}
205
206static const cmdinfo_t quit_cmd = {
207 .name = "quit",
208 .altname = "q",
209 .cfunc = quit_f,
210 .argmin = -1,
211 .argmax = -1,
212 .flags = CMD_FLAG_GLOBAL,
213 .oneline = "exit the program",
214};
215
aliguorie3aff4f2009-04-05 19:14:04 +0000216static void usage(const char *name)
217{
Devin Nakamura43642b32011-07-11 11:22:16 -0400218 printf(
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100219"Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200220"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000221"\n"
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000222" --object OBJECTDEF define an object such as 'secret' for\n"
223" passwords and/or encryption keys\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400224" -c, --cmd STRING execute command with its arguments\n"
225" from the given string\n"
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100226" -f, --format FMT specifies the block driver to use\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000227" -r, --read-only export read-only\n"
228" -s, --snapshot use snapshot file\n"
229" -n, --nocache disable host cache\n"
230" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200231" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200232" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000233" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000234" -h, --help display this help and exit\n"
235" -V, --version output version information and exit\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400236"\n"
237"See '%s -c help' for information on available commands."
aliguorie3aff4f2009-04-05 19:14:04 +0000238"\n",
Maria Kustovad208cc32014-03-18 09:59:19 +0400239 name, name);
aliguorie3aff4f2009-04-05 19:14:04 +0000240}
241
Kevin Wolfd1174f12013-06-05 14:19:37 +0200242static char *get_prompt(void)
243{
244 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
245
246 if (!prompt[0]) {
247 snprintf(prompt, sizeof(prompt), "%s> ", progname);
248 }
249
250 return prompt;
251}
252
Stefan Weild5d15072014-01-25 18:18:23 +0100253static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
254 const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200255{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100256 va_list ap;
257 va_start(ap, fmt);
258 vprintf(fmt, ap);
259 va_end(ap);
260}
261
262static void readline_flush_func(void *opaque)
263{
264 fflush(stdout);
265}
266
267static void readline_func(void *opaque, const char *str, void *readline_opaque)
268{
269 char **line = readline_opaque;
270 *line = g_strdup(str);
271}
272
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100273static void completion_match(const char *cmd, void *opaque)
274{
275 readline_add_completion(readline_state, cmd);
276}
277
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100278static void readline_completion_func(void *opaque, const char *str)
279{
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100280 readline_set_completion_index(readline_state, strlen(str));
281 qemuio_complete_command(str, completion_match, NULL);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100282}
283
284static char *fetchline_readline(void)
285{
286 char *line = NULL;
287
288 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
289 while (!line) {
290 int ch = getchar();
291 if (ch == EOF) {
292 break;
293 }
294 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200295 }
296 return line;
297}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200298
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100299#define MAXREADLINESZ 1024
300static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200301{
302 char *p, *line = g_malloc(MAXREADLINESZ);
303
304 if (!fgets(line, MAXREADLINESZ, stdin)) {
305 g_free(line);
306 return NULL;
307 }
308
309 p = line + strlen(line);
310 if (p != line && p[-1] == '\n') {
311 p[-1] = '\0';
312 }
313
314 return line;
315}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100316
317static char *fetchline(void)
318{
319 if (readline_state) {
320 return fetchline_readline();
321 } else {
322 return fetchline_fgets();
323 }
324}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200325
326static void prep_fetchline(void *opaque)
327{
328 int *fetchable = opaque;
329
330 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
331 *fetchable= 1;
332}
333
334static void command_loop(void)
335{
336 int i, done = 0, fetchable = 0, prompted = 0;
337 char *input;
338
339 for (i = 0; !done && i < ncmdline; i++) {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500340 done = qemuio_command(qemuio_blk, cmdline[i]);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200341 }
342 if (cmdline) {
343 g_free(cmdline);
344 return;
345 }
346
347 while (!done) {
348 if (!prompted) {
349 printf("%s", get_prompt());
350 fflush(stdout);
351 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
352 prompted = 1;
353 }
354
355 main_loop_wait(false);
356
357 if (!fetchable) {
358 continue;
359 }
360
361 input = fetchline();
362 if (input == NULL) {
363 break;
364 }
Max Reitz4c7b7e92015-02-05 13:58:22 -0500365 done = qemuio_command(qemuio_blk, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200366 g_free(input);
367
368 prompted = 0;
369 fetchable = 0;
370 }
371 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
372}
373
374static void add_user_command(char *optarg)
375{
Markus Armbruster5839e532014-08-19 10:31:08 +0200376 cmdline = g_renew(char *, cmdline, ++ncmdline);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200377 cmdline[ncmdline-1] = optarg;
378}
379
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100380static void reenable_tty_echo(void)
381{
382 qemu_set_tty_echo(STDIN_FILENO, true);
383}
384
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000385enum {
386 OPTION_OBJECT = 256,
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000387 OPTION_IMAGE_OPTS = 257,
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000388};
389
390static QemuOptsList qemu_object_opts = {
391 .name = "object",
392 .implied_opt_name = "qom-type",
393 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
394 .desc = {
395 { }
396 },
397};
398
399
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000400static QemuOptsList file_opts = {
401 .name = "file",
402 .implied_opt_name = "file",
403 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
404 .desc = {
405 /* no elements => accept any params */
406 { /* end of list */ }
407 },
408};
409
aliguorie3aff4f2009-04-05 19:14:04 +0000410int main(int argc, char **argv)
411{
Devin Nakamura43642b32011-07-11 11:22:16 -0400412 int readonly = 0;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100413 const char *sopt = "hVc:d:f:rsnmgkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -0400414 const struct option lopt[] = {
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000415 { "help", no_argument, NULL, 'h' },
416 { "version", no_argument, NULL, 'V' },
417 { "offset", required_argument, NULL, 'o' },
418 { "cmd", required_argument, NULL, 'c' },
419 { "format", required_argument, NULL, 'f' },
420 { "read-only", no_argument, NULL, 'r' },
421 { "snapshot", no_argument, NULL, 's' },
422 { "nocache", no_argument, NULL, 'n' },
423 { "misalign", no_argument, NULL, 'm' },
424 { "native-aio", no_argument, NULL, 'k' },
425 { "discard", required_argument, NULL, 'd' },
426 { "cache", required_argument, NULL, 't' },
427 { "trace", required_argument, NULL, 'T' },
428 { "object", required_argument, NULL, OPTION_OBJECT },
429 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
Devin Nakamura43642b32011-07-11 11:22:16 -0400430 { NULL, 0, NULL, 0 }
431 };
432 int c;
433 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100434 int flags = BDRV_O_UNMAP;
Kevin Wolfe151fc12016-03-15 12:52:30 +0100435 bool writethrough = true;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300436 Error *local_error = NULL;
Max Reitz1b58b432015-02-05 13:58:20 -0500437 QDict *opts = NULL;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000438 const char *format = NULL;
aliguorie3aff4f2009-04-05 19:14:04 +0000439
MORITA Kazutaka526eda12013-07-23 17:30:11 +0900440#ifdef CONFIG_POSIX
441 signal(SIGPIPE, SIG_IGN);
442#endif
443
Devin Nakamura43642b32011-07-11 11:22:16 -0400444 progname = basename(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800445 qemu_init_exec_dir(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000446
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100447 if (qcrypto_init(&local_error) < 0) {
448 error_reportf_err(local_error, "cannot initialize crypto: ");
449 exit(1);
450 }
451
Daniel P. Berrange064097d2016-02-10 18:41:01 +0000452 module_call_init(MODULE_INIT_QOM);
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000453 qemu_add_opts(&qemu_object_opts);
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100454 bdrv_init();
455
Devin Nakamura43642b32011-07-11 11:22:16 -0400456 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
457 switch (c) {
458 case 's':
459 flags |= BDRV_O_SNAPSHOT;
460 break;
461 case 'n':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100462 flags |= BDRV_O_NOCACHE;
463 writethrough = false;
Devin Nakamura43642b32011-07-11 11:22:16 -0400464 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100465 case 'd':
466 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
467 error_report("Invalid discard option: %s", optarg);
468 exit(1);
469 }
470 break;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100471 case 'f':
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000472 format = optarg;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100473 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400474 case 'c':
475 add_user_command(optarg);
476 break;
477 case 'r':
478 readonly = 1;
479 break;
480 case 'm':
Stefan Weilf9883882014-03-05 22:23:00 +0100481 qemuio_misalign = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400482 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400483 case 'k':
484 flags |= BDRV_O_NATIVE_AIO;
485 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200486 case 't':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100487 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
Kevin Wolf592fa072012-04-18 12:07:39 +0200488 error_report("Invalid cache option: %s", optarg);
489 exit(1);
490 }
491 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000492 case 'T':
Paolo Bonzini41fc57e2016-01-07 16:55:24 +0300493 if (!trace_init_backends()) {
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000494 exit(1); /* error message will have been printed */
495 }
496 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400497 case 'V':
Kevin Wolf02da3862013-06-05 14:19:40 +0200498 printf("%s version %s\n", progname, QEMU_VERSION);
Devin Nakamura43642b32011-07-11 11:22:16 -0400499 exit(0);
500 case 'h':
501 usage(progname);
502 exit(0);
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000503 case OPTION_OBJECT: {
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000504 QemuOpts *qopts;
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000505 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
506 optarg, true);
507 if (!qopts) {
508 exit(1);
509 }
510 } break;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000511 case OPTION_IMAGE_OPTS:
512 imageOpts = true;
513 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400514 default:
515 usage(progname);
516 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200517 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400518 }
aliguorie3aff4f2009-04-05 19:14:04 +0000519
Devin Nakamura43642b32011-07-11 11:22:16 -0400520 if ((argc - optind) > 1) {
521 usage(progname);
522 exit(1);
523 }
aliguorie3aff4f2009-04-05 19:14:04 +0000524
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000525 if (format && imageOpts) {
526 error_report("--image-opts and -f are mutually exclusive");
527 exit(1);
528 }
529
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300530 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100531 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300532 exit(1);
533 }
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800534
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000535 if (qemu_opts_foreach(&qemu_object_opts,
536 user_creatable_add_opts_foreach,
537 NULL, &local_error)) {
538 error_report_err(local_error);
539 exit(1);
540 }
541
Devin Nakamura43642b32011-07-11 11:22:16 -0400542 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200543 qemuio_add_command(&quit_cmd);
544 qemuio_add_command(&open_cmd);
545 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400546
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100547 if (isatty(STDIN_FILENO)) {
548 readline_state = readline_init(readline_printf_func,
549 readline_flush_func,
550 NULL,
551 readline_completion_func);
552 qemu_set_tty_echo(STDIN_FILENO, false);
553 atexit(reenable_tty_echo);
554 }
555
Devin Nakamura43642b32011-07-11 11:22:16 -0400556 /* open the device */
557 if (!readonly) {
558 flags |= BDRV_O_RDWR;
559 }
560
561 if ((argc - optind) == 1) {
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000562 if (imageOpts) {
563 QemuOpts *qopts = NULL;
564 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
565 if (!qopts) {
566 exit(1);
567 }
568 opts = qemu_opts_to_qdict(qopts, NULL);
Kevin Wolfe151fc12016-03-15 12:52:30 +0100569 openfile(NULL, flags, writethrough, opts);
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000570 } else {
571 if (format) {
572 opts = qdict_new();
573 qdict_put(opts, "driver", qstring_from_str(format));
574 }
Kevin Wolfe151fc12016-03-15 12:52:30 +0100575 openfile(argv[optind], flags, writethrough, opts);
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000576 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400577 }
578 command_loop();
579
580 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000581 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400582 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000583 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400584
Markus Armbruster26f54e92014-10-07 13:59:04 +0200585 blk_unref(qemuio_blk);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100586 g_free(readline_state);
Devin Nakamura43642b32011-07-11 11:22:16 -0400587 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000588}