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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <qemu-common.h>
21 #include "block_int.h"
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <netinet/tcp.h>
32 #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 " -v, --verbose display extra debugging information\n"
59 " -h, --help display this help and exit\n"
60 " -V, --version output version information and exit\n"
62 "Report bugs to <anthony@codemonkey.ws>\n"
66 static void version(const char *name)
69 "qemu-nbd version 0.0.1\n"
70 "Written by Anthony Liguori.\n"
72 "Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
73 "This is free software; see the source for copying conditions. There is NO\n"
74 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
78 struct partition_record
82 uint32_t start_cylinder;
88 uint32_t start_sector_abs;
89 uint32_t nb_sectors_abs;
92 static void read_partition(uint8_t *p, struct partition_record *r)
96 r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
97 r->start_sector = p[2] & 0x3f;
100 r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
101 r->end_sector = p[6] & 0x3f;
102 r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24;
103 r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24;
106 static int find_partition(BlockDriverState *bs, int partition,
107 off_t *offset, off_t *size)
109 struct partition_record mbr[4];
114 if (bdrv_read(bs, 0, data, 1))
115 errx(EINVAL, "error while reading");
117 if (data[510] != 0x55 || data[511] != 0xaa) {
122 for (i = 0; i < 4; i++) {
123 read_partition(&data[446 + 16 * i], &mbr[i]);
125 if (!mbr[i].nb_sectors_abs)
128 if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
129 struct partition_record ext[4];
133 if (bdrv_read(bs, mbr[i].start_sector_abs, data1, 1))
134 errx(EINVAL, "error while reading");
136 for (j = 0; j < 4; j++) {
137 read_partition(&data1[446 + 16 * j], &ext[j]);
138 if (!ext[j].nb_sectors_abs)
141 if ((ext_partnum + j + 1) == partition) {
142 *offset = (uint64_t)ext[j].start_sector_abs << 9;
143 *size = (uint64_t)ext[j].nb_sectors_abs << 9;
148 } else if ((i + 1) == partition) {
149 *offset = (uint64_t)mbr[i].start_sector_abs << 9;
150 *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
159 static void show_parts(const char *device)
164 /* linux just needs an open() to trigger
165 * the partition table update
166 * but remember to load the module with max_part != 0 :
167 * modprobe nbd max_part=63
169 nbd = open(device, O_RDWR);
176 int main(int argc, char **argv)
178 BlockDriverState *bs;
179 off_t dev_offset = 0;
181 bool readonly = false;
182 bool disconnect = false;
183 const char *bindto = "0.0.0.0";
186 struct sockaddr_in addr;
187 socklen_t addr_len = sizeof(addr);
192 const char *sopt = "hVbo:p:rsnP:c:dvk:";
193 struct option lopt[] = {
194 { "help", 0, 0, 'h' },
195 { "version", 0, 0, 'V' },
196 { "bind", 1, 0, 'b' },
197 { "port", 1, 0, 'p' },
198 { "socket", 1, 0, 'k' },
199 { "offset", 1, 0, 'o' },
200 { "read-only", 0, 0, 'r' },
201 { "partition", 1, 0, 'P' },
202 { "connect", 1, 0, 'c' },
203 { "disconnect", 0, 0, 'd' },
204 { "snapshot", 0, 0, 's' },
205 { "nocache", 0, 0, 'n' },
206 { "verbose", 0, 0, 'v' },
219 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
222 flags |= BDRV_O_SNAPSHOT;
225 flags |= BDRV_O_DIRECT;
231 li = strtol(optarg, &end, 0);
233 errx(EINVAL, "Invalid port `%s'", optarg);
235 if (li < 1 || li > 65535) {
236 errx(EINVAL, "Port out of range `%s'", optarg);
241 dev_offset = strtoll (optarg, &end, 0);
243 errx(EINVAL, "Invalid offset `%s'", optarg);
245 if (dev_offset < 0) {
246 errx(EINVAL, "Offset must be positive `%s'", optarg);
253 partition = strtol(optarg, &end, 0);
255 errx(EINVAL, "Invalid partition `%s'", optarg);
256 if (partition < 1 || partition > 8)
257 errx(EINVAL, "Invalid partition %d", partition);
261 if (socket[0] != '/')
262 errx(EINVAL, "socket path must be absolute\n");
282 errx(EINVAL, "Try `%s --help' for more information.",
287 if ((argc - optind) != 1) {
288 errx(EINVAL, "Invalid number of argument.\n"
289 "Try `%s --help' for more information.",
294 fd = open(argv[optind], O_RDWR);
296 errx(errno, "Cannot open %s", argv[optind]);
302 printf("%s disconnected\n", argv[optind]);
309 bs = bdrv_new("hda");
313 if (bdrv_open(bs, argv[optind], flags) == -1)
316 fd_size = bs->total_sectors * 512;
318 if (partition != -1 &&
319 find_partition(bs, partition, &dev_offset, &fd_size))
320 errx(errno, "Could not find partition %d", partition);
325 daemon(0, 0); /* detach client and server */
327 if (socket == NULL) {
328 sprintf(sockpath, SOCKET_PATH, basename(device));
343 sock = unix_socket_outgoing(socket);
345 if (errno != ENOENT && errno != ECONNREFUSED)
347 sleep(1); /* wait children */
349 } while (sock == -1);
351 fd = open(device, O_RDWR);
357 ret = nbd_receive_negotiate(sock, &size, &blocksize);
363 ret = nbd_init(fd, sock, size, blocksize);
369 printf("NBD device %s is now connected to file %s\n",
370 device, argv[optind]);
372 /* update partition table */
376 nbd_client(fd, sock);
388 sock = unix_socket_incoming(socket);
390 sock = tcp_socket_incoming(bindto, port);
397 (struct sockaddr *)&addr,
402 /* new fd_size is calculated by find_partition */
403 if (nbd_negotiate(bs, csock, fd_size) == -1)
406 data = qemu_memalign(512, NBD_BUFFER_SIZE);
407 while (nbd_trip(bs, csock, fd_size, dev_offset, &offset, readonly,
408 data, NBD_BUFFER_SIZE) == 0);