blob: 6cb1e00385eac5a684fbc8efc82b4034c1275ee3 [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 */
Markus Armbruster452fcdb2018-02-01 12:18:39 +010010
Peter Maydell80c71a22016-01-18 18:01:42 +000011#include "qemu/osdep.h"
aliguorie3aff4f2009-04-05 19:14:04 +000012#include <getopt.h>
Stefan Weilc32d7662009-08-31 22:16:16 +020013#include <libgen.h>
Daniel P. Berrange0e448a02018-02-12 18:48:49 +000014#ifndef _WIN32
15#include <termios.h>
16#endif
aliguorie3aff4f2009-04-05 19:14:04 +000017
Marc-André Lureau49f95222022-04-20 17:25:49 +040018#include "qemu/help-texts.h"
Marc-André Lureau06680b12022-05-25 16:41:26 +020019#include "qemu/cutils.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010020#include "qapi/error.h"
Kevin Wolf3d219942013-06-05 14:19:39 +020021#include "qemu-io.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010022#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010023#include "qemu/main-loop.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020024#include "qemu/module.h"
Max Reitzb543c5c2013-10-11 14:02:10 +020025#include "qemu/option.h"
26#include "qemu/config-file.h"
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010027#include "qemu/readline.h"
Denis V. Luneve9a80852016-06-17 17:44:11 +030028#include "qemu/log.h"
Daniel P. Berrangé98c5d2e2020-08-25 11:38:48 +010029#include "qemu/sockets.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010030#include "qapi/qmp/qstring.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010031#include "qapi/qmp/qdict.h"
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +000032#include "qom/object_interfaces.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020033#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010034#include "block/block_int.h"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +000035#include "trace/control.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010036#include "crypto/init.h"
Eric Blake4face322017-08-03 11:33:51 -050037#include "qemu-version.h"
aliguorie3aff4f2009-04-05 19:14:04 +000038
Devin Nakamura43642b32011-07-11 11:22:16 -040039#define CMD_NOFILE_OK 0x01
aliguorie3aff4f2009-04-05 19:14:04 +000040
Markus Armbruster26f54e92014-10-07 13:59:04 +020041static BlockBackend *qemuio_blk;
Max Reitzb444d0e2018-05-09 21:42:58 +020042static bool quit_qemu_io;
Kevin Wolf191c2892010-09-16 13:18:08 +020043
Kevin Wolfd1174f12013-06-05 14:19:37 +020044/* qemu-io commands passed using -c */
45static int ncmdline;
46static char **cmdline;
Daniel P. Berrange499afa22016-02-17 10:10:18 +000047static bool imageOpts;
Kevin Wolfd1174f12013-06-05 14:19:37 +020048
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010049static ReadLineState *readline_state;
50
Daniel P. Berrange0e448a02018-02-12 18:48:49 +000051static int ttyEOF;
52
53static int get_eof_char(void)
54{
55#ifdef _WIN32
56 return 0x4; /* Ctrl-D */
57#else
58 struct termios tty;
59 if (tcgetattr(STDIN_FILENO, &tty) != 0) {
60 if (errno == ENOTTY) {
61 return 0x0; /* just expect read() == 0 */
62 } else {
63 return 0x4; /* Ctrl-D */
64 }
65 }
66
67 return tty.c_cc[VEOF];
68#endif
69}
70
Max Reitzb32d7a32018-05-09 21:42:59 +020071static int close_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +000072{
Markus Armbruster26f54e92014-10-07 13:59:04 +020073 blk_unref(qemuio_blk);
Markus Armbruster26f54e92014-10-07 13:59:04 +020074 qemuio_blk = NULL;
Max Reitzb32d7a32018-05-09 21:42:59 +020075 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000076}
77
78static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -040079 .name = "close",
80 .altname = "c",
81 .cfunc = close_f,
82 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +000083};
84
Fam Zheng459571f2017-05-03 00:35:41 +080085static int openfile(char *name, int flags, bool writethrough, bool force_share,
86 QDict *opts)
aliguorie3aff4f2009-04-05 19:14:04 +000087{
Max Reitz34b5d2c2013-09-05 14:45:29 +020088 Error *local_err = NULL;
89
Max Reitz1b58b432015-02-05 13:58:20 -050090 if (qemuio_blk) {
Markus Armbrusterb9884682015-12-18 16:35:18 +010091 error_report("file open already, try 'help close'");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +020092 qobject_unref(opts);
Devin Nakamura43642b32011-07-11 11:22:16 -040093 return 1;
94 }
aliguorie3aff4f2009-04-05 19:14:04 +000095
Fam Zheng459571f2017-05-03 00:35:41 +080096 if (force_share) {
97 if (!opts) {
98 opts = qdict_new();
99 }
100 if (qdict_haskey(opts, BDRV_OPT_FORCE_SHARE)
Max Reitz2a01c012018-05-02 22:20:49 +0200101 && strcmp(qdict_get_str(opts, BDRV_OPT_FORCE_SHARE), "on")) {
Fam Zheng459571f2017-05-03 00:35:41 +0800102 error_report("-U conflicts with image options");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200103 qobject_unref(opts);
Fam Zheng459571f2017-05-03 00:35:41 +0800104 return 1;
105 }
Max Reitz2a01c012018-05-02 22:20:49 +0200106 qdict_put_str(opts, BDRV_OPT_FORCE_SHARE, "on");
Fam Zheng459571f2017-05-03 00:35:41 +0800107 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100108 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
Max Reitz1b58b432015-02-05 13:58:20 -0500109 if (!qemuio_blk) {
Markus Armbrusterb9884682015-12-18 16:35:18 +0100110 error_reportf_err(local_err, "can't open%s%s: ",
111 name ? " device " : "", name ?: "");
Markus Armbrusterdbb651c2014-09-08 18:50:58 +0200112 return 1;
Devin Nakamura43642b32011-07-11 11:22:16 -0400113 }
Christoph Hellwig1db69472009-07-15 23:11:21 +0200114
Kevin Wolfe151fc12016-03-15 12:52:30 +0100115 blk_set_enable_write_cache(qemuio_blk, !writethrough);
Daniel P. Berrange8caf0212015-05-12 17:09:21 +0100116
Devin Nakamura43642b32011-07-11 11:22:16 -0400117 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000118}
119
Devin Nakamura43642b32011-07-11 11:22:16 -0400120static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000121{
Devin Nakamura43642b32011-07-11 11:22:16 -0400122 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000123"\n"
124" opens a new file in the requested mode\n"
125"\n"
126" Example:\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600127" 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000128"\n"
129" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000130" -r, -- open file read-only\n"
131" -s, -- use snapshot file\n"
Eric Blake0f404442017-10-05 14:02:43 -0500132" -C, -- use copy-on-read\n"
Eric Blakeb8d970f2016-05-07 21:16:41 -0600133" -n, -- disable host cache, short for -t none\n"
Fam Zheng459571f2017-05-03 00:35:41 +0800134" -U, -- force shared permissions\n"
Aarushi Mehta1c5a2ae2020-01-20 14:18:54 +0000135" -k, -- use kernel AIO implementation (Linux only, prefer use of -i)\n"
136" -i, -- use AIO mode (threads, native or io_uring)\n"
Eric Blakeb8d970f2016-05-07 21:16:41 -0600137" -t, -- use the given cache mode for the image\n"
138" -d, -- use the given discard mode for the image\n"
Max Reitzb543c5c2013-10-11 14:02:10 +0200139" -o, -- options to be given to the block driver"
aliguorie3aff4f2009-04-05 19:14:04 +0000140"\n");
141}
142
Max Reitzb32d7a32018-05-09 21:42:59 +0200143static int open_f(BlockBackend *blk, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000144
145static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400146 .name = "open",
147 .altname = "o",
148 .cfunc = open_f,
149 .argmin = 1,
150 .argmax = -1,
151 .flags = CMD_NOFILE_OK,
Eric Blake0f404442017-10-05 14:02:43 -0500152 .args = "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
Devin Nakamura43642b32011-07-11 11:22:16 -0400153 .oneline = "open the file specified by path",
154 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000155};
aliguorie3aff4f2009-04-05 19:14:04 +0000156
Max Reitzb543c5c2013-10-11 14:02:10 +0200157static QemuOptsList empty_opts = {
158 .name = "drive",
Markus Armbruster443422f2014-05-28 11:16:58 +0200159 .merge_lists = true,
Max Reitzb543c5c2013-10-11 14:02:10 +0200160 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
161 .desc = {
162 /* no elements => accept any params */
163 { /* end of list */ }
164 },
165};
166
Max Reitzb32d7a32018-05-09 21:42:59 +0200167static int open_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000168{
Eric Blakeb8d970f2016-05-07 21:16:41 -0600169 int flags = BDRV_O_UNMAP;
Devin Nakamura43642b32011-07-11 11:22:16 -0400170 int readonly = 0;
Kevin Wolfe151fc12016-03-15 12:52:30 +0100171 bool writethrough = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400172 int c;
Max Reitzb32d7a32018-05-09 21:42:59 +0200173 int ret;
Max Reitzb543c5c2013-10-11 14:02:10 +0200174 QemuOpts *qopts;
Markus Armbruster443422f2014-05-28 11:16:58 +0200175 QDict *opts;
Fam Zheng459571f2017-05-03 00:35:41 +0800176 bool force_share = false;
aliguorie3aff4f2009-04-05 19:14:04 +0000177
Aarushi Mehta1c5a2ae2020-01-20 14:18:54 +0000178 while ((c = getopt(argc, argv, "snCro:ki:t:d:U")) != -1) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400179 switch (c) {
180 case 's':
181 flags |= BDRV_O_SNAPSHOT;
182 break;
183 case 'n':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100184 flags |= BDRV_O_NOCACHE;
185 writethrough = false;
Devin Nakamura43642b32011-07-11 11:22:16 -0400186 break;
Eric Blake0f404442017-10-05 14:02:43 -0500187 case 'C':
188 flags |= BDRV_O_COPY_ON_READ;
189 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400190 case 'r':
191 readonly = 1;
192 break;
Eric Blakeb8d970f2016-05-07 21:16:41 -0600193 case 'k':
194 flags |= BDRV_O_NATIVE_AIO;
195 break;
196 case 't':
197 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
198 error_report("Invalid cache option: %s", optarg);
199 qemu_opts_reset(&empty_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +0200200 return -EINVAL;
Eric Blakeb8d970f2016-05-07 21:16:41 -0600201 }
202 break;
203 case 'd':
204 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
205 error_report("Invalid discard option: %s", optarg);
206 qemu_opts_reset(&empty_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +0200207 return -EINVAL;
Eric Blakeb8d970f2016-05-07 21:16:41 -0600208 }
209 break;
Aarushi Mehta1c5a2ae2020-01-20 14:18:54 +0000210 case 'i':
211 if (bdrv_parse_aio(optarg, &flags) < 0) {
212 error_report("Invalid aio option: %s", optarg);
213 qemu_opts_reset(&empty_opts);
214 return -EINVAL;
215 }
216 break;
Max Reitzb543c5c2013-10-11 14:02:10 +0200217 case 'o':
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000218 if (imageOpts) {
219 printf("--image-opts and 'open -o' are mutually exclusive\n");
Eric Blakeb8d970f2016-05-07 21:16:41 -0600220 qemu_opts_reset(&empty_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +0200221 return -EINVAL;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000222 }
Markus Armbruster70b94332015-02-13 12:50:26 +0100223 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
Markus Armbruster443422f2014-05-28 11:16:58 +0200224 qemu_opts_reset(&empty_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +0200225 return -EINVAL;
Max Reitzb543c5c2013-10-11 14:02:10 +0200226 }
Max Reitzb543c5c2013-10-11 14:02:10 +0200227 break;
Fam Zheng459571f2017-05-03 00:35:41 +0800228 case 'U':
229 force_share = true;
230 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400231 default:
Markus Armbruster443422f2014-05-28 11:16:58 +0200232 qemu_opts_reset(&empty_opts);
Max Reitzb444d0e2018-05-09 21:42:58 +0200233 qemuio_command_usage(&open_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200234 return -EINVAL;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200235 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400236 }
aliguorie3aff4f2009-04-05 19:14:04 +0000237
Devin Nakamura43642b32011-07-11 11:22:16 -0400238 if (!readonly) {
239 flags |= BDRV_O_RDWR;
240 }
aliguorie3aff4f2009-04-05 19:14:04 +0000241
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000242 if (imageOpts && (optind == argc - 1)) {
243 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
244 qemu_opts_reset(&empty_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +0200245 return -EINVAL;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000246 }
247 optind++;
248 }
249
Markus Armbruster443422f2014-05-28 11:16:58 +0200250 qopts = qemu_opts_find(&empty_opts, NULL);
251 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
252 qemu_opts_reset(&empty_opts);
253
Max Reitzfd0fee32013-12-20 19:28:20 +0100254 if (optind == argc - 1) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200255 ret = openfile(argv[optind], flags, writethrough, force_share, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100256 } else if (optind == argc) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200257 ret = openfile(NULL, flags, writethrough, force_share, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100258 } else {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200259 qobject_unref(opts);
Eric Blake64ebf552017-06-05 15:38:42 -0500260 qemuio_command_usage(&open_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200261 return -EINVAL;
Devin Nakamura43642b32011-07-11 11:22:16 -0400262 }
Max Reitzb32d7a32018-05-09 21:42:59 +0200263
264 if (ret) {
265 return -EINVAL;
266 }
267
268 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000269}
270
Max Reitzb32d7a32018-05-09 21:42:59 +0200271static int quit_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfe681be72013-06-05 14:19:34 +0200272{
Max Reitzb444d0e2018-05-09 21:42:58 +0200273 quit_qemu_io = true;
Max Reitzb32d7a32018-05-09 21:42:59 +0200274 return 0;
Kevin Wolfe681be72013-06-05 14:19:34 +0200275}
276
277static const cmdinfo_t quit_cmd = {
278 .name = "quit",
279 .altname = "q",
280 .cfunc = quit_f,
281 .argmin = -1,
282 .argmax = -1,
283 .flags = CMD_FLAG_GLOBAL,
284 .oneline = "exit the program",
285};
286
aliguorie3aff4f2009-04-05 19:14:04 +0000287static void usage(const char *name)
288{
Devin Nakamura43642b32011-07-11 11:22:16 -0400289 printf(
Eric Blakee4e12bb2016-05-07 21:16:40 -0600290"Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200291"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000292"\n"
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000293" --object OBJECTDEF define an object such as 'secret' for\n"
294" passwords and/or encryption keys\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600295" --image-opts treat file as option string\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400296" -c, --cmd STRING execute command with its arguments\n"
297" from the given string\n"
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100298" -f, --format FMT specifies the block driver to use\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000299" -r, --read-only export read-only\n"
300" -s, --snapshot use snapshot file\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600301" -n, --nocache disable host cache, short for -t none\n"
Eric Blake0f404442017-10-05 14:02:43 -0500302" -C, --copy-on-read enable copy-on-read\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000303" -m, --misalign misalign allocations for O_DIRECT\n"
Aarushi Mehta1c5a2ae2020-01-20 14:18:54 +0000304" -k, --native-aio use kernel AIO implementation\n"
305" (Linux only, prefer use of -i)\n"
306" -i, --aio=MODE use AIO mode (threads, native or io_uring)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200307" -t, --cache=MODE use the given cache mode for the image\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600308" -d, --discard=MODE use the given discard mode for the image\n"
Denis V. Luneve9a80852016-06-17 17:44:11 +0300309" -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
310" specify tracing options\n"
311" see qemu-img(1) man page for full description\n"
Fam Zheng459571f2017-05-03 00:35:41 +0800312" -U, --force-share force shared permissions\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000313" -h, --help display this help and exit\n"
314" -V, --version output version information and exit\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400315"\n"
Eric Blakef5048cb2017-08-03 11:33:53 -0500316"See '%s -c help' for information on available commands.\n"
317"\n"
318QEMU_HELP_BOTTOM "\n",
Maria Kustovad208cc32014-03-18 09:59:19 +0400319 name, name);
aliguorie3aff4f2009-04-05 19:14:04 +0000320}
321
Kevin Wolfd1174f12013-06-05 14:19:37 +0200322static char *get_prompt(void)
323{
324 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
325
326 if (!prompt[0]) {
Marc-André Lureau336d3542022-02-21 14:11:47 +0400327 snprintf(prompt, sizeof(prompt), "%s> ", g_get_prgname());
Kevin Wolfd1174f12013-06-05 14:19:37 +0200328 }
329
330 return prompt;
331}
332
Marc-André Lureau9edc6312022-02-20 20:39:25 +0400333static void G_GNUC_PRINTF(2, 3) readline_printf_func(void *opaque,
Stefan Weild5d15072014-01-25 18:18:23 +0100334 const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200335{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100336 va_list ap;
337 va_start(ap, fmt);
338 vprintf(fmt, ap);
339 va_end(ap);
340}
341
342static void readline_flush_func(void *opaque)
343{
344 fflush(stdout);
345}
346
347static void readline_func(void *opaque, const char *str, void *readline_opaque)
348{
349 char **line = readline_opaque;
350 *line = g_strdup(str);
351}
352
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100353static void completion_match(const char *cmd, void *opaque)
354{
355 readline_add_completion(readline_state, cmd);
356}
357
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100358static void readline_completion_func(void *opaque, const char *str)
359{
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100360 readline_set_completion_index(readline_state, strlen(str));
361 qemuio_complete_command(str, completion_match, NULL);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100362}
363
364static char *fetchline_readline(void)
365{
366 char *line = NULL;
367
368 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
369 while (!line) {
370 int ch = getchar();
Daniel P. Berrange0e448a02018-02-12 18:48:49 +0000371 if (ttyEOF != 0x0 && ch == ttyEOF) {
372 printf("\n");
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100373 break;
374 }
375 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200376 }
377 return line;
378}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200379
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100380#define MAXREADLINESZ 1024
381static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200382{
383 char *p, *line = g_malloc(MAXREADLINESZ);
384
385 if (!fgets(line, MAXREADLINESZ, stdin)) {
386 g_free(line);
387 return NULL;
388 }
389
390 p = line + strlen(line);
391 if (p != line && p[-1] == '\n') {
392 p[-1] = '\0';
393 }
394
395 return line;
396}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100397
398static char *fetchline(void)
399{
400 if (readline_state) {
401 return fetchline_readline();
402 } else {
403 return fetchline_fgets();
404 }
405}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200406
407static void prep_fetchline(void *opaque)
408{
409 int *fetchable = opaque;
410
411 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
412 *fetchable= 1;
413}
414
Vladimir Sementsov-Ogievskiy78632a3d2021-04-23 16:42:33 +0300415static int do_qemuio_command(const char *cmd)
416{
Stefan Hajnoczib49f4752023-12-05 13:20:03 -0500417 return qemuio_command(qemuio_blk, cmd);
Vladimir Sementsov-Ogievskiy78632a3d2021-04-23 16:42:33 +0300418}
419
Max Reitz6b3aa842018-05-09 21:43:00 +0200420static int command_loop(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200421{
Max Reitzb444d0e2018-05-09 21:42:58 +0200422 int i, fetchable = 0, prompted = 0;
Max Reitz6b3aa842018-05-09 21:43:00 +0200423 int ret, last_error = 0;
Kevin Wolfd1174f12013-06-05 14:19:37 +0200424 char *input;
425
Max Reitzb444d0e2018-05-09 21:42:58 +0200426 for (i = 0; !quit_qemu_io && i < ncmdline; i++) {
Vladimir Sementsov-Ogievskiy78632a3d2021-04-23 16:42:33 +0300427 ret = do_qemuio_command(cmdline[i]);
Max Reitz6b3aa842018-05-09 21:43:00 +0200428 if (ret < 0) {
429 last_error = ret;
430 }
Kevin Wolfd1174f12013-06-05 14:19:37 +0200431 }
432 if (cmdline) {
433 g_free(cmdline);
Max Reitz6b3aa842018-05-09 21:43:00 +0200434 return last_error;
Kevin Wolfd1174f12013-06-05 14:19:37 +0200435 }
436
Max Reitzb444d0e2018-05-09 21:42:58 +0200437 while (!quit_qemu_io) {
Kevin Wolfd1174f12013-06-05 14:19:37 +0200438 if (!prompted) {
439 printf("%s", get_prompt());
440 fflush(stdout);
441 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
442 prompted = 1;
443 }
444
445 main_loop_wait(false);
446
447 if (!fetchable) {
448 continue;
449 }
450
451 input = fetchline();
452 if (input == NULL) {
453 break;
454 }
Vladimir Sementsov-Ogievskiy78632a3d2021-04-23 16:42:33 +0300455 ret = do_qemuio_command(input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200456 g_free(input);
457
Max Reitz6b3aa842018-05-09 21:43:00 +0200458 if (ret < 0) {
459 last_error = ret;
460 }
461
Kevin Wolfd1174f12013-06-05 14:19:37 +0200462 prompted = 0;
463 fetchable = 0;
464 }
465 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
Max Reitz6b3aa842018-05-09 21:43:00 +0200466
467 return last_error;
Kevin Wolfd1174f12013-06-05 14:19:37 +0200468}
469
Philippe Mathieu-Daudé21c029e2023-10-04 14:00:10 +0200470static void add_user_command(char *user_cmd)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200471{
Markus Armbruster5839e532014-08-19 10:31:08 +0200472 cmdline = g_renew(char *, cmdline, ++ncmdline);
Philippe Mathieu-Daudé21c029e2023-10-04 14:00:10 +0200473 cmdline[ncmdline - 1] = user_cmd;
Kevin Wolfd1174f12013-06-05 14:19:37 +0200474}
475
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100476static void reenable_tty_echo(void)
477{
478 qemu_set_tty_echo(STDIN_FILENO, true);
479}
480
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000481enum {
482 OPTION_OBJECT = 256,
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000483 OPTION_IMAGE_OPTS = 257,
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000484};
485
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000486static QemuOptsList file_opts = {
487 .name = "file",
488 .implied_opt_name = "file",
489 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
490 .desc = {
491 /* no elements => accept any params */
492 { /* end of list */ }
493 },
494};
495
aliguorie3aff4f2009-04-05 19:14:04 +0000496int main(int argc, char **argv)
497{
Devin Nakamura43642b32011-07-11 11:22:16 -0400498 int readonly = 0;
Aarushi Mehta1c5a2ae2020-01-20 14:18:54 +0000499 const char *sopt = "hVc:d:f:rsnCmki:t:T:U";
Devin Nakamura43642b32011-07-11 11:22:16 -0400500 const struct option lopt[] = {
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000501 { "help", no_argument, NULL, 'h' },
502 { "version", no_argument, NULL, 'V' },
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000503 { "cmd", required_argument, NULL, 'c' },
504 { "format", required_argument, NULL, 'f' },
505 { "read-only", no_argument, NULL, 'r' },
506 { "snapshot", no_argument, NULL, 's' },
507 { "nocache", no_argument, NULL, 'n' },
Eric Blake0f404442017-10-05 14:02:43 -0500508 { "copy-on-read", no_argument, NULL, 'C' },
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000509 { "misalign", no_argument, NULL, 'm' },
510 { "native-aio", no_argument, NULL, 'k' },
Aarushi Mehta1c5a2ae2020-01-20 14:18:54 +0000511 { "aio", required_argument, NULL, 'i' },
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000512 { "discard", required_argument, NULL, 'd' },
513 { "cache", required_argument, NULL, 't' },
514 { "trace", required_argument, NULL, 'T' },
515 { "object", required_argument, NULL, OPTION_OBJECT },
516 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
Fam Zheng459571f2017-05-03 00:35:41 +0800517 { "force-share", no_argument, 0, 'U'},
Devin Nakamura43642b32011-07-11 11:22:16 -0400518 { NULL, 0, NULL, 0 }
519 };
520 int c;
521 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100522 int flags = BDRV_O_UNMAP;
Max Reitz6b3aa842018-05-09 21:43:00 +0200523 int ret;
Kevin Wolfe151fc12016-03-15 12:52:30 +0100524 bool writethrough = true;
Max Reitz1b58b432015-02-05 13:58:20 -0500525 QDict *opts = NULL;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000526 const char *format = NULL;
Fam Zheng459571f2017-05-03 00:35:41 +0800527 bool force_share = false;
aliguorie3aff4f2009-04-05 19:14:04 +0000528
MORITA Kazutaka526eda12013-07-23 17:30:11 +0900529#ifdef CONFIG_POSIX
530 signal(SIGPIPE, SIG_IGN);
531#endif
532
Daniel P. Berrangé98c5d2e2020-08-25 11:38:48 +0100533 socket_init();
Christophe Fergeauf5852ef2019-01-31 17:46:14 +0100534 error_init(argv[0]);
Daniel P. Berrangefe4db842016-10-04 14:35:52 +0100535 module_call_init(MODULE_INIT_TRACE);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800536 qemu_init_exec_dir(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000537
Eduardo Habkoste8f2d272016-05-12 11:10:04 -0300538 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100539
Daniel P. Berrange064097d2016-02-10 18:41:01 +0000540 module_call_init(MODULE_INIT_QOM);
Denis V. Luneve9a80852016-06-17 17:44:11 +0300541 qemu_add_opts(&qemu_trace_opts);
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100542 bdrv_init();
543
Devin Nakamura43642b32011-07-11 11:22:16 -0400544 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
545 switch (c) {
546 case 's':
547 flags |= BDRV_O_SNAPSHOT;
548 break;
549 case 'n':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100550 flags |= BDRV_O_NOCACHE;
551 writethrough = false;
Devin Nakamura43642b32011-07-11 11:22:16 -0400552 break;
Eric Blake0f404442017-10-05 14:02:43 -0500553 case 'C':
554 flags |= BDRV_O_COPY_ON_READ;
555 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100556 case 'd':
557 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
558 error_report("Invalid discard option: %s", optarg);
559 exit(1);
560 }
561 break;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100562 case 'f':
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000563 format = optarg;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100564 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400565 case 'c':
566 add_user_command(optarg);
567 break;
568 case 'r':
569 readonly = 1;
570 break;
571 case 'm':
Stefan Weilf9883882014-03-05 22:23:00 +0100572 qemuio_misalign = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400573 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400574 case 'k':
575 flags |= BDRV_O_NATIVE_AIO;
576 break;
Aarushi Mehta1c5a2ae2020-01-20 14:18:54 +0000577 case 'i':
578 if (bdrv_parse_aio(optarg, &flags) < 0) {
579 error_report("Invalid aio option: %s", optarg);
580 exit(1);
581 }
582 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200583 case 't':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100584 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
Kevin Wolf592fa072012-04-18 12:07:39 +0200585 error_report("Invalid cache option: %s", optarg);
586 exit(1);
587 }
588 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000589 case 'T':
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500590 trace_opt_parse(optarg);
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000591 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400592 case 'V':
Thomas Huth7e563bf2018-02-15 12:06:47 +0100593 printf("%s version " QEMU_FULL_VERSION "\n"
Marc-André Lureau336d3542022-02-21 14:11:47 +0400594 QEMU_COPYRIGHT "\n", g_get_prgname());
Devin Nakamura43642b32011-07-11 11:22:16 -0400595 exit(0);
596 case 'h':
Marc-André Lureau336d3542022-02-21 14:11:47 +0400597 usage(g_get_prgname());
Devin Nakamura43642b32011-07-11 11:22:16 -0400598 exit(0);
Fam Zheng459571f2017-05-03 00:35:41 +0800599 case 'U':
600 force_share = true;
601 break;
Kevin Wolfb3e79bc2021-02-17 12:56:45 +0100602 case OPTION_OBJECT:
603 user_creatable_process_cmdline(optarg);
604 break;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000605 case OPTION_IMAGE_OPTS:
606 imageOpts = true;
607 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400608 default:
Marc-André Lureau336d3542022-02-21 14:11:47 +0400609 usage(g_get_prgname());
Devin Nakamura43642b32011-07-11 11:22:16 -0400610 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200611 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400612 }
aliguorie3aff4f2009-04-05 19:14:04 +0000613
Devin Nakamura43642b32011-07-11 11:22:16 -0400614 if ((argc - optind) > 1) {
Marc-André Lureau336d3542022-02-21 14:11:47 +0400615 usage(g_get_prgname());
Devin Nakamura43642b32011-07-11 11:22:16 -0400616 exit(1);
617 }
aliguorie3aff4f2009-04-05 19:14:04 +0000618
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000619 if (format && imageOpts) {
620 error_report("--image-opts and -f are mutually exclusive");
621 exit(1);
622 }
623
Markus Armbrusterf9734d52021-07-20 14:53:53 +0200624 qemu_init_main_loop(&error_fatal);
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800625
Denis V. Luneve9a80852016-06-17 17:44:11 +0300626 if (!trace_init_backends()) {
627 exit(1);
628 }
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500629 trace_init_file();
Richard Hendersonc5955f42022-04-17 11:29:44 -0700630 qemu_set_log(LOG_TRACE, &error_fatal);
Denis V. Luneve9a80852016-06-17 17:44:11 +0300631
Devin Nakamura43642b32011-07-11 11:22:16 -0400632 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200633 qemuio_add_command(&quit_cmd);
634 qemuio_add_command(&open_cmd);
635 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400636
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100637 if (isatty(STDIN_FILENO)) {
Daniel P. Berrange0e448a02018-02-12 18:48:49 +0000638 ttyEOF = get_eof_char();
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100639 readline_state = readline_init(readline_printf_func,
640 readline_flush_func,
641 NULL,
642 readline_completion_func);
643 qemu_set_tty_echo(STDIN_FILENO, false);
644 atexit(reenable_tty_echo);
645 }
646
Devin Nakamura43642b32011-07-11 11:22:16 -0400647 /* open the device */
648 if (!readonly) {
649 flags |= BDRV_O_RDWR;
650 }
651
652 if ((argc - optind) == 1) {
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000653 if (imageOpts) {
654 QemuOpts *qopts = NULL;
655 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
656 if (!qopts) {
657 exit(1);
658 }
659 opts = qemu_opts_to_qdict(qopts, NULL);
Fam Zheng459571f2017-05-03 00:35:41 +0800660 if (openfile(NULL, flags, writethrough, force_share, opts)) {
Nir Sofferb7aa1312017-02-01 02:31:18 +0200661 exit(1);
662 }
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000663 } else {
664 if (format) {
665 opts = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -0500666 qdict_put_str(opts, "driver", format);
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000667 }
Fam Zheng459571f2017-05-03 00:35:41 +0800668 if (openfile(argv[optind], flags, writethrough,
669 force_share, opts)) {
Nir Sofferb7aa1312017-02-01 02:31:18 +0200670 exit(1);
671 }
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000672 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400673 }
Max Reitz6b3aa842018-05-09 21:43:00 +0200674 ret = command_loop();
Devin Nakamura43642b32011-07-11 11:22:16 -0400675
676 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000677 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400678 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000679 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400680
Markus Armbruster26f54e92014-10-07 13:59:04 +0200681 blk_unref(qemuio_blk);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100682 g_free(readline_state);
Max Reitz6b3aa842018-05-09 21:43:00 +0200683
684 if (ret < 0) {
685 return 1;
686 } else {
687 return 0;
688 }
aliguorie3aff4f2009-04-05 19:14:04 +0000689}