blob: bf9c5fedceba45d36e275334f368e4d28efa72f8 [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
Richard W.M. Jones3d212b42021-11-15 14:29:43 -060050#ifdef CONFIG_SELINUX
51#include <selinux/selinux.h>
52#endif
53
Eric Blake3c1fa352018-12-15 07:53:08 -060054#ifdef __linux__
55#define HAVE_NBD_DEVICE 1
56#else
57#define HAVE_NBD_DEVICE 0
58#endif
59
Peter Lieven713cc672014-08-13 19:20:19 +020060#define SOCKET_PATH "/var/lock/qemu-nbd-%s"
Daniel P. Berrangefa8b7ce2016-02-17 10:10:21 +000061#define QEMU_NBD_OPT_CACHE 256
62#define QEMU_NBD_OPT_AIO 257
63#define QEMU_NBD_OPT_DISCARD 258
64#define QEMU_NBD_OPT_DETECT_ZEROES 259
65#define QEMU_NBD_OPT_OBJECT 260
66#define QEMU_NBD_OPT_TLSCREDS 261
67#define QEMU_NBD_OPT_IMAGE_OPTS 262
Max Reitzffb31e12016-09-28 22:46:42 +020068#define QEMU_NBD_OPT_FORK 263
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +000069#define QEMU_NBD_OPT_TLSAUTHZ 264
Max Reitz637bc5a2019-05-08 23:18:16 +020070#define QEMU_NBD_OPT_PID_FILE 265
Richard W.M. Jones3d212b42021-11-15 14:29:43 -060071#define QEMU_NBD_OPT_SELINUX_LABEL 266
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +000072#define QEMU_NBD_OPT_TLSHOSTNAME 267
bellard7a5ca862008-05-27 21:13:40 +000073
Eric Blakebd31c212016-05-06 10:26:42 -060074#define MBR_SIZE 512
75
blueswir1b1d8e522008-10-26 13:43:07 +000076static int verbose;
Paolo Bonzinia517e882011-11-04 15:51:21 +010077static char *srcpath;
Markus Armbrusterbd269eb2017-04-26 09:36:41 +020078static SocketAddress *saddr;
Paolo Bonzini7860a382012-09-18 13:31:56 +020079static int persistent = 0;
Kevin Wolfd794f7f2020-09-24 17:26:56 +020080static enum { RUNNING, TERMINATE, TERMINATED } state;
Paolo Bonzinia61c6782011-09-12 17:28:11 +020081static int shared = 1;
82static int nb_fds;
Daniel P. Berrangee4849c12017-12-18 10:16:43 +000083static QIONetListener *server;
Daniel P. Berrange145614a2016-02-10 18:41:13 +000084static QCryptoTLSCreds *tlscreds;
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +000085static const char *tlsauthz;
bellard7a5ca862008-05-27 21:13:40 +000086
87static void usage(const char *name)
88{
Paolo Bonzinib033cd82012-07-18 14:50:52 +020089 (printf) (
bellard7a5ca862008-05-27 21:13:40 +000090"Usage: %s [OPTIONS] FILE\n"
Eric Blake68b96f12019-01-17 13:36:56 -060091" or: %s -L [OPTIONS]\n"
92"QEMU Disk Network Block Device Utility\n"
bellard7a5ca862008-05-27 21:13:40 +000093"\n"
Peter Lieven713cc672014-08-13 19:20:19 +020094" -h, --help display this help and exit\n"
95" -V, --version output version information and exit\n"
bellard7a5ca862008-05-27 21:13:40 +000096"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020097"Connection properties:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020098" -p, --port=PORT port to listen on (default `%d')\n"
99" -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
100" -k, --socket=PATH path to the unix socket\n"
101" (default '"SOCKET_PATH"')\n"
102" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
103" -t, --persistent don't exit on the last connection\n"
104" -v, --verbose display extra debugging information\n"
Vladimir Sementsov-Ogievskiyf5cd0bb2018-10-03 20:02:27 +0300105" -x, --export-name=NAME expose export by name (default is empty string)\n"
106" -D, --description=TEXT export a human-readable description\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200107"\n"
108"Exposing part of the image:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200109" -o, --offset=OFFSET offset into the image\n"
Eric Blakedbc7b012020-10-27 00:05:55 -0500110" -A, --allocation-depth expose the allocation depth\n"
Eric Blake636192c2019-01-11 13:47:20 -0600111" -B, --bitmap=NAME expose a persistent dirty bitmap\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200112"\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000113"General purpose options:\n"
Eric Blake68b96f12019-01-17 13:36:56 -0600114" -L, --list list exports available from another NBD server\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000115" --object type,id=ID,... define an object such as 'secret' for providing\n"
116" passwords and/or encryption keys\n"
Eric Blakef7812df2018-10-03 13:04:26 -0500117" --tls-creds=ID use id of an earlier --object to provide TLS\n"
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000118" --tls-authz=ID use id of an earlier --object to provide\n"
119" authorization\n"
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300120" -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
121" specify tracing options\n"
Max Reitzffb31e12016-09-28 22:46:42 +0200122" --fork fork off the server process and exit the parent\n"
123" once the server is running\n"
Max Reitz637bc5a2019-05-08 23:18:16 +0200124" --pid-file=PATH store the server's process ID in the given file\n"
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600125#ifdef CONFIG_SELINUX
126" --selinux-label=LABEL set SELinux process label on listening socket\n"
127#endif
Eric Blake3c1fa352018-12-15 07:53:08 -0600128#if HAVE_NBD_DEVICE
129"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200130"Kernel NBD client support:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200131" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
132" -d, --disconnect disconnect the specified device\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200133#endif
134"\n"
135"Block device options:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200136" -f, --format=FORMAT set image format (raw, qcow2, ...)\n"
137" -r, --read-only export read-only\n"
138" -s, --snapshot use FILE as an external snapshot, create a temporary\n"
139" file with backing_file=FILE, redirect the write to\n"
140" the temporary one\n"
Wenchao Xia8c116b02013-12-04 17:10:55 +0800141" -l, --load-snapshot=SNAPSHOT_PARAM\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200142" load an internal snapshot inside FILE and export it\n"
143" as an read-only device, SNAPSHOT_PARAM format is\n"
144" 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
145" '[ID_OR_NAME]'\n"
146" -n, --nocache disable host cache\n"
Nir Soffer09615252021-08-13 23:55:19 +0300147" --cache=MODE set cache mode used to access the disk image, the\n"
148" valid options are: 'none', 'writeback' (default),\n"
149" 'writethrough', 'directsync' and 'unsafe'\n"
Aarushi Mehta76802742020-01-20 14:18:56 +0000150" --aio=MODE set AIO mode (native, io_uring or threads)\n"
Peter Lievenb3838a42014-08-13 19:20:18 +0200151" --discard=MODE set discard mode (ignore, unmap)\n"
Andrey Korolyov6883de62015-07-31 22:12:54 +0300152" --detect-zeroes=MODE set detect-zeroes mode (off, on, unmap)\n"
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000153" --image-opts treat FILE as a full set of image options\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200154"\n"
Eric Blakef5048cb2017-08-03 11:33:53 -0500155QEMU_HELP_BOTTOM "\n"
Eric Blake68b96f12019-01-17 13:36:56 -0600156 , name, name, NBD_DEFAULT_PORT, "DEVICE");
bellard7a5ca862008-05-27 21:13:40 +0000157}
158
159static void version(const char *name)
160{
161 printf(
Thomas Huth7e563bf2018-02-15 12:06:47 +0100162"%s " QEMU_FULL_VERSION "\n"
bellard7a5ca862008-05-27 21:13:40 +0000163"Written by Anthony Liguori.\n"
164"\n"
Eric Blakebe377132017-07-21 08:50:46 -0500165QEMU_COPYRIGHT "\n"
bellard7a5ca862008-05-27 21:13:40 +0000166"This is free software; see the source for copying conditions. There is NO\n"
167"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
ths315bc7a2008-07-18 18:06:23 +0000168 , name);
bellard7a5ca862008-05-27 21:13:40 +0000169}
170
Eric Blake029a88c2020-09-30 07:11:01 -0500171#ifdef CONFIG_POSIX
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +0100172/*
173 * The client thread uses SIGTERM to interrupt the server. A signal
174 * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
175 */
176void qemu_system_killed(int signum, pid_t pid)
Paolo Bonzinibb345112011-11-04 15:51:19 +0100177{
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100178 qatomic_cmpxchg(&state, RUNNING, TERMINATE);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200179 qemu_notify_event();
Paolo Bonzinibb345112011-11-04 15:51:19 +0100180}
Eric Blake029a88c2020-09-30 07:11:01 -0500181#endif /* CONFIG_POSIX */
Paolo Bonzini537b41f2014-02-17 14:43:51 +0100182
Eric Blake68b96f12019-01-17 13:36:56 -0600183static int qemu_nbd_client_list(SocketAddress *saddr, QCryptoTLSCreds *tls,
184 const char *hostname)
185{
186 int ret = EXIT_FAILURE;
187 int rc;
188 Error *err = NULL;
189 QIOChannelSocket *sioc;
190 NBDExportInfo *list;
191 int i, j;
192
193 sioc = qio_channel_socket_new();
194 if (qio_channel_socket_connect_sync(sioc, saddr, &err) < 0) {
195 error_report_err(err);
Alex Chen992809b2020-11-30 12:36:51 +0000196 goto out;
Eric Blake68b96f12019-01-17 13:36:56 -0600197 }
198 rc = nbd_receive_export_list(QIO_CHANNEL(sioc), tls, hostname, &list,
199 &err);
200 if (rc < 0) {
201 if (err) {
202 error_report_err(err);
203 }
204 goto out;
205 }
206 printf("exports available: %d\n", rc);
207 for (i = 0; i < rc; i++) {
208 printf(" export: '%s'\n", list[i].name);
209 if (list[i].description && *list[i].description) {
210 printf(" description: %s\n", list[i].description);
211 }
212 if (list[i].flags & NBD_FLAG_HAS_FLAGS) {
Max Reitzc4e2aff2019-04-05 21:16:35 +0200213 static const char *const flag_names[] = {
214 [NBD_FLAG_READ_ONLY_BIT] = "readonly",
215 [NBD_FLAG_SEND_FLUSH_BIT] = "flush",
216 [NBD_FLAG_SEND_FUA_BIT] = "fua",
217 [NBD_FLAG_ROTATIONAL_BIT] = "rotational",
218 [NBD_FLAG_SEND_TRIM_BIT] = "trim",
219 [NBD_FLAG_SEND_WRITE_ZEROES_BIT] = "zeroes",
220 [NBD_FLAG_SEND_DF_BIT] = "df",
221 [NBD_FLAG_CAN_MULTI_CONN_BIT] = "multi",
222 [NBD_FLAG_SEND_RESIZE_BIT] = "resize",
223 [NBD_FLAG_SEND_CACHE_BIT] = "cache",
Eric Blake0a479542019-08-23 09:37:23 -0500224 [NBD_FLAG_SEND_FAST_ZERO_BIT] = "fast-zero",
Max Reitzc4e2aff2019-04-05 21:16:35 +0200225 };
226
Eric Blake68b96f12019-01-17 13:36:56 -0600227 printf(" size: %" PRIu64 "\n", list[i].size);
228 printf(" flags: 0x%x (", list[i].flags);
Max Reitzc4e2aff2019-04-05 21:16:35 +0200229 for (size_t bit = 0; bit < ARRAY_SIZE(flag_names); bit++) {
230 if (flag_names[bit] && (list[i].flags & (1 << bit))) {
231 printf(" %s", flag_names[bit]);
232 }
Eric Blake68b96f12019-01-17 13:36:56 -0600233 }
234 printf(" )\n");
235 }
236 if (list[i].min_block) {
237 printf(" min block: %u\n", list[i].min_block);
238 printf(" opt block: %u\n", list[i].opt_block);
239 printf(" max block: %u\n", list[i].max_block);
240 }
241 if (list[i].n_contexts) {
242 printf(" available meta contexts: %d\n", list[i].n_contexts);
243 for (j = 0; j < list[i].n_contexts; j++) {
244 printf(" %s\n", list[i].contexts[j]);
245 }
246 }
247 }
248 nbd_free_export_list(list, rc);
249
250 ret = EXIT_SUCCESS;
251 out:
252 object_unref(OBJECT(sioc));
253 return ret;
254}
255
256
Eric Blake3c1fa352018-12-15 07:53:08 -0600257#if HAVE_NBD_DEVICE
Paolo Bonzinia517e882011-11-04 15:51:21 +0100258static void *show_parts(void *arg)
thscd831bd2008-07-03 10:23:51 +0000259{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100260 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100261 int nbd;
thscd831bd2008-07-03 10:23:51 +0000262
Paolo Bonzinia517e882011-11-04 15:51:21 +0100263 /* linux just needs an open() to trigger
264 * the partition table update
265 * but remember to load the module with max_part != 0 :
266 * modprobe nbd max_part=63
267 */
268 nbd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100269 if (nbd >= 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100270 close(nbd);
thscd831bd2008-07-03 10:23:51 +0000271 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100272 return NULL;
273}
274
275static void *nbd_client_thread(void *arg)
276{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100277 char *device = arg;
Eric Blake6dc16672019-01-17 13:36:46 -0600278 NBDExportInfo info = { .request_sizes = false, .name = g_strdup("") };
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000279 QIOChannelSocket *sioc;
Alex Chenaf74b552020-12-08 13:49:44 +0000280 int fd = -1;
281 int ret = EXIT_FAILURE;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100282 pthread_t show_parts_thread;
Max Reitz1ce52842015-01-26 21:02:59 -0500283 Error *local_error = NULL;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100284
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000285 sioc = qio_channel_socket_new();
286 if (qio_channel_socket_connect_sync(sioc,
287 saddr,
288 &local_error) < 0) {
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100289 error_report_err(local_error);
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000290 goto out;
291 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100292
Alex Chenaf74b552020-12-08 13:49:44 +0000293 if (nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc),
294 NULL, NULL, NULL, &info, &local_error) < 0) {
Max Reitz1ce52842015-01-26 21:02:59 -0500295 if (local_error) {
Markus Armbruster78288672015-12-18 16:35:07 +0100296 error_report_err(local_error);
Max Reitz1ce52842015-01-26 21:02:59 -0500297 }
Alex Chenaf74b552020-12-08 13:49:44 +0000298 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100299 }
300
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100301 fd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100302 if (fd < 0) {
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100303 /* Linux-only, we can use %m in printf. */
Markus Armbrusterb9884682015-12-18 16:35:18 +0100304 error_report("Failed to open %s: %m", device);
Alex Chenaf74b552020-12-08 13:49:44 +0000305 goto out;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100306 }
307
Alex Chenaf74b552020-12-08 13:49:44 +0000308 if (nbd_init(fd, sioc, &info, &local_error) < 0) {
Vladimir Sementsov-Ogievskiybe41c102017-05-26 14:09:13 +0300309 error_report_err(local_error);
Alex Chenaf74b552020-12-08 13:49:44 +0000310 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100311 }
312
313 /* update partition table */
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100314 pthread_create(&show_parts_thread, NULL, show_parts, device);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100315
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100316 if (verbose) {
317 fprintf(stderr, "NBD device %s is now connected to %s\n",
318 device, srcpath);
319 } else {
320 /* Close stderr so that the qemu-nbd process exits. */
321 dup2(STDOUT_FILENO, STDERR_FILENO);
322 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100323
Alex Chenaf74b552020-12-08 13:49:44 +0000324 if (nbd_client(fd) < 0) {
325 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100326 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100327
Alex Chenaf74b552020-12-08 13:49:44 +0000328 ret = EXIT_SUCCESS;
329
330 out:
331 if (fd >= 0) {
332 close(fd);
333 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000334 object_unref(OBJECT(sioc));
Eric Blake6dc16672019-01-17 13:36:46 -0600335 g_free(info.name);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100336 kill(getpid(), SIGTERM);
Alex Chenaf74b552020-12-08 13:49:44 +0000337 return (void *) (intptr_t) ret;
thscd831bd2008-07-03 10:23:51 +0000338}
Eric Blake3c1fa352018-12-15 07:53:08 -0600339#endif /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000340
Fam Zhenge4afbf42015-05-19 10:50:59 +0000341static int nbd_can_accept(void)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200342{
Eric Blake3dcf56e2021-02-09 09:27:59 -0600343 return state == RUNNING && (shared == 0 || nb_fds < shared);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200344}
345
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000346static void nbd_update_server_watch(void);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000347
Eric Blake0c9390d2017-06-08 17:26:17 -0500348static void nbd_client_closed(NBDClient *client, bool negotiated)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200349{
Paolo Bonzini1743b512011-09-19 14:33:23 +0200350 nb_fds--;
Eric Blake0c9390d2017-06-08 17:26:17 -0500351 if (negotiated && nb_fds == 0 && !persistent && state == RUNNING) {
Paolo Bonzini7860a382012-09-18 13:31:56 +0200352 state = TERMINATE;
353 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000354 nbd_update_server_watch();
Paolo Bonzini7860a382012-09-18 13:31:56 +0200355 nbd_client_put(client);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200356}
357
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000358static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc,
359 gpointer opaque)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200360{
Paolo Bonzini7860a382012-09-18 13:31:56 +0200361 if (state >= TERMINATE) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000362 return;
Paolo Bonzini7860a382012-09-18 13:31:56 +0200363 }
364
Fam Zhengee7d7aa2016-01-14 16:41:01 +0800365 nb_fds++;
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000366 nbd_update_server_watch();
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000367 nbd_client_new(cioc, tlscreds, tlsauthz, nbd_client_closed);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200368}
369
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000370static void nbd_update_server_watch(void)
Fam Zhenge4afbf42015-05-19 10:50:59 +0000371{
372 if (nbd_can_accept()) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000373 qio_net_listener_set_client_func(server, nbd_accept, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000374 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000375 qio_net_listener_set_client_func(server, NULL, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000376 }
377}
378
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100379
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200380static SocketAddress *nbd_build_socket_address(const char *sockpath,
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100381 const char *bindto,
382 const char *port)
383{
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200384 SocketAddress *saddr;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100385
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200386 saddr = g_new0(SocketAddress, 1);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100387 if (sockpath) {
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200388 saddr->type = SOCKET_ADDRESS_TYPE_UNIX;
389 saddr->u.q_unix.path = g_strdup(sockpath);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100390 } else {
Eric Blake03992932016-03-03 09:16:48 -0700391 InetSocketAddress *inet;
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200392 saddr->type = SOCKET_ADDRESS_TYPE_INET;
393 inet = &saddr->u.inet;
Eric Blake03992932016-03-03 09:16:48 -0700394 inet->host = g_strdup(bindto);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100395 if (port) {
Eric Blake03992932016-03-03 09:16:48 -0700396 inet->port = g_strdup(port);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100397 } else {
Eric Blake03992932016-03-03 09:16:48 -0700398 inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100399 }
400 }
401
402 return saddr;
403}
404
405
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000406static QemuOptsList file_opts = {
407 .name = "file",
408 .implied_opt_name = "file",
409 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
410 .desc = {
411 /* no elements => accept any params */
412 { /* end of list */ }
413 },
414};
415
Eric Blake68b96f12019-01-17 13:36:56 -0600416static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, bool list,
417 Error **errp)
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000418{
419 Object *obj;
420 QCryptoTLSCreds *creds;
421
422 obj = object_resolve_path_component(
423 object_get_objects_root(), id);
424 if (!obj) {
425 error_setg(errp, "No TLS credentials with id '%s'",
426 id);
427 return NULL;
428 }
429 creds = (QCryptoTLSCreds *)
430 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
431 if (!creds) {
432 error_setg(errp, "Object with id '%s' is not TLS credentials",
433 id);
434 return NULL;
435 }
436
Philippe Mathieu-Daudé0279cd92021-06-28 18:09:10 +0200437 if (!qcrypto_tls_creds_check_endpoint(creds,
438 list
439 ? QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
440 : QCRYPTO_TLS_CREDS_ENDPOINT_SERVER,
441 errp)) {
442 return NULL;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000443 }
444 object_ref(obj);
445 return creds;
446}
447
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000448static void setup_address_and_port(const char **address, const char **port)
449{
450 if (*address == NULL) {
451 *address = "0.0.0.0";
452 }
453
454 if (*port == NULL) {
455 *port = stringify(NBD_DEFAULT_PORT);
456 }
457}
458
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000459/*
460 * Check socket parameters compatibility when socket activation is used.
461 */
462static const char *socket_activation_validate_opts(const char *device,
463 const char *sockpath,
464 const char *address,
Eric Blake68b96f12019-01-17 13:36:56 -0600465 const char *port,
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600466 const char *selinux,
Eric Blake68b96f12019-01-17 13:36:56 -0600467 bool list)
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000468{
469 if (device != NULL) {
470 return "NBD device can't be set when using socket activation";
471 }
472
473 if (sockpath != NULL) {
474 return "Unix socket can't be set when using socket activation";
475 }
476
477 if (address != NULL) {
478 return "The interface can't be set when using socket activation";
479 }
480
481 if (port != NULL) {
482 return "TCP port number can't be set when using socket activation";
483 }
484
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600485 if (selinux != NULL) {
486 return "SELinux label can't be set when using socket activation";
487 }
488
Eric Blake68b96f12019-01-17 13:36:56 -0600489 if (list) {
490 return "List mode is incompatible with socket activation";
491 }
492
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000493 return NULL;
494}
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000495
Kevin Wolfb3b52992018-05-16 13:46:37 +0200496static void qemu_nbd_shutdown(void)
497{
498 job_cancel_sync_all();
Sergio Lopez1895b972021-02-01 13:50:32 +0100499 blk_exp_close_all();
Kevin Wolfb3b52992018-05-16 13:46:37 +0200500 bdrv_close_all();
501}
502
bellard7a5ca862008-05-27 21:13:40 +0000503int main(int argc, char **argv)
504{
Markus Armbruster26f54e92014-10-07 13:59:04 +0200505 BlockBackend *blk;
bellard7a5ca862008-05-27 21:13:40 +0000506 BlockDriverState *bs;
Eric Blake9d26dfc2019-01-17 13:36:43 -0600507 uint64_t dev_offset = 0;
Eric Blakedbb38ca2019-08-23 09:37:22 -0500508 bool readonly = false;
thscd831bd2008-07-03 10:23:51 +0000509 bool disconnect = false;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000510 const char *bindto = NULL;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100511 const char *port = NULL;
512 char *sockpath = NULL;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100513 char *device = NULL;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800514 QemuOpts *sn_opts = NULL;
515 const char *sn_id_or_name = NULL;
Eric Blakedbc7b012020-10-27 00:05:55 -0500516 const char *sopt = "hVb:o:p:rsnc:dvk:e:f:tl:x:T:D:AB:L";
bellard7a5ca862008-05-27 21:13:40 +0000517 struct option lopt[] = {
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000518 { "help", no_argument, NULL, 'h' },
519 { "version", no_argument, NULL, 'V' },
520 { "bind", required_argument, NULL, 'b' },
521 { "port", required_argument, NULL, 'p' },
522 { "socket", required_argument, NULL, 'k' },
523 { "offset", required_argument, NULL, 'o' },
524 { "read-only", no_argument, NULL, 'r' },
Eric Blakedbc7b012020-10-27 00:05:55 -0500525 { "allocation-depth", no_argument, NULL, 'A' },
Eric Blake636192c2019-01-11 13:47:20 -0600526 { "bitmap", required_argument, NULL, 'B' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000527 { "connect", required_argument, NULL, 'c' },
528 { "disconnect", no_argument, NULL, 'd' },
Eric Blake68b96f12019-01-17 13:36:56 -0600529 { "list", no_argument, NULL, 'L' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000530 { "snapshot", no_argument, NULL, 's' },
531 { "load-snapshot", required_argument, NULL, 'l' },
532 { "nocache", no_argument, NULL, 'n' },
533 { "cache", required_argument, NULL, QEMU_NBD_OPT_CACHE },
534 { "aio", required_argument, NULL, QEMU_NBD_OPT_AIO },
535 { "discard", required_argument, NULL, QEMU_NBD_OPT_DISCARD },
536 { "detect-zeroes", required_argument, NULL,
537 QEMU_NBD_OPT_DETECT_ZEROES },
538 { "shared", required_argument, NULL, 'e' },
539 { "format", required_argument, NULL, 'f' },
540 { "persistent", no_argument, NULL, 't' },
541 { "verbose", no_argument, NULL, 'v' },
542 { "object", required_argument, NULL, QEMU_NBD_OPT_OBJECT },
543 { "export-name", required_argument, NULL, 'x' },
Eric Blakeb1a75b32016-10-14 13:33:03 -0500544 { "description", required_argument, NULL, 'D' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000545 { "tls-creds", required_argument, NULL, QEMU_NBD_OPT_TLSCREDS },
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000546 { "tls-hostname", required_argument, NULL, QEMU_NBD_OPT_TLSHOSTNAME },
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000547 { "tls-authz", required_argument, NULL, QEMU_NBD_OPT_TLSAUTHZ },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000548 { "image-opts", no_argument, NULL, QEMU_NBD_OPT_IMAGE_OPTS },
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300549 { "trace", required_argument, NULL, 'T' },
Max Reitzffb31e12016-09-28 22:46:42 +0200550 { "fork", no_argument, NULL, QEMU_NBD_OPT_FORK },
Max Reitz637bc5a2019-05-08 23:18:16 +0200551 { "pid-file", required_argument, NULL, QEMU_NBD_OPT_PID_FILE },
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600552 { "selinux-label", required_argument, NULL,
553 QEMU_NBD_OPT_SELINUX_LABEL },
Blue Swirl660f11b2009-07-31 21:16:51 +0000554 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000555 };
556 int ch;
557 int opt_ind = 0;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200558 int flags = BDRV_O_RDWR;
Max Reitz4fbec262015-02-05 13:58:19 -0500559 int ret = 0;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200560 bool seen_cache = false;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100561 bool seen_discard = false;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200562 bool seen_aio = false;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100563 pthread_t client_thread;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000564 const char *fmt = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200565 Error *local_err = NULL;
Peter Lievenb3838a42014-08-13 19:20:18 +0200566 BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
Max Reitz4fbec262015-02-05 13:58:19 -0500567 QDict *options = NULL;
Eric Blake68b96f12019-01-17 13:36:56 -0600568 const char *export_name = NULL; /* defaults to "" later for server mode */
Eric Blakeb1a75b32016-10-14 13:33:03 -0500569 const char *export_description = NULL;
Eric Blakecbad81c2020-10-27 00:05:49 -0500570 strList *bitmaps = NULL;
Eric Blakedbc7b012020-10-27 00:05:55 -0500571 bool alloc_depth = false;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000572 const char *tlscredsid = NULL;
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000573 const char *tlshostname = NULL;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000574 bool imageOpts = false;
Nir Soffer09615252021-08-13 23:55:19 +0300575 bool writethrough = false; /* Client will flush as needed. */
Max Reitzffb31e12016-09-28 22:46:42 +0200576 bool fork_process = false;
Eric Blake68b96f12019-01-17 13:36:56 -0600577 bool list = false;
Max Reitzffb31e12016-09-28 22:46:42 +0200578 int old_stderr = -1;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000579 unsigned socket_activation;
Max Reitz637bc5a2019-05-08 23:18:16 +0200580 const char *pid_file_name = NULL;
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600581 const char *selinux_label = NULL;
Kevin Wolf00917172020-09-24 17:26:57 +0200582 BlockExportOptions *export_opts;
bellard7a5ca862008-05-27 21:13:40 +0000583
Eric Blake029a88c2020-09-30 07:11:01 -0500584#ifdef CONFIG_POSIX
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +0100585 os_setup_early_signal_handling();
586 os_setup_signal_handling();
Max Reitz041e32b2017-06-11 14:37:14 +0200587#endif
588
Daniel P. Berrangé98c5d2e2020-08-25 11:38:48 +0100589 socket_init();
Christophe Fergeauf5852ef2019-01-31 17:46:14 +0100590 error_init(argv[0]);
Daniel P. Berrangefe4db842016-10-04 14:35:52 +0100591 module_call_init(MODULE_INIT_TRACE);
Eduardo Habkoste8f2d272016-05-12 11:10:04 -0300592 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100593
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000594 module_call_init(MODULE_INIT_QOM);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300595 qemu_add_opts(&qemu_trace_opts);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800596 qemu_init_exec_dir(argv[0]);
Paolo Bonzinibb345112011-11-04 15:51:19 +0100597
bellard7a5ca862008-05-27 21:13:40 +0000598 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
599 switch (ch) {
600 case 's':
ths2f726482008-07-03 11:47:46 +0000601 flags |= BDRV_O_SNAPSHOT;
602 break;
603 case 'n':
Paolo Bonzini39a52352012-07-18 14:57:15 +0200604 optarg = (char *) "none";
605 /* fallthrough */
606 case QEMU_NBD_OPT_CACHE:
607 if (seen_cache) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100608 error_report("-n and --cache can only be specified once");
609 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200610 }
611 seen_cache = true;
Kevin Wolf6effd5b2016-03-14 11:43:28 +0100612 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100613 error_report("Invalid cache mode `%s'", optarg);
614 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200615 }
bellard7a5ca862008-05-27 21:13:40 +0000616 break;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200617 case QEMU_NBD_OPT_AIO:
618 if (seen_aio) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100619 error_report("--aio can only be specified once");
620 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200621 }
622 seen_aio = true;
Aarushi Mehta76802742020-01-20 14:18:56 +0000623 if (bdrv_parse_aio(optarg, &flags) < 0) {
624 error_report("Invalid aio mode '%s'", optarg);
625 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200626 }
627 break;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100628 case QEMU_NBD_OPT_DISCARD:
629 if (seen_discard) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100630 error_report("--discard can only be specified once");
631 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100632 }
633 seen_discard = true;
634 if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100635 error_report("Invalid discard mode `%s'", optarg);
636 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100637 }
638 break;
Peter Lievenb3838a42014-08-13 19:20:18 +0200639 case QEMU_NBD_OPT_DETECT_ZEROES:
640 detect_zeroes =
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200641 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
Peter Lievenb3838a42014-08-13 19:20:18 +0200642 optarg,
Peter Lievenb3838a42014-08-13 19:20:18 +0200643 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
644 &local_err);
645 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100646 error_reportf_err(local_err,
647 "Failed to parse detect_zeroes mode: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100648 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200649 }
650 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
651 !(flags & BDRV_O_UNMAP)) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100652 error_report("setting detect-zeroes to unmap is not allowed "
653 "without setting discard operation to unmap");
654 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200655 }
656 break;
bellard7a5ca862008-05-27 21:13:40 +0000657 case 'b':
658 bindto = optarg;
659 break;
660 case 'p':
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100661 port = optarg;
bellard7a5ca862008-05-27 21:13:40 +0000662 break;
663 case 'o':
Eric Blake43b51012019-01-17 13:36:44 -0600664 if (qemu_strtou64(optarg, NULL, 0, &dev_offset) < 0) {
665 error_report("Invalid offset '%s'", optarg);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100666 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000667 }
bellard7a5ca862008-05-27 21:13:40 +0000668 break;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800669 case 'l':
670 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +0100671 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
672 optarg, false);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800673 if (!sn_opts) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100674 error_report("Failed in parsing snapshot param `%s'",
675 optarg);
676 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800677 }
678 } else {
679 sn_id_or_name = optarg;
680 }
681 /* fall through */
bellard7a5ca862008-05-27 21:13:40 +0000682 case 'r':
Eric Blakedbb38ca2019-08-23 09:37:22 -0500683 readonly = true;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200684 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000685 break;
Eric Blakedbc7b012020-10-27 00:05:55 -0500686 case 'A':
687 alloc_depth = true;
688 break;
Eric Blake636192c2019-01-11 13:47:20 -0600689 case 'B':
Eric Blakecbad81c2020-10-27 00:05:49 -0500690 QAPI_LIST_PREPEND(bitmaps, g_strdup(optarg));
Eric Blake636192c2019-01-11 13:47:20 -0600691 break;
thscd831bd2008-07-03 10:23:51 +0000692 case 'k':
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100693 sockpath = optarg;
Peter Lieven713cc672014-08-13 19:20:19 +0200694 if (sockpath[0] != '/') {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +0100695 error_report("socket path must be absolute");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100696 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200697 }
thscd831bd2008-07-03 10:23:51 +0000698 break;
699 case 'd':
700 disconnect = true;
701 break;
702 case 'c':
703 device = optarg;
704 break;
ths3b05a8e2008-07-03 12:45:02 +0000705 case 'e':
Eric Blake43b51012019-01-17 13:36:44 -0600706 if (qemu_strtoi(optarg, NULL, 0, &shared) < 0 ||
Eric Blake3dcf56e2021-02-09 09:27:59 -0600707 shared < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100708 error_report("Invalid shared device number '%s'", optarg);
709 exit(EXIT_FAILURE);
ths3b05a8e2008-07-03 12:45:02 +0000710 }
ths3b05a8e2008-07-03 12:45:02 +0000711 break;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000712 case 'f':
713 fmt = optarg;
714 break;
Peter Lieven713cc672014-08-13 19:20:19 +0200715 case 't':
716 persistent = 1;
717 break;
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000718 case 'x':
719 export_name = optarg;
Eric Blake93676c82019-11-13 20:46:34 -0600720 if (strlen(export_name) > NBD_MAX_STRING_SIZE) {
721 error_report("export name '%s' too long", export_name);
722 exit(EXIT_FAILURE);
723 }
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000724 break;
Eric Blakeb1a75b32016-10-14 13:33:03 -0500725 case 'D':
726 export_description = optarg;
Eric Blake93676c82019-11-13 20:46:34 -0600727 if (strlen(export_description) > NBD_MAX_STRING_SIZE) {
728 error_report("export description '%s' too long",
729 export_description);
730 exit(EXIT_FAILURE);
731 }
Eric Blakeb1a75b32016-10-14 13:33:03 -0500732 break;
bellard7a5ca862008-05-27 21:13:40 +0000733 case 'v':
734 verbose = 1;
735 break;
736 case 'V':
737 version(argv[0]);
738 exit(0);
739 break;
740 case 'h':
741 usage(argv[0]);
742 exit(0);
743 break;
744 case '?':
Markus Armbruster85b01e02015-12-18 16:35:04 +0100745 error_report("Try `%s --help' for more information.", argv[0]);
746 exit(EXIT_FAILURE);
Kevin Wolffa40e432021-02-17 12:56:45 +0100747 case QEMU_NBD_OPT_OBJECT:
748 user_creatable_process_cmdline(optarg);
749 break;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000750 case QEMU_NBD_OPT_TLSCREDS:
751 tlscredsid = optarg;
752 break;
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000753 case QEMU_NBD_OPT_TLSHOSTNAME:
754 tlshostname = optarg;
755 break;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000756 case QEMU_NBD_OPT_IMAGE_OPTS:
757 imageOpts = true;
758 break;
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300759 case 'T':
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500760 trace_opt_parse(optarg);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300761 break;
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000762 case QEMU_NBD_OPT_TLSAUTHZ:
763 tlsauthz = optarg;
764 break;
Max Reitzffb31e12016-09-28 22:46:42 +0200765 case QEMU_NBD_OPT_FORK:
766 fork_process = true;
767 break;
Eric Blake68b96f12019-01-17 13:36:56 -0600768 case 'L':
769 list = true;
770 break;
Max Reitz637bc5a2019-05-08 23:18:16 +0200771 case QEMU_NBD_OPT_PID_FILE:
772 pid_file_name = optarg;
773 break;
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600774 case QEMU_NBD_OPT_SELINUX_LABEL:
775 selinux_label = optarg;
776 break;
bellard7a5ca862008-05-27 21:13:40 +0000777 }
778 }
779
Eric Blake68b96f12019-01-17 13:36:56 -0600780 if (list) {
781 if (argc != optind) {
782 error_report("List mode is incompatible with a file name");
783 exit(EXIT_FAILURE);
784 }
Eric Blake0bc16992020-01-23 10:46:50 -0600785 if (export_name || export_description || dev_offset ||
Eric Blakecbad81c2020-10-27 00:05:49 -0500786 device || disconnect || fmt || sn_id_or_name || bitmaps ||
Eric Blakedbc7b012020-10-27 00:05:55 -0500787 alloc_depth || seen_aio || seen_discard || seen_cache) {
Eric Blake68b96f12019-01-17 13:36:56 -0600788 error_report("List mode is incompatible with per-device settings");
789 exit(EXIT_FAILURE);
790 }
791 if (fork_process) {
792 error_report("List mode is incompatible with forking");
793 exit(EXIT_FAILURE);
794 }
795 } else if ((argc - optind) != 1) {
Markus Armbruster433672b2015-12-18 16:35:24 +0100796 error_report("Invalid number of arguments");
797 error_printf("Try `%s --help' for more information.\n", argv[0]);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100798 exit(EXIT_FAILURE);
Eric Blake68b96f12019-01-17 13:36:56 -0600799 } else if (!export_name) {
800 export_name = "";
bellard7a5ca862008-05-27 21:13:40 +0000801 }
802
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300803 if (!trace_init_backends()) {
804 exit(1);
805 }
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500806 trace_init_file();
Richard Hendersonc5955f42022-04-17 11:29:44 -0700807 qemu_set_log(LOG_TRACE, &error_fatal);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300808
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000809 socket_activation = check_socket_activation();
810 if (socket_activation == 0) {
Daniel P. Berrangée8ae8b12022-03-04 19:36:03 +0000811 if (!sockpath) {
812 setup_address_and_port(&bindto, &port);
813 }
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000814 } else {
815 /* Using socket activation - check user didn't use -p etc. */
816 const char *err_msg = socket_activation_validate_opts(device, sockpath,
Eric Blake68b96f12019-01-17 13:36:56 -0600817 bindto, port,
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600818 selinux_label,
Eric Blake68b96f12019-01-17 13:36:56 -0600819 list);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000820 if (err_msg != NULL) {
821 error_report("%s", err_msg);
822 exit(EXIT_FAILURE);
823 }
Paolo Bonzini53fabd42017-03-16 16:29:45 +0100824
825 /* qemu-nbd can only listen on a single socket. */
826 if (socket_activation > 1) {
827 error_report("qemu-nbd does not support socket activation with %s > 1",
828 "LISTEN_FDS");
829 exit(EXIT_FAILURE);
830 }
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000831 }
832
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000833 if (tlscredsid) {
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000834 if (device) {
835 error_report("TLS is not supported with a host device");
836 exit(EXIT_FAILURE);
837 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000838 if (tlsauthz && list) {
839 error_report("TLS authorization is incompatible with export list");
840 exit(EXIT_FAILURE);
841 }
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000842 if (tlshostname && !list) {
843 error_report("TLS hostname is only supported with export list");
844 exit(EXIT_FAILURE);
845 }
Eric Blake68b96f12019-01-17 13:36:56 -0600846 tlscreds = nbd_get_tls_creds(tlscredsid, list, &local_err);
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000847 if (local_err) {
Markus Armbruster5217f182020-05-05 12:19:03 +0200848 error_reportf_err(local_err, "Failed to get TLS creds: ");
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000849 exit(EXIT_FAILURE);
850 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000851 } else {
852 if (tlsauthz) {
853 error_report("--tls-authz is not permitted without --tls-creds");
854 exit(EXIT_FAILURE);
855 }
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000856 if (tlshostname) {
857 error_report("--tls-hostname is not permitted without --tls-creds");
858 exit(EXIT_FAILURE);
859 }
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000860 }
861
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600862 if (selinux_label) {
863#ifdef CONFIG_SELINUX
864 if (sockpath == NULL && device == NULL) {
865 error_report("--selinux-label is not permitted without --socket");
866 exit(EXIT_FAILURE);
867 }
868#else
869 error_report("SELinux support not enabled in this binary");
870 exit(EXIT_FAILURE);
871#endif
872 }
873
Eric Blake68b96f12019-01-17 13:36:56 -0600874 if (list) {
875 saddr = nbd_build_socket_address(sockpath, bindto, port);
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000876 return qemu_nbd_client_list(saddr, tlscreds,
877 tlshostname ? tlshostname : bindto);
Eric Blake68b96f12019-01-17 13:36:56 -0600878 }
879
Eric Blake3c1fa352018-12-15 07:53:08 -0600880#if !HAVE_NBD_DEVICE
881 if (disconnect || device) {
882 error_report("Kernel /dev/nbdN support not available");
883 exit(EXIT_FAILURE);
884 }
885#else /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000886 if (disconnect) {
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000887 int nbdfd = open(argv[optind], O_RDWR);
888 if (nbdfd < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100889 error_report("Cannot open %s: %s", argv[optind],
890 strerror(errno));
891 exit(EXIT_FAILURE);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100892 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000893 nbd_disconnect(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000894
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000895 close(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000896
897 printf("%s disconnected\n", argv[optind]);
898
Peter Lieven713cc672014-08-13 19:20:19 +0200899 return 0;
thscd831bd2008-07-03 10:23:51 +0000900 }
Eric Blake3c1fa352018-12-15 07:53:08 -0600901#endif
thscd831bd2008-07-03 10:23:51 +0000902
Max Reitzffb31e12016-09-28 22:46:42 +0200903 if ((device && !verbose) || fork_process) {
Daniel P. Berrangéeb705982020-08-25 11:38:50 +0100904#ifndef WIN32
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100905 int stderr_fd[2];
906 pid_t pid;
907 int ret;
908
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100909 if (qemu_pipe(stderr_fd) < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100910 error_report("Error setting up communication pipe: %s",
911 strerror(errno));
912 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100913 }
914
915 /* Now daemonize, but keep a communication channel open to
916 * print errors and exit with the proper status code.
917 */
918 pid = fork();
Max Reitz70d47392015-02-25 13:08:22 -0500919 if (pid < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100920 error_report("Failed to fork: %s", strerror(errno));
921 exit(EXIT_FAILURE);
Max Reitz70d47392015-02-25 13:08:22 -0500922 } else if (pid == 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100923 close(stderr_fd[0]);
Max Reitze6df58a2019-05-08 23:18:18 +0200924
Raphael Pour0eaf4532020-05-15 08:36:07 +0200925 /* Remember parent's stderr if we will be restoring it. */
926 if (fork_process) {
927 old_stderr = dup(STDERR_FILENO);
928 }
929
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400930 ret = qemu_daemon(1, 0);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100931
932 /* Temporarily redirect stderr to the parent's pipe... */
933 dup2(stderr_fd[1], STDERR_FILENO);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100934 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100935 error_report("Failed to daemonize: %s", strerror(errno));
936 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100937 }
938
939 /* ... close the descriptor we inherited and go on. */
940 close(stderr_fd[1]);
941 } else {
942 bool errors = false;
943 char *buf;
944
945 /* In the parent. Print error messages from the child until
946 * it closes the pipe.
947 */
948 close(stderr_fd[1]);
949 buf = g_malloc(1024);
950 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
951 errors = true;
952 ret = qemu_write_full(STDERR_FILENO, buf, ret);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100953 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100954 exit(EXIT_FAILURE);
955 }
956 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100957 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100958 error_report("Cannot read from daemon: %s",
959 strerror(errno));
960 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100961 }
962
963 /* Usually the daemon should not print any message.
964 * Exit with zero status in that case.
965 */
966 exit(errors);
967 }
Daniel P. Berrangéeb705982020-08-25 11:38:50 +0100968#else /* WIN32 */
969 error_report("Unable to fork into background on Windows hosts");
970 exit(EXIT_FAILURE);
971#endif /* WIN32 */
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100972 }
973
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100974 if (device != NULL && sockpath == NULL) {
975 sockpath = g_malloc(128);
976 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
thscd831bd2008-07-03 10:23:51 +0000977 }
978
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000979 server = qio_net_listener_new();
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000980 if (socket_activation == 0) {
Eric Blake582d4212021-02-09 09:27:58 -0600981 int backlog;
982
Eric Blake3dcf56e2021-02-09 09:27:59 -0600983 if (persistent || shared == 0) {
Eric Blake582d4212021-02-09 09:27:58 -0600984 backlog = SOMAXCONN;
985 } else {
986 backlog = MIN(shared, SOMAXCONN);
987 }
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600988#ifdef CONFIG_SELINUX
989 if (selinux_label && setsockcreatecon_raw(selinux_label) == -1) {
990 error_report("Cannot set SELinux socket create context to %s: %s",
991 selinux_label, strerror(errno));
992 exit(EXIT_FAILURE);
993 }
994#endif
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000995 saddr = nbd_build_socket_address(sockpath, bindto, port);
Eric Blake582d4212021-02-09 09:27:58 -0600996 if (qio_net_listener_open_sync(server, saddr, backlog,
997 &local_err) < 0) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000998 object_unref(OBJECT(server));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000999 error_report_err(local_err);
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001000 exit(EXIT_FAILURE);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001001 }
Richard W.M. Jones3d212b42021-11-15 14:29:43 -06001002#ifdef CONFIG_SELINUX
1003 if (selinux_label && setsockcreatecon_raw(NULL) == -1) {
1004 error_report("Cannot clear SELinux socket create context: %s",
1005 strerror(errno));
1006 exit(EXIT_FAILURE);
1007 }
1008#endif
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001009 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001010 size_t i;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001011 /* See comment in check_socket_activation above. */
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001012 for (i = 0; i < socket_activation; i++) {
1013 QIOChannelSocket *sioc;
1014 sioc = qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD + i,
1015 &local_err);
1016 if (sioc == NULL) {
1017 object_unref(OBJECT(server));
Markus Armbruster5217f182020-05-05 12:19:03 +02001018 error_reportf_err(local_err,
1019 "Failed to use socket activation: ");
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001020 exit(EXIT_FAILURE);
1021 }
1022 qio_net_listener_add(server, sioc);
1023 object_unref(OBJECT(sioc));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001024 }
1025 }
Daniel P. Berrange48bec072015-09-16 14:52:23 +01001026
Markus Armbrusterf9734d52021-07-20 14:53:53 +02001027 qemu_init_main_loop(&error_fatal);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001028 bdrv_init();
Kevin Wolfb3b52992018-05-16 13:46:37 +02001029 atexit(qemu_nbd_shutdown);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001030
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001031 srcpath = argv[optind];
1032 if (imageOpts) {
1033 QemuOpts *opts;
1034 if (fmt) {
1035 error_report("--image-opts and -f are mutually exclusive");
1036 exit(EXIT_FAILURE);
1037 }
1038 opts = qemu_opts_parse_noisily(&file_opts, srcpath, true);
1039 if (!opts) {
1040 qemu_opts_reset(&file_opts);
1041 exit(EXIT_FAILURE);
1042 }
1043 options = qemu_opts_to_qdict(opts, NULL);
1044 qemu_opts_reset(&file_opts);
Max Reitzefaa7c42016-03-16 19:54:38 +01001045 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001046 } else {
1047 if (fmt) {
1048 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05001049 qdict_put_str(options, "driver", fmt);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001050 }
Max Reitzefaa7c42016-03-16 19:54:38 +01001051 blk = blk_new_open(srcpath, NULL, options, flags, &local_err);
Daniel P. Berrangee6b63672013-03-19 11:20:20 +00001052 }
1053
Max Reitz4fbec262015-02-05 13:58:19 -05001054 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001055 error_reportf_err(local_err, "Failed to blk_new_open '%s': ",
1056 argv[optind]);
Markus Armbruster85b01e02015-12-18 16:35:04 +01001057 exit(EXIT_FAILURE);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001058 }
Max Reitz4fbec262015-02-05 13:58:19 -05001059 bs = blk_bs(blk);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001060
Kevin Wolfb57e4de2020-09-24 17:26:52 +02001061 if (dev_offset) {
1062 QDict *raw_opts = qdict_new();
1063 qdict_put_str(raw_opts, "driver", "raw");
1064 qdict_put_str(raw_opts, "file", bs->node_name);
1065 qdict_put_int(raw_opts, "offset", dev_offset);
1066 bs = bdrv_open(NULL, NULL, raw_opts, flags, &error_fatal);
1067 blk_remove_bs(blk);
1068 blk_insert_bs(blk, bs, &error_fatal);
1069 bdrv_unref(bs);
1070 }
1071
Kevin Wolf6effd5b2016-03-14 11:43:28 +01001072 blk_set_enable_write_cache(blk, !writethrough);
1073
Wenchao Xia8c116b02013-12-04 17:10:55 +08001074 if (sn_opts) {
1075 ret = bdrv_snapshot_load_tmp(bs,
1076 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1077 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1078 &local_err);
1079 } else if (sn_id_or_name) {
1080 ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
1081 &local_err);
1082 }
1083 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001084 error_reportf_err(local_err, "Failed to load snapshot: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +01001085 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001086 }
1087
Peter Lievenb3838a42014-08-13 19:20:18 +02001088 bs->detect_zeroes = detect_zeroes;
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001089
Kevin Wolf00917172020-09-24 17:26:57 +02001090 nbd_server_is_qemu_nbd(true);
1091
1092 export_opts = g_new(BlockExportOptions, 1);
1093 *export_opts = (BlockExportOptions) {
1094 .type = BLOCK_EXPORT_TYPE_NBD,
Kevin Wolfd53be9c2020-09-24 17:27:04 +02001095 .id = g_strdup("qemu-nbd-export"),
Kevin Wolfb6076af2020-09-24 17:27:01 +02001096 .node_name = g_strdup(bdrv_get_node_name(bs)),
Kevin Wolf00917172020-09-24 17:26:57 +02001097 .has_writethrough = true,
1098 .writethrough = writethrough,
Kevin Wolf30dbc812020-09-24 17:27:11 +02001099 .has_writable = true,
1100 .writable = !readonly,
Kevin Wolf00917172020-09-24 17:26:57 +02001101 .u.nbd = {
Eric Blakecbad81c2020-10-27 00:05:49 -05001102 .has_name = true,
1103 .name = g_strdup(export_name),
1104 .has_description = !!export_description,
1105 .description = g_strdup(export_description),
1106 .has_bitmaps = !!bitmaps,
1107 .bitmaps = bitmaps,
Eric Blakedbc7b012020-10-27 00:05:55 -05001108 .has_allocation_depth = alloc_depth,
1109 .allocation_depth = alloc_depth,
Kevin Wolf00917172020-09-24 17:26:57 +02001110 },
1111 };
1112 blk_exp_add(export_opts, &error_fatal);
1113 qapi_free_BlockExportOptions(export_opts);
ths3b05a8e2008-07-03 12:45:02 +00001114
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001115 if (device) {
Eric Blake3c1fa352018-12-15 07:53:08 -06001116#if HAVE_NBD_DEVICE
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001117 int ret;
1118
Paolo Bonzinia6ac2312011-12-06 09:07:00 +01001119 ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001120 if (ret != 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001121 error_report("Failed to create client thread: %s", strerror(ret));
1122 exit(EXIT_FAILURE);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001123 }
Eric Blake3c1fa352018-12-15 07:53:08 -06001124#endif
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001125 } else {
1126 /* Shut up GCC warnings. */
1127 memset(&client_thread, 0, sizeof(client_thread));
1128 }
1129
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +00001130 nbd_update_server_watch();
bellard7a5ca862008-05-27 21:13:40 +00001131
Max Reitz637bc5a2019-05-08 23:18:16 +02001132 if (pid_file_name) {
1133 qemu_write_pidfile(pid_file_name, &error_fatal);
1134 }
1135
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001136 /* now when the initialization is (almost) complete, chdir("/")
1137 * to free any busy filesystems */
1138 if (chdir("/") < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001139 error_report("Could not chdir to root directory: %s",
1140 strerror(errno));
1141 exit(EXIT_FAILURE);
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001142 }
1143
Max Reitzffb31e12016-09-28 22:46:42 +02001144 if (fork_process) {
1145 dup2(old_stderr, STDERR_FILENO);
1146 close(old_stderr);
1147 }
1148
Paolo Bonzini7860a382012-09-18 13:31:56 +02001149 state = RUNNING;
ths3b05a8e2008-07-03 12:45:02 +00001150 do {
Paolo Bonzinia61c6782011-09-12 17:28:11 +02001151 main_loop_wait(false);
Paolo Bonzini7860a382012-09-18 13:31:56 +02001152 if (state == TERMINATE) {
Kevin Wolfbc4ee652020-09-24 17:27:03 +02001153 blk_exp_close_all();
Kevin Wolfd794f7f2020-09-24 17:26:56 +02001154 state = TERMINATED;
Paolo Bonzini7860a382012-09-18 13:31:56 +02001155 }
1156 } while (state != TERMINATED);
ths3b05a8e2008-07-03 12:45:02 +00001157
Markus Armbruster26f54e92014-10-07 13:59:04 +02001158 blk_unref(blk);
Paolo Bonzinib32f6c22011-11-04 15:51:20 +01001159 if (sockpath) {
1160 unlink(sockpath);
1161 }
bellard7a5ca862008-05-27 21:13:40 +00001162
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02001163 qemu_opts_del(sn_opts);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001164
Paolo Bonzinia517e882011-11-04 15:51:21 +01001165 if (device) {
1166 void *ret;
1167 pthread_join(client_thread, &ret);
1168 exit(ret != NULL);
1169 } else {
1170 exit(EXIT_SUCCESS);
1171 }
bellard7a5ca862008-05-27 21:13:40 +00001172}