blob: d374015125da1f39fc307bdf230ce0091beccf0f [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"
Stefan Weil5a61cb62011-09-08 17:55:32 +020020#include "qemu-common.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020021#include "sysemu/block-backend.h"
Peter Lievenb3838a42014-08-13 19:20:18 +020022#include "block/block_int.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010023#include "block/nbd.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010024#include "qemu/main-loop.h"
Paolo Bonzini537b41f2014-02-17 14:43:51 +010025#include "qemu/sockets.h"
26#include "qemu/error-report.h"
Wenchao Xia8c116b02013-12-04 17:10:55 +080027#include "block/snapshot.h"
Peter Lievenb3838a42014-08-13 19:20:18 +020028#include "qapi/util.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010029#include "qapi/qmp/qstring.h"
bellard7a5ca862008-05-27 21:13:40 +000030
bellard7a5ca862008-05-27 21:13:40 +000031#include <getopt.h>
bellard7a5ca862008-05-27 21:13:40 +000032#include <sys/socket.h>
33#include <netinet/in.h>
34#include <netinet/tcp.h>
35#include <arpa/inet.h>
Blue Swirl2bff4b62009-12-23 15:34:04 +000036#include <libgen.h>
Paolo Bonzinia517e882011-11-04 15:51:21 +010037#include <pthread.h>
thscd831bd2008-07-03 10:23:51 +000038
Peter Lieven713cc672014-08-13 19:20:19 +020039#define SOCKET_PATH "/var/lock/qemu-nbd-%s"
40#define QEMU_NBD_OPT_CACHE 1
41#define QEMU_NBD_OPT_AIO 2
42#define QEMU_NBD_OPT_DISCARD 3
Peter Lievenb3838a42014-08-13 19:20:18 +020043#define QEMU_NBD_OPT_DETECT_ZEROES 4
bellard7a5ca862008-05-27 21:13:40 +000044
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +020045static NBDExport *exp;
blueswir1b1d8e522008-10-26 13:43:07 +000046static int verbose;
Paolo Bonzinia517e882011-11-04 15:51:21 +010047static char *srcpath;
Daniel P. Berrange48bec072015-09-16 14:52:23 +010048static SocketAddress *saddr;
Paolo Bonzini7860a382012-09-18 13:31:56 +020049static int persistent = 0;
50static enum { RUNNING, TERMINATE, TERMINATING, TERMINATED } state;
Paolo Bonzinia61c6782011-09-12 17:28:11 +020051static int shared = 1;
52static int nb_fds;
Fam Zhenge4afbf42015-05-19 10:50:59 +000053static int server_fd;
bellard7a5ca862008-05-27 21:13:40 +000054
55static void usage(const char *name)
56{
Paolo Bonzinib033cd82012-07-18 14:50:52 +020057 (printf) (
bellard7a5ca862008-05-27 21:13:40 +000058"Usage: %s [OPTIONS] FILE\n"
59"QEMU Disk Network Block Device Server\n"
60"\n"
Peter Lieven713cc672014-08-13 19:20:19 +020061" -h, --help display this help and exit\n"
62" -V, --version output version information and exit\n"
bellard7a5ca862008-05-27 21:13:40 +000063"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020064"Connection properties:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020065" -p, --port=PORT port to listen on (default `%d')\n"
66" -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
67" -k, --socket=PATH path to the unix socket\n"
68" (default '"SOCKET_PATH"')\n"
69" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
70" -t, --persistent don't exit on the last connection\n"
71" -v, --verbose display extra debugging information\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020072"\n"
73"Exposing part of the image:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020074" -o, --offset=OFFSET offset into the image\n"
75" -P, --partition=NUM only expose partition NUM\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020076"\n"
77#ifdef __linux__
78"Kernel NBD client support:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020079" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
80" -d, --disconnect disconnect the specified device\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020081"\n"
82#endif
83"\n"
84"Block device options:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020085" -f, --format=FORMAT set image format (raw, qcow2, ...)\n"
86" -r, --read-only export read-only\n"
87" -s, --snapshot use FILE as an external snapshot, create a temporary\n"
88" file with backing_file=FILE, redirect the write to\n"
89" the temporary one\n"
Wenchao Xia8c116b02013-12-04 17:10:55 +080090" -l, --load-snapshot=SNAPSHOT_PARAM\n"
Peter Lieven713cc672014-08-13 19:20:19 +020091" load an internal snapshot inside FILE and export it\n"
92" as an read-only device, SNAPSHOT_PARAM format is\n"
93" 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
94" '[ID_OR_NAME]'\n"
95" -n, --nocache disable host cache\n"
96" --cache=MODE set cache mode (none, writeback, ...)\n"
Peter Lieven713cc672014-08-13 19:20:19 +020097" --aio=MODE set AIO mode (native or threads)\n"
Peter Lievenb3838a42014-08-13 19:20:18 +020098" --discard=MODE set discard mode (ignore, unmap)\n"
Andrey Korolyov6883de62015-07-31 22:12:54 +030099" --detect-zeroes=MODE set detect-zeroes mode (off, on, unmap)\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200100"\n"
101"Report bugs to <qemu-devel@nongnu.org>\n"
Laurent Vivierc2e28722010-09-17 20:37:25 +0200102 , name, NBD_DEFAULT_PORT, "DEVICE");
bellard7a5ca862008-05-27 21:13:40 +0000103}
104
105static void version(const char *name)
106{
107 printf(
ths315bc7a2008-07-18 18:06:23 +0000108"%s version 0.0.1\n"
bellard7a5ca862008-05-27 21:13:40 +0000109"Written by Anthony Liguori.\n"
110"\n"
111"Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
112"This is free software; see the source for copying conditions. There is NO\n"
113"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
ths315bc7a2008-07-18 18:06:23 +0000114 , name);
bellard7a5ca862008-05-27 21:13:40 +0000115}
116
117struct partition_record
118{
119 uint8_t bootable;
120 uint8_t start_head;
121 uint32_t start_cylinder;
122 uint8_t start_sector;
123 uint8_t system;
124 uint8_t end_head;
125 uint8_t end_cylinder;
126 uint8_t end_sector;
127 uint32_t start_sector_abs;
128 uint32_t nb_sectors_abs;
129};
130
131static void read_partition(uint8_t *p, struct partition_record *r)
132{
133 r->bootable = p[0];
134 r->start_head = p[1];
135 r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
136 r->start_sector = p[2] & 0x3f;
137 r->system = p[4];
138 r->end_head = p[5];
139 r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
140 r->end_sector = p[6] & 0x3f;
Max Reitzac973932015-02-25 13:08:23 -0500141
142 r->start_sector_abs = le32_to_cpup((uint32_t *)(p + 8));
143 r->nb_sectors_abs = le32_to_cpup((uint32_t *)(p + 12));
bellard7a5ca862008-05-27 21:13:40 +0000144}
145
Max Reitz4c58e802014-11-18 12:21:19 +0100146static int find_partition(BlockBackend *blk, int partition,
bellard7a5ca862008-05-27 21:13:40 +0000147 off_t *offset, off_t *size)
148{
149 struct partition_record mbr[4];
150 uint8_t data[512];
151 int i;
152 int ext_partnum = 4;
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900153 int ret;
bellard7a5ca862008-05-27 21:13:40 +0000154
Max Reitz4c58e802014-11-18 12:21:19 +0100155 if ((ret = blk_read(blk, 0, data, 1)) < 0) {
Markus Armbrustera4699e52015-12-18 16:35:10 +0100156 error_report("error while reading: %s", strerror(-ret));
Markus Armbruster85b01e02015-12-18 16:35:04 +0100157 exit(EXIT_FAILURE);
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900158 }
bellard7a5ca862008-05-27 21:13:40 +0000159
160 if (data[510] != 0x55 || data[511] != 0xaa) {
Paolo Bonzini185b4332012-03-05 08:56:10 +0100161 return -EINVAL;
bellard7a5ca862008-05-27 21:13:40 +0000162 }
163
164 for (i = 0; i < 4; i++) {
165 read_partition(&data[446 + 16 * i], &mbr[i]);
166
Max Reitz453b07b2015-02-25 13:08:15 -0500167 if (!mbr[i].system || !mbr[i].nb_sectors_abs) {
bellard7a5ca862008-05-27 21:13:40 +0000168 continue;
Max Reitz453b07b2015-02-25 13:08:15 -0500169 }
bellard7a5ca862008-05-27 21:13:40 +0000170
171 if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
172 struct partition_record ext[4];
173 uint8_t data1[512];
174 int j;
175
Max Reitz4c58e802014-11-18 12:21:19 +0100176 if ((ret = blk_read(blk, mbr[i].start_sector_abs, data1, 1)) < 0) {
Markus Armbrustera4699e52015-12-18 16:35:10 +0100177 error_report("error while reading: %s", strerror(-ret));
Markus Armbruster85b01e02015-12-18 16:35:04 +0100178 exit(EXIT_FAILURE);
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900179 }
bellard7a5ca862008-05-27 21:13:40 +0000180
181 for (j = 0; j < 4; j++) {
182 read_partition(&data1[446 + 16 * j], &ext[j]);
Max Reitz453b07b2015-02-25 13:08:15 -0500183 if (!ext[j].system || !ext[j].nb_sectors_abs) {
bellard7a5ca862008-05-27 21:13:40 +0000184 continue;
Max Reitz453b07b2015-02-25 13:08:15 -0500185 }
bellard7a5ca862008-05-27 21:13:40 +0000186
187 if ((ext_partnum + j + 1) == partition) {
188 *offset = (uint64_t)ext[j].start_sector_abs << 9;
189 *size = (uint64_t)ext[j].nb_sectors_abs << 9;
190 return 0;
191 }
192 }
193 ext_partnum += 4;
194 } else if ((i + 1) == partition) {
195 *offset = (uint64_t)mbr[i].start_sector_abs << 9;
196 *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
197 return 0;
198 }
199 }
200
Paolo Bonzini185b4332012-03-05 08:56:10 +0100201 return -ENOENT;
bellard7a5ca862008-05-27 21:13:40 +0000202}
203
Paolo Bonzinibb345112011-11-04 15:51:19 +0100204static void termsig_handler(int signum)
205{
Paolo Bonzini7860a382012-09-18 13:31:56 +0200206 state = TERMINATE;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200207 qemu_notify_event();
Paolo Bonzinibb345112011-11-04 15:51:19 +0100208}
209
Paolo Bonzini537b41f2014-02-17 14:43:51 +0100210
Paolo Bonzinia517e882011-11-04 15:51:21 +0100211static void *show_parts(void *arg)
thscd831bd2008-07-03 10:23:51 +0000212{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100213 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100214 int nbd;
thscd831bd2008-07-03 10:23:51 +0000215
Paolo Bonzinia517e882011-11-04 15:51:21 +0100216 /* linux just needs an open() to trigger
217 * the partition table update
218 * but remember to load the module with max_part != 0 :
219 * modprobe nbd max_part=63
220 */
221 nbd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100222 if (nbd >= 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100223 close(nbd);
thscd831bd2008-07-03 10:23:51 +0000224 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100225 return NULL;
226}
227
228static void *nbd_client_thread(void *arg)
229{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100230 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100231 off_t size;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100232 uint32_t nbdflags;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100233 int fd, sock;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100234 int ret;
235 pthread_t show_parts_thread;
Max Reitz1ce52842015-01-26 21:02:59 -0500236 Error *local_error = NULL;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100237
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100238
239 sock = socket_connect(saddr, &local_error, NULL, NULL);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100240 if (sock < 0) {
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100241 error_report_err(local_error);
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000242 goto out;
243 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100244
245 ret = nbd_receive_negotiate(sock, NULL, &nbdflags,
Max Reitz3f472652015-02-25 13:08:25 -0500246 &size, &local_error);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100247 if (ret < 0) {
Max Reitz1ce52842015-01-26 21:02:59 -0500248 if (local_error) {
Markus Armbruster78288672015-12-18 16:35:07 +0100249 error_report_err(local_error);
Max Reitz1ce52842015-01-26 21:02:59 -0500250 }
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100251 goto out_socket;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100252 }
253
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100254 fd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100255 if (fd < 0) {
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100256 /* Linux-only, we can use %m in printf. */
Markus Armbrusterb9884682015-12-18 16:35:18 +0100257 error_report("Failed to open %s: %m", device);
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100258 goto out_socket;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100259 }
260
Max Reitz3f472652015-02-25 13:08:25 -0500261 ret = nbd_init(fd, sock, nbdflags, size);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100262 if (ret < 0) {
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100263 goto out_fd;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100264 }
265
266 /* update partition table */
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100267 pthread_create(&show_parts_thread, NULL, show_parts, device);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100268
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100269 if (verbose) {
270 fprintf(stderr, "NBD device %s is now connected to %s\n",
271 device, srcpath);
272 } else {
273 /* Close stderr so that the qemu-nbd process exits. */
274 dup2(STDOUT_FILENO, STDERR_FILENO);
275 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100276
277 ret = nbd_client(fd);
278 if (ret) {
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100279 goto out_fd;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100280 }
281 close(fd);
282 kill(getpid(), SIGTERM);
283 return (void *) EXIT_SUCCESS;
284
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100285out_fd:
286 close(fd);
287out_socket:
288 closesocket(sock);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100289out:
290 kill(getpid(), SIGTERM);
291 return (void *) EXIT_FAILURE;
thscd831bd2008-07-03 10:23:51 +0000292}
293
Fam Zhenge4afbf42015-05-19 10:50:59 +0000294static int nbd_can_accept(void)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200295{
296 return nb_fds < shared;
297}
298
Paolo Bonzini7860a382012-09-18 13:31:56 +0200299static void nbd_export_closed(NBDExport *exp)
300{
301 assert(state == TERMINATING);
302 state = TERMINATED;
303}
304
Fam Zhenge4afbf42015-05-19 10:50:59 +0000305static void nbd_update_server_fd_handler(int fd);
306
Paolo Bonzini1743b512011-09-19 14:33:23 +0200307static void nbd_client_closed(NBDClient *client)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200308{
Paolo Bonzini1743b512011-09-19 14:33:23 +0200309 nb_fds--;
Paolo Bonzini7860a382012-09-18 13:31:56 +0200310 if (nb_fds == 0 && !persistent && state == RUNNING) {
311 state = TERMINATE;
312 }
Fam Zhenge4afbf42015-05-19 10:50:59 +0000313 nbd_update_server_fd_handler(server_fd);
Paolo Bonzini7860a382012-09-18 13:31:56 +0200314 nbd_client_put(client);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200315}
316
317static void nbd_accept(void *opaque)
318{
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200319 struct sockaddr_in addr;
320 socklen_t addr_len = sizeof(addr);
321
322 int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100323 if (fd < 0) {
324 perror("accept");
325 return;
326 }
327
Paolo Bonzini7860a382012-09-18 13:31:56 +0200328 if (state >= TERMINATE) {
329 close(fd);
330 return;
331 }
332
Fam Zhengee7d7aa2016-01-14 16:41:01 +0800333 nb_fds++;
334 nbd_update_server_fd_handler(server_fd);
335 nbd_client_new(exp, fd, nbd_client_closed);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200336}
337
Fam Zhenge4afbf42015-05-19 10:50:59 +0000338static void nbd_update_server_fd_handler(int fd)
339{
340 if (nbd_can_accept()) {
341 qemu_set_fd_handler(fd, nbd_accept, NULL, (void *)(uintptr_t)fd);
342 } else {
343 qemu_set_fd_handler(fd, NULL, NULL, NULL);
344 }
345}
346
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100347
348static SocketAddress *nbd_build_socket_address(const char *sockpath,
349 const char *bindto,
350 const char *port)
351{
352 SocketAddress *saddr;
353
354 saddr = g_new0(SocketAddress, 1);
355 if (sockpath) {
Eric Blake2d32add2015-10-26 16:34:55 -0600356 saddr->type = SOCKET_ADDRESS_KIND_UNIX;
357 saddr->u.q_unix = g_new0(UnixSocketAddress, 1);
358 saddr->u.q_unix->path = g_strdup(sockpath);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100359 } else {
Eric Blake2d32add2015-10-26 16:34:55 -0600360 saddr->type = SOCKET_ADDRESS_KIND_INET;
361 saddr->u.inet = g_new0(InetSocketAddress, 1);
362 saddr->u.inet->host = g_strdup(bindto);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100363 if (port) {
Eric Blake2d32add2015-10-26 16:34:55 -0600364 saddr->u.inet->port = g_strdup(port);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100365 } else {
Eric Blake2d32add2015-10-26 16:34:55 -0600366 saddr->u.inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100367 }
368 }
369
370 return saddr;
371}
372
373
bellard7a5ca862008-05-27 21:13:40 +0000374int main(int argc, char **argv)
375{
Markus Armbruster26f54e92014-10-07 13:59:04 +0200376 BlockBackend *blk;
bellard7a5ca862008-05-27 21:13:40 +0000377 BlockDriverState *bs;
378 off_t dev_offset = 0;
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200379 uint32_t nbdflags = 0;
thscd831bd2008-07-03 10:23:51 +0000380 bool disconnect = false;
bellard7a5ca862008-05-27 21:13:40 +0000381 const char *bindto = "0.0.0.0";
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100382 const char *port = NULL;
383 char *sockpath = NULL;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100384 char *device = NULL;
bellard7a5ca862008-05-27 21:13:40 +0000385 off_t fd_size;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800386 QemuOpts *sn_opts = NULL;
387 const char *sn_id_or_name = NULL;
388 const char *sopt = "hVb:o:p:rsnP:c:dvk:e:f:tl:";
bellard7a5ca862008-05-27 21:13:40 +0000389 struct option lopt[] = {
Blue Swirl660f11b2009-07-31 21:16:51 +0000390 { "help", 0, NULL, 'h' },
391 { "version", 0, NULL, 'V' },
392 { "bind", 1, NULL, 'b' },
393 { "port", 1, NULL, 'p' },
394 { "socket", 1, NULL, 'k' },
395 { "offset", 1, NULL, 'o' },
396 { "read-only", 0, NULL, 'r' },
397 { "partition", 1, NULL, 'P' },
398 { "connect", 1, NULL, 'c' },
399 { "disconnect", 0, NULL, 'd' },
400 { "snapshot", 0, NULL, 's' },
Wenchao Xia8c116b02013-12-04 17:10:55 +0800401 { "load-snapshot", 1, NULL, 'l' },
Blue Swirl660f11b2009-07-31 21:16:51 +0000402 { "nocache", 0, NULL, 'n' },
Paolo Bonzini39a52352012-07-18 14:57:15 +0200403 { "cache", 1, NULL, QEMU_NBD_OPT_CACHE },
Paolo Bonzini39a52352012-07-18 14:57:15 +0200404 { "aio", 1, NULL, QEMU_NBD_OPT_AIO },
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100405 { "discard", 1, NULL, QEMU_NBD_OPT_DISCARD },
Peter Lievenb3838a42014-08-13 19:20:18 +0200406 { "detect-zeroes", 1, NULL, QEMU_NBD_OPT_DETECT_ZEROES },
Blue Swirl660f11b2009-07-31 21:16:51 +0000407 { "shared", 1, NULL, 'e' },
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000408 { "format", 1, NULL, 'f' },
Blue Swirl660f11b2009-07-31 21:16:51 +0000409 { "persistent", 0, NULL, 't' },
410 { "verbose", 0, NULL, 'v' },
411 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000412 };
413 int ch;
414 int opt_ind = 0;
bellard7a5ca862008-05-27 21:13:40 +0000415 char *end;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200416 int flags = BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000417 int partition = -1;
Max Reitz4fbec262015-02-05 13:58:19 -0500418 int ret = 0;
ths3b05a8e2008-07-03 12:45:02 +0000419 int fd;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200420 bool seen_cache = false;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100421 bool seen_discard = false;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200422 bool seen_aio = false;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100423 pthread_t client_thread;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000424 const char *fmt = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200425 Error *local_err = NULL;
Peter Lievenb3838a42014-08-13 19:20:18 +0200426 BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
Max Reitz4fbec262015-02-05 13:58:19 -0500427 QDict *options = NULL;
bellard7a5ca862008-05-27 21:13:40 +0000428
Paolo Bonzinia517e882011-11-04 15:51:21 +0100429 /* The client thread uses SIGTERM to interrupt the server. A signal
430 * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
431 */
Paolo Bonzinibb345112011-11-04 15:51:19 +0100432 struct sigaction sa_sigterm;
Paolo Bonzinibb345112011-11-04 15:51:19 +0100433 memset(&sa_sigterm, 0, sizeof(sa_sigterm));
434 sa_sigterm.sa_handler = termsig_handler;
435 sigaction(SIGTERM, &sa_sigterm, NULL);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800436 qemu_init_exec_dir(argv[0]);
Paolo Bonzinibb345112011-11-04 15:51:19 +0100437
bellard7a5ca862008-05-27 21:13:40 +0000438 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
439 switch (ch) {
440 case 's':
ths2f726482008-07-03 11:47:46 +0000441 flags |= BDRV_O_SNAPSHOT;
442 break;
443 case 'n':
Paolo Bonzini39a52352012-07-18 14:57:15 +0200444 optarg = (char *) "none";
445 /* fallthrough */
446 case QEMU_NBD_OPT_CACHE:
447 if (seen_cache) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100448 error_report("-n and --cache can only be specified once");
449 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200450 }
451 seen_cache = true;
452 if (bdrv_parse_cache_flags(optarg, &flags) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100453 error_report("Invalid cache mode `%s'", optarg);
454 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200455 }
bellard7a5ca862008-05-27 21:13:40 +0000456 break;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200457 case QEMU_NBD_OPT_AIO:
458 if (seen_aio) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100459 error_report("--aio can only be specified once");
460 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200461 }
462 seen_aio = true;
463 if (!strcmp(optarg, "native")) {
464 flags |= BDRV_O_NATIVE_AIO;
465 } else if (!strcmp(optarg, "threads")) {
466 /* this is the default */
467 } else {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100468 error_report("invalid aio mode `%s'", optarg);
469 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200470 }
471 break;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100472 case QEMU_NBD_OPT_DISCARD:
473 if (seen_discard) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100474 error_report("--discard can only be specified once");
475 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100476 }
477 seen_discard = true;
478 if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100479 error_report("Invalid discard mode `%s'", optarg);
480 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100481 }
482 break;
Peter Lievenb3838a42014-08-13 19:20:18 +0200483 case QEMU_NBD_OPT_DETECT_ZEROES:
484 detect_zeroes =
485 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
486 optarg,
Eric Blake7fb1cf12015-11-18 01:52:57 -0700487 BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX,
Peter Lievenb3838a42014-08-13 19:20:18 +0200488 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
489 &local_err);
490 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100491 error_reportf_err(local_err,
492 "Failed to parse detect_zeroes mode: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100493 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200494 }
495 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
496 !(flags & BDRV_O_UNMAP)) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100497 error_report("setting detect-zeroes to unmap is not allowed "
498 "without setting discard operation to unmap");
499 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200500 }
501 break;
bellard7a5ca862008-05-27 21:13:40 +0000502 case 'b':
503 bindto = optarg;
504 break;
505 case 'p':
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100506 port = optarg;
bellard7a5ca862008-05-27 21:13:40 +0000507 break;
508 case 'o':
509 dev_offset = strtoll (optarg, &end, 0);
510 if (*end) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100511 error_report("Invalid offset `%s'", optarg);
512 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000513 }
514 if (dev_offset < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100515 error_report("Offset must be positive `%s'", optarg);
516 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000517 }
518 break;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800519 case 'l':
520 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +0100521 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
522 optarg, false);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800523 if (!sn_opts) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100524 error_report("Failed in parsing snapshot param `%s'",
525 optarg);
526 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800527 }
528 } else {
529 sn_id_or_name = optarg;
530 }
531 /* fall through */
bellard7a5ca862008-05-27 21:13:40 +0000532 case 'r':
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200533 nbdflags |= NBD_FLAG_READ_ONLY;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200534 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000535 break;
536 case 'P':
537 partition = strtol(optarg, &end, 0);
Peter Lieven713cc672014-08-13 19:20:19 +0200538 if (*end) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100539 error_report("Invalid partition `%s'", optarg);
540 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200541 }
542 if (partition < 1 || partition > 8) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100543 error_report("Invalid partition %d", partition);
544 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200545 }
bellard7a5ca862008-05-27 21:13:40 +0000546 break;
thscd831bd2008-07-03 10:23:51 +0000547 case 'k':
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100548 sockpath = optarg;
Peter Lieven713cc672014-08-13 19:20:19 +0200549 if (sockpath[0] != '/') {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +0100550 error_report("socket path must be absolute");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100551 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200552 }
thscd831bd2008-07-03 10:23:51 +0000553 break;
554 case 'd':
555 disconnect = true;
556 break;
557 case 'c':
558 device = optarg;
559 break;
ths3b05a8e2008-07-03 12:45:02 +0000560 case 'e':
561 shared = strtol(optarg, &end, 0);
562 if (*end) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100563 error_report("Invalid shared device number '%s'", optarg);
564 exit(EXIT_FAILURE);
ths3b05a8e2008-07-03 12:45:02 +0000565 }
566 if (shared < 1) {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +0100567 error_report("Shared device number must be greater than 0");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100568 exit(EXIT_FAILURE);
ths3b05a8e2008-07-03 12:45:02 +0000569 }
570 break;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000571 case 'f':
572 fmt = optarg;
573 break;
Peter Lieven713cc672014-08-13 19:20:19 +0200574 case 't':
575 persistent = 1;
576 break;
bellard7a5ca862008-05-27 21:13:40 +0000577 case 'v':
578 verbose = 1;
579 break;
580 case 'V':
581 version(argv[0]);
582 exit(0);
583 break;
584 case 'h':
585 usage(argv[0]);
586 exit(0);
587 break;
588 case '?':
Markus Armbruster85b01e02015-12-18 16:35:04 +0100589 error_report("Try `%s --help' for more information.", argv[0]);
590 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000591 }
592 }
593
594 if ((argc - optind) != 1) {
Markus Armbruster433672b2015-12-18 16:35:24 +0100595 error_report("Invalid number of arguments");
596 error_printf("Try `%s --help' for more information.\n", argv[0]);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100597 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000598 }
599
thscd831bd2008-07-03 10:23:51 +0000600 if (disconnect) {
601 fd = open(argv[optind], O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100602 if (fd < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100603 error_report("Cannot open %s: %s", argv[optind],
604 strerror(errno));
605 exit(EXIT_FAILURE);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100606 }
thscd831bd2008-07-03 10:23:51 +0000607 nbd_disconnect(fd);
608
609 close(fd);
610
611 printf("%s disconnected\n", argv[optind]);
612
Peter Lieven713cc672014-08-13 19:20:19 +0200613 return 0;
thscd831bd2008-07-03 10:23:51 +0000614 }
615
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100616 if (device && !verbose) {
617 int stderr_fd[2];
618 pid_t pid;
619 int ret;
620
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100621 if (qemu_pipe(stderr_fd) < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100622 error_report("Error setting up communication pipe: %s",
623 strerror(errno));
624 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100625 }
626
627 /* Now daemonize, but keep a communication channel open to
628 * print errors and exit with the proper status code.
629 */
630 pid = fork();
Max Reitz70d47392015-02-25 13:08:22 -0500631 if (pid < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100632 error_report("Failed to fork: %s", strerror(errno));
633 exit(EXIT_FAILURE);
Max Reitz70d47392015-02-25 13:08:22 -0500634 } else if (pid == 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100635 close(stderr_fd[0]);
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400636 ret = qemu_daemon(1, 0);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100637
638 /* Temporarily redirect stderr to the parent's pipe... */
639 dup2(stderr_fd[1], STDERR_FILENO);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100640 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100641 error_report("Failed to daemonize: %s", strerror(errno));
642 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100643 }
644
645 /* ... close the descriptor we inherited and go on. */
646 close(stderr_fd[1]);
647 } else {
648 bool errors = false;
649 char *buf;
650
651 /* In the parent. Print error messages from the child until
652 * it closes the pipe.
653 */
654 close(stderr_fd[1]);
655 buf = g_malloc(1024);
656 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
657 errors = true;
658 ret = qemu_write_full(STDERR_FILENO, buf, ret);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100659 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100660 exit(EXIT_FAILURE);
661 }
662 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100663 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100664 error_report("Cannot read from daemon: %s",
665 strerror(errno));
666 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100667 }
668
669 /* Usually the daemon should not print any message.
670 * Exit with zero status in that case.
671 */
672 exit(errors);
673 }
674 }
675
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100676 if (device != NULL && sockpath == NULL) {
677 sockpath = g_malloc(128);
678 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
thscd831bd2008-07-03 10:23:51 +0000679 }
680
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100681 saddr = nbd_build_socket_address(sockpath, bindto, port);
682
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300683 if (qemu_init_main_loop(&local_err)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100684 error_report_err(local_err);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300685 exit(EXIT_FAILURE);
686 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100687 bdrv_init();
688 atexit(bdrv_close_all);
689
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000690 if (fmt) {
Max Reitz4fbec262015-02-05 13:58:19 -0500691 options = qdict_new();
692 qdict_put(options, "driver", qstring_from_str(fmt));
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000693 }
694
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100695 srcpath = argv[optind];
Max Reitz4fbec262015-02-05 13:58:19 -0500696 blk = blk_new_open("hda", srcpath, NULL, options, flags, &local_err);
697 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100698 error_reportf_err(local_err, "Failed to blk_new_open '%s': ",
699 argv[optind]);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100700 exit(EXIT_FAILURE);
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100701 }
Max Reitz4fbec262015-02-05 13:58:19 -0500702 bs = blk_bs(blk);
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100703
Wenchao Xia8c116b02013-12-04 17:10:55 +0800704 if (sn_opts) {
705 ret = bdrv_snapshot_load_tmp(bs,
706 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
707 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
708 &local_err);
709 } else if (sn_id_or_name) {
710 ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
711 &local_err);
712 }
713 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100714 error_reportf_err(local_err, "Failed to load snapshot: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100715 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800716 }
717
Peter Lievenb3838a42014-08-13 19:20:18 +0200718 bs->detect_zeroes = detect_zeroes;
Max Reitz4c58e802014-11-18 12:21:19 +0100719 fd_size = blk_getlength(blk);
Max Reitz98f44bb2015-02-25 13:08:21 -0500720 if (fd_size < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100721 error_report("Failed to determine the image length: %s",
722 strerror(-fd_size));
723 exit(EXIT_FAILURE);
Max Reitz98f44bb2015-02-25 13:08:21 -0500724 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100725
Paolo Bonzini185b4332012-03-05 08:56:10 +0100726 if (partition != -1) {
Max Reitz4c58e802014-11-18 12:21:19 +0100727 ret = find_partition(blk, partition, &dev_offset, &fd_size);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100728 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100729 error_report("Could not find partition %d: %s", partition,
Markus Armbrustera4699e52015-12-18 16:35:10 +0100730 strerror(-ret));
Markus Armbruster85b01e02015-12-18 16:35:04 +0100731 exit(EXIT_FAILURE);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100732 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100733 }
734
Max Reitz98f44bb2015-02-25 13:08:21 -0500735 exp = nbd_export_new(blk, dev_offset, fd_size, nbdflags, nbd_export_closed,
736 &local_err);
737 if (!exp) {
Markus Armbruster4fffeb52015-12-18 16:35:05 +0100738 error_report_err(local_err);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100739 exit(EXIT_FAILURE);
Max Reitz98f44bb2015-02-25 13:08:21 -0500740 }
ths3b05a8e2008-07-03 12:45:02 +0000741
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100742 fd = socket_listen(saddr, &local_err);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100743 if (fd < 0) {
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100744 error_report_err(local_err);
bellard7a5ca862008-05-27 21:13:40 +0000745 return 1;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200746 }
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100747
748 if (device) {
749 int ret;
750
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100751 ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100752 if (ret != 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100753 error_report("Failed to create client thread: %s", strerror(ret));
754 exit(EXIT_FAILURE);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100755 }
756 } else {
757 /* Shut up GCC warnings. */
758 memset(&client_thread, 0, sizeof(client_thread));
759 }
760
Fam Zhenge4afbf42015-05-19 10:50:59 +0000761 server_fd = fd;
762 nbd_update_server_fd_handler(fd);
bellard7a5ca862008-05-27 21:13:40 +0000763
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400764 /* now when the initialization is (almost) complete, chdir("/")
765 * to free any busy filesystems */
766 if (chdir("/") < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100767 error_report("Could not chdir to root directory: %s",
768 strerror(errno));
769 exit(EXIT_FAILURE);
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400770 }
771
Paolo Bonzini7860a382012-09-18 13:31:56 +0200772 state = RUNNING;
ths3b05a8e2008-07-03 12:45:02 +0000773 do {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200774 main_loop_wait(false);
Paolo Bonzini7860a382012-09-18 13:31:56 +0200775 if (state == TERMINATE) {
776 state = TERMINATING;
777 nbd_export_close(exp);
778 nbd_export_put(exp);
779 exp = NULL;
780 }
781 } while (state != TERMINATED);
ths3b05a8e2008-07-03 12:45:02 +0000782
Markus Armbruster26f54e92014-10-07 13:59:04 +0200783 blk_unref(blk);
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100784 if (sockpath) {
785 unlink(sockpath);
786 }
bellard7a5ca862008-05-27 21:13:40 +0000787
Markus Armbrusterfbf28a42014-09-29 16:07:55 +0200788 qemu_opts_del(sn_opts);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800789
Paolo Bonzinia517e882011-11-04 15:51:21 +0100790 if (device) {
791 void *ret;
792 pthread_join(client_thread, &ret);
793 exit(ret != NULL);
794 } else {
795 exit(EXIT_SUCCESS);
796 }
bellard7a5ca862008-05-27 21:13:40 +0000797}