blob: 626e5844f9bd167dda5a8f46722c1c4c8b0572de [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"
Paolo Bonzini537b41f2014-02-17 14:43:51 +010023#include "qemu/sockets.h"
24#include "qemu/error-report.h"
Wenchao Xia8c116b02013-12-04 17:10:55 +080025#include "block/snapshot.h"
bellard7a5ca862008-05-27 21:13:40 +000026
bellard7a5ca862008-05-27 21:13:40 +000027#include <stdarg.h>
28#include <stdio.h>
29#include <getopt.h>
30#include <err.h>
thscd831bd2008-07-03 10:23:51 +000031#include <sys/types.h>
bellard7a5ca862008-05-27 21:13:40 +000032#include <sys/socket.h>
33#include <netinet/in.h>
34#include <netinet/tcp.h>
35#include <arpa/inet.h>
thscd831bd2008-07-03 10:23:51 +000036#include <signal.h>
Blue Swirl2bff4b62009-12-23 15:34:04 +000037#include <libgen.h>
Paolo Bonzinia517e882011-11-04 15:51:21 +010038#include <pthread.h>
thscd831bd2008-07-03 10:23:51 +000039
Paolo Bonzinided9d2d2013-02-08 14:06:13 +010040#define SOCKET_PATH "/var/lock/qemu-nbd-%s"
41#define QEMU_NBD_OPT_CACHE 1
42#define QEMU_NBD_OPT_AIO 2
43#define QEMU_NBD_OPT_DISCARD 3
bellard7a5ca862008-05-27 21:13:40 +000044
Paolo Bonziniaf49bbb2011-09-19 14:03:37 +020045static NBDExport *exp;
blueswir1b1d8e522008-10-26 13:43:07 +000046static int verbose;
Paolo Bonzinia517e882011-11-04 15:51:21 +010047static char *srcpath;
48static char *sockpath;
Paolo Bonzini7860a382012-09-18 13:31:56 +020049static int persistent = 0;
50static enum { RUNNING, TERMINATE, TERMINATING, TERMINATED } state;
Paolo Bonzinia61c6782011-09-12 17:28:11 +020051static int shared = 1;
52static int nb_fds;
bellard7a5ca862008-05-27 21:13:40 +000053
54static void usage(const char *name)
55{
Paolo Bonzinib033cd82012-07-18 14:50:52 +020056 (printf) (
bellard7a5ca862008-05-27 21:13:40 +000057"Usage: %s [OPTIONS] FILE\n"
58"QEMU Disk Network Block Device Server\n"
59"\n"
bellard7a5ca862008-05-27 21:13:40 +000060" -h, --help display this help and exit\n"
61" -V, --version output version information and exit\n"
62"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020063"Connection properties:\n"
64" -p, --port=PORT port to listen on (default `%d')\n"
65" -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
66" -k, --socket=PATH path to the unix socket\n"
67" (default '"SOCKET_PATH"')\n"
68" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
69" -t, --persistent don't exit on the last connection\n"
70" -v, --verbose display extra debugging information\n"
71"\n"
72"Exposing part of the image:\n"
73" -o, --offset=OFFSET offset into the image\n"
74" -P, --partition=NUM only expose partition NUM\n"
75"\n"
76#ifdef __linux__
77"Kernel NBD client support:\n"
78" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
79" -d, --disconnect disconnect the specified device\n"
80"\n"
81#endif
82"\n"
83"Block device options:\n"
Wenchao Xia4323fdc2013-12-04 17:10:59 +080084" -f, --format=FORMAT set image format (raw, qcow2, ...)\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020085" -r, --read-only export read-only\n"
Wenchao Xia8c116b02013-12-04 17:10:55 +080086" -s, --snapshot use FILE as an external snapshot, create a temporary\n"
87" file with backing_file=FILE, redirect the write to\n"
88" the temporary one\n"
89" -l, --load-snapshot=SNAPSHOT_PARAM\n"
90" load an internal snapshot inside FILE and export it\n"
91" as an read-only device, SNAPSHOT_PARAM format is\n"
92" 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
93" '[ID_OR_NAME]'\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020094" -n, --nocache disable host cache\n"
Paolo Bonzini39a52352012-07-18 14:57:15 +020095" --cache=MODE set cache mode (none, writeback, ...)\n"
96#ifdef CONFIG_LINUX_AIO
97" --aio=MODE set AIO mode (native or threads)\n"
98#endif
Paolo Bonzinib033cd82012-07-18 14:50:52 +020099"\n"
100"Report bugs to <qemu-devel@nongnu.org>\n"
Laurent Vivierc2e28722010-09-17 20:37:25 +0200101 , name, NBD_DEFAULT_PORT, "DEVICE");
bellard7a5ca862008-05-27 21:13:40 +0000102}
103
104static void version(const char *name)
105{
106 printf(
ths315bc7a2008-07-18 18:06:23 +0000107"%s version 0.0.1\n"
bellard7a5ca862008-05-27 21:13:40 +0000108"Written by Anthony Liguori.\n"
109"\n"
110"Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
111"This is free software; see the source for copying conditions. There is NO\n"
112"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
ths315bc7a2008-07-18 18:06:23 +0000113 , name);
bellard7a5ca862008-05-27 21:13:40 +0000114}
115
116struct partition_record
117{
118 uint8_t bootable;
119 uint8_t start_head;
120 uint32_t start_cylinder;
121 uint8_t start_sector;
122 uint8_t system;
123 uint8_t end_head;
124 uint8_t end_cylinder;
125 uint8_t end_sector;
126 uint32_t start_sector_abs;
127 uint32_t nb_sectors_abs;
128};
129
130static void read_partition(uint8_t *p, struct partition_record *r)
131{
132 r->bootable = p[0];
133 r->start_head = p[1];
134 r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
135 r->start_sector = p[2] & 0x3f;
136 r->system = p[4];
137 r->end_head = p[5];
138 r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
139 r->end_sector = p[6] & 0x3f;
140 r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24;
141 r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24;
142}
143
144static int find_partition(BlockDriverState *bs, int partition,
145 off_t *offset, off_t *size)
146{
147 struct partition_record mbr[4];
148 uint8_t data[512];
149 int i;
150 int ext_partnum = 4;
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900151 int ret;
bellard7a5ca862008-05-27 21:13:40 +0000152
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900153 if ((ret = bdrv_read(bs, 0, data, 1)) < 0) {
154 errno = -ret;
155 err(EXIT_FAILURE, "error while reading");
156 }
bellard7a5ca862008-05-27 21:13:40 +0000157
158 if (data[510] != 0x55 || data[511] != 0xaa) {
Paolo Bonzini185b4332012-03-05 08:56:10 +0100159 return -EINVAL;
bellard7a5ca862008-05-27 21:13:40 +0000160 }
161
162 for (i = 0; i < 4; i++) {
163 read_partition(&data[446 + 16 * i], &mbr[i]);
164
165 if (!mbr[i].nb_sectors_abs)
166 continue;
167
168 if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
169 struct partition_record ext[4];
170 uint8_t data1[512];
171 int j;
172
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900173 if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) {
174 errno = -ret;
175 err(EXIT_FAILURE, "error while reading");
176 }
bellard7a5ca862008-05-27 21:13:40 +0000177
178 for (j = 0; j < 4; j++) {
179 read_partition(&data1[446 + 16 * j], &ext[j]);
180 if (!ext[j].nb_sectors_abs)
181 continue;
182
183 if ((ext_partnum + j + 1) == partition) {
184 *offset = (uint64_t)ext[j].start_sector_abs << 9;
185 *size = (uint64_t)ext[j].nb_sectors_abs << 9;
186 return 0;
187 }
188 }
189 ext_partnum += 4;
190 } else if ((i + 1) == partition) {
191 *offset = (uint64_t)mbr[i].start_sector_abs << 9;
192 *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
193 return 0;
194 }
195 }
196
Paolo Bonzini185b4332012-03-05 08:56:10 +0100197 return -ENOENT;
bellard7a5ca862008-05-27 21:13:40 +0000198}
199
Paolo Bonzinibb345112011-11-04 15:51:19 +0100200static void termsig_handler(int signum)
201{
Paolo Bonzini7860a382012-09-18 13:31:56 +0200202 state = TERMINATE;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200203 qemu_notify_event();
Paolo Bonzinibb345112011-11-04 15:51:19 +0100204}
205
Paolo Bonzini537b41f2014-02-17 14:43:51 +0100206static void combine_addr(char *buf, size_t len, const char* address,
207 uint16_t port)
208{
209 /* If the address-part contains a colon, it's an IPv6 IP so needs [] */
210 if (strstr(address, ":")) {
211 snprintf(buf, len, "[%s]:%u", address, port);
212 } else {
213 snprintf(buf, len, "%s:%u", address, port);
214 }
215}
216
217static int tcp_socket_incoming(const char *address, uint16_t port)
218{
219 char address_and_port[128];
220 Error *local_err = NULL;
221
222 combine_addr(address_and_port, 128, address, port);
223 int fd = inet_listen(address_and_port, NULL, 0, SOCK_STREAM, 0, &local_err);
224
225 if (local_err != NULL) {
Markus Armbruster3775ec62014-05-16 11:00:10 +0200226 error_report("%s", error_get_pretty(local_err));
Paolo Bonzini537b41f2014-02-17 14:43:51 +0100227 error_free(local_err);
228 }
229 return fd;
230}
231
232static int unix_socket_incoming(const char *path)
233{
234 Error *local_err = NULL;
235 int fd = unix_listen(path, NULL, 0, &local_err);
236
237 if (local_err != NULL) {
Markus Armbruster3775ec62014-05-16 11:00:10 +0200238 error_report("%s", error_get_pretty(local_err));
Paolo Bonzini537b41f2014-02-17 14:43:51 +0100239 error_free(local_err);
240 }
241 return fd;
242}
243
244static int unix_socket_outgoing(const char *path)
245{
246 Error *local_err = NULL;
247 int fd = unix_connect(path, &local_err);
248
249 if (local_err != NULL) {
Markus Armbruster3775ec62014-05-16 11:00:10 +0200250 error_report("%s", error_get_pretty(local_err));
Paolo Bonzini537b41f2014-02-17 14:43:51 +0100251 error_free(local_err);
252 }
253 return fd;
254}
255
Paolo Bonzinia517e882011-11-04 15:51:21 +0100256static void *show_parts(void *arg)
thscd831bd2008-07-03 10:23:51 +0000257{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100258 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100259 int nbd;
thscd831bd2008-07-03 10:23:51 +0000260
Paolo Bonzinia517e882011-11-04 15:51:21 +0100261 /* linux just needs an open() to trigger
262 * the partition table update
263 * but remember to load the module with max_part != 0 :
264 * modprobe nbd max_part=63
265 */
266 nbd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100267 if (nbd >= 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100268 close(nbd);
thscd831bd2008-07-03 10:23:51 +0000269 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100270 return NULL;
271}
272
273static void *nbd_client_thread(void *arg)
274{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100275 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100276 off_t size;
277 size_t blocksize;
278 uint32_t nbdflags;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100279 int fd, sock;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100280 int ret;
281 pthread_t show_parts_thread;
282
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000283 sock = unix_socket_outgoing(sockpath);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100284 if (sock < 0) {
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000285 goto out;
286 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100287
288 ret = nbd_receive_negotiate(sock, NULL, &nbdflags,
289 &size, &blocksize);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100290 if (ret < 0) {
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100291 goto out_socket;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100292 }
293
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100294 fd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100295 if (fd < 0) {
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100296 /* Linux-only, we can use %m in printf. */
Hani Benhabiles5672ee52014-05-13 00:35:16 +0100297 fprintf(stderr, "Failed to open %s: %m\n", device);
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100298 goto out_socket;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100299 }
300
Paolo Bonzinia517e882011-11-04 15:51:21 +0100301 ret = nbd_init(fd, sock, nbdflags, size, blocksize);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100302 if (ret < 0) {
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100303 goto out_fd;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100304 }
305
306 /* update partition table */
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100307 pthread_create(&show_parts_thread, NULL, show_parts, device);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100308
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100309 if (verbose) {
310 fprintf(stderr, "NBD device %s is now connected to %s\n",
311 device, srcpath);
312 } else {
313 /* Close stderr so that the qemu-nbd process exits. */
314 dup2(STDOUT_FILENO, STDERR_FILENO);
315 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100316
317 ret = nbd_client(fd);
318 if (ret) {
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100319 goto out_fd;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100320 }
321 close(fd);
322 kill(getpid(), SIGTERM);
323 return (void *) EXIT_SUCCESS;
324
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100325out_fd:
326 close(fd);
327out_socket:
328 closesocket(sock);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100329out:
330 kill(getpid(), SIGTERM);
331 return (void *) EXIT_FAILURE;
thscd831bd2008-07-03 10:23:51 +0000332}
333
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200334static int nbd_can_accept(void *opaque)
335{
336 return nb_fds < shared;
337}
338
Paolo Bonzini7860a382012-09-18 13:31:56 +0200339static void nbd_export_closed(NBDExport *exp)
340{
341 assert(state == TERMINATING);
342 state = TERMINATED;
343}
344
Paolo Bonzini1743b512011-09-19 14:33:23 +0200345static void nbd_client_closed(NBDClient *client)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200346{
Paolo Bonzini1743b512011-09-19 14:33:23 +0200347 nb_fds--;
Paolo Bonzini7860a382012-09-18 13:31:56 +0200348 if (nb_fds == 0 && !persistent && state == RUNNING) {
349 state = TERMINATE;
350 }
Paolo Bonzini1743b512011-09-19 14:33:23 +0200351 qemu_notify_event();
Paolo Bonzini7860a382012-09-18 13:31:56 +0200352 nbd_client_put(client);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200353}
354
355static void nbd_accept(void *opaque)
356{
357 int server_fd = (uintptr_t) opaque;
358 struct sockaddr_in addr;
359 socklen_t addr_len = sizeof(addr);
360
361 int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100362 if (fd < 0) {
363 perror("accept");
364 return;
365 }
366
Paolo Bonzini7860a382012-09-18 13:31:56 +0200367 if (state >= TERMINATE) {
368 close(fd);
369 return;
370 }
371
Hani Benhabiles36af5992014-05-13 00:35:15 +0100372 if (nbd_client_new(exp, fd, nbd_client_closed)) {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200373 nb_fds++;
Hani Benhabiles36af5992014-05-13 00:35:15 +0100374 } else {
Hani Benhabiles27e5eae2014-05-31 22:39:42 +0100375 shutdown(fd, 2);
Hani Benhabiles36af5992014-05-13 00:35:15 +0100376 close(fd);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200377 }
378}
379
bellard7a5ca862008-05-27 21:13:40 +0000380int main(int argc, char **argv)
381{
382 BlockDriverState *bs;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000383 BlockDriver *drv;
bellard7a5ca862008-05-27 21:13:40 +0000384 off_t dev_offset = 0;
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200385 uint32_t nbdflags = 0;
thscd831bd2008-07-03 10:23:51 +0000386 bool disconnect = false;
bellard7a5ca862008-05-27 21:13:40 +0000387 const char *bindto = "0.0.0.0";
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100388 char *device = NULL;
Laurent Vivierc2e28722010-09-17 20:37:25 +0200389 int port = NBD_DEFAULT_PORT;
bellard7a5ca862008-05-27 21:13:40 +0000390 off_t fd_size;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800391 QemuOpts *sn_opts = NULL;
392 const char *sn_id_or_name = NULL;
393 const char *sopt = "hVb:o:p:rsnP:c:dvk:e:f:tl:";
bellard7a5ca862008-05-27 21:13:40 +0000394 struct option lopt[] = {
Blue Swirl660f11b2009-07-31 21:16:51 +0000395 { "help", 0, NULL, 'h' },
396 { "version", 0, NULL, 'V' },
397 { "bind", 1, NULL, 'b' },
398 { "port", 1, NULL, 'p' },
399 { "socket", 1, NULL, 'k' },
400 { "offset", 1, NULL, 'o' },
401 { "read-only", 0, NULL, 'r' },
402 { "partition", 1, NULL, 'P' },
403 { "connect", 1, NULL, 'c' },
404 { "disconnect", 0, NULL, 'd' },
405 { "snapshot", 0, NULL, 's' },
Wenchao Xia8c116b02013-12-04 17:10:55 +0800406 { "load-snapshot", 1, NULL, 'l' },
Blue Swirl660f11b2009-07-31 21:16:51 +0000407 { "nocache", 0, NULL, 'n' },
Paolo Bonzini39a52352012-07-18 14:57:15 +0200408 { "cache", 1, NULL, QEMU_NBD_OPT_CACHE },
409#ifdef CONFIG_LINUX_AIO
410 { "aio", 1, NULL, QEMU_NBD_OPT_AIO },
411#endif
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100412 { "discard", 1, NULL, QEMU_NBD_OPT_DISCARD },
Blue Swirl660f11b2009-07-31 21:16:51 +0000413 { "shared", 1, NULL, 'e' },
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000414 { "format", 1, NULL, 'f' },
Blue Swirl660f11b2009-07-31 21:16:51 +0000415 { "persistent", 0, NULL, 't' },
416 { "verbose", 0, NULL, 'v' },
417 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000418 };
419 int ch;
420 int opt_ind = 0;
421 int li;
422 char *end;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200423 int flags = BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000424 int partition = -1;
thscd831bd2008-07-03 10:23:51 +0000425 int ret;
ths3b05a8e2008-07-03 12:45:02 +0000426 int fd;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200427 bool seen_cache = false;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100428 bool seen_discard = false;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200429#ifdef CONFIG_LINUX_AIO
430 bool seen_aio = false;
431#endif
Paolo Bonzinia517e882011-11-04 15:51:21 +0100432 pthread_t client_thread;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000433 const char *fmt = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200434 Error *local_err = NULL;
bellard7a5ca862008-05-27 21:13:40 +0000435
Paolo Bonzinia517e882011-11-04 15:51:21 +0100436 /* The client thread uses SIGTERM to interrupt the server. A signal
437 * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
438 */
Paolo Bonzinibb345112011-11-04 15:51:19 +0100439 struct sigaction sa_sigterm;
Paolo Bonzinibb345112011-11-04 15:51:19 +0100440 memset(&sa_sigterm, 0, sizeof(sa_sigterm));
441 sa_sigterm.sa_handler = termsig_handler;
442 sigaction(SIGTERM, &sa_sigterm, NULL);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800443 qemu_init_exec_dir(argv[0]);
Paolo Bonzinibb345112011-11-04 15:51:19 +0100444
bellard7a5ca862008-05-27 21:13:40 +0000445 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
446 switch (ch) {
447 case 's':
ths2f726482008-07-03 11:47:46 +0000448 flags |= BDRV_O_SNAPSHOT;
449 break;
450 case 'n':
Paolo Bonzini39a52352012-07-18 14:57:15 +0200451 optarg = (char *) "none";
452 /* fallthrough */
453 case QEMU_NBD_OPT_CACHE:
454 if (seen_cache) {
455 errx(EXIT_FAILURE, "-n and --cache can only be specified once");
456 }
457 seen_cache = true;
458 if (bdrv_parse_cache_flags(optarg, &flags) == -1) {
459 errx(EXIT_FAILURE, "Invalid cache mode `%s'", optarg);
460 }
bellard7a5ca862008-05-27 21:13:40 +0000461 break;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200462#ifdef CONFIG_LINUX_AIO
463 case QEMU_NBD_OPT_AIO:
464 if (seen_aio) {
465 errx(EXIT_FAILURE, "--aio can only be specified once");
466 }
467 seen_aio = true;
468 if (!strcmp(optarg, "native")) {
469 flags |= BDRV_O_NATIVE_AIO;
470 } else if (!strcmp(optarg, "threads")) {
471 /* this is the default */
472 } else {
473 errx(EXIT_FAILURE, "invalid aio mode `%s'", optarg);
474 }
475 break;
476#endif
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100477 case QEMU_NBD_OPT_DISCARD:
478 if (seen_discard) {
479 errx(EXIT_FAILURE, "--discard can only be specified once");
480 }
481 seen_discard = true;
482 if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
483 errx(EXIT_FAILURE, "Invalid discard mode `%s'", optarg);
484 }
485 break;
bellard7a5ca862008-05-27 21:13:40 +0000486 case 'b':
487 bindto = optarg;
488 break;
489 case 'p':
490 li = strtol(optarg, &end, 0);
491 if (*end) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900492 errx(EXIT_FAILURE, "Invalid port `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000493 }
494 if (li < 1 || li > 65535) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900495 errx(EXIT_FAILURE, "Port out of range `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000496 }
497 port = (uint16_t)li;
498 break;
499 case 'o':
500 dev_offset = strtoll (optarg, &end, 0);
501 if (*end) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900502 errx(EXIT_FAILURE, "Invalid offset `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000503 }
504 if (dev_offset < 0) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900505 errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000506 }
507 break;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800508 case 'l':
509 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
510 sn_opts = qemu_opts_parse(&internal_snapshot_opts, optarg, 0);
511 if (!sn_opts) {
512 errx(EXIT_FAILURE, "Failed in parsing snapshot param `%s'",
513 optarg);
514 }
515 } else {
516 sn_id_or_name = optarg;
517 }
518 /* fall through */
bellard7a5ca862008-05-27 21:13:40 +0000519 case 'r':
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200520 nbdflags |= NBD_FLAG_READ_ONLY;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200521 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000522 break;
523 case 'P':
524 partition = strtol(optarg, &end, 0);
525 if (*end)
Ryota Ozakib6353be2010-03-20 15:23:23 +0900526 errx(EXIT_FAILURE, "Invalid partition `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000527 if (partition < 1 || partition > 8)
Ryota Ozakib6353be2010-03-20 15:23:23 +0900528 errx(EXIT_FAILURE, "Invalid partition %d", partition);
bellard7a5ca862008-05-27 21:13:40 +0000529 break;
thscd831bd2008-07-03 10:23:51 +0000530 case 'k':
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100531 sockpath = optarg;
532 if (sockpath[0] != '/')
Ryota Ozakib6353be2010-03-20 15:23:23 +0900533 errx(EXIT_FAILURE, "socket path must be absolute\n");
thscd831bd2008-07-03 10:23:51 +0000534 break;
535 case 'd':
536 disconnect = true;
537 break;
538 case 'c':
539 device = optarg;
540 break;
ths3b05a8e2008-07-03 12:45:02 +0000541 case 'e':
542 shared = strtol(optarg, &end, 0);
543 if (*end) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900544 errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg);
ths3b05a8e2008-07-03 12:45:02 +0000545 }
546 if (shared < 1) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900547 errx(EXIT_FAILURE, "Shared device number must be greater than 0\n");
ths3b05a8e2008-07-03 12:45:02 +0000548 }
549 break;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000550 case 'f':
551 fmt = optarg;
552 break;
ths75818252008-07-03 13:41:03 +0000553 case 't':
554 persistent = 1;
555 break;
bellard7a5ca862008-05-27 21:13:40 +0000556 case 'v':
557 verbose = 1;
558 break;
559 case 'V':
560 version(argv[0]);
561 exit(0);
562 break;
563 case 'h':
564 usage(argv[0]);
565 exit(0);
566 break;
567 case '?':
Ryota Ozakib6353be2010-03-20 15:23:23 +0900568 errx(EXIT_FAILURE, "Try `%s --help' for more information.",
bellard7a5ca862008-05-27 21:13:40 +0000569 argv[0]);
570 }
571 }
572
573 if ((argc - optind) != 1) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900574 errx(EXIT_FAILURE, "Invalid number of argument.\n"
bellard7a5ca862008-05-27 21:13:40 +0000575 "Try `%s --help' for more information.",
576 argv[0]);
577 }
578
thscd831bd2008-07-03 10:23:51 +0000579 if (disconnect) {
580 fd = open(argv[optind], O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100581 if (fd < 0) {
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900582 err(EXIT_FAILURE, "Cannot open %s", argv[optind]);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100583 }
thscd831bd2008-07-03 10:23:51 +0000584 nbd_disconnect(fd);
585
586 close(fd);
587
588 printf("%s disconnected\n", argv[optind]);
589
590 return 0;
591 }
592
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100593 if (device && !verbose) {
594 int stderr_fd[2];
595 pid_t pid;
596 int ret;
597
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100598 if (qemu_pipe(stderr_fd) < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100599 err(EXIT_FAILURE, "Error setting up communication pipe");
600 }
601
602 /* Now daemonize, but keep a communication channel open to
603 * print errors and exit with the proper status code.
604 */
605 pid = fork();
606 if (pid == 0) {
607 close(stderr_fd[0]);
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400608 ret = qemu_daemon(1, 0);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100609
610 /* Temporarily redirect stderr to the parent's pipe... */
611 dup2(stderr_fd[1], STDERR_FILENO);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100612 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100613 err(EXIT_FAILURE, "Failed to daemonize");
614 }
615
616 /* ... close the descriptor we inherited and go on. */
617 close(stderr_fd[1]);
618 } else {
619 bool errors = false;
620 char *buf;
621
622 /* In the parent. Print error messages from the child until
623 * it closes the pipe.
624 */
625 close(stderr_fd[1]);
626 buf = g_malloc(1024);
627 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
628 errors = true;
629 ret = qemu_write_full(STDERR_FILENO, buf, ret);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100630 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100631 exit(EXIT_FAILURE);
632 }
633 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100634 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100635 err(EXIT_FAILURE, "Cannot read from daemon");
636 }
637
638 /* Usually the daemon should not print any message.
639 * Exit with zero status in that case.
640 */
641 exit(errors);
642 }
643 }
644
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100645 if (device != NULL && sockpath == NULL) {
646 sockpath = g_malloc(128);
647 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
thscd831bd2008-07-03 10:23:51 +0000648 }
649
Paolo Bonzini7e7f4a02012-11-03 18:06:26 +0100650 qemu_init_main_loop();
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100651 bdrv_init();
652 atexit(bdrv_close_all);
653
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000654 if (fmt) {
655 drv = bdrv_find_format(fmt);
656 if (!drv) {
657 errx(EXIT_FAILURE, "Unknown file format '%s'", fmt);
658 }
659 } else {
660 drv = NULL;
661 }
662
Kevin Wolf98522f62014-04-17 13:16:01 +0200663 bs = bdrv_new("hda", &error_abort);
664
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100665 srcpath = argv[optind];
Max Reitzddf56362014-02-18 18:33:06 +0100666 ret = bdrv_open(&bs, srcpath, NULL, NULL, flags, drv, &local_err);
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000667 if (ret < 0) {
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100668 errno = -ret;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200669 err(EXIT_FAILURE, "Failed to bdrv_open '%s': %s", argv[optind],
670 error_get_pretty(local_err));
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100671 }
672
Wenchao Xia8c116b02013-12-04 17:10:55 +0800673 if (sn_opts) {
674 ret = bdrv_snapshot_load_tmp(bs,
675 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
676 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
677 &local_err);
678 } else if (sn_id_or_name) {
679 ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
680 &local_err);
681 }
682 if (ret < 0) {
683 errno = -ret;
684 err(EXIT_FAILURE,
685 "Failed to load snapshot: %s",
686 error_get_pretty(local_err));
687 }
688
Paolo Bonzini38ceff02012-03-12 16:17:27 +0100689 fd_size = bdrv_getlength(bs);
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100690
Paolo Bonzini185b4332012-03-05 08:56:10 +0100691 if (partition != -1) {
692 ret = find_partition(bs, partition, &dev_offset, &fd_size);
693 if (ret < 0) {
694 errno = -ret;
695 err(EXIT_FAILURE, "Could not find partition %d", partition);
696 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100697 }
698
Paolo Bonzini7860a382012-09-18 13:31:56 +0200699 exp = nbd_export_new(bs, dev_offset, fd_size, nbdflags, nbd_export_closed);
ths3b05a8e2008-07-03 12:45:02 +0000700
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100701 if (sockpath) {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200702 fd = unix_socket_incoming(sockpath);
thscd831bd2008-07-03 10:23:51 +0000703 } else {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200704 fd = tcp_socket_incoming(bindto, port);
thscd831bd2008-07-03 10:23:51 +0000705 }
706
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100707 if (fd < 0) {
bellard7a5ca862008-05-27 21:13:40 +0000708 return 1;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200709 }
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100710
711 if (device) {
712 int ret;
713
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100714 ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +0100715 if (ret != 0) {
716 errx(EXIT_FAILURE, "Failed to create client thread: %s",
717 strerror(ret));
718 }
719 } else {
720 /* Shut up GCC warnings. */
721 memset(&client_thread, 0, sizeof(client_thread));
722 }
723
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200724 qemu_set_fd_handler2(fd, nbd_can_accept, nbd_accept, NULL,
725 (void *)(uintptr_t)fd);
bellard7a5ca862008-05-27 21:13:40 +0000726
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400727 /* now when the initialization is (almost) complete, chdir("/")
728 * to free any busy filesystems */
729 if (chdir("/") < 0) {
730 err(EXIT_FAILURE, "Could not chdir to root directory");
731 }
732
Paolo Bonzini7860a382012-09-18 13:31:56 +0200733 state = RUNNING;
ths3b05a8e2008-07-03 12:45:02 +0000734 do {
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200735 main_loop_wait(false);
Paolo Bonzini7860a382012-09-18 13:31:56 +0200736 if (state == TERMINATE) {
737 state = TERMINATING;
738 nbd_export_close(exp);
739 nbd_export_put(exp);
740 exp = NULL;
741 }
742 } while (state != TERMINATED);
ths3b05a8e2008-07-03 12:45:02 +0000743
Paolo Bonzinia4aab7b2012-08-22 18:50:30 +0200744 bdrv_close(bs);
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100745 if (sockpath) {
746 unlink(sockpath);
747 }
bellard7a5ca862008-05-27 21:13:40 +0000748
Wenchao Xia8c116b02013-12-04 17:10:55 +0800749 if (sn_opts) {
750 qemu_opts_del(sn_opts);
751 }
752
Paolo Bonzinia517e882011-11-04 15:51:21 +0100753 if (device) {
754 void *ret;
755 pthread_join(client_thread, &ret);
756 exit(ret != NULL);
757 } else {
758 exit(EXIT_SUCCESS);
759 }
bellard7a5ca862008-05-27 21:13:40 +0000760}