2 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include <qemu-common.h>
20 #include "block_int.h"
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <netinet/tcp.h>
31 #include <arpa/inet.h>
35 #define SOCKET_PATH "/var/lock/qemu-nbd-%s"
37 #define NBD_BUFFER_SIZE (1024*1024)
41 static void usage(const char *name
)
44 "Usage: %s [OPTIONS] FILE\n"
45 "QEMU Disk Network Block Device Server\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"
50 " -k, --socket=PATH path to the unix socket\n"
51 " (default '"SOCKET_PATH
"')\n"
52 " -r, --read-only export read-only\n"
53 " -P, --partition=NUM only expose partition NUM\n"
54 " -s, --snapshot use snapshot file\n"
55 " -n, --nocache disable host cache\n"
56 " -c, --connect=DEV connect FILE to the local NBD device DEV\n"
57 " -d, --disconnect disconnect the specified device\n"
58 " -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
59 " -t, --persistent don't exit on the last connection\n"
60 " -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"
64 "Report bugs to <anthony@codemonkey.ws>\n"
68 static void version(const char *name
)
72 "Written by Anthony Liguori.\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"
80 struct partition_record
84 uint32_t start_cylinder
;
90 uint32_t start_sector_abs
;
91 uint32_t nb_sectors_abs
;
94 static void read_partition(uint8_t *p
, struct partition_record
*r
)
98 r
->start_cylinder
= p
[3] | ((p
[2] << 2) & 0x0300);
99 r
->start_sector
= p
[2] & 0x3f;
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;
108 static int find_partition(BlockDriverState
*bs
, int partition
,
109 off_t
*offset
, off_t
*size
)
111 struct partition_record mbr
[4];
116 if (bdrv_read(bs
, 0, data
, 1))
117 errx(EXIT_FAILURE
, "error while reading");
119 if (data
[510] != 0x55 || data
[511] != 0xaa) {
124 for (i
= 0; i
< 4; i
++) {
125 read_partition(&data
[446 + 16 * i
], &mbr
[i
]);
127 if (!mbr
[i
].nb_sectors_abs
)
130 if (mbr
[i
].system
== 0xF || mbr
[i
].system
== 0x5) {
131 struct partition_record ext
[4];
135 if (bdrv_read(bs
, mbr
[i
].start_sector_abs
, data1
, 1))
136 errx(EXIT_FAILURE
, "error while reading");
138 for (j
= 0; j
< 4; j
++) {
139 read_partition(&data1
[446 + 16 * j
], &ext
[j
]);
140 if (!ext
[j
].nb_sectors_abs
)
143 if ((ext_partnum
+ j
+ 1) == partition
) {
144 *offset
= (uint64_t)ext
[j
].start_sector_abs
<< 9;
145 *size
= (uint64_t)ext
[j
].nb_sectors_abs
<< 9;
150 } else if ((i
+ 1) == partition
) {
151 *offset
= (uint64_t)mbr
[i
].start_sector_abs
<< 9;
152 *size
= (uint64_t)mbr
[i
].nb_sectors_abs
<< 9;
161 static void show_parts(const char *device
)
166 /* linux just needs an open() to trigger
167 * the partition table update
168 * but remember to load the module with max_part != 0 :
169 * modprobe nbd max_part=63
171 nbd
= open(device
, O_RDWR
);
178 int main(int argc
, char **argv
)
180 BlockDriverState
*bs
;
181 off_t dev_offset
= 0;
183 bool readonly
= false;
184 bool disconnect
= false;
185 const char *bindto
= "0.0.0.0";
187 struct sockaddr_in addr
;
188 socklen_t addr_len
= sizeof(addr
);
193 const char *sopt
= "hVb:o:p:rsnP:c:dvk:e:t";
194 struct option lopt
[] = {
195 { "help", 0, NULL
, 'h' },
196 { "version", 0, NULL
, 'V' },
197 { "bind", 1, NULL
, 'b' },
198 { "port", 1, NULL
, 'p' },
199 { "socket", 1, NULL
, 'k' },
200 { "offset", 1, NULL
, 'o' },
201 { "read-only", 0, NULL
, 'r' },
202 { "partition", 1, NULL
, 'P' },
203 { "connect", 1, NULL
, 'c' },
204 { "disconnect", 0, NULL
, 'd' },
205 { "snapshot", 0, NULL
, 's' },
206 { "nocache", 0, NULL
, 'n' },
207 { "shared", 1, NULL
, 'e' },
208 { "persistent", 0, NULL
, 't' },
209 { "verbose", 0, NULL
, 'v' },
216 int flags
= BDRV_O_RDWR
;
229 while ((ch
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_ind
)) != -1) {
232 flags
|= BDRV_O_SNAPSHOT
;
235 flags
|= BDRV_O_NOCACHE
;
241 li
= strtol(optarg
, &end
, 0);
243 errx(EXIT_FAILURE
, "Invalid port `%s'", optarg
);
245 if (li
< 1 || li
> 65535) {
246 errx(EXIT_FAILURE
, "Port out of range `%s'", optarg
);
251 dev_offset
= strtoll (optarg
, &end
, 0);
253 errx(EXIT_FAILURE
, "Invalid offset `%s'", optarg
);
255 if (dev_offset
< 0) {
256 errx(EXIT_FAILURE
, "Offset must be positive `%s'", optarg
);
261 flags
&= ~BDRV_O_RDWR
;
264 partition
= strtol(optarg
, &end
, 0);
266 errx(EXIT_FAILURE
, "Invalid partition `%s'", optarg
);
267 if (partition
< 1 || partition
> 8)
268 errx(EXIT_FAILURE
, "Invalid partition %d", partition
);
272 if (socket
[0] != '/')
273 errx(EXIT_FAILURE
, "socket path must be absolute\n");
282 shared
= strtol(optarg
, &end
, 0);
284 errx(EXIT_FAILURE
, "Invalid shared device number '%s'", optarg
);
287 errx(EXIT_FAILURE
, "Shared device number must be greater than 0\n");
305 errx(EXIT_FAILURE
, "Try `%s --help' for more information.",
310 if ((argc
- optind
) != 1) {
311 errx(EXIT_FAILURE
, "Invalid number of argument.\n"
312 "Try `%s --help' for more information.",
317 fd
= open(argv
[optind
], O_RDWR
);
319 errx(EXIT_FAILURE
, "Cannot open %s", argv
[optind
]);
325 printf("%s disconnected\n", argv
[optind
]);
332 bs
= bdrv_new("hda");
336 if (bdrv_open(bs
, argv
[optind
], flags
, NULL
) < 0)
339 fd_size
= bs
->total_sectors
* 512;
341 if (partition
!= -1 &&
342 find_partition(bs
, partition
, &dev_offset
, &fd_size
))
343 errx(EXIT_FAILURE
, "Could not find partition %d", partition
);
350 /* detach client and server */
351 if (daemon(0, 0) == -1) {
352 errx(EXIT_FAILURE
, "Failed to daemonize");
356 if (socket
== NULL
) {
357 snprintf(sockpath
, sizeof(sockpath
), SOCKET_PATH
,
373 sock
= unix_socket_outgoing(socket
);
375 if (errno
!= ENOENT
&& errno
!= ECONNREFUSED
)
377 sleep(1); /* wait children */
379 } while (sock
== -1);
381 fd
= open(device
, O_RDWR
);
387 ret
= nbd_receive_negotiate(sock
, &size
, &blocksize
);
393 ret
= nbd_init(fd
, sock
, size
, blocksize
);
399 printf("NBD device %s is now connected to file %s\n",
400 device
, argv
[optind
]);
402 /* update partition table */
406 nbd_client(fd
, sock
);
417 sharing_fds
= qemu_malloc((shared
+ 1) * sizeof(int));
420 sharing_fds
[0] = unix_socket_incoming(socket
);
422 sharing_fds
[0] = tcp_socket_incoming(bindto
, port
);
425 if (sharing_fds
[0] == -1)
427 max_fd
= sharing_fds
[0];
430 data
= qemu_memalign(512, NBD_BUFFER_SIZE
);
432 errx(EXIT_FAILURE
, "Cannot allocate data buffer");
437 for (i
= 0; i
< nb_fds
; i
++)
438 FD_SET(sharing_fds
[i
], &fds
);
440 ret
= select(max_fd
+ 1, &fds
, NULL
, NULL
, NULL
);
444 if (FD_ISSET(sharing_fds
[0], &fds
))
446 for (i
= 1; i
< nb_fds
&& ret
; i
++) {
447 if (FD_ISSET(sharing_fds
[i
], &fds
)) {
448 if (nbd_trip(bs
, sharing_fds
[i
], fd_size
, dev_offset
,
449 &offset
, readonly
, data
, NBD_BUFFER_SIZE
) != 0) {
450 close(sharing_fds
[i
]);
452 sharing_fds
[i
] = sharing_fds
[nb_fds
];
458 /* new connection ? */
459 if (FD_ISSET(sharing_fds
[0], &fds
)) {
460 if (nb_fds
< shared
+ 1) {
461 sharing_fds
[nb_fds
] = accept(sharing_fds
[0],
462 (struct sockaddr
*)&addr
,
464 if (sharing_fds
[nb_fds
] != -1 &&
465 nbd_negotiate(sharing_fds
[nb_fds
], fd_size
) != -1) {
466 if (sharing_fds
[nb_fds
] > max_fd
)
467 max_fd
= sharing_fds
[nb_fds
];
472 } while (persistent
|| nb_fds
> 1);
475 close(sharing_fds
[0]);
477 qemu_free(sharing_fds
);