blob: 023eacd1c8eaa182f5e7abf1a67b5b4bddfb7fc6 [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
Stefan Weil5a61cb62011-09-08 17:55:32 +020019#include "qemu-common.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020020#include "sysemu/block-backend.h"
Peter Lievenb3838a42014-08-13 19:20:18 +020021#include "block/block_int.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010022#include "block/nbd.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010023#include "qemu/main-loop.h"
Paolo Bonzini537b41f2014-02-17 14:43:51 +010024#include "qemu/sockets.h"
25#include "qemu/error-report.h"
Wenchao Xia8c116b02013-12-04 17:10:55 +080026#include "block/snapshot.h"
Peter Lievenb3838a42014-08-13 19:20:18 +020027#include "qapi/util.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010028#include "qapi/qmp/qstring.h"
bellard7a5ca862008-05-27 21:13:40 +000029
bellard7a5ca862008-05-27 21:13:40 +000030#include <stdarg.h>
31#include <stdio.h>
32#include <getopt.h>
thscd831bd2008-07-03 10:23:51 +000033#include <sys/types.h>
bellard7a5ca862008-05-27 21:13:40 +000034#include <sys/socket.h>
35#include <netinet/in.h>
36#include <netinet/tcp.h>
37#include <arpa/inet.h>
thscd831bd2008-07-03 10:23:51 +000038#include <signal.h>
Blue Swirl2bff4b62009-12-23 15:34:04 +000039#include <libgen.h>
Paolo Bonzinia517e882011-11-04 15:51:21 +010040#include <pthread.h>
thscd831bd2008-07-03 10:23:51 +000041
Peter Lieven713cc672014-08-13 19:20:19 +020042#define SOCKET_PATH "/var/lock/qemu-nbd-%s"
43#define QEMU_NBD_OPT_CACHE 1
44#define QEMU_NBD_OPT_AIO 2
45#define QEMU_NBD_OPT_DISCARD 3
Peter Lievenb3838a42014-08-13 19:20:18 +020046#define QEMU_NBD_OPT_DETECT_ZEROES 4
bellard7a5ca862008-05-27 21:13:40 +000047
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +020048static NBDExport *exp;
blueswir1b1d8e522008-10-26 13:43:07 +000049static int verbose;
Paolo Bonzinia517e882011-11-04 15:51:21 +010050static char *srcpath;
Daniel P. Berrange48bec072015-09-16 14:52:23 +010051static SocketAddress *saddr;
Paolo Bonzini7860a382012-09-18 13:31:56 +020052static int persistent = 0;
53static enum { RUNNING, TERMINATE, TERMINATING, TERMINATED } state;
Paolo Bonzinia61c6782011-09-12 17:28:11 +020054static int shared = 1;
55static int nb_fds;
Fam Zhenge4afbf42015-05-19 10:50:59 +000056static int server_fd;
bellard7a5ca862008-05-27 21:13:40 +000057
58static void usage(const char *name)
59{
Paolo Bonzinib033cd82012-07-18 14:50:52 +020060 (printf) (
bellard7a5ca862008-05-27 21:13:40 +000061"Usage: %s [OPTIONS] FILE\n"
62"QEMU Disk Network Block Device Server\n"
63"\n"
Peter Lieven713cc672014-08-13 19:20:19 +020064" -h, --help display this help and exit\n"
65" -V, --version output version information and exit\n"
bellard7a5ca862008-05-27 21:13:40 +000066"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020067"Connection properties:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020068" -p, --port=PORT port to listen on (default `%d')\n"
69" -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
70" -k, --socket=PATH path to the unix socket\n"
71" (default '"SOCKET_PATH"')\n"
72" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
73" -t, --persistent don't exit on the last connection\n"
74" -v, --verbose display extra debugging information\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020075"\n"
76"Exposing part of the image:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020077" -o, --offset=OFFSET offset into the image\n"
78" -P, --partition=NUM only expose partition NUM\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020079"\n"
80#ifdef __linux__
81"Kernel NBD client support:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020082" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
83" -d, --disconnect disconnect the specified device\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020084"\n"
85#endif
86"\n"
87"Block device options:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020088" -f, --format=FORMAT set image format (raw, qcow2, ...)\n"
89" -r, --read-only export read-only\n"
90" -s, --snapshot use FILE as an external snapshot, create a temporary\n"
91" file with backing_file=FILE, redirect the write to\n"
92" the temporary one\n"
Wenchao Xia8c116b02013-12-04 17:10:55 +080093" -l, --load-snapshot=SNAPSHOT_PARAM\n"
Peter Lieven713cc672014-08-13 19:20:19 +020094" load an internal snapshot inside FILE and export it\n"
95" as an read-only device, SNAPSHOT_PARAM format is\n"
96" 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
97" '[ID_OR_NAME]'\n"
98" -n, --nocache disable host cache\n"
99" --cache=MODE set cache mode (none, writeback, ...)\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200100" --aio=MODE set AIO mode (native or threads)\n"
Peter Lievenb3838a42014-08-13 19:20:18 +0200101" --discard=MODE set discard mode (ignore, unmap)\n"
Andrey Korolyov6883de62015-07-31 22:12:54 +0300102" --detect-zeroes=MODE set detect-zeroes mode (off, on, unmap)\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200103"\n"
104"Report bugs to <qemu-devel@nongnu.org>\n"
Laurent Vivierc2e28722010-09-17 20:37:25 +0200105 , name, NBD_DEFAULT_PORT, "DEVICE");
bellard7a5ca862008-05-27 21:13:40 +0000106}
107
108static void version(const char *name)
109{
110 printf(
ths315bc7a2008-07-18 18:06:23 +0000111"%s version 0.0.1\n"
bellard7a5ca862008-05-27 21:13:40 +0000112"Written by Anthony Liguori.\n"
113"\n"
114"Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
115"This is free software; see the source for copying conditions. There is NO\n"
116"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
ths315bc7a2008-07-18 18:06:23 +0000117 , name);
bellard7a5ca862008-05-27 21:13:40 +0000118}
119
120struct partition_record
121{
122 uint8_t bootable;
123 uint8_t start_head;
124 uint32_t start_cylinder;
125 uint8_t start_sector;
126 uint8_t system;
127 uint8_t end_head;
128 uint8_t end_cylinder;
129 uint8_t end_sector;
130 uint32_t start_sector_abs;
131 uint32_t nb_sectors_abs;
132};
133
134static void read_partition(uint8_t *p, struct partition_record *r)
135{
136 r->bootable = p[0];
137 r->start_head = p[1];
138 r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
139 r->start_sector = p[2] & 0x3f;
140 r->system = p[4];
141 r->end_head = p[5];
142 r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
143 r->end_sector = p[6] & 0x3f;
Max Reitzac973932015-02-25 13:08:23 -0500144
145 r->start_sector_abs = le32_to_cpup((uint32_t *)(p + 8));
146 r->nb_sectors_abs = le32_to_cpup((uint32_t *)(p + 12));
bellard7a5ca862008-05-27 21:13:40 +0000147}
148
Max Reitz4c58e802014-11-18 12:21:19 +0100149static int find_partition(BlockBackend *blk, int partition,
bellard7a5ca862008-05-27 21:13:40 +0000150 off_t *offset, off_t *size)
151{
152 struct partition_record mbr[4];
153 uint8_t data[512];
154 int i;
155 int ext_partnum = 4;
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900156 int ret;
bellard7a5ca862008-05-27 21:13:40 +0000157
Max Reitz4c58e802014-11-18 12:21:19 +0100158 if ((ret = blk_read(blk, 0, data, 1)) < 0) {
Markus Armbrustera4699e52015-12-18 16:35:10 +0100159 error_report("error while reading: %s", strerror(-ret));
Markus Armbruster85b01e02015-12-18 16:35:04 +0100160 exit(EXIT_FAILURE);
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900161 }
bellard7a5ca862008-05-27 21:13:40 +0000162
163 if (data[510] != 0x55 || data[511] != 0xaa) {
Paolo Bonzini185b4332012-03-05 08:56:10 +0100164 return -EINVAL;
bellard7a5ca862008-05-27 21:13:40 +0000165 }
166
167 for (i = 0; i < 4; i++) {
168 read_partition(&data[446 + 16 * i], &mbr[i]);
169
Max Reitz453b07b2015-02-25 13:08:15 -0500170 if (!mbr[i].system || !mbr[i].nb_sectors_abs) {
bellard7a5ca862008-05-27 21:13:40 +0000171 continue;
Max Reitz453b07b2015-02-25 13:08:15 -0500172 }
bellard7a5ca862008-05-27 21:13:40 +0000173
174 if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
175 struct partition_record ext[4];
176 uint8_t data1[512];
177 int j;
178
Max Reitz4c58e802014-11-18 12:21:19 +0100179 if ((ret = blk_read(blk, mbr[i].start_sector_abs, data1, 1)) < 0) {
Markus Armbrustera4699e52015-12-18 16:35:10 +0100180 error_report("error while reading: %s", strerror(-ret));
Markus Armbruster85b01e02015-12-18 16:35:04 +0100181 exit(EXIT_FAILURE);
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900182 }
bellard7a5ca862008-05-27 21:13:40 +0000183
184 for (j = 0; j < 4; j++) {
185 read_partition(&data1[446 + 16 * j], &ext[j]);
Max Reitz453b07b2015-02-25 13:08:15 -0500186 if (!ext[j].system || !ext[j].nb_sectors_abs) {
bellard7a5ca862008-05-27 21:13:40 +0000187 continue;
Max Reitz453b07b2015-02-25 13:08:15 -0500188 }
bellard7a5ca862008-05-27 21:13:40 +0000189
190 if ((ext_partnum + j + 1) == partition) {
191 *offset = (uint64_t)ext[j].start_sector_abs << 9;
192 *size = (uint64_t)ext[j].nb_sectors_abs << 9;
193 return 0;
194 }
195 }
196 ext_partnum += 4;
197 } else if ((i + 1) == partition) {
198 *offset = (uint64_t)mbr[i].start_sector_abs << 9;
199 *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
200 return 0;
201 }
202 }
203
Paolo Bonzini185b4332012-03-05 08:56:10 +0100204 return -ENOENT;
bellard7a5ca862008-05-27 21:13:40 +0000205}
206
Paolo Bonzinibb345112011-11-04 15:51:19 +0100207static void termsig_handler(int signum)
208{
Paolo Bonzini7860a382012-09-18 13:31:56 +0200209 state = TERMINATE;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200210 qemu_notify_event();
Paolo Bonzinibb345112011-11-04 15:51:19 +0100211}
212
Paolo Bonzini537b41f2014-02-17 14:43:51 +0100213
Paolo Bonzinia517e882011-11-04 15:51:21 +0100214static void *show_parts(void *arg)
thscd831bd2008-07-03 10:23:51 +0000215{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100216 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100217 int nbd;
thscd831bd2008-07-03 10:23:51 +0000218
Paolo Bonzinia517e882011-11-04 15:51:21 +0100219 /* linux just needs an open() to trigger
220 * the partition table update
221 * but remember to load the module with max_part != 0 :
222 * modprobe nbd max_part=63
223 */
224 nbd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100225 if (nbd >= 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100226 close(nbd);
thscd831bd2008-07-03 10:23:51 +0000227 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100228 return NULL;
229}
230
231static void *nbd_client_thread(void *arg)
232{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100233 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100234 off_t size;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100235 uint32_t nbdflags;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100236 int fd, sock;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100237 int ret;
238 pthread_t show_parts_thread;
Max Reitz1ce52842015-01-26 21:02:59 -0500239 Error *local_error = NULL;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100240
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100241
242 sock = socket_connect(saddr, &local_error, NULL, NULL);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100243 if (sock < 0) {
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100244 error_report_err(local_error);
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000245 goto out;
246 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100247
248 ret = nbd_receive_negotiate(sock, NULL, &nbdflags,
Max Reitz3f472652015-02-25 13:08:25 -0500249 &size, &local_error);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100250 if (ret < 0) {
Max Reitz1ce52842015-01-26 21:02:59 -0500251 if (local_error) {
Markus Armbruster78288672015-12-18 16:35:07 +0100252 error_report_err(local_error);
Max Reitz1ce52842015-01-26 21:02:59 -0500253 }
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100254 goto out_socket;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100255 }
256
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100257 fd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100258 if (fd < 0) {
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100259 /* Linux-only, we can use %m in printf. */
Markus Armbrusterb9884682015-12-18 16:35:18 +0100260 error_report("Failed to open %s: %m", device);
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100261 goto out_socket;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100262 }
263
Max Reitz3f472652015-02-25 13:08:25 -0500264 ret = nbd_init(fd, sock, nbdflags, size);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100265 if (ret < 0) {
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100266 goto out_fd;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100267 }
268
269 /* update partition table */
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100270 pthread_create(&show_parts_thread, NULL, show_parts, device);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100271
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100272 if (verbose) {
273 fprintf(stderr, "NBD device %s is now connected to %s\n",
274 device, srcpath);
275 } else {
276 /* Close stderr so that the qemu-nbd process exits. */
277 dup2(STDOUT_FILENO, STDERR_FILENO);
278 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100279
280 ret = nbd_client(fd);
281 if (ret) {
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100282 goto out_fd;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100283 }
284 close(fd);
285 kill(getpid(), SIGTERM);
286 return (void *) EXIT_SUCCESS;
287
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100288out_fd:
289 close(fd);
290out_socket:
291 closesocket(sock);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100292out:
293 kill(getpid(), SIGTERM);
294 return (void *) EXIT_FAILURE;
thscd831bd2008-07-03 10:23:51 +0000295}
296
Fam Zhenge4afbf42015-05-19 10:50:59 +0000297static int nbd_can_accept(void)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200298{
299 return nb_fds < shared;
300}
301
Paolo Bonzini7860a382012-09-18 13:31:56 +0200302static void nbd_export_closed(NBDExport *exp)
303{
304 assert(state == TERMINATING);
305 state = TERMINATED;
306}
307
Fam Zhenge4afbf42015-05-19 10:50:59 +0000308static void nbd_update_server_fd_handler(int fd);
309
Paolo Bonzini1743b512011-09-19 14:33:23 +0200310static void nbd_client_closed(NBDClient *client)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200311{
Paolo Bonzini1743b512011-09-19 14:33:23 +0200312 nb_fds--;
Paolo Bonzini7860a382012-09-18 13:31:56 +0200313 if (nb_fds == 0 && !persistent && state == RUNNING) {
314 state = TERMINATE;
315 }
Fam Zhenge4afbf42015-05-19 10:50:59 +0000316 nbd_update_server_fd_handler(server_fd);
Paolo Bonzini7860a382012-09-18 13:31:56 +0200317 nbd_client_put(client);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200318}
319
320static void nbd_accept(void *opaque)
321{
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200322 struct sockaddr_in addr;
323 socklen_t addr_len = sizeof(addr);
324
325 int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100326 if (fd < 0) {
327 perror("accept");
328 return;
329 }
330
Paolo Bonzini7860a382012-09-18 13:31:56 +0200331 if (state >= TERMINATE) {
332 close(fd);
333 return;
334 }
335
Hani Benhabiles36af5992014-05-13 00:35:15 +0100336 if (nbd_client_new(exp, fd, nbd_client_closed)) {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200337 nb_fds++;
Fam Zhenge4afbf42015-05-19 10:50:59 +0000338 nbd_update_server_fd_handler(server_fd);
Hani Benhabiles36af5992014-05-13 00:35:15 +0100339 } else {
Hani Benhabiles27e5eae2014-05-31 22:39:42 +0100340 shutdown(fd, 2);
Hani Benhabiles36af5992014-05-13 00:35:15 +0100341 close(fd);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200342 }
343}
344
Fam Zhenge4afbf42015-05-19 10:50:59 +0000345static void nbd_update_server_fd_handler(int fd)
346{
347 if (nbd_can_accept()) {
348 qemu_set_fd_handler(fd, nbd_accept, NULL, (void *)(uintptr_t)fd);
349 } else {
350 qemu_set_fd_handler(fd, NULL, NULL, NULL);
351 }
352}
353
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100354
355static SocketAddress *nbd_build_socket_address(const char *sockpath,
356 const char *bindto,
357 const char *port)
358{
359 SocketAddress *saddr;
360
361 saddr = g_new0(SocketAddress, 1);
362 if (sockpath) {
Eric Blake2d32add2015-10-26 16:34:55 -0600363 saddr->type = SOCKET_ADDRESS_KIND_UNIX;
364 saddr->u.q_unix = g_new0(UnixSocketAddress, 1);
365 saddr->u.q_unix->path = g_strdup(sockpath);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100366 } else {
Eric Blake2d32add2015-10-26 16:34:55 -0600367 saddr->type = SOCKET_ADDRESS_KIND_INET;
368 saddr->u.inet = g_new0(InetSocketAddress, 1);
369 saddr->u.inet->host = g_strdup(bindto);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100370 if (port) {
Eric Blake2d32add2015-10-26 16:34:55 -0600371 saddr->u.inet->port = g_strdup(port);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100372 } else {
Eric Blake2d32add2015-10-26 16:34:55 -0600373 saddr->u.inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100374 }
375 }
376
377 return saddr;
378}
379
380
bellard7a5ca862008-05-27 21:13:40 +0000381int main(int argc, char **argv)
382{
Markus Armbruster26f54e92014-10-07 13:59:04 +0200383 BlockBackend *blk;
bellard7a5ca862008-05-27 21:13:40 +0000384 BlockDriverState *bs;
385 off_t dev_offset = 0;
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200386 uint32_t nbdflags = 0;
thscd831bd2008-07-03 10:23:51 +0000387 bool disconnect = false;
bellard7a5ca862008-05-27 21:13:40 +0000388 const char *bindto = "0.0.0.0";
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100389 const char *port = NULL;
390 char *sockpath = NULL;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100391 char *device = NULL;
bellard7a5ca862008-05-27 21:13:40 +0000392 off_t fd_size;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800393 QemuOpts *sn_opts = NULL;
394 const char *sn_id_or_name = NULL;
395 const char *sopt = "hVb:o:p:rsnP:c:dvk:e:f:tl:";
bellard7a5ca862008-05-27 21:13:40 +0000396 struct option lopt[] = {
Blue Swirl660f11b2009-07-31 21:16:51 +0000397 { "help", 0, NULL, 'h' },
398 { "version", 0, NULL, 'V' },
399 { "bind", 1, NULL, 'b' },
400 { "port", 1, NULL, 'p' },
401 { "socket", 1, NULL, 'k' },
402 { "offset", 1, NULL, 'o' },
403 { "read-only", 0, NULL, 'r' },
404 { "partition", 1, NULL, 'P' },
405 { "connect", 1, NULL, 'c' },
406 { "disconnect", 0, NULL, 'd' },
407 { "snapshot", 0, NULL, 's' },
Wenchao Xia8c116b02013-12-04 17:10:55 +0800408 { "load-snapshot", 1, NULL, 'l' },
Blue Swirl660f11b2009-07-31 21:16:51 +0000409 { "nocache", 0, NULL, 'n' },
Paolo Bonzini39a52352012-07-18 14:57:15 +0200410 { "cache", 1, NULL, QEMU_NBD_OPT_CACHE },
Paolo Bonzini39a52352012-07-18 14:57:15 +0200411 { "aio", 1, NULL, QEMU_NBD_OPT_AIO },
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100412 { "discard", 1, NULL, QEMU_NBD_OPT_DISCARD },
Peter Lievenb3838a42014-08-13 19:20:18 +0200413 { "detect-zeroes", 1, NULL, QEMU_NBD_OPT_DETECT_ZEROES },
Blue Swirl660f11b2009-07-31 21:16:51 +0000414 { "shared", 1, NULL, 'e' },
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000415 { "format", 1, NULL, 'f' },
Blue Swirl660f11b2009-07-31 21:16:51 +0000416 { "persistent", 0, NULL, 't' },
417 { "verbose", 0, NULL, 'v' },
418 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000419 };
420 int ch;
421 int opt_ind = 0;
bellard7a5ca862008-05-27 21:13:40 +0000422 char *end;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200423 int flags = BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000424 int partition = -1;
Max Reitz4fbec262015-02-05 13:58:19 -0500425 int ret = 0;
ths3b05a8e2008-07-03 12:45:02 +0000426 int fd;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200427 bool seen_cache = false;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100428 bool seen_discard = false;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200429 bool seen_aio = false;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100430 pthread_t client_thread;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000431 const char *fmt = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200432 Error *local_err = NULL;
Peter Lievenb3838a42014-08-13 19:20:18 +0200433 BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
Max Reitz4fbec262015-02-05 13:58:19 -0500434 QDict *options = NULL;
bellard7a5ca862008-05-27 21:13:40 +0000435
Paolo Bonzinia517e882011-11-04 15:51:21 +0100436 /* The client thread uses SIGTERM to interrupt the server. A signal
437 * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
438 */
Paolo Bonzinibb345112011-11-04 15:51:19 +0100439 struct sigaction sa_sigterm;
Paolo Bonzinibb345112011-11-04 15:51:19 +0100440 memset(&sa_sigterm, 0, sizeof(sa_sigterm));
441 sa_sigterm.sa_handler = termsig_handler;
442 sigaction(SIGTERM, &sa_sigterm, NULL);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800443 qemu_init_exec_dir(argv[0]);
Paolo Bonzinibb345112011-11-04 15:51:19 +0100444
bellard7a5ca862008-05-27 21:13:40 +0000445 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
446 switch (ch) {
447 case 's':
ths2f726482008-07-03 11:47:46 +0000448 flags |= BDRV_O_SNAPSHOT;
449 break;
450 case 'n':
Paolo Bonzini39a52352012-07-18 14:57:15 +0200451 optarg = (char *) "none";
452 /* fallthrough */
453 case QEMU_NBD_OPT_CACHE:
454 if (seen_cache) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100455 error_report("-n and --cache can only be specified once");
456 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200457 }
458 seen_cache = true;
459 if (bdrv_parse_cache_flags(optarg, &flags) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100460 error_report("Invalid cache mode `%s'", optarg);
461 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200462 }
bellard7a5ca862008-05-27 21:13:40 +0000463 break;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200464 case QEMU_NBD_OPT_AIO:
465 if (seen_aio) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100466 error_report("--aio can only be specified once");
467 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200468 }
469 seen_aio = true;
470 if (!strcmp(optarg, "native")) {
471 flags |= BDRV_O_NATIVE_AIO;
472 } else if (!strcmp(optarg, "threads")) {
473 /* this is the default */
474 } else {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100475 error_report("invalid aio mode `%s'", optarg);
476 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200477 }
478 break;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100479 case QEMU_NBD_OPT_DISCARD:
480 if (seen_discard) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100481 error_report("--discard can only be specified once");
482 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100483 }
484 seen_discard = true;
485 if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100486 error_report("Invalid discard mode `%s'", optarg);
487 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100488 }
489 break;
Peter Lievenb3838a42014-08-13 19:20:18 +0200490 case QEMU_NBD_OPT_DETECT_ZEROES:
491 detect_zeroes =
492 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
493 optarg,
Eric Blake7fb1cf12015-11-18 01:52:57 -0700494 BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX,
Peter Lievenb3838a42014-08-13 19:20:18 +0200495 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
496 &local_err);
497 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100498 error_reportf_err(local_err,
499 "Failed to parse detect_zeroes mode: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100500 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200501 }
502 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
503 !(flags & BDRV_O_UNMAP)) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100504 error_report("setting detect-zeroes to unmap is not allowed "
505 "without setting discard operation to unmap");
506 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200507 }
508 break;
bellard7a5ca862008-05-27 21:13:40 +0000509 case 'b':
510 bindto = optarg;
511 break;
512 case 'p':
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100513 port = optarg;
bellard7a5ca862008-05-27 21:13:40 +0000514 break;
515 case 'o':
516 dev_offset = strtoll (optarg, &end, 0);
517 if (*end) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100518 error_report("Invalid offset `%s'", optarg);
519 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000520 }
521 if (dev_offset < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100522 error_report("Offset must be positive `%s'", optarg);
523 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000524 }
525 break;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800526 case 'l':
527 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +0100528 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
529 optarg, false);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800530 if (!sn_opts) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100531 error_report("Failed in parsing snapshot param `%s'",
532 optarg);
533 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800534 }
535 } else {
536 sn_id_or_name = optarg;
537 }
538 /* fall through */
bellard7a5ca862008-05-27 21:13:40 +0000539 case 'r':
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200540 nbdflags |= NBD_FLAG_READ_ONLY;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200541 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000542 break;
543 case 'P':
544 partition = strtol(optarg, &end, 0);
Peter Lieven713cc672014-08-13 19:20:19 +0200545 if (*end) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100546 error_report("Invalid partition `%s'", optarg);
547 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200548 }
549 if (partition < 1 || partition > 8) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100550 error_report("Invalid partition %d", partition);
551 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200552 }
bellard7a5ca862008-05-27 21:13:40 +0000553 break;
thscd831bd2008-07-03 10:23:51 +0000554 case 'k':
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100555 sockpath = optarg;
Peter Lieven713cc672014-08-13 19:20:19 +0200556 if (sockpath[0] != '/') {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +0100557 error_report("socket path must be absolute");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100558 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200559 }
thscd831bd2008-07-03 10:23:51 +0000560 break;
561 case 'd':
562 disconnect = true;
563 break;
564 case 'c':
565 device = optarg;
566 break;
ths3b05a8e2008-07-03 12:45:02 +0000567 case 'e':
568 shared = strtol(optarg, &end, 0);
569 if (*end) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100570 error_report("Invalid shared device number '%s'", optarg);
571 exit(EXIT_FAILURE);
ths3b05a8e2008-07-03 12:45:02 +0000572 }
573 if (shared < 1) {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +0100574 error_report("Shared device number must be greater than 0");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100575 exit(EXIT_FAILURE);
ths3b05a8e2008-07-03 12:45:02 +0000576 }
577 break;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000578 case 'f':
579 fmt = optarg;
580 break;
Peter Lieven713cc672014-08-13 19:20:19 +0200581 case 't':
582 persistent = 1;
583 break;
bellard7a5ca862008-05-27 21:13:40 +0000584 case 'v':
585 verbose = 1;
586 break;
587 case 'V':
588 version(argv[0]);
589 exit(0);
590 break;
591 case 'h':
592 usage(argv[0]);
593 exit(0);
594 break;
595 case '?':
Markus Armbruster85b01e02015-12-18 16:35:04 +0100596 error_report("Try `%s --help' for more information.", argv[0]);
597 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000598 }
599 }
600
601 if ((argc - optind) != 1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100602 error_report("Invalid number of argument.\n"
603 "Try `%s --help' for more information.",
604 argv[0]);
605 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000606 }
607
thscd831bd2008-07-03 10:23:51 +0000608 if (disconnect) {
609 fd = open(argv[optind], O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100610 if (fd < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100611 error_report("Cannot open %s: %s", argv[optind],
612 strerror(errno));
613 exit(EXIT_FAILURE);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100614 }
thscd831bd2008-07-03 10:23:51 +0000615 nbd_disconnect(fd);
616
617 close(fd);
618
619 printf("%s disconnected\n", argv[optind]);
620
Peter Lieven713cc672014-08-13 19:20:19 +0200621 return 0;
thscd831bd2008-07-03 10:23:51 +0000622 }
623
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100624 if (device && !verbose) {
625 int stderr_fd[2];
626 pid_t pid;
627 int ret;
628
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100629 if (qemu_pipe(stderr_fd) < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100630 error_report("Error setting up communication pipe: %s",
631 strerror(errno));
632 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100633 }
634
635 /* Now daemonize, but keep a communication channel open to
636 * print errors and exit with the proper status code.
637 */
638 pid = fork();
Max Reitz70d47392015-02-25 13:08:22 -0500639 if (pid < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100640 error_report("Failed to fork: %s", strerror(errno));
641 exit(EXIT_FAILURE);
Max Reitz70d47392015-02-25 13:08:22 -0500642 } else if (pid == 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100643 close(stderr_fd[0]);
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400644 ret = qemu_daemon(1, 0);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100645
646 /* Temporarily redirect stderr to the parent's pipe... */
647 dup2(stderr_fd[1], STDERR_FILENO);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100648 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100649 error_report("Failed to daemonize: %s", strerror(errno));
650 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100651 }
652
653 /* ... close the descriptor we inherited and go on. */
654 close(stderr_fd[1]);
655 } else {
656 bool errors = false;
657 char *buf;
658
659 /* In the parent. Print error messages from the child until
660 * it closes the pipe.
661 */
662 close(stderr_fd[1]);
663 buf = g_malloc(1024);
664 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
665 errors = true;
666 ret = qemu_write_full(STDERR_FILENO, buf, ret);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100667 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100668 exit(EXIT_FAILURE);
669 }
670 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100671 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100672 error_report("Cannot read from daemon: %s",
673 strerror(errno));
674 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100675 }
676
677 /* Usually the daemon should not print any message.
678 * Exit with zero status in that case.
679 */
680 exit(errors);
681 }
682 }
683
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100684 if (device != NULL && sockpath == NULL) {
685 sockpath = g_malloc(128);
686 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
thscd831bd2008-07-03 10:23:51 +0000687 }
688
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100689 saddr = nbd_build_socket_address(sockpath, bindto, port);
690
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300691 if (qemu_init_main_loop(&local_err)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100692 error_report_err(local_err);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300693 exit(EXIT_FAILURE);
694 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100695 bdrv_init();
696 atexit(bdrv_close_all);
697
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000698 if (fmt) {
Max Reitz4fbec262015-02-05 13:58:19 -0500699 options = qdict_new();
700 qdict_put(options, "driver", qstring_from_str(fmt));
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000701 }
702
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100703 srcpath = argv[optind];
Max Reitz4fbec262015-02-05 13:58:19 -0500704 blk = blk_new_open("hda", srcpath, NULL, options, flags, &local_err);
705 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100706 error_reportf_err(local_err, "Failed to blk_new_open '%s': ",
707 argv[optind]);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100708 exit(EXIT_FAILURE);
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100709 }
Max Reitz4fbec262015-02-05 13:58:19 -0500710 bs = blk_bs(blk);
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100711
Wenchao Xia8c116b02013-12-04 17:10:55 +0800712 if (sn_opts) {
713 ret = bdrv_snapshot_load_tmp(bs,
714 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
715 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
716 &local_err);
717 } else if (sn_id_or_name) {
718 ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
719 &local_err);
720 }
721 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100722 error_reportf_err(local_err, "Failed to load snapshot: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100723 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800724 }
725
Peter Lievenb3838a42014-08-13 19:20:18 +0200726 bs->detect_zeroes = detect_zeroes;
Max Reitz4c58e802014-11-18 12:21:19 +0100727 fd_size = blk_getlength(blk);
Max Reitz98f44bb2015-02-25 13:08:21 -0500728 if (fd_size < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100729 error_report("Failed to determine the image length: %s",
730 strerror(-fd_size));
731 exit(EXIT_FAILURE);
Max Reitz98f44bb2015-02-25 13:08:21 -0500732 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100733
Paolo Bonzini185b4332012-03-05 08:56:10 +0100734 if (partition != -1) {
Max Reitz4c58e802014-11-18 12:21:19 +0100735 ret = find_partition(blk, partition, &dev_offset, &fd_size);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100736 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100737 error_report("Could not find partition %d: %s", partition,
Markus Armbrustera4699e52015-12-18 16:35:10 +0100738 strerror(-ret));
Markus Armbruster85b01e02015-12-18 16:35:04 +0100739 exit(EXIT_FAILURE);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100740 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100741 }
742
Max Reitz98f44bb2015-02-25 13:08:21 -0500743 exp = nbd_export_new(blk, dev_offset, fd_size, nbdflags, nbd_export_closed,
744 &local_err);
745 if (!exp) {
Markus Armbruster4fffeb52015-12-18 16:35:05 +0100746 error_report_err(local_err);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100747 exit(EXIT_FAILURE);
Max Reitz98f44bb2015-02-25 13:08:21 -0500748 }
ths3b05a8e2008-07-03 12:45:02 +0000749
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100750 fd = socket_listen(saddr, &local_err);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100751 if (fd < 0) {
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100752 error_report_err(local_err);
bellard7a5ca862008-05-27 21:13:40 +0000753 return 1;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200754 }
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100755
756 if (device) {
757 int ret;
758
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100759 ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100760 if (ret != 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100761 error_report("Failed to create client thread: %s", strerror(ret));
762 exit(EXIT_FAILURE);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100763 }
764 } else {
765 /* Shut up GCC warnings. */
766 memset(&client_thread, 0, sizeof(client_thread));
767 }
768
Fam Zhenge4afbf42015-05-19 10:50:59 +0000769 server_fd = fd;
770 nbd_update_server_fd_handler(fd);
bellard7a5ca862008-05-27 21:13:40 +0000771
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400772 /* now when the initialization is (almost) complete, chdir("/")
773 * to free any busy filesystems */
774 if (chdir("/") < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100775 error_report("Could not chdir to root directory: %s",
776 strerror(errno));
777 exit(EXIT_FAILURE);
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400778 }
779
Paolo Bonzini7860a382012-09-18 13:31:56 +0200780 state = RUNNING;
ths3b05a8e2008-07-03 12:45:02 +0000781 do {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200782 main_loop_wait(false);
Paolo Bonzini7860a382012-09-18 13:31:56 +0200783 if (state == TERMINATE) {
784 state = TERMINATING;
785 nbd_export_close(exp);
786 nbd_export_put(exp);
787 exp = NULL;
788 }
789 } while (state != TERMINATED);
ths3b05a8e2008-07-03 12:45:02 +0000790
Markus Armbruster26f54e92014-10-07 13:59:04 +0200791 blk_unref(blk);
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100792 if (sockpath) {
793 unlink(sockpath);
794 }
bellard7a5ca862008-05-27 21:13:40 +0000795
Markus Armbrusterfbf28a42014-09-29 16:07:55 +0200796 qemu_opts_del(sn_opts);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800797
Paolo Bonzinia517e882011-11-04 15:51:21 +0100798 if (device) {
799 void *ret;
800 pthread_join(client_thread, &ret);
801 exit(ret != NULL);
802 } else {
803 exit(EXIT_SUCCESS);
804 }
bellard7a5ca862008-05-27 21:13:40 +0000805}