blob: 5e542904110e8a33e2a149a9f5c6411d9a99373b [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/error-report.h"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +000026#include "qemu/config-file.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"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +000030#include "qom/object_interfaces.h"
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +000031#include "io/channel-socket.h"
bellard7a5ca862008-05-27 21:13:40 +000032
bellard7a5ca862008-05-27 21:13:40 +000033#include <getopt.h>
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +000034#include <sys/types.h>
35#include <signal.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
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +000044#define QEMU_NBD_OPT_OBJECT 5
bellard7a5ca862008-05-27 21:13:40 +000045
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +020046static NBDExport *exp;
blueswir1b1d8e522008-10-26 13:43:07 +000047static int verbose;
Paolo Bonzinia517e882011-11-04 15:51:21 +010048static char *srcpath;
Daniel P. Berrange48bec072015-09-16 14:52:23 +010049static SocketAddress *saddr;
Paolo Bonzini7860a382012-09-18 13:31:56 +020050static int persistent = 0;
51static enum { RUNNING, TERMINATE, TERMINATING, TERMINATED } state;
Paolo Bonzinia61c6782011-09-12 17:28:11 +020052static int shared = 1;
53static int nb_fds;
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +000054static QIOChannelSocket *server_ioc;
55static int server_watch = -1;
bellard7a5ca862008-05-27 21:13:40 +000056
57static void usage(const char *name)
58{
Paolo Bonzinib033cd82012-07-18 14:50:52 +020059 (printf) (
bellard7a5ca862008-05-27 21:13:40 +000060"Usage: %s [OPTIONS] FILE\n"
61"QEMU Disk Network Block Device Server\n"
62"\n"
Peter Lieven713cc672014-08-13 19:20:19 +020063" -h, --help display this help and exit\n"
64" -V, --version output version information and exit\n"
bellard7a5ca862008-05-27 21:13:40 +000065"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020066"Connection properties:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020067" -p, --port=PORT port to listen on (default `%d')\n"
68" -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
69" -k, --socket=PATH path to the unix socket\n"
70" (default '"SOCKET_PATH"')\n"
71" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
72" -t, --persistent don't exit on the last connection\n"
73" -v, --verbose display extra debugging information\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020074"\n"
75"Exposing part of the image:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020076" -o, --offset=OFFSET offset into the image\n"
77" -P, --partition=NUM only expose partition NUM\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020078"\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +000079"General purpose options:\n"
80" --object type,id=ID,... define an object such as 'secret' for providing\n"
81" passwords and/or encryption keys\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020082#ifdef __linux__
83"Kernel NBD client support:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020084" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
85" -d, --disconnect disconnect the specified device\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020086"\n"
87#endif
88"\n"
89"Block device options:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020090" -f, --format=FORMAT set image format (raw, qcow2, ...)\n"
91" -r, --read-only export read-only\n"
92" -s, --snapshot use FILE as an external snapshot, create a temporary\n"
93" file with backing_file=FILE, redirect the write to\n"
94" the temporary one\n"
Wenchao Xia8c116b02013-12-04 17:10:55 +080095" -l, --load-snapshot=SNAPSHOT_PARAM\n"
Peter Lieven713cc672014-08-13 19:20:19 +020096" load an internal snapshot inside FILE and export it\n"
97" as an read-only device, SNAPSHOT_PARAM format is\n"
98" 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
99" '[ID_OR_NAME]'\n"
100" -n, --nocache disable host cache\n"
101" --cache=MODE set cache mode (none, writeback, ...)\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200102" --aio=MODE set AIO mode (native or threads)\n"
Peter Lievenb3838a42014-08-13 19:20:18 +0200103" --discard=MODE set discard mode (ignore, unmap)\n"
Andrey Korolyov6883de62015-07-31 22:12:54 +0300104" --detect-zeroes=MODE set detect-zeroes mode (off, on, unmap)\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200105"\n"
106"Report bugs to <qemu-devel@nongnu.org>\n"
Laurent Vivierc2e28722010-09-17 20:37:25 +0200107 , name, NBD_DEFAULT_PORT, "DEVICE");
bellard7a5ca862008-05-27 21:13:40 +0000108}
109
110static void version(const char *name)
111{
112 printf(
ths315bc7a2008-07-18 18:06:23 +0000113"%s version 0.0.1\n"
bellard7a5ca862008-05-27 21:13:40 +0000114"Written by Anthony Liguori.\n"
115"\n"
116"Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
117"This is free software; see the source for copying conditions. There is NO\n"
118"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
ths315bc7a2008-07-18 18:06:23 +0000119 , name);
bellard7a5ca862008-05-27 21:13:40 +0000120}
121
122struct partition_record
123{
124 uint8_t bootable;
125 uint8_t start_head;
126 uint32_t start_cylinder;
127 uint8_t start_sector;
128 uint8_t system;
129 uint8_t end_head;
130 uint8_t end_cylinder;
131 uint8_t end_sector;
132 uint32_t start_sector_abs;
133 uint32_t nb_sectors_abs;
134};
135
136static void read_partition(uint8_t *p, struct partition_record *r)
137{
138 r->bootable = p[0];
139 r->start_head = p[1];
140 r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
141 r->start_sector = p[2] & 0x3f;
142 r->system = p[4];
143 r->end_head = p[5];
144 r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
145 r->end_sector = p[6] & 0x3f;
Max Reitzac973932015-02-25 13:08:23 -0500146
147 r->start_sector_abs = le32_to_cpup((uint32_t *)(p + 8));
148 r->nb_sectors_abs = le32_to_cpup((uint32_t *)(p + 12));
bellard7a5ca862008-05-27 21:13:40 +0000149}
150
Max Reitz4c58e802014-11-18 12:21:19 +0100151static int find_partition(BlockBackend *blk, int partition,
bellard7a5ca862008-05-27 21:13:40 +0000152 off_t *offset, off_t *size)
153{
154 struct partition_record mbr[4];
155 uint8_t data[512];
156 int i;
157 int ext_partnum = 4;
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900158 int ret;
bellard7a5ca862008-05-27 21:13:40 +0000159
Max Reitz4c58e802014-11-18 12:21:19 +0100160 if ((ret = blk_read(blk, 0, data, 1)) < 0) {
Markus Armbrustera4699e52015-12-18 16:35:10 +0100161 error_report("error while reading: %s", strerror(-ret));
Markus Armbruster85b01e02015-12-18 16:35:04 +0100162 exit(EXIT_FAILURE);
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900163 }
bellard7a5ca862008-05-27 21:13:40 +0000164
165 if (data[510] != 0x55 || data[511] != 0xaa) {
Paolo Bonzini185b4332012-03-05 08:56:10 +0100166 return -EINVAL;
bellard7a5ca862008-05-27 21:13:40 +0000167 }
168
169 for (i = 0; i < 4; i++) {
170 read_partition(&data[446 + 16 * i], &mbr[i]);
171
Max Reitz453b07b2015-02-25 13:08:15 -0500172 if (!mbr[i].system || !mbr[i].nb_sectors_abs) {
bellard7a5ca862008-05-27 21:13:40 +0000173 continue;
Max Reitz453b07b2015-02-25 13:08:15 -0500174 }
bellard7a5ca862008-05-27 21:13:40 +0000175
176 if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
177 struct partition_record ext[4];
178 uint8_t data1[512];
179 int j;
180
Max Reitz4c58e802014-11-18 12:21:19 +0100181 if ((ret = blk_read(blk, mbr[i].start_sector_abs, data1, 1)) < 0) {
Markus Armbrustera4699e52015-12-18 16:35:10 +0100182 error_report("error while reading: %s", strerror(-ret));
Markus Armbruster85b01e02015-12-18 16:35:04 +0100183 exit(EXIT_FAILURE);
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900184 }
bellard7a5ca862008-05-27 21:13:40 +0000185
186 for (j = 0; j < 4; j++) {
187 read_partition(&data1[446 + 16 * j], &ext[j]);
Max Reitz453b07b2015-02-25 13:08:15 -0500188 if (!ext[j].system || !ext[j].nb_sectors_abs) {
bellard7a5ca862008-05-27 21:13:40 +0000189 continue;
Max Reitz453b07b2015-02-25 13:08:15 -0500190 }
bellard7a5ca862008-05-27 21:13:40 +0000191
192 if ((ext_partnum + j + 1) == partition) {
193 *offset = (uint64_t)ext[j].start_sector_abs << 9;
194 *size = (uint64_t)ext[j].nb_sectors_abs << 9;
195 return 0;
196 }
197 }
198 ext_partnum += 4;
199 } else if ((i + 1) == partition) {
200 *offset = (uint64_t)mbr[i].start_sector_abs << 9;
201 *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
202 return 0;
203 }
204 }
205
Paolo Bonzini185b4332012-03-05 08:56:10 +0100206 return -ENOENT;
bellard7a5ca862008-05-27 21:13:40 +0000207}
208
Paolo Bonzinibb345112011-11-04 15:51:19 +0100209static void termsig_handler(int signum)
210{
Paolo Bonzini7860a382012-09-18 13:31:56 +0200211 state = TERMINATE;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200212 qemu_notify_event();
Paolo Bonzinibb345112011-11-04 15:51:19 +0100213}
214
Paolo Bonzini537b41f2014-02-17 14:43:51 +0100215
Paolo Bonzinia517e882011-11-04 15:51:21 +0100216static void *show_parts(void *arg)
thscd831bd2008-07-03 10:23:51 +0000217{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100218 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100219 int nbd;
thscd831bd2008-07-03 10:23:51 +0000220
Paolo Bonzinia517e882011-11-04 15:51:21 +0100221 /* linux just needs an open() to trigger
222 * the partition table update
223 * but remember to load the module with max_part != 0 :
224 * modprobe nbd max_part=63
225 */
226 nbd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100227 if (nbd >= 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100228 close(nbd);
thscd831bd2008-07-03 10:23:51 +0000229 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100230 return NULL;
231}
232
233static void *nbd_client_thread(void *arg)
234{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100235 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100236 off_t size;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100237 uint32_t nbdflags;
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000238 QIOChannelSocket *sioc;
239 int fd;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100240 int ret;
241 pthread_t show_parts_thread;
Max Reitz1ce52842015-01-26 21:02:59 -0500242 Error *local_error = NULL;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100243
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000244 sioc = qio_channel_socket_new();
245 if (qio_channel_socket_connect_sync(sioc,
246 saddr,
247 &local_error) < 0) {
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100248 error_report_err(local_error);
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000249 goto out;
250 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100251
Daniel P. Berrange1c778ef2016-02-10 18:41:04 +0000252 ret = nbd_receive_negotiate(QIO_CHANNEL(sioc), NULL, &nbdflags,
Max Reitz3f472652015-02-25 13:08:25 -0500253 &size, &local_error);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100254 if (ret < 0) {
Max Reitz1ce52842015-01-26 21:02:59 -0500255 if (local_error) {
Markus Armbruster78288672015-12-18 16:35:07 +0100256 error_report_err(local_error);
Max Reitz1ce52842015-01-26 21:02:59 -0500257 }
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100258 goto out_socket;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100259 }
260
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100261 fd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100262 if (fd < 0) {
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100263 /* Linux-only, we can use %m in printf. */
Markus Armbrusterb9884682015-12-18 16:35:18 +0100264 error_report("Failed to open %s: %m", device);
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100265 goto out_socket;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100266 }
267
Daniel P. Berrange1c778ef2016-02-10 18:41:04 +0000268 ret = nbd_init(fd, sioc, nbdflags, size);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100269 if (ret < 0) {
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100270 goto out_fd;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100271 }
272
273 /* update partition table */
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100274 pthread_create(&show_parts_thread, NULL, show_parts, device);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100275
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100276 if (verbose) {
277 fprintf(stderr, "NBD device %s is now connected to %s\n",
278 device, srcpath);
279 } else {
280 /* Close stderr so that the qemu-nbd process exits. */
281 dup2(STDOUT_FILENO, STDERR_FILENO);
282 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100283
284 ret = nbd_client(fd);
285 if (ret) {
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100286 goto out_fd;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100287 }
288 close(fd);
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000289 object_unref(OBJECT(sioc));
Paolo Bonzinia517e882011-11-04 15:51:21 +0100290 kill(getpid(), SIGTERM);
291 return (void *) EXIT_SUCCESS;
292
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100293out_fd:
294 close(fd);
295out_socket:
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000296 object_unref(OBJECT(sioc));
Paolo Bonzinia517e882011-11-04 15:51:21 +0100297out:
298 kill(getpid(), SIGTERM);
299 return (void *) EXIT_FAILURE;
thscd831bd2008-07-03 10:23:51 +0000300}
301
Fam Zhenge4afbf42015-05-19 10:50:59 +0000302static int nbd_can_accept(void)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200303{
304 return nb_fds < shared;
305}
306
Paolo Bonzini7860a382012-09-18 13:31:56 +0200307static void nbd_export_closed(NBDExport *exp)
308{
309 assert(state == TERMINATING);
310 state = TERMINATED;
311}
312
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000313static void nbd_update_server_watch(void);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000314
Paolo Bonzini1743b512011-09-19 14:33:23 +0200315static void nbd_client_closed(NBDClient *client)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200316{
Paolo Bonzini1743b512011-09-19 14:33:23 +0200317 nb_fds--;
Paolo Bonzini7860a382012-09-18 13:31:56 +0200318 if (nb_fds == 0 && !persistent && state == RUNNING) {
319 state = TERMINATE;
320 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000321 nbd_update_server_watch();
Paolo Bonzini7860a382012-09-18 13:31:56 +0200322 nbd_client_put(client);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200323}
324
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000325static gboolean nbd_accept(QIOChannel *ioc, GIOCondition cond, gpointer opaque)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200326{
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000327 QIOChannelSocket *cioc;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200328
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000329 cioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc),
330 NULL);
331 if (!cioc) {
332 return TRUE;
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100333 }
334
Paolo Bonzini7860a382012-09-18 13:31:56 +0200335 if (state >= TERMINATE) {
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000336 object_unref(OBJECT(cioc));
337 return TRUE;
Paolo Bonzini7860a382012-09-18 13:31:56 +0200338 }
339
Fam Zhengee7d7aa2016-01-14 16:41:01 +0800340 nb_fds++;
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000341 nbd_update_server_watch();
Daniel P. Berrange1c778ef2016-02-10 18:41:04 +0000342 nbd_client_new(exp, cioc, nbd_client_closed);
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000343 object_unref(OBJECT(cioc));
344
345 return TRUE;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200346}
347
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000348static void nbd_update_server_watch(void)
Fam Zhenge4afbf42015-05-19 10:50:59 +0000349{
350 if (nbd_can_accept()) {
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000351 if (server_watch == -1) {
352 server_watch = qio_channel_add_watch(QIO_CHANNEL(server_ioc),
353 G_IO_IN,
354 nbd_accept,
355 NULL, NULL);
356 }
Fam Zhenge4afbf42015-05-19 10:50:59 +0000357 } else {
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000358 if (server_watch != -1) {
359 g_source_remove(server_watch);
360 server_watch = -1;
361 }
Fam Zhenge4afbf42015-05-19 10:50:59 +0000362 }
363}
364
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100365
366static SocketAddress *nbd_build_socket_address(const char *sockpath,
367 const char *bindto,
368 const char *port)
369{
370 SocketAddress *saddr;
371
372 saddr = g_new0(SocketAddress, 1);
373 if (sockpath) {
Eric Blake2d32add2015-10-26 16:34:55 -0600374 saddr->type = SOCKET_ADDRESS_KIND_UNIX;
375 saddr->u.q_unix = g_new0(UnixSocketAddress, 1);
376 saddr->u.q_unix->path = g_strdup(sockpath);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100377 } else {
Eric Blake2d32add2015-10-26 16:34:55 -0600378 saddr->type = SOCKET_ADDRESS_KIND_INET;
379 saddr->u.inet = g_new0(InetSocketAddress, 1);
380 saddr->u.inet->host = g_strdup(bindto);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100381 if (port) {
Eric Blake2d32add2015-10-26 16:34:55 -0600382 saddr->u.inet->port = g_strdup(port);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100383 } else {
Eric Blake2d32add2015-10-26 16:34:55 -0600384 saddr->u.inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100385 }
386 }
387
388 return saddr;
389}
390
391
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000392static QemuOptsList qemu_object_opts = {
393 .name = "object",
394 .implied_opt_name = "qom-type",
395 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
396 .desc = {
397 { }
398 },
399};
400
401
bellard7a5ca862008-05-27 21:13:40 +0000402int main(int argc, char **argv)
403{
Markus Armbruster26f54e92014-10-07 13:59:04 +0200404 BlockBackend *blk;
bellard7a5ca862008-05-27 21:13:40 +0000405 BlockDriverState *bs;
406 off_t dev_offset = 0;
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200407 uint32_t nbdflags = 0;
thscd831bd2008-07-03 10:23:51 +0000408 bool disconnect = false;
bellard7a5ca862008-05-27 21:13:40 +0000409 const char *bindto = "0.0.0.0";
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100410 const char *port = NULL;
411 char *sockpath = NULL;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100412 char *device = NULL;
bellard7a5ca862008-05-27 21:13:40 +0000413 off_t fd_size;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800414 QemuOpts *sn_opts = NULL;
415 const char *sn_id_or_name = NULL;
416 const char *sopt = "hVb:o:p:rsnP:c:dvk:e:f:tl:";
bellard7a5ca862008-05-27 21:13:40 +0000417 struct option lopt[] = {
Blue Swirl660f11b2009-07-31 21:16:51 +0000418 { "help", 0, NULL, 'h' },
419 { "version", 0, NULL, 'V' },
420 { "bind", 1, NULL, 'b' },
421 { "port", 1, NULL, 'p' },
422 { "socket", 1, NULL, 'k' },
423 { "offset", 1, NULL, 'o' },
424 { "read-only", 0, NULL, 'r' },
425 { "partition", 1, NULL, 'P' },
426 { "connect", 1, NULL, 'c' },
427 { "disconnect", 0, NULL, 'd' },
428 { "snapshot", 0, NULL, 's' },
Wenchao Xia8c116b02013-12-04 17:10:55 +0800429 { "load-snapshot", 1, NULL, 'l' },
Blue Swirl660f11b2009-07-31 21:16:51 +0000430 { "nocache", 0, NULL, 'n' },
Paolo Bonzini39a52352012-07-18 14:57:15 +0200431 { "cache", 1, NULL, QEMU_NBD_OPT_CACHE },
Paolo Bonzini39a52352012-07-18 14:57:15 +0200432 { "aio", 1, NULL, QEMU_NBD_OPT_AIO },
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100433 { "discard", 1, NULL, QEMU_NBD_OPT_DISCARD },
Peter Lievenb3838a42014-08-13 19:20:18 +0200434 { "detect-zeroes", 1, NULL, QEMU_NBD_OPT_DETECT_ZEROES },
Blue Swirl660f11b2009-07-31 21:16:51 +0000435 { "shared", 1, NULL, 'e' },
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000436 { "format", 1, NULL, 'f' },
Blue Swirl660f11b2009-07-31 21:16:51 +0000437 { "persistent", 0, NULL, 't' },
438 { "verbose", 0, NULL, 'v' },
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000439 { "object", 1, NULL, QEMU_NBD_OPT_OBJECT },
Blue Swirl660f11b2009-07-31 21:16:51 +0000440 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000441 };
442 int ch;
443 int opt_ind = 0;
bellard7a5ca862008-05-27 21:13:40 +0000444 char *end;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200445 int flags = BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000446 int partition = -1;
Max Reitz4fbec262015-02-05 13:58:19 -0500447 int ret = 0;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200448 bool seen_cache = false;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100449 bool seen_discard = false;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200450 bool seen_aio = false;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100451 pthread_t client_thread;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000452 const char *fmt = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200453 Error *local_err = NULL;
Peter Lievenb3838a42014-08-13 19:20:18 +0200454 BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
Max Reitz4fbec262015-02-05 13:58:19 -0500455 QDict *options = NULL;
bellard7a5ca862008-05-27 21:13:40 +0000456
Paolo Bonzinia517e882011-11-04 15:51:21 +0100457 /* The client thread uses SIGTERM to interrupt the server. A signal
458 * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
459 */
Paolo Bonzinibb345112011-11-04 15:51:19 +0100460 struct sigaction sa_sigterm;
Paolo Bonzinibb345112011-11-04 15:51:19 +0100461 memset(&sa_sigterm, 0, sizeof(sa_sigterm));
462 sa_sigterm.sa_handler = termsig_handler;
463 sigaction(SIGTERM, &sa_sigterm, NULL);
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000464 module_call_init(MODULE_INIT_QOM);
465 qemu_add_opts(&qemu_object_opts);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800466 qemu_init_exec_dir(argv[0]);
Paolo Bonzinibb345112011-11-04 15:51:19 +0100467
bellard7a5ca862008-05-27 21:13:40 +0000468 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
469 switch (ch) {
470 case 's':
ths2f726482008-07-03 11:47:46 +0000471 flags |= BDRV_O_SNAPSHOT;
472 break;
473 case 'n':
Paolo Bonzini39a52352012-07-18 14:57:15 +0200474 optarg = (char *) "none";
475 /* fallthrough */
476 case QEMU_NBD_OPT_CACHE:
477 if (seen_cache) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100478 error_report("-n and --cache can only be specified once");
479 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200480 }
481 seen_cache = true;
482 if (bdrv_parse_cache_flags(optarg, &flags) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100483 error_report("Invalid cache mode `%s'", optarg);
484 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200485 }
bellard7a5ca862008-05-27 21:13:40 +0000486 break;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200487 case QEMU_NBD_OPT_AIO:
488 if (seen_aio) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100489 error_report("--aio can only be specified once");
490 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200491 }
492 seen_aio = true;
493 if (!strcmp(optarg, "native")) {
494 flags |= BDRV_O_NATIVE_AIO;
495 } else if (!strcmp(optarg, "threads")) {
496 /* this is the default */
497 } else {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100498 error_report("invalid aio mode `%s'", optarg);
499 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200500 }
501 break;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100502 case QEMU_NBD_OPT_DISCARD:
503 if (seen_discard) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100504 error_report("--discard can only be specified once");
505 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100506 }
507 seen_discard = true;
508 if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100509 error_report("Invalid discard mode `%s'", optarg);
510 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100511 }
512 break;
Peter Lievenb3838a42014-08-13 19:20:18 +0200513 case QEMU_NBD_OPT_DETECT_ZEROES:
514 detect_zeroes =
515 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
516 optarg,
Eric Blake7fb1cf12015-11-18 01:52:57 -0700517 BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX,
Peter Lievenb3838a42014-08-13 19:20:18 +0200518 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
519 &local_err);
520 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100521 error_reportf_err(local_err,
522 "Failed to parse detect_zeroes mode: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100523 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200524 }
525 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
526 !(flags & BDRV_O_UNMAP)) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100527 error_report("setting detect-zeroes to unmap is not allowed "
528 "without setting discard operation to unmap");
529 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200530 }
531 break;
bellard7a5ca862008-05-27 21:13:40 +0000532 case 'b':
533 bindto = optarg;
534 break;
535 case 'p':
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100536 port = optarg;
bellard7a5ca862008-05-27 21:13:40 +0000537 break;
538 case 'o':
539 dev_offset = strtoll (optarg, &end, 0);
540 if (*end) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100541 error_report("Invalid offset `%s'", optarg);
542 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000543 }
544 if (dev_offset < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100545 error_report("Offset must be positive `%s'", optarg);
546 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000547 }
548 break;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800549 case 'l':
550 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +0100551 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
552 optarg, false);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800553 if (!sn_opts) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100554 error_report("Failed in parsing snapshot param `%s'",
555 optarg);
556 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800557 }
558 } else {
559 sn_id_or_name = optarg;
560 }
561 /* fall through */
bellard7a5ca862008-05-27 21:13:40 +0000562 case 'r':
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200563 nbdflags |= NBD_FLAG_READ_ONLY;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200564 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000565 break;
566 case 'P':
567 partition = strtol(optarg, &end, 0);
Peter Lieven713cc672014-08-13 19:20:19 +0200568 if (*end) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100569 error_report("Invalid partition `%s'", optarg);
570 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200571 }
572 if (partition < 1 || partition > 8) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100573 error_report("Invalid partition %d", partition);
574 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200575 }
bellard7a5ca862008-05-27 21:13:40 +0000576 break;
thscd831bd2008-07-03 10:23:51 +0000577 case 'k':
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100578 sockpath = optarg;
Peter Lieven713cc672014-08-13 19:20:19 +0200579 if (sockpath[0] != '/') {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +0100580 error_report("socket path must be absolute");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100581 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200582 }
thscd831bd2008-07-03 10:23:51 +0000583 break;
584 case 'd':
585 disconnect = true;
586 break;
587 case 'c':
588 device = optarg;
589 break;
ths3b05a8e2008-07-03 12:45:02 +0000590 case 'e':
591 shared = strtol(optarg, &end, 0);
592 if (*end) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100593 error_report("Invalid shared device number '%s'", optarg);
594 exit(EXIT_FAILURE);
ths3b05a8e2008-07-03 12:45:02 +0000595 }
596 if (shared < 1) {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +0100597 error_report("Shared device number must be greater than 0");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100598 exit(EXIT_FAILURE);
ths3b05a8e2008-07-03 12:45:02 +0000599 }
600 break;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000601 case 'f':
602 fmt = optarg;
603 break;
Peter Lieven713cc672014-08-13 19:20:19 +0200604 case 't':
605 persistent = 1;
606 break;
bellard7a5ca862008-05-27 21:13:40 +0000607 case 'v':
608 verbose = 1;
609 break;
610 case 'V':
611 version(argv[0]);
612 exit(0);
613 break;
614 case 'h':
615 usage(argv[0]);
616 exit(0);
617 break;
618 case '?':
Markus Armbruster85b01e02015-12-18 16:35:04 +0100619 error_report("Try `%s --help' for more information.", argv[0]);
620 exit(EXIT_FAILURE);
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000621 case QEMU_NBD_OPT_OBJECT: {
622 QemuOpts *opts;
623 opts = qemu_opts_parse_noisily(&qemu_object_opts,
624 optarg, true);
625 if (!opts) {
626 exit(EXIT_FAILURE);
627 }
628 } break;
bellard7a5ca862008-05-27 21:13:40 +0000629 }
630 }
631
632 if ((argc - optind) != 1) {
Markus Armbruster433672b2015-12-18 16:35:24 +0100633 error_report("Invalid number of arguments");
634 error_printf("Try `%s --help' for more information.\n", argv[0]);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100635 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000636 }
637
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000638 if (qemu_opts_foreach(&qemu_object_opts,
639 user_creatable_add_opts_foreach,
640 NULL, &local_err)) {
641 error_report_err(local_err);
642 exit(EXIT_FAILURE);
643 }
644
thscd831bd2008-07-03 10:23:51 +0000645 if (disconnect) {
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000646 int nbdfd = open(argv[optind], O_RDWR);
647 if (nbdfd < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100648 error_report("Cannot open %s: %s", argv[optind],
649 strerror(errno));
650 exit(EXIT_FAILURE);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100651 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000652 nbd_disconnect(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000653
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000654 close(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000655
656 printf("%s disconnected\n", argv[optind]);
657
Peter Lieven713cc672014-08-13 19:20:19 +0200658 return 0;
thscd831bd2008-07-03 10:23:51 +0000659 }
660
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100661 if (device && !verbose) {
662 int stderr_fd[2];
663 pid_t pid;
664 int ret;
665
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100666 if (qemu_pipe(stderr_fd) < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100667 error_report("Error setting up communication pipe: %s",
668 strerror(errno));
669 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100670 }
671
672 /* Now daemonize, but keep a communication channel open to
673 * print errors and exit with the proper status code.
674 */
675 pid = fork();
Max Reitz70d47392015-02-25 13:08:22 -0500676 if (pid < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100677 error_report("Failed to fork: %s", strerror(errno));
678 exit(EXIT_FAILURE);
Max Reitz70d47392015-02-25 13:08:22 -0500679 } else if (pid == 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100680 close(stderr_fd[0]);
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400681 ret = qemu_daemon(1, 0);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100682
683 /* Temporarily redirect stderr to the parent's pipe... */
684 dup2(stderr_fd[1], STDERR_FILENO);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100685 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100686 error_report("Failed to daemonize: %s", strerror(errno));
687 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100688 }
689
690 /* ... close the descriptor we inherited and go on. */
691 close(stderr_fd[1]);
692 } else {
693 bool errors = false;
694 char *buf;
695
696 /* In the parent. Print error messages from the child until
697 * it closes the pipe.
698 */
699 close(stderr_fd[1]);
700 buf = g_malloc(1024);
701 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
702 errors = true;
703 ret = qemu_write_full(STDERR_FILENO, buf, ret);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100704 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100705 exit(EXIT_FAILURE);
706 }
707 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100708 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100709 error_report("Cannot read from daemon: %s",
710 strerror(errno));
711 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100712 }
713
714 /* Usually the daemon should not print any message.
715 * Exit with zero status in that case.
716 */
717 exit(errors);
718 }
719 }
720
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100721 if (device != NULL && sockpath == NULL) {
722 sockpath = g_malloc(128);
723 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
thscd831bd2008-07-03 10:23:51 +0000724 }
725
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100726 saddr = nbd_build_socket_address(sockpath, bindto, port);
727
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300728 if (qemu_init_main_loop(&local_err)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100729 error_report_err(local_err);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300730 exit(EXIT_FAILURE);
731 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100732 bdrv_init();
733 atexit(bdrv_close_all);
734
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000735 if (fmt) {
Max Reitz4fbec262015-02-05 13:58:19 -0500736 options = qdict_new();
737 qdict_put(options, "driver", qstring_from_str(fmt));
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000738 }
739
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100740 srcpath = argv[optind];
Max Reitz4fbec262015-02-05 13:58:19 -0500741 blk = blk_new_open("hda", srcpath, NULL, options, flags, &local_err);
742 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100743 error_reportf_err(local_err, "Failed to blk_new_open '%s': ",
744 argv[optind]);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100745 exit(EXIT_FAILURE);
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100746 }
Max Reitz4fbec262015-02-05 13:58:19 -0500747 bs = blk_bs(blk);
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100748
Wenchao Xia8c116b02013-12-04 17:10:55 +0800749 if (sn_opts) {
750 ret = bdrv_snapshot_load_tmp(bs,
751 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
752 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
753 &local_err);
754 } else if (sn_id_or_name) {
755 ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
756 &local_err);
757 }
758 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100759 error_reportf_err(local_err, "Failed to load snapshot: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100760 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800761 }
762
Peter Lievenb3838a42014-08-13 19:20:18 +0200763 bs->detect_zeroes = detect_zeroes;
Max Reitz4c58e802014-11-18 12:21:19 +0100764 fd_size = blk_getlength(blk);
Max Reitz98f44bb2015-02-25 13:08:21 -0500765 if (fd_size < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100766 error_report("Failed to determine the image length: %s",
767 strerror(-fd_size));
768 exit(EXIT_FAILURE);
Max Reitz98f44bb2015-02-25 13:08:21 -0500769 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100770
Paolo Bonzini185b4332012-03-05 08:56:10 +0100771 if (partition != -1) {
Max Reitz4c58e802014-11-18 12:21:19 +0100772 ret = find_partition(blk, partition, &dev_offset, &fd_size);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100773 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100774 error_report("Could not find partition %d: %s", partition,
Markus Armbrustera4699e52015-12-18 16:35:10 +0100775 strerror(-ret));
Markus Armbruster85b01e02015-12-18 16:35:04 +0100776 exit(EXIT_FAILURE);
Paolo Bonzini185b4332012-03-05 08:56:10 +0100777 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100778 }
779
Max Reitz98f44bb2015-02-25 13:08:21 -0500780 exp = nbd_export_new(blk, dev_offset, fd_size, nbdflags, nbd_export_closed,
781 &local_err);
782 if (!exp) {
Markus Armbruster4fffeb52015-12-18 16:35:05 +0100783 error_report_err(local_err);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100784 exit(EXIT_FAILURE);
Max Reitz98f44bb2015-02-25 13:08:21 -0500785 }
ths3b05a8e2008-07-03 12:45:02 +0000786
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000787 server_ioc = qio_channel_socket_new();
788 if (qio_channel_socket_listen_sync(server_ioc, saddr, &local_err) < 0) {
789 object_unref(OBJECT(server_ioc));
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100790 error_report_err(local_err);
bellard7a5ca862008-05-27 21:13:40 +0000791 return 1;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200792 }
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100793
794 if (device) {
795 int ret;
796
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100797 ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100798 if (ret != 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100799 error_report("Failed to create client thread: %s", strerror(ret));
800 exit(EXIT_FAILURE);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100801 }
802 } else {
803 /* Shut up GCC warnings. */
804 memset(&client_thread, 0, sizeof(client_thread));
805 }
806
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000807 nbd_update_server_watch();
bellard7a5ca862008-05-27 21:13:40 +0000808
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400809 /* now when the initialization is (almost) complete, chdir("/")
810 * to free any busy filesystems */
811 if (chdir("/") < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100812 error_report("Could not chdir to root directory: %s",
813 strerror(errno));
814 exit(EXIT_FAILURE);
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400815 }
816
Paolo Bonzini7860a382012-09-18 13:31:56 +0200817 state = RUNNING;
ths3b05a8e2008-07-03 12:45:02 +0000818 do {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200819 main_loop_wait(false);
Paolo Bonzini7860a382012-09-18 13:31:56 +0200820 if (state == TERMINATE) {
821 state = TERMINATING;
822 nbd_export_close(exp);
823 nbd_export_put(exp);
824 exp = NULL;
825 }
826 } while (state != TERMINATED);
ths3b05a8e2008-07-03 12:45:02 +0000827
Markus Armbruster26f54e92014-10-07 13:59:04 +0200828 blk_unref(blk);
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100829 if (sockpath) {
830 unlink(sockpath);
831 }
bellard7a5ca862008-05-27 21:13:40 +0000832
Markus Armbrusterfbf28a42014-09-29 16:07:55 +0200833 qemu_opts_del(sn_opts);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800834
Paolo Bonzinia517e882011-11-04 15:51:21 +0100835 if (device) {
836 void *ret;
837 pthread_join(client_thread, &ret);
838 exit(ret != NULL);
839 } else {
840 exit(EXIT_SUCCESS);
841 }
bellard7a5ca862008-05-27 21:13:40 +0000842}