blob: c6c20df68a4dcb49bd47d02bcb5a674e8b9ec729 [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
bellard7a5ca862008-05-27 21:13:40 +000072
Eric Blakebd31c212016-05-06 10:26:42 -060073#define MBR_SIZE 512
74
blueswir1b1d8e522008-10-26 13:43:07 +000075static int verbose;
Paolo Bonzinia517e882011-11-04 15:51:21 +010076static char *srcpath;
Markus Armbrusterbd269eb2017-04-26 09:36:41 +020077static SocketAddress *saddr;
Paolo Bonzini7860a382012-09-18 13:31:56 +020078static int persistent = 0;
Kevin Wolfd794f7f2020-09-24 17:26:56 +020079static enum { RUNNING, TERMINATE, TERMINATED } state;
Paolo Bonzinia61c6782011-09-12 17:28:11 +020080static int shared = 1;
81static int nb_fds;
Daniel P. Berrangee4849c12017-12-18 10:16:43 +000082static QIONetListener *server;
Daniel P. Berrange145614a2016-02-10 18:41:13 +000083static QCryptoTLSCreds *tlscreds;
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +000084static const char *tlsauthz;
bellard7a5ca862008-05-27 21:13:40 +000085
86static void usage(const char *name)
87{
Paolo Bonzinib033cd82012-07-18 14:50:52 +020088 (printf) (
bellard7a5ca862008-05-27 21:13:40 +000089"Usage: %s [OPTIONS] FILE\n"
Eric Blake68b96f12019-01-17 13:36:56 -060090" or: %s -L [OPTIONS]\n"
91"QEMU Disk Network Block Device Utility\n"
bellard7a5ca862008-05-27 21:13:40 +000092"\n"
Peter Lieven713cc672014-08-13 19:20:19 +020093" -h, --help display this help and exit\n"
94" -V, --version output version information and exit\n"
bellard7a5ca862008-05-27 21:13:40 +000095"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020096"Connection properties:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020097" -p, --port=PORT port to listen on (default `%d')\n"
98" -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
99" -k, --socket=PATH path to the unix socket\n"
100" (default '"SOCKET_PATH"')\n"
101" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
102" -t, --persistent don't exit on the last connection\n"
103" -v, --verbose display extra debugging information\n"
Vladimir Sementsov-Ogievskiyf5cd0bb2018-10-03 20:02:27 +0300104" -x, --export-name=NAME expose export by name (default is empty string)\n"
105" -D, --description=TEXT export a human-readable description\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200106"\n"
107"Exposing part of the image:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200108" -o, --offset=OFFSET offset into the image\n"
Eric Blakedbc7b012020-10-27 00:05:55 -0500109" -A, --allocation-depth expose the allocation depth\n"
Eric Blake636192c2019-01-11 13:47:20 -0600110" -B, --bitmap=NAME expose a persistent dirty bitmap\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200111"\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000112"General purpose options:\n"
Eric Blake68b96f12019-01-17 13:36:56 -0600113" -L, --list list exports available from another NBD server\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000114" --object type,id=ID,... define an object such as 'secret' for providing\n"
115" passwords and/or encryption keys\n"
Eric Blakef7812df2018-10-03 13:04:26 -0500116" --tls-creds=ID use id of an earlier --object to provide TLS\n"
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000117" --tls-authz=ID use id of an earlier --object to provide\n"
118" authorization\n"
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300119" -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
120" specify tracing options\n"
Max Reitzffb31e12016-09-28 22:46:42 +0200121" --fork fork off the server process and exit the parent\n"
122" once the server is running\n"
Max Reitz637bc5a2019-05-08 23:18:16 +0200123" --pid-file=PATH store the server's process ID in the given file\n"
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600124#ifdef CONFIG_SELINUX
125" --selinux-label=LABEL set SELinux process label on listening socket\n"
126#endif
Eric Blake3c1fa352018-12-15 07:53:08 -0600127#if HAVE_NBD_DEVICE
128"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200129"Kernel NBD client support:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200130" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
131" -d, --disconnect disconnect the specified device\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200132#endif
133"\n"
134"Block device options:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200135" -f, --format=FORMAT set image format (raw, qcow2, ...)\n"
136" -r, --read-only export read-only\n"
137" -s, --snapshot use FILE as an external snapshot, create a temporary\n"
138" file with backing_file=FILE, redirect the write to\n"
139" the temporary one\n"
Wenchao Xia8c116b02013-12-04 17:10:55 +0800140" -l, --load-snapshot=SNAPSHOT_PARAM\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200141" load an internal snapshot inside FILE and export it\n"
142" as an read-only device, SNAPSHOT_PARAM format is\n"
143" 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
144" '[ID_OR_NAME]'\n"
145" -n, --nocache disable host cache\n"
Nir Soffer09615252021-08-13 23:55:19 +0300146" --cache=MODE set cache mode used to access the disk image, the\n"
147" valid options are: 'none', 'writeback' (default),\n"
148" 'writethrough', 'directsync' and 'unsafe'\n"
Aarushi Mehta76802742020-01-20 14:18:56 +0000149" --aio=MODE set AIO mode (native, io_uring or threads)\n"
Peter Lievenb3838a42014-08-13 19:20:18 +0200150" --discard=MODE set discard mode (ignore, unmap)\n"
Andrey Korolyov6883de62015-07-31 22:12:54 +0300151" --detect-zeroes=MODE set detect-zeroes mode (off, on, unmap)\n"
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000152" --image-opts treat FILE as a full set of image options\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200153"\n"
Eric Blakef5048cb2017-08-03 11:33:53 -0500154QEMU_HELP_BOTTOM "\n"
Eric Blake68b96f12019-01-17 13:36:56 -0600155 , name, name, NBD_DEFAULT_PORT, "DEVICE");
bellard7a5ca862008-05-27 21:13:40 +0000156}
157
158static void version(const char *name)
159{
160 printf(
Thomas Huth7e563bf2018-02-15 12:06:47 +0100161"%s " QEMU_FULL_VERSION "\n"
bellard7a5ca862008-05-27 21:13:40 +0000162"Written by Anthony Liguori.\n"
163"\n"
Eric Blakebe377132017-07-21 08:50:46 -0500164QEMU_COPYRIGHT "\n"
bellard7a5ca862008-05-27 21:13:40 +0000165"This is free software; see the source for copying conditions. There is NO\n"
166"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
ths315bc7a2008-07-18 18:06:23 +0000167 , name);
bellard7a5ca862008-05-27 21:13:40 +0000168}
169
Eric Blake029a88c2020-09-30 07:11:01 -0500170#ifdef CONFIG_POSIX
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +0100171/*
172 * The client thread uses SIGTERM to interrupt the server. A signal
173 * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
174 */
175void qemu_system_killed(int signum, pid_t pid)
Paolo Bonzinibb345112011-11-04 15:51:19 +0100176{
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100177 qatomic_cmpxchg(&state, RUNNING, TERMINATE);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200178 qemu_notify_event();
Paolo Bonzinibb345112011-11-04 15:51:19 +0100179}
Eric Blake029a88c2020-09-30 07:11:01 -0500180#endif /* CONFIG_POSIX */
Paolo Bonzini537b41f2014-02-17 14:43:51 +0100181
Eric Blake68b96f12019-01-17 13:36:56 -0600182static int qemu_nbd_client_list(SocketAddress *saddr, QCryptoTLSCreds *tls,
183 const char *hostname)
184{
185 int ret = EXIT_FAILURE;
186 int rc;
187 Error *err = NULL;
188 QIOChannelSocket *sioc;
189 NBDExportInfo *list;
190 int i, j;
191
192 sioc = qio_channel_socket_new();
193 if (qio_channel_socket_connect_sync(sioc, saddr, &err) < 0) {
194 error_report_err(err);
Alex Chen992809b2020-11-30 12:36:51 +0000195 goto out;
Eric Blake68b96f12019-01-17 13:36:56 -0600196 }
197 rc = nbd_receive_export_list(QIO_CHANNEL(sioc), tls, hostname, &list,
198 &err);
199 if (rc < 0) {
200 if (err) {
201 error_report_err(err);
202 }
203 goto out;
204 }
205 printf("exports available: %d\n", rc);
206 for (i = 0; i < rc; i++) {
207 printf(" export: '%s'\n", list[i].name);
208 if (list[i].description && *list[i].description) {
209 printf(" description: %s\n", list[i].description);
210 }
211 if (list[i].flags & NBD_FLAG_HAS_FLAGS) {
Max Reitzc4e2aff2019-04-05 21:16:35 +0200212 static const char *const flag_names[] = {
213 [NBD_FLAG_READ_ONLY_BIT] = "readonly",
214 [NBD_FLAG_SEND_FLUSH_BIT] = "flush",
215 [NBD_FLAG_SEND_FUA_BIT] = "fua",
216 [NBD_FLAG_ROTATIONAL_BIT] = "rotational",
217 [NBD_FLAG_SEND_TRIM_BIT] = "trim",
218 [NBD_FLAG_SEND_WRITE_ZEROES_BIT] = "zeroes",
219 [NBD_FLAG_SEND_DF_BIT] = "df",
220 [NBD_FLAG_CAN_MULTI_CONN_BIT] = "multi",
221 [NBD_FLAG_SEND_RESIZE_BIT] = "resize",
222 [NBD_FLAG_SEND_CACHE_BIT] = "cache",
Eric Blake0a479542019-08-23 09:37:23 -0500223 [NBD_FLAG_SEND_FAST_ZERO_BIT] = "fast-zero",
Max Reitzc4e2aff2019-04-05 21:16:35 +0200224 };
225
Eric Blake68b96f12019-01-17 13:36:56 -0600226 printf(" size: %" PRIu64 "\n", list[i].size);
227 printf(" flags: 0x%x (", list[i].flags);
Max Reitzc4e2aff2019-04-05 21:16:35 +0200228 for (size_t bit = 0; bit < ARRAY_SIZE(flag_names); bit++) {
229 if (flag_names[bit] && (list[i].flags & (1 << bit))) {
230 printf(" %s", flag_names[bit]);
231 }
Eric Blake68b96f12019-01-17 13:36:56 -0600232 }
233 printf(" )\n");
234 }
235 if (list[i].min_block) {
236 printf(" min block: %u\n", list[i].min_block);
237 printf(" opt block: %u\n", list[i].opt_block);
238 printf(" max block: %u\n", list[i].max_block);
239 }
240 if (list[i].n_contexts) {
241 printf(" available meta contexts: %d\n", list[i].n_contexts);
242 for (j = 0; j < list[i].n_contexts; j++) {
243 printf(" %s\n", list[i].contexts[j]);
244 }
245 }
246 }
247 nbd_free_export_list(list, rc);
248
249 ret = EXIT_SUCCESS;
250 out:
251 object_unref(OBJECT(sioc));
252 return ret;
253}
254
255
Eric Blake3c1fa352018-12-15 07:53:08 -0600256#if HAVE_NBD_DEVICE
Paolo Bonzinia517e882011-11-04 15:51:21 +0100257static void *show_parts(void *arg)
thscd831bd2008-07-03 10:23:51 +0000258{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100259 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100260 int nbd;
thscd831bd2008-07-03 10:23:51 +0000261
Paolo Bonzinia517e882011-11-04 15:51:21 +0100262 /* linux just needs an open() to trigger
263 * the partition table update
264 * but remember to load the module with max_part != 0 :
265 * modprobe nbd max_part=63
266 */
267 nbd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100268 if (nbd >= 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100269 close(nbd);
thscd831bd2008-07-03 10:23:51 +0000270 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100271 return NULL;
272}
273
274static void *nbd_client_thread(void *arg)
275{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100276 char *device = arg;
Eric Blake6dc16672019-01-17 13:36:46 -0600277 NBDExportInfo info = { .request_sizes = false, .name = g_strdup("") };
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000278 QIOChannelSocket *sioc;
Alex Chenaf74b552020-12-08 13:49:44 +0000279 int fd = -1;
280 int ret = EXIT_FAILURE;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100281 pthread_t show_parts_thread;
Max Reitz1ce52842015-01-26 21:02:59 -0500282 Error *local_error = NULL;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100283
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000284 sioc = qio_channel_socket_new();
285 if (qio_channel_socket_connect_sync(sioc,
286 saddr,
287 &local_error) < 0) {
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100288 error_report_err(local_error);
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000289 goto out;
290 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100291
Alex Chenaf74b552020-12-08 13:49:44 +0000292 if (nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc),
293 NULL, NULL, NULL, &info, &local_error) < 0) {
Max Reitz1ce52842015-01-26 21:02:59 -0500294 if (local_error) {
Markus Armbruster78288672015-12-18 16:35:07 +0100295 error_report_err(local_error);
Max Reitz1ce52842015-01-26 21:02:59 -0500296 }
Alex Chenaf74b552020-12-08 13:49:44 +0000297 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100298 }
299
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100300 fd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100301 if (fd < 0) {
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100302 /* Linux-only, we can use %m in printf. */
Markus Armbrusterb9884682015-12-18 16:35:18 +0100303 error_report("Failed to open %s: %m", device);
Alex Chenaf74b552020-12-08 13:49:44 +0000304 goto out;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100305 }
306
Alex Chenaf74b552020-12-08 13:49:44 +0000307 if (nbd_init(fd, sioc, &info, &local_error) < 0) {
Vladimir Sementsov-Ogievskiybe41c102017-05-26 14:09:13 +0300308 error_report_err(local_error);
Alex Chenaf74b552020-12-08 13:49:44 +0000309 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100310 }
311
312 /* update partition table */
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100313 pthread_create(&show_parts_thread, NULL, show_parts, device);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100314
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100315 if (verbose) {
316 fprintf(stderr, "NBD device %s is now connected to %s\n",
317 device, srcpath);
318 } else {
319 /* Close stderr so that the qemu-nbd process exits. */
320 dup2(STDOUT_FILENO, STDERR_FILENO);
321 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100322
Alex Chenaf74b552020-12-08 13:49:44 +0000323 if (nbd_client(fd) < 0) {
324 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100325 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100326
Alex Chenaf74b552020-12-08 13:49:44 +0000327 ret = EXIT_SUCCESS;
328
329 out:
330 if (fd >= 0) {
331 close(fd);
332 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000333 object_unref(OBJECT(sioc));
Eric Blake6dc16672019-01-17 13:36:46 -0600334 g_free(info.name);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100335 kill(getpid(), SIGTERM);
Alex Chenaf74b552020-12-08 13:49:44 +0000336 return (void *) (intptr_t) ret;
thscd831bd2008-07-03 10:23:51 +0000337}
Eric Blake3c1fa352018-12-15 07:53:08 -0600338#endif /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000339
Fam Zhenge4afbf42015-05-19 10:50:59 +0000340static int nbd_can_accept(void)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200341{
Eric Blake3dcf56e2021-02-09 09:27:59 -0600342 return state == RUNNING && (shared == 0 || nb_fds < shared);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200343}
344
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000345static void nbd_update_server_watch(void);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000346
Eric Blake0c9390d2017-06-08 17:26:17 -0500347static void nbd_client_closed(NBDClient *client, bool negotiated)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200348{
Paolo Bonzini1743b512011-09-19 14:33:23 +0200349 nb_fds--;
Eric Blake0c9390d2017-06-08 17:26:17 -0500350 if (negotiated && nb_fds == 0 && !persistent && state == RUNNING) {
Paolo Bonzini7860a382012-09-18 13:31:56 +0200351 state = TERMINATE;
352 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000353 nbd_update_server_watch();
Paolo Bonzini7860a382012-09-18 13:31:56 +0200354 nbd_client_put(client);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200355}
356
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000357static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc,
358 gpointer opaque)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200359{
Paolo Bonzini7860a382012-09-18 13:31:56 +0200360 if (state >= TERMINATE) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000361 return;
Paolo Bonzini7860a382012-09-18 13:31:56 +0200362 }
363
Fam Zhengee7d7aa2016-01-14 16:41:01 +0800364 nb_fds++;
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000365 nbd_update_server_watch();
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000366 nbd_client_new(cioc, tlscreds, tlsauthz, nbd_client_closed);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200367}
368
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000369static void nbd_update_server_watch(void)
Fam Zhenge4afbf42015-05-19 10:50:59 +0000370{
371 if (nbd_can_accept()) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000372 qio_net_listener_set_client_func(server, nbd_accept, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000373 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000374 qio_net_listener_set_client_func(server, NULL, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000375 }
376}
377
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100378
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200379static SocketAddress *nbd_build_socket_address(const char *sockpath,
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100380 const char *bindto,
381 const char *port)
382{
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200383 SocketAddress *saddr;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100384
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200385 saddr = g_new0(SocketAddress, 1);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100386 if (sockpath) {
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200387 saddr->type = SOCKET_ADDRESS_TYPE_UNIX;
388 saddr->u.q_unix.path = g_strdup(sockpath);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100389 } else {
Eric Blake03992932016-03-03 09:16:48 -0700390 InetSocketAddress *inet;
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200391 saddr->type = SOCKET_ADDRESS_TYPE_INET;
392 inet = &saddr->u.inet;
Eric Blake03992932016-03-03 09:16:48 -0700393 inet->host = g_strdup(bindto);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100394 if (port) {
Eric Blake03992932016-03-03 09:16:48 -0700395 inet->port = g_strdup(port);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100396 } else {
Eric Blake03992932016-03-03 09:16:48 -0700397 inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100398 }
399 }
400
401 return saddr;
402}
403
404
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000405static QemuOptsList file_opts = {
406 .name = "file",
407 .implied_opt_name = "file",
408 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
409 .desc = {
410 /* no elements => accept any params */
411 { /* end of list */ }
412 },
413};
414
Eric Blake68b96f12019-01-17 13:36:56 -0600415static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, bool list,
416 Error **errp)
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000417{
418 Object *obj;
419 QCryptoTLSCreds *creds;
420
421 obj = object_resolve_path_component(
422 object_get_objects_root(), id);
423 if (!obj) {
424 error_setg(errp, "No TLS credentials with id '%s'",
425 id);
426 return NULL;
427 }
428 creds = (QCryptoTLSCreds *)
429 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
430 if (!creds) {
431 error_setg(errp, "Object with id '%s' is not TLS credentials",
432 id);
433 return NULL;
434 }
435
Philippe Mathieu-Daudé0279cd92021-06-28 18:09:10 +0200436 if (!qcrypto_tls_creds_check_endpoint(creds,
437 list
438 ? QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
439 : QCRYPTO_TLS_CREDS_ENDPOINT_SERVER,
440 errp)) {
441 return NULL;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000442 }
443 object_ref(obj);
444 return creds;
445}
446
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000447static void setup_address_and_port(const char **address, const char **port)
448{
449 if (*address == NULL) {
450 *address = "0.0.0.0";
451 }
452
453 if (*port == NULL) {
454 *port = stringify(NBD_DEFAULT_PORT);
455 }
456}
457
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000458/*
459 * Check socket parameters compatibility when socket activation is used.
460 */
461static const char *socket_activation_validate_opts(const char *device,
462 const char *sockpath,
463 const char *address,
Eric Blake68b96f12019-01-17 13:36:56 -0600464 const char *port,
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600465 const char *selinux,
Eric Blake68b96f12019-01-17 13:36:56 -0600466 bool list)
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000467{
468 if (device != NULL) {
469 return "NBD device can't be set when using socket activation";
470 }
471
472 if (sockpath != NULL) {
473 return "Unix socket can't be set when using socket activation";
474 }
475
476 if (address != NULL) {
477 return "The interface can't be set when using socket activation";
478 }
479
480 if (port != NULL) {
481 return "TCP port number can't be set when using socket activation";
482 }
483
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600484 if (selinux != NULL) {
485 return "SELinux label can't be set when using socket activation";
486 }
487
Eric Blake68b96f12019-01-17 13:36:56 -0600488 if (list) {
489 return "List mode is incompatible with socket activation";
490 }
491
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000492 return NULL;
493}
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000494
Kevin Wolfb3b52992018-05-16 13:46:37 +0200495static void qemu_nbd_shutdown(void)
496{
497 job_cancel_sync_all();
Sergio Lopez1895b972021-02-01 13:50:32 +0100498 blk_exp_close_all();
Kevin Wolfb3b52992018-05-16 13:46:37 +0200499 bdrv_close_all();
500}
501
bellard7a5ca862008-05-27 21:13:40 +0000502int main(int argc, char **argv)
503{
Markus Armbruster26f54e92014-10-07 13:59:04 +0200504 BlockBackend *blk;
bellard7a5ca862008-05-27 21:13:40 +0000505 BlockDriverState *bs;
Eric Blake9d26dfc2019-01-17 13:36:43 -0600506 uint64_t dev_offset = 0;
Eric Blakedbb38ca2019-08-23 09:37:22 -0500507 bool readonly = false;
thscd831bd2008-07-03 10:23:51 +0000508 bool disconnect = false;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000509 const char *bindto = NULL;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100510 const char *port = NULL;
511 char *sockpath = NULL;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100512 char *device = NULL;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800513 QemuOpts *sn_opts = NULL;
514 const char *sn_id_or_name = NULL;
Eric Blakedbc7b012020-10-27 00:05:55 -0500515 const char *sopt = "hVb:o:p:rsnc:dvk:e:f:tl:x:T:D:AB:L";
bellard7a5ca862008-05-27 21:13:40 +0000516 struct option lopt[] = {
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000517 { "help", no_argument, NULL, 'h' },
518 { "version", no_argument, NULL, 'V' },
519 { "bind", required_argument, NULL, 'b' },
520 { "port", required_argument, NULL, 'p' },
521 { "socket", required_argument, NULL, 'k' },
522 { "offset", required_argument, NULL, 'o' },
523 { "read-only", no_argument, NULL, 'r' },
Eric Blakedbc7b012020-10-27 00:05:55 -0500524 { "allocation-depth", no_argument, NULL, 'A' },
Eric Blake636192c2019-01-11 13:47:20 -0600525 { "bitmap", required_argument, NULL, 'B' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000526 { "connect", required_argument, NULL, 'c' },
527 { "disconnect", no_argument, NULL, 'd' },
Eric Blake68b96f12019-01-17 13:36:56 -0600528 { "list", no_argument, NULL, 'L' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000529 { "snapshot", no_argument, NULL, 's' },
530 { "load-snapshot", required_argument, NULL, 'l' },
531 { "nocache", no_argument, NULL, 'n' },
532 { "cache", required_argument, NULL, QEMU_NBD_OPT_CACHE },
533 { "aio", required_argument, NULL, QEMU_NBD_OPT_AIO },
534 { "discard", required_argument, NULL, QEMU_NBD_OPT_DISCARD },
535 { "detect-zeroes", required_argument, NULL,
536 QEMU_NBD_OPT_DETECT_ZEROES },
537 { "shared", required_argument, NULL, 'e' },
538 { "format", required_argument, NULL, 'f' },
539 { "persistent", no_argument, NULL, 't' },
540 { "verbose", no_argument, NULL, 'v' },
541 { "object", required_argument, NULL, QEMU_NBD_OPT_OBJECT },
542 { "export-name", required_argument, NULL, 'x' },
Eric Blakeb1a75b32016-10-14 13:33:03 -0500543 { "description", required_argument, NULL, 'D' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000544 { "tls-creds", required_argument, NULL, QEMU_NBD_OPT_TLSCREDS },
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000545 { "tls-authz", required_argument, NULL, QEMU_NBD_OPT_TLSAUTHZ },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000546 { "image-opts", no_argument, NULL, QEMU_NBD_OPT_IMAGE_OPTS },
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300547 { "trace", required_argument, NULL, 'T' },
Max Reitzffb31e12016-09-28 22:46:42 +0200548 { "fork", no_argument, NULL, QEMU_NBD_OPT_FORK },
Max Reitz637bc5a2019-05-08 23:18:16 +0200549 { "pid-file", required_argument, NULL, QEMU_NBD_OPT_PID_FILE },
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600550 { "selinux-label", required_argument, NULL,
551 QEMU_NBD_OPT_SELINUX_LABEL },
Blue Swirl660f11b2009-07-31 21:16:51 +0000552 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000553 };
554 int ch;
555 int opt_ind = 0;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200556 int flags = BDRV_O_RDWR;
Max Reitz4fbec262015-02-05 13:58:19 -0500557 int ret = 0;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200558 bool seen_cache = false;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100559 bool seen_discard = false;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200560 bool seen_aio = false;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100561 pthread_t client_thread;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000562 const char *fmt = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200563 Error *local_err = NULL;
Peter Lievenb3838a42014-08-13 19:20:18 +0200564 BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
Max Reitz4fbec262015-02-05 13:58:19 -0500565 QDict *options = NULL;
Eric Blake68b96f12019-01-17 13:36:56 -0600566 const char *export_name = NULL; /* defaults to "" later for server mode */
Eric Blakeb1a75b32016-10-14 13:33:03 -0500567 const char *export_description = NULL;
Eric Blakecbad81c2020-10-27 00:05:49 -0500568 strList *bitmaps = NULL;
Eric Blakedbc7b012020-10-27 00:05:55 -0500569 bool alloc_depth = false;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000570 const char *tlscredsid = NULL;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000571 bool imageOpts = false;
Nir Soffer09615252021-08-13 23:55:19 +0300572 bool writethrough = false; /* Client will flush as needed. */
Max Reitzffb31e12016-09-28 22:46:42 +0200573 bool fork_process = false;
Eric Blake68b96f12019-01-17 13:36:56 -0600574 bool list = false;
Max Reitzffb31e12016-09-28 22:46:42 +0200575 int old_stderr = -1;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000576 unsigned socket_activation;
Max Reitz637bc5a2019-05-08 23:18:16 +0200577 const char *pid_file_name = NULL;
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600578 const char *selinux_label = NULL;
Kevin Wolf00917172020-09-24 17:26:57 +0200579 BlockExportOptions *export_opts;
bellard7a5ca862008-05-27 21:13:40 +0000580
Eric Blake029a88c2020-09-30 07:11:01 -0500581#ifdef CONFIG_POSIX
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +0100582 os_setup_early_signal_handling();
583 os_setup_signal_handling();
Max Reitz041e32b2017-06-11 14:37:14 +0200584#endif
585
Daniel P. Berrangé98c5d2e2020-08-25 11:38:48 +0100586 socket_init();
Christophe Fergeauf5852ef2019-01-31 17:46:14 +0100587 error_init(argv[0]);
Daniel P. Berrangefe4db842016-10-04 14:35:52 +0100588 module_call_init(MODULE_INIT_TRACE);
Eduardo Habkoste8f2d272016-05-12 11:10:04 -0300589 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100590
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000591 module_call_init(MODULE_INIT_QOM);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300592 qemu_add_opts(&qemu_trace_opts);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800593 qemu_init_exec_dir(argv[0]);
Paolo Bonzinibb345112011-11-04 15:51:19 +0100594
bellard7a5ca862008-05-27 21:13:40 +0000595 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
596 switch (ch) {
597 case 's':
ths2f726482008-07-03 11:47:46 +0000598 flags |= BDRV_O_SNAPSHOT;
599 break;
600 case 'n':
Paolo Bonzini39a52352012-07-18 14:57:15 +0200601 optarg = (char *) "none";
602 /* fallthrough */
603 case QEMU_NBD_OPT_CACHE:
604 if (seen_cache) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100605 error_report("-n and --cache can only be specified once");
606 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200607 }
608 seen_cache = true;
Kevin Wolf6effd5b2016-03-14 11:43:28 +0100609 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100610 error_report("Invalid cache mode `%s'", optarg);
611 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200612 }
bellard7a5ca862008-05-27 21:13:40 +0000613 break;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200614 case QEMU_NBD_OPT_AIO:
615 if (seen_aio) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100616 error_report("--aio can only be specified once");
617 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200618 }
619 seen_aio = true;
Aarushi Mehta76802742020-01-20 14:18:56 +0000620 if (bdrv_parse_aio(optarg, &flags) < 0) {
621 error_report("Invalid aio mode '%s'", optarg);
622 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200623 }
624 break;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100625 case QEMU_NBD_OPT_DISCARD:
626 if (seen_discard) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100627 error_report("--discard can only be specified once");
628 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100629 }
630 seen_discard = true;
631 if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100632 error_report("Invalid discard mode `%s'", optarg);
633 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100634 }
635 break;
Peter Lievenb3838a42014-08-13 19:20:18 +0200636 case QEMU_NBD_OPT_DETECT_ZEROES:
637 detect_zeroes =
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200638 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
Peter Lievenb3838a42014-08-13 19:20:18 +0200639 optarg,
Peter Lievenb3838a42014-08-13 19:20:18 +0200640 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
641 &local_err);
642 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100643 error_reportf_err(local_err,
644 "Failed to parse detect_zeroes mode: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100645 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200646 }
647 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
648 !(flags & BDRV_O_UNMAP)) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100649 error_report("setting detect-zeroes to unmap is not allowed "
650 "without setting discard operation to unmap");
651 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200652 }
653 break;
bellard7a5ca862008-05-27 21:13:40 +0000654 case 'b':
655 bindto = optarg;
656 break;
657 case 'p':
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100658 port = optarg;
bellard7a5ca862008-05-27 21:13:40 +0000659 break;
660 case 'o':
Eric Blake43b51012019-01-17 13:36:44 -0600661 if (qemu_strtou64(optarg, NULL, 0, &dev_offset) < 0) {
662 error_report("Invalid offset '%s'", optarg);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100663 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000664 }
bellard7a5ca862008-05-27 21:13:40 +0000665 break;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800666 case 'l':
667 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +0100668 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
669 optarg, false);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800670 if (!sn_opts) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100671 error_report("Failed in parsing snapshot param `%s'",
672 optarg);
673 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800674 }
675 } else {
676 sn_id_or_name = optarg;
677 }
678 /* fall through */
bellard7a5ca862008-05-27 21:13:40 +0000679 case 'r':
Eric Blakedbb38ca2019-08-23 09:37:22 -0500680 readonly = true;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200681 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000682 break;
Eric Blakedbc7b012020-10-27 00:05:55 -0500683 case 'A':
684 alloc_depth = true;
685 break;
Eric Blake636192c2019-01-11 13:47:20 -0600686 case 'B':
Eric Blakecbad81c2020-10-27 00:05:49 -0500687 QAPI_LIST_PREPEND(bitmaps, g_strdup(optarg));
Eric Blake636192c2019-01-11 13:47:20 -0600688 break;
thscd831bd2008-07-03 10:23:51 +0000689 case 'k':
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100690 sockpath = optarg;
Peter Lieven713cc672014-08-13 19:20:19 +0200691 if (sockpath[0] != '/') {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +0100692 error_report("socket path must be absolute");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100693 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200694 }
thscd831bd2008-07-03 10:23:51 +0000695 break;
696 case 'd':
697 disconnect = true;
698 break;
699 case 'c':
700 device = optarg;
701 break;
ths3b05a8e2008-07-03 12:45:02 +0000702 case 'e':
Eric Blake43b51012019-01-17 13:36:44 -0600703 if (qemu_strtoi(optarg, NULL, 0, &shared) < 0 ||
Eric Blake3dcf56e2021-02-09 09:27:59 -0600704 shared < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100705 error_report("Invalid shared device number '%s'", optarg);
706 exit(EXIT_FAILURE);
ths3b05a8e2008-07-03 12:45:02 +0000707 }
ths3b05a8e2008-07-03 12:45:02 +0000708 break;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000709 case 'f':
710 fmt = optarg;
711 break;
Peter Lieven713cc672014-08-13 19:20:19 +0200712 case 't':
713 persistent = 1;
714 break;
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000715 case 'x':
716 export_name = optarg;
Eric Blake93676c82019-11-13 20:46:34 -0600717 if (strlen(export_name) > NBD_MAX_STRING_SIZE) {
718 error_report("export name '%s' too long", export_name);
719 exit(EXIT_FAILURE);
720 }
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000721 break;
Eric Blakeb1a75b32016-10-14 13:33:03 -0500722 case 'D':
723 export_description = optarg;
Eric Blake93676c82019-11-13 20:46:34 -0600724 if (strlen(export_description) > NBD_MAX_STRING_SIZE) {
725 error_report("export description '%s' too long",
726 export_description);
727 exit(EXIT_FAILURE);
728 }
Eric Blakeb1a75b32016-10-14 13:33:03 -0500729 break;
bellard7a5ca862008-05-27 21:13:40 +0000730 case 'v':
731 verbose = 1;
732 break;
733 case 'V':
734 version(argv[0]);
735 exit(0);
736 break;
737 case 'h':
738 usage(argv[0]);
739 exit(0);
740 break;
741 case '?':
Markus Armbruster85b01e02015-12-18 16:35:04 +0100742 error_report("Try `%s --help' for more information.", argv[0]);
743 exit(EXIT_FAILURE);
Kevin Wolffa40e432021-02-17 12:56:45 +0100744 case QEMU_NBD_OPT_OBJECT:
745 user_creatable_process_cmdline(optarg);
746 break;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000747 case QEMU_NBD_OPT_TLSCREDS:
748 tlscredsid = optarg;
749 break;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000750 case QEMU_NBD_OPT_IMAGE_OPTS:
751 imageOpts = true;
752 break;
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300753 case 'T':
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500754 trace_opt_parse(optarg);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300755 break;
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000756 case QEMU_NBD_OPT_TLSAUTHZ:
757 tlsauthz = optarg;
758 break;
Max Reitzffb31e12016-09-28 22:46:42 +0200759 case QEMU_NBD_OPT_FORK:
760 fork_process = true;
761 break;
Eric Blake68b96f12019-01-17 13:36:56 -0600762 case 'L':
763 list = true;
764 break;
Max Reitz637bc5a2019-05-08 23:18:16 +0200765 case QEMU_NBD_OPT_PID_FILE:
766 pid_file_name = optarg;
767 break;
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600768 case QEMU_NBD_OPT_SELINUX_LABEL:
769 selinux_label = optarg;
770 break;
bellard7a5ca862008-05-27 21:13:40 +0000771 }
772 }
773
Eric Blake68b96f12019-01-17 13:36:56 -0600774 if (list) {
775 if (argc != optind) {
776 error_report("List mode is incompatible with a file name");
777 exit(EXIT_FAILURE);
778 }
Eric Blake0bc16992020-01-23 10:46:50 -0600779 if (export_name || export_description || dev_offset ||
Eric Blakecbad81c2020-10-27 00:05:49 -0500780 device || disconnect || fmt || sn_id_or_name || bitmaps ||
Eric Blakedbc7b012020-10-27 00:05:55 -0500781 alloc_depth || seen_aio || seen_discard || seen_cache) {
Eric Blake68b96f12019-01-17 13:36:56 -0600782 error_report("List mode is incompatible with per-device settings");
783 exit(EXIT_FAILURE);
784 }
785 if (fork_process) {
786 error_report("List mode is incompatible with forking");
787 exit(EXIT_FAILURE);
788 }
789 } else if ((argc - optind) != 1) {
Markus Armbruster433672b2015-12-18 16:35:24 +0100790 error_report("Invalid number of arguments");
791 error_printf("Try `%s --help' for more information.\n", argv[0]);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100792 exit(EXIT_FAILURE);
Eric Blake68b96f12019-01-17 13:36:56 -0600793 } else if (!export_name) {
794 export_name = "";
bellard7a5ca862008-05-27 21:13:40 +0000795 }
796
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300797 if (!trace_init_backends()) {
798 exit(1);
799 }
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500800 trace_init_file();
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300801 qemu_set_log(LOG_TRACE);
802
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000803 socket_activation = check_socket_activation();
804 if (socket_activation == 0) {
805 setup_address_and_port(&bindto, &port);
806 } else {
807 /* Using socket activation - check user didn't use -p etc. */
808 const char *err_msg = socket_activation_validate_opts(device, sockpath,
Eric Blake68b96f12019-01-17 13:36:56 -0600809 bindto, port,
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600810 selinux_label,
Eric Blake68b96f12019-01-17 13:36:56 -0600811 list);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000812 if (err_msg != NULL) {
813 error_report("%s", err_msg);
814 exit(EXIT_FAILURE);
815 }
Paolo Bonzini53fabd42017-03-16 16:29:45 +0100816
817 /* qemu-nbd can only listen on a single socket. */
818 if (socket_activation > 1) {
819 error_report("qemu-nbd does not support socket activation with %s > 1",
820 "LISTEN_FDS");
821 exit(EXIT_FAILURE);
822 }
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000823 }
824
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000825 if (tlscredsid) {
826 if (sockpath) {
827 error_report("TLS is only supported with IPv4/IPv6");
828 exit(EXIT_FAILURE);
829 }
830 if (device) {
831 error_report("TLS is not supported with a host device");
832 exit(EXIT_FAILURE);
833 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000834 if (tlsauthz && list) {
835 error_report("TLS authorization is incompatible with export list");
836 exit(EXIT_FAILURE);
837 }
Eric Blake68b96f12019-01-17 13:36:56 -0600838 tlscreds = nbd_get_tls_creds(tlscredsid, list, &local_err);
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000839 if (local_err) {
Markus Armbruster5217f182020-05-05 12:19:03 +0200840 error_reportf_err(local_err, "Failed to get TLS creds: ");
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000841 exit(EXIT_FAILURE);
842 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000843 } else {
844 if (tlsauthz) {
845 error_report("--tls-authz is not permitted without --tls-creds");
846 exit(EXIT_FAILURE);
847 }
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000848 }
849
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600850 if (selinux_label) {
851#ifdef CONFIG_SELINUX
852 if (sockpath == NULL && device == NULL) {
853 error_report("--selinux-label is not permitted without --socket");
854 exit(EXIT_FAILURE);
855 }
856#else
857 error_report("SELinux support not enabled in this binary");
858 exit(EXIT_FAILURE);
859#endif
860 }
861
Eric Blake68b96f12019-01-17 13:36:56 -0600862 if (list) {
863 saddr = nbd_build_socket_address(sockpath, bindto, port);
864 return qemu_nbd_client_list(saddr, tlscreds, bindto);
865 }
866
Eric Blake3c1fa352018-12-15 07:53:08 -0600867#if !HAVE_NBD_DEVICE
868 if (disconnect || device) {
869 error_report("Kernel /dev/nbdN support not available");
870 exit(EXIT_FAILURE);
871 }
872#else /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000873 if (disconnect) {
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000874 int nbdfd = open(argv[optind], O_RDWR);
875 if (nbdfd < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100876 error_report("Cannot open %s: %s", argv[optind],
877 strerror(errno));
878 exit(EXIT_FAILURE);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100879 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000880 nbd_disconnect(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000881
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000882 close(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000883
884 printf("%s disconnected\n", argv[optind]);
885
Peter Lieven713cc672014-08-13 19:20:19 +0200886 return 0;
thscd831bd2008-07-03 10:23:51 +0000887 }
Eric Blake3c1fa352018-12-15 07:53:08 -0600888#endif
thscd831bd2008-07-03 10:23:51 +0000889
Max Reitzffb31e12016-09-28 22:46:42 +0200890 if ((device && !verbose) || fork_process) {
Daniel P. Berrangéeb705982020-08-25 11:38:50 +0100891#ifndef WIN32
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100892 int stderr_fd[2];
893 pid_t pid;
894 int ret;
895
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100896 if (qemu_pipe(stderr_fd) < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100897 error_report("Error setting up communication pipe: %s",
898 strerror(errno));
899 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100900 }
901
902 /* Now daemonize, but keep a communication channel open to
903 * print errors and exit with the proper status code.
904 */
905 pid = fork();
Max Reitz70d47392015-02-25 13:08:22 -0500906 if (pid < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100907 error_report("Failed to fork: %s", strerror(errno));
908 exit(EXIT_FAILURE);
Max Reitz70d47392015-02-25 13:08:22 -0500909 } else if (pid == 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100910 close(stderr_fd[0]);
Max Reitze6df58a2019-05-08 23:18:18 +0200911
Raphael Pour0eaf4532020-05-15 08:36:07 +0200912 /* Remember parent's stderr if we will be restoring it. */
913 if (fork_process) {
914 old_stderr = dup(STDERR_FILENO);
915 }
916
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400917 ret = qemu_daemon(1, 0);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100918
919 /* Temporarily redirect stderr to the parent's pipe... */
920 dup2(stderr_fd[1], STDERR_FILENO);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100921 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100922 error_report("Failed to daemonize: %s", strerror(errno));
923 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100924 }
925
926 /* ... close the descriptor we inherited and go on. */
927 close(stderr_fd[1]);
928 } else {
929 bool errors = false;
930 char *buf;
931
932 /* In the parent. Print error messages from the child until
933 * it closes the pipe.
934 */
935 close(stderr_fd[1]);
936 buf = g_malloc(1024);
937 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
938 errors = true;
939 ret = qemu_write_full(STDERR_FILENO, buf, ret);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100940 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100941 exit(EXIT_FAILURE);
942 }
943 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100944 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100945 error_report("Cannot read from daemon: %s",
946 strerror(errno));
947 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100948 }
949
950 /* Usually the daemon should not print any message.
951 * Exit with zero status in that case.
952 */
953 exit(errors);
954 }
Daniel P. Berrangéeb705982020-08-25 11:38:50 +0100955#else /* WIN32 */
956 error_report("Unable to fork into background on Windows hosts");
957 exit(EXIT_FAILURE);
958#endif /* WIN32 */
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100959 }
960
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100961 if (device != NULL && sockpath == NULL) {
962 sockpath = g_malloc(128);
963 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
thscd831bd2008-07-03 10:23:51 +0000964 }
965
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000966 server = qio_net_listener_new();
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000967 if (socket_activation == 0) {
Eric Blake582d4212021-02-09 09:27:58 -0600968 int backlog;
969
Eric Blake3dcf56e2021-02-09 09:27:59 -0600970 if (persistent || shared == 0) {
Eric Blake582d4212021-02-09 09:27:58 -0600971 backlog = SOMAXCONN;
972 } else {
973 backlog = MIN(shared, SOMAXCONN);
974 }
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600975#ifdef CONFIG_SELINUX
976 if (selinux_label && setsockcreatecon_raw(selinux_label) == -1) {
977 error_report("Cannot set SELinux socket create context to %s: %s",
978 selinux_label, strerror(errno));
979 exit(EXIT_FAILURE);
980 }
981#endif
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000982 saddr = nbd_build_socket_address(sockpath, bindto, port);
Eric Blake582d4212021-02-09 09:27:58 -0600983 if (qio_net_listener_open_sync(server, saddr, backlog,
984 &local_err) < 0) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000985 object_unref(OBJECT(server));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000986 error_report_err(local_err);
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000987 exit(EXIT_FAILURE);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000988 }
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600989#ifdef CONFIG_SELINUX
990 if (selinux_label && setsockcreatecon_raw(NULL) == -1) {
991 error_report("Cannot clear SELinux socket create context: %s",
992 strerror(errno));
993 exit(EXIT_FAILURE);
994 }
995#endif
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000996 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000997 size_t i;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000998 /* See comment in check_socket_activation above. */
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000999 for (i = 0; i < socket_activation; i++) {
1000 QIOChannelSocket *sioc;
1001 sioc = qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD + i,
1002 &local_err);
1003 if (sioc == NULL) {
1004 object_unref(OBJECT(server));
Markus Armbruster5217f182020-05-05 12:19:03 +02001005 error_reportf_err(local_err,
1006 "Failed to use socket activation: ");
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001007 exit(EXIT_FAILURE);
1008 }
1009 qio_net_listener_add(server, sioc);
1010 object_unref(OBJECT(sioc));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001011 }
1012 }
Daniel P. Berrange48bec072015-09-16 14:52:23 +01001013
Markus Armbrusterf9734d52021-07-20 14:53:53 +02001014 qemu_init_main_loop(&error_fatal);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001015 bdrv_init();
Kevin Wolfb3b52992018-05-16 13:46:37 +02001016 atexit(qemu_nbd_shutdown);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001017
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001018 srcpath = argv[optind];
1019 if (imageOpts) {
1020 QemuOpts *opts;
1021 if (fmt) {
1022 error_report("--image-opts and -f are mutually exclusive");
1023 exit(EXIT_FAILURE);
1024 }
1025 opts = qemu_opts_parse_noisily(&file_opts, srcpath, true);
1026 if (!opts) {
1027 qemu_opts_reset(&file_opts);
1028 exit(EXIT_FAILURE);
1029 }
1030 options = qemu_opts_to_qdict(opts, NULL);
1031 qemu_opts_reset(&file_opts);
Max Reitzefaa7c42016-03-16 19:54:38 +01001032 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001033 } else {
1034 if (fmt) {
1035 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05001036 qdict_put_str(options, "driver", fmt);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001037 }
Max Reitzefaa7c42016-03-16 19:54:38 +01001038 blk = blk_new_open(srcpath, NULL, options, flags, &local_err);
Daniel P. Berrangee6b63672013-03-19 11:20:20 +00001039 }
1040
Max Reitz4fbec262015-02-05 13:58:19 -05001041 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001042 error_reportf_err(local_err, "Failed to blk_new_open '%s': ",
1043 argv[optind]);
Markus Armbruster85b01e02015-12-18 16:35:04 +01001044 exit(EXIT_FAILURE);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001045 }
Max Reitz4fbec262015-02-05 13:58:19 -05001046 bs = blk_bs(blk);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001047
Kevin Wolfb57e4de2020-09-24 17:26:52 +02001048 if (dev_offset) {
1049 QDict *raw_opts = qdict_new();
1050 qdict_put_str(raw_opts, "driver", "raw");
1051 qdict_put_str(raw_opts, "file", bs->node_name);
1052 qdict_put_int(raw_opts, "offset", dev_offset);
1053 bs = bdrv_open(NULL, NULL, raw_opts, flags, &error_fatal);
1054 blk_remove_bs(blk);
1055 blk_insert_bs(blk, bs, &error_fatal);
1056 bdrv_unref(bs);
1057 }
1058
Kevin Wolf6effd5b2016-03-14 11:43:28 +01001059 blk_set_enable_write_cache(blk, !writethrough);
1060
Wenchao Xia8c116b02013-12-04 17:10:55 +08001061 if (sn_opts) {
1062 ret = bdrv_snapshot_load_tmp(bs,
1063 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1064 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1065 &local_err);
1066 } else if (sn_id_or_name) {
1067 ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
1068 &local_err);
1069 }
1070 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001071 error_reportf_err(local_err, "Failed to load snapshot: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +01001072 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001073 }
1074
Peter Lievenb3838a42014-08-13 19:20:18 +02001075 bs->detect_zeroes = detect_zeroes;
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001076
Kevin Wolf00917172020-09-24 17:26:57 +02001077 nbd_server_is_qemu_nbd(true);
1078
1079 export_opts = g_new(BlockExportOptions, 1);
1080 *export_opts = (BlockExportOptions) {
1081 .type = BLOCK_EXPORT_TYPE_NBD,
Kevin Wolfd53be9c2020-09-24 17:27:04 +02001082 .id = g_strdup("qemu-nbd-export"),
Kevin Wolfb6076af2020-09-24 17:27:01 +02001083 .node_name = g_strdup(bdrv_get_node_name(bs)),
Kevin Wolf00917172020-09-24 17:26:57 +02001084 .has_writethrough = true,
1085 .writethrough = writethrough,
Kevin Wolf30dbc812020-09-24 17:27:11 +02001086 .has_writable = true,
1087 .writable = !readonly,
Kevin Wolf00917172020-09-24 17:26:57 +02001088 .u.nbd = {
Eric Blakecbad81c2020-10-27 00:05:49 -05001089 .has_name = true,
1090 .name = g_strdup(export_name),
1091 .has_description = !!export_description,
1092 .description = g_strdup(export_description),
1093 .has_bitmaps = !!bitmaps,
1094 .bitmaps = bitmaps,
Eric Blakedbc7b012020-10-27 00:05:55 -05001095 .has_allocation_depth = alloc_depth,
1096 .allocation_depth = alloc_depth,
Kevin Wolf00917172020-09-24 17:26:57 +02001097 },
1098 };
1099 blk_exp_add(export_opts, &error_fatal);
1100 qapi_free_BlockExportOptions(export_opts);
ths3b05a8e2008-07-03 12:45:02 +00001101
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001102 if (device) {
Eric Blake3c1fa352018-12-15 07:53:08 -06001103#if HAVE_NBD_DEVICE
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001104 int ret;
1105
Paolo Bonzinia6ac2312011-12-06 09:07:00 +01001106 ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001107 if (ret != 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001108 error_report("Failed to create client thread: %s", strerror(ret));
1109 exit(EXIT_FAILURE);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001110 }
Eric Blake3c1fa352018-12-15 07:53:08 -06001111#endif
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001112 } else {
1113 /* Shut up GCC warnings. */
1114 memset(&client_thread, 0, sizeof(client_thread));
1115 }
1116
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +00001117 nbd_update_server_watch();
bellard7a5ca862008-05-27 21:13:40 +00001118
Max Reitz637bc5a2019-05-08 23:18:16 +02001119 if (pid_file_name) {
1120 qemu_write_pidfile(pid_file_name, &error_fatal);
1121 }
1122
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001123 /* now when the initialization is (almost) complete, chdir("/")
1124 * to free any busy filesystems */
1125 if (chdir("/") < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001126 error_report("Could not chdir to root directory: %s",
1127 strerror(errno));
1128 exit(EXIT_FAILURE);
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001129 }
1130
Max Reitzffb31e12016-09-28 22:46:42 +02001131 if (fork_process) {
1132 dup2(old_stderr, STDERR_FILENO);
1133 close(old_stderr);
1134 }
1135
Paolo Bonzini7860a382012-09-18 13:31:56 +02001136 state = RUNNING;
ths3b05a8e2008-07-03 12:45:02 +00001137 do {
Paolo Bonzinia61c6782011-09-12 17:28:11 +02001138 main_loop_wait(false);
Paolo Bonzini7860a382012-09-18 13:31:56 +02001139 if (state == TERMINATE) {
Kevin Wolfbc4ee652020-09-24 17:27:03 +02001140 blk_exp_close_all();
Kevin Wolfd794f7f2020-09-24 17:26:56 +02001141 state = TERMINATED;
Paolo Bonzini7860a382012-09-18 13:31:56 +02001142 }
1143 } while (state != TERMINATED);
ths3b05a8e2008-07-03 12:45:02 +00001144
Markus Armbruster26f54e92014-10-07 13:59:04 +02001145 blk_unref(blk);
Paolo Bonzinib32f6c22011-11-04 15:51:20 +01001146 if (sockpath) {
1147 unlink(sockpath);
1148 }
bellard7a5ca862008-05-27 21:13:40 +00001149
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02001150 qemu_opts_del(sn_opts);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001151
Paolo Bonzinia517e882011-11-04 15:51:21 +01001152 if (device) {
1153 void *ret;
1154 pthread_join(client_thread, &ret);
1155 exit(ret != NULL);
1156 } else {
1157 exit(EXIT_SUCCESS);
1158 }
bellard7a5ca862008-05-27 21:13:40 +00001159}