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)
39 static int sigterm_wfd;
42 static void usage(const char *name)
45 "Usage: %s [OPTIONS] FILE\n"
46 "QEMU Disk Network Block Device Server\n"
48 " -p, --port=PORT port to listen on (default `%d')\n"
49 " -o, --offset=OFFSET offset into the image\n"
50 " -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
51 " -k, --socket=PATH path to the unix socket\n"
52 " (default '"SOCKET_PATH"')\n"
53 " -r, --read-only export read-only\n"
54 " -P, --partition=NUM only expose partition NUM\n"
55 " -s, --snapshot use snapshot file\n"
56 " -n, --nocache disable host cache\n"
57 " -c, --connect=DEV connect FILE to the local NBD device DEV\n"
58 " -d, --disconnect disconnect the specified device\n"
59 " -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
60 " -t, --persistent don't exit on the last connection\n"
61 " -v, --verbose display extra debugging information\n"
62 " -h, --help display this help and exit\n"
63 " -V, --version output version information and exit\n"
66 , name, NBD_DEFAULT_PORT, "DEVICE");
69 static void version(const char *name)
73 "Written by Anthony Liguori.\n"
76 "This is free software; see the source for copying conditions. There is NO\n"
77 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
81 struct partition_record
85 uint32_t start_cylinder;
91 uint32_t start_sector_abs;
92 uint32_t nb_sectors_abs;
95 static void read_partition(uint8_t *p, struct partition_record *r)
99 r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
100 r->start_sector = p[2] & 0x3f;
103 r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
104 r->end_sector = p[6] & 0x3f;
105 r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24;
106 r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24;
109 static int find_partition(BlockDriverState *bs, int partition,
110 off_t *offset, off_t *size)
112 struct partition_record mbr[4];
118 if ((ret = bdrv_read(bs, 0, data, 1)) < 0) {
120 err(EXIT_FAILURE, "error while reading");
123 if (data[510] != 0x55 || data[511] != 0xaa) {
128 for (i = 0; i < 4; i++) {
129 read_partition(&data[446 + 16 * i], &mbr[i]);
131 if (!mbr[i].nb_sectors_abs)
134 if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
135 struct partition_record ext[4];
139 if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) {
141 err(EXIT_FAILURE, "error while reading");
144 for (j = 0; j < 4; j++) {
145 read_partition(&data1[446 + 16 * j], &ext[j]);
146 if (!ext[j].nb_sectors_abs)
149 if ((ext_partnum + j + 1) == partition) {
150 *offset = (uint64_t)ext[j].start_sector_abs << 9;
151 *size = (uint64_t)ext[j].nb_sectors_abs << 9;
156 } else if ((i + 1) == partition) {
157 *offset = (uint64_t)mbr[i].start_sector_abs << 9;
158 *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
167 static void termsig_handler(int signum)
169 static int sigterm_reported;
170 if (!sigterm_reported) {
171 sigterm_reported = (write(sigterm_wfd, "", 1) == 1);
175 static void show_parts(const char *device)
180 /* linux just needs an open() to trigger
181 * the partition table update
182 * but remember to load the module with max_part != 0 :
183 * modprobe nbd max_part=63
185 nbd = open(device, O_RDWR);
192 int main(int argc, char **argv)
194 BlockDriverState *bs;
195 off_t dev_offset = 0;
197 uint32_t nbdflags = 0;
198 bool disconnect = false;
199 const char *bindto = "0.0.0.0";
200 int port = NBD_DEFAULT_PORT;
201 struct sockaddr_in addr;
202 socklen_t addr_len = sizeof(addr);
207 const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t";
208 struct option lopt[] = {
209 { "help", 0, NULL, 'h' },
210 { "version", 0, NULL, 'V' },
211 { "bind", 1, NULL, 'b' },
212 { "port", 1, NULL, 'p' },
213 { "socket", 1, NULL, 'k' },
214 { "offset", 1, NULL, 'o' },
215 { "read-only", 0, NULL, 'r' },
216 { "partition", 1, NULL, 'P' },
217 { "connect", 1, NULL, 'c' },
218 { "disconnect", 0, NULL, 'd' },
219 { "snapshot", 0, NULL, 's' },
220 { "nocache", 0, NULL, 'n' },
221 { "shared", 1, NULL, 'e' },
222 { "persistent", 0, NULL, 't' },
223 { "verbose", 0, NULL, 'v' },
230 int flags = BDRV_O_RDWR;
243 /* Set up a SIGTERM handler so that we exit with a nice status code. */
244 struct sigaction sa_sigterm;
246 if (qemu_pipe(sigterm_fd) == -1) {
247 err(EXIT_FAILURE, "Error setting up communication pipe");
250 sigterm_wfd = sigterm_fd[1];
251 memset(&sa_sigterm, 0, sizeof(sa_sigterm));
252 sa_sigterm.sa_handler = termsig_handler;
253 sigaction(SIGTERM, &sa_sigterm, NULL);
255 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
258 flags |= BDRV_O_SNAPSHOT;
261 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
267 li = strtol(optarg, &end, 0);
269 errx(EXIT_FAILURE, "Invalid port `%s'", optarg);
271 if (li < 1 || li > 65535) {
272 errx(EXIT_FAILURE, "Port out of range `%s'", optarg);
277 dev_offset = strtoll (optarg, &end, 0);
279 errx(EXIT_FAILURE, "Invalid offset `%s'", optarg);
281 if (dev_offset < 0) {
282 errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg);
286 nbdflags |= NBD_FLAG_READ_ONLY;
287 flags &= ~BDRV_O_RDWR;
290 partition = strtol(optarg, &end, 0);
292 errx(EXIT_FAILURE, "Invalid partition `%s'", optarg);
293 if (partition < 1 || partition > 8)
294 errx(EXIT_FAILURE, "Invalid partition %d", partition);
298 if (socket[0] != '/')
299 errx(EXIT_FAILURE, "socket path must be absolute\n");
308 shared = strtol(optarg, &end, 0);
310 errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg);
313 errx(EXIT_FAILURE, "Shared device number must be greater than 0\n");
331 errx(EXIT_FAILURE, "Try `%s --help' for more information.",
336 if ((argc - optind) != 1) {
337 errx(EXIT_FAILURE, "Invalid number of argument.\n"
338 "Try `%s --help' for more information.",
343 fd = open(argv[optind], O_RDWR);
345 err(EXIT_FAILURE, "Cannot open %s", argv[optind]);
351 printf("%s disconnected\n", argv[optind]);
358 bs = bdrv_new("hda");
360 if ((ret = bdrv_open(bs, argv[optind], flags, NULL)) < 0) {
362 err(EXIT_FAILURE, "Failed to bdrv_open '%s'", argv[optind]);
365 fd_size = bs->total_sectors * 512;
367 if (partition != -1 &&
368 find_partition(bs, partition, &dev_offset, &fd_size))
369 err(EXIT_FAILURE, "Could not find partition %d", partition);
375 /* want to fail before daemonizing */
376 if (access(device, R_OK|W_OK) == -1) {
377 err(EXIT_FAILURE, "Could not access '%s'", device);
381 /* detach client and server */
382 if (qemu_daemon(0, 0) == -1) {
383 err(EXIT_FAILURE, "Failed to daemonize");
387 if (socket == NULL) {
388 snprintf(sockpath, sizeof(sockpath), SOCKET_PATH,
404 sock = unix_socket_outgoing(socket);
406 if (errno != ENOENT && errno != ECONNREFUSED) {
410 sleep(1); /* wait children */
412 } while (sock == -1);
414 fd = open(device, O_RDWR);
420 ret = nbd_receive_negotiate(sock, NULL, &nbdflags,
427 ret = nbd_init(fd, sock, nbdflags, size, blocksize);
433 printf("NBD device %s is now connected to file %s\n",
434 device, argv[optind]);
436 /* update partition table */
440 ret = nbd_client(fd);
453 sharing_fds = g_malloc((shared + 1) * sizeof(int));
456 sharing_fds[0] = unix_socket_incoming(socket);
458 sharing_fds[0] = tcp_socket_incoming(bindto, port);
461 if (sharing_fds[0] == -1)
463 max_fd = sharing_fds[0];
466 data = qemu_blockalign(bs, NBD_BUFFER_SIZE);
468 errx(EXIT_FAILURE, "Cannot allocate data buffer");
473 FD_SET(sigterm_fd[0], &fds);
474 for (i = 0; i < nb_fds; i++)
475 FD_SET(sharing_fds[i], &fds);
478 ret = select(max_fd + 1, &fds, NULL, NULL, NULL);
479 } while (ret == -1 && errno == EINTR);
480 if (ret == -1 || FD_ISSET(sigterm_fd[0], &fds)) {
484 if (FD_ISSET(sharing_fds[0], &fds))
486 for (i = 1; i < nb_fds && ret; i++) {
487 if (FD_ISSET(sharing_fds[i], &fds)) {
488 if (nbd_trip(bs, sharing_fds[i], fd_size, dev_offset,
489 &offset, nbdflags, data, NBD_BUFFER_SIZE) != 0) {
490 close(sharing_fds[i]);
492 sharing_fds[i] = sharing_fds[nb_fds];
498 /* new connection ? */
499 if (FD_ISSET(sharing_fds[0], &fds)) {
500 if (nb_fds < shared + 1) {
501 sharing_fds[nb_fds] = accept(sharing_fds[0],
502 (struct sockaddr *)&addr,
504 if (sharing_fds[nb_fds] != -1 &&
505 nbd_negotiate(sharing_fds[nb_fds], fd_size, nbdflags) != -1) {
506 if (sharing_fds[nb_fds] > max_fd)
507 max_fd = sharing_fds[nb_fds];
512 } while (persistent || nb_fds > 1);
515 close(sharing_fds[0]);