blob: fe6053ca56ee8a5f7ab71e13a70d23881d26c8e0 [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"
Wenchao Xia8c116b02013-12-04 17:10:55 +080023#include "block/snapshot.h"
bellard7a5ca862008-05-27 21:13:40 +000024
bellard7a5ca862008-05-27 21:13:40 +000025#include <stdarg.h>
26#include <stdio.h>
27#include <getopt.h>
28#include <err.h>
thscd831bd2008-07-03 10:23:51 +000029#include <sys/types.h>
bellard7a5ca862008-05-27 21:13:40 +000030#include <sys/socket.h>
31#include <netinet/in.h>
32#include <netinet/tcp.h>
33#include <arpa/inet.h>
thscd831bd2008-07-03 10:23:51 +000034#include <signal.h>
Blue Swirl2bff4b62009-12-23 15:34:04 +000035#include <libgen.h>
Paolo Bonzinia517e882011-11-04 15:51:21 +010036#include <pthread.h>
thscd831bd2008-07-03 10:23:51 +000037
Paolo Bonzinided9d2d2013-02-08 14:06:13 +010038#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
bellard7a5ca862008-05-27 21:13:40 +000042
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +020043static NBDExport *exp;
blueswir1b1d8e522008-10-26 13:43:07 +000044static int verbose;
Paolo Bonzinia517e882011-11-04 15:51:21 +010045static char *srcpath;
46static char *sockpath;
Paolo Bonzini7860a382012-09-18 13:31:56 +020047static int persistent = 0;
48static enum { RUNNING, TERMINATE, TERMINATING, TERMINATED } state;
Paolo Bonzinia61c6782011-09-12 17:28:11 +020049static int shared = 1;
50static int nb_fds;
bellard7a5ca862008-05-27 21:13:40 +000051
52static void usage(const char *name)
53{
Paolo Bonzinib033cd82012-07-18 14:50:52 +020054 (printf) (
bellard7a5ca862008-05-27 21:13:40 +000055"Usage: %s [OPTIONS] FILE\n"
56"QEMU Disk Network Block Device Server\n"
57"\n"
bellard7a5ca862008-05-27 21:13:40 +000058" -h, --help display this help and exit\n"
59" -V, --version output version information and exit\n"
60"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020061"Connection properties:\n"
62" -p, --port=PORT port to listen on (default `%d')\n"
63" -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
64" -k, --socket=PATH path to the unix socket\n"
65" (default '"SOCKET_PATH"')\n"
66" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
67" -t, --persistent don't exit on the last connection\n"
68" -v, --verbose display extra debugging information\n"
69"\n"
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"
Wenchao Xia8c116b02013-12-04 17:10:55 +080083" -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"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020091" -n, --nocache disable host cache\n"
Paolo Bonzini39a52352012-07-18 14:57:15 +020092" --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
Paolo Bonzinib033cd82012-07-18 14:50:52 +020096"\n"
97"Report bugs to <qemu-devel@nongnu.org>\n"
Laurent Vivierc2e28722010-09-17 20:37:25 +020098 , name, NBD_DEFAULT_PORT, "DEVICE");
bellard7a5ca862008-05-27 21:13:40 +000099}
100
101static void version(const char *name)
102{
103 printf(
ths315bc7a2008-07-18 18:06:23 +0000104"%s version 0.0.1\n"
bellard7a5ca862008-05-27 21:13:40 +0000105"Written by Anthony Liguori.\n"
106"\n"
107"Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\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"
ths315bc7a2008-07-18 18:06:23 +0000110 , name);
bellard7a5ca862008-05-27 21:13:40 +0000111}
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;
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900148 int ret;
bellard7a5ca862008-05-27 21:13:40 +0000149
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900150 if ((ret = bdrv_read(bs, 0, data, 1)) < 0) {
151 errno = -ret;
152 err(EXIT_FAILURE, "error while reading");
153 }
bellard7a5ca862008-05-27 21:13:40 +0000154
155 if (data[510] != 0x55 || data[511] != 0xaa) {
Paolo Bonzini185b4332012-03-05 08:56:10 +0100156 return -EINVAL;
bellard7a5ca862008-05-27 21:13:40 +0000157 }
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
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900170 if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) {
171 errno = -ret;
172 err(EXIT_FAILURE, "error while reading");
173 }
bellard7a5ca862008-05-27 21:13:40 +0000174
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
Paolo Bonzini185b4332012-03-05 08:56:10 +0100194 return -ENOENT;
bellard7a5ca862008-05-27 21:13:40 +0000195}
196
Paolo Bonzinibb345112011-11-04 15:51:19 +0100197static void termsig_handler(int signum)
198{
Paolo Bonzini7860a382012-09-18 13:31:56 +0200199 state = TERMINATE;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200200 qemu_notify_event();
Paolo Bonzinibb345112011-11-04 15:51:19 +0100201}
202
Paolo Bonzinia517e882011-11-04 15:51:21 +0100203static void *show_parts(void *arg)
thscd831bd2008-07-03 10:23:51 +0000204{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100205 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100206 int nbd;
thscd831bd2008-07-03 10:23:51 +0000207
Paolo Bonzinia517e882011-11-04 15:51:21 +0100208 /* 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);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100214 if (nbd >= 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100215 close(nbd);
thscd831bd2008-07-03 10:23:51 +0000216 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100217 return NULL;
218}
219
220static void *nbd_client_thread(void *arg)
221{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100222 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100223 off_t size;
224 size_t blocksize;
225 uint32_t nbdflags;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100226 int fd, sock;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100227 int ret;
228 pthread_t show_parts_thread;
229
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000230 sock = unix_socket_outgoing(sockpath);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100231 if (sock < 0) {
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000232 goto out;
233 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100234
235 ret = nbd_receive_negotiate(sock, NULL, &nbdflags,
236 &size, &blocksize);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100237 if (ret < 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100238 goto out;
239 }
240
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100241 fd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100242 if (fd < 0) {
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100243 /* Linux-only, we can use %m in printf. */
244 fprintf(stderr, "Failed to open %s: %m", device);
245 goto out;
246 }
247
Paolo Bonzinia517e882011-11-04 15:51:21 +0100248 ret = nbd_init(fd, sock, nbdflags, size, blocksize);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100249 if (ret < 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100250 goto out;
251 }
252
253 /* update partition table */
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100254 pthread_create(&show_parts_thread, NULL, show_parts, device);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100255
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100256 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 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100263
264 ret = nbd_client(fd);
265 if (ret) {
266 goto out;
267 }
268 close(fd);
269 kill(getpid(), SIGTERM);
270 return (void *) EXIT_SUCCESS;
271
272out:
273 kill(getpid(), SIGTERM);
274 return (void *) EXIT_FAILURE;
thscd831bd2008-07-03 10:23:51 +0000275}
276
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200277static int nbd_can_accept(void *opaque)
278{
279 return nb_fds < shared;
280}
281
Paolo Bonzini7860a382012-09-18 13:31:56 +0200282static void nbd_export_closed(NBDExport *exp)
283{
284 assert(state == TERMINATING);
285 state = TERMINATED;
286}
287
Paolo Bonzini1743b512011-09-19 14:33:23 +0200288static void nbd_client_closed(NBDClient *client)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200289{
Paolo Bonzini1743b512011-09-19 14:33:23 +0200290 nb_fds--;
Paolo Bonzini7860a382012-09-18 13:31:56 +0200291 if (nb_fds == 0 && !persistent && state == RUNNING) {
292 state = TERMINATE;
293 }
Paolo Bonzini1743b512011-09-19 14:33:23 +0200294 qemu_notify_event();
Paolo Bonzini7860a382012-09-18 13:31:56 +0200295 nbd_client_put(client);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200296}
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);
Paolo Bonzini7860a382012-09-18 13:31:56 +0200305 if (state >= TERMINATE) {
306 close(fd);
307 return;
308 }
309
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100310 if (fd >= 0 && nbd_client_new(exp, fd, nbd_client_closed)) {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200311 nb_fds++;
312 }
313}
314
bellard7a5ca862008-05-27 21:13:40 +0000315int main(int argc, char **argv)
316{
317 BlockDriverState *bs;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000318 BlockDriver *drv;
bellard7a5ca862008-05-27 21:13:40 +0000319 off_t dev_offset = 0;
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200320 uint32_t nbdflags = 0;
thscd831bd2008-07-03 10:23:51 +0000321 bool disconnect = false;
bellard7a5ca862008-05-27 21:13:40 +0000322 const char *bindto = "0.0.0.0";
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100323 char *device = NULL;
Laurent Vivierc2e28722010-09-17 20:37:25 +0200324 int port = NBD_DEFAULT_PORT;
bellard7a5ca862008-05-27 21:13:40 +0000325 off_t fd_size;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800326 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:";
bellard7a5ca862008-05-27 21:13:40 +0000329 struct option lopt[] = {
Blue Swirl660f11b2009-07-31 21:16:51 +0000330 { "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' },
Wenchao Xia8c116b02013-12-04 17:10:55 +0800341 { "load-snapshot", 1, NULL, 'l' },
Blue Swirl660f11b2009-07-31 21:16:51 +0000342 { "nocache", 0, NULL, 'n' },
Paolo Bonzini39a52352012-07-18 14:57:15 +0200343 { "cache", 1, NULL, QEMU_NBD_OPT_CACHE },
344#ifdef CONFIG_LINUX_AIO
345 { "aio", 1, NULL, QEMU_NBD_OPT_AIO },
346#endif
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100347 { "discard", 1, NULL, QEMU_NBD_OPT_DISCARD },
Blue Swirl660f11b2009-07-31 21:16:51 +0000348 { "shared", 1, NULL, 'e' },
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000349 { "format", 1, NULL, 'f' },
Blue Swirl660f11b2009-07-31 21:16:51 +0000350 { "persistent", 0, NULL, 't' },
351 { "verbose", 0, NULL, 'v' },
352 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000353 };
354 int ch;
355 int opt_ind = 0;
356 int li;
357 char *end;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200358 int flags = BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000359 int partition = -1;
thscd831bd2008-07-03 10:23:51 +0000360 int ret;
ths3b05a8e2008-07-03 12:45:02 +0000361 int fd;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200362 bool seen_cache = false;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100363 bool seen_discard = false;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200364#ifdef CONFIG_LINUX_AIO
365 bool seen_aio = false;
366#endif
Paolo Bonzinia517e882011-11-04 15:51:21 +0100367 pthread_t client_thread;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000368 const char *fmt = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200369 Error *local_err = NULL;
bellard7a5ca862008-05-27 21:13:40 +0000370
Paolo Bonzinia517e882011-11-04 15:51:21 +0100371 /* 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 */
Paolo Bonzinibb345112011-11-04 15:51:19 +0100374 struct sigaction sa_sigterm;
Paolo Bonzinibb345112011-11-04 15:51:19 +0100375 memset(&sa_sigterm, 0, sizeof(sa_sigterm));
376 sa_sigterm.sa_handler = termsig_handler;
377 sigaction(SIGTERM, &sa_sigterm, NULL);
378
bellard7a5ca862008-05-27 21:13:40 +0000379 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
380 switch (ch) {
381 case 's':
ths2f726482008-07-03 11:47:46 +0000382 flags |= BDRV_O_SNAPSHOT;
383 break;
384 case 'n':
Paolo Bonzini39a52352012-07-18 14:57:15 +0200385 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 }
bellard7a5ca862008-05-27 21:13:40 +0000395 break;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200396#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
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100411 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;
bellard7a5ca862008-05-27 21:13:40 +0000420 case 'b':
421 bindto = optarg;
422 break;
423 case 'p':
424 li = strtol(optarg, &end, 0);
425 if (*end) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900426 errx(EXIT_FAILURE, "Invalid port `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000427 }
428 if (li < 1 || li > 65535) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900429 errx(EXIT_FAILURE, "Port out of range `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000430 }
431 port = (uint16_t)li;
432 break;
433 case 'o':
434 dev_offset = strtoll (optarg, &end, 0);
435 if (*end) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900436 errx(EXIT_FAILURE, "Invalid offset `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000437 }
438 if (dev_offset < 0) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900439 errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000440 }
441 break;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800442 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 */
bellard7a5ca862008-05-27 21:13:40 +0000453 case 'r':
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200454 nbdflags |= NBD_FLAG_READ_ONLY;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200455 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000456 break;
457 case 'P':
458 partition = strtol(optarg, &end, 0);
459 if (*end)
Ryota Ozakib6353be2010-03-20 15:23:23 +0900460 errx(EXIT_FAILURE, "Invalid partition `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000461 if (partition < 1 || partition > 8)
Ryota Ozakib6353be2010-03-20 15:23:23 +0900462 errx(EXIT_FAILURE, "Invalid partition %d", partition);
bellard7a5ca862008-05-27 21:13:40 +0000463 break;
thscd831bd2008-07-03 10:23:51 +0000464 case 'k':
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100465 sockpath = optarg;
466 if (sockpath[0] != '/')
Ryota Ozakib6353be2010-03-20 15:23:23 +0900467 errx(EXIT_FAILURE, "socket path must be absolute\n");
thscd831bd2008-07-03 10:23:51 +0000468 break;
469 case 'd':
470 disconnect = true;
471 break;
472 case 'c':
473 device = optarg;
474 break;
ths3b05a8e2008-07-03 12:45:02 +0000475 case 'e':
476 shared = strtol(optarg, &end, 0);
477 if (*end) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900478 errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg);
ths3b05a8e2008-07-03 12:45:02 +0000479 }
480 if (shared < 1) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900481 errx(EXIT_FAILURE, "Shared device number must be greater than 0\n");
ths3b05a8e2008-07-03 12:45:02 +0000482 }
483 break;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000484 case 'f':
485 fmt = optarg;
486 break;
ths75818252008-07-03 13:41:03 +0000487 case 't':
488 persistent = 1;
489 break;
bellard7a5ca862008-05-27 21:13:40 +0000490 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 '?':
Ryota Ozakib6353be2010-03-20 15:23:23 +0900502 errx(EXIT_FAILURE, "Try `%s --help' for more information.",
bellard7a5ca862008-05-27 21:13:40 +0000503 argv[0]);
504 }
505 }
506
507 if ((argc - optind) != 1) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900508 errx(EXIT_FAILURE, "Invalid number of argument.\n"
bellard7a5ca862008-05-27 21:13:40 +0000509 "Try `%s --help' for more information.",
510 argv[0]);
511 }
512
thscd831bd2008-07-03 10:23:51 +0000513 if (disconnect) {
514 fd = open(argv[optind], O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100515 if (fd < 0) {
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900516 err(EXIT_FAILURE, "Cannot open %s", argv[optind]);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100517 }
thscd831bd2008-07-03 10:23:51 +0000518 nbd_disconnect(fd);
519
520 close(fd);
521
522 printf("%s disconnected\n", argv[optind]);
523
524 return 0;
525 }
526
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100527 if (device && !verbose) {
528 int stderr_fd[2];
529 pid_t pid;
530 int ret;
531
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100532 if (qemu_pipe(stderr_fd) < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100533 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]);
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400542 ret = qemu_daemon(1, 0);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100543
544 /* Temporarily redirect stderr to the parent's pipe... */
545 dup2(stderr_fd[1], STDERR_FILENO);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100546 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100547 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);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100564 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100565 exit(EXIT_FAILURE);
566 }
567 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100568 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100569 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
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100579 if (device != NULL && sockpath == NULL) {
580 sockpath = g_malloc(128);
581 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
thscd831bd2008-07-03 10:23:51 +0000582 }
583
Paolo Bonzini7e7f4a02012-11-03 18:06:26 +0100584 qemu_init_main_loop();
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100585 bdrv_init();
586 atexit(bdrv_close_all);
587
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000588 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
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100597 bs = bdrv_new("hda");
598 srcpath = argv[optind];
Max Reitz34b5d2c2013-09-05 14:45:29 +0200599 ret = bdrv_open(bs, srcpath, NULL, flags, drv, &local_err);
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000600 if (ret < 0) {
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100601 errno = -ret;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200602 err(EXIT_FAILURE, "Failed to bdrv_open '%s': %s", argv[optind],
603 error_get_pretty(local_err));
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100604 }
605
Wenchao Xia8c116b02013-12-04 17:10:55 +0800606 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
Paolo Bonzini38ceff02012-03-12 16:17:27 +0100622 fd_size = bdrv_getlength(bs);
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100623
Paolo Bonzini185b4332012-03-05 08:56:10 +0100624 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 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100630 }
631
Paolo Bonzini7860a382012-09-18 13:31:56 +0200632 exp = nbd_export_new(bs, dev_offset, fd_size, nbdflags, nbd_export_closed);
ths3b05a8e2008-07-03 12:45:02 +0000633
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100634 if (sockpath) {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200635 fd = unix_socket_incoming(sockpath);
thscd831bd2008-07-03 10:23:51 +0000636 } else {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200637 fd = tcp_socket_incoming(bindto, port);
thscd831bd2008-07-03 10:23:51 +0000638 }
639
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100640 if (fd < 0) {
bellard7a5ca862008-05-27 21:13:40 +0000641 return 1;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200642 }
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100643
644 if (device) {
645 int ret;
646
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100647 ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100648 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
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200657 qemu_set_fd_handler2(fd, nbd_can_accept, nbd_accept, NULL,
658 (void *)(uintptr_t)fd);
bellard7a5ca862008-05-27 21:13:40 +0000659
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400660 /* 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
Paolo Bonzini7860a382012-09-18 13:31:56 +0200666 state = RUNNING;
ths3b05a8e2008-07-03 12:45:02 +0000667 do {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200668 main_loop_wait(false);
Paolo Bonzini7860a382012-09-18 13:31:56 +0200669 if (state == TERMINATE) {
670 state = TERMINATING;
671 nbd_export_close(exp);
672 nbd_export_put(exp);
673 exp = NULL;
674 }
675 } while (state != TERMINATED);
ths3b05a8e2008-07-03 12:45:02 +0000676
Paolo Bonzinia4aab7b2012-08-22 18:50:30 +0200677 bdrv_close(bs);
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100678 if (sockpath) {
679 unlink(sockpath);
680 }
bellard7a5ca862008-05-27 21:13:40 +0000681
Wenchao Xia8c116b02013-12-04 17:10:55 +0800682 if (sn_opts) {
683 qemu_opts_del(sn_opts);
684 }
685
Paolo Bonzinia517e882011-11-04 15:51:21 +0100686 if (device) {
687 void *ret;
688 pthread_join(client_thread, &ret);
689 exit(ret != NULL);
690 } else {
691 exit(EXIT_SUCCESS);
692 }
bellard7a5ca862008-05-27 21:13:40 +0000693}