blob: ede4a54d4ed7f25fb2cedd1b6ac7f6a0b9379e3c [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
Fam Zhengee7d7aa2016-01-14 16:41:01 +0800336 nb_fds++;
337 nbd_update_server_fd_handler(server_fd);
338 nbd_client_new(exp, fd, nbd_client_closed);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200339}
340
Fam Zhenge4afbf42015-05-19 10:50:59 +0000341static void nbd_update_server_fd_handler(int fd)
342{
343 if (nbd_can_accept()) {
344 qemu_set_fd_handler(fd, nbd_accept, NULL, (void *)(uintptr_t)fd);
345 } else {
346 qemu_set_fd_handler(fd, NULL, NULL, NULL);
347 }
348}
349
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100350
351static SocketAddress *nbd_build_socket_address(const char *sockpath,
352 const char *bindto,
353 const char *port)
354{
355 SocketAddress *saddr;
356
357 saddr = g_new0(SocketAddress, 1);
358 if (sockpath) {
Eric Blake2d32add2015-10-26 16:34:55 -0600359 saddr->type = SOCKET_ADDRESS_KIND_UNIX;
360 saddr->u.q_unix = g_new0(UnixSocketAddress, 1);
361 saddr->u.q_unix->path = g_strdup(sockpath);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100362 } else {
Eric Blake2d32add2015-10-26 16:34:55 -0600363 saddr->type = SOCKET_ADDRESS_KIND_INET;
364 saddr->u.inet = g_new0(InetSocketAddress, 1);
365 saddr->u.inet->host = g_strdup(bindto);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100366 if (port) {
Eric Blake2d32add2015-10-26 16:34:55 -0600367 saddr->u.inet->port = g_strdup(port);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100368 } else {
Eric Blake2d32add2015-10-26 16:34:55 -0600369 saddr->u.inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100370 }
371 }
372
373 return saddr;
374}
375
376
bellard7a5ca862008-05-27 21:13:40 +0000377int main(int argc, char **argv)
378{
Markus Armbruster26f54e92014-10-07 13:59:04 +0200379 BlockBackend *blk;
bellard7a5ca862008-05-27 21:13:40 +0000380 BlockDriverState *bs;
381 off_t dev_offset = 0;
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200382 uint32_t nbdflags = 0;
thscd831bd2008-07-03 10:23:51 +0000383 bool disconnect = false;
bellard7a5ca862008-05-27 21:13:40 +0000384 const char *bindto = "0.0.0.0";
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100385 const char *port = NULL;
386 char *sockpath = NULL;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100387 char *device = NULL;
bellard7a5ca862008-05-27 21:13:40 +0000388 off_t fd_size;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800389 QemuOpts *sn_opts = NULL;
390 const char *sn_id_or_name = NULL;
391 const char *sopt = "hVb:o:p:rsnP:c:dvk:e:f:tl:";
bellard7a5ca862008-05-27 21:13:40 +0000392 struct option lopt[] = {
Blue Swirl660f11b2009-07-31 21:16:51 +0000393 { "help", 0, NULL, 'h' },
394 { "version", 0, NULL, 'V' },
395 { "bind", 1, NULL, 'b' },
396 { "port", 1, NULL, 'p' },
397 { "socket", 1, NULL, 'k' },
398 { "offset", 1, NULL, 'o' },
399 { "read-only", 0, NULL, 'r' },
400 { "partition", 1, NULL, 'P' },
401 { "connect", 1, NULL, 'c' },
402 { "disconnect", 0, NULL, 'd' },
403 { "snapshot", 0, NULL, 's' },
Wenchao Xia8c116b02013-12-04 17:10:55 +0800404 { "load-snapshot", 1, NULL, 'l' },
Blue Swirl660f11b2009-07-31 21:16:51 +0000405 { "nocache", 0, NULL, 'n' },
Paolo Bonzini39a52352012-07-18 14:57:15 +0200406 { "cache", 1, NULL, QEMU_NBD_OPT_CACHE },
Paolo Bonzini39a52352012-07-18 14:57:15 +0200407 { "aio", 1, NULL, QEMU_NBD_OPT_AIO },
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100408 { "discard", 1, NULL, QEMU_NBD_OPT_DISCARD },
Peter Lievenb3838a42014-08-13 19:20:18 +0200409 { "detect-zeroes", 1, NULL, QEMU_NBD_OPT_DETECT_ZEROES },
Blue Swirl660f11b2009-07-31 21:16:51 +0000410 { "shared", 1, NULL, 'e' },
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000411 { "format", 1, NULL, 'f' },
Blue Swirl660f11b2009-07-31 21:16:51 +0000412 { "persistent", 0, NULL, 't' },
413 { "verbose", 0, NULL, 'v' },
414 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000415 };
416 int ch;
417 int opt_ind = 0;
bellard7a5ca862008-05-27 21:13:40 +0000418 char *end;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200419 int flags = BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000420 int partition = -1;
Max Reitz4fbec262015-02-05 13:58:19 -0500421 int ret = 0;
ths3b05a8e2008-07-03 12:45:02 +0000422 int fd;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200423 bool seen_cache = false;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100424 bool seen_discard = false;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200425 bool seen_aio = false;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100426 pthread_t client_thread;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000427 const char *fmt = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200428 Error *local_err = NULL;
Peter Lievenb3838a42014-08-13 19:20:18 +0200429 BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
Max Reitz4fbec262015-02-05 13:58:19 -0500430 QDict *options = NULL;
bellard7a5ca862008-05-27 21:13:40 +0000431
Paolo Bonzinia517e882011-11-04 15:51:21 +0100432 /* The client thread uses SIGTERM to interrupt the server. A signal
433 * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
434 */
Paolo Bonzinibb345112011-11-04 15:51:19 +0100435 struct sigaction sa_sigterm;
Paolo Bonzinibb345112011-11-04 15:51:19 +0100436 memset(&sa_sigterm, 0, sizeof(sa_sigterm));
437 sa_sigterm.sa_handler = termsig_handler;
438 sigaction(SIGTERM, &sa_sigterm, NULL);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800439 qemu_init_exec_dir(argv[0]);
Paolo Bonzinibb345112011-11-04 15:51:19 +0100440
bellard7a5ca862008-05-27 21:13:40 +0000441 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
442 switch (ch) {
443 case 's':
ths2f726482008-07-03 11:47:46 +0000444 flags |= BDRV_O_SNAPSHOT;
445 break;
446 case 'n':
Paolo Bonzini39a52352012-07-18 14:57:15 +0200447 optarg = (char *) "none";
448 /* fallthrough */
449 case QEMU_NBD_OPT_CACHE:
450 if (seen_cache) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100451 error_report("-n and --cache can only be specified once");
452 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200453 }
454 seen_cache = true;
455 if (bdrv_parse_cache_flags(optarg, &flags) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100456 error_report("Invalid cache mode `%s'", optarg);
457 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200458 }
bellard7a5ca862008-05-27 21:13:40 +0000459 break;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200460 case QEMU_NBD_OPT_AIO:
461 if (seen_aio) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100462 error_report("--aio can only be specified once");
463 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200464 }
465 seen_aio = true;
466 if (!strcmp(optarg, "native")) {
467 flags |= BDRV_O_NATIVE_AIO;
468 } else if (!strcmp(optarg, "threads")) {
469 /* this is the default */
470 } else {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100471 error_report("invalid aio mode `%s'", optarg);
472 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200473 }
474 break;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100475 case QEMU_NBD_OPT_DISCARD:
476 if (seen_discard) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100477 error_report("--discard can only be specified once");
478 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100479 }
480 seen_discard = true;
481 if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100482 error_report("Invalid discard mode `%s'", optarg);
483 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100484 }
485 break;
Peter Lievenb3838a42014-08-13 19:20:18 +0200486 case QEMU_NBD_OPT_DETECT_ZEROES:
487 detect_zeroes =
488 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
489 optarg,
Eric Blake7fb1cf12015-11-18 01:52:57 -0700490 BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX,
Peter Lievenb3838a42014-08-13 19:20:18 +0200491 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
492 &local_err);
493 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100494 error_reportf_err(local_err,
495 "Failed to parse detect_zeroes mode: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100496 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200497 }
498 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
499 !(flags & BDRV_O_UNMAP)) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100500 error_report("setting detect-zeroes to unmap is not allowed "
501 "without setting discard operation to unmap");
502 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200503 }
504 break;
bellard7a5ca862008-05-27 21:13:40 +0000505 case 'b':
506 bindto = optarg;
507 break;
508 case 'p':
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100509 port = optarg;
bellard7a5ca862008-05-27 21:13:40 +0000510 break;
511 case 'o':
512 dev_offset = strtoll (optarg, &end, 0);
513 if (*end) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100514 error_report("Invalid offset `%s'", optarg);
515 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000516 }
517 if (dev_offset < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100518 error_report("Offset must be positive `%s'", optarg);
519 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000520 }
521 break;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800522 case 'l':
523 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +0100524 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
525 optarg, false);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800526 if (!sn_opts) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100527 error_report("Failed in parsing snapshot param `%s'",
528 optarg);
529 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800530 }
531 } else {
532 sn_id_or_name = optarg;
533 }
534 /* fall through */
bellard7a5ca862008-05-27 21:13:40 +0000535 case 'r':
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200536 nbdflags |= NBD_FLAG_READ_ONLY;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200537 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000538 break;
539 case 'P':
540 partition = strtol(optarg, &end, 0);
Peter Lieven713cc672014-08-13 19:20:19 +0200541 if (*end) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100542 error_report("Invalid partition `%s'", optarg);
543 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200544 }
545 if (partition < 1 || partition > 8) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100546 error_report("Invalid partition %d", partition);
547 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200548 }
bellard7a5ca862008-05-27 21:13:40 +0000549 break;
thscd831bd2008-07-03 10:23:51 +0000550 case 'k':
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100551 sockpath = optarg;
Peter Lieven713cc672014-08-13 19:20:19 +0200552 if (sockpath[0] != '/') {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +0100553 error_report("socket path must be absolute");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100554 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200555 }
thscd831bd2008-07-03 10:23:51 +0000556 break;
557 case 'd':
558 disconnect = true;
559 break;
560 case 'c':
561 device = optarg;
562 break;
ths3b05a8e2008-07-03 12:45:02 +0000563 case 'e':
564 shared = strtol(optarg, &end, 0);
565 if (*end) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100566 error_report("Invalid shared device number '%s'", optarg);
567 exit(EXIT_FAILURE);
ths3b05a8e2008-07-03 12:45:02 +0000568 }
569 if (shared < 1) {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +0100570 error_report("Shared device number must be greater than 0");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100571 exit(EXIT_FAILURE);
ths3b05a8e2008-07-03 12:45:02 +0000572 }
573 break;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000574 case 'f':
575 fmt = optarg;
576 break;
Peter Lieven713cc672014-08-13 19:20:19 +0200577 case 't':
578 persistent = 1;
579 break;
bellard7a5ca862008-05-27 21:13:40 +0000580 case 'v':
581 verbose = 1;
582 break;
583 case 'V':
584 version(argv[0]);
585 exit(0);
586 break;
587 case 'h':
588 usage(argv[0]);
589 exit(0);
590 break;
591 case '?':
Markus Armbruster85b01e02015-12-18 16:35:04 +0100592 error_report("Try `%s --help' for more information.", argv[0]);
593 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000594 }
595 }
596
597 if ((argc - optind) != 1) {
Markus Armbruster433672b2015-12-18 16:35:24 +0100598 error_report("Invalid number of arguments");
599 error_printf("Try `%s --help' for more information.\n", argv[0]);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100600 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000601 }
602
thscd831bd2008-07-03 10:23:51 +0000603 if (disconnect) {
604 fd = open(argv[optind], O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100605 if (fd < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100606 error_report("Cannot open %s: %s", argv[optind],
607 strerror(errno));
608 exit(EXIT_FAILURE);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100609 }
thscd831bd2008-07-03 10:23:51 +0000610 nbd_disconnect(fd);
611
612 close(fd);
613
614 printf("%s disconnected\n", argv[optind]);
615
Peter Lieven713cc672014-08-13 19:20:19 +0200616 return 0;
thscd831bd2008-07-03 10:23:51 +0000617 }
618
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100619 if (device && !verbose) {
620 int stderr_fd[2];
621 pid_t pid;
622 int ret;
623
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100624 if (qemu_pipe(stderr_fd) < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100625 error_report("Error setting up communication pipe: %s",
626 strerror(errno));
627 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100628 }
629
630 /* Now daemonize, but keep a communication channel open to
631 * print errors and exit with the proper status code.
632 */
633 pid = fork();
Max Reitz70d47392015-02-25 13:08:22 -0500634 if (pid < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100635 error_report("Failed to fork: %s", strerror(errno));
636 exit(EXIT_FAILURE);
Max Reitz70d47392015-02-25 13:08:22 -0500637 } else if (pid == 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100638 close(stderr_fd[0]);
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400639 ret = qemu_daemon(1, 0);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100640
641 /* Temporarily redirect stderr to the parent's pipe... */
642 dup2(stderr_fd[1], STDERR_FILENO);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100643 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100644 error_report("Failed to daemonize: %s", strerror(errno));
645 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100646 }
647
648 /* ... close the descriptor we inherited and go on. */
649 close(stderr_fd[1]);
650 } else {
651 bool errors = false;
652 char *buf;
653
654 /* In the parent. Print error messages from the child until
655 * it closes the pipe.
656 */
657 close(stderr_fd[1]);
658 buf = g_malloc(1024);
659 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
660 errors = true;
661 ret = qemu_write_full(STDERR_FILENO, buf, ret);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100662 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100663 exit(EXIT_FAILURE);
664 }
665 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100666 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100667 error_report("Cannot read from daemon: %s",
668 strerror(errno));
669 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100670 }
671
672 /* Usually the daemon should not print any message.
673 * Exit with zero status in that case.
674 */
675 exit(errors);
676 }
677 }
678
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100679 if (device != NULL && sockpath == NULL) {
680 sockpath = g_malloc(128);
681 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
thscd831bd2008-07-03 10:23:51 +0000682 }
683
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100684 saddr = nbd_build_socket_address(sockpath, bindto, port);
685
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300686 if (qemu_init_main_loop(&local_err)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100687 error_report_err(local_err);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300688 exit(EXIT_FAILURE);
689 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100690 bdrv_init();
691 atexit(bdrv_close_all);
692
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000693 if (fmt) {
Max Reitz4fbec262015-02-05 13:58:19 -0500694 options = qdict_new();
695 qdict_put(options, "driver", qstring_from_str(fmt));
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000696 }
697
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100698 srcpath = argv[optind];
Max Reitz4fbec262015-02-05 13:58:19 -0500699 blk = blk_new_open("hda", srcpath, NULL, options, flags, &local_err);
700 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100701 error_reportf_err(local_err, "Failed to blk_new_open '%s': ",
702 argv[optind]);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100703 exit(EXIT_FAILURE);
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100704 }
Max Reitz4fbec262015-02-05 13:58:19 -0500705 bs = blk_bs(blk);
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100706
Wenchao Xia8c116b02013-12-04 17:10:55 +0800707 if (sn_opts) {
708 ret = bdrv_snapshot_load_tmp(bs,
709 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
710 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
711 &local_err);
712 } else if (sn_id_or_name) {
713 ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
714 &local_err);
715 }
716 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100717 error_reportf_err(local_err, "Failed to load snapshot: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100718 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800719 }
720
Peter Lievenb3838a42014-08-13 19:20:18 +0200721 bs->detect_zeroes = detect_zeroes;
Max Reitz4c58e802014-11-18 12:21:19 +0100722 fd_size = blk_getlength(blk);
Max Reitz98f44bb2015-02-25 13:08:21 -0500723 if (fd_size < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100724 error_report("Failed to determine the image length: %s",
725 strerror(-fd_size));
726 exit(EXIT_FAILURE);
Max Reitz98f44bb2015-02-25 13:08:21 -0500727 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100728
Paolo Bonzini185b4332012-03-05 08:56:10 +0100729 if (partition != -1) {
Max Reitz4c58e802014-11-18 12:21:19 +0100730 ret = find_partition(blk, partition, &dev_offset, &fd_size);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100731 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100732 error_report("Could not find partition %d: %s", partition,
Markus Armbrustera4699e52015-12-18 16:35:10 +0100733 strerror(-ret));
Markus Armbruster85b01e02015-12-18 16:35:04 +0100734 exit(EXIT_FAILURE);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100735 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100736 }
737
Max Reitz98f44bb2015-02-25 13:08:21 -0500738 exp = nbd_export_new(blk, dev_offset, fd_size, nbdflags, nbd_export_closed,
739 &local_err);
740 if (!exp) {
Markus Armbruster4fffeb52015-12-18 16:35:05 +0100741 error_report_err(local_err);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100742 exit(EXIT_FAILURE);
Max Reitz98f44bb2015-02-25 13:08:21 -0500743 }
ths3b05a8e2008-07-03 12:45:02 +0000744
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100745 fd = socket_listen(saddr, &local_err);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100746 if (fd < 0) {
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100747 error_report_err(local_err);
bellard7a5ca862008-05-27 21:13:40 +0000748 return 1;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200749 }
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100750
751 if (device) {
752 int ret;
753
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100754 ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100755 if (ret != 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100756 error_report("Failed to create client thread: %s", strerror(ret));
757 exit(EXIT_FAILURE);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100758 }
759 } else {
760 /* Shut up GCC warnings. */
761 memset(&client_thread, 0, sizeof(client_thread));
762 }
763
Fam Zhenge4afbf42015-05-19 10:50:59 +0000764 server_fd = fd;
765 nbd_update_server_fd_handler(fd);
bellard7a5ca862008-05-27 21:13:40 +0000766
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400767 /* now when the initialization is (almost) complete, chdir("/")
768 * to free any busy filesystems */
769 if (chdir("/") < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100770 error_report("Could not chdir to root directory: %s",
771 strerror(errno));
772 exit(EXIT_FAILURE);
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400773 }
774
Paolo Bonzini7860a382012-09-18 13:31:56 +0200775 state = RUNNING;
ths3b05a8e2008-07-03 12:45:02 +0000776 do {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200777 main_loop_wait(false);
Paolo Bonzini7860a382012-09-18 13:31:56 +0200778 if (state == TERMINATE) {
779 state = TERMINATING;
780 nbd_export_close(exp);
781 nbd_export_put(exp);
782 exp = NULL;
783 }
784 } while (state != TERMINATED);
ths3b05a8e2008-07-03 12:45:02 +0000785
Markus Armbruster26f54e92014-10-07 13:59:04 +0200786 blk_unref(blk);
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100787 if (sockpath) {
788 unlink(sockpath);
789 }
bellard7a5ca862008-05-27 21:13:40 +0000790
Markus Armbrusterfbf28a42014-09-29 16:07:55 +0200791 qemu_opts_del(sn_opts);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800792
Paolo Bonzinia517e882011-11-04 15:51:21 +0100793 if (device) {
794 void *ret;
795 pthread_join(client_thread, &ret);
796 exit(ret != NULL);
797 } else {
798 exit(EXIT_SUCCESS);
799 }
bellard7a5ca862008-05-27 21:13:40 +0000800}