blob: 26ffbf15af0a755dddc99e27c876d442fc46632f [file] [log] [blame]
thscd831bd2008-07-03 10:23:51 +00001/*
bellard7a5ca862008-05-27 21:13:40 +00002 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
3 *
4 * Network Block Device
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; under version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Blue Swirl8167ee82009-07-16 20:47:01 +000016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
bellard7a5ca862008-05-27 21:13:40 +000017 */
18
Peter Maydelld38ea872016-01-29 17:50:05 +000019#include "qemu/osdep.h"
Eric Blakec2a3d7d2017-07-21 08:50:47 -050020#include <getopt.h>
21#include <libgen.h>
22#include <pthread.h>
23
Markus Armbrustera8d25322019-05-23 16:35:08 +020024#include "qemu-common.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010025#include "qapi/error.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020026#include "qemu/cutils.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020027#include "sysemu/block-backend.h"
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +010028#include "sysemu/runstate.h" /* for qemu_system_killed() prototype */
Peter Lievenb3838a42014-08-13 19:20:18 +020029#include "block/block_int.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010030#include "block/nbd.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010031#include "qemu/main-loop.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020032#include "qemu/module.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010033#include "qemu/option.h"
Paolo Bonzini537b41f2014-02-17 14:43:51 +010034#include "qemu/error-report.h"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +000035#include "qemu/config-file.h"
Paolo Bonzini58369e22016-03-15 17:22:36 +010036#include "qemu/bswap.h"
Denis V. Lunev39ca4632016-06-17 17:44:12 +030037#include "qemu/log.h"
Paolo Bonzini53fabd42017-03-16 16:29:45 +010038#include "qemu/systemd.h"
Wenchao Xia8c116b02013-12-04 17:10:55 +080039#include "block/snapshot.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010040#include "qapi/qmp/qdict.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010041#include "qapi/qmp/qstring.h"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +000042#include "qom/object_interfaces.h"
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +000043#include "io/channel-socket.h"
Daniel P. Berrangee4849c12017-12-18 10:16:43 +000044#include "io/net-listener.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010045#include "crypto/init.h"
Philippe Mathieu-Daudé0279cd92021-06-28 18:09:10 +020046#include "crypto/tlscreds.h"
Denis V. Lunev39ca4632016-06-17 17:44:12 +030047#include "trace/control.h"
Eric Blakebe377132017-07-21 08:50:46 -050048#include "qemu-version.h"
bellard7a5ca862008-05-27 21:13:40 +000049
Eric Blake3c1fa352018-12-15 07:53:08 -060050#ifdef __linux__
51#define HAVE_NBD_DEVICE 1
52#else
53#define HAVE_NBD_DEVICE 0
54#endif
55
Peter Lieven713cc672014-08-13 19:20:19 +020056#define SOCKET_PATH "/var/lock/qemu-nbd-%s"
Daniel P. Berrangefa8b7ce2016-02-17 10:10:21 +000057#define QEMU_NBD_OPT_CACHE 256
58#define QEMU_NBD_OPT_AIO 257
59#define QEMU_NBD_OPT_DISCARD 258
60#define QEMU_NBD_OPT_DETECT_ZEROES 259
61#define QEMU_NBD_OPT_OBJECT 260
62#define QEMU_NBD_OPT_TLSCREDS 261
63#define QEMU_NBD_OPT_IMAGE_OPTS 262
Max Reitzffb31e12016-09-28 22:46:42 +020064#define QEMU_NBD_OPT_FORK 263
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +000065#define QEMU_NBD_OPT_TLSAUTHZ 264
Max Reitz637bc5a2019-05-08 23:18:16 +020066#define QEMU_NBD_OPT_PID_FILE 265
bellard7a5ca862008-05-27 21:13:40 +000067
Eric Blakebd31c212016-05-06 10:26:42 -060068#define MBR_SIZE 512
69
blueswir1b1d8e522008-10-26 13:43:07 +000070static int verbose;
Paolo Bonzinia517e882011-11-04 15:51:21 +010071static char *srcpath;
Markus Armbrusterbd269eb2017-04-26 09:36:41 +020072static SocketAddress *saddr;
Paolo Bonzini7860a382012-09-18 13:31:56 +020073static int persistent = 0;
Kevin Wolfd794f7f2020-09-24 17:26:56 +020074static enum { RUNNING, TERMINATE, TERMINATED } state;
Paolo Bonzinia61c6782011-09-12 17:28:11 +020075static int shared = 1;
76static int nb_fds;
Daniel P. Berrangee4849c12017-12-18 10:16:43 +000077static QIONetListener *server;
Daniel P. Berrange145614a2016-02-10 18:41:13 +000078static QCryptoTLSCreds *tlscreds;
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +000079static const char *tlsauthz;
bellard7a5ca862008-05-27 21:13:40 +000080
81static void usage(const char *name)
82{
Paolo Bonzinib033cd82012-07-18 14:50:52 +020083 (printf) (
bellard7a5ca862008-05-27 21:13:40 +000084"Usage: %s [OPTIONS] FILE\n"
Eric Blake68b96f12019-01-17 13:36:56 -060085" or: %s -L [OPTIONS]\n"
86"QEMU Disk Network Block Device Utility\n"
bellard7a5ca862008-05-27 21:13:40 +000087"\n"
Peter Lieven713cc672014-08-13 19:20:19 +020088" -h, --help display this help and exit\n"
89" -V, --version output version information and exit\n"
bellard7a5ca862008-05-27 21:13:40 +000090"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020091"Connection properties:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020092" -p, --port=PORT port to listen on (default `%d')\n"
93" -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
94" -k, --socket=PATH path to the unix socket\n"
95" (default '"SOCKET_PATH"')\n"
96" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
97" -t, --persistent don't exit on the last connection\n"
98" -v, --verbose display extra debugging information\n"
Vladimir Sementsov-Ogievskiyf5cd0bb2018-10-03 20:02:27 +030099" -x, --export-name=NAME expose export by name (default is empty string)\n"
100" -D, --description=TEXT export a human-readable description\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200101"\n"
102"Exposing part of the image:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200103" -o, --offset=OFFSET offset into the image\n"
Eric Blakedbc7b012020-10-27 00:05:55 -0500104" -A, --allocation-depth expose the allocation depth\n"
Eric Blake636192c2019-01-11 13:47:20 -0600105" -B, --bitmap=NAME expose a persistent dirty bitmap\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200106"\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000107"General purpose options:\n"
Eric Blake68b96f12019-01-17 13:36:56 -0600108" -L, --list list exports available from another NBD server\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000109" --object type,id=ID,... define an object such as 'secret' for providing\n"
110" passwords and/or encryption keys\n"
Eric Blakef7812df2018-10-03 13:04:26 -0500111" --tls-creds=ID use id of an earlier --object to provide TLS\n"
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000112" --tls-authz=ID use id of an earlier --object to provide\n"
113" authorization\n"
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300114" -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
115" specify tracing options\n"
Max Reitzffb31e12016-09-28 22:46:42 +0200116" --fork fork off the server process and exit the parent\n"
117" once the server is running\n"
Max Reitz637bc5a2019-05-08 23:18:16 +0200118" --pid-file=PATH store the server's process ID in the given file\n"
Eric Blake3c1fa352018-12-15 07:53:08 -0600119#if HAVE_NBD_DEVICE
120"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200121"Kernel NBD client support:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200122" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
123" -d, --disconnect disconnect the specified device\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200124#endif
125"\n"
126"Block device options:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200127" -f, --format=FORMAT set image format (raw, qcow2, ...)\n"
128" -r, --read-only export read-only\n"
129" -s, --snapshot use FILE as an external snapshot, create a temporary\n"
130" file with backing_file=FILE, redirect the write to\n"
131" the temporary one\n"
Wenchao Xia8c116b02013-12-04 17:10:55 +0800132" -l, --load-snapshot=SNAPSHOT_PARAM\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200133" load an internal snapshot inside FILE and export it\n"
134" as an read-only device, SNAPSHOT_PARAM format is\n"
135" 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
136" '[ID_OR_NAME]'\n"
137" -n, --nocache disable host cache\n"
138" --cache=MODE set cache mode (none, writeback, ...)\n"
Aarushi Mehta76802742020-01-20 14:18:56 +0000139" --aio=MODE set AIO mode (native, io_uring or threads)\n"
Peter Lievenb3838a42014-08-13 19:20:18 +0200140" --discard=MODE set discard mode (ignore, unmap)\n"
Andrey Korolyov6883de62015-07-31 22:12:54 +0300141" --detect-zeroes=MODE set detect-zeroes mode (off, on, unmap)\n"
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000142" --image-opts treat FILE as a full set of image options\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200143"\n"
Eric Blakef5048cb2017-08-03 11:33:53 -0500144QEMU_HELP_BOTTOM "\n"
Eric Blake68b96f12019-01-17 13:36:56 -0600145 , name, name, NBD_DEFAULT_PORT, "DEVICE");
bellard7a5ca862008-05-27 21:13:40 +0000146}
147
148static void version(const char *name)
149{
150 printf(
Thomas Huth7e563bf2018-02-15 12:06:47 +0100151"%s " QEMU_FULL_VERSION "\n"
bellard7a5ca862008-05-27 21:13:40 +0000152"Written by Anthony Liguori.\n"
153"\n"
Eric Blakebe377132017-07-21 08:50:46 -0500154QEMU_COPYRIGHT "\n"
bellard7a5ca862008-05-27 21:13:40 +0000155"This is free software; see the source for copying conditions. There is NO\n"
156"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
ths315bc7a2008-07-18 18:06:23 +0000157 , name);
bellard7a5ca862008-05-27 21:13:40 +0000158}
159
Eric Blake029a88c2020-09-30 07:11:01 -0500160#ifdef CONFIG_POSIX
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +0100161/*
162 * The client thread uses SIGTERM to interrupt the server. A signal
163 * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
164 */
165void qemu_system_killed(int signum, pid_t pid)
Paolo Bonzinibb345112011-11-04 15:51:19 +0100166{
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100167 qatomic_cmpxchg(&state, RUNNING, TERMINATE);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200168 qemu_notify_event();
Paolo Bonzinibb345112011-11-04 15:51:19 +0100169}
Eric Blake029a88c2020-09-30 07:11:01 -0500170#endif /* CONFIG_POSIX */
Paolo Bonzini537b41f2014-02-17 14:43:51 +0100171
Eric Blake68b96f12019-01-17 13:36:56 -0600172static int qemu_nbd_client_list(SocketAddress *saddr, QCryptoTLSCreds *tls,
173 const char *hostname)
174{
175 int ret = EXIT_FAILURE;
176 int rc;
177 Error *err = NULL;
178 QIOChannelSocket *sioc;
179 NBDExportInfo *list;
180 int i, j;
181
182 sioc = qio_channel_socket_new();
183 if (qio_channel_socket_connect_sync(sioc, saddr, &err) < 0) {
184 error_report_err(err);
Alex Chen992809b2020-11-30 12:36:51 +0000185 goto out;
Eric Blake68b96f12019-01-17 13:36:56 -0600186 }
187 rc = nbd_receive_export_list(QIO_CHANNEL(sioc), tls, hostname, &list,
188 &err);
189 if (rc < 0) {
190 if (err) {
191 error_report_err(err);
192 }
193 goto out;
194 }
195 printf("exports available: %d\n", rc);
196 for (i = 0; i < rc; i++) {
197 printf(" export: '%s'\n", list[i].name);
198 if (list[i].description && *list[i].description) {
199 printf(" description: %s\n", list[i].description);
200 }
201 if (list[i].flags & NBD_FLAG_HAS_FLAGS) {
Max Reitzc4e2aff2019-04-05 21:16:35 +0200202 static const char *const flag_names[] = {
203 [NBD_FLAG_READ_ONLY_BIT] = "readonly",
204 [NBD_FLAG_SEND_FLUSH_BIT] = "flush",
205 [NBD_FLAG_SEND_FUA_BIT] = "fua",
206 [NBD_FLAG_ROTATIONAL_BIT] = "rotational",
207 [NBD_FLAG_SEND_TRIM_BIT] = "trim",
208 [NBD_FLAG_SEND_WRITE_ZEROES_BIT] = "zeroes",
209 [NBD_FLAG_SEND_DF_BIT] = "df",
210 [NBD_FLAG_CAN_MULTI_CONN_BIT] = "multi",
211 [NBD_FLAG_SEND_RESIZE_BIT] = "resize",
212 [NBD_FLAG_SEND_CACHE_BIT] = "cache",
Eric Blake0a479542019-08-23 09:37:23 -0500213 [NBD_FLAG_SEND_FAST_ZERO_BIT] = "fast-zero",
Max Reitzc4e2aff2019-04-05 21:16:35 +0200214 };
215
Eric Blake68b96f12019-01-17 13:36:56 -0600216 printf(" size: %" PRIu64 "\n", list[i].size);
217 printf(" flags: 0x%x (", list[i].flags);
Max Reitzc4e2aff2019-04-05 21:16:35 +0200218 for (size_t bit = 0; bit < ARRAY_SIZE(flag_names); bit++) {
219 if (flag_names[bit] && (list[i].flags & (1 << bit))) {
220 printf(" %s", flag_names[bit]);
221 }
Eric Blake68b96f12019-01-17 13:36:56 -0600222 }
223 printf(" )\n");
224 }
225 if (list[i].min_block) {
226 printf(" min block: %u\n", list[i].min_block);
227 printf(" opt block: %u\n", list[i].opt_block);
228 printf(" max block: %u\n", list[i].max_block);
229 }
230 if (list[i].n_contexts) {
231 printf(" available meta contexts: %d\n", list[i].n_contexts);
232 for (j = 0; j < list[i].n_contexts; j++) {
233 printf(" %s\n", list[i].contexts[j]);
234 }
235 }
236 }
237 nbd_free_export_list(list, rc);
238
239 ret = EXIT_SUCCESS;
240 out:
241 object_unref(OBJECT(sioc));
242 return ret;
243}
244
245
Eric Blake3c1fa352018-12-15 07:53:08 -0600246#if HAVE_NBD_DEVICE
Paolo Bonzinia517e882011-11-04 15:51:21 +0100247static void *show_parts(void *arg)
thscd831bd2008-07-03 10:23:51 +0000248{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100249 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100250 int nbd;
thscd831bd2008-07-03 10:23:51 +0000251
Paolo Bonzinia517e882011-11-04 15:51:21 +0100252 /* linux just needs an open() to trigger
253 * the partition table update
254 * but remember to load the module with max_part != 0 :
255 * modprobe nbd max_part=63
256 */
257 nbd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100258 if (nbd >= 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100259 close(nbd);
thscd831bd2008-07-03 10:23:51 +0000260 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100261 return NULL;
262}
263
264static void *nbd_client_thread(void *arg)
265{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100266 char *device = arg;
Eric Blake6dc16672019-01-17 13:36:46 -0600267 NBDExportInfo info = { .request_sizes = false, .name = g_strdup("") };
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000268 QIOChannelSocket *sioc;
Alex Chenaf74b552020-12-08 13:49:44 +0000269 int fd = -1;
270 int ret = EXIT_FAILURE;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100271 pthread_t show_parts_thread;
Max Reitz1ce52842015-01-26 21:02:59 -0500272 Error *local_error = NULL;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100273
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000274 sioc = qio_channel_socket_new();
275 if (qio_channel_socket_connect_sync(sioc,
276 saddr,
277 &local_error) < 0) {
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100278 error_report_err(local_error);
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000279 goto out;
280 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100281
Alex Chenaf74b552020-12-08 13:49:44 +0000282 if (nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc),
283 NULL, NULL, NULL, &info, &local_error) < 0) {
Max Reitz1ce52842015-01-26 21:02:59 -0500284 if (local_error) {
Markus Armbruster78288672015-12-18 16:35:07 +0100285 error_report_err(local_error);
Max Reitz1ce52842015-01-26 21:02:59 -0500286 }
Alex Chenaf74b552020-12-08 13:49:44 +0000287 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100288 }
289
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100290 fd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100291 if (fd < 0) {
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100292 /* Linux-only, we can use %m in printf. */
Markus Armbrusterb9884682015-12-18 16:35:18 +0100293 error_report("Failed to open %s: %m", device);
Alex Chenaf74b552020-12-08 13:49:44 +0000294 goto out;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100295 }
296
Alex Chenaf74b552020-12-08 13:49:44 +0000297 if (nbd_init(fd, sioc, &info, &local_error) < 0) {
Vladimir Sementsov-Ogievskiybe41c102017-05-26 14:09:13 +0300298 error_report_err(local_error);
Alex Chenaf74b552020-12-08 13:49:44 +0000299 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100300 }
301
302 /* update partition table */
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100303 pthread_create(&show_parts_thread, NULL, show_parts, device);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100304
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100305 if (verbose) {
306 fprintf(stderr, "NBD device %s is now connected to %s\n",
307 device, srcpath);
308 } else {
309 /* Close stderr so that the qemu-nbd process exits. */
310 dup2(STDOUT_FILENO, STDERR_FILENO);
311 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100312
Alex Chenaf74b552020-12-08 13:49:44 +0000313 if (nbd_client(fd) < 0) {
314 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100315 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100316
Alex Chenaf74b552020-12-08 13:49:44 +0000317 ret = EXIT_SUCCESS;
318
319 out:
320 if (fd >= 0) {
321 close(fd);
322 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000323 object_unref(OBJECT(sioc));
Eric Blake6dc16672019-01-17 13:36:46 -0600324 g_free(info.name);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100325 kill(getpid(), SIGTERM);
Alex Chenaf74b552020-12-08 13:49:44 +0000326 return (void *) (intptr_t) ret;
thscd831bd2008-07-03 10:23:51 +0000327}
Eric Blake3c1fa352018-12-15 07:53:08 -0600328#endif /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000329
Fam Zhenge4afbf42015-05-19 10:50:59 +0000330static int nbd_can_accept(void)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200331{
Eric Blake3dcf56e2021-02-09 09:27:59 -0600332 return state == RUNNING && (shared == 0 || nb_fds < shared);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200333}
334
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000335static void nbd_update_server_watch(void);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000336
Eric Blake0c9390d2017-06-08 17:26:17 -0500337static void nbd_client_closed(NBDClient *client, bool negotiated)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200338{
Paolo Bonzini1743b512011-09-19 14:33:23 +0200339 nb_fds--;
Eric Blake0c9390d2017-06-08 17:26:17 -0500340 if (negotiated && nb_fds == 0 && !persistent && state == RUNNING) {
Paolo Bonzini7860a382012-09-18 13:31:56 +0200341 state = TERMINATE;
342 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000343 nbd_update_server_watch();
Paolo Bonzini7860a382012-09-18 13:31:56 +0200344 nbd_client_put(client);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200345}
346
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000347static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc,
348 gpointer opaque)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200349{
Paolo Bonzini7860a382012-09-18 13:31:56 +0200350 if (state >= TERMINATE) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000351 return;
Paolo Bonzini7860a382012-09-18 13:31:56 +0200352 }
353
Fam Zhengee7d7aa2016-01-14 16:41:01 +0800354 nb_fds++;
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000355 nbd_update_server_watch();
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000356 nbd_client_new(cioc, tlscreds, tlsauthz, nbd_client_closed);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200357}
358
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000359static void nbd_update_server_watch(void)
Fam Zhenge4afbf42015-05-19 10:50:59 +0000360{
361 if (nbd_can_accept()) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000362 qio_net_listener_set_client_func(server, nbd_accept, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000363 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000364 qio_net_listener_set_client_func(server, NULL, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000365 }
366}
367
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100368
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200369static SocketAddress *nbd_build_socket_address(const char *sockpath,
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100370 const char *bindto,
371 const char *port)
372{
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200373 SocketAddress *saddr;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100374
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200375 saddr = g_new0(SocketAddress, 1);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100376 if (sockpath) {
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200377 saddr->type = SOCKET_ADDRESS_TYPE_UNIX;
378 saddr->u.q_unix.path = g_strdup(sockpath);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100379 } else {
Eric Blake03992932016-03-03 09:16:48 -0700380 InetSocketAddress *inet;
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200381 saddr->type = SOCKET_ADDRESS_TYPE_INET;
382 inet = &saddr->u.inet;
Eric Blake03992932016-03-03 09:16:48 -0700383 inet->host = g_strdup(bindto);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100384 if (port) {
Eric Blake03992932016-03-03 09:16:48 -0700385 inet->port = g_strdup(port);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100386 } else {
Eric Blake03992932016-03-03 09:16:48 -0700387 inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100388 }
389 }
390
391 return saddr;
392}
393
394
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000395static QemuOptsList file_opts = {
396 .name = "file",
397 .implied_opt_name = "file",
398 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
399 .desc = {
400 /* no elements => accept any params */
401 { /* end of list */ }
402 },
403};
404
Eric Blake68b96f12019-01-17 13:36:56 -0600405static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, bool list,
406 Error **errp)
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000407{
408 Object *obj;
409 QCryptoTLSCreds *creds;
410
411 obj = object_resolve_path_component(
412 object_get_objects_root(), id);
413 if (!obj) {
414 error_setg(errp, "No TLS credentials with id '%s'",
415 id);
416 return NULL;
417 }
418 creds = (QCryptoTLSCreds *)
419 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
420 if (!creds) {
421 error_setg(errp, "Object with id '%s' is not TLS credentials",
422 id);
423 return NULL;
424 }
425
Philippe Mathieu-Daudé0279cd92021-06-28 18:09:10 +0200426 if (!qcrypto_tls_creds_check_endpoint(creds,
427 list
428 ? QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
429 : QCRYPTO_TLS_CREDS_ENDPOINT_SERVER,
430 errp)) {
431 return NULL;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000432 }
433 object_ref(obj);
434 return creds;
435}
436
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000437static void setup_address_and_port(const char **address, const char **port)
438{
439 if (*address == NULL) {
440 *address = "0.0.0.0";
441 }
442
443 if (*port == NULL) {
444 *port = stringify(NBD_DEFAULT_PORT);
445 }
446}
447
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000448/*
449 * Check socket parameters compatibility when socket activation is used.
450 */
451static const char *socket_activation_validate_opts(const char *device,
452 const char *sockpath,
453 const char *address,
Eric Blake68b96f12019-01-17 13:36:56 -0600454 const char *port,
455 bool list)
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000456{
457 if (device != NULL) {
458 return "NBD device can't be set when using socket activation";
459 }
460
461 if (sockpath != NULL) {
462 return "Unix socket can't be set when using socket activation";
463 }
464
465 if (address != NULL) {
466 return "The interface can't be set when using socket activation";
467 }
468
469 if (port != NULL) {
470 return "TCP port number can't be set when using socket activation";
471 }
472
Eric Blake68b96f12019-01-17 13:36:56 -0600473 if (list) {
474 return "List mode is incompatible with socket activation";
475 }
476
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000477 return NULL;
478}
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000479
Kevin Wolfb3b52992018-05-16 13:46:37 +0200480static void qemu_nbd_shutdown(void)
481{
482 job_cancel_sync_all();
Sergio Lopez1895b972021-02-01 13:50:32 +0100483 blk_exp_close_all();
Kevin Wolfb3b52992018-05-16 13:46:37 +0200484 bdrv_close_all();
485}
486
bellard7a5ca862008-05-27 21:13:40 +0000487int main(int argc, char **argv)
488{
Markus Armbruster26f54e92014-10-07 13:59:04 +0200489 BlockBackend *blk;
bellard7a5ca862008-05-27 21:13:40 +0000490 BlockDriverState *bs;
Eric Blake9d26dfc2019-01-17 13:36:43 -0600491 uint64_t dev_offset = 0;
Eric Blakedbb38ca2019-08-23 09:37:22 -0500492 bool readonly = false;
thscd831bd2008-07-03 10:23:51 +0000493 bool disconnect = false;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000494 const char *bindto = NULL;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100495 const char *port = NULL;
496 char *sockpath = NULL;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100497 char *device = NULL;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800498 QemuOpts *sn_opts = NULL;
499 const char *sn_id_or_name = NULL;
Eric Blakedbc7b012020-10-27 00:05:55 -0500500 const char *sopt = "hVb:o:p:rsnc:dvk:e:f:tl:x:T:D:AB:L";
bellard7a5ca862008-05-27 21:13:40 +0000501 struct option lopt[] = {
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000502 { "help", no_argument, NULL, 'h' },
503 { "version", no_argument, NULL, 'V' },
504 { "bind", required_argument, NULL, 'b' },
505 { "port", required_argument, NULL, 'p' },
506 { "socket", required_argument, NULL, 'k' },
507 { "offset", required_argument, NULL, 'o' },
508 { "read-only", no_argument, NULL, 'r' },
Eric Blakedbc7b012020-10-27 00:05:55 -0500509 { "allocation-depth", no_argument, NULL, 'A' },
Eric Blake636192c2019-01-11 13:47:20 -0600510 { "bitmap", required_argument, NULL, 'B' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000511 { "connect", required_argument, NULL, 'c' },
512 { "disconnect", no_argument, NULL, 'd' },
Eric Blake68b96f12019-01-17 13:36:56 -0600513 { "list", no_argument, NULL, 'L' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000514 { "snapshot", no_argument, NULL, 's' },
515 { "load-snapshot", required_argument, NULL, 'l' },
516 { "nocache", no_argument, NULL, 'n' },
517 { "cache", required_argument, NULL, QEMU_NBD_OPT_CACHE },
518 { "aio", required_argument, NULL, QEMU_NBD_OPT_AIO },
519 { "discard", required_argument, NULL, QEMU_NBD_OPT_DISCARD },
520 { "detect-zeroes", required_argument, NULL,
521 QEMU_NBD_OPT_DETECT_ZEROES },
522 { "shared", required_argument, NULL, 'e' },
523 { "format", required_argument, NULL, 'f' },
524 { "persistent", no_argument, NULL, 't' },
525 { "verbose", no_argument, NULL, 'v' },
526 { "object", required_argument, NULL, QEMU_NBD_OPT_OBJECT },
527 { "export-name", required_argument, NULL, 'x' },
Eric Blakeb1a75b32016-10-14 13:33:03 -0500528 { "description", required_argument, NULL, 'D' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000529 { "tls-creds", required_argument, NULL, QEMU_NBD_OPT_TLSCREDS },
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000530 { "tls-authz", required_argument, NULL, QEMU_NBD_OPT_TLSAUTHZ },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000531 { "image-opts", no_argument, NULL, QEMU_NBD_OPT_IMAGE_OPTS },
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300532 { "trace", required_argument, NULL, 'T' },
Max Reitzffb31e12016-09-28 22:46:42 +0200533 { "fork", no_argument, NULL, QEMU_NBD_OPT_FORK },
Max Reitz637bc5a2019-05-08 23:18:16 +0200534 { "pid-file", required_argument, NULL, QEMU_NBD_OPT_PID_FILE },
Blue Swirl660f11b2009-07-31 21:16:51 +0000535 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000536 };
537 int ch;
538 int opt_ind = 0;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200539 int flags = BDRV_O_RDWR;
Max Reitz4fbec262015-02-05 13:58:19 -0500540 int ret = 0;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200541 bool seen_cache = false;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100542 bool seen_discard = false;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200543 bool seen_aio = false;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100544 pthread_t client_thread;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000545 const char *fmt = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200546 Error *local_err = NULL;
Peter Lievenb3838a42014-08-13 19:20:18 +0200547 BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
Max Reitz4fbec262015-02-05 13:58:19 -0500548 QDict *options = NULL;
Eric Blake68b96f12019-01-17 13:36:56 -0600549 const char *export_name = NULL; /* defaults to "" later for server mode */
Eric Blakeb1a75b32016-10-14 13:33:03 -0500550 const char *export_description = NULL;
Eric Blakecbad81c2020-10-27 00:05:49 -0500551 strList *bitmaps = NULL;
Eric Blakedbc7b012020-10-27 00:05:55 -0500552 bool alloc_depth = false;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000553 const char *tlscredsid = NULL;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000554 bool imageOpts = false;
Kevin Wolf6effd5b2016-03-14 11:43:28 +0100555 bool writethrough = true;
Max Reitzffb31e12016-09-28 22:46:42 +0200556 bool fork_process = false;
Eric Blake68b96f12019-01-17 13:36:56 -0600557 bool list = false;
Max Reitzffb31e12016-09-28 22:46:42 +0200558 int old_stderr = -1;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000559 unsigned socket_activation;
Max Reitz637bc5a2019-05-08 23:18:16 +0200560 const char *pid_file_name = NULL;
Kevin Wolf00917172020-09-24 17:26:57 +0200561 BlockExportOptions *export_opts;
bellard7a5ca862008-05-27 21:13:40 +0000562
Eric Blake029a88c2020-09-30 07:11:01 -0500563#ifdef CONFIG_POSIX
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +0100564 os_setup_early_signal_handling();
565 os_setup_signal_handling();
Max Reitz041e32b2017-06-11 14:37:14 +0200566#endif
567
Daniel P. Berrangé98c5d2e2020-08-25 11:38:48 +0100568 socket_init();
Christophe Fergeauf5852ef2019-01-31 17:46:14 +0100569 error_init(argv[0]);
Daniel P. Berrangefe4db842016-10-04 14:35:52 +0100570 module_call_init(MODULE_INIT_TRACE);
Eduardo Habkoste8f2d272016-05-12 11:10:04 -0300571 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100572
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000573 module_call_init(MODULE_INIT_QOM);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300574 qemu_add_opts(&qemu_trace_opts);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800575 qemu_init_exec_dir(argv[0]);
Paolo Bonzinibb345112011-11-04 15:51:19 +0100576
bellard7a5ca862008-05-27 21:13:40 +0000577 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
578 switch (ch) {
579 case 's':
ths2f726482008-07-03 11:47:46 +0000580 flags |= BDRV_O_SNAPSHOT;
581 break;
582 case 'n':
Paolo Bonzini39a52352012-07-18 14:57:15 +0200583 optarg = (char *) "none";
584 /* fallthrough */
585 case QEMU_NBD_OPT_CACHE:
586 if (seen_cache) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100587 error_report("-n and --cache can only be specified once");
588 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200589 }
590 seen_cache = true;
Kevin Wolf6effd5b2016-03-14 11:43:28 +0100591 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100592 error_report("Invalid cache mode `%s'", optarg);
593 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200594 }
bellard7a5ca862008-05-27 21:13:40 +0000595 break;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200596 case QEMU_NBD_OPT_AIO:
597 if (seen_aio) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100598 error_report("--aio can only be specified once");
599 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200600 }
601 seen_aio = true;
Aarushi Mehta76802742020-01-20 14:18:56 +0000602 if (bdrv_parse_aio(optarg, &flags) < 0) {
603 error_report("Invalid aio mode '%s'", optarg);
604 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200605 }
606 break;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100607 case QEMU_NBD_OPT_DISCARD:
608 if (seen_discard) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100609 error_report("--discard can only be specified once");
610 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100611 }
612 seen_discard = true;
613 if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100614 error_report("Invalid discard mode `%s'", optarg);
615 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100616 }
617 break;
Peter Lievenb3838a42014-08-13 19:20:18 +0200618 case QEMU_NBD_OPT_DETECT_ZEROES:
619 detect_zeroes =
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200620 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
Peter Lievenb3838a42014-08-13 19:20:18 +0200621 optarg,
Peter Lievenb3838a42014-08-13 19:20:18 +0200622 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
623 &local_err);
624 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100625 error_reportf_err(local_err,
626 "Failed to parse detect_zeroes mode: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100627 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200628 }
629 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
630 !(flags & BDRV_O_UNMAP)) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100631 error_report("setting detect-zeroes to unmap is not allowed "
632 "without setting discard operation to unmap");
633 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200634 }
635 break;
bellard7a5ca862008-05-27 21:13:40 +0000636 case 'b':
637 bindto = optarg;
638 break;
639 case 'p':
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100640 port = optarg;
bellard7a5ca862008-05-27 21:13:40 +0000641 break;
642 case 'o':
Eric Blake43b51012019-01-17 13:36:44 -0600643 if (qemu_strtou64(optarg, NULL, 0, &dev_offset) < 0) {
644 error_report("Invalid offset '%s'", optarg);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100645 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000646 }
bellard7a5ca862008-05-27 21:13:40 +0000647 break;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800648 case 'l':
649 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +0100650 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
651 optarg, false);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800652 if (!sn_opts) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100653 error_report("Failed in parsing snapshot param `%s'",
654 optarg);
655 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800656 }
657 } else {
658 sn_id_or_name = optarg;
659 }
660 /* fall through */
bellard7a5ca862008-05-27 21:13:40 +0000661 case 'r':
Eric Blakedbb38ca2019-08-23 09:37:22 -0500662 readonly = true;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200663 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000664 break;
Eric Blakedbc7b012020-10-27 00:05:55 -0500665 case 'A':
666 alloc_depth = true;
667 break;
Eric Blake636192c2019-01-11 13:47:20 -0600668 case 'B':
Eric Blakecbad81c2020-10-27 00:05:49 -0500669 QAPI_LIST_PREPEND(bitmaps, g_strdup(optarg));
Eric Blake636192c2019-01-11 13:47:20 -0600670 break;
thscd831bd2008-07-03 10:23:51 +0000671 case 'k':
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100672 sockpath = optarg;
Peter Lieven713cc672014-08-13 19:20:19 +0200673 if (sockpath[0] != '/') {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +0100674 error_report("socket path must be absolute");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100675 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200676 }
thscd831bd2008-07-03 10:23:51 +0000677 break;
678 case 'd':
679 disconnect = true;
680 break;
681 case 'c':
682 device = optarg;
683 break;
ths3b05a8e2008-07-03 12:45:02 +0000684 case 'e':
Eric Blake43b51012019-01-17 13:36:44 -0600685 if (qemu_strtoi(optarg, NULL, 0, &shared) < 0 ||
Eric Blake3dcf56e2021-02-09 09:27:59 -0600686 shared < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100687 error_report("Invalid shared device number '%s'", optarg);
688 exit(EXIT_FAILURE);
ths3b05a8e2008-07-03 12:45:02 +0000689 }
ths3b05a8e2008-07-03 12:45:02 +0000690 break;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000691 case 'f':
692 fmt = optarg;
693 break;
Peter Lieven713cc672014-08-13 19:20:19 +0200694 case 't':
695 persistent = 1;
696 break;
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000697 case 'x':
698 export_name = optarg;
Eric Blake93676c82019-11-13 20:46:34 -0600699 if (strlen(export_name) > NBD_MAX_STRING_SIZE) {
700 error_report("export name '%s' too long", export_name);
701 exit(EXIT_FAILURE);
702 }
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000703 break;
Eric Blakeb1a75b32016-10-14 13:33:03 -0500704 case 'D':
705 export_description = optarg;
Eric Blake93676c82019-11-13 20:46:34 -0600706 if (strlen(export_description) > NBD_MAX_STRING_SIZE) {
707 error_report("export description '%s' too long",
708 export_description);
709 exit(EXIT_FAILURE);
710 }
Eric Blakeb1a75b32016-10-14 13:33:03 -0500711 break;
bellard7a5ca862008-05-27 21:13:40 +0000712 case 'v':
713 verbose = 1;
714 break;
715 case 'V':
716 version(argv[0]);
717 exit(0);
718 break;
719 case 'h':
720 usage(argv[0]);
721 exit(0);
722 break;
723 case '?':
Markus Armbruster85b01e02015-12-18 16:35:04 +0100724 error_report("Try `%s --help' for more information.", argv[0]);
725 exit(EXIT_FAILURE);
Kevin Wolffa40e432021-02-17 12:56:45 +0100726 case QEMU_NBD_OPT_OBJECT:
727 user_creatable_process_cmdline(optarg);
728 break;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000729 case QEMU_NBD_OPT_TLSCREDS:
730 tlscredsid = optarg;
731 break;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000732 case QEMU_NBD_OPT_IMAGE_OPTS:
733 imageOpts = true;
734 break;
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300735 case 'T':
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500736 trace_opt_parse(optarg);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300737 break;
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000738 case QEMU_NBD_OPT_TLSAUTHZ:
739 tlsauthz = optarg;
740 break;
Max Reitzffb31e12016-09-28 22:46:42 +0200741 case QEMU_NBD_OPT_FORK:
742 fork_process = true;
743 break;
Eric Blake68b96f12019-01-17 13:36:56 -0600744 case 'L':
745 list = true;
746 break;
Max Reitz637bc5a2019-05-08 23:18:16 +0200747 case QEMU_NBD_OPT_PID_FILE:
748 pid_file_name = optarg;
749 break;
bellard7a5ca862008-05-27 21:13:40 +0000750 }
751 }
752
Eric Blake68b96f12019-01-17 13:36:56 -0600753 if (list) {
754 if (argc != optind) {
755 error_report("List mode is incompatible with a file name");
756 exit(EXIT_FAILURE);
757 }
Eric Blake0bc16992020-01-23 10:46:50 -0600758 if (export_name || export_description || dev_offset ||
Eric Blakecbad81c2020-10-27 00:05:49 -0500759 device || disconnect || fmt || sn_id_or_name || bitmaps ||
Eric Blakedbc7b012020-10-27 00:05:55 -0500760 alloc_depth || seen_aio || seen_discard || seen_cache) {
Eric Blake68b96f12019-01-17 13:36:56 -0600761 error_report("List mode is incompatible with per-device settings");
762 exit(EXIT_FAILURE);
763 }
764 if (fork_process) {
765 error_report("List mode is incompatible with forking");
766 exit(EXIT_FAILURE);
767 }
768 } else if ((argc - optind) != 1) {
Markus Armbruster433672b2015-12-18 16:35:24 +0100769 error_report("Invalid number of arguments");
770 error_printf("Try `%s --help' for more information.\n", argv[0]);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100771 exit(EXIT_FAILURE);
Eric Blake68b96f12019-01-17 13:36:56 -0600772 } else if (!export_name) {
773 export_name = "";
bellard7a5ca862008-05-27 21:13:40 +0000774 }
775
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300776 if (!trace_init_backends()) {
777 exit(1);
778 }
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500779 trace_init_file();
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300780 qemu_set_log(LOG_TRACE);
781
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000782 socket_activation = check_socket_activation();
783 if (socket_activation == 0) {
784 setup_address_and_port(&bindto, &port);
785 } else {
786 /* Using socket activation - check user didn't use -p etc. */
787 const char *err_msg = socket_activation_validate_opts(device, sockpath,
Eric Blake68b96f12019-01-17 13:36:56 -0600788 bindto, port,
789 list);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000790 if (err_msg != NULL) {
791 error_report("%s", err_msg);
792 exit(EXIT_FAILURE);
793 }
Paolo Bonzini53fabd42017-03-16 16:29:45 +0100794
795 /* qemu-nbd can only listen on a single socket. */
796 if (socket_activation > 1) {
797 error_report("qemu-nbd does not support socket activation with %s > 1",
798 "LISTEN_FDS");
799 exit(EXIT_FAILURE);
800 }
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000801 }
802
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000803 if (tlscredsid) {
804 if (sockpath) {
805 error_report("TLS is only supported with IPv4/IPv6");
806 exit(EXIT_FAILURE);
807 }
808 if (device) {
809 error_report("TLS is not supported with a host device");
810 exit(EXIT_FAILURE);
811 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000812 if (tlsauthz && list) {
813 error_report("TLS authorization is incompatible with export list");
814 exit(EXIT_FAILURE);
815 }
Eric Blake68b96f12019-01-17 13:36:56 -0600816 tlscreds = nbd_get_tls_creds(tlscredsid, list, &local_err);
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000817 if (local_err) {
Markus Armbruster5217f182020-05-05 12:19:03 +0200818 error_reportf_err(local_err, "Failed to get TLS creds: ");
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000819 exit(EXIT_FAILURE);
820 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000821 } else {
822 if (tlsauthz) {
823 error_report("--tls-authz is not permitted without --tls-creds");
824 exit(EXIT_FAILURE);
825 }
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000826 }
827
Eric Blake68b96f12019-01-17 13:36:56 -0600828 if (list) {
829 saddr = nbd_build_socket_address(sockpath, bindto, port);
830 return qemu_nbd_client_list(saddr, tlscreds, bindto);
831 }
832
Eric Blake3c1fa352018-12-15 07:53:08 -0600833#if !HAVE_NBD_DEVICE
834 if (disconnect || device) {
835 error_report("Kernel /dev/nbdN support not available");
836 exit(EXIT_FAILURE);
837 }
838#else /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000839 if (disconnect) {
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000840 int nbdfd = open(argv[optind], O_RDWR);
841 if (nbdfd < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100842 error_report("Cannot open %s: %s", argv[optind],
843 strerror(errno));
844 exit(EXIT_FAILURE);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100845 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000846 nbd_disconnect(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000847
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000848 close(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000849
850 printf("%s disconnected\n", argv[optind]);
851
Peter Lieven713cc672014-08-13 19:20:19 +0200852 return 0;
thscd831bd2008-07-03 10:23:51 +0000853 }
Eric Blake3c1fa352018-12-15 07:53:08 -0600854#endif
thscd831bd2008-07-03 10:23:51 +0000855
Max Reitzffb31e12016-09-28 22:46:42 +0200856 if ((device && !verbose) || fork_process) {
Daniel P. Berrangéeb705982020-08-25 11:38:50 +0100857#ifndef WIN32
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100858 int stderr_fd[2];
859 pid_t pid;
860 int ret;
861
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100862 if (qemu_pipe(stderr_fd) < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100863 error_report("Error setting up communication pipe: %s",
864 strerror(errno));
865 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100866 }
867
868 /* Now daemonize, but keep a communication channel open to
869 * print errors and exit with the proper status code.
870 */
871 pid = fork();
Max Reitz70d47392015-02-25 13:08:22 -0500872 if (pid < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100873 error_report("Failed to fork: %s", strerror(errno));
874 exit(EXIT_FAILURE);
Max Reitz70d47392015-02-25 13:08:22 -0500875 } else if (pid == 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100876 close(stderr_fd[0]);
Max Reitze6df58a2019-05-08 23:18:18 +0200877
Raphael Pour0eaf4532020-05-15 08:36:07 +0200878 /* Remember parent's stderr if we will be restoring it. */
879 if (fork_process) {
880 old_stderr = dup(STDERR_FILENO);
881 }
882
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400883 ret = qemu_daemon(1, 0);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100884
885 /* Temporarily redirect stderr to the parent's pipe... */
886 dup2(stderr_fd[1], STDERR_FILENO);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100887 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100888 error_report("Failed to daemonize: %s", strerror(errno));
889 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100890 }
891
892 /* ... close the descriptor we inherited and go on. */
893 close(stderr_fd[1]);
894 } else {
895 bool errors = false;
896 char *buf;
897
898 /* In the parent. Print error messages from the child until
899 * it closes the pipe.
900 */
901 close(stderr_fd[1]);
902 buf = g_malloc(1024);
903 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
904 errors = true;
905 ret = qemu_write_full(STDERR_FILENO, buf, ret);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100906 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100907 exit(EXIT_FAILURE);
908 }
909 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100910 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100911 error_report("Cannot read from daemon: %s",
912 strerror(errno));
913 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100914 }
915
916 /* Usually the daemon should not print any message.
917 * Exit with zero status in that case.
918 */
919 exit(errors);
920 }
Daniel P. Berrangéeb705982020-08-25 11:38:50 +0100921#else /* WIN32 */
922 error_report("Unable to fork into background on Windows hosts");
923 exit(EXIT_FAILURE);
924#endif /* WIN32 */
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100925 }
926
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100927 if (device != NULL && sockpath == NULL) {
928 sockpath = g_malloc(128);
929 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
thscd831bd2008-07-03 10:23:51 +0000930 }
931
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000932 server = qio_net_listener_new();
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000933 if (socket_activation == 0) {
Eric Blake582d4212021-02-09 09:27:58 -0600934 int backlog;
935
Eric Blake3dcf56e2021-02-09 09:27:59 -0600936 if (persistent || shared == 0) {
Eric Blake582d4212021-02-09 09:27:58 -0600937 backlog = SOMAXCONN;
938 } else {
939 backlog = MIN(shared, SOMAXCONN);
940 }
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000941 saddr = nbd_build_socket_address(sockpath, bindto, port);
Eric Blake582d4212021-02-09 09:27:58 -0600942 if (qio_net_listener_open_sync(server, saddr, backlog,
943 &local_err) < 0) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000944 object_unref(OBJECT(server));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000945 error_report_err(local_err);
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000946 exit(EXIT_FAILURE);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000947 }
948 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000949 size_t i;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000950 /* See comment in check_socket_activation above. */
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000951 for (i = 0; i < socket_activation; i++) {
952 QIOChannelSocket *sioc;
953 sioc = qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD + i,
954 &local_err);
955 if (sioc == NULL) {
956 object_unref(OBJECT(server));
Markus Armbruster5217f182020-05-05 12:19:03 +0200957 error_reportf_err(local_err,
958 "Failed to use socket activation: ");
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000959 exit(EXIT_FAILURE);
960 }
961 qio_net_listener_add(server, sioc);
962 object_unref(OBJECT(sioc));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000963 }
964 }
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100965
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300966 if (qemu_init_main_loop(&local_err)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100967 error_report_err(local_err);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300968 exit(EXIT_FAILURE);
969 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100970 bdrv_init();
Kevin Wolfb3b52992018-05-16 13:46:37 +0200971 atexit(qemu_nbd_shutdown);
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100972
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000973 srcpath = argv[optind];
974 if (imageOpts) {
975 QemuOpts *opts;
976 if (fmt) {
977 error_report("--image-opts and -f are mutually exclusive");
978 exit(EXIT_FAILURE);
979 }
980 opts = qemu_opts_parse_noisily(&file_opts, srcpath, true);
981 if (!opts) {
982 qemu_opts_reset(&file_opts);
983 exit(EXIT_FAILURE);
984 }
985 options = qemu_opts_to_qdict(opts, NULL);
986 qemu_opts_reset(&file_opts);
Max Reitzefaa7c42016-03-16 19:54:38 +0100987 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000988 } else {
989 if (fmt) {
990 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -0500991 qdict_put_str(options, "driver", fmt);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000992 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100993 blk = blk_new_open(srcpath, NULL, options, flags, &local_err);
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000994 }
995
Max Reitz4fbec262015-02-05 13:58:19 -0500996 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100997 error_reportf_err(local_err, "Failed to blk_new_open '%s': ",
998 argv[optind]);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100999 exit(EXIT_FAILURE);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001000 }
Max Reitz4fbec262015-02-05 13:58:19 -05001001 bs = blk_bs(blk);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001002
Kevin Wolfb57e4de2020-09-24 17:26:52 +02001003 if (dev_offset) {
1004 QDict *raw_opts = qdict_new();
1005 qdict_put_str(raw_opts, "driver", "raw");
1006 qdict_put_str(raw_opts, "file", bs->node_name);
1007 qdict_put_int(raw_opts, "offset", dev_offset);
1008 bs = bdrv_open(NULL, NULL, raw_opts, flags, &error_fatal);
1009 blk_remove_bs(blk);
1010 blk_insert_bs(blk, bs, &error_fatal);
1011 bdrv_unref(bs);
1012 }
1013
Kevin Wolf6effd5b2016-03-14 11:43:28 +01001014 blk_set_enable_write_cache(blk, !writethrough);
1015
Wenchao Xia8c116b02013-12-04 17:10:55 +08001016 if (sn_opts) {
1017 ret = bdrv_snapshot_load_tmp(bs,
1018 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1019 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1020 &local_err);
1021 } else if (sn_id_or_name) {
1022 ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
1023 &local_err);
1024 }
1025 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001026 error_reportf_err(local_err, "Failed to load snapshot: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +01001027 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001028 }
1029
Peter Lievenb3838a42014-08-13 19:20:18 +02001030 bs->detect_zeroes = detect_zeroes;
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001031
Kevin Wolf00917172020-09-24 17:26:57 +02001032 nbd_server_is_qemu_nbd(true);
1033
1034 export_opts = g_new(BlockExportOptions, 1);
1035 *export_opts = (BlockExportOptions) {
1036 .type = BLOCK_EXPORT_TYPE_NBD,
Kevin Wolfd53be9c2020-09-24 17:27:04 +02001037 .id = g_strdup("qemu-nbd-export"),
Kevin Wolfb6076af2020-09-24 17:27:01 +02001038 .node_name = g_strdup(bdrv_get_node_name(bs)),
Kevin Wolf00917172020-09-24 17:26:57 +02001039 .has_writethrough = true,
1040 .writethrough = writethrough,
Kevin Wolf30dbc812020-09-24 17:27:11 +02001041 .has_writable = true,
1042 .writable = !readonly,
Kevin Wolf00917172020-09-24 17:26:57 +02001043 .u.nbd = {
Eric Blakecbad81c2020-10-27 00:05:49 -05001044 .has_name = true,
1045 .name = g_strdup(export_name),
1046 .has_description = !!export_description,
1047 .description = g_strdup(export_description),
1048 .has_bitmaps = !!bitmaps,
1049 .bitmaps = bitmaps,
Eric Blakedbc7b012020-10-27 00:05:55 -05001050 .has_allocation_depth = alloc_depth,
1051 .allocation_depth = alloc_depth,
Kevin Wolf00917172020-09-24 17:26:57 +02001052 },
1053 };
1054 blk_exp_add(export_opts, &error_fatal);
1055 qapi_free_BlockExportOptions(export_opts);
ths3b05a8e2008-07-03 12:45:02 +00001056
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001057 if (device) {
Eric Blake3c1fa352018-12-15 07:53:08 -06001058#if HAVE_NBD_DEVICE
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001059 int ret;
1060
Paolo Bonzinia6ac2312011-12-06 09:07:00 +01001061 ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001062 if (ret != 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001063 error_report("Failed to create client thread: %s", strerror(ret));
1064 exit(EXIT_FAILURE);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001065 }
Eric Blake3c1fa352018-12-15 07:53:08 -06001066#endif
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001067 } else {
1068 /* Shut up GCC warnings. */
1069 memset(&client_thread, 0, sizeof(client_thread));
1070 }
1071
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +00001072 nbd_update_server_watch();
bellard7a5ca862008-05-27 21:13:40 +00001073
Max Reitz637bc5a2019-05-08 23:18:16 +02001074 if (pid_file_name) {
1075 qemu_write_pidfile(pid_file_name, &error_fatal);
1076 }
1077
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001078 /* now when the initialization is (almost) complete, chdir("/")
1079 * to free any busy filesystems */
1080 if (chdir("/") < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001081 error_report("Could not chdir to root directory: %s",
1082 strerror(errno));
1083 exit(EXIT_FAILURE);
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001084 }
1085
Max Reitzffb31e12016-09-28 22:46:42 +02001086 if (fork_process) {
1087 dup2(old_stderr, STDERR_FILENO);
1088 close(old_stderr);
1089 }
1090
Paolo Bonzini7860a382012-09-18 13:31:56 +02001091 state = RUNNING;
ths3b05a8e2008-07-03 12:45:02 +00001092 do {
Paolo Bonzinia61c6782011-09-12 17:28:11 +02001093 main_loop_wait(false);
Paolo Bonzini7860a382012-09-18 13:31:56 +02001094 if (state == TERMINATE) {
Kevin Wolfbc4ee652020-09-24 17:27:03 +02001095 blk_exp_close_all();
Kevin Wolfd794f7f2020-09-24 17:26:56 +02001096 state = TERMINATED;
Paolo Bonzini7860a382012-09-18 13:31:56 +02001097 }
1098 } while (state != TERMINATED);
ths3b05a8e2008-07-03 12:45:02 +00001099
Markus Armbruster26f54e92014-10-07 13:59:04 +02001100 blk_unref(blk);
Paolo Bonzinib32f6c22011-11-04 15:51:20 +01001101 if (sockpath) {
1102 unlink(sockpath);
1103 }
bellard7a5ca862008-05-27 21:13:40 +00001104
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02001105 qemu_opts_del(sn_opts);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001106
Paolo Bonzinia517e882011-11-04 15:51:21 +01001107 if (device) {
1108 void *ret;
1109 pthread_join(client_thread, &ret);
1110 exit(ret != NULL);
1111 } else {
1112 exit(EXIT_SUCCESS);
1113 }
bellard7a5ca862008-05-27 21:13:40 +00001114}