]> Git Repo - qemu.git/blame - qemu-nbd.c
qemu-iotests: add test for snapshot in qemu-img convert
[qemu.git] / qemu-nbd.c
CommitLineData
cd831bd7 1/*
7a5ca864
FB
2 * Copyright (C) 2005 Anthony Liguori <[email protected]>
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
8167ee88 16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
7a5ca864
FB
17 */
18
5a61cb60 19#include "qemu-common.h"
737e150e
PB
20#include "block/block.h"
21#include "block/nbd.h"
6a1751b7 22#include "qemu/main-loop.h"
8c116b0e 23#include "block/snapshot.h"
7a5ca864 24
7a5ca864
FB
25#include <stdarg.h>
26#include <stdio.h>
27#include <getopt.h>
28#include <err.h>
cd831bd7 29#include <sys/types.h>
7a5ca864
FB
30#include <sys/socket.h>
31#include <netinet/in.h>
32#include <netinet/tcp.h>
33#include <arpa/inet.h>
cd831bd7 34#include <signal.h>
2bff4b6f 35#include <libgen.h>
a517e88b 36#include <pthread.h>
cd831bd7 37
ded9d2d5
PB
38#define SOCKET_PATH "/var/lock/qemu-nbd-%s"
39#define QEMU_NBD_OPT_CACHE 1
40#define QEMU_NBD_OPT_AIO 2
41#define QEMU_NBD_OPT_DISCARD 3
7a5ca864 42
af49bbbe 43static NBDExport *exp;
b1d8e52e 44static int verbose;
a517e88b
PB
45static char *srcpath;
46static char *sockpath;
7860a380
PB
47static int persistent = 0;
48static enum { RUNNING, TERMINATE, TERMINATING, TERMINATED } state;
a61c6782
PB
49static int shared = 1;
50static int nb_fds;
7a5ca864
FB
51
52static void usage(const char *name)
53{
b033cd86 54 (printf) (
7a5ca864
FB
55"Usage: %s [OPTIONS] FILE\n"
56"QEMU Disk Network Block Device Server\n"
57"\n"
b033cd86
PB
58" -h, --help display this help and exit\n"
59" -V, --version output version information and exit\n"
60"\n"
61"Connection properties:\n"
c2e2872b 62" -p, --port=PORT port to listen on (default `%d')\n"
7a5ca864 63" -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
cd831bd7
TS
64" -k, --socket=PATH path to the unix socket\n"
65" (default '"SOCKET_PATH"')\n"
3b05a8e9 66" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
75818250 67" -t, --persistent don't exit on the last connection\n"
7a5ca864 68" -v, --verbose display extra debugging information\n"
7a5ca864 69"\n"
b033cd86
PB
70"Exposing part of the image:\n"
71" -o, --offset=OFFSET offset into the image\n"
72" -P, --partition=NUM only expose partition NUM\n"
73"\n"
74#ifdef __linux__
75"Kernel NBD client support:\n"
76" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
77" -d, --disconnect disconnect the specified device\n"
78"\n"
79#endif
80"\n"
81"Block device options:\n"
82" -r, --read-only export read-only\n"
8c116b0e
WX
83" -s, --snapshot use FILE as an external snapshot, create a temporary\n"
84" file with backing_file=FILE, redirect the write to\n"
85" the temporary one\n"
86" -l, --load-snapshot=SNAPSHOT_PARAM\n"
87" load an internal snapshot inside FILE and export it\n"
88" as an read-only device, SNAPSHOT_PARAM format is\n"
89" 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
90" '[ID_OR_NAME]'\n"
b033cd86 91" -n, --nocache disable host cache\n"
39a5235c
PB
92" --cache=MODE set cache mode (none, writeback, ...)\n"
93#ifdef CONFIG_LINUX_AIO
94" --aio=MODE set AIO mode (native or threads)\n"
95#endif
b033cd86
PB
96"\n"
97"Report bugs to <[email protected]>\n"
c2e2872b 98 , name, NBD_DEFAULT_PORT, "DEVICE");
7a5ca864
FB
99}
100
101static void version(const char *name)
102{
103 printf(
315bc7aa 104"%s version 0.0.1\n"
7a5ca864
FB
105"Written by Anthony Liguori.\n"
106"\n"
107"Copyright (C) 2006 Anthony Liguori <[email protected]>.\n"
108"This is free software; see the source for copying conditions. There is NO\n"
109"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
315bc7aa 110 , name);
7a5ca864
FB
111}
112
113struct partition_record
114{
115 uint8_t bootable;
116 uint8_t start_head;
117 uint32_t start_cylinder;
118 uint8_t start_sector;
119 uint8_t system;
120 uint8_t end_head;
121 uint8_t end_cylinder;
122 uint8_t end_sector;
123 uint32_t start_sector_abs;
124 uint32_t nb_sectors_abs;
125};
126
127static void read_partition(uint8_t *p, struct partition_record *r)
128{
129 r->bootable = p[0];
130 r->start_head = p[1];
131 r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
132 r->start_sector = p[2] & 0x3f;
133 r->system = p[4];
134 r->end_head = p[5];
135 r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
136 r->end_sector = p[6] & 0x3f;
137 r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24;
138 r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24;
139}
140
141static int find_partition(BlockDriverState *bs, int partition,
142 off_t *offset, off_t *size)
143{
144 struct partition_record mbr[4];
145 uint8_t data[512];
146 int i;
147 int ext_partnum = 4;
cb7cf0e3 148 int ret;
7a5ca864 149
cb7cf0e3
RO
150 if ((ret = bdrv_read(bs, 0, data, 1)) < 0) {
151 errno = -ret;
152 err(EXIT_FAILURE, "error while reading");
153 }
7a5ca864
FB
154
155 if (data[510] != 0x55 || data[511] != 0xaa) {
185b4338 156 return -EINVAL;
7a5ca864
FB
157 }
158
159 for (i = 0; i < 4; i++) {
160 read_partition(&data[446 + 16 * i], &mbr[i]);
161
162 if (!mbr[i].nb_sectors_abs)
163 continue;
164
165 if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
166 struct partition_record ext[4];
167 uint8_t data1[512];
168 int j;
169
cb7cf0e3
RO
170 if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) {
171 errno = -ret;
172 err(EXIT_FAILURE, "error while reading");
173 }
7a5ca864
FB
174
175 for (j = 0; j < 4; j++) {
176 read_partition(&data1[446 + 16 * j], &ext[j]);
177 if (!ext[j].nb_sectors_abs)
178 continue;
179
180 if ((ext_partnum + j + 1) == partition) {
181 *offset = (uint64_t)ext[j].start_sector_abs << 9;
182 *size = (uint64_t)ext[j].nb_sectors_abs << 9;
183 return 0;
184 }
185 }
186 ext_partnum += 4;
187 } else if ((i + 1) == partition) {
188 *offset = (uint64_t)mbr[i].start_sector_abs << 9;
189 *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
190 return 0;
191 }
192 }
193
185b4338 194 return -ENOENT;
7a5ca864
FB
195}
196
bb345110
PB
197static void termsig_handler(int signum)
198{
7860a380 199 state = TERMINATE;
a61c6782 200 qemu_notify_event();
bb345110
PB
201}
202
a517e88b 203static void *show_parts(void *arg)
cd831bd7 204{
a6ac2313 205 char *device = arg;
a517e88b
PB
206 int nbd;
207
208 /* linux just needs an open() to trigger
209 * the partition table update
210 * but remember to load the module with max_part != 0 :
211 * modprobe nbd max_part=63
212 */
213 nbd = open(device, O_RDWR);
fc19f8a0 214 if (nbd >= 0) {
a517e88b
PB
215 close(nbd);
216 }
217 return NULL;
218}
cd831bd7 219
a517e88b
PB
220static void *nbd_client_thread(void *arg)
221{
a6ac2313 222 char *device = arg;
a517e88b
PB
223 off_t size;
224 size_t blocksize;
225 uint32_t nbdflags;
a6ac2313 226 int fd, sock;
a517e88b
PB
227 int ret;
228 pthread_t show_parts_thread;
229
dc10e8b3 230 sock = unix_socket_outgoing(sockpath);
fc19f8a0 231 if (sock < 0) {
dc10e8b3
SH
232 goto out;
233 }
a517e88b
PB
234
235 ret = nbd_receive_negotiate(sock, NULL, &nbdflags,
236 &size, &blocksize);
fc19f8a0 237 if (ret < 0) {
a517e88b
PB
238 goto out;
239 }
240
a6ac2313 241 fd = open(device, O_RDWR);
fc19f8a0 242 if (fd < 0) {
a6ac2313
PB
243 /* Linux-only, we can use %m in printf. */
244 fprintf(stderr, "Failed to open %s: %m", device);
245 goto out;
246 }
247
a517e88b 248 ret = nbd_init(fd, sock, nbdflags, size, blocksize);
fc19f8a0 249 if (ret < 0) {
a517e88b
PB
250 goto out;
251 }
252
253 /* update partition table */
a6ac2313 254 pthread_create(&show_parts_thread, NULL, show_parts, device);
a517e88b 255
c1f8fdc3
PB
256 if (verbose) {
257 fprintf(stderr, "NBD device %s is now connected to %s\n",
258 device, srcpath);
259 } else {
260 /* Close stderr so that the qemu-nbd process exits. */
261 dup2(STDOUT_FILENO, STDERR_FILENO);
262 }
a517e88b
PB
263
264 ret = nbd_client(fd);
265 if (ret) {
266 goto out;
cd831bd7 267 }
a517e88b
PB
268 close(fd);
269 kill(getpid(), SIGTERM);
270 return (void *) EXIT_SUCCESS;
271
272out:
273 kill(getpid(), SIGTERM);
274 return (void *) EXIT_FAILURE;
cd831bd7
TS
275}
276
a61c6782
PB
277static int nbd_can_accept(void *opaque)
278{
279 return nb_fds < shared;
280}
281
7860a380
PB
282static void nbd_export_closed(NBDExport *exp)
283{
284 assert(state == TERMINATING);
285 state = TERMINATED;
286}
287
1743b515 288static void nbd_client_closed(NBDClient *client)
a61c6782 289{
1743b515 290 nb_fds--;
7860a380
PB
291 if (nb_fds == 0 && !persistent && state == RUNNING) {
292 state = TERMINATE;
293 }
1743b515 294 qemu_notify_event();
7860a380 295 nbd_client_put(client);
a61c6782
PB
296}
297
298static void nbd_accept(void *opaque)
299{
300 int server_fd = (uintptr_t) opaque;
301 struct sockaddr_in addr;
302 socklen_t addr_len = sizeof(addr);
303
304 int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
7860a380
PB
305 if (state >= TERMINATE) {
306 close(fd);
307 return;
308 }
309
fc19f8a0 310 if (fd >= 0 && nbd_client_new(exp, fd, nbd_client_closed)) {
a61c6782
PB
311 nb_fds++;
312 }
313}
314
7a5ca864
FB
315int main(int argc, char **argv)
316{
317 BlockDriverState *bs;
e6b63677 318 BlockDriver *drv;
7a5ca864 319 off_t dev_offset = 0;
b90fb4b8 320 uint32_t nbdflags = 0;
cd831bd7 321 bool disconnect = false;
7a5ca864 322 const char *bindto = "0.0.0.0";
a6ac2313 323 char *device = NULL;
c2e2872b 324 int port = NBD_DEFAULT_PORT;
7a5ca864 325 off_t fd_size;
8c116b0e
WX
326 QemuOpts *sn_opts = NULL;
327 const char *sn_id_or_name = NULL;
328 const char *sopt = "hVb:o:p:rsnP:c:dvk:e:f:tl:";
7a5ca864 329 struct option lopt[] = {
660f11be
BS
330 { "help", 0, NULL, 'h' },
331 { "version", 0, NULL, 'V' },
332 { "bind", 1, NULL, 'b' },
333 { "port", 1, NULL, 'p' },
334 { "socket", 1, NULL, 'k' },
335 { "offset", 1, NULL, 'o' },
336 { "read-only", 0, NULL, 'r' },
337 { "partition", 1, NULL, 'P' },
338 { "connect", 1, NULL, 'c' },
339 { "disconnect", 0, NULL, 'd' },
340 { "snapshot", 0, NULL, 's' },
8c116b0e 341 { "load-snapshot", 1, NULL, 'l' },
660f11be 342 { "nocache", 0, NULL, 'n' },
39a5235c
PB
343 { "cache", 1, NULL, QEMU_NBD_OPT_CACHE },
344#ifdef CONFIG_LINUX_AIO
345 { "aio", 1, NULL, QEMU_NBD_OPT_AIO },
346#endif
ded9d2d5 347 { "discard", 1, NULL, QEMU_NBD_OPT_DISCARD },
660f11be 348 { "shared", 1, NULL, 'e' },
e6b63677 349 { "format", 1, NULL, 'f' },
660f11be
BS
350 { "persistent", 0, NULL, 't' },
351 { "verbose", 0, NULL, 'v' },
352 { NULL, 0, NULL, 0 }
7a5ca864
FB
353 };
354 int ch;
355 int opt_ind = 0;
356 int li;
357 char *end;
f5edb014 358 int flags = BDRV_O_RDWR;
7a5ca864 359 int partition = -1;
cd831bd7 360 int ret;
3b05a8e9 361 int fd;
39a5235c 362 bool seen_cache = false;
ded9d2d5 363 bool seen_discard = false;
39a5235c
PB
364#ifdef CONFIG_LINUX_AIO
365 bool seen_aio = false;
366#endif
a517e88b 367 pthread_t client_thread;
e6b63677 368 const char *fmt = NULL;
34b5d2c6 369 Error *local_err = NULL;
7a5ca864 370
a517e88b
PB
371 /* The client thread uses SIGTERM to interrupt the server. A signal
372 * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
373 */
bb345110 374 struct sigaction sa_sigterm;
bb345110
PB
375 memset(&sa_sigterm, 0, sizeof(sa_sigterm));
376 sa_sigterm.sa_handler = termsig_handler;
377 sigaction(SIGTERM, &sa_sigterm, NULL);
378
7a5ca864
FB
379 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
380 switch (ch) {
381 case 's':
2f726488
TS
382 flags |= BDRV_O_SNAPSHOT;
383 break;
384 case 'n':
39a5235c
PB
385 optarg = (char *) "none";
386 /* fallthrough */
387 case QEMU_NBD_OPT_CACHE:
388 if (seen_cache) {
389 errx(EXIT_FAILURE, "-n and --cache can only be specified once");
390 }
391 seen_cache = true;
392 if (bdrv_parse_cache_flags(optarg, &flags) == -1) {
393 errx(EXIT_FAILURE, "Invalid cache mode `%s'", optarg);
394 }
7a5ca864 395 break;
39a5235c
PB
396#ifdef CONFIG_LINUX_AIO
397 case QEMU_NBD_OPT_AIO:
398 if (seen_aio) {
399 errx(EXIT_FAILURE, "--aio can only be specified once");
400 }
401 seen_aio = true;
402 if (!strcmp(optarg, "native")) {
403 flags |= BDRV_O_NATIVE_AIO;
404 } else if (!strcmp(optarg, "threads")) {
405 /* this is the default */
406 } else {
407 errx(EXIT_FAILURE, "invalid aio mode `%s'", optarg);
408 }
409 break;
410#endif
ded9d2d5
PB
411 case QEMU_NBD_OPT_DISCARD:
412 if (seen_discard) {
413 errx(EXIT_FAILURE, "--discard can only be specified once");
414 }
415 seen_discard = true;
416 if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
417 errx(EXIT_FAILURE, "Invalid discard mode `%s'", optarg);
418 }
419 break;
7a5ca864
FB
420 case 'b':
421 bindto = optarg;
422 break;
423 case 'p':
424 li = strtol(optarg, &end, 0);
425 if (*end) {
b6353bea 426 errx(EXIT_FAILURE, "Invalid port `%s'", optarg);
7a5ca864
FB
427 }
428 if (li < 1 || li > 65535) {
b6353bea 429 errx(EXIT_FAILURE, "Port out of range `%s'", optarg);
7a5ca864
FB
430 }
431 port = (uint16_t)li;
432 break;
433 case 'o':
434 dev_offset = strtoll (optarg, &end, 0);
435 if (*end) {
b6353bea 436 errx(EXIT_FAILURE, "Invalid offset `%s'", optarg);
7a5ca864
FB
437 }
438 if (dev_offset < 0) {
b6353bea 439 errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg);
7a5ca864
FB
440 }
441 break;
8c116b0e
WX
442 case 'l':
443 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
444 sn_opts = qemu_opts_parse(&internal_snapshot_opts, optarg, 0);
445 if (!sn_opts) {
446 errx(EXIT_FAILURE, "Failed in parsing snapshot param `%s'",
447 optarg);
448 }
449 } else {
450 sn_id_or_name = optarg;
451 }
452 /* fall through */
7a5ca864 453 case 'r':
b90fb4b8 454 nbdflags |= NBD_FLAG_READ_ONLY;
07108b29 455 flags &= ~BDRV_O_RDWR;
7a5ca864
FB
456 break;
457 case 'P':
458 partition = strtol(optarg, &end, 0);
459 if (*end)
b6353bea 460 errx(EXIT_FAILURE, "Invalid partition `%s'", optarg);
7a5ca864 461 if (partition < 1 || partition > 8)
b6353bea 462 errx(EXIT_FAILURE, "Invalid partition %d", partition);
7a5ca864 463 break;
cd831bd7 464 case 'k':
b32f6c28
PB
465 sockpath = optarg;
466 if (sockpath[0] != '/')
b6353bea 467 errx(EXIT_FAILURE, "socket path must be absolute\n");
cd831bd7
TS
468 break;
469 case 'd':
470 disconnect = true;
471 break;
472 case 'c':
473 device = optarg;
474 break;
3b05a8e9
TS
475 case 'e':
476 shared = strtol(optarg, &end, 0);
477 if (*end) {
b6353bea 478 errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg);
3b05a8e9
TS
479 }
480 if (shared < 1) {
b6353bea 481 errx(EXIT_FAILURE, "Shared device number must be greater than 0\n");
3b05a8e9
TS
482 }
483 break;
e6b63677
DB
484 case 'f':
485 fmt = optarg;
486 break;
75818250
TS
487 case 't':
488 persistent = 1;
489 break;
7a5ca864
FB
490 case 'v':
491 verbose = 1;
492 break;
493 case 'V':
494 version(argv[0]);
495 exit(0);
496 break;
497 case 'h':
498 usage(argv[0]);
499 exit(0);
500 break;
501 case '?':
b6353bea 502 errx(EXIT_FAILURE, "Try `%s --help' for more information.",
7a5ca864
FB
503 argv[0]);
504 }
505 }
506
507 if ((argc - optind) != 1) {
b6353bea 508 errx(EXIT_FAILURE, "Invalid number of argument.\n"
7a5ca864
FB
509 "Try `%s --help' for more information.",
510 argv[0]);
511 }
512
cd831bd7
TS
513 if (disconnect) {
514 fd = open(argv[optind], O_RDWR);
fc19f8a0 515 if (fd < 0) {
cb7cf0e3 516 err(EXIT_FAILURE, "Cannot open %s", argv[optind]);
fc19f8a0 517 }
cd831bd7
TS
518 nbd_disconnect(fd);
519
520 close(fd);
521
522 printf("%s disconnected\n", argv[optind]);
523
524 return 0;
525 }
526
c1f8fdc3
PB
527 if (device && !verbose) {
528 int stderr_fd[2];
529 pid_t pid;
530 int ret;
531
fc19f8a0 532 if (qemu_pipe(stderr_fd) < 0) {
c1f8fdc3
PB
533 err(EXIT_FAILURE, "Error setting up communication pipe");
534 }
535
536 /* Now daemonize, but keep a communication channel open to
537 * print errors and exit with the proper status code.
538 */
539 pid = fork();
540 if (pid == 0) {
541 close(stderr_fd[0]);
9faf31b6 542 ret = qemu_daemon(1, 0);
c1f8fdc3
PB
543
544 /* Temporarily redirect stderr to the parent's pipe... */
545 dup2(stderr_fd[1], STDERR_FILENO);
fc19f8a0 546 if (ret < 0) {
c1f8fdc3
PB
547 err(EXIT_FAILURE, "Failed to daemonize");
548 }
549
550 /* ... close the descriptor we inherited and go on. */
551 close(stderr_fd[1]);
552 } else {
553 bool errors = false;
554 char *buf;
555
556 /* In the parent. Print error messages from the child until
557 * it closes the pipe.
558 */
559 close(stderr_fd[1]);
560 buf = g_malloc(1024);
561 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
562 errors = true;
563 ret = qemu_write_full(STDERR_FILENO, buf, ret);
fc19f8a0 564 if (ret < 0) {
c1f8fdc3
PB
565 exit(EXIT_FAILURE);
566 }
567 }
fc19f8a0 568 if (ret < 0) {
c1f8fdc3
PB
569 err(EXIT_FAILURE, "Cannot read from daemon");
570 }
571
572 /* Usually the daemon should not print any message.
573 * Exit with zero status in that case.
574 */
575 exit(errors);
576 }
577 }
578
a6ac2313
PB
579 if (device != NULL && sockpath == NULL) {
580 sockpath = g_malloc(128);
581 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
cd831bd7
TS
582 }
583
7e7f4a0e 584 qemu_init_main_loop();
802ddc37
PB
585 bdrv_init();
586 atexit(bdrv_close_all);
587
e6b63677
DB
588 if (fmt) {
589 drv = bdrv_find_format(fmt);
590 if (!drv) {
591 errx(EXIT_FAILURE, "Unknown file format '%s'", fmt);
592 }
593 } else {
594 drv = NULL;
595 }
596
802ddc37
PB
597 bs = bdrv_new("hda");
598 srcpath = argv[optind];
34b5d2c6 599 ret = bdrv_open(bs, srcpath, NULL, flags, drv, &local_err);
e6b63677 600 if (ret < 0) {
802ddc37 601 errno = -ret;
34b5d2c6
HR
602 err(EXIT_FAILURE, "Failed to bdrv_open '%s': %s", argv[optind],
603 error_get_pretty(local_err));
802ddc37
PB
604 }
605
8c116b0e
WX
606 if (sn_opts) {
607 ret = bdrv_snapshot_load_tmp(bs,
608 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
609 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
610 &local_err);
611 } else if (sn_id_or_name) {
612 ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
613 &local_err);
614 }
615 if (ret < 0) {
616 errno = -ret;
617 err(EXIT_FAILURE,
618 "Failed to load snapshot: %s",
619 error_get_pretty(local_err));
620 }
621
38ceff04 622 fd_size = bdrv_getlength(bs);
802ddc37 623
185b4338
PB
624 if (partition != -1) {
625 ret = find_partition(bs, partition, &dev_offset, &fd_size);
626 if (ret < 0) {
627 errno = -ret;
628 err(EXIT_FAILURE, "Could not find partition %d", partition);
629 }
802ddc37
PB
630 }
631
7860a380 632 exp = nbd_export_new(bs, dev_offset, fd_size, nbdflags, nbd_export_closed);
3b05a8e9 633
b32f6c28 634 if (sockpath) {
a61c6782 635 fd = unix_socket_incoming(sockpath);
cd831bd7 636 } else {
a61c6782 637 fd = tcp_socket_incoming(bindto, port);
cd831bd7
TS
638 }
639
fc19f8a0 640 if (fd < 0) {
7a5ca864 641 return 1;
a61c6782 642 }
f1ef5555
PB
643
644 if (device) {
645 int ret;
646
a6ac2313 647 ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
f1ef5555
PB
648 if (ret != 0) {
649 errx(EXIT_FAILURE, "Failed to create client thread: %s",
650 strerror(ret));
651 }
652 } else {
653 /* Shut up GCC warnings. */
654 memset(&client_thread, 0, sizeof(client_thread));
655 }
656
a61c6782
PB
657 qemu_set_fd_handler2(fd, nbd_can_accept, nbd_accept, NULL,
658 (void *)(uintptr_t)fd);
7a5ca864 659
9faf31b6
MT
660 /* now when the initialization is (almost) complete, chdir("/")
661 * to free any busy filesystems */
662 if (chdir("/") < 0) {
663 err(EXIT_FAILURE, "Could not chdir to root directory");
664 }
665
7860a380 666 state = RUNNING;
3b05a8e9 667 do {
a61c6782 668 main_loop_wait(false);
7860a380
PB
669 if (state == TERMINATE) {
670 state = TERMINATING;
671 nbd_export_close(exp);
672 nbd_export_put(exp);
673 exp = NULL;
674 }
675 } while (state != TERMINATED);
7a5ca864 676
a4aab7b4 677 bdrv_close(bs);
b32f6c28
PB
678 if (sockpath) {
679 unlink(sockpath);
680 }
7a5ca864 681
8c116b0e
WX
682 if (sn_opts) {
683 qemu_opts_del(sn_opts);
684 }
685
a517e88b
PB
686 if (device) {
687 void *ret;
688 pthread_join(client_thread, &ret);
689 exit(ret != NULL);
690 } else {
691 exit(EXIT_SUCCESS);
692 }
7a5ca864 693}
This page took 0.670304 seconds and 4 git commands to generate.