blob: 47587a709e6bf408399e7517217b5b68e2d2f4c1 [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"
Denis V. Lunev39ca4632016-06-17 17:44:12 +030046#include "trace/control.h"
Eric Blakebe377132017-07-21 08:50:46 -050047#include "qemu-version.h"
bellard7a5ca862008-05-27 21:13:40 +000048
Eric Blake3c1fa352018-12-15 07:53:08 -060049#ifdef __linux__
50#define HAVE_NBD_DEVICE 1
51#else
52#define HAVE_NBD_DEVICE 0
53#endif
54
Peter Lieven713cc672014-08-13 19:20:19 +020055#define SOCKET_PATH "/var/lock/qemu-nbd-%s"
Daniel P. Berrangefa8b7ce2016-02-17 10:10:21 +000056#define QEMU_NBD_OPT_CACHE 256
57#define QEMU_NBD_OPT_AIO 257
58#define QEMU_NBD_OPT_DISCARD 258
59#define QEMU_NBD_OPT_DETECT_ZEROES 259
60#define QEMU_NBD_OPT_OBJECT 260
61#define QEMU_NBD_OPT_TLSCREDS 261
62#define QEMU_NBD_OPT_IMAGE_OPTS 262
Max Reitzffb31e12016-09-28 22:46:42 +020063#define QEMU_NBD_OPT_FORK 263
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +000064#define QEMU_NBD_OPT_TLSAUTHZ 264
Max Reitz637bc5a2019-05-08 23:18:16 +020065#define QEMU_NBD_OPT_PID_FILE 265
bellard7a5ca862008-05-27 21:13:40 +000066
Eric Blakebd31c212016-05-06 10:26:42 -060067#define MBR_SIZE 512
68
blueswir1b1d8e522008-10-26 13:43:07 +000069static int verbose;
Paolo Bonzinia517e882011-11-04 15:51:21 +010070static char *srcpath;
Markus Armbrusterbd269eb2017-04-26 09:36:41 +020071static SocketAddress *saddr;
Paolo Bonzini7860a382012-09-18 13:31:56 +020072static int persistent = 0;
Kevin Wolfd794f7f2020-09-24 17:26:56 +020073static enum { RUNNING, TERMINATE, TERMINATED } state;
Paolo Bonzinia61c6782011-09-12 17:28:11 +020074static int shared = 1;
75static int nb_fds;
Daniel P. Berrangee4849c12017-12-18 10:16:43 +000076static QIONetListener *server;
Daniel P. Berrange145614a2016-02-10 18:41:13 +000077static QCryptoTLSCreds *tlscreds;
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +000078static const char *tlsauthz;
bellard7a5ca862008-05-27 21:13:40 +000079
80static void usage(const char *name)
81{
Paolo Bonzinib033cd82012-07-18 14:50:52 +020082 (printf) (
bellard7a5ca862008-05-27 21:13:40 +000083"Usage: %s [OPTIONS] FILE\n"
Eric Blake68b96f12019-01-17 13:36:56 -060084" or: %s -L [OPTIONS]\n"
85"QEMU Disk Network Block Device Utility\n"
bellard7a5ca862008-05-27 21:13:40 +000086"\n"
Peter Lieven713cc672014-08-13 19:20:19 +020087" -h, --help display this help and exit\n"
88" -V, --version output version information and exit\n"
bellard7a5ca862008-05-27 21:13:40 +000089"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020090"Connection properties:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020091" -p, --port=PORT port to listen on (default `%d')\n"
92" -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
93" -k, --socket=PATH path to the unix socket\n"
94" (default '"SOCKET_PATH"')\n"
95" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
96" -t, --persistent don't exit on the last connection\n"
97" -v, --verbose display extra debugging information\n"
Vladimir Sementsov-Ogievskiyf5cd0bb2018-10-03 20:02:27 +030098" -x, --export-name=NAME expose export by name (default is empty string)\n"
99" -D, --description=TEXT export a human-readable description\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200100"\n"
101"Exposing part of the image:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200102" -o, --offset=OFFSET offset into the image\n"
Eric Blakedbc7b012020-10-27 00:05:55 -0500103" -A, --allocation-depth expose the allocation depth\n"
Eric Blake636192c2019-01-11 13:47:20 -0600104" -B, --bitmap=NAME expose a persistent dirty bitmap\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200105"\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000106"General purpose options:\n"
Eric Blake68b96f12019-01-17 13:36:56 -0600107" -L, --list list exports available from another NBD server\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000108" --object type,id=ID,... define an object such as 'secret' for providing\n"
109" passwords and/or encryption keys\n"
Eric Blakef7812df2018-10-03 13:04:26 -0500110" --tls-creds=ID use id of an earlier --object to provide TLS\n"
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000111" --tls-authz=ID use id of an earlier --object to provide\n"
112" authorization\n"
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300113" -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
114" specify tracing options\n"
Max Reitzffb31e12016-09-28 22:46:42 +0200115" --fork fork off the server process and exit the parent\n"
116" once the server is running\n"
Max Reitz637bc5a2019-05-08 23:18:16 +0200117" --pid-file=PATH store the server's process ID in the given file\n"
Eric Blake3c1fa352018-12-15 07:53:08 -0600118#if HAVE_NBD_DEVICE
119"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200120"Kernel NBD client support:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200121" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
122" -d, --disconnect disconnect the specified device\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200123#endif
124"\n"
125"Block device options:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200126" -f, --format=FORMAT set image format (raw, qcow2, ...)\n"
127" -r, --read-only export read-only\n"
128" -s, --snapshot use FILE as an external snapshot, create a temporary\n"
129" file with backing_file=FILE, redirect the write to\n"
130" the temporary one\n"
Wenchao Xia8c116b02013-12-04 17:10:55 +0800131" -l, --load-snapshot=SNAPSHOT_PARAM\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200132" load an internal snapshot inside FILE and export it\n"
133" as an read-only device, SNAPSHOT_PARAM format is\n"
134" 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
135" '[ID_OR_NAME]'\n"
136" -n, --nocache disable host cache\n"
137" --cache=MODE set cache mode (none, writeback, ...)\n"
Aarushi Mehta76802742020-01-20 14:18:56 +0000138" --aio=MODE set AIO mode (native, io_uring or threads)\n"
Peter Lievenb3838a42014-08-13 19:20:18 +0200139" --discard=MODE set discard mode (ignore, unmap)\n"
Andrey Korolyov6883de62015-07-31 22:12:54 +0300140" --detect-zeroes=MODE set detect-zeroes mode (off, on, unmap)\n"
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000141" --image-opts treat FILE as a full set of image options\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200142"\n"
Eric Blakef5048cb2017-08-03 11:33:53 -0500143QEMU_HELP_BOTTOM "\n"
Eric Blake68b96f12019-01-17 13:36:56 -0600144 , name, name, NBD_DEFAULT_PORT, "DEVICE");
bellard7a5ca862008-05-27 21:13:40 +0000145}
146
147static void version(const char *name)
148{
149 printf(
Thomas Huth7e563bf2018-02-15 12:06:47 +0100150"%s " QEMU_FULL_VERSION "\n"
bellard7a5ca862008-05-27 21:13:40 +0000151"Written by Anthony Liguori.\n"
152"\n"
Eric Blakebe377132017-07-21 08:50:46 -0500153QEMU_COPYRIGHT "\n"
bellard7a5ca862008-05-27 21:13:40 +0000154"This is free software; see the source for copying conditions. There is NO\n"
155"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
ths315bc7a2008-07-18 18:06:23 +0000156 , name);
bellard7a5ca862008-05-27 21:13:40 +0000157}
158
Eric Blake029a88c2020-09-30 07:11:01 -0500159#ifdef CONFIG_POSIX
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +0100160/*
161 * The client thread uses SIGTERM to interrupt the server. A signal
162 * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
163 */
164void qemu_system_killed(int signum, pid_t pid)
Paolo Bonzinibb345112011-11-04 15:51:19 +0100165{
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100166 qatomic_cmpxchg(&state, RUNNING, TERMINATE);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200167 qemu_notify_event();
Paolo Bonzinibb345112011-11-04 15:51:19 +0100168}
Eric Blake029a88c2020-09-30 07:11:01 -0500169#endif /* CONFIG_POSIX */
Paolo Bonzini537b41f2014-02-17 14:43:51 +0100170
Eric Blake68b96f12019-01-17 13:36:56 -0600171static int qemu_nbd_client_list(SocketAddress *saddr, QCryptoTLSCreds *tls,
172 const char *hostname)
173{
174 int ret = EXIT_FAILURE;
175 int rc;
176 Error *err = NULL;
177 QIOChannelSocket *sioc;
178 NBDExportInfo *list;
179 int i, j;
180
181 sioc = qio_channel_socket_new();
182 if (qio_channel_socket_connect_sync(sioc, saddr, &err) < 0) {
183 error_report_err(err);
Alex Chen992809b2020-11-30 12:36:51 +0000184 goto out;
Eric Blake68b96f12019-01-17 13:36:56 -0600185 }
186 rc = nbd_receive_export_list(QIO_CHANNEL(sioc), tls, hostname, &list,
187 &err);
188 if (rc < 0) {
189 if (err) {
190 error_report_err(err);
191 }
192 goto out;
193 }
194 printf("exports available: %d\n", rc);
195 for (i = 0; i < rc; i++) {
196 printf(" export: '%s'\n", list[i].name);
197 if (list[i].description && *list[i].description) {
198 printf(" description: %s\n", list[i].description);
199 }
200 if (list[i].flags & NBD_FLAG_HAS_FLAGS) {
Max Reitzc4e2aff2019-04-05 21:16:35 +0200201 static const char *const flag_names[] = {
202 [NBD_FLAG_READ_ONLY_BIT] = "readonly",
203 [NBD_FLAG_SEND_FLUSH_BIT] = "flush",
204 [NBD_FLAG_SEND_FUA_BIT] = "fua",
205 [NBD_FLAG_ROTATIONAL_BIT] = "rotational",
206 [NBD_FLAG_SEND_TRIM_BIT] = "trim",
207 [NBD_FLAG_SEND_WRITE_ZEROES_BIT] = "zeroes",
208 [NBD_FLAG_SEND_DF_BIT] = "df",
209 [NBD_FLAG_CAN_MULTI_CONN_BIT] = "multi",
210 [NBD_FLAG_SEND_RESIZE_BIT] = "resize",
211 [NBD_FLAG_SEND_CACHE_BIT] = "cache",
Eric Blake0a479542019-08-23 09:37:23 -0500212 [NBD_FLAG_SEND_FAST_ZERO_BIT] = "fast-zero",
Max Reitzc4e2aff2019-04-05 21:16:35 +0200213 };
214
Eric Blake68b96f12019-01-17 13:36:56 -0600215 printf(" size: %" PRIu64 "\n", list[i].size);
216 printf(" flags: 0x%x (", list[i].flags);
Max Reitzc4e2aff2019-04-05 21:16:35 +0200217 for (size_t bit = 0; bit < ARRAY_SIZE(flag_names); bit++) {
218 if (flag_names[bit] && (list[i].flags & (1 << bit))) {
219 printf(" %s", flag_names[bit]);
220 }
Eric Blake68b96f12019-01-17 13:36:56 -0600221 }
222 printf(" )\n");
223 }
224 if (list[i].min_block) {
225 printf(" min block: %u\n", list[i].min_block);
226 printf(" opt block: %u\n", list[i].opt_block);
227 printf(" max block: %u\n", list[i].max_block);
228 }
229 if (list[i].n_contexts) {
230 printf(" available meta contexts: %d\n", list[i].n_contexts);
231 for (j = 0; j < list[i].n_contexts; j++) {
232 printf(" %s\n", list[i].contexts[j]);
233 }
234 }
235 }
236 nbd_free_export_list(list, rc);
237
238 ret = EXIT_SUCCESS;
239 out:
240 object_unref(OBJECT(sioc));
241 return ret;
242}
243
244
Eric Blake3c1fa352018-12-15 07:53:08 -0600245#if HAVE_NBD_DEVICE
Paolo Bonzinia517e882011-11-04 15:51:21 +0100246static void *show_parts(void *arg)
thscd831bd2008-07-03 10:23:51 +0000247{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100248 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100249 int nbd;
thscd831bd2008-07-03 10:23:51 +0000250
Paolo Bonzinia517e882011-11-04 15:51:21 +0100251 /* linux just needs an open() to trigger
252 * the partition table update
253 * but remember to load the module with max_part != 0 :
254 * modprobe nbd max_part=63
255 */
256 nbd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100257 if (nbd >= 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100258 close(nbd);
thscd831bd2008-07-03 10:23:51 +0000259 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100260 return NULL;
261}
262
263static void *nbd_client_thread(void *arg)
264{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100265 char *device = arg;
Eric Blake6dc16672019-01-17 13:36:46 -0600266 NBDExportInfo info = { .request_sizes = false, .name = g_strdup("") };
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000267 QIOChannelSocket *sioc;
268 int fd;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100269 int ret;
270 pthread_t show_parts_thread;
Max Reitz1ce52842015-01-26 21:02:59 -0500271 Error *local_error = NULL;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100272
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000273 sioc = qio_channel_socket_new();
274 if (qio_channel_socket_connect_sync(sioc,
275 saddr,
276 &local_error) < 0) {
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100277 error_report_err(local_error);
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000278 goto out;
279 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100280
Vladimir Sementsov-Ogievskiya8e2bb62019-06-18 14:43:21 +0300281 ret = nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc),
Eric Blake004a89f2017-07-07 15:30:41 -0500282 NULL, NULL, NULL, &info, &local_error);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100283 if (ret < 0) {
Max Reitz1ce52842015-01-26 21:02:59 -0500284 if (local_error) {
Markus Armbruster78288672015-12-18 16:35:07 +0100285 error_report_err(local_error);
Max Reitz1ce52842015-01-26 21:02:59 -0500286 }
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100287 goto out_socket;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100288 }
289
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100290 fd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100291 if (fd < 0) {
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100292 /* Linux-only, we can use %m in printf. */
Markus Armbrusterb9884682015-12-18 16:35:18 +0100293 error_report("Failed to open %s: %m", device);
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100294 goto out_socket;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100295 }
296
Eric Blake004a89f2017-07-07 15:30:41 -0500297 ret = nbd_init(fd, sioc, &info, &local_error);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100298 if (ret < 0) {
Vladimir Sementsov-Ogievskiybe41c102017-05-26 14:09:13 +0300299 error_report_err(local_error);
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100300 goto out_fd;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100301 }
302
303 /* update partition table */
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100304 pthread_create(&show_parts_thread, NULL, show_parts, device);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100305
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100306 if (verbose) {
307 fprintf(stderr, "NBD device %s is now connected to %s\n",
308 device, srcpath);
309 } else {
310 /* Close stderr so that the qemu-nbd process exits. */
311 dup2(STDOUT_FILENO, STDERR_FILENO);
312 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100313
314 ret = nbd_client(fd);
315 if (ret) {
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100316 goto out_fd;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100317 }
318 close(fd);
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000319 object_unref(OBJECT(sioc));
Eric Blake6dc16672019-01-17 13:36:46 -0600320 g_free(info.name);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100321 kill(getpid(), SIGTERM);
322 return (void *) EXIT_SUCCESS;
323
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100324out_fd:
325 close(fd);
326out_socket:
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000327 object_unref(OBJECT(sioc));
Paolo Bonzinia517e882011-11-04 15:51:21 +0100328out:
Eric Blake6dc16672019-01-17 13:36:46 -0600329 g_free(info.name);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100330 kill(getpid(), SIGTERM);
331 return (void *) EXIT_FAILURE;
thscd831bd2008-07-03 10:23:51 +0000332}
Eric Blake3c1fa352018-12-15 07:53:08 -0600333#endif /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000334
Fam Zhenge4afbf42015-05-19 10:50:59 +0000335static int nbd_can_accept(void)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200336{
Eric Blakedf8ad9f2017-05-26 22:04:21 -0500337 return state == RUNNING && nb_fds < shared;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200338}
339
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000340static void nbd_update_server_watch(void);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000341
Eric Blake0c9390d2017-06-08 17:26:17 -0500342static void nbd_client_closed(NBDClient *client, bool negotiated)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200343{
Paolo Bonzini1743b512011-09-19 14:33:23 +0200344 nb_fds--;
Eric Blake0c9390d2017-06-08 17:26:17 -0500345 if (negotiated && nb_fds == 0 && !persistent && state == RUNNING) {
Paolo Bonzini7860a382012-09-18 13:31:56 +0200346 state = TERMINATE;
347 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000348 nbd_update_server_watch();
Paolo Bonzini7860a382012-09-18 13:31:56 +0200349 nbd_client_put(client);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200350}
351
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000352static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc,
353 gpointer opaque)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200354{
Paolo Bonzini7860a382012-09-18 13:31:56 +0200355 if (state >= TERMINATE) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000356 return;
Paolo Bonzini7860a382012-09-18 13:31:56 +0200357 }
358
Fam Zhengee7d7aa2016-01-14 16:41:01 +0800359 nb_fds++;
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000360 nbd_update_server_watch();
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000361 nbd_client_new(cioc, tlscreds, tlsauthz, nbd_client_closed);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200362}
363
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000364static void nbd_update_server_watch(void)
Fam Zhenge4afbf42015-05-19 10:50:59 +0000365{
366 if (nbd_can_accept()) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000367 qio_net_listener_set_client_func(server, nbd_accept, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000368 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000369 qio_net_listener_set_client_func(server, NULL, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000370 }
371}
372
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100373
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200374static SocketAddress *nbd_build_socket_address(const char *sockpath,
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100375 const char *bindto,
376 const char *port)
377{
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200378 SocketAddress *saddr;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100379
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200380 saddr = g_new0(SocketAddress, 1);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100381 if (sockpath) {
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200382 saddr->type = SOCKET_ADDRESS_TYPE_UNIX;
383 saddr->u.q_unix.path = g_strdup(sockpath);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100384 } else {
Eric Blake03992932016-03-03 09:16:48 -0700385 InetSocketAddress *inet;
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200386 saddr->type = SOCKET_ADDRESS_TYPE_INET;
387 inet = &saddr->u.inet;
Eric Blake03992932016-03-03 09:16:48 -0700388 inet->host = g_strdup(bindto);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100389 if (port) {
Eric Blake03992932016-03-03 09:16:48 -0700390 inet->port = g_strdup(port);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100391 } else {
Eric Blake03992932016-03-03 09:16:48 -0700392 inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100393 }
394 }
395
396 return saddr;
397}
398
399
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000400static QemuOptsList file_opts = {
401 .name = "file",
402 .implied_opt_name = "file",
403 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
404 .desc = {
405 /* no elements => accept any params */
406 { /* end of list */ }
407 },
408};
409
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000410static QemuOptsList qemu_object_opts = {
411 .name = "object",
412 .implied_opt_name = "qom-type",
413 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
414 .desc = {
415 { }
416 },
417};
418
Kevin Wolf495bf892019-10-11 21:49:17 +0200419static bool qemu_nbd_object_print_help(const char *type, QemuOpts *opts)
420{
421 if (user_creatable_print_help(type, opts)) {
422 exit(0);
423 }
424 return true;
425}
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000426
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000427
Eric Blake68b96f12019-01-17 13:36:56 -0600428static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, bool list,
429 Error **errp)
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000430{
431 Object *obj;
432 QCryptoTLSCreds *creds;
433
434 obj = object_resolve_path_component(
435 object_get_objects_root(), id);
436 if (!obj) {
437 error_setg(errp, "No TLS credentials with id '%s'",
438 id);
439 return NULL;
440 }
441 creds = (QCryptoTLSCreds *)
442 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
443 if (!creds) {
444 error_setg(errp, "Object with id '%s' is not TLS credentials",
445 id);
446 return NULL;
447 }
448
Eric Blake68b96f12019-01-17 13:36:56 -0600449 if (list) {
450 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
451 error_setg(errp,
452 "Expecting TLS credentials with a client endpoint");
453 return NULL;
454 }
455 } else {
456 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
457 error_setg(errp,
458 "Expecting TLS credentials with a server endpoint");
459 return NULL;
460 }
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000461 }
462 object_ref(obj);
463 return creds;
464}
465
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000466static void setup_address_and_port(const char **address, const char **port)
467{
468 if (*address == NULL) {
469 *address = "0.0.0.0";
470 }
471
472 if (*port == NULL) {
473 *port = stringify(NBD_DEFAULT_PORT);
474 }
475}
476
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000477/*
478 * Check socket parameters compatibility when socket activation is used.
479 */
480static const char *socket_activation_validate_opts(const char *device,
481 const char *sockpath,
482 const char *address,
Eric Blake68b96f12019-01-17 13:36:56 -0600483 const char *port,
484 bool list)
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000485{
486 if (device != NULL) {
487 return "NBD device can't be set when using socket activation";
488 }
489
490 if (sockpath != NULL) {
491 return "Unix socket can't be set when using socket activation";
492 }
493
494 if (address != NULL) {
495 return "The interface can't be set when using socket activation";
496 }
497
498 if (port != NULL) {
499 return "TCP port number can't be set when using socket activation";
500 }
501
Eric Blake68b96f12019-01-17 13:36:56 -0600502 if (list) {
503 return "List mode is incompatible with socket activation";
504 }
505
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000506 return NULL;
507}
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000508
Kevin Wolfb3b52992018-05-16 13:46:37 +0200509static void qemu_nbd_shutdown(void)
510{
511 job_cancel_sync_all();
512 bdrv_close_all();
513}
514
bellard7a5ca862008-05-27 21:13:40 +0000515int main(int argc, char **argv)
516{
Markus Armbruster26f54e92014-10-07 13:59:04 +0200517 BlockBackend *blk;
bellard7a5ca862008-05-27 21:13:40 +0000518 BlockDriverState *bs;
Eric Blake9d26dfc2019-01-17 13:36:43 -0600519 uint64_t dev_offset = 0;
Eric Blakedbb38ca2019-08-23 09:37:22 -0500520 bool readonly = false;
thscd831bd2008-07-03 10:23:51 +0000521 bool disconnect = false;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000522 const char *bindto = NULL;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100523 const char *port = NULL;
524 char *sockpath = NULL;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100525 char *device = NULL;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800526 QemuOpts *sn_opts = NULL;
527 const char *sn_id_or_name = NULL;
Eric Blakedbc7b012020-10-27 00:05:55 -0500528 const char *sopt = "hVb:o:p:rsnc:dvk:e:f:tl:x:T:D:AB:L";
bellard7a5ca862008-05-27 21:13:40 +0000529 struct option lopt[] = {
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000530 { "help", no_argument, NULL, 'h' },
531 { "version", no_argument, NULL, 'V' },
532 { "bind", required_argument, NULL, 'b' },
533 { "port", required_argument, NULL, 'p' },
534 { "socket", required_argument, NULL, 'k' },
535 { "offset", required_argument, NULL, 'o' },
536 { "read-only", no_argument, NULL, 'r' },
Eric Blakedbc7b012020-10-27 00:05:55 -0500537 { "allocation-depth", no_argument, NULL, 'A' },
Eric Blake636192c2019-01-11 13:47:20 -0600538 { "bitmap", required_argument, NULL, 'B' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000539 { "connect", required_argument, NULL, 'c' },
540 { "disconnect", no_argument, NULL, 'd' },
Eric Blake68b96f12019-01-17 13:36:56 -0600541 { "list", no_argument, NULL, 'L' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000542 { "snapshot", no_argument, NULL, 's' },
543 { "load-snapshot", required_argument, NULL, 'l' },
544 { "nocache", no_argument, NULL, 'n' },
545 { "cache", required_argument, NULL, QEMU_NBD_OPT_CACHE },
546 { "aio", required_argument, NULL, QEMU_NBD_OPT_AIO },
547 { "discard", required_argument, NULL, QEMU_NBD_OPT_DISCARD },
548 { "detect-zeroes", required_argument, NULL,
549 QEMU_NBD_OPT_DETECT_ZEROES },
550 { "shared", required_argument, NULL, 'e' },
551 { "format", required_argument, NULL, 'f' },
552 { "persistent", no_argument, NULL, 't' },
553 { "verbose", no_argument, NULL, 'v' },
554 { "object", required_argument, NULL, QEMU_NBD_OPT_OBJECT },
555 { "export-name", required_argument, NULL, 'x' },
Eric Blakeb1a75b32016-10-14 13:33:03 -0500556 { "description", required_argument, NULL, 'D' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000557 { "tls-creds", required_argument, NULL, QEMU_NBD_OPT_TLSCREDS },
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000558 { "tls-authz", required_argument, NULL, QEMU_NBD_OPT_TLSAUTHZ },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000559 { "image-opts", no_argument, NULL, QEMU_NBD_OPT_IMAGE_OPTS },
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300560 { "trace", required_argument, NULL, 'T' },
Max Reitzffb31e12016-09-28 22:46:42 +0200561 { "fork", no_argument, NULL, QEMU_NBD_OPT_FORK },
Max Reitz637bc5a2019-05-08 23:18:16 +0200562 { "pid-file", required_argument, NULL, QEMU_NBD_OPT_PID_FILE },
Blue Swirl660f11b2009-07-31 21:16:51 +0000563 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000564 };
565 int ch;
566 int opt_ind = 0;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200567 int flags = BDRV_O_RDWR;
Max Reitz4fbec262015-02-05 13:58:19 -0500568 int ret = 0;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200569 bool seen_cache = false;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100570 bool seen_discard = false;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200571 bool seen_aio = false;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100572 pthread_t client_thread;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000573 const char *fmt = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200574 Error *local_err = NULL;
Peter Lievenb3838a42014-08-13 19:20:18 +0200575 BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
Max Reitz4fbec262015-02-05 13:58:19 -0500576 QDict *options = NULL;
Eric Blake68b96f12019-01-17 13:36:56 -0600577 const char *export_name = NULL; /* defaults to "" later for server mode */
Eric Blakeb1a75b32016-10-14 13:33:03 -0500578 const char *export_description = NULL;
Eric Blakecbad81c2020-10-27 00:05:49 -0500579 strList *bitmaps = NULL;
Eric Blakedbc7b012020-10-27 00:05:55 -0500580 bool alloc_depth = false;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000581 const char *tlscredsid = NULL;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000582 bool imageOpts = false;
Kevin Wolf6effd5b2016-03-14 11:43:28 +0100583 bool writethrough = true;
Max Reitzffb31e12016-09-28 22:46:42 +0200584 bool fork_process = false;
Eric Blake68b96f12019-01-17 13:36:56 -0600585 bool list = false;
Max Reitzffb31e12016-09-28 22:46:42 +0200586 int old_stderr = -1;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000587 unsigned socket_activation;
Max Reitz637bc5a2019-05-08 23:18:16 +0200588 const char *pid_file_name = NULL;
Kevin Wolf00917172020-09-24 17:26:57 +0200589 BlockExportOptions *export_opts;
bellard7a5ca862008-05-27 21:13:40 +0000590
Eric Blake029a88c2020-09-30 07:11:01 -0500591#ifdef CONFIG_POSIX
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +0100592 os_setup_early_signal_handling();
593 os_setup_signal_handling();
Max Reitz041e32b2017-06-11 14:37:14 +0200594#endif
595
Daniel P. Berrangé98c5d2e2020-08-25 11:38:48 +0100596 socket_init();
Christophe Fergeauf5852ef2019-01-31 17:46:14 +0100597 error_init(argv[0]);
Daniel P. Berrangefe4db842016-10-04 14:35:52 +0100598 module_call_init(MODULE_INIT_TRACE);
Eduardo Habkoste8f2d272016-05-12 11:10:04 -0300599 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100600
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000601 module_call_init(MODULE_INIT_QOM);
602 qemu_add_opts(&qemu_object_opts);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300603 qemu_add_opts(&qemu_trace_opts);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800604 qemu_init_exec_dir(argv[0]);
Paolo Bonzinibb345112011-11-04 15:51:19 +0100605
bellard7a5ca862008-05-27 21:13:40 +0000606 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
607 switch (ch) {
608 case 's':
ths2f726482008-07-03 11:47:46 +0000609 flags |= BDRV_O_SNAPSHOT;
610 break;
611 case 'n':
Paolo Bonzini39a52352012-07-18 14:57:15 +0200612 optarg = (char *) "none";
613 /* fallthrough */
614 case QEMU_NBD_OPT_CACHE:
615 if (seen_cache) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100616 error_report("-n and --cache can only be specified once");
617 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200618 }
619 seen_cache = true;
Kevin Wolf6effd5b2016-03-14 11:43:28 +0100620 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100621 error_report("Invalid cache mode `%s'", optarg);
622 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200623 }
bellard7a5ca862008-05-27 21:13:40 +0000624 break;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200625 case QEMU_NBD_OPT_AIO:
626 if (seen_aio) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100627 error_report("--aio can only be specified once");
628 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200629 }
630 seen_aio = true;
Aarushi Mehta76802742020-01-20 14:18:56 +0000631 if (bdrv_parse_aio(optarg, &flags) < 0) {
632 error_report("Invalid aio mode '%s'", optarg);
633 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200634 }
635 break;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100636 case QEMU_NBD_OPT_DISCARD:
637 if (seen_discard) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100638 error_report("--discard can only be specified once");
639 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100640 }
641 seen_discard = true;
642 if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100643 error_report("Invalid discard mode `%s'", optarg);
644 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100645 }
646 break;
Peter Lievenb3838a42014-08-13 19:20:18 +0200647 case QEMU_NBD_OPT_DETECT_ZEROES:
648 detect_zeroes =
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200649 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
Peter Lievenb3838a42014-08-13 19:20:18 +0200650 optarg,
Peter Lievenb3838a42014-08-13 19:20:18 +0200651 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
652 &local_err);
653 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100654 error_reportf_err(local_err,
655 "Failed to parse detect_zeroes mode: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100656 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200657 }
658 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
659 !(flags & BDRV_O_UNMAP)) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100660 error_report("setting detect-zeroes to unmap is not allowed "
661 "without setting discard operation to unmap");
662 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200663 }
664 break;
bellard7a5ca862008-05-27 21:13:40 +0000665 case 'b':
666 bindto = optarg;
667 break;
668 case 'p':
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100669 port = optarg;
bellard7a5ca862008-05-27 21:13:40 +0000670 break;
671 case 'o':
Eric Blake43b51012019-01-17 13:36:44 -0600672 if (qemu_strtou64(optarg, NULL, 0, &dev_offset) < 0) {
673 error_report("Invalid offset '%s'", optarg);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100674 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000675 }
bellard7a5ca862008-05-27 21:13:40 +0000676 break;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800677 case 'l':
678 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +0100679 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
680 optarg, false);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800681 if (!sn_opts) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100682 error_report("Failed in parsing snapshot param `%s'",
683 optarg);
684 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800685 }
686 } else {
687 sn_id_or_name = optarg;
688 }
689 /* fall through */
bellard7a5ca862008-05-27 21:13:40 +0000690 case 'r':
Eric Blakedbb38ca2019-08-23 09:37:22 -0500691 readonly = true;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200692 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000693 break;
Eric Blakedbc7b012020-10-27 00:05:55 -0500694 case 'A':
695 alloc_depth = true;
696 break;
Eric Blake636192c2019-01-11 13:47:20 -0600697 case 'B':
Eric Blakecbad81c2020-10-27 00:05:49 -0500698 QAPI_LIST_PREPEND(bitmaps, g_strdup(optarg));
Eric Blake636192c2019-01-11 13:47:20 -0600699 break;
thscd831bd2008-07-03 10:23:51 +0000700 case 'k':
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100701 sockpath = optarg;
Peter Lieven713cc672014-08-13 19:20:19 +0200702 if (sockpath[0] != '/') {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +0100703 error_report("socket path must be absolute");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100704 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200705 }
thscd831bd2008-07-03 10:23:51 +0000706 break;
707 case 'd':
708 disconnect = true;
709 break;
710 case 'c':
711 device = optarg;
712 break;
ths3b05a8e2008-07-03 12:45:02 +0000713 case 'e':
Eric Blake43b51012019-01-17 13:36:44 -0600714 if (qemu_strtoi(optarg, NULL, 0, &shared) < 0 ||
715 shared < 1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100716 error_report("Invalid shared device number '%s'", optarg);
717 exit(EXIT_FAILURE);
ths3b05a8e2008-07-03 12:45:02 +0000718 }
ths3b05a8e2008-07-03 12:45:02 +0000719 break;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000720 case 'f':
721 fmt = optarg;
722 break;
Peter Lieven713cc672014-08-13 19:20:19 +0200723 case 't':
724 persistent = 1;
725 break;
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000726 case 'x':
727 export_name = optarg;
Eric Blake93676c82019-11-13 20:46:34 -0600728 if (strlen(export_name) > NBD_MAX_STRING_SIZE) {
729 error_report("export name '%s' too long", export_name);
730 exit(EXIT_FAILURE);
731 }
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000732 break;
Eric Blakeb1a75b32016-10-14 13:33:03 -0500733 case 'D':
734 export_description = optarg;
Eric Blake93676c82019-11-13 20:46:34 -0600735 if (strlen(export_description) > NBD_MAX_STRING_SIZE) {
736 error_report("export description '%s' too long",
737 export_description);
738 exit(EXIT_FAILURE);
739 }
Eric Blakeb1a75b32016-10-14 13:33:03 -0500740 break;
bellard7a5ca862008-05-27 21:13:40 +0000741 case 'v':
742 verbose = 1;
743 break;
744 case 'V':
745 version(argv[0]);
746 exit(0);
747 break;
748 case 'h':
749 usage(argv[0]);
750 exit(0);
751 break;
752 case '?':
Markus Armbruster85b01e02015-12-18 16:35:04 +0100753 error_report("Try `%s --help' for more information.", argv[0]);
754 exit(EXIT_FAILURE);
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000755 case QEMU_NBD_OPT_OBJECT: {
756 QemuOpts *opts;
757 opts = qemu_opts_parse_noisily(&qemu_object_opts,
758 optarg, true);
759 if (!opts) {
760 exit(EXIT_FAILURE);
761 }
762 } break;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000763 case QEMU_NBD_OPT_TLSCREDS:
764 tlscredsid = optarg;
765 break;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000766 case QEMU_NBD_OPT_IMAGE_OPTS:
767 imageOpts = true;
768 break;
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300769 case 'T':
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500770 trace_opt_parse(optarg);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300771 break;
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000772 case QEMU_NBD_OPT_TLSAUTHZ:
773 tlsauthz = optarg;
774 break;
Max Reitzffb31e12016-09-28 22:46:42 +0200775 case QEMU_NBD_OPT_FORK:
776 fork_process = true;
777 break;
Eric Blake68b96f12019-01-17 13:36:56 -0600778 case 'L':
779 list = true;
780 break;
Max Reitz637bc5a2019-05-08 23:18:16 +0200781 case QEMU_NBD_OPT_PID_FILE:
782 pid_file_name = optarg;
783 break;
bellard7a5ca862008-05-27 21:13:40 +0000784 }
785 }
786
Eric Blake68b96f12019-01-17 13:36:56 -0600787 if (list) {
788 if (argc != optind) {
789 error_report("List mode is incompatible with a file name");
790 exit(EXIT_FAILURE);
791 }
Eric Blake0bc16992020-01-23 10:46:50 -0600792 if (export_name || export_description || dev_offset ||
Eric Blakecbad81c2020-10-27 00:05:49 -0500793 device || disconnect || fmt || sn_id_or_name || bitmaps ||
Eric Blakedbc7b012020-10-27 00:05:55 -0500794 alloc_depth || seen_aio || seen_discard || seen_cache) {
Eric Blake68b96f12019-01-17 13:36:56 -0600795 error_report("List mode is incompatible with per-device settings");
796 exit(EXIT_FAILURE);
797 }
798 if (fork_process) {
799 error_report("List mode is incompatible with forking");
800 exit(EXIT_FAILURE);
801 }
802 } else if ((argc - optind) != 1) {
Markus Armbruster433672b2015-12-18 16:35:24 +0100803 error_report("Invalid number of arguments");
804 error_printf("Try `%s --help' for more information.\n", argv[0]);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100805 exit(EXIT_FAILURE);
Eric Blake68b96f12019-01-17 13:36:56 -0600806 } else if (!export_name) {
807 export_name = "";
bellard7a5ca862008-05-27 21:13:40 +0000808 }
809
Markus Armbruster7e1e0c12018-10-17 10:26:43 +0200810 qemu_opts_foreach(&qemu_object_opts,
811 user_creatable_add_opts_foreach,
Kevin Wolf495bf892019-10-11 21:49:17 +0200812 qemu_nbd_object_print_help, &error_fatal);
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000813
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300814 if (!trace_init_backends()) {
815 exit(1);
816 }
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500817 trace_init_file();
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300818 qemu_set_log(LOG_TRACE);
819
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000820 socket_activation = check_socket_activation();
821 if (socket_activation == 0) {
822 setup_address_and_port(&bindto, &port);
823 } else {
824 /* Using socket activation - check user didn't use -p etc. */
825 const char *err_msg = socket_activation_validate_opts(device, sockpath,
Eric Blake68b96f12019-01-17 13:36:56 -0600826 bindto, port,
827 list);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000828 if (err_msg != NULL) {
829 error_report("%s", err_msg);
830 exit(EXIT_FAILURE);
831 }
Paolo Bonzini53fabd42017-03-16 16:29:45 +0100832
833 /* qemu-nbd can only listen on a single socket. */
834 if (socket_activation > 1) {
835 error_report("qemu-nbd does not support socket activation with %s > 1",
836 "LISTEN_FDS");
837 exit(EXIT_FAILURE);
838 }
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000839 }
840
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000841 if (tlscredsid) {
842 if (sockpath) {
843 error_report("TLS is only supported with IPv4/IPv6");
844 exit(EXIT_FAILURE);
845 }
846 if (device) {
847 error_report("TLS is not supported with a host device");
848 exit(EXIT_FAILURE);
849 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000850 if (tlsauthz && list) {
851 error_report("TLS authorization is incompatible with export list");
852 exit(EXIT_FAILURE);
853 }
Eric Blake68b96f12019-01-17 13:36:56 -0600854 tlscreds = nbd_get_tls_creds(tlscredsid, list, &local_err);
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000855 if (local_err) {
Markus Armbruster5217f182020-05-05 12:19:03 +0200856 error_reportf_err(local_err, "Failed to get TLS creds: ");
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000857 exit(EXIT_FAILURE);
858 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000859 } else {
860 if (tlsauthz) {
861 error_report("--tls-authz is not permitted without --tls-creds");
862 exit(EXIT_FAILURE);
863 }
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000864 }
865
Eric Blake68b96f12019-01-17 13:36:56 -0600866 if (list) {
867 saddr = nbd_build_socket_address(sockpath, bindto, port);
868 return qemu_nbd_client_list(saddr, tlscreds, bindto);
869 }
870
Eric Blake3c1fa352018-12-15 07:53:08 -0600871#if !HAVE_NBD_DEVICE
872 if (disconnect || device) {
873 error_report("Kernel /dev/nbdN support not available");
874 exit(EXIT_FAILURE);
875 }
876#else /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000877 if (disconnect) {
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000878 int nbdfd = open(argv[optind], O_RDWR);
879 if (nbdfd < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100880 error_report("Cannot open %s: %s", argv[optind],
881 strerror(errno));
882 exit(EXIT_FAILURE);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100883 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000884 nbd_disconnect(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000885
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000886 close(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000887
888 printf("%s disconnected\n", argv[optind]);
889
Peter Lieven713cc672014-08-13 19:20:19 +0200890 return 0;
thscd831bd2008-07-03 10:23:51 +0000891 }
Eric Blake3c1fa352018-12-15 07:53:08 -0600892#endif
thscd831bd2008-07-03 10:23:51 +0000893
Max Reitzffb31e12016-09-28 22:46:42 +0200894 if ((device && !verbose) || fork_process) {
Daniel P. Berrangéeb705982020-08-25 11:38:50 +0100895#ifndef WIN32
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100896 int stderr_fd[2];
897 pid_t pid;
898 int ret;
899
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100900 if (qemu_pipe(stderr_fd) < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100901 error_report("Error setting up communication pipe: %s",
902 strerror(errno));
903 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100904 }
905
906 /* Now daemonize, but keep a communication channel open to
907 * print errors and exit with the proper status code.
908 */
909 pid = fork();
Max Reitz70d47392015-02-25 13:08:22 -0500910 if (pid < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100911 error_report("Failed to fork: %s", strerror(errno));
912 exit(EXIT_FAILURE);
Max Reitz70d47392015-02-25 13:08:22 -0500913 } else if (pid == 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100914 close(stderr_fd[0]);
Max Reitze6df58a2019-05-08 23:18:18 +0200915
Raphael Pour0eaf4532020-05-15 08:36:07 +0200916 /* Remember parent's stderr if we will be restoring it. */
917 if (fork_process) {
918 old_stderr = dup(STDERR_FILENO);
919 }
920
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400921 ret = qemu_daemon(1, 0);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100922
923 /* Temporarily redirect stderr to the parent's pipe... */
924 dup2(stderr_fd[1], STDERR_FILENO);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100925 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100926 error_report("Failed to daemonize: %s", strerror(errno));
927 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100928 }
929
930 /* ... close the descriptor we inherited and go on. */
931 close(stderr_fd[1]);
932 } else {
933 bool errors = false;
934 char *buf;
935
936 /* In the parent. Print error messages from the child until
937 * it closes the pipe.
938 */
939 close(stderr_fd[1]);
940 buf = g_malloc(1024);
941 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
942 errors = true;
943 ret = qemu_write_full(STDERR_FILENO, buf, ret);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100944 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100945 exit(EXIT_FAILURE);
946 }
947 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100948 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100949 error_report("Cannot read from daemon: %s",
950 strerror(errno));
951 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100952 }
953
954 /* Usually the daemon should not print any message.
955 * Exit with zero status in that case.
956 */
957 exit(errors);
958 }
Daniel P. Berrangéeb705982020-08-25 11:38:50 +0100959#else /* WIN32 */
960 error_report("Unable to fork into background on Windows hosts");
961 exit(EXIT_FAILURE);
962#endif /* WIN32 */
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100963 }
964
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100965 if (device != NULL && sockpath == NULL) {
966 sockpath = g_malloc(128);
967 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
thscd831bd2008-07-03 10:23:51 +0000968 }
969
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000970 server = qio_net_listener_new();
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000971 if (socket_activation == 0) {
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000972 saddr = nbd_build_socket_address(sockpath, bindto, port);
Juan Quintelafc8135c2019-08-19 18:08:21 +0200973 if (qio_net_listener_open_sync(server, saddr, 1, &local_err) < 0) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000974 object_unref(OBJECT(server));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000975 error_report_err(local_err);
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000976 exit(EXIT_FAILURE);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000977 }
978 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000979 size_t i;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000980 /* See comment in check_socket_activation above. */
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000981 for (i = 0; i < socket_activation; i++) {
982 QIOChannelSocket *sioc;
983 sioc = qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD + i,
984 &local_err);
985 if (sioc == NULL) {
986 object_unref(OBJECT(server));
Markus Armbruster5217f182020-05-05 12:19:03 +0200987 error_reportf_err(local_err,
988 "Failed to use socket activation: ");
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000989 exit(EXIT_FAILURE);
990 }
991 qio_net_listener_add(server, sioc);
992 object_unref(OBJECT(sioc));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000993 }
994 }
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100995
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300996 if (qemu_init_main_loop(&local_err)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100997 error_report_err(local_err);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300998 exit(EXIT_FAILURE);
999 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001000 bdrv_init();
Kevin Wolfb3b52992018-05-16 13:46:37 +02001001 atexit(qemu_nbd_shutdown);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001002
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001003 srcpath = argv[optind];
1004 if (imageOpts) {
1005 QemuOpts *opts;
1006 if (fmt) {
1007 error_report("--image-opts and -f are mutually exclusive");
1008 exit(EXIT_FAILURE);
1009 }
1010 opts = qemu_opts_parse_noisily(&file_opts, srcpath, true);
1011 if (!opts) {
1012 qemu_opts_reset(&file_opts);
1013 exit(EXIT_FAILURE);
1014 }
1015 options = qemu_opts_to_qdict(opts, NULL);
1016 qemu_opts_reset(&file_opts);
Max Reitzefaa7c42016-03-16 19:54:38 +01001017 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001018 } else {
1019 if (fmt) {
1020 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05001021 qdict_put_str(options, "driver", fmt);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001022 }
Max Reitzefaa7c42016-03-16 19:54:38 +01001023 blk = blk_new_open(srcpath, NULL, options, flags, &local_err);
Daniel P. Berrangee6b63672013-03-19 11:20:20 +00001024 }
1025
Max Reitz4fbec262015-02-05 13:58:19 -05001026 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001027 error_reportf_err(local_err, "Failed to blk_new_open '%s': ",
1028 argv[optind]);
Markus Armbruster85b01e02015-12-18 16:35:04 +01001029 exit(EXIT_FAILURE);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001030 }
Max Reitz4fbec262015-02-05 13:58:19 -05001031 bs = blk_bs(blk);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001032
Kevin Wolfb57e4de2020-09-24 17:26:52 +02001033 if (dev_offset) {
1034 QDict *raw_opts = qdict_new();
1035 qdict_put_str(raw_opts, "driver", "raw");
1036 qdict_put_str(raw_opts, "file", bs->node_name);
1037 qdict_put_int(raw_opts, "offset", dev_offset);
1038 bs = bdrv_open(NULL, NULL, raw_opts, flags, &error_fatal);
1039 blk_remove_bs(blk);
1040 blk_insert_bs(blk, bs, &error_fatal);
1041 bdrv_unref(bs);
1042 }
1043
Kevin Wolf6effd5b2016-03-14 11:43:28 +01001044 blk_set_enable_write_cache(blk, !writethrough);
1045
Wenchao Xia8c116b02013-12-04 17:10:55 +08001046 if (sn_opts) {
1047 ret = bdrv_snapshot_load_tmp(bs,
1048 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1049 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1050 &local_err);
1051 } else if (sn_id_or_name) {
1052 ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
1053 &local_err);
1054 }
1055 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001056 error_reportf_err(local_err, "Failed to load snapshot: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +01001057 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001058 }
1059
Peter Lievenb3838a42014-08-13 19:20:18 +02001060 bs->detect_zeroes = detect_zeroes;
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001061
Kevin Wolf00917172020-09-24 17:26:57 +02001062 nbd_server_is_qemu_nbd(true);
1063
1064 export_opts = g_new(BlockExportOptions, 1);
1065 *export_opts = (BlockExportOptions) {
1066 .type = BLOCK_EXPORT_TYPE_NBD,
Kevin Wolfd53be9c2020-09-24 17:27:04 +02001067 .id = g_strdup("qemu-nbd-export"),
Kevin Wolfb6076af2020-09-24 17:27:01 +02001068 .node_name = g_strdup(bdrv_get_node_name(bs)),
Kevin Wolf00917172020-09-24 17:26:57 +02001069 .has_writethrough = true,
1070 .writethrough = writethrough,
Kevin Wolf30dbc812020-09-24 17:27:11 +02001071 .has_writable = true,
1072 .writable = !readonly,
Kevin Wolf00917172020-09-24 17:26:57 +02001073 .u.nbd = {
Eric Blakecbad81c2020-10-27 00:05:49 -05001074 .has_name = true,
1075 .name = g_strdup(export_name),
1076 .has_description = !!export_description,
1077 .description = g_strdup(export_description),
1078 .has_bitmaps = !!bitmaps,
1079 .bitmaps = bitmaps,
Eric Blakedbc7b012020-10-27 00:05:55 -05001080 .has_allocation_depth = alloc_depth,
1081 .allocation_depth = alloc_depth,
Kevin Wolf00917172020-09-24 17:26:57 +02001082 },
1083 };
1084 blk_exp_add(export_opts, &error_fatal);
1085 qapi_free_BlockExportOptions(export_opts);
ths3b05a8e2008-07-03 12:45:02 +00001086
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001087 if (device) {
Eric Blake3c1fa352018-12-15 07:53:08 -06001088#if HAVE_NBD_DEVICE
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001089 int ret;
1090
Paolo Bonzinia6ac2312011-12-06 09:07:00 +01001091 ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001092 if (ret != 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001093 error_report("Failed to create client thread: %s", strerror(ret));
1094 exit(EXIT_FAILURE);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001095 }
Eric Blake3c1fa352018-12-15 07:53:08 -06001096#endif
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001097 } else {
1098 /* Shut up GCC warnings. */
1099 memset(&client_thread, 0, sizeof(client_thread));
1100 }
1101
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +00001102 nbd_update_server_watch();
bellard7a5ca862008-05-27 21:13:40 +00001103
Max Reitz637bc5a2019-05-08 23:18:16 +02001104 if (pid_file_name) {
1105 qemu_write_pidfile(pid_file_name, &error_fatal);
1106 }
1107
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001108 /* now when the initialization is (almost) complete, chdir("/")
1109 * to free any busy filesystems */
1110 if (chdir("/") < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001111 error_report("Could not chdir to root directory: %s",
1112 strerror(errno));
1113 exit(EXIT_FAILURE);
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001114 }
1115
Max Reitzffb31e12016-09-28 22:46:42 +02001116 if (fork_process) {
1117 dup2(old_stderr, STDERR_FILENO);
1118 close(old_stderr);
1119 }
1120
Paolo Bonzini7860a382012-09-18 13:31:56 +02001121 state = RUNNING;
ths3b05a8e2008-07-03 12:45:02 +00001122 do {
Paolo Bonzinia61c6782011-09-12 17:28:11 +02001123 main_loop_wait(false);
Paolo Bonzini7860a382012-09-18 13:31:56 +02001124 if (state == TERMINATE) {
Kevin Wolfbc4ee652020-09-24 17:27:03 +02001125 blk_exp_close_all();
Kevin Wolfd794f7f2020-09-24 17:26:56 +02001126 state = TERMINATED;
Paolo Bonzini7860a382012-09-18 13:31:56 +02001127 }
1128 } while (state != TERMINATED);
ths3b05a8e2008-07-03 12:45:02 +00001129
Markus Armbruster26f54e92014-10-07 13:59:04 +02001130 blk_unref(blk);
Paolo Bonzinib32f6c22011-11-04 15:51:20 +01001131 if (sockpath) {
1132 unlink(sockpath);
1133 }
bellard7a5ca862008-05-27 21:13:40 +00001134
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02001135 qemu_opts_del(sn_opts);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001136
Paolo Bonzinia517e882011-11-04 15:51:21 +01001137 if (device) {
1138 void *ret;
1139 pthread_join(client_thread, &ret);
1140 exit(ret != NULL);
1141 } else {
1142 exit(EXIT_SUCCESS);
1143 }
bellard7a5ca862008-05-27 21:13:40 +00001144}