blob: 4e607cfb61b0d23c996bd0b77d53a55f5395dec9 [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
19#include <qemu-common.h>
20#include "block_int.h"
21#include "nbd.h"
22
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>
thscd831bd2008-07-03 10:23:51 +000034
35#define SOCKET_PATH "/var/lock/qemu-nbd-%s"
bellard7a5ca862008-05-27 21:13:40 +000036
ths2f726482008-07-03 11:47:46 +000037#define NBD_BUFFER_SIZE (1024*1024)
38
blueswir1b1d8e522008-10-26 13:43:07 +000039static int verbose;
bellard7a5ca862008-05-27 21:13:40 +000040
41static void usage(const char *name)
42{
43 printf(
44"Usage: %s [OPTIONS] FILE\n"
45"QEMU Disk Network Block Device Server\n"
46"\n"
47" -p, --port=PORT port to listen on (default `1024')\n"
48" -o, --offset=OFFSET offset into the image\n"
49" -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
thscd831bd2008-07-03 10:23:51 +000050" -k, --socket=PATH path to the unix socket\n"
51" (default '"SOCKET_PATH"')\n"
bellard7a5ca862008-05-27 21:13:40 +000052" -r, --read-only export read-only\n"
53" -P, --partition=NUM only expose partition NUM\n"
ths2f726482008-07-03 11:47:46 +000054" -s, --snapshot use snapshot file\n"
55" -n, --nocache disable host cache\n"
thscd831bd2008-07-03 10:23:51 +000056" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
57" -d, --disconnect disconnect the specified device\n"
ths3b05a8e2008-07-03 12:45:02 +000058" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
ths75818252008-07-03 13:41:03 +000059" -t, --persistent don't exit on the last connection\n"
bellard7a5ca862008-05-27 21:13:40 +000060" -v, --verbose display extra debugging information\n"
61" -h, --help display this help and exit\n"
62" -V, --version output version information and exit\n"
63"\n"
64"Report bugs to <anthony@codemonkey.ws>\n"
thscd831bd2008-07-03 10:23:51 +000065 , name, "DEVICE");
bellard7a5ca862008-05-27 21:13:40 +000066}
67
68static void version(const char *name)
69{
70 printf(
ths315bc7a2008-07-18 18:06:23 +000071"%s version 0.0.1\n"
bellard7a5ca862008-05-27 21:13:40 +000072"Written by Anthony Liguori.\n"
73"\n"
74"Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
75"This is free software; see the source for copying conditions. There is NO\n"
76"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
ths315bc7a2008-07-18 18:06:23 +000077 , name);
bellard7a5ca862008-05-27 21:13:40 +000078}
79
80struct partition_record
81{
82 uint8_t bootable;
83 uint8_t start_head;
84 uint32_t start_cylinder;
85 uint8_t start_sector;
86 uint8_t system;
87 uint8_t end_head;
88 uint8_t end_cylinder;
89 uint8_t end_sector;
90 uint32_t start_sector_abs;
91 uint32_t nb_sectors_abs;
92};
93
94static void read_partition(uint8_t *p, struct partition_record *r)
95{
96 r->bootable = p[0];
97 r->start_head = p[1];
98 r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
99 r->start_sector = p[2] & 0x3f;
100 r->system = p[4];
101 r->end_head = p[5];
102 r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
103 r->end_sector = p[6] & 0x3f;
104 r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24;
105 r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24;
106}
107
108static int find_partition(BlockDriverState *bs, int partition,
109 off_t *offset, off_t *size)
110{
111 struct partition_record mbr[4];
112 uint8_t data[512];
113 int i;
114 int ext_partnum = 4;
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900115 int ret;
bellard7a5ca862008-05-27 21:13:40 +0000116
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900117 if ((ret = bdrv_read(bs, 0, data, 1)) < 0) {
118 errno = -ret;
119 err(EXIT_FAILURE, "error while reading");
120 }
bellard7a5ca862008-05-27 21:13:40 +0000121
122 if (data[510] != 0x55 || data[511] != 0xaa) {
123 errno = -EINVAL;
124 return -1;
125 }
126
127 for (i = 0; i < 4; i++) {
128 read_partition(&data[446 + 16 * i], &mbr[i]);
129
130 if (!mbr[i].nb_sectors_abs)
131 continue;
132
133 if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
134 struct partition_record ext[4];
135 uint8_t data1[512];
136 int j;
137
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900138 if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) {
139 errno = -ret;
140 err(EXIT_FAILURE, "error while reading");
141 }
bellard7a5ca862008-05-27 21:13:40 +0000142
143 for (j = 0; j < 4; j++) {
144 read_partition(&data1[446 + 16 * j], &ext[j]);
145 if (!ext[j].nb_sectors_abs)
146 continue;
147
148 if ((ext_partnum + j + 1) == partition) {
149 *offset = (uint64_t)ext[j].start_sector_abs << 9;
150 *size = (uint64_t)ext[j].nb_sectors_abs << 9;
151 return 0;
152 }
153 }
154 ext_partnum += 4;
155 } else if ((i + 1) == partition) {
156 *offset = (uint64_t)mbr[i].start_sector_abs << 9;
157 *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
158 return 0;
159 }
160 }
161
162 errno = -ENOENT;
163 return -1;
164}
165
thscd831bd2008-07-03 10:23:51 +0000166static void show_parts(const char *device)
167{
168 if (fork() == 0) {
169 int nbd;
170
171 /* linux just needs an open() to trigger
172 * the partition table update
173 * but remember to load the module with max_part != 0 :
174 * modprobe nbd max_part=63
175 */
176 nbd = open(device, O_RDWR);
177 if (nbd != -1)
178 close(nbd);
179 exit(0);
180 }
181}
182
bellard7a5ca862008-05-27 21:13:40 +0000183int main(int argc, char **argv)
184{
185 BlockDriverState *bs;
186 off_t dev_offset = 0;
187 off_t offset = 0;
188 bool readonly = false;
thscd831bd2008-07-03 10:23:51 +0000189 bool disconnect = false;
bellard7a5ca862008-05-27 21:13:40 +0000190 const char *bindto = "0.0.0.0";
191 int port = 1024;
bellard7a5ca862008-05-27 21:13:40 +0000192 struct sockaddr_in addr;
193 socklen_t addr_len = sizeof(addr);
194 off_t fd_size;
thscd831bd2008-07-03 10:23:51 +0000195 char *device = NULL;
196 char *socket = NULL;
197 char sockpath[128];
aliguorid6aa6712009-01-08 19:34:35 +0000198 const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t";
bellard7a5ca862008-05-27 21:13:40 +0000199 struct option lopt[] = {
Blue Swirl660f11b2009-07-31 21:16:51 +0000200 { "help", 0, NULL, 'h' },
201 { "version", 0, NULL, 'V' },
202 { "bind", 1, NULL, 'b' },
203 { "port", 1, NULL, 'p' },
204 { "socket", 1, NULL, 'k' },
205 { "offset", 1, NULL, 'o' },
206 { "read-only", 0, NULL, 'r' },
207 { "partition", 1, NULL, 'P' },
208 { "connect", 1, NULL, 'c' },
209 { "disconnect", 0, NULL, 'd' },
210 { "snapshot", 0, NULL, 's' },
211 { "nocache", 0, NULL, 'n' },
212 { "shared", 1, NULL, 'e' },
213 { "persistent", 0, NULL, 't' },
214 { "verbose", 0, NULL, 'v' },
215 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000216 };
217 int ch;
218 int opt_ind = 0;
219 int li;
220 char *end;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200221 int flags = BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000222 int partition = -1;
thscd831bd2008-07-03 10:23:51 +0000223 int ret;
ths3b05a8e2008-07-03 12:45:02 +0000224 int shared = 1;
ths2f726482008-07-03 11:47:46 +0000225 uint8_t *data;
ths3b05a8e2008-07-03 12:45:02 +0000226 fd_set fds;
227 int *sharing_fds;
228 int fd;
229 int i;
230 int nb_fds = 0;
231 int max_fd;
ths75818252008-07-03 13:41:03 +0000232 int persistent = 0;
bellard7a5ca862008-05-27 21:13:40 +0000233
234 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
235 switch (ch) {
236 case 's':
ths2f726482008-07-03 11:47:46 +0000237 flags |= BDRV_O_SNAPSHOT;
238 break;
239 case 'n':
aliguori9f7965c2008-10-14 14:42:54 +0000240 flags |= BDRV_O_NOCACHE;
bellard7a5ca862008-05-27 21:13:40 +0000241 break;
242 case 'b':
243 bindto = optarg;
244 break;
245 case 'p':
246 li = strtol(optarg, &end, 0);
247 if (*end) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900248 errx(EXIT_FAILURE, "Invalid port `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000249 }
250 if (li < 1 || li > 65535) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900251 errx(EXIT_FAILURE, "Port out of range `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000252 }
253 port = (uint16_t)li;
254 break;
255 case 'o':
256 dev_offset = strtoll (optarg, &end, 0);
257 if (*end) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900258 errx(EXIT_FAILURE, "Invalid offset `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000259 }
260 if (dev_offset < 0) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900261 errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000262 }
263 break;
264 case 'r':
265 readonly = true;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200266 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000267 break;
268 case 'P':
269 partition = strtol(optarg, &end, 0);
270 if (*end)
Ryota Ozakib6353be2010-03-20 15:23:23 +0900271 errx(EXIT_FAILURE, "Invalid partition `%s'", optarg);
bellard7a5ca862008-05-27 21:13:40 +0000272 if (partition < 1 || partition > 8)
Ryota Ozakib6353be2010-03-20 15:23:23 +0900273 errx(EXIT_FAILURE, "Invalid partition %d", partition);
bellard7a5ca862008-05-27 21:13:40 +0000274 break;
thscd831bd2008-07-03 10:23:51 +0000275 case 'k':
276 socket = optarg;
277 if (socket[0] != '/')
Ryota Ozakib6353be2010-03-20 15:23:23 +0900278 errx(EXIT_FAILURE, "socket path must be absolute\n");
thscd831bd2008-07-03 10:23:51 +0000279 break;
280 case 'd':
281 disconnect = true;
282 break;
283 case 'c':
284 device = optarg;
285 break;
ths3b05a8e2008-07-03 12:45:02 +0000286 case 'e':
287 shared = strtol(optarg, &end, 0);
288 if (*end) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900289 errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg);
ths3b05a8e2008-07-03 12:45:02 +0000290 }
291 if (shared < 1) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900292 errx(EXIT_FAILURE, "Shared device number must be greater than 0\n");
ths3b05a8e2008-07-03 12:45:02 +0000293 }
294 break;
ths75818252008-07-03 13:41:03 +0000295 case 't':
296 persistent = 1;
297 break;
bellard7a5ca862008-05-27 21:13:40 +0000298 case 'v':
299 verbose = 1;
300 break;
301 case 'V':
302 version(argv[0]);
303 exit(0);
304 break;
305 case 'h':
306 usage(argv[0]);
307 exit(0);
308 break;
309 case '?':
Ryota Ozakib6353be2010-03-20 15:23:23 +0900310 errx(EXIT_FAILURE, "Try `%s --help' for more information.",
bellard7a5ca862008-05-27 21:13:40 +0000311 argv[0]);
312 }
313 }
314
315 if ((argc - optind) != 1) {
Ryota Ozakib6353be2010-03-20 15:23:23 +0900316 errx(EXIT_FAILURE, "Invalid number of argument.\n"
bellard7a5ca862008-05-27 21:13:40 +0000317 "Try `%s --help' for more information.",
318 argv[0]);
319 }
320
thscd831bd2008-07-03 10:23:51 +0000321 if (disconnect) {
322 fd = open(argv[optind], O_RDWR);
323 if (fd == -1)
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900324 err(EXIT_FAILURE, "Cannot open %s", argv[optind]);
thscd831bd2008-07-03 10:23:51 +0000325
326 nbd_disconnect(fd);
327
328 close(fd);
329
330 printf("%s disconnected\n", argv[optind]);
331
332 return 0;
333 }
334
bellard7a5ca862008-05-27 21:13:40 +0000335 bdrv_init();
336
337 bs = bdrv_new("hda");
338 if (bs == NULL)
339 return 1;
340
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900341 if ((ret = bdrv_open(bs, argv[optind], flags, NULL)) < 0) {
342 errno = -ret;
343 err(EXIT_FAILURE, "Failed to bdrv_open '%s'", argv[optind]);
344 }
bellard7a5ca862008-05-27 21:13:40 +0000345
346 fd_size = bs->total_sectors * 512;
347
348 if (partition != -1 &&
349 find_partition(bs, partition, &dev_offset, &fd_size))
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900350 err(EXIT_FAILURE, "Could not find partition %d", partition);
bellard7a5ca862008-05-27 21:13:40 +0000351
thscd831bd2008-07-03 10:23:51 +0000352 if (device) {
ths3b05a8e2008-07-03 12:45:02 +0000353 pid_t pid;
354 int sock;
355
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900356 /* want to fail before daemonizing */
357 if (access(device, R_OK|W_OK) == -1) {
358 err(EXIT_FAILURE, "Could not access '%s'", device);
359 }
360
Anthony Liguorif5de1412009-06-15 12:49:59 -0500361 if (!verbose) {
362 /* detach client and server */
363 if (daemon(0, 0) == -1) {
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900364 err(EXIT_FAILURE, "Failed to daemonize");
Anthony Liguorif5de1412009-06-15 12:49:59 -0500365 }
366 }
thscd831bd2008-07-03 10:23:51 +0000367
368 if (socket == NULL) {
Blue Swirl22ff51e2009-12-23 15:45:30 +0000369 snprintf(sockpath, sizeof(sockpath), SOCKET_PATH,
370 basename(device));
thscd831bd2008-07-03 10:23:51 +0000371 socket = sockpath;
372 }
373
374 pid = fork();
375 if (pid < 0)
376 return 1;
377 if (pid != 0) {
378 off_t size;
379 size_t blocksize;
380
381 ret = 0;
382 bdrv_close(bs);
383
384 do {
385 sock = unix_socket_outgoing(socket);
386 if (sock == -1) {
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900387 if (errno != ENOENT && errno != ECONNREFUSED) {
388 ret = 1;
thscd831bd2008-07-03 10:23:51 +0000389 goto out;
Ryota Ozakicb7cf0e2010-05-03 06:50:25 +0900390 }
thscd831bd2008-07-03 10:23:51 +0000391 sleep(1); /* wait children */
392 }
393 } while (sock == -1);
394
395 fd = open(device, O_RDWR);
396 if (fd == -1) {
397 ret = 1;
398 goto out;
399 }
400
401 ret = nbd_receive_negotiate(sock, &size, &blocksize);
402 if (ret == -1) {
403 ret = 1;
404 goto out;
405 }
406
407 ret = nbd_init(fd, sock, size, blocksize);
408 if (ret == -1) {
409 ret = 1;
410 goto out;
411 }
412
413 printf("NBD device %s is now connected to file %s\n",
414 device, argv[optind]);
415
416 /* update partition table */
417
418 show_parts(device);
419
420 nbd_client(fd, sock);
421 close(fd);
422 out:
423 kill(pid, SIGTERM);
424 unlink(socket);
425
426 return ret;
427 }
428 /* children */
429 }
430
ths3b05a8e2008-07-03 12:45:02 +0000431 sharing_fds = qemu_malloc((shared + 1) * sizeof(int));
ths3b05a8e2008-07-03 12:45:02 +0000432
thscd831bd2008-07-03 10:23:51 +0000433 if (socket) {
ths3b05a8e2008-07-03 12:45:02 +0000434 sharing_fds[0] = unix_socket_incoming(socket);
thscd831bd2008-07-03 10:23:51 +0000435 } else {
ths3b05a8e2008-07-03 12:45:02 +0000436 sharing_fds[0] = tcp_socket_incoming(bindto, port);
thscd831bd2008-07-03 10:23:51 +0000437 }
438
ths3b05a8e2008-07-03 12:45:02 +0000439 if (sharing_fds[0] == -1)
bellard7a5ca862008-05-27 21:13:40 +0000440 return 1;
ths3b05a8e2008-07-03 12:45:02 +0000441 max_fd = sharing_fds[0];
442 nb_fds++;
bellard7a5ca862008-05-27 21:13:40 +0000443
ths2f726482008-07-03 11:47:46 +0000444 data = qemu_memalign(512, NBD_BUFFER_SIZE);
ths3b05a8e2008-07-03 12:45:02 +0000445 if (data == NULL)
Ryota Ozakib6353be2010-03-20 15:23:23 +0900446 errx(EXIT_FAILURE, "Cannot allocate data buffer");
ths3b05a8e2008-07-03 12:45:02 +0000447
448 do {
449
450 FD_ZERO(&fds);
451 for (i = 0; i < nb_fds; i++)
452 FD_SET(sharing_fds[i], &fds);
453
454 ret = select(max_fd + 1, &fds, NULL, NULL, NULL);
455 if (ret == -1)
456 break;
457
458 if (FD_ISSET(sharing_fds[0], &fds))
459 ret--;
460 for (i = 1; i < nb_fds && ret; i++) {
461 if (FD_ISSET(sharing_fds[i], &fds)) {
462 if (nbd_trip(bs, sharing_fds[i], fd_size, dev_offset,
463 &offset, readonly, data, NBD_BUFFER_SIZE) != 0) {
464 close(sharing_fds[i]);
465 nb_fds--;
466 sharing_fds[i] = sharing_fds[nb_fds];
467 i--;
468 }
469 ret--;
470 }
471 }
472 /* new connection ? */
473 if (FD_ISSET(sharing_fds[0], &fds)) {
474 if (nb_fds < shared + 1) {
475 sharing_fds[nb_fds] = accept(sharing_fds[0],
476 (struct sockaddr *)&addr,
477 &addr_len);
478 if (sharing_fds[nb_fds] != -1 &&
aliguori27982662008-09-10 15:23:19 +0000479 nbd_negotiate(sharing_fds[nb_fds], fd_size) != -1) {
ths3b05a8e2008-07-03 12:45:02 +0000480 if (sharing_fds[nb_fds] > max_fd)
481 max_fd = sharing_fds[nb_fds];
482 nb_fds++;
483 }
484 }
485 }
ths75818252008-07-03 13:41:03 +0000486 } while (persistent || nb_fds > 1);
Herve Poussineauf8a83242010-01-24 21:23:56 +0000487 qemu_vfree(data);
bellard7a5ca862008-05-27 21:13:40 +0000488
ths3b05a8e2008-07-03 12:45:02 +0000489 close(sharing_fds[0]);
bellard7a5ca862008-05-27 21:13:40 +0000490 bdrv_close(bs);
ths3b05a8e2008-07-03 12:45:02 +0000491 qemu_free(sharing_fds);
thscd831bd2008-07-03 10:23:51 +0000492 if (socket)
493 unlink(socket);
bellard7a5ca862008-05-27 21:13:40 +0000494
495 return 0;
496}