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>
34 #define SOCKET_PATH "/var/lock/qemu-nbd-%s"
36 #define NBD_BUFFER_SIZE (1024*1024)
40 static void usage(const char *name
)
43 "Usage: %s [OPTIONS] FILE\n"
44 "QEMU Disk Network Block Device Server\n"
46 " -p, --port=PORT port to listen on (default `1024')\n"
47 " -o, --offset=OFFSET offset into the image\n"
48 " -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
49 " -k, --socket=PATH path to the unix socket\n"
50 " (default '"SOCKET_PATH
"')\n"
51 " -r, --read-only export read-only\n"
52 " -P, --partition=NUM only expose partition NUM\n"
53 " -s, --snapshot use snapshot file\n"
54 " -n, --nocache disable host cache\n"
55 " -c, --connect=DEV connect FILE to the local NBD device DEV\n"
56 " -d, --disconnect disconnect the specified device\n"
57 " -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
58 " -t, --persistent don't exit on the last connection\n"
59 " -v, --verbose display extra debugging information\n"
60 " -h, --help display this help and exit\n"
61 " -V, --version output version information and exit\n"
63 "Report bugs to <anthony@codemonkey.ws>\n"
67 static void version(const char *name
)
71 "Written by Anthony Liguori.\n"
73 "Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
74 "This is free software; see the source for copying conditions. There is NO\n"
75 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
79 struct partition_record
83 uint32_t start_cylinder
;
89 uint32_t start_sector_abs
;
90 uint32_t nb_sectors_abs
;
93 static void read_partition(uint8_t *p
, struct partition_record
*r
)
97 r
->start_cylinder
= p
[3] | ((p
[2] << 2) & 0x0300);
98 r
->start_sector
= p
[2] & 0x3f;
101 r
->end_cylinder
= p
[7] | ((p
[6] << 2) & 0x300);
102 r
->end_sector
= p
[6] & 0x3f;
103 r
->start_sector_abs
= p
[8] | p
[9] << 8 | p
[10] << 16 | p
[11] << 24;
104 r
->nb_sectors_abs
= p
[12] | p
[13] << 8 | p
[14] << 16 | p
[15] << 24;
107 static int find_partition(BlockDriverState
*bs
, int partition
,
108 off_t
*offset
, off_t
*size
)
110 struct partition_record mbr
[4];
115 if (bdrv_read(bs
, 0, data
, 1))
116 errx(EINVAL
, "error while reading");
118 if (data
[510] != 0x55 || data
[511] != 0xaa) {
123 for (i
= 0; i
< 4; i
++) {
124 read_partition(&data
[446 + 16 * i
], &mbr
[i
]);
126 if (!mbr
[i
].nb_sectors_abs
)
129 if (mbr
[i
].system
== 0xF || mbr
[i
].system
== 0x5) {
130 struct partition_record ext
[4];
134 if (bdrv_read(bs
, mbr
[i
].start_sector_abs
, data1
, 1))
135 errx(EINVAL
, "error while reading");
137 for (j
= 0; j
< 4; j
++) {
138 read_partition(&data1
[446 + 16 * j
], &ext
[j
]);
139 if (!ext
[j
].nb_sectors_abs
)
142 if ((ext_partnum
+ j
+ 1) == partition
) {
143 *offset
= (uint64_t)ext
[j
].start_sector_abs
<< 9;
144 *size
= (uint64_t)ext
[j
].nb_sectors_abs
<< 9;
149 } else if ((i
+ 1) == partition
) {
150 *offset
= (uint64_t)mbr
[i
].start_sector_abs
<< 9;
151 *size
= (uint64_t)mbr
[i
].nb_sectors_abs
<< 9;
160 static void show_parts(const char *device
)
165 /* linux just needs an open() to trigger
166 * the partition table update
167 * but remember to load the module with max_part != 0 :
168 * modprobe nbd max_part=63
170 nbd
= open(device
, O_RDWR
);
177 int main(int argc
, char **argv
)
179 BlockDriverState
*bs
;
180 off_t dev_offset
= 0;
182 bool readonly
= false;
183 bool disconnect
= false;
184 const char *bindto
= "0.0.0.0";
186 struct sockaddr_in addr
;
187 socklen_t addr_len
= sizeof(addr
);
192 const char *sopt
= "hVb:o:p:rsnP:c:dvk:e:t";
193 struct option lopt
[] = {
194 { "help", 0, NULL
, 'h' },
195 { "version", 0, NULL
, 'V' },
196 { "bind", 1, NULL
, 'b' },
197 { "port", 1, NULL
, 'p' },
198 { "socket", 1, NULL
, 'k' },
199 { "offset", 1, NULL
, 'o' },
200 { "read-only", 0, NULL
, 'r' },
201 { "partition", 1, NULL
, 'P' },
202 { "connect", 1, NULL
, 'c' },
203 { "disconnect", 0, NULL
, 'd' },
204 { "snapshot", 0, NULL
, 's' },
205 { "nocache", 0, NULL
, 'n' },
206 { "shared", 1, NULL
, 'e' },
207 { "persistent", 0, NULL
, 't' },
208 { "verbose", 0, NULL
, 'v' },
228 while ((ch
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_ind
)) != -1) {
231 flags
|= BDRV_O_SNAPSHOT
;
234 flags
|= BDRV_O_NOCACHE
;
240 li
= strtol(optarg
, &end
, 0);
242 errx(EINVAL
, "Invalid port `%s'", optarg
);
244 if (li
< 1 || li
> 65535) {
245 errx(EINVAL
, "Port out of range `%s'", optarg
);
250 dev_offset
= strtoll (optarg
, &end
, 0);
252 errx(EINVAL
, "Invalid offset `%s'", optarg
);
254 if (dev_offset
< 0) {
255 errx(EINVAL
, "Offset must be positive `%s'", optarg
);
262 partition
= strtol(optarg
, &end
, 0);
264 errx(EINVAL
, "Invalid partition `%s'", optarg
);
265 if (partition
< 1 || partition
> 8)
266 errx(EINVAL
, "Invalid partition %d", partition
);
270 if (socket
[0] != '/')
271 errx(EINVAL
, "socket path must be absolute\n");
280 shared
= strtol(optarg
, &end
, 0);
282 errx(EINVAL
, "Invalid shared device number '%s'", optarg
);
285 errx(EINVAL
, "Shared device number must be greater than 0\n");
303 errx(EINVAL
, "Try `%s --help' for more information.",
308 if ((argc
- optind
) != 1) {
309 errx(EINVAL
, "Invalid number of argument.\n"
310 "Try `%s --help' for more information.",
315 fd
= open(argv
[optind
], O_RDWR
);
317 errx(errno
, "Cannot open %s", argv
[optind
]);
323 printf("%s disconnected\n", argv
[optind
]);
330 bs
= bdrv_new("hda");
334 if (bdrv_open(bs
, argv
[optind
], flags
) == -1)
337 fd_size
= bs
->total_sectors
* 512;
339 if (partition
!= -1 &&
340 find_partition(bs
, partition
, &dev_offset
, &fd_size
))
341 errx(errno
, "Could not find partition %d", partition
);
348 /* detach client and server */
349 if (daemon(0, 0) == -1) {
350 errx(errno
, "Failed to daemonize");
354 if (socket
== NULL
) {
355 sprintf(sockpath
, SOCKET_PATH
, basename(device
));
370 sock
= unix_socket_outgoing(socket
);
372 if (errno
!= ENOENT
&& errno
!= ECONNREFUSED
)
374 sleep(1); /* wait children */
376 } while (sock
== -1);
378 fd
= open(device
, O_RDWR
);
384 ret
= nbd_receive_negotiate(sock
, &size
, &blocksize
);
390 ret
= nbd_init(fd
, sock
, size
, blocksize
);
396 printf("NBD device %s is now connected to file %s\n",
397 device
, argv
[optind
]);
399 /* update partition table */
403 nbd_client(fd
, sock
);
414 sharing_fds
= qemu_malloc((shared
+ 1) * sizeof(int));
417 sharing_fds
[0] = unix_socket_incoming(socket
);
419 sharing_fds
[0] = tcp_socket_incoming(bindto
, port
);
422 if (sharing_fds
[0] == -1)
424 max_fd
= sharing_fds
[0];
427 data
= qemu_memalign(512, NBD_BUFFER_SIZE
);
429 errx(ENOMEM
, "Cannot allocate data buffer");
434 for (i
= 0; i
< nb_fds
; i
++)
435 FD_SET(sharing_fds
[i
], &fds
);
437 ret
= select(max_fd
+ 1, &fds
, NULL
, NULL
, NULL
);
441 if (FD_ISSET(sharing_fds
[0], &fds
))
443 for (i
= 1; i
< nb_fds
&& ret
; i
++) {
444 if (FD_ISSET(sharing_fds
[i
], &fds
)) {
445 if (nbd_trip(bs
, sharing_fds
[i
], fd_size
, dev_offset
,
446 &offset
, readonly
, data
, NBD_BUFFER_SIZE
) != 0) {
447 close(sharing_fds
[i
]);
449 sharing_fds
[i
] = sharing_fds
[nb_fds
];
455 /* new connection ? */
456 if (FD_ISSET(sharing_fds
[0], &fds
)) {
457 if (nb_fds
< shared
+ 1) {
458 sharing_fds
[nb_fds
] = accept(sharing_fds
[0],
459 (struct sockaddr
*)&addr
,
461 if (sharing_fds
[nb_fds
] != -1 &&
462 nbd_negotiate(sharing_fds
[nb_fds
], fd_size
) != -1) {
463 if (sharing_fds
[nb_fds
] > max_fd
)
464 max_fd
= sharing_fds
[nb_fds
];
469 } while (persistent
|| nb_fds
> 1);
472 close(sharing_fds
[0]);
474 qemu_free(sharing_fds
);