ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 1 | /* |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 2 | * 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 Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <qemu-common.h> |
| 20 | #include "block_int.h" |
| 21 | #include "nbd.h" |
| 22 | |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 23 | #include <stdarg.h> |
| 24 | #include <stdio.h> |
| 25 | #include <getopt.h> |
| 26 | #include <err.h> |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 27 | #include <sys/types.h> |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 28 | #include <sys/socket.h> |
| 29 | #include <netinet/in.h> |
| 30 | #include <netinet/tcp.h> |
| 31 | #include <arpa/inet.h> |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 32 | #include <signal.h> |
Blue Swirl | 2bff4b6 | 2009-12-23 15:34:04 +0000 | [diff] [blame] | 33 | #include <libgen.h> |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 34 | |
| 35 | #define SOCKET_PATH "/var/lock/qemu-nbd-%s" |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 36 | |
ths | 2f72648 | 2008-07-03 11:47:46 +0000 | [diff] [blame] | 37 | #define NBD_BUFFER_SIZE (1024*1024) |
| 38 | |
blueswir1 | b1d8e52 | 2008-10-26 13:43:07 +0000 | [diff] [blame] | 39 | static int verbose; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 40 | |
| 41 | static 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" |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 50 | " -k, --socket=PATH path to the unix socket\n" |
| 51 | " (default '"SOCKET_PATH"')\n" |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 52 | " -r, --read-only export read-only\n" |
| 53 | " -P, --partition=NUM only expose partition NUM\n" |
ths | 2f72648 | 2008-07-03 11:47:46 +0000 | [diff] [blame] | 54 | " -s, --snapshot use snapshot file\n" |
| 55 | " -n, --nocache disable host cache\n" |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 56 | " -c, --connect=DEV connect FILE to the local NBD device DEV\n" |
| 57 | " -d, --disconnect disconnect the specified device\n" |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 58 | " -e, --shared=NUM device can be shared by NUM clients (default '1')\n" |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 59 | " -t, --persistent don't exit on the last connection\n" |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 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" |
| 63 | "\n" |
| 64 | "Report bugs to <anthony@codemonkey.ws>\n" |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 65 | , name, "DEVICE"); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | static void version(const char *name) |
| 69 | { |
| 70 | printf( |
ths | 315bc7a | 2008-07-18 18:06:23 +0000 | [diff] [blame] | 71 | "%s version 0.0.1\n" |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 72 | "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" |
ths | 315bc7a | 2008-07-18 18:06:23 +0000 | [diff] [blame] | 77 | , name); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | struct 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 | |
| 94 | static 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 | |
| 108 | static 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; |
| 115 | |
| 116 | if (bdrv_read(bs, 0, data, 1)) |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 117 | errx(EXIT_FAILURE, "error while reading"); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 118 | |
| 119 | if (data[510] != 0x55 || data[511] != 0xaa) { |
| 120 | errno = -EINVAL; |
| 121 | return -1; |
| 122 | } |
| 123 | |
| 124 | for (i = 0; i < 4; i++) { |
| 125 | read_partition(&data[446 + 16 * i], &mbr[i]); |
| 126 | |
| 127 | if (!mbr[i].nb_sectors_abs) |
| 128 | continue; |
| 129 | |
| 130 | if (mbr[i].system == 0xF || mbr[i].system == 0x5) { |
| 131 | struct partition_record ext[4]; |
| 132 | uint8_t data1[512]; |
| 133 | int j; |
| 134 | |
| 135 | if (bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 136 | errx(EXIT_FAILURE, "error while reading"); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 137 | |
| 138 | for (j = 0; j < 4; j++) { |
| 139 | read_partition(&data1[446 + 16 * j], &ext[j]); |
| 140 | if (!ext[j].nb_sectors_abs) |
| 141 | continue; |
| 142 | |
| 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; |
| 146 | return 0; |
| 147 | } |
| 148 | } |
| 149 | ext_partnum += 4; |
| 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; |
| 153 | return 0; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | errno = -ENOENT; |
| 158 | return -1; |
| 159 | } |
| 160 | |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 161 | static void show_parts(const char *device) |
| 162 | { |
| 163 | if (fork() == 0) { |
| 164 | int nbd; |
| 165 | |
| 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 |
| 170 | */ |
| 171 | nbd = open(device, O_RDWR); |
| 172 | if (nbd != -1) |
| 173 | close(nbd); |
| 174 | exit(0); |
| 175 | } |
| 176 | } |
| 177 | |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 178 | int main(int argc, char **argv) |
| 179 | { |
| 180 | BlockDriverState *bs; |
| 181 | off_t dev_offset = 0; |
| 182 | off_t offset = 0; |
| 183 | bool readonly = false; |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 184 | bool disconnect = false; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 185 | const char *bindto = "0.0.0.0"; |
| 186 | int port = 1024; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 187 | struct sockaddr_in addr; |
| 188 | socklen_t addr_len = sizeof(addr); |
| 189 | off_t fd_size; |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 190 | char *device = NULL; |
| 191 | char *socket = NULL; |
| 192 | char sockpath[128]; |
aliguori | d6aa671 | 2009-01-08 19:34:35 +0000 | [diff] [blame] | 193 | const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t"; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 194 | struct option lopt[] = { |
Blue Swirl | 660f11b | 2009-07-31 21:16:51 +0000 | [diff] [blame] | 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' }, |
| 210 | { NULL, 0, NULL, 0 } |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 211 | }; |
| 212 | int ch; |
| 213 | int opt_ind = 0; |
| 214 | int li; |
| 215 | char *end; |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 216 | int flags = BDRV_O_RDWR; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 217 | int partition = -1; |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 218 | int ret; |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 219 | int shared = 1; |
ths | 2f72648 | 2008-07-03 11:47:46 +0000 | [diff] [blame] | 220 | uint8_t *data; |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 221 | fd_set fds; |
| 222 | int *sharing_fds; |
| 223 | int fd; |
| 224 | int i; |
| 225 | int nb_fds = 0; |
| 226 | int max_fd; |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 227 | int persistent = 0; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 228 | |
| 229 | while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { |
| 230 | switch (ch) { |
| 231 | case 's': |
ths | 2f72648 | 2008-07-03 11:47:46 +0000 | [diff] [blame] | 232 | flags |= BDRV_O_SNAPSHOT; |
| 233 | break; |
| 234 | case 'n': |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 235 | flags |= BDRV_O_NOCACHE; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 236 | break; |
| 237 | case 'b': |
| 238 | bindto = optarg; |
| 239 | break; |
| 240 | case 'p': |
| 241 | li = strtol(optarg, &end, 0); |
| 242 | if (*end) { |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 243 | errx(EXIT_FAILURE, "Invalid port `%s'", optarg); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 244 | } |
| 245 | if (li < 1 || li > 65535) { |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 246 | errx(EXIT_FAILURE, "Port out of range `%s'", optarg); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 247 | } |
| 248 | port = (uint16_t)li; |
| 249 | break; |
| 250 | case 'o': |
| 251 | dev_offset = strtoll (optarg, &end, 0); |
| 252 | if (*end) { |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 253 | errx(EXIT_FAILURE, "Invalid offset `%s'", optarg); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 254 | } |
| 255 | if (dev_offset < 0) { |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 256 | errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 257 | } |
| 258 | break; |
| 259 | case 'r': |
| 260 | readonly = true; |
Naphtali Sprei | 07108b2 | 2010-03-14 15:19:57 +0200 | [diff] [blame] | 261 | flags &= ~BDRV_O_RDWR; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 262 | break; |
| 263 | case 'P': |
| 264 | partition = strtol(optarg, &end, 0); |
| 265 | if (*end) |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 266 | errx(EXIT_FAILURE, "Invalid partition `%s'", optarg); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 267 | if (partition < 1 || partition > 8) |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 268 | errx(EXIT_FAILURE, "Invalid partition %d", partition); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 269 | break; |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 270 | case 'k': |
| 271 | socket = optarg; |
| 272 | if (socket[0] != '/') |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 273 | errx(EXIT_FAILURE, "socket path must be absolute\n"); |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 274 | break; |
| 275 | case 'd': |
| 276 | disconnect = true; |
| 277 | break; |
| 278 | case 'c': |
| 279 | device = optarg; |
| 280 | break; |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 281 | case 'e': |
| 282 | shared = strtol(optarg, &end, 0); |
| 283 | if (*end) { |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 284 | errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg); |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 285 | } |
| 286 | if (shared < 1) { |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 287 | errx(EXIT_FAILURE, "Shared device number must be greater than 0\n"); |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 288 | } |
| 289 | break; |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 290 | case 't': |
| 291 | persistent = 1; |
| 292 | break; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 293 | case 'v': |
| 294 | verbose = 1; |
| 295 | break; |
| 296 | case 'V': |
| 297 | version(argv[0]); |
| 298 | exit(0); |
| 299 | break; |
| 300 | case 'h': |
| 301 | usage(argv[0]); |
| 302 | exit(0); |
| 303 | break; |
| 304 | case '?': |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 305 | errx(EXIT_FAILURE, "Try `%s --help' for more information.", |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 306 | argv[0]); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | if ((argc - optind) != 1) { |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 311 | errx(EXIT_FAILURE, "Invalid number of argument.\n" |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 312 | "Try `%s --help' for more information.", |
| 313 | argv[0]); |
| 314 | } |
| 315 | |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 316 | if (disconnect) { |
| 317 | fd = open(argv[optind], O_RDWR); |
| 318 | if (fd == -1) |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 319 | errx(EXIT_FAILURE, "Cannot open %s", argv[optind]); |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 320 | |
| 321 | nbd_disconnect(fd); |
| 322 | |
| 323 | close(fd); |
| 324 | |
| 325 | printf("%s disconnected\n", argv[optind]); |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 330 | bdrv_init(); |
| 331 | |
| 332 | bs = bdrv_new("hda"); |
| 333 | if (bs == NULL) |
| 334 | return 1; |
| 335 | |
Ryota Ozaki | a16c174 | 2010-03-20 15:23:22 +0900 | [diff] [blame] | 336 | if (bdrv_open(bs, argv[optind], flags) < 0) |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 337 | return 1; |
| 338 | |
| 339 | fd_size = bs->total_sectors * 512; |
| 340 | |
| 341 | if (partition != -1 && |
| 342 | find_partition(bs, partition, &dev_offset, &fd_size)) |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 343 | errx(EXIT_FAILURE, "Could not find partition %d", partition); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 344 | |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 345 | if (device) { |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 346 | pid_t pid; |
| 347 | int sock; |
| 348 | |
Anthony Liguori | f5de141 | 2009-06-15 12:49:59 -0500 | [diff] [blame] | 349 | if (!verbose) { |
| 350 | /* detach client and server */ |
| 351 | if (daemon(0, 0) == -1) { |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 352 | errx(EXIT_FAILURE, "Failed to daemonize"); |
Anthony Liguori | f5de141 | 2009-06-15 12:49:59 -0500 | [diff] [blame] | 353 | } |
| 354 | } |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 355 | |
| 356 | if (socket == NULL) { |
Blue Swirl | 22ff51e | 2009-12-23 15:45:30 +0000 | [diff] [blame] | 357 | snprintf(sockpath, sizeof(sockpath), SOCKET_PATH, |
| 358 | basename(device)); |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 359 | socket = sockpath; |
| 360 | } |
| 361 | |
| 362 | pid = fork(); |
| 363 | if (pid < 0) |
| 364 | return 1; |
| 365 | if (pid != 0) { |
| 366 | off_t size; |
| 367 | size_t blocksize; |
| 368 | |
| 369 | ret = 0; |
| 370 | bdrv_close(bs); |
| 371 | |
| 372 | do { |
| 373 | sock = unix_socket_outgoing(socket); |
| 374 | if (sock == -1) { |
| 375 | if (errno != ENOENT && errno != ECONNREFUSED) |
| 376 | goto out; |
| 377 | sleep(1); /* wait children */ |
| 378 | } |
| 379 | } while (sock == -1); |
| 380 | |
| 381 | fd = open(device, O_RDWR); |
| 382 | if (fd == -1) { |
| 383 | ret = 1; |
| 384 | goto out; |
| 385 | } |
| 386 | |
| 387 | ret = nbd_receive_negotiate(sock, &size, &blocksize); |
| 388 | if (ret == -1) { |
| 389 | ret = 1; |
| 390 | goto out; |
| 391 | } |
| 392 | |
| 393 | ret = nbd_init(fd, sock, size, blocksize); |
| 394 | if (ret == -1) { |
| 395 | ret = 1; |
| 396 | goto out; |
| 397 | } |
| 398 | |
| 399 | printf("NBD device %s is now connected to file %s\n", |
| 400 | device, argv[optind]); |
| 401 | |
| 402 | /* update partition table */ |
| 403 | |
| 404 | show_parts(device); |
| 405 | |
| 406 | nbd_client(fd, sock); |
| 407 | close(fd); |
| 408 | out: |
| 409 | kill(pid, SIGTERM); |
| 410 | unlink(socket); |
| 411 | |
| 412 | return ret; |
| 413 | } |
| 414 | /* children */ |
| 415 | } |
| 416 | |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 417 | sharing_fds = qemu_malloc((shared + 1) * sizeof(int)); |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 418 | |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 419 | if (socket) { |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 420 | sharing_fds[0] = unix_socket_incoming(socket); |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 421 | } else { |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 422 | sharing_fds[0] = tcp_socket_incoming(bindto, port); |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 423 | } |
| 424 | |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 425 | if (sharing_fds[0] == -1) |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 426 | return 1; |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 427 | max_fd = sharing_fds[0]; |
| 428 | nb_fds++; |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 429 | |
ths | 2f72648 | 2008-07-03 11:47:46 +0000 | [diff] [blame] | 430 | data = qemu_memalign(512, NBD_BUFFER_SIZE); |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 431 | if (data == NULL) |
Ryota Ozaki | b6353be | 2010-03-20 15:23:23 +0900 | [diff] [blame] | 432 | errx(EXIT_FAILURE, "Cannot allocate data buffer"); |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 433 | |
| 434 | do { |
| 435 | |
| 436 | FD_ZERO(&fds); |
| 437 | for (i = 0; i < nb_fds; i++) |
| 438 | FD_SET(sharing_fds[i], &fds); |
| 439 | |
| 440 | ret = select(max_fd + 1, &fds, NULL, NULL, NULL); |
| 441 | if (ret == -1) |
| 442 | break; |
| 443 | |
| 444 | if (FD_ISSET(sharing_fds[0], &fds)) |
| 445 | ret--; |
| 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]); |
| 451 | nb_fds--; |
| 452 | sharing_fds[i] = sharing_fds[nb_fds]; |
| 453 | i--; |
| 454 | } |
| 455 | ret--; |
| 456 | } |
| 457 | } |
| 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, |
| 463 | &addr_len); |
| 464 | if (sharing_fds[nb_fds] != -1 && |
aliguori | 2798266 | 2008-09-10 15:23:19 +0000 | [diff] [blame] | 465 | nbd_negotiate(sharing_fds[nb_fds], fd_size) != -1) { |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 466 | if (sharing_fds[nb_fds] > max_fd) |
| 467 | max_fd = sharing_fds[nb_fds]; |
| 468 | nb_fds++; |
| 469 | } |
| 470 | } |
| 471 | } |
ths | 7581825 | 2008-07-03 13:41:03 +0000 | [diff] [blame] | 472 | } while (persistent || nb_fds > 1); |
Herve Poussineau | f8a8324 | 2010-01-24 21:23:56 +0000 | [diff] [blame] | 473 | qemu_vfree(data); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 474 | |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 475 | close(sharing_fds[0]); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 476 | bdrv_close(bs); |
ths | 3b05a8e | 2008-07-03 12:45:02 +0000 | [diff] [blame] | 477 | qemu_free(sharing_fds); |
ths | cd831bd | 2008-07-03 10:23:51 +0000 | [diff] [blame] | 478 | if (socket) |
| 479 | unlink(socket); |
bellard | 7a5ca86 | 2008-05-27 21:13:40 +0000 | [diff] [blame] | 480 | |
| 481 | return 0; |
| 482 | } |