blob: f044546c28469bc8f289cb3b72e1d50b10c1a57b [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"
Alex Bligh6a1751b2013-08-21 16:02:47 +010022#include "qemu/main-loop.h"
bellard7a5ca862008-05-27 21:13:40 +000023
bellard7a5ca862008-05-27 21:13:40 +000024#include <stdarg.h>
25#include <stdio.h>
26#include <getopt.h>
27#include <err.h>
thscd831bd2008-07-03 10:23:51 +000028#include <sys/types.h>
bellard7a5ca862008-05-27 21:13:40 +000029#include <sys/socket.h>
30#include <netinet/in.h>
31#include <netinet/tcp.h>
32#include <arpa/inet.h>
thscd831bd2008-07-03 10:23:51 +000033#include <signal.h>
Blue Swirl2bff4b62009-12-23 15:34:04 +000034#include <libgen.h>
Paolo Bonzinia517e882011-11-04 15:51:21 +010035#include <pthread.h>
thscd831bd2008-07-03 10:23:51 +000036
Paolo Bonzinided9d2d2013-02-08 14:06:13 +010037#define SOCKET_PATH "/var/lock/qemu-nbd-%s"
38#define QEMU_NBD_OPT_CACHE 1
39#define QEMU_NBD_OPT_AIO 2
40#define QEMU_NBD_OPT_DISCARD 3
bellard7a5ca862008-05-27 21:13:40 +000041
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +020042static NBDExport *exp;
blueswir1b1d8e522008-10-26 13:43:07 +000043static int verbose;
Paolo Bonzinia517e882011-11-04 15:51:21 +010044static char *srcpath;
45static char *sockpath;
Paolo Bonzini7860a382012-09-18 13:31:56 +020046static int persistent = 0;
47static enum { RUNNING, TERMINATE, TERMINATING, TERMINATED } state;
Paolo Bonzinia61c6782011-09-12 17:28:11 +020048static int shared = 1;
49static int nb_fds;
bellard7a5ca862008-05-27 21:13:40 +000050
51static void usage(const char *name)
52{
Paolo Bonzinib033cd82012-07-18 14:50:52 +020053 (printf) (
bellard7a5ca862008-05-27 21:13:40 +000054"Usage: %s [OPTIONS] FILE\n"
55"QEMU Disk Network Block Device Server\n"
56"\n"
bellard7a5ca862008-05-27 21:13:40 +000057" -h, --help display this help and exit\n"
58" -V, --version output version information and exit\n"
59"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020060"Connection properties:\n"
61" -p, --port=PORT port to listen on (default `%d')\n"
62" -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
63" -k, --socket=PATH path to the unix socket\n"
64" (default '"SOCKET_PATH"')\n"
65" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
66" -t, --persistent don't exit on the last connection\n"
67" -v, --verbose display extra debugging information\n"
68"\n"
69"Exposing part of the image:\n"
70" -o, --offset=OFFSET offset into the image\n"
71" -P, --partition=NUM only expose partition NUM\n"
72"\n"
73#ifdef __linux__
74"Kernel NBD client support:\n"
75" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
76" -d, --disconnect disconnect the specified device\n"
77"\n"
78#endif
79"\n"
80"Block device options:\n"
81" -r, --read-only export read-only\n"
82" -s, --snapshot use snapshot file\n"
83" -n, --nocache disable host cache\n"
Paolo Bonzini39a52352012-07-18 14:57:15 +020084" --cache=MODE set cache mode (none, writeback, ...)\n"
85#ifdef CONFIG_LINUX_AIO
86" --aio=MODE set AIO mode (native or threads)\n"
87#endif
Paolo Bonzinib033cd82012-07-18 14:50:52 +020088"\n"
89"Report bugs to <qemu-devel@nongnu.org>\n"
Laurent Vivierc2e28722010-09-17 20:37:25 +020090 , name, NBD_DEFAULT_PORT, "DEVICE");
bellard7a5ca862008-05-27 21:13:40 +000091}
92
93static void version(const char *name)
94{
95 printf(
ths315bc7a2008-07-18 18:06:23 +000096"%s version 0.0.1\n"
bellard7a5ca862008-05-27 21:13:40 +000097"Written by Anthony Liguori.\n"
98"\n"
99"Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
100"This is free software; see the source for copying conditions. There is NO\n"
101"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
ths315bc7a2008-07-18 18:06:23 +0000102 , name);
bellard7a5ca862008-05-27 21:13:40 +0000103}
104
105struct partition_record
106{
107 uint8_t bootable;
108 uint8_t start_head;
109 uint32_t start_cylinder;
110 uint8_t start_sector;
111 uint8_t system;
112 uint8_t end_head;
113 uint8_t end_cylinder;
114 uint8_t end_sector;
115 uint32_t start_sector_abs;
116 uint32_t nb_sectors_abs;
117};
118
119static void read_partition(uint8_t *p, struct partition_record *r)
120{
121 r->bootable = p[0];
122 r->start_head = p[1];
123 r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
124 r->start_sector = p[2] & 0x3f;
125 r->system = p[4];
126 r->end_head = p[5];
127 r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
128 r->end_sector = p[6] & 0x3f;
129 r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24;
130 r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24;
131}
132
133static int find_partition(BlockDriverState *bs, int partition,
134 off_t *offset, off_t *size)
135{
136 struct partition_record mbr[4];
137 uint8_t data[512];
138 int i;
139 int ext_partnum = 4;
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900140 int ret;
bellard7a5ca862008-05-27 21:13:40 +0000141
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900142 if ((ret = bdrv_read(bs, 0, data, 1)) < 0) {
143 errno = -ret;
144 err(EXIT_FAILURE, "error while reading");
145 }
bellard7a5ca862008-05-27 21:13:40 +0000146
147 if (data[510] != 0x55 || data[511] != 0xaa) {
Paolo Bonzini185b4332012-03-05 08:56:10 +0100148 return -EINVAL;
bellard7a5ca862008-05-27 21:13:40 +0000149 }
150
151 for (i = 0; i < 4; i++) {
152 read_partition(&data[446 + 16 * i], &mbr[i]);
153
154 if (!mbr[i].nb_sectors_abs)
155 continue;
156
157 if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
158 struct partition_record ext[4];
159 uint8_t data1[512];
160 int j;
161
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900162 if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) {
163 errno = -ret;
164 err(EXIT_FAILURE, "error while reading");
165 }
bellard7a5ca862008-05-27 21:13:40 +0000166
167 for (j = 0; j < 4; j++) {
168 read_partition(&data1[446 + 16 * j], &ext[j]);
169 if (!ext[j].nb_sectors_abs)
170 continue;
171
172 if ((ext_partnum + j + 1) == partition) {
173 *offset = (uint64_t)ext[j].start_sector_abs << 9;
174 *size = (uint64_t)ext[j].nb_sectors_abs << 9;
175 return 0;
176 }
177 }
178 ext_partnum += 4;
179 } else if ((i + 1) == partition) {
180 *offset = (uint64_t)mbr[i].start_sector_abs << 9;
181 *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
182 return 0;
183 }
184 }
185
Paolo Bonzini185b4332012-03-05 08:56:10 +0100186 return -ENOENT;
bellard7a5ca862008-05-27 21:13:40 +0000187}
188
Paolo Bonzinibb345112011-11-04 15:51:19 +0100189static void termsig_handler(int signum)
190{
Paolo Bonzini7860a382012-09-18 13:31:56 +0200191 state = TERMINATE;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200192 qemu_notify_event();
Paolo Bonzinibb345112011-11-04 15:51:19 +0100193}
194
Paolo Bonzinia517e882011-11-04 15:51:21 +0100195static void *show_parts(void *arg)
thscd831bd2008-07-03 10:23:51 +0000196{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100197 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100198 int nbd;
thscd831bd2008-07-03 10:23:51 +0000199
Paolo Bonzinia517e882011-11-04 15:51:21 +0100200 /* linux just needs an open() to trigger
201 * the partition table update
202 * but remember to load the module with max_part != 0 :
203 * modprobe nbd max_part=63
204 */
205 nbd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100206 if (nbd >= 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100207 close(nbd);
thscd831bd2008-07-03 10:23:51 +0000208 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100209 return NULL;
210}
211
212static void *nbd_client_thread(void *arg)
213{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100214 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100215 off_t size;
216 size_t blocksize;
217 uint32_t nbdflags;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100218 int fd, sock;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100219 int ret;
220 pthread_t show_parts_thread;
221
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000222 sock = unix_socket_outgoing(sockpath);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100223 if (sock < 0) {
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000224 goto out;
225 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100226
227 ret = nbd_receive_negotiate(sock, NULL, &nbdflags,
228 &size, &blocksize);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100229 if (ret < 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100230 goto out;
231 }
232
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100233 fd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100234 if (fd < 0) {
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100235 /* Linux-only, we can use %m in printf. */
236 fprintf(stderr, "Failed to open %s: %m", device);
237 goto out;
238 }
239
Paolo Bonzinia517e882011-11-04 15:51:21 +0100240 ret = nbd_init(fd, sock, nbdflags, size, blocksize);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100241 if (ret < 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100242 goto out;
243 }
244
245 /* update partition table */
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100246 pthread_create(&show_parts_thread, NULL, show_parts, device);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100247
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100248 if (verbose) {
249 fprintf(stderr, "NBD device %s is now connected to %s\n",
250 device, srcpath);
251 } else {
252 /* Close stderr so that the qemu-nbd process exits. */
253 dup2(STDOUT_FILENO, STDERR_FILENO);
254 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100255
256 ret = nbd_client(fd);
257 if (ret) {
258 goto out;
259 }
260 close(fd);
261 kill(getpid(), SIGTERM);
262 return (void *) EXIT_SUCCESS;
263
264out:
265 kill(getpid(), SIGTERM);
266 return (void *) EXIT_FAILURE;
thscd831bd2008-07-03 10:23:51 +0000267}
268
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200269static int nbd_can_accept(void *opaque)
270{
271 return nb_fds < shared;
272}
273
Paolo Bonzini7860a382012-09-18 13:31:56 +0200274static void nbd_export_closed(NBDExport *exp)
275{
276 assert(state == TERMINATING);
277 state = TERMINATED;
278}
279
Paolo Bonzini1743b512011-09-19 14:33:23 +0200280static void nbd_client_closed(NBDClient *client)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200281{
Paolo Bonzini1743b512011-09-19 14:33:23 +0200282 nb_fds--;
Paolo Bonzini7860a382012-09-18 13:31:56 +0200283 if (nb_fds == 0 && !persistent && state == RUNNING) {
284 state = TERMINATE;
285 }
Paolo Bonzini1743b512011-09-19 14:33:23 +0200286 qemu_notify_event();
Paolo Bonzini7860a382012-09-18 13:31:56 +0200287 nbd_client_put(client);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200288}
289
290static void nbd_accept(void *opaque)
291{
292 int server_fd = (uintptr_t) opaque;
293 struct sockaddr_in addr;
294 socklen_t addr_len = sizeof(addr);
295
296 int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
Paolo Bonzini7860a382012-09-18 13:31:56 +0200297 if (state >= TERMINATE) {
298 close(fd);
299 return;
300 }
301
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100302 if (fd >= 0 && nbd_client_new(exp, fd, nbd_client_closed)) {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200303 nb_fds++;
304 }
305}
306
bellard7a5ca862008-05-27 21:13:40 +0000307int main(int argc, char **argv)
308{
309 BlockDriverState *bs;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000310 BlockDriver *drv;
bellard7a5ca862008-05-27 21:13:40 +0000311 off_t dev_offset = 0;
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200312 uint32_t nbdflags = 0;
thscd831bd2008-07-03 10:23:51 +0000313 bool disconnect = false;
bellard7a5ca862008-05-27 21:13:40 +0000314 const char *bindto = "0.0.0.0";
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100315 char *device = NULL;
Laurent Vivierc2e28722010-09-17 20:37:25 +0200316 int port = NBD_DEFAULT_PORT;
bellard7a5ca862008-05-27 21:13:40 +0000317 off_t fd_size;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000318 const char *sopt = "hVb:o:p:rsnP:c:dvk:e:f:t";
bellard7a5ca862008-05-27 21:13:40 +0000319 struct option lopt[] = {
Blue Swirl660f11b2009-07-31 21:16:51 +0000320 { "help", 0, NULL, 'h' },
321 { "version", 0, NULL, 'V' },
322 { "bind", 1, NULL, 'b' },
323 { "port", 1, NULL, 'p' },
324 { "socket", 1, NULL, 'k' },
325 { "offset", 1, NULL, 'o' },
326 { "read-only", 0, NULL, 'r' },
327 { "partition", 1, NULL, 'P' },
328 { "connect", 1, NULL, 'c' },
329 { "disconnect", 0, NULL, 'd' },
330 { "snapshot", 0, NULL, 's' },
331 { "nocache", 0, NULL, 'n' },
Paolo Bonzini39a52352012-07-18 14:57:15 +0200332 { "cache", 1, NULL, QEMU_NBD_OPT_CACHE },
333#ifdef CONFIG_LINUX_AIO
334 { "aio", 1, NULL, QEMU_NBD_OPT_AIO },
335#endif
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100336 { "discard", 1, NULL, QEMU_NBD_OPT_DISCARD },
Blue Swirl660f11b2009-07-31 21:16:51 +0000337 { "shared", 1, NULL, 'e' },
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000338 { "format", 1, NULL, 'f' },
Blue Swirl660f11b2009-07-31 21:16:51 +0000339 { "persistent", 0, NULL, 't' },
340 { "verbose", 0, NULL, 'v' },
341 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000342 };
343 int ch;
344 int opt_ind = 0;
345 int li;
346 char *end;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200347 int flags = BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000348 int partition = -1;
thscd831bd2008-07-03 10:23:51 +0000349 int ret;
ths3b05a8e2008-07-03 12:45:02 +0000350 int fd;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200351 bool seen_cache = false;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100352 bool seen_discard = false;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200353#ifdef CONFIG_LINUX_AIO
354 bool seen_aio = false;
355#endif
Paolo Bonzinia517e882011-11-04 15:51:21 +0100356 pthread_t client_thread;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000357 const char *fmt = NULL;
bellard7a5ca862008-05-27 21:13:40 +0000358
Paolo Bonzinia517e882011-11-04 15:51:21 +0100359 /* The client thread uses SIGTERM to interrupt the server. A signal
360 * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
361 */
Paolo Bonzinibb345112011-11-04 15:51:19 +0100362 struct sigaction sa_sigterm;
Paolo Bonzinibb345112011-11-04 15:51:19 +0100363 memset(&sa_sigterm, 0, sizeof(sa_sigterm));
364 sa_sigterm.sa_handler = termsig_handler;
365 sigaction(SIGTERM, &sa_sigterm, NULL);
366
bellard7a5ca862008-05-27 21:13:40 +0000367 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
368 switch (ch) {
369 case 's':
ths2f726482008-07-03 11:47:46 +0000370 flags |= BDRV_O_SNAPSHOT;
371 break;
372 case 'n':
Paolo Bonzini39a52352012-07-18 14:57:15 +0200373 optarg = (char *) "none";
374 /* fallthrough */
375 case QEMU_NBD_OPT_CACHE:
376 if (seen_cache) {
377 errx(EXIT_FAILURE, "-n and --cache can only be specified once");
378 }
379 seen_cache = true;
380 if (bdrv_parse_cache_flags(optarg, &flags) == -1) {
381 errx(EXIT_FAILURE, "Invalid cache mode `%s'", optarg);
382 }
bellard7a5ca862008-05-27 21:13:40 +0000383 break;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200384#ifdef CONFIG_LINUX_AIO
385 case QEMU_NBD_OPT_AIO:
386 if (seen_aio) {
387 errx(EXIT_FAILURE, "--aio can only be specified once");
388 }
389 seen_aio = true;
390 if (!strcmp(optarg, "native")) {
391 flags |= BDRV_O_NATIVE_AIO;
392 } else if (!strcmp(optarg, "threads")) {
393 /* this is the default */
394 } else {
395 errx(EXIT_FAILURE, "invalid aio mode `%s'", optarg);
396 }
397 break;
398#endif
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100399 case QEMU_NBD_OPT_DISCARD:
400 if (seen_discard) {
401 errx(EXIT_FAILURE, "--discard can only be specified once");
402 }
403 seen_discard = true;
404 if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
405 errx(EXIT_FAILURE, "Invalid discard mode `%s'", optarg);
406 }
407 break;
bellard7a5ca862008-05-27 21:13:40 +0000408 case 'b':
409 bindto = optarg;
410 break;
411 case 'p':
412 li = strtol(optarg, &end, 0);
413 if (*end) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900414 errx(EXIT_FAILURE, "Invalid port `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000415 }
416 if (li < 1 || li > 65535) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900417 errx(EXIT_FAILURE, "Port out of range `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000418 }
419 port = (uint16_t)li;
420 break;
421 case 'o':
422 dev_offset = strtoll (optarg, &end, 0);
423 if (*end) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900424 errx(EXIT_FAILURE, "Invalid offset `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000425 }
426 if (dev_offset < 0) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900427 errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000428 }
429 break;
430 case 'r':
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200431 nbdflags |= NBD_FLAG_READ_ONLY;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200432 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000433 break;
434 case 'P':
435 partition = strtol(optarg, &end, 0);
436 if (*end)
Ryota Ozakib6353be2010-03-20 15:23:23 +0900437 errx(EXIT_FAILURE, "Invalid partition `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000438 if (partition < 1 || partition > 8)
Ryota Ozakib6353be2010-03-20 15:23:23 +0900439 errx(EXIT_FAILURE, "Invalid partition %d", partition);
bellard7a5ca862008-05-27 21:13:40 +0000440 break;
thscd831bd2008-07-03 10:23:51 +0000441 case 'k':
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100442 sockpath = optarg;
443 if (sockpath[0] != '/')
Ryota Ozakib6353be2010-03-20 15:23:23 +0900444 errx(EXIT_FAILURE, "socket path must be absolute\n");
thscd831bd2008-07-03 10:23:51 +0000445 break;
446 case 'd':
447 disconnect = true;
448 break;
449 case 'c':
450 device = optarg;
451 break;
ths3b05a8e2008-07-03 12:45:02 +0000452 case 'e':
453 shared = strtol(optarg, &end, 0);
454 if (*end) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900455 errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg);
ths3b05a8e2008-07-03 12:45:02 +0000456 }
457 if (shared < 1) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900458 errx(EXIT_FAILURE, "Shared device number must be greater than 0\n");
ths3b05a8e2008-07-03 12:45:02 +0000459 }
460 break;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000461 case 'f':
462 fmt = optarg;
463 break;
ths75818252008-07-03 13:41:03 +0000464 case 't':
465 persistent = 1;
466 break;
bellard7a5ca862008-05-27 21:13:40 +0000467 case 'v':
468 verbose = 1;
469 break;
470 case 'V':
471 version(argv[0]);
472 exit(0);
473 break;
474 case 'h':
475 usage(argv[0]);
476 exit(0);
477 break;
478 case '?':
Ryota Ozakib6353be2010-03-20 15:23:23 +0900479 errx(EXIT_FAILURE, "Try `%s --help' for more information.",
bellard7a5ca862008-05-27 21:13:40 +0000480 argv[0]);
481 }
482 }
483
484 if ((argc - optind) != 1) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900485 errx(EXIT_FAILURE, "Invalid number of argument.\n"
bellard7a5ca862008-05-27 21:13:40 +0000486 "Try `%s --help' for more information.",
487 argv[0]);
488 }
489
thscd831bd2008-07-03 10:23:51 +0000490 if (disconnect) {
491 fd = open(argv[optind], O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100492 if (fd < 0) {
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900493 err(EXIT_FAILURE, "Cannot open %s", argv[optind]);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100494 }
thscd831bd2008-07-03 10:23:51 +0000495 nbd_disconnect(fd);
496
497 close(fd);
498
499 printf("%s disconnected\n", argv[optind]);
500
501 return 0;
502 }
503
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100504 if (device && !verbose) {
505 int stderr_fd[2];
506 pid_t pid;
507 int ret;
508
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100509 if (qemu_pipe(stderr_fd) < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100510 err(EXIT_FAILURE, "Error setting up communication pipe");
511 }
512
513 /* Now daemonize, but keep a communication channel open to
514 * print errors and exit with the proper status code.
515 */
516 pid = fork();
517 if (pid == 0) {
518 close(stderr_fd[0]);
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400519 ret = qemu_daemon(1, 0);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100520
521 /* Temporarily redirect stderr to the parent's pipe... */
522 dup2(stderr_fd[1], STDERR_FILENO);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100523 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100524 err(EXIT_FAILURE, "Failed to daemonize");
525 }
526
527 /* ... close the descriptor we inherited and go on. */
528 close(stderr_fd[1]);
529 } else {
530 bool errors = false;
531 char *buf;
532
533 /* In the parent. Print error messages from the child until
534 * it closes the pipe.
535 */
536 close(stderr_fd[1]);
537 buf = g_malloc(1024);
538 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
539 errors = true;
540 ret = qemu_write_full(STDERR_FILENO, buf, ret);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100541 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100542 exit(EXIT_FAILURE);
543 }
544 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100545 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100546 err(EXIT_FAILURE, "Cannot read from daemon");
547 }
548
549 /* Usually the daemon should not print any message.
550 * Exit with zero status in that case.
551 */
552 exit(errors);
553 }
554 }
555
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100556 if (device != NULL && sockpath == NULL) {
557 sockpath = g_malloc(128);
558 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
thscd831bd2008-07-03 10:23:51 +0000559 }
560
Paolo Bonzini7e7f4a02012-11-03 18:06:26 +0100561 qemu_init_main_loop();
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100562 bdrv_init();
563 atexit(bdrv_close_all);
564
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000565 if (fmt) {
566 drv = bdrv_find_format(fmt);
567 if (!drv) {
568 errx(EXIT_FAILURE, "Unknown file format '%s'", fmt);
569 }
570 } else {
571 drv = NULL;
572 }
573
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100574 bs = bdrv_new("hda");
575 srcpath = argv[optind];
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000576 ret = bdrv_open(bs, srcpath, NULL, flags, drv);
577 if (ret < 0) {
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100578 errno = -ret;
579 err(EXIT_FAILURE, "Failed to bdrv_open '%s'", argv[optind]);
580 }
581
Paolo Bonzini38ceff02012-03-12 16:17:27 +0100582 fd_size = bdrv_getlength(bs);
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100583
Paolo Bonzini185b4332012-03-05 08:56:10 +0100584 if (partition != -1) {
585 ret = find_partition(bs, partition, &dev_offset, &fd_size);
586 if (ret < 0) {
587 errno = -ret;
588 err(EXIT_FAILURE, "Could not find partition %d", partition);
589 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100590 }
591
Paolo Bonzini7860a382012-09-18 13:31:56 +0200592 exp = nbd_export_new(bs, dev_offset, fd_size, nbdflags, nbd_export_closed);
ths3b05a8e2008-07-03 12:45:02 +0000593
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100594 if (sockpath) {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200595 fd = unix_socket_incoming(sockpath);
thscd831bd2008-07-03 10:23:51 +0000596 } else {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200597 fd = tcp_socket_incoming(bindto, port);
thscd831bd2008-07-03 10:23:51 +0000598 }
599
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100600 if (fd < 0) {
bellard7a5ca862008-05-27 21:13:40 +0000601 return 1;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200602 }
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100603
604 if (device) {
605 int ret;
606
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100607 ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100608 if (ret != 0) {
609 errx(EXIT_FAILURE, "Failed to create client thread: %s",
610 strerror(ret));
611 }
612 } else {
613 /* Shut up GCC warnings. */
614 memset(&client_thread, 0, sizeof(client_thread));
615 }
616
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200617 qemu_set_fd_handler2(fd, nbd_can_accept, nbd_accept, NULL,
618 (void *)(uintptr_t)fd);
bellard7a5ca862008-05-27 21:13:40 +0000619
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400620 /* now when the initialization is (almost) complete, chdir("/")
621 * to free any busy filesystems */
622 if (chdir("/") < 0) {
623 err(EXIT_FAILURE, "Could not chdir to root directory");
624 }
625
Paolo Bonzini7860a382012-09-18 13:31:56 +0200626 state = RUNNING;
ths3b05a8e2008-07-03 12:45:02 +0000627 do {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200628 main_loop_wait(false);
Paolo Bonzini7860a382012-09-18 13:31:56 +0200629 if (state == TERMINATE) {
630 state = TERMINATING;
631 nbd_export_close(exp);
632 nbd_export_put(exp);
633 exp = NULL;
634 }
635 } while (state != TERMINATED);
ths3b05a8e2008-07-03 12:45:02 +0000636
Paolo Bonzinia4aab7b2012-08-22 18:50:30 +0200637 bdrv_close(bs);
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100638 if (sockpath) {
639 unlink(sockpath);
640 }
bellard7a5ca862008-05-27 21:13:40 +0000641
Paolo Bonzinia517e882011-11-04 15:51:21 +0100642 if (device) {
643 void *ret;
644 pthread_join(client_thread, &ret);
645 exit(ret != NULL);
646 } else {
647 exit(EXIT_SUCCESS);
648 }
bellard7a5ca862008-05-27 21:13:40 +0000649}