blob: 0a6091b6a8b6bd089291da440ac44c49ef7c7de2 [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 Bonzini737e1502012-12-17 18:19:44 +010020#include "block/block.h"
21#include "block/nbd.h"
bellard7a5ca862008-05-27 21:13:40 +000022
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
Paolo Bonzini39a52352012-07-18 14:57:15 +020036#define SOCKET_PATH "/var/lock/qemu-nbd-%s"
37#define QEMU_NBD_OPT_CACHE 1
38#define QEMU_NBD_OPT_AIO 2
bellard7a5ca862008-05-27 21:13:40 +000039
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +020040static NBDExport *exp;
blueswir1b1d8e522008-10-26 13:43:07 +000041static int verbose;
Paolo Bonzinia517e882011-11-04 15:51:21 +010042static char *srcpath;
43static char *sockpath;
Paolo Bonzini7860a382012-09-18 13:31:56 +020044static int persistent = 0;
45static enum { RUNNING, TERMINATE, TERMINATING, TERMINATED } state;
Paolo Bonzinia61c6782011-09-12 17:28:11 +020046static int shared = 1;
47static int nb_fds;
bellard7a5ca862008-05-27 21:13:40 +000048
49static void usage(const char *name)
50{
Paolo Bonzinib033cd82012-07-18 14:50:52 +020051 (printf) (
bellard7a5ca862008-05-27 21:13:40 +000052"Usage: %s [OPTIONS] FILE\n"
53"QEMU Disk Network Block Device Server\n"
54"\n"
bellard7a5ca862008-05-27 21:13:40 +000055" -h, --help display this help and exit\n"
56" -V, --version output version information and exit\n"
57"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020058"Connection properties:\n"
59" -p, --port=PORT port to listen on (default `%d')\n"
60" -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
61" -k, --socket=PATH path to the unix socket\n"
62" (default '"SOCKET_PATH"')\n"
63" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
64" -t, --persistent don't exit on the last connection\n"
65" -v, --verbose display extra debugging information\n"
66"\n"
67"Exposing part of the image:\n"
68" -o, --offset=OFFSET offset into the image\n"
69" -P, --partition=NUM only expose partition NUM\n"
70"\n"
71#ifdef __linux__
72"Kernel NBD client support:\n"
73" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
74" -d, --disconnect disconnect the specified device\n"
75"\n"
76#endif
77"\n"
78"Block device options:\n"
79" -r, --read-only export read-only\n"
80" -s, --snapshot use snapshot file\n"
81" -n, --nocache disable host cache\n"
Paolo Bonzini39a52352012-07-18 14:57:15 +020082" --cache=MODE set cache mode (none, writeback, ...)\n"
83#ifdef CONFIG_LINUX_AIO
84" --aio=MODE set AIO mode (native or threads)\n"
85#endif
Paolo Bonzinib033cd82012-07-18 14:50:52 +020086"\n"
87"Report bugs to <qemu-devel@nongnu.org>\n"
Laurent Vivierc2e28722010-09-17 20:37:25 +020088 , name, NBD_DEFAULT_PORT, "DEVICE");
bellard7a5ca862008-05-27 21:13:40 +000089}
90
91static void version(const char *name)
92{
93 printf(
ths315bc7a2008-07-18 18:06:23 +000094"%s version 0.0.1\n"
bellard7a5ca862008-05-27 21:13:40 +000095"Written by Anthony Liguori.\n"
96"\n"
97"Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
98"This is free software; see the source for copying conditions. There is NO\n"
99"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
ths315bc7a2008-07-18 18:06:23 +0000100 , name);
bellard7a5ca862008-05-27 21:13:40 +0000101}
102
103struct partition_record
104{
105 uint8_t bootable;
106 uint8_t start_head;
107 uint32_t start_cylinder;
108 uint8_t start_sector;
109 uint8_t system;
110 uint8_t end_head;
111 uint8_t end_cylinder;
112 uint8_t end_sector;
113 uint32_t start_sector_abs;
114 uint32_t nb_sectors_abs;
115};
116
117static void read_partition(uint8_t *p, struct partition_record *r)
118{
119 r->bootable = p[0];
120 r->start_head = p[1];
121 r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
122 r->start_sector = p[2] & 0x3f;
123 r->system = p[4];
124 r->end_head = p[5];
125 r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
126 r->end_sector = p[6] & 0x3f;
127 r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24;
128 r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24;
129}
130
131static int find_partition(BlockDriverState *bs, int partition,
132 off_t *offset, off_t *size)
133{
134 struct partition_record mbr[4];
135 uint8_t data[512];
136 int i;
137 int ext_partnum = 4;
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900138 int ret;
bellard7a5ca862008-05-27 21:13:40 +0000139
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900140 if ((ret = bdrv_read(bs, 0, data, 1)) < 0) {
141 errno = -ret;
142 err(EXIT_FAILURE, "error while reading");
143 }
bellard7a5ca862008-05-27 21:13:40 +0000144
145 if (data[510] != 0x55 || data[511] != 0xaa) {
Paolo Bonzini185b4332012-03-05 08:56:10 +0100146 return -EINVAL;
bellard7a5ca862008-05-27 21:13:40 +0000147 }
148
149 for (i = 0; i < 4; i++) {
150 read_partition(&data[446 + 16 * i], &mbr[i]);
151
152 if (!mbr[i].nb_sectors_abs)
153 continue;
154
155 if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
156 struct partition_record ext[4];
157 uint8_t data1[512];
158 int j;
159
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900160 if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) {
161 errno = -ret;
162 err(EXIT_FAILURE, "error while reading");
163 }
bellard7a5ca862008-05-27 21:13:40 +0000164
165 for (j = 0; j < 4; j++) {
166 read_partition(&data1[446 + 16 * j], &ext[j]);
167 if (!ext[j].nb_sectors_abs)
168 continue;
169
170 if ((ext_partnum + j + 1) == partition) {
171 *offset = (uint64_t)ext[j].start_sector_abs << 9;
172 *size = (uint64_t)ext[j].nb_sectors_abs << 9;
173 return 0;
174 }
175 }
176 ext_partnum += 4;
177 } else if ((i + 1) == partition) {
178 *offset = (uint64_t)mbr[i].start_sector_abs << 9;
179 *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
180 return 0;
181 }
182 }
183
Paolo Bonzini185b4332012-03-05 08:56:10 +0100184 return -ENOENT;
bellard7a5ca862008-05-27 21:13:40 +0000185}
186
Paolo Bonzinibb345112011-11-04 15:51:19 +0100187static void termsig_handler(int signum)
188{
Paolo Bonzini7860a382012-09-18 13:31:56 +0200189 state = TERMINATE;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200190 qemu_notify_event();
Paolo Bonzinibb345112011-11-04 15:51:19 +0100191}
192
Paolo Bonzinia517e882011-11-04 15:51:21 +0100193static void *show_parts(void *arg)
thscd831bd2008-07-03 10:23:51 +0000194{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100195 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100196 int nbd;
thscd831bd2008-07-03 10:23:51 +0000197
Paolo Bonzinia517e882011-11-04 15:51:21 +0100198 /* linux just needs an open() to trigger
199 * the partition table update
200 * but remember to load the module with max_part != 0 :
201 * modprobe nbd max_part=63
202 */
203 nbd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100204 if (nbd >= 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100205 close(nbd);
thscd831bd2008-07-03 10:23:51 +0000206 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100207 return NULL;
208}
209
210static void *nbd_client_thread(void *arg)
211{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100212 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100213 off_t size;
214 size_t blocksize;
215 uint32_t nbdflags;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100216 int fd, sock;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100217 int ret;
218 pthread_t show_parts_thread;
219
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000220 sock = unix_socket_outgoing(sockpath);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100221 if (sock < 0) {
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000222 goto out;
223 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100224
225 ret = nbd_receive_negotiate(sock, NULL, &nbdflags,
226 &size, &blocksize);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100227 if (ret < 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100228 goto out;
229 }
230
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100231 fd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100232 if (fd < 0) {
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100233 /* Linux-only, we can use %m in printf. */
234 fprintf(stderr, "Failed to open %s: %m", device);
235 goto out;
236 }
237
Paolo Bonzinia517e882011-11-04 15:51:21 +0100238 ret = nbd_init(fd, sock, nbdflags, size, blocksize);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100239 if (ret < 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100240 goto out;
241 }
242
243 /* update partition table */
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100244 pthread_create(&show_parts_thread, NULL, show_parts, device);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100245
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100246 if (verbose) {
247 fprintf(stderr, "NBD device %s is now connected to %s\n",
248 device, srcpath);
249 } else {
250 /* Close stderr so that the qemu-nbd process exits. */
251 dup2(STDOUT_FILENO, STDERR_FILENO);
252 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100253
254 ret = nbd_client(fd);
255 if (ret) {
256 goto out;
257 }
258 close(fd);
259 kill(getpid(), SIGTERM);
260 return (void *) EXIT_SUCCESS;
261
262out:
263 kill(getpid(), SIGTERM);
264 return (void *) EXIT_FAILURE;
thscd831bd2008-07-03 10:23:51 +0000265}
266
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200267static int nbd_can_accept(void *opaque)
268{
269 return nb_fds < shared;
270}
271
Paolo Bonzini7860a382012-09-18 13:31:56 +0200272static void nbd_export_closed(NBDExport *exp)
273{
274 assert(state == TERMINATING);
275 state = TERMINATED;
276}
277
Paolo Bonzini1743b512011-09-19 14:33:23 +0200278static void nbd_client_closed(NBDClient *client)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200279{
Paolo Bonzini1743b512011-09-19 14:33:23 +0200280 nb_fds--;
Paolo Bonzini7860a382012-09-18 13:31:56 +0200281 if (nb_fds == 0 && !persistent && state == RUNNING) {
282 state = TERMINATE;
283 }
Paolo Bonzini1743b512011-09-19 14:33:23 +0200284 qemu_notify_event();
Paolo Bonzini7860a382012-09-18 13:31:56 +0200285 nbd_client_put(client);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200286}
287
288static void nbd_accept(void *opaque)
289{
290 int server_fd = (uintptr_t) opaque;
291 struct sockaddr_in addr;
292 socklen_t addr_len = sizeof(addr);
293
294 int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
Paolo Bonzini7860a382012-09-18 13:31:56 +0200295 if (state >= TERMINATE) {
296 close(fd);
297 return;
298 }
299
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100300 if (fd >= 0 && nbd_client_new(exp, fd, nbd_client_closed)) {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200301 nb_fds++;
302 }
303}
304
bellard7a5ca862008-05-27 21:13:40 +0000305int main(int argc, char **argv)
306{
307 BlockDriverState *bs;
308 off_t dev_offset = 0;
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200309 uint32_t nbdflags = 0;
thscd831bd2008-07-03 10:23:51 +0000310 bool disconnect = false;
bellard7a5ca862008-05-27 21:13:40 +0000311 const char *bindto = "0.0.0.0";
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100312 char *device = NULL;
Laurent Vivierc2e28722010-09-17 20:37:25 +0200313 int port = NBD_DEFAULT_PORT;
bellard7a5ca862008-05-27 21:13:40 +0000314 off_t fd_size;
aliguorid6aa6712009-01-08 19:34:35 +0000315 const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t";
bellard7a5ca862008-05-27 21:13:40 +0000316 struct option lopt[] = {
Blue Swirl660f11b2009-07-31 21:16:51 +0000317 { "help", 0, NULL, 'h' },
318 { "version", 0, NULL, 'V' },
319 { "bind", 1, NULL, 'b' },
320 { "port", 1, NULL, 'p' },
321 { "socket", 1, NULL, 'k' },
322 { "offset", 1, NULL, 'o' },
323 { "read-only", 0, NULL, 'r' },
324 { "partition", 1, NULL, 'P' },
325 { "connect", 1, NULL, 'c' },
326 { "disconnect", 0, NULL, 'd' },
327 { "snapshot", 0, NULL, 's' },
328 { "nocache", 0, NULL, 'n' },
Paolo Bonzini39a52352012-07-18 14:57:15 +0200329 { "cache", 1, NULL, QEMU_NBD_OPT_CACHE },
330#ifdef CONFIG_LINUX_AIO
331 { "aio", 1, NULL, QEMU_NBD_OPT_AIO },
332#endif
Blue Swirl660f11b2009-07-31 21:16:51 +0000333 { "shared", 1, NULL, 'e' },
334 { "persistent", 0, NULL, 't' },
335 { "verbose", 0, NULL, 'v' },
336 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000337 };
338 int ch;
339 int opt_ind = 0;
340 int li;
341 char *end;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200342 int flags = BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000343 int partition = -1;
thscd831bd2008-07-03 10:23:51 +0000344 int ret;
ths3b05a8e2008-07-03 12:45:02 +0000345 int fd;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200346 bool seen_cache = false;
347#ifdef CONFIG_LINUX_AIO
348 bool seen_aio = false;
349#endif
Paolo Bonzinia517e882011-11-04 15:51:21 +0100350 pthread_t client_thread;
bellard7a5ca862008-05-27 21:13:40 +0000351
Paolo Bonzinia517e882011-11-04 15:51:21 +0100352 /* The client thread uses SIGTERM to interrupt the server. A signal
353 * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
354 */
Paolo Bonzinibb345112011-11-04 15:51:19 +0100355 struct sigaction sa_sigterm;
Paolo Bonzinibb345112011-11-04 15:51:19 +0100356 memset(&sa_sigterm, 0, sizeof(sa_sigterm));
357 sa_sigterm.sa_handler = termsig_handler;
358 sigaction(SIGTERM, &sa_sigterm, NULL);
359
bellard7a5ca862008-05-27 21:13:40 +0000360 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
361 switch (ch) {
362 case 's':
ths2f726482008-07-03 11:47:46 +0000363 flags |= BDRV_O_SNAPSHOT;
364 break;
365 case 'n':
Paolo Bonzini39a52352012-07-18 14:57:15 +0200366 optarg = (char *) "none";
367 /* fallthrough */
368 case QEMU_NBD_OPT_CACHE:
369 if (seen_cache) {
370 errx(EXIT_FAILURE, "-n and --cache can only be specified once");
371 }
372 seen_cache = true;
373 if (bdrv_parse_cache_flags(optarg, &flags) == -1) {
374 errx(EXIT_FAILURE, "Invalid cache mode `%s'", optarg);
375 }
bellard7a5ca862008-05-27 21:13:40 +0000376 break;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200377#ifdef CONFIG_LINUX_AIO
378 case QEMU_NBD_OPT_AIO:
379 if (seen_aio) {
380 errx(EXIT_FAILURE, "--aio can only be specified once");
381 }
382 seen_aio = true;
383 if (!strcmp(optarg, "native")) {
384 flags |= BDRV_O_NATIVE_AIO;
385 } else if (!strcmp(optarg, "threads")) {
386 /* this is the default */
387 } else {
388 errx(EXIT_FAILURE, "invalid aio mode `%s'", optarg);
389 }
390 break;
391#endif
bellard7a5ca862008-05-27 21:13:40 +0000392 case 'b':
393 bindto = optarg;
394 break;
395 case 'p':
396 li = strtol(optarg, &end, 0);
397 if (*end) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900398 errx(EXIT_FAILURE, "Invalid port `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000399 }
400 if (li < 1 || li > 65535) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900401 errx(EXIT_FAILURE, "Port out of range `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000402 }
403 port = (uint16_t)li;
404 break;
405 case 'o':
406 dev_offset = strtoll (optarg, &end, 0);
407 if (*end) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900408 errx(EXIT_FAILURE, "Invalid offset `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000409 }
410 if (dev_offset < 0) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900411 errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000412 }
413 break;
414 case 'r':
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200415 nbdflags |= NBD_FLAG_READ_ONLY;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200416 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000417 break;
418 case 'P':
419 partition = strtol(optarg, &end, 0);
420 if (*end)
Ryota Ozakib6353be2010-03-20 15:23:23 +0900421 errx(EXIT_FAILURE, "Invalid partition `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000422 if (partition < 1 || partition > 8)
Ryota Ozakib6353be2010-03-20 15:23:23 +0900423 errx(EXIT_FAILURE, "Invalid partition %d", partition);
bellard7a5ca862008-05-27 21:13:40 +0000424 break;
thscd831bd2008-07-03 10:23:51 +0000425 case 'k':
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100426 sockpath = optarg;
427 if (sockpath[0] != '/')
Ryota Ozakib6353be2010-03-20 15:23:23 +0900428 errx(EXIT_FAILURE, "socket path must be absolute\n");
thscd831bd2008-07-03 10:23:51 +0000429 break;
430 case 'd':
431 disconnect = true;
432 break;
433 case 'c':
434 device = optarg;
435 break;
ths3b05a8e2008-07-03 12:45:02 +0000436 case 'e':
437 shared = strtol(optarg, &end, 0);
438 if (*end) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900439 errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg);
ths3b05a8e2008-07-03 12:45:02 +0000440 }
441 if (shared < 1) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900442 errx(EXIT_FAILURE, "Shared device number must be greater than 0\n");
ths3b05a8e2008-07-03 12:45:02 +0000443 }
444 break;
ths75818252008-07-03 13:41:03 +0000445 case 't':
446 persistent = 1;
447 break;
bellard7a5ca862008-05-27 21:13:40 +0000448 case 'v':
449 verbose = 1;
450 break;
451 case 'V':
452 version(argv[0]);
453 exit(0);
454 break;
455 case 'h':
456 usage(argv[0]);
457 exit(0);
458 break;
459 case '?':
Ryota Ozakib6353be2010-03-20 15:23:23 +0900460 errx(EXIT_FAILURE, "Try `%s --help' for more information.",
bellard7a5ca862008-05-27 21:13:40 +0000461 argv[0]);
462 }
463 }
464
465 if ((argc - optind) != 1) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900466 errx(EXIT_FAILURE, "Invalid number of argument.\n"
bellard7a5ca862008-05-27 21:13:40 +0000467 "Try `%s --help' for more information.",
468 argv[0]);
469 }
470
thscd831bd2008-07-03 10:23:51 +0000471 if (disconnect) {
472 fd = open(argv[optind], O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100473 if (fd < 0) {
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900474 err(EXIT_FAILURE, "Cannot open %s", argv[optind]);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100475 }
thscd831bd2008-07-03 10:23:51 +0000476 nbd_disconnect(fd);
477
478 close(fd);
479
480 printf("%s disconnected\n", argv[optind]);
481
482 return 0;
483 }
484
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100485 if (device && !verbose) {
486 int stderr_fd[2];
487 pid_t pid;
488 int ret;
489
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100490 if (qemu_pipe(stderr_fd) < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100491 err(EXIT_FAILURE, "Error setting up communication pipe");
492 }
493
494 /* Now daemonize, but keep a communication channel open to
495 * print errors and exit with the proper status code.
496 */
497 pid = fork();
498 if (pid == 0) {
499 close(stderr_fd[0]);
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400500 ret = qemu_daemon(1, 0);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100501
502 /* Temporarily redirect stderr to the parent's pipe... */
503 dup2(stderr_fd[1], STDERR_FILENO);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100504 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100505 err(EXIT_FAILURE, "Failed to daemonize");
506 }
507
508 /* ... close the descriptor we inherited and go on. */
509 close(stderr_fd[1]);
510 } else {
511 bool errors = false;
512 char *buf;
513
514 /* In the parent. Print error messages from the child until
515 * it closes the pipe.
516 */
517 close(stderr_fd[1]);
518 buf = g_malloc(1024);
519 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
520 errors = true;
521 ret = qemu_write_full(STDERR_FILENO, buf, ret);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100522 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100523 exit(EXIT_FAILURE);
524 }
525 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100526 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100527 err(EXIT_FAILURE, "Cannot read from daemon");
528 }
529
530 /* Usually the daemon should not print any message.
531 * Exit with zero status in that case.
532 */
533 exit(errors);
534 }
535 }
536
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100537 if (device != NULL && sockpath == NULL) {
538 sockpath = g_malloc(128);
539 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
thscd831bd2008-07-03 10:23:51 +0000540 }
541
Paolo Bonzini7e7f4a02012-11-03 18:06:26 +0100542 qemu_init_main_loop();
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100543 bdrv_init();
544 atexit(bdrv_close_all);
545
546 bs = bdrv_new("hda");
547 srcpath = argv[optind];
548 if ((ret = bdrv_open(bs, srcpath, flags, NULL)) < 0) {
549 errno = -ret;
550 err(EXIT_FAILURE, "Failed to bdrv_open '%s'", argv[optind]);
551 }
552
Paolo Bonzini38ceff02012-03-12 16:17:27 +0100553 fd_size = bdrv_getlength(bs);
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100554
Paolo Bonzini185b4332012-03-05 08:56:10 +0100555 if (partition != -1) {
556 ret = find_partition(bs, partition, &dev_offset, &fd_size);
557 if (ret < 0) {
558 errno = -ret;
559 err(EXIT_FAILURE, "Could not find partition %d", partition);
560 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100561 }
562
Paolo Bonzini7860a382012-09-18 13:31:56 +0200563 exp = nbd_export_new(bs, dev_offset, fd_size, nbdflags, nbd_export_closed);
ths3b05a8e2008-07-03 12:45:02 +0000564
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100565 if (sockpath) {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200566 fd = unix_socket_incoming(sockpath);
thscd831bd2008-07-03 10:23:51 +0000567 } else {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200568 fd = tcp_socket_incoming(bindto, port);
thscd831bd2008-07-03 10:23:51 +0000569 }
570
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100571 if (fd < 0) {
bellard7a5ca862008-05-27 21:13:40 +0000572 return 1;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200573 }
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100574
575 if (device) {
576 int ret;
577
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100578 ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100579 if (ret != 0) {
580 errx(EXIT_FAILURE, "Failed to create client thread: %s",
581 strerror(ret));
582 }
583 } else {
584 /* Shut up GCC warnings. */
585 memset(&client_thread, 0, sizeof(client_thread));
586 }
587
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200588 qemu_set_fd_handler2(fd, nbd_can_accept, nbd_accept, NULL,
589 (void *)(uintptr_t)fd);
bellard7a5ca862008-05-27 21:13:40 +0000590
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400591 /* now when the initialization is (almost) complete, chdir("/")
592 * to free any busy filesystems */
593 if (chdir("/") < 0) {
594 err(EXIT_FAILURE, "Could not chdir to root directory");
595 }
596
Paolo Bonzini7860a382012-09-18 13:31:56 +0200597 state = RUNNING;
ths3b05a8e2008-07-03 12:45:02 +0000598 do {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200599 main_loop_wait(false);
Paolo Bonzini7860a382012-09-18 13:31:56 +0200600 if (state == TERMINATE) {
601 state = TERMINATING;
602 nbd_export_close(exp);
603 nbd_export_put(exp);
604 exp = NULL;
605 }
606 } while (state != TERMINATED);
ths3b05a8e2008-07-03 12:45:02 +0000607
Paolo Bonzinia4aab7b2012-08-22 18:50:30 +0200608 bdrv_close(bs);
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100609 if (sockpath) {
610 unlink(sockpath);
611 }
bellard7a5ca862008-05-27 21:13:40 +0000612
Paolo Bonzinia517e882011-11-04 15:51:21 +0100613 if (device) {
614 void *ret;
615 pthread_join(client_thread, &ret);
616 exit(ret != NULL);
617 } else {
618 exit(EXIT_SUCCESS);
619 }
bellard7a5ca862008-05-27 21:13:40 +0000620}