blob: 1c32290a8ee9c6f4366dbbd7beb7948b9c6af0f2 [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"
Paolo Bonzini38ceff02012-03-12 16:17:27 +010020#include "block.h"
bellard7a5ca862008-05-27 21:13:40 +000021#include "nbd.h"
22
bellard7a5ca862008-05-27 21:13:40 +000023#include <stdarg.h>
24#include <stdio.h>
25#include <getopt.h>
26#include <err.h>
thscd831bd2008-07-03 10:23:51 +000027#include <sys/types.h>
bellard7a5ca862008-05-27 21:13:40 +000028#include <sys/socket.h>
29#include <netinet/in.h>
30#include <netinet/tcp.h>
31#include <arpa/inet.h>
thscd831bd2008-07-03 10:23:51 +000032#include <signal.h>
Blue Swirl2bff4b62009-12-23 15:34:04 +000033#include <libgen.h>
Paolo Bonzinia517e882011-11-04 15:51:21 +010034#include <pthread.h>
thscd831bd2008-07-03 10:23:51 +000035
36#define SOCKET_PATH "/var/lock/qemu-nbd-%s"
bellard7a5ca862008-05-27 21:13:40 +000037
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +020038static NBDExport *exp;
blueswir1b1d8e522008-10-26 13:43:07 +000039static int verbose;
Paolo Bonzinia517e882011-11-04 15:51:21 +010040static char *srcpath;
41static char *sockpath;
Paolo Bonzinia61c6782011-09-12 17:28:11 +020042static bool sigterm_reported;
43static bool nbd_started;
44static int shared = 1;
45static int nb_fds;
bellard7a5ca862008-05-27 21:13:40 +000046
47static void usage(const char *name)
48{
Paolo Bonzinib033cd82012-07-18 14:50:52 +020049 (printf) (
bellard7a5ca862008-05-27 21:13:40 +000050"Usage: %s [OPTIONS] FILE\n"
51"QEMU Disk Network Block Device Server\n"
52"\n"
bellard7a5ca862008-05-27 21:13:40 +000053" -h, --help display this help and exit\n"
54" -V, --version output version information and exit\n"
55"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020056"Connection properties:\n"
57" -p, --port=PORT port to listen on (default `%d')\n"
58" -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
59" -k, --socket=PATH path to the unix socket\n"
60" (default '"SOCKET_PATH"')\n"
61" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
62" -t, --persistent don't exit on the last connection\n"
63" -v, --verbose display extra debugging information\n"
64"\n"
65"Exposing part of the image:\n"
66" -o, --offset=OFFSET offset into the image\n"
67" -P, --partition=NUM only expose partition NUM\n"
68"\n"
69#ifdef __linux__
70"Kernel NBD client support:\n"
71" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
72" -d, --disconnect disconnect the specified device\n"
73"\n"
74#endif
75"\n"
76"Block device options:\n"
77" -r, --read-only export read-only\n"
78" -s, --snapshot use snapshot file\n"
79" -n, --nocache disable host cache\n"
80"\n"
81"Report bugs to <qemu-devel@nongnu.org>\n"
Laurent Vivierc2e28722010-09-17 20:37:25 +020082 , name, NBD_DEFAULT_PORT, "DEVICE");
bellard7a5ca862008-05-27 21:13:40 +000083}
84
85static void version(const char *name)
86{
87 printf(
ths315bc7a2008-07-18 18:06:23 +000088"%s version 0.0.1\n"
bellard7a5ca862008-05-27 21:13:40 +000089"Written by Anthony Liguori.\n"
90"\n"
91"Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
92"This is free software; see the source for copying conditions. There is NO\n"
93"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
ths315bc7a2008-07-18 18:06:23 +000094 , name);
bellard7a5ca862008-05-27 21:13:40 +000095}
96
97struct partition_record
98{
99 uint8_t bootable;
100 uint8_t start_head;
101 uint32_t start_cylinder;
102 uint8_t start_sector;
103 uint8_t system;
104 uint8_t end_head;
105 uint8_t end_cylinder;
106 uint8_t end_sector;
107 uint32_t start_sector_abs;
108 uint32_t nb_sectors_abs;
109};
110
111static void read_partition(uint8_t *p, struct partition_record *r)
112{
113 r->bootable = p[0];
114 r->start_head = p[1];
115 r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
116 r->start_sector = p[2] & 0x3f;
117 r->system = p[4];
118 r->end_head = p[5];
119 r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
120 r->end_sector = p[6] & 0x3f;
121 r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24;
122 r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24;
123}
124
125static int find_partition(BlockDriverState *bs, int partition,
126 off_t *offset, off_t *size)
127{
128 struct partition_record mbr[4];
129 uint8_t data[512];
130 int i;
131 int ext_partnum = 4;
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900132 int ret;
bellard7a5ca862008-05-27 21:13:40 +0000133
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900134 if ((ret = bdrv_read(bs, 0, data, 1)) < 0) {
135 errno = -ret;
136 err(EXIT_FAILURE, "error while reading");
137 }
bellard7a5ca862008-05-27 21:13:40 +0000138
139 if (data[510] != 0x55 || data[511] != 0xaa) {
Paolo Bonzini185b4332012-03-05 08:56:10 +0100140 return -EINVAL;
bellard7a5ca862008-05-27 21:13:40 +0000141 }
142
143 for (i = 0; i < 4; i++) {
144 read_partition(&data[446 + 16 * i], &mbr[i]);
145
146 if (!mbr[i].nb_sectors_abs)
147 continue;
148
149 if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
150 struct partition_record ext[4];
151 uint8_t data1[512];
152 int j;
153
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900154 if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) {
155 errno = -ret;
156 err(EXIT_FAILURE, "error while reading");
157 }
bellard7a5ca862008-05-27 21:13:40 +0000158
159 for (j = 0; j < 4; j++) {
160 read_partition(&data1[446 + 16 * j], &ext[j]);
161 if (!ext[j].nb_sectors_abs)
162 continue;
163
164 if ((ext_partnum + j + 1) == partition) {
165 *offset = (uint64_t)ext[j].start_sector_abs << 9;
166 *size = (uint64_t)ext[j].nb_sectors_abs << 9;
167 return 0;
168 }
169 }
170 ext_partnum += 4;
171 } else if ((i + 1) == partition) {
172 *offset = (uint64_t)mbr[i].start_sector_abs << 9;
173 *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
174 return 0;
175 }
176 }
177
Paolo Bonzini185b4332012-03-05 08:56:10 +0100178 return -ENOENT;
bellard7a5ca862008-05-27 21:13:40 +0000179}
180
Paolo Bonzinibb345112011-11-04 15:51:19 +0100181static void termsig_handler(int signum)
182{
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200183 sigterm_reported = true;
184 qemu_notify_event();
Paolo Bonzinibb345112011-11-04 15:51:19 +0100185}
186
Paolo Bonzinia517e882011-11-04 15:51:21 +0100187static void *show_parts(void *arg)
thscd831bd2008-07-03 10:23:51 +0000188{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100189 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100190 int nbd;
thscd831bd2008-07-03 10:23:51 +0000191
Paolo Bonzinia517e882011-11-04 15:51:21 +0100192 /* linux just needs an open() to trigger
193 * the partition table update
194 * but remember to load the module with max_part != 0 :
195 * modprobe nbd max_part=63
196 */
197 nbd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100198 if (nbd >= 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100199 close(nbd);
thscd831bd2008-07-03 10:23:51 +0000200 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100201 return NULL;
202}
203
204static void *nbd_client_thread(void *arg)
205{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100206 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100207 off_t size;
208 size_t blocksize;
209 uint32_t nbdflags;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100210 int fd, sock;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100211 int ret;
212 pthread_t show_parts_thread;
213
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000214 sock = unix_socket_outgoing(sockpath);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100215 if (sock < 0) {
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000216 goto out;
217 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100218
219 ret = nbd_receive_negotiate(sock, NULL, &nbdflags,
220 &size, &blocksize);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100221 if (ret < 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100222 goto out;
223 }
224
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100225 fd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100226 if (fd < 0) {
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100227 /* Linux-only, we can use %m in printf. */
228 fprintf(stderr, "Failed to open %s: %m", device);
229 goto out;
230 }
231
Paolo Bonzinia517e882011-11-04 15:51:21 +0100232 ret = nbd_init(fd, sock, nbdflags, size, blocksize);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100233 if (ret < 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100234 goto out;
235 }
236
237 /* update partition table */
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100238 pthread_create(&show_parts_thread, NULL, show_parts, device);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100239
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100240 if (verbose) {
241 fprintf(stderr, "NBD device %s is now connected to %s\n",
242 device, srcpath);
243 } else {
244 /* Close stderr so that the qemu-nbd process exits. */
245 dup2(STDOUT_FILENO, STDERR_FILENO);
246 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100247
248 ret = nbd_client(fd);
249 if (ret) {
250 goto out;
251 }
252 close(fd);
253 kill(getpid(), SIGTERM);
254 return (void *) EXIT_SUCCESS;
255
256out:
257 kill(getpid(), SIGTERM);
258 return (void *) EXIT_FAILURE;
thscd831bd2008-07-03 10:23:51 +0000259}
260
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200261static int nbd_can_accept(void *opaque)
262{
263 return nb_fds < shared;
264}
265
Paolo Bonzini1743b512011-09-19 14:33:23 +0200266static void nbd_client_closed(NBDClient *client)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200267{
Paolo Bonzini1743b512011-09-19 14:33:23 +0200268 nb_fds--;
269 qemu_notify_event();
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200270}
271
272static void nbd_accept(void *opaque)
273{
274 int server_fd = (uintptr_t) opaque;
275 struct sockaddr_in addr;
276 socklen_t addr_len = sizeof(addr);
277
278 int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
279 nbd_started = true;
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100280 if (fd >= 0 && nbd_client_new(exp, fd, nbd_client_closed)) {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200281 nb_fds++;
282 }
283}
284
bellard7a5ca862008-05-27 21:13:40 +0000285int main(int argc, char **argv)
286{
287 BlockDriverState *bs;
288 off_t dev_offset = 0;
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200289 uint32_t nbdflags = 0;
thscd831bd2008-07-03 10:23:51 +0000290 bool disconnect = false;
bellard7a5ca862008-05-27 21:13:40 +0000291 const char *bindto = "0.0.0.0";
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100292 char *device = NULL;
Laurent Vivierc2e28722010-09-17 20:37:25 +0200293 int port = NBD_DEFAULT_PORT;
bellard7a5ca862008-05-27 21:13:40 +0000294 off_t fd_size;
aliguorid6aa6712009-01-08 19:34:35 +0000295 const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t";
bellard7a5ca862008-05-27 21:13:40 +0000296 struct option lopt[] = {
Blue Swirl660f11b2009-07-31 21:16:51 +0000297 { "help", 0, NULL, 'h' },
298 { "version", 0, NULL, 'V' },
299 { "bind", 1, NULL, 'b' },
300 { "port", 1, NULL, 'p' },
301 { "socket", 1, NULL, 'k' },
302 { "offset", 1, NULL, 'o' },
303 { "read-only", 0, NULL, 'r' },
304 { "partition", 1, NULL, 'P' },
305 { "connect", 1, NULL, 'c' },
306 { "disconnect", 0, NULL, 'd' },
307 { "snapshot", 0, NULL, 's' },
308 { "nocache", 0, NULL, 'n' },
309 { "shared", 1, NULL, 'e' },
310 { "persistent", 0, NULL, 't' },
311 { "verbose", 0, NULL, 'v' },
312 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000313 };
314 int ch;
315 int opt_ind = 0;
316 int li;
317 char *end;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200318 int flags = BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000319 int partition = -1;
thscd831bd2008-07-03 10:23:51 +0000320 int ret;
ths3b05a8e2008-07-03 12:45:02 +0000321 int fd;
ths75818252008-07-03 13:41:03 +0000322 int persistent = 0;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100323 pthread_t client_thread;
bellard7a5ca862008-05-27 21:13:40 +0000324
Paolo Bonzinia517e882011-11-04 15:51:21 +0100325 /* The client thread uses SIGTERM to interrupt the server. A signal
326 * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
327 */
Paolo Bonzinibb345112011-11-04 15:51:19 +0100328 struct sigaction sa_sigterm;
Paolo Bonzinibb345112011-11-04 15:51:19 +0100329 memset(&sa_sigterm, 0, sizeof(sa_sigterm));
330 sa_sigterm.sa_handler = termsig_handler;
331 sigaction(SIGTERM, &sa_sigterm, NULL);
332
bellard7a5ca862008-05-27 21:13:40 +0000333 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
334 switch (ch) {
335 case 's':
ths2f726482008-07-03 11:47:46 +0000336 flags |= BDRV_O_SNAPSHOT;
337 break;
338 case 'n':
Christoph Hellwiga6599792011-05-17 18:04:06 +0200339 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
bellard7a5ca862008-05-27 21:13:40 +0000340 break;
341 case 'b':
342 bindto = optarg;
343 break;
344 case 'p':
345 li = strtol(optarg, &end, 0);
346 if (*end) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900347 errx(EXIT_FAILURE, "Invalid port `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000348 }
349 if (li < 1 || li > 65535) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900350 errx(EXIT_FAILURE, "Port out of range `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000351 }
352 port = (uint16_t)li;
353 break;
354 case 'o':
355 dev_offset = strtoll (optarg, &end, 0);
356 if (*end) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900357 errx(EXIT_FAILURE, "Invalid offset `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000358 }
359 if (dev_offset < 0) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900360 errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000361 }
362 break;
363 case 'r':
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200364 nbdflags |= NBD_FLAG_READ_ONLY;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200365 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000366 break;
367 case 'P':
368 partition = strtol(optarg, &end, 0);
369 if (*end)
Ryota Ozakib6353be2010-03-20 15:23:23 +0900370 errx(EXIT_FAILURE, "Invalid partition `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000371 if (partition < 1 || partition > 8)
Ryota Ozakib6353be2010-03-20 15:23:23 +0900372 errx(EXIT_FAILURE, "Invalid partition %d", partition);
bellard7a5ca862008-05-27 21:13:40 +0000373 break;
thscd831bd2008-07-03 10:23:51 +0000374 case 'k':
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100375 sockpath = optarg;
376 if (sockpath[0] != '/')
Ryota Ozakib6353be2010-03-20 15:23:23 +0900377 errx(EXIT_FAILURE, "socket path must be absolute\n");
thscd831bd2008-07-03 10:23:51 +0000378 break;
379 case 'd':
380 disconnect = true;
381 break;
382 case 'c':
383 device = optarg;
384 break;
ths3b05a8e2008-07-03 12:45:02 +0000385 case 'e':
386 shared = strtol(optarg, &end, 0);
387 if (*end) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900388 errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg);
ths3b05a8e2008-07-03 12:45:02 +0000389 }
390 if (shared < 1) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900391 errx(EXIT_FAILURE, "Shared device number must be greater than 0\n");
ths3b05a8e2008-07-03 12:45:02 +0000392 }
393 break;
ths75818252008-07-03 13:41:03 +0000394 case 't':
395 persistent = 1;
396 break;
bellard7a5ca862008-05-27 21:13:40 +0000397 case 'v':
398 verbose = 1;
399 break;
400 case 'V':
401 version(argv[0]);
402 exit(0);
403 break;
404 case 'h':
405 usage(argv[0]);
406 exit(0);
407 break;
408 case '?':
Ryota Ozakib6353be2010-03-20 15:23:23 +0900409 errx(EXIT_FAILURE, "Try `%s --help' for more information.",
bellard7a5ca862008-05-27 21:13:40 +0000410 argv[0]);
411 }
412 }
413
414 if ((argc - optind) != 1) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900415 errx(EXIT_FAILURE, "Invalid number of argument.\n"
bellard7a5ca862008-05-27 21:13:40 +0000416 "Try `%s --help' for more information.",
417 argv[0]);
418 }
419
thscd831bd2008-07-03 10:23:51 +0000420 if (disconnect) {
421 fd = open(argv[optind], O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100422 if (fd < 0) {
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900423 err(EXIT_FAILURE, "Cannot open %s", argv[optind]);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100424 }
thscd831bd2008-07-03 10:23:51 +0000425 nbd_disconnect(fd);
426
427 close(fd);
428
429 printf("%s disconnected\n", argv[optind]);
430
431 return 0;
432 }
433
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100434 if (device && !verbose) {
435 int stderr_fd[2];
436 pid_t pid;
437 int ret;
438
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100439 if (qemu_pipe(stderr_fd) < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100440 err(EXIT_FAILURE, "Error setting up communication pipe");
441 }
442
443 /* Now daemonize, but keep a communication channel open to
444 * print errors and exit with the proper status code.
445 */
446 pid = fork();
447 if (pid == 0) {
448 close(stderr_fd[0]);
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400449 ret = qemu_daemon(1, 0);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100450
451 /* Temporarily redirect stderr to the parent's pipe... */
452 dup2(stderr_fd[1], STDERR_FILENO);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100453 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100454 err(EXIT_FAILURE, "Failed to daemonize");
455 }
456
457 /* ... close the descriptor we inherited and go on. */
458 close(stderr_fd[1]);
459 } else {
460 bool errors = false;
461 char *buf;
462
463 /* In the parent. Print error messages from the child until
464 * it closes the pipe.
465 */
466 close(stderr_fd[1]);
467 buf = g_malloc(1024);
468 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
469 errors = true;
470 ret = qemu_write_full(STDERR_FILENO, buf, ret);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100471 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100472 exit(EXIT_FAILURE);
473 }
474 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100475 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100476 err(EXIT_FAILURE, "Cannot read from daemon");
477 }
478
479 /* Usually the daemon should not print any message.
480 * Exit with zero status in that case.
481 */
482 exit(errors);
483 }
484 }
485
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100486 if (device != NULL && sockpath == NULL) {
487 sockpath = g_malloc(128);
488 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
thscd831bd2008-07-03 10:23:51 +0000489 }
490
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100491 bdrv_init();
492 atexit(bdrv_close_all);
493
494 bs = bdrv_new("hda");
495 srcpath = argv[optind];
496 if ((ret = bdrv_open(bs, srcpath, flags, NULL)) < 0) {
497 errno = -ret;
498 err(EXIT_FAILURE, "Failed to bdrv_open '%s'", argv[optind]);
499 }
500
Paolo Bonzini38ceff02012-03-12 16:17:27 +0100501 fd_size = bdrv_getlength(bs);
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100502
Paolo Bonzini185b4332012-03-05 08:56:10 +0100503 if (partition != -1) {
504 ret = find_partition(bs, partition, &dev_offset, &fd_size);
505 if (ret < 0) {
506 errno = -ret;
507 err(EXIT_FAILURE, "Could not find partition %d", partition);
508 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100509 }
510
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200511 exp = nbd_export_new(bs, dev_offset, fd_size, nbdflags);
ths3b05a8e2008-07-03 12:45:02 +0000512
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100513 if (sockpath) {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200514 fd = unix_socket_incoming(sockpath);
thscd831bd2008-07-03 10:23:51 +0000515 } else {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200516 fd = tcp_socket_incoming(bindto, port);
thscd831bd2008-07-03 10:23:51 +0000517 }
518
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100519 if (fd < 0) {
bellard7a5ca862008-05-27 21:13:40 +0000520 return 1;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200521 }
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100522
523 if (device) {
524 int ret;
525
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100526 ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100527 if (ret != 0) {
528 errx(EXIT_FAILURE, "Failed to create client thread: %s",
529 strerror(ret));
530 }
531 } else {
532 /* Shut up GCC warnings. */
533 memset(&client_thread, 0, sizeof(client_thread));
534 }
535
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200536 qemu_init_main_loop();
537 qemu_set_fd_handler2(fd, nbd_can_accept, nbd_accept, NULL,
538 (void *)(uintptr_t)fd);
bellard7a5ca862008-05-27 21:13:40 +0000539
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400540 /* now when the initialization is (almost) complete, chdir("/")
541 * to free any busy filesystems */
542 if (chdir("/") < 0) {
543 err(EXIT_FAILURE, "Could not chdir to root directory");
544 }
545
ths3b05a8e2008-07-03 12:45:02 +0000546 do {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200547 main_loop_wait(false);
548 } while (!sigterm_reported && (persistent || !nbd_started || nb_fds > 0));
ths3b05a8e2008-07-03 12:45:02 +0000549
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +0200550 nbd_export_close(exp);
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100551 if (sockpath) {
552 unlink(sockpath);
553 }
bellard7a5ca862008-05-27 21:13:40 +0000554
Paolo Bonzinia517e882011-11-04 15:51:21 +0100555 if (device) {
556 void *ret;
557 pthread_join(client_thread, &ret);
558 exit(ret != NULL);
559 } else {
560 exit(EXIT_SUCCESS);
561 }
bellard7a5ca862008-05-27 21:13:40 +0000562}